[XFS] kill unnessecary ioops indirection
[safe/jmp/linux-2.6] / fs / xfs / xfs_iomap.c
1 /*
2  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_btree.h"
40 #include "xfs_bmap.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
44 #include "xfs_rw.h"
45 #include "xfs_acl.h"
46 #include "xfs_attr.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_utils.h"
50 #include "xfs_iomap.h"
51
52 #if defined(XFS_RW_TRACE)
53 void
54 xfs_iomap_enter_trace(
55         int             tag,
56         xfs_inode_t     *ip,
57         xfs_off_t       offset,
58         ssize_t         count)
59 {
60         xfs_iocore_t    *io = &ip->i_iocore;
61
62         if (!ip->i_rwtrace)
63                 return;
64
65         ktrace_enter(ip->i_rwtrace,
66                 (void *)((unsigned long)tag),
67                 (void *)ip,
68                 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
69                 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
70                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
71                 (void *)((unsigned long)(offset & 0xffffffff)),
72                 (void *)((unsigned long)count),
73                 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
74                 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
75                 (void *)((unsigned long)current_pid()),
76                 (void *)NULL,
77                 (void *)NULL,
78                 (void *)NULL,
79                 (void *)NULL,
80                 (void *)NULL,
81                 (void *)NULL);
82 }
83
84 void
85 xfs_iomap_map_trace(
86         int             tag,
87         xfs_inode_t     *ip,
88         xfs_off_t       offset,
89         ssize_t         count,
90         xfs_iomap_t     *iomapp,
91         xfs_bmbt_irec_t *imapp,
92         int             flags)
93 {
94         if (!ip->i_rwtrace)
95                 return;
96
97         ktrace_enter(ip->i_rwtrace,
98                 (void *)((unsigned long)tag),
99                 (void *)ip,
100                 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
101                 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
102                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
103                 (void *)((unsigned long)(offset & 0xffffffff)),
104                 (void *)((unsigned long)count),
105                 (void *)((unsigned long)flags),
106                 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
107                 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
108                 (void *)((unsigned long)(iomapp->iomap_delta)),
109                 (void *)((unsigned long)(iomapp->iomap_bsize)),
110                 (void *)((unsigned long)(iomapp->iomap_bn)),
111                 (void *)(__psint_t)(imapp->br_startoff),
112                 (void *)((unsigned long)(imapp->br_blockcount)),
113                 (void *)(__psint_t)(imapp->br_startblock));
114 }
115 #else
116 #define xfs_iomap_enter_trace(tag, io, offset, count)
117 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
118 #endif
119
120 #define XFS_WRITEIO_ALIGN(mp,off)       (((off) >> mp->m_writeio_log) \
121                                                 << mp->m_writeio_log)
122 #define XFS_STRAT_WRITE_IMAPS   2
123 #define XFS_WRITE_IMAPS         XFS_BMAP_MAX_NMAP
124
125 STATIC int
126 xfs_imap_to_bmap(
127         xfs_inode_t     *ip,
128         xfs_off_t       offset,
129         xfs_bmbt_irec_t *imap,
130         xfs_iomap_t     *iomapp,
131         int             imaps,                  /* Number of imap entries */
132         int             iomaps,                 /* Number of iomap entries */
133         int             flags)
134 {
135         xfs_mount_t     *mp = ip->i_mount;
136         int             pbm;
137         xfs_fsblock_t   start_block;
138
139
140         for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
141                 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
142                 iomapp->iomap_delta = offset - iomapp->iomap_offset;
143                 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
144                 iomapp->iomap_flags = flags;
145
146                 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
147                         iomapp->iomap_flags |= IOMAP_REALTIME;
148                         iomapp->iomap_target = mp->m_rtdev_targp;
149                 } else {
150                         iomapp->iomap_target = mp->m_ddev_targp;
151                 }
152                 start_block = imap->br_startblock;
153                 if (start_block == HOLESTARTBLOCK) {
154                         iomapp->iomap_bn = IOMAP_DADDR_NULL;
155                         iomapp->iomap_flags |= IOMAP_HOLE;
156                 } else if (start_block == DELAYSTARTBLOCK) {
157                         iomapp->iomap_bn = IOMAP_DADDR_NULL;
158                         iomapp->iomap_flags |= IOMAP_DELAY;
159                 } else {
160                         iomapp->iomap_bn = XFS_FSB_TO_DB(ip, start_block);
161                         if (ISUNWRITTEN(imap))
162                                 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
163                 }
164
165                 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
166         }
167         return pbm;     /* Return the number filled */
168 }
169
170 int
171 xfs_iomap(
172         xfs_inode_t     *ip,
173         xfs_off_t       offset,
174         ssize_t         count,
175         int             flags,
176         xfs_iomap_t     *iomapp,
177         int             *niomaps)
178 {
179         xfs_mount_t     *mp = ip->i_mount;
180         xfs_fileoff_t   offset_fsb, end_fsb;
181         int             error = 0;
182         int             lockmode = 0;
183         xfs_bmbt_irec_t imap;
184         int             nimaps = 1;
185         int             bmapi_flags = 0;
186         int             iomap_flags = 0;
187
188         ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
189         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
190                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
191
192         if (XFS_FORCED_SHUTDOWN(mp))
193                 return XFS_ERROR(EIO);
194
195         switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
196         case BMAPI_READ:
197                 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, ip, offset, count);
198                 lockmode = xfs_ilock_map_shared(ip);
199                 bmapi_flags = XFS_BMAPI_ENTIRE;
200                 break;
201         case BMAPI_WRITE:
202                 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, ip, offset, count);
203                 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
204                 if (flags & BMAPI_IGNSTATE)
205                         bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
206                 xfs_ilock(ip, lockmode);
207                 break;
208         case BMAPI_ALLOCATE:
209                 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, ip, offset, count);
210                 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
211                 bmapi_flags = XFS_BMAPI_ENTIRE;
212
213                 /* Attempt non-blocking lock */
214                 if (flags & BMAPI_TRYLOCK) {
215                         if (!xfs_ilock_nowait(ip, lockmode))
216                                 return XFS_ERROR(EAGAIN);
217                 } else {
218                         xfs_ilock(ip, lockmode);
219                 }
220                 break;
221         default:
222                 BUG();
223         }
224
225         ASSERT(offset <= mp->m_maxioffset);
226         if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
227                 count = mp->m_maxioffset - offset;
228         end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
229         offset_fsb = XFS_B_TO_FSBT(mp, offset);
230
231         error = xfs_bmapi(NULL, ip, offset_fsb,
232                         (xfs_filblks_t)(end_fsb - offset_fsb),
233                         bmapi_flags,  NULL, 0, &imap,
234                         &nimaps, NULL, NULL);
235
236         if (error)
237                 goto out;
238
239         switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
240         case BMAPI_WRITE:
241                 /* If we found an extent, return it */
242                 if (nimaps &&
243                     (imap.br_startblock != HOLESTARTBLOCK) &&
244                     (imap.br_startblock != DELAYSTARTBLOCK)) {
245                         xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
246                                         offset, count, iomapp, &imap, flags);
247                         break;
248                 }
249
250                 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
251                         error = xfs_iomap_write_direct(ip, offset, count, flags,
252                                                        &imap, &nimaps, nimaps);
253                 } else {
254                         error = xfs_iomap_write_delay(ip, offset, count, flags,
255                                                       &imap, &nimaps);
256                 }
257                 if (!error) {
258                         xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, ip,
259                                         offset, count, iomapp, &imap, flags);
260                 }
261                 iomap_flags = IOMAP_NEW;
262                 break;
263         case BMAPI_ALLOCATE:
264                 /* If we found an extent, return it */
265                 xfs_iunlock(ip, lockmode);
266                 lockmode = 0;
267
268                 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
269                         xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
270                                         offset, count, iomapp, &imap, flags);
271                         break;
272                 }
273
274                 error = xfs_iomap_write_allocate(ip, offset, count,
275                                                  &imap, &nimaps);
276                 break;
277         }
278
279         if (nimaps) {
280                 *niomaps = xfs_imap_to_bmap(ip, offset, &imap,
281                                             iomapp, nimaps, *niomaps, iomap_flags);
282         } else if (niomaps) {
283                 *niomaps = 0;
284         }
285
286 out:
287         if (lockmode)
288                 xfs_iunlock(ip, lockmode);
289         return XFS_ERROR(error);
290 }
291
292
293 STATIC int
294 xfs_iomap_eof_align_last_fsb(
295         xfs_mount_t     *mp,
296         xfs_inode_t     *ip,
297         xfs_fsize_t     isize,
298         xfs_extlen_t    extsize,
299         xfs_fileoff_t   *last_fsb)
300 {
301         xfs_fileoff_t   new_last_fsb = 0;
302         xfs_extlen_t    align;
303         int             eof, error;
304
305         if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
306                 ;
307         /*
308          * If mounted with the "-o swalloc" option, roundup the allocation
309          * request to a stripe width boundary if the file size is >=
310          * stripe width and we are allocating past the allocation eof.
311          */
312         else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
313                 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
314                 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
315         /*
316          * Roundup the allocation request to a stripe unit (m_dalign) boundary
317          * if the file size is >= stripe unit size, and we are allocating past
318          * the allocation eof.
319          */
320         else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
321                 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
322
323         /*
324          * Always round up the allocation request to an extent boundary
325          * (when file on a real-time subvolume or has di_extsize hint).
326          */
327         if (extsize) {
328                 if (new_last_fsb)
329                         align = roundup_64(new_last_fsb, extsize);
330                 else
331                         align = extsize;
332                 new_last_fsb = roundup_64(*last_fsb, align);
333         }
334
335         if (new_last_fsb) {
336                 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
337                 if (error)
338                         return error;
339                 if (eof)
340                         *last_fsb = new_last_fsb;
341         }
342         return 0;
343 }
344
345 STATIC int
346 xfs_flush_space(
347         xfs_inode_t     *ip,
348         int             *fsynced,
349         int             *ioflags)
350 {
351         switch (*fsynced) {
352         case 0:
353                 if (ip->i_delayed_blks) {
354                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
355                         xfs_flush_inode(ip);
356                         xfs_ilock(ip, XFS_ILOCK_EXCL);
357                         *fsynced = 1;
358                 } else {
359                         *ioflags |= BMAPI_SYNC;
360                         *fsynced = 2;
361                 }
362                 return 0;
363         case 1:
364                 *fsynced = 2;
365                 *ioflags |= BMAPI_SYNC;
366                 return 0;
367         case 2:
368                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
369                 xfs_flush_device(ip);
370                 xfs_ilock(ip, XFS_ILOCK_EXCL);
371                 *fsynced = 3;
372                 return 0;
373         }
374         return 1;
375 }
376
377 STATIC int
378 xfs_cmn_err_fsblock_zero(
379         xfs_inode_t     *ip,
380         xfs_bmbt_irec_t *imap)
381 {
382         xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
383                         "Access to block zero in inode %llu "
384                         "start_block: %llx start_off: %llx "
385                         "blkcnt: %llx extent-state: %x\n",
386                 (unsigned long long)ip->i_ino,
387                 (unsigned long long)imap->br_startblock,
388                 (unsigned long long)imap->br_startoff,
389                 (unsigned long long)imap->br_blockcount,
390                 imap->br_state);
391         return EFSCORRUPTED;
392 }
393
394 int
395 xfs_iomap_write_direct(
396         xfs_inode_t     *ip,
397         xfs_off_t       offset,
398         size_t          count,
399         int             flags,
400         xfs_bmbt_irec_t *ret_imap,
401         int             *nmaps,
402         int             found)
403 {
404         xfs_mount_t     *mp = ip->i_mount;
405         xfs_iocore_t    *io = &ip->i_iocore;
406         xfs_fileoff_t   offset_fsb;
407         xfs_fileoff_t   last_fsb;
408         xfs_filblks_t   count_fsb, resaligned;
409         xfs_fsblock_t   firstfsb;
410         xfs_extlen_t    extsz, temp;
411         xfs_fsize_t     isize;
412         int             nimaps;
413         int             bmapi_flag;
414         int             quota_flag;
415         int             rt;
416         xfs_trans_t     *tp;
417         xfs_bmbt_irec_t imap;
418         xfs_bmap_free_t free_list;
419         uint            qblocks, resblks, resrtextents;
420         int             committed;
421         int             error;
422
423         /*
424          * Make sure that the dquots are there. This doesn't hold
425          * the ilock across a disk read.
426          */
427         error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
428         if (error)
429                 return XFS_ERROR(error);
430
431         rt = XFS_IS_REALTIME_INODE(ip);
432         extsz = xfs_get_extsz_hint(ip);
433
434         isize = ip->i_size;
435         if (io->io_new_size > isize)
436                 isize = io->io_new_size;
437
438         offset_fsb = XFS_B_TO_FSBT(mp, offset);
439         last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
440         if ((offset + count) > isize) {
441                 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
442                                                         &last_fsb);
443                 if (error)
444                         goto error_out;
445         } else {
446                 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
447                         last_fsb = MIN(last_fsb, (xfs_fileoff_t)
448                                         ret_imap->br_blockcount +
449                                         ret_imap->br_startoff);
450         }
451         count_fsb = last_fsb - offset_fsb;
452         ASSERT(count_fsb > 0);
453
454         resaligned = count_fsb;
455         if (unlikely(extsz)) {
456                 if ((temp = do_mod(offset_fsb, extsz)))
457                         resaligned += temp;
458                 if ((temp = do_mod(resaligned, extsz)))
459                         resaligned += extsz - temp;
460         }
461
462         if (unlikely(rt)) {
463                 resrtextents = qblocks = resaligned;
464                 resrtextents /= mp->m_sb.sb_rextsize;
465                 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
466                 quota_flag = XFS_QMOPT_RES_RTBLKS;
467         } else {
468                 resrtextents = 0;
469                 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
470                 quota_flag = XFS_QMOPT_RES_REGBLKS;
471         }
472
473         /*
474          * Allocate and setup the transaction
475          */
476         xfs_iunlock(ip, XFS_ILOCK_EXCL);
477         tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
478         error = xfs_trans_reserve(tp, resblks,
479                         XFS_WRITE_LOG_RES(mp), resrtextents,
480                         XFS_TRANS_PERM_LOG_RES,
481                         XFS_WRITE_LOG_COUNT);
482         /*
483          * Check for running out of space, note: need lock to return
484          */
485         if (error)
486                 xfs_trans_cancel(tp, 0);
487         xfs_ilock(ip, XFS_ILOCK_EXCL);
488         if (error)
489                 goto error_out;
490
491         error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
492                                               qblocks, 0, quota_flag);
493         if (error)
494                 goto error1;
495
496         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
497         xfs_trans_ihold(tp, ip);
498
499         bmapi_flag = XFS_BMAPI_WRITE;
500         if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
501                 bmapi_flag |= XFS_BMAPI_PREALLOC;
502
503         /*
504          * Issue the xfs_bmapi() call to allocate the blocks
505          */
506         XFS_BMAP_INIT(&free_list, &firstfsb);
507         nimaps = 1;
508         error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
509                 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
510         if (error)
511                 goto error0;
512
513         /*
514          * Complete the transaction
515          */
516         error = xfs_bmap_finish(&tp, &free_list, &committed);
517         if (error)
518                 goto error0;
519         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
520         if (error)
521                 goto error_out;
522
523         /*
524          * Copy any maps to caller's array and return any error.
525          */
526         if (nimaps == 0) {
527                 error = ENOSPC;
528                 goto error_out;
529         }
530
531         if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
532                 error = xfs_cmn_err_fsblock_zero(ip, &imap);
533                 goto error_out;
534         }
535
536         *ret_imap = imap;
537         *nmaps = 1;
538         return 0;
539
540 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
541         xfs_bmap_cancel(&free_list);
542         XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
543
544 error1: /* Just cancel transaction */
545         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
546         *nmaps = 0;     /* nothing set-up here */
547
548 error_out:
549         return XFS_ERROR(error);
550 }
551
552 /*
553  * If the caller is doing a write at the end of the file,
554  * then extend the allocation out to the file system's write
555  * iosize.  We clean up any extra space left over when the
556  * file is closed in xfs_inactive().
557  *
558  * For sync writes, we are flushing delayed allocate space to
559  * try to make additional space available for allocation near
560  * the filesystem full boundary - preallocation hurts in that
561  * situation, of course.
562  */
563 STATIC int
564 xfs_iomap_eof_want_preallocate(
565         xfs_mount_t     *mp,
566         xfs_inode_t     *ip,
567         xfs_fsize_t     isize,
568         xfs_off_t       offset,
569         size_t          count,
570         int             ioflag,
571         xfs_bmbt_irec_t *imap,
572         int             nimaps,
573         int             *prealloc)
574 {
575         xfs_fileoff_t   start_fsb;
576         xfs_filblks_t   count_fsb;
577         xfs_fsblock_t   firstblock;
578         int             n, error, imaps;
579
580         *prealloc = 0;
581         if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
582                 return 0;
583
584         /*
585          * If there are any real blocks past eof, then don't
586          * do any speculative allocation.
587          */
588         start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
589         count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
590         while (count_fsb > 0) {
591                 imaps = nimaps;
592                 firstblock = NULLFSBLOCK;
593                 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
594                                   &firstblock, 0, imap, &imaps, NULL, NULL);
595                 if (error)
596                         return error;
597                 for (n = 0; n < imaps; n++) {
598                         if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
599                             (imap[n].br_startblock != DELAYSTARTBLOCK))
600                                 return 0;
601                         start_fsb += imap[n].br_blockcount;
602                         count_fsb -= imap[n].br_blockcount;
603                 }
604         }
605         *prealloc = 1;
606         return 0;
607 }
608
609 int
610 xfs_iomap_write_delay(
611         xfs_inode_t     *ip,
612         xfs_off_t       offset,
613         size_t          count,
614         int             ioflag,
615         xfs_bmbt_irec_t *ret_imap,
616         int             *nmaps)
617 {
618         xfs_mount_t     *mp = ip->i_mount;
619         xfs_iocore_t    *io = &ip->i_iocore;
620         xfs_fileoff_t   offset_fsb;
621         xfs_fileoff_t   last_fsb;
622         xfs_off_t       aligned_offset;
623         xfs_fileoff_t   ioalign;
624         xfs_fsblock_t   firstblock;
625         xfs_extlen_t    extsz;
626         xfs_fsize_t     isize;
627         int             nimaps;
628         xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
629         int             prealloc, fsynced = 0;
630         int             error;
631
632         ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
633
634         /*
635          * Make sure that the dquots are there. This doesn't hold
636          * the ilock across a disk read.
637          */
638         error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
639         if (error)
640                 return XFS_ERROR(error);
641
642         extsz = xfs_get_extsz_hint(ip);
643         offset_fsb = XFS_B_TO_FSBT(mp, offset);
644
645 retry:
646         isize = ip->i_size;
647         if (io->io_new_size > isize)
648                 isize = io->io_new_size;
649
650         error = xfs_iomap_eof_want_preallocate(mp, ip, isize, offset, count,
651                                 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
652         if (error)
653                 return error;
654
655         if (prealloc) {
656                 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
657                 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
658                 last_fsb = ioalign + mp->m_writeio_blocks;
659         } else {
660                 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
661         }
662
663         if (prealloc || extsz) {
664                 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
665                                                         &last_fsb);
666                 if (error)
667                         return error;
668         }
669
670         nimaps = XFS_WRITE_IMAPS;
671         firstblock = NULLFSBLOCK;
672         error = xfs_bmapi(NULL, ip, offset_fsb,
673                           (xfs_filblks_t)(last_fsb - offset_fsb),
674                           XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
675                           XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
676                           &nimaps, NULL, NULL);
677         if (error && (error != ENOSPC))
678                 return XFS_ERROR(error);
679
680         /*
681          * If bmapi returned us nothing, and if we didn't get back EDQUOT,
682          * then we must have run out of space - flush delalloc, and retry..
683          */
684         if (nimaps == 0) {
685                 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
686                                         ip, offset, count);
687                 if (xfs_flush_space(ip, &fsynced, &ioflag))
688                         return XFS_ERROR(ENOSPC);
689
690                 error = 0;
691                 goto retry;
692         }
693
694         if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
695                 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
696
697         *ret_imap = imap[0];
698         *nmaps = 1;
699
700         return 0;
701 }
702
703 /*
704  * Pass in a delayed allocate extent, convert it to real extents;
705  * return to the caller the extent we create which maps on top of
706  * the originating callers request.
707  *
708  * Called without a lock on the inode.
709  */
710 int
711 xfs_iomap_write_allocate(
712         xfs_inode_t     *ip,
713         xfs_off_t       offset,
714         size_t          count,
715         xfs_bmbt_irec_t *map,
716         int             *retmap)
717 {
718         xfs_mount_t     *mp = ip->i_mount;
719         xfs_iocore_t    *io = &ip->i_iocore;
720         xfs_fileoff_t   offset_fsb, last_block;
721         xfs_fileoff_t   end_fsb, map_start_fsb;
722         xfs_fsblock_t   first_block;
723         xfs_bmap_free_t free_list;
724         xfs_filblks_t   count_fsb;
725         xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
726         xfs_trans_t     *tp;
727         int             i, nimaps, committed;
728         int             error = 0;
729         int             nres;
730
731         *retmap = 0;
732
733         /*
734          * Make sure that the dquots are there.
735          */
736         if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
737                 return XFS_ERROR(error);
738
739         offset_fsb = XFS_B_TO_FSBT(mp, offset);
740         count_fsb = map->br_blockcount;
741         map_start_fsb = map->br_startoff;
742
743         XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
744
745         while (count_fsb != 0) {
746                 /*
747                  * Set up a transaction with which to allocate the
748                  * backing store for the file.  Do allocations in a
749                  * loop until we get some space in the range we are
750                  * interested in.  The other space that might be allocated
751                  * is in the delayed allocation extent on which we sit
752                  * but before our buffer starts.
753                  */
754
755                 nimaps = 0;
756                 while (nimaps == 0) {
757                         tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
758                         tp->t_flags |= XFS_TRANS_RESERVE;
759                         nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
760                         error = xfs_trans_reserve(tp, nres,
761                                         XFS_WRITE_LOG_RES(mp),
762                                         0, XFS_TRANS_PERM_LOG_RES,
763                                         XFS_WRITE_LOG_COUNT);
764                         if (error) {
765                                 xfs_trans_cancel(tp, 0);
766                                 return XFS_ERROR(error);
767                         }
768                         xfs_ilock(ip, XFS_ILOCK_EXCL);
769                         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
770                         xfs_trans_ihold(tp, ip);
771
772                         XFS_BMAP_INIT(&free_list, &first_block);
773
774                         nimaps = XFS_STRAT_WRITE_IMAPS;
775                         /*
776                          * Ensure we don't go beyond eof - it is possible
777                          * the extents changed since we did the read call,
778                          * we dropped the ilock in the interim.
779                          */
780
781                         end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
782                         xfs_bmap_last_offset(NULL, ip, &last_block,
783                                 XFS_DATA_FORK);
784                         last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
785                         if ((map_start_fsb + count_fsb) > last_block) {
786                                 count_fsb = last_block - map_start_fsb;
787                                 if (count_fsb == 0) {
788                                         error = EAGAIN;
789                                         goto trans_cancel;
790                                 }
791                         }
792
793                         /* Go get the actual blocks */
794                         error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
795                                         XFS_BMAPI_WRITE, &first_block, 1,
796                                         imap, &nimaps, &free_list, NULL);
797                         if (error)
798                                 goto trans_cancel;
799
800                         error = xfs_bmap_finish(&tp, &free_list, &committed);
801                         if (error)
802                                 goto trans_cancel;
803
804                         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
805                         if (error)
806                                 goto error0;
807
808                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
809                 }
810
811                 /*
812                  * See if we were able to allocate an extent that
813                  * covers at least part of the callers request
814                  */
815                 for (i = 0; i < nimaps; i++) {
816                         if (unlikely(!imap[i].br_startblock &&
817                                      !(io->io_flags & XFS_IOCORE_RT)))
818                                 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
819                         if ((offset_fsb >= imap[i].br_startoff) &&
820                             (offset_fsb < (imap[i].br_startoff +
821                                            imap[i].br_blockcount))) {
822                                 *map = imap[i];
823                                 *retmap = 1;
824                                 XFS_STATS_INC(xs_xstrat_quick);
825                                 return 0;
826                         }
827                         count_fsb -= imap[i].br_blockcount;
828                 }
829
830                 /* So far we have not mapped the requested part of the
831                  * file, just surrounding data, try again.
832                  */
833                 nimaps--;
834                 map_start_fsb = imap[nimaps].br_startoff +
835                                 imap[nimaps].br_blockcount;
836         }
837
838 trans_cancel:
839         xfs_bmap_cancel(&free_list);
840         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
841 error0:
842         xfs_iunlock(ip, XFS_ILOCK_EXCL);
843         return XFS_ERROR(error);
844 }
845
846 int
847 xfs_iomap_write_unwritten(
848         xfs_inode_t     *ip,
849         xfs_off_t       offset,
850         size_t          count)
851 {
852         xfs_mount_t     *mp = ip->i_mount;
853         xfs_iocore_t    *io = &ip->i_iocore;
854         xfs_fileoff_t   offset_fsb;
855         xfs_filblks_t   count_fsb;
856         xfs_filblks_t   numblks_fsb;
857         xfs_fsblock_t   firstfsb;
858         int             nimaps;
859         xfs_trans_t     *tp;
860         xfs_bmbt_irec_t imap;
861         xfs_bmap_free_t free_list;
862         uint            resblks;
863         int             committed;
864         int             error;
865
866         xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, ip, offset, count);
867
868         offset_fsb = XFS_B_TO_FSBT(mp, offset);
869         count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
870         count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
871
872         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
873
874         do {
875                 /*
876                  * set up a transaction to convert the range of extents
877                  * from unwritten to real. Do allocations in a loop until
878                  * we have covered the range passed in.
879                  */
880                 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
881                 tp->t_flags |= XFS_TRANS_RESERVE;
882                 error = xfs_trans_reserve(tp, resblks,
883                                 XFS_WRITE_LOG_RES(mp), 0,
884                                 XFS_TRANS_PERM_LOG_RES,
885                                 XFS_WRITE_LOG_COUNT);
886                 if (error) {
887                         xfs_trans_cancel(tp, 0);
888                         return XFS_ERROR(error);
889                 }
890
891                 xfs_ilock(ip, XFS_ILOCK_EXCL);
892                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
893                 xfs_trans_ihold(tp, ip);
894
895                 /*
896                  * Modify the unwritten extent state of the buffer.
897                  */
898                 XFS_BMAP_INIT(&free_list, &firstfsb);
899                 nimaps = 1;
900                 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
901                                   XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
902                                   1, &imap, &nimaps, &free_list, NULL);
903                 if (error)
904                         goto error_on_bmapi_transaction;
905
906                 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
907                 if (error)
908                         goto error_on_bmapi_transaction;
909
910                 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
911                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
912                 if (error)
913                         return XFS_ERROR(error);
914
915                 if (unlikely(!imap.br_startblock &&
916                              !(io->io_flags & XFS_IOCORE_RT)))
917                         return xfs_cmn_err_fsblock_zero(ip, &imap);
918
919                 if ((numblks_fsb = imap.br_blockcount) == 0) {
920                         /*
921                          * The numblks_fsb value should always get
922                          * smaller, otherwise the loop is stuck.
923                          */
924                         ASSERT(imap.br_blockcount);
925                         break;
926                 }
927                 offset_fsb += numblks_fsb;
928                 count_fsb -= numblks_fsb;
929         } while (count_fsb > 0);
930
931         return 0;
932
933 error_on_bmapi_transaction:
934         xfs_bmap_cancel(&free_list);
935         xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
936         xfs_iunlock(ip, XFS_ILOCK_EXCL);
937         return XFS_ERROR(error);
938 }