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