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