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