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