Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[pandora-kernel.git] / fs / xfs / xfs_bmap.c
1 /*
2  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_btree.h"
36 #include "xfs_mount.h"
37 #include "xfs_itable.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_inode_item.h"
42 #include "xfs_extfree_item.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_rw.h"
49 #include "xfs_quota.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_filestream.h"
53 #include "xfs_vnodeops.h"
54 #include "xfs_trace.h"
55
56
57 #ifdef DEBUG
58 STATIC void
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60 #endif
61
62 kmem_zone_t             *xfs_bmap_free_item_zone;
63
64 /*
65  * Prototypes for internal bmap routines.
66  */
67
68
69 /*
70  * Called from xfs_bmap_add_attrfork to handle extents format files.
71  */
72 STATIC int                                      /* error */
73 xfs_bmap_add_attrfork_extents(
74         xfs_trans_t             *tp,            /* transaction pointer */
75         xfs_inode_t             *ip,            /* incore inode pointer */
76         xfs_fsblock_t           *firstblock,    /* first block allocated */
77         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
78         int                     *flags);        /* inode logging flags */
79
80 /*
81  * Called from xfs_bmap_add_attrfork to handle local format files.
82  */
83 STATIC int                                      /* error */
84 xfs_bmap_add_attrfork_local(
85         xfs_trans_t             *tp,            /* transaction pointer */
86         xfs_inode_t             *ip,            /* incore inode pointer */
87         xfs_fsblock_t           *firstblock,    /* first block allocated */
88         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
89         int                     *flags);        /* inode logging flags */
90
91 /*
92  * Called by xfs_bmapi to update file extent records and the btree
93  * after allocating space (or doing a delayed allocation).
94  */
95 STATIC int                              /* error */
96 xfs_bmap_add_extent(
97         xfs_inode_t             *ip,    /* incore inode pointer */
98         xfs_extnum_t            idx,    /* extent number to update/insert */
99         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
100         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
101         xfs_fsblock_t           *first, /* pointer to firstblock variable */
102         xfs_bmap_free_t         *flist, /* list of extents to be freed */
103         int                     *logflagsp, /* inode logging flags */
104         int                     whichfork, /* data or attr fork */
105         int                     rsvd);  /* OK to allocate reserved blocks */
106
107 /*
108  * Called by xfs_bmap_add_extent to handle cases converting a delayed
109  * allocation to a real allocation.
110  */
111 STATIC int                              /* error */
112 xfs_bmap_add_extent_delay_real(
113         xfs_inode_t             *ip,    /* incore inode pointer */
114         xfs_extnum_t            idx,    /* extent number to update/insert */
115         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
116         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
117         xfs_filblks_t           *dnew,  /* new delayed-alloc indirect blocks */
118         xfs_fsblock_t           *first, /* pointer to firstblock variable */
119         xfs_bmap_free_t         *flist, /* list of extents to be freed */
120         int                     *logflagsp, /* inode logging flags */
121         int                     rsvd);  /* OK to allocate reserved blocks */
122
123 /*
124  * Called by xfs_bmap_add_extent to handle cases converting a hole
125  * to a delayed allocation.
126  */
127 STATIC int                              /* error */
128 xfs_bmap_add_extent_hole_delay(
129         xfs_inode_t             *ip,    /* incore inode pointer */
130         xfs_extnum_t            idx,    /* extent number to update/insert */
131         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
132         int                     *logflagsp,/* inode logging flags */
133         int                     rsvd);  /* OK to allocate reserved blocks */
134
135 /*
136  * Called by xfs_bmap_add_extent to handle cases converting a hole
137  * to a real allocation.
138  */
139 STATIC int                              /* error */
140 xfs_bmap_add_extent_hole_real(
141         xfs_inode_t             *ip,    /* incore inode pointer */
142         xfs_extnum_t            idx,    /* extent number to update/insert */
143         xfs_btree_cur_t         *cur,   /* if null, not a btree */
144         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
145         int                     *logflagsp, /* inode logging flags */
146         int                     whichfork); /* data or attr fork */
147
148 /*
149  * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150  * allocation to a real allocation or vice versa.
151  */
152 STATIC int                              /* error */
153 xfs_bmap_add_extent_unwritten_real(
154         xfs_inode_t             *ip,    /* incore inode pointer */
155         xfs_extnum_t            idx,    /* extent number to update/insert */
156         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
157         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
158         int                     *logflagsp); /* inode logging flags */
159
160 /*
161  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
162  * It figures out where to ask the underlying allocator to put the new extent.
163  */
164 STATIC int                              /* error */
165 xfs_bmap_alloc(
166         xfs_bmalloca_t          *ap);   /* bmap alloc argument struct */
167
168 /*
169  * Transform a btree format file with only one leaf node, where the
170  * extents list will fit in the inode, into an extents format file.
171  * Since the file extents are already in-core, all we have to do is
172  * give up the space for the btree root and pitch the leaf block.
173  */
174 STATIC int                              /* error */
175 xfs_bmap_btree_to_extents(
176         xfs_trans_t             *tp,    /* transaction pointer */
177         xfs_inode_t             *ip,    /* incore inode pointer */
178         xfs_btree_cur_t         *cur,   /* btree cursor */
179         int                     *logflagsp, /* inode logging flags */
180         int                     whichfork); /* data or attr fork */
181
182 /*
183  * Called by xfs_bmapi to update file extent records and the btree
184  * after removing space (or undoing a delayed allocation).
185  */
186 STATIC int                              /* error */
187 xfs_bmap_del_extent(
188         xfs_inode_t             *ip,    /* incore inode pointer */
189         xfs_trans_t             *tp,    /* current trans pointer */
190         xfs_extnum_t            idx,    /* extent number to update/insert */
191         xfs_bmap_free_t         *flist, /* list of extents to be freed */
192         xfs_btree_cur_t         *cur,   /* if null, not a btree */
193         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
194         int                     *logflagsp,/* inode logging flags */
195         int                     whichfork, /* data or attr fork */
196         int                     rsvd);   /* OK to allocate reserved blocks */
197
198 /*
199  * Remove the entry "free" from the free item list.  Prev points to the
200  * previous entry, unless "free" is the head of the list.
201  */
202 STATIC void
203 xfs_bmap_del_free(
204         xfs_bmap_free_t         *flist, /* free item list header */
205         xfs_bmap_free_item_t    *prev,  /* previous item on list, if any */
206         xfs_bmap_free_item_t    *free); /* list item to be freed */
207
208 /*
209  * Convert an extents-format file into a btree-format file.
210  * The new file will have a root block (in the inode) and a single child block.
211  */
212 STATIC int                                      /* error */
213 xfs_bmap_extents_to_btree(
214         xfs_trans_t             *tp,            /* transaction pointer */
215         xfs_inode_t             *ip,            /* incore inode pointer */
216         xfs_fsblock_t           *firstblock,    /* first-block-allocated */
217         xfs_bmap_free_t         *flist,         /* blocks freed in xaction */
218         xfs_btree_cur_t         **curp,         /* cursor returned to caller */
219         int                     wasdel,         /* converting a delayed alloc */
220         int                     *logflagsp,     /* inode logging flags */
221         int                     whichfork);     /* data or attr fork */
222
223 /*
224  * Convert a local file to an extents file.
225  * This code is sort of bogus, since the file data needs to get
226  * logged so it won't be lost.  The bmap-level manipulations are ok, though.
227  */
228 STATIC int                              /* error */
229 xfs_bmap_local_to_extents(
230         xfs_trans_t     *tp,            /* transaction pointer */
231         xfs_inode_t     *ip,            /* incore inode pointer */
232         xfs_fsblock_t   *firstblock,    /* first block allocated in xaction */
233         xfs_extlen_t    total,          /* total blocks needed by transaction */
234         int             *logflagsp,     /* inode logging flags */
235         int             whichfork);     /* data or attr fork */
236
237 /*
238  * Search the extents list for the inode, for the extent containing bno.
239  * If bno lies in a hole, point to the next entry.  If bno lies past eof,
240  * *eofp will be set, and *prevp will contain the last entry (null if none).
241  * Else, *lastxp will be set to the index of the found
242  * entry; *gotp will contain the entry.
243  */
244 STATIC xfs_bmbt_rec_host_t *            /* pointer to found extent entry */
245 xfs_bmap_search_extents(
246         xfs_inode_t     *ip,            /* incore inode pointer */
247         xfs_fileoff_t   bno,            /* block number searched for */
248         int             whichfork,      /* data or attr fork */
249         int             *eofp,          /* out: end of file found */
250         xfs_extnum_t    *lastxp,        /* out: last extent index */
251         xfs_bmbt_irec_t *gotp,          /* out: extent entry found */
252         xfs_bmbt_irec_t *prevp);        /* out: previous extent entry found */
253
254 /*
255  * Check the last inode extent to determine whether this allocation will result
256  * in blocks being allocated at the end of the file. When we allocate new data
257  * blocks at the end of the file which do not start at the previous data block,
258  * we will try to align the new blocks at stripe unit boundaries.
259  */
260 STATIC int                              /* error */
261 xfs_bmap_isaeof(
262         xfs_inode_t     *ip,            /* incore inode pointer */
263         xfs_fileoff_t   off,            /* file offset in fsblocks */
264         int             whichfork,      /* data or attribute fork */
265         char            *aeof);         /* return value */
266
267 /*
268  * Compute the worst-case number of indirect blocks that will be used
269  * for ip's delayed extent of length "len".
270  */
271 STATIC xfs_filblks_t
272 xfs_bmap_worst_indlen(
273         xfs_inode_t             *ip,    /* incore inode pointer */
274         xfs_filblks_t           len);   /* delayed extent length */
275
276 #ifdef DEBUG
277 /*
278  * Perform various validation checks on the values being returned
279  * from xfs_bmapi().
280  */
281 STATIC void
282 xfs_bmap_validate_ret(
283         xfs_fileoff_t           bno,
284         xfs_filblks_t           len,
285         int                     flags,
286         xfs_bmbt_irec_t         *mval,
287         int                     nmap,
288         int                     ret_nmap);
289 #else
290 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
291 #endif /* DEBUG */
292
293 STATIC int
294 xfs_bmap_count_tree(
295         xfs_mount_t     *mp,
296         xfs_trans_t     *tp,
297         xfs_ifork_t     *ifp,
298         xfs_fsblock_t   blockno,
299         int             levelin,
300         int             *count);
301
302 STATIC void
303 xfs_bmap_count_leaves(
304         xfs_ifork_t             *ifp,
305         xfs_extnum_t            idx,
306         int                     numrecs,
307         int                     *count);
308
309 STATIC void
310 xfs_bmap_disk_count_leaves(
311         struct xfs_mount        *mp,
312         struct xfs_btree_block  *block,
313         int                     numrecs,
314         int                     *count);
315
316 /*
317  * Bmap internal routines.
318  */
319
320 STATIC int                              /* error */
321 xfs_bmbt_lookup_eq(
322         struct xfs_btree_cur    *cur,
323         xfs_fileoff_t           off,
324         xfs_fsblock_t           bno,
325         xfs_filblks_t           len,
326         int                     *stat)  /* success/failure */
327 {
328         cur->bc_rec.b.br_startoff = off;
329         cur->bc_rec.b.br_startblock = bno;
330         cur->bc_rec.b.br_blockcount = len;
331         return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
332 }
333
334 STATIC int                              /* error */
335 xfs_bmbt_lookup_ge(
336         struct xfs_btree_cur    *cur,
337         xfs_fileoff_t           off,
338         xfs_fsblock_t           bno,
339         xfs_filblks_t           len,
340         int                     *stat)  /* success/failure */
341 {
342         cur->bc_rec.b.br_startoff = off;
343         cur->bc_rec.b.br_startblock = bno;
344         cur->bc_rec.b.br_blockcount = len;
345         return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
346 }
347
348 /*
349 * Update the record referred to by cur to the value given
350  * by [off, bno, len, state].
351  * This either works (return 0) or gets an EFSCORRUPTED error.
352  */
353 STATIC int
354 xfs_bmbt_update(
355         struct xfs_btree_cur    *cur,
356         xfs_fileoff_t           off,
357         xfs_fsblock_t           bno,
358         xfs_filblks_t           len,
359         xfs_exntst_t            state)
360 {
361         union xfs_btree_rec     rec;
362
363         xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364         return xfs_btree_update(cur, &rec);
365 }
366
367 /*
368  * Called from xfs_bmap_add_attrfork to handle btree format files.
369  */
370 STATIC int                                      /* error */
371 xfs_bmap_add_attrfork_btree(
372         xfs_trans_t             *tp,            /* transaction pointer */
373         xfs_inode_t             *ip,            /* incore inode pointer */
374         xfs_fsblock_t           *firstblock,    /* first block allocated */
375         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
376         int                     *flags)         /* inode logging flags */
377 {
378         xfs_btree_cur_t         *cur;           /* btree cursor */
379         int                     error;          /* error return value */
380         xfs_mount_t             *mp;            /* file system mount struct */
381         int                     stat;           /* newroot status */
382
383         mp = ip->i_mount;
384         if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385                 *flags |= XFS_ILOG_DBROOT;
386         else {
387                 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
388                 cur->bc_private.b.flist = flist;
389                 cur->bc_private.b.firstblock = *firstblock;
390                 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
391                         goto error0;
392                 /* must be at least one entry */
393                 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
394                 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
395                         goto error0;
396                 if (stat == 0) {
397                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398                         return XFS_ERROR(ENOSPC);
399                 }
400                 *firstblock = cur->bc_private.b.firstblock;
401                 cur->bc_private.b.allocated = 0;
402                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
403         }
404         return 0;
405 error0:
406         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
407         return error;
408 }
409
410 /*
411  * Called from xfs_bmap_add_attrfork to handle extents format files.
412  */
413 STATIC int                                      /* error */
414 xfs_bmap_add_attrfork_extents(
415         xfs_trans_t             *tp,            /* transaction pointer */
416         xfs_inode_t             *ip,            /* incore inode pointer */
417         xfs_fsblock_t           *firstblock,    /* first block allocated */
418         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
419         int                     *flags)         /* inode logging flags */
420 {
421         xfs_btree_cur_t         *cur;           /* bmap btree cursor */
422         int                     error;          /* error return value */
423
424         if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
425                 return 0;
426         cur = NULL;
427         error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
428                 flags, XFS_DATA_FORK);
429         if (cur) {
430                 cur->bc_private.b.allocated = 0;
431                 xfs_btree_del_cursor(cur,
432                         error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
433         }
434         return error;
435 }
436
437 /*
438  * Called from xfs_bmap_add_attrfork to handle local format files.
439  */
440 STATIC int                                      /* error */
441 xfs_bmap_add_attrfork_local(
442         xfs_trans_t             *tp,            /* transaction pointer */
443         xfs_inode_t             *ip,            /* incore inode pointer */
444         xfs_fsblock_t           *firstblock,    /* first block allocated */
445         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
446         int                     *flags)         /* inode logging flags */
447 {
448         xfs_da_args_t           dargs;          /* args for dir/attr code */
449         int                     error;          /* error return value */
450         xfs_mount_t             *mp;            /* mount structure pointer */
451
452         if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
453                 return 0;
454         if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
455                 mp = ip->i_mount;
456                 memset(&dargs, 0, sizeof(dargs));
457                 dargs.dp = ip;
458                 dargs.firstblock = firstblock;
459                 dargs.flist = flist;
460                 dargs.total = mp->m_dirblkfsbs;
461                 dargs.whichfork = XFS_DATA_FORK;
462                 dargs.trans = tp;
463                 error = xfs_dir2_sf_to_block(&dargs);
464         } else
465                 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
466                         XFS_DATA_FORK);
467         return error;
468 }
469
470 /*
471  * Called by xfs_bmapi to update file extent records and the btree
472  * after allocating space (or doing a delayed allocation).
473  */
474 STATIC int                              /* error */
475 xfs_bmap_add_extent(
476         xfs_inode_t             *ip,    /* incore inode pointer */
477         xfs_extnum_t            idx,    /* extent number to update/insert */
478         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
479         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
480         xfs_fsblock_t           *first, /* pointer to firstblock variable */
481         xfs_bmap_free_t         *flist, /* list of extents to be freed */
482         int                     *logflagsp, /* inode logging flags */
483         int                     whichfork, /* data or attr fork */
484         int                     rsvd)   /* OK to use reserved data blocks */
485 {
486         xfs_btree_cur_t         *cur;   /* btree cursor or null */
487         xfs_filblks_t           da_new; /* new count del alloc blocks used */
488         xfs_filblks_t           da_old; /* old count del alloc blocks used */
489         int                     error;  /* error return value */
490         xfs_ifork_t             *ifp;   /* inode fork ptr */
491         int                     logflags; /* returned value */
492         xfs_extnum_t            nextents; /* number of extents in file now */
493
494         XFS_STATS_INC(xs_add_exlist);
495         cur = *curp;
496         ifp = XFS_IFORK_PTR(ip, whichfork);
497         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
498         ASSERT(idx <= nextents);
499         da_old = da_new = 0;
500         error = 0;
501         /*
502          * This is the first extent added to a new/empty file.
503          * Special case this one, so other routines get to assume there are
504          * already extents in the list.
505          */
506         if (nextents == 0) {
507                 xfs_iext_insert(ip, 0, 1, new,
508                                 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
509
510                 ASSERT(cur == NULL);
511                 ifp->if_lastex = 0;
512                 if (!isnullstartblock(new->br_startblock)) {
513                         XFS_IFORK_NEXT_SET(ip, whichfork, 1);
514                         logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
515                 } else
516                         logflags = 0;
517         }
518         /*
519          * Any kind of new delayed allocation goes here.
520          */
521         else if (isnullstartblock(new->br_startblock)) {
522                 if (cur)
523                         ASSERT((cur->bc_private.b.flags &
524                                 XFS_BTCUR_BPRV_WASDEL) == 0);
525                 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
526                                 &logflags, rsvd)))
527                         goto done;
528         }
529         /*
530          * Real allocation off the end of the file.
531          */
532         else if (idx == nextents) {
533                 if (cur)
534                         ASSERT((cur->bc_private.b.flags &
535                                 XFS_BTCUR_BPRV_WASDEL) == 0);
536                 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
537                                 &logflags, whichfork)))
538                         goto done;
539         } else {
540                 xfs_bmbt_irec_t prev;   /* old extent at offset idx */
541
542                 /*
543                  * Get the record referred to by idx.
544                  */
545                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
546                 /*
547                  * If it's a real allocation record, and the new allocation ends
548                  * after the start of the referred to record, then we're filling
549                  * in a delayed or unwritten allocation with a real one, or
550                  * converting real back to unwritten.
551                  */
552                 if (!isnullstartblock(new->br_startblock) &&
553                     new->br_startoff + new->br_blockcount > prev.br_startoff) {
554                         if (prev.br_state != XFS_EXT_UNWRITTEN &&
555                             isnullstartblock(prev.br_startblock)) {
556                                 da_old = startblockval(prev.br_startblock);
557                                 if (cur)
558                                         ASSERT(cur->bc_private.b.flags &
559                                                 XFS_BTCUR_BPRV_WASDEL);
560                                 if ((error = xfs_bmap_add_extent_delay_real(ip,
561                                         idx, &cur, new, &da_new, first, flist,
562                                         &logflags, rsvd)))
563                                         goto done;
564                         } else if (new->br_state == XFS_EXT_NORM) {
565                                 ASSERT(new->br_state == XFS_EXT_NORM);
566                                 if ((error = xfs_bmap_add_extent_unwritten_real(
567                                         ip, idx, &cur, new, &logflags)))
568                                         goto done;
569                         } else {
570                                 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571                                 if ((error = xfs_bmap_add_extent_unwritten_real(
572                                         ip, idx, &cur, new, &logflags)))
573                                         goto done;
574                         }
575                         ASSERT(*curp == cur || *curp == NULL);
576                 }
577                 /*
578                  * Otherwise we're filling in a hole with an allocation.
579                  */
580                 else {
581                         if (cur)
582                                 ASSERT((cur->bc_private.b.flags &
583                                         XFS_BTCUR_BPRV_WASDEL) == 0);
584                         if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
585                                         new, &logflags, whichfork)))
586                                 goto done;
587                 }
588         }
589
590         ASSERT(*curp == cur || *curp == NULL);
591         /*
592          * Convert to a btree if necessary.
593          */
594         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
595             XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
596                 int     tmp_logflags;   /* partial log flag return val */
597
598                 ASSERT(cur == NULL);
599                 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
600                         flist, &cur, da_old > 0, &tmp_logflags, whichfork);
601                 logflags |= tmp_logflags;
602                 if (error)
603                         goto done;
604         }
605         /*
606          * Adjust for changes in reserved delayed indirect blocks.
607          * Nothing to do for disk quotas here.
608          */
609         if (da_old || da_new) {
610                 xfs_filblks_t   nblks;
611
612                 nblks = da_new;
613                 if (cur)
614                         nblks += cur->bc_private.b.allocated;
615                 ASSERT(nblks <= da_old);
616                 if (nblks < da_old)
617                         xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
618                                 (int64_t)(da_old - nblks), rsvd);
619         }
620         /*
621          * Clear out the allocated field, done with it now in any case.
622          */
623         if (cur) {
624                 cur->bc_private.b.allocated = 0;
625                 *curp = cur;
626         }
627 done:
628 #ifdef DEBUG
629         if (!error)
630                 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
631 #endif
632         *logflagsp = logflags;
633         return error;
634 }
635
636 /*
637  * Called by xfs_bmap_add_extent to handle cases converting a delayed
638  * allocation to a real allocation.
639  */
640 STATIC int                              /* error */
641 xfs_bmap_add_extent_delay_real(
642         xfs_inode_t             *ip,    /* incore inode pointer */
643         xfs_extnum_t            idx,    /* extent number to update/insert */
644         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
645         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
646         xfs_filblks_t           *dnew,  /* new delayed-alloc indirect blocks */
647         xfs_fsblock_t           *first, /* pointer to firstblock variable */
648         xfs_bmap_free_t         *flist, /* list of extents to be freed */
649         int                     *logflagsp, /* inode logging flags */
650         int                     rsvd)   /* OK to use reserved data block allocation */
651 {
652         xfs_btree_cur_t         *cur;   /* btree cursor */
653         int                     diff;   /* temp value */
654         xfs_bmbt_rec_host_t     *ep;    /* extent entry for idx */
655         int                     error;  /* error return value */
656         int                     i;      /* temp state */
657         xfs_ifork_t             *ifp;   /* inode fork pointer */
658         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
659         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
660                                         /* left is 0, right is 1, prev is 2 */
661         int                     rval=0; /* return value (logging flags) */
662         int                     state = 0;/* state bits, accessed thru macros */
663         xfs_filblks_t           temp=0; /* value for dnew calculations */
664         xfs_filblks_t           temp2=0;/* value for dnew calculations */
665         int                     tmp_rval;       /* partial logging flags */
666
667 #define LEFT            r[0]
668 #define RIGHT           r[1]
669 #define PREV            r[2]
670
671         /*
672          * Set up a bunch of variables to make the tests simpler.
673          */
674         cur = *curp;
675         ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676         ep = xfs_iext_get_ext(ifp, idx);
677         xfs_bmbt_get_all(ep, &PREV);
678         new_endoff = new->br_startoff + new->br_blockcount;
679         ASSERT(PREV.br_startoff <= new->br_startoff);
680         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
681
682         /*
683          * Set flags determining what part of the previous delayed allocation
684          * extent is being replaced by a real allocation.
685          */
686         if (PREV.br_startoff == new->br_startoff)
687                 state |= BMAP_LEFT_FILLING;
688         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
689                 state |= BMAP_RIGHT_FILLING;
690
691         /*
692          * Check and set flags if this segment has a left neighbor.
693          * Don't set contiguous if the combined extent would be too large.
694          */
695         if (idx > 0) {
696                 state |= BMAP_LEFT_VALID;
697                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
698
699                 if (isnullstartblock(LEFT.br_startblock))
700                         state |= BMAP_LEFT_DELAY;
701         }
702
703         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
704             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
705             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
706             LEFT.br_state == new->br_state &&
707             LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
708                 state |= BMAP_LEFT_CONTIG;
709
710         /*
711          * Check and set flags if this segment has a right neighbor.
712          * Don't set contiguous if the combined extent would be too large.
713          * Also check for all-three-contiguous being too large.
714          */
715         if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716                 state |= BMAP_RIGHT_VALID;
717                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
718
719                 if (isnullstartblock(RIGHT.br_startblock))
720                         state |= BMAP_RIGHT_DELAY;
721         }
722
723         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
724             new_endoff == RIGHT.br_startoff &&
725             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
726             new->br_state == RIGHT.br_state &&
727             new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
728             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
729                        BMAP_RIGHT_FILLING)) !=
730                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
731                        BMAP_RIGHT_FILLING) ||
732              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
733                         <= MAXEXTLEN))
734                 state |= BMAP_RIGHT_CONTIG;
735
736         error = 0;
737         /*
738          * Switch out based on the FILLING and CONTIG state bits.
739          */
740         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
741                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
742         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
743              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
744                 /*
745                  * Filling in all of a previously delayed allocation extent.
746                  * The left and right neighbors are both contiguous with new.
747                  */
748                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
749                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
750                         LEFT.br_blockcount + PREV.br_blockcount +
751                         RIGHT.br_blockcount);
752                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
753
754                 xfs_iext_remove(ip, idx, 2, state);
755                 ip->i_df.if_lastex = idx - 1;
756                 ip->i_d.di_nextents--;
757                 if (cur == NULL)
758                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
759                 else {
760                         rval = XFS_ILOG_CORE;
761                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
762                                         RIGHT.br_startblock,
763                                         RIGHT.br_blockcount, &i)))
764                                 goto done;
765                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
766                         if ((error = xfs_btree_delete(cur, &i)))
767                                 goto done;
768                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
769                         if ((error = xfs_btree_decrement(cur, 0, &i)))
770                                 goto done;
771                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
772                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
773                                         LEFT.br_startblock,
774                                         LEFT.br_blockcount +
775                                         PREV.br_blockcount +
776                                         RIGHT.br_blockcount, LEFT.br_state)))
777                                 goto done;
778                 }
779                 *dnew = 0;
780                 break;
781
782         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
783                 /*
784                  * Filling in all of a previously delayed allocation extent.
785                  * The left neighbor is contiguous, the right is not.
786                  */
787                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
788                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
789                         LEFT.br_blockcount + PREV.br_blockcount);
790                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
791
792                 ip->i_df.if_lastex = idx - 1;
793                 xfs_iext_remove(ip, idx, 1, state);
794                 if (cur == NULL)
795                         rval = XFS_ILOG_DEXT;
796                 else {
797                         rval = 0;
798                         if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
799                                         LEFT.br_startblock, LEFT.br_blockcount,
800                                         &i)))
801                                 goto done;
802                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
803                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
804                                         LEFT.br_startblock,
805                                         LEFT.br_blockcount +
806                                         PREV.br_blockcount, LEFT.br_state)))
807                                 goto done;
808                 }
809                 *dnew = 0;
810                 break;
811
812         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
813                 /*
814                  * Filling in all of a previously delayed allocation extent.
815                  * The right neighbor is contiguous, the left is not.
816                  */
817                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
818                 xfs_bmbt_set_startblock(ep, new->br_startblock);
819                 xfs_bmbt_set_blockcount(ep,
820                         PREV.br_blockcount + RIGHT.br_blockcount);
821                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
822
823                 ip->i_df.if_lastex = idx;
824                 xfs_iext_remove(ip, idx + 1, 1, state);
825                 if (cur == NULL)
826                         rval = XFS_ILOG_DEXT;
827                 else {
828                         rval = 0;
829                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830                                         RIGHT.br_startblock,
831                                         RIGHT.br_blockcount, &i)))
832                                 goto done;
833                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
834                         if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
835                                         new->br_startblock,
836                                         PREV.br_blockcount +
837                                         RIGHT.br_blockcount, PREV.br_state)))
838                                 goto done;
839                 }
840                 *dnew = 0;
841                 break;
842
843         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
844                 /*
845                  * Filling in all of a previously delayed allocation extent.
846                  * Neither the left nor right neighbors are contiguous with
847                  * the new one.
848                  */
849                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
850                 xfs_bmbt_set_startblock(ep, new->br_startblock);
851                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
852
853                 ip->i_df.if_lastex = idx;
854                 ip->i_d.di_nextents++;
855                 if (cur == NULL)
856                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
857                 else {
858                         rval = XFS_ILOG_CORE;
859                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
860                                         new->br_startblock, new->br_blockcount,
861                                         &i)))
862                                 goto done;
863                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
864                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
865                         if ((error = xfs_btree_insert(cur, &i)))
866                                 goto done;
867                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
868                 }
869                 *dnew = 0;
870                 break;
871
872         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
873                 /*
874                  * Filling in the first part of a previous delayed allocation.
875                  * The left neighbor is contiguous.
876                  */
877                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
878                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
879                         LEFT.br_blockcount + new->br_blockcount);
880                 xfs_bmbt_set_startoff(ep,
881                         PREV.br_startoff + new->br_blockcount);
882                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
883
884                 temp = PREV.br_blockcount - new->br_blockcount;
885                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
886                 xfs_bmbt_set_blockcount(ep, temp);
887                 ip->i_df.if_lastex = idx - 1;
888                 if (cur == NULL)
889                         rval = XFS_ILOG_DEXT;
890                 else {
891                         rval = 0;
892                         if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
893                                         LEFT.br_startblock, LEFT.br_blockcount,
894                                         &i)))
895                                 goto done;
896                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
897                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
898                                         LEFT.br_startblock,
899                                         LEFT.br_blockcount +
900                                         new->br_blockcount,
901                                         LEFT.br_state)))
902                                 goto done;
903                 }
904                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
905                         startblockval(PREV.br_startblock));
906                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
907                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
908                 *dnew = temp;
909                 break;
910
911         case BMAP_LEFT_FILLING:
912                 /*
913                  * Filling in the first part of a previous delayed allocation.
914                  * The left neighbor is not contiguous.
915                  */
916                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
917                 xfs_bmbt_set_startoff(ep, new_endoff);
918                 temp = PREV.br_blockcount - new->br_blockcount;
919                 xfs_bmbt_set_blockcount(ep, temp);
920                 xfs_iext_insert(ip, idx, 1, new, state);
921                 ip->i_df.if_lastex = idx;
922                 ip->i_d.di_nextents++;
923                 if (cur == NULL)
924                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
925                 else {
926                         rval = XFS_ILOG_CORE;
927                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928                                         new->br_startblock, new->br_blockcount,
929                                         &i)))
930                                 goto done;
931                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
932                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
933                         if ((error = xfs_btree_insert(cur, &i)))
934                                 goto done;
935                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
936                 }
937                 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
938                     ip->i_d.di_nextents > ip->i_df.if_ext_max) {
939                         error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
940                                         first, flist, &cur, 1, &tmp_rval,
941                                         XFS_DATA_FORK);
942                         rval |= tmp_rval;
943                         if (error)
944                                 goto done;
945                 }
946                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
947                         startblockval(PREV.br_startblock) -
948                         (cur ? cur->bc_private.b.allocated : 0));
949                 ep = xfs_iext_get_ext(ifp, idx + 1);
950                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
951                 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
952                 *dnew = temp;
953                 break;
954
955         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
956                 /*
957                  * Filling in the last part of a previous delayed allocation.
958                  * The right neighbor is contiguous with the new allocation.
959                  */
960                 temp = PREV.br_blockcount - new->br_blockcount;
961                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962                 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
963                 xfs_bmbt_set_blockcount(ep, temp);
964                 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965                         new->br_startoff, new->br_startblock,
966                         new->br_blockcount + RIGHT.br_blockcount,
967                         RIGHT.br_state);
968                 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
969                 ip->i_df.if_lastex = idx + 1;
970                 if (cur == NULL)
971                         rval = XFS_ILOG_DEXT;
972                 else {
973                         rval = 0;
974                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
975                                         RIGHT.br_startblock,
976                                         RIGHT.br_blockcount, &i)))
977                                 goto done;
978                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
979                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
980                                         new->br_startblock,
981                                         new->br_blockcount +
982                                         RIGHT.br_blockcount,
983                                         RIGHT.br_state)))
984                                 goto done;
985                 }
986                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
987                         startblockval(PREV.br_startblock));
988                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
989                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
990                 *dnew = temp;
991                 break;
992
993         case BMAP_RIGHT_FILLING:
994                 /*
995                  * Filling in the last part of a previous delayed allocation.
996                  * The right neighbor is not contiguous.
997                  */
998                 temp = PREV.br_blockcount - new->br_blockcount;
999                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1000                 xfs_bmbt_set_blockcount(ep, temp);
1001                 xfs_iext_insert(ip, idx + 1, 1, new, state);
1002                 ip->i_df.if_lastex = idx + 1;
1003                 ip->i_d.di_nextents++;
1004                 if (cur == NULL)
1005                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1006                 else {
1007                         rval = XFS_ILOG_CORE;
1008                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1009                                         new->br_startblock, new->br_blockcount,
1010                                         &i)))
1011                                 goto done;
1012                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1013                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
1014                         if ((error = xfs_btree_insert(cur, &i)))
1015                                 goto done;
1016                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1017                 }
1018                 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1019                     ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1020                         error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1021                                 first, flist, &cur, 1, &tmp_rval,
1022                                 XFS_DATA_FORK);
1023                         rval |= tmp_rval;
1024                         if (error)
1025                                 goto done;
1026                 }
1027                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1028                         startblockval(PREV.br_startblock) -
1029                         (cur ? cur->bc_private.b.allocated : 0));
1030                 ep = xfs_iext_get_ext(ifp, idx);
1031                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1032                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1033                 *dnew = temp;
1034                 break;
1035
1036         case 0:
1037                 /*
1038                  * Filling in the middle part of a previous delayed allocation.
1039                  * Contiguity is impossible here.
1040                  * This case is avoided almost all the time.
1041                  *
1042                  * We start with a delayed allocation:
1043                  *
1044                  * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1045                  *  PREV @ idx
1046                  *
1047                  * and we are allocating:
1048                  *                     +rrrrrrrrrrrrrrrrr+
1049                  *                            new
1050                  *
1051                  * and we set it up for insertion as:
1052                  * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1053                  *                            new
1054                  *  PREV @ idx          LEFT              RIGHT
1055                  *                      inserted at idx + 1
1056                  */
1057                 temp = new->br_startoff - PREV.br_startoff;
1058                 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1059                 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1060                 xfs_bmbt_set_blockcount(ep, temp);      /* truncate PREV */
1061                 LEFT = *new;
1062                 RIGHT.br_state = PREV.br_state;
1063                 RIGHT.br_startblock = nullstartblock(
1064                                 (int)xfs_bmap_worst_indlen(ip, temp2));
1065                 RIGHT.br_startoff = new_endoff;
1066                 RIGHT.br_blockcount = temp2;
1067                 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1068                 xfs_iext_insert(ip, idx + 1, 2, &LEFT, state);
1069                 ip->i_df.if_lastex = idx + 1;
1070                 ip->i_d.di_nextents++;
1071                 if (cur == NULL)
1072                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1073                 else {
1074                         rval = XFS_ILOG_CORE;
1075                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1076                                         new->br_startblock, new->br_blockcount,
1077                                         &i)))
1078                                 goto done;
1079                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1080                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
1081                         if ((error = xfs_btree_insert(cur, &i)))
1082                                 goto done;
1083                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1084                 }
1085                 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1086                     ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1087                         error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1088                                         first, flist, &cur, 1, &tmp_rval,
1089                                         XFS_DATA_FORK);
1090                         rval |= tmp_rval;
1091                         if (error)
1092                                 goto done;
1093                 }
1094                 temp = xfs_bmap_worst_indlen(ip, temp);
1095                 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1096                 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1097                         (cur ? cur->bc_private.b.allocated : 0));
1098                 if (diff > 0 &&
1099                     xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1100                                              -((int64_t)diff), rsvd)) {
1101                         /*
1102                          * Ick gross gag me with a spoon.
1103                          */
1104                         ASSERT(0);      /* want to see if this ever happens! */
1105                         while (diff > 0) {
1106                                 if (temp) {
1107                                         temp--;
1108                                         diff--;
1109                                         if (!diff ||
1110                                             !xfs_icsb_modify_counters(ip->i_mount,
1111                                                     XFS_SBS_FDBLOCKS,
1112                                                     -((int64_t)diff), rsvd))
1113                                                 break;
1114                                 }
1115                                 if (temp2) {
1116                                         temp2--;
1117                                         diff--;
1118                                         if (!diff ||
1119                                             !xfs_icsb_modify_counters(ip->i_mount,
1120                                                     XFS_SBS_FDBLOCKS,
1121                                                     -((int64_t)diff), rsvd))
1122                                                 break;
1123                                 }
1124                         }
1125                 }
1126                 ep = xfs_iext_get_ext(ifp, idx);
1127                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1128                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1129                 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1130                 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1131                         nullstartblock((int)temp2));
1132                 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1133                 *dnew = temp + temp2;
1134                 break;
1135
1136         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1137         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1138         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1139         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1140         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1141         case BMAP_LEFT_CONTIG:
1142         case BMAP_RIGHT_CONTIG:
1143                 /*
1144                  * These cases are all impossible.
1145                  */
1146                 ASSERT(0);
1147         }
1148         *curp = cur;
1149 done:
1150         *logflagsp = rval;
1151         return error;
1152 #undef  LEFT
1153 #undef  RIGHT
1154 #undef  PREV
1155 }
1156
1157 /*
1158  * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1159  * allocation to a real allocation or vice versa.
1160  */
1161 STATIC int                              /* error */
1162 xfs_bmap_add_extent_unwritten_real(
1163         xfs_inode_t             *ip,    /* incore inode pointer */
1164         xfs_extnum_t            idx,    /* extent number to update/insert */
1165         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
1166         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
1167         int                     *logflagsp) /* inode logging flags */
1168 {
1169         xfs_btree_cur_t         *cur;   /* btree cursor */
1170         xfs_bmbt_rec_host_t     *ep;    /* extent entry for idx */
1171         int                     error;  /* error return value */
1172         int                     i;      /* temp state */
1173         xfs_ifork_t             *ifp;   /* inode fork pointer */
1174         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
1175         xfs_exntst_t            newext; /* new extent state */
1176         xfs_exntst_t            oldext; /* old extent state */
1177         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
1178                                         /* left is 0, right is 1, prev is 2 */
1179         int                     rval=0; /* return value (logging flags) */
1180         int                     state = 0;/* state bits, accessed thru macros */
1181
1182 #define LEFT            r[0]
1183 #define RIGHT           r[1]
1184 #define PREV            r[2]
1185         /*
1186          * Set up a bunch of variables to make the tests simpler.
1187          */
1188         error = 0;
1189         cur = *curp;
1190         ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1191         ep = xfs_iext_get_ext(ifp, idx);
1192         xfs_bmbt_get_all(ep, &PREV);
1193         newext = new->br_state;
1194         oldext = (newext == XFS_EXT_UNWRITTEN) ?
1195                 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1196         ASSERT(PREV.br_state == oldext);
1197         new_endoff = new->br_startoff + new->br_blockcount;
1198         ASSERT(PREV.br_startoff <= new->br_startoff);
1199         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1200
1201         /*
1202          * Set flags determining what part of the previous oldext allocation
1203          * extent is being replaced by a newext allocation.
1204          */
1205         if (PREV.br_startoff == new->br_startoff)
1206                 state |= BMAP_LEFT_FILLING;
1207         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1208                 state |= BMAP_RIGHT_FILLING;
1209
1210         /*
1211          * Check and set flags if this segment has a left neighbor.
1212          * Don't set contiguous if the combined extent would be too large.
1213          */
1214         if (idx > 0) {
1215                 state |= BMAP_LEFT_VALID;
1216                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1217
1218                 if (isnullstartblock(LEFT.br_startblock))
1219                         state |= BMAP_LEFT_DELAY;
1220         }
1221
1222         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1223             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1224             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1225             LEFT.br_state == newext &&
1226             LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1227                 state |= BMAP_LEFT_CONTIG;
1228
1229         /*
1230          * Check and set flags if this segment has a right neighbor.
1231          * Don't set contiguous if the combined extent would be too large.
1232          * Also check for all-three-contiguous being too large.
1233          */
1234         if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1235                 state |= BMAP_RIGHT_VALID;
1236                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1237                 if (isnullstartblock(RIGHT.br_startblock))
1238                         state |= BMAP_RIGHT_DELAY;
1239         }
1240
1241         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1242             new_endoff == RIGHT.br_startoff &&
1243             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1244             newext == RIGHT.br_state &&
1245             new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1246             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1247                        BMAP_RIGHT_FILLING)) !=
1248                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1249                        BMAP_RIGHT_FILLING) ||
1250              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1251                         <= MAXEXTLEN))
1252                 state |= BMAP_RIGHT_CONTIG;
1253
1254         /*
1255          * Switch out based on the FILLING and CONTIG state bits.
1256          */
1257         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1258                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1259         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1260              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1261                 /*
1262                  * Setting all of a previous oldext extent to newext.
1263                  * The left and right neighbors are both contiguous with new.
1264                  */
1265                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1266                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1267                         LEFT.br_blockcount + PREV.br_blockcount +
1268                         RIGHT.br_blockcount);
1269                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1270
1271                 xfs_iext_remove(ip, idx, 2, state);
1272                 ip->i_df.if_lastex = idx - 1;
1273                 ip->i_d.di_nextents -= 2;
1274                 if (cur == NULL)
1275                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1276                 else {
1277                         rval = XFS_ILOG_CORE;
1278                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1279                                         RIGHT.br_startblock,
1280                                         RIGHT.br_blockcount, &i)))
1281                                 goto done;
1282                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1283                         if ((error = xfs_btree_delete(cur, &i)))
1284                                 goto done;
1285                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1286                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1287                                 goto done;
1288                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1289                         if ((error = xfs_btree_delete(cur, &i)))
1290                                 goto done;
1291                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1292                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1293                                 goto done;
1294                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1295                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1296                                 LEFT.br_startblock,
1297                                 LEFT.br_blockcount + PREV.br_blockcount +
1298                                 RIGHT.br_blockcount, LEFT.br_state)))
1299                                 goto done;
1300                 }
1301                 break;
1302
1303         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1304                 /*
1305                  * Setting all of a previous oldext extent to newext.
1306                  * The left neighbor is contiguous, the right is not.
1307                  */
1308                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1309                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1310                         LEFT.br_blockcount + PREV.br_blockcount);
1311                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1312
1313                 ip->i_df.if_lastex = idx - 1;
1314                 xfs_iext_remove(ip, idx, 1, state);
1315                 ip->i_d.di_nextents--;
1316                 if (cur == NULL)
1317                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1318                 else {
1319                         rval = XFS_ILOG_CORE;
1320                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1321                                         PREV.br_startblock, PREV.br_blockcount,
1322                                         &i)))
1323                                 goto done;
1324                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1325                         if ((error = xfs_btree_delete(cur, &i)))
1326                                 goto done;
1327                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1328                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1329                                 goto done;
1330                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1331                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1332                                 LEFT.br_startblock,
1333                                 LEFT.br_blockcount + PREV.br_blockcount,
1334                                 LEFT.br_state)))
1335                                 goto done;
1336                 }
1337                 break;
1338
1339         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1340                 /*
1341                  * Setting all of a previous oldext extent to newext.
1342                  * The right neighbor is contiguous, the left is not.
1343                  */
1344                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1345                 xfs_bmbt_set_blockcount(ep,
1346                         PREV.br_blockcount + RIGHT.br_blockcount);
1347                 xfs_bmbt_set_state(ep, newext);
1348                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1349                 ip->i_df.if_lastex = idx;
1350                 xfs_iext_remove(ip, idx + 1, 1, state);
1351                 ip->i_d.di_nextents--;
1352                 if (cur == NULL)
1353                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1354                 else {
1355                         rval = XFS_ILOG_CORE;
1356                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1357                                         RIGHT.br_startblock,
1358                                         RIGHT.br_blockcount, &i)))
1359                                 goto done;
1360                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1361                         if ((error = xfs_btree_delete(cur, &i)))
1362                                 goto done;
1363                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1364                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1365                                 goto done;
1366                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1367                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1368                                 new->br_startblock,
1369                                 new->br_blockcount + RIGHT.br_blockcount,
1370                                 newext)))
1371                                 goto done;
1372                 }
1373                 break;
1374
1375         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1376                 /*
1377                  * Setting all of a previous oldext extent to newext.
1378                  * Neither the left nor right neighbors are contiguous with
1379                  * the new one.
1380                  */
1381                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1382                 xfs_bmbt_set_state(ep, newext);
1383                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1384
1385                 ip->i_df.if_lastex = idx;
1386                 if (cur == NULL)
1387                         rval = XFS_ILOG_DEXT;
1388                 else {
1389                         rval = 0;
1390                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1391                                         new->br_startblock, new->br_blockcount,
1392                                         &i)))
1393                                 goto done;
1394                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1395                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1396                                 new->br_startblock, new->br_blockcount,
1397                                 newext)))
1398                                 goto done;
1399                 }
1400                 break;
1401
1402         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1403                 /*
1404                  * Setting the first part of a previous oldext extent to newext.
1405                  * The left neighbor is contiguous.
1406                  */
1407                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1408                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1409                         LEFT.br_blockcount + new->br_blockcount);
1410                 xfs_bmbt_set_startoff(ep,
1411                         PREV.br_startoff + new->br_blockcount);
1412                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1413
1414                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1415                 xfs_bmbt_set_startblock(ep,
1416                         new->br_startblock + new->br_blockcount);
1417                 xfs_bmbt_set_blockcount(ep,
1418                         PREV.br_blockcount - new->br_blockcount);
1419                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1420
1421                 ip->i_df.if_lastex = idx - 1;
1422                 if (cur == NULL)
1423                         rval = XFS_ILOG_DEXT;
1424                 else {
1425                         rval = 0;
1426                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1427                                         PREV.br_startblock, PREV.br_blockcount,
1428                                         &i)))
1429                                 goto done;
1430                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1431                         if ((error = xfs_bmbt_update(cur,
1432                                 PREV.br_startoff + new->br_blockcount,
1433                                 PREV.br_startblock + new->br_blockcount,
1434                                 PREV.br_blockcount - new->br_blockcount,
1435                                 oldext)))
1436                                 goto done;
1437                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1438                                 goto done;
1439                         if (xfs_bmbt_update(cur, LEFT.br_startoff,
1440                                 LEFT.br_startblock,
1441                                 LEFT.br_blockcount + new->br_blockcount,
1442                                 LEFT.br_state))
1443                                 goto done;
1444                 }
1445                 break;
1446
1447         case BMAP_LEFT_FILLING:
1448                 /*
1449                  * Setting the first part of a previous oldext extent to newext.
1450                  * The left neighbor is not contiguous.
1451                  */
1452                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1453                 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1454                 xfs_bmbt_set_startoff(ep, new_endoff);
1455                 xfs_bmbt_set_blockcount(ep,
1456                         PREV.br_blockcount - new->br_blockcount);
1457                 xfs_bmbt_set_startblock(ep,
1458                         new->br_startblock + new->br_blockcount);
1459                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1460
1461                 xfs_iext_insert(ip, idx, 1, new, state);
1462                 ip->i_df.if_lastex = idx;
1463                 ip->i_d.di_nextents++;
1464                 if (cur == NULL)
1465                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1466                 else {
1467                         rval = XFS_ILOG_CORE;
1468                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1469                                         PREV.br_startblock, PREV.br_blockcount,
1470                                         &i)))
1471                                 goto done;
1472                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1473                         if ((error = xfs_bmbt_update(cur,
1474                                 PREV.br_startoff + new->br_blockcount,
1475                                 PREV.br_startblock + new->br_blockcount,
1476                                 PREV.br_blockcount - new->br_blockcount,
1477                                 oldext)))
1478                                 goto done;
1479                         cur->bc_rec.b = *new;
1480                         if ((error = xfs_btree_insert(cur, &i)))
1481                                 goto done;
1482                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1483                 }
1484                 break;
1485
1486         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1487                 /*
1488                  * Setting the last part of a previous oldext extent to newext.
1489                  * The right neighbor is contiguous with the new allocation.
1490                  */
1491                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1492                 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1493                 xfs_bmbt_set_blockcount(ep,
1494                         PREV.br_blockcount - new->br_blockcount);
1495                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1496                 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1497                         new->br_startoff, new->br_startblock,
1498                         new->br_blockcount + RIGHT.br_blockcount, newext);
1499                 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1500
1501                 ip->i_df.if_lastex = idx + 1;
1502                 if (cur == NULL)
1503                         rval = XFS_ILOG_DEXT;
1504                 else {
1505                         rval = 0;
1506                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1507                                         PREV.br_startblock,
1508                                         PREV.br_blockcount, &i)))
1509                                 goto done;
1510                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1511                         if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1512                                 PREV.br_startblock,
1513                                 PREV.br_blockcount - new->br_blockcount,
1514                                 oldext)))
1515                                 goto done;
1516                         if ((error = xfs_btree_increment(cur, 0, &i)))
1517                                 goto done;
1518                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1519                                 new->br_startblock,
1520                                 new->br_blockcount + RIGHT.br_blockcount,
1521                                 newext)))
1522                                 goto done;
1523                 }
1524                 break;
1525
1526         case BMAP_RIGHT_FILLING:
1527                 /*
1528                  * Setting the last part of a previous oldext extent to newext.
1529                  * The right neighbor is not contiguous.
1530                  */
1531                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1532                 xfs_bmbt_set_blockcount(ep,
1533                         PREV.br_blockcount - new->br_blockcount);
1534                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1535
1536                 xfs_iext_insert(ip, idx + 1, 1, new, state);
1537                 ip->i_df.if_lastex = idx + 1;
1538                 ip->i_d.di_nextents++;
1539                 if (cur == NULL)
1540                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1541                 else {
1542                         rval = XFS_ILOG_CORE;
1543                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1544                                         PREV.br_startblock, PREV.br_blockcount,
1545                                         &i)))
1546                                 goto done;
1547                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1548                         if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1549                                 PREV.br_startblock,
1550                                 PREV.br_blockcount - new->br_blockcount,
1551                                 oldext)))
1552                                 goto done;
1553                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1554                                         new->br_startblock, new->br_blockcount,
1555                                         &i)))
1556                                 goto done;
1557                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1558                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
1559                         if ((error = xfs_btree_insert(cur, &i)))
1560                                 goto done;
1561                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1562                 }
1563                 break;
1564
1565         case 0:
1566                 /*
1567                  * Setting the middle part of a previous oldext extent to
1568                  * newext.  Contiguity is impossible here.
1569                  * One extent becomes three extents.
1570                  */
1571                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1572                 xfs_bmbt_set_blockcount(ep,
1573                         new->br_startoff - PREV.br_startoff);
1574                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1575
1576                 r[0] = *new;
1577                 r[1].br_startoff = new_endoff;
1578                 r[1].br_blockcount =
1579                         PREV.br_startoff + PREV.br_blockcount - new_endoff;
1580                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1581                 r[1].br_state = oldext;
1582                 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1583                 ip->i_df.if_lastex = idx + 1;
1584                 ip->i_d.di_nextents += 2;
1585                 if (cur == NULL)
1586                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1587                 else {
1588                         rval = XFS_ILOG_CORE;
1589                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1590                                         PREV.br_startblock, PREV.br_blockcount,
1591                                         &i)))
1592                                 goto done;
1593                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1594                         /* new right extent - oldext */
1595                         if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1596                                 r[1].br_startblock, r[1].br_blockcount,
1597                                 r[1].br_state)))
1598                                 goto done;
1599                         /* new left extent - oldext */
1600                         cur->bc_rec.b = PREV;
1601                         cur->bc_rec.b.br_blockcount =
1602                                 new->br_startoff - PREV.br_startoff;
1603                         if ((error = xfs_btree_insert(cur, &i)))
1604                                 goto done;
1605                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1606                         /*
1607                          * Reset the cursor to the position of the new extent
1608                          * we are about to insert as we can't trust it after
1609                          * the previous insert.
1610                          */
1611                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1612                                         new->br_startblock, new->br_blockcount,
1613                                         &i)))
1614                                 goto done;
1615                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1616                         /* new middle extent - newext */
1617                         cur->bc_rec.b.br_state = new->br_state;
1618                         if ((error = xfs_btree_insert(cur, &i)))
1619                                 goto done;
1620                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1621                 }
1622                 break;
1623
1624         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1625         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1626         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1627         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1628         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1629         case BMAP_LEFT_CONTIG:
1630         case BMAP_RIGHT_CONTIG:
1631                 /*
1632                  * These cases are all impossible.
1633                  */
1634                 ASSERT(0);
1635         }
1636         *curp = cur;
1637 done:
1638         *logflagsp = rval;
1639         return error;
1640 #undef  LEFT
1641 #undef  RIGHT
1642 #undef  PREV
1643 }
1644
1645 /*
1646  * Called by xfs_bmap_add_extent to handle cases converting a hole
1647  * to a delayed allocation.
1648  */
1649 /*ARGSUSED*/
1650 STATIC int                              /* error */
1651 xfs_bmap_add_extent_hole_delay(
1652         xfs_inode_t             *ip,    /* incore inode pointer */
1653         xfs_extnum_t            idx,    /* extent number to update/insert */
1654         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
1655         int                     *logflagsp, /* inode logging flags */
1656         int                     rsvd)           /* OK to allocate reserved blocks */
1657 {
1658         xfs_bmbt_rec_host_t     *ep;    /* extent record for idx */
1659         xfs_ifork_t             *ifp;   /* inode fork pointer */
1660         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
1661         xfs_filblks_t           newlen=0;       /* new indirect size */
1662         xfs_filblks_t           oldlen=0;       /* old indirect size */
1663         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
1664         int                     state;  /* state bits, accessed thru macros */
1665         xfs_filblks_t           temp=0; /* temp for indirect calculations */
1666
1667         ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1668         ep = xfs_iext_get_ext(ifp, idx);
1669         state = 0;
1670         ASSERT(isnullstartblock(new->br_startblock));
1671
1672         /*
1673          * Check and set flags if this segment has a left neighbor
1674          */
1675         if (idx > 0) {
1676                 state |= BMAP_LEFT_VALID;
1677                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1678
1679                 if (isnullstartblock(left.br_startblock))
1680                         state |= BMAP_LEFT_DELAY;
1681         }
1682
1683         /*
1684          * Check and set flags if the current (right) segment exists.
1685          * If it doesn't exist, we're converting the hole at end-of-file.
1686          */
1687         if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1688                 state |= BMAP_RIGHT_VALID;
1689                 xfs_bmbt_get_all(ep, &right);
1690
1691                 if (isnullstartblock(right.br_startblock))
1692                         state |= BMAP_RIGHT_DELAY;
1693         }
1694
1695         /*
1696          * Set contiguity flags on the left and right neighbors.
1697          * Don't let extents get too large, even if the pieces are contiguous.
1698          */
1699         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1700             left.br_startoff + left.br_blockcount == new->br_startoff &&
1701             left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1702                 state |= BMAP_LEFT_CONTIG;
1703
1704         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1705             new->br_startoff + new->br_blockcount == right.br_startoff &&
1706             new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1707             (!(state & BMAP_LEFT_CONTIG) ||
1708              (left.br_blockcount + new->br_blockcount +
1709               right.br_blockcount <= MAXEXTLEN)))
1710                 state |= BMAP_RIGHT_CONTIG;
1711
1712         /*
1713          * Switch out based on the contiguity flags.
1714          */
1715         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1716         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1717                 /*
1718                  * New allocation is contiguous with delayed allocations
1719                  * on the left and on the right.
1720                  * Merge all three into a single extent record.
1721                  */
1722                 temp = left.br_blockcount + new->br_blockcount +
1723                         right.br_blockcount;
1724
1725                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1726                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1727                 oldlen = startblockval(left.br_startblock) +
1728                         startblockval(new->br_startblock) +
1729                         startblockval(right.br_startblock);
1730                 newlen = xfs_bmap_worst_indlen(ip, temp);
1731                 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1732                         nullstartblock((int)newlen));
1733                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1734
1735                 xfs_iext_remove(ip, idx, 1, state);
1736                 ip->i_df.if_lastex = idx - 1;
1737                 break;
1738
1739         case BMAP_LEFT_CONTIG:
1740                 /*
1741                  * New allocation is contiguous with a delayed allocation
1742                  * on the left.
1743                  * Merge the new allocation with the left neighbor.
1744                  */
1745                 temp = left.br_blockcount + new->br_blockcount;
1746                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1747                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1748                 oldlen = startblockval(left.br_startblock) +
1749                         startblockval(new->br_startblock);
1750                 newlen = xfs_bmap_worst_indlen(ip, temp);
1751                 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1752                         nullstartblock((int)newlen));
1753                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1754
1755                 ip->i_df.if_lastex = idx - 1;
1756                 break;
1757
1758         case BMAP_RIGHT_CONTIG:
1759                 /*
1760                  * New allocation is contiguous with a delayed allocation
1761                  * on the right.
1762                  * Merge the new allocation with the right neighbor.
1763                  */
1764                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1765                 temp = new->br_blockcount + right.br_blockcount;
1766                 oldlen = startblockval(new->br_startblock) +
1767                         startblockval(right.br_startblock);
1768                 newlen = xfs_bmap_worst_indlen(ip, temp);
1769                 xfs_bmbt_set_allf(ep, new->br_startoff,
1770                         nullstartblock((int)newlen), temp, right.br_state);
1771                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1772
1773                 ip->i_df.if_lastex = idx;
1774                 break;
1775
1776         case 0:
1777                 /*
1778                  * New allocation is not contiguous with another
1779                  * delayed allocation.
1780                  * Insert a new entry.
1781                  */
1782                 oldlen = newlen = 0;
1783                 xfs_iext_insert(ip, idx, 1, new, state);
1784                 ip->i_df.if_lastex = idx;
1785                 break;
1786         }
1787         if (oldlen != newlen) {
1788                 ASSERT(oldlen > newlen);
1789                 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1790                         (int64_t)(oldlen - newlen), rsvd);
1791                 /*
1792                  * Nothing to do for disk quota accounting here.
1793                  */
1794         }
1795         *logflagsp = 0;
1796         return 0;
1797 }
1798
1799 /*
1800  * Called by xfs_bmap_add_extent to handle cases converting a hole
1801  * to a real allocation.
1802  */
1803 STATIC int                              /* error */
1804 xfs_bmap_add_extent_hole_real(
1805         xfs_inode_t             *ip,    /* incore inode pointer */
1806         xfs_extnum_t            idx,    /* extent number to update/insert */
1807         xfs_btree_cur_t         *cur,   /* if null, not a btree */
1808         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
1809         int                     *logflagsp, /* inode logging flags */
1810         int                     whichfork) /* data or attr fork */
1811 {
1812         xfs_bmbt_rec_host_t     *ep;    /* pointer to extent entry ins. point */
1813         int                     error;  /* error return value */
1814         int                     i;      /* temp state */
1815         xfs_ifork_t             *ifp;   /* inode fork pointer */
1816         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
1817         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
1818         int                     rval=0; /* return value (logging flags) */
1819         int                     state;  /* state bits, accessed thru macros */
1820
1821         ifp = XFS_IFORK_PTR(ip, whichfork);
1822         ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1823         ep = xfs_iext_get_ext(ifp, idx);
1824         state = 0;
1825
1826         if (whichfork == XFS_ATTR_FORK)
1827                 state |= BMAP_ATTRFORK;
1828
1829         /*
1830          * Check and set flags if this segment has a left neighbor.
1831          */
1832         if (idx > 0) {
1833                 state |= BMAP_LEFT_VALID;
1834                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1835                 if (isnullstartblock(left.br_startblock))
1836                         state |= BMAP_LEFT_DELAY;
1837         }
1838
1839         /*
1840          * Check and set flags if this segment has a current value.
1841          * Not true if we're inserting into the "hole" at eof.
1842          */
1843         if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1844                 state |= BMAP_RIGHT_VALID;
1845                 xfs_bmbt_get_all(ep, &right);
1846                 if (isnullstartblock(right.br_startblock))
1847                         state |= BMAP_RIGHT_DELAY;
1848         }
1849
1850         /*
1851          * We're inserting a real allocation between "left" and "right".
1852          * Set the contiguity flags.  Don't let extents get too large.
1853          */
1854         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1855             left.br_startoff + left.br_blockcount == new->br_startoff &&
1856             left.br_startblock + left.br_blockcount == new->br_startblock &&
1857             left.br_state == new->br_state &&
1858             left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1859                 state |= BMAP_LEFT_CONTIG;
1860
1861         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1862             new->br_startoff + new->br_blockcount == right.br_startoff &&
1863             new->br_startblock + new->br_blockcount == right.br_startblock &&
1864             new->br_state == right.br_state &&
1865             new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1866             (!(state & BMAP_LEFT_CONTIG) ||
1867              left.br_blockcount + new->br_blockcount +
1868              right.br_blockcount <= MAXEXTLEN))
1869                 state |= BMAP_RIGHT_CONTIG;
1870
1871         error = 0;
1872         /*
1873          * Select which case we're in here, and implement it.
1874          */
1875         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1876         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1877                 /*
1878                  * New allocation is contiguous with real allocations on the
1879                  * left and on the right.
1880                  * Merge all three into a single extent record.
1881                  */
1882                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1883                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1884                         left.br_blockcount + new->br_blockcount +
1885                         right.br_blockcount);
1886                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1887
1888                 xfs_iext_remove(ip, idx, 1, state);
1889                 ifp->if_lastex = idx - 1;
1890                 XFS_IFORK_NEXT_SET(ip, whichfork,
1891                         XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1892                 if (cur == NULL) {
1893                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1894                 } else {
1895                         rval = XFS_ILOG_CORE;
1896                         if ((error = xfs_bmbt_lookup_eq(cur,
1897                                         right.br_startoff,
1898                                         right.br_startblock,
1899                                         right.br_blockcount, &i)))
1900                                 goto done;
1901                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1902                         if ((error = xfs_btree_delete(cur, &i)))
1903                                 goto done;
1904                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1905                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1906                                 goto done;
1907                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1908                         if ((error = xfs_bmbt_update(cur, left.br_startoff,
1909                                         left.br_startblock,
1910                                         left.br_blockcount +
1911                                                 new->br_blockcount +
1912                                                 right.br_blockcount,
1913                                         left.br_state)))
1914                                 goto done;
1915                 }
1916                 break;
1917
1918         case BMAP_LEFT_CONTIG:
1919                 /*
1920                  * New allocation is contiguous with a real allocation
1921                  * on the left.
1922                  * Merge the new allocation with the left neighbor.
1923                  */
1924                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1925                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1926                         left.br_blockcount + new->br_blockcount);
1927                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1928
1929                 ifp->if_lastex = idx - 1;
1930                 if (cur == NULL) {
1931                         rval = xfs_ilog_fext(whichfork);
1932                 } else {
1933                         rval = 0;
1934                         if ((error = xfs_bmbt_lookup_eq(cur,
1935                                         left.br_startoff,
1936                                         left.br_startblock,
1937                                         left.br_blockcount, &i)))
1938                                 goto done;
1939                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1940                         if ((error = xfs_bmbt_update(cur, left.br_startoff,
1941                                         left.br_startblock,
1942                                         left.br_blockcount +
1943                                                 new->br_blockcount,
1944                                         left.br_state)))
1945                                 goto done;
1946                 }
1947                 break;
1948
1949         case BMAP_RIGHT_CONTIG:
1950                 /*
1951                  * New allocation is contiguous with a real allocation
1952                  * on the right.
1953                  * Merge the new allocation with the right neighbor.
1954                  */
1955                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1956                 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1957                         new->br_blockcount + right.br_blockcount,
1958                         right.br_state);
1959                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1960
1961                 ifp->if_lastex = idx;
1962                 if (cur == NULL) {
1963                         rval = xfs_ilog_fext(whichfork);
1964                 } else {
1965                         rval = 0;
1966                         if ((error = xfs_bmbt_lookup_eq(cur,
1967                                         right.br_startoff,
1968                                         right.br_startblock,
1969                                         right.br_blockcount, &i)))
1970                                 goto done;
1971                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1972                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1973                                         new->br_startblock,
1974                                         new->br_blockcount +
1975                                                 right.br_blockcount,
1976                                         right.br_state)))
1977                                 goto done;
1978                 }
1979                 break;
1980
1981         case 0:
1982                 /*
1983                  * New allocation is not contiguous with another
1984                  * real allocation.
1985                  * Insert a new entry.
1986                  */
1987                 xfs_iext_insert(ip, idx, 1, new, state);
1988                 ifp->if_lastex = idx;
1989                 XFS_IFORK_NEXT_SET(ip, whichfork,
1990                         XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1991                 if (cur == NULL) {
1992                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1993                 } else {
1994                         rval = XFS_ILOG_CORE;
1995                         if ((error = xfs_bmbt_lookup_eq(cur,
1996                                         new->br_startoff,
1997                                         new->br_startblock,
1998                                         new->br_blockcount, &i)))
1999                                 goto done;
2000                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2001                         cur->bc_rec.b.br_state = new->br_state;
2002                         if ((error = xfs_btree_insert(cur, &i)))
2003                                 goto done;
2004                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2005                 }
2006                 break;
2007         }
2008 done:
2009         *logflagsp = rval;
2010         return error;
2011 }
2012
2013 /*
2014  * Adjust the size of the new extent based on di_extsize and rt extsize.
2015  */
2016 STATIC int
2017 xfs_bmap_extsize_align(
2018         xfs_mount_t     *mp,
2019         xfs_bmbt_irec_t *gotp,          /* next extent pointer */
2020         xfs_bmbt_irec_t *prevp,         /* previous extent pointer */
2021         xfs_extlen_t    extsz,          /* align to this extent size */
2022         int             rt,             /* is this a realtime inode? */
2023         int             eof,            /* is extent at end-of-file? */
2024         int             delay,          /* creating delalloc extent? */
2025         int             convert,        /* overwriting unwritten extent? */
2026         xfs_fileoff_t   *offp,          /* in/out: aligned offset */
2027         xfs_extlen_t    *lenp)          /* in/out: aligned length */
2028 {
2029         xfs_fileoff_t   orig_off;       /* original offset */
2030         xfs_extlen_t    orig_alen;      /* original length */
2031         xfs_fileoff_t   orig_end;       /* original off+len */
2032         xfs_fileoff_t   nexto;          /* next file offset */
2033         xfs_fileoff_t   prevo;          /* previous file offset */
2034         xfs_fileoff_t   align_off;      /* temp for offset */
2035         xfs_extlen_t    align_alen;     /* temp for length */
2036         xfs_extlen_t    temp;           /* temp for calculations */
2037
2038         if (convert)
2039                 return 0;
2040
2041         orig_off = align_off = *offp;
2042         orig_alen = align_alen = *lenp;
2043         orig_end = orig_off + orig_alen;
2044
2045         /*
2046          * If this request overlaps an existing extent, then don't
2047          * attempt to perform any additional alignment.
2048          */
2049         if (!delay && !eof &&
2050             (orig_off >= gotp->br_startoff) &&
2051             (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2052                 return 0;
2053         }
2054
2055         /*
2056          * If the file offset is unaligned vs. the extent size
2057          * we need to align it.  This will be possible unless
2058          * the file was previously written with a kernel that didn't
2059          * perform this alignment, or if a truncate shot us in the
2060          * foot.
2061          */
2062         temp = do_mod(orig_off, extsz);
2063         if (temp) {
2064                 align_alen += temp;
2065                 align_off -= temp;
2066         }
2067         /*
2068          * Same adjustment for the end of the requested area.
2069          */
2070         if ((temp = (align_alen % extsz))) {
2071                 align_alen += extsz - temp;
2072         }
2073         /*
2074          * If the previous block overlaps with this proposed allocation
2075          * then move the start forward without adjusting the length.
2076          */
2077         if (prevp->br_startoff != NULLFILEOFF) {
2078                 if (prevp->br_startblock == HOLESTARTBLOCK)
2079                         prevo = prevp->br_startoff;
2080                 else
2081                         prevo = prevp->br_startoff + prevp->br_blockcount;
2082         } else
2083                 prevo = 0;
2084         if (align_off != orig_off && align_off < prevo)
2085                 align_off = prevo;
2086         /*
2087          * If the next block overlaps with this proposed allocation
2088          * then move the start back without adjusting the length,
2089          * but not before offset 0.
2090          * This may of course make the start overlap previous block,
2091          * and if we hit the offset 0 limit then the next block
2092          * can still overlap too.
2093          */
2094         if (!eof && gotp->br_startoff != NULLFILEOFF) {
2095                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2096                     (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2097                         nexto = gotp->br_startoff + gotp->br_blockcount;
2098                 else
2099                         nexto = gotp->br_startoff;
2100         } else
2101                 nexto = NULLFILEOFF;
2102         if (!eof &&
2103             align_off + align_alen != orig_end &&
2104             align_off + align_alen > nexto)
2105                 align_off = nexto > align_alen ? nexto - align_alen : 0;
2106         /*
2107          * If we're now overlapping the next or previous extent that
2108          * means we can't fit an extsz piece in this hole.  Just move
2109          * the start forward to the first valid spot and set
2110          * the length so we hit the end.
2111          */
2112         if (align_off != orig_off && align_off < prevo)
2113                 align_off = prevo;
2114         if (align_off + align_alen != orig_end &&
2115             align_off + align_alen > nexto &&
2116             nexto != NULLFILEOFF) {
2117                 ASSERT(nexto > prevo);
2118                 align_alen = nexto - align_off;
2119         }
2120
2121         /*
2122          * If realtime, and the result isn't a multiple of the realtime
2123          * extent size we need to remove blocks until it is.
2124          */
2125         if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2126                 /*
2127                  * We're not covering the original request, or
2128                  * we won't be able to once we fix the length.
2129                  */
2130                 if (orig_off < align_off ||
2131                     orig_end > align_off + align_alen ||
2132                     align_alen - temp < orig_alen)
2133                         return XFS_ERROR(EINVAL);
2134                 /*
2135                  * Try to fix it by moving the start up.
2136                  */
2137                 if (align_off + temp <= orig_off) {
2138                         align_alen -= temp;
2139                         align_off += temp;
2140                 }
2141                 /*
2142                  * Try to fix it by moving the end in.
2143                  */
2144                 else if (align_off + align_alen - temp >= orig_end)
2145                         align_alen -= temp;
2146                 /*
2147                  * Set the start to the minimum then trim the length.
2148                  */
2149                 else {
2150                         align_alen -= orig_off - align_off;
2151                         align_off = orig_off;
2152                         align_alen -= align_alen % mp->m_sb.sb_rextsize;
2153                 }
2154                 /*
2155                  * Result doesn't cover the request, fail it.
2156                  */
2157                 if (orig_off < align_off || orig_end > align_off + align_alen)
2158                         return XFS_ERROR(EINVAL);
2159         } else {
2160                 ASSERT(orig_off >= align_off);
2161                 ASSERT(orig_end <= align_off + align_alen);
2162         }
2163
2164 #ifdef DEBUG
2165         if (!eof && gotp->br_startoff != NULLFILEOFF)
2166                 ASSERT(align_off + align_alen <= gotp->br_startoff);
2167         if (prevp->br_startoff != NULLFILEOFF)
2168                 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2169 #endif
2170
2171         *lenp = align_alen;
2172         *offp = align_off;
2173         return 0;
2174 }
2175
2176 #define XFS_ALLOC_GAP_UNITS     4
2177
2178 STATIC void
2179 xfs_bmap_adjacent(
2180         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2181 {
2182         xfs_fsblock_t   adjust;         /* adjustment to block numbers */
2183         xfs_agnumber_t  fb_agno;        /* ag number of ap->firstblock */
2184         xfs_mount_t     *mp;            /* mount point structure */
2185         int             nullfb;         /* true if ap->firstblock isn't set */
2186         int             rt;             /* true if inode is realtime */
2187
2188 #define ISVALID(x,y)    \
2189         (rt ? \
2190                 (x) < mp->m_sb.sb_rblocks : \
2191                 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2192                 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2193                 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2194
2195         mp = ap->ip->i_mount;
2196         nullfb = ap->firstblock == NULLFSBLOCK;
2197         rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2198         fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2199         /*
2200          * If allocating at eof, and there's a previous real block,
2201          * try to use its last block as our starting point.
2202          */
2203         if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2204             !isnullstartblock(ap->prevp->br_startblock) &&
2205             ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2206                     ap->prevp->br_startblock)) {
2207                 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2208                 /*
2209                  * Adjust for the gap between prevp and us.
2210                  */
2211                 adjust = ap->off -
2212                         (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2213                 if (adjust &&
2214                     ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2215                         ap->rval += adjust;
2216         }
2217         /*
2218          * If not at eof, then compare the two neighbor blocks.
2219          * Figure out whether either one gives us a good starting point,
2220          * and pick the better one.
2221          */
2222         else if (!ap->eof) {
2223                 xfs_fsblock_t   gotbno;         /* right side block number */
2224                 xfs_fsblock_t   gotdiff=0;      /* right side difference */
2225                 xfs_fsblock_t   prevbno;        /* left side block number */
2226                 xfs_fsblock_t   prevdiff=0;     /* left side difference */
2227
2228                 /*
2229                  * If there's a previous (left) block, select a requested
2230                  * start block based on it.
2231                  */
2232                 if (ap->prevp->br_startoff != NULLFILEOFF &&
2233                     !isnullstartblock(ap->prevp->br_startblock) &&
2234                     (prevbno = ap->prevp->br_startblock +
2235                                ap->prevp->br_blockcount) &&
2236                     ISVALID(prevbno, ap->prevp->br_startblock)) {
2237                         /*
2238                          * Calculate gap to end of previous block.
2239                          */
2240                         adjust = prevdiff = ap->off -
2241                                 (ap->prevp->br_startoff +
2242                                  ap->prevp->br_blockcount);
2243                         /*
2244                          * Figure the startblock based on the previous block's
2245                          * end and the gap size.
2246                          * Heuristic!
2247                          * If the gap is large relative to the piece we're
2248                          * allocating, or using it gives us an invalid block
2249                          * number, then just use the end of the previous block.
2250                          */
2251                         if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2252                             ISVALID(prevbno + prevdiff,
2253                                     ap->prevp->br_startblock))
2254                                 prevbno += adjust;
2255                         else
2256                                 prevdiff += adjust;
2257                         /*
2258                          * If the firstblock forbids it, can't use it,
2259                          * must use default.
2260                          */
2261                         if (!rt && !nullfb &&
2262                             XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2263                                 prevbno = NULLFSBLOCK;
2264                 }
2265                 /*
2266                  * No previous block or can't follow it, just default.
2267                  */
2268                 else
2269                         prevbno = NULLFSBLOCK;
2270                 /*
2271                  * If there's a following (right) block, select a requested
2272                  * start block based on it.
2273                  */
2274                 if (!isnullstartblock(ap->gotp->br_startblock)) {
2275                         /*
2276                          * Calculate gap to start of next block.
2277                          */
2278                         adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2279                         /*
2280                          * Figure the startblock based on the next block's
2281                          * start and the gap size.
2282                          */
2283                         gotbno = ap->gotp->br_startblock;
2284                         /*
2285                          * Heuristic!
2286                          * If the gap is large relative to the piece we're
2287                          * allocating, or using it gives us an invalid block
2288                          * number, then just use the start of the next block
2289                          * offset by our length.
2290                          */
2291                         if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2292                             ISVALID(gotbno - gotdiff, gotbno))
2293                                 gotbno -= adjust;
2294                         else if (ISVALID(gotbno - ap->alen, gotbno)) {
2295                                 gotbno -= ap->alen;
2296                                 gotdiff += adjust - ap->alen;
2297                         } else
2298                                 gotdiff += adjust;
2299                         /*
2300                          * If the firstblock forbids it, can't use it,
2301                          * must use default.
2302                          */
2303                         if (!rt && !nullfb &&
2304                             XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2305                                 gotbno = NULLFSBLOCK;
2306                 }
2307                 /*
2308                  * No next block, just default.
2309                  */
2310                 else
2311                         gotbno = NULLFSBLOCK;
2312                 /*
2313                  * If both valid, pick the better one, else the only good
2314                  * one, else ap->rval is already set (to 0 or the inode block).
2315                  */
2316                 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2317                         ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2318                 else if (prevbno != NULLFSBLOCK)
2319                         ap->rval = prevbno;
2320                 else if (gotbno != NULLFSBLOCK)
2321                         ap->rval = gotbno;
2322         }
2323 #undef ISVALID
2324 }
2325
2326 STATIC int
2327 xfs_bmap_rtalloc(
2328         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2329 {
2330         xfs_alloctype_t atype = 0;      /* type for allocation routines */
2331         int             error;          /* error return value */
2332         xfs_mount_t     *mp;            /* mount point structure */
2333         xfs_extlen_t    prod = 0;       /* product factor for allocators */
2334         xfs_extlen_t    ralen = 0;      /* realtime allocation length */
2335         xfs_extlen_t    align;          /* minimum allocation alignment */
2336         xfs_rtblock_t   rtb;
2337
2338         mp = ap->ip->i_mount;
2339         align = xfs_get_extsz_hint(ap->ip);
2340         prod = align / mp->m_sb.sb_rextsize;
2341         error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2342                                         align, 1, ap->eof, 0,
2343                                         ap->conv, &ap->off, &ap->alen);
2344         if (error)
2345                 return error;
2346         ASSERT(ap->alen);
2347         ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2348
2349         /*
2350          * If the offset & length are not perfectly aligned
2351          * then kill prod, it will just get us in trouble.
2352          */
2353         if (do_mod(ap->off, align) || ap->alen % align)
2354                 prod = 1;
2355         /*
2356          * Set ralen to be the actual requested length in rtextents.
2357          */
2358         ralen = ap->alen / mp->m_sb.sb_rextsize;
2359         /*
2360          * If the old value was close enough to MAXEXTLEN that
2361          * we rounded up to it, cut it back so it's valid again.
2362          * Note that if it's a really large request (bigger than
2363          * MAXEXTLEN), we don't hear about that number, and can't
2364          * adjust the starting point to match it.
2365          */
2366         if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2367                 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2368
2369         /*
2370          * Lock out other modifications to the RT bitmap inode.
2371          */
2372         xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2373         xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2374
2375         /*
2376          * If it's an allocation to an empty file at offset 0,
2377          * pick an extent that will space things out in the rt area.
2378          */
2379         if (ap->eof && ap->off == 0) {
2380                 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2381
2382                 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2383                 if (error)
2384                         return error;
2385                 ap->rval = rtx * mp->m_sb.sb_rextsize;
2386         } else {
2387                 ap->rval = 0;
2388         }
2389
2390         xfs_bmap_adjacent(ap);
2391
2392         /*
2393          * Realtime allocation, done through xfs_rtallocate_extent.
2394          */
2395         atype = ap->rval == 0 ?  XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2396         do_div(ap->rval, mp->m_sb.sb_rextsize);
2397         rtb = ap->rval;
2398         ap->alen = ralen;
2399         if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2400                                 &ralen, atype, ap->wasdel, prod, &rtb)))
2401                 return error;
2402         if (rtb == NULLFSBLOCK && prod > 1 &&
2403             (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2404                                            ap->alen, &ralen, atype,
2405                                            ap->wasdel, 1, &rtb)))
2406                 return error;
2407         ap->rval = rtb;
2408         if (ap->rval != NULLFSBLOCK) {
2409                 ap->rval *= mp->m_sb.sb_rextsize;
2410                 ralen *= mp->m_sb.sb_rextsize;
2411                 ap->alen = ralen;
2412                 ap->ip->i_d.di_nblocks += ralen;
2413                 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2414                 if (ap->wasdel)
2415                         ap->ip->i_delayed_blks -= ralen;
2416                 /*
2417                  * Adjust the disk quota also. This was reserved
2418                  * earlier.
2419                  */
2420                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2421                         ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2422                                         XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2423         } else {
2424                 ap->alen = 0;
2425         }
2426         return 0;
2427 }
2428
2429 STATIC int
2430 xfs_bmap_btalloc_nullfb(
2431         struct xfs_bmalloca     *ap,
2432         struct xfs_alloc_arg    *args,
2433         xfs_extlen_t            *blen)
2434 {
2435         struct xfs_mount        *mp = ap->ip->i_mount;
2436         struct xfs_perag        *pag;
2437         xfs_agnumber_t          ag, startag;
2438         int                     notinit = 0;
2439         int                     error;
2440
2441         if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2442                 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2443         else
2444                 args->type = XFS_ALLOCTYPE_START_BNO;
2445         args->total = ap->total;
2446
2447         /*
2448          * Search for an allocation group with a single extent large enough
2449          * for the request.  If one isn't found, then adjust the minimum
2450          * allocation size to the largest space found.
2451          */
2452         startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2453         if (startag == NULLAGNUMBER)
2454                 startag = ag = 0;
2455
2456         pag = xfs_perag_get(mp, ag);
2457         while (*blen < args->maxlen) {
2458                 if (!pag->pagf_init) {
2459                         error = xfs_alloc_pagf_init(mp, args->tp, ag,
2460                                                     XFS_ALLOC_FLAG_TRYLOCK);
2461                         if (error) {
2462                                 xfs_perag_put(pag);
2463                                 return error;
2464                         }
2465                 }
2466
2467                 /*
2468                  * See xfs_alloc_fix_freelist...
2469                  */
2470                 if (pag->pagf_init) {
2471                         xfs_extlen_t    longest;
2472                         longest = xfs_alloc_longest_free_extent(mp, pag);
2473                         if (*blen < longest)
2474                                 *blen = longest;
2475                 } else
2476                         notinit = 1;
2477
2478                 if (xfs_inode_is_filestream(ap->ip)) {
2479                         if (*blen >= args->maxlen)
2480                                 break;
2481
2482                         if (ap->userdata) {
2483                                 /*
2484                                  * If startag is an invalid AG, we've
2485                                  * come here once before and
2486                                  * xfs_filestream_new_ag picked the
2487                                  * best currently available.
2488                                  *
2489                                  * Don't continue looping, since we
2490                                  * could loop forever.
2491                                  */
2492                                 if (startag == NULLAGNUMBER)
2493                                         break;
2494
2495                                 error = xfs_filestream_new_ag(ap, &ag);
2496                                 xfs_perag_put(pag);
2497                                 if (error)
2498                                         return error;
2499
2500                                 /* loop again to set 'blen'*/
2501                                 startag = NULLAGNUMBER;
2502                                 pag = xfs_perag_get(mp, ag);
2503                                 continue;
2504                         }
2505                 }
2506                 if (++ag == mp->m_sb.sb_agcount)
2507                         ag = 0;
2508                 if (ag == startag)
2509                         break;
2510                 xfs_perag_put(pag);
2511                 pag = xfs_perag_get(mp, ag);
2512         }
2513         xfs_perag_put(pag);
2514
2515         /*
2516          * Since the above loop did a BUF_TRYLOCK, it is
2517          * possible that there is space for this request.
2518          */
2519         if (notinit || *blen < ap->minlen)
2520                 args->minlen = ap->minlen;
2521         /*
2522          * If the best seen length is less than the request
2523          * length, use the best as the minimum.
2524          */
2525         else if (*blen < args->maxlen)
2526                 args->minlen = *blen;
2527         /*
2528          * Otherwise we've seen an extent as big as maxlen,
2529          * use that as the minimum.
2530          */
2531         else
2532                 args->minlen = args->maxlen;
2533
2534         /*
2535          * set the failure fallback case to look in the selected
2536          * AG as the stream may have moved.
2537          */
2538         if (xfs_inode_is_filestream(ap->ip))
2539                 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2540
2541         return 0;
2542 }
2543
2544 STATIC int
2545 xfs_bmap_btalloc(
2546         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2547 {
2548         xfs_mount_t     *mp;            /* mount point structure */
2549         xfs_alloctype_t atype = 0;      /* type for allocation routines */
2550         xfs_extlen_t    align;          /* minimum allocation alignment */
2551         xfs_agnumber_t  fb_agno;        /* ag number of ap->firstblock */
2552         xfs_agnumber_t  ag;
2553         xfs_alloc_arg_t args;
2554         xfs_extlen_t    blen;
2555         xfs_extlen_t    nextminlen = 0;
2556         int             nullfb;         /* true if ap->firstblock isn't set */
2557         int             isaligned;
2558         int             tryagain;
2559         int             error;
2560
2561         mp = ap->ip->i_mount;
2562         align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2563         if (unlikely(align)) {
2564                 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2565                                                 align, 0, ap->eof, 0, ap->conv,
2566                                                 &ap->off, &ap->alen);
2567                 ASSERT(!error);
2568                 ASSERT(ap->alen);
2569         }
2570         nullfb = ap->firstblock == NULLFSBLOCK;
2571         fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2572         if (nullfb) {
2573                 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2574                         ag = xfs_filestream_lookup_ag(ap->ip);
2575                         ag = (ag != NULLAGNUMBER) ? ag : 0;
2576                         ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2577                 } else {
2578                         ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2579                 }
2580         } else
2581                 ap->rval = ap->firstblock;
2582
2583         xfs_bmap_adjacent(ap);
2584
2585         /*
2586          * If allowed, use ap->rval; otherwise must use firstblock since
2587          * it's in the right allocation group.
2588          */
2589         if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2590                 ;
2591         else
2592                 ap->rval = ap->firstblock;
2593         /*
2594          * Normal allocation, done through xfs_alloc_vextent.
2595          */
2596         tryagain = isaligned = 0;
2597         args.tp = ap->tp;
2598         args.mp = mp;
2599         args.fsbno = ap->rval;
2600
2601         /* Trim the allocation back to the maximum an AG can fit. */
2602         args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
2603         args.firstblock = ap->firstblock;
2604         blen = 0;
2605         if (nullfb) {
2606                 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2607                 if (error)
2608                         return error;
2609         } else if (ap->low) {
2610                 if (xfs_inode_is_filestream(ap->ip))
2611                         args.type = XFS_ALLOCTYPE_FIRST_AG;
2612                 else
2613                         args.type = XFS_ALLOCTYPE_START_BNO;
2614                 args.total = args.minlen = ap->minlen;
2615         } else {
2616                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2617                 args.total = ap->total;
2618                 args.minlen = ap->minlen;
2619         }
2620         /* apply extent size hints if obtained earlier */
2621         if (unlikely(align)) {
2622                 args.prod = align;
2623                 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2624                         args.mod = (xfs_extlen_t)(args.prod - args.mod);
2625         } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2626                 args.prod = 1;
2627                 args.mod = 0;
2628         } else {
2629                 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2630                 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2631                         args.mod = (xfs_extlen_t)(args.prod - args.mod);
2632         }
2633         /*
2634          * If we are not low on available data blocks, and the
2635          * underlying logical volume manager is a stripe, and
2636          * the file offset is zero then try to allocate data
2637          * blocks on stripe unit boundary.
2638          * NOTE: ap->aeof is only set if the allocation length
2639          * is >= the stripe unit and the allocation offset is
2640          * at the end of file.
2641          */
2642         if (!ap->low && ap->aeof) {
2643                 if (!ap->off) {
2644                         args.alignment = mp->m_dalign;
2645                         atype = args.type;
2646                         isaligned = 1;
2647                         /*
2648                          * Adjust for alignment
2649                          */
2650                         if (blen > args.alignment && blen <= args.maxlen)
2651                                 args.minlen = blen - args.alignment;
2652                         args.minalignslop = 0;
2653                 } else {
2654                         /*
2655                          * First try an exact bno allocation.
2656                          * If it fails then do a near or start bno
2657                          * allocation with alignment turned on.
2658                          */
2659                         atype = args.type;
2660                         tryagain = 1;
2661                         args.type = XFS_ALLOCTYPE_THIS_BNO;
2662                         args.alignment = 1;
2663                         /*
2664                          * Compute the minlen+alignment for the
2665                          * next case.  Set slop so that the value
2666                          * of minlen+alignment+slop doesn't go up
2667                          * between the calls.
2668                          */
2669                         if (blen > mp->m_dalign && blen <= args.maxlen)
2670                                 nextminlen = blen - mp->m_dalign;
2671                         else
2672                                 nextminlen = args.minlen;
2673                         if (nextminlen + mp->m_dalign > args.minlen + 1)
2674                                 args.minalignslop =
2675                                         nextminlen + mp->m_dalign -
2676                                         args.minlen - 1;
2677                         else
2678                                 args.minalignslop = 0;
2679                 }
2680         } else {
2681                 args.alignment = 1;
2682                 args.minalignslop = 0;
2683         }
2684         args.minleft = ap->minleft;
2685         args.wasdel = ap->wasdel;
2686         args.isfl = 0;
2687         args.userdata = ap->userdata;
2688         if ((error = xfs_alloc_vextent(&args)))
2689                 return error;
2690         if (tryagain && args.fsbno == NULLFSBLOCK) {
2691                 /*
2692                  * Exact allocation failed. Now try with alignment
2693                  * turned on.
2694                  */
2695                 args.type = atype;
2696                 args.fsbno = ap->rval;
2697                 args.alignment = mp->m_dalign;
2698                 args.minlen = nextminlen;
2699                 args.minalignslop = 0;
2700                 isaligned = 1;
2701                 if ((error = xfs_alloc_vextent(&args)))
2702                         return error;
2703         }
2704         if (isaligned && args.fsbno == NULLFSBLOCK) {
2705                 /*
2706                  * allocation failed, so turn off alignment and
2707                  * try again.
2708                  */
2709                 args.type = atype;
2710                 args.fsbno = ap->rval;
2711                 args.alignment = 0;
2712                 if ((error = xfs_alloc_vextent(&args)))
2713                         return error;
2714         }
2715         if (args.fsbno == NULLFSBLOCK && nullfb &&
2716             args.minlen > ap->minlen) {
2717                 args.minlen = ap->minlen;
2718                 args.type = XFS_ALLOCTYPE_START_BNO;
2719                 args.fsbno = ap->rval;
2720                 if ((error = xfs_alloc_vextent(&args)))
2721                         return error;
2722         }
2723         if (args.fsbno == NULLFSBLOCK && nullfb) {
2724                 args.fsbno = 0;
2725                 args.type = XFS_ALLOCTYPE_FIRST_AG;
2726                 args.total = ap->minlen;
2727                 args.minleft = 0;
2728                 if ((error = xfs_alloc_vextent(&args)))
2729                         return error;
2730                 ap->low = 1;
2731         }
2732         if (args.fsbno != NULLFSBLOCK) {
2733                 ap->firstblock = ap->rval = args.fsbno;
2734                 ASSERT(nullfb || fb_agno == args.agno ||
2735                        (ap->low && fb_agno < args.agno));
2736                 ap->alen = args.len;
2737                 ap->ip->i_d.di_nblocks += args.len;
2738                 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2739                 if (ap->wasdel)
2740                         ap->ip->i_delayed_blks -= args.len;
2741                 /*
2742                  * Adjust the disk quota also. This was reserved
2743                  * earlier.
2744                  */
2745                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2746                         ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2747                                         XFS_TRANS_DQ_BCOUNT,
2748                         (long) args.len);
2749         } else {
2750                 ap->rval = NULLFSBLOCK;
2751                 ap->alen = 0;
2752         }
2753         return 0;
2754 }
2755
2756 /*
2757  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2758  * It figures out where to ask the underlying allocator to put the new extent.
2759  */
2760 STATIC int
2761 xfs_bmap_alloc(
2762         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2763 {
2764         if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2765                 return xfs_bmap_rtalloc(ap);
2766         return xfs_bmap_btalloc(ap);
2767 }
2768
2769 /*
2770  * Transform a btree format file with only one leaf node, where the
2771  * extents list will fit in the inode, into an extents format file.
2772  * Since the file extents are already in-core, all we have to do is
2773  * give up the space for the btree root and pitch the leaf block.
2774  */
2775 STATIC int                              /* error */
2776 xfs_bmap_btree_to_extents(
2777         xfs_trans_t             *tp,    /* transaction pointer */
2778         xfs_inode_t             *ip,    /* incore inode pointer */
2779         xfs_btree_cur_t         *cur,   /* btree cursor */
2780         int                     *logflagsp, /* inode logging flags */
2781         int                     whichfork)  /* data or attr fork */
2782 {
2783         /* REFERENCED */
2784         struct xfs_btree_block  *cblock;/* child btree block */
2785         xfs_fsblock_t           cbno;   /* child block number */
2786         xfs_buf_t               *cbp;   /* child block's buffer */
2787         int                     error;  /* error return value */
2788         xfs_ifork_t             *ifp;   /* inode fork data */
2789         xfs_mount_t             *mp;    /* mount point structure */
2790         __be64                  *pp;    /* ptr to block address */
2791         struct xfs_btree_block  *rblock;/* root btree block */
2792
2793         mp = ip->i_mount;
2794         ifp = XFS_IFORK_PTR(ip, whichfork);
2795         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2796         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2797         rblock = ifp->if_broot;
2798         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2799         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2800         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2801         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2802         cbno = be64_to_cpu(*pp);
2803         *logflagsp = 0;
2804 #ifdef DEBUG
2805         if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2806                 return error;
2807 #endif
2808         if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2809                         XFS_BMAP_BTREE_REF)))
2810                 return error;
2811         cblock = XFS_BUF_TO_BLOCK(cbp);
2812         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2813                 return error;
2814         xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2815         ip->i_d.di_nblocks--;
2816         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2817         xfs_trans_binval(tp, cbp);
2818         if (cur->bc_bufs[0] == cbp)
2819                 cur->bc_bufs[0] = NULL;
2820         xfs_iroot_realloc(ip, -1, whichfork);
2821         ASSERT(ifp->if_broot == NULL);
2822         ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2823         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2824         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2825         return 0;