2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
35 #include "xfs_mount.h"
36 #include "xfs_itable.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_extfree_item.h"
39 #include "xfs_alloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_attr_leaf.h"
45 #include "xfs_quota.h"
46 #include "xfs_trans_space.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_filestream.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
55 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
58 kmem_zone_t *xfs_bmap_free_item_zone;
61 * Prototypes for internal bmap routines.
66 * Called from xfs_bmap_add_attrfork to handle extents format files.
68 STATIC int /* error */
69 xfs_bmap_add_attrfork_extents(
70 xfs_trans_t *tp, /* transaction pointer */
71 xfs_inode_t *ip, /* incore inode pointer */
72 xfs_fsblock_t *firstblock, /* first block allocated */
73 xfs_bmap_free_t *flist, /* blocks to free at commit */
74 int *flags); /* inode logging flags */
77 * Called from xfs_bmap_add_attrfork to handle local format files.
79 STATIC int /* error */
80 xfs_bmap_add_attrfork_local(
81 xfs_trans_t *tp, /* transaction pointer */
82 xfs_inode_t *ip, /* incore inode pointer */
83 xfs_fsblock_t *firstblock, /* first block allocated */
84 xfs_bmap_free_t *flist, /* blocks to free at commit */
85 int *flags); /* inode logging flags */
88 * Called by xfs_bmap_add_extent to handle cases converting a delayed
89 * allocation to a real allocation.
91 STATIC int /* error */
92 xfs_bmap_add_extent_delay_real(
93 struct xfs_trans *tp, /* transaction pointer */
94 xfs_inode_t *ip, /* incore inode pointer */
95 xfs_extnum_t *idx, /* extent number to update/insert */
96 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
97 xfs_bmbt_irec_t *new, /* new data to add to file extents */
98 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
99 xfs_fsblock_t *first, /* pointer to firstblock variable */
100 xfs_bmap_free_t *flist, /* list of extents to be freed */
101 int *logflagsp); /* inode logging flags */
104 * Called by xfs_bmap_add_extent to handle cases converting a hole
105 * to a real allocation.
107 STATIC int /* error */
108 xfs_bmap_add_extent_hole_real(
109 xfs_inode_t *ip, /* incore inode pointer */
110 xfs_extnum_t *idx, /* extent number to update/insert */
111 xfs_btree_cur_t *cur, /* if null, not a btree */
112 xfs_bmbt_irec_t *new, /* new data to add to file extents */
113 int *logflagsp, /* inode logging flags */
114 int whichfork); /* data or attr fork */
117 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
118 * allocation to a real allocation or vice versa.
120 STATIC int /* error */
121 xfs_bmap_add_extent_unwritten_real(
122 xfs_inode_t *ip, /* incore inode pointer */
123 xfs_extnum_t *idx, /* extent number to update/insert */
124 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
125 xfs_bmbt_irec_t *new, /* new data to add to file extents */
126 int *logflagsp); /* inode logging flags */
129 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
130 * It figures out where to ask the underlying allocator to put the new extent.
132 STATIC int /* error */
134 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
137 * Transform a btree format file with only one leaf node, where the
138 * extents list will fit in the inode, into an extents format file.
139 * Since the file extents are already in-core, all we have to do is
140 * give up the space for the btree root and pitch the leaf block.
142 STATIC int /* error */
143 xfs_bmap_btree_to_extents(
144 xfs_trans_t *tp, /* transaction pointer */
145 xfs_inode_t *ip, /* incore inode pointer */
146 xfs_btree_cur_t *cur, /* btree cursor */
147 int *logflagsp, /* inode logging flags */
148 int whichfork); /* data or attr fork */
151 * Remove the entry "free" from the free item list. Prev points to the
152 * previous entry, unless "free" is the head of the list.
156 xfs_bmap_free_t *flist, /* free item list header */
157 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
158 xfs_bmap_free_item_t *free); /* list item to be freed */
161 * Convert an extents-format file into a btree-format file.
162 * The new file will have a root block (in the inode) and a single child block.
164 STATIC int /* error */
165 xfs_bmap_extents_to_btree(
166 xfs_trans_t *tp, /* transaction pointer */
167 xfs_inode_t *ip, /* incore inode pointer */
168 xfs_fsblock_t *firstblock, /* first-block-allocated */
169 xfs_bmap_free_t *flist, /* blocks freed in xaction */
170 xfs_btree_cur_t **curp, /* cursor returned to caller */
171 int wasdel, /* converting a delayed alloc */
172 int *logflagsp, /* inode logging flags */
173 int whichfork); /* data or attr fork */
176 * Convert a local file to an extents file.
177 * This code is sort of bogus, since the file data needs to get
178 * logged so it won't be lost. The bmap-level manipulations are ok, though.
180 STATIC int /* error */
181 xfs_bmap_local_to_extents(
182 xfs_trans_t *tp, /* transaction pointer */
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
185 xfs_extlen_t total, /* total blocks needed by transaction */
186 int *logflagsp, /* inode logging flags */
187 int whichfork); /* data or attr fork */
190 * Search the extents list for the inode, for the extent containing bno.
191 * If bno lies in a hole, point to the next entry. If bno lies past eof,
192 * *eofp will be set, and *prevp will contain the last entry (null if none).
193 * Else, *lastxp will be set to the index of the found
194 * entry; *gotp will contain the entry.
196 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
197 xfs_bmap_search_extents(
198 xfs_inode_t *ip, /* incore inode pointer */
199 xfs_fileoff_t bno, /* block number searched for */
200 int whichfork, /* data or attr fork */
201 int *eofp, /* out: end of file found */
202 xfs_extnum_t *lastxp, /* out: last extent index */
203 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
204 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
207 * Compute the worst-case number of indirect blocks that will be used
208 * for ip's delayed extent of length "len".
211 xfs_bmap_worst_indlen(
212 xfs_inode_t *ip, /* incore inode pointer */
213 xfs_filblks_t len); /* delayed extent length */
217 * Perform various validation checks on the values being returned
221 xfs_bmap_validate_ret(
225 xfs_bmbt_irec_t *mval,
229 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
237 xfs_fsblock_t blockno,
242 xfs_bmap_count_leaves(
249 xfs_bmap_disk_count_leaves(
250 struct xfs_mount *mp,
251 struct xfs_btree_block *block,
256 * Bmap internal routines.
259 STATIC int /* error */
261 struct xfs_btree_cur *cur,
265 int *stat) /* success/failure */
267 cur->bc_rec.b.br_startoff = off;
268 cur->bc_rec.b.br_startblock = bno;
269 cur->bc_rec.b.br_blockcount = len;
270 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
273 STATIC int /* error */
275 struct xfs_btree_cur *cur,
279 int *stat) /* success/failure */
281 cur->bc_rec.b.br_startoff = off;
282 cur->bc_rec.b.br_startblock = bno;
283 cur->bc_rec.b.br_blockcount = len;
284 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
288 * Update the record referred to by cur to the value given
289 * by [off, bno, len, state].
290 * This either works (return 0) or gets an EFSCORRUPTED error.
294 struct xfs_btree_cur *cur,
300 union xfs_btree_rec rec;
302 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
303 return xfs_btree_update(cur, &rec);
307 * Called from xfs_bmap_add_attrfork to handle btree format files.
309 STATIC int /* error */
310 xfs_bmap_add_attrfork_btree(
311 xfs_trans_t *tp, /* transaction pointer */
312 xfs_inode_t *ip, /* incore inode pointer */
313 xfs_fsblock_t *firstblock, /* first block allocated */
314 xfs_bmap_free_t *flist, /* blocks to free at commit */
315 int *flags) /* inode logging flags */
317 xfs_btree_cur_t *cur; /* btree cursor */
318 int error; /* error return value */
319 xfs_mount_t *mp; /* file system mount struct */
320 int stat; /* newroot status */
323 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
324 *flags |= XFS_ILOG_DBROOT;
326 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
327 cur->bc_private.b.flist = flist;
328 cur->bc_private.b.firstblock = *firstblock;
329 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
331 /* must be at least one entry */
332 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
333 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
336 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
337 return XFS_ERROR(ENOSPC);
339 *firstblock = cur->bc_private.b.firstblock;
340 cur->bc_private.b.allocated = 0;
341 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
345 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
350 * Called from xfs_bmap_add_attrfork to handle extents format files.
352 STATIC int /* error */
353 xfs_bmap_add_attrfork_extents(
354 xfs_trans_t *tp, /* transaction pointer */
355 xfs_inode_t *ip, /* incore inode pointer */
356 xfs_fsblock_t *firstblock, /* first block allocated */
357 xfs_bmap_free_t *flist, /* blocks to free at commit */
358 int *flags) /* inode logging flags */
360 xfs_btree_cur_t *cur; /* bmap btree cursor */
361 int error; /* error return value */
363 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
366 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
367 flags, XFS_DATA_FORK);
369 cur->bc_private.b.allocated = 0;
370 xfs_btree_del_cursor(cur,
371 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
377 * Called from xfs_bmap_add_attrfork to handle local format files.
379 STATIC int /* error */
380 xfs_bmap_add_attrfork_local(
381 xfs_trans_t *tp, /* transaction pointer */
382 xfs_inode_t *ip, /* incore inode pointer */
383 xfs_fsblock_t *firstblock, /* first block allocated */
384 xfs_bmap_free_t *flist, /* blocks to free at commit */
385 int *flags) /* inode logging flags */
387 xfs_da_args_t dargs; /* args for dir/attr code */
388 int error; /* error return value */
389 xfs_mount_t *mp; /* mount structure pointer */
391 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
393 if (S_ISDIR(ip->i_d.di_mode)) {
395 memset(&dargs, 0, sizeof(dargs));
397 dargs.firstblock = firstblock;
399 dargs.total = mp->m_dirblkfsbs;
400 dargs.whichfork = XFS_DATA_FORK;
402 error = xfs_dir2_sf_to_block(&dargs);
404 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
410 * Update file extent records and the btree after allocating space.
412 STATIC int /* error */
414 struct xfs_trans *tp, /* transaction pointer */
415 xfs_inode_t *ip, /* incore inode pointer */
416 xfs_extnum_t *idx, /* extent number to update/insert */
417 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
418 xfs_bmbt_irec_t *new, /* new data to add to file extents */
419 xfs_fsblock_t *first, /* pointer to firstblock variable */
420 xfs_bmap_free_t *flist, /* list of extents to be freed */
421 int *logflagsp, /* inode logging flags */
422 int whichfork) /* data or attr fork */
424 xfs_btree_cur_t *cur; /* btree cursor or null */
425 xfs_filblks_t da_new; /* new count del alloc blocks used */
426 xfs_filblks_t da_old; /* old count del alloc blocks used */
427 int error; /* error return value */
428 xfs_ifork_t *ifp; /* inode fork ptr */
429 int logflags; /* returned value */
430 xfs_extnum_t nextents; /* number of extents in file now */
432 XFS_STATS_INC(xs_add_exlist);
435 ifp = XFS_IFORK_PTR(ip, whichfork);
436 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
441 ASSERT(*idx <= nextents);
442 ASSERT(!isnullstartblock(new->br_startblock));
445 * Real allocation off the end of the file.
447 if (*idx == nextents) {
449 ASSERT((cur->bc_private.b.flags &
450 XFS_BTCUR_BPRV_WASDEL) == 0);
451 error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
452 &logflags, whichfork);
454 xfs_bmbt_irec_t prev; /* old extent at offset idx */
457 * Get the record referred to by idx.
459 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev);
461 * If it's a real allocation record, and the new allocation ends
462 * after the start of the referred to record, then we're filling
463 * in a delayed or unwritten allocation with a real one, or
464 * converting real back to unwritten.
466 if (!isnullstartblock(new->br_startblock) &&
467 new->br_startoff + new->br_blockcount > prev.br_startoff) {
468 if (prev.br_state != XFS_EXT_UNWRITTEN &&
469 isnullstartblock(prev.br_startblock)) {
470 da_old = startblockval(prev.br_startblock);
472 ASSERT(cur->bc_private.b.flags &
473 XFS_BTCUR_BPRV_WASDEL);
474 error = xfs_bmap_add_extent_delay_real(tp, ip,
475 idx, &cur, new, &da_new,
476 first, flist, &logflags);
478 ASSERT(new->br_state == XFS_EXT_NORM ||
479 new->br_state == XFS_EXT_UNWRITTEN);
481 error = xfs_bmap_add_extent_unwritten_real(ip,
482 idx, &cur, new, &logflags);
488 * Otherwise we're filling in a hole with an allocation.
492 ASSERT((cur->bc_private.b.flags &
493 XFS_BTCUR_BPRV_WASDEL) == 0);
494 error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
495 new, &logflags, whichfork);
501 ASSERT(*curp == cur || *curp == NULL);
504 * Convert to a btree if necessary.
506 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
507 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
508 int tmp_logflags; /* partial log flag return val */
511 error = xfs_bmap_extents_to_btree(tp, ip, first,
512 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
513 logflags |= tmp_logflags;
518 * Adjust for changes in reserved delayed indirect blocks.
519 * Nothing to do for disk quotas here.
521 if (da_old || da_new) {
526 nblks += cur->bc_private.b.allocated;
527 ASSERT(nblks <= da_old);
529 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
530 (int64_t)(da_old - nblks), 0);
533 * Clear out the allocated field, done with it now in any case.
536 cur->bc_private.b.allocated = 0;
542 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
544 *logflagsp = logflags;
549 * Called by xfs_bmap_add_extent to handle cases converting a delayed
550 * allocation to a real allocation.
552 STATIC int /* error */
553 xfs_bmap_add_extent_delay_real(
554 struct xfs_trans *tp, /* transaction pointer */
555 xfs_inode_t *ip, /* incore inode pointer */
556 xfs_extnum_t *idx, /* extent number to update/insert */
557 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
558 xfs_bmbt_irec_t *new, /* new data to add to file extents */
559 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
560 xfs_fsblock_t *first, /* pointer to firstblock variable */
561 xfs_bmap_free_t *flist, /* list of extents to be freed */
562 int *logflagsp) /* inode logging flags */
564 xfs_btree_cur_t *cur; /* btree cursor */
565 int diff; /* temp value */
566 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
567 int error; /* error return value */
568 int i; /* temp state */
569 xfs_ifork_t *ifp; /* inode fork pointer */
570 xfs_fileoff_t new_endoff; /* end offset of new entry */
571 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
572 /* left is 0, right is 1, prev is 2 */
573 int rval=0; /* return value (logging flags) */
574 int state = 0;/* state bits, accessed thru macros */
575 xfs_filblks_t temp=0; /* value for dnew calculations */
576 xfs_filblks_t temp2=0;/* value for dnew calculations */
577 int tmp_rval; /* partial logging flags */
584 * Set up a bunch of variables to make the tests simpler.
587 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
588 ep = xfs_iext_get_ext(ifp, *idx);
589 xfs_bmbt_get_all(ep, &PREV);
590 new_endoff = new->br_startoff + new->br_blockcount;
591 ASSERT(PREV.br_startoff <= new->br_startoff);
592 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
595 * Set flags determining what part of the previous delayed allocation
596 * extent is being replaced by a real allocation.
598 if (PREV.br_startoff == new->br_startoff)
599 state |= BMAP_LEFT_FILLING;
600 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
601 state |= BMAP_RIGHT_FILLING;
604 * Check and set flags if this segment has a left neighbor.
605 * Don't set contiguous if the combined extent would be too large.
608 state |= BMAP_LEFT_VALID;
609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
611 if (isnullstartblock(LEFT.br_startblock))
612 state |= BMAP_LEFT_DELAY;
615 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
616 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
617 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
618 LEFT.br_state == new->br_state &&
619 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
620 state |= BMAP_LEFT_CONTIG;
623 * Check and set flags if this segment has a right neighbor.
624 * Don't set contiguous if the combined extent would be too large.
625 * Also check for all-three-contiguous being too large.
627 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
628 state |= BMAP_RIGHT_VALID;
629 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
631 if (isnullstartblock(RIGHT.br_startblock))
632 state |= BMAP_RIGHT_DELAY;
635 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
636 new_endoff == RIGHT.br_startoff &&
637 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
638 new->br_state == RIGHT.br_state &&
639 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
640 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
641 BMAP_RIGHT_FILLING)) !=
642 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
643 BMAP_RIGHT_FILLING) ||
644 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
646 state |= BMAP_RIGHT_CONTIG;
650 * Switch out based on the FILLING and CONTIG state bits.
652 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
653 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
654 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
655 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
657 * Filling in all of a previously delayed allocation extent.
658 * The left and right neighbors are both contiguous with new.
661 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
662 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
663 LEFT.br_blockcount + PREV.br_blockcount +
664 RIGHT.br_blockcount);
665 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
667 xfs_iext_remove(ip, *idx + 1, 2, state);
668 ip->i_d.di_nextents--;
670 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
672 rval = XFS_ILOG_CORE;
673 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
675 RIGHT.br_blockcount, &i)))
677 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
678 if ((error = xfs_btree_delete(cur, &i)))
680 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
681 if ((error = xfs_btree_decrement(cur, 0, &i)))
683 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
684 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
688 RIGHT.br_blockcount, LEFT.br_state)))
694 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
696 * Filling in all of a previously delayed allocation extent.
697 * The left neighbor is contiguous, the right is not.
701 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
702 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
703 LEFT.br_blockcount + PREV.br_blockcount);
704 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
706 xfs_iext_remove(ip, *idx + 1, 1, state);
708 rval = XFS_ILOG_DEXT;
711 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
712 LEFT.br_startblock, LEFT.br_blockcount,
715 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
716 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
719 PREV.br_blockcount, LEFT.br_state)))
725 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
727 * Filling in all of a previously delayed allocation extent.
728 * The right neighbor is contiguous, the left is not.
730 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
731 xfs_bmbt_set_startblock(ep, new->br_startblock);
732 xfs_bmbt_set_blockcount(ep,
733 PREV.br_blockcount + RIGHT.br_blockcount);
734 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
736 xfs_iext_remove(ip, *idx + 1, 1, state);
738 rval = XFS_ILOG_DEXT;
741 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
743 RIGHT.br_blockcount, &i)))
745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
746 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
749 RIGHT.br_blockcount, PREV.br_state)))
756 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
758 * Filling in all of a previously delayed allocation extent.
759 * Neither the left nor right neighbors are contiguous with
762 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
763 xfs_bmbt_set_startblock(ep, new->br_startblock);
764 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
766 ip->i_d.di_nextents++;
768 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
770 rval = XFS_ILOG_CORE;
771 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
772 new->br_startblock, new->br_blockcount,
775 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
776 cur->bc_rec.b.br_state = XFS_EXT_NORM;
777 if ((error = xfs_btree_insert(cur, &i)))
779 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
785 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
787 * Filling in the first part of a previous delayed allocation.
788 * The left neighbor is contiguous.
790 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
791 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
792 LEFT.br_blockcount + new->br_blockcount);
793 xfs_bmbt_set_startoff(ep,
794 PREV.br_startoff + new->br_blockcount);
795 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
797 temp = PREV.br_blockcount - new->br_blockcount;
798 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
799 xfs_bmbt_set_blockcount(ep, temp);
801 rval = XFS_ILOG_DEXT;
804 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
805 LEFT.br_startblock, LEFT.br_blockcount,
808 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
809 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
816 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
817 startblockval(PREV.br_startblock));
818 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
819 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
825 case BMAP_LEFT_FILLING:
827 * Filling in the first part of a previous delayed allocation.
828 * The left neighbor is not contiguous.
830 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
831 xfs_bmbt_set_startoff(ep, new_endoff);
832 temp = PREV.br_blockcount - new->br_blockcount;
833 xfs_bmbt_set_blockcount(ep, temp);
834 xfs_iext_insert(ip, *idx, 1, new, state);
835 ip->i_d.di_nextents++;
837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
839 rval = XFS_ILOG_CORE;
840 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
841 new->br_startblock, new->br_blockcount,
844 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
845 cur->bc_rec.b.br_state = XFS_EXT_NORM;
846 if ((error = xfs_btree_insert(cur, &i)))
848 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
850 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
851 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
852 error = xfs_bmap_extents_to_btree(tp, ip,
853 first, flist, &cur, 1, &tmp_rval,
859 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
860 startblockval(PREV.br_startblock) -
861 (cur ? cur->bc_private.b.allocated : 0));
862 ep = xfs_iext_get_ext(ifp, *idx + 1);
863 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
864 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
869 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
871 * Filling in the last part of a previous delayed allocation.
872 * The right neighbor is contiguous with the new allocation.
874 temp = PREV.br_blockcount - new->br_blockcount;
875 trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_);
876 xfs_bmbt_set_blockcount(ep, temp);
877 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1),
878 new->br_startoff, new->br_startblock,
879 new->br_blockcount + RIGHT.br_blockcount,
881 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
883 rval = XFS_ILOG_DEXT;
886 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
888 RIGHT.br_blockcount, &i)))
890 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
891 if ((error = xfs_bmbt_update(cur, new->br_startoff,
899 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
900 startblockval(PREV.br_startblock));
901 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
902 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
903 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
909 case BMAP_RIGHT_FILLING:
911 * Filling in the last part of a previous delayed allocation.
912 * The right neighbor is not contiguous.
914 temp = PREV.br_blockcount - new->br_blockcount;
915 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
916 xfs_bmbt_set_blockcount(ep, temp);
917 xfs_iext_insert(ip, *idx + 1, 1, new, state);
918 ip->i_d.di_nextents++;
920 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
922 rval = XFS_ILOG_CORE;
923 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
924 new->br_startblock, new->br_blockcount,
927 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
928 cur->bc_rec.b.br_state = XFS_EXT_NORM;
929 if ((error = xfs_btree_insert(cur, &i)))
931 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
933 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
934 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
935 error = xfs_bmap_extents_to_btree(tp, ip,
936 first, flist, &cur, 1, &tmp_rval,
942 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
943 startblockval(PREV.br_startblock) -
944 (cur ? cur->bc_private.b.allocated : 0));
945 ep = xfs_iext_get_ext(ifp, *idx);
946 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
947 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
955 * Filling in the middle part of a previous delayed allocation.
956 * Contiguity is impossible here.
957 * This case is avoided almost all the time.
959 * We start with a delayed allocation:
961 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
964 * and we are allocating:
965 * +rrrrrrrrrrrrrrrrr+
968 * and we set it up for insertion as:
969 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
971 * PREV @ idx LEFT RIGHT
972 * inserted at idx + 1
974 temp = new->br_startoff - PREV.br_startoff;
975 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
976 trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_);
977 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
979 RIGHT.br_state = PREV.br_state;
980 RIGHT.br_startblock = nullstartblock(
981 (int)xfs_bmap_worst_indlen(ip, temp2));
982 RIGHT.br_startoff = new_endoff;
983 RIGHT.br_blockcount = temp2;
984 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
985 xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state);
986 ip->i_d.di_nextents++;
988 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
990 rval = XFS_ILOG_CORE;
991 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
992 new->br_startblock, new->br_blockcount,
995 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
996 cur->bc_rec.b.br_state = XFS_EXT_NORM;
997 if ((error = xfs_btree_insert(cur, &i)))
999 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1001 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1002 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1003 error = xfs_bmap_extents_to_btree(tp, ip,
1004 first, flist, &cur, 1, &tmp_rval,
1010 temp = xfs_bmap_worst_indlen(ip, temp);
1011 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1012 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1013 (cur ? cur->bc_private.b.allocated : 0));
1015 error = xfs_icsb_modify_counters(ip->i_mount,
1017 -((int64_t)diff), 0);
1023 ep = xfs_iext_get_ext(ifp, *idx);
1024 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1025 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1026 trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_);
1027 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2),
1028 nullstartblock((int)temp2));
1029 trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_);
1032 *dnew = temp + temp2;
1035 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1036 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1037 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1038 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1039 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1040 case BMAP_LEFT_CONTIG:
1041 case BMAP_RIGHT_CONTIG:
1043 * These cases are all impossible.
1057 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1058 * allocation to a real allocation or vice versa.
1060 STATIC int /* error */
1061 xfs_bmap_add_extent_unwritten_real(
1062 xfs_inode_t *ip, /* incore inode pointer */
1063 xfs_extnum_t *idx, /* extent number to update/insert */
1064 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1065 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1066 int *logflagsp) /* inode logging flags */
1068 xfs_btree_cur_t *cur; /* btree cursor */
1069 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1070 int error; /* error return value */
1071 int i; /* temp state */
1072 xfs_ifork_t *ifp; /* inode fork pointer */
1073 xfs_fileoff_t new_endoff; /* end offset of new entry */
1074 xfs_exntst_t newext; /* new extent state */
1075 xfs_exntst_t oldext; /* old extent state */
1076 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1077 /* left is 0, right is 1, prev is 2 */
1078 int rval=0; /* return value (logging flags) */
1079 int state = 0;/* state bits, accessed thru macros */
1085 * Set up a bunch of variables to make the tests simpler.
1089 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1090 ep = xfs_iext_get_ext(ifp, *idx);
1091 xfs_bmbt_get_all(ep, &PREV);
1092 newext = new->br_state;
1093 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1094 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1095 ASSERT(PREV.br_state == oldext);
1096 new_endoff = new->br_startoff + new->br_blockcount;
1097 ASSERT(PREV.br_startoff <= new->br_startoff);
1098 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1101 * Set flags determining what part of the previous oldext allocation
1102 * extent is being replaced by a newext allocation.
1104 if (PREV.br_startoff == new->br_startoff)
1105 state |= BMAP_LEFT_FILLING;
1106 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1107 state |= BMAP_RIGHT_FILLING;
1110 * Check and set flags if this segment has a left neighbor.
1111 * Don't set contiguous if the combined extent would be too large.
1114 state |= BMAP_LEFT_VALID;
1115 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
1117 if (isnullstartblock(LEFT.br_startblock))
1118 state |= BMAP_LEFT_DELAY;
1121 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1122 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1123 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1124 LEFT.br_state == newext &&
1125 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1126 state |= BMAP_LEFT_CONTIG;
1129 * Check and set flags if this segment has a right neighbor.
1130 * Don't set contiguous if the combined extent would be too large.
1131 * Also check for all-three-contiguous being too large.
1133 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1134 state |= BMAP_RIGHT_VALID;
1135 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
1136 if (isnullstartblock(RIGHT.br_startblock))
1137 state |= BMAP_RIGHT_DELAY;
1140 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1141 new_endoff == RIGHT.br_startoff &&
1142 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1143 newext == RIGHT.br_state &&
1144 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1145 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1146 BMAP_RIGHT_FILLING)) !=
1147 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1148 BMAP_RIGHT_FILLING) ||
1149 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1151 state |= BMAP_RIGHT_CONTIG;
1154 * Switch out based on the FILLING and CONTIG state bits.
1156 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1157 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1158 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1159 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1161 * Setting all of a previous oldext extent to newext.
1162 * The left and right neighbors are both contiguous with new.
1166 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1167 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1168 LEFT.br_blockcount + PREV.br_blockcount +
1169 RIGHT.br_blockcount);
1170 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1172 xfs_iext_remove(ip, *idx + 1, 2, state);
1173 ip->i_d.di_nextents -= 2;
1175 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1177 rval = XFS_ILOG_CORE;
1178 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1179 RIGHT.br_startblock,
1180 RIGHT.br_blockcount, &i)))
1182 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1183 if ((error = xfs_btree_delete(cur, &i)))
1185 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1186 if ((error = xfs_btree_decrement(cur, 0, &i)))
1188 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1189 if ((error = xfs_btree_delete(cur, &i)))
1191 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1192 if ((error = xfs_btree_decrement(cur, 0, &i)))
1194 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1195 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1197 LEFT.br_blockcount + PREV.br_blockcount +
1198 RIGHT.br_blockcount, LEFT.br_state)))
1203 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1205 * Setting all of a previous oldext extent to newext.
1206 * The left neighbor is contiguous, the right is not.
1210 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1211 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1212 LEFT.br_blockcount + PREV.br_blockcount);
1213 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1215 xfs_iext_remove(ip, *idx + 1, 1, state);
1216 ip->i_d.di_nextents--;
1218 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1220 rval = XFS_ILOG_CORE;
1221 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1222 PREV.br_startblock, PREV.br_blockcount,
1225 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1226 if ((error = xfs_btree_delete(cur, &i)))
1228 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1229 if ((error = xfs_btree_decrement(cur, 0, &i)))
1231 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1232 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1234 LEFT.br_blockcount + PREV.br_blockcount,
1240 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1242 * Setting all of a previous oldext extent to newext.
1243 * The right neighbor is contiguous, the left is not.
1245 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1246 xfs_bmbt_set_blockcount(ep,
1247 PREV.br_blockcount + RIGHT.br_blockcount);
1248 xfs_bmbt_set_state(ep, newext);
1249 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1250 xfs_iext_remove(ip, *idx + 1, 1, state);
1251 ip->i_d.di_nextents--;
1253 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1255 rval = XFS_ILOG_CORE;
1256 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1257 RIGHT.br_startblock,
1258 RIGHT.br_blockcount, &i)))
1260 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1261 if ((error = xfs_btree_delete(cur, &i)))
1263 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1264 if ((error = xfs_btree_decrement(cur, 0, &i)))
1266 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1267 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1269 new->br_blockcount + RIGHT.br_blockcount,
1275 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1277 * Setting all of a previous oldext extent to newext.
1278 * Neither the left nor right neighbors are contiguous with
1281 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1282 xfs_bmbt_set_state(ep, newext);
1283 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1286 rval = XFS_ILOG_DEXT;
1289 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1290 new->br_startblock, new->br_blockcount,
1293 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1294 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1295 new->br_startblock, new->br_blockcount,
1301 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1303 * Setting the first part of a previous oldext extent to newext.
1304 * The left neighbor is contiguous.
1306 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1307 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
1308 LEFT.br_blockcount + new->br_blockcount);
1309 xfs_bmbt_set_startoff(ep,
1310 PREV.br_startoff + new->br_blockcount);
1311 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
1313 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1314 xfs_bmbt_set_startblock(ep,
1315 new->br_startblock + new->br_blockcount);
1316 xfs_bmbt_set_blockcount(ep,
1317 PREV.br_blockcount - new->br_blockcount);
1318 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1323 rval = XFS_ILOG_DEXT;
1326 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1327 PREV.br_startblock, PREV.br_blockcount,
1330 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1331 if ((error = xfs_bmbt_update(cur,
1332 PREV.br_startoff + new->br_blockcount,
1333 PREV.br_startblock + new->br_blockcount,
1334 PREV.br_blockcount - new->br_blockcount,
1337 if ((error = xfs_btree_decrement(cur, 0, &i)))
1339 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1341 LEFT.br_blockcount + new->br_blockcount,
1347 case BMAP_LEFT_FILLING:
1349 * Setting the first part of a previous oldext extent to newext.
1350 * The left neighbor is not contiguous.
1352 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1353 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1354 xfs_bmbt_set_startoff(ep, new_endoff);
1355 xfs_bmbt_set_blockcount(ep,
1356 PREV.br_blockcount - new->br_blockcount);
1357 xfs_bmbt_set_startblock(ep,
1358 new->br_startblock + new->br_blockcount);
1359 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1361 xfs_iext_insert(ip, *idx, 1, new, state);
1362 ip->i_d.di_nextents++;
1364 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1366 rval = XFS_ILOG_CORE;
1367 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1368 PREV.br_startblock, PREV.br_blockcount,
1371 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1372 if ((error = xfs_bmbt_update(cur,
1373 PREV.br_startoff + new->br_blockcount,
1374 PREV.br_startblock + new->br_blockcount,
1375 PREV.br_blockcount - new->br_blockcount,
1378 cur->bc_rec.b = *new;
1379 if ((error = xfs_btree_insert(cur, &i)))
1381 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1385 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1387 * Setting the last part of a previous oldext extent to newext.
1388 * The right neighbor is contiguous with the new allocation.
1390 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1391 xfs_bmbt_set_blockcount(ep,
1392 PREV.br_blockcount - new->br_blockcount);
1393 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1397 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1398 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1399 new->br_startoff, new->br_startblock,
1400 new->br_blockcount + RIGHT.br_blockcount, newext);
1401 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1404 rval = XFS_ILOG_DEXT;
1407 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1409 PREV.br_blockcount, &i)))
1411 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1412 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1414 PREV.br_blockcount - new->br_blockcount,
1417 if ((error = xfs_btree_increment(cur, 0, &i)))
1419 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1421 new->br_blockcount + RIGHT.br_blockcount,
1427 case BMAP_RIGHT_FILLING:
1429 * Setting the last part of a previous oldext extent to newext.
1430 * The right neighbor is not contiguous.
1432 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1433 xfs_bmbt_set_blockcount(ep,
1434 PREV.br_blockcount - new->br_blockcount);
1435 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1438 xfs_iext_insert(ip, *idx, 1, new, state);
1440 ip->i_d.di_nextents++;
1442 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1444 rval = XFS_ILOG_CORE;
1445 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1446 PREV.br_startblock, PREV.br_blockcount,
1449 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1450 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1452 PREV.br_blockcount - new->br_blockcount,
1455 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1456 new->br_startblock, new->br_blockcount,
1459 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1460 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1461 if ((error = xfs_btree_insert(cur, &i)))
1463 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1469 * Setting the middle part of a previous oldext extent to
1470 * newext. Contiguity is impossible here.
1471 * One extent becomes three extents.
1473 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1474 xfs_bmbt_set_blockcount(ep,
1475 new->br_startoff - PREV.br_startoff);
1476 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1479 r[1].br_startoff = new_endoff;
1480 r[1].br_blockcount =
1481 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1482 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1483 r[1].br_state = oldext;
1486 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1488 ip->i_d.di_nextents += 2;
1490 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1492 rval = XFS_ILOG_CORE;
1493 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1494 PREV.br_startblock, PREV.br_blockcount,
1497 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1498 /* new right extent - oldext */
1499 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1500 r[1].br_startblock, r[1].br_blockcount,
1503 /* new left extent - oldext */
1504 cur->bc_rec.b = PREV;
1505 cur->bc_rec.b.br_blockcount =
1506 new->br_startoff - PREV.br_startoff;
1507 if ((error = xfs_btree_insert(cur, &i)))
1509 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1511 * Reset the cursor to the position of the new extent
1512 * we are about to insert as we can't trust it after
1513 * the previous insert.
1515 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1516 new->br_startblock, new->br_blockcount,
1519 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1520 /* new middle extent - newext */
1521 cur->bc_rec.b.br_state = new->br_state;
1522 if ((error = xfs_btree_insert(cur, &i)))
1524 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1528 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1529 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1530 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1531 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1532 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1533 case BMAP_LEFT_CONTIG:
1534 case BMAP_RIGHT_CONTIG:
1536 * These cases are all impossible.
1550 * Convert a hole to a delayed allocation.
1553 xfs_bmap_add_extent_hole_delay(
1554 xfs_inode_t *ip, /* incore inode pointer */
1555 xfs_extnum_t *idx, /* extent number to update/insert */
1556 xfs_bmbt_irec_t *new) /* new data to add to file extents */
1558 xfs_ifork_t *ifp; /* inode fork pointer */
1559 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1560 xfs_filblks_t newlen=0; /* new indirect size */
1561 xfs_filblks_t oldlen=0; /* old indirect size */
1562 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1563 int state; /* state bits, accessed thru macros */
1564 xfs_filblks_t temp=0; /* temp for indirect calculations */
1566 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1568 ASSERT(isnullstartblock(new->br_startblock));
1571 * Check and set flags if this segment has a left neighbor
1574 state |= BMAP_LEFT_VALID;
1575 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1577 if (isnullstartblock(left.br_startblock))
1578 state |= BMAP_LEFT_DELAY;
1582 * Check and set flags if the current (right) segment exists.
1583 * If it doesn't exist, we're converting the hole at end-of-file.
1585 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1586 state |= BMAP_RIGHT_VALID;
1587 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1589 if (isnullstartblock(right.br_startblock))
1590 state |= BMAP_RIGHT_DELAY;
1594 * Set contiguity flags on the left and right neighbors.
1595 * Don't let extents get too large, even if the pieces are contiguous.
1597 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1598 left.br_startoff + left.br_blockcount == new->br_startoff &&
1599 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1600 state |= BMAP_LEFT_CONTIG;
1602 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1603 new->br_startoff + new->br_blockcount == right.br_startoff &&
1604 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1605 (!(state & BMAP_LEFT_CONTIG) ||
1606 (left.br_blockcount + new->br_blockcount +
1607 right.br_blockcount <= MAXEXTLEN)))
1608 state |= BMAP_RIGHT_CONTIG;
1611 * Switch out based on the contiguity flags.
1613 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1614 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1616 * New allocation is contiguous with delayed allocations
1617 * on the left and on the right.
1618 * Merge all three into a single extent record.
1621 temp = left.br_blockcount + new->br_blockcount +
1622 right.br_blockcount;
1624 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1625 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1626 oldlen = startblockval(left.br_startblock) +
1627 startblockval(new->br_startblock) +
1628 startblockval(right.br_startblock);
1629 newlen = xfs_bmap_worst_indlen(ip, temp);
1630 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1631 nullstartblock((int)newlen));
1632 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1634 xfs_iext_remove(ip, *idx + 1, 1, state);
1637 case BMAP_LEFT_CONTIG:
1639 * New allocation is contiguous with a delayed allocation
1641 * Merge the new allocation with the left neighbor.
1644 temp = left.br_blockcount + new->br_blockcount;
1646 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1647 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1648 oldlen = startblockval(left.br_startblock) +
1649 startblockval(new->br_startblock);
1650 newlen = xfs_bmap_worst_indlen(ip, temp);
1651 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1652 nullstartblock((int)newlen));
1653 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1656 case BMAP_RIGHT_CONTIG:
1658 * New allocation is contiguous with a delayed allocation
1660 * Merge the new allocation with the right neighbor.
1662 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1663 temp = new->br_blockcount + right.br_blockcount;
1664 oldlen = startblockval(new->br_startblock) +
1665 startblockval(right.br_startblock);
1666 newlen = xfs_bmap_worst_indlen(ip, temp);
1667 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1669 nullstartblock((int)newlen), temp, right.br_state);
1670 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1675 * New allocation is not contiguous with another
1676 * delayed allocation.
1677 * Insert a new entry.
1679 oldlen = newlen = 0;
1680 xfs_iext_insert(ip, *idx, 1, new, state);
1683 if (oldlen != newlen) {
1684 ASSERT(oldlen > newlen);
1685 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1686 (int64_t)(oldlen - newlen), 0);
1688 * Nothing to do for disk quota accounting here.
1694 * Called by xfs_bmap_add_extent to handle cases converting a hole
1695 * to a real allocation.
1697 STATIC int /* error */
1698 xfs_bmap_add_extent_hole_real(
1699 xfs_inode_t *ip, /* incore inode pointer */
1700 xfs_extnum_t *idx, /* extent number to update/insert */
1701 xfs_btree_cur_t *cur, /* if null, not a btree */
1702 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1703 int *logflagsp, /* inode logging flags */
1704 int whichfork) /* data or attr fork */
1706 int error; /* error return value */
1707 int i; /* temp state */
1708 xfs_ifork_t *ifp; /* inode fork pointer */
1709 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1710 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1711 int rval=0; /* return value (logging flags) */
1712 int state; /* state bits, accessed thru macros */
1714 ifp = XFS_IFORK_PTR(ip, whichfork);
1715 ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1718 if (whichfork == XFS_ATTR_FORK)
1719 state |= BMAP_ATTRFORK;
1722 * Check and set flags if this segment has a left neighbor.
1725 state |= BMAP_LEFT_VALID;
1726 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1727 if (isnullstartblock(left.br_startblock))
1728 state |= BMAP_LEFT_DELAY;
1732 * Check and set flags if this segment has a current value.
1733 * Not true if we're inserting into the "hole" at eof.
1735 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1736 state |= BMAP_RIGHT_VALID;
1737 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1738 if (isnullstartblock(right.br_startblock))
1739 state |= BMAP_RIGHT_DELAY;
1743 * We're inserting a real allocation between "left" and "right".
1744 * Set the contiguity flags. Don't let extents get too large.
1746 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1747 left.br_startoff + left.br_blockcount == new->br_startoff &&
1748 left.br_startblock + left.br_blockcount == new->br_startblock &&
1749 left.br_state == new->br_state &&
1750 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1751 state |= BMAP_LEFT_CONTIG;
1753 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1754 new->br_startoff + new->br_blockcount == right.br_startoff &&
1755 new->br_startblock + new->br_blockcount == right.br_startblock &&
1756 new->br_state == right.br_state &&
1757 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1758 (!(state & BMAP_LEFT_CONTIG) ||
1759 left.br_blockcount + new->br_blockcount +
1760 right.br_blockcount <= MAXEXTLEN))
1761 state |= BMAP_RIGHT_CONTIG;
1765 * Select which case we're in here, and implement it.
1767 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1768 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1770 * New allocation is contiguous with real allocations on the
1771 * left and on the right.
1772 * Merge all three into a single extent record.
1775 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1776 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1777 left.br_blockcount + new->br_blockcount +
1778 right.br_blockcount);
1779 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1781 xfs_iext_remove(ip, *idx + 1, 1, state);
1783 XFS_IFORK_NEXT_SET(ip, whichfork,
1784 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1786 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1788 rval = XFS_ILOG_CORE;
1789 if ((error = xfs_bmbt_lookup_eq(cur,
1791 right.br_startblock,
1792 right.br_blockcount, &i)))
1794 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1795 if ((error = xfs_btree_delete(cur, &i)))
1797 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1798 if ((error = xfs_btree_decrement(cur, 0, &i)))
1800 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1801 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1803 left.br_blockcount +
1804 new->br_blockcount +
1805 right.br_blockcount,
1811 case BMAP_LEFT_CONTIG:
1813 * New allocation is contiguous with a real allocation
1815 * Merge the new allocation with the left neighbor.
1818 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1819 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1820 left.br_blockcount + new->br_blockcount);
1821 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1824 rval = xfs_ilog_fext(whichfork);
1827 if ((error = xfs_bmbt_lookup_eq(cur,
1830 left.br_blockcount, &i)))
1832 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1833 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1835 left.br_blockcount +
1842 case BMAP_RIGHT_CONTIG:
1844 * New allocation is contiguous with a real allocation
1846 * Merge the new allocation with the right neighbor.
1848 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1849 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1850 new->br_startoff, new->br_startblock,
1851 new->br_blockcount + right.br_blockcount,
1853 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1856 rval = xfs_ilog_fext(whichfork);
1859 if ((error = xfs_bmbt_lookup_eq(cur,
1861 right.br_startblock,
1862 right.br_blockcount, &i)))
1864 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1865 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1867 new->br_blockcount +
1868 right.br_blockcount,
1876 * New allocation is not contiguous with another
1878 * Insert a new entry.
1880 xfs_iext_insert(ip, *idx, 1, new, state);
1881 XFS_IFORK_NEXT_SET(ip, whichfork,
1882 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1884 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1886 rval = XFS_ILOG_CORE;
1887 if ((error = xfs_bmbt_lookup_eq(cur,
1890 new->br_blockcount, &i)))
1892 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1893 cur->bc_rec.b.br_state = new->br_state;
1894 if ((error = xfs_btree_insert(cur, &i)))
1896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1906 * Adjust the size of the new extent based on di_extsize and rt extsize.
1909 xfs_bmap_extsize_align(
1911 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1912 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1913 xfs_extlen_t extsz, /* align to this extent size */
1914 int rt, /* is this a realtime inode? */
1915 int eof, /* is extent at end-of-file? */
1916 int delay, /* creating delalloc extent? */
1917 int convert, /* overwriting unwritten extent? */
1918 xfs_fileoff_t *offp, /* in/out: aligned offset */
1919 xfs_extlen_t *lenp) /* in/out: aligned length */
1921 xfs_fileoff_t orig_off; /* original offset */
1922 xfs_extlen_t orig_alen; /* original length */
1923 xfs_fileoff_t orig_end; /* original off+len */
1924 xfs_fileoff_t nexto; /* next file offset */
1925 xfs_fileoff_t prevo; /* previous file offset */
1926 xfs_fileoff_t align_off; /* temp for offset */
1927 xfs_extlen_t align_alen; /* temp for length */
1928 xfs_extlen_t temp; /* temp for calculations */
1933 orig_off = align_off = *offp;
1934 orig_alen = align_alen = *lenp;
1935 orig_end = orig_off + orig_alen;
1938 * If this request overlaps an existing extent, then don't
1939 * attempt to perform any additional alignment.
1941 if (!delay && !eof &&
1942 (orig_off >= gotp->br_startoff) &&
1943 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1948 * If the file offset is unaligned vs. the extent size
1949 * we need to align it. This will be possible unless
1950 * the file was previously written with a kernel that didn't
1951 * perform this alignment, or if a truncate shot us in the
1954 temp = do_mod(orig_off, extsz);
1960 * Same adjustment for the end of the requested area.
1962 if ((temp = (align_alen % extsz))) {
1963 align_alen += extsz - temp;
1966 * If the previous block overlaps with this proposed allocation
1967 * then move the start forward without adjusting the length.
1969 if (prevp->br_startoff != NULLFILEOFF) {
1970 if (prevp->br_startblock == HOLESTARTBLOCK)
1971 prevo = prevp->br_startoff;
1973 prevo = prevp->br_startoff + prevp->br_blockcount;
1976 if (align_off != orig_off && align_off < prevo)
1979 * If the next block overlaps with this proposed allocation
1980 * then move the start back without adjusting the length,
1981 * but not before offset 0.
1982 * This may of course make the start overlap previous block,
1983 * and if we hit the offset 0 limit then the next block
1984 * can still overlap too.
1986 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1987 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1988 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1989 nexto = gotp->br_startoff + gotp->br_blockcount;
1991 nexto = gotp->br_startoff;
1993 nexto = NULLFILEOFF;
1995 align_off + align_alen != orig_end &&
1996 align_off + align_alen > nexto)
1997 align_off = nexto > align_alen ? nexto - align_alen : 0;
1999 * If we're now overlapping the next or previous extent that
2000 * means we can't fit an extsz piece in this hole. Just move
2001 * the start forward to the first valid spot and set
2002 * the length so we hit the end.
2004 if (align_off != orig_off && align_off < prevo)
2006 if (align_off + align_alen != orig_end &&
2007 align_off + align_alen > nexto &&
2008 nexto != NULLFILEOFF) {
2009 ASSERT(nexto > prevo);
2010 align_alen = nexto - align_off;
2014 * If realtime, and the result isn't a multiple of the realtime
2015 * extent size we need to remove blocks until it is.
2017 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2019 * We're not covering the original request, or
2020 * we won't be able to once we fix the length.
2022 if (orig_off < align_off ||
2023 orig_end > align_off + align_alen ||
2024 align_alen - temp < orig_alen)
2025 return XFS_ERROR(EINVAL);
2027 * Try to fix it by moving the start up.
2029 if (align_off + temp <= orig_off) {
2034 * Try to fix it by moving the end in.
2036 else if (align_off + align_alen - temp >= orig_end)
2039 * Set the start to the minimum then trim the length.
2042 align_alen -= orig_off - align_off;
2043 align_off = orig_off;
2044 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2047 * Result doesn't cover the request, fail it.
2049 if (orig_off < align_off || orig_end > align_off + align_alen)
2050 return XFS_ERROR(EINVAL);
2052 ASSERT(orig_off >= align_off);
2053 ASSERT(orig_end <= align_off + align_alen);
2057 if (!eof && gotp->br_startoff != NULLFILEOFF)
2058 ASSERT(align_off + align_alen <= gotp->br_startoff);
2059 if (prevp->br_startoff != NULLFILEOFF)
2060 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2068 #define XFS_ALLOC_GAP_UNITS 4
2072 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2074 xfs_fsblock_t adjust; /* adjustment to block numbers */
2075 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2076 xfs_mount_t *mp; /* mount point structure */
2077 int nullfb; /* true if ap->firstblock isn't set */
2078 int rt; /* true if inode is realtime */
2080 #define ISVALID(x,y) \
2082 (x) < mp->m_sb.sb_rblocks : \
2083 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2084 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2085 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2087 mp = ap->ip->i_mount;
2088 nullfb = ap->firstblock == NULLFSBLOCK;
2089 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2090 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2092 * If allocating at eof, and there's a previous real block,
2093 * try to use its last block as our starting point.
2095 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2096 !isnullstartblock(ap->prevp->br_startblock) &&
2097 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2098 ap->prevp->br_startblock)) {
2099 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2101 * Adjust for the gap between prevp and us.
2104 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2106 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2110 * If not at eof, then compare the two neighbor blocks.
2111 * Figure out whether either one gives us a good starting point,
2112 * and pick the better one.
2114 else if (!ap->eof) {
2115 xfs_fsblock_t gotbno; /* right side block number */
2116 xfs_fsblock_t gotdiff=0; /* right side difference */
2117 xfs_fsblock_t prevbno; /* left side block number */
2118 xfs_fsblock_t prevdiff=0; /* left side difference */
2121 * If there's a previous (left) block, select a requested
2122 * start block based on it.
2124 if (ap->prevp->br_startoff != NULLFILEOFF &&
2125 !isnullstartblock(ap->prevp->br_startblock) &&
2126 (prevbno = ap->prevp->br_startblock +
2127 ap->prevp->br_blockcount) &&
2128 ISVALID(prevbno, ap->prevp->br_startblock)) {
2130 * Calculate gap to end of previous block.
2132 adjust = prevdiff = ap->off -
2133 (ap->prevp->br_startoff +
2134 ap->prevp->br_blockcount);
2136 * Figure the startblock based on the previous block's
2137 * end and the gap size.
2139 * If the gap is large relative to the piece we're
2140 * allocating, or using it gives us an invalid block
2141 * number, then just use the end of the previous block.
2143 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2144 ISVALID(prevbno + prevdiff,
2145 ap->prevp->br_startblock))
2150 * If the firstblock forbids it, can't use it,
2153 if (!rt && !nullfb &&
2154 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2155 prevbno = NULLFSBLOCK;
2158 * No previous block or can't follow it, just default.
2161 prevbno = NULLFSBLOCK;
2163 * If there's a following (right) block, select a requested
2164 * start block based on it.
2166 if (!isnullstartblock(ap->gotp->br_startblock)) {
2168 * Calculate gap to start of next block.
2170 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2172 * Figure the startblock based on the next block's
2173 * start and the gap size.
2175 gotbno = ap->gotp->br_startblock;
2178 * If the gap is large relative to the piece we're
2179 * allocating, or using it gives us an invalid block
2180 * number, then just use the start of the next block
2181 * offset by our length.
2183 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2184 ISVALID(gotbno - gotdiff, gotbno))
2186 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2188 gotdiff += adjust - ap->alen;
2192 * If the firstblock forbids it, can't use it,
2195 if (!rt && !nullfb &&
2196 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2197 gotbno = NULLFSBLOCK;
2200 * No next block, just default.
2203 gotbno = NULLFSBLOCK;
2205 * If both valid, pick the better one, else the only good
2206 * one, else ap->rval is already set (to 0 or the inode block).
2208 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2209 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2210 else if (prevbno != NULLFSBLOCK)
2212 else if (gotbno != NULLFSBLOCK)
2220 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2222 xfs_alloctype_t atype = 0; /* type for allocation routines */
2223 int error; /* error return value */
2224 xfs_mount_t *mp; /* mount point structure */
2225 xfs_extlen_t prod = 0; /* product factor for allocators */
2226 xfs_extlen_t ralen = 0; /* realtime allocation length */
2227 xfs_extlen_t align; /* minimum allocation alignment */
2230 mp = ap->ip->i_mount;
2231 align = xfs_get_extsz_hint(ap->ip);
2232 prod = align / mp->m_sb.sb_rextsize;
2233 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2234 align, 1, ap->eof, 0,
2235 ap->conv, &ap->off, &ap->alen);
2239 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2242 * If the offset & length are not perfectly aligned
2243 * then kill prod, it will just get us in trouble.
2245 if (do_mod(ap->off, align) || ap->alen % align)
2248 * Set ralen to be the actual requested length in rtextents.
2250 ralen = ap->alen / mp->m_sb.sb_rextsize;
2252 * If the old value was close enough to MAXEXTLEN that
2253 * we rounded up to it, cut it back so it's valid again.
2254 * Note that if it's a really large request (bigger than
2255 * MAXEXTLEN), we don't hear about that number, and can't
2256 * adjust the starting point to match it.
2258 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2259 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2262 * Lock out other modifications to the RT bitmap inode.
2264 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2265 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2268 * If it's an allocation to an empty file at offset 0,
2269 * pick an extent that will space things out in the rt area.
2271 if (ap->eof && ap->off == 0) {
2272 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2274 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2277 ap->rval = rtx * mp->m_sb.sb_rextsize;
2282 xfs_bmap_adjacent(ap);
2285 * Realtime allocation, done through xfs_rtallocate_extent.
2287 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2288 do_div(ap->rval, mp->m_sb.sb_rextsize);
2291 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2292 &ralen, atype, ap->wasdel, prod, &rtb)))
2294 if (rtb == NULLFSBLOCK && prod > 1 &&
2295 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2296 ap->alen, &ralen, atype,
2297 ap->wasdel, 1, &rtb)))
2300 if (ap->rval != NULLFSBLOCK) {
2301 ap->rval *= mp->m_sb.sb_rextsize;
2302 ralen *= mp->m_sb.sb_rextsize;
2304 ap->ip->i_d.di_nblocks += ralen;
2305 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2307 ap->ip->i_delayed_blks -= ralen;
2309 * Adjust the disk quota also. This was reserved
2312 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2313 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2314 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2322 xfs_bmap_btalloc_nullfb(
2323 struct xfs_bmalloca *ap,
2324 struct xfs_alloc_arg *args,
2327 struct xfs_mount *mp = ap->ip->i_mount;
2328 struct xfs_perag *pag;
2329 xfs_agnumber_t ag, startag;
2333 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2334 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2336 args->type = XFS_ALLOCTYPE_START_BNO;
2337 args->total = ap->total;
2340 * Search for an allocation group with a single extent large enough
2341 * for the request. If one isn't found, then adjust the minimum
2342 * allocation size to the largest space found.
2344 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2345 if (startag == NULLAGNUMBER)
2348 pag = xfs_perag_get(mp, ag);
2349 while (*blen < args->maxlen) {
2350 if (!pag->pagf_init) {
2351 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2352 XFS_ALLOC_FLAG_TRYLOCK);
2360 * See xfs_alloc_fix_freelist...
2362 if (pag->pagf_init) {
2363 xfs_extlen_t longest;
2364 longest = xfs_alloc_longest_free_extent(mp, pag);
2365 if (*blen < longest)
2370 if (xfs_inode_is_filestream(ap->ip)) {
2371 if (*blen >= args->maxlen)
2376 * If startag is an invalid AG, we've
2377 * come here once before and
2378 * xfs_filestream_new_ag picked the
2379 * best currently available.
2381 * Don't continue looping, since we
2382 * could loop forever.
2384 if (startag == NULLAGNUMBER)
2387 error = xfs_filestream_new_ag(ap, &ag);
2392 /* loop again to set 'blen'*/
2393 startag = NULLAGNUMBER;
2394 pag = xfs_perag_get(mp, ag);
2398 if (++ag == mp->m_sb.sb_agcount)
2403 pag = xfs_perag_get(mp, ag);
2408 * Since the above loop did a BUF_TRYLOCK, it is
2409 * possible that there is space for this request.
2411 if (notinit || *blen < ap->minlen)
2412 args->minlen = ap->minlen;
2414 * If the best seen length is less than the request
2415 * length, use the best as the minimum.
2417 else if (*blen < args->maxlen)
2418 args->minlen = *blen;
2420 * Otherwise we've seen an extent as big as maxlen,
2421 * use that as the minimum.
2424 args->minlen = args->maxlen;
2427 * set the failure fallback case to look in the selected
2428 * AG as the stream may have moved.
2430 if (xfs_inode_is_filestream(ap->ip))
2431 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2438 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2440 xfs_mount_t *mp; /* mount point structure */
2441 xfs_alloctype_t atype = 0; /* type for allocation routines */
2442 xfs_extlen_t align; /* minimum allocation alignment */
2443 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2445 xfs_alloc_arg_t args;
2447 xfs_extlen_t nextminlen = 0;
2448 int nullfb; /* true if ap->firstblock isn't set */
2453 mp = ap->ip->i_mount;
2454 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2455 if (unlikely(align)) {
2456 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2457 align, 0, ap->eof, 0, ap->conv,
2458 &ap->off, &ap->alen);
2462 nullfb = ap->firstblock == NULLFSBLOCK;
2463 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2465 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2466 ag = xfs_filestream_lookup_ag(ap->ip);
2467 ag = (ag != NULLAGNUMBER) ? ag : 0;
2468 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2470 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2473 ap->rval = ap->firstblock;
2475 xfs_bmap_adjacent(ap);
2478 * If allowed, use ap->rval; otherwise must use firstblock since
2479 * it's in the right allocation group.
2481 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2484 ap->rval = ap->firstblock;
2486 * Normal allocation, done through xfs_alloc_vextent.
2488 tryagain = isaligned = 0;
2491 args.fsbno = ap->rval;
2493 /* Trim the allocation back to the maximum an AG can fit. */
2494 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
2495 args.firstblock = ap->firstblock;
2498 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2501 } else if (ap->low) {
2502 if (xfs_inode_is_filestream(ap->ip))
2503 args.type = XFS_ALLOCTYPE_FIRST_AG;
2505 args.type = XFS_ALLOCTYPE_START_BNO;
2506 args.total = args.minlen = ap->minlen;
2508 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2509 args.total = ap->total;
2510 args.minlen = ap->minlen;
2512 /* apply extent size hints if obtained earlier */
2513 if (unlikely(align)) {
2515 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2516 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2517 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2521 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2522 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2523 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2526 * If we are not low on available data blocks, and the
2527 * underlying logical volume manager is a stripe, and
2528 * the file offset is zero then try to allocate data
2529 * blocks on stripe unit boundary.
2530 * NOTE: ap->aeof is only set if the allocation length
2531 * is >= the stripe unit and the allocation offset is
2532 * at the end of file.
2534 if (!ap->low && ap->aeof) {
2536 args.alignment = mp->m_dalign;
2540 * Adjust for alignment
2542 if (blen > args.alignment && blen <= args.maxlen)
2543 args.minlen = blen - args.alignment;
2544 args.minalignslop = 0;
2547 * First try an exact bno allocation.
2548 * If it fails then do a near or start bno
2549 * allocation with alignment turned on.
2553 args.type = XFS_ALLOCTYPE_THIS_BNO;
2556 * Compute the minlen+alignment for the
2557 * next case. Set slop so that the value
2558 * of minlen+alignment+slop doesn't go up
2559 * between the calls.
2561 if (blen > mp->m_dalign && blen <= args.maxlen)
2562 nextminlen = blen - mp->m_dalign;
2564 nextminlen = args.minlen;
2565 if (nextminlen + mp->m_dalign > args.minlen + 1)
2567 nextminlen + mp->m_dalign -
2570 args.minalignslop = 0;
2574 args.minalignslop = 0;
2576 args.minleft = ap->minleft;
2577 args.wasdel = ap->wasdel;
2579 args.userdata = ap->userdata;
2580 if ((error = xfs_alloc_vextent(&args)))
2582 if (tryagain && args.fsbno == NULLFSBLOCK) {
2584 * Exact allocation failed. Now try with alignment
2588 args.fsbno = ap->rval;
2589 args.alignment = mp->m_dalign;
2590 args.minlen = nextminlen;
2591 args.minalignslop = 0;
2593 if ((error = xfs_alloc_vextent(&args)))
2596 if (isaligned && args.fsbno == NULLFSBLOCK) {
2598 * allocation failed, so turn off alignment and
2602 args.fsbno = ap->rval;
2604 if ((error = xfs_alloc_vextent(&args)))
2607 if (args.fsbno == NULLFSBLOCK && nullfb &&
2608 args.minlen > ap->minlen) {
2609 args.minlen = ap->minlen;
2610 args.type = XFS_ALLOCTYPE_START_BNO;
2611 args.fsbno = ap->rval;
2612 if ((error = xfs_alloc_vextent(&args)))
2615 if (args.fsbno == NULLFSBLOCK && nullfb) {
2617 args.type = XFS_ALLOCTYPE_FIRST_AG;
2618 args.total = ap->minlen;
2620 if ((error = xfs_alloc_vextent(&args)))
2624 if (args.fsbno != NULLFSBLOCK) {
2625 ap->firstblock = ap->rval = args.fsbno;
2626 ASSERT(nullfb || fb_agno == args.agno ||
2627 (ap->low && fb_agno < args.agno));
2628 ap->alen = args.len;
2629 ap->ip->i_d.di_nblocks += args.len;
2630 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2632 ap->ip->i_delayed_blks -= args.len;
2634 * Adjust the disk quota also. This was reserved
2637 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2638 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2639 XFS_TRANS_DQ_BCOUNT,
2642 ap->rval = NULLFSBLOCK;
2649 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2650 * It figures out where to ask the underlying allocator to put the new extent.
2654 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2656 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2657 return xfs_bmap_rtalloc(ap);
2658 return xfs_bmap_btalloc(ap);
2662 * Transform a btree format file with only one leaf node, where the
2663 * extents list will fit in the inode, into an extents format file.
2664 * Since the file extents are already in-core, all we have to do is
2665 * give up the space for the btree root and pitch the leaf block.
2667 STATIC int /* error */
2668 xfs_bmap_btree_to_extents(
2669 xfs_trans_t *tp, /* transaction pointer */
2670 xfs_inode_t *ip, /* incore inode pointer */
2671 xfs_btree_cur_t *cur, /* btree cursor */
2672 int *logflagsp, /* inode logging flags */
2673 int whichfork) /* data or attr fork */
2676 struct xfs_btree_block *cblock;/* child btree block */
2677 xfs_fsblock_t cbno; /* child block number */
2678 xfs_buf_t *cbp; /* child block's buffer */
2679 int error; /* error return value */
2680 xfs_ifork_t *ifp; /* inode fork data */
2681 xfs_mount_t *mp; /* mount point structure */
2682 __be64 *pp; /* ptr to block address */
2683 struct xfs_btree_block *rblock;/* root btree block */
2686 ifp = XFS_IFORK_PTR(ip, whichfork);
2687 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2688 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2689 rblock = ifp->if_broot;
2690 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2691 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2692 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2693 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2694 cbno = be64_to_cpu(*pp);
2697 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2700 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2701 XFS_BMAP_BTREE_REF)))
2703 cblock = XFS_BUF_TO_BLOCK(cbp);
2704 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2706 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2707 ip->i_d.di_nblocks--;
2708 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2709 xfs_trans_binval(tp, cbp);
2710 if (cur->bc_bufs[0] == cbp)
2711 cur->bc_bufs[0] = NULL;
2712 xfs_iroot_realloc(ip, -1, whichfork);
2713 ASSERT(ifp->if_broot == NULL);
2714 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2715 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2716 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2721 * Called by xfs_bmapi to update file extent records and the btree
2722 * after removing space (or undoing a delayed allocation).
2724 STATIC int /* error */
2725 xfs_bmap_del_extent(
2726 xfs_inode_t *ip, /* incore inode pointer */
2727 xfs_trans_t *tp, /* current transaction pointer */
2728 xfs_extnum_t *idx, /* extent number to update/delete */
2729 xfs_bmap_free_t *flist, /* list of extents to be freed */
2730 xfs_btree_cur_t *cur, /* if null, not a btree */
2731 xfs_bmbt_irec_t *del, /* data to remove from extents */
2732 int *logflagsp, /* inode logging flags */
2733 int whichfork) /* data or attr fork */
2735 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2736 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2737 xfs_fsblock_t del_endblock=0; /* first block past del */
2738 xfs_fileoff_t del_endoff; /* first offset past del */
2739 int delay; /* current block is delayed allocated */
2740 int do_fx; /* free extent at end of routine */
2741 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2742 int error; /* error return value */
2743 int flags; /* inode logging flags */
2744 xfs_bmbt_irec_t got; /* current extent entry */
2745 xfs_fileoff_t got_endoff; /* first offset past got */
2746 int i; /* temp state */
2747 xfs_ifork_t *ifp; /* inode fork pointer */
2748 xfs_mount_t *mp; /* mount structure */
2749 xfs_filblks_t nblks; /* quota/sb block count */
2750 xfs_bmbt_irec_t new; /* new record to be inserted */
2752 uint qfield; /* quota field to update */
2753 xfs_filblks_t temp; /* for indirect length calculations */
2754 xfs_filblks_t temp2; /* for indirect length calculations */
2757 XFS_STATS_INC(xs_del_exlist);
2759 if (whichfork == XFS_ATTR_FORK)
2760 state |= BMAP_ATTRFORK;
2763 ifp = XFS_IFORK_PTR(ip, whichfork);
2764 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
2765 (uint)sizeof(xfs_bmbt_rec_t)));
2766 ASSERT(del->br_blockcount > 0);
2767 ep = xfs_iext_get_ext(ifp, *idx);
2768 xfs_bmbt_get_all(ep, &got);
2769 ASSERT(got.br_startoff <= del->br_startoff);
2770 del_endoff = del->br_startoff + del->br_blockcount;
2771 got_endoff = got.br_startoff + got.br_blockcount;
2772 ASSERT(got_endoff >= del_endoff);
2773 delay = isnullstartblock(got.br_startblock);
2774 ASSERT(isnullstartblock(del->br_startblock) == delay);
2779 * If deleting a real allocation, must free up the disk space.
2782 flags = XFS_ILOG_CORE;
2784 * Realtime allocation. Free it and record di_nblocks update.
2786 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2790 ASSERT(do_mod(del->br_blockcount,
2791 mp->m_sb.sb_rextsize) == 0);
2792 ASSERT(do_mod(del->br_startblock,
2793 mp->m_sb.sb_rextsize) == 0);
2794 bno = del->br_startblock;
2795 len = del->br_blockcount;
2796 do_div(bno, mp->m_sb.sb_rextsize);
2797 do_div(len, mp->m_sb.sb_rextsize);
2798 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2802 nblks = len * mp->m_sb.sb_rextsize;
2803 qfield = XFS_TRANS_DQ_RTBCOUNT;
2806 * Ordinary allocation.
2810 nblks = del->br_blockcount;
2811 qfield = XFS_TRANS_DQ_BCOUNT;
2814 * Set up del_endblock and cur for later.
2816 del_endblock = del->br_startblock + del->br_blockcount;
2818 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2819 got.br_startblock, got.br_blockcount,
2822 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2824 da_old = da_new = 0;
2826 da_old = startblockval(got.br_startblock);
2832 * Set flag value to use in switch statement.
2833 * Left-contig is 2, right-contig is 1.
2835 switch (((got.br_startoff == del->br_startoff) << 1) |
2836 (got_endoff == del_endoff)) {
2839 * Matches the whole extent. Delete the entry.
2841 xfs_iext_remove(ip, *idx, 1,
2842 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2847 XFS_IFORK_NEXT_SET(ip, whichfork,
2848 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2849 flags |= XFS_ILOG_CORE;
2851 flags |= xfs_ilog_fext(whichfork);
2854 if ((error = xfs_btree_delete(cur, &i)))
2856 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2861 * Deleting the first part of the extent.
2863 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2864 xfs_bmbt_set_startoff(ep, del_endoff);
2865 temp = got.br_blockcount - del->br_blockcount;
2866 xfs_bmbt_set_blockcount(ep, temp);
2868 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2870 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2871 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2875 xfs_bmbt_set_startblock(ep, del_endblock);
2876 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2878 flags |= xfs_ilog_fext(whichfork);
2881 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2882 got.br_blockcount - del->br_blockcount,
2889 * Deleting the last part of the extent.
2891 temp = got.br_blockcount - del->br_blockcount;
2892 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2893 xfs_bmbt_set_blockcount(ep, temp);
2895 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2897 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2898 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2902 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2904 flags |= xfs_ilog_fext(whichfork);
2907 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2909 got.br_blockcount - del->br_blockcount,
2916 * Deleting the middle of the extent.
2918 temp = del->br_startoff - got.br_startoff;
2919 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2920 xfs_bmbt_set_blockcount(ep, temp);
2921 new.br_startoff = del_endoff;
2922 temp2 = got_endoff - del_endoff;
2923 new.br_blockcount = temp2;
2924 new.br_state = got.br_state;
2926 new.br_startblock = del_endblock;
2927 flags |= XFS_ILOG_CORE;
2929 if ((error = xfs_bmbt_update(cur,
2931 got.br_startblock, temp,
2934 if ((error = xfs_btree_increment(cur, 0, &i)))
2936 cur->bc_rec.b = new;
2937 error = xfs_btree_insert(cur, &i);
2938 if (error && error != ENOSPC)
2941 * If get no-space back from btree insert,
2942 * it tried a split, and we have a zero
2943 * block reservation.
2944 * Fix up our state and return the error.
2946 if (error == ENOSPC) {
2948 * Reset the cursor, don't trust
2949 * it after any insert operation.
2951 if ((error = xfs_bmbt_lookup_eq(cur,
2956 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2958 * Update the btree record back
2959 * to the original value.
2961 if ((error = xfs_bmbt_update(cur,
2968 * Reset the extent record back
2969 * to the original value.
2971 xfs_bmbt_set_blockcount(ep,
2974 error = XFS_ERROR(ENOSPC);
2977 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2979 flags |= xfs_ilog_fext(whichfork);
2980 XFS_IFORK_NEXT_SET(ip, whichfork,
2981 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2983 ASSERT(whichfork == XFS_DATA_FORK);
2984 temp = xfs_bmap_worst_indlen(ip, temp);
2985 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2986 temp2 = xfs_bmap_worst_indlen(ip, temp2);
2987 new.br_startblock = nullstartblock((int)temp2);
2988 da_new = temp + temp2;
2989 while (da_new > da_old) {
2993 xfs_bmbt_set_startblock(ep,
2994 nullstartblock((int)temp));
2996 if (da_new == da_old)
3002 nullstartblock((int)temp2);
3006 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3007 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3012 * If we need to, add to list of extents to delete.
3015 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3018 * Adjust inode # blocks in the file.
3021 ip->i_d.di_nblocks -= nblks;
3023 * Adjust quota data.
3026 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3029 * Account for change in delayed indirect blocks.
3030 * Nothing to do for disk quota accounting here.
3032 ASSERT(da_old >= da_new);
3033 if (da_old > da_new) {
3034 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3035 (int64_t)(da_old - da_new), 0);
3043 * Remove the entry "free" from the free item list. Prev points to the
3044 * previous entry, unless "free" is the head of the list.
3048 xfs_bmap_free_t *flist, /* free item list header */
3049 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3050 xfs_bmap_free_item_t *free) /* list item to be freed */
3053 prev->xbfi_next = free->xbfi_next;
3055 flist->xbf_first = free->xbfi_next;
3057 kmem_zone_free(xfs_bmap_free_item_zone, free);
3061 * Convert an extents-format file into a btree-format file.
3062 * The new file will have a root block (in the inode) and a single child block.
3064 STATIC int /* error */
3065 xfs_bmap_extents_to_btree(
3066 xfs_trans_t *tp, /* transaction pointer */
3067 xfs_inode_t *ip, /* incore inode pointer */
3068 xfs_fsblock_t *firstblock, /* first-block-allocated */
3069 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3070 xfs_btree_cur_t **curp, /* cursor returned to caller */
3071 int wasdel, /* converting a delayed alloc */
3072 int *logflagsp, /* inode logging flags */
3073 int whichfork) /* data or attr fork */
3075 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3076 xfs_buf_t *abp; /* buffer for ablock */
3077 xfs_alloc_arg_t args; /* allocation arguments */
3078 xfs_bmbt_rec_t *arp; /* child record pointer */
3079 struct xfs_btree_block *block; /* btree root block */
3080 xfs_btree_cur_t *cur; /* bmap btree cursor */
3081 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3082 int error; /* error return value */
3083 xfs_extnum_t i, cnt; /* extent record index */
3084 xfs_ifork_t *ifp; /* inode fork pointer */
3085 xfs_bmbt_key_t *kp; /* root block key pointer */
3086 xfs_mount_t *mp; /* mount structure */
3087 xfs_extnum_t nextents; /* number of file extents */
3088 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3090 ifp = XFS_IFORK_PTR(ip, whichfork);
3091 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3092 ASSERT(ifp->if_ext_max ==
3093 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3095 * Make space in the inode incore.
3097 xfs_iroot_realloc(ip, 1, whichfork);
3098 ifp->if_flags |= XFS_IFBROOT;
3103 block = ifp->if_broot;
3104 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3105 block->bb_level = cpu_to_be16(1);
3106 block->bb_numrecs = cpu_to_be16(1);
3107 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3108 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3111 * Need a cursor. Can't allocate until bb_level is filled in.
3114 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3115 cur->bc_private.b.firstblock = *firstblock;
3116 cur->bc_private.b.flist = flist;
3117 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3119 * Convert to a btree with two levels, one record in root.
3121 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3124 args.firstblock = *firstblock;
3125 if (*firstblock == NULLFSBLOCK) {
3126 args.type = XFS_ALLOCTYPE_START_BNO;
3127 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3128 } else if (flist->xbf_low) {
3129 args.type = XFS_ALLOCTYPE_START_BNO;
3130 args.fsbno = *firstblock;
3132 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3133 args.fsbno = *firstblock;
3135 args.minlen = args.maxlen = args.prod = 1;
3136 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3137 args.minalignslop = 0;
3138 args.wasdel = wasdel;
3140 if ((error = xfs_alloc_vextent(&args))) {
3141 xfs_iroot_realloc(ip, -1, whichfork);
3142 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3146 * Allocation can't fail, the space was reserved.
3148 ASSERT(args.fsbno != NULLFSBLOCK);
3149 ASSERT(*firstblock == NULLFSBLOCK ||
3150 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3152 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3153 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3154 cur->bc_private.b.allocated++;
3155 ip->i_d.di_nblocks++;
3156 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3157 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3159 * Fill in the child block.
3161 ablock = XFS_BUF_TO_BLOCK(abp);
3162 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3163 ablock->bb_level = 0;
3164 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3165 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3166 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3167 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3168 for (cnt = i = 0; i < nextents; i++) {
3169 ep = xfs_iext_get_ext(ifp, i);
3170 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3171 arp->l0 = cpu_to_be64(ep->l0);
3172 arp->l1 = cpu_to_be64(ep->l1);
3176 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3177 xfs_btree_set_numrecs(ablock, cnt);
3180 * Fill in the root key and pointer.
3182 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3183 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3184 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3185 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3186 be16_to_cpu(block->bb_level)));
3187 *pp = cpu_to_be64(args.fsbno);
3190 * Do all this logging at the end so that
3191 * the root is at the right level.
3193 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3194 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3195 ASSERT(*curp == NULL);
3197 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3202 * Calculate the default attribute fork offset for newly created inodes.
3205 xfs_default_attroffset(
3206 struct xfs_inode *ip)
3208 struct xfs_mount *mp = ip->i_mount;
3211 if (mp->m_sb.sb_inodesize == 256) {
3212 offset = XFS_LITINO(mp) -
3213 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3215 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3218 ASSERT(offset < XFS_LITINO(mp));
3223 * Helper routine to reset inode di_forkoff field when switching
3224 * attribute fork from local to extent format - we reset it where
3225 * possible to make space available for inline data fork extents.
3228 xfs_bmap_forkoff_reset(
3233 if (whichfork == XFS_ATTR_FORK &&
3234 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3235 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3236 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3237 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3239 if (dfl_forkoff > ip->i_d.di_forkoff) {
3240 ip->i_d.di_forkoff = dfl_forkoff;
3241 ip->i_df.if_ext_max =
3242 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3243 ip->i_afp->if_ext_max =
3244 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3250 * Convert a local file to an extents file.
3251 * This code is out of bounds for data forks of regular files,
3252 * since the file data needs to get logged so things will stay consistent.
3253 * (The bmap-level manipulations are ok, though).
3255 STATIC int /* error */
3256 xfs_bmap_local_to_extents(
3257 xfs_trans_t *tp, /* transaction pointer */
3258 xfs_inode_t *ip, /* incore inode pointer */
3259 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3260 xfs_extlen_t total, /* total blocks needed by transaction */
3261 int *logflagsp, /* inode logging flags */
3262 int whichfork) /* data or attr fork */
3264 int error; /* error return value */
3265 int flags; /* logging flags returned */
3266 xfs_ifork_t *ifp; /* inode fork pointer */
3269 * We don't want to deal with the case of keeping inode data inline yet.
3270 * So sending the data fork of a regular inode is invalid.
3272 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
3273 ifp = XFS_IFORK_PTR(ip, whichfork);
3274 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3277 if (ifp->if_bytes) {
3278 xfs_alloc_arg_t args; /* allocation arguments */
3279 xfs_buf_t *bp; /* buffer for extent block */
3280 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3283 args.mp = ip->i_mount;
3284 args.firstblock = *firstblock;
3285 ASSERT((ifp->if_flags &
3286 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3288 * Allocate a block. We know we need only one, since the
3289 * file currently fits in an inode.
3291 if (*firstblock == NULLFSBLOCK) {
3292 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3293 args.type = XFS_ALLOCTYPE_START_BNO;
3295 args.fsbno = *firstblock;
3296 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3299 args.mod = args.minleft = args.alignment = args.wasdel =
3300 args.isfl = args.minalignslop = 0;
3301 args.minlen = args.maxlen = args.prod = 1;
3302 if ((error = xfs_alloc_vextent(&args)))
3305 * Can't fail, the space was reserved.
3307 ASSERT(args.fsbno != NULLFSBLOCK);
3308 ASSERT(args.len == 1);
3309 *firstblock = args.fsbno;
3310 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3311 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
3312 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3313 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3314 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3315 xfs_iext_add(ifp, 0, 1);
3316 ep = xfs_iext_get_ext(ifp, 0);
3317 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3318 trace_xfs_bmap_post_update(ip, 0,
3319 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3321 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3322 ip->i_d.di_nblocks = 1;
3323 xfs_trans_mod_dquot_byino(tp, ip,
3324 XFS_TRANS_DQ_BCOUNT, 1L);
3325 flags |= xfs_ilog_fext(whichfork);
3327 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3328 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3330 ifp->if_flags &= ~XFS_IFINLINE;
3331 ifp->if_flags |= XFS_IFEXTENTS;
3332 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3333 flags |= XFS_ILOG_CORE;
3340 * Search the extent records for the entry containing block bno.
3341 * If bno lies in a hole, point to the next entry. If bno lies
3342 * past eof, *eofp will be set, and *prevp will contain the last
3343 * entry (null if none). Else, *lastxp will be set to the index
3344 * of the found entry; *gotp will contain the entry.
3346 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3347 xfs_bmap_search_multi_extents(
3348 xfs_ifork_t *ifp, /* inode fork pointer */
3349 xfs_fileoff_t bno, /* block number searched for */
3350 int *eofp, /* out: end of file found */
3351 xfs_extnum_t *lastxp, /* out: last extent index */
3352 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3353 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3355 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3356 xfs_extnum_t lastx; /* last extent index */
3359 * Initialize the extent entry structure to catch access to
3360 * uninitialized br_startblock field.
3362 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3363 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3364 gotp->br_state = XFS_EXT_INVALID;
3366 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3368 gotp->br_startblock = 0xffffa5a5;
3370 prevp->br_startoff = NULLFILEOFF;
3372 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3374 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3376 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3377 xfs_bmbt_get_all(ep, gotp);
3391 * Search the extents list for the inode, for the extent containing bno.
3392 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3393 * *eofp will be set, and *prevp will contain the last entry (null if none).
3394 * Else, *lastxp will be set to the index of the found
3395 * entry; *gotp will contain the entry.
3397 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3398 xfs_bmap_search_extents(
3399 xfs_inode_t *ip, /* incore inode pointer */
3400 xfs_fileoff_t bno, /* block number searched for */
3401 int fork, /* data or attr fork */
3402 int *eofp, /* out: end of file found */
3403 xfs_extnum_t *lastxp, /* out: last extent index */
3404 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3405 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3407 xfs_ifork_t *ifp; /* inode fork pointer */
3408 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3410 XFS_STATS_INC(xs_look_exlist);
3411 ifp = XFS_IFORK_PTR(ip, fork);
3413 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3415 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3416 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3417 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
3418 "Access to block zero in inode %llu "
3419 "start_block: %llx start_off: %llx "
3420 "blkcnt: %llx extent-state: %x lastx: %x\n",
3421 (unsigned long long)ip->i_ino,
3422 (unsigned long long)gotp->br_startblock,
3423 (unsigned long long)gotp->br_startoff,
3424 (unsigned long long)gotp->br_blockcount,
3425 gotp->br_state, *lastxp);
3426 *lastxp = NULLEXTNUM;
3434 * Compute the worst-case number of indirect blocks that will be used
3435 * for ip's delayed extent of length "len".
3437 STATIC xfs_filblks_t
3438 xfs_bmap_worst_indlen(
3439 xfs_inode_t *ip, /* incore inode pointer */
3440 xfs_filblks_t len) /* delayed extent length */
3442 int level; /* btree level number */
3443 int maxrecs; /* maximum record count at this level */
3444 xfs_mount_t *mp; /* mount structure */
3445 xfs_filblks_t rval; /* return value */
3448 maxrecs = mp->m_bmap_dmxr[0];
3449 for (level = 0, rval = 0;
3450 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3453 do_div(len, maxrecs);
3456 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3459 maxrecs = mp->m_bmap_dmxr[1];
3465 * Convert inode from non-attributed to attributed.
3466 * Must not be in a transaction, ip must not be locked.
3468 int /* error code */
3469 xfs_bmap_add_attrfork(
3470 xfs_inode_t *ip, /* incore inode pointer */
3471 int size, /* space new attribute needs */
3472 int rsvd) /* xact may use reserved blks */
3474 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3475 xfs_bmap_free_t flist; /* freed extent records */
3476 xfs_mount_t *mp; /* mount structure */
3477 xfs_trans_t *tp; /* transaction pointer */
3478 int blks; /* space reservation */
3479 int version = 1; /* superblock attr version */
3480 int committed; /* xaction was committed */
3481 int logflags; /* logging flags */
3482 int error; /* error return value */
3484 ASSERT(XFS_IFORK_Q(ip) == 0);
3485 ASSERT(ip->i_df.if_ext_max ==
3486 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3489 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3490 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3491 blks = XFS_ADDAFORK_SPACE_RES(mp);
3493 tp->t_flags |= XFS_TRANS_RESERVE;
3494 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3495 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3497 xfs_ilock(ip, XFS_ILOCK_EXCL);
3498 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3499 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3500 XFS_QMOPT_RES_REGBLKS);
3502 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3503 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3506 if (XFS_IFORK_Q(ip))
3508 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3510 * For inodes coming from pre-6.2 filesystems.
3512 ASSERT(ip->i_d.di_aformat == 0);
3513 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3515 ASSERT(ip->i_d.di_anextents == 0);
3517 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3518 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3520 switch (ip->i_d.di_format) {
3521 case XFS_DINODE_FMT_DEV:
3522 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3524 case XFS_DINODE_FMT_UUID:
3525 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3527 case XFS_DINODE_FMT_LOCAL:
3528 case XFS_DINODE_FMT_EXTENTS:
3529 case XFS_DINODE_FMT_BTREE:
3530 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3531 if (!ip->i_d.di_forkoff)
3532 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3533 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3538 error = XFS_ERROR(EINVAL);
3541 ip->i_df.if_ext_max =
3542 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3543 ASSERT(ip->i_afp == NULL);
3544 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3545 ip->i_afp->if_ext_max =
3546 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3547 ip->i_afp->if_flags = XFS_IFEXTENTS;
3549 xfs_bmap_init(&flist, &firstblock);
3550 switch (ip->i_d.di_format) {
3551 case XFS_DINODE_FMT_LOCAL:
3552 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3555 case XFS_DINODE_FMT_EXTENTS:
3556 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3559 case XFS_DINODE_FMT_BTREE:
3560 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3568 xfs_trans_log_inode(tp, ip, logflags);
3571 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3572 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3573 __int64_t sbfields = 0;
3575 spin_lock(&mp->m_sb_lock);
3576 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3577 xfs_sb_version_addattr(&mp->m_sb);
3578 sbfields |= XFS_SB_VERSIONNUM;
3580 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3581 xfs_sb_version_addattr2(&mp->m_sb);
3582 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3585 spin_unlock(&mp->m_sb_lock);
3586 xfs_mod_sb(tp, sbfields);
3588 spin_unlock(&mp->m_sb_lock);
3590 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3592 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3593 ASSERT(ip->i_df.if_ext_max ==
3594 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3597 xfs_bmap_cancel(&flist);
3599 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3601 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3602 ASSERT(ip->i_df.if_ext_max ==
3603 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3608 * Add the extent to the list of extents to be free at transaction end.
3609 * The list is maintained sorted (by block number).
3614 xfs_fsblock_t bno, /* fs block number of extent */
3615 xfs_filblks_t len, /* length of extent */
3616 xfs_bmap_free_t *flist, /* list of extents */
3617 xfs_mount_t *mp) /* mount point structure */
3619 xfs_bmap_free_item_t *cur; /* current (next) element */
3620 xfs_bmap_free_item_t *new; /* new element */
3621 xfs_bmap_free_item_t *prev; /* previous element */
3623 xfs_agnumber_t agno;
3624 xfs_agblock_t agbno;
3626 ASSERT(bno != NULLFSBLOCK);
3628 ASSERT(len <= MAXEXTLEN);
3629 ASSERT(!isnullstartblock(bno));
3630 agno = XFS_FSB_TO_AGNO(mp, bno);
3631 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3632 ASSERT(agno < mp->m_sb.sb_agcount);
3633 ASSERT(agbno < mp->m_sb.sb_agblocks);
3634 ASSERT(len < mp->m_sb.sb_agblocks);
3635 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3637 ASSERT(xfs_bmap_free_item_zone != NULL);
3638 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3639 new->xbfi_startblock = bno;
3640 new->xbfi_blockcount = (xfs_extlen_t)len;
3641 for (prev = NULL, cur = flist->xbf_first;
3643 prev = cur, cur = cur->xbfi_next) {
3644 if (cur->xbfi_startblock >= bno)
3648 prev->xbfi_next = new;
3650 flist->xbf_first = new;
3651 new->xbfi_next = cur;
3656 * Compute and fill in the value of the maximum depth of a bmap btree
3657 * in this filesystem. Done once, during mount.
3660 xfs_bmap_compute_maxlevels(
3661 xfs_mount_t *mp, /* file system mount structure */
3662 int whichfork) /* data or attr fork */
3664 int level; /* btree level */
3665 uint maxblocks; /* max blocks at this level */
3666 uint maxleafents; /* max leaf entries possible */
3667 int maxrootrecs; /* max records in root block */
3668 int minleafrecs; /* min records in leaf block */
3669 int minnoderecs; /* min records in node block */
3670 int sz; /* root block size */
3673 * The maximum number of extents in a file, hence the maximum
3674 * number of leaf entries, is controlled by the type of di_nextents
3675 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3676 * (a signed 16-bit number, xfs_aextnum_t).
3678 * Note that we can no longer assume that if we are in ATTR1 that
3679 * the fork offset of all the inodes will be
3680 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3681 * with ATTR2 and then mounted back with ATTR1, keeping the
3682 * di_forkoff's fixed but probably at various positions. Therefore,
3683 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3684 * of a minimum size available.
3686 if (whichfork == XFS_DATA_FORK) {
3687 maxleafents = MAXEXTNUM;
3688 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3690 maxleafents = MAXAEXTNUM;
3691 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3693 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3694 minleafrecs = mp->m_bmap_dmnr[0];
3695 minnoderecs = mp->m_bmap_dmnr[1];
3696 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3697 for (level = 1; maxblocks > 1; level++) {
3698 if (maxblocks <= maxrootrecs)
3701 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3703 mp->m_bm_maxlevels[whichfork] = level;
3707 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3708 * caller. Frees all the extents that need freeing, which must be done
3709 * last due to locking considerations. We never free any extents in
3710 * the first transaction.
3712 * Return 1 if the given transaction was committed and a new one
3713 * started, and 0 otherwise in the committed parameter.
3717 xfs_trans_t **tp, /* transaction pointer addr */
3718 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3719 int *committed) /* xact committed or not */
3721 xfs_efd_log_item_t *efd; /* extent free data */
3722 xfs_efi_log_item_t *efi; /* extent free intention */
3723 int error; /* error return value */
3724 xfs_bmap_free_item_t *free; /* free extent item */
3725 unsigned int logres; /* new log reservation */
3726 unsigned int logcount; /* new log count */
3727 xfs_mount_t *mp; /* filesystem mount structure */
3728 xfs_bmap_free_item_t *next; /* next item on free list */
3729 xfs_trans_t *ntp; /* new transaction pointer */
3731 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3732 if (flist->xbf_count == 0) {
3737 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3738 for (free = flist->xbf_first; free; free = free->xbfi_next)
3739 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3740 free->xbfi_blockcount);
3741 logres = ntp->t_log_res;
3742 logcount = ntp->t_log_count;
3743 ntp = xfs_trans_dup(*tp);
3744 error = xfs_trans_commit(*tp, 0);
3748 * We have a new transaction, so we should return committed=1,
3749 * even though we're returning an error.
3755 * transaction commit worked ok so we can drop the extra ticket
3756 * reference that we gained in xfs_trans_dup()
3758 xfs_log_ticket_put(ntp->t_ticket);
3760 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3763 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3764 for (free = flist->xbf_first; free != NULL; free = next) {
3765 next = free->xbfi_next;
3766 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3767 free->xbfi_blockcount))) {
3769 * The bmap free list will be cleaned up at a
3770 * higher level. The EFI will be canceled when
3771 * this transaction is aborted.
3772 * Need to force shutdown here to make sure it
3773 * happens, since this transaction may not be
3777 if (!XFS_FORCED_SHUTDOWN(mp))
3778 xfs_force_shutdown(mp,
3779 (error == EFSCORRUPTED) ?
3780 SHUTDOWN_CORRUPT_INCORE :
3781 SHUTDOWN_META_IO_ERROR);
3784 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3785 free->xbfi_blockcount);
3786 xfs_bmap_del_free(flist, NULL, free);
3792 * Free up any items left in the list.
3796 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3798 xfs_bmap_free_item_t *free; /* free list item */
3799 xfs_bmap_free_item_t *next;
3801 if (flist->xbf_count == 0)
3803 ASSERT(flist->xbf_first != NULL);
3804 for (free = flist->xbf_first; free; free = next) {
3805 next = free->xbfi_next;
3806 xfs_bmap_del_free(flist, NULL, free);
3808 ASSERT(flist->xbf_count == 0);
3812 * Returns the file-relative block number of the first unused block(s)
3813 * in the file with at least "len" logically contiguous blocks free.
3814 * This is the lowest-address hole if the file has holes, else the first block
3815 * past the end of file.
3816 * Return 0 if the file is currently local (in-inode).
3819 xfs_bmap_first_unused(
3820 xfs_trans_t *tp, /* transaction pointer */
3821 xfs_inode_t *ip, /* incore inode */
3822 xfs_extlen_t len, /* size of hole to find */
3823 xfs_fileoff_t *first_unused, /* unused block */
3824 int whichfork) /* data or attr fork */
3826 int error; /* error return value */
3827 int idx; /* extent record index */
3828 xfs_ifork_t *ifp; /* inode fork pointer */
3829 xfs_fileoff_t lastaddr; /* last block number seen */
3830 xfs_fileoff_t lowest; /* lowest useful block */
3831 xfs_fileoff_t max; /* starting useful block */
3832 xfs_fileoff_t off; /* offset for this block */
3833 xfs_extnum_t nextents; /* number of extent entries */
3835 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3836 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3837 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3838 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
3843 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3844 (error = xfs_iread_extents(tp, ip, whichfork)))
3846 lowest = *first_unused;
3847 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3848 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3849 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3850 off = xfs_bmbt_get_startoff(ep);
3852 * See if the hole before this extent will work.
3854 if (off >= lowest + len && off - max >= len) {
3855 *first_unused = max;
3858 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3859 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3861 *first_unused = max;
3866 * Returns the file-relative block number of the last block + 1 before
3867 * last_block (input value) in the file.
3868 * This is not based on i_size, it is based on the extent records.
3869 * Returns 0 for local files, as they do not have extent records.
3872 xfs_bmap_last_before(
3873 xfs_trans_t *tp, /* transaction pointer */
3874 xfs_inode_t *ip, /* incore inode */
3875 xfs_fileoff_t *last_block, /* last block */
3876 int whichfork) /* data or attr fork */
3878 xfs_fileoff_t bno; /* input file offset */
3879 int eof; /* hit end of file */
3880 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3881 int error; /* error return value */
3882 xfs_bmbt_irec_t got; /* current extent value */
3883 xfs_ifork_t *ifp; /* inode fork pointer */
3884 xfs_extnum_t lastx; /* last extent used */
3885 xfs_bmbt_irec_t prev; /* previous extent value */
3887 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3888 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3889 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3890 return XFS_ERROR(EIO);
3891 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3895 ifp = XFS_IFORK_PTR(ip, whichfork);
3896 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3897 (error = xfs_iread_extents(tp, ip, whichfork)))
3899 bno = *last_block - 1;
3900 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3902 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3903 if (prev.br_startoff == NULLFILEOFF)
3906 *last_block = prev.br_startoff + prev.br_blockcount;
3909 * Otherwise *last_block is already the right answer.
3915 xfs_bmap_last_extent(
3916 struct xfs_trans *tp,
3917 struct xfs_inode *ip,
3919 struct xfs_bmbt_irec *rec,
3922 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3926 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3927 error = xfs_iread_extents(tp, ip, whichfork);
3932 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3933 if (nextents == 0) {
3938 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3944 * Check the last inode extent to determine whether this allocation will result
3945 * in blocks being allocated at the end of the file. When we allocate new data
3946 * blocks at the end of the file which do not start at the previous data block,
3947 * we will try to align the new blocks at stripe unit boundaries.
3949 * Returns 0 in *aeof if the file (fork) is empty as any new write will be at,
3954 struct xfs_inode *ip,
3959 struct xfs_bmbt_irec rec;
3964 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
3965 if (error || is_empty)
3969 * Check if we are allocation or past the last extent, or at least into
3970 * the last delayed allocated extent.
3972 *aeof = off >= rec.br_startoff + rec.br_blockcount ||
3973 (off >= rec.br_startoff && isnullstartblock(rec.br_startblock));
3978 * Check if the endoff is outside the last extent. If so the caller will grow
3979 * the allocation to a stripe unit boundary. All offsets are considered outside
3980 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3984 struct xfs_inode *ip,
3985 xfs_fileoff_t endoff,
3989 struct xfs_bmbt_irec rec;
3992 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
3996 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
4001 * Returns the file-relative block number of the first block past eof in
4002 * the file. This is not based on i_size, it is based on the extent records.
4003 * Returns 0 for local files, as they do not have extent records.
4006 xfs_bmap_last_offset(
4007 struct xfs_trans *tp,
4008 struct xfs_inode *ip,
4009 xfs_fileoff_t *last_block,
4012 struct xfs_bmbt_irec rec;
4018 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4023 return XFS_ERROR(EIO);
4025 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
4026 if (error || is_empty)
4029 *last_block = rec.br_startoff + rec.br_blockcount;
4034 * Returns whether the selected fork of the inode has exactly one
4035 * block or not. For the data fork we check this matches di_size,
4036 * implying the file's range is 0..bsize-1.
4038 int /* 1=>1 block, 0=>otherwise */
4040 xfs_inode_t *ip, /* incore inode */
4041 int whichfork) /* data or attr fork */
4043 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4044 xfs_ifork_t *ifp; /* inode fork pointer */
4045 int rval; /* return value */
4046 xfs_bmbt_irec_t s; /* internal version of extent */
4049 if (whichfork == XFS_DATA_FORK) {
4050 return S_ISREG(ip->i_d.di_mode) ?
4051 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4052 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4055 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4057 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4059 ifp = XFS_IFORK_PTR(ip, whichfork);
4060 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4061 ep = xfs_iext_get_ext(ifp, 0);
4062 xfs_bmbt_get_all(ep, &s);
4063 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4064 if (rval && whichfork == XFS_DATA_FORK)
4065 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4070 xfs_bmap_sanity_check(
4071 struct xfs_mount *mp,
4075 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4077 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
4078 be16_to_cpu(block->bb_level) != level ||
4079 be16_to_cpu(block->bb_numrecs) == 0 ||
4080 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4086 * Read in the extents to if_extents.
4087 * All inode fields are set up by caller, we just traverse the btree
4088 * and copy the records in. If the file system cannot contain unwritten
4089 * extents, the records are checked for no "state" flags.
4092 xfs_bmap_read_extents(
4093 xfs_trans_t *tp, /* transaction pointer */
4094 xfs_inode_t *ip, /* incore inode */
4095 int whichfork) /* data or attr fork */
4097 struct xfs_btree_block *block; /* current btree block */
4098 xfs_fsblock_t bno; /* block # of "block" */
4099 xfs_buf_t *bp; /* buffer for "block" */
4100 int error; /* error return value */
4101 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4102 xfs_extnum_t i, j; /* index into the extents list */
4103 xfs_ifork_t *ifp; /* fork structure */
4104 int level; /* btree level, for checking */
4105 xfs_mount_t *mp; /* file system mount structure */
4106 __be64 *pp; /* pointer to block address */
4108 xfs_extnum_t room; /* number of entries there's room for */
4112 ifp = XFS_IFORK_PTR(ip, whichfork);
4113 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4114 XFS_EXTFMT_INODE(ip);
4115 block = ifp->if_broot;
4117 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4119 level = be16_to_cpu(block->bb_level);
4121 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4122 bno = be64_to_cpu(*pp);
4123 ASSERT(bno != NULLDFSBNO);
4124 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4125 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4127 * Go down the tree until leaf level is reached, following the first
4128 * pointer (leftmost) at each level.
4130 while (level-- > 0) {
4131 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4132 XFS_BMAP_BTREE_REF)))
4134 block = XFS_BUF_TO_BLOCK(bp);
4135 XFS_WANT_CORRUPTED_GOTO(
4136 xfs_bmap_sanity_check(mp, bp, level),
4140 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4141 bno = be64_to_cpu(*pp);
4142 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4143 xfs_trans_brelse(tp, bp);
4146 * Here with bp and block set to the leftmost leaf node in the tree.
4148 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4151 * Loop over all leaf nodes. Copy information to the extent records.
4154 xfs_bmbt_rec_t *frp;
4155 xfs_fsblock_t nextbno;
4156 xfs_extnum_t num_recs;
4159 num_recs = xfs_btree_get_numrecs(block);
4160 if (unlikely(i + num_recs > room)) {
4161 ASSERT(i + num_recs <= room);
4162 xfs_warn(ip->i_mount,
4163 "corrupt dinode %Lu, (btree extents).",
4164 (unsigned long long) ip->i_ino);
4165 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4166 XFS_ERRLEVEL_LOW, ip->i_mount, block);
4169 XFS_WANT_CORRUPTED_GOTO(
4170 xfs_bmap_sanity_check(mp, bp, 0),
4173 * Read-ahead the next leaf block, if any.
4175 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4176 if (nextbno != NULLFSBLOCK)
4177 xfs_btree_reada_bufl(mp, nextbno, 1);
4179 * Copy records into the extent records.
4181 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4183 for (j = 0; j < num_recs; j++, i++, frp++) {
4184 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4185 trp->l0 = be64_to_cpu(frp->l0);
4186 trp->l1 = be64_to_cpu(frp->l1);
4188 if (exntf == XFS_EXTFMT_NOSTATE) {
4190 * Check all attribute bmap btree records and
4191 * any "older" data bmap btree records for a
4192 * set bit in the "extent flag" position.
4194 if (unlikely(xfs_check_nostate_extents(ifp,
4195 start, num_recs))) {
4196 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4202 xfs_trans_brelse(tp, bp);
4205 * If we've reached the end, stop.
4207 if (bno == NULLFSBLOCK)
4209 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4210 XFS_BMAP_BTREE_REF)))
4212 block = XFS_BUF_TO_BLOCK(bp);
4214 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4215 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4216 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4219 xfs_trans_brelse(tp, bp);
4220 return XFS_ERROR(EFSCORRUPTED);
4225 * Add bmap trace insert entries for all the contents of the extent records.
4228 xfs_bmap_trace_exlist(
4229 xfs_inode_t *ip, /* incore inode pointer */
4230 xfs_extnum_t cnt, /* count of entries in the list */
4231 int whichfork, /* data or attr fork */
4232 unsigned long caller_ip)
4234 xfs_extnum_t idx; /* extent record index */
4235 xfs_ifork_t *ifp; /* inode fork pointer */
4238 if (whichfork == XFS_ATTR_FORK)
4239 state |= BMAP_ATTRFORK;
4241 ifp = XFS_IFORK_PTR(ip, whichfork);
4242 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4243 for (idx = 0; idx < cnt; idx++)
4244 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4248 * Validate that the bmbt_irecs being returned from bmapi are valid
4249 * given the callers original parameters. Specifically check the
4250 * ranges of the returned irecs to ensure that they only extent beyond
4251 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4254 xfs_bmap_validate_ret(
4258 xfs_bmbt_irec_t *mval,
4262 int i; /* index to map values */
4264 ASSERT(ret_nmap <= nmap);
4266 for (i = 0; i < ret_nmap; i++) {
4267 ASSERT(mval[i].br_blockcount > 0);
4268 if (!(flags & XFS_BMAPI_ENTIRE)) {
4269 ASSERT(mval[i].br_startoff >= bno);
4270 ASSERT(mval[i].br_blockcount <= len);
4271 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4274 ASSERT(mval[i].br_startoff < bno + len);
4275 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4279 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4280 mval[i].br_startoff);
4281 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4282 mval[i].br_startblock != HOLESTARTBLOCK);
4283 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4284 mval[i].br_state == XFS_EXT_UNWRITTEN);
4291 * Trim the returned map to the required bounds
4295 struct xfs_bmbt_irec *mval,
4296 struct xfs_bmbt_irec *got,
4304 if ((flags & XFS_BMAPI_ENTIRE) ||
4305 got->br_startoff + got->br_blockcount <= obno) {
4307 if (isnullstartblock(got->br_startblock))
4308 mval->br_startblock = DELAYSTARTBLOCK;
4314 ASSERT((*bno >= obno) || (n == 0));
4316 mval->br_startoff = *bno;
4317 if (isnullstartblock(got->br_startblock))
4318 mval->br_startblock = DELAYSTARTBLOCK;
4320 mval->br_startblock = got->br_startblock +
4321 (*bno - got->br_startoff);
4323 * Return the minimum of what we got and what we asked for for
4324 * the length. We can use the len variable here because it is
4325 * modified below and we could have been there before coming
4326 * here if the first part of the allocation didn't overlap what
4329 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4330 got->br_blockcount - (*bno - got->br_startoff));
4331 mval->br_state = got->br_state;
4332 ASSERT(mval->br_blockcount <= len);
4337 * Update and validate the extent map to return
4340 xfs_bmapi_update_map(
4341 struct xfs_bmbt_irec **map,
4349 xfs_bmbt_irec_t *mval = *map;
4351 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4352 ((mval->br_startoff + mval->br_blockcount) <= end));
4353 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4354 (mval->br_startoff < obno));
4356 *bno = mval->br_startoff + mval->br_blockcount;
4358 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4359 /* update previous map with new information */
4360 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4361 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4362 ASSERT(mval->br_state == mval[-1].br_state);
4363 mval[-1].br_blockcount = mval->br_blockcount;
4364 mval[-1].br_state = mval->br_state;
4365 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4366 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4367 mval[-1].br_startblock != HOLESTARTBLOCK &&
4368 mval->br_startblock == mval[-1].br_startblock +
4369 mval[-1].br_blockcount &&
4370 ((flags & XFS_BMAPI_IGSTATE) ||
4371 mval[-1].br_state == mval->br_state)) {
4372 ASSERT(mval->br_startoff ==
4373 mval[-1].br_startoff + mval[-1].br_blockcount);
4374 mval[-1].br_blockcount += mval->br_blockcount;
4375 } else if (*n > 0 &&
4376 mval->br_startblock == DELAYSTARTBLOCK &&
4377 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4378 mval->br_startoff ==
4379 mval[-1].br_startoff + mval[-1].br_blockcount) {
4380 mval[-1].br_blockcount += mval->br_blockcount;
4381 mval[-1].br_state = mval->br_state;
4382 } else if (!((*n == 0) &&
4383 ((mval->br_startoff + mval->br_blockcount) <=
4392 * Map file blocks to filesystem blocks without allocation.
4396 struct xfs_inode *ip,
4399 struct xfs_bmbt_irec *mval,
4403 struct xfs_mount *mp = ip->i_mount;
4404 struct xfs_ifork *ifp;
4405 struct xfs_bmbt_irec got;
4406 struct xfs_bmbt_irec prev;
4413 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4414 XFS_ATTR_FORK : XFS_DATA_FORK;
4417 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4418 XFS_BMAPI_IGSTATE)));
4420 if (unlikely(XFS_TEST_ERROR(
4421 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4422 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4423 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4424 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4425 return XFS_ERROR(EFSCORRUPTED);
4428 if (XFS_FORCED_SHUTDOWN(mp))
4429 return XFS_ERROR(EIO);
4431 XFS_STATS_INC(xs_blk_mapr);
4433 ifp = XFS_IFORK_PTR(ip, whichfork);
4434 ASSERT(ifp->if_ext_max ==
4435 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4437 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4438 error = xfs_iread_extents(NULL, ip, whichfork);
4443 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4447 while (bno < end && n < *nmap) {
4448 /* Reading past eof, act as though there's a hole up to end. */
4450 got.br_startoff = end;
4451 if (got.br_startoff > bno) {
4452 /* Reading in a hole. */
4453 mval->br_startoff = bno;
4454 mval->br_startblock = HOLESTARTBLOCK;
4455 mval->br_blockcount =
4456 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4457 mval->br_state = XFS_EXT_NORM;
4458 bno += mval->br_blockcount;
4459 len -= mval->br_blockcount;
4465 /* set up the extent map to return. */
4466 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4467 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4469 /* If we're done, stop now. */
4470 if (bno >= end || n >= *nmap)
4473 /* Else go on to the next record. */
4474 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4475 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4484 xfs_bmapi_reserve_delalloc(
4485 struct xfs_inode *ip,
4488 struct xfs_bmbt_irec *got,
4489 struct xfs_bmbt_irec *prev,
4490 xfs_extnum_t *lastx,
4493 struct xfs_mount *mp = ip->i_mount;
4494 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4496 xfs_extlen_t indlen;
4497 char rt = XFS_IS_REALTIME_INODE(ip);
4501 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4503 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4505 /* Figure out the extent size, adjust alen */
4506 extsz = xfs_get_extsz_hint(ip);
4509 * Make sure we don't exceed a single extent length when we
4510 * align the extent by reducing length we are going to
4511 * allocate by the maximum amount extent size aligment may
4514 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4515 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4516 1, 0, &aoff, &alen);
4521 extsz = alen / mp->m_sb.sb_rextsize;
4524 * Make a transaction-less quota reservation for delayed allocation
4525 * blocks. This number gets adjusted later. We return if we haven't
4526 * allocated blocks already inside this loop.
4528 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4529 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4534 * Split changing sb for alen and indlen since they could be coming
4535 * from different places.
4537 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4541 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4542 -((int64_t)extsz), 0);
4544 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4545 -((int64_t)alen), 0);
4549 goto out_unreserve_quota;
4551 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4552 -((int64_t)indlen), 0);
4554 goto out_unreserve_blocks;
4557 ip->i_delayed_blks += alen;
4559 got->br_startoff = aoff;
4560 got->br_startblock = nullstartblock(indlen);
4561 got->br_blockcount = alen;
4562 got->br_state = XFS_EXT_NORM;
4563 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4566 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4567 * might have merged it into one of the neighbouring ones.
4569 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4571 ASSERT(got->br_startoff <= aoff);
4572 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4573 ASSERT(isnullstartblock(got->br_startblock));
4574 ASSERT(got->br_state == XFS_EXT_NORM);
4577 out_unreserve_blocks:
4579 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4581 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4582 out_unreserve_quota:
4583 if (XFS_IS_QUOTA_ON(mp))
4584 xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ?
4585 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4590 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4594 struct xfs_inode *ip, /* incore inode */
4595 xfs_fileoff_t bno, /* starting file offs. mapped */
4596 xfs_filblks_t len, /* length to map in file */
4597 struct xfs_bmbt_irec *mval, /* output: map values */
4598 int *nmap, /* i/o: mval size/count */
4599 int flags) /* XFS_BMAPI_... */
4601 struct xfs_mount *mp = ip->i_mount;
4602 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4603 struct xfs_bmbt_irec got; /* current file extent record */
4604 struct xfs_bmbt_irec prev; /* previous file extent record */
4605 xfs_fileoff_t obno; /* old block number (offset) */
4606 xfs_fileoff_t end; /* end of mapped file region */
4607 xfs_extnum_t lastx; /* last useful extent number */
4608 int eof; /* we've hit the end of extents */
4609 int n = 0; /* current extent index */
4613 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4614 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4616 if (unlikely(XFS_TEST_ERROR(
4617 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4618 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4619 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4620 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4621 return XFS_ERROR(EFSCORRUPTED);
4624 if (XFS_FORCED_SHUTDOWN(mp))
4625 return XFS_ERROR(EIO);
4627 XFS_STATS_INC(xs_blk_mapw);
4629 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4630 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4635 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4639 while (bno < end && n < *nmap) {
4640 if (eof || got.br_startoff > bno) {
4641 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4642 &prev, &lastx, eof);
4652 /* set up the extent map to return. */
4653 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4654 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4656 /* If we're done, stop now. */
4657 if (bno >= end || n >= *nmap)
4660 /* Else go on to the next record. */
4662 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4663 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4675 struct xfs_bmalloca *bma,
4676 xfs_extnum_t *lastx,
4677 struct xfs_btree_cur **cur,
4678 xfs_fsblock_t *firstblock,
4679 struct xfs_bmap_free *flist,
4684 struct xfs_mount *mp = bma->ip->i_mount;
4685 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4686 XFS_ATTR_FORK : XFS_DATA_FORK;
4687 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4694 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4697 * For the wasdelay case, we could also just allocate the stuff asked
4698 * for in this bmap call but that wouldn't be as good.
4701 alen = (xfs_extlen_t)bma->gotp->br_blockcount;
4702 aoff = bma->gotp->br_startoff;
4703 if (*lastx != NULLEXTNUM && *lastx) {
4704 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx - 1),
4708 alen = (xfs_extlen_t)XFS_FILBLKS_MIN(bma->alen, MAXEXTLEN);
4710 alen = (xfs_extlen_t)XFS_FILBLKS_MIN(alen,
4711 bma->gotp->br_startoff - bma->off);
4716 * Indicate if this is the first user data in the file, or just any
4719 if (!(flags & XFS_BMAPI_METADATA)) {
4720 bma->userdata = (aoff == 0) ?
4721 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4725 * Fill in changeable bma fields.
4729 bma->firstblock = *firstblock;
4730 bma->minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4731 bma->low = flist->xbf_low;
4735 * Only want to do the alignment at the eof if it is userdata and
4736 * allocation length is larger than a stripe unit.
4738 if (mp->m_dalign && alen >= mp->m_dalign &&
4739 !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4740 error = xfs_bmap_isaeof(bma->ip, aoff, whichfork, &bma->aeof);
4745 error = xfs_bmap_alloc(bma);
4750 * Copy out result fields.
4753 flist->xbf_low = bma->low;
4756 ASSERT(*firstblock == NULLFSBLOCK ||
4757 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4758 XFS_FSB_TO_AGNO(mp, bma->firstblock) ||
4760 XFS_FSB_TO_AGNO(mp, *firstblock) <
4761 XFS_FSB_TO_AGNO(mp, bma->firstblock)));
4762 *firstblock = bma->firstblock;
4764 (*cur)->bc_private.b.firstblock = *firstblock;
4765 if (abno == NULLFSBLOCK)
4767 if ((ifp->if_flags & XFS_IFBROOT) && !*cur) {
4768 (*cur) = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4769 (*cur)->bc_private.b.firstblock = *firstblock;
4770 (*cur)->bc_private.b.flist = flist;
4773 * Bump the number of extents we've allocated
4779 (*cur)->bc_private.b.flags =
4780 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4782 bma->gotp->br_startoff = aoff;
4783 bma->gotp->br_startblock = abno;
4784 bma->gotp->br_blockcount = alen;
4785 bma->gotp->br_state = XFS_EXT_NORM;
4788 * A wasdelay extent has been initialized, so shouldn't be flagged
4791 if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) &&
4792 xfs_sb_version_hasextflgbit(&mp->m_sb))
4793 bma->gotp->br_state = XFS_EXT_UNWRITTEN;
4795 error = xfs_bmap_add_extent(bma->tp, bma->ip, lastx, cur, bma->gotp,
4796 firstblock, flist, logflags, whichfork);
4801 * Update our extent pointer, given that xfs_bmap_add_extent might
4802 * have merged it into one of the neighbouring ones.
4804 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), bma->gotp);
4806 ASSERT(bma->gotp->br_startoff <= aoff);
4807 ASSERT(bma->gotp->br_startoff + bma->gotp->br_blockcount >=
4809 ASSERT(bma->gotp->br_state == XFS_EXT_NORM ||
4810 bma->gotp->br_state == XFS_EXT_UNWRITTEN);
4815 xfs_bmapi_convert_unwritten(
4816 struct xfs_bmalloca *bma,
4817 struct xfs_bmbt_irec *mval,
4819 xfs_extnum_t *lastx,
4820 struct xfs_btree_cur **cur,
4821 xfs_fsblock_t *firstblock,
4822 struct xfs_bmap_free *flist,
4826 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4827 XFS_ATTR_FORK : XFS_DATA_FORK;
4828 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4833 /* check if we need to do unwritten->real conversion */
4834 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4835 (flags & XFS_BMAPI_PREALLOC))
4838 /* check if we need to do real->unwritten conversion */
4839 if (mval->br_state == XFS_EXT_NORM &&
4840 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4841 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4845 * Modify (by adding) the state flag, if writing.
4847 ASSERT(mval->br_blockcount <= len);
4848 if ((ifp->if_flags & XFS_IFBROOT) && !*cur) {
4849 *cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4850 bma->ip, whichfork);
4851 (*cur)->bc_private.b.firstblock = *firstblock;
4852 (*cur)->bc_private.b.flist = flist;
4854 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4855 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4857 error = xfs_bmap_add_extent(bma->tp, bma->ip, lastx, cur, mval,
4858 firstblock, flist, logflags, whichfork);
4863 * Update our extent pointer, given that xfs_bmap_add_extent might
4864 * have merged it into one of the neighbouring ones.
4866 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), bma->gotp);
4869 * We may have combined previously unwritten space with written space,
4870 * so generate another request.
4872 if (mval->br_blockcount < len)
4878 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4879 * extent state if necessary. Details behaviour is controlled by the flags
4880 * parameter. Only allocates blocks from a single allocation group, to avoid
4883 * The returned value in "firstblock" from the first call in a transaction
4884 * must be remembered and presented to subsequent calls in "firstblock".
4885 * An upper bound for the number of blocks to be allocated is supplied to
4886 * the first call in "total"; if no allocation group has that many free
4887 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4891 struct xfs_trans *tp, /* transaction pointer */
4892 struct xfs_inode *ip, /* incore inode */
4893 xfs_fileoff_t bno, /* starting file offs. mapped */
4894 xfs_filblks_t len, /* length to map in file */
4895 int flags, /* XFS_BMAPI_... */
4896 xfs_fsblock_t *firstblock, /* first allocated block
4897 controls a.g. for allocs */
4898 xfs_extlen_t total, /* total blocks needed */
4899 struct xfs_bmbt_irec *mval, /* output: map values */
4900 int *nmap, /* i/o: mval size/count */
4901 struct xfs_bmap_free *flist) /* i/o: list extents to free */
4903 struct xfs_mount *mp = ip->i_mount;
4904 struct xfs_ifork *ifp;
4905 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
4906 struct xfs_btree_cur *cur; /* bmap btree cursor */
4907 xfs_fileoff_t end; /* end of mapped file region */
4908 int eof; /* after the end of extents */
4909 int error; /* error return */
4910 struct xfs_bmbt_irec got; /* current file extent record */
4911 xfs_extnum_t lastx; /* last useful extent number */
4912 int logflags; /* flags for transaction logging */
4913 xfs_extlen_t minleft; /* min blocks left after allocation */
4914 int n; /* current extent index */
4915 int nallocs; /* number of extents alloc'd */
4916 xfs_fileoff_t obno; /* old block number (offset) */
4917 struct xfs_bmbt_irec prev; /* previous file extent record */
4918 int tmp_logflags; /* temp flags holder */
4919 int whichfork; /* data or attr fork */
4920 char inhole; /* current location is hole in file */
4921 char wasdelay; /* old extent was delayed */
4924 xfs_fileoff_t orig_bno; /* original block number value */
4925 int orig_flags; /* original flags arg value */
4926 xfs_filblks_t orig_len; /* original value of len arg */
4927 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4928 int orig_nmap; /* original value of *nmap */
4938 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4939 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4942 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4943 XFS_ATTR_FORK : XFS_DATA_FORK;
4945 if (unlikely(XFS_TEST_ERROR(
4946 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4947 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4948 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4949 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4950 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4951 return XFS_ERROR(EFSCORRUPTED);
4954 if (XFS_FORCED_SHUTDOWN(mp))
4955 return XFS_ERROR(EIO);
4957 ifp = XFS_IFORK_PTR(ip, whichfork);
4958 ASSERT(ifp->if_ext_max ==
4959 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4961 XFS_STATS_INC(xs_blk_mapw);
4967 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4968 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
4969 &logflags, whichfork);
4974 if (*firstblock == NULLFSBLOCK) {
4975 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4976 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4983 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4984 error = xfs_iread_extents(tp, ip, whichfork);
4989 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
5001 while (bno < end && n < *nmap) {
5002 inhole = eof || got.br_startoff > bno;
5003 wasdelay = !inhole && isnullstartblock(got.br_startblock);
5006 * First, deal with the hole before the allocated space
5007 * that we found, if any.
5009 if (inhole || wasdelay) {
5011 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5012 bma.wasdel = wasdelay;
5015 bma.minleft = minleft;
5017 error = xfs_bmapi_allocate(&bma, &lastx, &cur,
5018 firstblock, flist, flags, &nallocs,
5020 logflags |= tmp_logflags;
5023 if (flist && flist->xbf_low)
5025 if (bma.rval == NULLFSBLOCK)
5029 /* Deal with the allocated space we found. */
5030 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
5032 /* Execute unwritten extent conversion if necessary */
5033 error = xfs_bmapi_convert_unwritten(&bma, mval, len, &lastx,
5034 &cur, firstblock, flist,
5035 flags, &tmp_logflags);
5036 logflags |= tmp_logflags;
5037 if (error == EAGAIN)
5042 /* update the extent map to return */
5043 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5046 * If we're done, stop now. Stop when we've allocated
5047 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5048 * the transaction may get too big.
5050 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5053 /* Else go on to the next record. */
5055 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
5056 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
5063 * Transform from btree to extents, give it cur.
5065 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5066 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5068 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5069 &tmp_logflags, whichfork);
5070 logflags |= tmp_logflags;
5074 ASSERT(ifp->if_ext_max ==
5075 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5076 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5077 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5081 * Log everything. Do this after conversion, there's no point in
5082 * logging the extent records if we've converted to btree format.
5084 if ((logflags & xfs_ilog_fext(whichfork)) &&
5085 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5086 logflags &= ~xfs_ilog_fext(whichfork);
5087 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5088 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5089 logflags &= ~xfs_ilog_fbroot(whichfork);
5091 * Log whatever the flags say, even if error. Otherwise we might miss
5092 * detecting a case where the data is changed, there's an error,
5093 * and it's not logged so we don't shutdown when we should.
5096 xfs_trans_log_inode(tp, ip, logflags);
5100 ASSERT(*firstblock == NULLFSBLOCK ||
5101 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5103 cur->bc_private.b.firstblock) ||
5105 XFS_FSB_TO_AGNO(mp, *firstblock) <
5107 cur->bc_private.b.firstblock)));
5108 *firstblock = cur->bc_private.b.firstblock;
5110 xfs_btree_del_cursor(cur,
5111 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5114 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5120 * Unmap (remove) blocks from a file.
5121 * If nexts is nonzero then the number of extents to remove is limited to
5122 * that value. If not all extents in the block range can be removed then
5127 xfs_trans_t *tp, /* transaction pointer */
5128 struct xfs_inode *ip, /* incore inode */
5129 xfs_fileoff_t bno, /* starting offset to unmap */
5130 xfs_filblks_t len, /* length to unmap in file */
5131 int flags, /* misc flags */
5132 xfs_extnum_t nexts, /* number of extents max */
5133 xfs_fsblock_t *firstblock, /* first allocated block
5134 controls a.g. for allocs */
5135 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5136 int *done) /* set if not done yet */
5138 xfs_btree_cur_t *cur; /* bmap btree cursor */
5139 xfs_bmbt_irec_t del; /* extent being deleted */
5140 int eof; /* is deleting at eof */
5141 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5142 int error; /* error return value */
5143 xfs_extnum_t extno; /* extent number in list */
5144 xfs_bmbt_irec_t got; /* current extent record */
5145 xfs_ifork_t *ifp; /* inode fork pointer */
5146 int isrt; /* freeing in rt area */
5147 xfs_extnum_t lastx; /* last extent index used */
5148 int logflags; /* transaction logging flags */
5149 xfs_extlen_t mod; /* rt extent offset */
5150 xfs_mount_t *mp; /* mount structure */
5151 xfs_extnum_t nextents; /* number of file extents */
5152 xfs_bmbt_irec_t prev; /* previous extent record */
5153 xfs_fileoff_t start; /* first file offset deleted */
5154 int tmp_logflags; /* partial logging flags */
5155 int wasdel; /* was a delayed alloc extent */
5156 int whichfork; /* data or attribute fork */
5159 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5161 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5162 XFS_ATTR_FORK : XFS_DATA_FORK;
5163 ifp = XFS_IFORK_PTR(ip, whichfork);
5165 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5166 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5167 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5169 return XFS_ERROR(EFSCORRUPTED);
5172 if (XFS_FORCED_SHUTDOWN(mp))
5173 return XFS_ERROR(EIO);
5177 ASSERT(ifp->if_ext_max ==
5178 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5179 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5180 (error = xfs_iread_extents(tp, ip, whichfork)))
5182 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5183 if (nextents == 0) {
5187 XFS_STATS_INC(xs_blk_unmap);
5188 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5190 bno = start + len - 1;
5191 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5195 * Check to see if the given block number is past the end of the
5196 * file, back up to the last block if so...
5199 ep = xfs_iext_get_ext(ifp, --lastx);
5200 xfs_bmbt_get_all(ep, &got);
5201 bno = got.br_startoff + got.br_blockcount - 1;
5204 if (ifp->if_flags & XFS_IFBROOT) {
5205 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5206 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5207 cur->bc_private.b.firstblock = *firstblock;
5208 cur->bc_private.b.flist = flist;
5209 cur->bc_private.b.flags = 0;
5213 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5214 (nexts == 0 || extno < nexts)) {
5216 * Is the found extent after a hole in which bno lives?
5217 * Just back up to the previous extent, if so.
5219 if (got.br_startoff > bno) {
5222 ep = xfs_iext_get_ext(ifp, lastx);
5223 xfs_bmbt_get_all(ep, &got);
5226 * Is the last block of this extent before the range
5227 * we're supposed to delete? If so, we're done.
5229 bno = XFS_FILEOFF_MIN(bno,
5230 got.br_startoff + got.br_blockcount - 1);
5234 * Then deal with the (possibly delayed) allocated space
5239 wasdel = isnullstartblock(del.br_startblock);
5240 if (got.br_startoff < start) {
5241 del.br_startoff = start;
5242 del.br_blockcount -= start - got.br_startoff;
5244 del.br_startblock += start - got.br_startoff;
5246 if (del.br_startoff + del.br_blockcount > bno + 1)
5247 del.br_blockcount = bno + 1 - del.br_startoff;
5248 sum = del.br_startblock + del.br_blockcount;
5250 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5252 * Realtime extent not lined up at the end.
5253 * The extent could have been split into written
5254 * and unwritten pieces, or we could just be
5255 * unmapping part of it. But we can't really
5256 * get rid of part of a realtime extent.
5258 if (del.br_state == XFS_EXT_UNWRITTEN ||
5259 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5261 * This piece is unwritten, or we're not
5262 * using unwritten extents. Skip over it.
5265 bno -= mod > del.br_blockcount ?
5266 del.br_blockcount : mod;
5267 if (bno < got.br_startoff) {
5269 xfs_bmbt_get_all(xfs_iext_get_ext(
5275 * It's written, turn it unwritten.
5276 * This is better than zeroing it.
5278 ASSERT(del.br_state == XFS_EXT_NORM);
5279 ASSERT(xfs_trans_get_block_res(tp) > 0);
5281 * If this spans a realtime extent boundary,
5282 * chop it back to the start of the one we end at.
5284 if (del.br_blockcount > mod) {
5285 del.br_startoff += del.br_blockcount - mod;
5286 del.br_startblock += del.br_blockcount - mod;
5287 del.br_blockcount = mod;
5289 del.br_state = XFS_EXT_UNWRITTEN;
5290 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del,
5291 firstblock, flist, &logflags,
5297 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5299 * Realtime extent is lined up at the end but not
5300 * at the front. We'll get rid of full extents if
5303 mod = mp->m_sb.sb_rextsize - mod;
5304 if (del.br_blockcount > mod) {
5305 del.br_blockcount -= mod;
5306 del.br_startoff += mod;
5307 del.br_startblock += mod;
5308 } else if ((del.br_startoff == start &&
5309 (del.br_state == XFS_EXT_UNWRITTEN ||
5310 xfs_trans_get_block_res(tp) == 0)) ||
5311 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5313 * Can't make it unwritten. There isn't
5314 * a full extent here so just skip it.
5316 ASSERT(bno >= del.br_blockcount);
5317 bno -= del.br_blockcount;
5318 if (got.br_startoff > bno) {
5320 ep = xfs_iext_get_ext(ifp,
5322 xfs_bmbt_get_all(ep, &got);
5326 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5328 * This one is already unwritten.
5329 * It must have a written left neighbor.
5330 * Unwrite the killed part of that one and
5334 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5336 ASSERT(prev.br_state == XFS_EXT_NORM);
5337 ASSERT(!isnullstartblock(prev.br_startblock));
5338 ASSERT(del.br_startblock ==
5339 prev.br_startblock + prev.br_blockcount);
5340 if (prev.br_startoff < start) {
5341 mod = start - prev.br_startoff;
5342 prev.br_blockcount -= mod;
5343 prev.br_startblock += mod;
5344 prev.br_startoff = start;
5346 prev.br_state = XFS_EXT_UNWRITTEN;
5348 error = xfs_bmap_add_extent(tp, ip, &lastx,
5349 &cur, &prev, firstblock, flist,
5350 &logflags, XFS_DATA_FORK);
5355 ASSERT(del.br_state == XFS_EXT_NORM);
5356 del.br_state = XFS_EXT_UNWRITTEN;
5357 error = xfs_bmap_add_extent(tp, ip, &lastx,
5358 &cur, &del, firstblock, flist,
5359 &logflags, XFS_DATA_FORK);
5366 ASSERT(startblockval(del.br_startblock) > 0);
5367 /* Update realtime/data freespace, unreserve quota */
5369 xfs_filblks_t rtexts;
5371 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5372 do_div(rtexts, mp->m_sb.sb_rextsize);
5373 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5374 (int64_t)rtexts, 0);
5375 (void)xfs_trans_reserve_quota_nblks(NULL,
5376 ip, -((long)del.br_blockcount), 0,
5377 XFS_QMOPT_RES_RTBLKS);
5379 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5380 (int64_t)del.br_blockcount, 0);
5381 (void)xfs_trans_reserve_quota_nblks(NULL,
5382 ip, -((long)del.br_blockcount), 0,
5383 XFS_QMOPT_RES_REGBLKS);
5385 ip->i_delayed_blks -= del.br_blockcount;
5387 cur->bc_private.b.flags |=
5388 XFS_BTCUR_BPRV_WASDEL;
5390 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5392 * If it's the case where the directory code is running
5393 * with no block reservation, and the deleted block is in
5394 * the middle of its extent, and the resulting insert
5395 * of an extent would cause transformation to btree format,
5396 * then reject it. The calling code will then swap
5397 * blocks around instead.
5398 * We have to do this now, rather than waiting for the
5399 * conversion to btree format, since the transaction
5402 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5403 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5404 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5405 del.br_startoff > got.br_startoff &&
5406 del.br_startoff + del.br_blockcount <
5407 got.br_startoff + got.br_blockcount) {
5408 error = XFS_ERROR(ENOSPC);
5411 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5412 &tmp_logflags, whichfork);
5413 logflags |= tmp_logflags;
5416 bno = del.br_startoff - 1;
5419 * If not done go on to the next (previous) record.
5421 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5423 ep = xfs_iext_get_ext(ifp, lastx);
5424 if (xfs_bmbt_get_startoff(ep) > bno) {
5426 ep = xfs_iext_get_ext(ifp,
5429 xfs_bmbt_get_all(ep, &got);
5434 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5435 ASSERT(ifp->if_ext_max ==
5436 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5438 * Convert to a btree if necessary.
5440 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5441 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5442 ASSERT(cur == NULL);
5443 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5444 &cur, 0, &tmp_logflags, whichfork);
5445 logflags |= tmp_logflags;
5450 * transform from btree to extents, give it cur
5452 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5453 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5454 ASSERT(cur != NULL);
5455 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5457 logflags |= tmp_logflags;
5462 * transform from extents to local?
5464 ASSERT(ifp->if_ext_max ==
5465 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5469 * Log everything. Do this after conversion, there's no point in
5470 * logging the extent records if we've converted to btree format.
5472 if ((logflags & xfs_ilog_fext(whichfork)) &&
5473 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5474 logflags &= ~xfs_ilog_fext(whichfork);
5475 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5476 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5477 logflags &= ~xfs_ilog_fbroot(whichfork);
5479 * Log inode even in the error case, if the transaction
5480 * is dirty we'll need to shut down the filesystem.
5483 xfs_trans_log_inode(tp, ip, logflags);
5486 *firstblock = cur->bc_private.b.firstblock;
5487 cur->bc_private.b.allocated = 0;
5489 xfs_btree_del_cursor(cur,
5490 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5496 * returns 1 for success, 0 if we failed to map the extent.
5499 xfs_getbmapx_fix_eof_hole(
5500 xfs_inode_t *ip, /* xfs incore inode pointer */
5501 struct getbmapx *out, /* output structure */
5502 int prealloced, /* this is a file with
5503 * preallocated data space */
5504 __int64_t end, /* last block requested */
5505 xfs_fsblock_t startblock)
5508 xfs_mount_t *mp; /* file system mount point */
5509 xfs_ifork_t *ifp; /* inode fork pointer */
5510 xfs_extnum_t lastx; /* last extent pointer */
5511 xfs_fileoff_t fileblock;
5513 if (startblock == HOLESTARTBLOCK) {
5515 out->bmv_block = -1;
5516 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5517 fixlen -= out->bmv_offset;
5518 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5519 /* Came to hole at EOF. Trim it. */
5522 out->bmv_length = fixlen;
5525 if (startblock == DELAYSTARTBLOCK)
5526 out->bmv_block = -2;
5528 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5529 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5530 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5531 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5532 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5533 out->bmv_oflags |= BMV_OF_LAST;
5540 * Get inode's extents as described in bmv, and format for output.
5541 * Calls formatter to fill the user's buffer until all extents
5542 * are mapped, until the passed-in bmv->bmv_count slots have
5543 * been filled, or until the formatter short-circuits the loop,
5544 * if it is tracking filled-in extents on its own.
5546 int /* error code */
5549 struct getbmapx *bmv, /* user bmap structure */
5550 xfs_bmap_format_t formatter, /* format to user */
5551 void *arg) /* formatter arg */
5553 __int64_t bmvend; /* last block requested */
5554 int error = 0; /* return value */
5555 __int64_t fixlen; /* length for -1 case */
5556 int i; /* extent number */
5557 int lock; /* lock state */
5558 xfs_bmbt_irec_t *map; /* buffer for user's data */
5559 xfs_mount_t *mp; /* file system mount point */
5560 int nex; /* # of user extents can do */
5561 int nexleft; /* # of user extents left */
5562 int subnex; /* # of bmapi's can do */
5563 int nmap; /* number of map entries */
5564 struct getbmapx *out; /* output structure */
5565 int whichfork; /* data or attr fork */
5566 int prealloced; /* this is a file with
5567 * preallocated data space */
5568 int iflags; /* interface flags */
5569 int bmapi_flags; /* flags for xfs_bmapi */
5573 iflags = bmv->bmv_iflags;
5574 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5576 if (whichfork == XFS_ATTR_FORK) {
5577 if (XFS_IFORK_Q(ip)) {
5578 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5579 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5580 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5581 return XFS_ERROR(EINVAL);
5582 } else if (unlikely(
5583 ip->i_d.di_aformat != 0 &&
5584 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5585 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5587 return XFS_ERROR(EFSCORRUPTED);
5593 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5594 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5595 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5596 return XFS_ERROR(EINVAL);
5598 if (xfs_get_extsz_hint(ip) ||
5599 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5601 fixlen = XFS_MAXIOFFSET(mp);
5604 fixlen = ip->i_size;
5608 if (bmv->bmv_length == -1) {
5609 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5611 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5612 } else if (bmv->bmv_length == 0) {
5613 bmv->bmv_entries = 0;
5615 } else if (bmv->bmv_length < 0) {
5616 return XFS_ERROR(EINVAL);
5619 nex = bmv->bmv_count - 1;
5621 return XFS_ERROR(EINVAL);
5622 bmvend = bmv->bmv_offset + bmv->bmv_length;
5625 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5626 return XFS_ERROR(ENOMEM);
5627 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5629 return XFS_ERROR(ENOMEM);
5631 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5632 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5633 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5634 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5636 goto out_unlock_iolock;
5639 * even after flushing the inode, there can still be delalloc
5640 * blocks on the inode beyond EOF due to speculative
5641 * preallocation. These are not removed until the release
5642 * function is called or the inode is inactivated. Hence we
5643 * cannot assert here that ip->i_delayed_blks == 0.
5647 lock = xfs_ilock_map_shared(ip);
5650 * Don't let nex be bigger than the number of extents
5651 * we can have assuming alternating holes and real extents.
5653 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5654 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5656 bmapi_flags = xfs_bmapi_aflag(whichfork);
5657 if (!(iflags & BMV_IF_PREALLOC))
5658 bmapi_flags |= XFS_BMAPI_IGSTATE;
5661 * Allocate enough space to handle "subnex" maps at a time.
5665 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5667 goto out_unlock_ilock;
5669 bmv->bmv_entries = 0;
5671 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5672 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5680 nmap = (nexleft > subnex) ? subnex : nexleft;
5681 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5682 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5683 map, &nmap, bmapi_flags);
5686 ASSERT(nmap <= subnex);
5688 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5689 out[cur_ext].bmv_oflags = 0;
5690 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5691 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5692 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5693 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5694 out[cur_ext].bmv_offset =
5695 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5696 out[cur_ext].bmv_length =
5697 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5698 out[cur_ext].bmv_unused1 = 0;
5699 out[cur_ext].bmv_unused2 = 0;
5700 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5701 (map[i].br_startblock != DELAYSTARTBLOCK));
5702 if (map[i].br_startblock == HOLESTARTBLOCK &&
5703 whichfork == XFS_ATTR_FORK) {
5704 /* came to the end of attribute fork */
5705 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5709 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5711 map[i].br_startblock))
5715 out[cur_ext].bmv_offset +
5716 out[cur_ext].bmv_length;
5718 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5721 * In case we don't want to return the hole,
5722 * don't increase cur_ext so that we can reuse
5723 * it in the next loop.
5725 if ((iflags & BMV_IF_NO_HOLES) &&
5726 map[i].br_startblock == HOLESTARTBLOCK) {
5727 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5735 } while (nmap && nexleft && bmv->bmv_length);
5740 xfs_iunlock_map_shared(ip, lock);
5742 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5744 for (i = 0; i < cur_ext; i++) {
5745 int full = 0; /* user array is full */
5747 /* format results & advance arg */
5748 error = formatter(&arg, &out[i], &full);
5758 STATIC struct xfs_buf *
5760 struct xfs_btree_cur *cur,
5763 struct xfs_log_item_desc *lidp;
5769 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5770 if (!cur->bc_bufs[i])
5772 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5773 return cur->bc_bufs[i];
5776 /* Chase down all the log items to see if the bp is there */
5777 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5778 struct xfs_buf_log_item *bip;
5779 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5780 if (bip->bli_item.li_type == XFS_LI_BUF &&
5781 XFS_BUF_ADDR(bip->bli_buf) == bno)
5782 return bip->bli_buf;
5790 struct xfs_btree_block *block,
5796 __be64 *pp, *thispa; /* pointer to block address */
5797 xfs_bmbt_key_t *prevp, *keyp;
5799 ASSERT(be16_to_cpu(block->bb_level) > 0);
5802 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5803 dmxr = mp->m_bmap_dmxr[0];
5804 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5807 ASSERT(be64_to_cpu(prevp->br_startoff) <
5808 be64_to_cpu(keyp->br_startoff));
5813 * Compare the block numbers to see if there are dups.
5816 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5818 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5820 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5822 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5824 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5825 if (*thispa == *pp) {
5826 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
5828 (unsigned long long)be64_to_cpu(*thispa));
5829 panic("%s: ptrs are equal in node\n",
5837 * Check that the extents for the inode ip are in the right order in all
5842 xfs_bmap_check_leaf_extents(
5843 xfs_btree_cur_t *cur, /* btree cursor or null */
5844 xfs_inode_t *ip, /* incore inode pointer */
5845 int whichfork) /* data or attr fork */
5847 struct xfs_btree_block *block; /* current btree block */
5848 xfs_fsblock_t bno; /* block # of "block" */
5849 xfs_buf_t *bp; /* buffer for "block" */
5850 int error; /* error return value */
5851 xfs_extnum_t i=0, j; /* index into the extents list */
5852 xfs_ifork_t *ifp; /* fork structure */
5853 int level; /* btree level, for checking */
5854 xfs_mount_t *mp; /* file system mount structure */
5855 __be64 *pp; /* pointer to block address */
5856 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5857 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5858 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5861 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5867 ifp = XFS_IFORK_PTR(ip, whichfork);
5868 block = ifp->if_broot;
5870 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5872 level = be16_to_cpu(block->bb_level);
5874 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5875 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5876 bno = be64_to_cpu(*pp);
5878 ASSERT(bno != NULLDFSBNO);
5879 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5880 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5883 * Go down the tree until leaf level is reached, following the first
5884 * pointer (leftmost) at each level.
5886 while (level-- > 0) {
5887 /* See if buf is in cur first */
5888 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5894 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5895 XFS_BMAP_BTREE_REF)))
5897 block = XFS_BUF_TO_BLOCK(bp);
5898 XFS_WANT_CORRUPTED_GOTO(
5899 xfs_bmap_sanity_check(mp, bp, level),
5905 * Check this block for basic sanity (increasing keys and
5906 * no duplicate blocks).
5909 xfs_check_block(block, mp, 0, 0);
5910 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5911 bno = be64_to_cpu(*pp);
5912 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5915 xfs_trans_brelse(NULL, bp);
5920 * Here with bp and block set to the leftmost leaf node in the tree.
5925 * Loop over all leaf nodes checking that all extents are in the right order.
5928 xfs_fsblock_t nextbno;
5929 xfs_extnum_t num_recs;
5932 num_recs = xfs_btree_get_numrecs(block);
5935 * Read-ahead the next leaf block, if any.
5938 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5941 * Check all the extents to make sure they are OK.
5942 * If we had a previous block, the last entry should
5943 * conform with the first entry in this one.
5946 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5948 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5949 xfs_bmbt_disk_get_blockcount(&last) <=
5950 xfs_bmbt_disk_get_startoff(ep));
5952 for (j = 1; j < num_recs; j++) {
5953 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5954 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5955 xfs_bmbt_disk_get_blockcount(ep) <=
5956 xfs_bmbt_disk_get_startoff(nextp));
5964 xfs_trans_brelse(NULL, bp);
5968 * If we've reached the end, stop.
5970 if (bno == NULLFSBLOCK)
5973 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5979 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5980 XFS_BMAP_BTREE_REF)))
5982 block = XFS_BUF_TO_BLOCK(bp);
5986 xfs_trans_brelse(NULL, bp);
5991 xfs_warn(mp, "%s: at error0", __func__);
5993 xfs_trans_brelse(NULL, bp);
5995 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
5997 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6003 * Count fsblocks of the given fork.
6006 xfs_bmap_count_blocks(
6007 xfs_trans_t *tp, /* transaction pointer */
6008 xfs_inode_t *ip, /* incore inode */
6009 int whichfork, /* data or attr fork */
6010 int *count) /* out: count of blocks */
6012 struct xfs_btree_block *block; /* current btree block */
6013 xfs_fsblock_t bno; /* block # of "block" */
6014 xfs_ifork_t *ifp; /* fork structure */
6015 int level; /* btree level, for checking */
6016 xfs_mount_t *mp; /* file system mount structure */
6017 __be64 *pp; /* pointer to block address */
6021 ifp = XFS_IFORK_PTR(ip, whichfork);
6022 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6023 xfs_bmap_count_leaves(ifp, 0,
6024 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6030 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6032 block = ifp->if_broot;
6033 level = be16_to_cpu(block->bb_level);
6035 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6036 bno = be64_to_cpu(*pp);
6037 ASSERT(bno != NULLDFSBNO);
6038 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6039 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6041 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6042 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6044 return XFS_ERROR(EFSCORRUPTED);
6051 * Recursively walks each level of a btree
6052 * to count total fsblocks is use.
6054 STATIC int /* error */
6055 xfs_bmap_count_tree(
6056 xfs_mount_t *mp, /* file system mount point */
6057 xfs_trans_t *tp, /* transaction pointer */
6058 xfs_ifork_t *ifp, /* inode fork pointer */
6059 xfs_fsblock_t blockno, /* file system block number */
6060 int levelin, /* level in btree */
6061 int *count) /* Count of blocks */
6064 xfs_buf_t *bp, *nbp;
6065 int level = levelin;
6067 xfs_fsblock_t bno = blockno;
6068 xfs_fsblock_t nextbno;
6069 struct xfs_btree_block *block, *nextblock;
6072 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6075 block = XFS_BUF_TO_BLOCK(bp);
6078 /* Not at node above leaves, count this level of nodes */
6079 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6080 while (nextbno != NULLFSBLOCK) {
6081 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6082 0, &nbp, XFS_BMAP_BTREE_REF)))
6085 nextblock = XFS_BUF_TO_BLOCK(nbp);
6086 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6087 xfs_trans_brelse(tp, nbp);
6090 /* Dive to the next level */
6091 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6092 bno = be64_to_cpu(*pp);
6093 if (unlikely((error =
6094 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6095 xfs_trans_brelse(tp, bp);
6096 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6097 XFS_ERRLEVEL_LOW, mp);
6098 return XFS_ERROR(EFSCORRUPTED);
6100 xfs_trans_brelse(tp, bp);
6102 /* count all level 1 nodes and their leaves */
6104 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6105 numrecs = be16_to_cpu(block->bb_numrecs);
6106 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6107 xfs_trans_brelse(tp, bp);
6108 if (nextbno == NULLFSBLOCK)
6111 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6112 XFS_BMAP_BTREE_REF)))
6115 block = XFS_BUF_TO_BLOCK(bp);
6122 * Count leaf blocks given a range of extent records.
6125 xfs_bmap_count_leaves(
6133 for (b = 0; b < numrecs; b++) {
6134 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6135 *count += xfs_bmbt_get_blockcount(frp);
6140 * Count leaf blocks given a range of extent records originally
6144 xfs_bmap_disk_count_leaves(
6145 struct xfs_mount *mp,
6146 struct xfs_btree_block *block,
6151 xfs_bmbt_rec_t *frp;
6153 for (b = 1; b <= numrecs; b++) {
6154 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6155 *count += xfs_bmbt_disk_get_blockcount(frp);
6160 * dead simple method of punching delalyed allocation blocks from a range in
6161 * the inode. Walks a block at a time so will be slow, but is only executed in
6162 * rare error cases so the overhead is not critical. This will alays punch out
6163 * both the start and end blocks, even if the ranges only partially overlap
6164 * them, so it is up to the caller to ensure that partial blocks are not
6168 xfs_bmap_punch_delalloc_range(
6169 struct xfs_inode *ip,
6170 xfs_fileoff_t start_fsb,
6171 xfs_fileoff_t length)
6173 xfs_fileoff_t remaining = length;
6176 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6180 xfs_bmbt_irec_t imap;
6182 xfs_fsblock_t firstblock;
6183 xfs_bmap_free_t flist;
6186 * Map the range first and check that it is a delalloc extent
6187 * before trying to unmap the range. Otherwise we will be
6188 * trying to remove a real extent (which requires a
6189 * transaction) or a hole, which is probably a bad idea...
6191 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6195 /* something screwed, just bail */
6196 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6197 xfs_alert(ip->i_mount,
6198 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6199 ip->i_ino, start_fsb);
6207 if (imap.br_startblock != DELAYSTARTBLOCK) {
6208 /* been converted, ignore */
6211 WARN_ON(imap.br_blockcount == 0);
6214 * Note: while we initialise the firstblock/flist pair, they
6215 * should never be used because blocks should never be
6216 * allocated or freed for a delalloc extent and hence we need
6217 * don't cancel or finish them after the xfs_bunmapi() call.
6219 xfs_bmap_init(&flist, &firstblock);
6220 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6225 ASSERT(!flist.xbf_count && !flist.xbf_first);
6229 } while(remaining > 0);