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