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