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