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