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 int whichfork); /* data or attr fork */
107 * Called by xfs_bmap_add_extent to handle cases converting a delayed
108 * allocation to a real allocation.
110 STATIC int /* error */
111 xfs_bmap_add_extent_delay_real(
112 xfs_inode_t *ip, /* incore inode pointer */
113 xfs_extnum_t idx, /* extent number to update/insert */
114 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
115 xfs_bmbt_irec_t *new, /* new data to add to file extents */
116 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
117 xfs_fsblock_t *first, /* pointer to firstblock variable */
118 xfs_bmap_free_t *flist, /* list of extents to be freed */
119 int *logflagsp); /* inode logging flags */
122 * Called by xfs_bmap_add_extent to handle cases converting a hole
123 * to a delayed allocation.
125 STATIC int /* error */
126 xfs_bmap_add_extent_hole_delay(
127 xfs_inode_t *ip, /* incore inode pointer */
128 xfs_extnum_t idx, /* extent number to update/insert */
129 xfs_bmbt_irec_t *new, /* new data to add to file extents */
130 int *logflagsp); /* inode logging flags */
133 * Called by xfs_bmap_add_extent to handle cases converting a hole
134 * to a real allocation.
136 STATIC int /* error */
137 xfs_bmap_add_extent_hole_real(
138 xfs_inode_t *ip, /* incore inode pointer */
139 xfs_extnum_t idx, /* extent number to update/insert */
140 xfs_btree_cur_t *cur, /* if null, not a btree */
141 xfs_bmbt_irec_t *new, /* new data to add to file extents */
142 int *logflagsp, /* inode logging flags */
143 int whichfork); /* data or attr fork */
146 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
147 * allocation to a real allocation or vice versa.
149 STATIC int /* error */
150 xfs_bmap_add_extent_unwritten_real(
151 xfs_inode_t *ip, /* incore inode pointer */
152 xfs_extnum_t idx, /* extent number to update/insert */
153 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
154 xfs_bmbt_irec_t *new, /* new data to add to file extents */
155 int *logflagsp); /* inode logging flags */
158 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
159 * It figures out where to ask the underlying allocator to put the new extent.
161 STATIC int /* error */
163 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
166 * Transform a btree format file with only one leaf node, where the
167 * extents list will fit in the inode, into an extents format file.
168 * Since the file extents are already in-core, all we have to do is
169 * give up the space for the btree root and pitch the leaf block.
171 STATIC int /* error */
172 xfs_bmap_btree_to_extents(
173 xfs_trans_t *tp, /* transaction pointer */
174 xfs_inode_t *ip, /* incore inode pointer */
175 xfs_btree_cur_t *cur, /* btree cursor */
176 int *logflagsp, /* inode logging flags */
177 int whichfork); /* data or attr fork */
180 * Remove the entry "free" from the free item list. Prev points to the
181 * previous entry, unless "free" is the head of the list.
185 xfs_bmap_free_t *flist, /* free item list header */
186 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
187 xfs_bmap_free_item_t *free); /* list item to be freed */
190 * Convert an extents-format file into a btree-format file.
191 * The new file will have a root block (in the inode) and a single child block.
193 STATIC int /* error */
194 xfs_bmap_extents_to_btree(
195 xfs_trans_t *tp, /* transaction pointer */
196 xfs_inode_t *ip, /* incore inode pointer */
197 xfs_fsblock_t *firstblock, /* first-block-allocated */
198 xfs_bmap_free_t *flist, /* blocks freed in xaction */
199 xfs_btree_cur_t **curp, /* cursor returned to caller */
200 int wasdel, /* converting a delayed alloc */
201 int *logflagsp, /* inode logging flags */
202 int whichfork); /* data or attr fork */
205 * Convert a local file to an extents file.
206 * This code is sort of bogus, since the file data needs to get
207 * logged so it won't be lost. The bmap-level manipulations are ok, though.
209 STATIC int /* error */
210 xfs_bmap_local_to_extents(
211 xfs_trans_t *tp, /* transaction pointer */
212 xfs_inode_t *ip, /* incore inode pointer */
213 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
214 xfs_extlen_t total, /* total blocks needed by transaction */
215 int *logflagsp, /* inode logging flags */
216 int whichfork); /* data or attr fork */
219 * Search the extents list for the inode, for the extent containing bno.
220 * If bno lies in a hole, point to the next entry. If bno lies past eof,
221 * *eofp will be set, and *prevp will contain the last entry (null if none).
222 * Else, *lastxp will be set to the index of the found
223 * entry; *gotp will contain the entry.
225 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
226 xfs_bmap_search_extents(
227 xfs_inode_t *ip, /* incore inode pointer */
228 xfs_fileoff_t bno, /* block number searched for */
229 int whichfork, /* data or attr fork */
230 int *eofp, /* out: end of file found */
231 xfs_extnum_t *lastxp, /* out: last extent index */
232 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
233 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
236 * Check the last inode extent to determine whether this allocation will result
237 * in blocks being allocated at the end of the file. When we allocate new data
238 * blocks at the end of the file which do not start at the previous data block,
239 * we will try to align the new blocks at stripe unit boundaries.
241 STATIC int /* error */
243 xfs_inode_t *ip, /* incore inode pointer */
244 xfs_fileoff_t off, /* file offset in fsblocks */
245 int whichfork, /* data or attribute fork */
246 char *aeof); /* return value */
249 * Compute the worst-case number of indirect blocks that will be used
250 * for ip's delayed extent of length "len".
253 xfs_bmap_worst_indlen(
254 xfs_inode_t *ip, /* incore inode pointer */
255 xfs_filblks_t len); /* delayed extent length */
259 * Perform various validation checks on the values being returned
263 xfs_bmap_validate_ret(
267 xfs_bmbt_irec_t *mval,
271 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
279 xfs_fsblock_t blockno,
284 xfs_bmap_count_leaves(
291 xfs_bmap_disk_count_leaves(
292 struct xfs_mount *mp,
293 struct xfs_btree_block *block,
298 * Bmap internal routines.
301 STATIC int /* error */
303 struct xfs_btree_cur *cur,
307 int *stat) /* success/failure */
309 cur->bc_rec.b.br_startoff = off;
310 cur->bc_rec.b.br_startblock = bno;
311 cur->bc_rec.b.br_blockcount = len;
312 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
315 STATIC int /* error */
317 struct xfs_btree_cur *cur,
321 int *stat) /* success/failure */
323 cur->bc_rec.b.br_startoff = off;
324 cur->bc_rec.b.br_startblock = bno;
325 cur->bc_rec.b.br_blockcount = len;
326 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
330 * Update the record referred to by cur to the value given
331 * by [off, bno, len, state].
332 * This either works (return 0) or gets an EFSCORRUPTED error.
336 struct xfs_btree_cur *cur,
342 union xfs_btree_rec rec;
344 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
345 return xfs_btree_update(cur, &rec);
349 * Called from xfs_bmap_add_attrfork to handle btree format files.
351 STATIC int /* error */
352 xfs_bmap_add_attrfork_btree(
353 xfs_trans_t *tp, /* transaction pointer */
354 xfs_inode_t *ip, /* incore inode pointer */
355 xfs_fsblock_t *firstblock, /* first block allocated */
356 xfs_bmap_free_t *flist, /* blocks to free at commit */
357 int *flags) /* inode logging flags */
359 xfs_btree_cur_t *cur; /* btree cursor */
360 int error; /* error return value */
361 xfs_mount_t *mp; /* file system mount struct */
362 int stat; /* newroot status */
365 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
366 *flags |= XFS_ILOG_DBROOT;
368 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
369 cur->bc_private.b.flist = flist;
370 cur->bc_private.b.firstblock = *firstblock;
371 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
373 /* must be at least one entry */
374 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
375 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
378 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
379 return XFS_ERROR(ENOSPC);
381 *firstblock = cur->bc_private.b.firstblock;
382 cur->bc_private.b.allocated = 0;
383 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
387 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
392 * Called from xfs_bmap_add_attrfork to handle extents format files.
394 STATIC int /* error */
395 xfs_bmap_add_attrfork_extents(
396 xfs_trans_t *tp, /* transaction pointer */
397 xfs_inode_t *ip, /* incore inode pointer */
398 xfs_fsblock_t *firstblock, /* first block allocated */
399 xfs_bmap_free_t *flist, /* blocks to free at commit */
400 int *flags) /* inode logging flags */
402 xfs_btree_cur_t *cur; /* bmap btree cursor */
403 int error; /* error return value */
405 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
408 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
409 flags, XFS_DATA_FORK);
411 cur->bc_private.b.allocated = 0;
412 xfs_btree_del_cursor(cur,
413 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
419 * Called from xfs_bmap_add_attrfork to handle local format files.
421 STATIC int /* error */
422 xfs_bmap_add_attrfork_local(
423 xfs_trans_t *tp, /* transaction pointer */
424 xfs_inode_t *ip, /* incore inode pointer */
425 xfs_fsblock_t *firstblock, /* first block allocated */
426 xfs_bmap_free_t *flist, /* blocks to free at commit */
427 int *flags) /* inode logging flags */
429 xfs_da_args_t dargs; /* args for dir/attr code */
430 int error; /* error return value */
431 xfs_mount_t *mp; /* mount structure pointer */
433 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
435 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
437 memset(&dargs, 0, sizeof(dargs));
439 dargs.firstblock = firstblock;
441 dargs.total = mp->m_dirblkfsbs;
442 dargs.whichfork = XFS_DATA_FORK;
444 error = xfs_dir2_sf_to_block(&dargs);
446 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
452 * Called by xfs_bmapi to update file extent records and the btree
453 * after allocating space (or doing a delayed allocation).
455 STATIC int /* error */
457 xfs_inode_t *ip, /* incore inode pointer */
458 xfs_extnum_t idx, /* extent number to update/insert */
459 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
460 xfs_bmbt_irec_t *new, /* new data to add to file extents */
461 xfs_fsblock_t *first, /* pointer to firstblock variable */
462 xfs_bmap_free_t *flist, /* list of extents to be freed */
463 int *logflagsp, /* inode logging flags */
464 int whichfork) /* data or attr fork */
466 xfs_btree_cur_t *cur; /* btree cursor or null */
467 xfs_filblks_t da_new; /* new count del alloc blocks used */
468 xfs_filblks_t da_old; /* old count del alloc blocks used */
469 int error; /* error return value */
470 xfs_ifork_t *ifp; /* inode fork ptr */
471 int logflags; /* returned value */
472 xfs_extnum_t nextents; /* number of extents in file now */
474 XFS_STATS_INC(xs_add_exlist);
476 ifp = XFS_IFORK_PTR(ip, whichfork);
477 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
478 ASSERT(idx <= nextents);
482 * This is the first extent added to a new/empty file.
483 * Special case this one, so other routines get to assume there are
484 * already extents in the list.
487 xfs_iext_insert(ip, 0, 1, new,
488 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
492 if (!isnullstartblock(new->br_startblock)) {
493 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
494 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
499 * Any kind of new delayed allocation goes here.
501 else if (isnullstartblock(new->br_startblock)) {
503 ASSERT((cur->bc_private.b.flags &
504 XFS_BTCUR_BPRV_WASDEL) == 0);
505 error = xfs_bmap_add_extent_hole_delay(ip, idx, new, &logflags);
510 * Real allocation off the end of the file.
512 else if (idx == nextents) {
514 ASSERT((cur->bc_private.b.flags &
515 XFS_BTCUR_BPRV_WASDEL) == 0);
516 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
517 &logflags, whichfork)))
520 xfs_bmbt_irec_t prev; /* old extent at offset idx */
523 * Get the record referred to by idx.
525 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
527 * If it's a real allocation record, and the new allocation ends
528 * after the start of the referred to record, then we're filling
529 * in a delayed or unwritten allocation with a real one, or
530 * converting real back to unwritten.
532 if (!isnullstartblock(new->br_startblock) &&
533 new->br_startoff + new->br_blockcount > prev.br_startoff) {
534 if (prev.br_state != XFS_EXT_UNWRITTEN &&
535 isnullstartblock(prev.br_startblock)) {
536 da_old = startblockval(prev.br_startblock);
538 ASSERT(cur->bc_private.b.flags &
539 XFS_BTCUR_BPRV_WASDEL);
540 error = xfs_bmap_add_extent_delay_real(ip, idx,
541 &cur, new, &da_new, first,
545 } else if (new->br_state == XFS_EXT_NORM) {
546 ASSERT(new->br_state == XFS_EXT_NORM);
547 if ((error = xfs_bmap_add_extent_unwritten_real(
548 ip, idx, &cur, new, &logflags)))
551 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
552 if ((error = xfs_bmap_add_extent_unwritten_real(
553 ip, idx, &cur, new, &logflags)))
556 ASSERT(*curp == cur || *curp == NULL);
559 * Otherwise we're filling in a hole with an allocation.
563 ASSERT((cur->bc_private.b.flags &
564 XFS_BTCUR_BPRV_WASDEL) == 0);
565 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
566 new, &logflags, whichfork)))
571 ASSERT(*curp == cur || *curp == NULL);
573 * Convert to a btree if necessary.
575 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
576 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
577 int tmp_logflags; /* partial log flag return val */
580 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
581 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
582 logflags |= tmp_logflags;
587 * Adjust for changes in reserved delayed indirect blocks.
588 * Nothing to do for disk quotas here.
590 if (da_old || da_new) {
595 nblks += cur->bc_private.b.allocated;
596 ASSERT(nblks <= da_old);
598 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
599 (int64_t)(da_old - nblks), 0);
602 * Clear out the allocated field, done with it now in any case.
605 cur->bc_private.b.allocated = 0;
611 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
613 *logflagsp = logflags;
618 * Called by xfs_bmap_add_extent to handle cases converting a delayed
619 * allocation to a real allocation.
621 STATIC int /* error */
622 xfs_bmap_add_extent_delay_real(
623 xfs_inode_t *ip, /* incore inode pointer */
624 xfs_extnum_t idx, /* extent number to update/insert */
625 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
626 xfs_bmbt_irec_t *new, /* new data to add to file extents */
627 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
628 xfs_fsblock_t *first, /* pointer to firstblock variable */
629 xfs_bmap_free_t *flist, /* list of extents to be freed */
630 int *logflagsp) /* inode logging flags */
632 xfs_btree_cur_t *cur; /* btree cursor */
633 int diff; /* temp value */
634 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
635 int error; /* error return value */
636 int i; /* temp state */
637 xfs_ifork_t *ifp; /* inode fork pointer */
638 xfs_fileoff_t new_endoff; /* end offset of new entry */
639 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
640 /* left is 0, right is 1, prev is 2 */
641 int rval=0; /* return value (logging flags) */
642 int state = 0;/* state bits, accessed thru macros */
643 xfs_filblks_t temp=0; /* value for dnew calculations */
644 xfs_filblks_t temp2=0;/* value for dnew calculations */
645 int tmp_rval; /* partial logging flags */
652 * Set up a bunch of variables to make the tests simpler.
655 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
656 ep = xfs_iext_get_ext(ifp, idx);
657 xfs_bmbt_get_all(ep, &PREV);
658 new_endoff = new->br_startoff + new->br_blockcount;
659 ASSERT(PREV.br_startoff <= new->br_startoff);
660 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
663 * Set flags determining what part of the previous delayed allocation
664 * extent is being replaced by a real allocation.
666 if (PREV.br_startoff == new->br_startoff)
667 state |= BMAP_LEFT_FILLING;
668 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
669 state |= BMAP_RIGHT_FILLING;
672 * Check and set flags if this segment has a left neighbor.
673 * Don't set contiguous if the combined extent would be too large.
676 state |= BMAP_LEFT_VALID;
677 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
679 if (isnullstartblock(LEFT.br_startblock))
680 state |= BMAP_LEFT_DELAY;
683 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
684 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
685 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
686 LEFT.br_state == new->br_state &&
687 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
688 state |= BMAP_LEFT_CONTIG;
691 * Check and set flags if this segment has a right neighbor.
692 * Don't set contiguous if the combined extent would be too large.
693 * Also check for all-three-contiguous being too large.
695 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
696 state |= BMAP_RIGHT_VALID;
697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
699 if (isnullstartblock(RIGHT.br_startblock))
700 state |= BMAP_RIGHT_DELAY;
703 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
704 new_endoff == RIGHT.br_startoff &&
705 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
706 new->br_state == RIGHT.br_state &&
707 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
708 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
709 BMAP_RIGHT_FILLING)) !=
710 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
711 BMAP_RIGHT_FILLING) ||
712 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
714 state |= BMAP_RIGHT_CONTIG;
718 * Switch out based on the FILLING and CONTIG state bits.
720 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
721 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
722 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
723 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
725 * Filling in all of a previously delayed allocation extent.
726 * The left and right neighbors are both contiguous with new.
728 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
729 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
730 LEFT.br_blockcount + PREV.br_blockcount +
731 RIGHT.br_blockcount);
732 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
734 xfs_iext_remove(ip, idx, 2, state);
735 ip->i_df.if_lastex = idx - 1;
736 ip->i_d.di_nextents--;
738 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
740 rval = XFS_ILOG_CORE;
741 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
743 RIGHT.br_blockcount, &i)))
745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
746 if ((error = xfs_btree_delete(cur, &i)))
748 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
749 if ((error = xfs_btree_decrement(cur, 0, &i)))
751 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
752 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
756 RIGHT.br_blockcount, LEFT.br_state)))
762 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
764 * Filling in all of a previously delayed allocation extent.
765 * The left neighbor is contiguous, the right is not.
767 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
768 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
769 LEFT.br_blockcount + PREV.br_blockcount);
770 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
772 ip->i_df.if_lastex = idx - 1;
773 xfs_iext_remove(ip, idx, 1, state);
775 rval = XFS_ILOG_DEXT;
778 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
779 LEFT.br_startblock, LEFT.br_blockcount,
782 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
783 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
786 PREV.br_blockcount, LEFT.br_state)))
792 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
794 * Filling in all of a previously delayed allocation extent.
795 * The right neighbor is contiguous, the left is not.
797 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
798 xfs_bmbt_set_startblock(ep, new->br_startblock);
799 xfs_bmbt_set_blockcount(ep,
800 PREV.br_blockcount + RIGHT.br_blockcount);
801 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
803 ip->i_df.if_lastex = idx;
804 xfs_iext_remove(ip, idx + 1, 1, state);
806 rval = XFS_ILOG_DEXT;
809 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
811 RIGHT.br_blockcount, &i)))
813 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
814 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
817 RIGHT.br_blockcount, PREV.br_state)))
823 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
825 * Filling in all of a previously delayed allocation extent.
826 * Neither the left nor right neighbors are contiguous with
829 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
830 xfs_bmbt_set_startblock(ep, new->br_startblock);
831 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
833 ip->i_df.if_lastex = idx;
834 ip->i_d.di_nextents++;
836 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
838 rval = XFS_ILOG_CORE;
839 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
840 new->br_startblock, new->br_blockcount,
843 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
844 cur->bc_rec.b.br_state = XFS_EXT_NORM;
845 if ((error = xfs_btree_insert(cur, &i)))
847 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
852 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
854 * Filling in the first part of a previous delayed allocation.
855 * The left neighbor is contiguous.
857 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
858 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
859 LEFT.br_blockcount + new->br_blockcount);
860 xfs_bmbt_set_startoff(ep,
861 PREV.br_startoff + new->br_blockcount);
862 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
864 temp = PREV.br_blockcount - new->br_blockcount;
865 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
866 xfs_bmbt_set_blockcount(ep, temp);
867 ip->i_df.if_lastex = idx - 1;
869 rval = XFS_ILOG_DEXT;
872 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
873 LEFT.br_startblock, LEFT.br_blockcount,
876 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
877 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
884 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
885 startblockval(PREV.br_startblock));
886 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
887 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
891 case BMAP_LEFT_FILLING:
893 * Filling in the first part of a previous delayed allocation.
894 * The left neighbor is not contiguous.
896 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
897 xfs_bmbt_set_startoff(ep, new_endoff);
898 temp = PREV.br_blockcount - new->br_blockcount;
899 xfs_bmbt_set_blockcount(ep, temp);
900 xfs_iext_insert(ip, idx, 1, new, state);
901 ip->i_df.if_lastex = idx;
902 ip->i_d.di_nextents++;
904 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
906 rval = XFS_ILOG_CORE;
907 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
908 new->br_startblock, new->br_blockcount,
911 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
912 cur->bc_rec.b.br_state = XFS_EXT_NORM;
913 if ((error = xfs_btree_insert(cur, &i)))
915 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
917 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
918 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
919 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
920 first, flist, &cur, 1, &tmp_rval,
926 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
927 startblockval(PREV.br_startblock) -
928 (cur ? cur->bc_private.b.allocated : 0));
929 ep = xfs_iext_get_ext(ifp, idx + 1);
930 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
931 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
935 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
937 * Filling in the last part of a previous delayed allocation.
938 * The right neighbor is contiguous with the new allocation.
940 temp = PREV.br_blockcount - new->br_blockcount;
941 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
942 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
943 xfs_bmbt_set_blockcount(ep, temp);
944 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
945 new->br_startoff, new->br_startblock,
946 new->br_blockcount + RIGHT.br_blockcount,
948 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
949 ip->i_df.if_lastex = idx + 1;
951 rval = XFS_ILOG_DEXT;
954 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
956 RIGHT.br_blockcount, &i)))
958 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
959 if ((error = xfs_bmbt_update(cur, new->br_startoff,
966 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
967 startblockval(PREV.br_startblock));
968 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
969 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
973 case BMAP_RIGHT_FILLING:
975 * Filling in the last part of a previous delayed allocation.
976 * The right neighbor is not contiguous.
978 temp = PREV.br_blockcount - new->br_blockcount;
979 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
980 xfs_bmbt_set_blockcount(ep, temp);
981 xfs_iext_insert(ip, idx + 1, 1, new, state);
982 ip->i_df.if_lastex = idx + 1;
983 ip->i_d.di_nextents++;
985 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
987 rval = XFS_ILOG_CORE;
988 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
989 new->br_startblock, new->br_blockcount,
992 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
993 cur->bc_rec.b.br_state = XFS_EXT_NORM;
994 if ((error = xfs_btree_insert(cur, &i)))
996 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
998 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
999 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1000 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1001 first, flist, &cur, 1, &tmp_rval,
1007 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1008 startblockval(PREV.br_startblock) -
1009 (cur ? cur->bc_private.b.allocated : 0));
1010 ep = xfs_iext_get_ext(ifp, idx);
1011 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1012 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1018 * Filling in the middle part of a previous delayed allocation.
1019 * Contiguity is impossible here.
1020 * This case is avoided almost all the time.
1022 * We start with a delayed allocation:
1024 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1027 * and we are allocating:
1028 * +rrrrrrrrrrrrrrrrr+
1031 * and we set it up for insertion as:
1032 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1034 * PREV @ idx LEFT RIGHT
1035 * inserted at idx + 1
1037 temp = new->br_startoff - PREV.br_startoff;
1038 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1039 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1040 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
1042 RIGHT.br_state = PREV.br_state;
1043 RIGHT.br_startblock = nullstartblock(
1044 (int)xfs_bmap_worst_indlen(ip, temp2));
1045 RIGHT.br_startoff = new_endoff;
1046 RIGHT.br_blockcount = temp2;
1047 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1048 xfs_iext_insert(ip, idx + 1, 2, &LEFT, state);
1049 ip->i_df.if_lastex = idx + 1;
1050 ip->i_d.di_nextents++;
1052 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1054 rval = XFS_ILOG_CORE;
1055 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1056 new->br_startblock, new->br_blockcount,
1059 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1060 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1061 if ((error = xfs_btree_insert(cur, &i)))
1063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1065 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1066 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1067 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1068 first, flist, &cur, 1, &tmp_rval,
1074 temp = xfs_bmap_worst_indlen(ip, temp);
1075 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1076 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1077 (cur ? cur->bc_private.b.allocated : 0));
1079 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1080 -((int64_t)diff), 0)) {
1082 * Ick gross gag me with a spoon.
1084 ASSERT(0); /* want to see if this ever happens! */
1090 !xfs_icsb_modify_counters(ip->i_mount,
1092 -((int64_t)diff), 0))
1099 !xfs_icsb_modify_counters(ip->i_mount,
1101 -((int64_t)diff), 0))
1106 ep = xfs_iext_get_ext(ifp, idx);
1107 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1108 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1109 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1110 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1111 nullstartblock((int)temp2));
1112 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1113 *dnew = temp + temp2;
1116 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1117 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1118 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1119 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1120 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_CONTIG:
1122 case BMAP_RIGHT_CONTIG:
1124 * These cases are all impossible.
1138 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1139 * allocation to a real allocation or vice versa.
1141 STATIC int /* error */
1142 xfs_bmap_add_extent_unwritten_real(
1143 xfs_inode_t *ip, /* incore inode pointer */
1144 xfs_extnum_t idx, /* extent number to update/insert */
1145 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1146 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1147 int *logflagsp) /* inode logging flags */
1149 xfs_btree_cur_t *cur; /* btree cursor */
1150 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1151 int error; /* error return value */
1152 int i; /* temp state */
1153 xfs_ifork_t *ifp; /* inode fork pointer */
1154 xfs_fileoff_t new_endoff; /* end offset of new entry */
1155 xfs_exntst_t newext; /* new extent state */
1156 xfs_exntst_t oldext; /* old extent state */
1157 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1158 /* left is 0, right is 1, prev is 2 */
1159 int rval=0; /* return value (logging flags) */
1160 int state = 0;/* state bits, accessed thru macros */
1166 * Set up a bunch of variables to make the tests simpler.
1170 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1171 ep = xfs_iext_get_ext(ifp, idx);
1172 xfs_bmbt_get_all(ep, &PREV);
1173 newext = new->br_state;
1174 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1175 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1176 ASSERT(PREV.br_state == oldext);
1177 new_endoff = new->br_startoff + new->br_blockcount;
1178 ASSERT(PREV.br_startoff <= new->br_startoff);
1179 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1182 * Set flags determining what part of the previous oldext allocation
1183 * extent is being replaced by a newext allocation.
1185 if (PREV.br_startoff == new->br_startoff)
1186 state |= BMAP_LEFT_FILLING;
1187 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1188 state |= BMAP_RIGHT_FILLING;
1191 * Check and set flags if this segment has a left neighbor.
1192 * Don't set contiguous if the combined extent would be too large.
1195 state |= BMAP_LEFT_VALID;
1196 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1198 if (isnullstartblock(LEFT.br_startblock))
1199 state |= BMAP_LEFT_DELAY;
1202 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1203 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1204 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1205 LEFT.br_state == newext &&
1206 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1207 state |= BMAP_LEFT_CONTIG;
1210 * Check and set flags if this segment has a right neighbor.
1211 * Don't set contiguous if the combined extent would be too large.
1212 * Also check for all-three-contiguous being too large.
1214 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1215 state |= BMAP_RIGHT_VALID;
1216 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1217 if (isnullstartblock(RIGHT.br_startblock))
1218 state |= BMAP_RIGHT_DELAY;
1221 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1222 new_endoff == RIGHT.br_startoff &&
1223 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1224 newext == RIGHT.br_state &&
1225 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1226 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1227 BMAP_RIGHT_FILLING)) !=
1228 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1229 BMAP_RIGHT_FILLING) ||
1230 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1232 state |= BMAP_RIGHT_CONTIG;
1235 * Switch out based on the FILLING and CONTIG state bits.
1237 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1238 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1239 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1240 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1242 * Setting all of a previous oldext extent to newext.
1243 * The left and right neighbors are both contiguous with new.
1245 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1246 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1247 LEFT.br_blockcount + PREV.br_blockcount +
1248 RIGHT.br_blockcount);
1249 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1251 xfs_iext_remove(ip, idx, 2, state);
1252 ip->i_df.if_lastex = idx - 1;
1253 ip->i_d.di_nextents -= 2;
1255 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1257 rval = XFS_ILOG_CORE;
1258 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1259 RIGHT.br_startblock,
1260 RIGHT.br_blockcount, &i)))
1262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1263 if ((error = xfs_btree_delete(cur, &i)))
1265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1266 if ((error = xfs_btree_decrement(cur, 0, &i)))
1268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1269 if ((error = xfs_btree_delete(cur, &i)))
1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1272 if ((error = xfs_btree_decrement(cur, 0, &i)))
1274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1275 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1277 LEFT.br_blockcount + PREV.br_blockcount +
1278 RIGHT.br_blockcount, LEFT.br_state)))
1283 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1285 * Setting all of a previous oldext extent to newext.
1286 * The left neighbor is contiguous, the right is not.
1288 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1289 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1290 LEFT.br_blockcount + PREV.br_blockcount);
1291 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1293 ip->i_df.if_lastex = idx - 1;
1294 xfs_iext_remove(ip, idx, 1, state);
1295 ip->i_d.di_nextents--;
1297 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1299 rval = XFS_ILOG_CORE;
1300 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1301 PREV.br_startblock, PREV.br_blockcount,
1304 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1305 if ((error = xfs_btree_delete(cur, &i)))
1307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1308 if ((error = xfs_btree_decrement(cur, 0, &i)))
1310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1311 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1313 LEFT.br_blockcount + PREV.br_blockcount,
1319 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1321 * Setting all of a previous oldext extent to newext.
1322 * The right neighbor is contiguous, the left is not.
1324 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1325 xfs_bmbt_set_blockcount(ep,
1326 PREV.br_blockcount + RIGHT.br_blockcount);
1327 xfs_bmbt_set_state(ep, newext);
1328 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1329 ip->i_df.if_lastex = idx;
1330 xfs_iext_remove(ip, idx + 1, 1, state);
1331 ip->i_d.di_nextents--;
1333 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1335 rval = XFS_ILOG_CORE;
1336 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1337 RIGHT.br_startblock,
1338 RIGHT.br_blockcount, &i)))
1340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1341 if ((error = xfs_btree_delete(cur, &i)))
1343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1344 if ((error = xfs_btree_decrement(cur, 0, &i)))
1346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1347 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1349 new->br_blockcount + RIGHT.br_blockcount,
1355 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1357 * Setting all of a previous oldext extent to newext.
1358 * Neither the left nor right neighbors are contiguous with
1361 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1362 xfs_bmbt_set_state(ep, newext);
1363 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1365 ip->i_df.if_lastex = idx;
1367 rval = XFS_ILOG_DEXT;
1370 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1371 new->br_startblock, new->br_blockcount,
1374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1375 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1376 new->br_startblock, new->br_blockcount,
1382 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1384 * Setting the first part of a previous oldext extent to newext.
1385 * The left neighbor is contiguous.
1387 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1388 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1389 LEFT.br_blockcount + new->br_blockcount);
1390 xfs_bmbt_set_startoff(ep,
1391 PREV.br_startoff + new->br_blockcount);
1392 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1394 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1395 xfs_bmbt_set_startblock(ep,
1396 new->br_startblock + new->br_blockcount);
1397 xfs_bmbt_set_blockcount(ep,
1398 PREV.br_blockcount - new->br_blockcount);
1399 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1401 ip->i_df.if_lastex = idx - 1;
1403 rval = XFS_ILOG_DEXT;
1406 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1407 PREV.br_startblock, PREV.br_blockcount,
1410 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1411 if ((error = xfs_bmbt_update(cur,
1412 PREV.br_startoff + new->br_blockcount,
1413 PREV.br_startblock + new->br_blockcount,
1414 PREV.br_blockcount - new->br_blockcount,
1417 if ((error = xfs_btree_decrement(cur, 0, &i)))
1419 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1421 LEFT.br_blockcount + new->br_blockcount,
1427 case BMAP_LEFT_FILLING:
1429 * Setting the first part of a previous oldext extent to newext.
1430 * The left neighbor is not contiguous.
1432 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1433 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1434 xfs_bmbt_set_startoff(ep, new_endoff);
1435 xfs_bmbt_set_blockcount(ep,
1436 PREV.br_blockcount - new->br_blockcount);
1437 xfs_bmbt_set_startblock(ep,
1438 new->br_startblock + new->br_blockcount);
1439 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1441 xfs_iext_insert(ip, idx, 1, new, state);
1442 ip->i_df.if_lastex = idx;
1443 ip->i_d.di_nextents++;
1445 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1447 rval = XFS_ILOG_CORE;
1448 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1449 PREV.br_startblock, PREV.br_blockcount,
1452 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1453 if ((error = xfs_bmbt_update(cur,
1454 PREV.br_startoff + new->br_blockcount,
1455 PREV.br_startblock + new->br_blockcount,
1456 PREV.br_blockcount - new->br_blockcount,
1459 cur->bc_rec.b = *new;
1460 if ((error = xfs_btree_insert(cur, &i)))
1462 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1466 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1468 * Setting the last part of a previous oldext extent to newext.
1469 * The right neighbor is contiguous with the new allocation.
1471 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1472 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1473 xfs_bmbt_set_blockcount(ep,
1474 PREV.br_blockcount - new->br_blockcount);
1475 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1476 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1477 new->br_startoff, new->br_startblock,
1478 new->br_blockcount + RIGHT.br_blockcount, newext);
1479 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1481 ip->i_df.if_lastex = idx + 1;
1483 rval = XFS_ILOG_DEXT;
1486 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1488 PREV.br_blockcount, &i)))
1490 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1491 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1493 PREV.br_blockcount - new->br_blockcount,
1496 if ((error = xfs_btree_increment(cur, 0, &i)))
1498 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1500 new->br_blockcount + RIGHT.br_blockcount,
1506 case BMAP_RIGHT_FILLING:
1508 * Setting the last part of a previous oldext extent to newext.
1509 * The right neighbor is not contiguous.
1511 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1512 xfs_bmbt_set_blockcount(ep,
1513 PREV.br_blockcount - new->br_blockcount);
1514 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1516 xfs_iext_insert(ip, idx + 1, 1, new, state);
1517 ip->i_df.if_lastex = idx + 1;
1518 ip->i_d.di_nextents++;
1520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1522 rval = XFS_ILOG_CORE;
1523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1524 PREV.br_startblock, PREV.br_blockcount,
1527 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1528 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1530 PREV.br_blockcount - new->br_blockcount,
1533 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1534 new->br_startblock, new->br_blockcount,
1537 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1538 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1539 if ((error = xfs_btree_insert(cur, &i)))
1541 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1547 * Setting the middle part of a previous oldext extent to
1548 * newext. Contiguity is impossible here.
1549 * One extent becomes three extents.
1551 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1552 xfs_bmbt_set_blockcount(ep,
1553 new->br_startoff - PREV.br_startoff);
1554 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1557 r[1].br_startoff = new_endoff;
1558 r[1].br_blockcount =
1559 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1560 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1561 r[1].br_state = oldext;
1562 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1563 ip->i_df.if_lastex = idx + 1;
1564 ip->i_d.di_nextents += 2;
1566 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1568 rval = XFS_ILOG_CORE;
1569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1570 PREV.br_startblock, PREV.br_blockcount,
1573 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1574 /* new right extent - oldext */
1575 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1576 r[1].br_startblock, r[1].br_blockcount,
1579 /* new left extent - oldext */
1580 cur->bc_rec.b = PREV;
1581 cur->bc_rec.b.br_blockcount =
1582 new->br_startoff - PREV.br_startoff;
1583 if ((error = xfs_btree_insert(cur, &i)))
1585 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1587 * Reset the cursor to the position of the new extent
1588 * we are about to insert as we can't trust it after
1589 * the previous insert.
1591 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1592 new->br_startblock, new->br_blockcount,
1595 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1596 /* new middle extent - newext */
1597 cur->bc_rec.b.br_state = new->br_state;
1598 if ((error = xfs_btree_insert(cur, &i)))
1600 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1604 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1606 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1607 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1608 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_CONTIG:
1610 case BMAP_RIGHT_CONTIG:
1612 * These cases are all impossible.
1626 * Called by xfs_bmap_add_extent to handle cases converting a hole
1627 * to a delayed allocation.
1630 STATIC int /* error */
1631 xfs_bmap_add_extent_hole_delay(
1632 xfs_inode_t *ip, /* incore inode pointer */
1633 xfs_extnum_t idx, /* extent number to update/insert */
1634 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1635 int *logflagsp) /* inode logging flags */
1637 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1638 xfs_ifork_t *ifp; /* inode fork pointer */
1639 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1640 xfs_filblks_t newlen=0; /* new indirect size */
1641 xfs_filblks_t oldlen=0; /* old indirect size */
1642 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1643 int state; /* state bits, accessed thru macros */
1644 xfs_filblks_t temp=0; /* temp for indirect calculations */
1646 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1647 ep = xfs_iext_get_ext(ifp, idx);
1649 ASSERT(isnullstartblock(new->br_startblock));
1652 * Check and set flags if this segment has a left neighbor
1655 state |= BMAP_LEFT_VALID;
1656 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1658 if (isnullstartblock(left.br_startblock))
1659 state |= BMAP_LEFT_DELAY;
1663 * Check and set flags if the current (right) segment exists.
1664 * If it doesn't exist, we're converting the hole at end-of-file.
1666 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1667 state |= BMAP_RIGHT_VALID;
1668 xfs_bmbt_get_all(ep, &right);
1670 if (isnullstartblock(right.br_startblock))
1671 state |= BMAP_RIGHT_DELAY;
1675 * Set contiguity flags on the left and right neighbors.
1676 * Don't let extents get too large, even if the pieces are contiguous.
1678 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1679 left.br_startoff + left.br_blockcount == new->br_startoff &&
1680 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1681 state |= BMAP_LEFT_CONTIG;
1683 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1684 new->br_startoff + new->br_blockcount == right.br_startoff &&
1685 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1686 (!(state & BMAP_LEFT_CONTIG) ||
1687 (left.br_blockcount + new->br_blockcount +
1688 right.br_blockcount <= MAXEXTLEN)))
1689 state |= BMAP_RIGHT_CONTIG;
1692 * Switch out based on the contiguity flags.
1694 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1695 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1697 * New allocation is contiguous with delayed allocations
1698 * on the left and on the right.
1699 * Merge all three into a single extent record.
1701 temp = left.br_blockcount + new->br_blockcount +
1702 right.br_blockcount;
1704 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1705 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1706 oldlen = startblockval(left.br_startblock) +
1707 startblockval(new->br_startblock) +
1708 startblockval(right.br_startblock);
1709 newlen = xfs_bmap_worst_indlen(ip, temp);
1710 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1711 nullstartblock((int)newlen));
1712 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1714 xfs_iext_remove(ip, idx, 1, state);
1715 ip->i_df.if_lastex = idx - 1;
1718 case BMAP_LEFT_CONTIG:
1720 * New allocation is contiguous with a delayed allocation
1722 * Merge the new allocation with the left neighbor.
1724 temp = left.br_blockcount + new->br_blockcount;
1725 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1726 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1727 oldlen = startblockval(left.br_startblock) +
1728 startblockval(new->br_startblock);
1729 newlen = xfs_bmap_worst_indlen(ip, temp);
1730 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1731 nullstartblock((int)newlen));
1732 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1734 ip->i_df.if_lastex = idx - 1;
1737 case BMAP_RIGHT_CONTIG:
1739 * New allocation is contiguous with a delayed allocation
1741 * Merge the new allocation with the right neighbor.
1743 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1744 temp = new->br_blockcount + right.br_blockcount;
1745 oldlen = startblockval(new->br_startblock) +
1746 startblockval(right.br_startblock);
1747 newlen = xfs_bmap_worst_indlen(ip, temp);
1748 xfs_bmbt_set_allf(ep, new->br_startoff,
1749 nullstartblock((int)newlen), temp, right.br_state);
1750 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1752 ip->i_df.if_lastex = idx;
1757 * New allocation is not contiguous with another
1758 * delayed allocation.
1759 * Insert a new entry.
1761 oldlen = newlen = 0;
1762 xfs_iext_insert(ip, idx, 1, new, state);
1763 ip->i_df.if_lastex = idx;
1766 if (oldlen != newlen) {
1767 ASSERT(oldlen > newlen);
1768 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1769 (int64_t)(oldlen - newlen), 0);
1771 * Nothing to do for disk quota accounting here.
1779 * Called by xfs_bmap_add_extent to handle cases converting a hole
1780 * to a real allocation.
1782 STATIC int /* error */
1783 xfs_bmap_add_extent_hole_real(
1784 xfs_inode_t *ip, /* incore inode pointer */
1785 xfs_extnum_t idx, /* extent number to update/insert */
1786 xfs_btree_cur_t *cur, /* if null, not a btree */
1787 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1788 int *logflagsp, /* inode logging flags */
1789 int whichfork) /* data or attr fork */
1791 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
1792 int error; /* error return value */
1793 int i; /* temp state */
1794 xfs_ifork_t *ifp; /* inode fork pointer */
1795 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1796 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1797 int rval=0; /* return value (logging flags) */
1798 int state; /* state bits, accessed thru macros */
1800 ifp = XFS_IFORK_PTR(ip, whichfork);
1801 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1802 ep = xfs_iext_get_ext(ifp, idx);
1805 if (whichfork == XFS_ATTR_FORK)
1806 state |= BMAP_ATTRFORK;
1809 * Check and set flags if this segment has a left neighbor.
1812 state |= BMAP_LEFT_VALID;
1813 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1814 if (isnullstartblock(left.br_startblock))
1815 state |= BMAP_LEFT_DELAY;
1819 * Check and set flags if this segment has a current value.
1820 * Not true if we're inserting into the "hole" at eof.
1822 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1823 state |= BMAP_RIGHT_VALID;
1824 xfs_bmbt_get_all(ep, &right);
1825 if (isnullstartblock(right.br_startblock))
1826 state |= BMAP_RIGHT_DELAY;
1830 * We're inserting a real allocation between "left" and "right".
1831 * Set the contiguity flags. Don't let extents get too large.
1833 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1834 left.br_startoff + left.br_blockcount == new->br_startoff &&
1835 left.br_startblock + left.br_blockcount == new->br_startblock &&
1836 left.br_state == new->br_state &&
1837 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1838 state |= BMAP_LEFT_CONTIG;
1840 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1841 new->br_startoff + new->br_blockcount == right.br_startoff &&
1842 new->br_startblock + new->br_blockcount == right.br_startblock &&
1843 new->br_state == right.br_state &&
1844 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1845 (!(state & BMAP_LEFT_CONTIG) ||
1846 left.br_blockcount + new->br_blockcount +
1847 right.br_blockcount <= MAXEXTLEN))
1848 state |= BMAP_RIGHT_CONTIG;
1852 * Select which case we're in here, and implement it.
1854 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1855 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1857 * New allocation is contiguous with real allocations on the
1858 * left and on the right.
1859 * Merge all three into a single extent record.
1861 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1863 left.br_blockcount + new->br_blockcount +
1864 right.br_blockcount);
1865 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1867 xfs_iext_remove(ip, idx, 1, state);
1868 ifp->if_lastex = idx - 1;
1869 XFS_IFORK_NEXT_SET(ip, whichfork,
1870 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1872 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1874 rval = XFS_ILOG_CORE;
1875 if ((error = xfs_bmbt_lookup_eq(cur,
1877 right.br_startblock,
1878 right.br_blockcount, &i)))
1880 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1881 if ((error = xfs_btree_delete(cur, &i)))
1883 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1884 if ((error = xfs_btree_decrement(cur, 0, &i)))
1886 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1887 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1889 left.br_blockcount +
1890 new->br_blockcount +
1891 right.br_blockcount,
1897 case BMAP_LEFT_CONTIG:
1899 * New allocation is contiguous with a real allocation
1901 * Merge the new allocation with the left neighbor.
1903 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1904 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1905 left.br_blockcount + new->br_blockcount);
1906 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1908 ifp->if_lastex = idx - 1;
1910 rval = xfs_ilog_fext(whichfork);
1913 if ((error = xfs_bmbt_lookup_eq(cur,
1916 left.br_blockcount, &i)))
1918 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1919 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1921 left.br_blockcount +
1928 case BMAP_RIGHT_CONTIG:
1930 * New allocation is contiguous with a real allocation
1932 * Merge the new allocation with the right neighbor.
1934 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1935 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1936 new->br_blockcount + right.br_blockcount,
1938 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1940 ifp->if_lastex = idx;
1942 rval = xfs_ilog_fext(whichfork);
1945 if ((error = xfs_bmbt_lookup_eq(cur,
1947 right.br_startblock,
1948 right.br_blockcount, &i)))
1950 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1951 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1953 new->br_blockcount +
1954 right.br_blockcount,
1962 * New allocation is not contiguous with another
1964 * Insert a new entry.
1966 xfs_iext_insert(ip, idx, 1, new, state);
1967 ifp->if_lastex = idx;
1968 XFS_IFORK_NEXT_SET(ip, whichfork,
1969 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1971 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1973 rval = XFS_ILOG_CORE;
1974 if ((error = xfs_bmbt_lookup_eq(cur,
1977 new->br_blockcount, &i)))
1979 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1980 cur->bc_rec.b.br_state = new->br_state;
1981 if ((error = xfs_btree_insert(cur, &i)))
1983 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1993 * Adjust the size of the new extent based on di_extsize and rt extsize.
1996 xfs_bmap_extsize_align(
1998 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1999 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2000 xfs_extlen_t extsz, /* align to this extent size */
2001 int rt, /* is this a realtime inode? */
2002 int eof, /* is extent at end-of-file? */
2003 int delay, /* creating delalloc extent? */
2004 int convert, /* overwriting unwritten extent? */
2005 xfs_fileoff_t *offp, /* in/out: aligned offset */
2006 xfs_extlen_t *lenp) /* in/out: aligned length */
2008 xfs_fileoff_t orig_off; /* original offset */
2009 xfs_extlen_t orig_alen; /* original length */
2010 xfs_fileoff_t orig_end; /* original off+len */
2011 xfs_fileoff_t nexto; /* next file offset */
2012 xfs_fileoff_t prevo; /* previous file offset */
2013 xfs_fileoff_t align_off; /* temp for offset */
2014 xfs_extlen_t align_alen; /* temp for length */
2015 xfs_extlen_t temp; /* temp for calculations */
2020 orig_off = align_off = *offp;
2021 orig_alen = align_alen = *lenp;
2022 orig_end = orig_off + orig_alen;
2025 * If this request overlaps an existing extent, then don't
2026 * attempt to perform any additional alignment.
2028 if (!delay && !eof &&
2029 (orig_off >= gotp->br_startoff) &&
2030 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2035 * If the file offset is unaligned vs. the extent size
2036 * we need to align it. This will be possible unless
2037 * the file was previously written with a kernel that didn't
2038 * perform this alignment, or if a truncate shot us in the
2041 temp = do_mod(orig_off, extsz);
2047 * Same adjustment for the end of the requested area.
2049 if ((temp = (align_alen % extsz))) {
2050 align_alen += extsz - temp;
2053 * If the previous block overlaps with this proposed allocation
2054 * then move the start forward without adjusting the length.
2056 if (prevp->br_startoff != NULLFILEOFF) {
2057 if (prevp->br_startblock == HOLESTARTBLOCK)
2058 prevo = prevp->br_startoff;
2060 prevo = prevp->br_startoff + prevp->br_blockcount;
2063 if (align_off != orig_off && align_off < prevo)
2066 * If the next block overlaps with this proposed allocation
2067 * then move the start back without adjusting the length,
2068 * but not before offset 0.
2069 * This may of course make the start overlap previous block,
2070 * and if we hit the offset 0 limit then the next block
2071 * can still overlap too.
2073 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2074 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2075 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2076 nexto = gotp->br_startoff + gotp->br_blockcount;
2078 nexto = gotp->br_startoff;
2080 nexto = NULLFILEOFF;
2082 align_off + align_alen != orig_end &&
2083 align_off + align_alen > nexto)
2084 align_off = nexto > align_alen ? nexto - align_alen : 0;
2086 * If we're now overlapping the next or previous extent that
2087 * means we can't fit an extsz piece in this hole. Just move
2088 * the start forward to the first valid spot and set
2089 * the length so we hit the end.
2091 if (align_off != orig_off && align_off < prevo)
2093 if (align_off + align_alen != orig_end &&
2094 align_off + align_alen > nexto &&
2095 nexto != NULLFILEOFF) {
2096 ASSERT(nexto > prevo);
2097 align_alen = nexto - align_off;
2101 * If realtime, and the result isn't a multiple of the realtime
2102 * extent size we need to remove blocks until it is.
2104 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2106 * We're not covering the original request, or
2107 * we won't be able to once we fix the length.
2109 if (orig_off < align_off ||
2110 orig_end > align_off + align_alen ||
2111 align_alen - temp < orig_alen)
2112 return XFS_ERROR(EINVAL);
2114 * Try to fix it by moving the start up.
2116 if (align_off + temp <= orig_off) {
2121 * Try to fix it by moving the end in.
2123 else if (align_off + align_alen - temp >= orig_end)
2126 * Set the start to the minimum then trim the length.
2129 align_alen -= orig_off - align_off;
2130 align_off = orig_off;
2131 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2134 * Result doesn't cover the request, fail it.
2136 if (orig_off < align_off || orig_end > align_off + align_alen)
2137 return XFS_ERROR(EINVAL);
2139 ASSERT(orig_off >= align_off);
2140 ASSERT(orig_end <= align_off + align_alen);
2144 if (!eof && gotp->br_startoff != NULLFILEOFF)
2145 ASSERT(align_off + align_alen <= gotp->br_startoff);
2146 if (prevp->br_startoff != NULLFILEOFF)
2147 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2155 #define XFS_ALLOC_GAP_UNITS 4
2159 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2161 xfs_fsblock_t adjust; /* adjustment to block numbers */
2162 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2163 xfs_mount_t *mp; /* mount point structure */
2164 int nullfb; /* true if ap->firstblock isn't set */
2165 int rt; /* true if inode is realtime */
2167 #define ISVALID(x,y) \
2169 (x) < mp->m_sb.sb_rblocks : \
2170 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2171 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2172 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2174 mp = ap->ip->i_mount;
2175 nullfb = ap->firstblock == NULLFSBLOCK;
2176 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2177 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2179 * If allocating at eof, and there's a previous real block,
2180 * try to use its last block as our starting point.
2182 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2183 !isnullstartblock(ap->prevp->br_startblock) &&
2184 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2185 ap->prevp->br_startblock)) {
2186 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2188 * Adjust for the gap between prevp and us.
2191 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2193 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2197 * If not at eof, then compare the two neighbor blocks.
2198 * Figure out whether either one gives us a good starting point,
2199 * and pick the better one.
2201 else if (!ap->eof) {
2202 xfs_fsblock_t gotbno; /* right side block number */
2203 xfs_fsblock_t gotdiff=0; /* right side difference */
2204 xfs_fsblock_t prevbno; /* left side block number */
2205 xfs_fsblock_t prevdiff=0; /* left side difference */
2208 * If there's a previous (left) block, select a requested
2209 * start block based on it.
2211 if (ap->prevp->br_startoff != NULLFILEOFF &&
2212 !isnullstartblock(ap->prevp->br_startblock) &&
2213 (prevbno = ap->prevp->br_startblock +
2214 ap->prevp->br_blockcount) &&
2215 ISVALID(prevbno, ap->prevp->br_startblock)) {
2217 * Calculate gap to end of previous block.
2219 adjust = prevdiff = ap->off -
2220 (ap->prevp->br_startoff +
2221 ap->prevp->br_blockcount);
2223 * Figure the startblock based on the previous block's
2224 * end and the gap size.
2226 * If the gap is large relative to the piece we're
2227 * allocating, or using it gives us an invalid block
2228 * number, then just use the end of the previous block.
2230 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2231 ISVALID(prevbno + prevdiff,
2232 ap->prevp->br_startblock))
2237 * If the firstblock forbids it, can't use it,
2240 if (!rt && !nullfb &&
2241 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2242 prevbno = NULLFSBLOCK;
2245 * No previous block or can't follow it, just default.
2248 prevbno = NULLFSBLOCK;
2250 * If there's a following (right) block, select a requested
2251 * start block based on it.
2253 if (!isnullstartblock(ap->gotp->br_startblock)) {
2255 * Calculate gap to start of next block.
2257 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2259 * Figure the startblock based on the next block's
2260 * start and the gap size.
2262 gotbno = ap->gotp->br_startblock;
2265 * If the gap is large relative to the piece we're
2266 * allocating, or using it gives us an invalid block
2267 * number, then just use the start of the next block
2268 * offset by our length.
2270 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2271 ISVALID(gotbno - gotdiff, gotbno))
2273 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2275 gotdiff += adjust - ap->alen;
2279 * If the firstblock forbids it, can't use it,
2282 if (!rt && !nullfb &&
2283 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2284 gotbno = NULLFSBLOCK;
2287 * No next block, just default.
2290 gotbno = NULLFSBLOCK;
2292 * If both valid, pick the better one, else the only good
2293 * one, else ap->rval is already set (to 0 or the inode block).
2295 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2296 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2297 else if (prevbno != NULLFSBLOCK)
2299 else if (gotbno != NULLFSBLOCK)
2307 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2309 xfs_alloctype_t atype = 0; /* type for allocation routines */
2310 int error; /* error return value */
2311 xfs_mount_t *mp; /* mount point structure */
2312 xfs_extlen_t prod = 0; /* product factor for allocators */
2313 xfs_extlen_t ralen = 0; /* realtime allocation length */
2314 xfs_extlen_t align; /* minimum allocation alignment */
2317 mp = ap->ip->i_mount;
2318 align = xfs_get_extsz_hint(ap->ip);
2319 prod = align / mp->m_sb.sb_rextsize;
2320 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2321 align, 1, ap->eof, 0,
2322 ap->conv, &ap->off, &ap->alen);
2326 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2329 * If the offset & length are not perfectly aligned
2330 * then kill prod, it will just get us in trouble.
2332 if (do_mod(ap->off, align) || ap->alen % align)
2335 * Set ralen to be the actual requested length in rtextents.
2337 ralen = ap->alen / mp->m_sb.sb_rextsize;
2339 * If the old value was close enough to MAXEXTLEN that
2340 * we rounded up to it, cut it back so it's valid again.
2341 * Note that if it's a really large request (bigger than
2342 * MAXEXTLEN), we don't hear about that number, and can't
2343 * adjust the starting point to match it.
2345 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2346 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2349 * Lock out other modifications to the RT bitmap inode.
2351 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2352 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2355 * If it's an allocation to an empty file at offset 0,
2356 * pick an extent that will space things out in the rt area.
2358 if (ap->eof && ap->off == 0) {
2359 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2361 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2364 ap->rval = rtx * mp->m_sb.sb_rextsize;
2369 xfs_bmap_adjacent(ap);
2372 * Realtime allocation, done through xfs_rtallocate_extent.
2374 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2375 do_div(ap->rval, mp->m_sb.sb_rextsize);
2378 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2379 &ralen, atype, ap->wasdel, prod, &rtb)))
2381 if (rtb == NULLFSBLOCK && prod > 1 &&
2382 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2383 ap->alen, &ralen, atype,
2384 ap->wasdel, 1, &rtb)))
2387 if (ap->rval != NULLFSBLOCK) {
2388 ap->rval *= mp->m_sb.sb_rextsize;
2389 ralen *= mp->m_sb.sb_rextsize;
2391 ap->ip->i_d.di_nblocks += ralen;
2392 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2394 ap->ip->i_delayed_blks -= ralen;
2396 * Adjust the disk quota also. This was reserved
2399 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2400 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2401 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2409 xfs_bmap_btalloc_nullfb(
2410 struct xfs_bmalloca *ap,
2411 struct xfs_alloc_arg *args,
2414 struct xfs_mount *mp = ap->ip->i_mount;
2415 struct xfs_perag *pag;
2416 xfs_agnumber_t ag, startag;
2420 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2421 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2423 args->type = XFS_ALLOCTYPE_START_BNO;
2424 args->total = ap->total;
2427 * Search for an allocation group with a single extent large enough
2428 * for the request. If one isn't found, then adjust the minimum
2429 * allocation size to the largest space found.
2431 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2432 if (startag == NULLAGNUMBER)
2435 pag = xfs_perag_get(mp, ag);
2436 while (*blen < args->maxlen) {
2437 if (!pag->pagf_init) {
2438 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2439 XFS_ALLOC_FLAG_TRYLOCK);
2447 * See xfs_alloc_fix_freelist...
2449 if (pag->pagf_init) {
2450 xfs_extlen_t longest;
2451 longest = xfs_alloc_longest_free_extent(mp, pag);
2452 if (*blen < longest)
2457 if (xfs_inode_is_filestream(ap->ip)) {
2458 if (*blen >= args->maxlen)
2463 * If startag is an invalid AG, we've
2464 * come here once before and
2465 * xfs_filestream_new_ag picked the
2466 * best currently available.
2468 * Don't continue looping, since we
2469 * could loop forever.
2471 if (startag == NULLAGNUMBER)
2474 error = xfs_filestream_new_ag(ap, &ag);
2479 /* loop again to set 'blen'*/
2480 startag = NULLAGNUMBER;
2481 pag = xfs_perag_get(mp, ag);
2485 if (++ag == mp->m_sb.sb_agcount)
2490 pag = xfs_perag_get(mp, ag);
2495 * Since the above loop did a BUF_TRYLOCK, it is
2496 * possible that there is space for this request.
2498 if (notinit || *blen < ap->minlen)
2499 args->minlen = ap->minlen;
2501 * If the best seen length is less than the request
2502 * length, use the best as the minimum.
2504 else if (*blen < args->maxlen)
2505 args->minlen = *blen;
2507 * Otherwise we've seen an extent as big as maxlen,
2508 * use that as the minimum.
2511 args->minlen = args->maxlen;
2514 * set the failure fallback case to look in the selected
2515 * AG as the stream may have moved.
2517 if (xfs_inode_is_filestream(ap->ip))
2518 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2525 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2527 xfs_mount_t *mp; /* mount point structure */
2528 xfs_alloctype_t atype = 0; /* type for allocation routines */
2529 xfs_extlen_t align; /* minimum allocation alignment */
2530 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2532 xfs_alloc_arg_t args;
2534 xfs_extlen_t nextminlen = 0;
2535 int nullfb; /* true if ap->firstblock isn't set */
2540 mp = ap->ip->i_mount;
2541 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2542 if (unlikely(align)) {
2543 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2544 align, 0, ap->eof, 0, ap->conv,
2545 &ap->off, &ap->alen);
2549 nullfb = ap->firstblock == NULLFSBLOCK;
2550 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2552 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2553 ag = xfs_filestream_lookup_ag(ap->ip);
2554 ag = (ag != NULLAGNUMBER) ? ag : 0;
2555 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2557 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2560 ap->rval = ap->firstblock;
2562 xfs_bmap_adjacent(ap);
2565 * If allowed, use ap->rval; otherwise must use firstblock since
2566 * it's in the right allocation group.
2568 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2571 ap->rval = ap->firstblock;
2573 * Normal allocation, done through xfs_alloc_vextent.
2575 tryagain = isaligned = 0;
2578 args.fsbno = ap->rval;
2580 /* Trim the allocation back to the maximum an AG can fit. */
2581 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
2582 args.firstblock = ap->firstblock;
2585 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2588 } else if (ap->low) {
2589 if (xfs_inode_is_filestream(ap->ip))
2590 args.type = XFS_ALLOCTYPE_FIRST_AG;
2592 args.type = XFS_ALLOCTYPE_START_BNO;
2593 args.total = args.minlen = ap->minlen;
2595 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2596 args.total = ap->total;
2597 args.minlen = ap->minlen;
2599 /* apply extent size hints if obtained earlier */
2600 if (unlikely(align)) {
2602 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2603 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2604 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2608 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2609 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2610 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2613 * If we are not low on available data blocks, and the
2614 * underlying logical volume manager is a stripe, and
2615 * the file offset is zero then try to allocate data
2616 * blocks on stripe unit boundary.
2617 * NOTE: ap->aeof is only set if the allocation length
2618 * is >= the stripe unit and the allocation offset is
2619 * at the end of file.
2621 if (!ap->low && ap->aeof) {
2623 args.alignment = mp->m_dalign;
2627 * Adjust for alignment
2629 if (blen > args.alignment && blen <= args.maxlen)
2630 args.minlen = blen - args.alignment;
2631 args.minalignslop = 0;
2634 * First try an exact bno allocation.
2635 * If it fails then do a near or start bno
2636 * allocation with alignment turned on.
2640 args.type = XFS_ALLOCTYPE_THIS_BNO;
2643 * Compute the minlen+alignment for the
2644 * next case. Set slop so that the value
2645 * of minlen+alignment+slop doesn't go up
2646 * between the calls.
2648 if (blen > mp->m_dalign && blen <= args.maxlen)
2649 nextminlen = blen - mp->m_dalign;
2651 nextminlen = args.minlen;
2652 if (nextminlen + mp->m_dalign > args.minlen + 1)
2654 nextminlen + mp->m_dalign -
2657 args.minalignslop = 0;
2661 args.minalignslop = 0;
2663 args.minleft = ap->minleft;
2664 args.wasdel = ap->wasdel;
2666 args.userdata = ap->userdata;
2667 if ((error = xfs_alloc_vextent(&args)))
2669 if (tryagain && args.fsbno == NULLFSBLOCK) {
2671 * Exact allocation failed. Now try with alignment
2675 args.fsbno = ap->rval;
2676 args.alignment = mp->m_dalign;
2677 args.minlen = nextminlen;
2678 args.minalignslop = 0;
2680 if ((error = xfs_alloc_vextent(&args)))
2683 if (isaligned && args.fsbno == NULLFSBLOCK) {
2685 * allocation failed, so turn off alignment and
2689 args.fsbno = ap->rval;
2691 if ((error = xfs_alloc_vextent(&args)))
2694 if (args.fsbno == NULLFSBLOCK && nullfb &&
2695 args.minlen > ap->minlen) {
2696 args.minlen = ap->minlen;
2697 args.type = XFS_ALLOCTYPE_START_BNO;
2698 args.fsbno = ap->rval;
2699 if ((error = xfs_alloc_vextent(&args)))
2702 if (args.fsbno == NULLFSBLOCK && nullfb) {
2704 args.type = XFS_ALLOCTYPE_FIRST_AG;
2705 args.total = ap->minlen;
2707 if ((error = xfs_alloc_vextent(&args)))
2711 if (args.fsbno != NULLFSBLOCK) {
2712 ap->firstblock = ap->rval = args.fsbno;
2713 ASSERT(nullfb || fb_agno == args.agno ||
2714 (ap->low && fb_agno < args.agno));
2715 ap->alen = args.len;
2716 ap->ip->i_d.di_nblocks += args.len;
2717 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2719 ap->ip->i_delayed_blks -= args.len;
2721 * Adjust the disk quota also. This was reserved
2724 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2725 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2726 XFS_TRANS_DQ_BCOUNT,
2729 ap->rval = NULLFSBLOCK;
2736 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2737 * It figures out where to ask the underlying allocator to put the new extent.
2741 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2743 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2744 return xfs_bmap_rtalloc(ap);
2745 return xfs_bmap_btalloc(ap);
2749 * Transform a btree format file with only one leaf node, where the
2750 * extents list will fit in the inode, into an extents format file.
2751 * Since the file extents are already in-core, all we have to do is
2752 * give up the space for the btree root and pitch the leaf block.
2754 STATIC int /* error */
2755 xfs_bmap_btree_to_extents(
2756 xfs_trans_t *tp, /* transaction pointer */
2757 xfs_inode_t *ip, /* incore inode pointer */
2758 xfs_btree_cur_t *cur, /* btree cursor */
2759 int *logflagsp, /* inode logging flags */
2760 int whichfork) /* data or attr fork */
2763 struct xfs_btree_block *cblock;/* child btree block */
2764 xfs_fsblock_t cbno; /* child block number */
2765 xfs_buf_t *cbp; /* child block's buffer */
2766 int error; /* error return value */
2767 xfs_ifork_t *ifp; /* inode fork data */
2768 xfs_mount_t *mp; /* mount point structure */
2769 __be64 *pp; /* ptr to block address */
2770 struct xfs_btree_block *rblock;/* root btree block */
2773 ifp = XFS_IFORK_PTR(ip, whichfork);
2774 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2775 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2776 rblock = ifp->if_broot;
2777 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2778 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2779 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2780 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2781 cbno = be64_to_cpu(*pp);
2784 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2787 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2788 XFS_BMAP_BTREE_REF)))
2790 cblock = XFS_BUF_TO_BLOCK(cbp);
2791 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2793 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2794 ip->i_d.di_nblocks--;
2795 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2796 xfs_trans_binval(tp, cbp);
2797 if (cur->bc_bufs[0] == cbp)
2798 cur->bc_bufs[0] = NULL;
2799 xfs_iroot_realloc(ip, -1, whichfork);
2800 ASSERT(ifp->if_broot == NULL);
2801 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2802 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2803 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2808 * Called by xfs_bmapi to update file extent records and the btree
2809 * after removing space (or undoing a delayed allocation).
2811 STATIC int /* error */
2812 xfs_bmap_del_extent(
2813 xfs_inode_t *ip, /* incore inode pointer */
2814 xfs_trans_t *tp, /* current transaction pointer */
2815 xfs_extnum_t idx, /* extent number to update/delete */
2816 xfs_bmap_free_t *flist, /* list of extents to be freed */
2817 xfs_btree_cur_t *cur, /* if null, not a btree */
2818 xfs_bmbt_irec_t *del, /* data to remove from extents */
2819 int *logflagsp, /* inode logging flags */
2820 int whichfork) /* data or attr fork */
2822 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2823 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2824 xfs_fsblock_t del_endblock=0; /* first block past del */
2825 xfs_fileoff_t del_endoff; /* first offset past del */
2826 int delay; /* current block is delayed allocated */
2827 int do_fx; /* free extent at end of routine */
2828 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2829 int error; /* error return value */
2830 int flags; /* inode logging flags */
2831 xfs_bmbt_irec_t got; /* current extent entry */
2832 xfs_fileoff_t got_endoff; /* first offset past got */
2833 int i; /* temp state */
2834 xfs_ifork_t *ifp; /* inode fork pointer */
2835 xfs_mount_t *mp; /* mount structure */
2836 xfs_filblks_t nblks; /* quota/sb block count */
2837 xfs_bmbt_irec_t new; /* new record to be inserted */
2839 uint qfield; /* quota field to update */
2840 xfs_filblks_t temp; /* for indirect length calculations */
2841 xfs_filblks_t temp2; /* for indirect length calculations */
2844 XFS_STATS_INC(xs_del_exlist);
2846 if (whichfork == XFS_ATTR_FORK)
2847 state |= BMAP_ATTRFORK;
2850 ifp = XFS_IFORK_PTR(ip, whichfork);
2851 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2852 (uint)sizeof(xfs_bmbt_rec_t)));
2853 ASSERT(del->br_blockcount > 0);
2854 ep = xfs_iext_get_ext(ifp, idx);
2855 xfs_bmbt_get_all(ep, &got);
2856 ASSERT(got.br_startoff <= del->br_startoff);
2857 del_endoff = del->br_startoff + del->br_blockcount;
2858 got_endoff = got.br_startoff + got.br_blockcount;
2859 ASSERT(got_endoff >= del_endoff);
2860 delay = isnullstartblock(got.br_startblock);
2861 ASSERT(isnullstartblock(del->br_startblock) == delay);
2866 * If deleting a real allocation, must free up the disk space.
2869 flags = XFS_ILOG_CORE;
2871 * Realtime allocation. Free it and record di_nblocks update.
2873 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2877 ASSERT(do_mod(del->br_blockcount,
2878 mp->m_sb.sb_rextsize) == 0);
2879 ASSERT(do_mod(del->br_startblock,
2880 mp->m_sb.sb_rextsize) == 0);
2881 bno = del->br_startblock;
2882 len = del->br_blockcount;
2883 do_div(bno, mp->m_sb.sb_rextsize);
2884 do_div(len, mp->m_sb.sb_rextsize);
2885 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2886 (xfs_extlen_t)len)))
2889 nblks = len * mp->m_sb.sb_rextsize;
2890 qfield = XFS_TRANS_DQ_RTBCOUNT;
2893 * Ordinary allocation.
2897 nblks = del->br_blockcount;
2898 qfield = XFS_TRANS_DQ_BCOUNT;
2901 * Set up del_endblock and cur for later.
2903 del_endblock = del->br_startblock + del->br_blockcount;
2905 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2906 got.br_startblock, got.br_blockcount,
2909 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2911 da_old = da_new = 0;
2913 da_old = startblockval(got.br_startblock);
2919 * Set flag value to use in switch statement.
2920 * Left-contig is 2, right-contig is 1.
2922 switch (((got.br_startoff == del->br_startoff) << 1) |
2923 (got_endoff == del_endoff)) {
2926 * Matches the whole extent. Delete the entry.
2928 xfs_iext_remove(ip, idx, 1,
2929 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2930 ifp->if_lastex = idx;
2933 XFS_IFORK_NEXT_SET(ip, whichfork,
2934 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2935 flags |= XFS_ILOG_CORE;
2937 flags |= xfs_ilog_fext(whichfork);
2940 if ((error = xfs_btree_delete(cur, &i)))
2942 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2947 * Deleting the first part of the extent.
2949 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2950 xfs_bmbt_set_startoff(ep, del_endoff);
2951 temp = got.br_blockcount - del->br_blockcount;
2952 xfs_bmbt_set_blockcount(ep, temp);
2953 ifp->if_lastex = idx;
2955 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2957 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2958 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2962 xfs_bmbt_set_startblock(ep, del_endblock);
2963 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2965 flags |= xfs_ilog_fext(whichfork);
2968 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2969 got.br_blockcount - del->br_blockcount,
2976 * Deleting the last part of the extent.
2978 temp = got.br_blockcount - del->br_blockcount;
2979 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2980 xfs_bmbt_set_blockcount(ep, temp);
2981 ifp->if_lastex = idx;
2983 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2985 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2986 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2990 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2992 flags |= xfs_ilog_fext(whichfork);
2995 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2997 got.br_blockcount - del->br_blockcount,
3004 * Deleting the middle of the extent.
3006 temp = del->br_startoff - got.br_startoff;
3007 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3008 xfs_bmbt_set_blockcount(ep, temp);
3009 new.br_startoff = del_endoff;
3010 temp2 = got_endoff - del_endoff;
3011 new.br_blockcount = temp2;
3012 new.br_state = got.br_state;
3014 new.br_startblock = del_endblock;
3015 flags |= XFS_ILOG_CORE;
3017 if ((error = xfs_bmbt_update(cur,
3019 got.br_startblock, temp,
3022 if ((error = xfs_btree_increment(cur, 0, &i)))
3024 cur->bc_rec.b = new;
3025 error = xfs_btree_insert(cur, &i);
3026 if (error && error != ENOSPC)
3029 * If get no-space back from btree insert,
3030 * it tried a split, and we have a zero
3031 * block reservation.
3032 * Fix up our state and return the error.
3034 if (error == ENOSPC) {
3036 * Reset the cursor, don't trust
3037 * it after any insert operation.
3039 if ((error = xfs_bmbt_lookup_eq(cur,
3044 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3046 * Update the btree record back
3047 * to the original value.
3049 if ((error = xfs_bmbt_update(cur,
3056 * Reset the extent record back
3057 * to the original value.
3059 xfs_bmbt_set_blockcount(ep,
3062 error = XFS_ERROR(ENOSPC);
3065 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3067 flags |= xfs_ilog_fext(whichfork);
3068 XFS_IFORK_NEXT_SET(ip, whichfork,
3069 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3071 ASSERT(whichfork == XFS_DATA_FORK);
3072 temp = xfs_bmap_worst_indlen(ip, temp);
3073 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3074 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3075 new.br_startblock = nullstartblock((int)temp2);
3076 da_new = temp + temp2;
3077 while (da_new > da_old) {
3081 xfs_bmbt_set_startblock(ep,
3082 nullstartblock((int)temp));
3084 if (da_new == da_old)
3090 nullstartblock((int)temp2);
3094 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3095 xfs_iext_insert(ip, idx + 1, 1, &new, state);
3096 ifp->if_lastex = idx + 1;
3100 * If we need to, add to list of extents to delete.
3103 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3106 * Adjust inode # blocks in the file.
3109 ip->i_d.di_nblocks -= nblks;
3111 * Adjust quota data.
3114 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3117 * Account for change in delayed indirect blocks.
3118 * Nothing to do for disk quota accounting here.
3120 ASSERT(da_old >= da_new);
3121 if (da_old > da_new) {
3122 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3123 (int64_t)(da_old - da_new), 0);
3131 * Remove the entry "free" from the free item list. Prev points to the
3132 * previous entry, unless "free" is the head of the list.
3136 xfs_bmap_free_t *flist, /* free item list header */
3137 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3138 xfs_bmap_free_item_t *free) /* list item to be freed */
3141 prev->xbfi_next = free->xbfi_next;
3143 flist->xbf_first = free->xbfi_next;
3145 kmem_zone_free(xfs_bmap_free_item_zone, free);
3149 * Convert an extents-format file into a btree-format file.
3150 * The new file will have a root block (in the inode) and a single child block.
3152 STATIC int /* error */
3153 xfs_bmap_extents_to_btree(
3154 xfs_trans_t *tp, /* transaction pointer */
3155 xfs_inode_t *ip, /* incore inode pointer */
3156 xfs_fsblock_t *firstblock, /* first-block-allocated */
3157 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3158 xfs_btree_cur_t **curp, /* cursor returned to caller */
3159 int wasdel, /* converting a delayed alloc */
3160 int *logflagsp, /* inode logging flags */
3161 int whichfork) /* data or attr fork */
3163 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3164 xfs_buf_t *abp; /* buffer for ablock */
3165 xfs_alloc_arg_t args; /* allocation arguments */
3166 xfs_bmbt_rec_t *arp; /* child record pointer */
3167 struct xfs_btree_block *block; /* btree root block */
3168 xfs_btree_cur_t *cur; /* bmap btree cursor */
3169 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3170 int error; /* error return value */
3171 xfs_extnum_t i, cnt; /* extent record index */
3172 xfs_ifork_t *ifp; /* inode fork pointer */
3173 xfs_bmbt_key_t *kp; /* root block key pointer */
3174 xfs_mount_t *mp; /* mount structure */
3175 xfs_extnum_t nextents; /* number of file extents */
3176 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3178 ifp = XFS_IFORK_PTR(ip, whichfork);
3179 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3180 ASSERT(ifp->if_ext_max ==
3181 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3183 * Make space in the inode incore.
3185 xfs_iroot_realloc(ip, 1, whichfork);
3186 ifp->if_flags |= XFS_IFBROOT;
3191 block = ifp->if_broot;
3192 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3193 block->bb_level = cpu_to_be16(1);
3194 block->bb_numrecs = cpu_to_be16(1);
3195 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3196 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3199 * Need a cursor. Can't allocate until bb_level is filled in.
3202 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3203 cur->bc_private.b.firstblock = *firstblock;
3204 cur->bc_private.b.flist = flist;
3205 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3207 * Convert to a btree with two levels, one record in root.
3209 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3212 args.firstblock = *firstblock;
3213 if (*firstblock == NULLFSBLOCK) {
3214 args.type = XFS_ALLOCTYPE_START_BNO;
3215 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3216 } else if (flist->xbf_low) {
3217 args.type = XFS_ALLOCTYPE_START_BNO;
3218 args.fsbno = *firstblock;
3220 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3221 args.fsbno = *firstblock;
3223 args.minlen = args.maxlen = args.prod = 1;
3224 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3225 args.minalignslop = 0;
3226 args.wasdel = wasdel;
3228 if ((error = xfs_alloc_vextent(&args))) {
3229 xfs_iroot_realloc(ip, -1, whichfork);
3230 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3234 * Allocation can't fail, the space was reserved.
3236 ASSERT(args.fsbno != NULLFSBLOCK);
3237 ASSERT(*firstblock == NULLFSBLOCK ||
3238 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3240 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3241 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3242 cur->bc_private.b.allocated++;
3243 ip->i_d.di_nblocks++;
3244 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3245 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3247 * Fill in the child block.
3249 ablock = XFS_BUF_TO_BLOCK(abp);
3250 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3251 ablock->bb_level = 0;
3252 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3253 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3254 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3255 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3256 for (cnt = i = 0; i < nextents; i++) {
3257 ep = xfs_iext_get_ext(ifp, i);
3258 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3259 arp->l0 = cpu_to_be64(ep->l0);
3260 arp->l1 = cpu_to_be64(ep->l1);
3264 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3265 xfs_btree_set_numrecs(ablock, cnt);
3268 * Fill in the root key and pointer.
3270 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3271 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3272 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3273 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3274 be16_to_cpu(block->bb_level)));
3275 *pp = cpu_to_be64(args.fsbno);
3278 * Do all this logging at the end so that
3279 * the root is at the right level.
3281 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3282 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3283 ASSERT(*curp == NULL);
3285 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3290 * Calculate the default attribute fork offset for newly created inodes.
3293 xfs_default_attroffset(
3294 struct xfs_inode *ip)
3296 struct xfs_mount *mp = ip->i_mount;
3299 if (mp->m_sb.sb_inodesize == 256) {
3300 offset = XFS_LITINO(mp) -
3301 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3303 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3306 ASSERT(offset < XFS_LITINO(mp));
3311 * Helper routine to reset inode di_forkoff field when switching
3312 * attribute fork from local to extent format - we reset it where
3313 * possible to make space available for inline data fork extents.
3316 xfs_bmap_forkoff_reset(
3321 if (whichfork == XFS_ATTR_FORK &&
3322 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3323 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3324 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3325 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3327 if (dfl_forkoff > ip->i_d.di_forkoff) {
3328 ip->i_d.di_forkoff = dfl_forkoff;
3329 ip->i_df.if_ext_max =
3330 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3331 ip->i_afp->if_ext_max =
3332 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3338 * Convert a local file to an extents file.
3339 * This code is out of bounds for data forks of regular files,
3340 * since the file data needs to get logged so things will stay consistent.
3341 * (The bmap-level manipulations are ok, though).
3343 STATIC int /* error */
3344 xfs_bmap_local_to_extents(
3345 xfs_trans_t *tp, /* transaction pointer */
3346 xfs_inode_t *ip, /* incore inode pointer */
3347 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3348 xfs_extlen_t total, /* total blocks needed by transaction */
3349 int *logflagsp, /* inode logging flags */
3350 int whichfork) /* data or attr fork */
3352 int error; /* error return value */
3353 int flags; /* logging flags returned */
3354 xfs_ifork_t *ifp; /* inode fork pointer */
3357 * We don't want to deal with the case of keeping inode data inline yet.
3358 * So sending the data fork of a regular inode is invalid.
3360 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3361 whichfork == XFS_DATA_FORK));
3362 ifp = XFS_IFORK_PTR(ip, whichfork);
3363 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3366 if (ifp->if_bytes) {
3367 xfs_alloc_arg_t args; /* allocation arguments */
3368 xfs_buf_t *bp; /* buffer for extent block */
3369 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3372 args.mp = ip->i_mount;
3373 args.firstblock = *firstblock;
3374 ASSERT((ifp->if_flags &
3375 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3377 * Allocate a block. We know we need only one, since the
3378 * file currently fits in an inode.
3380 if (*firstblock == NULLFSBLOCK) {
3381 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3382 args.type = XFS_ALLOCTYPE_START_BNO;
3384 args.fsbno = *firstblock;
3385 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3388 args.mod = args.minleft = args.alignment = args.wasdel =
3389 args.isfl = args.minalignslop = 0;
3390 args.minlen = args.maxlen = args.prod = 1;
3391 if ((error = xfs_alloc_vextent(&args)))
3394 * Can't fail, the space was reserved.
3396 ASSERT(args.fsbno != NULLFSBLOCK);
3397 ASSERT(args.len == 1);
3398 *firstblock = args.fsbno;
3399 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3400 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3402 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3403 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3404 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3405 xfs_iext_add(ifp, 0, 1);
3406 ep = xfs_iext_get_ext(ifp, 0);
3407 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3408 trace_xfs_bmap_post_update(ip, 0,
3409 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3411 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3412 ip->i_d.di_nblocks = 1;
3413 xfs_trans_mod_dquot_byino(tp, ip,
3414 XFS_TRANS_DQ_BCOUNT, 1L);
3415 flags |= xfs_ilog_fext(whichfork);
3417 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3418 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3420 ifp->if_flags &= ~XFS_IFINLINE;
3421 ifp->if_flags |= XFS_IFEXTENTS;
3422 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3423 flags |= XFS_ILOG_CORE;
3430 * Search the extent records for the entry containing block bno.
3431 * If bno lies in a hole, point to the next entry. If bno lies
3432 * past eof, *eofp will be set, and *prevp will contain the last
3433 * entry (null if none). Else, *lastxp will be set to the index
3434 * of the found entry; *gotp will contain the entry.
3436 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3437 xfs_bmap_search_multi_extents(
3438 xfs_ifork_t *ifp, /* inode fork pointer */
3439 xfs_fileoff_t bno, /* block number searched for */
3440 int *eofp, /* out: end of file found */
3441 xfs_extnum_t *lastxp, /* out: last extent index */
3442 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3443 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3445 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3446 xfs_extnum_t lastx; /* last extent index */
3449 * Initialize the extent entry structure to catch access to
3450 * uninitialized br_startblock field.
3452 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3453 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3454 gotp->br_state = XFS_EXT_INVALID;
3456 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3458 gotp->br_startblock = 0xffffa5a5;
3460 prevp->br_startoff = NULLFILEOFF;
3462 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3464 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3466 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3467 xfs_bmbt_get_all(ep, gotp);
3481 * Search the extents list for the inode, for the extent containing bno.
3482 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3483 * *eofp will be set, and *prevp will contain the last entry (null if none).
3484 * Else, *lastxp will be set to the index of the found
3485 * entry; *gotp will contain the entry.
3487 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3488 xfs_bmap_search_extents(
3489 xfs_inode_t *ip, /* incore inode pointer */
3490 xfs_fileoff_t bno, /* block number searched for */
3491 int fork, /* data or attr fork */
3492 int *eofp, /* out: end of file found */
3493 xfs_extnum_t *lastxp, /* out: last extent index */
3494 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3495 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3497 xfs_ifork_t *ifp; /* inode fork pointer */
3498 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3500 XFS_STATS_INC(xs_look_exlist);
3501 ifp = XFS_IFORK_PTR(ip, fork);
3503 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3505 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3506 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3507 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
3508 "Access to block zero in inode %llu "
3509 "start_block: %llx start_off: %llx "
3510 "blkcnt: %llx extent-state: %x lastx: %x\n",
3511 (unsigned long long)ip->i_ino,
3512 (unsigned long long)gotp->br_startblock,
3513 (unsigned long long)gotp->br_startoff,
3514 (unsigned long long)gotp->br_blockcount,
3515 gotp->br_state, *lastxp);
3516 *lastxp = NULLEXTNUM;
3524 * Compute the worst-case number of indirect blocks that will be used
3525 * for ip's delayed extent of length "len".
3527 STATIC xfs_filblks_t
3528 xfs_bmap_worst_indlen(
3529 xfs_inode_t *ip, /* incore inode pointer */
3530 xfs_filblks_t len) /* delayed extent length */
3532 int level; /* btree level number */
3533 int maxrecs; /* maximum record count at this level */
3534 xfs_mount_t *mp; /* mount structure */
3535 xfs_filblks_t rval; /* return value */
3538 maxrecs = mp->m_bmap_dmxr[0];
3539 for (level = 0, rval = 0;
3540 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3543 do_div(len, maxrecs);
3546 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3549 maxrecs = mp->m_bmap_dmxr[1];
3555 * Convert inode from non-attributed to attributed.
3556 * Must not be in a transaction, ip must not be locked.
3558 int /* error code */
3559 xfs_bmap_add_attrfork(
3560 xfs_inode_t *ip, /* incore inode pointer */
3561 int size, /* space new attribute needs */
3562 int rsvd) /* xact may use reserved blks */
3564 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3565 xfs_bmap_free_t flist; /* freed extent records */
3566 xfs_mount_t *mp; /* mount structure */
3567 xfs_trans_t *tp; /* transaction pointer */
3568 int blks; /* space reservation */
3569 int version = 1; /* superblock attr version */
3570 int committed; /* xaction was committed */
3571 int logflags; /* logging flags */
3572 int error; /* error return value */
3574 ASSERT(XFS_IFORK_Q(ip) == 0);
3575 ASSERT(ip->i_df.if_ext_max ==
3576 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3579 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3580 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3581 blks = XFS_ADDAFORK_SPACE_RES(mp);
3583 tp->t_flags |= XFS_TRANS_RESERVE;
3584 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3585 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3587 xfs_ilock(ip, XFS_ILOCK_EXCL);
3588 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3589 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3590 XFS_QMOPT_RES_REGBLKS);
3592 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3593 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3596 if (XFS_IFORK_Q(ip))
3598 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3600 * For inodes coming from pre-6.2 filesystems.
3602 ASSERT(ip->i_d.di_aformat == 0);
3603 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3605 ASSERT(ip->i_d.di_anextents == 0);
3607 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3608 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3610 switch (ip->i_d.di_format) {
3611 case XFS_DINODE_FMT_DEV:
3612 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3614 case XFS_DINODE_FMT_UUID:
3615 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3617 case XFS_DINODE_FMT_LOCAL:
3618 case XFS_DINODE_FMT_EXTENTS:
3619 case XFS_DINODE_FMT_BTREE:
3620 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3621 if (!ip->i_d.di_forkoff)
3622 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3623 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3628 error = XFS_ERROR(EINVAL);
3631 ip->i_df.if_ext_max =
3632 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3633 ASSERT(ip->i_afp == NULL);
3634 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3635 ip->i_afp->if_ext_max =
3636 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3637 ip->i_afp->if_flags = XFS_IFEXTENTS;
3639 xfs_bmap_init(&flist, &firstblock);
3640 switch (ip->i_d.di_format) {
3641 case XFS_DINODE_FMT_LOCAL:
3642 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3645 case XFS_DINODE_FMT_EXTENTS:
3646 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3649 case XFS_DINODE_FMT_BTREE:
3650 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3658 xfs_trans_log_inode(tp, ip, logflags);
3661 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3662 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3663 __int64_t sbfields = 0;
3665 spin_lock(&mp->m_sb_lock);
3666 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3667 xfs_sb_version_addattr(&mp->m_sb);
3668 sbfields |= XFS_SB_VERSIONNUM;
3670 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3671 xfs_sb_version_addattr2(&mp->m_sb);
3672 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3675 spin_unlock(&mp->m_sb_lock);
3676 xfs_mod_sb(tp, sbfields);
3678 spin_unlock(&mp->m_sb_lock);
3680 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3682 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3683 ASSERT(ip->i_df.if_ext_max ==
3684 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3687 xfs_bmap_cancel(&flist);
3689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3691 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3692 ASSERT(ip->i_df.if_ext_max ==
3693 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3698 * Add the extent to the list of extents to be free at transaction end.
3699 * The list is maintained sorted (by block number).
3704 xfs_fsblock_t bno, /* fs block number of extent */
3705 xfs_filblks_t len, /* length of extent */
3706 xfs_bmap_free_t *flist, /* list of extents */
3707 xfs_mount_t *mp) /* mount point structure */
3709 xfs_bmap_free_item_t *cur; /* current (next) element */
3710 xfs_bmap_free_item_t *new; /* new element */
3711 xfs_bmap_free_item_t *prev; /* previous element */
3713 xfs_agnumber_t agno;
3714 xfs_agblock_t agbno;
3716 ASSERT(bno != NULLFSBLOCK);
3718 ASSERT(len <= MAXEXTLEN);
3719 ASSERT(!isnullstartblock(bno));
3720 agno = XFS_FSB_TO_AGNO(mp, bno);
3721 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3722 ASSERT(agno < mp->m_sb.sb_agcount);
3723 ASSERT(agbno < mp->m_sb.sb_agblocks);
3724 ASSERT(len < mp->m_sb.sb_agblocks);
3725 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3727 ASSERT(xfs_bmap_free_item_zone != NULL);
3728 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3729 new->xbfi_startblock = bno;
3730 new->xbfi_blockcount = (xfs_extlen_t)len;
3731 for (prev = NULL, cur = flist->xbf_first;
3733 prev = cur, cur = cur->xbfi_next) {
3734 if (cur->xbfi_startblock >= bno)
3738 prev->xbfi_next = new;
3740 flist->xbf_first = new;
3741 new->xbfi_next = cur;
3746 * Compute and fill in the value of the maximum depth of a bmap btree
3747 * in this filesystem. Done once, during mount.
3750 xfs_bmap_compute_maxlevels(
3751 xfs_mount_t *mp, /* file system mount structure */
3752 int whichfork) /* data or attr fork */
3754 int level; /* btree level */
3755 uint maxblocks; /* max blocks at this level */
3756 uint maxleafents; /* max leaf entries possible */
3757 int maxrootrecs; /* max records in root block */
3758 int minleafrecs; /* min records in leaf block */
3759 int minnoderecs; /* min records in node block */
3760 int sz; /* root block size */
3763 * The maximum number of extents in a file, hence the maximum
3764 * number of leaf entries, is controlled by the type of di_nextents
3765 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3766 * (a signed 16-bit number, xfs_aextnum_t).
3768 * Note that we can no longer assume that if we are in ATTR1 that
3769 * the fork offset of all the inodes will be
3770 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3771 * with ATTR2 and then mounted back with ATTR1, keeping the
3772 * di_forkoff's fixed but probably at various positions. Therefore,
3773 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3774 * of a minimum size available.
3776 if (whichfork == XFS_DATA_FORK) {
3777 maxleafents = MAXEXTNUM;
3778 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3780 maxleafents = MAXAEXTNUM;
3781 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3783 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3784 minleafrecs = mp->m_bmap_dmnr[0];
3785 minnoderecs = mp->m_bmap_dmnr[1];
3786 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3787 for (level = 1; maxblocks > 1; level++) {
3788 if (maxblocks <= maxrootrecs)
3791 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3793 mp->m_bm_maxlevels[whichfork] = level;
3797 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3798 * caller. Frees all the extents that need freeing, which must be done
3799 * last due to locking considerations. We never free any extents in
3800 * the first transaction. This is to allow the caller to make the first
3801 * transaction a synchronous one so that the pointers to the data being
3802 * broken in this transaction will be permanent before the data is actually
3803 * freed. This is necessary to prevent blocks from being reallocated
3804 * and written to before the free and reallocation are actually permanent.
3805 * We do not just make the first transaction synchronous here, because
3806 * there are more efficient ways to gain the same protection in some cases
3807 * (see the file truncation code).
3809 * Return 1 if the given transaction was committed and a new one
3810 * started, and 0 otherwise in the committed parameter.
3815 xfs_trans_t **tp, /* transaction pointer addr */
3816 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3817 int *committed) /* xact committed or not */
3819 xfs_efd_log_item_t *efd; /* extent free data */
3820 xfs_efi_log_item_t *efi; /* extent free intention */
3821 int error; /* error return value */
3822 xfs_bmap_free_item_t *free; /* free extent item */
3823 unsigned int logres; /* new log reservation */
3824 unsigned int logcount; /* new log count */
3825 xfs_mount_t *mp; /* filesystem mount structure */
3826 xfs_bmap_free_item_t *next; /* next item on free list */
3827 xfs_trans_t *ntp; /* new transaction pointer */
3829 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3830 if (flist->xbf_count == 0) {
3835 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3836 for (free = flist->xbf_first; free; free = free->xbfi_next)
3837 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3838 free->xbfi_blockcount);
3839 logres = ntp->t_log_res;
3840 logcount = ntp->t_log_count;
3841 ntp = xfs_trans_dup(*tp);
3842 error = xfs_trans_commit(*tp, 0);
3846 * We have a new transaction, so we should return committed=1,
3847 * even though we're returning an error.
3853 * transaction commit worked ok so we can drop the extra ticket
3854 * reference that we gained in xfs_trans_dup()
3856 xfs_log_ticket_put(ntp->t_ticket);
3858 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3861 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3862 for (free = flist->xbf_first; free != NULL; free = next) {
3863 next = free->xbfi_next;
3864 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3865 free->xbfi_blockcount))) {
3867 * The bmap free list will be cleaned up at a
3868 * higher level. The EFI will be canceled when
3869 * this transaction is aborted.
3870 * Need to force shutdown here to make sure it
3871 * happens, since this transaction may not be
3875 if (!XFS_FORCED_SHUTDOWN(mp))
3876 xfs_force_shutdown(mp,
3877 (error == EFSCORRUPTED) ?
3878 SHUTDOWN_CORRUPT_INCORE :
3879 SHUTDOWN_META_IO_ERROR);
3882 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3883 free->xbfi_blockcount);
3884 xfs_bmap_del_free(flist, NULL, free);
3890 * Free up any items left in the list.
3894 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3896 xfs_bmap_free_item_t *free; /* free list item */
3897 xfs_bmap_free_item_t *next;
3899 if (flist->xbf_count == 0)
3901 ASSERT(flist->xbf_first != NULL);
3902 for (free = flist->xbf_first; free; free = next) {
3903 next = free->xbfi_next;
3904 xfs_bmap_del_free(flist, NULL, free);
3906 ASSERT(flist->xbf_count == 0);
3910 * Returns the file-relative block number of the first unused block(s)
3911 * in the file with at least "len" logically contiguous blocks free.
3912 * This is the lowest-address hole if the file has holes, else the first block
3913 * past the end of file.
3914 * Return 0 if the file is currently local (in-inode).
3917 xfs_bmap_first_unused(
3918 xfs_trans_t *tp, /* transaction pointer */
3919 xfs_inode_t *ip, /* incore inode */
3920 xfs_extlen_t len, /* size of hole to find */
3921 xfs_fileoff_t *first_unused, /* unused block */
3922 int whichfork) /* data or attr fork */
3924 int error; /* error return value */
3925 int idx; /* extent record index */
3926 xfs_ifork_t *ifp; /* inode fork pointer */
3927 xfs_fileoff_t lastaddr; /* last block number seen */
3928 xfs_fileoff_t lowest; /* lowest useful block */
3929 xfs_fileoff_t max; /* starting useful block */
3930 xfs_fileoff_t off; /* offset for this block */
3931 xfs_extnum_t nextents; /* number of extent entries */
3933 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3934 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3935 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3936 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3940 ifp = XFS_IFORK_PTR(ip, whichfork);
3941 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3942 (error = xfs_iread_extents(tp, ip, whichfork)))
3944 lowest = *first_unused;
3945 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3946 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3947 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3948 off = xfs_bmbt_get_startoff(ep);
3950 * See if the hole before this extent will work.
3952 if (off >= lowest + len && off - max >= len) {
3953 *first_unused = max;
3956 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3957 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3959 *first_unused = max;
3964 * Returns the file-relative block number of the last block + 1 before
3965 * last_block (input value) in the file.
3966 * This is not based on i_size, it is based on the extent records.
3967 * Returns 0 for local files, as they do not have extent records.
3970 xfs_bmap_last_before(
3971 xfs_trans_t *tp, /* transaction pointer */
3972 xfs_inode_t *ip, /* incore inode */
3973 xfs_fileoff_t *last_block, /* last block */
3974 int whichfork) /* data or attr fork */
3976 xfs_fileoff_t bno; /* input file offset */
3977 int eof; /* hit end of file */
3978 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3979 int error; /* error return value */
3980 xfs_bmbt_irec_t got; /* current extent value */
3981 xfs_ifork_t *ifp; /* inode fork pointer */
3982 xfs_extnum_t lastx; /* last extent used */
3983 xfs_bmbt_irec_t prev; /* previous extent value */
3985 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3986 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3987 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3988 return XFS_ERROR(EIO);
3989 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3993 ifp = XFS_IFORK_PTR(ip, whichfork);
3994 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3995 (error = xfs_iread_extents(tp, ip, whichfork)))
3997 bno = *last_block - 1;
3998 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4000 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4001 if (prev.br_startoff == NULLFILEOFF)
4004 *last_block = prev.br_startoff + prev.br_blockcount;
4007 * Otherwise *last_block is already the right answer.
4013 * Returns the file-relative block number of the first block past eof in
4014 * the file. This is not based on i_size, it is based on the extent records.
4015 * Returns 0 for local files, as they do not have extent records.
4018 xfs_bmap_last_offset(
4019 xfs_trans_t *tp, /* transaction pointer */
4020 xfs_inode_t *ip, /* incore inode */
4021 xfs_fileoff_t *last_block, /* last block */
4022 int whichfork) /* data or attr fork */
4024 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4025 int error; /* error return value */
4026 xfs_ifork_t *ifp; /* inode fork pointer */
4027 xfs_extnum_t nextents; /* number of extent entries */
4029 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4030 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4031 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4032 return XFS_ERROR(EIO);
4033 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4037 ifp = XFS_IFORK_PTR(ip, whichfork);
4038 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4039 (error = xfs_iread_extents(tp, ip, whichfork)))
4041 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4046 ep = xfs_iext_get_ext(ifp, nextents - 1);
4047 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4052 * Returns whether the selected fork of the inode has exactly one
4053 * block or not. For the data fork we check this matches di_size,
4054 * implying the file's range is 0..bsize-1.
4056 int /* 1=>1 block, 0=>otherwise */
4058 xfs_inode_t *ip, /* incore inode */
4059 int whichfork) /* data or attr fork */
4061 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4062 xfs_ifork_t *ifp; /* inode fork pointer */
4063 int rval; /* return value */
4064 xfs_bmbt_irec_t s; /* internal version of extent */
4067 if (whichfork == XFS_DATA_FORK) {
4068 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4069 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4070 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4073 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4075 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4077 ifp = XFS_IFORK_PTR(ip, whichfork);
4078 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4079 ep = xfs_iext_get_ext(ifp, 0);
4080 xfs_bmbt_get_all(ep, &s);
4081 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4082 if (rval && whichfork == XFS_DATA_FORK)
4083 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4088 xfs_bmap_sanity_check(
4089 struct xfs_mount *mp,
4093 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4095 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4096 be16_to_cpu(block->bb_level) != level ||
4097 be16_to_cpu(block->bb_numrecs) == 0 ||
4098 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4104 * Read in the extents to if_extents.
4105 * All inode fields are set up by caller, we just traverse the btree
4106 * and copy the records in. If the file system cannot contain unwritten
4107 * extents, the records are checked for no "state" flags.
4110 xfs_bmap_read_extents(
4111 xfs_trans_t *tp, /* transaction pointer */
4112 xfs_inode_t *ip, /* incore inode */
4113 int whichfork) /* data or attr fork */
4115 struct xfs_btree_block *block; /* current btree block */
4116 xfs_fsblock_t bno; /* block # of "block" */
4117 xfs_buf_t *bp; /* buffer for "block" */
4118 int error; /* error return value */
4119 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4120 xfs_extnum_t i, j; /* index into the extents list */
4121 xfs_ifork_t *ifp; /* fork structure */
4122 int level; /* btree level, for checking */
4123 xfs_mount_t *mp; /* file system mount structure */
4124 __be64 *pp; /* pointer to block address */
4126 xfs_extnum_t room; /* number of entries there's room for */
4130 ifp = XFS_IFORK_PTR(ip, whichfork);
4131 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4132 XFS_EXTFMT_INODE(ip);
4133 block = ifp->if_broot;
4135 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4137 level = be16_to_cpu(block->bb_level);
4139 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4140 bno = be64_to_cpu(*pp);
4141 ASSERT(bno != NULLDFSBNO);
4142 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4143 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4145 * Go down the tree until leaf level is reached, following the first
4146 * pointer (leftmost) at each level.
4148 while (level-- > 0) {
4149 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4150 XFS_BMAP_BTREE_REF)))
4152 block = XFS_BUF_TO_BLOCK(bp);
4153 XFS_WANT_CORRUPTED_GOTO(
4154 xfs_bmap_sanity_check(mp, bp, level),
4158 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4159 bno = be64_to_cpu(*pp);
4160 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4161 xfs_trans_brelse(tp, bp);
4164 * Here with bp and block set to the leftmost leaf node in the tree.
4166 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4169 * Loop over all leaf nodes. Copy information to the extent records.
4172 xfs_bmbt_rec_t *frp;
4173 xfs_fsblock_t nextbno;
4174 xfs_extnum_t num_recs;
4178 num_recs = xfs_btree_get_numrecs(block);
4179 if (unlikely(i + num_recs > room)) {
4180 ASSERT(i + num_recs <= room);
4181 xfs_warn(ip->i_mount,
4182 "corrupt dinode %Lu, (btree extents).",
4183 (unsigned long long) ip->i_ino);
4184 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4185 XFS_ERRLEVEL_LOW, ip->i_mount, block);
4188 XFS_WANT_CORRUPTED_GOTO(
4189 xfs_bmap_sanity_check(mp, bp, 0),
4192 * Read-ahead the next leaf block, if any.
4194 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4195 if (nextbno != NULLFSBLOCK)
4196 xfs_btree_reada_bufl(mp, nextbno, 1);
4198 * Copy records into the extent records.
4200 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4202 for (j = 0; j < num_recs; j++, i++, frp++) {
4203 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4204 trp->l0 = be64_to_cpu(frp->l0);
4205 trp->l1 = be64_to_cpu(frp->l1);
4207 if (exntf == XFS_EXTFMT_NOSTATE) {
4209 * Check all attribute bmap btree records and
4210 * any "older" data bmap btree records for a
4211 * set bit in the "extent flag" position.
4213 if (unlikely(xfs_check_nostate_extents(ifp,
4214 start, num_recs))) {
4215 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4221 xfs_trans_brelse(tp, bp);
4224 * If we've reached the end, stop.
4226 if (bno == NULLFSBLOCK)
4228 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4229 XFS_BMAP_BTREE_REF)))
4231 block = XFS_BUF_TO_BLOCK(bp);
4233 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4234 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4235 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4238 xfs_trans_brelse(tp, bp);
4239 return XFS_ERROR(EFSCORRUPTED);
4244 * Add bmap trace insert entries for all the contents of the extent records.
4247 xfs_bmap_trace_exlist(
4248 xfs_inode_t *ip, /* incore inode pointer */
4249 xfs_extnum_t cnt, /* count of entries in the list */
4250 int whichfork, /* data or attr fork */
4251 unsigned long caller_ip)
4253 xfs_extnum_t idx; /* extent record index */
4254 xfs_ifork_t *ifp; /* inode fork pointer */
4257 if (whichfork == XFS_ATTR_FORK)
4258 state |= BMAP_ATTRFORK;
4260 ifp = XFS_IFORK_PTR(ip, whichfork);
4261 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4262 for (idx = 0; idx < cnt; idx++)
4263 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4267 * Validate that the bmbt_irecs being returned from bmapi are valid
4268 * given the callers original parameters. Specifically check the
4269 * ranges of the returned irecs to ensure that they only extent beyond
4270 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4273 xfs_bmap_validate_ret(
4277 xfs_bmbt_irec_t *mval,
4281 int i; /* index to map values */
4283 ASSERT(ret_nmap <= nmap);
4285 for (i = 0; i < ret_nmap; i++) {
4286 ASSERT(mval[i].br_blockcount > 0);
4287 if (!(flags & XFS_BMAPI_ENTIRE)) {
4288 ASSERT(mval[i].br_startoff >= bno);
4289 ASSERT(mval[i].br_blockcount <= len);
4290 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4293 ASSERT(mval[i].br_startoff < bno + len);
4294 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4298 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4299 mval[i].br_startoff);
4300 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4301 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4302 mval[i].br_startblock != HOLESTARTBLOCK);
4303 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4304 mval[i].br_state == XFS_EXT_UNWRITTEN);
4311 * Map file blocks to filesystem blocks.
4312 * File range is given by the bno/len pair.
4313 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4314 * into a hole or past eof.
4315 * Only allocates blocks from a single allocation group,
4316 * to avoid locking problems.
4317 * The returned value in "firstblock" from the first call in a transaction
4318 * must be remembered and presented to subsequent calls in "firstblock".
4319 * An upper bound for the number of blocks to be allocated is supplied to
4320 * the first call in "total"; if no allocation group has that many free
4321 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4325 xfs_trans_t *tp, /* transaction pointer */
4326 xfs_inode_t *ip, /* incore inode */
4327 xfs_fileoff_t bno, /* starting file offs. mapped */
4328 xfs_filblks_t len, /* length to map in file */
4329 int flags, /* XFS_BMAPI_... */
4330 xfs_fsblock_t *firstblock, /* first allocated block
4331 controls a.g. for allocs */
4332 xfs_extlen_t total, /* total blocks needed */
4333 xfs_bmbt_irec_t *mval, /* output: map values */
4334 int *nmap, /* i/o: mval size/count */
4335 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4337 xfs_fsblock_t abno; /* allocated block number */
4338 xfs_extlen_t alen; /* allocated extent length */
4339 xfs_fileoff_t aoff; /* allocated file offset */
4340 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
4341 xfs_btree_cur_t *cur; /* bmap btree cursor */
4342 xfs_fileoff_t end; /* end of mapped file region */
4343 int eof; /* we've hit the end of extents */
4344 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4345 int error; /* error return */
4346 xfs_bmbt_irec_t got; /* current file extent record */
4347 xfs_ifork_t *ifp; /* inode fork pointer */
4348 xfs_extlen_t indlen; /* indirect blocks length */
4349 xfs_extnum_t lastx; /* last useful extent number */
4350 int logflags; /* flags for transaction logging */
4351 xfs_extlen_t minleft; /* min blocks left after allocation */
4352 xfs_extlen_t minlen; /* min allocation size */
4353 xfs_mount_t *mp; /* xfs mount structure */
4354 int n; /* current extent index */
4355 int nallocs; /* number of extents alloc'd */
4356 xfs_extnum_t nextents; /* number of extents in file */
4357 xfs_fileoff_t obno; /* old block number (offset) */
4358 xfs_bmbt_irec_t prev; /* previous file extent record */
4359 int tmp_logflags; /* temp flags holder */
4360 int whichfork; /* data or attr fork */
4361 char inhole; /* current location is hole in file */
4362 char wasdelay; /* old extent was delayed */
4363 char wr; /* this is a write request */
4364 char rt; /* this is a realtime file */
4366 xfs_fileoff_t orig_bno; /* original block number value */
4367 int orig_flags; /* original flags arg value */
4368 xfs_filblks_t orig_len; /* original value of len arg */
4369 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4370 int orig_nmap; /* original value of *nmap */
4379 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4380 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4381 XFS_ATTR_FORK : XFS_DATA_FORK;
4383 if (unlikely(XFS_TEST_ERROR(
4384 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4385 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4386 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4387 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4388 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4389 return XFS_ERROR(EFSCORRUPTED);
4391 if (XFS_FORCED_SHUTDOWN(mp))
4392 return XFS_ERROR(EIO);
4393 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4394 ifp = XFS_IFORK_PTR(ip, whichfork);
4395 ASSERT(ifp->if_ext_max ==
4396 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4397 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4398 XFS_STATS_INC(xs_blk_mapw);
4400 XFS_STATS_INC(xs_blk_mapr);
4402 * IGSTATE flag is used to combine extents which
4403 * differ only due to the state of the extents.
4404 * This technique is used from xfs_getbmap()
4405 * when the caller does not wish to see the
4406 * separation (which is the default).
4408 * This technique is also used when writing a
4409 * buffer which has been partially written,
4410 * (usually by being flushed during a chunkread),
4411 * to ensure one write takes place. This also
4412 * prevents a change in the xfs inode extents at
4413 * this time, intentionally. This change occurs
4414 * on completion of the write operation, in
4415 * xfs_strat_comp(), where the xfs_bmapi() call
4416 * is transactioned, and the extents combined.
4418 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4419 wr = 0; /* no allocations are allowed */
4420 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4424 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4426 if ((error = xfs_bmap_local_to_extents(tp, ip,
4427 firstblock, total, &logflags, whichfork)))
4430 if (wr && *firstblock == NULLFSBLOCK) {
4431 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4432 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4437 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4438 (error = xfs_iread_extents(tp, ip, whichfork)))
4440 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4442 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4448 while (bno < end && n < *nmap) {
4450 * Reading past eof, act as though there's a hole
4454 got.br_startoff = end;
4455 inhole = eof || got.br_startoff > bno;
4456 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4457 isnullstartblock(got.br_startblock);
4459 * First, deal with the hole before the allocated space
4460 * that we found, if any.
4462 if (wr && (inhole || wasdelay)) {
4464 * For the wasdelay case, we could also just
4465 * allocate the stuff asked for in this bmap call
4466 * but that wouldn't be as good.
4469 alen = (xfs_extlen_t)got.br_blockcount;
4470 aoff = got.br_startoff;
4471 if (lastx != NULLEXTNUM && lastx) {
4472 ep = xfs_iext_get_ext(ifp, lastx - 1);
4473 xfs_bmbt_get_all(ep, &prev);
4476 alen = (xfs_extlen_t)
4477 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4479 alen = (xfs_extlen_t)
4480 XFS_FILBLKS_MIN(alen,
4481 got.br_startoff - bno);
4484 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4485 if (flags & XFS_BMAPI_DELAY) {
4488 /* Figure out the extent size, adjust alen */
4489 extsz = xfs_get_extsz_hint(ip);
4492 * make sure we don't exceed a single
4493 * extent length when we align the
4494 * extent by reducing length we are
4495 * going to allocate by the maximum
4496 * amount extent size aligment may
4499 alen = XFS_FILBLKS_MIN(len,
4500 MAXEXTLEN - (2 * extsz - 1));
4501 error = xfs_bmap_extsize_align(mp,
4504 flags&XFS_BMAPI_DELAY,
4505 flags&XFS_BMAPI_CONVERT,
4511 extsz = alen / mp->m_sb.sb_rextsize;
4514 * Make a transaction-less quota reservation for
4515 * delayed allocation blocks. This number gets
4516 * adjusted later. We return if we haven't
4517 * allocated blocks already inside this loop.
4519 error = xfs_trans_reserve_quota_nblks(
4520 NULL, ip, (long)alen, 0,
4521 rt ? XFS_QMOPT_RES_RTBLKS :
4522 XFS_QMOPT_RES_REGBLKS);
4526 ASSERT(cur == NULL);
4533 * Split changing sb for alen and indlen since
4534 * they could be coming from different places.
4536 indlen = (xfs_extlen_t)
4537 xfs_bmap_worst_indlen(ip, alen);
4541 error = xfs_mod_incore_sb(mp,
4543 -((int64_t)extsz), 0);
4545 error = xfs_icsb_modify_counters(mp,
4547 -((int64_t)alen), 0);
4550 error = xfs_icsb_modify_counters(mp,
4552 -((int64_t)indlen), 0);
4554 xfs_mod_incore_sb(mp,
4558 xfs_icsb_modify_counters(mp,
4564 if (XFS_IS_QUOTA_ON(mp))
4565 /* unreserve the blocks now */
4567 xfs_trans_unreserve_quota_nblks(
4570 XFS_QMOPT_RES_RTBLKS :
4571 XFS_QMOPT_RES_REGBLKS);
4575 ip->i_delayed_blks += alen;
4576 abno = nullstartblock(indlen);
4579 * If first time, allocate and fill in
4580 * once-only bma fields.
4582 if (bma.ip == NULL) {
4590 /* Indicate if this is the first user data
4591 * in the file, or just any user data.
4593 if (!(flags & XFS_BMAPI_METADATA)) {
4594 bma.userdata = (aoff == 0) ?
4595 XFS_ALLOC_INITIAL_USER_DATA :
4599 * Fill in changeable bma fields.
4602 bma.firstblock = *firstblock;
4605 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4606 bma.wasdel = wasdelay;
4607 bma.minlen = minlen;
4608 bma.low = flist->xbf_low;
4609 bma.minleft = minleft;
4611 * Only want to do the alignment at the
4612 * eof if it is userdata and allocation length
4613 * is larger than a stripe unit.
4615 if (mp->m_dalign && alen >= mp->m_dalign &&
4616 (!(flags & XFS_BMAPI_METADATA)) &&
4617 (whichfork == XFS_DATA_FORK)) {
4618 if ((error = xfs_bmap_isaeof(ip, aoff,
4619 whichfork, &bma.aeof)))
4626 if ((error = xfs_bmap_alloc(&bma)))
4629 * Copy out result fields.
4632 if ((flist->xbf_low = bma.low))
4636 ASSERT(*firstblock == NULLFSBLOCK ||
4637 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4638 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4640 XFS_FSB_TO_AGNO(mp, *firstblock) <
4641 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4642 *firstblock = bma.firstblock;
4644 cur->bc_private.b.firstblock =
4646 if (abno == NULLFSBLOCK)
4648 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4649 cur = xfs_bmbt_init_cursor(mp, tp,
4651 cur->bc_private.b.firstblock =
4653 cur->bc_private.b.flist = flist;
4656 * Bump the number of extents we've allocated
4662 cur->bc_private.b.flags =
4663 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4664 got.br_startoff = aoff;
4665 got.br_startblock = abno;
4666 got.br_blockcount = alen;
4667 got.br_state = XFS_EXT_NORM; /* assume normal */
4669 * Determine state of extent, and the filesystem.
4670 * A wasdelay extent has been initialized, so
4671 * shouldn't be flagged as unwritten.
4673 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4674 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4675 got.br_state = XFS_EXT_UNWRITTEN;
4677 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4678 firstblock, flist, &tmp_logflags,
4680 logflags |= tmp_logflags;
4683 lastx = ifp->if_lastex;
4684 ep = xfs_iext_get_ext(ifp, lastx);
4685 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4686 xfs_bmbt_get_all(ep, &got);
4687 ASSERT(got.br_startoff <= aoff);
4688 ASSERT(got.br_startoff + got.br_blockcount >=
4691 if (flags & XFS_BMAPI_DELAY) {
4692 ASSERT(isnullstartblock(got.br_startblock));
4693 ASSERT(startblockval(got.br_startblock) > 0);
4695 ASSERT(got.br_state == XFS_EXT_NORM ||
4696 got.br_state == XFS_EXT_UNWRITTEN);
4699 * Fall down into the found allocated space case.
4701 } else if (inhole) {
4703 * Reading in a hole.
4705 mval->br_startoff = bno;
4706 mval->br_startblock = HOLESTARTBLOCK;
4707 mval->br_blockcount =
4708 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4709 mval->br_state = XFS_EXT_NORM;
4710 bno += mval->br_blockcount;
4711 len -= mval->br_blockcount;
4717 * Then deal with the allocated space we found.
4720 if (!(flags & XFS_BMAPI_ENTIRE) &&
4721 (got.br_startoff + got.br_blockcount > obno)) {
4724 ASSERT((bno >= obno) || (n == 0));
4726 mval->br_startoff = bno;
4727 if (isnullstartblock(got.br_startblock)) {
4728 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4729 mval->br_startblock = DELAYSTARTBLOCK;
4731 mval->br_startblock =
4733 (bno - got.br_startoff);
4735 * Return the minimum of what we got and what we
4736 * asked for for the length. We can use the len
4737 * variable here because it is modified below
4738 * and we could have been there before coming
4739 * here if the first part of the allocation
4740 * didn't overlap what was asked for.
4742 mval->br_blockcount =
4743 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4744 (bno - got.br_startoff));
4745 mval->br_state = got.br_state;
4746 ASSERT(mval->br_blockcount <= len);
4749 if (isnullstartblock(mval->br_startblock)) {
4750 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4751 mval->br_startblock = DELAYSTARTBLOCK;
4756 * Check if writing previously allocated but
4757 * unwritten extents.
4760 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4761 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4762 (mval->br_state == XFS_EXT_NORM &&
4763 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4764 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
4766 * Modify (by adding) the state flag, if writing.
4768 ASSERT(mval->br_blockcount <= len);
4769 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4770 cur = xfs_bmbt_init_cursor(mp,
4772 cur->bc_private.b.firstblock =
4774 cur->bc_private.b.flist = flist;
4776 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4778 : XFS_EXT_UNWRITTEN;
4779 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4780 firstblock, flist, &tmp_logflags,
4782 logflags |= tmp_logflags;
4785 lastx = ifp->if_lastex;
4786 ep = xfs_iext_get_ext(ifp, lastx);
4787 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4788 xfs_bmbt_get_all(ep, &got);
4790 * We may have combined previously unwritten
4791 * space with written space, so generate
4794 if (mval->br_blockcount < len)
4798 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4799 ((mval->br_startoff + mval->br_blockcount) <= end));
4800 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4801 (mval->br_blockcount <= len) ||
4802 (mval->br_startoff < obno));
4803 bno = mval->br_startoff + mval->br_blockcount;
4805 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4806 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4807 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4808 ASSERT(mval->br_state == mval[-1].br_state);
4809 mval[-1].br_blockcount = mval->br_blockcount;
4810 mval[-1].br_state = mval->br_state;
4811 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4812 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4813 mval[-1].br_startblock != HOLESTARTBLOCK &&
4814 mval->br_startblock ==
4815 mval[-1].br_startblock + mval[-1].br_blockcount &&
4816 ((flags & XFS_BMAPI_IGSTATE) ||
4817 mval[-1].br_state == mval->br_state)) {
4818 ASSERT(mval->br_startoff ==
4819 mval[-1].br_startoff + mval[-1].br_blockcount);
4820 mval[-1].br_blockcount += mval->br_blockcount;
4822 mval->br_startblock == DELAYSTARTBLOCK &&
4823 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4824 mval->br_startoff ==
4825 mval[-1].br_startoff + mval[-1].br_blockcount) {
4826 mval[-1].br_blockcount += mval->br_blockcount;
4827 mval[-1].br_state = mval->br_state;
4828 } else if (!((n == 0) &&
4829 ((mval->br_startoff + mval->br_blockcount) <=
4835 * If we're done, stop now. Stop when we've allocated
4836 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4837 * the transaction may get too big.
4839 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4842 * Else go on to the next record.
4844 ep = xfs_iext_get_ext(ifp, ++lastx);
4846 if (lastx >= nextents)
4849 xfs_bmbt_get_all(ep, &got);
4851 ifp->if_lastex = lastx;
4854 * Transform from btree to extents, give it cur.
4856 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4857 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4859 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4860 &tmp_logflags, whichfork);
4861 logflags |= tmp_logflags;
4865 ASSERT(ifp->if_ext_max ==
4866 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4867 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4868 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4872 * Log everything. Do this after conversion, there's no point in
4873 * logging the extent records if we've converted to btree format.
4875 if ((logflags & xfs_ilog_fext(whichfork)) &&
4876 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4877 logflags &= ~xfs_ilog_fext(whichfork);
4878 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
4879 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4880 logflags &= ~xfs_ilog_fbroot(whichfork);
4882 * Log whatever the flags say, even if error. Otherwise we might miss
4883 * detecting a case where the data is changed, there's an error,
4884 * and it's not logged so we don't shutdown when we should.
4888 xfs_trans_log_inode(tp, ip, logflags);
4892 ASSERT(*firstblock == NULLFSBLOCK ||
4893 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4895 cur->bc_private.b.firstblock) ||
4897 XFS_FSB_TO_AGNO(mp, *firstblock) <
4899 cur->bc_private.b.firstblock)));
4900 *firstblock = cur->bc_private.b.firstblock;
4902 xfs_btree_del_cursor(cur,
4903 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4906 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4912 * Map file blocks to filesystem blocks, simple version.
4913 * One block (extent) only, read-only.
4914 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4915 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4916 * was set and all the others were clear.
4920 xfs_trans_t *tp, /* transaction pointer */
4921 xfs_inode_t *ip, /* incore inode */
4922 int whichfork, /* data or attr fork */
4923 xfs_fsblock_t *fsb, /* output: mapped block */
4924 xfs_fileoff_t bno) /* starting file offs. mapped */
4926 int eof; /* we've hit the end of extents */
4927 int error; /* error return */
4928 xfs_bmbt_irec_t got; /* current file extent record */
4929 xfs_ifork_t *ifp; /* inode fork pointer */
4930 xfs_extnum_t lastx; /* last useful extent number */
4931 xfs_bmbt_irec_t prev; /* previous file extent record */
4933 ifp = XFS_IFORK_PTR(ip, whichfork);
4935 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4936 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4937 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4939 return XFS_ERROR(EFSCORRUPTED);
4941 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4942 return XFS_ERROR(EIO);
4943 XFS_STATS_INC(xs_blk_mapr);
4944 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4945 (error = xfs_iread_extents(tp, ip, whichfork)))
4947 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4950 * Reading past eof, act as though there's a hole
4953 if (eof || got.br_startoff > bno) {
4957 ASSERT(!isnullstartblock(got.br_startblock));
4958 ASSERT(bno < got.br_startoff + got.br_blockcount);
4959 *fsb = got.br_startblock + (bno - got.br_startoff);
4960 ifp->if_lastex = lastx;
4965 * Unmap (remove) blocks from a file.
4966 * If nexts is nonzero then the number of extents to remove is limited to
4967 * that value. If not all extents in the block range can be removed then
4972 xfs_trans_t *tp, /* transaction pointer */
4973 struct xfs_inode *ip, /* incore inode */
4974 xfs_fileoff_t bno, /* starting offset to unmap */
4975 xfs_filblks_t len, /* length to unmap in file */
4976 int flags, /* misc flags */
4977 xfs_extnum_t nexts, /* number of extents max */
4978 xfs_fsblock_t *firstblock, /* first allocated block
4979 controls a.g. for allocs */
4980 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4981 int *done) /* set if not done yet */
4983 xfs_btree_cur_t *cur; /* bmap btree cursor */
4984 xfs_bmbt_irec_t del; /* extent being deleted */
4985 int eof; /* is deleting at eof */
4986 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4987 int error; /* error return value */
4988 xfs_extnum_t extno; /* extent number in list */
4989 xfs_bmbt_irec_t got; /* current extent record */
4990 xfs_ifork_t *ifp; /* inode fork pointer */
4991 int isrt; /* freeing in rt area */
4992 xfs_extnum_t lastx; /* last extent index used */
4993 int logflags; /* transaction logging flags */
4994 xfs_extlen_t mod; /* rt extent offset */
4995 xfs_mount_t *mp; /* mount structure */
4996 xfs_extnum_t nextents; /* number of file extents */
4997 xfs_bmbt_irec_t prev; /* previous extent record */
4998 xfs_fileoff_t start; /* first file offset deleted */
4999 int tmp_logflags; /* partial logging flags */
5000 int wasdel; /* was a delayed alloc extent */
5001 int whichfork; /* data or attribute fork */
5004 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5006 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5007 XFS_ATTR_FORK : XFS_DATA_FORK;
5008 ifp = XFS_IFORK_PTR(ip, whichfork);
5010 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5011 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5012 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5014 return XFS_ERROR(EFSCORRUPTED);
5017 if (XFS_FORCED_SHUTDOWN(mp))
5018 return XFS_ERROR(EIO);
5022 ASSERT(ifp->if_ext_max ==
5023 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5024 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5025 (error = xfs_iread_extents(tp, ip, whichfork)))
5027 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5028 if (nextents == 0) {
5032 XFS_STATS_INC(xs_blk_unmap);
5033 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5035 bno = start + len - 1;
5036 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5040 * Check to see if the given block number is past the end of the
5041 * file, back up to the last block if so...
5044 ep = xfs_iext_get_ext(ifp, --lastx);
5045 xfs_bmbt_get_all(ep, &got);
5046 bno = got.br_startoff + got.br_blockcount - 1;
5049 if (ifp->if_flags & XFS_IFBROOT) {
5050 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5051 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5052 cur->bc_private.b.firstblock = *firstblock;
5053 cur->bc_private.b.flist = flist;
5054 cur->bc_private.b.flags = 0;
5058 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5059 (nexts == 0 || extno < nexts)) {
5061 * Is the found extent after a hole in which bno lives?
5062 * Just back up to the previous extent, if so.
5064 if (got.br_startoff > bno) {
5067 ep = xfs_iext_get_ext(ifp, lastx);
5068 xfs_bmbt_get_all(ep, &got);
5071 * Is the last block of this extent before the range
5072 * we're supposed to delete? If so, we're done.
5074 bno = XFS_FILEOFF_MIN(bno,
5075 got.br_startoff + got.br_blockcount - 1);
5079 * Then deal with the (possibly delayed) allocated space
5084 wasdel = isnullstartblock(del.br_startblock);
5085 if (got.br_startoff < start) {
5086 del.br_startoff = start;
5087 del.br_blockcount -= start - got.br_startoff;
5089 del.br_startblock += start - got.br_startoff;
5091 if (del.br_startoff + del.br_blockcount > bno + 1)
5092 del.br_blockcount = bno + 1 - del.br_startoff;
5093 sum = del.br_startblock + del.br_blockcount;
5095 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5097 * Realtime extent not lined up at the end.
5098 * The extent could have been split into written
5099 * and unwritten pieces, or we could just be
5100 * unmapping part of it. But we can't really
5101 * get rid of part of a realtime extent.
5103 if (del.br_state == XFS_EXT_UNWRITTEN ||
5104 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5106 * This piece is unwritten, or we're not
5107 * using unwritten extents. Skip over it.
5110 bno -= mod > del.br_blockcount ?
5111 del.br_blockcount : mod;
5112 if (bno < got.br_startoff) {
5114 xfs_bmbt_get_all(xfs_iext_get_ext(
5120 * It's written, turn it unwritten.
5121 * This is better than zeroing it.
5123 ASSERT(del.br_state == XFS_EXT_NORM);
5124 ASSERT(xfs_trans_get_block_res(tp) > 0);
5126 * If this spans a realtime extent boundary,
5127 * chop it back to the start of the one we end at.
5129 if (del.br_blockcount > mod) {
5130 del.br_startoff += del.br_blockcount - mod;
5131 del.br_startblock += del.br_blockcount - mod;
5132 del.br_blockcount = mod;
5134 del.br_state = XFS_EXT_UNWRITTEN;
5135 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5136 firstblock, flist, &logflags,
5142 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5144 * Realtime extent is lined up at the end but not
5145 * at the front. We'll get rid of full extents if
5148 mod = mp->m_sb.sb_rextsize - mod;
5149 if (del.br_blockcount > mod) {
5150 del.br_blockcount -= mod;
5151 del.br_startoff += mod;
5152 del.br_startblock += mod;
5153 } else if ((del.br_startoff == start &&
5154 (del.br_state == XFS_EXT_UNWRITTEN ||
5155 xfs_trans_get_block_res(tp) == 0)) ||
5156 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5158 * Can't make it unwritten. There isn't
5159 * a full extent here so just skip it.
5161 ASSERT(bno >= del.br_blockcount);
5162 bno -= del.br_blockcount;
5163 if (bno < got.br_startoff) {
5165 xfs_bmbt_get_all(--ep, &got);
5168 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5170 * This one is already unwritten.
5171 * It must have a written left neighbor.
5172 * Unwrite the killed part of that one and
5176 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5178 ASSERT(prev.br_state == XFS_EXT_NORM);
5179 ASSERT(!isnullstartblock(prev.br_startblock));
5180 ASSERT(del.br_startblock ==
5181 prev.br_startblock + prev.br_blockcount);
5182 if (prev.br_startoff < start) {
5183 mod = start - prev.br_startoff;
5184 prev.br_blockcount -= mod;
5185 prev.br_startblock += mod;
5186 prev.br_startoff = start;
5188 prev.br_state = XFS_EXT_UNWRITTEN;
5189 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5190 &prev, firstblock, flist, &logflags,
5196 ASSERT(del.br_state == XFS_EXT_NORM);
5197 del.br_state = XFS_EXT_UNWRITTEN;
5198 error = xfs_bmap_add_extent(ip, lastx, &cur,
5199 &del, firstblock, flist, &logflags,
5207 ASSERT(startblockval(del.br_startblock) > 0);
5208 /* Update realtime/data freespace, unreserve quota */
5210 xfs_filblks_t rtexts;
5212 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5213 do_div(rtexts, mp->m_sb.sb_rextsize);
5214 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5215 (int64_t)rtexts, 0);
5216 (void)xfs_trans_reserve_quota_nblks(NULL,
5217 ip, -((long)del.br_blockcount), 0,
5218 XFS_QMOPT_RES_RTBLKS);
5220 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5221 (int64_t)del.br_blockcount, 0);
5222 (void)xfs_trans_reserve_quota_nblks(NULL,
5223 ip, -((long)del.br_blockcount), 0,
5224 XFS_QMOPT_RES_REGBLKS);
5226 ip->i_delayed_blks -= del.br_blockcount;
5228 cur->bc_private.b.flags |=
5229 XFS_BTCUR_BPRV_WASDEL;
5231 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5233 * If it's the case where the directory code is running
5234 * with no block reservation, and the deleted block is in
5235 * the middle of its extent, and the resulting insert
5236 * of an extent would cause transformation to btree format,
5237 * then reject it. The calling code will then swap
5238 * blocks around instead.
5239 * We have to do this now, rather than waiting for the
5240 * conversion to btree format, since the transaction
5243 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5244 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5245 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5246 del.br_startoff > got.br_startoff &&
5247 del.br_startoff + del.br_blockcount <
5248 got.br_startoff + got.br_blockcount) {
5249 error = XFS_ERROR(ENOSPC);
5252 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5253 &tmp_logflags, whichfork);
5254 logflags |= tmp_logflags;
5257 bno = del.br_startoff - 1;
5259 lastx = ifp->if_lastex;
5261 * If not done go on to the next (previous) record.
5262 * Reset ep in case the extents array was re-alloced.
5264 ep = xfs_iext_get_ext(ifp, lastx);
5265 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5266 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5267 xfs_bmbt_get_startoff(ep) > bno) {
5269 ep = xfs_iext_get_ext(ifp, lastx);
5272 xfs_bmbt_get_all(ep, &got);
5276 ifp->if_lastex = lastx;
5277 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5278 ASSERT(ifp->if_ext_max ==
5279 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5281 * Convert to a btree if necessary.
5283 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5284 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5285 ASSERT(cur == NULL);
5286 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5287 &cur, 0, &tmp_logflags, whichfork);
5288 logflags |= tmp_logflags;
5293 * transform from btree to extents, give it cur
5295 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5296 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5297 ASSERT(cur != NULL);
5298 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5300 logflags |= tmp_logflags;
5305 * transform from extents to local?
5307 ASSERT(ifp->if_ext_max ==
5308 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5312 * Log everything. Do this after conversion, there's no point in
5313 * logging the extent records if we've converted to btree format.
5315 if ((logflags & xfs_ilog_fext(whichfork)) &&
5316 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5317 logflags &= ~xfs_ilog_fext(whichfork);
5318 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5319 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5320 logflags &= ~xfs_ilog_fbroot(whichfork);
5322 * Log inode even in the error case, if the transaction
5323 * is dirty we'll need to shut down the filesystem.
5326 xfs_trans_log_inode(tp, ip, logflags);
5329 *firstblock = cur->bc_private.b.firstblock;
5330 cur->bc_private.b.allocated = 0;
5332 xfs_btree_del_cursor(cur,
5333 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5339 * returns 1 for success, 0 if we failed to map the extent.
5342 xfs_getbmapx_fix_eof_hole(
5343 xfs_inode_t *ip, /* xfs incore inode pointer */
5344 struct getbmapx *out, /* output structure */
5345 int prealloced, /* this is a file with
5346 * preallocated data space */
5347 __int64_t end, /* last block requested */
5348 xfs_fsblock_t startblock)
5351 xfs_mount_t *mp; /* file system mount point */
5352 xfs_ifork_t *ifp; /* inode fork pointer */
5353 xfs_extnum_t lastx; /* last extent pointer */
5354 xfs_fileoff_t fileblock;
5356 if (startblock == HOLESTARTBLOCK) {
5358 out->bmv_block = -1;
5359 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5360 fixlen -= out->bmv_offset;
5361 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5362 /* Came to hole at EOF. Trim it. */
5365 out->bmv_length = fixlen;
5368 if (startblock == DELAYSTARTBLOCK)
5369 out->bmv_block = -2;
5371 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5372 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5373 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5374 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5375 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5376 out->bmv_oflags |= BMV_OF_LAST;
5383 * Get inode's extents as described in bmv, and format for output.
5384 * Calls formatter to fill the user's buffer until all extents
5385 * are mapped, until the passed-in bmv->bmv_count slots have
5386 * been filled, or until the formatter short-circuits the loop,
5387 * if it is tracking filled-in extents on its own.
5389 int /* error code */
5392 struct getbmapx *bmv, /* user bmap structure */
5393 xfs_bmap_format_t formatter, /* format to user */
5394 void *arg) /* formatter arg */
5396 __int64_t bmvend; /* last block requested */
5397 int error = 0; /* return value */
5398 __int64_t fixlen; /* length for -1 case */
5399 int i; /* extent number */
5400 int lock; /* lock state */
5401 xfs_bmbt_irec_t *map; /* buffer for user's data */
5402 xfs_mount_t *mp; /* file system mount point */
5403 int nex; /* # of user extents can do */
5404 int nexleft; /* # of user extents left */
5405 int subnex; /* # of bmapi's can do */
5406 int nmap; /* number of map entries */
5407 struct getbmapx *out; /* output structure */
5408 int whichfork; /* data or attr fork */
5409 int prealloced; /* this is a file with
5410 * preallocated data space */
5411 int iflags; /* interface flags */
5412 int bmapi_flags; /* flags for xfs_bmapi */
5416 iflags = bmv->bmv_iflags;
5417 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5419 if (whichfork == XFS_ATTR_FORK) {
5420 if (XFS_IFORK_Q(ip)) {
5421 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5422 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5423 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5424 return XFS_ERROR(EINVAL);
5425 } else if (unlikely(
5426 ip->i_d.di_aformat != 0 &&
5427 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5428 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5430 return XFS_ERROR(EFSCORRUPTED);
5436 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5437 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5438 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5439 return XFS_ERROR(EINVAL);
5441 if (xfs_get_extsz_hint(ip) ||
5442 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5444 fixlen = XFS_MAXIOFFSET(mp);
5447 fixlen = ip->i_size;
5451 if (bmv->bmv_length == -1) {
5452 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5454 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5455 } else if (bmv->bmv_length == 0) {
5456 bmv->bmv_entries = 0;
5458 } else if (bmv->bmv_length < 0) {
5459 return XFS_ERROR(EINVAL);
5462 nex = bmv->bmv_count - 1;
5464 return XFS_ERROR(EINVAL);
5465 bmvend = bmv->bmv_offset + bmv->bmv_length;
5468 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5469 return XFS_ERROR(ENOMEM);
5470 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5472 return XFS_ERROR(ENOMEM);
5474 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5475 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5476 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5477 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5479 goto out_unlock_iolock;
5482 * even after flushing the inode, there can still be delalloc
5483 * blocks on the inode beyond EOF due to speculative
5484 * preallocation. These are not removed until the release
5485 * function is called or the inode is inactivated. Hence we
5486 * cannot assert here that ip->i_delayed_blks == 0.
5490 lock = xfs_ilock_map_shared(ip);
5493 * Don't let nex be bigger than the number of extents
5494 * we can have assuming alternating holes and real extents.
5496 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5497 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5499 bmapi_flags = xfs_bmapi_aflag(whichfork);
5500 if (!(iflags & BMV_IF_PREALLOC))
5501 bmapi_flags |= XFS_BMAPI_IGSTATE;
5504 * Allocate enough space to handle "subnex" maps at a time.
5508 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5510 goto out_unlock_ilock;
5512 bmv->bmv_entries = 0;
5514 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5515 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5523 nmap = (nexleft > subnex) ? subnex : nexleft;
5524 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5525 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5526 bmapi_flags, NULL, 0, map, &nmap,
5530 ASSERT(nmap <= subnex);
5532 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5533 out[cur_ext].bmv_oflags = 0;
5534 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5535 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5536 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5537 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5538 out[cur_ext].bmv_offset =
5539 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5540 out[cur_ext].bmv_length =
5541 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5542 out[cur_ext].bmv_unused1 = 0;
5543 out[cur_ext].bmv_unused2 = 0;
5544 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5545 (map[i].br_startblock != DELAYSTARTBLOCK));
5546 if (map[i].br_startblock == HOLESTARTBLOCK &&
5547 whichfork == XFS_ATTR_FORK) {
5548 /* came to the end of attribute fork */
5549 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5553 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5555 map[i].br_startblock))
5559 out[cur_ext].bmv_offset +
5560 out[cur_ext].bmv_length;
5562 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5565 * In case we don't want to return the hole,
5566 * don't increase cur_ext so that we can reuse
5567 * it in the next loop.
5569 if ((iflags & BMV_IF_NO_HOLES) &&
5570 map[i].br_startblock == HOLESTARTBLOCK) {
5571 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5579 } while (nmap && nexleft && bmv->bmv_length);
5584 xfs_iunlock_map_shared(ip, lock);
5586 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5588 for (i = 0; i < cur_ext; i++) {
5589 int full = 0; /* user array is full */
5591 /* format results & advance arg */
5592 error = formatter(&arg, &out[i], &full);
5602 * Check the last inode extent to determine whether this allocation will result
5603 * in blocks being allocated at the end of the file. When we allocate new data
5604 * blocks at the end of the file which do not start at the previous data block,
5605 * we will try to align the new blocks at stripe unit boundaries.
5607 STATIC int /* error */
5609 xfs_inode_t *ip, /* incore inode pointer */
5610 xfs_fileoff_t off, /* file offset in fsblocks */
5611 int whichfork, /* data or attribute fork */
5612 char *aeof) /* return value */
5614 int error; /* error return value */
5615 xfs_ifork_t *ifp; /* inode fork pointer */
5616 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5617 xfs_extnum_t nextents; /* number of file extents */
5618 xfs_bmbt_irec_t s; /* expanded extent record */
5620 ASSERT(whichfork == XFS_DATA_FORK);
5621 ifp = XFS_IFORK_PTR(ip, whichfork);
5622 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5623 (error = xfs_iread_extents(NULL, ip, whichfork)))
5625 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5626 if (nextents == 0) {
5631 * Go to the last extent
5633 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5634 xfs_bmbt_get_all(lastrec, &s);
5636 * Check we are allocating in the last extent (for delayed allocations)
5637 * or past the last extent for non-delayed allocations.
5639 *aeof = (off >= s.br_startoff &&
5640 off < s.br_startoff + s.br_blockcount &&
5641 isnullstartblock(s.br_startblock)) ||
5642 off >= s.br_startoff + s.br_blockcount;
5647 * Check if the endoff is outside the last extent. If so the caller will grow
5648 * the allocation to a stripe unit boundary.
5652 xfs_inode_t *ip, /* incore inode pointer */
5653 xfs_fileoff_t endoff, /* file offset in fsblocks */
5654 int whichfork, /* data or attribute fork */
5655 int *eof) /* result value */
5657 xfs_fsblock_t blockcount; /* extent block count */
5658 int error; /* error return value */
5659 xfs_ifork_t *ifp; /* inode fork pointer */
5660 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5661 xfs_extnum_t nextents; /* number of file extents */
5662 xfs_fileoff_t startoff; /* extent starting file offset */
5664 ASSERT(whichfork == XFS_DATA_FORK);
5665 ifp = XFS_IFORK_PTR(ip, whichfork);
5666 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5667 (error = xfs_iread_extents(NULL, ip, whichfork)))
5669 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5670 if (nextents == 0) {
5675 * Go to the last extent
5677 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5678 startoff = xfs_bmbt_get_startoff(lastrec);
5679 blockcount = xfs_bmbt_get_blockcount(lastrec);
5680 *eof = endoff >= startoff + blockcount;
5685 STATIC struct xfs_buf *
5687 struct xfs_btree_cur *cur,
5690 struct xfs_log_item_desc *lidp;
5696 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5697 if (!cur->bc_bufs[i])
5699 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5700 return cur->bc_bufs[i];
5703 /* Chase down all the log items to see if the bp is there */
5704 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5705 struct xfs_buf_log_item *bip;
5706 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5707 if (bip->bli_item.li_type == XFS_LI_BUF &&
5708 XFS_BUF_ADDR(bip->bli_buf) == bno)
5709 return bip->bli_buf;
5717 struct xfs_btree_block *block,
5723 __be64 *pp, *thispa; /* pointer to block address */
5724 xfs_bmbt_key_t *prevp, *keyp;
5726 ASSERT(be16_to_cpu(block->bb_level) > 0);
5729 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5730 dmxr = mp->m_bmap_dmxr[0];
5731 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5734 ASSERT(be64_to_cpu(prevp->br_startoff) <
5735 be64_to_cpu(keyp->br_startoff));
5740 * Compare the block numbers to see if there are dups.
5743 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5745 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5747 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5749 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5751 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5752 if (*thispa == *pp) {
5753 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
5755 (unsigned long long)be64_to_cpu(*thispa));
5756 panic("%s: ptrs are equal in node\n",
5764 * Check that the extents for the inode ip are in the right order in all
5769 xfs_bmap_check_leaf_extents(
5770 xfs_btree_cur_t *cur, /* btree cursor or null */
5771 xfs_inode_t *ip, /* incore inode pointer */
5772 int whichfork) /* data or attr fork */
5774 struct xfs_btree_block *block; /* current btree block */
5775 xfs_fsblock_t bno; /* block # of "block" */
5776 xfs_buf_t *bp; /* buffer for "block" */
5777 int error; /* error return value */
5778 xfs_extnum_t i=0, j; /* index into the extents list */
5779 xfs_ifork_t *ifp; /* fork structure */
5780 int level; /* btree level, for checking */
5781 xfs_mount_t *mp; /* file system mount structure */
5782 __be64 *pp; /* pointer to block address */
5783 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5784 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5785 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5788 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5794 ifp = XFS_IFORK_PTR(ip, whichfork);
5795 block = ifp->if_broot;
5797 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5799 level = be16_to_cpu(block->bb_level);
5801 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5802 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5803 bno = be64_to_cpu(*pp);
5805 ASSERT(bno != NULLDFSBNO);
5806 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5807 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5810 * Go down the tree until leaf level is reached, following the first
5811 * pointer (leftmost) at each level.
5813 while (level-- > 0) {
5814 /* See if buf is in cur first */
5815 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5821 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5822 XFS_BMAP_BTREE_REF)))
5824 block = XFS_BUF_TO_BLOCK(bp);
5825 XFS_WANT_CORRUPTED_GOTO(
5826 xfs_bmap_sanity_check(mp, bp, level),
5832 * Check this block for basic sanity (increasing keys and
5833 * no duplicate blocks).
5836 xfs_check_block(block, mp, 0, 0);
5837 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5838 bno = be64_to_cpu(*pp);
5839 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5842 xfs_trans_brelse(NULL, bp);
5847 * Here with bp and block set to the leftmost leaf node in the tree.
5852 * Loop over all leaf nodes checking that all extents are in the right order.
5855 xfs_fsblock_t nextbno;
5856 xfs_extnum_t num_recs;
5859 num_recs = xfs_btree_get_numrecs(block);
5862 * Read-ahead the next leaf block, if any.
5865 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5868 * Check all the extents to make sure they are OK.
5869 * If we had a previous block, the last entry should
5870 * conform with the first entry in this one.
5873 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5875 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5876 xfs_bmbt_disk_get_blockcount(&last) <=
5877 xfs_bmbt_disk_get_startoff(ep));
5879 for (j = 1; j < num_recs; j++) {
5880 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5881 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5882 xfs_bmbt_disk_get_blockcount(ep) <=
5883 xfs_bmbt_disk_get_startoff(nextp));
5891 xfs_trans_brelse(NULL, bp);
5895 * If we've reached the end, stop.
5897 if (bno == NULLFSBLOCK)
5900 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5906 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5907 XFS_BMAP_BTREE_REF)))
5909 block = XFS_BUF_TO_BLOCK(bp);
5913 xfs_trans_brelse(NULL, bp);
5918 xfs_warn(mp, "%s: at error0", __func__);
5920 xfs_trans_brelse(NULL, bp);
5922 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
5924 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
5930 * Count fsblocks of the given fork.
5933 xfs_bmap_count_blocks(
5934 xfs_trans_t *tp, /* transaction pointer */
5935 xfs_inode_t *ip, /* incore inode */
5936 int whichfork, /* data or attr fork */
5937 int *count) /* out: count of blocks */
5939 struct xfs_btree_block *block; /* current btree block */
5940 xfs_fsblock_t bno; /* block # of "block" */
5941 xfs_ifork_t *ifp; /* fork structure */
5942 int level; /* btree level, for checking */
5943 xfs_mount_t *mp; /* file system mount structure */
5944 __be64 *pp; /* pointer to block address */
5948 ifp = XFS_IFORK_PTR(ip, whichfork);
5949 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
5950 xfs_bmap_count_leaves(ifp, 0,
5951 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
5957 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5959 block = ifp->if_broot;
5960 level = be16_to_cpu(block->bb_level);
5962 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5963 bno = be64_to_cpu(*pp);
5964 ASSERT(bno != NULLDFSBNO);
5965 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5966 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5968 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
5969 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5971 return XFS_ERROR(EFSCORRUPTED);
5978 * Recursively walks each level of a btree
5979 * to count total fsblocks is use.
5981 STATIC int /* error */
5982 xfs_bmap_count_tree(
5983 xfs_mount_t *mp, /* file system mount point */
5984 xfs_trans_t *tp, /* transaction pointer */
5985 xfs_ifork_t *ifp, /* inode fork pointer */
5986 xfs_fsblock_t blockno, /* file system block number */
5987 int levelin, /* level in btree */
5988 int *count) /* Count of blocks */
5991 xfs_buf_t *bp, *nbp;
5992 int level = levelin;
5994 xfs_fsblock_t bno = blockno;
5995 xfs_fsblock_t nextbno;
5996 struct xfs_btree_block *block, *nextblock;
5999 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6002 block = XFS_BUF_TO_BLOCK(bp);
6005 /* Not at node above leaves, count this level of nodes */
6006 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6007 while (nextbno != NULLFSBLOCK) {
6008 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6009 0, &nbp, XFS_BMAP_BTREE_REF)))
6012 nextblock = XFS_BUF_TO_BLOCK(nbp);
6013 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6014 xfs_trans_brelse(tp, nbp);
6017 /* Dive to the next level */
6018 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6019 bno = be64_to_cpu(*pp);
6020 if (unlikely((error =
6021 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6022 xfs_trans_brelse(tp, bp);
6023 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6024 XFS_ERRLEVEL_LOW, mp);
6025 return XFS_ERROR(EFSCORRUPTED);
6027 xfs_trans_brelse(tp, bp);
6029 /* count all level 1 nodes and their leaves */
6031 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6032 numrecs = be16_to_cpu(block->bb_numrecs);
6033 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6034 xfs_trans_brelse(tp, bp);
6035 if (nextbno == NULLFSBLOCK)
6038 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6039 XFS_BMAP_BTREE_REF)))
6042 block = XFS_BUF_TO_BLOCK(bp);
6049 * Count leaf blocks given a range of extent records.
6052 xfs_bmap_count_leaves(
6060 for (b = 0; b < numrecs; b++) {
6061 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6062 *count += xfs_bmbt_get_blockcount(frp);
6067 * Count leaf blocks given a range of extent records originally
6071 xfs_bmap_disk_count_leaves(
6072 struct xfs_mount *mp,
6073 struct xfs_btree_block *block,
6078 xfs_bmbt_rec_t *frp;
6080 for (b = 1; b <= numrecs; b++) {
6081 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6082 *count += xfs_bmbt_disk_get_blockcount(frp);
6087 * dead simple method of punching delalyed allocation blocks from a range in
6088 * the inode. Walks a block at a time so will be slow, but is only executed in
6089 * rare error cases so the overhead is not critical. This will alays punch out
6090 * both the start and end blocks, even if the ranges only partially overlap
6091 * them, so it is up to the caller to ensure that partial blocks are not
6095 xfs_bmap_punch_delalloc_range(
6096 struct xfs_inode *ip,
6097 xfs_fileoff_t start_fsb,
6098 xfs_fileoff_t length)
6100 xfs_fileoff_t remaining = length;
6103 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6107 xfs_bmbt_irec_t imap;
6109 xfs_fsblock_t firstblock;
6110 xfs_bmap_free_t flist;
6113 * Map the range first and check that it is a delalloc extent
6114 * before trying to unmap the range. Otherwise we will be
6115 * trying to remove a real extent (which requires a
6116 * transaction) or a hole, which is probably a bad idea...
6118 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6119 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6123 /* something screwed, just bail */
6124 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6125 xfs_alert(ip->i_mount,
6126 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6127 ip->i_ino, start_fsb);
6135 if (imap.br_startblock != DELAYSTARTBLOCK) {
6136 /* been converted, ignore */
6139 WARN_ON(imap.br_blockcount == 0);
6142 * Note: while we initialise the firstblock/flist pair, they
6143 * should never be used because blocks should never be
6144 * allocated or freed for a delalloc extent and hence we need
6145 * don't cancel or finish them after the xfs_bunmapi() call.
6147 xfs_bmap_init(&flist, &firstblock);
6148 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6153 ASSERT(!flist.xbf_count && !flist.xbf_first);
6157 } while(remaining > 0);