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_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 xfs_extdelta_t *delta, /* Change made to incore extents */
105 int whichfork, /* data or attr fork */
106 int rsvd); /* OK to allocate reserved blocks */
109 * Called by xfs_bmap_add_extent to handle cases converting a delayed
110 * allocation to a real allocation.
112 STATIC int /* error */
113 xfs_bmap_add_extent_delay_real(
114 xfs_inode_t *ip, /* incore inode pointer */
115 xfs_extnum_t idx, /* extent number to update/insert */
116 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
117 xfs_bmbt_irec_t *new, /* new data to add to file extents */
118 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
119 xfs_fsblock_t *first, /* pointer to firstblock variable */
120 xfs_bmap_free_t *flist, /* list of extents to be freed */
121 int *logflagsp, /* inode logging flags */
122 xfs_extdelta_t *delta, /* Change made to incore extents */
123 int rsvd); /* OK to allocate reserved blocks */
126 * Called by xfs_bmap_add_extent to handle cases converting a hole
127 * to a delayed allocation.
129 STATIC int /* error */
130 xfs_bmap_add_extent_hole_delay(
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_extnum_t idx, /* extent number to update/insert */
133 xfs_bmbt_irec_t *new, /* new data to add to file extents */
134 int *logflagsp,/* inode logging flags */
135 xfs_extdelta_t *delta, /* Change made to incore extents */
136 int rsvd); /* OK to allocate reserved blocks */
139 * Called by xfs_bmap_add_extent to handle cases converting a hole
140 * to a real allocation.
142 STATIC int /* error */
143 xfs_bmap_add_extent_hole_real(
144 xfs_inode_t *ip, /* incore inode pointer */
145 xfs_extnum_t idx, /* extent number to update/insert */
146 xfs_btree_cur_t *cur, /* if null, not a btree */
147 xfs_bmbt_irec_t *new, /* new data to add to file extents */
148 int *logflagsp, /* inode logging flags */
149 xfs_extdelta_t *delta, /* Change made to incore extents */
150 int whichfork); /* data or attr fork */
153 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
154 * allocation to a real allocation or vice versa.
156 STATIC int /* error */
157 xfs_bmap_add_extent_unwritten_real(
158 xfs_inode_t *ip, /* incore inode pointer */
159 xfs_extnum_t idx, /* extent number to update/insert */
160 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
161 xfs_bmbt_irec_t *new, /* new data to add to file extents */
162 int *logflagsp, /* inode logging flags */
163 xfs_extdelta_t *delta); /* Change made to incore extents */
166 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
167 * It figures out where to ask the underlying allocator to put the new extent.
169 STATIC int /* error */
171 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
174 * Transform a btree format file with only one leaf node, where the
175 * extents list will fit in the inode, into an extents format file.
176 * Since the file extents are already in-core, all we have to do is
177 * give up the space for the btree root and pitch the leaf block.
179 STATIC int /* error */
180 xfs_bmap_btree_to_extents(
181 xfs_trans_t *tp, /* transaction pointer */
182 xfs_inode_t *ip, /* incore inode pointer */
183 xfs_btree_cur_t *cur, /* btree cursor */
184 int *logflagsp, /* inode logging flags */
185 int whichfork); /* data or attr fork */
188 * Called by xfs_bmapi to update file extent records and the btree
189 * after removing space (or undoing a delayed allocation).
191 STATIC int /* error */
193 xfs_inode_t *ip, /* incore inode pointer */
194 xfs_trans_t *tp, /* current trans pointer */
195 xfs_extnum_t idx, /* extent number to update/insert */
196 xfs_bmap_free_t *flist, /* list of extents to be freed */
197 xfs_btree_cur_t *cur, /* if null, not a btree */
198 xfs_bmbt_irec_t *new, /* new data to add to file extents */
199 int *logflagsp,/* inode logging flags */
200 xfs_extdelta_t *delta, /* Change made to incore extents */
201 int whichfork, /* data or attr fork */
202 int rsvd); /* OK to allocate reserved blocks */
205 * Remove the entry "free" from the free item list. Prev points to the
206 * previous entry, unless "free" is the head of the list.
210 xfs_bmap_free_t *flist, /* free item list header */
211 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
212 xfs_bmap_free_item_t *free); /* list item to be freed */
215 * Convert an extents-format file into a btree-format file.
216 * The new file will have a root block (in the inode) and a single child block.
218 STATIC int /* error */
219 xfs_bmap_extents_to_btree(
220 xfs_trans_t *tp, /* transaction pointer */
221 xfs_inode_t *ip, /* incore inode pointer */
222 xfs_fsblock_t *firstblock, /* first-block-allocated */
223 xfs_bmap_free_t *flist, /* blocks freed in xaction */
224 xfs_btree_cur_t **curp, /* cursor returned to caller */
225 int wasdel, /* converting a delayed alloc */
226 int *logflagsp, /* inode logging flags */
227 int whichfork); /* data or attr fork */
230 * Convert a local file to an extents file.
231 * This code is sort of bogus, since the file data needs to get
232 * logged so it won't be lost. The bmap-level manipulations are ok, though.
234 STATIC int /* error */
235 xfs_bmap_local_to_extents(
236 xfs_trans_t *tp, /* transaction pointer */
237 xfs_inode_t *ip, /* incore inode pointer */
238 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
239 xfs_extlen_t total, /* total blocks needed by transaction */
240 int *logflagsp, /* inode logging flags */
241 int whichfork); /* data or attr fork */
244 * Search the extents list for the inode, for the extent containing bno.
245 * If bno lies in a hole, point to the next entry. If bno lies past eof,
246 * *eofp will be set, and *prevp will contain the last entry (null if none).
247 * Else, *lastxp will be set to the index of the found
248 * entry; *gotp will contain the entry.
250 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
251 xfs_bmap_search_extents(
252 xfs_inode_t *ip, /* incore inode pointer */
253 xfs_fileoff_t bno, /* block number searched for */
254 int whichfork, /* data or attr fork */
255 int *eofp, /* out: end of file found */
256 xfs_extnum_t *lastxp, /* out: last extent index */
257 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
258 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
261 * Check the last inode extent to determine whether this allocation will result
262 * in blocks being allocated at the end of the file. When we allocate new data
263 * blocks at the end of the file which do not start at the previous data block,
264 * we will try to align the new blocks at stripe unit boundaries.
266 STATIC int /* error */
268 xfs_inode_t *ip, /* incore inode pointer */
269 xfs_fileoff_t off, /* file offset in fsblocks */
270 int whichfork, /* data or attribute fork */
271 char *aeof); /* return value */
274 * Compute the worst-case number of indirect blocks that will be used
275 * for ip's delayed extent of length "len".
278 xfs_bmap_worst_indlen(
279 xfs_inode_t *ip, /* incore inode pointer */
280 xfs_filblks_t len); /* delayed extent length */
284 * Perform various validation checks on the values being returned
288 xfs_bmap_validate_ret(
292 xfs_bmbt_irec_t *mval,
296 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
304 xfs_fsblock_t blockno,
309 xfs_bmap_count_leaves(
316 xfs_bmap_disk_count_leaves(
317 struct xfs_mount *mp,
318 struct xfs_btree_block *block,
323 * Bmap internal routines.
326 STATIC int /* error */
328 struct xfs_btree_cur *cur,
332 int *stat) /* success/failure */
334 cur->bc_rec.b.br_startoff = off;
335 cur->bc_rec.b.br_startblock = bno;
336 cur->bc_rec.b.br_blockcount = len;
337 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
340 STATIC int /* error */
342 struct xfs_btree_cur *cur,
346 int *stat) /* success/failure */
348 cur->bc_rec.b.br_startoff = off;
349 cur->bc_rec.b.br_startblock = bno;
350 cur->bc_rec.b.br_blockcount = len;
351 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
355 * Update the record referred to by cur to the value given
356 * by [off, bno, len, state].
357 * This either works (return 0) or gets an EFSCORRUPTED error.
361 struct xfs_btree_cur *cur,
367 union xfs_btree_rec rec;
369 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
370 return xfs_btree_update(cur, &rec);
374 * Called from xfs_bmap_add_attrfork to handle btree format files.
376 STATIC int /* error */
377 xfs_bmap_add_attrfork_btree(
378 xfs_trans_t *tp, /* transaction pointer */
379 xfs_inode_t *ip, /* incore inode pointer */
380 xfs_fsblock_t *firstblock, /* first block allocated */
381 xfs_bmap_free_t *flist, /* blocks to free at commit */
382 int *flags) /* inode logging flags */
384 xfs_btree_cur_t *cur; /* btree cursor */
385 int error; /* error return value */
386 xfs_mount_t *mp; /* file system mount struct */
387 int stat; /* newroot status */
390 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
391 *flags |= XFS_ILOG_DBROOT;
393 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
394 cur->bc_private.b.flist = flist;
395 cur->bc_private.b.firstblock = *firstblock;
396 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
398 /* must be at least one entry */
399 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
400 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
403 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
404 return XFS_ERROR(ENOSPC);
406 *firstblock = cur->bc_private.b.firstblock;
407 cur->bc_private.b.allocated = 0;
408 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
412 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
417 * Called from xfs_bmap_add_attrfork to handle extents format files.
419 STATIC int /* error */
420 xfs_bmap_add_attrfork_extents(
421 xfs_trans_t *tp, /* transaction pointer */
422 xfs_inode_t *ip, /* incore inode pointer */
423 xfs_fsblock_t *firstblock, /* first block allocated */
424 xfs_bmap_free_t *flist, /* blocks to free at commit */
425 int *flags) /* inode logging flags */
427 xfs_btree_cur_t *cur; /* bmap btree cursor */
428 int error; /* error return value */
430 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
433 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
434 flags, XFS_DATA_FORK);
436 cur->bc_private.b.allocated = 0;
437 xfs_btree_del_cursor(cur,
438 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
444 * Called from xfs_bmap_add_attrfork to handle local format files.
446 STATIC int /* error */
447 xfs_bmap_add_attrfork_local(
448 xfs_trans_t *tp, /* transaction pointer */
449 xfs_inode_t *ip, /* incore inode pointer */
450 xfs_fsblock_t *firstblock, /* first block allocated */
451 xfs_bmap_free_t *flist, /* blocks to free at commit */
452 int *flags) /* inode logging flags */
454 xfs_da_args_t dargs; /* args for dir/attr code */
455 int error; /* error return value */
456 xfs_mount_t *mp; /* mount structure pointer */
458 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
460 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
462 memset(&dargs, 0, sizeof(dargs));
464 dargs.firstblock = firstblock;
466 dargs.total = mp->m_dirblkfsbs;
467 dargs.whichfork = XFS_DATA_FORK;
469 error = xfs_dir2_sf_to_block(&dargs);
471 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
477 * Called by xfs_bmapi to update file extent records and the btree
478 * after allocating space (or doing a delayed allocation).
480 STATIC int /* error */
482 xfs_inode_t *ip, /* incore inode pointer */
483 xfs_extnum_t idx, /* extent number to update/insert */
484 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
485 xfs_bmbt_irec_t *new, /* new data to add to file extents */
486 xfs_fsblock_t *first, /* pointer to firstblock variable */
487 xfs_bmap_free_t *flist, /* list of extents to be freed */
488 int *logflagsp, /* inode logging flags */
489 xfs_extdelta_t *delta, /* Change made to incore extents */
490 int whichfork, /* data or attr fork */
491 int rsvd) /* OK to use reserved data blocks */
493 xfs_btree_cur_t *cur; /* btree cursor or null */
494 xfs_filblks_t da_new; /* new count del alloc blocks used */
495 xfs_filblks_t da_old; /* old count del alloc blocks used */
496 int error; /* error return value */
497 xfs_ifork_t *ifp; /* inode fork ptr */
498 int logflags; /* returned value */
499 xfs_extnum_t nextents; /* number of extents in file now */
501 XFS_STATS_INC(xs_add_exlist);
503 ifp = XFS_IFORK_PTR(ip, whichfork);
504 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
505 ASSERT(idx <= nextents);
509 * This is the first extent added to a new/empty file.
510 * Special case this one, so other routines get to assume there are
511 * already extents in the list.
514 xfs_iext_insert(ip, 0, 1, new,
515 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
519 if (!isnullstartblock(new->br_startblock)) {
520 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
521 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
524 /* DELTA: single new extent */
526 if (delta->xed_startoff > new->br_startoff)
527 delta->xed_startoff = new->br_startoff;
528 if (delta->xed_blockcount <
529 new->br_startoff + new->br_blockcount)
530 delta->xed_blockcount = new->br_startoff +
535 * Any kind of new delayed allocation goes here.
537 else if (isnullstartblock(new->br_startblock)) {
539 ASSERT((cur->bc_private.b.flags &
540 XFS_BTCUR_BPRV_WASDEL) == 0);
541 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
542 &logflags, delta, rsvd)))
546 * Real allocation off the end of the file.
548 else if (idx == nextents) {
550 ASSERT((cur->bc_private.b.flags &
551 XFS_BTCUR_BPRV_WASDEL) == 0);
552 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
553 &logflags, delta, whichfork)))
556 xfs_bmbt_irec_t prev; /* old extent at offset idx */
559 * Get the record referred to by idx.
561 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
563 * If it's a real allocation record, and the new allocation ends
564 * after the start of the referred to record, then we're filling
565 * in a delayed or unwritten allocation with a real one, or
566 * converting real back to unwritten.
568 if (!isnullstartblock(new->br_startblock) &&
569 new->br_startoff + new->br_blockcount > prev.br_startoff) {
570 if (prev.br_state != XFS_EXT_UNWRITTEN &&
571 isnullstartblock(prev.br_startblock)) {
572 da_old = startblockval(prev.br_startblock);
574 ASSERT(cur->bc_private.b.flags &
575 XFS_BTCUR_BPRV_WASDEL);
576 if ((error = xfs_bmap_add_extent_delay_real(ip,
577 idx, &cur, new, &da_new, first, flist,
578 &logflags, delta, rsvd)))
580 } else if (new->br_state == XFS_EXT_NORM) {
581 ASSERT(new->br_state == XFS_EXT_NORM);
582 if ((error = xfs_bmap_add_extent_unwritten_real(
583 ip, idx, &cur, new, &logflags, delta)))
586 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
587 if ((error = xfs_bmap_add_extent_unwritten_real(
588 ip, idx, &cur, new, &logflags, delta)))
591 ASSERT(*curp == cur || *curp == NULL);
594 * Otherwise we're filling in a hole with an allocation.
598 ASSERT((cur->bc_private.b.flags &
599 XFS_BTCUR_BPRV_WASDEL) == 0);
600 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
601 new, &logflags, delta, whichfork)))
606 ASSERT(*curp == cur || *curp == NULL);
608 * Convert to a btree if necessary.
610 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
611 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
612 int tmp_logflags; /* partial log flag return val */
615 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
616 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
617 logflags |= tmp_logflags;
622 * Adjust for changes in reserved delayed indirect blocks.
623 * Nothing to do for disk quotas here.
625 if (da_old || da_new) {
630 nblks += cur->bc_private.b.allocated;
631 ASSERT(nblks <= da_old);
633 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
634 (int64_t)(da_old - nblks), rsvd);
637 * Clear out the allocated field, done with it now in any case.
640 cur->bc_private.b.allocated = 0;
646 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
648 *logflagsp = logflags;
653 * Called by xfs_bmap_add_extent to handle cases converting a delayed
654 * allocation to a real allocation.
656 STATIC int /* error */
657 xfs_bmap_add_extent_delay_real(
658 xfs_inode_t *ip, /* incore inode pointer */
659 xfs_extnum_t idx, /* extent number to update/insert */
660 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
661 xfs_bmbt_irec_t *new, /* new data to add to file extents */
662 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
663 xfs_fsblock_t *first, /* pointer to firstblock variable */
664 xfs_bmap_free_t *flist, /* list of extents to be freed */
665 int *logflagsp, /* inode logging flags */
666 xfs_extdelta_t *delta, /* Change made to incore extents */
667 int rsvd) /* OK to use reserved data block allocation */
669 xfs_btree_cur_t *cur; /* btree cursor */
670 int diff; /* temp value */
671 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
672 int error; /* error return value */
673 int i; /* temp state */
674 xfs_ifork_t *ifp; /* inode fork pointer */
675 xfs_fileoff_t new_endoff; /* end offset of new entry */
676 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
677 /* left is 0, right is 1, prev is 2 */
678 int rval=0; /* return value (logging flags) */
679 int state = 0;/* state bits, accessed thru macros */
680 xfs_filblks_t temp=0; /* value for dnew calculations */
681 xfs_filblks_t temp2=0;/* value for dnew calculations */
682 int tmp_rval; /* partial logging flags */
689 * Set up a bunch of variables to make the tests simpler.
692 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
693 ep = xfs_iext_get_ext(ifp, idx);
694 xfs_bmbt_get_all(ep, &PREV);
695 new_endoff = new->br_startoff + new->br_blockcount;
696 ASSERT(PREV.br_startoff <= new->br_startoff);
697 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
700 * Set flags determining what part of the previous delayed allocation
701 * extent is being replaced by a real allocation.
703 if (PREV.br_startoff == new->br_startoff)
704 state |= BMAP_LEFT_FILLING;
705 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
706 state |= BMAP_RIGHT_FILLING;
709 * Check and set flags if this segment has a left neighbor.
710 * Don't set contiguous if the combined extent would be too large.
713 state |= BMAP_LEFT_VALID;
714 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
716 if (isnullstartblock(LEFT.br_startblock))
717 state |= BMAP_LEFT_DELAY;
720 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
721 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
722 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
723 LEFT.br_state == new->br_state &&
724 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
725 state |= BMAP_LEFT_CONTIG;
728 * Check and set flags if this segment has a right neighbor.
729 * Don't set contiguous if the combined extent would be too large.
730 * Also check for all-three-contiguous being too large.
732 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
733 state |= BMAP_RIGHT_VALID;
734 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
736 if (isnullstartblock(RIGHT.br_startblock))
737 state |= BMAP_RIGHT_DELAY;
740 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
741 new_endoff == RIGHT.br_startoff &&
742 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
743 new->br_state == RIGHT.br_state &&
744 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
745 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
746 BMAP_RIGHT_FILLING)) !=
747 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
748 BMAP_RIGHT_FILLING) ||
749 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
751 state |= BMAP_RIGHT_CONTIG;
755 * Switch out based on the FILLING and CONTIG state bits.
757 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
758 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
759 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
760 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
762 * Filling in all of a previously delayed allocation extent.
763 * The left and right neighbors are both contiguous with new.
765 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
766 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
767 LEFT.br_blockcount + PREV.br_blockcount +
768 RIGHT.br_blockcount);
769 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
771 xfs_iext_remove(ip, idx, 2, state);
772 ip->i_df.if_lastex = idx - 1;
773 ip->i_d.di_nextents--;
775 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
777 rval = XFS_ILOG_CORE;
778 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
780 RIGHT.br_blockcount, &i)))
782 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
783 if ((error = xfs_btree_delete(cur, &i)))
785 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
786 if ((error = xfs_btree_decrement(cur, 0, &i)))
788 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
789 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
793 RIGHT.br_blockcount, LEFT.br_state)))
797 /* DELTA: Three in-core extents are replaced by one. */
798 temp = LEFT.br_startoff;
799 temp2 = LEFT.br_blockcount +
804 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
806 * Filling in all of a previously delayed allocation extent.
807 * The left neighbor is contiguous, the right is not.
809 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
810 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
811 LEFT.br_blockcount + PREV.br_blockcount);
812 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
814 ip->i_df.if_lastex = idx - 1;
815 xfs_iext_remove(ip, idx, 1, state);
817 rval = XFS_ILOG_DEXT;
820 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
821 LEFT.br_startblock, LEFT.br_blockcount,
824 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
825 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
828 PREV.br_blockcount, LEFT.br_state)))
832 /* DELTA: Two in-core extents are replaced by one. */
833 temp = LEFT.br_startoff;
834 temp2 = LEFT.br_blockcount +
838 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
840 * Filling in all of a previously delayed allocation extent.
841 * The right neighbor is contiguous, the left is not.
843 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
844 xfs_bmbt_set_startblock(ep, new->br_startblock);
845 xfs_bmbt_set_blockcount(ep,
846 PREV.br_blockcount + RIGHT.br_blockcount);
847 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
849 ip->i_df.if_lastex = idx;
850 xfs_iext_remove(ip, idx + 1, 1, state);
852 rval = XFS_ILOG_DEXT;
855 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
857 RIGHT.br_blockcount, &i)))
859 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
860 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
863 RIGHT.br_blockcount, PREV.br_state)))
867 /* DELTA: Two in-core extents are replaced by one. */
868 temp = PREV.br_startoff;
869 temp2 = PREV.br_blockcount +
873 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
875 * Filling in all of a previously delayed allocation extent.
876 * Neither the left nor right neighbors are contiguous with
879 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
880 xfs_bmbt_set_startblock(ep, new->br_startblock);
881 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
883 ip->i_df.if_lastex = idx;
884 ip->i_d.di_nextents++;
886 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
888 rval = XFS_ILOG_CORE;
889 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
890 new->br_startblock, new->br_blockcount,
893 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
894 cur->bc_rec.b.br_state = XFS_EXT_NORM;
895 if ((error = xfs_btree_insert(cur, &i)))
897 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
900 /* DELTA: The in-core extent described by new changed type. */
901 temp = new->br_startoff;
902 temp2 = new->br_blockcount;
905 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
907 * Filling in the first part of a previous delayed allocation.
908 * The left neighbor is contiguous.
910 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
911 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
912 LEFT.br_blockcount + new->br_blockcount);
913 xfs_bmbt_set_startoff(ep,
914 PREV.br_startoff + new->br_blockcount);
915 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
917 temp = PREV.br_blockcount - new->br_blockcount;
918 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
919 xfs_bmbt_set_blockcount(ep, temp);
920 ip->i_df.if_lastex = idx - 1;
922 rval = XFS_ILOG_DEXT;
925 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
926 LEFT.br_startblock, LEFT.br_blockcount,
929 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
930 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
937 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
938 startblockval(PREV.br_startblock));
939 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
940 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
942 /* DELTA: The boundary between two in-core extents moved. */
943 temp = LEFT.br_startoff;
944 temp2 = LEFT.br_blockcount +
948 case BMAP_LEFT_FILLING:
950 * Filling in the first part of a previous delayed allocation.
951 * The left neighbor is not contiguous.
953 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
954 xfs_bmbt_set_startoff(ep, new_endoff);
955 temp = PREV.br_blockcount - new->br_blockcount;
956 xfs_bmbt_set_blockcount(ep, temp);
957 xfs_iext_insert(ip, idx, 1, new, state);
958 ip->i_df.if_lastex = idx;
959 ip->i_d.di_nextents++;
961 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
963 rval = XFS_ILOG_CORE;
964 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
965 new->br_startblock, new->br_blockcount,
968 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
969 cur->bc_rec.b.br_state = XFS_EXT_NORM;
970 if ((error = xfs_btree_insert(cur, &i)))
972 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
974 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
975 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
976 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
977 first, flist, &cur, 1, &tmp_rval,
983 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
984 startblockval(PREV.br_startblock) -
985 (cur ? cur->bc_private.b.allocated : 0));
986 ep = xfs_iext_get_ext(ifp, idx + 1);
987 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
988 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
990 /* DELTA: One in-core extent is split in two. */
991 temp = PREV.br_startoff;
992 temp2 = PREV.br_blockcount;
995 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
997 * Filling in the last part of a previous delayed allocation.
998 * The right neighbor is contiguous with the new allocation.
1000 temp = PREV.br_blockcount - new->br_blockcount;
1001 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1002 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1003 xfs_bmbt_set_blockcount(ep, temp);
1004 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1005 new->br_startoff, new->br_startblock,
1006 new->br_blockcount + RIGHT.br_blockcount,
1008 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1009 ip->i_df.if_lastex = idx + 1;
1011 rval = XFS_ILOG_DEXT;
1014 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1015 RIGHT.br_startblock,
1016 RIGHT.br_blockcount, &i)))
1018 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1019 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1021 new->br_blockcount +
1022 RIGHT.br_blockcount,
1026 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1027 startblockval(PREV.br_startblock));
1028 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1029 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1031 /* DELTA: The boundary between two in-core extents moved. */
1032 temp = PREV.br_startoff;
1033 temp2 = PREV.br_blockcount +
1034 RIGHT.br_blockcount;
1037 case BMAP_RIGHT_FILLING:
1039 * Filling in the last part of a previous delayed allocation.
1040 * The right neighbor is not contiguous.
1042 temp = PREV.br_blockcount - new->br_blockcount;
1043 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1044 xfs_bmbt_set_blockcount(ep, temp);
1045 xfs_iext_insert(ip, idx + 1, 1, new, state);
1046 ip->i_df.if_lastex = idx + 1;
1047 ip->i_d.di_nextents++;
1049 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1051 rval = XFS_ILOG_CORE;
1052 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1053 new->br_startblock, new->br_blockcount,
1056 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1057 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1058 if ((error = xfs_btree_insert(cur, &i)))
1060 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1062 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1063 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1064 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1065 first, flist, &cur, 1, &tmp_rval,
1071 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1072 startblockval(PREV.br_startblock) -
1073 (cur ? cur->bc_private.b.allocated : 0));
1074 ep = xfs_iext_get_ext(ifp, idx);
1075 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1076 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1078 /* DELTA: One in-core extent is split in two. */
1079 temp = PREV.br_startoff;
1080 temp2 = PREV.br_blockcount;
1085 * Filling in the middle part of a previous delayed allocation.
1086 * Contiguity is impossible here.
1087 * This case is avoided almost all the time.
1089 temp = new->br_startoff - PREV.br_startoff;
1090 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1091 xfs_bmbt_set_blockcount(ep, temp);
1093 r[1].br_state = PREV.br_state;
1094 r[1].br_startblock = 0;
1095 r[1].br_startoff = new_endoff;
1096 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1097 r[1].br_blockcount = temp2;
1098 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1099 ip->i_df.if_lastex = idx + 1;
1100 ip->i_d.di_nextents++;
1102 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1104 rval = XFS_ILOG_CORE;
1105 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1106 new->br_startblock, new->br_blockcount,
1109 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1110 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1111 if ((error = xfs_btree_insert(cur, &i)))
1113 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1115 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1116 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1117 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1118 first, flist, &cur, 1, &tmp_rval,
1124 temp = xfs_bmap_worst_indlen(ip, temp);
1125 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1126 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1127 (cur ? cur->bc_private.b.allocated : 0));
1129 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1131 * Ick gross gag me with a spoon.
1133 ASSERT(0); /* want to see if this ever happens! */
1139 !xfs_mod_incore_sb(ip->i_mount,
1140 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1147 !xfs_mod_incore_sb(ip->i_mount,
1148 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1153 ep = xfs_iext_get_ext(ifp, idx);
1154 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1155 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1156 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1157 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1158 nullstartblock((int)temp2));
1159 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1160 *dnew = temp + temp2;
1161 /* DELTA: One in-core extent is split in three. */
1162 temp = PREV.br_startoff;
1163 temp2 = PREV.br_blockcount;
1166 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1167 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1168 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1169 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1170 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1171 case BMAP_LEFT_CONTIG:
1172 case BMAP_RIGHT_CONTIG:
1174 * These cases are all impossible.
1181 if (delta->xed_startoff > temp)
1182 delta->xed_startoff = temp;
1183 if (delta->xed_blockcount < temp2)
1184 delta->xed_blockcount = temp2;
1195 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1196 * allocation to a real allocation or vice versa.
1198 STATIC int /* error */
1199 xfs_bmap_add_extent_unwritten_real(
1200 xfs_inode_t *ip, /* incore inode pointer */
1201 xfs_extnum_t idx, /* extent number to update/insert */
1202 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1203 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1204 int *logflagsp, /* inode logging flags */
1205 xfs_extdelta_t *delta) /* Change made to incore extents */
1207 xfs_btree_cur_t *cur; /* btree cursor */
1208 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1209 int error; /* error return value */
1210 int i; /* temp state */
1211 xfs_ifork_t *ifp; /* inode fork pointer */
1212 xfs_fileoff_t new_endoff; /* end offset of new entry */
1213 xfs_exntst_t newext; /* new extent state */
1214 xfs_exntst_t oldext; /* old extent state */
1215 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1216 /* left is 0, right is 1, prev is 2 */
1217 int rval=0; /* return value (logging flags) */
1218 int state = 0;/* state bits, accessed thru macros */
1219 xfs_filblks_t temp=0;
1220 xfs_filblks_t temp2=0;
1226 * Set up a bunch of variables to make the tests simpler.
1230 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1231 ep = xfs_iext_get_ext(ifp, idx);
1232 xfs_bmbt_get_all(ep, &PREV);
1233 newext = new->br_state;
1234 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1235 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1236 ASSERT(PREV.br_state == oldext);
1237 new_endoff = new->br_startoff + new->br_blockcount;
1238 ASSERT(PREV.br_startoff <= new->br_startoff);
1239 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1242 * Set flags determining what part of the previous oldext allocation
1243 * extent is being replaced by a newext allocation.
1245 if (PREV.br_startoff == new->br_startoff)
1246 state |= BMAP_LEFT_FILLING;
1247 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1248 state |= BMAP_RIGHT_FILLING;
1251 * Check and set flags if this segment has a left neighbor.
1252 * Don't set contiguous if the combined extent would be too large.
1255 state |= BMAP_LEFT_VALID;
1256 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1258 if (isnullstartblock(LEFT.br_startblock))
1259 state |= BMAP_LEFT_DELAY;
1262 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1263 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1264 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1265 LEFT.br_state == newext &&
1266 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1267 state |= BMAP_LEFT_CONTIG;
1270 * Check and set flags if this segment has a right neighbor.
1271 * Don't set contiguous if the combined extent would be too large.
1272 * Also check for all-three-contiguous being too large.
1274 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1275 state |= BMAP_RIGHT_VALID;
1276 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1277 if (isnullstartblock(RIGHT.br_startblock))
1278 state |= BMAP_RIGHT_DELAY;
1281 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1282 new_endoff == RIGHT.br_startoff &&
1283 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1284 newext == RIGHT.br_state &&
1285 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1286 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1287 BMAP_RIGHT_FILLING)) !=
1288 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1289 BMAP_RIGHT_FILLING) ||
1290 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1292 state |= BMAP_RIGHT_CONTIG;
1295 * Switch out based on the FILLING and CONTIG state bits.
1297 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1298 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1299 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1300 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1302 * Setting all of a previous oldext extent to newext.
1303 * The left and right neighbors are both contiguous with new.
1305 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1306 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1307 LEFT.br_blockcount + PREV.br_blockcount +
1308 RIGHT.br_blockcount);
1309 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1311 xfs_iext_remove(ip, idx, 2, state);
1312 ip->i_df.if_lastex = idx - 1;
1313 ip->i_d.di_nextents -= 2;
1315 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1317 rval = XFS_ILOG_CORE;
1318 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1319 RIGHT.br_startblock,
1320 RIGHT.br_blockcount, &i)))
1322 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1323 if ((error = xfs_btree_delete(cur, &i)))
1325 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1326 if ((error = xfs_btree_decrement(cur, 0, &i)))
1328 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1329 if ((error = xfs_btree_delete(cur, &i)))
1331 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1332 if ((error = xfs_btree_decrement(cur, 0, &i)))
1334 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1335 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1337 LEFT.br_blockcount + PREV.br_blockcount +
1338 RIGHT.br_blockcount, LEFT.br_state)))
1341 /* DELTA: Three in-core extents are replaced by one. */
1342 temp = LEFT.br_startoff;
1343 temp2 = LEFT.br_blockcount +
1344 PREV.br_blockcount +
1345 RIGHT.br_blockcount;
1348 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1350 * Setting all of a previous oldext extent to newext.
1351 * The left neighbor is contiguous, the right is not.
1353 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1354 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1355 LEFT.br_blockcount + PREV.br_blockcount);
1356 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1358 ip->i_df.if_lastex = idx - 1;
1359 xfs_iext_remove(ip, idx, 1, state);
1360 ip->i_d.di_nextents--;
1362 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1364 rval = XFS_ILOG_CORE;
1365 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1366 PREV.br_startblock, PREV.br_blockcount,
1369 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1370 if ((error = xfs_btree_delete(cur, &i)))
1372 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1373 if ((error = xfs_btree_decrement(cur, 0, &i)))
1375 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1376 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1378 LEFT.br_blockcount + PREV.br_blockcount,
1382 /* DELTA: Two in-core extents are replaced by one. */
1383 temp = LEFT.br_startoff;
1384 temp2 = LEFT.br_blockcount +
1388 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1390 * Setting all of a previous oldext extent to newext.
1391 * The right neighbor is contiguous, the left is not.
1393 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1394 xfs_bmbt_set_blockcount(ep,
1395 PREV.br_blockcount + RIGHT.br_blockcount);
1396 xfs_bmbt_set_state(ep, newext);
1397 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1398 ip->i_df.if_lastex = idx;
1399 xfs_iext_remove(ip, idx + 1, 1, state);
1400 ip->i_d.di_nextents--;
1402 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1404 rval = XFS_ILOG_CORE;
1405 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1406 RIGHT.br_startblock,
1407 RIGHT.br_blockcount, &i)))
1409 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1410 if ((error = xfs_btree_delete(cur, &i)))
1412 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1413 if ((error = xfs_btree_decrement(cur, 0, &i)))
1415 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1416 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1418 new->br_blockcount + RIGHT.br_blockcount,
1422 /* DELTA: Two in-core extents are replaced by one. */
1423 temp = PREV.br_startoff;
1424 temp2 = PREV.br_blockcount +
1425 RIGHT.br_blockcount;
1428 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1430 * Setting all of a previous oldext extent to newext.
1431 * Neither the left nor right neighbors are contiguous with
1434 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1435 xfs_bmbt_set_state(ep, newext);
1436 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1438 ip->i_df.if_lastex = idx;
1440 rval = XFS_ILOG_DEXT;
1443 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1444 new->br_startblock, new->br_blockcount,
1447 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1448 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1449 new->br_startblock, new->br_blockcount,
1453 /* DELTA: The in-core extent described by new changed type. */
1454 temp = new->br_startoff;
1455 temp2 = new->br_blockcount;
1458 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1460 * Setting the first part of a previous oldext extent to newext.
1461 * The left neighbor is contiguous.
1463 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1464 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1465 LEFT.br_blockcount + new->br_blockcount);
1466 xfs_bmbt_set_startoff(ep,
1467 PREV.br_startoff + new->br_blockcount);
1468 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1470 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1471 xfs_bmbt_set_startblock(ep,
1472 new->br_startblock + new->br_blockcount);
1473 xfs_bmbt_set_blockcount(ep,
1474 PREV.br_blockcount - new->br_blockcount);
1475 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1477 ip->i_df.if_lastex = idx - 1;
1479 rval = XFS_ILOG_DEXT;
1482 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1483 PREV.br_startblock, PREV.br_blockcount,
1486 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1487 if ((error = xfs_bmbt_update(cur,
1488 PREV.br_startoff + new->br_blockcount,
1489 PREV.br_startblock + new->br_blockcount,
1490 PREV.br_blockcount - new->br_blockcount,
1493 if ((error = xfs_btree_decrement(cur, 0, &i)))
1495 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1497 LEFT.br_blockcount + new->br_blockcount,
1501 /* DELTA: The boundary between two in-core extents moved. */
1502 temp = LEFT.br_startoff;
1503 temp2 = LEFT.br_blockcount +
1507 case BMAP_LEFT_FILLING:
1509 * Setting the first part of a previous oldext extent to newext.
1510 * The left neighbor is not contiguous.
1512 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1513 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1514 xfs_bmbt_set_startoff(ep, new_endoff);
1515 xfs_bmbt_set_blockcount(ep,
1516 PREV.br_blockcount - new->br_blockcount);
1517 xfs_bmbt_set_startblock(ep,
1518 new->br_startblock + new->br_blockcount);
1519 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1521 xfs_iext_insert(ip, idx, 1, new, state);
1522 ip->i_df.if_lastex = idx;
1523 ip->i_d.di_nextents++;
1525 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1527 rval = XFS_ILOG_CORE;
1528 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1529 PREV.br_startblock, PREV.br_blockcount,
1532 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1533 if ((error = xfs_bmbt_update(cur,
1534 PREV.br_startoff + new->br_blockcount,
1535 PREV.br_startblock + new->br_blockcount,
1536 PREV.br_blockcount - new->br_blockcount,
1539 cur->bc_rec.b = *new;
1540 if ((error = xfs_btree_insert(cur, &i)))
1542 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1544 /* DELTA: One in-core extent is split in two. */
1545 temp = PREV.br_startoff;
1546 temp2 = PREV.br_blockcount;
1549 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1551 * Setting the last part of a previous oldext extent to newext.
1552 * The right neighbor is contiguous with the new allocation.
1554 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1555 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1556 xfs_bmbt_set_blockcount(ep,
1557 PREV.br_blockcount - new->br_blockcount);
1558 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1559 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1560 new->br_startoff, new->br_startblock,
1561 new->br_blockcount + RIGHT.br_blockcount, newext);
1562 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1564 ip->i_df.if_lastex = idx + 1;
1566 rval = XFS_ILOG_DEXT;
1569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1571 PREV.br_blockcount, &i)))
1573 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1574 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1576 PREV.br_blockcount - new->br_blockcount,
1579 if ((error = xfs_btree_increment(cur, 0, &i)))
1581 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1583 new->br_blockcount + RIGHT.br_blockcount,
1587 /* DELTA: The boundary between two in-core extents moved. */
1588 temp = PREV.br_startoff;
1589 temp2 = PREV.br_blockcount +
1590 RIGHT.br_blockcount;
1593 case BMAP_RIGHT_FILLING:
1595 * Setting the last part of a previous oldext extent to newext.
1596 * The right neighbor is not contiguous.
1598 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1599 xfs_bmbt_set_blockcount(ep,
1600 PREV.br_blockcount - new->br_blockcount);
1601 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1603 xfs_iext_insert(ip, idx + 1, 1, new, state);
1604 ip->i_df.if_lastex = idx + 1;
1605 ip->i_d.di_nextents++;
1607 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1609 rval = XFS_ILOG_CORE;
1610 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1611 PREV.br_startblock, PREV.br_blockcount,
1614 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1615 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1617 PREV.br_blockcount - new->br_blockcount,
1620 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1621 new->br_startblock, new->br_blockcount,
1624 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1625 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1626 if ((error = xfs_btree_insert(cur, &i)))
1628 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1630 /* DELTA: One in-core extent is split in two. */
1631 temp = PREV.br_startoff;
1632 temp2 = PREV.br_blockcount;
1637 * Setting the middle part of a previous oldext extent to
1638 * newext. Contiguity is impossible here.
1639 * One extent becomes three extents.
1641 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1642 xfs_bmbt_set_blockcount(ep,
1643 new->br_startoff - PREV.br_startoff);
1644 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1647 r[1].br_startoff = new_endoff;
1648 r[1].br_blockcount =
1649 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1650 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1651 r[1].br_state = oldext;
1652 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1653 ip->i_df.if_lastex = idx + 1;
1654 ip->i_d.di_nextents += 2;
1656 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1658 rval = XFS_ILOG_CORE;
1659 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1660 PREV.br_startblock, PREV.br_blockcount,
1663 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1664 /* new right extent - oldext */
1665 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1666 r[1].br_startblock, r[1].br_blockcount,
1669 /* new left extent - oldext */
1670 cur->bc_rec.b = PREV;
1671 cur->bc_rec.b.br_blockcount =
1672 new->br_startoff - PREV.br_startoff;
1673 if ((error = xfs_btree_insert(cur, &i)))
1675 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1677 * Reset the cursor to the position of the new extent
1678 * we are about to insert as we can't trust it after
1679 * the previous insert.
1681 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1682 new->br_startblock, new->br_blockcount,
1685 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1686 /* new middle extent - newext */
1687 cur->bc_rec.b.br_state = new->br_state;
1688 if ((error = xfs_btree_insert(cur, &i)))
1690 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1692 /* DELTA: One in-core extent is split in three. */
1693 temp = PREV.br_startoff;
1694 temp2 = PREV.br_blockcount;
1697 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1698 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1699 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1700 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1701 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1702 case BMAP_LEFT_CONTIG:
1703 case BMAP_RIGHT_CONTIG:
1705 * These cases are all impossible.
1712 if (delta->xed_startoff > temp)
1713 delta->xed_startoff = temp;
1714 if (delta->xed_blockcount < temp2)
1715 delta->xed_blockcount = temp2;
1726 * Called by xfs_bmap_add_extent to handle cases converting a hole
1727 * to a delayed allocation.
1730 STATIC int /* error */
1731 xfs_bmap_add_extent_hole_delay(
1732 xfs_inode_t *ip, /* incore inode pointer */
1733 xfs_extnum_t idx, /* extent number to update/insert */
1734 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1735 int *logflagsp, /* inode logging flags */
1736 xfs_extdelta_t *delta, /* Change made to incore extents */
1737 int rsvd) /* OK to allocate reserved blocks */
1739 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1740 xfs_ifork_t *ifp; /* inode fork pointer */
1741 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1742 xfs_filblks_t newlen=0; /* new indirect size */
1743 xfs_filblks_t oldlen=0; /* old indirect size */
1744 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1745 int state; /* state bits, accessed thru macros */
1746 xfs_filblks_t temp=0; /* temp for indirect calculations */
1747 xfs_filblks_t temp2=0;
1749 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1750 ep = xfs_iext_get_ext(ifp, idx);
1752 ASSERT(isnullstartblock(new->br_startblock));
1755 * Check and set flags if this segment has a left neighbor
1758 state |= BMAP_LEFT_VALID;
1759 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1761 if (isnullstartblock(left.br_startblock))
1762 state |= BMAP_LEFT_DELAY;
1766 * Check and set flags if the current (right) segment exists.
1767 * If it doesn't exist, we're converting the hole at end-of-file.
1769 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1770 state |= BMAP_RIGHT_VALID;
1771 xfs_bmbt_get_all(ep, &right);
1773 if (isnullstartblock(right.br_startblock))
1774 state |= BMAP_RIGHT_DELAY;
1778 * Set contiguity flags on the left and right neighbors.
1779 * Don't let extents get too large, even if the pieces are contiguous.
1781 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1782 left.br_startoff + left.br_blockcount == new->br_startoff &&
1783 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1784 state |= BMAP_LEFT_CONTIG;
1786 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1787 new->br_startoff + new->br_blockcount == right.br_startoff &&
1788 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1789 (!(state & BMAP_LEFT_CONTIG) ||
1790 (left.br_blockcount + new->br_blockcount +
1791 right.br_blockcount <= MAXEXTLEN)))
1792 state |= BMAP_RIGHT_CONTIG;
1795 * Switch out based on the contiguity flags.
1797 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1798 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1800 * New allocation is contiguous with delayed allocations
1801 * on the left and on the right.
1802 * Merge all three into a single extent record.
1804 temp = left.br_blockcount + new->br_blockcount +
1805 right.br_blockcount;
1807 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1808 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1809 oldlen = startblockval(left.br_startblock) +
1810 startblockval(new->br_startblock) +
1811 startblockval(right.br_startblock);
1812 newlen = xfs_bmap_worst_indlen(ip, temp);
1813 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1814 nullstartblock((int)newlen));
1815 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1817 xfs_iext_remove(ip, idx, 1, state);
1818 ip->i_df.if_lastex = idx - 1;
1819 /* DELTA: Two in-core extents were replaced by one. */
1821 temp = left.br_startoff;
1824 case BMAP_LEFT_CONTIG:
1826 * New allocation is contiguous with a delayed allocation
1828 * Merge the new allocation with the left neighbor.
1830 temp = left.br_blockcount + new->br_blockcount;
1831 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1832 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1833 oldlen = startblockval(left.br_startblock) +
1834 startblockval(new->br_startblock);
1835 newlen = xfs_bmap_worst_indlen(ip, temp);
1836 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1837 nullstartblock((int)newlen));
1838 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1840 ip->i_df.if_lastex = idx - 1;
1841 /* DELTA: One in-core extent grew into a hole. */
1843 temp = left.br_startoff;
1846 case BMAP_RIGHT_CONTIG:
1848 * New allocation is contiguous with a delayed allocation
1850 * Merge the new allocation with the right neighbor.
1852 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1853 temp = new->br_blockcount + right.br_blockcount;
1854 oldlen = startblockval(new->br_startblock) +
1855 startblockval(right.br_startblock);
1856 newlen = xfs_bmap_worst_indlen(ip, temp);
1857 xfs_bmbt_set_allf(ep, new->br_startoff,
1858 nullstartblock((int)newlen), temp, right.br_state);
1859 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1861 ip->i_df.if_lastex = idx;
1862 /* DELTA: One in-core extent grew into a hole. */
1864 temp = new->br_startoff;
1869 * New allocation is not contiguous with another
1870 * delayed allocation.
1871 * Insert a new entry.
1873 oldlen = newlen = 0;
1874 xfs_iext_insert(ip, idx, 1, new, state);
1875 ip->i_df.if_lastex = idx;
1876 /* DELTA: A new in-core extent was added in a hole. */
1877 temp2 = new->br_blockcount;
1878 temp = new->br_startoff;
1881 if (oldlen != newlen) {
1882 ASSERT(oldlen > newlen);
1883 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1884 (int64_t)(oldlen - newlen), rsvd);
1886 * Nothing to do for disk quota accounting here.
1891 if (delta->xed_startoff > temp)
1892 delta->xed_startoff = temp;
1893 if (delta->xed_blockcount < temp2)
1894 delta->xed_blockcount = temp2;
1901 * Called by xfs_bmap_add_extent to handle cases converting a hole
1902 * to a real allocation.
1904 STATIC int /* error */
1905 xfs_bmap_add_extent_hole_real(
1906 xfs_inode_t *ip, /* incore inode pointer */
1907 xfs_extnum_t idx, /* extent number to update/insert */
1908 xfs_btree_cur_t *cur, /* if null, not a btree */
1909 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1910 int *logflagsp, /* inode logging flags */
1911 xfs_extdelta_t *delta, /* Change made to incore extents */
1912 int whichfork) /* data or attr fork */
1914 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
1915 int error; /* error return value */
1916 int i; /* temp state */
1917 xfs_ifork_t *ifp; /* inode fork pointer */
1918 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1919 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1920 int rval=0; /* return value (logging flags) */
1921 int state; /* state bits, accessed thru macros */
1922 xfs_filblks_t temp=0;
1923 xfs_filblks_t temp2=0;
1925 ifp = XFS_IFORK_PTR(ip, whichfork);
1926 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1927 ep = xfs_iext_get_ext(ifp, idx);
1930 if (whichfork == XFS_ATTR_FORK)
1931 state |= BMAP_ATTRFORK;
1934 * Check and set flags if this segment has a left neighbor.
1937 state |= BMAP_LEFT_VALID;
1938 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1939 if (isnullstartblock(left.br_startblock))
1940 state |= BMAP_LEFT_DELAY;
1944 * Check and set flags if this segment has a current value.
1945 * Not true if we're inserting into the "hole" at eof.
1947 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1948 state |= BMAP_RIGHT_VALID;
1949 xfs_bmbt_get_all(ep, &right);
1950 if (isnullstartblock(right.br_startblock))
1951 state |= BMAP_RIGHT_DELAY;
1955 * We're inserting a real allocation between "left" and "right".
1956 * Set the contiguity flags. Don't let extents get too large.
1958 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1959 left.br_startoff + left.br_blockcount == new->br_startoff &&
1960 left.br_startblock + left.br_blockcount == new->br_startblock &&
1961 left.br_state == new->br_state &&
1962 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1963 state |= BMAP_LEFT_CONTIG;
1965 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1966 new->br_startoff + new->br_blockcount == right.br_startoff &&
1967 new->br_startblock + new->br_blockcount == right.br_startblock &&
1968 new->br_state == right.br_state &&
1969 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1970 (!(state & BMAP_LEFT_CONTIG) ||
1971 left.br_blockcount + new->br_blockcount +
1972 right.br_blockcount <= MAXEXTLEN))
1973 state |= BMAP_RIGHT_CONTIG;
1977 * Select which case we're in here, and implement it.
1979 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1980 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1982 * New allocation is contiguous with real allocations on the
1983 * left and on the right.
1984 * Merge all three into a single extent record.
1986 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1987 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1988 left.br_blockcount + new->br_blockcount +
1989 right.br_blockcount);
1990 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1992 xfs_iext_remove(ip, idx, 1, state);
1993 ifp->if_lastex = idx - 1;
1994 XFS_IFORK_NEXT_SET(ip, whichfork,
1995 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1997 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1999 rval = XFS_ILOG_CORE;
2000 if ((error = xfs_bmbt_lookup_eq(cur,
2002 right.br_startblock,
2003 right.br_blockcount, &i)))
2005 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2006 if ((error = xfs_btree_delete(cur, &i)))
2008 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2009 if ((error = xfs_btree_decrement(cur, 0, &i)))
2011 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2012 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2014 left.br_blockcount +
2015 new->br_blockcount +
2016 right.br_blockcount,
2020 /* DELTA: Two in-core extents were replaced by one. */
2021 temp = left.br_startoff;
2022 temp2 = left.br_blockcount +
2023 new->br_blockcount +
2024 right.br_blockcount;
2027 case BMAP_LEFT_CONTIG:
2029 * New allocation is contiguous with a real allocation
2031 * Merge the new allocation with the left neighbor.
2033 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
2034 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2035 left.br_blockcount + new->br_blockcount);
2036 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
2038 ifp->if_lastex = idx - 1;
2040 rval = xfs_ilog_fext(whichfork);
2043 if ((error = xfs_bmbt_lookup_eq(cur,
2046 left.br_blockcount, &i)))
2048 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2049 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2051 left.br_blockcount +
2056 /* DELTA: One in-core extent grew. */
2057 temp = left.br_startoff;
2058 temp2 = left.br_blockcount +
2062 case BMAP_RIGHT_CONTIG:
2064 * New allocation is contiguous with a real allocation
2066 * Merge the new allocation with the right neighbor.
2068 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2069 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2070 new->br_blockcount + right.br_blockcount,
2072 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2074 ifp->if_lastex = idx;
2076 rval = xfs_ilog_fext(whichfork);
2079 if ((error = xfs_bmbt_lookup_eq(cur,
2081 right.br_startblock,
2082 right.br_blockcount, &i)))
2084 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2085 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2087 new->br_blockcount +
2088 right.br_blockcount,
2092 /* DELTA: One in-core extent grew. */
2093 temp = new->br_startoff;
2094 temp2 = new->br_blockcount +
2095 right.br_blockcount;
2100 * New allocation is not contiguous with another
2102 * Insert a new entry.
2104 xfs_iext_insert(ip, idx, 1, new, state);
2105 ifp->if_lastex = idx;
2106 XFS_IFORK_NEXT_SET(ip, whichfork,
2107 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2109 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2111 rval = XFS_ILOG_CORE;
2112 if ((error = xfs_bmbt_lookup_eq(cur,
2115 new->br_blockcount, &i)))
2117 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2118 cur->bc_rec.b.br_state = new->br_state;
2119 if ((error = xfs_btree_insert(cur, &i)))
2121 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2123 /* DELTA: A new extent was added in a hole. */
2124 temp = new->br_startoff;
2125 temp2 = new->br_blockcount;
2130 if (delta->xed_startoff > temp)
2131 delta->xed_startoff = temp;
2132 if (delta->xed_blockcount < temp2)
2133 delta->xed_blockcount = temp2;
2141 * Adjust the size of the new extent based on di_extsize and rt extsize.
2144 xfs_bmap_extsize_align(
2146 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2147 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2148 xfs_extlen_t extsz, /* align to this extent size */
2149 int rt, /* is this a realtime inode? */
2150 int eof, /* is extent at end-of-file? */
2151 int delay, /* creating delalloc extent? */
2152 int convert, /* overwriting unwritten extent? */
2153 xfs_fileoff_t *offp, /* in/out: aligned offset */
2154 xfs_extlen_t *lenp) /* in/out: aligned length */
2156 xfs_fileoff_t orig_off; /* original offset */
2157 xfs_extlen_t orig_alen; /* original length */
2158 xfs_fileoff_t orig_end; /* original off+len */
2159 xfs_fileoff_t nexto; /* next file offset */
2160 xfs_fileoff_t prevo; /* previous file offset */
2161 xfs_fileoff_t align_off; /* temp for offset */
2162 xfs_extlen_t align_alen; /* temp for length */
2163 xfs_extlen_t temp; /* temp for calculations */
2168 orig_off = align_off = *offp;
2169 orig_alen = align_alen = *lenp;
2170 orig_end = orig_off + orig_alen;
2173 * If this request overlaps an existing extent, then don't
2174 * attempt to perform any additional alignment.
2176 if (!delay && !eof &&
2177 (orig_off >= gotp->br_startoff) &&
2178 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2183 * If the file offset is unaligned vs. the extent size
2184 * we need to align it. This will be possible unless
2185 * the file was previously written with a kernel that didn't
2186 * perform this alignment, or if a truncate shot us in the
2189 temp = do_mod(orig_off, extsz);
2195 * Same adjustment for the end of the requested area.
2197 if ((temp = (align_alen % extsz))) {
2198 align_alen += extsz - temp;
2201 * If the previous block overlaps with this proposed allocation
2202 * then move the start forward without adjusting the length.
2204 if (prevp->br_startoff != NULLFILEOFF) {
2205 if (prevp->br_startblock == HOLESTARTBLOCK)
2206 prevo = prevp->br_startoff;
2208 prevo = prevp->br_startoff + prevp->br_blockcount;
2211 if (align_off != orig_off && align_off < prevo)
2214 * If the next block overlaps with this proposed allocation
2215 * then move the start back without adjusting the length,
2216 * but not before offset 0.
2217 * This may of course make the start overlap previous block,
2218 * and if we hit the offset 0 limit then the next block
2219 * can still overlap too.
2221 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2222 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2223 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2224 nexto = gotp->br_startoff + gotp->br_blockcount;
2226 nexto = gotp->br_startoff;
2228 nexto = NULLFILEOFF;
2230 align_off + align_alen != orig_end &&
2231 align_off + align_alen > nexto)
2232 align_off = nexto > align_alen ? nexto - align_alen : 0;
2234 * If we're now overlapping the next or previous extent that
2235 * means we can't fit an extsz piece in this hole. Just move
2236 * the start forward to the first valid spot and set
2237 * the length so we hit the end.
2239 if (align_off != orig_off && align_off < prevo)
2241 if (align_off + align_alen != orig_end &&
2242 align_off + align_alen > nexto &&
2243 nexto != NULLFILEOFF) {
2244 ASSERT(nexto > prevo);
2245 align_alen = nexto - align_off;
2249 * If realtime, and the result isn't a multiple of the realtime
2250 * extent size we need to remove blocks until it is.
2252 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2254 * We're not covering the original request, or
2255 * we won't be able to once we fix the length.
2257 if (orig_off < align_off ||
2258 orig_end > align_off + align_alen ||
2259 align_alen - temp < orig_alen)
2260 return XFS_ERROR(EINVAL);
2262 * Try to fix it by moving the start up.
2264 if (align_off + temp <= orig_off) {
2269 * Try to fix it by moving the end in.
2271 else if (align_off + align_alen - temp >= orig_end)
2274 * Set the start to the minimum then trim the length.
2277 align_alen -= orig_off - align_off;
2278 align_off = orig_off;
2279 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2282 * Result doesn't cover the request, fail it.
2284 if (orig_off < align_off || orig_end > align_off + align_alen)
2285 return XFS_ERROR(EINVAL);
2287 ASSERT(orig_off >= align_off);
2288 ASSERT(orig_end <= align_off + align_alen);
2292 if (!eof && gotp->br_startoff != NULLFILEOFF)
2293 ASSERT(align_off + align_alen <= gotp->br_startoff);
2294 if (prevp->br_startoff != NULLFILEOFF)
2295 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2303 #define XFS_ALLOC_GAP_UNITS 4
2307 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2309 xfs_fsblock_t adjust; /* adjustment to block numbers */
2310 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2311 xfs_mount_t *mp; /* mount point structure */
2312 int nullfb; /* true if ap->firstblock isn't set */
2313 int rt; /* true if inode is realtime */
2315 #define ISVALID(x,y) \
2317 (x) < mp->m_sb.sb_rblocks : \
2318 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2319 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2320 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2322 mp = ap->ip->i_mount;
2323 nullfb = ap->firstblock == NULLFSBLOCK;
2324 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2325 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2327 * If allocating at eof, and there's a previous real block,
2328 * try to use its last block as our starting point.
2330 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2331 !isnullstartblock(ap->prevp->br_startblock) &&
2332 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2333 ap->prevp->br_startblock)) {
2334 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2336 * Adjust for the gap between prevp and us.
2339 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2341 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2345 * If not at eof, then compare the two neighbor blocks.
2346 * Figure out whether either one gives us a good starting point,
2347 * and pick the better one.
2349 else if (!ap->eof) {
2350 xfs_fsblock_t gotbno; /* right side block number */
2351 xfs_fsblock_t gotdiff=0; /* right side difference */
2352 xfs_fsblock_t prevbno; /* left side block number */
2353 xfs_fsblock_t prevdiff=0; /* left side difference */
2356 * If there's a previous (left) block, select a requested
2357 * start block based on it.
2359 if (ap->prevp->br_startoff != NULLFILEOFF &&
2360 !isnullstartblock(ap->prevp->br_startblock) &&
2361 (prevbno = ap->prevp->br_startblock +
2362 ap->prevp->br_blockcount) &&
2363 ISVALID(prevbno, ap->prevp->br_startblock)) {
2365 * Calculate gap to end of previous block.
2367 adjust = prevdiff = ap->off -
2368 (ap->prevp->br_startoff +
2369 ap->prevp->br_blockcount);
2371 * Figure the startblock based on the previous block's
2372 * end and the gap size.
2374 * If the gap is large relative to the piece we're
2375 * allocating, or using it gives us an invalid block
2376 * number, then just use the end of the previous block.
2378 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2379 ISVALID(prevbno + prevdiff,
2380 ap->prevp->br_startblock))
2385 * If the firstblock forbids it, can't use it,
2388 if (!rt && !nullfb &&
2389 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2390 prevbno = NULLFSBLOCK;
2393 * No previous block or can't follow it, just default.
2396 prevbno = NULLFSBLOCK;
2398 * If there's a following (right) block, select a requested
2399 * start block based on it.
2401 if (!isnullstartblock(ap->gotp->br_startblock)) {
2403 * Calculate gap to start of next block.
2405 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2407 * Figure the startblock based on the next block's
2408 * start and the gap size.
2410 gotbno = ap->gotp->br_startblock;
2413 * If the gap is large relative to the piece we're
2414 * allocating, or using it gives us an invalid block
2415 * number, then just use the start of the next block
2416 * offset by our length.
2418 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2419 ISVALID(gotbno - gotdiff, gotbno))
2421 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2423 gotdiff += adjust - ap->alen;
2427 * If the firstblock forbids it, can't use it,
2430 if (!rt && !nullfb &&
2431 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2432 gotbno = NULLFSBLOCK;
2435 * No next block, just default.
2438 gotbno = NULLFSBLOCK;
2440 * If both valid, pick the better one, else the only good
2441 * one, else ap->rval is already set (to 0 or the inode block).
2443 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2444 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2445 else if (prevbno != NULLFSBLOCK)
2447 else if (gotbno != NULLFSBLOCK)
2455 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2457 xfs_alloctype_t atype = 0; /* type for allocation routines */
2458 int error; /* error return value */
2459 xfs_mount_t *mp; /* mount point structure */
2460 xfs_extlen_t prod = 0; /* product factor for allocators */
2461 xfs_extlen_t ralen = 0; /* realtime allocation length */
2462 xfs_extlen_t align; /* minimum allocation alignment */
2465 mp = ap->ip->i_mount;
2466 align = xfs_get_extsz_hint(ap->ip);
2467 prod = align / mp->m_sb.sb_rextsize;
2468 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2469 align, 1, ap->eof, 0,
2470 ap->conv, &ap->off, &ap->alen);
2474 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2477 * If the offset & length are not perfectly aligned
2478 * then kill prod, it will just get us in trouble.
2480 if (do_mod(ap->off, align) || ap->alen % align)
2483 * Set ralen to be the actual requested length in rtextents.
2485 ralen = ap->alen / mp->m_sb.sb_rextsize;
2487 * If the old value was close enough to MAXEXTLEN that
2488 * we rounded up to it, cut it back so it's valid again.
2489 * Note that if it's a really large request (bigger than
2490 * MAXEXTLEN), we don't hear about that number, and can't
2491 * adjust the starting point to match it.
2493 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2494 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2496 * If it's an allocation to an empty file at offset 0,
2497 * pick an extent that will space things out in the rt area.
2499 if (ap->eof && ap->off == 0) {
2500 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2502 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2505 ap->rval = rtx * mp->m_sb.sb_rextsize;
2510 xfs_bmap_adjacent(ap);
2513 * Realtime allocation, done through xfs_rtallocate_extent.
2515 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2516 do_div(ap->rval, mp->m_sb.sb_rextsize);
2519 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2520 &ralen, atype, ap->wasdel, prod, &rtb)))
2522 if (rtb == NULLFSBLOCK && prod > 1 &&
2523 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2524 ap->alen, &ralen, atype,
2525 ap->wasdel, 1, &rtb)))
2528 if (ap->rval != NULLFSBLOCK) {
2529 ap->rval *= mp->m_sb.sb_rextsize;
2530 ralen *= mp->m_sb.sb_rextsize;
2532 ap->ip->i_d.di_nblocks += ralen;
2533 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2535 ap->ip->i_delayed_blks -= ralen;
2537 * Adjust the disk quota also. This was reserved
2540 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2541 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2542 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2550 xfs_bmap_btalloc_nullfb(
2551 struct xfs_bmalloca *ap,
2552 struct xfs_alloc_arg *args,
2555 struct xfs_mount *mp = ap->ip->i_mount;
2556 struct xfs_perag *pag;
2557 xfs_agnumber_t ag, startag;
2561 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2562 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2564 args->type = XFS_ALLOCTYPE_START_BNO;
2565 args->total = ap->total;
2568 * Search for an allocation group with a single extent large enough
2569 * for the request. If one isn't found, then adjust the minimum
2570 * allocation size to the largest space found.
2572 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2573 if (startag == NULLAGNUMBER)
2576 pag = xfs_perag_get(mp, ag);
2577 while (*blen < ap->alen) {
2578 if (!pag->pagf_init) {
2579 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2580 XFS_ALLOC_FLAG_TRYLOCK);
2588 * See xfs_alloc_fix_freelist...
2590 if (pag->pagf_init) {
2591 xfs_extlen_t longest;
2592 longest = xfs_alloc_longest_free_extent(mp, pag);
2593 if (*blen < longest)
2598 if (xfs_inode_is_filestream(ap->ip)) {
2599 if (*blen >= ap->alen)
2604 * If startag is an invalid AG, we've
2605 * come here once before and
2606 * xfs_filestream_new_ag picked the
2607 * best currently available.
2609 * Don't continue looping, since we
2610 * could loop forever.
2612 if (startag == NULLAGNUMBER)
2615 error = xfs_filestream_new_ag(ap, &ag);
2620 /* loop again to set 'blen'*/
2621 startag = NULLAGNUMBER;
2622 pag = xfs_perag_get(mp, ag);
2626 if (++ag == mp->m_sb.sb_agcount)
2631 pag = xfs_perag_get(mp, ag);
2636 * Since the above loop did a BUF_TRYLOCK, it is
2637 * possible that there is space for this request.
2639 if (notinit || *blen < ap->minlen)
2640 args->minlen = ap->minlen;
2642 * If the best seen length is less than the request
2643 * length, use the best as the minimum.
2645 else if (*blen < ap->alen)
2646 args->minlen = *blen;
2648 * Otherwise we've seen an extent as big as alen,
2649 * use that as the minimum.
2652 args->minlen = ap->alen;
2655 * set the failure fallback case to look in the selected
2656 * AG as the stream may have moved.
2658 if (xfs_inode_is_filestream(ap->ip))
2659 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2666 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2668 xfs_mount_t *mp; /* mount point structure */
2669 xfs_alloctype_t atype = 0; /* type for allocation routines */
2670 xfs_extlen_t align; /* minimum allocation alignment */
2671 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2673 xfs_alloc_arg_t args;
2675 xfs_extlen_t nextminlen = 0;
2676 int nullfb; /* true if ap->firstblock isn't set */
2681 mp = ap->ip->i_mount;
2682 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2683 if (unlikely(align)) {
2684 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2685 align, 0, ap->eof, 0, ap->conv,
2686 &ap->off, &ap->alen);
2690 nullfb = ap->firstblock == NULLFSBLOCK;
2691 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2693 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2694 ag = xfs_filestream_lookup_ag(ap->ip);
2695 ag = (ag != NULLAGNUMBER) ? ag : 0;
2696 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2698 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2701 ap->rval = ap->firstblock;
2703 xfs_bmap_adjacent(ap);
2706 * If allowed, use ap->rval; otherwise must use firstblock since
2707 * it's in the right allocation group.
2709 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2712 ap->rval = ap->firstblock;
2714 * Normal allocation, done through xfs_alloc_vextent.
2716 tryagain = isaligned = 0;
2719 args.fsbno = ap->rval;
2720 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2721 args.firstblock = ap->firstblock;
2724 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2727 } else if (ap->low) {
2728 if (xfs_inode_is_filestream(ap->ip))
2729 args.type = XFS_ALLOCTYPE_FIRST_AG;
2731 args.type = XFS_ALLOCTYPE_START_BNO;
2732 args.total = args.minlen = ap->minlen;
2734 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2735 args.total = ap->total;
2736 args.minlen = ap->minlen;
2738 /* apply extent size hints if obtained earlier */
2739 if (unlikely(align)) {
2741 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2742 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2743 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2747 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2748 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2749 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2752 * If we are not low on available data blocks, and the
2753 * underlying logical volume manager is a stripe, and
2754 * the file offset is zero then try to allocate data
2755 * blocks on stripe unit boundary.
2756 * NOTE: ap->aeof is only set if the allocation length
2757 * is >= the stripe unit and the allocation offset is
2758 * at the end of file.
2760 if (!ap->low && ap->aeof) {
2762 args.alignment = mp->m_dalign;
2766 * Adjust for alignment
2768 if (blen > args.alignment && blen <= ap->alen)
2769 args.minlen = blen - args.alignment;
2770 args.minalignslop = 0;
2773 * First try an exact bno allocation.
2774 * If it fails then do a near or start bno
2775 * allocation with alignment turned on.
2779 args.type = XFS_ALLOCTYPE_THIS_BNO;
2782 * Compute the minlen+alignment for the
2783 * next case. Set slop so that the value
2784 * of minlen+alignment+slop doesn't go up
2785 * between the calls.
2787 if (blen > mp->m_dalign && blen <= ap->alen)
2788 nextminlen = blen - mp->m_dalign;
2790 nextminlen = args.minlen;
2791 if (nextminlen + mp->m_dalign > args.minlen + 1)
2793 nextminlen + mp->m_dalign -
2796 args.minalignslop = 0;
2800 args.minalignslop = 0;
2802 args.minleft = ap->minleft;
2803 args.wasdel = ap->wasdel;
2805 args.userdata = ap->userdata;
2806 if ((error = xfs_alloc_vextent(&args)))
2808 if (tryagain && args.fsbno == NULLFSBLOCK) {
2810 * Exact allocation failed. Now try with alignment
2814 args.fsbno = ap->rval;
2815 args.alignment = mp->m_dalign;
2816 args.minlen = nextminlen;
2817 args.minalignslop = 0;
2819 if ((error = xfs_alloc_vextent(&args)))
2822 if (isaligned && args.fsbno == NULLFSBLOCK) {
2824 * allocation failed, so turn off alignment and
2828 args.fsbno = ap->rval;
2830 if ((error = xfs_alloc_vextent(&args)))
2833 if (args.fsbno == NULLFSBLOCK && nullfb &&
2834 args.minlen > ap->minlen) {
2835 args.minlen = ap->minlen;
2836 args.type = XFS_ALLOCTYPE_START_BNO;
2837 args.fsbno = ap->rval;
2838 if ((error = xfs_alloc_vextent(&args)))
2841 if (args.fsbno == NULLFSBLOCK && nullfb) {
2843 args.type = XFS_ALLOCTYPE_FIRST_AG;
2844 args.total = ap->minlen;
2846 if ((error = xfs_alloc_vextent(&args)))
2850 if (args.fsbno != NULLFSBLOCK) {
2851 ap->firstblock = ap->rval = args.fsbno;
2852 ASSERT(nullfb || fb_agno == args.agno ||
2853 (ap->low && fb_agno < args.agno));
2854 ap->alen = args.len;
2855 ap->ip->i_d.di_nblocks += args.len;
2856 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2858 ap->ip->i_delayed_blks -= args.len;
2860 * Adjust the disk quota also. This was reserved
2863 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2864 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2865 XFS_TRANS_DQ_BCOUNT,
2868 ap->rval = NULLFSBLOCK;
2875 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2876 * It figures out where to ask the underlying allocator to put the new extent.
2880 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2882 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2883 return xfs_bmap_rtalloc(ap);
2884 return xfs_bmap_btalloc(ap);
2888 * Transform a btree format file with only one leaf node, where the
2889 * extents list will fit in the inode, into an extents format file.
2890 * Since the file extents are already in-core, all we have to do is
2891 * give up the space for the btree root and pitch the leaf block.
2893 STATIC int /* error */
2894 xfs_bmap_btree_to_extents(
2895 xfs_trans_t *tp, /* transaction pointer */
2896 xfs_inode_t *ip, /* incore inode pointer */
2897 xfs_btree_cur_t *cur, /* btree cursor */
2898 int *logflagsp, /* inode logging flags */
2899 int whichfork) /* data or attr fork */
2902 struct xfs_btree_block *cblock;/* child btree block */
2903 xfs_fsblock_t cbno; /* child block number */
2904 xfs_buf_t *cbp; /* child block's buffer */
2905 int error; /* error return value */
2906 xfs_ifork_t *ifp; /* inode fork data */
2907 xfs_mount_t *mp; /* mount point structure */
2908 __be64 *pp; /* ptr to block address */
2909 struct xfs_btree_block *rblock;/* root btree block */
2912 ifp = XFS_IFORK_PTR(ip, whichfork);
2913 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2914 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2915 rblock = ifp->if_broot;
2916 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2917 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2918 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2919 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2920 cbno = be64_to_cpu(*pp);
2923 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2926 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2927 XFS_BMAP_BTREE_REF)))
2929 cblock = XFS_BUF_TO_BLOCK(cbp);
2930 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2932 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2933 ip->i_d.di_nblocks--;
2934 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2935 xfs_trans_binval(tp, cbp);
2936 if (cur->bc_bufs[0] == cbp)
2937 cur->bc_bufs[0] = NULL;
2938 xfs_iroot_realloc(ip, -1, whichfork);
2939 ASSERT(ifp->if_broot == NULL);
2940 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2941 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2942 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2947 * Called by xfs_bmapi to update file extent records and the btree
2948 * after removing space (or undoing a delayed allocation).
2950 STATIC int /* error */
2951 xfs_bmap_del_extent(
2952 xfs_inode_t *ip, /* incore inode pointer */
2953 xfs_trans_t *tp, /* current transaction pointer */
2954 xfs_extnum_t idx, /* extent number to update/delete */
2955 xfs_bmap_free_t *flist, /* list of extents to be freed */
2956 xfs_btree_cur_t *cur, /* if null, not a btree */
2957 xfs_bmbt_irec_t *del, /* data to remove from extents */
2958 int *logflagsp, /* inode logging flags */
2959 xfs_extdelta_t *delta, /* Change made to incore extents */
2960 int whichfork, /* data or attr fork */
2961 int rsvd) /* OK to allocate reserved blocks */
2963 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2964 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2965 xfs_fsblock_t del_endblock=0; /* first block past del */
2966 xfs_fileoff_t del_endoff; /* first offset past del */
2967 int delay; /* current block is delayed allocated */
2968 int do_fx; /* free extent at end of routine */
2969 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2970 int error; /* error return value */
2971 int flags; /* inode logging flags */
2972 xfs_bmbt_irec_t got; /* current extent entry */
2973 xfs_fileoff_t got_endoff; /* first offset past got */
2974 int i; /* temp state */
2975 xfs_ifork_t *ifp; /* inode fork pointer */
2976 xfs_mount_t *mp; /* mount structure */
2977 xfs_filblks_t nblks; /* quota/sb block count */
2978 xfs_bmbt_irec_t new; /* new record to be inserted */
2980 uint qfield; /* quota field to update */
2981 xfs_filblks_t temp; /* for indirect length calculations */
2982 xfs_filblks_t temp2; /* for indirect length calculations */
2985 XFS_STATS_INC(xs_del_exlist);
2987 if (whichfork == XFS_ATTR_FORK)
2988 state |= BMAP_ATTRFORK;
2991 ifp = XFS_IFORK_PTR(ip, whichfork);
2992 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2993 (uint)sizeof(xfs_bmbt_rec_t)));
2994 ASSERT(del->br_blockcount > 0);
2995 ep = xfs_iext_get_ext(ifp, idx);
2996 xfs_bmbt_get_all(ep, &got);
2997 ASSERT(got.br_startoff <= del->br_startoff);
2998 del_endoff = del->br_startoff + del->br_blockcount;
2999 got_endoff = got.br_startoff + got.br_blockcount;
3000 ASSERT(got_endoff >= del_endoff);
3001 delay = isnullstartblock(got.br_startblock);
3002 ASSERT(isnullstartblock(del->br_startblock) == delay);
3007 * If deleting a real allocation, must free up the disk space.
3010 flags = XFS_ILOG_CORE;
3012 * Realtime allocation. Free it and record di_nblocks update.
3014 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3018 ASSERT(do_mod(del->br_blockcount,
3019 mp->m_sb.sb_rextsize) == 0);
3020 ASSERT(do_mod(del->br_startblock,
3021 mp->m_sb.sb_rextsize) == 0);
3022 bno = del->br_startblock;
3023 len = del->br_blockcount;
3024 do_div(bno, mp->m_sb.sb_rextsize);
3025 do_div(len, mp->m_sb.sb_rextsize);
3026 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3027 (xfs_extlen_t)len)))
3030 nblks = len * mp->m_sb.sb_rextsize;
3031 qfield = XFS_TRANS_DQ_RTBCOUNT;
3034 * Ordinary allocation.
3038 nblks = del->br_blockcount;
3039 qfield = XFS_TRANS_DQ_BCOUNT;
3042 * Set up del_endblock and cur for later.
3044 del_endblock = del->br_startblock + del->br_blockcount;
3046 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3047 got.br_startblock, got.br_blockcount,
3050 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3052 da_old = da_new = 0;
3054 da_old = startblockval(got.br_startblock);
3060 * Set flag value to use in switch statement.
3061 * Left-contig is 2, right-contig is 1.
3063 switch (((got.br_startoff == del->br_startoff) << 1) |
3064 (got_endoff == del_endoff)) {
3067 * Matches the whole extent. Delete the entry.
3069 xfs_iext_remove(ip, idx, 1,
3070 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
3071 ifp->if_lastex = idx;
3074 XFS_IFORK_NEXT_SET(ip, whichfork,
3075 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3076 flags |= XFS_ILOG_CORE;
3078 flags |= xfs_ilog_fext(whichfork);
3081 if ((error = xfs_btree_delete(cur, &i)))
3083 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3088 * Deleting the first part of the extent.
3090 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3091 xfs_bmbt_set_startoff(ep, del_endoff);
3092 temp = got.br_blockcount - del->br_blockcount;
3093 xfs_bmbt_set_blockcount(ep, temp);
3094 ifp->if_lastex = idx;
3096 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3098 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3099 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3103 xfs_bmbt_set_startblock(ep, del_endblock);
3104 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3106 flags |= xfs_ilog_fext(whichfork);
3109 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3110 got.br_blockcount - del->br_blockcount,
3117 * Deleting the last part of the extent.
3119 temp = got.br_blockcount - del->br_blockcount;
3120 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3121 xfs_bmbt_set_blockcount(ep, temp);
3122 ifp->if_lastex = idx;
3124 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3126 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3127 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3131 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3133 flags |= xfs_ilog_fext(whichfork);
3136 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3138 got.br_blockcount - del->br_blockcount,
3145 * Deleting the middle of the extent.
3147 temp = del->br_startoff - got.br_startoff;
3148 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3149 xfs_bmbt_set_blockcount(ep, temp);
3150 new.br_startoff = del_endoff;
3151 temp2 = got_endoff - del_endoff;
3152 new.br_blockcount = temp2;
3153 new.br_state = got.br_state;
3155 new.br_startblock = del_endblock;
3156 flags |= XFS_ILOG_CORE;
3158 if ((error = xfs_bmbt_update(cur,
3160 got.br_startblock, temp,
3163 if ((error = xfs_btree_increment(cur, 0, &i)))
3165 cur->bc_rec.b = new;
3166 error = xfs_btree_insert(cur, &i);
3167 if (error && error != ENOSPC)
3170 * If get no-space back from btree insert,
3171 * it tried a split, and we have a zero
3172 * block reservation.
3173 * Fix up our state and return the error.
3175 if (error == ENOSPC) {
3177 * Reset the cursor, don't trust
3178 * it after any insert operation.
3180 if ((error = xfs_bmbt_lookup_eq(cur,
3185 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3187 * Update the btree record back
3188 * to the original value.
3190 if ((error = xfs_bmbt_update(cur,
3197 * Reset the extent record back
3198 * to the original value.
3200 xfs_bmbt_set_blockcount(ep,
3203 error = XFS_ERROR(ENOSPC);
3206 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3208 flags |= xfs_ilog_fext(whichfork);
3209 XFS_IFORK_NEXT_SET(ip, whichfork,
3210 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3212 ASSERT(whichfork == XFS_DATA_FORK);
3213 temp = xfs_bmap_worst_indlen(ip, temp);
3214 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3215 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3216 new.br_startblock = nullstartblock((int)temp2);
3217 da_new = temp + temp2;
3218 while (da_new > da_old) {
3222 xfs_bmbt_set_startblock(ep,
3223 nullstartblock((int)temp));
3225 if (da_new == da_old)
3231 nullstartblock((int)temp2);
3235 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3236 xfs_iext_insert(ip, idx + 1, 1, &new, state);
3237 ifp->if_lastex = idx + 1;
3241 * If we need to, add to list of extents to delete.
3244 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3247 * Adjust inode # blocks in the file.
3250 ip->i_d.di_nblocks -= nblks;
3252 * Adjust quota data.
3255 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3258 * Account for change in delayed indirect blocks.
3259 * Nothing to do for disk quota accounting here.
3261 ASSERT(da_old >= da_new);
3262 if (da_old > da_new)
3263 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3266 /* DELTA: report the original extent. */
3267 if (delta->xed_startoff > got.br_startoff)
3268 delta->xed_startoff = got.br_startoff;
3269 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3270 delta->xed_blockcount = got.br_startoff +
3279 * Remove the entry "free" from the free item list. Prev points to the
3280 * previous entry, unless "free" is the head of the list.
3284 xfs_bmap_free_t *flist, /* free item list header */
3285 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3286 xfs_bmap_free_item_t *free) /* list item to be freed */
3289 prev->xbfi_next = free->xbfi_next;
3291 flist->xbf_first = free->xbfi_next;
3293 kmem_zone_free(xfs_bmap_free_item_zone, free);
3297 * Convert an extents-format file into a btree-format file.
3298 * The new file will have a root block (in the inode) and a single child block.
3300 STATIC int /* error */
3301 xfs_bmap_extents_to_btree(
3302 xfs_trans_t *tp, /* transaction pointer */
3303 xfs_inode_t *ip, /* incore inode pointer */
3304 xfs_fsblock_t *firstblock, /* first-block-allocated */
3305 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3306 xfs_btree_cur_t **curp, /* cursor returned to caller */
3307 int wasdel, /* converting a delayed alloc */
3308 int *logflagsp, /* inode logging flags */
3309 int whichfork) /* data or attr fork */
3311 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3312 xfs_buf_t *abp; /* buffer for ablock */
3313 xfs_alloc_arg_t args; /* allocation arguments */
3314 xfs_bmbt_rec_t *arp; /* child record pointer */
3315 struct xfs_btree_block *block; /* btree root block */
3316 xfs_btree_cur_t *cur; /* bmap btree cursor */
3317 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3318 int error; /* error return value */
3319 xfs_extnum_t i, cnt; /* extent record index */
3320 xfs_ifork_t *ifp; /* inode fork pointer */
3321 xfs_bmbt_key_t *kp; /* root block key pointer */
3322 xfs_mount_t *mp; /* mount structure */
3323 xfs_extnum_t nextents; /* number of file extents */
3324 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3326 ifp = XFS_IFORK_PTR(ip, whichfork);
3327 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3328 ASSERT(ifp->if_ext_max ==
3329 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3331 * Make space in the inode incore.
3333 xfs_iroot_realloc(ip, 1, whichfork);
3334 ifp->if_flags |= XFS_IFBROOT;
3339 block = ifp->if_broot;
3340 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3341 block->bb_level = cpu_to_be16(1);
3342 block->bb_numrecs = cpu_to_be16(1);
3343 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3344 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3347 * Need a cursor. Can't allocate until bb_level is filled in.
3350 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3351 cur->bc_private.b.firstblock = *firstblock;
3352 cur->bc_private.b.flist = flist;
3353 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3355 * Convert to a btree with two levels, one record in root.
3357 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3360 args.firstblock = *firstblock;
3361 if (*firstblock == NULLFSBLOCK) {
3362 args.type = XFS_ALLOCTYPE_START_BNO;
3363 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3364 } else if (flist->xbf_low) {
3365 args.type = XFS_ALLOCTYPE_START_BNO;
3366 args.fsbno = *firstblock;
3368 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3369 args.fsbno = *firstblock;
3371 args.minlen = args.maxlen = args.prod = 1;
3372 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3373 args.minalignslop = 0;
3374 args.wasdel = wasdel;
3376 if ((error = xfs_alloc_vextent(&args))) {
3377 xfs_iroot_realloc(ip, -1, whichfork);
3378 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3382 * Allocation can't fail, the space was reserved.
3384 ASSERT(args.fsbno != NULLFSBLOCK);
3385 ASSERT(*firstblock == NULLFSBLOCK ||
3386 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3388 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3389 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3390 cur->bc_private.b.allocated++;
3391 ip->i_d.di_nblocks++;
3392 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3393 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3395 * Fill in the child block.
3397 ablock = XFS_BUF_TO_BLOCK(abp);
3398 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3399 ablock->bb_level = 0;
3400 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3401 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3402 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3403 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3404 for (cnt = i = 0; i < nextents; i++) {
3405 ep = xfs_iext_get_ext(ifp, i);
3406 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3407 arp->l0 = cpu_to_be64(ep->l0);
3408 arp->l1 = cpu_to_be64(ep->l1);
3412 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3413 xfs_btree_set_numrecs(ablock, cnt);
3416 * Fill in the root key and pointer.
3418 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3419 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3420 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3421 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3422 be16_to_cpu(block->bb_level)));
3423 *pp = cpu_to_be64(args.fsbno);
3426 * Do all this logging at the end so that
3427 * the root is at the right level.
3429 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3430 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3431 ASSERT(*curp == NULL);
3433 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3438 * Calculate the default attribute fork offset for newly created inodes.
3441 xfs_default_attroffset(
3442 struct xfs_inode *ip)
3444 struct xfs_mount *mp = ip->i_mount;
3447 if (mp->m_sb.sb_inodesize == 256) {
3448 offset = XFS_LITINO(mp) -
3449 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3451 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3454 ASSERT(offset < XFS_LITINO(mp));
3459 * Helper routine to reset inode di_forkoff field when switching
3460 * attribute fork from local to extent format - we reset it where
3461 * possible to make space available for inline data fork extents.
3464 xfs_bmap_forkoff_reset(
3469 if (whichfork == XFS_ATTR_FORK &&
3470 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3471 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3472 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3473 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3475 if (dfl_forkoff > ip->i_d.di_forkoff) {
3476 ip->i_d.di_forkoff = dfl_forkoff;
3477 ip->i_df.if_ext_max =
3478 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3479 ip->i_afp->if_ext_max =
3480 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3486 * Convert a local file to an extents file.
3487 * This code is out of bounds for data forks of regular files,
3488 * since the file data needs to get logged so things will stay consistent.
3489 * (The bmap-level manipulations are ok, though).
3491 STATIC int /* error */
3492 xfs_bmap_local_to_extents(
3493 xfs_trans_t *tp, /* transaction pointer */
3494 xfs_inode_t *ip, /* incore inode pointer */
3495 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3496 xfs_extlen_t total, /* total blocks needed by transaction */
3497 int *logflagsp, /* inode logging flags */
3498 int whichfork) /* data or attr fork */
3500 int error; /* error return value */
3501 int flags; /* logging flags returned */
3502 xfs_ifork_t *ifp; /* inode fork pointer */
3505 * We don't want to deal with the case of keeping inode data inline yet.
3506 * So sending the data fork of a regular inode is invalid.
3508 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3509 whichfork == XFS_DATA_FORK));
3510 ifp = XFS_IFORK_PTR(ip, whichfork);
3511 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3514 if (ifp->if_bytes) {
3515 xfs_alloc_arg_t args; /* allocation arguments */
3516 xfs_buf_t *bp; /* buffer for extent block */
3517 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3520 args.mp = ip->i_mount;
3521 args.firstblock = *firstblock;
3522 ASSERT((ifp->if_flags &
3523 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3525 * Allocate a block. We know we need only one, since the
3526 * file currently fits in an inode.
3528 if (*firstblock == NULLFSBLOCK) {
3529 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3530 args.type = XFS_ALLOCTYPE_START_BNO;
3532 args.fsbno = *firstblock;
3533 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3536 args.mod = args.minleft = args.alignment = args.wasdel =
3537 args.isfl = args.minalignslop = 0;
3538 args.minlen = args.maxlen = args.prod = 1;
3539 if ((error = xfs_alloc_vextent(&args)))
3542 * Can't fail, the space was reserved.
3544 ASSERT(args.fsbno != NULLFSBLOCK);
3545 ASSERT(args.len == 1);
3546 *firstblock = args.fsbno;
3547 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3548 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3550 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3551 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3552 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3553 xfs_iext_add(ifp, 0, 1);
3554 ep = xfs_iext_get_ext(ifp, 0);
3555 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3556 trace_xfs_bmap_post_update(ip, 0,
3557 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3559 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3560 ip->i_d.di_nblocks = 1;
3561 xfs_trans_mod_dquot_byino(tp, ip,
3562 XFS_TRANS_DQ_BCOUNT, 1L);
3563 flags |= xfs_ilog_fext(whichfork);
3565 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3566 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3568 ifp->if_flags &= ~XFS_IFINLINE;
3569 ifp->if_flags |= XFS_IFEXTENTS;
3570 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3571 flags |= XFS_ILOG_CORE;
3578 * Search the extent records for the entry containing block bno.
3579 * If bno lies in a hole, point to the next entry. If bno lies
3580 * past eof, *eofp will be set, and *prevp will contain the last
3581 * entry (null if none). Else, *lastxp will be set to the index
3582 * of the found entry; *gotp will contain the entry.
3584 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3585 xfs_bmap_search_multi_extents(
3586 xfs_ifork_t *ifp, /* inode fork pointer */
3587 xfs_fileoff_t bno, /* block number searched for */
3588 int *eofp, /* out: end of file found */
3589 xfs_extnum_t *lastxp, /* out: last extent index */
3590 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3591 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3593 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3594 xfs_extnum_t lastx; /* last extent index */
3597 * Initialize the extent entry structure to catch access to
3598 * uninitialized br_startblock field.
3600 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3601 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3602 gotp->br_state = XFS_EXT_INVALID;
3604 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3606 gotp->br_startblock = 0xffffa5a5;
3608 prevp->br_startoff = NULLFILEOFF;
3610 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3612 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3614 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3615 xfs_bmbt_get_all(ep, gotp);
3629 * Search the extents list for the inode, for the extent containing bno.
3630 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3631 * *eofp will be set, and *prevp will contain the last entry (null if none).
3632 * Else, *lastxp will be set to the index of the found
3633 * entry; *gotp will contain the entry.
3635 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3636 xfs_bmap_search_extents(
3637 xfs_inode_t *ip, /* incore inode pointer */
3638 xfs_fileoff_t bno, /* block number searched for */
3639 int fork, /* data or attr fork */
3640 int *eofp, /* out: end of file found */
3641 xfs_extnum_t *lastxp, /* out: last extent index */
3642 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3643 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3645 xfs_ifork_t *ifp; /* inode fork pointer */
3646 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3648 XFS_STATS_INC(xs_look_exlist);
3649 ifp = XFS_IFORK_PTR(ip, fork);
3651 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3653 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3654 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3655 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3656 "Access to block zero in inode %llu "
3657 "start_block: %llx start_off: %llx "
3658 "blkcnt: %llx extent-state: %x lastx: %x\n",
3659 (unsigned long long)ip->i_ino,
3660 (unsigned long long)gotp->br_startblock,
3661 (unsigned long long)gotp->br_startoff,
3662 (unsigned long long)gotp->br_blockcount,
3663 gotp->br_state, *lastxp);
3664 *lastxp = NULLEXTNUM;
3672 * Compute the worst-case number of indirect blocks that will be used
3673 * for ip's delayed extent of length "len".
3675 STATIC xfs_filblks_t
3676 xfs_bmap_worst_indlen(
3677 xfs_inode_t *ip, /* incore inode pointer */
3678 xfs_filblks_t len) /* delayed extent length */
3680 int level; /* btree level number */
3681 int maxrecs; /* maximum record count at this level */
3682 xfs_mount_t *mp; /* mount structure */
3683 xfs_filblks_t rval; /* return value */
3686 maxrecs = mp->m_bmap_dmxr[0];
3687 for (level = 0, rval = 0;
3688 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3691 do_div(len, maxrecs);
3694 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3697 maxrecs = mp->m_bmap_dmxr[1];
3703 * Convert inode from non-attributed to attributed.
3704 * Must not be in a transaction, ip must not be locked.
3706 int /* error code */
3707 xfs_bmap_add_attrfork(
3708 xfs_inode_t *ip, /* incore inode pointer */
3709 int size, /* space new attribute needs */
3710 int rsvd) /* xact may use reserved blks */
3712 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3713 xfs_bmap_free_t flist; /* freed extent records */
3714 xfs_mount_t *mp; /* mount structure */
3715 xfs_trans_t *tp; /* transaction pointer */
3716 int blks; /* space reservation */
3717 int version = 1; /* superblock attr version */
3718 int committed; /* xaction was committed */
3719 int logflags; /* logging flags */
3720 int error; /* error return value */
3722 ASSERT(XFS_IFORK_Q(ip) == 0);
3723 ASSERT(ip->i_df.if_ext_max ==
3724 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3727 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3728 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3729 blks = XFS_ADDAFORK_SPACE_RES(mp);
3731 tp->t_flags |= XFS_TRANS_RESERVE;
3732 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3733 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3735 xfs_ilock(ip, XFS_ILOCK_EXCL);
3736 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3737 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3738 XFS_QMOPT_RES_REGBLKS);
3740 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3741 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3744 if (XFS_IFORK_Q(ip))
3746 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3748 * For inodes coming from pre-6.2 filesystems.
3750 ASSERT(ip->i_d.di_aformat == 0);
3751 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3753 ASSERT(ip->i_d.di_anextents == 0);
3755 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3756 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3758 switch (ip->i_d.di_format) {
3759 case XFS_DINODE_FMT_DEV:
3760 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3762 case XFS_DINODE_FMT_UUID:
3763 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3765 case XFS_DINODE_FMT_LOCAL:
3766 case XFS_DINODE_FMT_EXTENTS:
3767 case XFS_DINODE_FMT_BTREE:
3768 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3769 if (!ip->i_d.di_forkoff)
3770 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3771 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3776 error = XFS_ERROR(EINVAL);
3779 ip->i_df.if_ext_max =
3780 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3781 ASSERT(ip->i_afp == NULL);
3782 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3783 ip->i_afp->if_ext_max =
3784 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3785 ip->i_afp->if_flags = XFS_IFEXTENTS;
3787 xfs_bmap_init(&flist, &firstblock);
3788 switch (ip->i_d.di_format) {
3789 case XFS_DINODE_FMT_LOCAL:
3790 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3793 case XFS_DINODE_FMT_EXTENTS:
3794 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3797 case XFS_DINODE_FMT_BTREE:
3798 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3806 xfs_trans_log_inode(tp, ip, logflags);
3809 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3810 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3811 __int64_t sbfields = 0;
3813 spin_lock(&mp->m_sb_lock);
3814 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3815 xfs_sb_version_addattr(&mp->m_sb);
3816 sbfields |= XFS_SB_VERSIONNUM;
3818 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3819 xfs_sb_version_addattr2(&mp->m_sb);
3820 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3823 spin_unlock(&mp->m_sb_lock);
3824 xfs_mod_sb(tp, sbfields);
3826 spin_unlock(&mp->m_sb_lock);
3828 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3830 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3831 ASSERT(ip->i_df.if_ext_max ==
3832 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3835 xfs_bmap_cancel(&flist);
3837 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3839 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3840 ASSERT(ip->i_df.if_ext_max ==
3841 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3846 * Add the extent to the list of extents to be free at transaction end.
3847 * The list is maintained sorted (by block number).
3852 xfs_fsblock_t bno, /* fs block number of extent */
3853 xfs_filblks_t len, /* length of extent */
3854 xfs_bmap_free_t *flist, /* list of extents */
3855 xfs_mount_t *mp) /* mount point structure */
3857 xfs_bmap_free_item_t *cur; /* current (next) element */
3858 xfs_bmap_free_item_t *new; /* new element */
3859 xfs_bmap_free_item_t *prev; /* previous element */
3861 xfs_agnumber_t agno;
3862 xfs_agblock_t agbno;
3864 ASSERT(bno != NULLFSBLOCK);
3866 ASSERT(len <= MAXEXTLEN);
3867 ASSERT(!isnullstartblock(bno));
3868 agno = XFS_FSB_TO_AGNO(mp, bno);
3869 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3870 ASSERT(agno < mp->m_sb.sb_agcount);
3871 ASSERT(agbno < mp->m_sb.sb_agblocks);
3872 ASSERT(len < mp->m_sb.sb_agblocks);
3873 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3875 ASSERT(xfs_bmap_free_item_zone != NULL);
3876 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3877 new->xbfi_startblock = bno;
3878 new->xbfi_blockcount = (xfs_extlen_t)len;
3879 for (prev = NULL, cur = flist->xbf_first;
3881 prev = cur, cur = cur->xbfi_next) {
3882 if (cur->xbfi_startblock >= bno)
3886 prev->xbfi_next = new;
3888 flist->xbf_first = new;
3889 new->xbfi_next = cur;
3894 * Compute and fill in the value of the maximum depth of a bmap btree
3895 * in this filesystem. Done once, during mount.
3898 xfs_bmap_compute_maxlevels(
3899 xfs_mount_t *mp, /* file system mount structure */
3900 int whichfork) /* data or attr fork */
3902 int level; /* btree level */
3903 uint maxblocks; /* max blocks at this level */
3904 uint maxleafents; /* max leaf entries possible */
3905 int maxrootrecs; /* max records in root block */
3906 int minleafrecs; /* min records in leaf block */
3907 int minnoderecs; /* min records in node block */
3908 int sz; /* root block size */
3911 * The maximum number of extents in a file, hence the maximum
3912 * number of leaf entries, is controlled by the type of di_nextents
3913 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3914 * (a signed 16-bit number, xfs_aextnum_t).
3916 * Note that we can no longer assume that if we are in ATTR1 that
3917 * the fork offset of all the inodes will be
3918 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3919 * with ATTR2 and then mounted back with ATTR1, keeping the
3920 * di_forkoff's fixed but probably at various positions. Therefore,
3921 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3922 * of a minimum size available.
3924 if (whichfork == XFS_DATA_FORK) {
3925 maxleafents = MAXEXTNUM;
3926 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3928 maxleafents = MAXAEXTNUM;
3929 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3931 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3932 minleafrecs = mp->m_bmap_dmnr[0];
3933 minnoderecs = mp->m_bmap_dmnr[1];
3934 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3935 for (level = 1; maxblocks > 1; level++) {
3936 if (maxblocks <= maxrootrecs)
3939 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3941 mp->m_bm_maxlevels[whichfork] = level;
3945 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3946 * caller. Frees all the extents that need freeing, which must be done
3947 * last due to locking considerations. We never free any extents in
3948 * the first transaction. This is to allow the caller to make the first
3949 * transaction a synchronous one so that the pointers to the data being
3950 * broken in this transaction will be permanent before the data is actually
3951 * freed. This is necessary to prevent blocks from being reallocated
3952 * and written to before the free and reallocation are actually permanent.
3953 * We do not just make the first transaction synchronous here, because
3954 * there are more efficient ways to gain the same protection in some cases
3955 * (see the file truncation code).
3957 * Return 1 if the given transaction was committed and a new one
3958 * started, and 0 otherwise in the committed parameter.
3963 xfs_trans_t **tp, /* transaction pointer addr */
3964 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3965 int *committed) /* xact committed or not */
3967 xfs_efd_log_item_t *efd; /* extent free data */
3968 xfs_efi_log_item_t *efi; /* extent free intention */
3969 int error; /* error return value */
3970 xfs_bmap_free_item_t *free; /* free extent item */
3971 unsigned int logres; /* new log reservation */
3972 unsigned int logcount; /* new log count */
3973 xfs_mount_t *mp; /* filesystem mount structure */
3974 xfs_bmap_free_item_t *next; /* next item on free list */
3975 xfs_trans_t *ntp; /* new transaction pointer */
3977 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3978 if (flist->xbf_count == 0) {
3983 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3984 for (free = flist->xbf_first; free; free = free->xbfi_next)
3985 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3986 free->xbfi_blockcount);
3987 logres = ntp->t_log_res;
3988 logcount = ntp->t_log_count;
3989 ntp = xfs_trans_dup(*tp);
3990 error = xfs_trans_commit(*tp, 0);
3994 * We have a new transaction, so we should return committed=1,
3995 * even though we're returning an error.
4001 * transaction commit worked ok so we can drop the extra ticket
4002 * reference that we gained in xfs_trans_dup()
4004 xfs_log_ticket_put(ntp->t_ticket);
4006 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4009 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4010 for (free = flist->xbf_first; free != NULL; free = next) {
4011 next = free->xbfi_next;
4012 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4013 free->xbfi_blockcount))) {
4015 * The bmap free list will be cleaned up at a
4016 * higher level. The EFI will be canceled when
4017 * this transaction is aborted.
4018 * Need to force shutdown here to make sure it
4019 * happens, since this transaction may not be
4023 if (!XFS_FORCED_SHUTDOWN(mp))
4024 xfs_force_shutdown(mp,
4025 (error == EFSCORRUPTED) ?
4026 SHUTDOWN_CORRUPT_INCORE :
4027 SHUTDOWN_META_IO_ERROR);
4030 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4031 free->xbfi_blockcount);
4032 xfs_bmap_del_free(flist, NULL, free);
4038 * Free up any items left in the list.
4042 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4044 xfs_bmap_free_item_t *free; /* free list item */
4045 xfs_bmap_free_item_t *next;
4047 if (flist->xbf_count == 0)
4049 ASSERT(flist->xbf_first != NULL);
4050 for (free = flist->xbf_first; free; free = next) {
4051 next = free->xbfi_next;
4052 xfs_bmap_del_free(flist, NULL, free);
4054 ASSERT(flist->xbf_count == 0);
4058 * Returns the file-relative block number of the first unused block(s)
4059 * in the file with at least "len" logically contiguous blocks free.
4060 * This is the lowest-address hole if the file has holes, else the first block
4061 * past the end of file.
4062 * Return 0 if the file is currently local (in-inode).
4065 xfs_bmap_first_unused(
4066 xfs_trans_t *tp, /* transaction pointer */
4067 xfs_inode_t *ip, /* incore inode */
4068 xfs_extlen_t len, /* size of hole to find */
4069 xfs_fileoff_t *first_unused, /* unused block */
4070 int whichfork) /* data or attr fork */
4072 int error; /* error return value */
4073 int idx; /* extent record index */
4074 xfs_ifork_t *ifp; /* inode fork pointer */
4075 xfs_fileoff_t lastaddr; /* last block number seen */
4076 xfs_fileoff_t lowest; /* lowest useful block */
4077 xfs_fileoff_t max; /* starting useful block */
4078 xfs_fileoff_t off; /* offset for this block */
4079 xfs_extnum_t nextents; /* number of extent entries */
4081 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4082 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4083 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4084 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4088 ifp = XFS_IFORK_PTR(ip, whichfork);
4089 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4090 (error = xfs_iread_extents(tp, ip, whichfork)))
4092 lowest = *first_unused;
4093 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4094 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4095 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4096 off = xfs_bmbt_get_startoff(ep);
4098 * See if the hole before this extent will work.
4100 if (off >= lowest + len && off - max >= len) {
4101 *first_unused = max;
4104 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4105 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4107 *first_unused = max;
4112 * Returns the file-relative block number of the last block + 1 before
4113 * last_block (input value) in the file.
4114 * This is not based on i_size, it is based on the extent records.
4115 * Returns 0 for local files, as they do not have extent records.
4118 xfs_bmap_last_before(
4119 xfs_trans_t *tp, /* transaction pointer */
4120 xfs_inode_t *ip, /* incore inode */
4121 xfs_fileoff_t *last_block, /* last block */
4122 int whichfork) /* data or attr fork */
4124 xfs_fileoff_t bno; /* input file offset */
4125 int eof; /* hit end of file */
4126 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4127 int error; /* error return value */
4128 xfs_bmbt_irec_t got; /* current extent value */
4129 xfs_ifork_t *ifp; /* inode fork pointer */
4130 xfs_extnum_t lastx; /* last extent used */
4131 xfs_bmbt_irec_t prev; /* previous extent value */
4133 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4134 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4135 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4136 return XFS_ERROR(EIO);
4137 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4141 ifp = XFS_IFORK_PTR(ip, whichfork);
4142 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4143 (error = xfs_iread_extents(tp, ip, whichfork)))
4145 bno = *last_block - 1;
4146 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4148 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4149 if (prev.br_startoff == NULLFILEOFF)
4152 *last_block = prev.br_startoff + prev.br_blockcount;
4155 * Otherwise *last_block is already the right answer.
4161 * Returns the file-relative block number of the first block past eof in
4162 * the file. This is not based on i_size, it is based on the extent records.
4163 * Returns 0 for local files, as they do not have extent records.
4166 xfs_bmap_last_offset(
4167 xfs_trans_t *tp, /* transaction pointer */
4168 xfs_inode_t *ip, /* incore inode */
4169 xfs_fileoff_t *last_block, /* last block */
4170 int whichfork) /* data or attr fork */
4172 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4173 int error; /* error return value */
4174 xfs_ifork_t *ifp; /* inode fork pointer */
4175 xfs_extnum_t nextents; /* number of extent entries */
4177 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4178 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4179 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4180 return XFS_ERROR(EIO);
4181 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4185 ifp = XFS_IFORK_PTR(ip, whichfork);
4186 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4187 (error = xfs_iread_extents(tp, ip, whichfork)))
4189 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4194 ep = xfs_iext_get_ext(ifp, nextents - 1);
4195 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4200 * Returns whether the selected fork of the inode has exactly one
4201 * block or not. For the data fork we check this matches di_size,
4202 * implying the file's range is 0..bsize-1.
4204 int /* 1=>1 block, 0=>otherwise */
4206 xfs_inode_t *ip, /* incore inode */
4207 int whichfork) /* data or attr fork */
4209 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4210 xfs_ifork_t *ifp; /* inode fork pointer */
4211 int rval; /* return value */
4212 xfs_bmbt_irec_t s; /* internal version of extent */
4215 if (whichfork == XFS_DATA_FORK) {
4216 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4217 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4218 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4221 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4223 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4225 ifp = XFS_IFORK_PTR(ip, whichfork);
4226 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4227 ep = xfs_iext_get_ext(ifp, 0);
4228 xfs_bmbt_get_all(ep, &s);
4229 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4230 if (rval && whichfork == XFS_DATA_FORK)
4231 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4236 xfs_bmap_sanity_check(
4237 struct xfs_mount *mp,
4241 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4243 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4244 be16_to_cpu(block->bb_level) != level ||
4245 be16_to_cpu(block->bb_numrecs) == 0 ||
4246 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4252 * Read in the extents to if_extents.
4253 * All inode fields are set up by caller, we just traverse the btree
4254 * and copy the records in. If the file system cannot contain unwritten
4255 * extents, the records are checked for no "state" flags.
4258 xfs_bmap_read_extents(
4259 xfs_trans_t *tp, /* transaction pointer */
4260 xfs_inode_t *ip, /* incore inode */
4261 int whichfork) /* data or attr fork */
4263 struct xfs_btree_block *block; /* current btree block */
4264 xfs_fsblock_t bno; /* block # of "block" */
4265 xfs_buf_t *bp; /* buffer for "block" */
4266 int error; /* error return value */
4267 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4268 xfs_extnum_t i, j; /* index into the extents list */
4269 xfs_ifork_t *ifp; /* fork structure */
4270 int level; /* btree level, for checking */
4271 xfs_mount_t *mp; /* file system mount structure */
4272 __be64 *pp; /* pointer to block address */
4274 xfs_extnum_t room; /* number of entries there's room for */
4278 ifp = XFS_IFORK_PTR(ip, whichfork);
4279 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4280 XFS_EXTFMT_INODE(ip);
4281 block = ifp->if_broot;
4283 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4285 level = be16_to_cpu(block->bb_level);
4287 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4288 bno = be64_to_cpu(*pp);
4289 ASSERT(bno != NULLDFSBNO);
4290 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4291 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4293 * Go down the tree until leaf level is reached, following the first
4294 * pointer (leftmost) at each level.
4296 while (level-- > 0) {
4297 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4298 XFS_BMAP_BTREE_REF)))
4300 block = XFS_BUF_TO_BLOCK(bp);
4301 XFS_WANT_CORRUPTED_GOTO(
4302 xfs_bmap_sanity_check(mp, bp, level),
4306 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4307 bno = be64_to_cpu(*pp);
4308 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4309 xfs_trans_brelse(tp, bp);
4312 * Here with bp and block set to the leftmost leaf node in the tree.
4314 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4317 * Loop over all leaf nodes. Copy information to the extent records.
4320 xfs_bmbt_rec_t *frp;
4321 xfs_fsblock_t nextbno;
4322 xfs_extnum_t num_recs;
4326 num_recs = xfs_btree_get_numrecs(block);
4327 if (unlikely(i + num_recs > room)) {
4328 ASSERT(i + num_recs <= room);
4329 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4330 "corrupt dinode %Lu, (btree extents).",
4331 (unsigned long long) ip->i_ino);
4332 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4337 XFS_WANT_CORRUPTED_GOTO(
4338 xfs_bmap_sanity_check(mp, bp, 0),
4341 * Read-ahead the next leaf block, if any.
4343 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4344 if (nextbno != NULLFSBLOCK)
4345 xfs_btree_reada_bufl(mp, nextbno, 1);
4347 * Copy records into the extent records.
4349 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4351 for (j = 0; j < num_recs; j++, i++, frp++) {
4352 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4353 trp->l0 = be64_to_cpu(frp->l0);
4354 trp->l1 = be64_to_cpu(frp->l1);
4356 if (exntf == XFS_EXTFMT_NOSTATE) {
4358 * Check all attribute bmap btree records and
4359 * any "older" data bmap btree records for a
4360 * set bit in the "extent flag" position.
4362 if (unlikely(xfs_check_nostate_extents(ifp,
4363 start, num_recs))) {
4364 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4370 xfs_trans_brelse(tp, bp);
4373 * If we've reached the end, stop.
4375 if (bno == NULLFSBLOCK)
4377 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4378 XFS_BMAP_BTREE_REF)))
4380 block = XFS_BUF_TO_BLOCK(bp);
4382 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4383 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4384 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4387 xfs_trans_brelse(tp, bp);
4388 return XFS_ERROR(EFSCORRUPTED);
4393 * Add bmap trace insert entries for all the contents of the extent records.
4396 xfs_bmap_trace_exlist(
4397 xfs_inode_t *ip, /* incore inode pointer */
4398 xfs_extnum_t cnt, /* count of entries in the list */
4399 int whichfork, /* data or attr fork */
4400 unsigned long caller_ip)
4402 xfs_extnum_t idx; /* extent record index */
4403 xfs_ifork_t *ifp; /* inode fork pointer */
4406 if (whichfork == XFS_ATTR_FORK)
4407 state |= BMAP_ATTRFORK;
4409 ifp = XFS_IFORK_PTR(ip, whichfork);
4410 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4411 for (idx = 0; idx < cnt; idx++)
4412 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4416 * Validate that the bmbt_irecs being returned from bmapi are valid
4417 * given the callers original parameters. Specifically check the
4418 * ranges of the returned irecs to ensure that they only extent beyond
4419 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4422 xfs_bmap_validate_ret(
4426 xfs_bmbt_irec_t *mval,
4430 int i; /* index to map values */
4432 ASSERT(ret_nmap <= nmap);
4434 for (i = 0; i < ret_nmap; i++) {
4435 ASSERT(mval[i].br_blockcount > 0);
4436 if (!(flags & XFS_BMAPI_ENTIRE)) {
4437 ASSERT(mval[i].br_startoff >= bno);
4438 ASSERT(mval[i].br_blockcount <= len);
4439 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4442 ASSERT(mval[i].br_startoff < bno + len);
4443 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4447 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4448 mval[i].br_startoff);
4449 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4450 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4451 mval[i].br_startblock != HOLESTARTBLOCK);
4452 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4453 mval[i].br_state == XFS_EXT_UNWRITTEN);
4460 * Map file blocks to filesystem blocks.
4461 * File range is given by the bno/len pair.
4462 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4463 * into a hole or past eof.
4464 * Only allocates blocks from a single allocation group,
4465 * to avoid locking problems.
4466 * The returned value in "firstblock" from the first call in a transaction
4467 * must be remembered and presented to subsequent calls in "firstblock".
4468 * An upper bound for the number of blocks to be allocated is supplied to
4469 * the first call in "total"; if no allocation group has that many free
4470 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4474 xfs_trans_t *tp, /* transaction pointer */
4475 xfs_inode_t *ip, /* incore inode */
4476 xfs_fileoff_t bno, /* starting file offs. mapped */
4477 xfs_filblks_t len, /* length to map in file */
4478 int flags, /* XFS_BMAPI_... */
4479 xfs_fsblock_t *firstblock, /* first allocated block
4480 controls a.g. for allocs */
4481 xfs_extlen_t total, /* total blocks needed */
4482 xfs_bmbt_irec_t *mval, /* output: map values */
4483 int *nmap, /* i/o: mval size/count */
4484 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4485 xfs_extdelta_t *delta) /* o: change made to incore extents */
4487 xfs_fsblock_t abno; /* allocated block number */
4488 xfs_extlen_t alen; /* allocated extent length */
4489 xfs_fileoff_t aoff; /* allocated file offset */
4490 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
4491 xfs_btree_cur_t *cur; /* bmap btree cursor */
4492 xfs_fileoff_t end; /* end of mapped file region */
4493 int eof; /* we've hit the end of extents */
4494 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4495 int error; /* error return */
4496 xfs_bmbt_irec_t got; /* current file extent record */
4497 xfs_ifork_t *ifp; /* inode fork pointer */
4498 xfs_extlen_t indlen; /* indirect blocks length */
4499 xfs_extnum_t lastx; /* last useful extent number */
4500 int logflags; /* flags for transaction logging */
4501 xfs_extlen_t minleft; /* min blocks left after allocation */
4502 xfs_extlen_t minlen; /* min allocation size */
4503 xfs_mount_t *mp; /* xfs mount structure */
4504 int n; /* current extent index */
4505 int nallocs; /* number of extents alloc'd */
4506 xfs_extnum_t nextents; /* number of extents in file */
4507 xfs_fileoff_t obno; /* old block number (offset) */
4508 xfs_bmbt_irec_t prev; /* previous file extent record */
4509 int tmp_logflags; /* temp flags holder */
4510 int whichfork; /* data or attr fork */
4511 char inhole; /* current location is hole in file */
4512 char wasdelay; /* old extent was delayed */
4513 char wr; /* this is a write request */
4514 char rt; /* this is a realtime file */
4516 xfs_fileoff_t orig_bno; /* original block number value */
4517 int orig_flags; /* original flags arg value */
4518 xfs_filblks_t orig_len; /* original value of len arg */
4519 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4520 int orig_nmap; /* original value of *nmap */
4529 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4530 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4531 XFS_ATTR_FORK : XFS_DATA_FORK;
4533 if (unlikely(XFS_TEST_ERROR(
4534 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4535 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4536 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4537 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4538 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4539 return XFS_ERROR(EFSCORRUPTED);
4541 if (XFS_FORCED_SHUTDOWN(mp))
4542 return XFS_ERROR(EIO);
4543 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4544 ifp = XFS_IFORK_PTR(ip, whichfork);
4545 ASSERT(ifp->if_ext_max ==
4546 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4547 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4548 XFS_STATS_INC(xs_blk_mapw);
4550 XFS_STATS_INC(xs_blk_mapr);
4552 * IGSTATE flag is used to combine extents which
4553 * differ only due to the state of the extents.
4554 * This technique is used from xfs_getbmap()
4555 * when the caller does not wish to see the
4556 * separation (which is the default).
4558 * This technique is also used when writing a
4559 * buffer which has been partially written,
4560 * (usually by being flushed during a chunkread),
4561 * to ensure one write takes place. This also
4562 * prevents a change in the xfs inode extents at
4563 * this time, intentionally. This change occurs
4564 * on completion of the write operation, in
4565 * xfs_strat_comp(), where the xfs_bmapi() call
4566 * is transactioned, and the extents combined.
4568 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4569 wr = 0; /* no allocations are allowed */
4570 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4574 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4576 if ((error = xfs_bmap_local_to_extents(tp, ip,
4577 firstblock, total, &logflags, whichfork)))
4580 if (wr && *firstblock == NULLFSBLOCK) {
4581 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4582 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4587 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4588 (error = xfs_iread_extents(tp, ip, whichfork)))
4590 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4592 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4598 delta->xed_startoff = NULLFILEOFF;
4599 delta->xed_blockcount = 0;
4601 while (bno < end && n < *nmap) {
4603 * Reading past eof, act as though there's a hole
4607 got.br_startoff = end;
4608 inhole = eof || got.br_startoff > bno;
4609 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4610 isnullstartblock(got.br_startblock);
4612 * First, deal with the hole before the allocated space
4613 * that we found, if any.
4615 if (wr && (inhole || wasdelay)) {
4617 * For the wasdelay case, we could also just
4618 * allocate the stuff asked for in this bmap call
4619 * but that wouldn't be as good.
4621 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4622 alen = (xfs_extlen_t)got.br_blockcount;
4623 aoff = got.br_startoff;
4624 if (lastx != NULLEXTNUM && lastx) {
4625 ep = xfs_iext_get_ext(ifp, lastx - 1);
4626 xfs_bmbt_get_all(ep, &prev);
4628 } else if (wasdelay) {
4629 alen = (xfs_extlen_t)
4630 XFS_FILBLKS_MIN(len,
4632 got.br_blockcount) - bno);
4635 alen = (xfs_extlen_t)
4636 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4638 alen = (xfs_extlen_t)
4639 XFS_FILBLKS_MIN(alen,
4640 got.br_startoff - bno);
4643 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4644 if (flags & XFS_BMAPI_DELAY) {
4647 /* Figure out the extent size, adjust alen */
4648 extsz = xfs_get_extsz_hint(ip);
4650 error = xfs_bmap_extsize_align(mp,
4653 flags&XFS_BMAPI_DELAY,
4654 flags&XFS_BMAPI_CONVERT,
4660 extsz = alen / mp->m_sb.sb_rextsize;
4663 * Make a transaction-less quota reservation for
4664 * delayed allocation blocks. This number gets
4665 * adjusted later. We return if we haven't
4666 * allocated blocks already inside this loop.
4668 error = xfs_trans_reserve_quota_nblks(
4669 NULL, ip, (long)alen, 0,
4670 rt ? XFS_QMOPT_RES_RTBLKS :
4671 XFS_QMOPT_RES_REGBLKS);
4675 ASSERT(cur == NULL);
4682 * Split changing sb for alen and indlen since
4683 * they could be coming from different places.
4685 indlen = (xfs_extlen_t)
4686 xfs_bmap_worst_indlen(ip, alen);
4690 error = xfs_mod_incore_sb(mp,
4692 -((int64_t)extsz), (flags &
4693 XFS_BMAPI_RSVBLOCKS));
4695 error = xfs_mod_incore_sb(mp,
4697 -((int64_t)alen), (flags &
4698 XFS_BMAPI_RSVBLOCKS));
4701 error = xfs_mod_incore_sb(mp,
4703 -((int64_t)indlen), (flags &
4704 XFS_BMAPI_RSVBLOCKS));
4706 xfs_mod_incore_sb(mp,
4708 (int64_t)extsz, (flags &
4709 XFS_BMAPI_RSVBLOCKS));
4711 xfs_mod_incore_sb(mp,
4713 (int64_t)alen, (flags &
4714 XFS_BMAPI_RSVBLOCKS));
4718 if (XFS_IS_QUOTA_ON(mp))
4719 /* unreserve the blocks now */
4721 xfs_trans_unreserve_quota_nblks(
4724 XFS_QMOPT_RES_RTBLKS :
4725 XFS_QMOPT_RES_REGBLKS);
4729 ip->i_delayed_blks += alen;
4730 abno = nullstartblock(indlen);
4733 * If first time, allocate and fill in
4734 * once-only bma fields.
4736 if (bma.ip == NULL) {
4744 /* Indicate if this is the first user data
4745 * in the file, or just any user data.
4747 if (!(flags & XFS_BMAPI_METADATA)) {
4748 bma.userdata = (aoff == 0) ?
4749 XFS_ALLOC_INITIAL_USER_DATA :
4753 * Fill in changeable bma fields.
4756 bma.firstblock = *firstblock;
4759 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4760 bma.wasdel = wasdelay;
4761 bma.minlen = minlen;
4762 bma.low = flist->xbf_low;
4763 bma.minleft = minleft;
4765 * Only want to do the alignment at the
4766 * eof if it is userdata and allocation length
4767 * is larger than a stripe unit.
4769 if (mp->m_dalign && alen >= mp->m_dalign &&
4770 (!(flags & XFS_BMAPI_METADATA)) &&
4771 (whichfork == XFS_DATA_FORK)) {
4772 if ((error = xfs_bmap_isaeof(ip, aoff,
4773 whichfork, &bma.aeof)))
4780 if ((error = xfs_bmap_alloc(&bma)))
4783 * Copy out result fields.
4786 if ((flist->xbf_low = bma.low))
4790 ASSERT(*firstblock == NULLFSBLOCK ||
4791 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4792 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4794 XFS_FSB_TO_AGNO(mp, *firstblock) <
4795 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4796 *firstblock = bma.firstblock;
4798 cur->bc_private.b.firstblock =
4800 if (abno == NULLFSBLOCK)
4802 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4803 cur = xfs_bmbt_init_cursor(mp, tp,
4805 cur->bc_private.b.firstblock =
4807 cur->bc_private.b.flist = flist;
4810 * Bump the number of extents we've allocated
4816 cur->bc_private.b.flags =
4817 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4818 got.br_startoff = aoff;
4819 got.br_startblock = abno;
4820 got.br_blockcount = alen;
4821 got.br_state = XFS_EXT_NORM; /* assume normal */
4823 * Determine state of extent, and the filesystem.
4824 * A wasdelay extent has been initialized, so
4825 * shouldn't be flagged as unwritten.
4827 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4828 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4829 got.br_state = XFS_EXT_UNWRITTEN;
4831 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4832 firstblock, flist, &tmp_logflags, delta,
4833 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4834 logflags |= tmp_logflags;
4837 lastx = ifp->if_lastex;
4838 ep = xfs_iext_get_ext(ifp, lastx);
4839 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4840 xfs_bmbt_get_all(ep, &got);
4841 ASSERT(got.br_startoff <= aoff);
4842 ASSERT(got.br_startoff + got.br_blockcount >=
4845 if (flags & XFS_BMAPI_DELAY) {
4846 ASSERT(isnullstartblock(got.br_startblock));
4847 ASSERT(startblockval(got.br_startblock) > 0);
4849 ASSERT(got.br_state == XFS_EXT_NORM ||
4850 got.br_state == XFS_EXT_UNWRITTEN);
4853 * Fall down into the found allocated space case.
4855 } else if (inhole) {
4857 * Reading in a hole.
4859 mval->br_startoff = bno;
4860 mval->br_startblock = HOLESTARTBLOCK;
4861 mval->br_blockcount =
4862 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4863 mval->br_state = XFS_EXT_NORM;
4864 bno += mval->br_blockcount;
4865 len -= mval->br_blockcount;
4871 * Then deal with the allocated space we found.
4874 if (!(flags & XFS_BMAPI_ENTIRE) &&
4875 (got.br_startoff + got.br_blockcount > obno)) {
4878 ASSERT((bno >= obno) || (n == 0));
4880 mval->br_startoff = bno;
4881 if (isnullstartblock(got.br_startblock)) {
4882 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4883 mval->br_startblock = DELAYSTARTBLOCK;
4885 mval->br_startblock =
4887 (bno - got.br_startoff);
4889 * Return the minimum of what we got and what we
4890 * asked for for the length. We can use the len
4891 * variable here because it is modified below
4892 * and we could have been there before coming
4893 * here if the first part of the allocation
4894 * didn't overlap what was asked for.
4896 mval->br_blockcount =
4897 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4898 (bno - got.br_startoff));
4899 mval->br_state = got.br_state;
4900 ASSERT(mval->br_blockcount <= len);
4903 if (isnullstartblock(mval->br_startblock)) {
4904 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4905 mval->br_startblock = DELAYSTARTBLOCK;
4910 * Check if writing previously allocated but
4911 * unwritten extents.
4913 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4914 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4916 * Modify (by adding) the state flag, if writing.
4918 ASSERT(mval->br_blockcount <= len);
4919 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4920 cur = xfs_bmbt_init_cursor(mp,
4922 cur->bc_private.b.firstblock =
4924 cur->bc_private.b.flist = flist;
4926 mval->br_state = XFS_EXT_NORM;
4927 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4928 firstblock, flist, &tmp_logflags, delta,
4929 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4930 logflags |= tmp_logflags;
4933 lastx = ifp->if_lastex;
4934 ep = xfs_iext_get_ext(ifp, lastx);
4935 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4936 xfs_bmbt_get_all(ep, &got);
4938 * We may have combined previously unwritten
4939 * space with written space, so generate
4942 if (mval->br_blockcount < len)
4946 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4947 ((mval->br_startoff + mval->br_blockcount) <= end));
4948 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4949 (mval->br_blockcount <= len) ||
4950 (mval->br_startoff < obno));
4951 bno = mval->br_startoff + mval->br_blockcount;
4953 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4954 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4955 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4956 ASSERT(mval->br_state == mval[-1].br_state);
4957 mval[-1].br_blockcount = mval->br_blockcount;
4958 mval[-1].br_state = mval->br_state;
4959 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4960 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4961 mval[-1].br_startblock != HOLESTARTBLOCK &&
4962 mval->br_startblock ==
4963 mval[-1].br_startblock + mval[-1].br_blockcount &&
4964 ((flags & XFS_BMAPI_IGSTATE) ||
4965 mval[-1].br_state == mval->br_state)) {
4966 ASSERT(mval->br_startoff ==
4967 mval[-1].br_startoff + mval[-1].br_blockcount);
4968 mval[-1].br_blockcount += mval->br_blockcount;
4970 mval->br_startblock == DELAYSTARTBLOCK &&
4971 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4972 mval->br_startoff ==
4973 mval[-1].br_startoff + mval[-1].br_blockcount) {
4974 mval[-1].br_blockcount += mval->br_blockcount;
4975 mval[-1].br_state = mval->br_state;
4976 } else if (!((n == 0) &&
4977 ((mval->br_startoff + mval->br_blockcount) <=
4983 * If we're done, stop now. Stop when we've allocated
4984 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4985 * the transaction may get too big.
4987 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4990 * Else go on to the next record.
4992 ep = xfs_iext_get_ext(ifp, ++lastx);
4994 if (lastx >= nextents)
4997 xfs_bmbt_get_all(ep, &got);
4999 ifp->if_lastex = lastx;
5002 * Transform from btree to extents, give it cur.
5004 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5005 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5007 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5008 &tmp_logflags, whichfork);
5009 logflags |= tmp_logflags;
5013 ASSERT(ifp->if_ext_max ==
5014 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5015 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5016 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5018 if (delta && delta->xed_startoff != NULLFILEOFF) {
5019 /* A change was actually made.
5020 * Note that delta->xed_blockount is an offset at this
5021 * point and needs to be converted to a block count.
5023 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5024 delta->xed_blockcount -= delta->xed_startoff;
5028 * Log everything. Do this after conversion, there's no point in
5029 * logging the extent records if we've converted to btree format.
5031 if ((logflags & xfs_ilog_fext(whichfork)) &&
5032 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5033 logflags &= ~xfs_ilog_fext(whichfork);
5034 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5035 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5036 logflags &= ~xfs_ilog_fbroot(whichfork);
5038 * Log whatever the flags say, even if error. Otherwise we might miss
5039 * detecting a case where the data is changed, there's an error,
5040 * and it's not logged so we don't shutdown when we should.
5044 xfs_trans_log_inode(tp, ip, logflags);
5048 ASSERT(*firstblock == NULLFSBLOCK ||
5049 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5051 cur->bc_private.b.firstblock) ||
5053 XFS_FSB_TO_AGNO(mp, *firstblock) <
5055 cur->bc_private.b.firstblock)));
5056 *firstblock = cur->bc_private.b.firstblock;
5058 xfs_btree_del_cursor(cur,
5059 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5062 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5068 * Map file blocks to filesystem blocks, simple version.
5069 * One block (extent) only, read-only.
5070 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5071 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5072 * was set and all the others were clear.
5076 xfs_trans_t *tp, /* transaction pointer */
5077 xfs_inode_t *ip, /* incore inode */
5078 int whichfork, /* data or attr fork */
5079 xfs_fsblock_t *fsb, /* output: mapped block */
5080 xfs_fileoff_t bno) /* starting file offs. mapped */
5082 int eof; /* we've hit the end of extents */
5083 int error; /* error return */
5084 xfs_bmbt_irec_t got; /* current file extent record */
5085 xfs_ifork_t *ifp; /* inode fork pointer */
5086 xfs_extnum_t lastx; /* last useful extent number */
5087 xfs_bmbt_irec_t prev; /* previous file extent record */
5089 ifp = XFS_IFORK_PTR(ip, whichfork);
5091 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5092 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5093 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5095 return XFS_ERROR(EFSCORRUPTED);
5097 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5098 return XFS_ERROR(EIO);
5099 XFS_STATS_INC(xs_blk_mapr);
5100 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5101 (error = xfs_iread_extents(tp, ip, whichfork)))
5103 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5106 * Reading past eof, act as though there's a hole
5109 if (eof || got.br_startoff > bno) {
5113 ASSERT(!isnullstartblock(got.br_startblock));
5114 ASSERT(bno < got.br_startoff + got.br_blockcount);
5115 *fsb = got.br_startblock + (bno - got.br_startoff);
5116 ifp->if_lastex = lastx;
5121 * Unmap (remove) blocks from a file.
5122 * If nexts is nonzero then the number of extents to remove is limited to
5123 * that value. If not all extents in the block range can be removed then
5128 xfs_trans_t *tp, /* transaction pointer */
5129 struct xfs_inode *ip, /* incore inode */
5130 xfs_fileoff_t bno, /* starting offset to unmap */
5131 xfs_filblks_t len, /* length to unmap in file */
5132 int flags, /* misc flags */
5133 xfs_extnum_t nexts, /* number of extents max */
5134 xfs_fsblock_t *firstblock, /* first allocated block
5135 controls a.g. for allocs */
5136 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5137 xfs_extdelta_t *delta, /* o: change made to incore
5139 int *done) /* set if not done yet */
5141 xfs_btree_cur_t *cur; /* bmap btree cursor */
5142 xfs_bmbt_irec_t del; /* extent being deleted */
5143 int eof; /* is deleting at eof */
5144 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5145 int error; /* error return value */
5146 xfs_extnum_t extno; /* extent number in list */
5147 xfs_bmbt_irec_t got; /* current extent record */
5148 xfs_ifork_t *ifp; /* inode fork pointer */
5149 int isrt; /* freeing in rt area */
5150 xfs_extnum_t lastx; /* last extent index used */
5151 int logflags; /* transaction logging flags */
5152 xfs_extlen_t mod; /* rt extent offset */
5153 xfs_mount_t *mp; /* mount structure */
5154 xfs_extnum_t nextents; /* number of file extents */
5155 xfs_bmbt_irec_t prev; /* previous extent record */
5156 xfs_fileoff_t start; /* first file offset deleted */
5157 int tmp_logflags; /* partial logging flags */
5158 int wasdel; /* was a delayed alloc extent */
5159 int whichfork; /* data or attribute fork */
5160 int rsvd; /* OK to allocate reserved blocks */
5163 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5165 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5166 XFS_ATTR_FORK : XFS_DATA_FORK;
5167 ifp = XFS_IFORK_PTR(ip, whichfork);
5169 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5170 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5171 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5173 return XFS_ERROR(EFSCORRUPTED);
5176 if (XFS_FORCED_SHUTDOWN(mp))
5177 return XFS_ERROR(EIO);
5178 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5181 ASSERT(ifp->if_ext_max ==
5182 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5183 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5184 (error = xfs_iread_extents(tp, ip, whichfork)))
5186 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5187 if (nextents == 0) {
5191 XFS_STATS_INC(xs_blk_unmap);
5192 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5194 bno = start + len - 1;
5195 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5198 delta->xed_startoff = NULLFILEOFF;
5199 delta->xed_blockcount = 0;
5202 * Check to see if the given block number is past the end of the
5203 * file, back up to the last block if so...
5206 ep = xfs_iext_get_ext(ifp, --lastx);
5207 xfs_bmbt_get_all(ep, &got);
5208 bno = got.br_startoff + got.br_blockcount - 1;
5211 if (ifp->if_flags & XFS_IFBROOT) {
5212 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5213 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5214 cur->bc_private.b.firstblock = *firstblock;
5215 cur->bc_private.b.flist = flist;
5216 cur->bc_private.b.flags = 0;
5220 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5221 (nexts == 0 || extno < nexts)) {
5223 * Is the found extent after a hole in which bno lives?
5224 * Just back up to the previous extent, if so.
5226 if (got.br_startoff > bno) {
5229 ep = xfs_iext_get_ext(ifp, lastx);
5230 xfs_bmbt_get_all(ep, &got);
5233 * Is the last block of this extent before the range
5234 * we're supposed to delete? If so, we're done.
5236 bno = XFS_FILEOFF_MIN(bno,
5237 got.br_startoff + got.br_blockcount - 1);
5241 * Then deal with the (possibly delayed) allocated space
5246 wasdel = isnullstartblock(del.br_startblock);
5247 if (got.br_startoff < start) {
5248 del.br_startoff = start;
5249 del.br_blockcount -= start - got.br_startoff;
5251 del.br_startblock += start - got.br_startoff;
5253 if (del.br_startoff + del.br_blockcount > bno + 1)
5254 del.br_blockcount = bno + 1 - del.br_startoff;
5255 sum = del.br_startblock + del.br_blockcount;
5257 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5259 * Realtime extent not lined up at the end.
5260 * The extent could have been split into written
5261 * and unwritten pieces, or we could just be
5262 * unmapping part of it. But we can't really
5263 * get rid of part of a realtime extent.
5265 if (del.br_state == XFS_EXT_UNWRITTEN ||
5266 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5268 * This piece is unwritten, or we're not
5269 * using unwritten extents. Skip over it.
5272 bno -= mod > del.br_blockcount ?
5273 del.br_blockcount : mod;
5274 if (bno < got.br_startoff) {
5276 xfs_bmbt_get_all(xfs_iext_get_ext(
5282 * It's written, turn it unwritten.
5283 * This is better than zeroing it.
5285 ASSERT(del.br_state == XFS_EXT_NORM);
5286 ASSERT(xfs_trans_get_block_res(tp) > 0);
5288 * If this spans a realtime extent boundary,
5289 * chop it back to the start of the one we end at.
5291 if (del.br_blockcount > mod) {
5292 del.br_startoff += del.br_blockcount - mod;
5293 del.br_startblock += del.br_blockcount - mod;
5294 del.br_blockcount = mod;
5296 del.br_state = XFS_EXT_UNWRITTEN;
5297 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5298 firstblock, flist, &logflags, delta,
5304 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5306 * Realtime extent is lined up at the end but not
5307 * at the front. We'll get rid of full extents if
5310 mod = mp->m_sb.sb_rextsize - mod;
5311 if (del.br_blockcount > mod) {
5312 del.br_blockcount -= mod;
5313 del.br_startoff += mod;
5314 del.br_startblock += mod;
5315 } else if ((del.br_startoff == start &&
5316 (del.br_state == XFS_EXT_UNWRITTEN ||
5317 xfs_trans_get_block_res(tp) == 0)) ||
5318 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5320 * Can't make it unwritten. There isn't
5321 * a full extent here so just skip it.
5323 ASSERT(bno >= del.br_blockcount);
5324 bno -= del.br_blockcount;
5325 if (bno < got.br_startoff) {
5327 xfs_bmbt_get_all(--ep, &got);
5330 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5332 * This one is already unwritten.
5333 * It must have a written left neighbor.
5334 * Unwrite the killed part of that one and
5338 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5340 ASSERT(prev.br_state == XFS_EXT_NORM);
5341 ASSERT(!isnullstartblock(prev.br_startblock));
5342 ASSERT(del.br_startblock ==
5343 prev.br_startblock + prev.br_blockcount);
5344 if (prev.br_startoff < start) {
5345 mod = start - prev.br_startoff;
5346 prev.br_blockcount -= mod;
5347 prev.br_startblock += mod;
5348 prev.br_startoff = start;
5350 prev.br_state = XFS_EXT_UNWRITTEN;
5351 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5352 &prev, firstblock, flist, &logflags,
5353 delta, XFS_DATA_FORK, 0);
5358 ASSERT(del.br_state == XFS_EXT_NORM);
5359 del.br_state = XFS_EXT_UNWRITTEN;
5360 error = xfs_bmap_add_extent(ip, lastx, &cur,
5361 &del, firstblock, flist, &logflags,
5362 delta, XFS_DATA_FORK, 0);
5369 ASSERT(startblockval(del.br_startblock) > 0);
5370 /* Update realtime/data freespace, unreserve quota */
5372 xfs_filblks_t rtexts;
5374 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5375 do_div(rtexts, mp->m_sb.sb_rextsize);
5376 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5377 (int64_t)rtexts, rsvd);
5378 (void)xfs_trans_reserve_quota_nblks(NULL,
5379 ip, -((long)del.br_blockcount), 0,
5380 XFS_QMOPT_RES_RTBLKS);
5382 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5383 (int64_t)del.br_blockcount, rsvd);
5384 (void)xfs_trans_reserve_quota_nblks(NULL,
5385 ip, -((long)del.br_blockcount), 0,
5386 XFS_QMOPT_RES_REGBLKS);
5388 ip->i_delayed_blks -= del.br_blockcount;
5390 cur->bc_private.b.flags |=
5391 XFS_BTCUR_BPRV_WASDEL;
5393 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5395 * If it's the case where the directory code is running
5396 * with no block reservation, and the deleted block is in
5397 * the middle of its extent, and the resulting insert
5398 * of an extent would cause transformation to btree format,
5399 * then reject it. The calling code will then swap
5400 * blocks around instead.
5401 * We have to do this now, rather than waiting for the
5402 * conversion to btree format, since the transaction
5405 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5406 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5407 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5408 del.br_startoff > got.br_startoff &&
5409 del.br_startoff + del.br_blockcount <
5410 got.br_startoff + got.br_blockcount) {
5411 error = XFS_ERROR(ENOSPC);
5414 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5415 &tmp_logflags, delta, whichfork, rsvd);
5416 logflags |= tmp_logflags;
5419 bno = del.br_startoff - 1;
5421 lastx = ifp->if_lastex;
5423 * If not done go on to the next (previous) record.
5424 * Reset ep in case the extents array was re-alloced.
5426 ep = xfs_iext_get_ext(ifp, lastx);
5427 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5428 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5429 xfs_bmbt_get_startoff(ep) > bno) {
5431 ep = xfs_iext_get_ext(ifp, lastx);
5434 xfs_bmbt_get_all(ep, &got);
5438 ifp->if_lastex = lastx;
5439 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5440 ASSERT(ifp->if_ext_max ==
5441 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5443 * Convert to a btree if necessary.
5445 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5446 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5447 ASSERT(cur == NULL);
5448 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5449 &cur, 0, &tmp_logflags, whichfork);
5450 logflags |= tmp_logflags;
5455 * transform from btree to extents, give it cur
5457 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5458 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5459 ASSERT(cur != NULL);
5460 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5462 logflags |= tmp_logflags;
5467 * transform from extents to local?
5469 ASSERT(ifp->if_ext_max ==
5470 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5472 if (delta && delta->xed_startoff != NULLFILEOFF) {
5473 /* A change was actually made.
5474 * Note that delta->xed_blockount is an offset at this
5475 * point and needs to be converted to a block count.
5477 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5478 delta->xed_blockcount -= delta->xed_startoff;
5482 * Log everything. Do this after conversion, there's no point in
5483 * logging the extent records if we've converted to btree format.
5485 if ((logflags & xfs_ilog_fext(whichfork)) &&
5486 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5487 logflags &= ~xfs_ilog_fext(whichfork);
5488 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5489 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5490 logflags &= ~xfs_ilog_fbroot(whichfork);
5492 * Log inode even in the error case, if the transaction
5493 * is dirty we'll need to shut down the filesystem.
5496 xfs_trans_log_inode(tp, ip, logflags);
5499 *firstblock = cur->bc_private.b.firstblock;
5500 cur->bc_private.b.allocated = 0;
5502 xfs_btree_del_cursor(cur,
5503 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5509 * returns 1 for success, 0 if we failed to map the extent.
5512 xfs_getbmapx_fix_eof_hole(
5513 xfs_inode_t *ip, /* xfs incore inode pointer */
5514 struct getbmapx *out, /* output structure */
5515 int prealloced, /* this is a file with
5516 * preallocated data space */
5517 __int64_t end, /* last block requested */
5518 xfs_fsblock_t startblock)
5521 xfs_mount_t *mp; /* file system mount point */
5522 xfs_ifork_t *ifp; /* inode fork pointer */
5523 xfs_extnum_t lastx; /* last extent pointer */
5524 xfs_fileoff_t fileblock;
5526 if (startblock == HOLESTARTBLOCK) {
5528 out->bmv_block = -1;
5529 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5530 fixlen -= out->bmv_offset;
5531 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5532 /* Came to hole at EOF. Trim it. */
5535 out->bmv_length = fixlen;
5538 if (startblock == DELAYSTARTBLOCK)
5539 out->bmv_block = -2;
5541 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5542 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5543 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5544 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5545 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5546 out->bmv_oflags |= BMV_OF_LAST;
5553 * Get inode's extents as described in bmv, and format for output.
5554 * Calls formatter to fill the user's buffer until all extents
5555 * are mapped, until the passed-in bmv->bmv_count slots have
5556 * been filled, or until the formatter short-circuits the loop,
5557 * if it is tracking filled-in extents on its own.
5559 int /* error code */
5562 struct getbmapx *bmv, /* user bmap structure */
5563 xfs_bmap_format_t formatter, /* format to user */
5564 void *arg) /* formatter arg */
5566 __int64_t bmvend; /* last block requested */
5567 int error = 0; /* return value */
5568 __int64_t fixlen; /* length for -1 case */
5569 int i; /* extent number */
5570 int lock; /* lock state */
5571 xfs_bmbt_irec_t *map; /* buffer for user's data */
5572 xfs_mount_t *mp; /* file system mount point */
5573 int nex; /* # of user extents can do */
5574 int nexleft; /* # of user extents left */
5575 int subnex; /* # of bmapi's can do */
5576 int nmap; /* number of map entries */
5577 struct getbmapx *out; /* output structure */
5578 int whichfork; /* data or attr fork */
5579 int prealloced; /* this is a file with
5580 * preallocated data space */
5581 int iflags; /* interface flags */
5582 int bmapi_flags; /* flags for xfs_bmapi */
5586 iflags = bmv->bmv_iflags;
5587 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5589 if (whichfork == XFS_ATTR_FORK) {
5590 if (XFS_IFORK_Q(ip)) {
5591 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5592 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5593 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5594 return XFS_ERROR(EINVAL);
5595 } else if (unlikely(
5596 ip->i_d.di_aformat != 0 &&
5597 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5598 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5600 return XFS_ERROR(EFSCORRUPTED);
5606 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5607 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5608 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5609 return XFS_ERROR(EINVAL);
5611 if (xfs_get_extsz_hint(ip) ||
5612 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5614 fixlen = XFS_MAXIOFFSET(mp);
5617 fixlen = ip->i_size;
5621 if (bmv->bmv_length == -1) {
5622 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5624 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5625 } else if (bmv->bmv_length == 0) {
5626 bmv->bmv_entries = 0;
5628 } else if (bmv->bmv_length < 0) {
5629 return XFS_ERROR(EINVAL);
5632 nex = bmv->bmv_count - 1;
5634 return XFS_ERROR(EINVAL);
5635 bmvend = bmv->bmv_offset + bmv->bmv_length;
5638 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5639 return XFS_ERROR(ENOMEM);
5640 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5642 return XFS_ERROR(ENOMEM);
5644 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5645 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5646 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5647 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5649 goto out_unlock_iolock;
5652 ASSERT(ip->i_delayed_blks == 0);
5655 lock = xfs_ilock_map_shared(ip);
5658 * Don't let nex be bigger than the number of extents
5659 * we can have assuming alternating holes and real extents.
5661 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5662 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5664 bmapi_flags = xfs_bmapi_aflag(whichfork);
5665 if (!(iflags & BMV_IF_PREALLOC))
5666 bmapi_flags |= XFS_BMAPI_IGSTATE;
5669 * Allocate enough space to handle "subnex" maps at a time.
5673 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5675 goto out_unlock_ilock;
5677 bmv->bmv_entries = 0;
5679 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5680 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5688 nmap = (nexleft > subnex) ? subnex : nexleft;
5689 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5690 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5691 bmapi_flags, NULL, 0, map, &nmap,
5695 ASSERT(nmap <= subnex);
5697 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5698 out[cur_ext].bmv_oflags = 0;
5699 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5700 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5701 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5702 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5703 out[cur_ext].bmv_offset =
5704 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5705 out[cur_ext].bmv_length =
5706 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5707 out[cur_ext].bmv_unused1 = 0;
5708 out[cur_ext].bmv_unused2 = 0;
5709 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5710 (map[i].br_startblock != DELAYSTARTBLOCK));
5711 if (map[i].br_startblock == HOLESTARTBLOCK &&
5712 whichfork == XFS_ATTR_FORK) {
5713 /* came to the end of attribute fork */
5714 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5718 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5720 map[i].br_startblock))
5725 out[cur_ext].bmv_offset +
5726 out[cur_ext].bmv_length;
5728 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5732 } while (nmap && nexleft && bmv->bmv_length);
5737 xfs_iunlock_map_shared(ip, lock);
5739 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5741 for (i = 0; i < cur_ext; i++) {
5742 int full = 0; /* user array is full */
5744 /* format results & advance arg */
5745 error = formatter(&arg, &out[i], &full);
5755 * Check the last inode extent to determine whether this allocation will result
5756 * in blocks being allocated at the end of the file. When we allocate new data
5757 * blocks at the end of the file which do not start at the previous data block,
5758 * we will try to align the new blocks at stripe unit boundaries.
5760 STATIC int /* error */
5762 xfs_inode_t *ip, /* incore inode pointer */
5763 xfs_fileoff_t off, /* file offset in fsblocks */
5764 int whichfork, /* data or attribute fork */
5765 char *aeof) /* return value */
5767 int error; /* error return value */
5768 xfs_ifork_t *ifp; /* inode fork pointer */
5769 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5770 xfs_extnum_t nextents; /* number of file extents */
5771 xfs_bmbt_irec_t s; /* expanded extent record */
5773 ASSERT(whichfork == XFS_DATA_FORK);
5774 ifp = XFS_IFORK_PTR(ip, whichfork);
5775 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5776 (error = xfs_iread_extents(NULL, ip, whichfork)))
5778 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5779 if (nextents == 0) {
5784 * Go to the last extent
5786 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5787 xfs_bmbt_get_all(lastrec, &s);
5789 * Check we are allocating in the last extent (for delayed allocations)
5790 * or past the last extent for non-delayed allocations.
5792 *aeof = (off >= s.br_startoff &&
5793 off < s.br_startoff + s.br_blockcount &&
5794 isnullstartblock(s.br_startblock)) ||
5795 off >= s.br_startoff + s.br_blockcount;
5800 * Check if the endoff is outside the last extent. If so the caller will grow
5801 * the allocation to a stripe unit boundary.
5805 xfs_inode_t *ip, /* incore inode pointer */
5806 xfs_fileoff_t endoff, /* file offset in fsblocks */
5807 int whichfork, /* data or attribute fork */
5808 int *eof) /* result value */
5810 xfs_fsblock_t blockcount; /* extent block count */
5811 int error; /* error return value */
5812 xfs_ifork_t *ifp; /* inode fork pointer */
5813 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5814 xfs_extnum_t nextents; /* number of file extents */
5815 xfs_fileoff_t startoff; /* extent starting file offset */
5817 ASSERT(whichfork == XFS_DATA_FORK);
5818 ifp = XFS_IFORK_PTR(ip, whichfork);
5819 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5820 (error = xfs_iread_extents(NULL, ip, whichfork)))
5822 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5823 if (nextents == 0) {
5828 * Go to the last extent
5830 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5831 startoff = xfs_bmbt_get_startoff(lastrec);
5832 blockcount = xfs_bmbt_get_blockcount(lastrec);
5833 *eof = endoff >= startoff + blockcount;
5841 xfs_btree_cur_t *cur,
5851 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5852 bp = cur->bc_bufs[i];
5854 if (XFS_BUF_ADDR(bp) == bno)
5855 break; /* Found it */
5857 if (i == XFS_BTREE_MAXLEVELS)
5860 if (!bp) { /* Chase down all the log items to see if the bp is there */
5861 struct xfs_log_item_desc *lidp;
5862 struct xfs_buf_log_item *bip;
5864 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5865 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5866 if (bip->bli_item.li_type == XFS_LI_BUF &&
5867 XFS_BUF_ADDR(bip->bli_buf) == bno)
5868 return bip->bli_buf;
5877 struct xfs_btree_block *block,
5883 __be64 *pp, *thispa; /* pointer to block address */
5884 xfs_bmbt_key_t *prevp, *keyp;
5886 ASSERT(be16_to_cpu(block->bb_level) > 0);
5889 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5890 dmxr = mp->m_bmap_dmxr[0];
5891 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5894 ASSERT(be64_to_cpu(prevp->br_startoff) <
5895 be64_to_cpu(keyp->br_startoff));
5900 * Compare the block numbers to see if there are dups.
5903 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5905 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5907 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5909 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5911 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5912 if (*thispa == *pp) {
5913 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
5915 (unsigned long long)be64_to_cpu(*thispa));
5916 panic("%s: ptrs are equal in node\n",
5924 * Check that the extents for the inode ip are in the right order in all
5929 xfs_bmap_check_leaf_extents(
5930 xfs_btree_cur_t *cur, /* btree cursor or null */
5931 xfs_inode_t *ip, /* incore inode pointer */
5932 int whichfork) /* data or attr fork */
5934 struct xfs_btree_block *block; /* current btree block */
5935 xfs_fsblock_t bno; /* block # of "block" */
5936 xfs_buf_t *bp; /* buffer for "block" */
5937 int error; /* error return value */
5938 xfs_extnum_t i=0, j; /* index into the extents list */
5939 xfs_ifork_t *ifp; /* fork structure */
5940 int level; /* btree level, for checking */
5941 xfs_mount_t *mp; /* file system mount structure */
5942 __be64 *pp; /* pointer to block address */
5943 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5944 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5945 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5948 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5954 ifp = XFS_IFORK_PTR(ip, whichfork);
5955 block = ifp->if_broot;
5957 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5959 level = be16_to_cpu(block->bb_level);
5961 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5962 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5963 bno = be64_to_cpu(*pp);
5965 ASSERT(bno != NULLDFSBNO);
5966 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5967 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5970 * Go down the tree until leaf level is reached, following the first
5971 * pointer (leftmost) at each level.
5973 while (level-- > 0) {
5974 /* See if buf is in cur first */
5975 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5981 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5982 XFS_BMAP_BTREE_REF)))
5984 block = XFS_BUF_TO_BLOCK(bp);
5985 XFS_WANT_CORRUPTED_GOTO(
5986 xfs_bmap_sanity_check(mp, bp, level),
5992 * Check this block for basic sanity (increasing keys and
5993 * no duplicate blocks).
5996 xfs_check_block(block, mp, 0, 0);
5997 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5998 bno = be64_to_cpu(*pp);
5999 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6002 xfs_trans_brelse(NULL, bp);
6007 * Here with bp and block set to the leftmost leaf node in the tree.
6012 * Loop over all leaf nodes checking that all extents are in the right order.
6015 xfs_fsblock_t nextbno;
6016 xfs_extnum_t num_recs;
6019 num_recs = xfs_btree_get_numrecs(block);
6022 * Read-ahead the next leaf block, if any.
6025 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6028 * Check all the extents to make sure they are OK.
6029 * If we had a previous block, the last entry should
6030 * conform with the first entry in this one.
6033 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
6035 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6036 xfs_bmbt_disk_get_blockcount(&last) <=
6037 xfs_bmbt_disk_get_startoff(ep));
6039 for (j = 1; j < num_recs; j++) {
6040 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
6041 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6042 xfs_bmbt_disk_get_blockcount(ep) <=
6043 xfs_bmbt_disk_get_startoff(nextp));
6051 xfs_trans_brelse(NULL, bp);
6055 * If we've reached the end, stop.
6057 if (bno == NULLFSBLOCK)
6060 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6066 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6067 XFS_BMAP_BTREE_REF)))
6069 block = XFS_BUF_TO_BLOCK(bp);
6073 xfs_trans_brelse(NULL, bp);
6078 cmn_err(CE_WARN, "%s: at error0", __func__);
6080 xfs_trans_brelse(NULL, bp);
6082 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6084 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6090 * Count fsblocks of the given fork.
6093 xfs_bmap_count_blocks(
6094 xfs_trans_t *tp, /* transaction pointer */
6095 xfs_inode_t *ip, /* incore inode */
6096 int whichfork, /* data or attr fork */
6097 int *count) /* out: count of blocks */
6099 struct xfs_btree_block *block; /* current btree block */
6100 xfs_fsblock_t bno; /* block # of "block" */
6101 xfs_ifork_t *ifp; /* fork structure */
6102 int level; /* btree level, for checking */
6103 xfs_mount_t *mp; /* file system mount structure */
6104 __be64 *pp; /* pointer to block address */
6108 ifp = XFS_IFORK_PTR(ip, whichfork);
6109 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6110 xfs_bmap_count_leaves(ifp, 0,
6111 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6117 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6119 block = ifp->if_broot;
6120 level = be16_to_cpu(block->bb_level);
6122 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6123 bno = be64_to_cpu(*pp);
6124 ASSERT(bno != NULLDFSBNO);
6125 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6126 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6128 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6129 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6131 return XFS_ERROR(EFSCORRUPTED);
6138 * Recursively walks each level of a btree
6139 * to count total fsblocks is use.
6141 STATIC int /* error */
6142 xfs_bmap_count_tree(
6143 xfs_mount_t *mp, /* file system mount point */
6144 xfs_trans_t *tp, /* transaction pointer */
6145 xfs_ifork_t *ifp, /* inode fork pointer */
6146 xfs_fsblock_t blockno, /* file system block number */
6147 int levelin, /* level in btree */
6148 int *count) /* Count of blocks */
6151 xfs_buf_t *bp, *nbp;
6152 int level = levelin;
6154 xfs_fsblock_t bno = blockno;
6155 xfs_fsblock_t nextbno;
6156 struct xfs_btree_block *block, *nextblock;
6159 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6162 block = XFS_BUF_TO_BLOCK(bp);
6165 /* Not at node above leaves, count this level of nodes */
6166 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6167 while (nextbno != NULLFSBLOCK) {
6168 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6169 0, &nbp, XFS_BMAP_BTREE_REF)))
6172 nextblock = XFS_BUF_TO_BLOCK(nbp);
6173 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6174 xfs_trans_brelse(tp, nbp);
6177 /* Dive to the next level */
6178 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6179 bno = be64_to_cpu(*pp);
6180 if (unlikely((error =
6181 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6182 xfs_trans_brelse(tp, bp);
6183 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6184 XFS_ERRLEVEL_LOW, mp);
6185 return XFS_ERROR(EFSCORRUPTED);
6187 xfs_trans_brelse(tp, bp);
6189 /* count all level 1 nodes and their leaves */
6191 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6192 numrecs = be16_to_cpu(block->bb_numrecs);
6193 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6194 xfs_trans_brelse(tp, bp);
6195 if (nextbno == NULLFSBLOCK)
6198 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6199 XFS_BMAP_BTREE_REF)))
6202 block = XFS_BUF_TO_BLOCK(bp);
6209 * Count leaf blocks given a range of extent records.
6212 xfs_bmap_count_leaves(
6220 for (b = 0; b < numrecs; b++) {
6221 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6222 *count += xfs_bmbt_get_blockcount(frp);
6227 * Count leaf blocks given a range of extent records originally
6231 xfs_bmap_disk_count_leaves(
6232 struct xfs_mount *mp,
6233 struct xfs_btree_block *block,
6238 xfs_bmbt_rec_t *frp;
6240 for (b = 1; b <= numrecs; b++) {
6241 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6242 *count += xfs_bmbt_disk_get_blockcount(frp);