]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/xfs/xfs_bmap.c
a9b95d9cf2ad2b0a56331370b76dccf5576b3f27
[net-next-2.6.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 #include "xfs_trace.h"
58
59
60 #ifdef DEBUG
61 STATIC void
62 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
63 #endif
64
65 kmem_zone_t             *xfs_bmap_free_item_zone;
66
67 /*
68  * Prototypes for internal bmap routines.
69  */
70
71
72 /*
73  * Called from xfs_bmap_add_attrfork to handle extents format files.
74  */
75 STATIC int                                      /* error */
76 xfs_bmap_add_attrfork_extents(
77         xfs_trans_t             *tp,            /* transaction pointer */
78         xfs_inode_t             *ip,            /* incore inode pointer */
79         xfs_fsblock_t           *firstblock,    /* first block allocated */
80         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
81         int                     *flags);        /* inode logging flags */
82
83 /*
84  * Called from xfs_bmap_add_attrfork to handle local format files.
85  */
86 STATIC int                                      /* error */
87 xfs_bmap_add_attrfork_local(
88         xfs_trans_t             *tp,            /* transaction pointer */
89         xfs_inode_t             *ip,            /* incore inode pointer */
90         xfs_fsblock_t           *firstblock,    /* first block allocated */
91         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
92         int                     *flags);        /* inode logging flags */
93
94 /*
95  * Called by xfs_bmapi to update file extent records and the btree
96  * after allocating space (or doing a delayed allocation).
97  */
98 STATIC int                              /* error */
99 xfs_bmap_add_extent(
100         xfs_inode_t             *ip,    /* incore inode pointer */
101         xfs_extnum_t            idx,    /* extent number to update/insert */
102         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
103         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
104         xfs_fsblock_t           *first, /* pointer to firstblock variable */
105         xfs_bmap_free_t         *flist, /* list of extents to be freed */
106         int                     *logflagsp, /* inode logging flags */
107         xfs_extdelta_t          *delta, /* Change made to incore extents */
108         int                     whichfork, /* data or attr fork */
109         int                     rsvd);  /* OK to allocate reserved blocks */
110
111 /*
112  * Called by xfs_bmap_add_extent to handle cases converting a delayed
113  * allocation to a real allocation.
114  */
115 STATIC int                              /* error */
116 xfs_bmap_add_extent_delay_real(
117         xfs_inode_t             *ip,    /* incore inode pointer */
118         xfs_extnum_t            idx,    /* extent number to update/insert */
119         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
120         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
121         xfs_filblks_t           *dnew,  /* new delayed-alloc indirect blocks */
122         xfs_fsblock_t           *first, /* pointer to firstblock variable */
123         xfs_bmap_free_t         *flist, /* list of extents to be freed */
124         int                     *logflagsp, /* inode logging flags */
125         xfs_extdelta_t          *delta, /* Change made to incore extents */
126         int                     rsvd);  /* OK to allocate reserved blocks */
127
128 /*
129  * Called by xfs_bmap_add_extent to handle cases converting a hole
130  * to a delayed allocation.
131  */
132 STATIC int                              /* error */
133 xfs_bmap_add_extent_hole_delay(
134         xfs_inode_t             *ip,    /* incore inode pointer */
135         xfs_extnum_t            idx,    /* extent number to update/insert */
136         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
137         int                     *logflagsp,/* inode logging flags */
138         xfs_extdelta_t          *delta, /* Change made to incore extents */
139         int                     rsvd);  /* OK to allocate reserved blocks */
140
141 /*
142  * Called by xfs_bmap_add_extent to handle cases converting a hole
143  * to a real allocation.
144  */
145 STATIC int                              /* error */
146 xfs_bmap_add_extent_hole_real(
147         xfs_inode_t             *ip,    /* incore inode pointer */
148         xfs_extnum_t            idx,    /* extent number to update/insert */
149         xfs_btree_cur_t         *cur,   /* if null, not a btree */
150         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
151         int                     *logflagsp, /* inode logging flags */
152         xfs_extdelta_t          *delta, /* Change made to incore extents */
153         int                     whichfork); /* data or attr fork */
154
155 /*
156  * Called by xfs_bmap_add_extent to handle cases converting an unwritten
157  * allocation to a real allocation or vice versa.
158  */
159 STATIC int                              /* error */
160 xfs_bmap_add_extent_unwritten_real(
161         xfs_inode_t             *ip,    /* incore inode pointer */
162         xfs_extnum_t            idx,    /* extent number to update/insert */
163         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
164         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
165         int                     *logflagsp, /* inode logging flags */
166         xfs_extdelta_t          *delta); /* Change made to incore extents */
167
168 /*
169  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
170  * It figures out where to ask the underlying allocator to put the new extent.
171  */
172 STATIC int                              /* error */
173 xfs_bmap_alloc(
174         xfs_bmalloca_t          *ap);   /* bmap alloc argument struct */
175
176 /*
177  * Transform a btree format file with only one leaf node, where the
178  * extents list will fit in the inode, into an extents format file.
179  * Since the file extents are already in-core, all we have to do is
180  * give up the space for the btree root and pitch the leaf block.
181  */
182 STATIC int                              /* error */
183 xfs_bmap_btree_to_extents(
184         xfs_trans_t             *tp,    /* transaction pointer */
185         xfs_inode_t             *ip,    /* incore inode pointer */
186         xfs_btree_cur_t         *cur,   /* btree cursor */
187         int                     *logflagsp, /* inode logging flags */
188         int                     whichfork); /* data or attr fork */
189
190 /*
191  * Called by xfs_bmapi to update file extent records and the btree
192  * after removing space (or undoing a delayed allocation).
193  */
194 STATIC int                              /* error */
195 xfs_bmap_del_extent(
196         xfs_inode_t             *ip,    /* incore inode pointer */
197         xfs_trans_t             *tp,    /* current trans pointer */
198         xfs_extnum_t            idx,    /* extent number to update/insert */
199         xfs_bmap_free_t         *flist, /* list of extents to be freed */
200         xfs_btree_cur_t         *cur,   /* if null, not a btree */
201         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
202         int                     *logflagsp,/* inode logging flags */
203         xfs_extdelta_t          *delta, /* Change made to incore extents */
204         int                     whichfork, /* data or attr fork */
205         int                     rsvd);   /* OK to allocate reserved blocks */
206
207 /*
208  * Remove the entry "free" from the free item list.  Prev points to the
209  * previous entry, unless "free" is the head of the list.
210  */
211 STATIC void
212 xfs_bmap_del_free(
213         xfs_bmap_free_t         *flist, /* free item list header */
214         xfs_bmap_free_item_t    *prev,  /* previous item on list, if any */
215         xfs_bmap_free_item_t    *free); /* list item to be freed */
216
217 /*
218  * Convert an extents-format file into a btree-format file.
219  * The new file will have a root block (in the inode) and a single child block.
220  */
221 STATIC int                                      /* error */
222 xfs_bmap_extents_to_btree(
223         xfs_trans_t             *tp,            /* transaction pointer */
224         xfs_inode_t             *ip,            /* incore inode pointer */
225         xfs_fsblock_t           *firstblock,    /* first-block-allocated */
226         xfs_bmap_free_t         *flist,         /* blocks freed in xaction */
227         xfs_btree_cur_t         **curp,         /* cursor returned to caller */
228         int                     wasdel,         /* converting a delayed alloc */
229         int                     *logflagsp,     /* inode logging flags */
230         int                     whichfork);     /* data or attr fork */
231
232 /*
233  * Convert a local file to an extents file.
234  * This code is sort of bogus, since the file data needs to get
235  * logged so it won't be lost.  The bmap-level manipulations are ok, though.
236  */
237 STATIC int                              /* error */
238 xfs_bmap_local_to_extents(
239         xfs_trans_t     *tp,            /* transaction pointer */
240         xfs_inode_t     *ip,            /* incore inode pointer */
241         xfs_fsblock_t   *firstblock,    /* first block allocated in xaction */
242         xfs_extlen_t    total,          /* total blocks needed by transaction */
243         int             *logflagsp,     /* inode logging flags */
244         int             whichfork);     /* data or attr fork */
245
246 /*
247  * Search the extents list for the inode, for the extent containing bno.
248  * If bno lies in a hole, point to the next entry.  If bno lies past eof,
249  * *eofp will be set, and *prevp will contain the last entry (null if none).
250  * Else, *lastxp will be set to the index of the found
251  * entry; *gotp will contain the entry.
252  */
253 STATIC xfs_bmbt_rec_host_t *            /* pointer to found extent entry */
254 xfs_bmap_search_extents(
255         xfs_inode_t     *ip,            /* incore inode pointer */
256         xfs_fileoff_t   bno,            /* block number searched for */
257         int             whichfork,      /* data or attr fork */
258         int             *eofp,          /* out: end of file found */
259         xfs_extnum_t    *lastxp,        /* out: last extent index */
260         xfs_bmbt_irec_t *gotp,          /* out: extent entry found */
261         xfs_bmbt_irec_t *prevp);        /* out: previous extent entry found */
262
263 /*
264  * Check the last inode extent to determine whether this allocation will result
265  * in blocks being allocated at the end of the file. When we allocate new data
266  * blocks at the end of the file which do not start at the previous data block,
267  * we will try to align the new blocks at stripe unit boundaries.
268  */
269 STATIC int                              /* error */
270 xfs_bmap_isaeof(
271         xfs_inode_t     *ip,            /* incore inode pointer */
272         xfs_fileoff_t   off,            /* file offset in fsblocks */
273         int             whichfork,      /* data or attribute fork */
274         char            *aeof);         /* return value */
275
276 /*
277  * Compute the worst-case number of indirect blocks that will be used
278  * for ip's delayed extent of length "len".
279  */
280 STATIC xfs_filblks_t
281 xfs_bmap_worst_indlen(
282         xfs_inode_t             *ip,    /* incore inode pointer */
283         xfs_filblks_t           len);   /* delayed extent length */
284
285 #ifdef DEBUG
286 /*
287  * Perform various validation checks on the values being returned
288  * from xfs_bmapi().
289  */
290 STATIC void
291 xfs_bmap_validate_ret(
292         xfs_fileoff_t           bno,
293         xfs_filblks_t           len,
294         int                     flags,
295         xfs_bmbt_irec_t         *mval,
296         int                     nmap,
297         int                     ret_nmap);
298 #else
299 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
300 #endif /* DEBUG */
301
302 STATIC int
303 xfs_bmap_count_tree(
304         xfs_mount_t     *mp,
305         xfs_trans_t     *tp,
306         xfs_ifork_t     *ifp,
307         xfs_fsblock_t   blockno,
308         int             levelin,
309         int             *count);
310
311 STATIC void
312 xfs_bmap_count_leaves(
313         xfs_ifork_t             *ifp,
314         xfs_extnum_t            idx,
315         int                     numrecs,
316         int                     *count);
317
318 STATIC void
319 xfs_bmap_disk_count_leaves(
320         struct xfs_mount        *mp,
321         struct xfs_btree_block  *block,
322         int                     numrecs,
323         int                     *count);
324
325 /*
326  * Bmap internal routines.
327  */
328
329 STATIC int                              /* error */
330 xfs_bmbt_lookup_eq(
331         struct xfs_btree_cur    *cur,
332         xfs_fileoff_t           off,
333         xfs_fsblock_t           bno,
334         xfs_filblks_t           len,
335         int                     *stat)  /* success/failure */
336 {
337         cur->bc_rec.b.br_startoff = off;
338         cur->bc_rec.b.br_startblock = bno;
339         cur->bc_rec.b.br_blockcount = len;
340         return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
341 }
342
343 STATIC int                              /* error */
344 xfs_bmbt_lookup_ge(
345         struct xfs_btree_cur    *cur,
346         xfs_fileoff_t           off,
347         xfs_fsblock_t           bno,
348         xfs_filblks_t           len,
349         int                     *stat)  /* success/failure */
350 {
351         cur->bc_rec.b.br_startoff = off;
352         cur->bc_rec.b.br_startblock = bno;
353         cur->bc_rec.b.br_blockcount = len;
354         return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
355 }
356
357 /*
358 * Update the record referred to by cur to the value given
359  * by [off, bno, len, state].
360  * This either works (return 0) or gets an EFSCORRUPTED error.
361  */
362 STATIC int
363 xfs_bmbt_update(
364         struct xfs_btree_cur    *cur,
365         xfs_fileoff_t           off,
366         xfs_fsblock_t           bno,
367         xfs_filblks_t           len,
368         xfs_exntst_t            state)
369 {
370         union xfs_btree_rec     rec;
371
372         xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
373         return xfs_btree_update(cur, &rec);
374 }
375
376 /*
377  * Called from xfs_bmap_add_attrfork to handle btree format files.
378  */
379 STATIC int                                      /* error */
380 xfs_bmap_add_attrfork_btree(
381         xfs_trans_t             *tp,            /* transaction pointer */
382         xfs_inode_t             *ip,            /* incore inode pointer */
383         xfs_fsblock_t           *firstblock,    /* first block allocated */
384         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
385         int                     *flags)         /* inode logging flags */
386 {
387         xfs_btree_cur_t         *cur;           /* btree cursor */
388         int                     error;          /* error return value */
389         xfs_mount_t             *mp;            /* file system mount struct */
390         int                     stat;           /* newroot status */
391
392         mp = ip->i_mount;
393         if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
394                 *flags |= XFS_ILOG_DBROOT;
395         else {
396                 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
397                 cur->bc_private.b.flist = flist;
398                 cur->bc_private.b.firstblock = *firstblock;
399                 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
400                         goto error0;
401                 /* must be at least one entry */
402                 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
403                 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
404                         goto error0;
405                 if (stat == 0) {
406                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
407                         return XFS_ERROR(ENOSPC);
408                 }
409                 *firstblock = cur->bc_private.b.firstblock;
410                 cur->bc_private.b.allocated = 0;
411                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
412         }
413         return 0;
414 error0:
415         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
416         return error;
417 }
418
419 /*
420  * Called from xfs_bmap_add_attrfork to handle extents format files.
421  */
422 STATIC int                                      /* error */
423 xfs_bmap_add_attrfork_extents(
424         xfs_trans_t             *tp,            /* transaction pointer */
425         xfs_inode_t             *ip,            /* incore inode pointer */
426         xfs_fsblock_t           *firstblock,    /* first block allocated */
427         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
428         int                     *flags)         /* inode logging flags */
429 {
430         xfs_btree_cur_t         *cur;           /* bmap btree cursor */
431         int                     error;          /* error return value */
432
433         if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
434                 return 0;
435         cur = NULL;
436         error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
437                 flags, XFS_DATA_FORK);
438         if (cur) {
439                 cur->bc_private.b.allocated = 0;
440                 xfs_btree_del_cursor(cur,
441                         error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
442         }
443         return error;
444 }
445
446 /*
447  * Called from xfs_bmap_add_attrfork to handle local format files.
448  */
449 STATIC int                                      /* error */
450 xfs_bmap_add_attrfork_local(
451         xfs_trans_t             *tp,            /* transaction pointer */
452         xfs_inode_t             *ip,            /* incore inode pointer */
453         xfs_fsblock_t           *firstblock,    /* first block allocated */
454         xfs_bmap_free_t         *flist,         /* blocks to free at commit */
455         int                     *flags)         /* inode logging flags */
456 {
457         xfs_da_args_t           dargs;          /* args for dir/attr code */
458         int                     error;          /* error return value */
459         xfs_mount_t             *mp;            /* mount structure pointer */
460
461         if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
462                 return 0;
463         if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
464                 mp = ip->i_mount;
465                 memset(&dargs, 0, sizeof(dargs));
466                 dargs.dp = ip;
467                 dargs.firstblock = firstblock;
468                 dargs.flist = flist;
469                 dargs.total = mp->m_dirblkfsbs;
470                 dargs.whichfork = XFS_DATA_FORK;
471                 dargs.trans = tp;
472                 error = xfs_dir2_sf_to_block(&dargs);
473         } else
474                 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
475                         XFS_DATA_FORK);
476         return error;
477 }
478
479 /*
480  * Called by xfs_bmapi to update file extent records and the btree
481  * after allocating space (or doing a delayed allocation).
482  */
483 STATIC int                              /* error */
484 xfs_bmap_add_extent(
485         xfs_inode_t             *ip,    /* incore inode pointer */
486         xfs_extnum_t            idx,    /* extent number to update/insert */
487         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
488         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
489         xfs_fsblock_t           *first, /* pointer to firstblock variable */
490         xfs_bmap_free_t         *flist, /* list of extents to be freed */
491         int                     *logflagsp, /* inode logging flags */
492         xfs_extdelta_t          *delta, /* Change made to incore extents */
493         int                     whichfork, /* data or attr fork */
494         int                     rsvd)   /* OK to use reserved data blocks */
495 {
496         xfs_btree_cur_t         *cur;   /* btree cursor or null */
497         xfs_filblks_t           da_new; /* new count del alloc blocks used */
498         xfs_filblks_t           da_old; /* old count del alloc blocks used */
499         int                     error;  /* error return value */
500         xfs_ifork_t             *ifp;   /* inode fork ptr */
501         int                     logflags; /* returned value */
502         xfs_extnum_t            nextents; /* number of extents in file now */
503
504         XFS_STATS_INC(xs_add_exlist);
505         cur = *curp;
506         ifp = XFS_IFORK_PTR(ip, whichfork);
507         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
508         ASSERT(idx <= nextents);
509         da_old = da_new = 0;
510         error = 0;
511         /*
512          * This is the first extent added to a new/empty file.
513          * Special case this one, so other routines get to assume there are
514          * already extents in the list.
515          */
516         if (nextents == 0) {
517                 xfs_iext_insert(ip, 0, 1, new,
518                                 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
519
520                 ASSERT(cur == NULL);
521                 ifp->if_lastex = 0;
522                 if (!isnullstartblock(new->br_startblock)) {
523                         XFS_IFORK_NEXT_SET(ip, whichfork, 1);
524                         logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
525                 } else
526                         logflags = 0;
527                 /* DELTA: single new extent */
528                 if (delta) {
529                         if (delta->xed_startoff > new->br_startoff)
530                                 delta->xed_startoff = new->br_startoff;
531                         if (delta->xed_blockcount <
532                                         new->br_startoff + new->br_blockcount)
533                                 delta->xed_blockcount = new->br_startoff +
534                                                 new->br_blockcount;
535                 }
536         }
537         /*
538          * Any kind of new delayed allocation goes here.
539          */
540         else if (isnullstartblock(new->br_startblock)) {
541                 if (cur)
542                         ASSERT((cur->bc_private.b.flags &
543                                 XFS_BTCUR_BPRV_WASDEL) == 0);
544                 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
545                                 &logflags, delta, rsvd)))
546                         goto done;
547         }
548         /*
549          * Real allocation off the end of the file.
550          */
551         else if (idx == nextents) {
552                 if (cur)
553                         ASSERT((cur->bc_private.b.flags &
554                                 XFS_BTCUR_BPRV_WASDEL) == 0);
555                 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
556                                 &logflags, delta, whichfork)))
557                         goto done;
558         } else {
559                 xfs_bmbt_irec_t prev;   /* old extent at offset idx */
560
561                 /*
562                  * Get the record referred to by idx.
563                  */
564                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
565                 /*
566                  * If it's a real allocation record, and the new allocation ends
567                  * after the start of the referred to record, then we're filling
568                  * in a delayed or unwritten allocation with a real one, or
569                  * converting real back to unwritten.
570                  */
571                 if (!isnullstartblock(new->br_startblock) &&
572                     new->br_startoff + new->br_blockcount > prev.br_startoff) {
573                         if (prev.br_state != XFS_EXT_UNWRITTEN &&
574                             isnullstartblock(prev.br_startblock)) {
575                                 da_old = startblockval(prev.br_startblock);
576                                 if (cur)
577                                         ASSERT(cur->bc_private.b.flags &
578                                                 XFS_BTCUR_BPRV_WASDEL);
579                                 if ((error = xfs_bmap_add_extent_delay_real(ip,
580                                         idx, &cur, new, &da_new, first, flist,
581                                         &logflags, delta, rsvd)))
582                                         goto done;
583                         } else if (new->br_state == XFS_EXT_NORM) {
584                                 ASSERT(new->br_state == XFS_EXT_NORM);
585                                 if ((error = xfs_bmap_add_extent_unwritten_real(
586                                         ip, idx, &cur, new, &logflags, delta)))
587                                         goto done;
588                         } else {
589                                 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
590                                 if ((error = xfs_bmap_add_extent_unwritten_real(
591                                         ip, idx, &cur, new, &logflags, delta)))
592                                         goto done;
593                         }
594                         ASSERT(*curp == cur || *curp == NULL);
595                 }
596                 /*
597                  * Otherwise we're filling in a hole with an allocation.
598                  */
599                 else {
600                         if (cur)
601                                 ASSERT((cur->bc_private.b.flags &
602                                         XFS_BTCUR_BPRV_WASDEL) == 0);
603                         if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
604                                         new, &logflags, delta, whichfork)))
605                                 goto done;
606                 }
607         }
608
609         ASSERT(*curp == cur || *curp == NULL);
610         /*
611          * Convert to a btree if necessary.
612          */
613         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
614             XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
615                 int     tmp_logflags;   /* partial log flag return val */
616
617                 ASSERT(cur == NULL);
618                 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
619                         flist, &cur, da_old > 0, &tmp_logflags, whichfork);
620                 logflags |= tmp_logflags;
621                 if (error)
622                         goto done;
623         }
624         /*
625          * Adjust for changes in reserved delayed indirect blocks.
626          * Nothing to do for disk quotas here.
627          */
628         if (da_old || da_new) {
629                 xfs_filblks_t   nblks;
630
631                 nblks = da_new;
632                 if (cur)
633                         nblks += cur->bc_private.b.allocated;
634                 ASSERT(nblks <= da_old);
635                 if (nblks < da_old)
636                         xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
637                                 (int64_t)(da_old - nblks), rsvd);
638         }
639         /*
640          * Clear out the allocated field, done with it now in any case.
641          */
642         if (cur) {
643                 cur->bc_private.b.allocated = 0;
644                 *curp = cur;
645         }
646 done:
647 #ifdef DEBUG
648         if (!error)
649                 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
650 #endif
651         *logflagsp = logflags;
652         return error;
653 }
654
655 /*
656  * Called by xfs_bmap_add_extent to handle cases converting a delayed
657  * allocation to a real allocation.
658  */
659 STATIC int                              /* error */
660 xfs_bmap_add_extent_delay_real(
661         xfs_inode_t             *ip,    /* incore inode pointer */
662         xfs_extnum_t            idx,    /* extent number to update/insert */
663         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
664         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
665         xfs_filblks_t           *dnew,  /* new delayed-alloc indirect blocks */
666         xfs_fsblock_t           *first, /* pointer to firstblock variable */
667         xfs_bmap_free_t         *flist, /* list of extents to be freed */
668         int                     *logflagsp, /* inode logging flags */
669         xfs_extdelta_t          *delta, /* Change made to incore extents */
670         int                     rsvd)   /* OK to use reserved data block allocation */
671 {
672         xfs_btree_cur_t         *cur;   /* btree cursor */
673         int                     diff;   /* temp value */
674         xfs_bmbt_rec_host_t     *ep;    /* extent entry for idx */
675         int                     error;  /* error return value */
676         int                     i;      /* temp state */
677         xfs_ifork_t             *ifp;   /* inode fork pointer */
678         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
679         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
680                                         /* left is 0, right is 1, prev is 2 */
681         int                     rval=0; /* return value (logging flags) */
682         int                     state = 0;/* state bits, accessed thru macros */
683         xfs_filblks_t           temp=0; /* value for dnew calculations */
684         xfs_filblks_t           temp2=0;/* value for dnew calculations */
685         int                     tmp_rval;       /* partial logging flags */
686
687 #define LEFT            r[0]
688 #define RIGHT           r[1]
689 #define PREV            r[2]
690
691         /*
692          * Set up a bunch of variables to make the tests simpler.
693          */
694         cur = *curp;
695         ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
696         ep = xfs_iext_get_ext(ifp, idx);
697         xfs_bmbt_get_all(ep, &PREV);
698         new_endoff = new->br_startoff + new->br_blockcount;
699         ASSERT(PREV.br_startoff <= new->br_startoff);
700         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
701
702         /*
703          * Set flags determining what part of the previous delayed allocation
704          * extent is being replaced by a real allocation.
705          */
706         if (PREV.br_startoff == new->br_startoff)
707                 state |= BMAP_LEFT_FILLING;
708         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
709                 state |= BMAP_RIGHT_FILLING;
710
711         /*
712          * Check and set flags if this segment has a left neighbor.
713          * Don't set contiguous if the combined extent would be too large.
714          */
715         if (idx > 0) {
716                 state |= BMAP_LEFT_VALID;
717                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
718
719                 if (isnullstartblock(LEFT.br_startblock))
720                         state |= BMAP_LEFT_DELAY;
721         }
722
723         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
724             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
725             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
726             LEFT.br_state == new->br_state &&
727             LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
728                 state |= BMAP_LEFT_CONTIG;
729
730         /*
731          * Check and set flags if this segment has a right neighbor.
732          * Don't set contiguous if the combined extent would be too large.
733          * Also check for all-three-contiguous being too large.
734          */
735         if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
736                 state |= BMAP_RIGHT_VALID;
737                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
738
739                 if (isnullstartblock(RIGHT.br_startblock))
740                         state |= BMAP_RIGHT_DELAY;
741         }
742
743         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
744             new_endoff == RIGHT.br_startoff &&
745             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
746             new->br_state == RIGHT.br_state &&
747             new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
748             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
749                        BMAP_RIGHT_FILLING)) !=
750                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
751                        BMAP_RIGHT_FILLING) ||
752              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
753                         <= MAXEXTLEN))
754                 state |= BMAP_RIGHT_CONTIG;
755
756         error = 0;
757         /*
758          * Switch out based on the FILLING and CONTIG state bits.
759          */
760         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
761                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
762         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
763              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
764                 /*
765                  * Filling in all of a previously delayed allocation extent.
766                  * The left and right neighbors are both contiguous with new.
767                  */
768                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
769                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
770                         LEFT.br_blockcount + PREV.br_blockcount +
771                         RIGHT.br_blockcount);
772                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
773
774                 xfs_iext_remove(ip, idx, 2, state);
775                 ip->i_df.if_lastex = idx - 1;
776                 ip->i_d.di_nextents--;
777                 if (cur == NULL)
778                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
779                 else {
780                         rval = XFS_ILOG_CORE;
781                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
782                                         RIGHT.br_startblock,
783                                         RIGHT.br_blockcount, &i)))
784                                 goto done;
785                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
786                         if ((error = xfs_btree_delete(cur, &i)))
787                                 goto done;
788                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
789                         if ((error = xfs_btree_decrement(cur, 0, &i)))
790                                 goto done;
791                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
792                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
793                                         LEFT.br_startblock,
794                                         LEFT.br_blockcount +
795                                         PREV.br_blockcount +
796                                         RIGHT.br_blockcount, LEFT.br_state)))
797                                 goto done;
798                 }
799                 *dnew = 0;
800                 /* DELTA: Three in-core extents are replaced by one. */
801                 temp = LEFT.br_startoff;
802                 temp2 = LEFT.br_blockcount +
803                         PREV.br_blockcount +
804                         RIGHT.br_blockcount;
805                 break;
806
807         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
808                 /*
809                  * Filling in all of a previously delayed allocation extent.
810                  * The left neighbor is contiguous, the right is not.
811                  */
812                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
813                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
814                         LEFT.br_blockcount + PREV.br_blockcount);
815                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
816
817                 ip->i_df.if_lastex = idx - 1;
818                 xfs_iext_remove(ip, idx, 1, state);
819                 if (cur == NULL)
820                         rval = XFS_ILOG_DEXT;
821                 else {
822                         rval = 0;
823                         if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
824                                         LEFT.br_startblock, LEFT.br_blockcount,
825                                         &i)))
826                                 goto done;
827                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
828                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
829                                         LEFT.br_startblock,
830                                         LEFT.br_blockcount +
831                                         PREV.br_blockcount, LEFT.br_state)))
832                                 goto done;
833                 }
834                 *dnew = 0;
835                 /* DELTA: Two in-core extents are replaced by one. */
836                 temp = LEFT.br_startoff;
837                 temp2 = LEFT.br_blockcount +
838                         PREV.br_blockcount;
839                 break;
840
841         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
842                 /*
843                  * Filling in all of a previously delayed allocation extent.
844                  * The right neighbor is contiguous, the left is not.
845                  */
846                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
847                 xfs_bmbt_set_startblock(ep, new->br_startblock);
848                 xfs_bmbt_set_blockcount(ep,
849                         PREV.br_blockcount + RIGHT.br_blockcount);
850                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
851
852                 ip->i_df.if_lastex = idx;
853                 xfs_iext_remove(ip, idx + 1, 1, state);
854                 if (cur == NULL)
855                         rval = XFS_ILOG_DEXT;
856                 else {
857                         rval = 0;
858                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
859                                         RIGHT.br_startblock,
860                                         RIGHT.br_blockcount, &i)))
861                                 goto done;
862                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
863                         if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
864                                         new->br_startblock,
865                                         PREV.br_blockcount +
866                                         RIGHT.br_blockcount, PREV.br_state)))
867                                 goto done;
868                 }
869                 *dnew = 0;
870                 /* DELTA: Two in-core extents are replaced by one. */
871                 temp = PREV.br_startoff;
872                 temp2 = PREV.br_blockcount +
873                         RIGHT.br_blockcount;
874                 break;
875
876         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
877                 /*
878                  * Filling in all of a previously delayed allocation extent.
879                  * Neither the left nor right neighbors are contiguous with
880                  * the new one.
881                  */
882                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
883                 xfs_bmbt_set_startblock(ep, new->br_startblock);
884                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
885
886                 ip->i_df.if_lastex = idx;
887                 ip->i_d.di_nextents++;
888                 if (cur == NULL)
889                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
890                 else {
891                         rval = XFS_ILOG_CORE;
892                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
893                                         new->br_startblock, new->br_blockcount,
894                                         &i)))
895                                 goto done;
896                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
897                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
898                         if ((error = xfs_btree_insert(cur, &i)))
899                                 goto done;
900                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
901                 }
902                 *dnew = 0;
903                 /* DELTA: The in-core extent described by new changed type. */
904                 temp = new->br_startoff;
905                 temp2 = new->br_blockcount;
906                 break;
907
908         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
909                 /*
910                  * Filling in the first part of a previous delayed allocation.
911                  * The left neighbor is contiguous.
912                  */
913                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
914                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
915                         LEFT.br_blockcount + new->br_blockcount);
916                 xfs_bmbt_set_startoff(ep,
917                         PREV.br_startoff + new->br_blockcount);
918                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
919
920                 temp = PREV.br_blockcount - new->br_blockcount;
921                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
922                 xfs_bmbt_set_blockcount(ep, temp);
923                 ip->i_df.if_lastex = idx - 1;
924                 if (cur == NULL)
925                         rval = XFS_ILOG_DEXT;
926                 else {
927                         rval = 0;
928                         if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
929                                         LEFT.br_startblock, LEFT.br_blockcount,
930                                         &i)))
931                                 goto done;
932                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
933                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
934                                         LEFT.br_startblock,
935                                         LEFT.br_blockcount +
936                                         new->br_blockcount,
937                                         LEFT.br_state)))
938                                 goto done;
939                 }
940                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
941                         startblockval(PREV.br_startblock));
942                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
943                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
944                 *dnew = temp;
945                 /* DELTA: The boundary between two in-core extents moved. */
946                 temp = LEFT.br_startoff;
947                 temp2 = LEFT.br_blockcount +
948                         PREV.br_blockcount;
949                 break;
950
951         case BMAP_LEFT_FILLING:
952                 /*
953                  * Filling in the first part of a previous delayed allocation.
954                  * The left neighbor is not contiguous.
955                  */
956                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
957                 xfs_bmbt_set_startoff(ep, new_endoff);
958                 temp = PREV.br_blockcount - new->br_blockcount;
959                 xfs_bmbt_set_blockcount(ep, temp);
960                 xfs_iext_insert(ip, idx, 1, new, state);
961                 ip->i_df.if_lastex = idx;
962                 ip->i_d.di_nextents++;
963                 if (cur == NULL)
964                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
965                 else {
966                         rval = XFS_ILOG_CORE;
967                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
968                                         new->br_startblock, new->br_blockcount,
969                                         &i)))
970                                 goto done;
971                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
972                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
973                         if ((error = xfs_btree_insert(cur, &i)))
974                                 goto done;
975                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
976                 }
977                 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
978                     ip->i_d.di_nextents > ip->i_df.if_ext_max) {
979                         error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
980                                         first, flist, &cur, 1, &tmp_rval,
981                                         XFS_DATA_FORK);
982                         rval |= tmp_rval;
983                         if (error)
984                                 goto done;
985                 }
986                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
987                         startblockval(PREV.br_startblock) -
988                         (cur ? cur->bc_private.b.allocated : 0));
989                 ep = xfs_iext_get_ext(ifp, idx + 1);
990                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
991                 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
992                 *dnew = temp;
993                 /* DELTA: One in-core extent is split in two. */
994                 temp = PREV.br_startoff;
995                 temp2 = PREV.br_blockcount;
996                 break;
997
998         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
999                 /*
1000                  * Filling in the last part of a previous delayed allocation.
1001                  * The right neighbor is contiguous with the new allocation.
1002                  */
1003                 temp = PREV.br_blockcount - new->br_blockcount;
1004                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1005                 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1006                 xfs_bmbt_set_blockcount(ep, temp);
1007                 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1008                         new->br_startoff, new->br_startblock,
1009                         new->br_blockcount + RIGHT.br_blockcount,
1010                         RIGHT.br_state);
1011                 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1012                 ip->i_df.if_lastex = idx + 1;
1013                 if (cur == NULL)
1014                         rval = XFS_ILOG_DEXT;
1015                 else {
1016                         rval = 0;
1017                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1018                                         RIGHT.br_startblock,
1019                                         RIGHT.br_blockcount, &i)))
1020                                 goto done;
1021                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1022                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1023                                         new->br_startblock,
1024                                         new->br_blockcount +
1025                                         RIGHT.br_blockcount,
1026                                         RIGHT.br_state)))
1027                                 goto done;
1028                 }
1029                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1030                         startblockval(PREV.br_startblock));
1031                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1032                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1033                 *dnew = temp;
1034                 /* DELTA: The boundary between two in-core extents moved. */
1035                 temp = PREV.br_startoff;
1036                 temp2 = PREV.br_blockcount +
1037                         RIGHT.br_blockcount;
1038                 break;
1039
1040         case BMAP_RIGHT_FILLING:
1041                 /*
1042                  * Filling in the last part of a previous delayed allocation.
1043                  * The right neighbor is not contiguous.
1044                  */
1045                 temp = PREV.br_blockcount - new->br_blockcount;
1046                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1047                 xfs_bmbt_set_blockcount(ep, temp);
1048                 xfs_iext_insert(ip, idx + 1, 1, new, state);
1049                 ip->i_df.if_lastex = idx + 1;
1050                 ip->i_d.di_nextents++;
1051                 if (cur == NULL)
1052                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1053                 else {
1054                         rval = XFS_ILOG_CORE;
1055                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1056                                         new->br_startblock, new->br_blockcount,
1057                                         &i)))
1058                                 goto done;
1059                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1060                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
1061                         if ((error = xfs_btree_insert(cur, &i)))
1062                                 goto done;
1063                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1064                 }
1065                 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1066                     ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1067                         error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1068                                 first, flist, &cur, 1, &tmp_rval,
1069                                 XFS_DATA_FORK);
1070                         rval |= tmp_rval;
1071                         if (error)
1072                                 goto done;
1073                 }
1074                 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1075                         startblockval(PREV.br_startblock) -
1076                         (cur ? cur->bc_private.b.allocated : 0));
1077                 ep = xfs_iext_get_ext(ifp, idx);
1078                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1079                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1080                 *dnew = temp;
1081                 /* DELTA: One in-core extent is split in two. */
1082                 temp = PREV.br_startoff;
1083                 temp2 = PREV.br_blockcount;
1084                 break;
1085
1086         case 0:
1087                 /*
1088                  * Filling in the middle part of a previous delayed allocation.
1089                  * Contiguity is impossible here.
1090                  * This case is avoided almost all the time.
1091                  */
1092                 temp = new->br_startoff - PREV.br_startoff;
1093                 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1094                 xfs_bmbt_set_blockcount(ep, temp);
1095                 r[0] = *new;
1096                 r[1].br_state = PREV.br_state;
1097                 r[1].br_startblock = 0;
1098                 r[1].br_startoff = new_endoff;
1099                 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1100                 r[1].br_blockcount = temp2;
1101                 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1102                 ip->i_df.if_lastex = idx + 1;
1103                 ip->i_d.di_nextents++;
1104                 if (cur == NULL)
1105                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1106                 else {
1107                         rval = XFS_ILOG_CORE;
1108                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1109                                         new->br_startblock, new->br_blockcount,
1110                                         &i)))
1111                                 goto done;
1112                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1113                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
1114                         if ((error = xfs_btree_insert(cur, &i)))
1115                                 goto done;
1116                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1117                 }
1118                 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1119                     ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1120                         error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1121                                         first, flist, &cur, 1, &tmp_rval,
1122                                         XFS_DATA_FORK);
1123                         rval |= tmp_rval;
1124                         if (error)
1125                                 goto done;
1126                 }
1127                 temp = xfs_bmap_worst_indlen(ip, temp);
1128                 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1129                 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1130                         (cur ? cur->bc_private.b.allocated : 0));
1131                 if (diff > 0 &&
1132                     xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1133                         /*
1134                          * Ick gross gag me with a spoon.
1135                          */
1136                         ASSERT(0);      /* want to see if this ever happens! */
1137                         while (diff > 0) {
1138                                 if (temp) {
1139                                         temp--;
1140                                         diff--;
1141                                         if (!diff ||
1142                                             !xfs_mod_incore_sb(ip->i_mount,
1143                                                     XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1144                                                 break;
1145                                 }
1146                                 if (temp2) {
1147                                         temp2--;
1148                                         diff--;
1149                                         if (!diff ||
1150                                             !xfs_mod_incore_sb(ip->i_mount,
1151                                                     XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1152                                                 break;
1153                                 }
1154                         }
1155                 }
1156                 ep = xfs_iext_get_ext(ifp, idx);
1157                 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1158                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1159                 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1160                 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1161                         nullstartblock((int)temp2));
1162                 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1163                 *dnew = temp + temp2;
1164                 /* DELTA: One in-core extent is split in three. */
1165                 temp = PREV.br_startoff;
1166                 temp2 = PREV.br_blockcount;
1167                 break;
1168
1169         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1170         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1171         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1172         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1173         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1174         case BMAP_LEFT_CONTIG:
1175         case BMAP_RIGHT_CONTIG:
1176                 /*
1177                  * These cases are all impossible.
1178                  */
1179                 ASSERT(0);
1180         }
1181         *curp = cur;
1182         if (delta) {
1183                 temp2 += temp;
1184                 if (delta->xed_startoff > temp)
1185                         delta->xed_startoff = temp;
1186                 if (delta->xed_blockcount < temp2)
1187                         delta->xed_blockcount = temp2;
1188         }
1189 done:
1190         *logflagsp = rval;
1191         return error;
1192 #undef  LEFT
1193 #undef  RIGHT
1194 #undef  PREV
1195 }
1196
1197 /*
1198  * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1199  * allocation to a real allocation or vice versa.
1200  */
1201 STATIC int                              /* error */
1202 xfs_bmap_add_extent_unwritten_real(
1203         xfs_inode_t             *ip,    /* incore inode pointer */
1204         xfs_extnum_t            idx,    /* extent number to update/insert */
1205         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
1206         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
1207         int                     *logflagsp, /* inode logging flags */
1208         xfs_extdelta_t          *delta) /* Change made to incore extents */
1209 {
1210         xfs_btree_cur_t         *cur;   /* btree cursor */
1211         xfs_bmbt_rec_host_t     *ep;    /* extent entry for idx */
1212         int                     error;  /* error return value */
1213         int                     i;      /* temp state */
1214         xfs_ifork_t             *ifp;   /* inode fork pointer */
1215         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
1216         xfs_exntst_t            newext; /* new extent state */
1217         xfs_exntst_t            oldext; /* old extent state */
1218         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
1219                                         /* left is 0, right is 1, prev is 2 */
1220         int                     rval=0; /* return value (logging flags) */
1221         int                     state = 0;/* state bits, accessed thru macros */
1222         xfs_filblks_t           temp=0;
1223         xfs_filblks_t           temp2=0;
1224
1225 #define LEFT            r[0]
1226 #define RIGHT           r[1]
1227 #define PREV            r[2]
1228         /*
1229          * Set up a bunch of variables to make the tests simpler.
1230          */
1231         error = 0;
1232         cur = *curp;
1233         ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1234         ep = xfs_iext_get_ext(ifp, idx);
1235         xfs_bmbt_get_all(ep, &PREV);
1236         newext = new->br_state;
1237         oldext = (newext == XFS_EXT_UNWRITTEN) ?
1238                 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1239         ASSERT(PREV.br_state == oldext);
1240         new_endoff = new->br_startoff + new->br_blockcount;
1241         ASSERT(PREV.br_startoff <= new->br_startoff);
1242         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1243
1244         /*
1245          * Set flags determining what part of the previous oldext allocation
1246          * extent is being replaced by a newext allocation.
1247          */
1248         if (PREV.br_startoff == new->br_startoff)
1249                 state |= BMAP_LEFT_FILLING;
1250         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1251                 state |= BMAP_RIGHT_FILLING;
1252
1253         /*
1254          * Check and set flags if this segment has a left neighbor.
1255          * Don't set contiguous if the combined extent would be too large.
1256          */
1257         if (idx > 0) {
1258                 state |= BMAP_LEFT_VALID;
1259                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1260
1261                 if (isnullstartblock(LEFT.br_startblock))
1262                         state |= BMAP_LEFT_DELAY;
1263         }
1264
1265         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1266             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1267             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1268             LEFT.br_state == newext &&
1269             LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1270                 state |= BMAP_LEFT_CONTIG;
1271
1272         /*
1273          * Check and set flags if this segment has a right neighbor.
1274          * Don't set contiguous if the combined extent would be too large.
1275          * Also check for all-three-contiguous being too large.
1276          */
1277         if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1278                 state |= BMAP_RIGHT_VALID;
1279                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1280                 if (isnullstartblock(RIGHT.br_startblock))
1281                         state |= BMAP_RIGHT_DELAY;
1282         }
1283
1284         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1285             new_endoff == RIGHT.br_startoff &&
1286             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1287             newext == RIGHT.br_state &&
1288             new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1289             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1290                        BMAP_RIGHT_FILLING)) !=
1291                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1292                        BMAP_RIGHT_FILLING) ||
1293              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1294                         <= MAXEXTLEN))
1295                 state |= BMAP_RIGHT_CONTIG;
1296
1297         /*
1298          * Switch out based on the FILLING and CONTIG state bits.
1299          */
1300         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1301                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1302         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1303              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1304                 /*
1305                  * Setting all of a previous oldext extent to newext.
1306                  * The left and right neighbors are both contiguous with new.
1307                  */
1308                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1309                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1310                         LEFT.br_blockcount + PREV.br_blockcount +
1311                         RIGHT.br_blockcount);
1312                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1313
1314                 xfs_iext_remove(ip, idx, 2, state);
1315                 ip->i_df.if_lastex = idx - 1;
1316                 ip->i_d.di_nextents -= 2;
1317                 if (cur == NULL)
1318                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1319                 else {
1320                         rval = XFS_ILOG_CORE;
1321                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1322                                         RIGHT.br_startblock,
1323                                         RIGHT.br_blockcount, &i)))
1324                                 goto done;
1325                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1326                         if ((error = xfs_btree_delete(cur, &i)))
1327                                 goto done;
1328                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1329                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1330                                 goto done;
1331                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1332                         if ((error = xfs_btree_delete(cur, &i)))
1333                                 goto done;
1334                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1335                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1336                                 goto done;
1337                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1338                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1339                                 LEFT.br_startblock,
1340                                 LEFT.br_blockcount + PREV.br_blockcount +
1341                                 RIGHT.br_blockcount, LEFT.br_state)))
1342                                 goto done;
1343                 }
1344                 /* DELTA: Three in-core extents are replaced by one. */
1345                 temp = LEFT.br_startoff;
1346                 temp2 = LEFT.br_blockcount +
1347                         PREV.br_blockcount +
1348                         RIGHT.br_blockcount;
1349                 break;
1350
1351         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1352                 /*
1353                  * Setting all of a previous oldext extent to newext.
1354                  * The left neighbor is contiguous, the right is not.
1355                  */
1356                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1357                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1358                         LEFT.br_blockcount + PREV.br_blockcount);
1359                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1360
1361                 ip->i_df.if_lastex = idx - 1;
1362                 xfs_iext_remove(ip, idx, 1, state);
1363                 ip->i_d.di_nextents--;
1364                 if (cur == NULL)
1365                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1366                 else {
1367                         rval = XFS_ILOG_CORE;
1368                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1369                                         PREV.br_startblock, PREV.br_blockcount,
1370                                         &i)))
1371                                 goto done;
1372                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1373                         if ((error = xfs_btree_delete(cur, &i)))
1374                                 goto done;
1375                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1376                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1377                                 goto done;
1378                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1379                         if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1380                                 LEFT.br_startblock,
1381                                 LEFT.br_blockcount + PREV.br_blockcount,
1382                                 LEFT.br_state)))
1383                                 goto done;
1384                 }
1385                 /* DELTA: Two in-core extents are replaced by one. */
1386                 temp = LEFT.br_startoff;
1387                 temp2 = LEFT.br_blockcount +
1388                         PREV.br_blockcount;
1389                 break;
1390
1391         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1392                 /*
1393                  * Setting all of a previous oldext extent to newext.
1394                  * The right neighbor is contiguous, the left is not.
1395                  */
1396                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1397                 xfs_bmbt_set_blockcount(ep,
1398                         PREV.br_blockcount + RIGHT.br_blockcount);
1399                 xfs_bmbt_set_state(ep, newext);
1400                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1401                 ip->i_df.if_lastex = idx;
1402                 xfs_iext_remove(ip, idx + 1, 1, state);
1403                 ip->i_d.di_nextents--;
1404                 if (cur == NULL)
1405                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1406                 else {
1407                         rval = XFS_ILOG_CORE;
1408                         if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1409                                         RIGHT.br_startblock,
1410                                         RIGHT.br_blockcount, &i)))
1411                                 goto done;
1412                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1413                         if ((error = xfs_btree_delete(cur, &i)))
1414                                 goto done;
1415                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1416                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1417                                 goto done;
1418                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1419                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1420                                 new->br_startblock,
1421                                 new->br_blockcount + RIGHT.br_blockcount,
1422                                 newext)))
1423                                 goto done;
1424                 }
1425                 /* DELTA: Two in-core extents are replaced by one. */
1426                 temp = PREV.br_startoff;
1427                 temp2 = PREV.br_blockcount +
1428                         RIGHT.br_blockcount;
1429                 break;
1430
1431         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1432                 /*
1433                  * Setting all of a previous oldext extent to newext.
1434                  * Neither the left nor right neighbors are contiguous with
1435                  * the new one.
1436                  */
1437                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1438                 xfs_bmbt_set_state(ep, newext);
1439                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1440
1441                 ip->i_df.if_lastex = idx;
1442                 if (cur == NULL)
1443                         rval = XFS_ILOG_DEXT;
1444                 else {
1445                         rval = 0;
1446                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1447                                         new->br_startblock, new->br_blockcount,
1448                                         &i)))
1449                                 goto done;
1450                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1451                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1452                                 new->br_startblock, new->br_blockcount,
1453                                 newext)))
1454                                 goto done;
1455                 }
1456                 /* DELTA: The in-core extent described by new changed type. */
1457                 temp = new->br_startoff;
1458                 temp2 = new->br_blockcount;
1459                 break;
1460
1461         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1462                 /*
1463                  * Setting the first part of a previous oldext extent to newext.
1464                  * The left neighbor is contiguous.
1465                  */
1466                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1467                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1468                         LEFT.br_blockcount + new->br_blockcount);
1469                 xfs_bmbt_set_startoff(ep,
1470                         PREV.br_startoff + new->br_blockcount);
1471                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1472
1473                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1474                 xfs_bmbt_set_startblock(ep,
1475                         new->br_startblock + new->br_blockcount);
1476                 xfs_bmbt_set_blockcount(ep,
1477                         PREV.br_blockcount - new->br_blockcount);
1478                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1479
1480                 ip->i_df.if_lastex = idx - 1;
1481                 if (cur == NULL)
1482                         rval = XFS_ILOG_DEXT;
1483                 else {
1484                         rval = 0;
1485                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1486                                         PREV.br_startblock, PREV.br_blockcount,
1487                                         &i)))
1488                                 goto done;
1489                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1490                         if ((error = xfs_bmbt_update(cur,
1491                                 PREV.br_startoff + new->br_blockcount,
1492                                 PREV.br_startblock + new->br_blockcount,
1493                                 PREV.br_blockcount - new->br_blockcount,
1494                                 oldext)))
1495                                 goto done;
1496                         if ((error = xfs_btree_decrement(cur, 0, &i)))
1497                                 goto done;
1498                         if (xfs_bmbt_update(cur, LEFT.br_startoff,
1499                                 LEFT.br_startblock,
1500                                 LEFT.br_blockcount + new->br_blockcount,
1501                                 LEFT.br_state))
1502                                 goto done;
1503                 }
1504                 /* DELTA: The boundary between two in-core extents moved. */
1505                 temp = LEFT.br_startoff;
1506                 temp2 = LEFT.br_blockcount +
1507                         PREV.br_blockcount;
1508                 break;
1509
1510         case BMAP_LEFT_FILLING:
1511                 /*
1512                  * Setting the first part of a previous oldext extent to newext.
1513                  * The left neighbor is not contiguous.
1514                  */
1515                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1516                 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1517                 xfs_bmbt_set_startoff(ep, new_endoff);
1518                 xfs_bmbt_set_blockcount(ep,
1519                         PREV.br_blockcount - new->br_blockcount);
1520                 xfs_bmbt_set_startblock(ep,
1521                         new->br_startblock + new->br_blockcount);
1522                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1523
1524                 xfs_iext_insert(ip, idx, 1, new, state);
1525                 ip->i_df.if_lastex = idx;
1526                 ip->i_d.di_nextents++;
1527                 if (cur == NULL)
1528                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1529                 else {
1530                         rval = XFS_ILOG_CORE;
1531                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1532                                         PREV.br_startblock, PREV.br_blockcount,
1533                                         &i)))
1534                                 goto done;
1535                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1536                         if ((error = xfs_bmbt_update(cur,
1537                                 PREV.br_startoff + new->br_blockcount,
1538                                 PREV.br_startblock + new->br_blockcount,
1539                                 PREV.br_blockcount - new->br_blockcount,
1540                                 oldext)))
1541                                 goto done;
1542                         cur->bc_rec.b = *new;
1543                         if ((error = xfs_btree_insert(cur, &i)))
1544                                 goto done;
1545                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1546                 }
1547                 /* DELTA: One in-core extent is split in two. */
1548                 temp = PREV.br_startoff;
1549                 temp2 = PREV.br_blockcount;
1550                 break;
1551
1552         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1553                 /*
1554                  * Setting the last part of a previous oldext extent to newext.
1555                  * The right neighbor is contiguous with the new allocation.
1556                  */
1557                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1558                 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1559                 xfs_bmbt_set_blockcount(ep,
1560                         PREV.br_blockcount - new->br_blockcount);
1561                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1562                 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1563                         new->br_startoff, new->br_startblock,
1564                         new->br_blockcount + RIGHT.br_blockcount, newext);
1565                 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1566
1567                 ip->i_df.if_lastex = idx + 1;
1568                 if (cur == NULL)
1569                         rval = XFS_ILOG_DEXT;
1570                 else {
1571                         rval = 0;
1572                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1573                                         PREV.br_startblock,
1574                                         PREV.br_blockcount, &i)))
1575                                 goto done;
1576                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1577                         if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1578                                 PREV.br_startblock,
1579                                 PREV.br_blockcount - new->br_blockcount,
1580                                 oldext)))
1581                                 goto done;
1582                         if ((error = xfs_btree_increment(cur, 0, &i)))
1583                                 goto done;
1584                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
1585                                 new->br_startblock,
1586                                 new->br_blockcount + RIGHT.br_blockcount,
1587                                 newext)))
1588                                 goto done;
1589                 }
1590                 /* DELTA: The boundary between two in-core extents moved. */
1591                 temp = PREV.br_startoff;
1592                 temp2 = PREV.br_blockcount +
1593                         RIGHT.br_blockcount;
1594                 break;
1595
1596         case BMAP_RIGHT_FILLING:
1597                 /*
1598                  * Setting the last part of a previous oldext extent to newext.
1599                  * The right neighbor is not contiguous.
1600                  */
1601                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1602                 xfs_bmbt_set_blockcount(ep,
1603                         PREV.br_blockcount - new->br_blockcount);
1604                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1605
1606                 xfs_iext_insert(ip, idx + 1, 1, new, state);
1607                 ip->i_df.if_lastex = idx + 1;
1608                 ip->i_d.di_nextents++;
1609                 if (cur == NULL)
1610                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1611                 else {
1612                         rval = XFS_ILOG_CORE;
1613                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1614                                         PREV.br_startblock, PREV.br_blockcount,
1615                                         &i)))
1616                                 goto done;
1617                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1618                         if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1619                                 PREV.br_startblock,
1620                                 PREV.br_blockcount - new->br_blockcount,
1621                                 oldext)))
1622                                 goto done;
1623                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1624                                         new->br_startblock, new->br_blockcount,
1625                                         &i)))
1626                                 goto done;
1627                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1628                         cur->bc_rec.b.br_state = XFS_EXT_NORM;
1629                         if ((error = xfs_btree_insert(cur, &i)))
1630                                 goto done;
1631                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1632                 }
1633                 /* DELTA: One in-core extent is split in two. */
1634                 temp = PREV.br_startoff;
1635                 temp2 = PREV.br_blockcount;
1636                 break;
1637
1638         case 0:
1639                 /*
1640                  * Setting the middle part of a previous oldext extent to
1641                  * newext.  Contiguity is impossible here.
1642                  * One extent becomes three extents.
1643                  */
1644                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1645                 xfs_bmbt_set_blockcount(ep,
1646                         new->br_startoff - PREV.br_startoff);
1647                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1648
1649                 r[0] = *new;
1650                 r[1].br_startoff = new_endoff;
1651                 r[1].br_blockcount =
1652                         PREV.br_startoff + PREV.br_blockcount - new_endoff;
1653                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1654                 r[1].br_state = oldext;
1655                 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1656                 ip->i_df.if_lastex = idx + 1;
1657                 ip->i_d.di_nextents += 2;
1658                 if (cur == NULL)
1659                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1660                 else {
1661                         rval = XFS_ILOG_CORE;
1662                         if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1663                                         PREV.br_startblock, PREV.br_blockcount,
1664                                         &i)))
1665                                 goto done;
1666                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1667                         /* new right extent - oldext */
1668                         if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1669                                 r[1].br_startblock, r[1].br_blockcount,
1670                                 r[1].br_state)))
1671                                 goto done;
1672                         /* new left extent - oldext */
1673                         cur->bc_rec.b = PREV;
1674                         cur->bc_rec.b.br_blockcount =
1675                                 new->br_startoff - PREV.br_startoff;
1676                         if ((error = xfs_btree_insert(cur, &i)))
1677                                 goto done;
1678                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1679                         /*
1680                          * Reset the cursor to the position of the new extent
1681                          * we are about to insert as we can't trust it after
1682                          * the previous insert.
1683                          */
1684                         if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1685                                         new->br_startblock, new->br_blockcount,
1686                                         &i)))
1687                                 goto done;
1688                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1689                         /* new middle extent - newext */
1690                         cur->bc_rec.b.br_state = new->br_state;
1691                         if ((error = xfs_btree_insert(cur, &i)))
1692                                 goto done;
1693                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1694                 }
1695                 /* DELTA: One in-core extent is split in three. */
1696                 temp = PREV.br_startoff;
1697                 temp2 = PREV.br_blockcount;
1698                 break;
1699
1700         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1701         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1702         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1703         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1704         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1705         case BMAP_LEFT_CONTIG:
1706         case BMAP_RIGHT_CONTIG:
1707                 /*
1708                  * These cases are all impossible.
1709                  */
1710                 ASSERT(0);
1711         }
1712         *curp = cur;
1713         if (delta) {
1714                 temp2 += temp;
1715                 if (delta->xed_startoff > temp)
1716                         delta->xed_startoff = temp;
1717                 if (delta->xed_blockcount < temp2)
1718                         delta->xed_blockcount = temp2;
1719         }
1720 done:
1721         *logflagsp = rval;
1722         return error;
1723 #undef  LEFT
1724 #undef  RIGHT
1725 #undef  PREV
1726 }
1727
1728 /*
1729  * Called by xfs_bmap_add_extent to handle cases converting a hole
1730  * to a delayed allocation.
1731  */
1732 /*ARGSUSED*/
1733 STATIC int                              /* error */
1734 xfs_bmap_add_extent_hole_delay(
1735         xfs_inode_t             *ip,    /* incore inode pointer */
1736         xfs_extnum_t            idx,    /* extent number to update/insert */
1737         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
1738         int                     *logflagsp, /* inode logging flags */
1739         xfs_extdelta_t          *delta, /* Change made to incore extents */
1740         int                     rsvd)           /* OK to allocate reserved blocks */
1741 {
1742         xfs_bmbt_rec_host_t     *ep;    /* extent record for idx */
1743         xfs_ifork_t             *ifp;   /* inode fork pointer */
1744         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
1745         xfs_filblks_t           newlen=0;       /* new indirect size */
1746         xfs_filblks_t           oldlen=0;       /* old indirect size */
1747         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
1748         int                     state;  /* state bits, accessed thru macros */
1749         xfs_filblks_t           temp=0; /* temp for indirect calculations */
1750         xfs_filblks_t           temp2=0;
1751
1752         ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1753         ep = xfs_iext_get_ext(ifp, idx);
1754         state = 0;
1755         ASSERT(isnullstartblock(new->br_startblock));
1756
1757         /*
1758          * Check and set flags if this segment has a left neighbor
1759          */
1760         if (idx > 0) {
1761                 state |= BMAP_LEFT_VALID;
1762                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1763
1764                 if (isnullstartblock(left.br_startblock))
1765                         state |= BMAP_LEFT_DELAY;
1766         }
1767
1768         /*
1769          * Check and set flags if the current (right) segment exists.
1770          * If it doesn't exist, we're converting the hole at end-of-file.
1771          */
1772         if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1773                 state |= BMAP_RIGHT_VALID;
1774                 xfs_bmbt_get_all(ep, &right);
1775
1776                 if (isnullstartblock(right.br_startblock))
1777                         state |= BMAP_RIGHT_DELAY;
1778         }
1779
1780         /*
1781          * Set contiguity flags on the left and right neighbors.
1782          * Don't let extents get too large, even if the pieces are contiguous.
1783          */
1784         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1785             left.br_startoff + left.br_blockcount == new->br_startoff &&
1786             left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1787                 state |= BMAP_LEFT_CONTIG;
1788
1789         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1790             new->br_startoff + new->br_blockcount == right.br_startoff &&
1791             new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1792             (!(state & BMAP_LEFT_CONTIG) ||
1793              (left.br_blockcount + new->br_blockcount +
1794               right.br_blockcount <= MAXEXTLEN)))
1795                 state |= BMAP_RIGHT_CONTIG;
1796
1797         /*
1798          * Switch out based on the contiguity flags.
1799          */
1800         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1801         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1802                 /*
1803                  * New allocation is contiguous with delayed allocations
1804                  * on the left and on the right.
1805                  * Merge all three into a single extent record.
1806                  */
1807                 temp = left.br_blockcount + new->br_blockcount +
1808                         right.br_blockcount;
1809
1810                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1811                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1812                 oldlen = startblockval(left.br_startblock) +
1813                         startblockval(new->br_startblock) +
1814                         startblockval(right.br_startblock);
1815                 newlen = xfs_bmap_worst_indlen(ip, temp);
1816                 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1817                         nullstartblock((int)newlen));
1818                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1819
1820                 xfs_iext_remove(ip, idx, 1, state);
1821                 ip->i_df.if_lastex = idx - 1;
1822                 /* DELTA: Two in-core extents were replaced by one. */
1823                 temp2 = temp;
1824                 temp = left.br_startoff;
1825                 break;
1826
1827         case BMAP_LEFT_CONTIG:
1828                 /*
1829                  * New allocation is contiguous with a delayed allocation
1830                  * on the left.
1831                  * Merge the new allocation with the left neighbor.
1832                  */
1833                 temp = left.br_blockcount + new->br_blockcount;
1834                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1835                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1836                 oldlen = startblockval(left.br_startblock) +
1837                         startblockval(new->br_startblock);
1838                 newlen = xfs_bmap_worst_indlen(ip, temp);
1839                 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1840                         nullstartblock((int)newlen));
1841                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1842
1843                 ip->i_df.if_lastex = idx - 1;
1844                 /* DELTA: One in-core extent grew into a hole. */
1845                 temp2 = temp;
1846                 temp = left.br_startoff;
1847                 break;
1848
1849         case BMAP_RIGHT_CONTIG:
1850                 /*
1851                  * New allocation is contiguous with a delayed allocation
1852                  * on the right.
1853                  * Merge the new allocation with the right neighbor.
1854                  */
1855                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1856                 temp = new->br_blockcount + right.br_blockcount;
1857                 oldlen = startblockval(new->br_startblock) +
1858                         startblockval(right.br_startblock);
1859                 newlen = xfs_bmap_worst_indlen(ip, temp);
1860                 xfs_bmbt_set_allf(ep, new->br_startoff,
1861                         nullstartblock((int)newlen), temp, right.br_state);
1862                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1863
1864                 ip->i_df.if_lastex = idx;
1865                 /* DELTA: One in-core extent grew into a hole. */
1866                 temp2 = temp;
1867                 temp = new->br_startoff;
1868                 break;
1869
1870         case 0:
1871                 /*
1872                  * New allocation is not contiguous with another
1873                  * delayed allocation.
1874                  * Insert a new entry.
1875                  */
1876                 oldlen = newlen = 0;
1877                 xfs_iext_insert(ip, idx, 1, new, state);
1878                 ip->i_df.if_lastex = idx;
1879                 /* DELTA: A new in-core extent was added in a hole. */
1880                 temp2 = new->br_blockcount;
1881                 temp = new->br_startoff;
1882                 break;
1883         }
1884         if (oldlen != newlen) {
1885                 ASSERT(oldlen > newlen);
1886                 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1887                         (int64_t)(oldlen - newlen), rsvd);
1888                 /*
1889                  * Nothing to do for disk quota accounting here.
1890                  */
1891         }
1892         if (delta) {
1893                 temp2 += temp;
1894                 if (delta->xed_startoff > temp)
1895                         delta->xed_startoff = temp;
1896                 if (delta->xed_blockcount < temp2)
1897                         delta->xed_blockcount = temp2;
1898         }
1899         *logflagsp = 0;
1900         return 0;
1901 }
1902
1903 /*
1904  * Called by xfs_bmap_add_extent to handle cases converting a hole
1905  * to a real allocation.
1906  */
1907 STATIC int                              /* error */
1908 xfs_bmap_add_extent_hole_real(
1909         xfs_inode_t             *ip,    /* incore inode pointer */
1910         xfs_extnum_t            idx,    /* extent number to update/insert */
1911         xfs_btree_cur_t         *cur,   /* if null, not a btree */
1912         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
1913         int                     *logflagsp, /* inode logging flags */
1914         xfs_extdelta_t          *delta, /* Change made to incore extents */
1915         int                     whichfork) /* data or attr fork */
1916 {
1917         xfs_bmbt_rec_host_t     *ep;    /* pointer to extent entry ins. point */
1918         int                     error;  /* error return value */
1919         int                     i;      /* temp state */
1920         xfs_ifork_t             *ifp;   /* inode fork pointer */
1921         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
1922         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
1923         int                     rval=0; /* return value (logging flags) */
1924         int                     state;  /* state bits, accessed thru macros */
1925         xfs_filblks_t           temp=0;
1926         xfs_filblks_t           temp2=0;
1927
1928         ifp = XFS_IFORK_PTR(ip, whichfork);
1929         ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1930         ep = xfs_iext_get_ext(ifp, idx);
1931         state = 0;
1932
1933         if (whichfork == XFS_ATTR_FORK)
1934                 state |= BMAP_ATTRFORK;
1935
1936         /*
1937          * Check and set flags if this segment has a left neighbor.
1938          */
1939         if (idx > 0) {
1940                 state |= BMAP_LEFT_VALID;
1941                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1942                 if (isnullstartblock(left.br_startblock))
1943                         state |= BMAP_LEFT_DELAY;
1944         }
1945
1946         /*
1947          * Check and set flags if this segment has a current value.
1948          * Not true if we're inserting into the "hole" at eof.
1949          */
1950         if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1951                 state |= BMAP_RIGHT_VALID;
1952                 xfs_bmbt_get_all(ep, &right);
1953                 if (isnullstartblock(right.br_startblock))
1954                         state |= BMAP_RIGHT_DELAY;
1955         }
1956
1957         /*
1958          * We're inserting a real allocation between "left" and "right".
1959          * Set the contiguity flags.  Don't let extents get too large.
1960          */
1961         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1962             left.br_startoff + left.br_blockcount == new->br_startoff &&
1963             left.br_startblock + left.br_blockcount == new->br_startblock &&
1964             left.br_state == new->br_state &&
1965             left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1966                 state |= BMAP_LEFT_CONTIG;
1967
1968         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1969             new->br_startoff + new->br_blockcount == right.br_startoff &&
1970             new->br_startblock + new->br_blockcount == right.br_startblock &&
1971             new->br_state == right.br_state &&
1972             new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1973             (!(state & BMAP_LEFT_CONTIG) ||
1974              left.br_blockcount + new->br_blockcount +
1975              right.br_blockcount <= MAXEXTLEN))
1976                 state |= BMAP_RIGHT_CONTIG;
1977
1978         error = 0;
1979         /*
1980          * Select which case we're in here, and implement it.
1981          */
1982         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1983         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1984                 /*
1985                  * New allocation is contiguous with real allocations on the
1986                  * left and on the right.
1987                  * Merge all three into a single extent record.
1988                  */
1989                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1990                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1991                         left.br_blockcount + new->br_blockcount +
1992                         right.br_blockcount);
1993                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1994
1995                 xfs_iext_remove(ip, idx, 1, state);
1996                 ifp->if_lastex = idx - 1;
1997                 XFS_IFORK_NEXT_SET(ip, whichfork,
1998                         XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1999                 if (cur == NULL) {
2000                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2001                 } else {
2002                         rval = XFS_ILOG_CORE;
2003                         if ((error = xfs_bmbt_lookup_eq(cur,
2004                                         right.br_startoff,
2005                                         right.br_startblock,
2006                                         right.br_blockcount, &i)))
2007                                 goto done;
2008                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2009                         if ((error = xfs_btree_delete(cur, &i)))
2010                                 goto done;
2011                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2012                         if ((error = xfs_btree_decrement(cur, 0, &i)))
2013                                 goto done;
2014                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2015                         if ((error = xfs_bmbt_update(cur, left.br_startoff,
2016                                         left.br_startblock,
2017                                         left.br_blockcount +
2018                                                 new->br_blockcount +
2019                                                 right.br_blockcount,
2020                                         left.br_state)))
2021                                 goto done;
2022                 }
2023                 /* DELTA: Two in-core extents were replaced by one. */
2024                 temp = left.br_startoff;
2025                 temp2 = left.br_blockcount +
2026                         new->br_blockcount +
2027                         right.br_blockcount;
2028                 break;
2029
2030         case BMAP_LEFT_CONTIG:
2031                 /*
2032                  * New allocation is contiguous with a real allocation
2033                  * on the left.
2034                  * Merge the new allocation with the left neighbor.
2035                  */
2036                 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
2037                 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2038                         left.br_blockcount + new->br_blockcount);
2039                 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
2040
2041                 ifp->if_lastex = idx - 1;
2042                 if (cur == NULL) {
2043                         rval = xfs_ilog_fext(whichfork);
2044                 } else {
2045                         rval = 0;
2046                         if ((error = xfs_bmbt_lookup_eq(cur,
2047                                         left.br_startoff,
2048                                         left.br_startblock,
2049                                         left.br_blockcount, &i)))
2050                                 goto done;
2051                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2052                         if ((error = xfs_bmbt_update(cur, left.br_startoff,
2053                                         left.br_startblock,
2054                                         left.br_blockcount +
2055                                                 new->br_blockcount,
2056                                         left.br_state)))
2057                                 goto done;
2058                 }
2059                 /* DELTA: One in-core extent grew. */
2060                 temp = left.br_startoff;
2061                 temp2 = left.br_blockcount +
2062                         new->br_blockcount;
2063                 break;
2064
2065         case BMAP_RIGHT_CONTIG:
2066                 /*
2067                  * New allocation is contiguous with a real allocation
2068                  * on the right.
2069                  * Merge the new allocation with the right neighbor.
2070                  */
2071                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2072                 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2073                         new->br_blockcount + right.br_blockcount,
2074                         right.br_state);
2075                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2076
2077                 ifp->if_lastex = idx;
2078                 if (cur == NULL) {
2079                         rval = xfs_ilog_fext(whichfork);
2080                 } else {
2081                         rval = 0;
2082                         if ((error = xfs_bmbt_lookup_eq(cur,
2083                                         right.br_startoff,
2084                                         right.br_startblock,
2085                                         right.br_blockcount, &i)))
2086                                 goto done;
2087                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2088                         if ((error = xfs_bmbt_update(cur, new->br_startoff,
2089                                         new->br_startblock,
2090                                         new->br_blockcount +
2091                                                 right.br_blockcount,
2092                                         right.br_state)))
2093                                 goto done;
2094                 }
2095                 /* DELTA: One in-core extent grew. */
2096                 temp = new->br_startoff;
2097                 temp2 = new->br_blockcount +
2098                         right.br_blockcount;
2099                 break;
2100
2101         case 0:
2102                 /*
2103                  * New allocation is not contiguous with another
2104                  * real allocation.
2105                  * Insert a new entry.
2106                  */
2107                 xfs_iext_insert(ip, idx, 1, new, state);
2108                 ifp->if_lastex = idx;
2109                 XFS_IFORK_NEXT_SET(ip, whichfork,
2110                         XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2111                 if (cur == NULL) {
2112                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2113                 } else {
2114                         rval = XFS_ILOG_CORE;
2115                         if ((error = xfs_bmbt_lookup_eq(cur,
2116                                         new->br_startoff,
2117                                         new->br_startblock,
2118                                         new->br_blockcount, &i)))
2119                                 goto done;
2120                         XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2121                         cur->bc_rec.b.br_state = new->br_state;
2122                         if ((error = xfs_btree_insert(cur, &i)))
2123                                 goto done;
2124                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2125                 }
2126                 /* DELTA: A new extent was added in a hole. */
2127                 temp = new->br_startoff;
2128                 temp2 = new->br_blockcount;
2129                 break;
2130         }
2131         if (delta) {
2132                 temp2 += temp;
2133                 if (delta->xed_startoff > temp)
2134                         delta->xed_startoff = temp;
2135                 if (delta->xed_blockcount < temp2)
2136                         delta->xed_blockcount = temp2;
2137         }
2138 done:
2139         *logflagsp = rval;
2140         return error;
2141 }
2142
2143 /*
2144  * Adjust the size of the new extent based on di_extsize and rt extsize.
2145  */
2146 STATIC int
2147 xfs_bmap_extsize_align(
2148         xfs_mount_t     *mp,
2149         xfs_bmbt_irec_t *gotp,          /* next extent pointer */
2150         xfs_bmbt_irec_t *prevp,         /* previous extent pointer */
2151         xfs_extlen_t    extsz,          /* align to this extent size */
2152         int             rt,             /* is this a realtime inode? */
2153         int             eof,            /* is extent at end-of-file? */
2154         int             delay,          /* creating delalloc extent? */
2155         int             convert,        /* overwriting unwritten extent? */
2156         xfs_fileoff_t   *offp,          /* in/out: aligned offset */
2157         xfs_extlen_t    *lenp)          /* in/out: aligned length */
2158 {
2159         xfs_fileoff_t   orig_off;       /* original offset */
2160         xfs_extlen_t    orig_alen;      /* original length */
2161         xfs_fileoff_t   orig_end;       /* original off+len */
2162         xfs_fileoff_t   nexto;          /* next file offset */
2163         xfs_fileoff_t   prevo;          /* previous file offset */
2164         xfs_fileoff_t   align_off;      /* temp for offset */
2165         xfs_extlen_t    align_alen;     /* temp for length */
2166         xfs_extlen_t    temp;           /* temp for calculations */
2167
2168         if (convert)
2169                 return 0;
2170
2171         orig_off = align_off = *offp;
2172         orig_alen = align_alen = *lenp;
2173         orig_end = orig_off + orig_alen;
2174
2175         /*
2176          * If this request overlaps an existing extent, then don't
2177          * attempt to perform any additional alignment.
2178          */
2179         if (!delay && !eof &&
2180             (orig_off >= gotp->br_startoff) &&
2181             (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2182                 return 0;
2183         }
2184
2185         /*
2186          * If the file offset is unaligned vs. the extent size
2187          * we need to align it.  This will be possible unless
2188          * the file was previously written with a kernel that didn't
2189          * perform this alignment, or if a truncate shot us in the
2190          * foot.
2191          */
2192         temp = do_mod(orig_off, extsz);
2193         if (temp) {
2194                 align_alen += temp;
2195                 align_off -= temp;
2196         }
2197         /*
2198          * Same adjustment for the end of the requested area.
2199          */
2200         if ((temp = (align_alen % extsz))) {
2201                 align_alen += extsz - temp;
2202         }
2203         /*
2204          * If the previous block overlaps with this proposed allocation
2205          * then move the start forward without adjusting the length.
2206          */
2207         if (prevp->br_startoff != NULLFILEOFF) {
2208                 if (prevp->br_startblock == HOLESTARTBLOCK)
2209                         prevo = prevp->br_startoff;
2210                 else
2211                         prevo = prevp->br_startoff + prevp->br_blockcount;
2212         } else
2213                 prevo = 0;
2214         if (align_off != orig_off && align_off < prevo)
2215                 align_off = prevo;
2216         /*
2217          * If the next block overlaps with this proposed allocation
2218          * then move the start back without adjusting the length,
2219          * but not before offset 0.
2220          * This may of course make the start overlap previous block,
2221          * and if we hit the offset 0 limit then the next block
2222          * can still overlap too.
2223          */
2224         if (!eof && gotp->br_startoff != NULLFILEOFF) {
2225                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2226                     (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2227                         nexto = gotp->br_startoff + gotp->br_blockcount;
2228                 else
2229                         nexto = gotp->br_startoff;
2230         } else
2231                 nexto = NULLFILEOFF;
2232         if (!eof &&
2233             align_off + align_alen != orig_end &&
2234             align_off + align_alen > nexto)
2235                 align_off = nexto > align_alen ? nexto - align_alen : 0;
2236         /*
2237          * If we're now overlapping the next or previous extent that
2238          * means we can't fit an extsz piece in this hole.  Just move
2239          * the start forward to the first valid spot and set
2240          * the length so we hit the end.
2241          */
2242         if (align_off != orig_off && align_off < prevo)
2243                 align_off = prevo;
2244         if (align_off + align_alen != orig_end &&
2245             align_off + align_alen > nexto &&
2246             nexto != NULLFILEOFF) {
2247                 ASSERT(nexto > prevo);
2248                 align_alen = nexto - align_off;
2249         }
2250
2251         /*
2252          * If realtime, and the result isn't a multiple of the realtime
2253          * extent size we need to remove blocks until it is.
2254          */
2255         if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2256                 /*
2257                  * We're not covering the original request, or
2258                  * we won't be able to once we fix the length.
2259                  */
2260                 if (orig_off < align_off ||
2261                     orig_end > align_off + align_alen ||
2262                     align_alen - temp < orig_alen)
2263                         return XFS_ERROR(EINVAL);
2264                 /*
2265                  * Try to fix it by moving the start up.
2266                  */
2267                 if (align_off + temp <= orig_off) {
2268                         align_alen -= temp;
2269                         align_off += temp;
2270                 }
2271                 /*
2272                  * Try to fix it by moving the end in.
2273                  */
2274                 else if (align_off + align_alen - temp >= orig_end)
2275                         align_alen -= temp;
2276                 /*
2277                  * Set the start to the minimum then trim the length.
2278                  */
2279                 else {
2280                         align_alen -= orig_off - align_off;
2281                         align_off = orig_off;
2282                         align_alen -= align_alen % mp->m_sb.sb_rextsize;
2283                 }
2284                 /*
2285                  * Result doesn't cover the request, fail it.
2286                  */
2287                 if (orig_off < align_off || orig_end > align_off + align_alen)
2288                         return XFS_ERROR(EINVAL);
2289         } else {
2290                 ASSERT(orig_off >= align_off);
2291                 ASSERT(orig_end <= align_off + align_alen);
2292         }
2293
2294 #ifdef DEBUG
2295         if (!eof && gotp->br_startoff != NULLFILEOFF)
2296                 ASSERT(align_off + align_alen <= gotp->br_startoff);
2297         if (prevp->br_startoff != NULLFILEOFF)
2298                 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2299 #endif
2300
2301         *lenp = align_alen;
2302         *offp = align_off;
2303         return 0;
2304 }
2305
2306 #define XFS_ALLOC_GAP_UNITS     4
2307
2308 STATIC void
2309 xfs_bmap_adjacent(
2310         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2311 {
2312         xfs_fsblock_t   adjust;         /* adjustment to block numbers */
2313         xfs_agnumber_t  fb_agno;        /* ag number of ap->firstblock */
2314         xfs_mount_t     *mp;            /* mount point structure */
2315         int             nullfb;         /* true if ap->firstblock isn't set */
2316         int             rt;             /* true if inode is realtime */
2317
2318 #define ISVALID(x,y)    \
2319         (rt ? \
2320                 (x) < mp->m_sb.sb_rblocks : \
2321                 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2322                 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2323                 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2324
2325         mp = ap->ip->i_mount;
2326         nullfb = ap->firstblock == NULLFSBLOCK;
2327         rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2328         fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2329         /*
2330          * If allocating at eof, and there's a previous real block,
2331          * try to use its last block as our starting point.
2332          */
2333         if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2334             !isnullstartblock(ap->prevp->br_startblock) &&
2335             ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2336                     ap->prevp->br_startblock)) {
2337                 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2338                 /*
2339                  * Adjust for the gap between prevp and us.
2340                  */
2341                 adjust = ap->off -
2342                         (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2343                 if (adjust &&
2344                     ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2345                         ap->rval += adjust;
2346         }
2347         /*
2348          * If not at eof, then compare the two neighbor blocks.
2349          * Figure out whether either one gives us a good starting point,
2350          * and pick the better one.
2351          */
2352         else if (!ap->eof) {
2353                 xfs_fsblock_t   gotbno;         /* right side block number */
2354                 xfs_fsblock_t   gotdiff=0;      /* right side difference */
2355                 xfs_fsblock_t   prevbno;        /* left side block number */
2356                 xfs_fsblock_t   prevdiff=0;     /* left side difference */
2357
2358                 /*
2359                  * If there's a previous (left) block, select a requested
2360                  * start block based on it.
2361                  */
2362                 if (ap->prevp->br_startoff != NULLFILEOFF &&
2363                     !isnullstartblock(ap->prevp->br_startblock) &&
2364                     (prevbno = ap->prevp->br_startblock +
2365                                ap->prevp->br_blockcount) &&
2366                     ISVALID(prevbno, ap->prevp->br_startblock)) {
2367                         /*
2368                          * Calculate gap to end of previous block.
2369                          */
2370                         adjust = prevdiff = ap->off -
2371                                 (ap->prevp->br_startoff +
2372                                  ap->prevp->br_blockcount);
2373                         /*
2374                          * Figure the startblock based on the previous block's
2375                          * end and the gap size.
2376                          * Heuristic!
2377                          * If the gap is large relative to the piece we're
2378                          * allocating, or using it gives us an invalid block
2379                          * number, then just use the end of the previous block.
2380                          */
2381                         if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2382                             ISVALID(prevbno + prevdiff,
2383                                     ap->prevp->br_startblock))
2384                                 prevbno += adjust;
2385                         else
2386                                 prevdiff += adjust;
2387                         /*
2388                          * If the firstblock forbids it, can't use it,
2389                          * must use default.
2390                          */
2391                         if (!rt && !nullfb &&
2392                             XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2393                                 prevbno = NULLFSBLOCK;
2394                 }
2395                 /*
2396                  * No previous block or can't follow it, just default.
2397                  */
2398                 else
2399                         prevbno = NULLFSBLOCK;
2400                 /*
2401                  * If there's a following (right) block, select a requested
2402                  * start block based on it.
2403                  */
2404                 if (!isnullstartblock(ap->gotp->br_startblock)) {
2405                         /*
2406                          * Calculate gap to start of next block.
2407                          */
2408                         adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2409                         /*
2410                          * Figure the startblock based on the next block's
2411                          * start and the gap size.
2412                          */
2413                         gotbno = ap->gotp->br_startblock;
2414                         /*
2415                          * Heuristic!
2416                          * If the gap is large relative to the piece we're
2417                          * allocating, or using it gives us an invalid block
2418                          * number, then just use the start of the next block
2419                          * offset by our length.
2420                          */
2421                         if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2422                             ISVALID(gotbno - gotdiff, gotbno))
2423                                 gotbno -= adjust;
2424                         else if (ISVALID(gotbno - ap->alen, gotbno)) {
2425                                 gotbno -= ap->alen;
2426                                 gotdiff += adjust - ap->alen;
2427                         } else
2428                                 gotdiff += adjust;
2429                         /*
2430                          * If the firstblock forbids it, can't use it,
2431                          * must use default.
2432                          */
2433                         if (!rt && !nullfb &&
2434                             XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2435                                 gotbno = NULLFSBLOCK;
2436                 }
2437                 /*
2438                  * No next block, just default.
2439                  */
2440                 else
2441                         gotbno = NULLFSBLOCK;
2442                 /*
2443                  * If both valid, pick the better one, else the only good
2444                  * one, else ap->rval is already set (to 0 or the inode block).
2445                  */
2446                 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2447                         ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2448                 else if (prevbno != NULLFSBLOCK)
2449                         ap->rval = prevbno;
2450                 else if (gotbno != NULLFSBLOCK)
2451                         ap->rval = gotbno;
2452         }
2453 #undef ISVALID
2454 }
2455
2456 STATIC int
2457 xfs_bmap_rtalloc(
2458         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2459 {
2460         xfs_alloctype_t atype = 0;      /* type for allocation routines */
2461         int             error;          /* error return value */
2462         xfs_mount_t     *mp;            /* mount point structure */
2463         xfs_extlen_t    prod = 0;       /* product factor for allocators */
2464         xfs_extlen_t    ralen = 0;      /* realtime allocation length */
2465         xfs_extlen_t    align;          /* minimum allocation alignment */
2466         xfs_rtblock_t   rtb;
2467
2468         mp = ap->ip->i_mount;
2469         align = xfs_get_extsz_hint(ap->ip);
2470         prod = align / mp->m_sb.sb_rextsize;
2471         error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2472                                         align, 1, ap->eof, 0,
2473                                         ap->conv, &ap->off, &ap->alen);
2474         if (error)
2475                 return error;
2476         ASSERT(ap->alen);
2477         ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2478
2479         /*
2480          * If the offset & length are not perfectly aligned
2481          * then kill prod, it will just get us in trouble.
2482          */
2483         if (do_mod(ap->off, align) || ap->alen % align)
2484                 prod = 1;
2485         /*
2486          * Set ralen to be the actual requested length in rtextents.
2487          */
2488         ralen = ap->alen / mp->m_sb.sb_rextsize;
2489         /*
2490          * If the old value was close enough to MAXEXTLEN that
2491          * we rounded up to it, cut it back so it's valid again.
2492          * Note that if it's a really large request (bigger than
2493          * MAXEXTLEN), we don't hear about that number, and can't
2494          * adjust the starting point to match it.
2495          */
2496         if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2497                 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2498         /*
2499          * If it's an allocation to an empty file at offset 0,
2500          * pick an extent that will space things out in the rt area.
2501          */
2502         if (ap->eof && ap->off == 0) {
2503                 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2504
2505                 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2506                 if (error)
2507                         return error;
2508                 ap->rval = rtx * mp->m_sb.sb_rextsize;
2509         } else {
2510                 ap->rval = 0;
2511         }
2512
2513         xfs_bmap_adjacent(ap);
2514
2515         /*
2516          * Realtime allocation, done through xfs_rtallocate_extent.
2517          */
2518         atype = ap->rval == 0 ?  XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2519         do_div(ap->rval, mp->m_sb.sb_rextsize);
2520         rtb = ap->rval;
2521         ap->alen = ralen;
2522         if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2523                                 &ralen, atype, ap->wasdel, prod, &rtb)))
2524                 return error;
2525         if (rtb == NULLFSBLOCK && prod > 1 &&
2526             (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2527                                            ap->alen, &ralen, atype,
2528                                            ap->wasdel, 1, &rtb)))
2529                 return error;
2530         ap->rval = rtb;
2531         if (ap->rval != NULLFSBLOCK) {
2532                 ap->rval *= mp->m_sb.sb_rextsize;
2533                 ralen *= mp->m_sb.sb_rextsize;
2534                 ap->alen = ralen;
2535                 ap->ip->i_d.di_nblocks += ralen;
2536                 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2537                 if (ap->wasdel)
2538                         ap->ip->i_delayed_blks -= ralen;
2539                 /*
2540                  * Adjust the disk quota also. This was reserved
2541                  * earlier.
2542                  */
2543                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2544                         ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2545                                         XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2546         } else {
2547                 ap->alen = 0;
2548         }
2549         return 0;
2550 }
2551
2552 STATIC int
2553 xfs_bmap_btalloc(
2554         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2555 {
2556         xfs_mount_t     *mp;            /* mount point structure */
2557         xfs_alloctype_t atype = 0;      /* type for allocation routines */
2558         xfs_extlen_t    align;          /* minimum allocation alignment */
2559         xfs_agnumber_t  ag;
2560         xfs_agnumber_t  fb_agno;        /* ag number of ap->firstblock */
2561         xfs_agnumber_t  startag;
2562         xfs_alloc_arg_t args;
2563         xfs_extlen_t    blen;
2564         xfs_extlen_t    nextminlen = 0;
2565         xfs_perag_t     *pag;
2566         int             nullfb;         /* true if ap->firstblock isn't set */
2567         int             isaligned;
2568         int             notinit;
2569         int             tryagain;
2570         int             error;
2571
2572         mp = ap->ip->i_mount;
2573         align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2574         if (unlikely(align)) {
2575                 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2576                                                 align, 0, ap->eof, 0, ap->conv,
2577                                                 &ap->off, &ap->alen);
2578                 ASSERT(!error);
2579                 ASSERT(ap->alen);
2580         }
2581         nullfb = ap->firstblock == NULLFSBLOCK;
2582         fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2583         if (nullfb) {
2584                 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2585                         ag = xfs_filestream_lookup_ag(ap->ip);
2586                         ag = (ag != NULLAGNUMBER) ? ag : 0;
2587                         ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2588                 } else {
2589                         ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2590                 }
2591         } else
2592                 ap->rval = ap->firstblock;
2593
2594         xfs_bmap_adjacent(ap);
2595
2596         /*
2597          * If allowed, use ap->rval; otherwise must use firstblock since
2598          * it's in the right allocation group.
2599          */
2600         if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2601                 ;
2602         else
2603                 ap->rval = ap->firstblock;
2604         /*
2605          * Normal allocation, done through xfs_alloc_vextent.
2606          */
2607         tryagain = isaligned = 0;
2608         args.tp = ap->tp;
2609         args.mp = mp;
2610         args.fsbno = ap->rval;
2611         args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2612         args.firstblock = ap->firstblock;
2613         blen = 0;
2614         if (nullfb) {
2615                 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2616                         args.type = XFS_ALLOCTYPE_NEAR_BNO;
2617                 else
2618                         args.type = XFS_ALLOCTYPE_START_BNO;
2619                 args.total = ap->total;
2620
2621                 /*
2622                  * Search for an allocation group with a single extent
2623                  * large enough for the request.
2624                  *
2625                  * If one isn't found, then adjust the minimum allocation
2626                  * size to the largest space found.
2627                  */
2628                 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2629                 if (startag == NULLAGNUMBER)
2630                         startag = ag = 0;
2631                 notinit = 0;
2632                 down_read(&mp->m_peraglock);
2633                 pag = xfs_perag_get(mp, ag);
2634                 while (blen < ap->alen) {
2635                         if (!pag->pagf_init &&
2636                             (error = xfs_alloc_pagf_init(mp, args.tp,
2637                                     ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2638                                 xfs_perag_put(pag);
2639                                 up_read(&mp->m_peraglock);
2640                                 return error;
2641                         }
2642                         /*
2643                          * See xfs_alloc_fix_freelist...
2644                          */
2645                         if (pag->pagf_init) {
2646                                 xfs_extlen_t    longest;
2647                                 longest = xfs_alloc_longest_free_extent(mp, pag);
2648                                 if (blen < longest)
2649                                         blen = longest;
2650                         } else
2651                                 notinit = 1;
2652
2653                         if (xfs_inode_is_filestream(ap->ip)) {
2654                                 if (blen >= ap->alen)
2655                                         break;
2656
2657                                 if (ap->userdata) {
2658                                         /*
2659                                          * If startag is an invalid AG, we've
2660                                          * come here once before and
2661                                          * xfs_filestream_new_ag picked the
2662                                          * best currently available.
2663                                          *
2664                                          * Don't continue looping, since we
2665                                          * could loop forever.
2666                                          */
2667                                         if (startag == NULLAGNUMBER)
2668                                                 break;
2669
2670                                         error = xfs_filestream_new_ag(ap, &ag);
2671                                         xfs_perag_put(pag);
2672                                         if (error) {
2673                                                 up_read(&mp->m_peraglock);
2674                                                 return error;
2675                                         }
2676
2677                                         /* loop again to set 'blen'*/
2678                                         startag = NULLAGNUMBER;
2679                                         pag = xfs_perag_get(mp, ag);
2680                                         continue;
2681                                 }
2682                         }
2683                         if (++ag == mp->m_sb.sb_agcount)
2684                                 ag = 0;
2685                         if (ag == startag)
2686                                 break;
2687                         xfs_perag_put(pag);
2688                         pag = xfs_perag_get(mp, ag);
2689                 }
2690                 xfs_perag_put(pag);
2691                 up_read(&mp->m_peraglock);
2692                 /*
2693                  * Since the above loop did a BUF_TRYLOCK, it is
2694                  * possible that there is space for this request.
2695                  */
2696                 if (notinit || blen < ap->minlen)
2697                         args.minlen = ap->minlen;
2698                 /*
2699                  * If the best seen length is less than the request
2700                  * length, use the best as the minimum.
2701                  */
2702                 else if (blen < ap->alen)
2703                         args.minlen = blen;
2704                 /*
2705                  * Otherwise we've seen an extent as big as alen,
2706                  * use that as the minimum.
2707                  */
2708                 else
2709                         args.minlen = ap->alen;
2710
2711                 /*
2712                  * set the failure fallback case to look in the selected
2713                  * AG as the stream may have moved.
2714                  */
2715                 if (xfs_inode_is_filestream(ap->ip))
2716                         ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2717         } else if (ap->low) {
2718                 if (xfs_inode_is_filestream(ap->ip))
2719                         args.type = XFS_ALLOCTYPE_FIRST_AG;
2720                 else
2721                         args.type = XFS_ALLOCTYPE_START_BNO;
2722                 args.total = args.minlen = ap->minlen;
2723         } else {
2724                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2725                 args.total = ap->total;
2726                 args.minlen = ap->minlen;
2727         }
2728         /* apply extent size hints if obtained earlier */
2729         if (unlikely(align)) {
2730                 args.prod = align;
2731                 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2732                         args.mod = (xfs_extlen_t)(args.prod - args.mod);
2733         } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2734                 args.prod = 1;
2735                 args.mod = 0;
2736         } else {
2737                 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2738                 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2739                         args.mod = (xfs_extlen_t)(args.prod - args.mod);
2740         }
2741         /*
2742          * If we are not low on available data blocks, and the
2743          * underlying logical volume manager is a stripe, and
2744          * the file offset is zero then try to allocate data
2745          * blocks on stripe unit boundary.
2746          * NOTE: ap->aeof is only set if the allocation length
2747          * is >= the stripe unit and the allocation offset is
2748          * at the end of file.
2749          */
2750         if (!ap->low && ap->aeof) {
2751                 if (!ap->off) {
2752                         args.alignment = mp->m_dalign;
2753                         atype = args.type;
2754                         isaligned = 1;
2755                         /*
2756                          * Adjust for alignment
2757                          */
2758                         if (blen > args.alignment && blen <= ap->alen)
2759                                 args.minlen = blen - args.alignment;
2760                         args.minalignslop = 0;
2761                 } else {
2762                         /*
2763                          * First try an exact bno allocation.
2764                          * If it fails then do a near or start bno
2765                          * allocation with alignment turned on.
2766                          */
2767                         atype = args.type;
2768                         tryagain = 1;
2769                         args.type = XFS_ALLOCTYPE_THIS_BNO;
2770                         args.alignment = 1;
2771                         /*
2772                          * Compute the minlen+alignment for the
2773                          * next case.  Set slop so that the value
2774                          * of minlen+alignment+slop doesn't go up
2775                          * between the calls.
2776                          */
2777                         if (blen > mp->m_dalign && blen <= ap->alen)
2778                                 nextminlen = blen - mp->m_dalign;
2779                         else
2780                                 nextminlen = args.minlen;
2781                         if (nextminlen + mp->m_dalign > args.minlen + 1)
2782                                 args.minalignslop =
2783                                         nextminlen + mp->m_dalign -
2784                                         args.minlen - 1;
2785                         else
2786                                 args.minalignslop = 0;
2787                 }
2788         } else {
2789                 args.alignment = 1;
2790                 args.minalignslop = 0;
2791         }
2792         args.minleft = ap->minleft;
2793         args.wasdel = ap->wasdel;
2794         args.isfl = 0;
2795         args.userdata = ap->userdata;
2796         if ((error = xfs_alloc_vextent(&args)))
2797                 return error;
2798         if (tryagain && args.fsbno == NULLFSBLOCK) {
2799                 /*
2800                  * Exact allocation failed. Now try with alignment
2801                  * turned on.
2802                  */
2803                 args.type = atype;
2804                 args.fsbno = ap->rval;
2805                 args.alignment = mp->m_dalign;
2806                 args.minlen = nextminlen;
2807                 args.minalignslop = 0;
2808                 isaligned = 1;
2809                 if ((error = xfs_alloc_vextent(&args)))
2810                         return error;
2811         }
2812         if (isaligned && args.fsbno == NULLFSBLOCK) {
2813                 /*
2814                  * allocation failed, so turn off alignment and
2815                  * try again.
2816                  */
2817                 args.type = atype;
2818                 args.fsbno = ap->rval;
2819                 args.alignment = 0;
2820                 if ((error = xfs_alloc_vextent(&args)))
2821                         return error;
2822         }
2823         if (args.fsbno == NULLFSBLOCK && nullfb &&
2824             args.minlen > ap->minlen) {
2825                 args.minlen = ap->minlen;
2826                 args.type = XFS_ALLOCTYPE_START_BNO;
2827                 args.fsbno = ap->rval;
2828                 if ((error = xfs_alloc_vextent(&args)))
2829                         return error;
2830         }
2831         if (args.fsbno == NULLFSBLOCK && nullfb) {
2832                 args.fsbno = 0;
2833                 args.type = XFS_ALLOCTYPE_FIRST_AG;
2834                 args.total = ap->minlen;
2835                 args.minleft = 0;
2836                 if ((error = xfs_alloc_vextent(&args)))
2837                         return error;
2838                 ap->low = 1;
2839         }
2840         if (args.fsbno != NULLFSBLOCK) {
2841                 ap->firstblock = ap->rval = args.fsbno;
2842                 ASSERT(nullfb || fb_agno == args.agno ||
2843                        (ap->low && fb_agno < args.agno));
2844                 ap->alen = args.len;
2845                 ap->ip->i_d.di_nblocks += args.len;
2846                 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2847                 if (ap->wasdel)
2848                         ap->ip->i_delayed_blks -= args.len;
2849                 /*
2850                  * Adjust the disk quota also. This was reserved
2851                  * earlier.
2852                  */
2853                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2854                         ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2855                                         XFS_TRANS_DQ_BCOUNT,
2856                         (long) args.len);
2857         } else {
2858                 ap->rval = NULLFSBLOCK;
2859                 ap->alen = 0;
2860         }
2861         return 0;
2862 }
2863
2864 /*
2865  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2866  * It figures out where to ask the underlying allocator to put the new extent.
2867  */
2868 STATIC int
2869 xfs_bmap_alloc(
2870         xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
2871 {
2872         if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2873                 return xfs_bmap_rtalloc(ap);
2874         return xfs_bmap_btalloc(ap);
2875 }
2876
2877 /*
2878  * Transform a btree format file with only one leaf node, where the
2879  * extents list will fit in the inode, into an extents format file.
2880  * Since the file extents are already in-core, all we have to do is
2881  * give up the space for the btree root and pitch the leaf block.
2882  */
2883 STATIC int                              /* error */
2884 xfs_bmap_btree_to_extents(
2885         xfs_trans_t             *tp,    /* transaction pointer */
2886         xfs_inode_t             *ip,    /* incore inode pointer */
2887         xfs_btree_cur_t         *cur,   /* btree cursor */
2888         int                     *logflagsp, /* inode logging flags */
2889         int                     whichfork)  /* data or attr fork */
2890 {
2891         /* REFERENCED */
2892         struct xfs_btree_block  *cblock;/* child btree block */
2893         xfs_fsblock_t           cbno;   /* child block number */
2894         xfs_buf_t               *cbp;   /* child block's buffer */
2895         int                     error;  /* error return value */
2896         xfs_ifork_t             *ifp;   /* inode fork data */
2897         xfs_mount_t             *mp;    /* mount point structure */
2898         __be64                  *pp;    /* ptr to block address */
2899         struct xfs_btree_block  *rblock;/* root btree block */
2900
2901         mp = ip->i_mount;
2902         ifp = XFS_IFORK_PTR(ip, whichfork);
2903         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2904         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2905         rblock = ifp->if_broot;
2906         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2907         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2908         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2909         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2910         cbno = be64_to_cpu(*pp);
2911         *logflagsp = 0;
2912 #ifdef DEBUG
2913         if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2914                 return error;
2915 #endif
2916         if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2917                         XFS_BMAP_BTREE_REF)))
2918                 return error;
2919         cblock = XFS_BUF_TO_BLOCK(cbp);
2920         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2921                 return error;
2922         xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2923         ip->i_d.di_nblocks--;
2924         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2925         xfs_trans_binval(tp, cbp);
2926         if (cur->bc_bufs[0] == cbp)
2927                 cur->bc_bufs[0] = NULL;
2928         xfs_iroot_realloc(ip, -1, whichfork);
2929         ASSERT(ifp->if_broot == NULL);
2930         ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2931         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2932         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2933         return 0;
2934 }
2935
2936 /*
2937  * Called by xfs_bmapi to update file extent records and the btree
2938  * after removing space (or undoing a delayed allocation).
2939  */
2940 STATIC int                              /* error */
2941 xfs_bmap_del_extent(
2942         xfs_inode_t             *ip,    /* incore inode pointer */
2943         xfs_trans_t             *tp,    /* current transaction pointer */
2944         xfs_extnum_t            idx,    /* extent number to update/delete */
2945         xfs_bmap_free_t         *flist, /* list of extents to be freed */
2946         xfs_btree_cur_t         *cur,   /* if null, not a btree */
2947         xfs_bmbt_irec_t         *del,   /* data to remove from extents */
2948         int                     *logflagsp, /* inode logging flags */
2949         xfs_extdelta_t          *delta, /* Change made to incore extents */
2950         int                     whichfork, /* data or attr fork */
2951         int                     rsvd)   /* OK to allocate reserved blocks */
2952 {
2953         xfs_filblks_t           da_new; /* new delay-alloc indirect blocks */
2954         xfs_filblks_t           da_old; /* old delay-alloc indirect blocks */
2955         xfs_fsblock_t           del_endblock=0; /* first block past del */
2956         xfs_fileoff_t           del_endoff;     /* first offset past del */
2957         int                     delay;  /* current block is delayed allocated */
2958         int                     do_fx;  /* free extent at end of routine */
2959         xfs_bmbt_rec_host_t     *ep;    /* current extent entry pointer */
2960         int                     error;  /* error return value */
2961         int                     flags;  /* inode logging flags */
2962         xfs_bmbt_irec_t         got;    /* current extent entry */
2963         xfs_fileoff_t           got_endoff;     /* first offset past got */
2964         int                     i;      /* temp state */
2965         xfs_ifork_t             *ifp;   /* inode fork pointer */
2966         xfs_mount_t             *mp;    /* mount structure */
2967         xfs_filblks_t           nblks;  /* quota/sb block count */
2968         xfs_bmbt_irec_t         new;    /* new record to be inserted */
2969         /* REFERENCED */
2970         uint                    qfield; /* quota field to update */
2971         xfs_filblks_t           temp;   /* for indirect length calculations */
2972         xfs_filblks_t           temp2;  /* for indirect length calculations */
2973         int                     state = 0;
2974
2975         XFS_STATS_INC(xs_del_exlist);
2976
2977         if (whichfork == XFS_ATTR_FORK)
2978                 state |= BMAP_ATTRFORK;
2979
2980         mp = ip->i_mount;
2981         ifp = XFS_IFORK_PTR(ip, whichfork);
2982         ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2983                 (uint)sizeof(xfs_bmbt_rec_t)));
2984         ASSERT(del->br_blockcount > 0);
2985         ep = xfs_iext_get_ext(ifp, idx);
2986         xfs_bmbt_get_all(ep, &got);
2987         ASSERT(got.br_startoff <= del->br_startoff);
2988         del_endoff = del->br_startoff + del->br_blockcount;
2989         got_endoff = got.br_startoff + got.br_blockcount;
2990         ASSERT(got_endoff >= del_endoff);
2991         delay = isnullstartblock(got.br_startblock);
2992         ASSERT(isnullstartblock(del->br_startblock) == delay);
2993         flags = 0;
2994         qfield = 0;
2995         error = 0;
2996         /*
2997          * If deleting a real allocation, must free up the disk space.
2998          */
2999         if (!delay) {
3000                 flags = XFS_ILOG_CORE;
3001                 /*
3002                  * Realtime allocation.  Free it and record di_nblocks update.
3003                  */
3004                 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3005                         xfs_fsblock_t   bno;
3006                         xfs_filblks_t   len;
3007
3008                         ASSERT(do_mod(del->br_blockcount,
3009                                       mp->m_sb.sb_rextsize) == 0);
3010                         ASSERT(do_mod(del->br_startblock,
3011                                       mp->m_sb.sb_rextsize) == 0);
3012                         bno = del->br_startblock;
3013                         len = del->br_blockcount;
3014                         do_div(bno, mp->m_sb.sb_rextsize);
3015                         do_div(len, mp->m_sb.sb_rextsize);
3016                         if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3017                                         (xfs_extlen_t)len)))
3018                                 goto done;
3019                         do_fx = 0;
3020                         nblks = len * mp->m_sb.sb_rextsize;
3021                         qfield = XFS_TRANS_DQ_RTBCOUNT;
3022                 }
3023                 /*
3024                  * Ordinary allocation.
3025                  */
3026                 else {
3027                         do_fx = 1;
3028                         nblks = del->br_blockcount;
3029                         qfield = XFS_TRANS_DQ_BCOUNT;
3030                 }
3031                 /*
3032                  * Set up del_endblock and cur for later.
3033                  */
3034                 del_endblock = del->br_startblock + del->br_blockcount;
3035                 if (cur) {
3036                         if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3037                                         got.br_startblock, got.br_blockcount,
3038                                         &i)))
3039                                 goto done;
3040                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3041                 }
3042                 da_old = da_new = 0;
3043         } else {
3044                 da_old = startblockval(got.br_startblock);
3045                 da_new = 0;
3046                 nblks = 0;
3047                 do_fx = 0;
3048         }
3049         /*
3050          * Set flag value to use in switch statement.
3051          * Left-contig is 2, right-contig is 1.
3052          */
3053         switch (((got.br_startoff == del->br_startoff) << 1) |
3054                 (got_endoff == del_endoff)) {
3055         case 3:
3056                 /*
3057                  * Matches the whole extent.  Delete the entry.
3058                  */
3059                 xfs_iext_remove(ip, idx, 1,
3060                                 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
3061                 ifp->if_lastex = idx;
3062                 if (delay)
3063                         break;
3064                 XFS_IFORK_NEXT_SET(ip, whichfork,
3065                         XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3066                 flags |= XFS_ILOG_CORE;
3067                 if (!cur) {
3068                         flags |= xfs_ilog_fext(whichfork);
3069                         break;
3070                 }
3071                 if ((error = xfs_btree_delete(cur, &i)))
3072                         goto done;
3073                 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3074                 break;
3075
3076         case 2:
3077                 /*
3078                  * Deleting the first part of the extent.
3079                  */
3080                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3081                 xfs_bmbt_set_startoff(ep, del_endoff);
3082                 temp = got.br_blockcount - del->br_blockcount;
3083                 xfs_bmbt_set_blockcount(ep, temp);
3084                 ifp->if_lastex = idx;
3085                 if (delay) {
3086                         temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3087                                 da_old);
3088                         xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3089                         trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3090                         da_new = temp;
3091                         break;
3092                 }
3093                 xfs_bmbt_set_startblock(ep, del_endblock);
3094                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3095                 if (!cur) {
3096                         flags |= xfs_ilog_fext(whichfork);
3097                         break;
3098                 }
3099                 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3100                                 got.br_blockcount - del->br_blockcount,
3101                                 got.br_state)))
3102                         goto done;
3103                 break;
3104
3105         case 1:
3106                 /*
3107                  * Deleting the last part of the extent.
3108                  */
3109                 temp = got.br_blockcount - del->br_blockcount;
3110                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3111                 xfs_bmbt_set_blockcount(ep, temp);
3112                 ifp->if_lastex = idx;
3113                 if (delay) {
3114                         temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3115                                 da_old);
3116                         xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3117                         trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3118                         da_new = temp;
3119                         break;
3120                 }
3121                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3122                 if (!cur) {
3123                         flags |= xfs_ilog_fext(whichfork);
3124                         break;
3125                 }
3126                 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3127                                 got.br_startblock,
3128                                 got.br_blockcount - del->br_blockcount,
3129                                 got.br_state)))
3130                         goto done;
3131                 break;
3132
3133         case 0:
3134                 /*
3135                  * Deleting the middle of the extent.
3136                  */
3137                 temp = del->br_startoff - got.br_startoff;
3138                 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3139                 xfs_bmbt_set_blockcount(ep, temp);
3140                 new.br_startoff = del_endoff;
3141                 temp2 = got_endoff - del_endoff;
3142                 new.br_blockcount = temp2;
3143                 new.br_state = got.br_state;
3144                 if (!delay) {
3145                         new.br_startblock = del_endblock;
3146                         flags |= XFS_ILOG_CORE;
3147                         if (cur) {
3148                                 if ((error = xfs_bmbt_update(cur,
3149                                                 got.br_startoff,
3150                                                 got.br_startblock, temp,
3151                                                 got.br_state)))
3152                                         goto done;
3153                                 if ((error = xfs_btree_increment(cur, 0, &i)))
3154                                         goto done;
3155                                 cur->bc_rec.b = new;
3156                                 error = xfs_btree_insert(cur, &i);
3157                                 if (error && error != ENOSPC)
3158                                         goto done;
3159                                 /*
3160                                  * If get no-space back from btree insert,
3161                                  * it tried a split, and we have a zero
3162                                  * block reservation.
3163                                  * Fix up our state and return the error.
3164                                  */
3165                                 if (error == ENOSPC) {
3166                                         /*
3167                                          * Reset the cursor, don't trust
3168                                          * it after any insert operation.
3169                                          */
3170                                         if ((error = xfs_bmbt_lookup_eq(cur,
3171                                                         got.br_startoff,
3172                                                         got.br_startblock,
3173                                                         temp, &i)))
3174                                                 goto done;
3175                                         XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3176                                         /*
3177                                          * Update the btree record back
3178                                          * to the original value.
3179                                          */
3180                                         if ((error = xfs_bmbt_update(cur,
3181                                                         got.br_startoff,
3182                                                         got.br_startblock,
3183                                                         got.br_blockcount,
3184                                                         got.br_state)))
3185                                                 goto done;
3186                                         /*
3187                                          * Reset the extent record back
3188                                          * to the original value.
3189                                          */
3190                                         xfs_bmbt_set_blockcount(ep,
3191                                                 got.br_blockcount);
3192                                         flags = 0;
3193                                         error = XFS_ERROR(ENOSPC);
3194                                         goto done;
3195                                 }
3196                                 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3197                         } else
3198                                 flags |= xfs_ilog_fext(whichfork);
3199                         XFS_IFORK_NEXT_SET(ip, whichfork,
3200                                 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3201                 } else {
3202                         ASSERT(whichfork == XFS_DATA_FORK);
3203                         temp = xfs_bmap_worst_indlen(ip, temp);
3204                         xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3205                         temp2 = xfs_bmap_worst_indlen(ip, temp2);
3206                         new.br_startblock = nullstartblock((int)temp2);
3207                         da_new = temp + temp2;
3208                         while (da_new > da_old) {
3209                                 if (temp) {
3210                                         temp--;
3211                                         da_new--;
3212                                         xfs_bmbt_set_startblock(ep,
3213                                                 nullstartblock((int)temp));
3214                                 }
3215                                 if (da_new == da_old)
3216                                         break;
3217                                 if (temp2) {
3218                                         temp2--;
3219                                         da_new--;
3220                                         new.br_startblock =
3221                                                 nullstartblock((int)temp2);
3222                                 }
3223                         }
3224                 }
3225                 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3226                 xfs_iext_insert(ip, idx + 1, 1, &new, state);
3227                 ifp->if_lastex = idx + 1;
3228                 break;
3229         }
3230         /*
3231          * If we need to, add to list of extents to delete.
3232          */
3233         if (do_fx)
3234                 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3235                         mp);
3236         /*
3237          * Adjust inode # blocks in the file.
3238          */
3239         if (nblks)
3240                 ip->i_d.di_nblocks -= nblks;
3241         /*
3242          * Adjust quota data.
3243          */
3244         if (qfield)
3245                 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3246
3247         /*
3248          * Account for change in delayed indirect blocks.
3249          * Nothing to do for disk quota accounting here.
3250          */
3251         ASSERT(da_old >= da_new);
3252         if (da_old > da_new)
3253                 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3254                         rsvd);
3255         if (delta) {
3256                 /* DELTA: report the original extent. */
3257                 if (delta->xed_startoff > got.br_startoff)
3258                         delta->xed_startoff = got.br_startoff;
3259                 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3260                         delta->xed_blockcount = got.br_startoff +
3261                                                         got.br_blockcount;
3262         }
3263 done:
3264         *logflagsp = flags;
3265         return error;
3266 }
3267
3268 /*
3269  * Remove the entry "free" from the free item list.  Prev points to the
3270  * previous entry, unless "free" is the head of the list.
3271  */
3272 STATIC void
3273 xfs_bmap_del_free(
3274         xfs_bmap_free_t         *flist, /* free item list header */
3275         xfs_bmap_free_item_t    *prev,  /* previous item on list, if any */
3276         xfs_bmap_free_item_t    *free)  /* list item to be freed */
3277 {
3278         if (prev)
3279                 prev->xbfi_next = free->xbfi_next;
3280         else
3281                 flist->xbf_first = free->xbfi_next;
3282         flist->xbf_count--;
3283         kmem_zone_free(xfs_bmap_free_item_zone, free);
3284 }
3285
3286 /*
3287  * Convert an extents-format file into a btree-format file.
3288  * The new file will have a root block (in the inode) and a single child block.
3289  */
3290 STATIC int                                      /* error */
3291 xfs_bmap_extents_to_btree(
3292         xfs_trans_t             *tp,            /* transaction pointer */
3293         xfs_inode_t             *ip,            /* incore inode pointer */
3294         xfs_fsblock_t           *firstblock,    /* first-block-allocated */
3295         xfs_bmap_free_t         *flist,         /* blocks freed in xaction */
3296         xfs_btree_cur_t         **curp,         /* cursor returned to caller */
3297         int                     wasdel,         /* converting a delayed alloc */
3298         int                     *logflagsp,     /* inode logging flags */
3299         int                     whichfork)      /* data or attr fork */
3300 {
3301         struct xfs_btree_block  *ablock;        /* allocated (child) bt block */
3302         xfs_buf_t               *abp;           /* buffer for ablock */
3303         xfs_alloc_arg_t         args;           /* allocation arguments */
3304         xfs_bmbt_rec_t          *arp;           /* child record pointer */
3305         struct xfs_btree_block  *block;         /* btree root block */
3306         xfs_btree_cur_t         *cur;           /* bmap btree cursor */
3307         xfs_bmbt_rec_host_t     *ep;            /* extent record pointer */
3308         int                     error;          /* error return value */
3309         xfs_extnum_t            i, cnt;         /* extent record index */
3310         xfs_ifork_t             *ifp;           /* inode fork pointer */
3311         xfs_bmbt_key_t          *kp;            /* root block key pointer */
3312         xfs_mount_t             *mp;            /* mount structure */
3313         xfs_extnum_t            nextents;       /* number of file extents */
3314         xfs_bmbt_ptr_t          *pp;            /* root block address pointer */
3315
3316         ifp = XFS_IFORK_PTR(ip, whichfork);
3317         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3318         ASSERT(ifp->if_ext_max ==
3319                XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3320         /*
3321          * Make space in the inode incore.
3322          */
3323         xfs_iroot_realloc(ip, 1, whichfork);
3324         ifp->if_flags |= XFS_IFBROOT;
3325
3326         /*
3327          * Fill in the root.
3328          */
3329         block = ifp->if_broot;
3330         block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3331         block->bb_level = cpu_to_be16(1);
3332         block->bb_numrecs = cpu_to_be16(1);
3333         block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3334         block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3335
3336         /*
3337          * Need a cursor.  Can't allocate until bb_level is filled in.
3338          */
3339         mp = ip->i_mount;
3340         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3341         cur->bc_private.b.firstblock = *firstblock;
3342         cur->bc_private.b.flist = flist;
3343         cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3344         /*
3345          * Convert to a btree with two levels, one record in root.
3346          */
3347         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3348         args.tp = tp;
3349         args.mp = mp;
3350         args.firstblock = *firstblock;
3351         if (*firstblock == NULLFSBLOCK) {
3352                 args.type = XFS_ALLOCTYPE_START_BNO;
3353                 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3354         } else if (flist->xbf_low) {
3355                 args.type = XFS_ALLOCTYPE_START_BNO;
3356                 args.fsbno = *firstblock;
3357         } else {
3358                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3359                 args.fsbno = *firstblock;
3360         }
3361         args.minlen = args.maxlen = args.prod = 1;
3362         args.total = args.minleft = args.alignment = args.mod = args.isfl =
3363                 args.minalignslop = 0;
3364         args.wasdel = wasdel;
3365         *logflagsp = 0;
3366         if ((error = xfs_alloc_vextent(&args))) {
3367                 xfs_iroot_realloc(ip, -1, whichfork);
3368                 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3369                 return error;
3370         }
3371         /*
3372          * Allocation can't fail, the space was reserved.
3373          */
3374         ASSERT(args.fsbno != NULLFSBLOCK);
3375         ASSERT(*firstblock == NULLFSBLOCK ||
3376                args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3377                (flist->xbf_low &&
3378                 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3379         *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3380         cur->bc_private.b.allocated++;
3381         ip->i_d.di_nblocks++;
3382         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3383         abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3384         /*
3385          * Fill in the child block.
3386          */
3387         ablock = XFS_BUF_TO_BLOCK(abp);
3388         ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3389         ablock->bb_level = 0;
3390         ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3391         ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3392         arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3393         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3394         for (cnt = i = 0; i < nextents; i++) {
3395                 ep = xfs_iext_get_ext(ifp, i);
3396                 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3397                         arp->l0 = cpu_to_be64(ep->l0);
3398                         arp->l1 = cpu_to_be64(ep->l1);
3399                         arp++; cnt++;
3400                 }
3401         }
3402         ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3403         xfs_btree_set_numrecs(ablock, cnt);
3404
3405         /*
3406          * Fill in the root key and pointer.
3407          */
3408         kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3409         arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3410         kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3411         pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3412                                                 be16_to_cpu(block->bb_level)));
3413         *pp = cpu_to_be64(args.fsbno);
3414
3415         /*
3416          * Do all this logging at the end so that
3417          * the root is at the right level.
3418          */
3419         xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3420         xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3421         ASSERT(*curp == NULL);
3422         *curp = cur;
3423         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3424         return 0;
3425 }
3426
3427 /*
3428  * Calculate the default attribute fork offset for newly created inodes.
3429  */
3430 uint
3431 xfs_default_attroffset(
3432         struct xfs_inode        *ip)
3433 {
3434         struct xfs_mount        *mp = ip->i_mount;
3435         uint                    offset;
3436
3437         if (mp->m_sb.sb_inodesize == 256) {
3438                 offset = XFS_LITINO(mp) -
3439                                 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3440         } else {
3441                 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3442         }
3443
3444         ASSERT(offset < XFS_LITINO(mp));
3445         return offset;
3446 }
3447
3448 /*
3449  * Helper routine to reset inode di_forkoff field when switching
3450  * attribute fork from local to extent format - we reset it where
3451  * possible to make space available for inline data fork extents.
3452  */
3453 STATIC void
3454 xfs_bmap_forkoff_reset(
3455         xfs_mount_t     *mp,
3456         xfs_inode_t     *ip,
3457         int             whichfork)
3458 {
3459         if (whichfork == XFS_ATTR_FORK &&
3460             ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3461             ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3462             ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3463                 uint    dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3464
3465                 if (dfl_forkoff > ip->i_d.di_forkoff) {
3466                         ip->i_d.di_forkoff = dfl_forkoff;
3467                         ip->i_df.if_ext_max =
3468                                 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3469                         ip->i_afp->if_ext_max =
3470                                 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3471                 }
3472         }
3473 }
3474
3475 /*
3476  * Convert a local file to an extents file.
3477  * This code is out of bounds for data forks of regular files,
3478  * since the file data needs to get logged so things will stay consistent.
3479  * (The bmap-level manipulations are ok, though).
3480  */
3481 STATIC int                              /* error */
3482 xfs_bmap_local_to_extents(
3483         xfs_trans_t     *tp,            /* transaction pointer */
3484         xfs_inode_t     *ip,            /* incore inode pointer */
3485         xfs_fsblock_t   *firstblock,    /* first block allocated in xaction */
3486         xfs_extlen_t    total,          /* total blocks needed by transaction */
3487         int             *logflagsp,     /* inode logging flags */
3488         int             whichfork)      /* data or attr fork */
3489 {
3490         int             error;          /* error return value */
3491         int             flags;          /* logging flags returned */
3492         xfs_ifork_t     *ifp;           /* inode fork pointer */
3493
3494         /*
3495          * We don't want to deal with the case of keeping inode data inline yet.
3496          * So sending the data fork of a regular inode is invalid.
3497          */
3498         ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3499                  whichfork == XFS_DATA_FORK));
3500         ifp = XFS_IFORK_PTR(ip, whichfork);
3501         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3502         flags = 0;
3503         error = 0;
3504         if (ifp->if_bytes) {
3505                 xfs_alloc_arg_t args;   /* allocation arguments */
3506                 xfs_buf_t       *bp;    /* buffer for extent block */
3507                 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3508
3509                 args.tp = tp;
3510                 args.mp = ip->i_mount;
3511                 args.firstblock = *firstblock;
3512                 ASSERT((ifp->if_flags &
3513                         (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3514                 /*
3515                  * Allocate a block.  We know we need only one, since the
3516                  * file currently fits in an inode.
3517                  */
3518                 if (*firstblock == NULLFSBLOCK) {
3519                         args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3520                         args.type = XFS_ALLOCTYPE_START_BNO;
3521                 } else {
3522                         args.fsbno = *firstblock;
3523                         args.type = XFS_ALLOCTYPE_NEAR_BNO;
3524                 }
3525                 args.total = total;
3526                 args.mod = args.minleft = args.alignment = args.wasdel =
3527                         args.isfl = args.minalignslop = 0;
3528                 args.minlen = args.maxlen = args.prod = 1;
3529                 if ((error = xfs_alloc_vextent(&args)))
3530                         goto done;
3531                 /*
3532                  * Can't fail, the space was reserved.
3533                  */
3534                 ASSERT(args.fsbno != NULLFSBLOCK);
3535                 ASSERT(args.len == 1);
3536                 *firstblock = args.fsbno;
3537                 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3538                 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3539                         ifp->if_bytes);
3540                 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3541                 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3542                 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3543                 xfs_iext_add(ifp, 0, 1);
3544                 ep = xfs_iext_get_ext(ifp, 0);
3545                 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3546                 trace_xfs_bmap_post_update(ip, 0,
3547                                 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3548                                 _THIS_IP_);
3549                 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3550                 ip->i_d.di_nblocks = 1;
3551                 xfs_trans_mod_dquot_byino(tp, ip,
3552                         XFS_TRANS_DQ_BCOUNT, 1L);
3553                 flags |= xfs_ilog_fext(whichfork);
3554         } else {
3555                 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3556                 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3557         }
3558         ifp->if_flags &= ~XFS_IFINLINE;
3559         ifp->if_flags |= XFS_IFEXTENTS;
3560         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3561         flags |= XFS_ILOG_CORE;
3562 done:
3563         *logflagsp = flags;
3564         return error;
3565 }
3566
3567 /*
3568  * Search the extent records for the entry containing block bno.
3569  * If bno lies in a hole, point to the next entry.  If bno lies
3570  * past eof, *eofp will be set, and *prevp will contain the last
3571  * entry (null if none).  Else, *lastxp will be set to the index
3572  * of the found entry; *gotp will contain the entry.
3573  */
3574 STATIC xfs_bmbt_rec_host_t *            /* pointer to found extent entry */
3575 xfs_bmap_search_multi_extents(
3576         xfs_ifork_t     *ifp,           /* inode fork pointer */
3577         xfs_fileoff_t   bno,            /* block number searched for */
3578         int             *eofp,          /* out: end of file found */
3579         xfs_extnum_t    *lastxp,        /* out: last extent index */
3580         xfs_bmbt_irec_t *gotp,          /* out: extent entry found */
3581         xfs_bmbt_irec_t *prevp)         /* out: previous extent entry found */
3582 {
3583         xfs_bmbt_rec_host_t *ep;                /* extent record pointer */
3584         xfs_extnum_t    lastx;          /* last extent index */
3585
3586         /*
3587          * Initialize the extent entry structure to catch access to
3588          * uninitialized br_startblock field.
3589          */
3590         gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3591         gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3592         gotp->br_state = XFS_EXT_INVALID;
3593 #if XFS_BIG_BLKNOS
3594         gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3595 #else
3596         gotp->br_startblock = 0xffffa5a5;
3597 #endif
3598         prevp->br_startoff = NULLFILEOFF;
3599
3600         ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3601         if (lastx > 0) {
3602                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3603         }
3604         if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3605                 xfs_bmbt_get_all(ep, gotp);
3606                 *eofp = 0;
3607         } else {
3608                 if (lastx > 0) {
3609                         *gotp = *prevp;
3610                 }
3611                 *eofp = 1;
3612                 ep = NULL;
3613         }
3614         *lastxp = lastx;
3615         return ep;
3616 }
3617
3618 /*
3619  * Search the extents list for the inode, for the extent containing bno.
3620  * If bno lies in a hole, point to the next entry.  If bno lies past eof,
3621  * *eofp will be set, and *prevp will contain the last entry (null if none).
3622  * Else, *lastxp will be set to the index of the found
3623  * entry; *gotp will contain the entry.
3624  */
3625 STATIC xfs_bmbt_rec_host_t *                 /* pointer to found extent entry */
3626 xfs_bmap_search_extents(
3627         xfs_inode_t     *ip,            /* incore inode pointer */
3628         xfs_fileoff_t   bno,            /* block number searched for */
3629         int             fork,           /* data or attr fork */
3630         int             *eofp,          /* out: end of file found */
3631         xfs_extnum_t    *lastxp,        /* out: last extent index */
3632         xfs_bmbt_irec_t *gotp,          /* out: extent entry found */
3633         xfs_bmbt_irec_t *prevp)         /* out: previous extent entry found */
3634 {
3635         xfs_ifork_t     *ifp;           /* inode fork pointer */
3636         xfs_bmbt_rec_host_t  *ep;            /* extent record pointer */
3637
3638         XFS_STATS_INC(xs_look_exlist);
3639         ifp = XFS_IFORK_PTR(ip, fork);
3640
3641         ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3642
3643         if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3644                      !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3645                 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3646                                 "Access to block zero in inode %llu "
3647                                 "start_block: %llx start_off: %llx "
3648                                 "blkcnt: %llx extent-state: %x lastx: %x\n",
3649                         (unsigned long long)ip->i_ino,
3650                         (unsigned long long)gotp->br_startblock,
3651                         (unsigned long long)gotp->br_startoff,
3652                         (unsigned long long)gotp->br_blockcount,
3653                         gotp->br_state, *lastxp);
3654                 *lastxp = NULLEXTNUM;
3655                 *eofp = 1;
3656                 return NULL;
3657         }
3658         return ep;
3659 }
3660
3661 /*
3662  * Compute the worst-case number of indirect blocks that will be used
3663  * for ip's delayed extent of length "len".
3664  */
3665 STATIC xfs_filblks_t
3666 xfs_bmap_worst_indlen(
3667         xfs_inode_t     *ip,            /* incore inode pointer */
3668         xfs_filblks_t   len)            /* delayed extent length */
3669 {
3670         int             level;          /* btree level number */
3671         int             maxrecs;        /* maximum record count at this level */
3672         xfs_mount_t     *mp;            /* mount structure */
3673         xfs_filblks_t   rval;           /* return value */
3674
3675         mp = ip->i_mount;
3676         maxrecs = mp->m_bmap_dmxr[0];
3677         for (level = 0, rval = 0;
3678              level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3679              level++) {
3680                 len += maxrecs - 1;
3681                 do_div(len, maxrecs);
3682                 rval += len;
3683                 if (len == 1)
3684                         return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3685                                 level - 1;
3686                 if (level == 0)
3687                         maxrecs = mp->m_bmap_dmxr[1];
3688         }
3689         return rval;
3690 }
3691
3692 /*
3693  * Convert inode from non-attributed to attributed.
3694  * Must not be in a transaction, ip must not be locked.
3695  */
3696 int                                             /* error code */
3697 xfs_bmap_add_attrfork(
3698         xfs_inode_t             *ip,            /* incore inode pointer */
3699         int                     size,           /* space new attribute needs */
3700         int                     rsvd)           /* xact may use reserved blks */
3701 {
3702         xfs_fsblock_t           firstblock;     /* 1st block/ag allocated */
3703         xfs_bmap_free_t         flist;          /* freed extent records */
3704         xfs_mount_t             *mp;            /* mount structure */
3705         xfs_trans_t             *tp;            /* transaction pointer */
3706         int                     blks;           /* space reservation */
3707         int                     version = 1;    /* superblock attr version */
3708         int                     committed;      /* xaction was committed */
3709         int                     logflags;       /* logging flags */
3710         int                     error;          /* error return value */
3711
3712         ASSERT(XFS_IFORK_Q(ip) == 0);
3713         ASSERT(ip->i_df.if_ext_max ==
3714                XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3715
3716         mp = ip->i_mount;
3717         ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3718         tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3719         blks = XFS_ADDAFORK_SPACE_RES(mp);
3720         if (rsvd)
3721                 tp->t_flags |= XFS_TRANS_RESERVE;
3722         if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3723                         XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3724                 goto error0;
3725         xfs_ilock(ip, XFS_ILOCK_EXCL);
3726         error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3727                         XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3728                         XFS_QMOPT_RES_REGBLKS);
3729         if (error) {
3730                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3731                 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3732                 return error;
3733         }
3734         if (XFS_IFORK_Q(ip))
3735                 goto error1;
3736         if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3737                 /*
3738                  * For inodes coming from pre-6.2 filesystems.
3739                  */
3740                 ASSERT(ip->i_d.di_aformat == 0);
3741                 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3742         }
3743         ASSERT(ip->i_d.di_anextents == 0);
3744         IHOLD(ip);
3745         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3746         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3747         switch (ip->i_d.di_format) {
3748         case XFS_DINODE_FMT_DEV:
3749                 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3750                 break;
3751         case XFS_DINODE_FMT_UUID:
3752                 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3753                 break;
3754         case XFS_DINODE_FMT_LOCAL:
3755         case XFS_DINODE_FMT_EXTENTS:
3756         case XFS_DINODE_FMT_BTREE:
3757                 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3758                 if (!ip->i_d.di_forkoff)
3759                         ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3760                 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3761                         version = 2;
3762                 break;
3763         default:
3764                 ASSERT(0);
3765                 error = XFS_ERROR(EINVAL);
3766                 goto error1;
3767         }
3768         ip->i_df.if_ext_max =
3769                 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3770         ASSERT(ip->i_afp == NULL);
3771         ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3772         ip->i_afp->if_ext_max =
3773                 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3774         ip->i_afp->if_flags = XFS_IFEXTENTS;
3775         logflags = 0;
3776         xfs_bmap_init(&flist, &firstblock);
3777         switch (ip->i_d.di_format) {
3778         case XFS_DINODE_FMT_LOCAL:
3779                 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3780                         &logflags);
3781                 break;
3782         case XFS_DINODE_FMT_EXTENTS:
3783                 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3784                         &flist, &logflags);
3785                 break;
3786         case XFS_DINODE_FMT_BTREE:
3787                 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3788                         &logflags);
3789                 break;
3790         default:
3791                 error = 0;
3792                 break;
3793         }
3794         if (logflags)
3795                 xfs_trans_log_inode(tp, ip, logflags);
3796         if (error)
3797                 goto error2;
3798         if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3799            (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3800                 __int64_t sbfields = 0;
3801
3802                 spin_lock(&mp->m_sb_lock);
3803                 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3804                         xfs_sb_version_addattr(&mp->m_sb);
3805                         sbfields |= XFS_SB_VERSIONNUM;
3806                 }
3807                 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3808                         xfs_sb_version_addattr2(&mp->m_sb);
3809                         sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3810                 }
3811                 if (sbfields) {
3812                         spin_unlock(&mp->m_sb_lock);
3813                         xfs_mod_sb(tp, sbfields);
3814                 } else
3815                         spin_unlock(&mp->m_sb_lock);
3816         }
3817         if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3818                 goto error2;
3819         error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
3820         ASSERT(ip->i_df.if_ext_max ==
3821                XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3822         return error;
3823 error2:
3824         xfs_bmap_cancel(&flist);
3825 error1:
3826         xfs_iunlock(ip, XFS_ILOCK_EXCL);
3827 error0:
3828         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3829         ASSERT(ip->i_df.if_ext_max ==
3830                XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3831         return error;
3832 }
3833
3834 /*
3835  * Add the extent to the list of extents to be free at transaction end.
3836  * The list is maintained sorted (by block number).
3837  */
3838 /* ARGSUSED */
3839 void
3840 xfs_bmap_add_free(
3841         xfs_fsblock_t           bno,            /* fs block number of extent */
3842         xfs_filblks_t           len,            /* length of extent */
3843         xfs_bmap_free_t         *flist,         /* list of extents */
3844         xfs_mount_t             *mp)            /* mount point structure */
3845 {
3846         xfs_bmap_free_item_t    *cur;           /* current (next) element */
3847         xfs_bmap_free_item_t    *new;           /* new element */
3848         xfs_bmap_free_item_t    *prev;          /* previous element */
3849 #ifdef DEBUG
3850         xfs_agnumber_t          agno;
3851         xfs_agblock_t           agbno;
3852
3853         ASSERT(bno != NULLFSBLOCK);
3854         ASSERT(len > 0);
3855         ASSERT(len <= MAXEXTLEN);
3856         ASSERT(!isnullstartblock(bno));
3857         agno = XFS_FSB_TO_AGNO(mp, bno);
3858         agbno = XFS_FSB_TO_AGBNO(mp, bno);
3859         ASSERT(agno < mp->m_sb.sb_agcount);
3860         ASSERT(agbno < mp->m_sb.sb_agblocks);
3861         ASSERT(len < mp->m_sb.sb_agblocks);
3862         ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3863 #endif
3864         ASSERT(xfs_bmap_free_item_zone != NULL);
3865         new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3866         new->xbfi_startblock = bno;
3867         new->xbfi_blockcount = (xfs_extlen_t)len;
3868         for (prev = NULL, cur = flist->xbf_first;
3869              cur != NULL;
3870              prev = cur, cur = cur->xbfi_next) {
3871                 if (cur->xbfi_startblock >= bno)
3872                         break;
3873         }
3874         if (prev)
3875                 prev->xbfi_next = new;
3876         else
3877                 flist->xbf_first = new;
3878         new->xbfi_next = cur;
3879         flist->xbf_count++;
3880 }
3881
3882 /*
3883  * Compute and fill in the value of the maximum depth of a bmap btree
3884  * in this filesystem.  Done once, during mount.
3885  */
3886 void
3887 xfs_bmap_compute_maxlevels(
3888         xfs_mount_t     *mp,            /* file system mount structure */
3889         int             whichfork)      /* data or attr fork */
3890 {
3891         int             level;          /* btree level */
3892         uint            maxblocks;      /* max blocks at this level */
3893         uint            maxleafents;    /* max leaf entries possible */
3894         int             maxrootrecs;    /* max records in root block */
3895         int             minleafrecs;    /* min records in leaf block */
3896         int             minnoderecs;    /* min records in node block */
3897         int             sz;             /* root block size */
3898
3899         /*
3900          * The maximum number of extents in a file, hence the maximum
3901          * number of leaf entries, is controlled by the type of di_nextents
3902          * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3903          * (a signed 16-bit number, xfs_aextnum_t).
3904          *
3905          * Note that we can no longer assume that if we are in ATTR1 that
3906          * the fork offset of all the inodes will be
3907          * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3908          * with ATTR2 and then mounted back with ATTR1, keeping the
3909          * di_forkoff's fixed but probably at various positions. Therefore,
3910          * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3911          * of a minimum size available.
3912          */
3913         if (whichfork == XFS_DATA_FORK) {
3914                 maxleafents = MAXEXTNUM;
3915                 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3916         } else {
3917                 maxleafents = MAXAEXTNUM;
3918                 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3919         }
3920         maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3921         minleafrecs = mp->m_bmap_dmnr[0];
3922         minnoderecs = mp->m_bmap_dmnr[1];
3923         maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3924         for (level = 1; maxblocks > 1; level++) {
3925                 if (maxblocks <= maxrootrecs)
3926                         maxblocks = 1;
3927                 else
3928                         maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3929         }
3930         mp->m_bm_maxlevels[whichfork] = level;
3931 }
3932
3933 /*
3934  * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3935  * caller.  Frees all the extents that need freeing, which must be done
3936  * last due to locking considerations.  We never free any extents in
3937  * the first transaction.  This is to allow the caller to make the first
3938  * transaction a synchronous one so that the pointers to the data being
3939  * broken in this transaction will be permanent before the data is actually
3940  * freed.  This is necessary to prevent blocks from being reallocated
3941  * and written to before the free and reallocation are actually permanent.
3942  * We do not just make the first transaction synchronous here, because
3943  * there are more efficient ways to gain the same protection in some cases
3944  * (see the file truncation code).
3945  *
3946  * Return 1 if the given transaction was committed and a new one
3947  * started, and 0 otherwise in the committed parameter.
3948  */
3949 /*ARGSUSED*/
3950 int                                             /* error */
3951 xfs_bmap_finish(
3952         xfs_trans_t             **tp,           /* transaction pointer addr */
3953         xfs_bmap_free_t         *flist,         /* i/o: list extents to free */
3954         int                     *committed)     /* xact committed or not */
3955 {
3956         xfs_efd_log_item_t      *efd;           /* extent free data */
3957         xfs_efi_log_item_t      *efi;           /* extent free intention */
3958         int                     error;          /* error return value */
3959         xfs_bmap_free_item_t    *free;          /* free extent item */
3960         unsigned int            logres;         /* new log reservation */
3961         unsigned int            logcount;       /* new log count */
3962         xfs_mount_t             *mp;            /* filesystem mount structure */
3963         xfs_bmap_free_item_t    *next;          /* next item on free list */
3964         xfs_trans_t             *ntp;           /* new transaction pointer */
3965
3966         ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3967         if (flist->xbf_count == 0) {
3968                 *committed = 0;
3969                 return 0;
3970         }
3971         ntp = *tp;
3972         efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3973         for (free = flist->xbf_first; free; free = free->xbfi_next)
3974                 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3975                         free->xbfi_blockcount);
3976         logres = ntp->t_log_res;
3977         logcount = ntp->t_log_count;
3978         ntp = xfs_trans_dup(*tp);
3979         error = xfs_trans_commit(*tp, 0);
3980         *tp = ntp;
3981         *committed = 1;
3982         /*
3983          * We have a new transaction, so we should return committed=1,
3984          * even though we're returning an error.
3985          */
3986         if (error)
3987                 return error;
3988
3989         /*
3990          * transaction commit worked ok so we can drop the extra ticket
3991          * reference that we gained in xfs_trans_dup()
3992          */
3993         xfs_log_ticket_put(ntp->t_ticket);
3994
3995         if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3996                         logcount)))
3997                 return error;
3998         efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3999         for (free = flist->xbf_first; free != NULL; free = next) {
4000                 next = free->xbfi_next;
4001                 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4002                                 free->xbfi_blockcount))) {
4003                         /*
4004                          * The bmap free list will be cleaned up at a
4005                          * higher level.  The EFI will be canceled when
4006                          * this transaction is aborted.
4007                          * Need to force shutdown here to make sure it
4008                          * happens, since this transaction may not be
4009                          * dirty yet.
4010                          */
4011                         mp = ntp->t_mountp;
4012                         if (!XFS_FORCED_SHUTDOWN(mp))
4013                                 xfs_force_shutdown(mp,
4014                                                    (error == EFSCORRUPTED) ?
4015                                                    SHUTDOWN_CORRUPT_INCORE :
4016                                                    SHUTDOWN_META_IO_ERROR);
4017                         return error;
4018                 }
4019                 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4020                         free->xbfi_blockcount);
4021                 xfs_bmap_del_free(flist, NULL, free);
4022         }
4023         return 0;
4024 }
4025
4026 /*
4027  * Free up any items left in the list.
4028  */
4029 void
4030 xfs_bmap_cancel(
4031         xfs_bmap_free_t         *flist) /* list of bmap_free_items */
4032 {
4033         xfs_bmap_free_item_t    *free;  /* free list item */
4034         xfs_bmap_free_item_t    *next;
4035
4036         if (flist->xbf_count == 0)
4037                 return;
4038         ASSERT(flist->xbf_first != NULL);
4039         for (free = flist->xbf_first; free; free = next) {
4040                 next = free->xbfi_next;
4041                 xfs_bmap_del_free(flist, NULL, free);
4042         }
4043         ASSERT(flist->xbf_count == 0);
4044 }
4045
4046 /*
4047  * Returns the file-relative block number of the first unused block(s)
4048  * in the file with at least "len" logically contiguous blocks free.
4049  * This is the lowest-address hole if the file has holes, else the first block
4050  * past the end of file.
4051  * Return 0 if the file is currently local (in-inode).
4052  */
4053 int                                             /* error */
4054 xfs_bmap_first_unused(
4055         xfs_trans_t     *tp,                    /* transaction pointer */
4056         xfs_inode_t     *ip,                    /* incore inode */
4057         xfs_extlen_t    len,                    /* size of hole to find */
4058         xfs_fileoff_t   *first_unused,          /* unused block */
4059         int             whichfork)              /* data or attr fork */
4060 {
4061         int             error;                  /* error return value */
4062         int             idx;                    /* extent record index */
4063         xfs_ifork_t     *ifp;                   /* inode fork pointer */
4064         xfs_fileoff_t   lastaddr;               /* last block number seen */
4065         xfs_fileoff_t   lowest;                 /* lowest useful block */
4066         xfs_fileoff_t   max;                    /* starting useful block */
4067         xfs_fileoff_t   off;                    /* offset for this block */
4068         xfs_extnum_t    nextents;               /* number of extent entries */
4069
4070         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4071                XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4072                XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4073         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4074                 *first_unused = 0;
4075                 return 0;
4076         }
4077         ifp = XFS_IFORK_PTR(ip, whichfork);
4078         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4079             (error = xfs_iread_extents(tp, ip, whichfork)))
4080                 return error;
4081         lowest = *first_unused;
4082         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4083         for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4084                 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4085                 off = xfs_bmbt_get_startoff(ep);
4086                 /*
4087                  * See if the hole before this extent will work.
4088                  */
4089                 if (off >= lowest + len && off - max >= len) {
4090                         *first_unused = max;
4091                         return 0;
4092                 }
4093                 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4094                 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4095         }
4096         *first_unused = max;
4097         return 0;
4098 }
4099
4100 /*
4101  * Returns the file-relative block number of the last block + 1 before
4102  * last_block (input value) in the file.
4103  * This is not based on i_size, it is based on the extent records.
4104  * Returns 0 for local files, as they do not have extent records.
4105  */
4106 int                                             /* error */
4107 xfs_bmap_last_before(
4108         xfs_trans_t     *tp,                    /* transaction pointer */
4109         xfs_inode_t     *ip,                    /* incore inode */
4110         xfs_fileoff_t   *last_block,            /* last block */
4111         int             whichfork)              /* data or attr fork */
4112 {
4113         xfs_fileoff_t   bno;                    /* input file offset */
4114         int             eof;                    /* hit end of file */
4115         xfs_bmbt_rec_host_t *ep;                /* pointer to last extent */
4116         int             error;                  /* error return value */
4117         xfs_bmbt_irec_t got;                    /* current extent value */
4118         xfs_ifork_t     *ifp;                   /* inode fork pointer */
4119         xfs_extnum_t    lastx;                  /* last extent used */
4120         xfs_bmbt_irec_t prev;                   /* previous extent value */
4121
4122         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4123             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4124             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4125                return XFS_ERROR(EIO);
4126         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4127                 *last_block = 0;
4128                 return 0;
4129         }
4130         ifp = XFS_IFORK_PTR(ip, whichfork);
4131         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4132             (error = xfs_iread_extents(tp, ip, whichfork)))
4133                 return error;
4134         bno = *last_block - 1;
4135         ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4136                 &prev);
4137         if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4138                 if (prev.br_startoff == NULLFILEOFF)
4139                         *last_block = 0;
4140                 else
4141                         *last_block = prev.br_startoff + prev.br_blockcount;
4142         }
4143         /*
4144          * Otherwise *last_block is already the right answer.
4145          */
4146         return 0;
4147 }
4148
4149 /*
4150  * Returns the file-relative block number of the first block past eof in
4151  * the file.  This is not based on i_size, it is based on the extent records.
4152  * Returns 0 for local files, as they do not have extent records.
4153  */
4154 int                                             /* error */
4155 xfs_bmap_last_offset(
4156         xfs_trans_t     *tp,                    /* transaction pointer */
4157         xfs_inode_t     *ip,                    /* incore inode */
4158         xfs_fileoff_t   *last_block,            /* last block */
4159         int             whichfork)              /* data or attr fork */
4160 {
4161         xfs_bmbt_rec_host_t *ep;                /* pointer to last extent */
4162         int             error;                  /* error return value */
4163         xfs_ifork_t     *ifp;                   /* inode fork pointer */
4164         xfs_extnum_t    nextents;               /* number of extent entries */
4165
4166         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4167             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4168             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4169                return XFS_ERROR(EIO);
4170         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4171                 *last_block = 0;
4172                 return 0;
4173         }
4174         ifp = XFS_IFORK_PTR(ip, whichfork);
4175         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4176             (error = xfs_iread_extents(tp, ip, whichfork)))
4177                 return error;
4178         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4179         if (!nextents) {
4180                 *last_block = 0;
4181                 return 0;
4182         }
4183         ep = xfs_iext_get_ext(ifp, nextents - 1);
4184         *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4185         return 0;
4186 }
4187
4188 /*
4189  * Returns whether the selected fork of the inode has exactly one
4190  * block or not.  For the data fork we check this matches di_size,
4191  * implying the file's range is 0..bsize-1.
4192  */
4193 int                                     /* 1=>1 block, 0=>otherwise */
4194 xfs_bmap_one_block(
4195         xfs_inode_t     *ip,            /* incore inode */
4196         int             whichfork)      /* data or attr fork */
4197 {
4198         xfs_bmbt_rec_host_t *ep;        /* ptr to fork's extent */
4199         xfs_ifork_t     *ifp;           /* inode fork pointer */
4200         int             rval;           /* return value */
4201         xfs_bmbt_irec_t s;              /* internal version of extent */
4202
4203 #ifndef DEBUG
4204         if (whichfork == XFS_DATA_FORK) {
4205                 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4206                         (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4207                         (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4208         }
4209 #endif  /* !DEBUG */
4210         if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4211                 return 0;
4212         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4213                 return 0;
4214         ifp = XFS_IFORK_PTR(ip, whichfork);
4215         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4216         ep = xfs_iext_get_ext(ifp, 0);
4217         xfs_bmbt_get_all(ep, &s);
4218         rval = s.br_startoff == 0 && s.br_blockcount == 1;
4219         if (rval && whichfork == XFS_DATA_FORK)
4220                 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4221         return rval;
4222 }
4223
4224 STATIC int
4225 xfs_bmap_sanity_check(
4226         struct xfs_mount        *mp,
4227         struct xfs_buf          *bp,
4228         int                     level)
4229 {
4230         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
4231
4232         if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4233             be16_to_cpu(block->bb_level) != level ||
4234             be16_to_cpu(block->bb_numrecs) == 0 ||
4235             be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4236                 return 0;
4237         return 1;
4238 }
4239
4240 /*
4241  * Read in the extents to if_extents.
4242  * All inode fields are set up by caller, we just traverse the btree
4243  * and copy the records in. If the file system cannot contain unwritten
4244  * extents, the records are checked for no "state" flags.
4245  */
4246 int                                     /* error */
4247 xfs_bmap_read_extents(
4248         xfs_trans_t             *tp,    /* transaction pointer */
4249         xfs_inode_t             *ip,    /* incore inode */
4250         int                     whichfork) /* data or attr fork */
4251 {
4252         struct xfs_btree_block  *block; /* current btree block */
4253         xfs_fsblock_t           bno;    /* block # of "block" */
4254         xfs_buf_t               *bp;    /* buffer for "block" */
4255         int                     error;  /* error return value */
4256         xfs_exntfmt_t           exntf;  /* XFS_EXTFMT_NOSTATE, if checking */
4257         xfs_extnum_t            i, j;   /* index into the extents list */
4258         xfs_ifork_t             *ifp;   /* fork structure */
4259         int                     level;  /* btree level, for checking */
4260         xfs_mount_t             *mp;    /* file system mount structure */
4261         __be64                  *pp;    /* pointer to block address */
4262         /* REFERENCED */
4263         xfs_extnum_t            room;   /* number of entries there's room for */
4264
4265         bno = NULLFSBLOCK;
4266         mp = ip->i_mount;
4267         ifp = XFS_IFORK_PTR(ip, whichfork);
4268         exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4269                                         XFS_EXTFMT_INODE(ip);
4270         block = ifp->if_broot;
4271         /*
4272          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4273          */
4274         level = be16_to_cpu(block->bb_level);
4275         ASSERT(level > 0);
4276         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4277         bno = be64_to_cpu(*pp);
4278         ASSERT(bno != NULLDFSBNO);
4279         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4280         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4281         /*
4282          * Go down the tree until leaf level is reached, following the first
4283          * pointer (leftmost) at each level.
4284          */
4285         while (level-- > 0) {
4286                 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4287                                 XFS_BMAP_BTREE_REF)))
4288                         return error;
4289                 block = XFS_BUF_TO_BLOCK(bp);
4290                 XFS_WANT_CORRUPTED_GOTO(
4291                         xfs_bmap_sanity_check(mp, bp, level),
4292                         error0);
4293                 if (level == 0)
4294                         break;
4295                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4296                 bno = be64_to_cpu(*pp);
4297                 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4298                 xfs_trans_brelse(tp, bp);
4299         }
4300         /*
4301          * Here with bp and block set to the leftmost leaf node in the tree.
4302          */
4303         room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4304         i = 0;
4305         /*
4306          * Loop over all leaf nodes.  Copy information to the extent records.
4307          */
4308         for (;;) {
4309                 xfs_bmbt_rec_t  *frp;
4310                 xfs_fsblock_t   nextbno;
4311                 xfs_extnum_t    num_recs;
4312                 xfs_extnum_t    start;
4313
4314
4315                 num_recs = xfs_btree_get_numrecs(block);
4316                 if (unlikely(i + num_recs > room)) {
4317                         ASSERT(i + num_recs <= room);
4318                         xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4319                                 "corrupt dinode %Lu, (btree extents).",
4320                                 (unsigned long long) ip->i_ino);
4321                         XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4322                                          XFS_ERRLEVEL_LOW,
4323                                         ip->i_mount);
4324                         goto error0;
4325                 }
4326                 XFS_WANT_CORRUPTED_GOTO(
4327                         xfs_bmap_sanity_check(mp, bp, 0),
4328                         error0);
4329                 /*
4330                  * Read-ahead the next leaf block, if any.
4331                  */
4332                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4333                 if (nextbno != NULLFSBLOCK)
4334                         xfs_btree_reada_bufl(mp, nextbno, 1);
4335                 /*
4336                  * Copy records into the extent records.
4337                  */
4338                 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4339                 start = i;
4340                 for (j = 0; j < num_recs; j++, i++, frp++) {
4341                         xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4342                         trp->l0 = be64_to_cpu(frp->l0);
4343                         trp->l1 = be64_to_cpu(frp->l1);
4344                 }
4345                 if (exntf == XFS_EXTFMT_NOSTATE) {
4346                         /*
4347                          * Check all attribute bmap btree records and
4348                          * any "older" data bmap btree records for a
4349                          * set bit in the "extent flag" position.
4350                          */
4351                         if (unlikely(xfs_check_nostate_extents(ifp,
4352                                         start, num_recs))) {
4353                                 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4354                                                  XFS_ERRLEVEL_LOW,
4355                                                  ip->i_mount);
4356                                 goto error0;
4357                         }
4358                 }
4359                 xfs_trans_brelse(tp, bp);
4360                 bno = nextbno;
4361                 /*
4362                  * If we've reached the end, stop.
4363                  */
4364                 if (bno == NULLFSBLOCK)
4365                         break;
4366                 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4367                                 XFS_BMAP_BTREE_REF)))
4368                         return error;
4369                 block = XFS_BUF_TO_BLOCK(bp);
4370         }
4371         ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4372         ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4373         XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4374         return 0;
4375 error0:
4376         xfs_trans_brelse(tp, bp);
4377         return XFS_ERROR(EFSCORRUPTED);
4378 }
4379
4380 #ifdef DEBUG
4381 /*
4382  * Add bmap trace insert entries for all the contents of the extent records.
4383  */
4384 void
4385 xfs_bmap_trace_exlist(
4386         xfs_inode_t     *ip,            /* incore inode pointer */
4387         xfs_extnum_t    cnt,            /* count of entries in the list */
4388         int             whichfork,      /* data or attr fork */
4389         unsigned long   caller_ip)
4390 {
4391         xfs_extnum_t    idx;            /* extent record index */
4392         xfs_ifork_t     *ifp;           /* inode fork pointer */
4393         int             state = 0;
4394
4395         if (whichfork == XFS_ATTR_FORK)
4396                 state |= BMAP_ATTRFORK;
4397
4398         ifp = XFS_IFORK_PTR(ip, whichfork);
4399         ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4400         for (idx = 0; idx < cnt; idx++)
4401                 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4402 }
4403
4404 /*
4405  * Validate that the bmbt_irecs being returned from bmapi are valid
4406  * given the callers original parameters.  Specifically check the
4407  * ranges of the returned irecs to ensure that they only extent beyond
4408  * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4409  */
4410 STATIC void
4411 xfs_bmap_validate_ret(
4412         xfs_fileoff_t           bno,
4413         xfs_filblks_t           len,
4414         int                     flags,
4415         xfs_bmbt_irec_t         *mval,
4416         int                     nmap,
4417         int                     ret_nmap)
4418 {
4419         int                     i;              /* index to map values */
4420
4421         ASSERT(ret_nmap <= nmap);
4422
4423         for (i = 0; i < ret_nmap; i++) {
4424                 ASSERT(mval[i].br_blockcount > 0);
4425                 if (!(flags & XFS_BMAPI_ENTIRE)) {
4426                         ASSERT(mval[i].br_startoff >= bno);
4427                         ASSERT(mval[i].br_blockcount <= len);
4428                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4429                                bno + len);
4430                 } else {
4431                         ASSERT(mval[i].br_startoff < bno + len);
4432                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4433                                bno);
4434                 }
4435                 ASSERT(i == 0 ||
4436                        mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4437                        mval[i].br_startoff);
4438                 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4439                         ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4440                                mval[i].br_startblock != HOLESTARTBLOCK);
4441                 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4442                        mval[i].br_state == XFS_EXT_UNWRITTEN);
4443         }
4444 }
4445 #endif /* DEBUG */
4446
4447
4448 /*
4449  * Map file blocks to filesystem blocks.
4450  * File range is given by the bno/len pair.
4451  * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4452  * into a hole or past eof.
4453  * Only allocates blocks from a single allocation group,
4454  * to avoid locking problems.
4455  * The returned value in "firstblock" from the first call in a transaction
4456  * must be remembered and presented to subsequent calls in "firstblock".
4457  * An upper bound for the number of blocks to be allocated is supplied to
4458  * the first call in "total"; if no allocation group has that many free
4459  * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4460  */
4461 int                                     /* error */
4462 xfs_bmapi(
4463         xfs_trans_t     *tp,            /* transaction pointer */
4464         xfs_inode_t     *ip,            /* incore inode */
4465         xfs_fileoff_t   bno,            /* starting file offs. mapped */
4466         xfs_filblks_t   len,            /* length to map in file */
4467         int             flags,          /* XFS_BMAPI_... */
4468         xfs_fsblock_t   *firstblock,    /* first allocated block
4469                                            controls a.g. for allocs */
4470         xfs_extlen_t    total,          /* total blocks needed */
4471         xfs_bmbt_irec_t *mval,          /* output: map values */
4472         int             *nmap,          /* i/o: mval size/count */
4473         xfs_bmap_free_t *flist,         /* i/o: list extents to free */
4474         xfs_extdelta_t  *delta)         /* o: change made to incore extents */
4475 {
4476         xfs_fsblock_t   abno;           /* allocated block number */
4477         xfs_extlen_t    alen;           /* allocated extent length */
4478         xfs_fileoff_t   aoff;           /* allocated file offset */
4479         xfs_bmalloca_t  bma;            /* args for xfs_bmap_alloc */
4480         xfs_btree_cur_t *cur;           /* bmap btree cursor */
4481         xfs_fileoff_t   end;            /* end of mapped file region */
4482         int             eof;            /* we've hit the end of extents */
4483         xfs_bmbt_rec_host_t *ep;        /* extent record pointer */
4484         int             error;          /* error return */
4485         xfs_bmbt_irec_t got;            /* current file extent record */
4486         xfs_ifork_t     *ifp;           /* inode fork pointer */
4487         xfs_extlen_t    indlen;         /* indirect blocks length */
4488         xfs_extnum_t    lastx;          /* last useful extent number */
4489         int             logflags;       /* flags for transaction logging */
4490         xfs_extlen_t    minleft;        /* min blocks left after allocation */
4491         xfs_extlen_t    minlen;         /* min allocation size */
4492         xfs_mount_t     *mp;            /* xfs mount structure */
4493         int             n;              /* current extent index */
4494         int             nallocs;        /* number of extents alloc'd */
4495         xfs_extnum_t    nextents;       /* number of extents in file */
4496         xfs_fileoff_t   obno;           /* old block number (offset) */
4497         xfs_bmbt_irec_t prev;           /* previous file extent record */
4498         int             tmp_logflags;   /* temp flags holder */
4499         int             whichfork;      /* data or attr fork */
4500         char            inhole;         /* current location is hole in file */
4501         char            wasdelay;       /* old extent was delayed */
4502         char            wr;             /* this is a write request */
4503         char            rt;             /* this is a realtime file */
4504 #ifdef DEBUG
4505         xfs_fileoff_t   orig_bno;       /* original block number value */
4506         int             orig_flags;     /* original flags arg value */
4507         xfs_filblks_t   orig_len;       /* original value of len arg */
4508         xfs_bmbt_irec_t *orig_mval;     /* original value of mval */
4509         int             orig_nmap;      /* original value of *nmap */
4510
4511         orig_bno = bno;
4512         orig_len = len;
4513         orig_flags = flags;
4514         orig_mval = mval;
4515         orig_nmap = *nmap;
4516 #endif
4517         ASSERT(*nmap >= 1);
4518         ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4519         whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4520                 XFS_ATTR_FORK : XFS_DATA_FORK;
4521         mp = ip->i_mount;
4522         if (unlikely(XFS_TEST_ERROR(
4523             (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4524              XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4525              XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4526              mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4527                 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4528                 return XFS_ERROR(EFSCORRUPTED);
4529         }
4530         if (XFS_FORCED_SHUTDOWN(mp))
4531                 return XFS_ERROR(EIO);
4532         rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4533         ifp = XFS_IFORK_PTR(ip, whichfork);
4534         ASSERT(ifp->if_ext_max ==
4535                XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4536         if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4537                 XFS_STATS_INC(xs_blk_mapw);
4538         else
4539                 XFS_STATS_INC(xs_blk_mapr);
4540         /*
4541          * IGSTATE flag is used to combine extents which
4542          * differ only due to the state of the extents.
4543          * This technique is used from xfs_getbmap()
4544          * when the caller does not wish to see the
4545          * separation (which is the default).
4546          *
4547          * This technique is also used when writing a
4548          * buffer which has been partially written,
4549          * (usually by being flushed during a chunkread),
4550          * to ensure one write takes place. This also
4551          * prevents a change in the xfs inode extents at
4552          * this time, intentionally. This change occurs
4553          * on completion of the write operation, in
4554          * xfs_strat_comp(), where the xfs_bmapi() call
4555          * is transactioned, and the extents combined.
4556          */
4557         if ((flags & XFS_BMAPI_IGSTATE) && wr)  /* if writing unwritten space */
4558                 wr = 0;                         /* no allocations are allowed */
4559         ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4560         logflags = 0;
4561         nallocs = 0;
4562         cur = NULL;
4563         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4564                 ASSERT(wr && tp);
4565                 if ((error = xfs_bmap_local_to_extents(tp, ip,
4566                                 firstblock, total, &logflags, whichfork)))
4567                         goto error0;
4568         }
4569         if (wr && *firstblock == NULLFSBLOCK) {
4570                 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4571                         minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4572                 else
4573                         minleft = 1;
4574         } else
4575                 minleft = 0;
4576         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4577             (error = xfs_iread_extents(tp, ip, whichfork)))
4578                 goto error0;
4579         ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4580                 &prev);
4581         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4582         n = 0;
4583         end = bno + len;
4584         obno = bno;
4585         bma.ip = NULL;
4586         if (delta) {
4587                 delta->xed_startoff = NULLFILEOFF;
4588                 delta->xed_blockcount = 0;
4589         }
4590         while (bno < end && n < *nmap) {
4591                 /*
4592                  * Reading past eof, act as though there's a hole
4593                  * up to end.
4594                  */
4595                 if (eof && !wr)
4596                         got.br_startoff = end;
4597                 inhole = eof || got.br_startoff > bno;
4598                 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4599                         isnullstartblock(got.br_startblock);
4600                 /*
4601                  * First, deal with the hole before the allocated space
4602                  * that we found, if any.
4603                  */
4604                 if (wr && (inhole || wasdelay)) {
4605                         /*
4606                          * For the wasdelay case, we could also just
4607                          * allocate the stuff asked for in this bmap call
4608                          * but that wouldn't be as good.
4609                          */
4610                         if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4611                                 alen = (xfs_extlen_t)got.br_blockcount;
4612                                 aoff = got.br_startoff;
4613                                 if (lastx != NULLEXTNUM && lastx) {
4614                                         ep = xfs_iext_get_ext(ifp, lastx - 1);
4615                                         xfs_bmbt_get_all(ep, &prev);
4616                                 }
4617                         } else if (wasdelay) {
4618                                 alen = (xfs_extlen_t)
4619                                         XFS_FILBLKS_MIN(len,
4620                                                 (got.br_startoff +
4621                                                  got.br_blockcount) - bno);
4622                                 aoff = bno;
4623                         } else {
4624                                 alen = (xfs_extlen_t)
4625                                         XFS_FILBLKS_MIN(len, MAXEXTLEN);
4626                                 if (!eof)
4627                                         alen = (xfs_extlen_t)
4628                                                 XFS_FILBLKS_MIN(alen,
4629                                                         got.br_startoff - bno);
4630                                 aoff = bno;
4631                         }
4632                         minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4633                         if (flags & XFS_BMAPI_DELAY) {
4634                                 xfs_extlen_t    extsz;
4635
4636                                 /* Figure out the extent size, adjust alen */
4637                                 extsz = xfs_get_extsz_hint(ip);
4638                                 if (extsz) {
4639                                         error = xfs_bmap_extsize_align(mp,
4640                                                         &got, &prev, extsz,
4641                                                         rt, eof,
4642                                                         flags&XFS_BMAPI_DELAY,
4643                                                         flags&XFS_BMAPI_CONVERT,
4644                                                         &aoff, &alen);
4645                                         ASSERT(!error);
4646                                 }
4647
4648                                 if (rt)
4649                                         extsz = alen / mp->m_sb.sb_rextsize;
4650
4651                                 /*
4652                                  * Make a transaction-less quota reservation for
4653                                  * delayed allocation blocks. This number gets
4654                                  * adjusted later.  We return if we haven't
4655                                  * allocated blocks already inside this loop.
4656                                  */
4657                                 error = xfs_trans_reserve_quota_nblks(
4658                                                 NULL, ip, (long)alen, 0,
4659                                                 rt ? XFS_QMOPT_RES_RTBLKS :
4660                                                      XFS_QMOPT_RES_REGBLKS);
4661                                 if (error) {
4662                                         if (n == 0) {
4663                                                 *nmap = 0;
4664                                                 ASSERT(cur == NULL);
4665                                                 return error;
4666                                         }
4667                                         break;
4668                                 }
4669
4670                                 /*
4671                                  * Split changing sb for alen and indlen since
4672                                  * they could be coming from different places.
4673                                  */
4674                                 indlen = (xfs_extlen_t)
4675                                         xfs_bmap_worst_indlen(ip, alen);
4676                                 ASSERT(indlen > 0);
4677
4678                                 if (rt) {
4679                                         error = xfs_mod_incore_sb(mp,
4680                                                         XFS_SBS_FREXTENTS,
4681                                                         -((int64_t)extsz), (flags &
4682                                                         XFS_BMAPI_RSVBLOCKS));
4683                                 } else {
4684                                         error = xfs_mod_incore_sb(mp,
4685                                                         XFS_SBS_FDBLOCKS,
4686                                                         -((int64_t)alen), (flags &
4687                                                         XFS_BMAPI_RSVBLOCKS));
4688                                 }
4689                                 if (!error) {
4690                                         error = xfs_mod_incore_sb(mp,
4691                                                         XFS_SBS_FDBLOCKS,
4692                                                         -((int64_t)indlen), (flags &
4693                                                         XFS_BMAPI_RSVBLOCKS));
4694                                         if (error && rt)
4695                                                 xfs_mod_incore_sb(mp,
4696                                                         XFS_SBS_FREXTENTS,
4697                                                         (int64_t)extsz, (flags &
4698                                                         XFS_BMAPI_RSVBLOCKS));
4699                                         else if (error)
4700                                                 xfs_mod_incore_sb(mp,
4701                                                         XFS_SBS_FDBLOCKS,
4702                                                         (int64_t)alen, (flags &
4703                                                         XFS_BMAPI_RSVBLOCKS));
4704                                 }
4705
4706                                 if (error) {
4707                                         if (XFS_IS_QUOTA_ON(mp))
4708                                                 /* unreserve the blocks now */
4709                                                 (void)
4710                                                 xfs_trans_unreserve_quota_nblks(
4711                                                         NULL, ip,
4712                                                         (long)alen, 0, rt ?
4713                                                         XFS_QMOPT_RES_RTBLKS :
4714                                                         XFS_QMOPT_RES_REGBLKS);
4715                                         break;
4716                                 }
4717
4718                                 ip->i_delayed_blks += alen;
4719                                 abno = nullstartblock(indlen);
4720                         } else {
4721                                 /*
4722                                  * If first time, allocate and fill in
4723                                  * once-only bma fields.
4724                                  */
4725                                 if (bma.ip == NULL) {
4726                                         bma.tp = tp;
4727                                         bma.ip = ip;
4728                                         bma.prevp = &prev;
4729                                         bma.gotp = &got;
4730                                         bma.total = total;
4731                                         bma.userdata = 0;
4732                                 }
4733                                 /* Indicate if this is the first user data
4734                                  * in the file, or just any user data.
4735                                  */
4736                                 if (!(flags & XFS_BMAPI_METADATA)) {
4737                                         bma.userdata = (aoff == 0) ?
4738                                                 XFS_ALLOC_INITIAL_USER_DATA :
4739                                                 XFS_ALLOC_USERDATA;
4740                                 }
4741                                 /*
4742                                  * Fill in changeable bma fields.
4743                                  */
4744                                 bma.eof = eof;
4745                                 bma.firstblock = *firstblock;
4746                                 bma.alen = alen;
4747                                 bma.off = aoff;
4748                                 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4749                                 bma.wasdel = wasdelay;
4750                                 bma.minlen = minlen;
4751                                 bma.low = flist->xbf_low;
4752                                 bma.minleft = minleft;
4753                                 /*
4754                                  * Only want to do the alignment at the
4755                                  * eof if it is userdata and allocation length
4756                                  * is larger than a stripe unit.
4757                                  */
4758                                 if (mp->m_dalign && alen >= mp->m_dalign &&
4759                                     (!(flags & XFS_BMAPI_METADATA)) &&
4760                                     (whichfork == XFS_DATA_FORK)) {
4761                                         if ((error = xfs_bmap_isaeof(ip, aoff,
4762                                                         whichfork, &bma.aeof)))
4763                                                 goto error0;
4764                                 } else
4765                                         bma.aeof = 0;
4766                                 /*
4767                                  * Call allocator.
4768                                  */
4769                                 if ((error = xfs_bmap_alloc(&bma)))
4770                                         goto error0;
4771                                 /*
4772                                  * Copy out result fields.
4773                                  */
4774                                 abno = bma.rval;
4775                                 if ((flist->xbf_low = bma.low))
4776                                         minleft = 0;
4777                                 alen = bma.alen;
4778                                 aoff = bma.off;
4779                                 ASSERT(*firstblock == NULLFSBLOCK ||
4780                                        XFS_FSB_TO_AGNO(mp, *firstblock) ==
4781                                        XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4782                                        (flist->xbf_low &&
4783                                         XFS_FSB_TO_AGNO(mp, *firstblock) <
4784                                         XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4785                                 *firstblock = bma.firstblock;
4786                                 if (cur)
4787                                         cur->bc_private.b.firstblock =
4788                                                 *firstblock;
4789                                 if (abno == NULLFSBLOCK)
4790                                         break;
4791                                 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4792                                         cur = xfs_bmbt_init_cursor(mp, tp,
4793                                                 ip, whichfork);
4794                                         cur->bc_private.b.firstblock =
4795                                                 *firstblock;
4796                                         cur->bc_private.b.flist = flist;
4797                                 }
4798                                 /*
4799                                  * Bump the number of extents we've allocated
4800                                  * in this call.
4801                                  */
4802                                 nallocs++;
4803                         }
4804                         if (cur)
4805                                 cur->bc_private.b.flags =
4806                                         wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4807                         got.br_startoff = aoff;
4808                         got.br_startblock = abno;
4809                         got.br_blockcount = alen;
4810                         got.br_state = XFS_EXT_NORM;    /* assume normal */
4811                         /*
4812                          * Determine state of extent, and the filesystem.
4813                          * A wasdelay extent has been initialized, so
4814                          * shouldn't be flagged as unwritten.
4815                          */
4816                         if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4817                                 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4818                                         got.br_state = XFS_EXT_UNWRITTEN;
4819                         }
4820                         error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4821                                 firstblock, flist, &tmp_logflags, delta,
4822                                 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4823                         logflags |= tmp_logflags;
4824                         if (error)
4825                                 goto error0;
4826                         lastx = ifp->if_lastex;
4827                         ep = xfs_iext_get_ext(ifp, lastx);
4828                         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4829                         xfs_bmbt_get_all(ep, &got);
4830                         ASSERT(got.br_startoff <= aoff);
4831                         ASSERT(got.br_startoff + got.br_blockcount >=
4832                                 aoff + alen);
4833 #ifdef DEBUG
4834                         if (flags & XFS_BMAPI_DELAY) {
4835                                 ASSERT(isnullstartblock(got.br_startblock));
4836                                 ASSERT(startblockval(got.br_startblock) > 0);
4837                         }
4838                         ASSERT(got.br_state == XFS_EXT_NORM ||
4839                                got.br_state == XFS_EXT_UNWRITTEN);
4840 #endif
4841                         /*
4842                          * Fall down into the found allocated space case.
4843                          */
4844                 } else if (inhole) {
4845                         /*
4846                          * Reading in a hole.
4847                          */
4848                         mval->br_startoff = bno;
4849                         mval->br_startblock = HOLESTARTBLOCK;
4850                         mval->br_blockcount =
4851                                 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4852                         mval->br_state = XFS_EXT_NORM;
4853                         bno += mval->br_blockcount;
4854                         len -= mval->br_blockcount;
4855                         mval++;
4856                         n++;
4857                         continue;
4858                 }
4859                 /*
4860                  * Then deal with the allocated space we found.
4861                  */
4862                 ASSERT(ep != NULL);
4863                 if (!(flags & XFS_BMAPI_ENTIRE) &&
4864                     (got.br_startoff + got.br_blockcount > obno)) {
4865                         if (obno > bno)
4866                                 bno = obno;
4867                         ASSERT((bno >= obno) || (n == 0));
4868                         ASSERT(bno < end);
4869                         mval->br_startoff = bno;
4870                         if (isnullstartblock(got.br_startblock)) {
4871                                 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4872                                 mval->br_startblock = DELAYSTARTBLOCK;
4873                         } else
4874                                 mval->br_startblock =
4875                                         got.br_startblock +
4876                                         (bno - got.br_startoff);
4877                         /*
4878                          * Return the minimum of what we got and what we
4879                          * asked for for the length.  We can use the len
4880                          * variable here because it is modified below
4881                          * and we could have been there before coming
4882                          * here if the first part of the allocation
4883                          * didn't overlap what was asked for.
4884                          */
4885                         mval->br_blockcount =
4886                                 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4887                                         (bno - got.br_startoff));
4888                         mval->br_state = got.br_state;
4889                         ASSERT(mval->br_blockcount <= len);
4890                 } else {
4891                         *mval = got;
4892                         if (isnullstartblock(mval->br_startblock)) {
4893                                 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4894                                 mval->br_startblock = DELAYSTARTBLOCK;
4895                         }
4896                 }
4897
4898                 /*
4899                  * Check if writing previously allocated but
4900                  * unwritten extents.
4901                  */
4902                 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4903                     ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4904                         /*
4905                          * Modify (by adding) the state flag, if writing.
4906                          */
4907                         ASSERT(mval->br_blockcount <= len);
4908                         if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4909                                 cur = xfs_bmbt_init_cursor(mp,
4910                                         tp, ip, whichfork);
4911                                 cur->bc_private.b.firstblock =
4912                                         *firstblock;
4913                                 cur->bc_private.b.flist = flist;
4914                         }
4915                         mval->br_state = XFS_EXT_NORM;
4916                         error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4917                                 firstblock, flist, &tmp_logflags, delta,
4918                                 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4919                         logflags |= tmp_logflags;
4920                         if (error)
4921                                 goto error0;
4922                         lastx = ifp->if_lastex;
4923                         ep = xfs_iext_get_ext(ifp, lastx);
4924                         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4925                         xfs_bmbt_get_all(ep, &got);
4926                         /*
4927                          * We may have combined previously unwritten
4928                          * space with written space, so generate
4929                          * another request.
4930                          */
4931                         if (mval->br_blockcount < len)
4932                                 continue;
4933                 }
4934
4935                 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4936                        ((mval->br_startoff + mval->br_blockcount) <= end));
4937                 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4938                        (mval->br_blockcount <= len) ||
4939                        (mval->br_startoff < obno));
4940                 bno = mval->br_startoff + mval->br_blockcount;
4941                 len = end - bno;
4942                 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4943                         ASSERT(mval->br_startblock == mval[-1].br_startblock);
4944                         ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4945                         ASSERT(mval->br_state == mval[-1].br_state);
4946                         mval[-1].br_blockcount = mval->br_blockcount;
4947                         mval[-1].br_state = mval->br_state;
4948                 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4949                            mval[-1].br_startblock != DELAYSTARTBLOCK &&
4950                            mval[-1].br_startblock != HOLESTARTBLOCK &&
4951                            mval->br_startblock ==
4952                            mval[-1].br_startblock + mval[-1].br_blockcount &&
4953                            ((flags & XFS_BMAPI_IGSTATE) ||
4954                                 mval[-1].br_state == mval->br_state)) {
4955                         ASSERT(mval->br_startoff ==
4956                                mval[-1].br_startoff + mval[-1].br_blockcount);
4957                         mval[-1].br_blockcount += mval->br_blockcount;
4958                 } else if (n > 0 &&
4959                            mval->br_startblock == DELAYSTARTBLOCK &&
4960                            mval[-1].br_startblock == DELAYSTARTBLOCK &&
4961                            mval->br_startoff ==
4962                            mval[-1].br_startoff + mval[-1].br_blockcount) {
4963                         mval[-1].br_blockcount += mval->br_blockcount;
4964                         mval[-1].br_state = mval->br_state;
4965                 } else if (!((n == 0) &&
4966                              ((mval->br_startoff + mval->br_blockcount) <=
4967                               obno))) {
4968                         mval++;
4969                         n++;
4970                 }
4971                 /*
4972                  * If we're done, stop now.  Stop when we've allocated
4973                  * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise
4974                  * the transaction may get too big.
4975                  */
4976                 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4977                         break;
4978                 /*
4979                  * Else go on to the next record.
4980                  */
4981                 ep = xfs_iext_get_ext(ifp, ++lastx);
4982                 prev = got;
4983                 if (lastx >= nextents)
4984                         eof = 1;
4985                 else
4986                         xfs_bmbt_get_all(ep, &got);
4987         }
4988         ifp->if_lastex = lastx;
4989         *nmap = n;
4990         /*
4991          * Transform from btree to extents, give it cur.
4992          */
4993         if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4994             XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4995                 ASSERT(wr && cur);
4996                 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4997                         &tmp_logflags, whichfork);
4998                 logflags |= tmp_logflags;
4999                 if (error)
5000                         goto error0;
5001         }
5002         ASSERT(ifp->if_ext_max ==
5003                XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5004         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5005                XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5006         error = 0;
5007         if (delta && delta->xed_startoff != NULLFILEOFF) {
5008                 /* A change was actually made.
5009                  * Note that delta->xed_blockount is an offset at this
5010                  * point and needs to be converted to a block count.
5011                  */
5012                 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5013                 delta->xed_blockcount -= delta->xed_startoff;
5014         }
5015 error0:
5016         /*
5017          * Log everything.  Do this after conversion, there's no point in
5018          * logging the extent records if we've converted to btree format.
5019          */
5020         if ((logflags & xfs_ilog_fext(whichfork)) &&
5021             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5022                 logflags &= ~xfs_ilog_fext(whichfork);
5023         else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5024                  XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5025                 logflags &= ~xfs_ilog_fbroot(whichfork);
5026         /*
5027          * Log whatever the flags say, even if error.  Otherwise we might miss
5028          * detecting a case where the data is changed, there's an error,
5029          * and it's not logged so we don't shutdown when we should.
5030          */
5031         if (logflags) {
5032                 ASSERT(tp && wr);
5033                 xfs_trans_log_inode(tp, ip, logflags);
5034         }
5035         if (cur) {
5036                 if (!error) {
5037                         ASSERT(*firstblock == NULLFSBLOCK ||
5038                                XFS_FSB_TO_AGNO(mp, *firstblock) ==
5039                                XFS_FSB_TO_AGNO(mp,
5040                                        cur->bc_private.b.firstblock) ||
5041                                (flist->xbf_low &&
5042                                 XFS_FSB_TO_AGNO(mp, *firstblock) <
5043                                 XFS_FSB_TO_AGNO(mp,
5044                                         cur->bc_private.b.firstblock)));
5045                         *firstblock = cur->bc_private.b.firstblock;
5046                 }
5047                 xfs_btree_del_cursor(cur,
5048                         error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5049         }
5050         if (!error)
5051                 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5052                         orig_nmap, *nmap);
5053         return error;
5054 }
5055
5056 /*
5057  * Map file blocks to filesystem blocks, simple version.
5058  * One block (extent) only, read-only.
5059  * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5060  * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5061  * was set and all the others were clear.
5062  */
5063 int                                             /* error */
5064 xfs_bmapi_single(
5065         xfs_trans_t     *tp,            /* transaction pointer */
5066         xfs_inode_t     *ip,            /* incore inode */
5067         int             whichfork,      /* data or attr fork */
5068         xfs_fsblock_t   *fsb,           /* output: mapped block */
5069         xfs_fileoff_t   bno)            /* starting file offs. mapped */
5070 {
5071         int             eof;            /* we've hit the end of extents */
5072         int             error;          /* error return */
5073         xfs_bmbt_irec_t got;            /* current file extent record */
5074         xfs_ifork_t     *ifp;           /* inode fork pointer */
5075         xfs_extnum_t    lastx;          /* last useful extent number */
5076         xfs_bmbt_irec_t prev;           /* previous file extent record */
5077
5078         ifp = XFS_IFORK_PTR(ip, whichfork);
5079         if (unlikely(
5080             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5081             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5082                XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5083                                 ip->i_mount);
5084                return XFS_ERROR(EFSCORRUPTED);
5085         }
5086         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5087                 return XFS_ERROR(EIO);
5088         XFS_STATS_INC(xs_blk_mapr);
5089         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5090             (error = xfs_iread_extents(tp, ip, whichfork)))
5091                 return error;
5092         (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5093                 &prev);
5094         /*
5095          * Reading past eof, act as though there's a hole
5096          * up to end.
5097          */
5098         if (eof || got.br_startoff > bno) {
5099                 *fsb = NULLFSBLOCK;
5100                 return 0;
5101         }
5102         ASSERT(!isnullstartblock(got.br_startblock));
5103         ASSERT(bno < got.br_startoff + got.br_blockcount);
5104         *fsb = got.br_startblock + (bno - got.br_startoff);
5105         ifp->if_lastex = lastx;
5106         return 0;
5107 }
5108
5109 /*
5110  * Unmap (remove) blocks from a file.
5111  * If nexts is nonzero then the number of extents to remove is limited to
5112  * that value.  If not all extents in the block range can be removed then
5113  * *done is set.
5114  */
5115 int                                             /* error */
5116 xfs_bunmapi(
5117         xfs_trans_t             *tp,            /* transaction pointer */
5118         struct xfs_inode        *ip,            /* incore inode */
5119         xfs_fileoff_t           bno,            /* starting offset to unmap */
5120         xfs_filblks_t           len,            /* length to unmap in file */
5121         int                     flags,          /* misc flags */
5122         xfs_extnum_t            nexts,          /* number of extents max */
5123         xfs_fsblock_t           *firstblock,    /* first allocated block
5124                                                    controls a.g. for allocs */
5125         xfs_bmap_free_t         *flist,         /* i/o: list extents to free */
5126         xfs_extdelta_t          *delta,         /* o: change made to incore
5127                                                    extents */
5128         int                     *done)          /* set if not done yet */
5129 {
5130         xfs_btree_cur_t         *cur;           /* bmap btree cursor */
5131         xfs_bmbt_irec_t         del;            /* extent being deleted */
5132         int                     eof;            /* is deleting at eof */
5133         xfs_bmbt_rec_host_t     *ep;            /* extent record pointer */
5134         int                     error;          /* error return value */
5135         xfs_extnum_t            extno;          /* extent number in list */
5136         xfs_bmbt_irec_t         got;            /* current extent record */
5137         xfs_ifork_t             *ifp;           /* inode fork pointer */
5138         int                     isrt;           /* freeing in rt area */
5139         xfs_extnum_t            lastx;          /* last extent index used */
5140         int                     logflags;       /* transaction logging flags */
5141         xfs_extlen_t            mod;            /* rt extent offset */
5142         xfs_mount_t             *mp;            /* mount structure */
5143         xfs_extnum_t            nextents;       /* number of file extents */
5144         xfs_bmbt_irec_t         prev;           /* previous extent record */
5145         xfs_fileoff_t           start;          /* first file offset deleted */
5146         int                     tmp_logflags;   /* partial logging flags */
5147         int                     wasdel;         /* was a delayed alloc extent */
5148         int                     whichfork;      /* data or attribute fork */
5149         int                     rsvd;           /* OK to allocate reserved blocks */
5150         xfs_fsblock_t           sum;
5151
5152         trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5153
5154         whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5155                 XFS_ATTR_FORK : XFS_DATA_FORK;
5156         ifp = XFS_IFORK_PTR(ip, whichfork);
5157         if (unlikely(
5158             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5159             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5160                 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5161                                  ip->i_mount);
5162                 return XFS_ERROR(EFSCORRUPTED);
5163         }
5164         mp = ip->i_mount;
5165         if (XFS_FORCED_SHUTDOWN(mp))
5166                 return XFS_ERROR(EIO);
5167         rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5168         ASSERT(len > 0);
5169         ASSERT(nexts >= 0);
5170         ASSERT(ifp->if_ext_max ==
5171                XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5172         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5173             (error = xfs_iread_extents(tp, ip, whichfork)))
5174                 return error;
5175         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5176         if (nextents == 0) {
5177                 *done = 1;
5178                 return 0;
5179         }
5180         XFS_STATS_INC(xs_blk_unmap);
5181         isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5182         start = bno;
5183         bno = start + len - 1;
5184         ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5185                 &prev);
5186         if (delta) {
5187                 delta->xed_startoff = NULLFILEOFF;
5188                 delta->xed_blockcount = 0;
5189         }
5190         /*
5191          * Check to see if the given block number is past the end of the
5192          * file, back up to the last block if so...
5193          */
5194         if (eof) {
5195                 ep = xfs_iext_get_ext(ifp, --lastx);
5196                 xfs_bmbt_get_all(ep, &got);
5197                 bno = got.br_startoff + got.br_blockcount - 1;
5198         }
5199         logflags = 0;
5200         if (ifp->if_flags & XFS_IFBROOT) {
5201                 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5202                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5203                 cur->bc_private.b.firstblock = *firstblock;
5204                 cur->bc_private.b.flist = flist;
5205                 cur->bc_private.b.flags = 0;
5206         } else
5207                 cur = NULL;
5208         extno = 0;
5209         while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5210                (nexts == 0 || extno < nexts)) {
5211                 /*
5212                  * Is the found extent after a hole in which bno lives?
5213                  * Just back up to the previous extent, if so.
5214                  */
5215                 if (got.br_startoff > bno) {
5216                         if (--lastx < 0)
5217                                 break;
5218                         ep = xfs_iext_get_ext(ifp, lastx);
5219                         xfs_bmbt_get_all(ep, &got);
5220                 }
5221                 /*
5222                  * Is the last block of this extent before the range
5223                  * we're supposed to delete?  If so, we're done.
5224                  */
5225                 bno = XFS_FILEOFF_MIN(bno,
5226                         got.br_startoff + got.br_blockcount - 1);
5227                 if (bno < start)
5228                         break;
5229                 /*
5230                  * Then deal with the (possibly delayed) allocated space
5231                  * we found.
5232                  */
5233                 ASSERT(ep != NULL);
5234                 del = got;
5235                 wasdel = isnullstartblock(del.br_startblock);
5236                 if (got.br_startoff < start) {
5237                         del.br_startoff = start;
5238                         del.br_blockcount -= start - got.br_startoff;
5239                         if (!wasdel)
5240                                 del.br_startblock += start - got.br_startoff;
5241                 }
5242                 if (del.br_startoff + del.br_blockcount > bno + 1)
5243                         del.br_blockcount = bno + 1 - del.br_startoff;
5244                 sum = del.br_startblock + del.br_blockcount;
5245                 if (isrt &&
5246                     (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5247                         /*
5248                          * Realtime extent not lined up at the end.
5249                          * The extent could have been split into written
5250                          * and unwritten pieces, or we could just be
5251                          * unmapping part of it.  But we can't really
5252                          * get rid of part of a realtime extent.
5253                          */
5254                         if (del.br_state == XFS_EXT_UNWRITTEN ||
5255                             !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5256                                 /*
5257                                  * This piece is unwritten, or we're not
5258                                  * using unwritten extents.  Skip over it.
5259                                  */
5260                                 ASSERT(bno >= mod);
5261                                 bno -= mod > del.br_blockcount ?
5262                                         del.br_blockcount : mod;
5263                                 if (bno < got.br_startoff) {
5264                                         if (--lastx >= 0)
5265                                                 xfs_bmbt_get_all(xfs_iext_get_ext(
5266                                                         ifp, lastx), &got);
5267                                 }
5268                                 continue;
5269                         }
5270                         /*
5271                          * It's written, turn it unwritten.
5272                          * This is better than zeroing it.
5273                          */
5274                         ASSERT(del.br_state == XFS_EXT_NORM);
5275                         ASSERT(xfs_trans_get_block_res(tp) > 0);
5276                         /*
5277                          * If this spans a realtime extent boundary,
5278                          * chop it back to the start of the one we end at.
5279                          */
5280                         if (del.br_blockcount > mod) {
5281                                 del.br_startoff += del.br_blockcount - mod;
5282                                 del.br_startblock += del.br_blockcount - mod;
5283                                 del.br_blockcount = mod;
5284                         }
5285                         del.br_state = XFS_EXT_UNWRITTEN;
5286                         error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5287                                 firstblock, flist, &logflags, delta,
5288                                 XFS_DATA_FORK, 0);
5289                         if (error)
5290                                 goto error0;
5291                         goto nodelete;
5292                 }
5293                 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5294                         /*
5295                          * Realtime extent is lined up at the end but not
5296                          * at the front.  We'll get rid of full extents if
5297                          * we can.
5298                          */
5299                         mod = mp->m_sb.sb_rextsize - mod;
5300                         if (del.br_blockcount > mod) {
5301                                 del.br_blockcount -= mod;
5302                                 del.br_startoff += mod;
5303                                 del.br_startblock += mod;
5304                         } else if ((del.br_startoff == start &&
5305                                     (del.br_state == XFS_EXT_UNWRITTEN ||
5306                                      xfs_trans_get_block_res(tp) == 0)) ||
5307                                    !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5308                                 /*
5309                                  * Can't make it unwritten.  There isn't
5310                                  * a full extent here so just skip it.
5311                                  */
5312                                 ASSERT(bno >= del.br_blockcount);
5313                                 bno -= del.br_blockcount;
5314                                 if (bno < got.br_startoff) {
5315                                         if (--lastx >= 0)
5316                                                 xfs_bmbt_get_all(--ep, &got);
5317                                 }
5318                                 continue;
5319                         } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5320                                 /*
5321                                  * This one is already unwritten.
5322                                  * It must have a written left neighbor.
5323                                  * Unwrite the killed part of that one and
5324                                  * try again.
5325                                  */
5326                                 ASSERT(lastx > 0);
5327                                 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5328                                                 lastx - 1), &prev);
5329                                 ASSERT(prev.br_state == XFS_EXT_NORM);
5330                                 ASSERT(!isnullstartblock(prev.br_startblock));
5331                                 ASSERT(del.br_startblock ==
5332                                        prev.br_startblock + prev.br_blockcount);
5333                                 if (prev.br_startoff < start) {
5334                                         mod = start - prev.br_startoff;
5335                                         prev.br_blockcount -= mod;
5336                                         prev.br_startblock += mod;
5337                                         prev.br_startoff = start;
5338                                 }
5339                                 prev.br_state = XFS_EXT_UNWRITTEN;
5340                                 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5341                                         &prev, firstblock, flist, &logflags,
5342                                         delta, XFS_DATA_FORK, 0);
5343                                 if (error)
5344                                         goto error0;
5345                                 goto nodelete;
5346                         } else {
5347                                 ASSERT(del.br_state == XFS_EXT_NORM);
5348                                 del.br_state = XFS_EXT_UNWRITTEN;
5349                                 error = xfs_bmap_add_extent(ip, lastx, &cur,
5350                                         &del, firstblock, flist, &logflags,
5351                                         delta, XFS_DATA_FORK, 0);
5352                                 if (error)
5353                                         goto error0;
5354                                 goto nodelete;
5355                         }
5356                 }
5357                 if (wasdel) {
5358                         ASSERT(startblockval(del.br_startblock) > 0);
5359                         /* Update realtime/data freespace, unreserve quota */
5360                         if (isrt) {
5361                                 xfs_filblks_t rtexts;
5362
5363                                 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5364                                 do_div(rtexts, mp->m_sb.sb_rextsize);
5365                                 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5366                                                 (int64_t)rtexts, rsvd);
5367                                 (void)xfs_trans_reserve_quota_nblks(NULL,
5368                                         ip, -((long)del.br_blockcount), 0,
5369                                         XFS_QMOPT_RES_RTBLKS);
5370                         } else {
5371                                 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5372                                                 (int64_t)del.br_blockcount, rsvd);
5373                                 (void)xfs_trans_reserve_quota_nblks(NULL,
5374                                         ip, -((long)del.br_blockcount), 0,
5375                                         XFS_QMOPT_RES_REGBLKS);
5376                         }
5377                         ip->i_delayed_blks -= del.br_blockcount;
5378                         if (cur)
5379                                 cur->bc_private.b.flags |=
5380                                         XFS_BTCUR_BPRV_WASDEL;
5381                 } else if (cur)
5382                         cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5383                 /*
5384                  * If it's the case where the directory code is running
5385                  * with no block reservation, and the deleted block is in
5386                  * the middle of its extent, and the resulting insert
5387                  * of an extent would cause transformation to btree format,
5388                  * then reject it.  The calling code will then swap
5389                  * blocks around instead.
5390                  * We have to do this now, rather than waiting for the
5391                  * conversion to btree format, since the transaction
5392                  * will be dirty.
5393                  */
5394                 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5395                     XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5396                     XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5397                     del.br_startoff > got.br_startoff &&
5398                     del.br_startoff + del.br_blockcount <
5399                     got.br_startoff + got.br_blockcount) {
5400                         error = XFS_ERROR(ENOSPC);
5401                         goto error0;
5402                 }
5403                 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5404                                 &tmp_logflags, delta, whichfork, rsvd);
5405                 logflags |= tmp_logflags;
5406                 if (error)
5407                         goto error0;
5408                 bno = del.br_startoff - 1;
5409 nodelete:
5410                 lastx = ifp->if_lastex;
5411                 /*
5412                  * If not done go on to the next (previous) record.
5413                  * Reset ep in case the extents array was re-alloced.
5414                  */
5415                 ep = xfs_iext_get_ext(ifp, lastx);
5416                 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5417                         if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5418                             xfs_bmbt_get_startoff(ep) > bno) {
5419                                 if (--lastx >= 0)
5420                                         ep = xfs_iext_get_ext(ifp, lastx);
5421                         }
5422                         if (lastx >= 0)
5423                                 xfs_bmbt_get_all(ep, &got);
5424                         extno++;
5425                 }
5426         }
5427         ifp->if_lastex = lastx;
5428         *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5429         ASSERT(ifp->if_ext_max ==
5430                XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5431         /*
5432          * Convert to a btree if necessary.
5433          */
5434         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5435             XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5436                 ASSERT(cur == NULL);
5437                 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5438                         &cur, 0, &tmp_logflags, whichfork);
5439                 logflags |= tmp_logflags;
5440                 if (error)
5441                         goto error0;
5442         }
5443         /*
5444          * transform from btree to extents, give it cur
5445          */
5446         else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5447                  XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5448                 ASSERT(cur != NULL);
5449                 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5450                         whichfork);
5451                 logflags |= tmp_logflags;
5452                 if (error)
5453                         goto error0;
5454         }
5455         /*
5456          * transform from extents to local?
5457          */
5458         ASSERT(ifp->if_ext_max ==
5459                XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5460         error = 0;
5461         if (delta && delta->xed_startoff != NULLFILEOFF) {
5462                 /* A change was actually made.
5463                  * Note that delta->xed_blockount is an offset at this
5464                  * point and needs to be converted to a block count.
5465                  */
5466                 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5467                 delta->xed_blockcount -= delta->xed_startoff;
5468         }
5469 error0:
5470         /*
5471          * Log everything.  Do this after conversion, there's no point in
5472          * logging the extent records if we've converted to btree format.
5473          */
5474         if ((logflags & xfs_ilog_fext(whichfork)) &&
5475             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5476                 logflags &= ~xfs_ilog_fext(whichfork);
5477         else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5478                  XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5479                 logflags &= ~xfs_ilog_fbroot(whichfork);
5480         /*
5481          * Log inode even in the error case, if the transaction
5482          * is dirty we'll need to shut down the filesystem.
5483          */
5484         if (logflags)
5485                 xfs_trans_log_inode(tp, ip, logflags);
5486         if (cur) {
5487                 if (!error) {
5488                         *firstblock = cur->bc_private.b.firstblock;
5489                         cur->bc_private.b.allocated = 0;
5490                 }
5491                 xfs_btree_del_cursor(cur,
5492                         error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5493         }
5494         return error;
5495 }
5496
5497 /*
5498  * returns 1 for success, 0 if we failed to map the extent.
5499  */
5500 STATIC int
5501 xfs_getbmapx_fix_eof_hole(
5502         xfs_inode_t             *ip,            /* xfs incore inode pointer */
5503         struct getbmapx         *out,           /* output structure */
5504         int                     prealloced,     /* this is a file with
5505                                                  * preallocated data space */
5506         __int64_t               end,            /* last block requested */
5507         xfs_fsblock_t           startblock)
5508 {
5509         __int64_t               fixlen;
5510         xfs_mount_t             *mp;            /* file system mount point */
5511         xfs_ifork_t             *ifp;           /* inode fork pointer */
5512         xfs_extnum_t            lastx;          /* last extent pointer */
5513         xfs_fileoff_t           fileblock;
5514
5515         if (startblock == HOLESTARTBLOCK) {
5516                 mp = ip->i_mount;
5517                 out->bmv_block = -1;
5518                 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5519                 fixlen -= out->bmv_offset;
5520                 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5521                         /* Came to hole at EOF. Trim it. */
5522                         if (fixlen <= 0)
5523                                 return 0;
5524                         out->bmv_length = fixlen;
5525                 }
5526         } else {
5527                 if (startblock == DELAYSTARTBLOCK)
5528                         out->bmv_block = -2;
5529                 else
5530                         out->bmv_block = xfs_fsb_to_db(ip, startblock);
5531                 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5532                 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5533                 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5534                    (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5535                         out->bmv_oflags |= BMV_OF_LAST;
5536         }
5537
5538         return 1;
5539 }
5540
5541 /*
5542  * Get inode's extents as described in bmv, and format for output.
5543  * Calls formatter to fill the user's buffer until all extents
5544  * are mapped, until the passed-in bmv->bmv_count slots have
5545  * been filled, or until the formatter short-circuits the loop,
5546  * if it is tracking filled-in extents on its own.
5547  */
5548 int                                             /* error code */
5549 xfs_getbmap(
5550         xfs_inode_t             *ip,
5551         struct getbmapx         *bmv,           /* user bmap structure */
5552         xfs_bmap_format_t       formatter,      /* format to user */
5553         void                    *arg)           /* formatter arg */
5554 {
5555         __int64_t               bmvend;         /* last block requested */
5556         int                     error = 0;      /* return value */
5557         __int64_t               fixlen;         /* length for -1 case */
5558         int                     i;              /* extent number */
5559         int                     lock;           /* lock state */
5560         xfs_bmbt_irec_t         *map;           /* buffer for user's data */
5561         xfs_mount_t             *mp;            /* file system mount point */
5562         int                     nex;            /* # of user extents can do */
5563         int                     nexleft;        /* # of user extents left */
5564         int                     subnex;         /* # of bmapi's can do */
5565         int                     nmap;           /* number of map entries */
5566         struct getbmapx         *out;           /* output structure */
5567         int                     whichfork;      /* data or attr fork */
5568         int                     prealloced;     /* this is a file with
5569                                                  * preallocated data space */
5570         int                     iflags;         /* interface flags */
5571         int                     bmapi_flags;    /* flags for xfs_bmapi */
5572         int                     cur_ext = 0;
5573
5574         mp = ip->i_mount;
5575         iflags = bmv->bmv_iflags;
5576         whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5577
5578         if (whichfork == XFS_ATTR_FORK) {
5579                 if (XFS_IFORK_Q(ip)) {
5580                         if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5581                             ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5582                             ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5583                                 return XFS_ERROR(EINVAL);
5584                 } else if (unlikely(
5585                            ip->i_d.di_aformat != 0 &&
5586                            ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5587                         XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5588                                          ip->i_mount);
5589                         return XFS_ERROR(EFSCORRUPTED);
5590                 }
5591
5592                 prealloced = 0;
5593                 fixlen = 1LL << 32;
5594         } else {
5595                 /*
5596                  * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
5597                  * not generate a DMAPI read event.  Otherwise, if the
5598                  * DM_EVENT_READ bit is set for the file, generate a read
5599                  * event in order that the DMAPI application may do its thing
5600                  * before we return the extents.  Usually this means restoring
5601                  * user file data to regions of the file that look like holes.
5602                  *
5603                  * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5604                  * BMV_IF_NO_DMAPI_READ so that read events are generated.
5605                  * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
5606                  * could misinterpret holes in a DMAPI file as true holes,
5607                  * when in fact they may represent offline user data.
5608                  */
5609                 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5610                     !(iflags & BMV_IF_NO_DMAPI_READ)) {
5611                         error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
5612                                               0, 0, 0, NULL);
5613                         if (error)
5614                                 return XFS_ERROR(error);
5615                 }
5616
5617                 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5618                     ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5619                     ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5620                         return XFS_ERROR(EINVAL);
5621
5622                 if (xfs_get_extsz_hint(ip) ||
5623                     ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5624                         prealloced = 1;
5625                         fixlen = XFS_MAXIOFFSET(mp);
5626                 } else {
5627                         prealloced = 0;
5628                         fixlen = ip->i_size;
5629                 }
5630         }
5631
5632         if (bmv->bmv_length == -1) {
5633                 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5634                 bmv->bmv_length =
5635                         max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5636         } else if (bmv->bmv_length == 0) {
5637                 bmv->bmv_entries = 0;
5638                 return 0;
5639         } else if (bmv->bmv_length < 0) {
5640                 return XFS_ERROR(EINVAL);
5641         }
5642
5643         nex = bmv->bmv_count - 1;
5644         if (nex <= 0)
5645                 return XFS_ERROR(EINVAL);
5646         bmvend = bmv->bmv_offset + bmv->bmv_length;
5647
5648
5649         if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5650                 return XFS_ERROR(ENOMEM);
5651         out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5652         if (!out)
5653                 return XFS_ERROR(ENOMEM);
5654
5655         xfs_ilock(ip, XFS_IOLOCK_SHARED);
5656         if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5657                 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5658                         error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5659                         if (error)
5660                                 goto out_unlock_iolock;
5661                 }
5662
5663                 ASSERT(ip->i_delayed_blks == 0);
5664         }
5665
5666         lock = xfs_ilock_map_shared(ip);
5667
5668         /*
5669          * Don't let nex be bigger than the number of extents
5670          * we can have assuming alternating holes and real extents.
5671          */
5672         if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5673                 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5674
5675         bmapi_flags = xfs_bmapi_aflag(whichfork);
5676         if (!(iflags & BMV_IF_PREALLOC))
5677                 bmapi_flags |= XFS_BMAPI_IGSTATE;
5678
5679         /*
5680          * Allocate enough space to handle "subnex" maps at a time.
5681          */
5682         error = ENOMEM;
5683         subnex = 16;
5684         map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5685         if (!map)
5686                 goto out_unlock_ilock;
5687
5688         bmv->bmv_entries = 0;
5689
5690         if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5691             (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5692                 error = 0;
5693                 goto out_free_map;
5694         }
5695
5696         nexleft = nex;
5697
5698         do {
5699                 nmap = (nexleft > subnex) ? subnex : nexleft;
5700                 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5701                                   XFS_BB_TO_FSB(mp, bmv->bmv_length),
5702                                   bmapi_flags, NULL, 0, map, &nmap,
5703                                   NULL, NULL);
5704                 if (error)
5705                         goto out_free_map;
5706                 ASSERT(nmap <= subnex);
5707
5708                 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5709                         out[cur_ext].bmv_oflags = 0;
5710                         if (map[i].br_state == XFS_EXT_UNWRITTEN)
5711                                 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5712                         else if (map[i].br_startblock == DELAYSTARTBLOCK)
5713                                 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5714                         out[cur_ext].bmv_offset =
5715                                 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5716                         out[cur_ext].bmv_length =
5717                                 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5718                         out[cur_ext].bmv_unused1 = 0;
5719                         out[cur_ext].bmv_unused2 = 0;
5720                         ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5721                               (map[i].br_startblock != DELAYSTARTBLOCK));
5722                         if (map[i].br_startblock == HOLESTARTBLOCK &&
5723                             whichfork == XFS_ATTR_FORK) {
5724                                 /* came to the end of attribute fork */
5725                                 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5726                                 goto out_free_map;
5727                         }
5728
5729                         if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5730                                         prealloced, bmvend,
5731                                         map[i].br_startblock))
5732                                 goto out_free_map;
5733
5734                         nexleft--;
5735                         bmv->bmv_offset =
5736                                 out[cur_ext].bmv_offset +
5737                                 out[cur_ext].bmv_length;
5738                         bmv->bmv_length =
5739                                 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5740                         bmv->bmv_entries++;
5741                         cur_ext++;
5742                 }
5743         } while (nmap && nexleft && bmv->bmv_length);
5744
5745  out_free_map:
5746         kmem_free(map);
5747  out_unlock_ilock:
5748         xfs_iunlock_map_shared(ip, lock);
5749  out_unlock_iolock:
5750         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5751
5752         for (i = 0; i < cur_ext; i++) {
5753                 int full = 0;   /* user array is full */
5754
5755                 /* format results & advance arg */
5756                 error = formatter(&arg, &out[i], &full);
5757                 if (error || full)
5758                         break;
5759         }
5760
5761         kmem_free(out);
5762         return error;
5763 }
5764
5765 /*
5766  * Check the last inode extent to determine whether this allocation will result
5767  * in blocks being allocated at the end of the file. When we allocate new data
5768  * blocks at the end of the file which do not start at the previous data block,
5769  * we will try to align the new blocks at stripe unit boundaries.
5770  */
5771 STATIC int                              /* error */
5772 xfs_bmap_isaeof(
5773         xfs_inode_t     *ip,            /* incore inode pointer */
5774         xfs_fileoff_t   off,            /* file offset in fsblocks */
5775         int             whichfork,      /* data or attribute fork */
5776         char            *aeof)          /* return value */
5777 {
5778         int             error;          /* error return value */
5779         xfs_ifork_t     *ifp;           /* inode fork pointer */
5780         xfs_bmbt_rec_host_t *lastrec;   /* extent record pointer */
5781         xfs_extnum_t    nextents;       /* number of file extents */
5782         xfs_bmbt_irec_t s;              /* expanded extent record */
5783
5784         ASSERT(whichfork == XFS_DATA_FORK);
5785         ifp = XFS_IFORK_PTR(ip, whichfork);
5786         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5787             (error = xfs_iread_extents(NULL, ip, whichfork)))
5788                 return error;
5789         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5790         if (nextents == 0) {
5791                 *aeof = 1;
5792                 return 0;
5793         }
5794         /*
5795          * Go to the last extent
5796          */
5797         lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5798         xfs_bmbt_get_all(lastrec, &s);
5799         /*
5800          * Check we are allocating in the last extent (for delayed allocations)
5801          * or past the last extent for non-delayed allocations.
5802          */
5803         *aeof = (off >= s.br_startoff &&
5804                  off < s.br_startoff + s.br_blockcount &&
5805                  isnullstartblock(s.br_startblock)) ||
5806                 off >= s.br_startoff + s.br_blockcount;
5807         return 0;
5808 }
5809
5810 /*
5811  * Check if the endoff is outside the last extent. If so the caller will grow
5812  * the allocation to a stripe unit boundary.
5813  */
5814 int                                     /* error */
5815 xfs_bmap_eof(
5816         xfs_inode_t     *ip,            /* incore inode pointer */
5817         xfs_fileoff_t   endoff,         /* file offset in fsblocks */
5818         int             whichfork,      /* data or attribute fork */
5819         int             *eof)           /* result value */
5820 {
5821         xfs_fsblock_t   blockcount;     /* extent block count */
5822         int             error;          /* error return value */
5823         xfs_ifork_t     *ifp;           /* inode fork pointer */
5824         xfs_bmbt_rec_host_t *lastrec;   /* extent record pointer */
5825         xfs_extnum_t    nextents;       /* number of file extents */
5826         xfs_fileoff_t   startoff;       /* extent starting file offset */
5827
5828         ASSERT(whichfork == XFS_DATA_FORK);
5829         ifp = XFS_IFORK_PTR(ip, whichfork);
5830         if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5831             (error = xfs_iread_extents(NULL, ip, whichfork)))
5832                 return error;
5833         nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5834         if (nextents == 0) {
5835                 *eof = 1;
5836                 return 0;
5837         }
5838         /*
5839          * Go to the last extent
5840          */
5841         lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5842         startoff = xfs_bmbt_get_startoff(lastrec);
5843         blockcount = xfs_bmbt_get_blockcount(lastrec);
5844         *eof = endoff >= startoff + blockcount;
5845         return 0;
5846 }
5847
5848 #ifdef DEBUG
5849 STATIC
5850 xfs_buf_t *
5851 xfs_bmap_get_bp(
5852         xfs_btree_cur_t         *cur,
5853         xfs_fsblock_t           bno)
5854 {
5855         int i;
5856         xfs_buf_t *bp;
5857
5858         if (!cur)
5859                 return(NULL);
5860
5861         bp = NULL;
5862         for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5863                 bp = cur->bc_bufs[i];
5864                 if (!bp) break;
5865                 if (XFS_BUF_ADDR(bp) == bno)
5866                         break;  /* Found it */
5867         }
5868         if (i == XFS_BTREE_MAXLEVELS)
5869                 bp = NULL;
5870
5871         if (!bp) { /* Chase down all the log items to see if the bp is there */
5872                 xfs_log_item_chunk_t    *licp;
5873                 xfs_trans_t             *tp;
5874
5875                 tp = cur->bc_tp;
5876                 licp = &tp->t_items;
5877                 while (!bp && licp != NULL) {
5878                         if (xfs_lic_are_all_free(licp)) {
5879                                 licp = licp->lic_next;
5880                                 continue;
5881                         }
5882                         for (i = 0; i < licp->lic_unused; i++) {
5883                                 xfs_log_item_desc_t     *lidp;
5884                                 xfs_log_item_t          *lip;
5885                                 xfs_buf_log_item_t      *bip;
5886                                 xfs_buf_t               *lbp;
5887
5888                                 if (xfs_lic_isfree(licp, i)) {
5889                                         continue;
5890                                 }
5891
5892                                 lidp = xfs_lic_slot(licp, i);
5893                                 lip = lidp->lid_item;
5894                                 if (lip->li_type != XFS_LI_BUF)
5895                                         continue;
5896
5897                                 bip = (xfs_buf_log_item_t *)lip;
5898                                 lbp = bip->bli_buf;
5899
5900                                 if (XFS_BUF_ADDR(lbp) == bno) {
5901                                         bp = lbp;
5902                                         break; /* Found it */
5903                                 }
5904                         }
5905                         licp = licp->lic_next;
5906                 }
5907         }
5908         return(bp);
5909 }
5910
5911 STATIC void
5912 xfs_check_block(
5913         struct xfs_btree_block  *block,
5914         xfs_mount_t             *mp,
5915         int                     root,
5916         short                   sz)
5917 {
5918         int                     i, j, dmxr;
5919         __be64                  *pp, *thispa;   /* pointer to block address */
5920         xfs_bmbt_key_t          *prevp, *keyp;
5921
5922         ASSERT(be16_to_cpu(block->bb_level) > 0);
5923
5924         prevp = NULL;
5925         for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5926                 dmxr = mp->m_bmap_dmxr[0];
5927                 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5928
5929                 if (prevp) {
5930                         ASSERT(be64_to_cpu(prevp->br_startoff) <
5931                                be64_to_cpu(keyp->br_startoff));
5932                 }
5933                 prevp = keyp;
5934
5935                 /*
5936                  * Compare the block numbers to see if there are dups.
5937                  */
5938                 if (root)
5939                         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5940                 else
5941                         pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5942
5943                 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5944                         if (root)
5945                                 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5946                         else
5947                                 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5948                         if (*thispa == *pp) {
5949                                 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
5950                                         __func__, j, i,
5951                                         (unsigned long long)be64_to_cpu(*thispa));
5952                                 panic("%s: ptrs are equal in node\n",
5953                                         __func__);
5954                         }
5955                 }
5956         }
5957 }
5958
5959 /*
5960  * Check that the extents for the inode ip are in the right order in all
5961  * btree leaves.
5962  */
5963
5964 STATIC void
5965 xfs_bmap_check_leaf_extents(
5966         xfs_btree_cur_t         *cur,   /* btree cursor or null */
5967         xfs_inode_t             *ip,            /* incore inode pointer */
5968         int                     whichfork)      /* data or attr fork */
5969 {
5970         struct xfs_btree_block  *block; /* current btree block */
5971         xfs_fsblock_t           bno;    /* block # of "block" */
5972         xfs_buf_t               *bp;    /* buffer for "block" */
5973         int                     error;  /* error return value */
5974         xfs_extnum_t            i=0, j; /* index into the extents list */
5975         xfs_ifork_t             *ifp;   /* fork structure */
5976         int                     level;  /* btree level, for checking */
5977         xfs_mount_t             *mp;    /* file system mount structure */
5978         __be64                  *pp;    /* pointer to block address */
5979         xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
5980         xfs_bmbt_rec_t          last = {0, 0}; /* last extent in prev block */
5981         xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
5982         int                     bp_release = 0;
5983
5984         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5985                 return;
5986         }
5987
5988         bno = NULLFSBLOCK;
5989         mp = ip->i_mount;
5990         ifp = XFS_IFORK_PTR(ip, whichfork);
5991         block = ifp->if_broot;
5992         /*
5993          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5994          */
5995         level = be16_to_cpu(block->bb_level);
5996         ASSERT(level > 0);
5997         xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5998         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5999         bno = be64_to_cpu(*pp);
6000
6001         ASSERT(bno != NULLDFSBNO);
6002         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6003         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6004
6005         /*
6006          * Go down the tree until leaf level is reached, following the first
6007          * pointer (leftmost) at each level.
6008          */
6009         while (level-- > 0) {
6010                 /* See if buf is in cur first */
6011                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6012                 if (bp) {
6013                         bp_release = 0;
6014                 } else {
6015                         bp_release = 1;
6016                 }
6017                 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6018                                 XFS_BMAP_BTREE_REF)))
6019                         goto error_norelse;
6020                 block = XFS_BUF_TO_BLOCK(bp);
6021                 XFS_WANT_CORRUPTED_GOTO(
6022                         xfs_bmap_sanity_check(mp, bp, level),
6023                         error0);
6024                 if (level == 0)
6025                         break;
6026
6027                 /*
6028                  * Check this block for basic sanity (increasing keys and
6029                  * no duplicate blocks).
6030                  */
6031
6032                 xfs_check_block(block, mp, 0, 0);
6033                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6034                 bno = be64_to_cpu(*pp);
6035                 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6036                 if (bp_release) {
6037                         bp_release = 0;
6038                         xfs_trans_brelse(NULL, bp);
6039                 }
6040         }
6041
6042         /*
6043          * Here with bp and block set to the leftmost leaf node in the tree.
6044          */
6045         i = 0;
6046
6047         /*
6048          * Loop over all leaf nodes checking that all extents are in the right order.
6049          */
6050         for (;;) {
6051                 xfs_fsblock_t   nextbno;
6052                 xfs_extnum_t    num_recs;
6053
6054
6055                 num_recs = xfs_btree_get_numrecs(block);
6056
6057                 /*
6058                  * Read-ahead the next leaf block, if any.
6059                  */
6060
6061                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6062
6063                 /*
6064                  * Check all the extents to make sure they are OK.
6065                  * If we had a previous block, the last entry should
6066                  * conform with the first entry in this one.
6067                  */
6068
6069                 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
6070                 if (i) {
6071                         ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6072                                xfs_bmbt_disk_get_blockcount(&last) <=
6073                                xfs_bmbt_disk_get_startoff(ep));
6074                 }
6075                 for (j = 1; j < num_recs; j++) {
6076                         nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
6077                         ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6078                                xfs_bmbt_disk_get_blockcount(ep) <=
6079                                xfs_bmbt_disk_get_startoff(nextp));
6080                         ep = nextp;
6081                 }
6082
6083                 last = *ep;
6084                 i += num_recs;
6085                 if (bp_release) {
6086                         bp_release = 0;
6087                         xfs_trans_brelse(NULL, bp);
6088                 }
6089                 bno = nextbno;
6090                 /*
6091                  * If we've reached the end, stop.
6092                  */
6093                 if (bno == NULLFSBLOCK)
6094                         break;
6095
6096                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6097                 if (bp) {
6098                         bp_release = 0;
6099                 } else {
6100                         bp_release = 1;
6101                 }
6102                 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6103                                 XFS_BMAP_BTREE_REF)))
6104                         goto error_norelse;
6105                 block = XFS_BUF_TO_BLOCK(bp);
6106         }
6107         if (bp_release) {
6108                 bp_release = 0;
6109                 xfs_trans_brelse(NULL, bp);
6110         }
6111         return;
6112
6113 error0:
6114         cmn_err(CE_WARN, "%s: at error0", __func__);
6115         if (bp_release)
6116                 xfs_trans_brelse(NULL, bp);
6117 error_norelse:
6118         cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6119                 __func__, i);
6120         panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6121         return;
6122 }
6123 #endif
6124
6125 /*
6126  * Count fsblocks of the given fork.
6127  */
6128 int                                             /* error */
6129 xfs_bmap_count_blocks(
6130         xfs_trans_t             *tp,            /* transaction pointer */
6131         xfs_inode_t             *ip,            /* incore inode */
6132         int                     whichfork,      /* data or attr fork */
6133         int                     *count)         /* out: count of blocks */
6134 {
6135         struct xfs_btree_block  *block; /* current btree block */
6136         xfs_fsblock_t           bno;    /* block # of "block" */
6137         xfs_ifork_t             *ifp;   /* fork structure */
6138         int                     level;  /* btree level, for checking */
6139         xfs_mount_t             *mp;    /* file system mount structure */
6140         __be64                  *pp;    /* pointer to block address */
6141
6142         bno = NULLFSBLOCK;
6143         mp = ip->i_mount;
6144         ifp = XFS_IFORK_PTR(ip, whichfork);
6145         if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6146                 xfs_bmap_count_leaves(ifp, 0,
6147                         ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6148                         count);
6149                 return 0;
6150         }
6151
6152         /*
6153          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6154          */
6155         block = ifp->if_broot;
6156         level = be16_to_cpu(block->bb_level);
6157         ASSERT(level > 0);
6158         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6159         bno = be64_to_cpu(*pp);
6160         ASSERT(bno != NULLDFSBNO);
6161         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6162         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6163
6164         if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6165                 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6166                                  mp);
6167                 return XFS_ERROR(EFSCORRUPTED);
6168         }
6169
6170         return 0;
6171 }
6172
6173 /*
6174  * Recursively walks each level of a btree
6175  * to count total fsblocks is use.
6176  */
6177 STATIC int                                     /* error */
6178 xfs_bmap_count_tree(
6179         xfs_mount_t     *mp,            /* file system mount point */
6180         xfs_trans_t     *tp,            /* transaction pointer */
6181         xfs_ifork_t     *ifp,           /* inode fork pointer */
6182         xfs_fsblock_t   blockno,        /* file system block number */
6183         int             levelin,        /* level in btree */
6184         int             *count)         /* Count of blocks */
6185 {
6186         int                     error;
6187         xfs_buf_t               *bp, *nbp;
6188         int                     level = levelin;
6189         __be64                  *pp;
6190         xfs_fsblock_t           bno = blockno;
6191         xfs_fsblock_t           nextbno;
6192         struct xfs_btree_block  *block, *nextblock;
6193         int                     numrecs;
6194
6195         if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6196                 return error;
6197         *count += 1;
6198         block = XFS_BUF_TO_BLOCK(bp);
6199
6200         if (--level) {
6201                 /* Not at node above leaves, count this level of nodes */
6202                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6203                 while (nextbno != NULLFSBLOCK) {
6204                         if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6205                                 0, &nbp, XFS_BMAP_BTREE_REF)))
6206                                 return error;
6207                         *count += 1;
6208                         nextblock = XFS_BUF_TO_BLOCK(nbp);
6209                         nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6210                         xfs_trans_brelse(tp, nbp);
6211                 }
6212
6213                 /* Dive to the next level */
6214                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6215                 bno = be64_to_cpu(*pp);
6216                 if (unlikely((error =
6217                      xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6218                         xfs_trans_brelse(tp, bp);
6219                         XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6220                                          XFS_ERRLEVEL_LOW, mp);
6221                         return XFS_ERROR(EFSCORRUPTED);
6222                 }
6223                 xfs_trans_brelse(tp, bp);
6224         } else {
6225                 /* count all level 1 nodes and their leaves */
6226                 for (;;) {
6227                         nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6228                         numrecs = be16_to_cpu(block->bb_numrecs);
6229                         xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6230                         xfs_trans_brelse(tp, bp);
6231                         if (nextbno == NULLFSBLOCK)
6232                                 break;
6233                         bno = nextbno;
6234                         if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6235                                 XFS_BMAP_BTREE_REF)))
6236                                 return error;
6237                         *count += 1;
6238                         block = XFS_BUF_TO_BLOCK(bp);
6239                 }
6240         }
6241         return 0;
6242 }
6243
6244 /*
6245  * Count leaf blocks given a range of extent records.
6246  */
6247 STATIC void
6248 xfs_bmap_count_leaves(
6249         xfs_ifork_t             *ifp,
6250         xfs_extnum_t            idx,
6251         int                     numrecs,
6252         int                     *count)
6253 {
6254         int             b;
6255
6256         for (b = 0; b < numrecs; b++) {
6257                 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6258                 *count += xfs_bmbt_get_blockcount(frp);
6259         }
6260 }
6261
6262 /*
6263  * Count leaf blocks given a range of extent records originally
6264  * in btree format.
6265  */
6266 STATIC void
6267 xfs_bmap_disk_count_leaves(
6268         struct xfs_mount        *mp,
6269         struct xfs_btree_block  *block,
6270         int                     numrecs,
6271         int                     *count)
6272 {
6273         int             b;
6274         xfs_bmbt_rec_t  *frp;
6275
6276         for (b = 1; b <= numrecs; b++) {
6277                 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6278                 *count += xfs_bmbt_disk_get_blockcount(frp);
6279         }
6280 }