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_dir2_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_btree.h"
36 #include "xfs_mount.h"
37 #include "xfs_itable.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_inode_item.h"
42 #include "xfs_extfree_item.h"
43 #include "xfs_alloc.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_attr_leaf.h"
49 #include "xfs_quota.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_filestream.h"
53 #include "xfs_vnodeops.h"
54 #include "xfs_trace.h"
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmap_add_extent to handle cases converting a delayed
93 * allocation to a real allocation.
95 STATIC int /* error */
96 xfs_bmap_add_extent_delay_real(
97 struct xfs_trans *tp, /* transaction pointer */
98 xfs_inode_t *ip, /* incore inode pointer */
99 xfs_extnum_t *idx, /* extent number to update/insert */
100 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
101 xfs_bmbt_irec_t *new, /* new data to add to file extents */
102 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
103 xfs_fsblock_t *first, /* pointer to firstblock variable */
104 xfs_bmap_free_t *flist, /* list of extents to be freed */
105 int *logflagsp); /* inode logging flags */
108 * Called by xfs_bmap_add_extent to handle cases converting a hole
109 * to a delayed allocation.
111 STATIC int /* error */
112 xfs_bmap_add_extent_hole_delay(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t *idx, /* extent number to update/insert */
115 xfs_bmbt_irec_t *new, /* new data to add to file extents */
116 int *logflagsp); /* inode logging flags */
119 * Called by xfs_bmap_add_extent to handle cases converting a hole
120 * to a real allocation.
122 STATIC int /* error */
123 xfs_bmap_add_extent_hole_real(
124 xfs_inode_t *ip, /* incore inode pointer */
125 xfs_extnum_t *idx, /* extent number to update/insert */
126 xfs_btree_cur_t *cur, /* if null, not a btree */
127 xfs_bmbt_irec_t *new, /* new data to add to file extents */
128 int *logflagsp, /* inode logging flags */
129 int whichfork); /* data or attr fork */
132 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
133 * allocation to a real allocation or vice versa.
135 STATIC int /* error */
136 xfs_bmap_add_extent_unwritten_real(
137 xfs_inode_t *ip, /* incore inode pointer */
138 xfs_extnum_t *idx, /* extent number to update/insert */
139 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
140 xfs_bmbt_irec_t *new, /* new data to add to file extents */
141 int *logflagsp); /* inode logging flags */
144 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
145 * It figures out where to ask the underlying allocator to put the new extent.
147 STATIC int /* error */
149 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
152 * Transform a btree format file with only one leaf node, where the
153 * extents list will fit in the inode, into an extents format file.
154 * Since the file extents are already in-core, all we have to do is
155 * give up the space for the btree root and pitch the leaf block.
157 STATIC int /* error */
158 xfs_bmap_btree_to_extents(
159 xfs_trans_t *tp, /* transaction pointer */
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_btree_cur_t *cur, /* btree cursor */
162 int *logflagsp, /* inode logging flags */
163 int whichfork); /* data or attr fork */
166 * Remove the entry "free" from the free item list. Prev points to the
167 * previous entry, unless "free" is the head of the list.
171 xfs_bmap_free_t *flist, /* free item list header */
172 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
173 xfs_bmap_free_item_t *free); /* list item to be freed */
176 * Convert an extents-format file into a btree-format file.
177 * The new file will have a root block (in the inode) and a single child block.
179 STATIC int /* error */
180 xfs_bmap_extents_to_btree(
181 xfs_trans_t *tp, /* transaction pointer */
182 xfs_inode_t *ip, /* incore inode pointer */
183 xfs_fsblock_t *firstblock, /* first-block-allocated */
184 xfs_bmap_free_t *flist, /* blocks freed in xaction */
185 xfs_btree_cur_t **curp, /* cursor returned to caller */
186 int wasdel, /* converting a delayed alloc */
187 int *logflagsp, /* inode logging flags */
188 int whichfork); /* data or attr fork */
191 * Convert a local file to an extents file.
192 * This code is sort of bogus, since the file data needs to get
193 * logged so it won't be lost. The bmap-level manipulations are ok, though.
195 STATIC int /* error */
196 xfs_bmap_local_to_extents(
197 xfs_trans_t *tp, /* transaction pointer */
198 xfs_inode_t *ip, /* incore inode pointer */
199 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
200 xfs_extlen_t total, /* total blocks needed by transaction */
201 int *logflagsp, /* inode logging flags */
202 int whichfork); /* data or attr fork */
205 * Search the extents list for the inode, for the extent containing bno.
206 * If bno lies in a hole, point to the next entry. If bno lies past eof,
207 * *eofp will be set, and *prevp will contain the last entry (null if none).
208 * Else, *lastxp will be set to the index of the found
209 * entry; *gotp will contain the entry.
211 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
212 xfs_bmap_search_extents(
213 xfs_inode_t *ip, /* incore inode pointer */
214 xfs_fileoff_t bno, /* block number searched for */
215 int whichfork, /* data or attr fork */
216 int *eofp, /* out: end of file found */
217 xfs_extnum_t *lastxp, /* out: last extent index */
218 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
219 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
222 * Check the last inode extent to determine whether this allocation will result
223 * in blocks being allocated at the end of the file. When we allocate new data
224 * blocks at the end of the file which do not start at the previous data block,
225 * we will try to align the new blocks at stripe unit boundaries.
227 STATIC int /* error */
229 xfs_inode_t *ip, /* incore inode pointer */
230 xfs_fileoff_t off, /* file offset in fsblocks */
231 int whichfork, /* data or attribute fork */
232 char *aeof); /* return value */
235 * Compute the worst-case number of indirect blocks that will be used
236 * for ip's delayed extent of length "len".
239 xfs_bmap_worst_indlen(
240 xfs_inode_t *ip, /* incore inode pointer */
241 xfs_filblks_t len); /* delayed extent length */
245 * Perform various validation checks on the values being returned
249 xfs_bmap_validate_ret(
253 xfs_bmbt_irec_t *mval,
257 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
265 xfs_fsblock_t blockno,
270 xfs_bmap_count_leaves(
277 xfs_bmap_disk_count_leaves(
278 struct xfs_mount *mp,
279 struct xfs_btree_block *block,
284 * Bmap internal routines.
287 STATIC int /* error */
289 struct xfs_btree_cur *cur,
293 int *stat) /* success/failure */
295 cur->bc_rec.b.br_startoff = off;
296 cur->bc_rec.b.br_startblock = bno;
297 cur->bc_rec.b.br_blockcount = len;
298 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
301 STATIC int /* error */
303 struct xfs_btree_cur *cur,
307 int *stat) /* success/failure */
309 cur->bc_rec.b.br_startoff = off;
310 cur->bc_rec.b.br_startblock = bno;
311 cur->bc_rec.b.br_blockcount = len;
312 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
316 * Update the record referred to by cur to the value given
317 * by [off, bno, len, state].
318 * This either works (return 0) or gets an EFSCORRUPTED error.
322 struct xfs_btree_cur *cur,
328 union xfs_btree_rec rec;
330 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
331 return xfs_btree_update(cur, &rec);
335 * Called from xfs_bmap_add_attrfork to handle btree format files.
337 STATIC int /* error */
338 xfs_bmap_add_attrfork_btree(
339 xfs_trans_t *tp, /* transaction pointer */
340 xfs_inode_t *ip, /* incore inode pointer */
341 xfs_fsblock_t *firstblock, /* first block allocated */
342 xfs_bmap_free_t *flist, /* blocks to free at commit */
343 int *flags) /* inode logging flags */
345 xfs_btree_cur_t *cur; /* btree cursor */
346 int error; /* error return value */
347 xfs_mount_t *mp; /* file system mount struct */
348 int stat; /* newroot status */
351 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
352 *flags |= XFS_ILOG_DBROOT;
354 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
355 cur->bc_private.b.flist = flist;
356 cur->bc_private.b.firstblock = *firstblock;
357 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
359 /* must be at least one entry */
360 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
361 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
364 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
365 return XFS_ERROR(ENOSPC);
367 *firstblock = cur->bc_private.b.firstblock;
368 cur->bc_private.b.allocated = 0;
369 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
373 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
378 * Called from xfs_bmap_add_attrfork to handle extents format files.
380 STATIC int /* error */
381 xfs_bmap_add_attrfork_extents(
382 xfs_trans_t *tp, /* transaction pointer */
383 xfs_inode_t *ip, /* incore inode pointer */
384 xfs_fsblock_t *firstblock, /* first block allocated */
385 xfs_bmap_free_t *flist, /* blocks to free at commit */
386 int *flags) /* inode logging flags */
388 xfs_btree_cur_t *cur; /* bmap btree cursor */
389 int error; /* error return value */
391 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
394 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
395 flags, XFS_DATA_FORK);
397 cur->bc_private.b.allocated = 0;
398 xfs_btree_del_cursor(cur,
399 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
405 * Called from xfs_bmap_add_attrfork to handle local format files.
407 STATIC int /* error */
408 xfs_bmap_add_attrfork_local(
409 xfs_trans_t *tp, /* transaction pointer */
410 xfs_inode_t *ip, /* incore inode pointer */
411 xfs_fsblock_t *firstblock, /* first block allocated */
412 xfs_bmap_free_t *flist, /* blocks to free at commit */
413 int *flags) /* inode logging flags */
415 xfs_da_args_t dargs; /* args for dir/attr code */
416 int error; /* error return value */
417 xfs_mount_t *mp; /* mount structure pointer */
419 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
421 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
423 memset(&dargs, 0, sizeof(dargs));
425 dargs.firstblock = firstblock;
427 dargs.total = mp->m_dirblkfsbs;
428 dargs.whichfork = XFS_DATA_FORK;
430 error = xfs_dir2_sf_to_block(&dargs);
432 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
438 * Called by xfs_bmapi to update file extent records and the btree
439 * after allocating space (or doing a delayed allocation).
441 STATIC int /* error */
443 struct xfs_trans *tp, /* transaction pointer */
444 xfs_inode_t *ip, /* incore inode pointer */
445 xfs_extnum_t *idx, /* extent number to update/insert */
446 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
447 xfs_bmbt_irec_t *new, /* new data to add to file extents */
448 xfs_fsblock_t *first, /* pointer to firstblock variable */
449 xfs_bmap_free_t *flist, /* list of extents to be freed */
450 int *logflagsp, /* inode logging flags */
451 int whichfork) /* data or attr fork */
453 xfs_btree_cur_t *cur; /* btree cursor or null */
454 xfs_filblks_t da_new; /* new count del alloc blocks used */
455 xfs_filblks_t da_old; /* old count del alloc blocks used */
456 int error; /* error return value */
457 xfs_ifork_t *ifp; /* inode fork ptr */
458 int logflags; /* returned value */
459 xfs_extnum_t nextents; /* number of extents in file now */
461 XFS_STATS_INC(xs_add_exlist);
464 ifp = XFS_IFORK_PTR(ip, whichfork);
465 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
470 ASSERT(*idx <= nextents);
473 * This is the first extent added to a new/empty file.
474 * Special case this one, so other routines get to assume there are
475 * already extents in the list.
478 xfs_iext_insert(ip, *idx, 1, new,
479 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
483 if (!isnullstartblock(new->br_startblock)) {
484 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
485 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
490 * Any kind of new delayed allocation goes here.
492 else if (isnullstartblock(new->br_startblock)) {
494 ASSERT((cur->bc_private.b.flags &
495 XFS_BTCUR_BPRV_WASDEL) == 0);
496 error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
500 * Real allocation off the end of the file.
502 else if (*idx == nextents) {
504 ASSERT((cur->bc_private.b.flags &
505 XFS_BTCUR_BPRV_WASDEL) == 0);
506 error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
507 &logflags, whichfork);
509 xfs_bmbt_irec_t prev; /* old extent at offset idx */
512 * Get the record referred to by idx.
514 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev);
516 * If it's a real allocation record, and the new allocation ends
517 * after the start of the referred to record, then we're filling
518 * in a delayed or unwritten allocation with a real one, or
519 * converting real back to unwritten.
521 if (!isnullstartblock(new->br_startblock) &&
522 new->br_startoff + new->br_blockcount > prev.br_startoff) {
523 if (prev.br_state != XFS_EXT_UNWRITTEN &&
524 isnullstartblock(prev.br_startblock)) {
525 da_old = startblockval(prev.br_startblock);
527 ASSERT(cur->bc_private.b.flags &
528 XFS_BTCUR_BPRV_WASDEL);
529 error = xfs_bmap_add_extent_delay_real(tp, ip,
530 idx, &cur, new, &da_new,
531 first, flist, &logflags);
533 ASSERT(new->br_state == XFS_EXT_NORM ||
534 new->br_state == XFS_EXT_UNWRITTEN);
536 error = xfs_bmap_add_extent_unwritten_real(ip,
537 idx, &cur, new, &logflags);
543 * Otherwise we're filling in a hole with an allocation.
547 ASSERT((cur->bc_private.b.flags &
548 XFS_BTCUR_BPRV_WASDEL) == 0);
549 error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
550 new, &logflags, whichfork);
556 ASSERT(*curp == cur || *curp == NULL);
559 * Convert to a btree if necessary.
561 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
562 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
563 int tmp_logflags; /* partial log flag return val */
566 error = xfs_bmap_extents_to_btree(tp, ip, first,
567 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
568 logflags |= tmp_logflags;
573 * Adjust for changes in reserved delayed indirect blocks.
574 * Nothing to do for disk quotas here.
576 if (da_old || da_new) {
581 nblks += cur->bc_private.b.allocated;
582 ASSERT(nblks <= da_old);
584 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
585 (int64_t)(da_old - nblks), 0);
588 * Clear out the allocated field, done with it now in any case.
591 cur->bc_private.b.allocated = 0;
597 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
599 *logflagsp = logflags;
604 * Called by xfs_bmap_add_extent to handle cases converting a delayed
605 * allocation to a real allocation.
607 STATIC int /* error */
608 xfs_bmap_add_extent_delay_real(
609 struct xfs_trans *tp, /* transaction pointer */
610 xfs_inode_t *ip, /* incore inode pointer */
611 xfs_extnum_t *idx, /* extent number to update/insert */
612 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
613 xfs_bmbt_irec_t *new, /* new data to add to file extents */
614 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
615 xfs_fsblock_t *first, /* pointer to firstblock variable */
616 xfs_bmap_free_t *flist, /* list of extents to be freed */
617 int *logflagsp) /* inode logging flags */
619 xfs_btree_cur_t *cur; /* btree cursor */
620 int diff; /* temp value */
621 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
622 int error; /* error return value */
623 int i; /* temp state */
624 xfs_ifork_t *ifp; /* inode fork pointer */
625 xfs_fileoff_t new_endoff; /* end offset of new entry */
626 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
627 /* left is 0, right is 1, prev is 2 */
628 int rval=0; /* return value (logging flags) */
629 int state = 0;/* state bits, accessed thru macros */
630 xfs_filblks_t temp=0; /* value for dnew calculations */
631 xfs_filblks_t temp2=0;/* value for dnew calculations */
632 int tmp_rval; /* partial logging flags */
639 * Set up a bunch of variables to make the tests simpler.
642 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
643 ep = xfs_iext_get_ext(ifp, *idx);
644 xfs_bmbt_get_all(ep, &PREV);
645 new_endoff = new->br_startoff + new->br_blockcount;
646 ASSERT(PREV.br_startoff <= new->br_startoff);
647 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
650 * Set flags determining what part of the previous delayed allocation
651 * extent is being replaced by a real allocation.
653 if (PREV.br_startoff == new->br_startoff)
654 state |= BMAP_LEFT_FILLING;
655 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
656 state |= BMAP_RIGHT_FILLING;
659 * Check and set flags if this segment has a left neighbor.
660 * Don't set contiguous if the combined extent would be too large.
663 state |= BMAP_LEFT_VALID;
664 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
666 if (isnullstartblock(LEFT.br_startblock))
667 state |= BMAP_LEFT_DELAY;
670 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
671 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
672 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
673 LEFT.br_state == new->br_state &&
674 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
675 state |= BMAP_LEFT_CONTIG;
678 * Check and set flags if this segment has a right neighbor.
679 * Don't set contiguous if the combined extent would be too large.
680 * Also check for all-three-contiguous being too large.
682 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
683 state |= BMAP_RIGHT_VALID;
684 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
686 if (isnullstartblock(RIGHT.br_startblock))
687 state |= BMAP_RIGHT_DELAY;
690 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
691 new_endoff == RIGHT.br_startoff &&
692 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
693 new->br_state == RIGHT.br_state &&
694 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
695 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
696 BMAP_RIGHT_FILLING)) !=
697 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
698 BMAP_RIGHT_FILLING) ||
699 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
701 state |= BMAP_RIGHT_CONTIG;
705 * Switch out based on the FILLING and CONTIG state bits.
707 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
708 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
709 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
710 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
712 * Filling in all of a previously delayed allocation extent.
713 * The left and right neighbors are both contiguous with new.
716 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
717 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
718 LEFT.br_blockcount + PREV.br_blockcount +
719 RIGHT.br_blockcount);
720 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
722 xfs_iext_remove(ip, *idx + 1, 2, state);
723 ip->i_d.di_nextents--;
725 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
727 rval = XFS_ILOG_CORE;
728 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
730 RIGHT.br_blockcount, &i)))
732 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
733 if ((error = xfs_btree_delete(cur, &i)))
735 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
736 if ((error = xfs_btree_decrement(cur, 0, &i)))
738 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
739 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
743 RIGHT.br_blockcount, LEFT.br_state)))
749 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
751 * Filling in all of a previously delayed allocation extent.
752 * The left neighbor is contiguous, the right is not.
756 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
757 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
758 LEFT.br_blockcount + PREV.br_blockcount);
759 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
761 xfs_iext_remove(ip, *idx + 1, 1, state);
763 rval = XFS_ILOG_DEXT;
766 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
767 LEFT.br_startblock, LEFT.br_blockcount,
770 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
771 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
774 PREV.br_blockcount, LEFT.br_state)))
780 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
782 * Filling in all of a previously delayed allocation extent.
783 * The right neighbor is contiguous, the left is not.
785 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
786 xfs_bmbt_set_startblock(ep, new->br_startblock);
787 xfs_bmbt_set_blockcount(ep,
788 PREV.br_blockcount + RIGHT.br_blockcount);
789 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
791 xfs_iext_remove(ip, *idx + 1, 1, state);
793 rval = XFS_ILOG_DEXT;
796 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
798 RIGHT.br_blockcount, &i)))
800 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
801 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
804 RIGHT.br_blockcount, PREV.br_state)))
811 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
813 * Filling in all of a previously delayed allocation extent.
814 * Neither the left nor right neighbors are contiguous with
817 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
821 ip->i_d.di_nextents++;
823 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
825 rval = XFS_ILOG_CORE;
826 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
827 new->br_startblock, new->br_blockcount,
830 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
831 cur->bc_rec.b.br_state = XFS_EXT_NORM;
832 if ((error = xfs_btree_insert(cur, &i)))
834 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
840 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
842 * Filling in the first part of a previous delayed allocation.
843 * The left neighbor is contiguous.
845 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
846 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
847 LEFT.br_blockcount + new->br_blockcount);
848 xfs_bmbt_set_startoff(ep,
849 PREV.br_startoff + new->br_blockcount);
850 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
852 temp = PREV.br_blockcount - new->br_blockcount;
853 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
854 xfs_bmbt_set_blockcount(ep, temp);
856 rval = XFS_ILOG_DEXT;
859 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
860 LEFT.br_startblock, LEFT.br_blockcount,
863 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
864 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
871 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
872 startblockval(PREV.br_startblock));
873 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
874 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
880 case BMAP_LEFT_FILLING:
882 * Filling in the first part of a previous delayed allocation.
883 * The left neighbor is not contiguous.
885 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
886 xfs_bmbt_set_startoff(ep, new_endoff);
887 temp = PREV.br_blockcount - new->br_blockcount;
888 xfs_bmbt_set_blockcount(ep, temp);
889 xfs_iext_insert(ip, *idx, 1, new, state);
890 ip->i_d.di_nextents++;
892 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
894 rval = XFS_ILOG_CORE;
895 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
896 new->br_startblock, new->br_blockcount,
899 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
900 cur->bc_rec.b.br_state = XFS_EXT_NORM;
901 if ((error = xfs_btree_insert(cur, &i)))
903 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
905 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
906 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
907 error = xfs_bmap_extents_to_btree(tp, ip,
908 first, flist, &cur, 1, &tmp_rval,
914 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
915 startblockval(PREV.br_startblock) -
916 (cur ? cur->bc_private.b.allocated : 0));
917 ep = xfs_iext_get_ext(ifp, *idx + 1);
918 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
919 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
924 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
926 * Filling in the last part of a previous delayed allocation.
927 * The right neighbor is contiguous with the new allocation.
929 temp = PREV.br_blockcount - new->br_blockcount;
930 trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_);
931 xfs_bmbt_set_blockcount(ep, temp);
932 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1),
933 new->br_startoff, new->br_startblock,
934 new->br_blockcount + RIGHT.br_blockcount,
936 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
938 rval = XFS_ILOG_DEXT;
941 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
943 RIGHT.br_blockcount, &i)))
945 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
946 if ((error = xfs_bmbt_update(cur, new->br_startoff,
954 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
955 startblockval(PREV.br_startblock));
956 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
957 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
958 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
964 case BMAP_RIGHT_FILLING:
966 * Filling in the last part of a previous delayed allocation.
967 * The right neighbor is not contiguous.
969 temp = PREV.br_blockcount - new->br_blockcount;
970 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
971 xfs_bmbt_set_blockcount(ep, temp);
972 xfs_iext_insert(ip, *idx + 1, 1, new, state);
973 ip->i_d.di_nextents++;
975 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
977 rval = XFS_ILOG_CORE;
978 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
979 new->br_startblock, new->br_blockcount,
982 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
983 cur->bc_rec.b.br_state = XFS_EXT_NORM;
984 if ((error = xfs_btree_insert(cur, &i)))
986 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
988 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
989 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
990 error = xfs_bmap_extents_to_btree(tp, ip,
991 first, flist, &cur, 1, &tmp_rval,
997 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
998 startblockval(PREV.br_startblock) -
999 (cur ? cur->bc_private.b.allocated : 0));
1000 ep = xfs_iext_get_ext(ifp, *idx);
1001 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1002 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1010 * Filling in the middle part of a previous delayed allocation.
1011 * Contiguity is impossible here.
1012 * This case is avoided almost all the time.
1014 * We start with a delayed allocation:
1016 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1019 * and we are allocating:
1020 * +rrrrrrrrrrrrrrrrr+
1023 * and we set it up for insertion as:
1024 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1026 * PREV @ idx LEFT RIGHT
1027 * inserted at idx + 1
1029 temp = new->br_startoff - PREV.br_startoff;
1030 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1031 trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_);
1032 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
1034 RIGHT.br_state = PREV.br_state;
1035 RIGHT.br_startblock = nullstartblock(
1036 (int)xfs_bmap_worst_indlen(ip, temp2));
1037 RIGHT.br_startoff = new_endoff;
1038 RIGHT.br_blockcount = temp2;
1039 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1040 xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state);
1041 ip->i_d.di_nextents++;
1043 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1045 rval = XFS_ILOG_CORE;
1046 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1047 new->br_startblock, new->br_blockcount,
1050 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1051 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1052 if ((error = xfs_btree_insert(cur, &i)))
1054 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1056 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1057 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1058 error = xfs_bmap_extents_to_btree(tp, ip,
1059 first, flist, &cur, 1, &tmp_rval,
1065 temp = xfs_bmap_worst_indlen(ip, temp);
1066 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1067 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1068 (cur ? cur->bc_private.b.allocated : 0));
1070 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1071 -((int64_t)diff), 0)) {
1073 * Ick gross gag me with a spoon.
1075 ASSERT(0); /* want to see if this ever happens! */
1081 !xfs_icsb_modify_counters(ip->i_mount,
1083 -((int64_t)diff), 0))
1090 !xfs_icsb_modify_counters(ip->i_mount,
1092 -((int64_t)diff), 0))
1097 ep = xfs_iext_get_ext(ifp, *idx);
1098 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1099 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1100 trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_);
1101 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2),
1102 nullstartblock((int)temp2));
1103 trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_);
1106 *dnew = temp + temp2;
1109 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1110 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1111 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1112 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1113 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1114 case BMAP_LEFT_CONTIG:
1115 case BMAP_RIGHT_CONTIG:
1117 * These cases are all impossible.
1131 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1132 * allocation to a real allocation or vice versa.
1134 STATIC int /* error */
1135 xfs_bmap_add_extent_unwritten_real(
1136 xfs_inode_t *ip, /* incore inode pointer */
1137 xfs_extnum_t *idx, /* extent number to update/insert */
1138 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1139 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1140 int *logflagsp) /* inode logging flags */
1142 xfs_btree_cur_t *cur; /* btree cursor */
1143 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1144 int error; /* error return value */
1145 int i; /* temp state */
1146 xfs_ifork_t *ifp; /* inode fork pointer */
1147 xfs_fileoff_t new_endoff; /* end offset of new entry */
1148 xfs_exntst_t newext; /* new extent state */
1149 xfs_exntst_t oldext; /* old extent state */
1150 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1151 /* left is 0, right is 1, prev is 2 */
1152 int rval=0; /* return value (logging flags) */
1153 int state = 0;/* state bits, accessed thru macros */
1159 * Set up a bunch of variables to make the tests simpler.
1163 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1164 ep = xfs_iext_get_ext(ifp, *idx);
1165 xfs_bmbt_get_all(ep, &PREV);
1166 newext = new->br_state;
1167 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1168 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1169 ASSERT(PREV.br_state == oldext);
1170 new_endoff = new->br_startoff + new->br_blockcount;
1171 ASSERT(PREV.br_startoff <= new->br_startoff);
1172 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1175 * Set flags determining what part of the previous oldext allocation
1176 * extent is being replaced by a newext allocation.
1178 if (PREV.br_startoff == new->br_startoff)
1179 state |= BMAP_LEFT_FILLING;
1180 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1181 state |= BMAP_RIGHT_FILLING;
1184 * Check and set flags if this segment has a left neighbor.
1185 * Don't set contiguous if the combined extent would be too large.
1188 state |= BMAP_LEFT_VALID;
1189 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
1191 if (isnullstartblock(LEFT.br_startblock))
1192 state |= BMAP_LEFT_DELAY;
1195 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1196 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1197 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1198 LEFT.br_state == newext &&
1199 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1200 state |= BMAP_LEFT_CONTIG;
1203 * Check and set flags if this segment has a right neighbor.
1204 * Don't set contiguous if the combined extent would be too large.
1205 * Also check for all-three-contiguous being too large.
1207 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1208 state |= BMAP_RIGHT_VALID;
1209 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
1210 if (isnullstartblock(RIGHT.br_startblock))
1211 state |= BMAP_RIGHT_DELAY;
1214 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1215 new_endoff == RIGHT.br_startoff &&
1216 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1217 newext == RIGHT.br_state &&
1218 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1219 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1220 BMAP_RIGHT_FILLING)) !=
1221 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1222 BMAP_RIGHT_FILLING) ||
1223 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1225 state |= BMAP_RIGHT_CONTIG;
1228 * Switch out based on the FILLING and CONTIG state bits.
1230 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1231 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1232 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1233 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1235 * Setting all of a previous oldext extent to newext.
1236 * The left and right neighbors are both contiguous with new.
1240 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1241 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1242 LEFT.br_blockcount + PREV.br_blockcount +
1243 RIGHT.br_blockcount);
1244 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1246 xfs_iext_remove(ip, *idx + 1, 2, state);
1247 ip->i_d.di_nextents -= 2;
1249 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1251 rval = XFS_ILOG_CORE;
1252 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1253 RIGHT.br_startblock,
1254 RIGHT.br_blockcount, &i)))
1256 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1257 if ((error = xfs_btree_delete(cur, &i)))
1259 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1260 if ((error = xfs_btree_decrement(cur, 0, &i)))
1262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1263 if ((error = xfs_btree_delete(cur, &i)))
1265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1266 if ((error = xfs_btree_decrement(cur, 0, &i)))
1268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1269 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1271 LEFT.br_blockcount + PREV.br_blockcount +
1272 RIGHT.br_blockcount, LEFT.br_state)))
1277 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1279 * Setting all of a previous oldext extent to newext.
1280 * The left neighbor is contiguous, the right is not.
1284 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1285 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1286 LEFT.br_blockcount + PREV.br_blockcount);
1287 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1289 xfs_iext_remove(ip, *idx + 1, 1, state);
1290 ip->i_d.di_nextents--;
1292 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1294 rval = XFS_ILOG_CORE;
1295 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1296 PREV.br_startblock, PREV.br_blockcount,
1299 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1300 if ((error = xfs_btree_delete(cur, &i)))
1302 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1303 if ((error = xfs_btree_decrement(cur, 0, &i)))
1305 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1306 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1308 LEFT.br_blockcount + PREV.br_blockcount,
1314 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1316 * Setting all of a previous oldext extent to newext.
1317 * The right neighbor is contiguous, the left is not.
1319 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1320 xfs_bmbt_set_blockcount(ep,
1321 PREV.br_blockcount + RIGHT.br_blockcount);
1322 xfs_bmbt_set_state(ep, newext);
1323 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1324 xfs_iext_remove(ip, *idx + 1, 1, state);
1325 ip->i_d.di_nextents--;
1327 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1329 rval = XFS_ILOG_CORE;
1330 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1331 RIGHT.br_startblock,
1332 RIGHT.br_blockcount, &i)))
1334 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1335 if ((error = xfs_btree_delete(cur, &i)))
1337 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1338 if ((error = xfs_btree_decrement(cur, 0, &i)))
1340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1341 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1343 new->br_blockcount + RIGHT.br_blockcount,
1349 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1351 * Setting all of a previous oldext extent to newext.
1352 * Neither the left nor right neighbors are contiguous with
1355 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1356 xfs_bmbt_set_state(ep, newext);
1357 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1360 rval = XFS_ILOG_DEXT;
1363 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1364 new->br_startblock, new->br_blockcount,
1367 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1368 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1369 new->br_startblock, new->br_blockcount,
1375 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1377 * Setting the first part of a previous oldext extent to newext.
1378 * The left neighbor is contiguous.
1380 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1381 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
1382 LEFT.br_blockcount + new->br_blockcount);
1383 xfs_bmbt_set_startoff(ep,
1384 PREV.br_startoff + new->br_blockcount);
1385 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
1387 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1388 xfs_bmbt_set_startblock(ep,
1389 new->br_startblock + new->br_blockcount);
1390 xfs_bmbt_set_blockcount(ep,
1391 PREV.br_blockcount - new->br_blockcount);
1392 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1397 rval = XFS_ILOG_DEXT;
1400 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1401 PREV.br_startblock, PREV.br_blockcount,
1404 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1405 if ((error = xfs_bmbt_update(cur,
1406 PREV.br_startoff + new->br_blockcount,
1407 PREV.br_startblock + new->br_blockcount,
1408 PREV.br_blockcount - new->br_blockcount,
1411 if ((error = xfs_btree_decrement(cur, 0, &i)))
1413 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1415 LEFT.br_blockcount + new->br_blockcount,
1421 case BMAP_LEFT_FILLING:
1423 * Setting the first part of a previous oldext extent to newext.
1424 * The left neighbor is not contiguous.
1426 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1427 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1428 xfs_bmbt_set_startoff(ep, new_endoff);
1429 xfs_bmbt_set_blockcount(ep,
1430 PREV.br_blockcount - new->br_blockcount);
1431 xfs_bmbt_set_startblock(ep,
1432 new->br_startblock + new->br_blockcount);
1433 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1435 xfs_iext_insert(ip, *idx, 1, new, state);
1436 ip->i_d.di_nextents++;
1438 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1440 rval = XFS_ILOG_CORE;
1441 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1442 PREV.br_startblock, PREV.br_blockcount,
1445 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1446 if ((error = xfs_bmbt_update(cur,
1447 PREV.br_startoff + new->br_blockcount,
1448 PREV.br_startblock + new->br_blockcount,
1449 PREV.br_blockcount - new->br_blockcount,
1452 cur->bc_rec.b = *new;
1453 if ((error = xfs_btree_insert(cur, &i)))
1455 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1459 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1461 * Setting the last part of a previous oldext extent to newext.
1462 * The right neighbor is contiguous with the new allocation.
1464 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1465 xfs_bmbt_set_blockcount(ep,
1466 PREV.br_blockcount - new->br_blockcount);
1467 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1471 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1472 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1473 new->br_startoff, new->br_startblock,
1474 new->br_blockcount + RIGHT.br_blockcount, newext);
1475 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1478 rval = XFS_ILOG_DEXT;
1481 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1483 PREV.br_blockcount, &i)))
1485 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1486 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1488 PREV.br_blockcount - new->br_blockcount,
1491 if ((error = xfs_btree_increment(cur, 0, &i)))
1493 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1495 new->br_blockcount + RIGHT.br_blockcount,
1501 case BMAP_RIGHT_FILLING:
1503 * Setting the last part of a previous oldext extent to newext.
1504 * The right neighbor is not contiguous.
1506 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1507 xfs_bmbt_set_blockcount(ep,
1508 PREV.br_blockcount - new->br_blockcount);
1509 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1512 xfs_iext_insert(ip, *idx, 1, new, state);
1514 ip->i_d.di_nextents++;
1516 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1518 rval = XFS_ILOG_CORE;
1519 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1520 PREV.br_startblock, PREV.br_blockcount,
1523 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1524 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1526 PREV.br_blockcount - new->br_blockcount,
1529 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1530 new->br_startblock, new->br_blockcount,
1533 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1534 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1535 if ((error = xfs_btree_insert(cur, &i)))
1537 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1543 * Setting the middle part of a previous oldext extent to
1544 * newext. Contiguity is impossible here.
1545 * One extent becomes three extents.
1547 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1548 xfs_bmbt_set_blockcount(ep,
1549 new->br_startoff - PREV.br_startoff);
1550 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1553 r[1].br_startoff = new_endoff;
1554 r[1].br_blockcount =
1555 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1556 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1557 r[1].br_state = oldext;
1560 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1562 ip->i_d.di_nextents += 2;
1564 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1566 rval = XFS_ILOG_CORE;
1567 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1568 PREV.br_startblock, PREV.br_blockcount,
1571 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1572 /* new right extent - oldext */
1573 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1574 r[1].br_startblock, r[1].br_blockcount,
1577 /* new left extent - oldext */
1578 cur->bc_rec.b = PREV;
1579 cur->bc_rec.b.br_blockcount =
1580 new->br_startoff - PREV.br_startoff;
1581 if ((error = xfs_btree_insert(cur, &i)))
1583 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1585 * Reset the cursor to the position of the new extent
1586 * we are about to insert as we can't trust it after
1587 * the previous insert.
1589 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1590 new->br_startblock, new->br_blockcount,
1593 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1594 /* new middle extent - newext */
1595 cur->bc_rec.b.br_state = new->br_state;
1596 if ((error = xfs_btree_insert(cur, &i)))
1598 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1602 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1603 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1604 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1606 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1607 case BMAP_LEFT_CONTIG:
1608 case BMAP_RIGHT_CONTIG:
1610 * These cases are all impossible.
1624 * Called by xfs_bmap_add_extent to handle cases converting a hole
1625 * to a delayed allocation.
1628 STATIC int /* error */
1629 xfs_bmap_add_extent_hole_delay(
1630 xfs_inode_t *ip, /* incore inode pointer */
1631 xfs_extnum_t *idx, /* extent number to update/insert */
1632 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1633 int *logflagsp) /* inode logging flags */
1635 xfs_ifork_t *ifp; /* inode fork pointer */
1636 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1637 xfs_filblks_t newlen=0; /* new indirect size */
1638 xfs_filblks_t oldlen=0; /* old indirect size */
1639 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1640 int state; /* state bits, accessed thru macros */
1641 xfs_filblks_t temp=0; /* temp for indirect calculations */
1643 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1645 ASSERT(isnullstartblock(new->br_startblock));
1648 * Check and set flags if this segment has a left neighbor
1651 state |= BMAP_LEFT_VALID;
1652 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1654 if (isnullstartblock(left.br_startblock))
1655 state |= BMAP_LEFT_DELAY;
1659 * Check and set flags if the current (right) segment exists.
1660 * If it doesn't exist, we're converting the hole at end-of-file.
1662 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1663 state |= BMAP_RIGHT_VALID;
1664 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1666 if (isnullstartblock(right.br_startblock))
1667 state |= BMAP_RIGHT_DELAY;
1671 * Set contiguity flags on the left and right neighbors.
1672 * Don't let extents get too large, even if the pieces are contiguous.
1674 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1675 left.br_startoff + left.br_blockcount == new->br_startoff &&
1676 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1677 state |= BMAP_LEFT_CONTIG;
1679 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1680 new->br_startoff + new->br_blockcount == right.br_startoff &&
1681 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1682 (!(state & BMAP_LEFT_CONTIG) ||
1683 (left.br_blockcount + new->br_blockcount +
1684 right.br_blockcount <= MAXEXTLEN)))
1685 state |= BMAP_RIGHT_CONTIG;
1688 * Switch out based on the contiguity flags.
1690 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1691 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1693 * New allocation is contiguous with delayed allocations
1694 * on the left and on the right.
1695 * Merge all three into a single extent record.
1698 temp = left.br_blockcount + new->br_blockcount +
1699 right.br_blockcount;
1701 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1702 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1703 oldlen = startblockval(left.br_startblock) +
1704 startblockval(new->br_startblock) +
1705 startblockval(right.br_startblock);
1706 newlen = xfs_bmap_worst_indlen(ip, temp);
1707 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1708 nullstartblock((int)newlen));
1709 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1711 xfs_iext_remove(ip, *idx + 1, 1, state);
1714 case BMAP_LEFT_CONTIG:
1716 * New allocation is contiguous with a delayed allocation
1718 * Merge the new allocation with the left neighbor.
1721 temp = left.br_blockcount + new->br_blockcount;
1723 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1724 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1725 oldlen = startblockval(left.br_startblock) +
1726 startblockval(new->br_startblock);
1727 newlen = xfs_bmap_worst_indlen(ip, temp);
1728 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1729 nullstartblock((int)newlen));
1730 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1733 case BMAP_RIGHT_CONTIG:
1735 * New allocation is contiguous with a delayed allocation
1737 * Merge the new allocation with the right neighbor.
1739 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1740 temp = new->br_blockcount + right.br_blockcount;
1741 oldlen = startblockval(new->br_startblock) +
1742 startblockval(right.br_startblock);
1743 newlen = xfs_bmap_worst_indlen(ip, temp);
1744 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1746 nullstartblock((int)newlen), temp, right.br_state);
1747 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1752 * New allocation is not contiguous with another
1753 * delayed allocation.
1754 * Insert a new entry.
1756 oldlen = newlen = 0;
1757 xfs_iext_insert(ip, *idx, 1, new, state);
1760 if (oldlen != newlen) {
1761 ASSERT(oldlen > newlen);
1762 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1763 (int64_t)(oldlen - newlen), 0);
1765 * Nothing to do for disk quota accounting here.
1773 * Called by xfs_bmap_add_extent to handle cases converting a hole
1774 * to a real allocation.
1776 STATIC int /* error */
1777 xfs_bmap_add_extent_hole_real(
1778 xfs_inode_t *ip, /* incore inode pointer */
1779 xfs_extnum_t *idx, /* extent number to update/insert */
1780 xfs_btree_cur_t *cur, /* if null, not a btree */
1781 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1782 int *logflagsp, /* inode logging flags */
1783 int whichfork) /* data or attr fork */
1785 int error; /* error return value */
1786 int i; /* temp state */
1787 xfs_ifork_t *ifp; /* inode fork pointer */
1788 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1789 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1790 int rval=0; /* return value (logging flags) */
1791 int state; /* state bits, accessed thru macros */
1793 ifp = XFS_IFORK_PTR(ip, whichfork);
1794 ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1797 if (whichfork == XFS_ATTR_FORK)
1798 state |= BMAP_ATTRFORK;
1801 * Check and set flags if this segment has a left neighbor.
1804 state |= BMAP_LEFT_VALID;
1805 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1806 if (isnullstartblock(left.br_startblock))
1807 state |= BMAP_LEFT_DELAY;
1811 * Check and set flags if this segment has a current value.
1812 * Not true if we're inserting into the "hole" at eof.
1814 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1815 state |= BMAP_RIGHT_VALID;
1816 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1817 if (isnullstartblock(right.br_startblock))
1818 state |= BMAP_RIGHT_DELAY;
1822 * We're inserting a real allocation between "left" and "right".
1823 * Set the contiguity flags. Don't let extents get too large.
1825 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1826 left.br_startoff + left.br_blockcount == new->br_startoff &&
1827 left.br_startblock + left.br_blockcount == new->br_startblock &&
1828 left.br_state == new->br_state &&
1829 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1830 state |= BMAP_LEFT_CONTIG;
1832 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1833 new->br_startoff + new->br_blockcount == right.br_startoff &&
1834 new->br_startblock + new->br_blockcount == right.br_startblock &&
1835 new->br_state == right.br_state &&
1836 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1837 (!(state & BMAP_LEFT_CONTIG) ||
1838 left.br_blockcount + new->br_blockcount +
1839 right.br_blockcount <= MAXEXTLEN))
1840 state |= BMAP_RIGHT_CONTIG;
1844 * Select which case we're in here, and implement it.
1846 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1847 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1849 * New allocation is contiguous with real allocations on the
1850 * left and on the right.
1851 * Merge all three into a single extent record.
1854 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1855 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1856 left.br_blockcount + new->br_blockcount +
1857 right.br_blockcount);
1858 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1860 xfs_iext_remove(ip, *idx + 1, 1, state);
1862 XFS_IFORK_NEXT_SET(ip, whichfork,
1863 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1865 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1867 rval = XFS_ILOG_CORE;
1868 if ((error = xfs_bmbt_lookup_eq(cur,
1870 right.br_startblock,
1871 right.br_blockcount, &i)))
1873 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1874 if ((error = xfs_btree_delete(cur, &i)))
1876 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1877 if ((error = xfs_btree_decrement(cur, 0, &i)))
1879 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1880 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1882 left.br_blockcount +
1883 new->br_blockcount +
1884 right.br_blockcount,
1890 case BMAP_LEFT_CONTIG:
1892 * New allocation is contiguous with a real allocation
1894 * Merge the new allocation with the left neighbor.
1897 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1898 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1899 left.br_blockcount + new->br_blockcount);
1900 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1903 rval = xfs_ilog_fext(whichfork);
1906 if ((error = xfs_bmbt_lookup_eq(cur,
1909 left.br_blockcount, &i)))
1911 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1912 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1914 left.br_blockcount +
1921 case BMAP_RIGHT_CONTIG:
1923 * New allocation is contiguous with a real allocation
1925 * Merge the new allocation with the right neighbor.
1927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1928 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1929 new->br_startoff, new->br_startblock,
1930 new->br_blockcount + right.br_blockcount,
1932 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1935 rval = xfs_ilog_fext(whichfork);
1938 if ((error = xfs_bmbt_lookup_eq(cur,
1940 right.br_startblock,
1941 right.br_blockcount, &i)))
1943 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1944 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1946 new->br_blockcount +
1947 right.br_blockcount,
1955 * New allocation is not contiguous with another
1957 * Insert a new entry.
1959 xfs_iext_insert(ip, *idx, 1, new, state);
1960 XFS_IFORK_NEXT_SET(ip, whichfork,
1961 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1963 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1965 rval = XFS_ILOG_CORE;
1966 if ((error = xfs_bmbt_lookup_eq(cur,
1969 new->br_blockcount, &i)))
1971 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1972 cur->bc_rec.b.br_state = new->br_state;
1973 if ((error = xfs_btree_insert(cur, &i)))
1975 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1985 * Adjust the size of the new extent based on di_extsize and rt extsize.
1988 xfs_bmap_extsize_align(
1990 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1991 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1992 xfs_extlen_t extsz, /* align to this extent size */
1993 int rt, /* is this a realtime inode? */
1994 int eof, /* is extent at end-of-file? */
1995 int delay, /* creating delalloc extent? */
1996 int convert, /* overwriting unwritten extent? */
1997 xfs_fileoff_t *offp, /* in/out: aligned offset */
1998 xfs_extlen_t *lenp) /* in/out: aligned length */
2000 xfs_fileoff_t orig_off; /* original offset */
2001 xfs_extlen_t orig_alen; /* original length */
2002 xfs_fileoff_t orig_end; /* original off+len */
2003 xfs_fileoff_t nexto; /* next file offset */
2004 xfs_fileoff_t prevo; /* previous file offset */
2005 xfs_fileoff_t align_off; /* temp for offset */
2006 xfs_extlen_t align_alen; /* temp for length */
2007 xfs_extlen_t temp; /* temp for calculations */
2012 orig_off = align_off = *offp;
2013 orig_alen = align_alen = *lenp;
2014 orig_end = orig_off + orig_alen;
2017 * If this request overlaps an existing extent, then don't
2018 * attempt to perform any additional alignment.
2020 if (!delay && !eof &&
2021 (orig_off >= gotp->br_startoff) &&
2022 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2027 * If the file offset is unaligned vs. the extent size
2028 * we need to align it. This will be possible unless
2029 * the file was previously written with a kernel that didn't
2030 * perform this alignment, or if a truncate shot us in the
2033 temp = do_mod(orig_off, extsz);
2039 * Same adjustment for the end of the requested area.
2041 if ((temp = (align_alen % extsz))) {
2042 align_alen += extsz - temp;
2045 * If the previous block overlaps with this proposed allocation
2046 * then move the start forward without adjusting the length.
2048 if (prevp->br_startoff != NULLFILEOFF) {
2049 if (prevp->br_startblock == HOLESTARTBLOCK)
2050 prevo = prevp->br_startoff;
2052 prevo = prevp->br_startoff + prevp->br_blockcount;
2055 if (align_off != orig_off && align_off < prevo)
2058 * If the next block overlaps with this proposed allocation
2059 * then move the start back without adjusting the length,
2060 * but not before offset 0.
2061 * This may of course make the start overlap previous block,
2062 * and if we hit the offset 0 limit then the next block
2063 * can still overlap too.
2065 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2066 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2067 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2068 nexto = gotp->br_startoff + gotp->br_blockcount;
2070 nexto = gotp->br_startoff;
2072 nexto = NULLFILEOFF;
2074 align_off + align_alen != orig_end &&
2075 align_off + align_alen > nexto)
2076 align_off = nexto > align_alen ? nexto - align_alen : 0;
2078 * If we're now overlapping the next or previous extent that
2079 * means we can't fit an extsz piece in this hole. Just move
2080 * the start forward to the first valid spot and set
2081 * the length so we hit the end.
2083 if (align_off != orig_off && align_off < prevo)
2085 if (align_off + align_alen != orig_end &&
2086 align_off + align_alen > nexto &&
2087 nexto != NULLFILEOFF) {
2088 ASSERT(nexto > prevo);
2089 align_alen = nexto - align_off;
2093 * If realtime, and the result isn't a multiple of the realtime
2094 * extent size we need to remove blocks until it is.
2096 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2098 * We're not covering the original request, or
2099 * we won't be able to once we fix the length.
2101 if (orig_off < align_off ||
2102 orig_end > align_off + align_alen ||
2103 align_alen - temp < orig_alen)
2104 return XFS_ERROR(EINVAL);
2106 * Try to fix it by moving the start up.
2108 if (align_off + temp <= orig_off) {
2113 * Try to fix it by moving the end in.
2115 else if (align_off + align_alen - temp >= orig_end)
2118 * Set the start to the minimum then trim the length.
2121 align_alen -= orig_off - align_off;
2122 align_off = orig_off;
2123 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2126 * Result doesn't cover the request, fail it.
2128 if (orig_off < align_off || orig_end > align_off + align_alen)
2129 return XFS_ERROR(EINVAL);
2131 ASSERT(orig_off >= align_off);
2132 ASSERT(orig_end <= align_off + align_alen);
2136 if (!eof && gotp->br_startoff != NULLFILEOFF)
2137 ASSERT(align_off + align_alen <= gotp->br_startoff);
2138 if (prevp->br_startoff != NULLFILEOFF)
2139 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2147 #define XFS_ALLOC_GAP_UNITS 4
2151 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2153 xfs_fsblock_t adjust; /* adjustment to block numbers */
2154 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2155 xfs_mount_t *mp; /* mount point structure */
2156 int nullfb; /* true if ap->firstblock isn't set */
2157 int rt; /* true if inode is realtime */
2159 #define ISVALID(x,y) \
2161 (x) < mp->m_sb.sb_rblocks : \
2162 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2163 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2164 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2166 mp = ap->ip->i_mount;
2167 nullfb = ap->firstblock == NULLFSBLOCK;
2168 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2169 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2171 * If allocating at eof, and there's a previous real block,
2172 * try to use its last block as our starting point.
2174 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2175 !isnullstartblock(ap->prevp->br_startblock) &&
2176 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2177 ap->prevp->br_startblock)) {
2178 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2180 * Adjust for the gap between prevp and us.
2183 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2185 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2189 * If not at eof, then compare the two neighbor blocks.
2190 * Figure out whether either one gives us a good starting point,
2191 * and pick the better one.
2193 else if (!ap->eof) {
2194 xfs_fsblock_t gotbno; /* right side block number */
2195 xfs_fsblock_t gotdiff=0; /* right side difference */
2196 xfs_fsblock_t prevbno; /* left side block number */
2197 xfs_fsblock_t prevdiff=0; /* left side difference */
2200 * If there's a previous (left) block, select a requested
2201 * start block based on it.
2203 if (ap->prevp->br_startoff != NULLFILEOFF &&
2204 !isnullstartblock(ap->prevp->br_startblock) &&
2205 (prevbno = ap->prevp->br_startblock +
2206 ap->prevp->br_blockcount) &&
2207 ISVALID(prevbno, ap->prevp->br_startblock)) {
2209 * Calculate gap to end of previous block.
2211 adjust = prevdiff = ap->off -
2212 (ap->prevp->br_startoff +
2213 ap->prevp->br_blockcount);
2215 * Figure the startblock based on the previous block's
2216 * end and the gap size.
2218 * If the gap is large relative to the piece we're
2219 * allocating, or using it gives us an invalid block
2220 * number, then just use the end of the previous block.
2222 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2223 ISVALID(prevbno + prevdiff,
2224 ap->prevp->br_startblock))
2229 * If the firstblock forbids it, can't use it,
2232 if (!rt && !nullfb &&
2233 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2234 prevbno = NULLFSBLOCK;
2237 * No previous block or can't follow it, just default.
2240 prevbno = NULLFSBLOCK;
2242 * If there's a following (right) block, select a requested
2243 * start block based on it.
2245 if (!isnullstartblock(ap->gotp->br_startblock)) {
2247 * Calculate gap to start of next block.
2249 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2251 * Figure the startblock based on the next block's
2252 * start and the gap size.
2254 gotbno = ap->gotp->br_startblock;
2257 * If the gap is large relative to the piece we're
2258 * allocating, or using it gives us an invalid block
2259 * number, then just use the start of the next block
2260 * offset by our length.
2262 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2263 ISVALID(gotbno - gotdiff, gotbno))
2265 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2267 gotdiff += adjust - ap->alen;
2271 * If the firstblock forbids it, can't use it,
2274 if (!rt && !nullfb &&
2275 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2276 gotbno = NULLFSBLOCK;
2279 * No next block, just default.
2282 gotbno = NULLFSBLOCK;
2284 * If both valid, pick the better one, else the only good
2285 * one, else ap->rval is already set (to 0 or the inode block).
2287 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2288 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2289 else if (prevbno != NULLFSBLOCK)
2291 else if (gotbno != NULLFSBLOCK)
2299 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2301 xfs_alloctype_t atype = 0; /* type for allocation routines */
2302 int error; /* error return value */
2303 xfs_mount_t *mp; /* mount point structure */
2304 xfs_extlen_t prod = 0; /* product factor for allocators */
2305 xfs_extlen_t ralen = 0; /* realtime allocation length */
2306 xfs_extlen_t align; /* minimum allocation alignment */
2309 mp = ap->ip->i_mount;
2310 align = xfs_get_extsz_hint(ap->ip);
2311 prod = align / mp->m_sb.sb_rextsize;
2312 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2313 align, 1, ap->eof, 0,
2314 ap->conv, &ap->off, &ap->alen);
2318 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2321 * If the offset & length are not perfectly aligned
2322 * then kill prod, it will just get us in trouble.
2324 if (do_mod(ap->off, align) || ap->alen % align)
2327 * Set ralen to be the actual requested length in rtextents.
2329 ralen = ap->alen / mp->m_sb.sb_rextsize;
2331 * If the old value was close enough to MAXEXTLEN that
2332 * we rounded up to it, cut it back so it's valid again.
2333 * Note that if it's a really large request (bigger than
2334 * MAXEXTLEN), we don't hear about that number, and can't
2335 * adjust the starting point to match it.
2337 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2338 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2341 * Lock out other modifications to the RT bitmap inode.
2343 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2344 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2347 * If it's an allocation to an empty file at offset 0,
2348 * pick an extent that will space things out in the rt area.
2350 if (ap->eof && ap->off == 0) {
2351 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2353 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2356 ap->rval = rtx * mp->m_sb.sb_rextsize;
2361 xfs_bmap_adjacent(ap);
2364 * Realtime allocation, done through xfs_rtallocate_extent.
2366 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2367 do_div(ap->rval, mp->m_sb.sb_rextsize);
2370 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2371 &ralen, atype, ap->wasdel, prod, &rtb)))
2373 if (rtb == NULLFSBLOCK && prod > 1 &&
2374 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2375 ap->alen, &ralen, atype,
2376 ap->wasdel, 1, &rtb)))
2379 if (ap->rval != NULLFSBLOCK) {
2380 ap->rval *= mp->m_sb.sb_rextsize;
2381 ralen *= mp->m_sb.sb_rextsize;
2383 ap->ip->i_d.di_nblocks += ralen;
2384 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2386 ap->ip->i_delayed_blks -= ralen;
2388 * Adjust the disk quota also. This was reserved
2391 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2392 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2393 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2401 xfs_bmap_btalloc_nullfb(
2402 struct xfs_bmalloca *ap,
2403 struct xfs_alloc_arg *args,
2406 struct xfs_mount *mp = ap->ip->i_mount;
2407 struct xfs_perag *pag;
2408 xfs_agnumber_t ag, startag;
2412 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2413 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2415 args->type = XFS_ALLOCTYPE_START_BNO;
2416 args->total = ap->total;
2419 * Search for an allocation group with a single extent large enough
2420 * for the request. If one isn't found, then adjust the minimum
2421 * allocation size to the largest space found.
2423 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2424 if (startag == NULLAGNUMBER)
2427 pag = xfs_perag_get(mp, ag);
2428 while (*blen < args->maxlen) {
2429 if (!pag->pagf_init) {
2430 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2431 XFS_ALLOC_FLAG_TRYLOCK);
2439 * See xfs_alloc_fix_freelist...
2441 if (pag->pagf_init) {
2442 xfs_extlen_t longest;
2443 longest = xfs_alloc_longest_free_extent(mp, pag);
2444 if (*blen < longest)
2449 if (xfs_inode_is_filestream(ap->ip)) {
2450 if (*blen >= args->maxlen)
2455 * If startag is an invalid AG, we've
2456 * come here once before and
2457 * xfs_filestream_new_ag picked the
2458 * best currently available.
2460 * Don't continue looping, since we
2461 * could loop forever.
2463 if (startag == NULLAGNUMBER)
2466 error = xfs_filestream_new_ag(ap, &ag);
2471 /* loop again to set 'blen'*/
2472 startag = NULLAGNUMBER;
2473 pag = xfs_perag_get(mp, ag);
2477 if (++ag == mp->m_sb.sb_agcount)
2482 pag = xfs_perag_get(mp, ag);
2487 * Since the above loop did a BUF_TRYLOCK, it is
2488 * possible that there is space for this request.
2490 if (notinit || *blen < ap->minlen)
2491 args->minlen = ap->minlen;
2493 * If the best seen length is less than the request
2494 * length, use the best as the minimum.
2496 else if (*blen < args->maxlen)
2497 args->minlen = *blen;
2499 * Otherwise we've seen an extent as big as maxlen,
2500 * use that as the minimum.
2503 args->minlen = args->maxlen;
2506 * set the failure fallback case to look in the selected
2507 * AG as the stream may have moved.
2509 if (xfs_inode_is_filestream(ap->ip))
2510 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2517 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2519 xfs_mount_t *mp; /* mount point structure */
2520 xfs_alloctype_t atype = 0; /* type for allocation routines */
2521 xfs_extlen_t align; /* minimum allocation alignment */
2522 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2524 xfs_alloc_arg_t args;
2526 xfs_extlen_t nextminlen = 0;
2527 int nullfb; /* true if ap->firstblock isn't set */
2532 mp = ap->ip->i_mount;
2533 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2534 if (unlikely(align)) {
2535 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2536 align, 0, ap->eof, 0, ap->conv,
2537 &ap->off, &ap->alen);
2541 nullfb = ap->firstblock == NULLFSBLOCK;
2542 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2544 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2545 ag = xfs_filestream_lookup_ag(ap->ip);
2546 ag = (ag != NULLAGNUMBER) ? ag : 0;
2547 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2549 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2552 ap->rval = ap->firstblock;
2554 xfs_bmap_adjacent(ap);
2557 * If allowed, use ap->rval; otherwise must use firstblock since
2558 * it's in the right allocation group.
2560 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2563 ap->rval = ap->firstblock;
2565 * Normal allocation, done through xfs_alloc_vextent.
2567 tryagain = isaligned = 0;
2570 args.fsbno = ap->rval;
2572 /* Trim the allocation back to the maximum an AG can fit. */
2573 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
2574 args.firstblock = ap->firstblock;
2577 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2580 } else if (ap->low) {
2581 if (xfs_inode_is_filestream(ap->ip))
2582 args.type = XFS_ALLOCTYPE_FIRST_AG;
2584 args.type = XFS_ALLOCTYPE_START_BNO;
2585 args.total = args.minlen = ap->minlen;
2587 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2588 args.total = ap->total;
2589 args.minlen = ap->minlen;
2591 /* apply extent size hints if obtained earlier */
2592 if (unlikely(align)) {
2594 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2595 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2596 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2600 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2601 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2602 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2605 * If we are not low on available data blocks, and the
2606 * underlying logical volume manager is a stripe, and
2607 * the file offset is zero then try to allocate data
2608 * blocks on stripe unit boundary.
2609 * NOTE: ap->aeof is only set if the allocation length
2610 * is >= the stripe unit and the allocation offset is
2611 * at the end of file.
2613 if (!ap->low && ap->aeof) {
2615 args.alignment = mp->m_dalign;
2619 * Adjust for alignment
2621 if (blen > args.alignment && blen <= args.maxlen)
2622 args.minlen = blen - args.alignment;
2623 args.minalignslop = 0;
2626 * First try an exact bno allocation.
2627 * If it fails then do a near or start bno
2628 * allocation with alignment turned on.
2632 args.type = XFS_ALLOCTYPE_THIS_BNO;
2635 * Compute the minlen+alignment for the
2636 * next case. Set slop so that the value
2637 * of minlen+alignment+slop doesn't go up
2638 * between the calls.
2640 if (blen > mp->m_dalign && blen <= args.maxlen)
2641 nextminlen = blen - mp->m_dalign;
2643 nextminlen = args.minlen;
2644 if (nextminlen + mp->m_dalign > args.minlen + 1)
2646 nextminlen + mp->m_dalign -
2649 args.minalignslop = 0;
2653 args.minalignslop = 0;
2655 args.minleft = ap->minleft;
2656 args.wasdel = ap->wasdel;
2658 args.userdata = ap->userdata;
2659 if ((error = xfs_alloc_vextent(&args)))
2661 if (tryagain && args.fsbno == NULLFSBLOCK) {
2663 * Exact allocation failed. Now try with alignment
2667 args.fsbno = ap->rval;
2668 args.alignment = mp->m_dalign;
2669 args.minlen = nextminlen;
2670 args.minalignslop = 0;
2672 if ((error = xfs_alloc_vextent(&args)))
2675 if (isaligned && args.fsbno == NULLFSBLOCK) {
2677 * allocation failed, so turn off alignment and
2681 args.fsbno = ap->rval;
2683 if ((error = xfs_alloc_vextent(&args)))
2686 if (args.fsbno == NULLFSBLOCK && nullfb &&
2687 args.minlen > ap->minlen) {
2688 args.minlen = ap->minlen;
2689 args.type = XFS_ALLOCTYPE_START_BNO;
2690 args.fsbno = ap->rval;
2691 if ((error = xfs_alloc_vextent(&args)))
2694 if (args.fsbno == NULLFSBLOCK && nullfb) {
2696 args.type = XFS_ALLOCTYPE_FIRST_AG;
2697 args.total = ap->minlen;
2699 if ((error = xfs_alloc_vextent(&args)))
2703 if (args.fsbno != NULLFSBLOCK) {
2704 ap->firstblock = ap->rval = args.fsbno;
2705 ASSERT(nullfb || fb_agno == args.agno ||
2706 (ap->low && fb_agno < args.agno));
2707 ap->alen = args.len;
2708 ap->ip->i_d.di_nblocks += args.len;
2709 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2711 ap->ip->i_delayed_blks -= args.len;
2713 * Adjust the disk quota also. This was reserved
2716 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2717 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2718 XFS_TRANS_DQ_BCOUNT,
2721 ap->rval = NULLFSBLOCK;
2728 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2729 * It figures out where to ask the underlying allocator to put the new extent.
2733 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2735 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2736 return xfs_bmap_rtalloc(ap);
2737 return xfs_bmap_btalloc(ap);
2741 * Transform a btree format file with only one leaf node, where the
2742 * extents list will fit in the inode, into an extents format file.
2743 * Since the file extents are already in-core, all we have to do is
2744 * give up the space for the btree root and pitch the leaf block.
2746 STATIC int /* error */
2747 xfs_bmap_btree_to_extents(
2748 xfs_trans_t *tp, /* transaction pointer */
2749 xfs_inode_t *ip, /* incore inode pointer */
2750 xfs_btree_cur_t *cur, /* btree cursor */
2751 int *logflagsp, /* inode logging flags */
2752 int whichfork) /* data or attr fork */
2755 struct xfs_btree_block *cblock;/* child btree block */
2756 xfs_fsblock_t cbno; /* child block number */
2757 xfs_buf_t *cbp; /* child block's buffer */
2758 int error; /* error return value */
2759 xfs_ifork_t *ifp; /* inode fork data */
2760 xfs_mount_t *mp; /* mount point structure */
2761 __be64 *pp; /* ptr to block address */
2762 struct xfs_btree_block *rblock;/* root btree block */
2765 ifp = XFS_IFORK_PTR(ip, whichfork);
2766 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2767 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2768 rblock = ifp->if_broot;
2769 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2770 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2771 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2772 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2773 cbno = be64_to_cpu(*pp);
2776 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2779 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2780 XFS_BMAP_BTREE_REF)))
2782 cblock = XFS_BUF_TO_BLOCK(cbp);
2783 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2785 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2786 ip->i_d.di_nblocks--;
2787 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2788 xfs_trans_binval(tp, cbp);
2789 if (cur->bc_bufs[0] == cbp)
2790 cur->bc_bufs[0] = NULL;
2791 xfs_iroot_realloc(ip, -1, whichfork);
2792 ASSERT(ifp->if_broot == NULL);
2793 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2794 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2795 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2800 * Called by xfs_bmapi to update file extent records and the btree
2801 * after removing space (or undoing a delayed allocation).
2803 STATIC int /* error */
2804 xfs_bmap_del_extent(
2805 xfs_inode_t *ip, /* incore inode pointer */
2806 xfs_trans_t *tp, /* current transaction pointer */
2807 xfs_extnum_t *idx, /* extent number to update/delete */
2808 xfs_bmap_free_t *flist, /* list of extents to be freed */
2809 xfs_btree_cur_t *cur, /* if null, not a btree */
2810 xfs_bmbt_irec_t *del, /* data to remove from extents */
2811 int *logflagsp, /* inode logging flags */
2812 int whichfork) /* data or attr fork */
2814 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2815 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2816 xfs_fsblock_t del_endblock=0; /* first block past del */
2817 xfs_fileoff_t del_endoff; /* first offset past del */
2818 int delay; /* current block is delayed allocated */
2819 int do_fx; /* free extent at end of routine */
2820 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2821 int error; /* error return value */
2822 int flags; /* inode logging flags */
2823 xfs_bmbt_irec_t got; /* current extent entry */
2824 xfs_fileoff_t got_endoff; /* first offset past got */
2825 int i; /* temp state */
2826 xfs_ifork_t *ifp; /* inode fork pointer */
2827 xfs_mount_t *mp; /* mount structure */
2828 xfs_filblks_t nblks; /* quota/sb block count */
2829 xfs_bmbt_irec_t new; /* new record to be inserted */
2831 uint qfield; /* quota field to update */
2832 xfs_filblks_t temp; /* for indirect length calculations */
2833 xfs_filblks_t temp2; /* for indirect length calculations */
2836 XFS_STATS_INC(xs_del_exlist);
2838 if (whichfork == XFS_ATTR_FORK)
2839 state |= BMAP_ATTRFORK;
2842 ifp = XFS_IFORK_PTR(ip, whichfork);
2843 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
2844 (uint)sizeof(xfs_bmbt_rec_t)));
2845 ASSERT(del->br_blockcount > 0);
2846 ep = xfs_iext_get_ext(ifp, *idx);
2847 xfs_bmbt_get_all(ep, &got);
2848 ASSERT(got.br_startoff <= del->br_startoff);
2849 del_endoff = del->br_startoff + del->br_blockcount;
2850 got_endoff = got.br_startoff + got.br_blockcount;
2851 ASSERT(got_endoff >= del_endoff);
2852 delay = isnullstartblock(got.br_startblock);
2853 ASSERT(isnullstartblock(del->br_startblock) == delay);
2858 * If deleting a real allocation, must free up the disk space.
2861 flags = XFS_ILOG_CORE;
2863 * Realtime allocation. Free it and record di_nblocks update.
2865 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2869 ASSERT(do_mod(del->br_blockcount,
2870 mp->m_sb.sb_rextsize) == 0);
2871 ASSERT(do_mod(del->br_startblock,
2872 mp->m_sb.sb_rextsize) == 0);
2873 bno = del->br_startblock;
2874 len = del->br_blockcount;
2875 do_div(bno, mp->m_sb.sb_rextsize);
2876 do_div(len, mp->m_sb.sb_rextsize);
2877 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2881 nblks = len * mp->m_sb.sb_rextsize;
2882 qfield = XFS_TRANS_DQ_RTBCOUNT;
2885 * Ordinary allocation.
2889 nblks = del->br_blockcount;
2890 qfield = XFS_TRANS_DQ_BCOUNT;
2893 * Set up del_endblock and cur for later.
2895 del_endblock = del->br_startblock + del->br_blockcount;
2897 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2898 got.br_startblock, got.br_blockcount,
2901 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2903 da_old = da_new = 0;
2905 da_old = startblockval(got.br_startblock);
2911 * Set flag value to use in switch statement.
2912 * Left-contig is 2, right-contig is 1.
2914 switch (((got.br_startoff == del->br_startoff) << 1) |
2915 (got_endoff == del_endoff)) {
2918 * Matches the whole extent. Delete the entry.
2920 xfs_iext_remove(ip, *idx, 1,
2921 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2928 flags |= XFS_ILOG_CORE;
2930 flags |= xfs_ilog_fext(whichfork);
2933 if ((error = xfs_btree_delete(cur, &i)))
2935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2940 * Deleting the first part of the extent.
2942 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2943 xfs_bmbt_set_startoff(ep, del_endoff);
2944 temp = got.br_blockcount - del->br_blockcount;
2945 xfs_bmbt_set_blockcount(ep, temp);
2947 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2949 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2950 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2954 xfs_bmbt_set_startblock(ep, del_endblock);
2955 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2957 flags |= xfs_ilog_fext(whichfork);
2960 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2961 got.br_blockcount - del->br_blockcount,
2968 * Deleting the last part of the extent.
2970 temp = got.br_blockcount - del->br_blockcount;
2971 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2972 xfs_bmbt_set_blockcount(ep, temp);
2974 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2976 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2977 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2981 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2983 flags |= xfs_ilog_fext(whichfork);
2986 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2988 got.br_blockcount - del->br_blockcount,
2995 * Deleting the middle of the extent.
2997 temp = del->br_startoff - got.br_startoff;
2998 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2999 xfs_bmbt_set_blockcount(ep, temp);
3000 new.br_startoff = del_endoff;
3001 temp2 = got_endoff - del_endoff;
3002 new.br_blockcount = temp2;
3003 new.br_state = got.br_state;
3005 new.br_startblock = del_endblock;
3006 flags |= XFS_ILOG_CORE;
3008 if ((error = xfs_bmbt_update(cur,
3010 got.br_startblock, temp,
3013 if ((error = xfs_btree_increment(cur, 0, &i)))
3015 cur->bc_rec.b = new;
3016 error = xfs_btree_insert(cur, &i);
3017 if (error && error != ENOSPC)
3020 * If get no-space back from btree insert,
3021 * it tried a split, and we have a zero
3022 * block reservation.
3023 * Fix up our state and return the error.
3025 if (error == ENOSPC) {
3027 * Reset the cursor, don't trust
3028 * it after any insert operation.
3030 if ((error = xfs_bmbt_lookup_eq(cur,
3035 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3037 * Update the btree record back
3038 * to the original value.
3040 if ((error = xfs_bmbt_update(cur,
3047 * Reset the extent record back
3048 * to the original value.
3050 xfs_bmbt_set_blockcount(ep,
3053 error = XFS_ERROR(ENOSPC);
3056 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3058 flags |= xfs_ilog_fext(whichfork);
3059 XFS_IFORK_NEXT_SET(ip, whichfork,
3060 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3062 ASSERT(whichfork == XFS_DATA_FORK);
3063 temp = xfs_bmap_worst_indlen(ip, temp);
3064 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3065 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3066 new.br_startblock = nullstartblock((int)temp2);
3067 da_new = temp + temp2;
3068 while (da_new > da_old) {
3072 xfs_bmbt_set_startblock(ep,
3073 nullstartblock((int)temp));
3075 if (da_new == da_old)
3081 nullstartblock((int)temp2);
3085 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3086 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3091 * If we need to, add to list of extents to delete.
3094 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3097 * Adjust inode # blocks in the file.
3100 ip->i_d.di_nblocks -= nblks;
3102 * Adjust quota data.
3105 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3108 * Account for change in delayed indirect blocks.
3109 * Nothing to do for disk quota accounting here.
3111 ASSERT(da_old >= da_new);
3112 if (da_old > da_new) {
3113 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3114 (int64_t)(da_old - da_new), 0);
3122 * Remove the entry "free" from the free item list. Prev points to the
3123 * previous entry, unless "free" is the head of the list.
3127 xfs_bmap_free_t *flist, /* free item list header */
3128 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3129 xfs_bmap_free_item_t *free) /* list item to be freed */
3132 prev->xbfi_next = free->xbfi_next;
3134 flist->xbf_first = free->xbfi_next;
3136 kmem_zone_free(xfs_bmap_free_item_zone, free);
3140 * Convert an extents-format file into a btree-format file.
3141 * The new file will have a root block (in the inode) and a single child block.
3143 STATIC int /* error */
3144 xfs_bmap_extents_to_btree(
3145 xfs_trans_t *tp, /* transaction pointer */
3146 xfs_inode_t *ip, /* incore inode pointer */
3147 xfs_fsblock_t *firstblock, /* first-block-allocated */
3148 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3149 xfs_btree_cur_t **curp, /* cursor returned to caller */
3150 int wasdel, /* converting a delayed alloc */
3151 int *logflagsp, /* inode logging flags */
3152 int whichfork) /* data or attr fork */
3154 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3155 xfs_buf_t *abp; /* buffer for ablock */
3156 xfs_alloc_arg_t args; /* allocation arguments */
3157 xfs_bmbt_rec_t *arp; /* child record pointer */
3158 struct xfs_btree_block *block; /* btree root block */
3159 xfs_btree_cur_t *cur; /* bmap btree cursor */
3160 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3161 int error; /* error return value */
3162 xfs_extnum_t i, cnt; /* extent record index */
3163 xfs_ifork_t *ifp; /* inode fork pointer */
3164 xfs_bmbt_key_t *kp; /* root block key pointer */
3165 xfs_mount_t *mp; /* mount structure */
3166 xfs_extnum_t nextents; /* number of file extents */
3167 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3169 ifp = XFS_IFORK_PTR(ip, whichfork);
3170 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3171 ASSERT(ifp->if_ext_max ==
3172 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3174 * Make space in the inode incore.
3176 xfs_iroot_realloc(ip, 1, whichfork);
3177 ifp->if_flags |= XFS_IFBROOT;
3182 block = ifp->if_broot;
3183 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3184 block->bb_level = cpu_to_be16(1);
3185 block->bb_numrecs = cpu_to_be16(1);
3186 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3187 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3190 * Need a cursor. Can't allocate until bb_level is filled in.
3193 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3194 cur->bc_private.b.firstblock = *firstblock;
3195 cur->bc_private.b.flist = flist;
3196 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3198 * Convert to a btree with two levels, one record in root.
3200 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3203 args.firstblock = *firstblock;
3204 if (*firstblock == NULLFSBLOCK) {
3205 args.type = XFS_ALLOCTYPE_START_BNO;
3206 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3207 } else if (flist->xbf_low) {
3208 args.type = XFS_ALLOCTYPE_START_BNO;
3209 args.fsbno = *firstblock;
3211 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3212 args.fsbno = *firstblock;
3214 args.minlen = args.maxlen = args.prod = 1;
3215 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3216 args.minalignslop = 0;
3217 args.wasdel = wasdel;
3219 if ((error = xfs_alloc_vextent(&args))) {
3220 xfs_iroot_realloc(ip, -1, whichfork);
3221 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3225 * Allocation can't fail, the space was reserved.
3227 ASSERT(args.fsbno != NULLFSBLOCK);
3228 ASSERT(*firstblock == NULLFSBLOCK ||
3229 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3231 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3232 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3233 cur->bc_private.b.allocated++;
3234 ip->i_d.di_nblocks++;
3235 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3236 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3238 * Fill in the child block.
3240 ablock = XFS_BUF_TO_BLOCK(abp);
3241 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3242 ablock->bb_level = 0;
3243 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3244 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3245 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3246 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3247 for (cnt = i = 0; i < nextents; i++) {
3248 ep = xfs_iext_get_ext(ifp, i);
3249 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3250 arp->l0 = cpu_to_be64(ep->l0);
3251 arp->l1 = cpu_to_be64(ep->l1);
3255 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3256 xfs_btree_set_numrecs(ablock, cnt);
3259 * Fill in the root key and pointer.
3261 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3262 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3263 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3264 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3265 be16_to_cpu(block->bb_level)));
3266 *pp = cpu_to_be64(args.fsbno);
3269 * Do all this logging at the end so that
3270 * the root is at the right level.
3272 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3273 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3274 ASSERT(*curp == NULL);
3276 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3281 * Calculate the default attribute fork offset for newly created inodes.
3284 xfs_default_attroffset(
3285 struct xfs_inode *ip)
3287 struct xfs_mount *mp = ip->i_mount;
3290 if (mp->m_sb.sb_inodesize == 256) {
3291 offset = XFS_LITINO(mp) -
3292 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3294 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3297 ASSERT(offset < XFS_LITINO(mp));
3302 * Helper routine to reset inode di_forkoff field when switching
3303 * attribute fork from local to extent format - we reset it where
3304 * possible to make space available for inline data fork extents.
3307 xfs_bmap_forkoff_reset(
3312 if (whichfork == XFS_ATTR_FORK &&
3313 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3314 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3315 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3316 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3318 if (dfl_forkoff > ip->i_d.di_forkoff) {
3319 ip->i_d.di_forkoff = dfl_forkoff;
3320 ip->i_df.if_ext_max =
3321 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3322 ip->i_afp->if_ext_max =
3323 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3329 * Convert a local file to an extents file.
3330 * This code is out of bounds for data forks of regular files,
3331 * since the file data needs to get logged so things will stay consistent.
3332 * (The bmap-level manipulations are ok, though).
3334 STATIC int /* error */
3335 xfs_bmap_local_to_extents(
3336 xfs_trans_t *tp, /* transaction pointer */
3337 xfs_inode_t *ip, /* incore inode pointer */
3338 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3339 xfs_extlen_t total, /* total blocks needed by transaction */
3340 int *logflagsp, /* inode logging flags */
3341 int whichfork) /* data or attr fork */
3343 int error; /* error return value */
3344 int flags; /* logging flags returned */
3345 xfs_ifork_t *ifp; /* inode fork pointer */
3348 * We don't want to deal with the case of keeping inode data inline yet.
3349 * So sending the data fork of a regular inode is invalid.
3351 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3352 whichfork == XFS_DATA_FORK));
3353 ifp = XFS_IFORK_PTR(ip, whichfork);
3354 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3357 if (ifp->if_bytes) {
3358 xfs_alloc_arg_t args; /* allocation arguments */
3359 xfs_buf_t *bp; /* buffer for extent block */
3360 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3363 args.mp = ip->i_mount;
3364 args.firstblock = *firstblock;
3365 ASSERT((ifp->if_flags &
3366 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3368 * Allocate a block. We know we need only one, since the
3369 * file currently fits in an inode.
3371 if (*firstblock == NULLFSBLOCK) {
3372 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3373 args.type = XFS_ALLOCTYPE_START_BNO;
3375 args.fsbno = *firstblock;
3376 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3379 args.mod = args.minleft = args.alignment = args.wasdel =
3380 args.isfl = args.minalignslop = 0;
3381 args.minlen = args.maxlen = args.prod = 1;
3382 if ((error = xfs_alloc_vextent(&args)))
3385 * Can't fail, the space was reserved.
3387 ASSERT(args.fsbno != NULLFSBLOCK);
3388 ASSERT(args.len == 1);
3389 *firstblock = args.fsbno;
3390 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3391 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3393 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3394 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3395 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3396 xfs_iext_add(ifp, 0, 1);
3397 ep = xfs_iext_get_ext(ifp, 0);
3398 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3399 trace_xfs_bmap_post_update(ip, 0,
3400 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3402 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3403 ip->i_d.di_nblocks = 1;
3404 xfs_trans_mod_dquot_byino(tp, ip,
3405 XFS_TRANS_DQ_BCOUNT, 1L);
3406 flags |= xfs_ilog_fext(whichfork);
3408 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3409 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3411 ifp->if_flags &= ~XFS_IFINLINE;
3412 ifp->if_flags |= XFS_IFEXTENTS;
3413 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3414 flags |= XFS_ILOG_CORE;
3421 * Search the extent records for the entry containing block bno.
3422 * If bno lies in a hole, point to the next entry. If bno lies
3423 * past eof, *eofp will be set, and *prevp will contain the last
3424 * entry (null if none). Else, *lastxp will be set to the index
3425 * of the found entry; *gotp will contain the entry.
3427 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3428 xfs_bmap_search_multi_extents(
3429 xfs_ifork_t *ifp, /* inode fork pointer */
3430 xfs_fileoff_t bno, /* block number searched for */
3431 int *eofp, /* out: end of file found */
3432 xfs_extnum_t *lastxp, /* out: last extent index */
3433 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3434 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3436 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3437 xfs_extnum_t lastx; /* last extent index */
3440 * Initialize the extent entry structure to catch access to
3441 * uninitialized br_startblock field.
3443 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3444 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3445 gotp->br_state = XFS_EXT_INVALID;
3447 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3449 gotp->br_startblock = 0xffffa5a5;
3451 prevp->br_startoff = NULLFILEOFF;
3453 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3455 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3457 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3458 xfs_bmbt_get_all(ep, gotp);
3472 * Search the extents list for the inode, for the extent containing bno.
3473 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3474 * *eofp will be set, and *prevp will contain the last entry (null if none).
3475 * Else, *lastxp will be set to the index of the found
3476 * entry; *gotp will contain the entry.
3478 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3479 xfs_bmap_search_extents(
3480 xfs_inode_t *ip, /* incore inode pointer */
3481 xfs_fileoff_t bno, /* block number searched for */
3482 int fork, /* data or attr fork */
3483 int *eofp, /* out: end of file found */
3484 xfs_extnum_t *lastxp, /* out: last extent index */
3485 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3486 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3488 xfs_ifork_t *ifp; /* inode fork pointer */
3489 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3491 XFS_STATS_INC(xs_look_exlist);
3492 ifp = XFS_IFORK_PTR(ip, fork);
3494 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3496 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3497 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3498 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
3499 "Access to block zero in inode %llu "
3500 "start_block: %llx start_off: %llx "
3501 "blkcnt: %llx extent-state: %x lastx: %x\n",
3502 (unsigned long long)ip->i_ino,
3503 (unsigned long long)gotp->br_startblock,
3504 (unsigned long long)gotp->br_startoff,
3505 (unsigned long long)gotp->br_blockcount,
3506 gotp->br_state, *lastxp);
3507 *lastxp = NULLEXTNUM;
3515 * Compute the worst-case number of indirect blocks that will be used
3516 * for ip's delayed extent of length "len".
3518 STATIC xfs_filblks_t
3519 xfs_bmap_worst_indlen(
3520 xfs_inode_t *ip, /* incore inode pointer */
3521 xfs_filblks_t len) /* delayed extent length */
3523 int level; /* btree level number */
3524 int maxrecs; /* maximum record count at this level */
3525 xfs_mount_t *mp; /* mount structure */
3526 xfs_filblks_t rval; /* return value */
3529 maxrecs = mp->m_bmap_dmxr[0];
3530 for (level = 0, rval = 0;
3531 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3534 do_div(len, maxrecs);
3537 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3540 maxrecs = mp->m_bmap_dmxr[1];
3546 * Convert inode from non-attributed to attributed.
3547 * Must not be in a transaction, ip must not be locked.
3549 int /* error code */
3550 xfs_bmap_add_attrfork(
3551 xfs_inode_t *ip, /* incore inode pointer */
3552 int size, /* space new attribute needs */
3553 int rsvd) /* xact may use reserved blks */
3555 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3556 xfs_bmap_free_t flist; /* freed extent records */
3557 xfs_mount_t *mp; /* mount structure */
3558 xfs_trans_t *tp; /* transaction pointer */
3559 int blks; /* space reservation */
3560 int version = 1; /* superblock attr version */
3561 int committed; /* xaction was committed */
3562 int logflags; /* logging flags */
3563 int error; /* error return value */
3565 ASSERT(XFS_IFORK_Q(ip) == 0);
3566 ASSERT(ip->i_df.if_ext_max ==
3567 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3570 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3571 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3572 blks = XFS_ADDAFORK_SPACE_RES(mp);
3574 tp->t_flags |= XFS_TRANS_RESERVE;
3575 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3576 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3578 xfs_ilock(ip, XFS_ILOCK_EXCL);
3579 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3580 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3581 XFS_QMOPT_RES_REGBLKS);
3583 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3584 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3587 if (XFS_IFORK_Q(ip))
3589 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3591 * For inodes coming from pre-6.2 filesystems.
3593 ASSERT(ip->i_d.di_aformat == 0);
3594 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3596 ASSERT(ip->i_d.di_anextents == 0);
3598 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3599 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3601 switch (ip->i_d.di_format) {
3602 case XFS_DINODE_FMT_DEV:
3603 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3605 case XFS_DINODE_FMT_UUID:
3606 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3608 case XFS_DINODE_FMT_LOCAL:
3609 case XFS_DINODE_FMT_EXTENTS:
3610 case XFS_DINODE_FMT_BTREE:
3611 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3612 if (!ip->i_d.di_forkoff)
3613 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3614 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3619 error = XFS_ERROR(EINVAL);
3622 ip->i_df.if_ext_max =
3623 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3624 ASSERT(ip->i_afp == NULL);
3625 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3626 ip->i_afp->if_ext_max =
3627 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3628 ip->i_afp->if_flags = XFS_IFEXTENTS;
3630 xfs_bmap_init(&flist, &firstblock);
3631 switch (ip->i_d.di_format) {
3632 case XFS_DINODE_FMT_LOCAL:
3633 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3636 case XFS_DINODE_FMT_EXTENTS:
3637 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3640 case XFS_DINODE_FMT_BTREE:
3641 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3649 xfs_trans_log_inode(tp, ip, logflags);
3652 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3653 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3654 __int64_t sbfields = 0;
3656 spin_lock(&mp->m_sb_lock);
3657 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3658 xfs_sb_version_addattr(&mp->m_sb);
3659 sbfields |= XFS_SB_VERSIONNUM;
3661 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3662 xfs_sb_version_addattr2(&mp->m_sb);
3663 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3666 spin_unlock(&mp->m_sb_lock);
3667 xfs_mod_sb(tp, sbfields);
3669 spin_unlock(&mp->m_sb_lock);
3671 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3673 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3674 ASSERT(ip->i_df.if_ext_max ==
3675 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3678 xfs_bmap_cancel(&flist);
3680 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3682 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3683 ASSERT(ip->i_df.if_ext_max ==
3684 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3689 * Add the extent to the list of extents to be free at transaction end.
3690 * The list is maintained sorted (by block number).
3695 xfs_fsblock_t bno, /* fs block number of extent */
3696 xfs_filblks_t len, /* length of extent */
3697 xfs_bmap_free_t *flist, /* list of extents */
3698 xfs_mount_t *mp) /* mount point structure */
3700 xfs_bmap_free_item_t *cur; /* current (next) element */
3701 xfs_bmap_free_item_t *new; /* new element */
3702 xfs_bmap_free_item_t *prev; /* previous element */
3704 xfs_agnumber_t agno;
3705 xfs_agblock_t agbno;
3707 ASSERT(bno != NULLFSBLOCK);
3709 ASSERT(len <= MAXEXTLEN);
3710 ASSERT(!isnullstartblock(bno));
3711 agno = XFS_FSB_TO_AGNO(mp, bno);
3712 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3713 ASSERT(agno < mp->m_sb.sb_agcount);
3714 ASSERT(agbno < mp->m_sb.sb_agblocks);
3715 ASSERT(len < mp->m_sb.sb_agblocks);
3716 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3718 ASSERT(xfs_bmap_free_item_zone != NULL);
3719 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3720 new->xbfi_startblock = bno;
3721 new->xbfi_blockcount = (xfs_extlen_t)len;
3722 for (prev = NULL, cur = flist->xbf_first;
3724 prev = cur, cur = cur->xbfi_next) {
3725 if (cur->xbfi_startblock >= bno)
3729 prev->xbfi_next = new;
3731 flist->xbf_first = new;
3732 new->xbfi_next = cur;
3737 * Compute and fill in the value of the maximum depth of a bmap btree
3738 * in this filesystem. Done once, during mount.
3741 xfs_bmap_compute_maxlevels(
3742 xfs_mount_t *mp, /* file system mount structure */
3743 int whichfork) /* data or attr fork */
3745 int level; /* btree level */
3746 uint maxblocks; /* max blocks at this level */
3747 uint maxleafents; /* max leaf entries possible */
3748 int maxrootrecs; /* max records in root block */
3749 int minleafrecs; /* min records in leaf block */
3750 int minnoderecs; /* min records in node block */
3751 int sz; /* root block size */
3754 * The maximum number of extents in a file, hence the maximum
3755 * number of leaf entries, is controlled by the type of di_nextents
3756 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3757 * (a signed 16-bit number, xfs_aextnum_t).
3759 * Note that we can no longer assume that if we are in ATTR1 that
3760 * the fork offset of all the inodes will be
3761 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3762 * with ATTR2 and then mounted back with ATTR1, keeping the
3763 * di_forkoff's fixed but probably at various positions. Therefore,
3764 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3765 * of a minimum size available.
3767 if (whichfork == XFS_DATA_FORK) {
3768 maxleafents = MAXEXTNUM;
3769 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3771 maxleafents = MAXAEXTNUM;
3772 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3774 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3775 minleafrecs = mp->m_bmap_dmnr[0];
3776 minnoderecs = mp->m_bmap_dmnr[1];
3777 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3778 for (level = 1; maxblocks > 1; level++) {
3779 if (maxblocks <= maxrootrecs)
3782 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3784 mp->m_bm_maxlevels[whichfork] = level;
3788 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3789 * caller. Frees all the extents that need freeing, which must be done
3790 * last due to locking considerations. We never free any extents in
3791 * the first transaction. This is to allow the caller to make the first
3792 * transaction a synchronous one so that the pointers to the data being
3793 * broken in this transaction will be permanent before the data is actually
3794 * freed. This is necessary to prevent blocks from being reallocated
3795 * and written to before the free and reallocation are actually permanent.
3796 * We do not just make the first transaction synchronous here, because
3797 * there are more efficient ways to gain the same protection in some cases
3798 * (see the file truncation code).
3800 * Return 1 if the given transaction was committed and a new one
3801 * started, and 0 otherwise in the committed parameter.
3806 xfs_trans_t **tp, /* transaction pointer addr */
3807 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3808 int *committed) /* xact committed or not */
3810 xfs_efd_log_item_t *efd; /* extent free data */
3811 xfs_efi_log_item_t *efi; /* extent free intention */
3812 int error; /* error return value */
3813 xfs_bmap_free_item_t *free; /* free extent item */
3814 unsigned int logres; /* new log reservation */
3815 unsigned int logcount; /* new log count */
3816 xfs_mount_t *mp; /* filesystem mount structure */
3817 xfs_bmap_free_item_t *next; /* next item on free list */
3818 xfs_trans_t *ntp; /* new transaction pointer */
3820 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3821 if (flist->xbf_count == 0) {
3826 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3827 for (free = flist->xbf_first; free; free = free->xbfi_next)
3828 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3829 free->xbfi_blockcount);
3830 logres = ntp->t_log_res;
3831 logcount = ntp->t_log_count;
3832 ntp = xfs_trans_dup(*tp);
3833 error = xfs_trans_commit(*tp, 0);
3837 * We have a new transaction, so we should return committed=1,
3838 * even though we're returning an error.
3844 * transaction commit worked ok so we can drop the extra ticket
3845 * reference that we gained in xfs_trans_dup()
3847 xfs_log_ticket_put(ntp->t_ticket);
3849 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3852 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3853 for (free = flist->xbf_first; free != NULL; free = next) {
3854 next = free->xbfi_next;
3855 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3856 free->xbfi_blockcount))) {
3858 * The bmap free list will be cleaned up at a
3859 * higher level. The EFI will be canceled when
3860 * this transaction is aborted.
3861 * Need to force shutdown here to make sure it
3862 * happens, since this transaction may not be
3866 if (!XFS_FORCED_SHUTDOWN(mp))
3867 xfs_force_shutdown(mp,
3868 (error == EFSCORRUPTED) ?
3869 SHUTDOWN_CORRUPT_INCORE :
3870 SHUTDOWN_META_IO_ERROR);
3873 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3874 free->xbfi_blockcount);
3875 xfs_bmap_del_free(flist, NULL, free);
3881 * Free up any items left in the list.
3885 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3887 xfs_bmap_free_item_t *free; /* free list item */
3888 xfs_bmap_free_item_t *next;
3890 if (flist->xbf_count == 0)
3892 ASSERT(flist->xbf_first != NULL);
3893 for (free = flist->xbf_first; free; free = next) {
3894 next = free->xbfi_next;
3895 xfs_bmap_del_free(flist, NULL, free);
3897 ASSERT(flist->xbf_count == 0);
3901 * Returns the file-relative block number of the first unused block(s)
3902 * in the file with at least "len" logically contiguous blocks free.
3903 * This is the lowest-address hole if the file has holes, else the first block
3904 * past the end of file.
3905 * Return 0 if the file is currently local (in-inode).
3908 xfs_bmap_first_unused(
3909 xfs_trans_t *tp, /* transaction pointer */
3910 xfs_inode_t *ip, /* incore inode */
3911 xfs_extlen_t len, /* size of hole to find */
3912 xfs_fileoff_t *first_unused, /* unused block */
3913 int whichfork) /* data or attr fork */
3915 int error; /* error return value */
3916 int idx; /* extent record index */
3917 xfs_ifork_t *ifp; /* inode fork pointer */
3918 xfs_fileoff_t lastaddr; /* last block number seen */
3919 xfs_fileoff_t lowest; /* lowest useful block */
3920 xfs_fileoff_t max; /* starting useful block */
3921 xfs_fileoff_t off; /* offset for this block */
3922 xfs_extnum_t nextents; /* number of extent entries */
3924 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3925 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3926 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3927 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3931 ifp = XFS_IFORK_PTR(ip, whichfork);
3932 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3933 (error = xfs_iread_extents(tp, ip, whichfork)))
3935 lowest = *first_unused;
3936 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3937 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3938 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3939 off = xfs_bmbt_get_startoff(ep);
3941 * See if the hole before this extent will work.
3943 if (off >= lowest + len && off - max >= len) {
3944 *first_unused = max;
3947 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3948 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3950 *first_unused = max;
3955 * Returns the file-relative block number of the last block + 1 before
3956 * last_block (input value) in the file.
3957 * This is not based on i_size, it is based on the extent records.
3958 * Returns 0 for local files, as they do not have extent records.
3961 xfs_bmap_last_before(
3962 xfs_trans_t *tp, /* transaction pointer */
3963 xfs_inode_t *ip, /* incore inode */
3964 xfs_fileoff_t *last_block, /* last block */
3965 int whichfork) /* data or attr fork */
3967 xfs_fileoff_t bno; /* input file offset */
3968 int eof; /* hit end of file */
3969 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3970 int error; /* error return value */
3971 xfs_bmbt_irec_t got; /* current extent value */
3972 xfs_ifork_t *ifp; /* inode fork pointer */
3973 xfs_extnum_t lastx; /* last extent used */
3974 xfs_bmbt_irec_t prev; /* previous extent value */
3976 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3977 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3978 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3979 return XFS_ERROR(EIO);
3980 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3984 ifp = XFS_IFORK_PTR(ip, whichfork);
3985 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3986 (error = xfs_iread_extents(tp, ip, whichfork)))
3988 bno = *last_block - 1;
3989 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3991 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3992 if (prev.br_startoff == NULLFILEOFF)
3995 *last_block = prev.br_startoff + prev.br_blockcount;
3998 * Otherwise *last_block is already the right answer.
4004 * Returns the file-relative block number of the first block past eof in
4005 * the file. This is not based on i_size, it is based on the extent records.
4006 * Returns 0 for local files, as they do not have extent records.
4009 xfs_bmap_last_offset(
4010 xfs_trans_t *tp, /* transaction pointer */
4011 xfs_inode_t *ip, /* incore inode */
4012 xfs_fileoff_t *last_block, /* last block */
4013 int whichfork) /* data or attr fork */
4015 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4016 int error; /* error return value */
4017 xfs_ifork_t *ifp; /* inode fork pointer */
4018 xfs_extnum_t nextents; /* number of extent entries */
4020 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4021 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4023 return XFS_ERROR(EIO);
4024 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4028 ifp = XFS_IFORK_PTR(ip, whichfork);
4029 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4030 (error = xfs_iread_extents(tp, ip, whichfork)))
4032 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4037 ep = xfs_iext_get_ext(ifp, nextents - 1);
4038 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4043 * Returns whether the selected fork of the inode has exactly one
4044 * block or not. For the data fork we check this matches di_size,
4045 * implying the file's range is 0..bsize-1.
4047 int /* 1=>1 block, 0=>otherwise */
4049 xfs_inode_t *ip, /* incore inode */
4050 int whichfork) /* data or attr fork */
4052 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4053 xfs_ifork_t *ifp; /* inode fork pointer */
4054 int rval; /* return value */
4055 xfs_bmbt_irec_t s; /* internal version of extent */
4058 if (whichfork == XFS_DATA_FORK) {
4059 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4060 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4061 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4064 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4066 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4068 ifp = XFS_IFORK_PTR(ip, whichfork);
4069 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4070 ep = xfs_iext_get_ext(ifp, 0);
4071 xfs_bmbt_get_all(ep, &s);
4072 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4073 if (rval && whichfork == XFS_DATA_FORK)
4074 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4079 xfs_bmap_sanity_check(
4080 struct xfs_mount *mp,
4084 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4086 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4087 be16_to_cpu(block->bb_level) != level ||
4088 be16_to_cpu(block->bb_numrecs) == 0 ||
4089 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4095 * Read in the extents to if_extents.
4096 * All inode fields are set up by caller, we just traverse the btree
4097 * and copy the records in. If the file system cannot contain unwritten
4098 * extents, the records are checked for no "state" flags.
4101 xfs_bmap_read_extents(
4102 xfs_trans_t *tp, /* transaction pointer */
4103 xfs_inode_t *ip, /* incore inode */
4104 int whichfork) /* data or attr fork */
4106 struct xfs_btree_block *block; /* current btree block */
4107 xfs_fsblock_t bno; /* block # of "block" */
4108 xfs_buf_t *bp; /* buffer for "block" */
4109 int error; /* error return value */
4110 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4111 xfs_extnum_t i, j; /* index into the extents list */
4112 xfs_ifork_t *ifp; /* fork structure */
4113 int level; /* btree level, for checking */
4114 xfs_mount_t *mp; /* file system mount structure */
4115 __be64 *pp; /* pointer to block address */
4117 xfs_extnum_t room; /* number of entries there's room for */
4121 ifp = XFS_IFORK_PTR(ip, whichfork);
4122 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4123 XFS_EXTFMT_INODE(ip);
4124 block = ifp->if_broot;
4126 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4128 level = be16_to_cpu(block->bb_level);
4130 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4131 bno = be64_to_cpu(*pp);
4132 ASSERT(bno != NULLDFSBNO);
4133 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4134 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4136 * Go down the tree until leaf level is reached, following the first
4137 * pointer (leftmost) at each level.
4139 while (level-- > 0) {
4140 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4141 XFS_BMAP_BTREE_REF)))
4143 block = XFS_BUF_TO_BLOCK(bp);
4144 XFS_WANT_CORRUPTED_GOTO(
4145 xfs_bmap_sanity_check(mp, bp, level),
4149 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4150 bno = be64_to_cpu(*pp);
4151 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4152 xfs_trans_brelse(tp, bp);
4155 * Here with bp and block set to the leftmost leaf node in the tree.
4157 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4160 * Loop over all leaf nodes. Copy information to the extent records.
4163 xfs_bmbt_rec_t *frp;
4164 xfs_fsblock_t nextbno;
4165 xfs_extnum_t num_recs;
4169 num_recs = xfs_btree_get_numrecs(block);
4170 if (unlikely(i + num_recs > room)) {
4171 ASSERT(i + num_recs <= room);
4172 xfs_warn(ip->i_mount,
4173 "corrupt dinode %Lu, (btree extents).",
4174 (unsigned long long) ip->i_ino);
4175 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4176 XFS_ERRLEVEL_LOW, ip->i_mount, block);
4179 XFS_WANT_CORRUPTED_GOTO(
4180 xfs_bmap_sanity_check(mp, bp, 0),
4183 * Read-ahead the next leaf block, if any.
4185 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4186 if (nextbno != NULLFSBLOCK)
4187 xfs_btree_reada_bufl(mp, nextbno, 1);
4189 * Copy records into the extent records.
4191 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4193 for (j = 0; j < num_recs; j++, i++, frp++) {
4194 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4195 trp->l0 = be64_to_cpu(frp->l0);
4196 trp->l1 = be64_to_cpu(frp->l1);
4198 if (exntf == XFS_EXTFMT_NOSTATE) {
4200 * Check all attribute bmap btree records and
4201 * any "older" data bmap btree records for a
4202 * set bit in the "extent flag" position.
4204 if (unlikely(xfs_check_nostate_extents(ifp,
4205 start, num_recs))) {
4206 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4212 xfs_trans_brelse(tp, bp);
4215 * If we've reached the end, stop.
4217 if (bno == NULLFSBLOCK)
4219 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4220 XFS_BMAP_BTREE_REF)))
4222 block = XFS_BUF_TO_BLOCK(bp);
4224 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4225 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4226 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4229 xfs_trans_brelse(tp, bp);
4230 return XFS_ERROR(EFSCORRUPTED);
4235 * Add bmap trace insert entries for all the contents of the extent records.
4238 xfs_bmap_trace_exlist(
4239 xfs_inode_t *ip, /* incore inode pointer */
4240 xfs_extnum_t cnt, /* count of entries in the list */
4241 int whichfork, /* data or attr fork */
4242 unsigned long caller_ip)
4244 xfs_extnum_t idx; /* extent record index */
4245 xfs_ifork_t *ifp; /* inode fork pointer */
4248 if (whichfork == XFS_ATTR_FORK)
4249 state |= BMAP_ATTRFORK;
4251 ifp = XFS_IFORK_PTR(ip, whichfork);
4252 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4253 for (idx = 0; idx < cnt; idx++)
4254 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4258 * Validate that the bmbt_irecs being returned from bmapi are valid
4259 * given the callers original parameters. Specifically check the
4260 * ranges of the returned irecs to ensure that they only extent beyond
4261 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4264 xfs_bmap_validate_ret(
4268 xfs_bmbt_irec_t *mval,
4272 int i; /* index to map values */
4274 ASSERT(ret_nmap <= nmap);
4276 for (i = 0; i < ret_nmap; i++) {
4277 ASSERT(mval[i].br_blockcount > 0);
4278 if (!(flags & XFS_BMAPI_ENTIRE)) {
4279 ASSERT(mval[i].br_startoff >= bno);
4280 ASSERT(mval[i].br_blockcount <= len);
4281 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4284 ASSERT(mval[i].br_startoff < bno + len);
4285 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4289 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4290 mval[i].br_startoff);
4291 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4292 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4293 mval[i].br_startblock != HOLESTARTBLOCK);
4294 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4295 mval[i].br_state == XFS_EXT_UNWRITTEN);
4302 * Map file blocks to filesystem blocks.
4303 * File range is given by the bno/len pair.
4304 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4305 * into a hole or past eof.
4306 * Only allocates blocks from a single allocation group,
4307 * to avoid locking problems.
4308 * The returned value in "firstblock" from the first call in a transaction
4309 * must be remembered and presented to subsequent calls in "firstblock".
4310 * An upper bound for the number of blocks to be allocated is supplied to
4311 * the first call in "total"; if no allocation group has that many free
4312 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4316 xfs_trans_t *tp, /* transaction pointer */
4317 xfs_inode_t *ip, /* incore inode */
4318 xfs_fileoff_t bno, /* starting file offs. mapped */
4319 xfs_filblks_t len, /* length to map in file */
4320 int flags, /* XFS_BMAPI_... */
4321 xfs_fsblock_t *firstblock, /* first allocated block
4322 controls a.g. for allocs */
4323 xfs_extlen_t total, /* total blocks needed */
4324 xfs_bmbt_irec_t *mval, /* output: map values */
4325 int *nmap, /* i/o: mval size/count */
4326 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4328 xfs_fsblock_t abno; /* allocated block number */
4329 xfs_extlen_t alen; /* allocated extent length */
4330 xfs_fileoff_t aoff; /* allocated file offset */
4331 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
4332 xfs_btree_cur_t *cur; /* bmap btree cursor */
4333 xfs_fileoff_t end; /* end of mapped file region */
4334 int eof; /* we've hit the end of extents */
4335 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4336 int error; /* error return */
4337 xfs_bmbt_irec_t got; /* current file extent record */
4338 xfs_ifork_t *ifp; /* inode fork pointer */
4339 xfs_extlen_t indlen; /* indirect blocks length */
4340 xfs_extnum_t lastx; /* last useful extent number */
4341 int logflags; /* flags for transaction logging */
4342 xfs_extlen_t minleft; /* min blocks left after allocation */
4343 xfs_extlen_t minlen; /* min allocation size */
4344 xfs_mount_t *mp; /* xfs mount structure */
4345 int n; /* current extent index */
4346 int nallocs; /* number of extents alloc'd */
4347 xfs_extnum_t nextents; /* number of extents in file */
4348 xfs_fileoff_t obno; /* old block number (offset) */
4349 xfs_bmbt_irec_t prev; /* previous file extent record */
4350 int tmp_logflags; /* temp flags holder */
4351 int whichfork; /* data or attr fork */
4352 char inhole; /* current location is hole in file */
4353 char wasdelay; /* old extent was delayed */
4354 char wr; /* this is a write request */
4355 char rt; /* this is a realtime file */
4357 xfs_fileoff_t orig_bno; /* original block number value */
4358 int orig_flags; /* original flags arg value */
4359 xfs_filblks_t orig_len; /* original value of len arg */
4360 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4361 int orig_nmap; /* original value of *nmap */
4370 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4371 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4372 XFS_ATTR_FORK : XFS_DATA_FORK;
4374 if (unlikely(XFS_TEST_ERROR(
4375 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4376 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4377 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4378 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4379 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4380 return XFS_ERROR(EFSCORRUPTED);
4382 if (XFS_FORCED_SHUTDOWN(mp))
4383 return XFS_ERROR(EIO);
4384 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4385 ifp = XFS_IFORK_PTR(ip, whichfork);
4386 ASSERT(ifp->if_ext_max ==
4387 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4388 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4389 XFS_STATS_INC(xs_blk_mapw);
4391 XFS_STATS_INC(xs_blk_mapr);
4393 * IGSTATE flag is used to combine extents which
4394 * differ only due to the state of the extents.
4395 * This technique is used from xfs_getbmap()
4396 * when the caller does not wish to see the
4397 * separation (which is the default).
4399 * This technique is also used when writing a
4400 * buffer which has been partially written,
4401 * (usually by being flushed during a chunkread),
4402 * to ensure one write takes place. This also
4403 * prevents a change in the xfs inode extents at
4404 * this time, intentionally. This change occurs
4405 * on completion of the write operation, in
4406 * xfs_strat_comp(), where the xfs_bmapi() call
4407 * is transactioned, and the extents combined.
4409 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4410 wr = 0; /* no allocations are allowed */
4411 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4415 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4417 if ((error = xfs_bmap_local_to_extents(tp, ip,
4418 firstblock, total, &logflags, whichfork)))
4421 if (wr && *firstblock == NULLFSBLOCK) {
4422 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4423 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4428 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4429 (error = xfs_iread_extents(tp, ip, whichfork)))
4431 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4433 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4439 while (bno < end && n < *nmap) {
4441 * Reading past eof, act as though there's a hole
4445 got.br_startoff = end;
4446 inhole = eof || got.br_startoff > bno;
4447 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4448 isnullstartblock(got.br_startblock);
4450 * First, deal with the hole before the allocated space
4451 * that we found, if any.
4453 if (wr && (inhole || wasdelay)) {
4455 * For the wasdelay case, we could also just
4456 * allocate the stuff asked for in this bmap call
4457 * but that wouldn't be as good.
4460 alen = (xfs_extlen_t)got.br_blockcount;
4461 aoff = got.br_startoff;
4462 if (lastx != NULLEXTNUM && lastx) {
4463 ep = xfs_iext_get_ext(ifp, lastx - 1);
4464 xfs_bmbt_get_all(ep, &prev);
4467 alen = (xfs_extlen_t)
4468 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4470 alen = (xfs_extlen_t)
4471 XFS_FILBLKS_MIN(alen,
4472 got.br_startoff - bno);
4475 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4476 if (flags & XFS_BMAPI_DELAY) {
4479 /* Figure out the extent size, adjust alen */
4480 extsz = xfs_get_extsz_hint(ip);
4483 * make sure we don't exceed a single
4484 * extent length when we align the
4485 * extent by reducing length we are
4486 * going to allocate by the maximum
4487 * amount extent size aligment may
4490 alen = XFS_FILBLKS_MIN(len,
4491 MAXEXTLEN - (2 * extsz - 1));
4492 error = xfs_bmap_extsize_align(mp,
4495 flags&XFS_BMAPI_DELAY,
4496 flags&XFS_BMAPI_CONVERT,
4502 extsz = alen / mp->m_sb.sb_rextsize;
4505 * Make a transaction-less quota reservation for
4506 * delayed allocation blocks. This number gets
4507 * adjusted later. We return if we haven't
4508 * allocated blocks already inside this loop.
4510 error = xfs_trans_reserve_quota_nblks(
4511 NULL, ip, (long)alen, 0,
4512 rt ? XFS_QMOPT_RES_RTBLKS :
4513 XFS_QMOPT_RES_REGBLKS);
4517 ASSERT(cur == NULL);
4524 * Split changing sb for alen and indlen since
4525 * they could be coming from different places.
4527 indlen = (xfs_extlen_t)
4528 xfs_bmap_worst_indlen(ip, alen);
4532 error = xfs_mod_incore_sb(mp,
4534 -((int64_t)extsz), 0);
4536 error = xfs_icsb_modify_counters(mp,
4538 -((int64_t)alen), 0);
4541 error = xfs_icsb_modify_counters(mp,
4543 -((int64_t)indlen), 0);
4545 xfs_mod_incore_sb(mp,
4549 xfs_icsb_modify_counters(mp,
4555 if (XFS_IS_QUOTA_ON(mp))
4556 /* unreserve the blocks now */
4558 xfs_trans_unreserve_quota_nblks(
4561 XFS_QMOPT_RES_RTBLKS :
4562 XFS_QMOPT_RES_REGBLKS);
4566 ip->i_delayed_blks += alen;
4567 abno = nullstartblock(indlen);
4570 * If first time, allocate and fill in
4571 * once-only bma fields.
4573 if (bma.ip == NULL) {
4581 /* Indicate if this is the first user data
4582 * in the file, or just any user data.
4584 if (!(flags & XFS_BMAPI_METADATA)) {
4585 bma.userdata = (aoff == 0) ?
4586 XFS_ALLOC_INITIAL_USER_DATA :
4590 * Fill in changeable bma fields.
4593 bma.firstblock = *firstblock;
4596 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4597 bma.wasdel = wasdelay;
4598 bma.minlen = minlen;
4599 bma.low = flist->xbf_low;
4600 bma.minleft = minleft;
4602 * Only want to do the alignment at the
4603 * eof if it is userdata and allocation length
4604 * is larger than a stripe unit.
4606 if (mp->m_dalign && alen >= mp->m_dalign &&
4607 (!(flags & XFS_BMAPI_METADATA)) &&
4608 (whichfork == XFS_DATA_FORK)) {
4609 if ((error = xfs_bmap_isaeof(ip, aoff,
4610 whichfork, &bma.aeof)))
4617 if ((error = xfs_bmap_alloc(&bma)))
4620 * Copy out result fields.
4623 if ((flist->xbf_low = bma.low))
4627 ASSERT(*firstblock == NULLFSBLOCK ||
4628 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4629 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4631 XFS_FSB_TO_AGNO(mp, *firstblock) <
4632 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4633 *firstblock = bma.firstblock;
4635 cur->bc_private.b.firstblock =
4637 if (abno == NULLFSBLOCK)
4639 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4640 cur = xfs_bmbt_init_cursor(mp, tp,
4642 cur->bc_private.b.firstblock =
4644 cur->bc_private.b.flist = flist;
4647 * Bump the number of extents we've allocated
4653 cur->bc_private.b.flags =
4654 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4655 got.br_startoff = aoff;
4656 got.br_startblock = abno;
4657 got.br_blockcount = alen;
4658 got.br_state = XFS_EXT_NORM; /* assume normal */
4660 * Determine state of extent, and the filesystem.
4661 * A wasdelay extent has been initialized, so
4662 * shouldn't be flagged as unwritten.
4664 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4665 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4666 got.br_state = XFS_EXT_UNWRITTEN;
4668 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &got,
4669 firstblock, flist, &tmp_logflags,
4671 logflags |= tmp_logflags;
4674 ep = xfs_iext_get_ext(ifp, lastx);
4675 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4676 xfs_bmbt_get_all(ep, &got);
4677 ASSERT(got.br_startoff <= aoff);
4678 ASSERT(got.br_startoff + got.br_blockcount >=
4681 if (flags & XFS_BMAPI_DELAY) {
4682 ASSERT(isnullstartblock(got.br_startblock));
4683 ASSERT(startblockval(got.br_startblock) > 0);
4685 ASSERT(got.br_state == XFS_EXT_NORM ||
4686 got.br_state == XFS_EXT_UNWRITTEN);
4689 * Fall down into the found allocated space case.
4691 } else if (inhole) {
4693 * Reading in a hole.
4695 mval->br_startoff = bno;
4696 mval->br_startblock = HOLESTARTBLOCK;
4697 mval->br_blockcount =
4698 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4699 mval->br_state = XFS_EXT_NORM;
4700 bno += mval->br_blockcount;
4701 len -= mval->br_blockcount;
4707 * Then deal with the allocated space we found.
4710 if (!(flags & XFS_BMAPI_ENTIRE) &&
4711 (got.br_startoff + got.br_blockcount > obno)) {
4714 ASSERT((bno >= obno) || (n == 0));
4716 mval->br_startoff = bno;
4717 if (isnullstartblock(got.br_startblock)) {
4718 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4719 mval->br_startblock = DELAYSTARTBLOCK;
4721 mval->br_startblock =
4723 (bno - got.br_startoff);
4725 * Return the minimum of what we got and what we
4726 * asked for for the length. We can use the len
4727 * variable here because it is modified below
4728 * and we could have been there before coming
4729 * here if the first part of the allocation
4730 * didn't overlap what was asked for.
4732 mval->br_blockcount =
4733 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4734 (bno - got.br_startoff));
4735 mval->br_state = got.br_state;
4736 ASSERT(mval->br_blockcount <= len);
4739 if (isnullstartblock(mval->br_startblock)) {
4740 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4741 mval->br_startblock = DELAYSTARTBLOCK;
4746 * Check if writing previously allocated but
4747 * unwritten extents.
4750 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4751 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4752 (mval->br_state == XFS_EXT_NORM &&
4753 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4754 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
4756 * Modify (by adding) the state flag, if writing.
4758 ASSERT(mval->br_blockcount <= len);
4759 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4760 cur = xfs_bmbt_init_cursor(mp,
4762 cur->bc_private.b.firstblock =
4764 cur->bc_private.b.flist = flist;
4766 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4768 : XFS_EXT_UNWRITTEN;
4769 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, mval,
4770 firstblock, flist, &tmp_logflags,
4772 logflags |= tmp_logflags;
4775 ep = xfs_iext_get_ext(ifp, lastx);
4776 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4777 xfs_bmbt_get_all(ep, &got);
4779 * We may have combined previously unwritten
4780 * space with written space, so generate
4783 if (mval->br_blockcount < len)
4787 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4788 ((mval->br_startoff + mval->br_blockcount) <= end));
4789 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4790 (mval->br_blockcount <= len) ||
4791 (mval->br_startoff < obno));
4792 bno = mval->br_startoff + mval->br_blockcount;
4794 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4795 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4796 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4797 ASSERT(mval->br_state == mval[-1].br_state);
4798 mval[-1].br_blockcount = mval->br_blockcount;
4799 mval[-1].br_state = mval->br_state;
4800 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4801 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4802 mval[-1].br_startblock != HOLESTARTBLOCK &&
4803 mval->br_startblock ==
4804 mval[-1].br_startblock + mval[-1].br_blockcount &&
4805 ((flags & XFS_BMAPI_IGSTATE) ||
4806 mval[-1].br_state == mval->br_state)) {
4807 ASSERT(mval->br_startoff ==
4808 mval[-1].br_startoff + mval[-1].br_blockcount);
4809 mval[-1].br_blockcount += mval->br_blockcount;
4811 mval->br_startblock == DELAYSTARTBLOCK &&
4812 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4813 mval->br_startoff ==
4814 mval[-1].br_startoff + mval[-1].br_blockcount) {
4815 mval[-1].br_blockcount += mval->br_blockcount;
4816 mval[-1].br_state = mval->br_state;
4817 } else if (!((n == 0) &&
4818 ((mval->br_startoff + mval->br_blockcount) <=
4824 * If we're done, stop now. Stop when we've allocated
4825 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4826 * the transaction may get too big.
4828 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4831 * Else go on to the next record.
4834 if (++lastx < nextents) {
4835 ep = xfs_iext_get_ext(ifp, lastx);
4836 xfs_bmbt_get_all(ep, &got);
4843 * Transform from btree to extents, give it cur.
4845 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4846 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4848 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4849 &tmp_logflags, whichfork);
4850 logflags |= tmp_logflags;
4854 ASSERT(ifp->if_ext_max ==
4855 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4856 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4857 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4861 * Log everything. Do this after conversion, there's no point in
4862 * logging the extent records if we've converted to btree format.
4864 if ((logflags & xfs_ilog_fext(whichfork)) &&
4865 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4866 logflags &= ~xfs_ilog_fext(whichfork);
4867 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
4868 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4869 logflags &= ~xfs_ilog_fbroot(whichfork);
4871 * Log whatever the flags say, even if error. Otherwise we might miss
4872 * detecting a case where the data is changed, there's an error,
4873 * and it's not logged so we don't shutdown when we should.
4877 xfs_trans_log_inode(tp, ip, logflags);
4881 ASSERT(*firstblock == NULLFSBLOCK ||
4882 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4884 cur->bc_private.b.firstblock) ||
4886 XFS_FSB_TO_AGNO(mp, *firstblock) <
4888 cur->bc_private.b.firstblock)));
4889 *firstblock = cur->bc_private.b.firstblock;
4891 xfs_btree_del_cursor(cur,
4892 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4895 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4901 * Map file blocks to filesystem blocks, simple version.
4902 * One block (extent) only, read-only.
4903 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4904 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4905 * was set and all the others were clear.
4909 xfs_trans_t *tp, /* transaction pointer */
4910 xfs_inode_t *ip, /* incore inode */
4911 int whichfork, /* data or attr fork */
4912 xfs_fsblock_t *fsb, /* output: mapped block */
4913 xfs_fileoff_t bno) /* starting file offs. mapped */
4915 int eof; /* we've hit the end of extents */
4916 int error; /* error return */
4917 xfs_bmbt_irec_t got; /* current file extent record */
4918 xfs_ifork_t *ifp; /* inode fork pointer */
4919 xfs_extnum_t lastx; /* last useful extent number */
4920 xfs_bmbt_irec_t prev; /* previous file extent record */
4922 ifp = XFS_IFORK_PTR(ip, whichfork);
4924 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4925 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4926 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4928 return XFS_ERROR(EFSCORRUPTED);
4930 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4931 return XFS_ERROR(EIO);
4932 XFS_STATS_INC(xs_blk_mapr);
4933 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4934 (error = xfs_iread_extents(tp, ip, whichfork)))
4936 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4939 * Reading past eof, act as though there's a hole
4942 if (eof || got.br_startoff > bno) {
4946 ASSERT(!isnullstartblock(got.br_startblock));
4947 ASSERT(bno < got.br_startoff + got.br_blockcount);
4948 *fsb = got.br_startblock + (bno - got.br_startoff);
4953 * Unmap (remove) blocks from a file.
4954 * If nexts is nonzero then the number of extents to remove is limited to
4955 * that value. If not all extents in the block range can be removed then
4960 xfs_trans_t *tp, /* transaction pointer */
4961 struct xfs_inode *ip, /* incore inode */
4962 xfs_fileoff_t bno, /* starting offset to unmap */
4963 xfs_filblks_t len, /* length to unmap in file */
4964 int flags, /* misc flags */
4965 xfs_extnum_t nexts, /* number of extents max */
4966 xfs_fsblock_t *firstblock, /* first allocated block
4967 controls a.g. for allocs */
4968 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4969 int *done) /* set if not done yet */
4971 xfs_btree_cur_t *cur; /* bmap btree cursor */
4972 xfs_bmbt_irec_t del; /* extent being deleted */
4973 int eof; /* is deleting at eof */
4974 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4975 int error; /* error return value */
4976 xfs_extnum_t extno; /* extent number in list */
4977 xfs_bmbt_irec_t got; /* current extent record */
4978 xfs_ifork_t *ifp; /* inode fork pointer */
4979 int isrt; /* freeing in rt area */
4980 xfs_extnum_t lastx; /* last extent index used */
4981 int logflags; /* transaction logging flags */
4982 xfs_extlen_t mod; /* rt extent offset */
4983 xfs_mount_t *mp; /* mount structure */
4984 xfs_extnum_t nextents; /* number of file extents */
4985 xfs_bmbt_irec_t prev; /* previous extent record */
4986 xfs_fileoff_t start; /* first file offset deleted */
4987 int tmp_logflags; /* partial logging flags */
4988 int wasdel; /* was a delayed alloc extent */
4989 int whichfork; /* data or attribute fork */
4992 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4994 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4995 XFS_ATTR_FORK : XFS_DATA_FORK;
4996 ifp = XFS_IFORK_PTR(ip, whichfork);
4998 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4999 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5000 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5002 return XFS_ERROR(EFSCORRUPTED);
5005 if (XFS_FORCED_SHUTDOWN(mp))
5006 return XFS_ERROR(EIO);
5010 ASSERT(ifp->if_ext_max ==
5011 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5012 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5013 (error = xfs_iread_extents(tp, ip, whichfork)))
5015 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5016 if (nextents == 0) {
5020 XFS_STATS_INC(xs_blk_unmap);
5021 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5023 bno = start + len - 1;
5024 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5028 * Check to see if the given block number is past the end of the
5029 * file, back up to the last block if so...
5032 ep = xfs_iext_get_ext(ifp, --lastx);
5033 xfs_bmbt_get_all(ep, &got);
5034 bno = got.br_startoff + got.br_blockcount - 1;
5037 if (ifp->if_flags & XFS_IFBROOT) {
5038 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5039 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5040 cur->bc_private.b.firstblock = *firstblock;
5041 cur->bc_private.b.flist = flist;
5042 cur->bc_private.b.flags = 0;
5046 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5047 (nexts == 0 || extno < nexts)) {
5049 * Is the found extent after a hole in which bno lives?
5050 * Just back up to the previous extent, if so.
5052 if (got.br_startoff > bno) {
5055 ep = xfs_iext_get_ext(ifp, lastx);
5056 xfs_bmbt_get_all(ep, &got);
5059 * Is the last block of this extent before the range
5060 * we're supposed to delete? If so, we're done.
5062 bno = XFS_FILEOFF_MIN(bno,
5063 got.br_startoff + got.br_blockcount - 1);
5067 * Then deal with the (possibly delayed) allocated space
5072 wasdel = isnullstartblock(del.br_startblock);
5073 if (got.br_startoff < start) {
5074 del.br_startoff = start;
5075 del.br_blockcount -= start - got.br_startoff;
5077 del.br_startblock += start - got.br_startoff;
5079 if (del.br_startoff + del.br_blockcount > bno + 1)
5080 del.br_blockcount = bno + 1 - del.br_startoff;
5081 sum = del.br_startblock + del.br_blockcount;
5083 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5085 * Realtime extent not lined up at the end.
5086 * The extent could have been split into written
5087 * and unwritten pieces, or we could just be
5088 * unmapping part of it. But we can't really
5089 * get rid of part of a realtime extent.
5091 if (del.br_state == XFS_EXT_UNWRITTEN ||
5092 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5094 * This piece is unwritten, or we're not
5095 * using unwritten extents. Skip over it.
5098 bno -= mod > del.br_blockcount ?
5099 del.br_blockcount : mod;
5100 if (bno < got.br_startoff) {
5102 xfs_bmbt_get_all(xfs_iext_get_ext(
5108 * It's written, turn it unwritten.
5109 * This is better than zeroing it.
5111 ASSERT(del.br_state == XFS_EXT_NORM);
5112 ASSERT(xfs_trans_get_block_res(tp) > 0);
5114 * If this spans a realtime extent boundary,
5115 * chop it back to the start of the one we end at.
5117 if (del.br_blockcount > mod) {
5118 del.br_startoff += del.br_blockcount - mod;
5119 del.br_startblock += del.br_blockcount - mod;
5120 del.br_blockcount = mod;
5122 del.br_state = XFS_EXT_UNWRITTEN;
5123 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del,
5124 firstblock, flist, &logflags,
5130 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5132 * Realtime extent is lined up at the end but not
5133 * at the front. We'll get rid of full extents if
5136 mod = mp->m_sb.sb_rextsize - mod;
5137 if (del.br_blockcount > mod) {
5138 del.br_blockcount -= mod;
5139 del.br_startoff += mod;
5140 del.br_startblock += mod;
5141 } else if ((del.br_startoff == start &&
5142 (del.br_state == XFS_EXT_UNWRITTEN ||
5143 xfs_trans_get_block_res(tp) == 0)) ||
5144 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5146 * Can't make it unwritten. There isn't
5147 * a full extent here so just skip it.
5149 ASSERT(bno >= del.br_blockcount);
5150 bno -= del.br_blockcount;
5151 if (got.br_startoff > bno) {
5153 ep = xfs_iext_get_ext(ifp,
5155 xfs_bmbt_get_all(ep, &got);
5159 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5161 * This one is already unwritten.
5162 * It must have a written left neighbor.
5163 * Unwrite the killed part of that one and
5167 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5169 ASSERT(prev.br_state == XFS_EXT_NORM);
5170 ASSERT(!isnullstartblock(prev.br_startblock));
5171 ASSERT(del.br_startblock ==
5172 prev.br_startblock + prev.br_blockcount);
5173 if (prev.br_startoff < start) {
5174 mod = start - prev.br_startoff;
5175 prev.br_blockcount -= mod;
5176 prev.br_startblock += mod;
5177 prev.br_startoff = start;
5179 prev.br_state = XFS_EXT_UNWRITTEN;
5181 error = xfs_bmap_add_extent(tp, ip, &lastx,
5182 &cur, &prev, firstblock, flist,
5183 &logflags, XFS_DATA_FORK);
5188 ASSERT(del.br_state == XFS_EXT_NORM);
5189 del.br_state = XFS_EXT_UNWRITTEN;
5190 error = xfs_bmap_add_extent(tp, ip, &lastx,
5191 &cur, &del, firstblock, flist,
5192 &logflags, XFS_DATA_FORK);
5199 ASSERT(startblockval(del.br_startblock) > 0);
5200 /* Update realtime/data freespace, unreserve quota */
5202 xfs_filblks_t rtexts;
5204 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5205 do_div(rtexts, mp->m_sb.sb_rextsize);
5206 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5207 (int64_t)rtexts, 0);
5208 (void)xfs_trans_reserve_quota_nblks(NULL,
5209 ip, -((long)del.br_blockcount), 0,
5210 XFS_QMOPT_RES_RTBLKS);
5212 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5213 (int64_t)del.br_blockcount, 0);
5214 (void)xfs_trans_reserve_quota_nblks(NULL,
5215 ip, -((long)del.br_blockcount), 0,
5216 XFS_QMOPT_RES_REGBLKS);
5218 ip->i_delayed_blks -= del.br_blockcount;
5220 cur->bc_private.b.flags |=
5221 XFS_BTCUR_BPRV_WASDEL;
5223 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5225 * If it's the case where the directory code is running
5226 * with no block reservation, and the deleted block is in
5227 * the middle of its extent, and the resulting insert
5228 * of an extent would cause transformation to btree format,
5229 * then reject it. The calling code will then swap
5230 * blocks around instead.
5231 * We have to do this now, rather than waiting for the
5232 * conversion to btree format, since the transaction
5235 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5236 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5237 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5238 del.br_startoff > got.br_startoff &&
5239 del.br_startoff + del.br_blockcount <
5240 got.br_startoff + got.br_blockcount) {
5241 error = XFS_ERROR(ENOSPC);
5244 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5245 &tmp_logflags, whichfork);
5246 logflags |= tmp_logflags;
5249 bno = del.br_startoff - 1;
5252 * If not done go on to the next (previous) record.
5254 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5256 ep = xfs_iext_get_ext(ifp, lastx);
5257 if (xfs_bmbt_get_startoff(ep) > bno) {
5259 ep = xfs_iext_get_ext(ifp,
5262 xfs_bmbt_get_all(ep, &got);
5267 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5268 ASSERT(ifp->if_ext_max ==
5269 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5271 * Convert to a btree if necessary.
5273 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5274 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5275 ASSERT(cur == NULL);
5276 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5277 &cur, 0, &tmp_logflags, whichfork);
5278 logflags |= tmp_logflags;
5283 * transform from btree to extents, give it cur
5285 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5286 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5287 ASSERT(cur != NULL);
5288 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5290 logflags |= tmp_logflags;
5295 * transform from extents to local?
5297 ASSERT(ifp->if_ext_max ==
5298 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5302 * Log everything. Do this after conversion, there's no point in
5303 * logging the extent records if we've converted to btree format.
5305 if ((logflags & xfs_ilog_fext(whichfork)) &&
5306 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5307 logflags &= ~xfs_ilog_fext(whichfork);
5308 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5310 logflags &= ~xfs_ilog_fbroot(whichfork);
5312 * Log inode even in the error case, if the transaction
5313 * is dirty we'll need to shut down the filesystem.
5316 xfs_trans_log_inode(tp, ip, logflags);
5319 *firstblock = cur->bc_private.b.firstblock;
5320 cur->bc_private.b.allocated = 0;
5322 xfs_btree_del_cursor(cur,
5323 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5329 * returns 1 for success, 0 if we failed to map the extent.
5332 xfs_getbmapx_fix_eof_hole(
5333 xfs_inode_t *ip, /* xfs incore inode pointer */
5334 struct getbmapx *out, /* output structure */
5335 int prealloced, /* this is a file with
5336 * preallocated data space */
5337 __int64_t end, /* last block requested */
5338 xfs_fsblock_t startblock)
5341 xfs_mount_t *mp; /* file system mount point */
5342 xfs_ifork_t *ifp; /* inode fork pointer */
5343 xfs_extnum_t lastx; /* last extent pointer */
5344 xfs_fileoff_t fileblock;
5346 if (startblock == HOLESTARTBLOCK) {
5348 out->bmv_block = -1;
5349 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5350 fixlen -= out->bmv_offset;
5351 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5352 /* Came to hole at EOF. Trim it. */
5355 out->bmv_length = fixlen;
5358 if (startblock == DELAYSTARTBLOCK)
5359 out->bmv_block = -2;
5361 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5362 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5363 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5364 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5365 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5366 out->bmv_oflags |= BMV_OF_LAST;
5373 * Get inode's extents as described in bmv, and format for output.
5374 * Calls formatter to fill the user's buffer until all extents
5375 * are mapped, until the passed-in bmv->bmv_count slots have
5376 * been filled, or until the formatter short-circuits the loop,
5377 * if it is tracking filled-in extents on its own.
5379 int /* error code */
5382 struct getbmapx *bmv, /* user bmap structure */
5383 xfs_bmap_format_t formatter, /* format to user */
5384 void *arg) /* formatter arg */
5386 __int64_t bmvend; /* last block requested */
5387 int error = 0; /* return value */
5388 __int64_t fixlen; /* length for -1 case */
5389 int i; /* extent number */
5390 int lock; /* lock state */
5391 xfs_bmbt_irec_t *map; /* buffer for user's data */
5392 xfs_mount_t *mp; /* file system mount point */
5393 int nex; /* # of user extents can do */
5394 int nexleft; /* # of user extents left */
5395 int subnex; /* # of bmapi's can do */
5396 int nmap; /* number of map entries */
5397 struct getbmapx *out; /* output structure */
5398 int whichfork; /* data or attr fork */
5399 int prealloced; /* this is a file with
5400 * preallocated data space */
5401 int iflags; /* interface flags */
5402 int bmapi_flags; /* flags for xfs_bmapi */
5406 iflags = bmv->bmv_iflags;
5407 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5409 if (whichfork == XFS_ATTR_FORK) {
5410 if (XFS_IFORK_Q(ip)) {
5411 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5412 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5413 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5414 return XFS_ERROR(EINVAL);
5415 } else if (unlikely(
5416 ip->i_d.di_aformat != 0 &&
5417 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5418 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5420 return XFS_ERROR(EFSCORRUPTED);
5426 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5427 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5428 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5429 return XFS_ERROR(EINVAL);
5431 if (xfs_get_extsz_hint(ip) ||
5432 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5434 fixlen = XFS_MAXIOFFSET(mp);
5437 fixlen = ip->i_size;
5441 if (bmv->bmv_length == -1) {
5442 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5444 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5445 } else if (bmv->bmv_length == 0) {
5446 bmv->bmv_entries = 0;
5448 } else if (bmv->bmv_length < 0) {
5449 return XFS_ERROR(EINVAL);
5452 nex = bmv->bmv_count - 1;
5454 return XFS_ERROR(EINVAL);
5455 bmvend = bmv->bmv_offset + bmv->bmv_length;
5458 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5459 return XFS_ERROR(ENOMEM);
5460 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5462 return XFS_ERROR(ENOMEM);
5464 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5465 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5466 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5467 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5469 goto out_unlock_iolock;
5472 * even after flushing the inode, there can still be delalloc
5473 * blocks on the inode beyond EOF due to speculative
5474 * preallocation. These are not removed until the release
5475 * function is called or the inode is inactivated. Hence we
5476 * cannot assert here that ip->i_delayed_blks == 0.
5480 lock = xfs_ilock_map_shared(ip);
5483 * Don't let nex be bigger than the number of extents
5484 * we can have assuming alternating holes and real extents.
5486 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5487 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5489 bmapi_flags = xfs_bmapi_aflag(whichfork);
5490 if (!(iflags & BMV_IF_PREALLOC))
5491 bmapi_flags |= XFS_BMAPI_IGSTATE;
5494 * Allocate enough space to handle "subnex" maps at a time.
5498 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5500 goto out_unlock_ilock;
5502 bmv->bmv_entries = 0;
5504 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5505 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5513 nmap = (nexleft > subnex) ? subnex : nexleft;
5514 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5515 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5516 bmapi_flags, NULL, 0, map, &nmap,
5520 ASSERT(nmap <= subnex);
5522 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5523 out[cur_ext].bmv_oflags = 0;
5524 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5525 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5526 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5527 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5528 out[cur_ext].bmv_offset =
5529 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5530 out[cur_ext].bmv_length =
5531 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5532 out[cur_ext].bmv_unused1 = 0;
5533 out[cur_ext].bmv_unused2 = 0;
5534 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5535 (map[i].br_startblock != DELAYSTARTBLOCK));
5536 if (map[i].br_startblock == HOLESTARTBLOCK &&
5537 whichfork == XFS_ATTR_FORK) {
5538 /* came to the end of attribute fork */
5539 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5543 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5545 map[i].br_startblock))
5549 out[cur_ext].bmv_offset +
5550 out[cur_ext].bmv_length;
5552 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5555 * In case we don't want to return the hole,
5556 * don't increase cur_ext so that we can reuse
5557 * it in the next loop.
5559 if ((iflags & BMV_IF_NO_HOLES) &&
5560 map[i].br_startblock == HOLESTARTBLOCK) {
5561 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5569 } while (nmap && nexleft && bmv->bmv_length);
5574 xfs_iunlock_map_shared(ip, lock);
5576 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5578 for (i = 0; i < cur_ext; i++) {
5579 int full = 0; /* user array is full */
5581 /* format results & advance arg */
5582 error = formatter(&arg, &out[i], &full);
5592 * Check the last inode extent to determine whether this allocation will result
5593 * in blocks being allocated at the end of the file. When we allocate new data
5594 * blocks at the end of the file which do not start at the previous data block,
5595 * we will try to align the new blocks at stripe unit boundaries.
5597 STATIC int /* error */
5599 xfs_inode_t *ip, /* incore inode pointer */
5600 xfs_fileoff_t off, /* file offset in fsblocks */
5601 int whichfork, /* data or attribute fork */
5602 char *aeof) /* return value */
5604 int error; /* error return value */
5605 xfs_ifork_t *ifp; /* inode fork pointer */
5606 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5607 xfs_extnum_t nextents; /* number of file extents */
5608 xfs_bmbt_irec_t s; /* expanded extent record */
5610 ASSERT(whichfork == XFS_DATA_FORK);
5611 ifp = XFS_IFORK_PTR(ip, whichfork);
5612 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5613 (error = xfs_iread_extents(NULL, ip, whichfork)))
5615 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5616 if (nextents == 0) {
5621 * Go to the last extent
5623 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5624 xfs_bmbt_get_all(lastrec, &s);
5626 * Check we are allocating in the last extent (for delayed allocations)
5627 * or past the last extent for non-delayed allocations.
5629 *aeof = (off >= s.br_startoff &&
5630 off < s.br_startoff + s.br_blockcount &&
5631 isnullstartblock(s.br_startblock)) ||
5632 off >= s.br_startoff + s.br_blockcount;
5637 * Check if the endoff is outside the last extent. If so the caller will grow
5638 * the allocation to a stripe unit boundary.
5642 xfs_inode_t *ip, /* incore inode pointer */
5643 xfs_fileoff_t endoff, /* file offset in fsblocks */
5644 int whichfork, /* data or attribute fork */
5645 int *eof) /* result value */
5647 xfs_fsblock_t blockcount; /* extent block count */
5648 int error; /* error return value */
5649 xfs_ifork_t *ifp; /* inode fork pointer */
5650 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5651 xfs_extnum_t nextents; /* number of file extents */
5652 xfs_fileoff_t startoff; /* extent starting file offset */
5654 ASSERT(whichfork == XFS_DATA_FORK);
5655 ifp = XFS_IFORK_PTR(ip, whichfork);
5656 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5657 (error = xfs_iread_extents(NULL, ip, whichfork)))
5659 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5660 if (nextents == 0) {
5665 * Go to the last extent
5667 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5668 startoff = xfs_bmbt_get_startoff(lastrec);
5669 blockcount = xfs_bmbt_get_blockcount(lastrec);
5670 *eof = endoff >= startoff + blockcount;
5675 STATIC struct xfs_buf *
5677 struct xfs_btree_cur *cur,
5680 struct xfs_log_item_desc *lidp;
5686 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5687 if (!cur->bc_bufs[i])
5689 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5690 return cur->bc_bufs[i];
5693 /* Chase down all the log items to see if the bp is there */
5694 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5695 struct xfs_buf_log_item *bip;
5696 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5697 if (bip->bli_item.li_type == XFS_LI_BUF &&
5698 XFS_BUF_ADDR(bip->bli_buf) == bno)
5699 return bip->bli_buf;
5707 struct xfs_btree_block *block,
5713 __be64 *pp, *thispa; /* pointer to block address */
5714 xfs_bmbt_key_t *prevp, *keyp;
5716 ASSERT(be16_to_cpu(block->bb_level) > 0);
5719 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5720 dmxr = mp->m_bmap_dmxr[0];
5721 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5724 ASSERT(be64_to_cpu(prevp->br_startoff) <
5725 be64_to_cpu(keyp->br_startoff));
5730 * Compare the block numbers to see if there are dups.
5733 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5735 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5737 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5739 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5741 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5742 if (*thispa == *pp) {
5743 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
5745 (unsigned long long)be64_to_cpu(*thispa));
5746 panic("%s: ptrs are equal in node\n",
5754 * Check that the extents for the inode ip are in the right order in all
5759 xfs_bmap_check_leaf_extents(
5760 xfs_btree_cur_t *cur, /* btree cursor or null */
5761 xfs_inode_t *ip, /* incore inode pointer */
5762 int whichfork) /* data or attr fork */
5764 struct xfs_btree_block *block; /* current btree block */
5765 xfs_fsblock_t bno; /* block # of "block" */
5766 xfs_buf_t *bp; /* buffer for "block" */
5767 int error; /* error return value */
5768 xfs_extnum_t i=0, j; /* index into the extents list */
5769 xfs_ifork_t *ifp; /* fork structure */
5770 int level; /* btree level, for checking */
5771 xfs_mount_t *mp; /* file system mount structure */
5772 __be64 *pp; /* pointer to block address */
5773 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5774 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5775 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5778 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5784 ifp = XFS_IFORK_PTR(ip, whichfork);
5785 block = ifp->if_broot;
5787 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5789 level = be16_to_cpu(block->bb_level);
5791 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5792 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5793 bno = be64_to_cpu(*pp);
5795 ASSERT(bno != NULLDFSBNO);
5796 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5797 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5800 * Go down the tree until leaf level is reached, following the first
5801 * pointer (leftmost) at each level.
5803 while (level-- > 0) {
5804 /* See if buf is in cur first */
5805 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5811 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5812 XFS_BMAP_BTREE_REF)))
5814 block = XFS_BUF_TO_BLOCK(bp);
5815 XFS_WANT_CORRUPTED_GOTO(
5816 xfs_bmap_sanity_check(mp, bp, level),
5822 * Check this block for basic sanity (increasing keys and
5823 * no duplicate blocks).
5826 xfs_check_block(block, mp, 0, 0);
5827 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5828 bno = be64_to_cpu(*pp);
5829 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5832 xfs_trans_brelse(NULL, bp);
5837 * Here with bp and block set to the leftmost leaf node in the tree.
5842 * Loop over all leaf nodes checking that all extents are in the right order.
5845 xfs_fsblock_t nextbno;
5846 xfs_extnum_t num_recs;
5849 num_recs = xfs_btree_get_numrecs(block);
5852 * Read-ahead the next leaf block, if any.
5855 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5858 * Check all the extents to make sure they are OK.
5859 * If we had a previous block, the last entry should
5860 * conform with the first entry in this one.
5863 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5865 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5866 xfs_bmbt_disk_get_blockcount(&last) <=
5867 xfs_bmbt_disk_get_startoff(ep));
5869 for (j = 1; j < num_recs; j++) {
5870 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5871 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5872 xfs_bmbt_disk_get_blockcount(ep) <=
5873 xfs_bmbt_disk_get_startoff(nextp));
5881 xfs_trans_brelse(NULL, bp);
5885 * If we've reached the end, stop.
5887 if (bno == NULLFSBLOCK)
5890 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5896 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5897 XFS_BMAP_BTREE_REF)))
5899 block = XFS_BUF_TO_BLOCK(bp);
5903 xfs_trans_brelse(NULL, bp);
5908 xfs_warn(mp, "%s: at error0", __func__);
5910 xfs_trans_brelse(NULL, bp);
5912 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
5914 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
5920 * Count fsblocks of the given fork.
5923 xfs_bmap_count_blocks(
5924 xfs_trans_t *tp, /* transaction pointer */
5925 xfs_inode_t *ip, /* incore inode */
5926 int whichfork, /* data or attr fork */
5927 int *count) /* out: count of blocks */
5929 struct xfs_btree_block *block; /* current btree block */
5930 xfs_fsblock_t bno; /* block # of "block" */
5931 xfs_ifork_t *ifp; /* fork structure */
5932 int level; /* btree level, for checking */
5933 xfs_mount_t *mp; /* file system mount structure */
5934 __be64 *pp; /* pointer to block address */
5938 ifp = XFS_IFORK_PTR(ip, whichfork);
5939 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
5940 xfs_bmap_count_leaves(ifp, 0,
5941 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
5947 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5949 block = ifp->if_broot;
5950 level = be16_to_cpu(block->bb_level);
5952 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5953 bno = be64_to_cpu(*pp);
5954 ASSERT(bno != NULLDFSBNO);
5955 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5956 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5958 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
5959 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5961 return XFS_ERROR(EFSCORRUPTED);
5968 * Recursively walks each level of a btree
5969 * to count total fsblocks is use.
5971 STATIC int /* error */
5972 xfs_bmap_count_tree(
5973 xfs_mount_t *mp, /* file system mount point */
5974 xfs_trans_t *tp, /* transaction pointer */
5975 xfs_ifork_t *ifp, /* inode fork pointer */
5976 xfs_fsblock_t blockno, /* file system block number */
5977 int levelin, /* level in btree */
5978 int *count) /* Count of blocks */
5981 xfs_buf_t *bp, *nbp;
5982 int level = levelin;
5984 xfs_fsblock_t bno = blockno;
5985 xfs_fsblock_t nextbno;
5986 struct xfs_btree_block *block, *nextblock;
5989 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5992 block = XFS_BUF_TO_BLOCK(bp);
5995 /* Not at node above leaves, count this level of nodes */
5996 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5997 while (nextbno != NULLFSBLOCK) {
5998 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5999 0, &nbp, XFS_BMAP_BTREE_REF)))
6002 nextblock = XFS_BUF_TO_BLOCK(nbp);
6003 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6004 xfs_trans_brelse(tp, nbp);
6007 /* Dive to the next level */
6008 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6009 bno = be64_to_cpu(*pp);
6010 if (unlikely((error =
6011 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6012 xfs_trans_brelse(tp, bp);
6013 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6014 XFS_ERRLEVEL_LOW, mp);
6015 return XFS_ERROR(EFSCORRUPTED);
6017 xfs_trans_brelse(tp, bp);
6019 /* count all level 1 nodes and their leaves */
6021 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6022 numrecs = be16_to_cpu(block->bb_numrecs);
6023 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6024 xfs_trans_brelse(tp, bp);
6025 if (nextbno == NULLFSBLOCK)
6028 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6029 XFS_BMAP_BTREE_REF)))
6032 block = XFS_BUF_TO_BLOCK(bp);
6039 * Count leaf blocks given a range of extent records.
6042 xfs_bmap_count_leaves(
6050 for (b = 0; b < numrecs; b++) {
6051 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6052 *count += xfs_bmbt_get_blockcount(frp);
6057 * Count leaf blocks given a range of extent records originally
6061 xfs_bmap_disk_count_leaves(
6062 struct xfs_mount *mp,
6063 struct xfs_btree_block *block,
6068 xfs_bmbt_rec_t *frp;
6070 for (b = 1; b <= numrecs; b++) {
6071 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6072 *count += xfs_bmbt_disk_get_blockcount(frp);
6077 * dead simple method of punching delalyed allocation blocks from a range in
6078 * the inode. Walks a block at a time so will be slow, but is only executed in
6079 * rare error cases so the overhead is not critical. This will alays punch out
6080 * both the start and end blocks, even if the ranges only partially overlap
6081 * them, so it is up to the caller to ensure that partial blocks are not
6085 xfs_bmap_punch_delalloc_range(
6086 struct xfs_inode *ip,
6087 xfs_fileoff_t start_fsb,
6088 xfs_fileoff_t length)
6090 xfs_fileoff_t remaining = length;
6093 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6097 xfs_bmbt_irec_t imap;
6099 xfs_fsblock_t firstblock;
6100 xfs_bmap_free_t flist;
6103 * Map the range first and check that it is a delalloc extent
6104 * before trying to unmap the range. Otherwise we will be
6105 * trying to remove a real extent (which requires a
6106 * transaction) or a hole, which is probably a bad idea...
6108 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6109 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6113 /* something screwed, just bail */
6114 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6115 xfs_alert(ip->i_mount,
6116 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6117 ip->i_ino, start_fsb);
6125 if (imap.br_startblock != DELAYSTARTBLOCK) {
6126 /* been converted, ignore */
6129 WARN_ON(imap.br_blockcount == 0);
6132 * Note: while we initialise the firstblock/flist pair, they
6133 * should never be used because blocks should never be
6134 * allocated or freed for a delalloc extent and hence we need
6135 * don't cancel or finish them after the xfs_bunmapi() call.
6137 xfs_bmap_init(&flist, &firstblock);
6138 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6143 ASSERT(!flist.xbf_count && !flist.xbf_first);
6147 } while(remaining > 0);