xfs: mark xfs_iomap_write_ helpers static
[linux-2.6.git] / 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_attr.h"
46 #include "xfs_buf_item.h"
47 #include "xfs_trans_space.h"
48 #include "xfs_utils.h"
49 #include "xfs_iomap.h"
50 #include "xfs_trace.h"
51
52
53 #define XFS_WRITEIO_ALIGN(mp,off)       (((off) >> mp->m_writeio_log) \
54                                                 << mp->m_writeio_log)
55 #define XFS_STRAT_WRITE_IMAPS   2
56 #define XFS_WRITE_IMAPS         XFS_BMAP_MAX_NMAP
57
58 STATIC int xfs_iomap_write_direct(struct xfs_inode *, xfs_off_t, size_t,
59                                   int, struct xfs_bmbt_irec *, int *);
60 STATIC int xfs_iomap_write_delay(struct xfs_inode *, xfs_off_t, size_t, int,
61                                  struct xfs_bmbt_irec *, int *);
62 STATIC int xfs_iomap_write_allocate(struct xfs_inode *, xfs_off_t, size_t,
63                                 struct xfs_bmbt_irec *, int *);
64
65 int
66 xfs_iomap(
67         struct xfs_inode        *ip,
68         xfs_off_t               offset,
69         ssize_t                 count,
70         int                     flags,
71         struct xfs_bmbt_irec    *imap,
72         int                     *nimaps,
73         int                     *new)
74 {
75         struct xfs_mount        *mp = ip->i_mount;
76         xfs_fileoff_t           offset_fsb, end_fsb;
77         int                     error = 0;
78         int                     lockmode = 0;
79         int                     bmapi_flags = 0;
80
81         ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
82
83         *new = 0;
84
85         if (XFS_FORCED_SHUTDOWN(mp))
86                 return XFS_ERROR(EIO);
87
88         trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
89
90         switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
91         case BMAPI_READ:
92                 lockmode = xfs_ilock_map_shared(ip);
93                 bmapi_flags = XFS_BMAPI_ENTIRE;
94                 break;
95         case BMAPI_WRITE:
96                 lockmode = XFS_ILOCK_EXCL;
97                 if (flags & BMAPI_IGNSTATE)
98                         bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
99                 xfs_ilock(ip, lockmode);
100                 break;
101         case BMAPI_ALLOCATE:
102                 lockmode = XFS_ILOCK_SHARED;
103                 bmapi_flags = XFS_BMAPI_ENTIRE;
104
105                 /* Attempt non-blocking lock */
106                 if (flags & BMAPI_TRYLOCK) {
107                         if (!xfs_ilock_nowait(ip, lockmode))
108                                 return XFS_ERROR(EAGAIN);
109                 } else {
110                         xfs_ilock(ip, lockmode);
111                 }
112                 break;
113         default:
114                 BUG();
115         }
116
117         ASSERT(offset <= mp->m_maxioffset);
118         if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
119                 count = mp->m_maxioffset - offset;
120         end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
121         offset_fsb = XFS_B_TO_FSBT(mp, offset);
122
123         error = xfs_bmapi(NULL, ip, offset_fsb,
124                         (xfs_filblks_t)(end_fsb - offset_fsb),
125                         bmapi_flags,  NULL, 0, imap,
126                         nimaps, NULL, NULL);
127
128         if (error)
129                 goto out;
130
131         switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
132         case BMAPI_WRITE:
133                 /* If we found an extent, return it */
134                 if (*nimaps &&
135                     (imap->br_startblock != HOLESTARTBLOCK) &&
136                     (imap->br_startblock != DELAYSTARTBLOCK)) {
137                         trace_xfs_iomap_found(ip, offset, count, flags, imap);
138                         break;
139                 }
140
141                 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
142                         error = xfs_iomap_write_direct(ip, offset, count, flags,
143                                                        imap, nimaps);
144                 } else {
145                         error = xfs_iomap_write_delay(ip, offset, count, flags,
146                                                       imap, nimaps);
147                 }
148                 if (!error) {
149                         trace_xfs_iomap_alloc(ip, offset, count, flags, imap);
150                 }
151                 *new = 1;
152                 break;
153         case BMAPI_ALLOCATE:
154                 /* If we found an extent, return it */
155                 xfs_iunlock(ip, lockmode);
156                 lockmode = 0;
157
158                 if (*nimaps && !isnullstartblock(imap->br_startblock)) {
159                         trace_xfs_iomap_found(ip, offset, count, flags, imap);
160                         break;
161                 }
162
163                 error = xfs_iomap_write_allocate(ip, offset, count,
164                                                  imap, nimaps);
165                 break;
166         }
167
168         ASSERT(*nimaps <= 1);
169
170 out:
171         if (lockmode)
172                 xfs_iunlock(ip, lockmode);
173         return XFS_ERROR(error);
174 }
175
176 STATIC int
177 xfs_iomap_eof_align_last_fsb(
178         xfs_mount_t     *mp,
179         xfs_inode_t     *ip,
180         xfs_extlen_t    extsize,
181         xfs_fileoff_t   *last_fsb)
182 {
183         xfs_fileoff_t   new_last_fsb = 0;
184         xfs_extlen_t    align;
185         int             eof, error;
186
187         if (XFS_IS_REALTIME_INODE(ip))
188                 ;
189         /*
190          * If mounted with the "-o swalloc" option, roundup the allocation
191          * request to a stripe width boundary if the file size is >=
192          * stripe width and we are allocating past the allocation eof.
193          */
194         else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
195                 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
196                 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
197         /*
198          * Roundup the allocation request to a stripe unit (m_dalign) boundary
199          * if the file size is >= stripe unit size, and we are allocating past
200          * the allocation eof.
201          */
202         else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
203                 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
204
205         /*
206          * Always round up the allocation request to an extent boundary
207          * (when file on a real-time subvolume or has di_extsize hint).
208          */
209         if (extsize) {
210                 if (new_last_fsb)
211                         align = roundup_64(new_last_fsb, extsize);
212                 else
213                         align = extsize;
214                 new_last_fsb = roundup_64(*last_fsb, align);
215         }
216
217         if (new_last_fsb) {
218                 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
219                 if (error)
220                         return error;
221                 if (eof)
222                         *last_fsb = new_last_fsb;
223         }
224         return 0;
225 }
226
227 STATIC int
228 xfs_cmn_err_fsblock_zero(
229         xfs_inode_t     *ip,
230         xfs_bmbt_irec_t *imap)
231 {
232         xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
233                         "Access to block zero in inode %llu "
234                         "start_block: %llx start_off: %llx "
235                         "blkcnt: %llx extent-state: %x\n",
236                 (unsigned long long)ip->i_ino,
237                 (unsigned long long)imap->br_startblock,
238                 (unsigned long long)imap->br_startoff,
239                 (unsigned long long)imap->br_blockcount,
240                 imap->br_state);
241         return EFSCORRUPTED;
242 }
243
244 STATIC int
245 xfs_iomap_write_direct(
246         xfs_inode_t     *ip,
247         xfs_off_t       offset,
248         size_t          count,
249         int             flags,
250         xfs_bmbt_irec_t *ret_imap,
251         int             *nmaps)
252 {
253         xfs_mount_t     *mp = ip->i_mount;
254         xfs_fileoff_t   offset_fsb;
255         xfs_fileoff_t   last_fsb;
256         xfs_filblks_t   count_fsb, resaligned;
257         xfs_fsblock_t   firstfsb;
258         xfs_extlen_t    extsz, temp;
259         int             nimaps;
260         int             bmapi_flag;
261         int             quota_flag;
262         int             rt;
263         xfs_trans_t     *tp;
264         xfs_bmbt_irec_t imap;
265         xfs_bmap_free_t free_list;
266         uint            qblocks, resblks, resrtextents;
267         int             committed;
268         int             error;
269
270         /*
271          * Make sure that the dquots are there. This doesn't hold
272          * the ilock across a disk read.
273          */
274         error = xfs_qm_dqattach_locked(ip, 0);
275         if (error)
276                 return XFS_ERROR(error);
277
278         rt = XFS_IS_REALTIME_INODE(ip);
279         extsz = xfs_get_extsz_hint(ip);
280
281         offset_fsb = XFS_B_TO_FSBT(mp, offset);
282         last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
283         if ((offset + count) > ip->i_size) {
284                 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
285                 if (error)
286                         goto error_out;
287         } else {
288                 if (*nmaps && (ret_imap->br_startblock == HOLESTARTBLOCK))
289                         last_fsb = MIN(last_fsb, (xfs_fileoff_t)
290                                         ret_imap->br_blockcount +
291                                         ret_imap->br_startoff);
292         }
293         count_fsb = last_fsb - offset_fsb;
294         ASSERT(count_fsb > 0);
295
296         resaligned = count_fsb;
297         if (unlikely(extsz)) {
298                 if ((temp = do_mod(offset_fsb, extsz)))
299                         resaligned += temp;
300                 if ((temp = do_mod(resaligned, extsz)))
301                         resaligned += extsz - temp;
302         }
303
304         if (unlikely(rt)) {
305                 resrtextents = qblocks = resaligned;
306                 resrtextents /= mp->m_sb.sb_rextsize;
307                 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
308                 quota_flag = XFS_QMOPT_RES_RTBLKS;
309         } else {
310                 resrtextents = 0;
311                 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
312                 quota_flag = XFS_QMOPT_RES_REGBLKS;
313         }
314
315         /*
316          * Allocate and setup the transaction
317          */
318         xfs_iunlock(ip, XFS_ILOCK_EXCL);
319         tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
320         error = xfs_trans_reserve(tp, resblks,
321                         XFS_WRITE_LOG_RES(mp), resrtextents,
322                         XFS_TRANS_PERM_LOG_RES,
323                         XFS_WRITE_LOG_COUNT);
324         /*
325          * Check for running out of space, note: need lock to return
326          */
327         if (error)
328                 xfs_trans_cancel(tp, 0);
329         xfs_ilock(ip, XFS_ILOCK_EXCL);
330         if (error)
331                 goto error_out;
332
333         error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
334         if (error)
335                 goto error1;
336
337         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
338         xfs_trans_ihold(tp, ip);
339
340         bmapi_flag = XFS_BMAPI_WRITE;
341         if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
342                 bmapi_flag |= XFS_BMAPI_PREALLOC;
343
344         /*
345          * Issue the xfs_bmapi() call to allocate the blocks
346          */
347         xfs_bmap_init(&free_list, &firstfsb);
348         nimaps = 1;
349         error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
350                 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
351         if (error)
352                 goto error0;
353
354         /*
355          * Complete the transaction
356          */
357         error = xfs_bmap_finish(&tp, &free_list, &committed);
358         if (error)
359                 goto error0;
360         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
361         if (error)
362                 goto error_out;
363
364         /*
365          * Copy any maps to caller's array and return any error.
366          */
367         if (nimaps == 0) {
368                 error = ENOSPC;
369                 goto error_out;
370         }
371
372         if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
373                 error = xfs_cmn_err_fsblock_zero(ip, &imap);
374                 goto error_out;
375         }
376
377         *ret_imap = imap;
378         *nmaps = 1;
379         return 0;
380
381 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
382         xfs_bmap_cancel(&free_list);
383         xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
384
385 error1: /* Just cancel transaction */
386         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
387         *nmaps = 0;     /* nothing set-up here */
388
389 error_out:
390         return XFS_ERROR(error);
391 }
392
393 /*
394  * If the caller is doing a write at the end of the file, then extend the
395  * allocation out to the file system's write iosize.  We clean up any extra
396  * space left over when the file is closed in xfs_inactive().
397  */
398 STATIC int
399 xfs_iomap_eof_want_preallocate(
400         xfs_mount_t     *mp,
401         xfs_inode_t     *ip,
402         xfs_off_t       offset,
403         size_t          count,
404         int             ioflag,
405         xfs_bmbt_irec_t *imap,
406         int             nimaps,
407         int             *prealloc)
408 {
409         xfs_fileoff_t   start_fsb;
410         xfs_filblks_t   count_fsb;
411         xfs_fsblock_t   firstblock;
412         int             n, error, imaps;
413
414         *prealloc = 0;
415         if ((offset + count) <= ip->i_size)
416                 return 0;
417
418         /*
419          * If there are any real blocks past eof, then don't
420          * do any speculative allocation.
421          */
422         start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
423         count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
424         while (count_fsb > 0) {
425                 imaps = nimaps;
426                 firstblock = NULLFSBLOCK;
427                 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
428                                   &firstblock, 0, imap, &imaps, NULL, NULL);
429                 if (error)
430                         return error;
431                 for (n = 0; n < imaps; n++) {
432                         if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
433                             (imap[n].br_startblock != DELAYSTARTBLOCK))
434                                 return 0;
435                         start_fsb += imap[n].br_blockcount;
436                         count_fsb -= imap[n].br_blockcount;
437                 }
438         }
439         *prealloc = 1;
440         return 0;
441 }
442
443 STATIC int
444 xfs_iomap_write_delay(
445         xfs_inode_t     *ip,
446         xfs_off_t       offset,
447         size_t          count,
448         int             ioflag,
449         xfs_bmbt_irec_t *ret_imap,
450         int             *nmaps)
451 {
452         xfs_mount_t     *mp = ip->i_mount;
453         xfs_fileoff_t   offset_fsb;
454         xfs_fileoff_t   last_fsb;
455         xfs_off_t       aligned_offset;
456         xfs_fileoff_t   ioalign;
457         xfs_fsblock_t   firstblock;
458         xfs_extlen_t    extsz;
459         int             nimaps;
460         xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
461         int             prealloc, flushed = 0;
462         int             error;
463
464         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
465
466         /*
467          * Make sure that the dquots are there. This doesn't hold
468          * the ilock across a disk read.
469          */
470         error = xfs_qm_dqattach_locked(ip, 0);
471         if (error)
472                 return XFS_ERROR(error);
473
474         extsz = xfs_get_extsz_hint(ip);
475         offset_fsb = XFS_B_TO_FSBT(mp, offset);
476
477         error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
478                                 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
479         if (error)
480                 return error;
481
482 retry:
483         if (prealloc) {
484                 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
485                 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
486                 last_fsb = ioalign + mp->m_writeio_blocks;
487         } else {
488                 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
489         }
490
491         if (prealloc || extsz) {
492                 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
493                 if (error)
494                         return error;
495         }
496
497         nimaps = XFS_WRITE_IMAPS;
498         firstblock = NULLFSBLOCK;
499         error = xfs_bmapi(NULL, ip, offset_fsb,
500                           (xfs_filblks_t)(last_fsb - offset_fsb),
501                           XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
502                           XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
503                           &nimaps, NULL, NULL);
504         if (error && (error != ENOSPC))
505                 return XFS_ERROR(error);
506
507         /*
508          * If bmapi returned us nothing, and if we didn't get back EDQUOT,
509          * then we must have run out of space - flush all other inodes with
510          * delalloc blocks and retry without EOF preallocation.
511          */
512         if (nimaps == 0) {
513                 trace_xfs_delalloc_enospc(ip, offset, count);
514                 if (flushed)
515                         return XFS_ERROR(ENOSPC);
516
517                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
518                 xfs_flush_inodes(ip);
519                 xfs_ilock(ip, XFS_ILOCK_EXCL);
520
521                 flushed = 1;
522                 error = 0;
523                 prealloc = 0;
524                 goto retry;
525         }
526
527         if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
528                 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
529
530         *ret_imap = imap[0];
531         *nmaps = 1;
532
533         return 0;
534 }
535
536 /*
537  * Pass in a delayed allocate extent, convert it to real extents;
538  * return to the caller the extent we create which maps on top of
539  * the originating callers request.
540  *
541  * Called without a lock on the inode.
542  *
543  * We no longer bother to look at the incoming map - all we have to
544  * guarantee is that whatever we allocate fills the required range.
545  */
546 STATIC int
547 xfs_iomap_write_allocate(
548         xfs_inode_t     *ip,
549         xfs_off_t       offset,
550         size_t          count,
551         xfs_bmbt_irec_t *map,
552         int             *retmap)
553 {
554         xfs_mount_t     *mp = ip->i_mount;
555         xfs_fileoff_t   offset_fsb, last_block;
556         xfs_fileoff_t   end_fsb, map_start_fsb;
557         xfs_fsblock_t   first_block;
558         xfs_bmap_free_t free_list;
559         xfs_filblks_t   count_fsb;
560         xfs_bmbt_irec_t imap;
561         xfs_trans_t     *tp;
562         int             nimaps, committed;
563         int             error = 0;
564         int             nres;
565
566         *retmap = 0;
567
568         /*
569          * Make sure that the dquots are there.
570          */
571         error = xfs_qm_dqattach(ip, 0);
572         if (error)
573                 return XFS_ERROR(error);
574
575         offset_fsb = XFS_B_TO_FSBT(mp, offset);
576         count_fsb = map->br_blockcount;
577         map_start_fsb = map->br_startoff;
578
579         XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
580
581         while (count_fsb != 0) {
582                 /*
583                  * Set up a transaction with which to allocate the
584                  * backing store for the file.  Do allocations in a
585                  * loop until we get some space in the range we are
586                  * interested in.  The other space that might be allocated
587                  * is in the delayed allocation extent on which we sit
588                  * but before our buffer starts.
589                  */
590
591                 nimaps = 0;
592                 while (nimaps == 0) {
593                         tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
594                         tp->t_flags |= XFS_TRANS_RESERVE;
595                         nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
596                         error = xfs_trans_reserve(tp, nres,
597                                         XFS_WRITE_LOG_RES(mp),
598                                         0, XFS_TRANS_PERM_LOG_RES,
599                                         XFS_WRITE_LOG_COUNT);
600                         if (error) {
601                                 xfs_trans_cancel(tp, 0);
602                                 return XFS_ERROR(error);
603                         }
604                         xfs_ilock(ip, XFS_ILOCK_EXCL);
605                         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
606                         xfs_trans_ihold(tp, ip);
607
608                         xfs_bmap_init(&free_list, &first_block);
609
610                         /*
611                          * it is possible that the extents have changed since
612                          * we did the read call as we dropped the ilock for a
613                          * while. We have to be careful about truncates or hole
614                          * punchs here - we are not allowed to allocate
615                          * non-delalloc blocks here.
616                          *
617                          * The only protection against truncation is the pages
618                          * for the range we are being asked to convert are
619                          * locked and hence a truncate will block on them
620                          * first.
621                          *
622                          * As a result, if we go beyond the range we really
623                          * need and hit an delalloc extent boundary followed by
624                          * a hole while we have excess blocks in the map, we
625                          * will fill the hole incorrectly and overrun the
626                          * transaction reservation.
627                          *
628                          * Using a single map prevents this as we are forced to
629                          * check each map we look for overlap with the desired
630                          * range and abort as soon as we find it. Also, given
631                          * that we only return a single map, having one beyond
632                          * what we can return is probably a bit silly.
633                          *
634                          * We also need to check that we don't go beyond EOF;
635                          * this is a truncate optimisation as a truncate sets
636                          * the new file size before block on the pages we
637                          * currently have locked under writeback. Because they
638                          * are about to be tossed, we don't need to write them
639                          * back....
640                          */
641                         nimaps = 1;
642                         end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
643                         error = xfs_bmap_last_offset(NULL, ip, &last_block,
644                                                         XFS_DATA_FORK);
645                         if (error)
646                                 goto trans_cancel;
647
648                         last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
649                         if ((map_start_fsb + count_fsb) > last_block) {
650                                 count_fsb = last_block - map_start_fsb;
651                                 if (count_fsb == 0) {
652                                         error = EAGAIN;
653                                         goto trans_cancel;
654                                 }
655                         }
656
657                         /* Go get the actual blocks */
658                         error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
659                                         XFS_BMAPI_WRITE, &first_block, 1,
660                                         &imap, &nimaps, &free_list, NULL);
661                         if (error)
662                                 goto trans_cancel;
663
664                         error = xfs_bmap_finish(&tp, &free_list, &committed);
665                         if (error)
666                                 goto trans_cancel;
667
668                         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
669                         if (error)
670                                 goto error0;
671
672                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
673                 }
674
675                 /*
676                  * See if we were able to allocate an extent that
677                  * covers at least part of the callers request
678                  */
679                 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
680                         return xfs_cmn_err_fsblock_zero(ip, &imap);
681
682                 if ((offset_fsb >= imap.br_startoff) &&
683                     (offset_fsb < (imap.br_startoff +
684                                    imap.br_blockcount))) {
685                         *map = imap;
686                         *retmap = 1;
687                         XFS_STATS_INC(xs_xstrat_quick);
688                         return 0;
689                 }
690
691                 /*
692                  * So far we have not mapped the requested part of the
693                  * file, just surrounding data, try again.
694                  */
695                 count_fsb -= imap.br_blockcount;
696                 map_start_fsb = imap.br_startoff + imap.br_blockcount;
697         }
698
699 trans_cancel:
700         xfs_bmap_cancel(&free_list);
701         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
702 error0:
703         xfs_iunlock(ip, XFS_ILOCK_EXCL);
704         return XFS_ERROR(error);
705 }
706
707 int
708 xfs_iomap_write_unwritten(
709         xfs_inode_t     *ip,
710         xfs_off_t       offset,
711         size_t          count)
712 {
713         xfs_mount_t     *mp = ip->i_mount;
714         xfs_fileoff_t   offset_fsb;
715         xfs_filblks_t   count_fsb;
716         xfs_filblks_t   numblks_fsb;
717         xfs_fsblock_t   firstfsb;
718         int             nimaps;
719         xfs_trans_t     *tp;
720         xfs_bmbt_irec_t imap;
721         xfs_bmap_free_t free_list;
722         uint            resblks;
723         int             committed;
724         int             error;
725
726         trace_xfs_unwritten_convert(ip, offset, count);
727
728         offset_fsb = XFS_B_TO_FSBT(mp, offset);
729         count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
730         count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
731
732         /*
733          * Reserve enough blocks in this transaction for two complete extent
734          * btree splits.  We may be converting the middle part of an unwritten
735          * extent and in this case we will insert two new extents in the btree
736          * each of which could cause a full split.
737          *
738          * This reservation amount will be used in the first call to
739          * xfs_bmbt_split() to select an AG with enough space to satisfy the
740          * rest of the operation.
741          */
742         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
743
744         do {
745                 /*
746                  * set up a transaction to convert the range of extents
747                  * from unwritten to real. Do allocations in a loop until
748                  * we have covered the range passed in.
749                  *
750                  * Note that we open code the transaction allocation here
751                  * to pass KM_NOFS--we can't risk to recursing back into
752                  * the filesystem here as we might be asked to write out
753                  * the same inode that we complete here and might deadlock
754                  * on the iolock.
755                  */
756                 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
757                 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
758                 tp->t_flags |= XFS_TRANS_RESERVE;
759                 error = xfs_trans_reserve(tp, resblks,
760                                 XFS_WRITE_LOG_RES(mp), 0,
761                                 XFS_TRANS_PERM_LOG_RES,
762                                 XFS_WRITE_LOG_COUNT);
763                 if (error) {
764                         xfs_trans_cancel(tp, 0);
765                         return XFS_ERROR(error);
766                 }
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                 /*
773                  * Modify the unwritten extent state of the buffer.
774                  */
775                 xfs_bmap_init(&free_list, &firstfsb);
776                 nimaps = 1;
777                 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
778                                   XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
779                                   1, &imap, &nimaps, &free_list, NULL);
780                 if (error)
781                         goto error_on_bmapi_transaction;
782
783                 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
784                 if (error)
785                         goto error_on_bmapi_transaction;
786
787                 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
788                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
789                 if (error)
790                         return XFS_ERROR(error);
791
792                 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
793                         return xfs_cmn_err_fsblock_zero(ip, &imap);
794
795                 if ((numblks_fsb = imap.br_blockcount) == 0) {
796                         /*
797                          * The numblks_fsb value should always get
798                          * smaller, otherwise the loop is stuck.
799                          */
800                         ASSERT(imap.br_blockcount);
801                         break;
802                 }
803                 offset_fsb += numblks_fsb;
804                 count_fsb -= numblks_fsb;
805         } while (count_fsb > 0);
806
807         return 0;
808
809 error_on_bmapi_transaction:
810         xfs_bmap_cancel(&free_list);
811         xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
812         xfs_iunlock(ip, XFS_ILOCK_EXCL);
813         return XFS_ERROR(error);
814 }