1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
17 #include "xfs_inode.h"
18 #include "xfs_btree.h"
19 #include "xfs_trans.h"
20 #include "xfs_alloc.h"
22 #include "xfs_bmap_util.h"
23 #include "xfs_bmap_btree.h"
24 #include "xfs_rtalloc.h"
25 #include "xfs_errortag.h"
26 #include "xfs_error.h"
27 #include "xfs_quota.h"
28 #include "xfs_trans_space.h"
29 #include "xfs_buf_item.h"
30 #include "xfs_trace.h"
31 #include "xfs_attr_leaf.h"
32 #include "xfs_filestream.h"
35 #include "xfs_ag_resv.h"
36 #include "xfs_refcount.h"
37 #include "xfs_icache.h"
38 #include "xfs_iomap.h"
40 struct kmem_cache *xfs_bmap_intent_cache;
43 * Miscellaneous helper functions
47 * Compute and fill in the value of the maximum depth of a bmap btree
48 * in this filesystem. Done once, during mount.
51 xfs_bmap_compute_maxlevels(
52 xfs_mount_t *mp, /* file system mount structure */
53 int whichfork) /* data or attr fork */
55 uint64_t maxblocks; /* max blocks at this level */
56 xfs_extnum_t maxleafents; /* max leaf entries possible */
57 int level; /* btree level */
58 int maxrootrecs; /* max records in root block */
59 int minleafrecs; /* min records in leaf block */
60 int minnoderecs; /* min records in node block */
61 int sz; /* root block size */
64 * The maximum number of extents in a fork, hence the maximum number of
65 * leaf entries, is controlled by the size of the on-disk extent count.
67 * Note that we can no longer assume that if we are in ATTR1 that the
68 * fork offset of all the inodes will be
69 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
70 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
71 * but probably at various positions. Therefore, for both ATTR1 and
72 * ATTR2 we have to assume the worst case scenario of a minimum size
75 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
77 if (whichfork == XFS_DATA_FORK)
78 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
80 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
82 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
83 minleafrecs = mp->m_bmap_dmnr[0];
84 minnoderecs = mp->m_bmap_dmnr[1];
85 maxblocks = howmany_64(maxleafents, minleafrecs);
86 for (level = 1; maxblocks > 1; level++) {
87 if (maxblocks <= maxrootrecs)
90 maxblocks = howmany_64(maxblocks, minnoderecs);
92 mp->m_bm_maxlevels[whichfork] = level;
93 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
97 xfs_bmap_compute_attr_offset(
100 if (mp->m_sb.sb_inodesize == 256)
101 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
102 return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
105 STATIC int /* error */
107 struct xfs_btree_cur *cur,
108 struct xfs_bmbt_irec *irec,
109 int *stat) /* success/failure */
111 cur->bc_rec.b = *irec;
112 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
115 STATIC int /* error */
116 xfs_bmbt_lookup_first(
117 struct xfs_btree_cur *cur,
118 int *stat) /* success/failure */
120 cur->bc_rec.b.br_startoff = 0;
121 cur->bc_rec.b.br_startblock = 0;
122 cur->bc_rec.b.br_blockcount = 0;
123 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
127 * Check if the inode needs to be converted to btree format.
129 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
131 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
133 return whichfork != XFS_COW_FORK &&
134 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
135 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
139 * Check if the inode should be converted to extent format.
141 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
143 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
145 return whichfork != XFS_COW_FORK &&
146 ifp->if_format == XFS_DINODE_FMT_BTREE &&
147 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
151 * Update the record referred to by cur to the value given by irec
152 * This either works (return 0) or gets an EFSCORRUPTED error.
156 struct xfs_btree_cur *cur,
157 struct xfs_bmbt_irec *irec)
159 union xfs_btree_rec rec;
161 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
162 return xfs_btree_update(cur, &rec);
166 * Compute the worst-case number of indirect blocks that will be used
167 * for ip's delayed extent of length "len".
170 xfs_bmap_worst_indlen(
171 xfs_inode_t *ip, /* incore inode pointer */
172 xfs_filblks_t len) /* delayed extent length */
174 int level; /* btree level number */
175 int maxrecs; /* maximum record count at this level */
176 xfs_mount_t *mp; /* mount structure */
177 xfs_filblks_t rval; /* return value */
180 maxrecs = mp->m_bmap_dmxr[0];
181 for (level = 0, rval = 0;
182 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
185 do_div(len, maxrecs);
188 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
191 maxrecs = mp->m_bmap_dmxr[1];
197 * Calculate the default attribute fork offset for newly created inodes.
200 xfs_default_attroffset(
201 struct xfs_inode *ip)
203 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
204 return roundup(sizeof(xfs_dev_t), 8);
205 return M_IGEO(ip->i_mount)->attr_fork_offset;
209 * Helper routine to reset inode i_forkoff field when switching attribute fork
210 * from local to extent format - we reset it where possible to make space
211 * available for inline data fork extents.
214 xfs_bmap_forkoff_reset(
218 if (whichfork == XFS_ATTR_FORK &&
219 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
220 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
221 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
223 if (dfl_forkoff > ip->i_forkoff)
224 ip->i_forkoff = dfl_forkoff;
229 STATIC struct xfs_buf *
231 struct xfs_btree_cur *cur,
234 struct xfs_log_item *lip;
240 for (i = 0; i < cur->bc_maxlevels; i++) {
241 if (!cur->bc_levels[i].bp)
243 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
244 return cur->bc_levels[i].bp;
247 /* Chase down all the log items to see if the bp is there */
248 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
249 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
251 if (bip->bli_item.li_type == XFS_LI_BUF &&
252 xfs_buf_daddr(bip->bli_buf) == bno)
261 struct xfs_btree_block *block,
267 __be64 *pp, *thispa; /* pointer to block address */
268 xfs_bmbt_key_t *prevp, *keyp;
270 ASSERT(be16_to_cpu(block->bb_level) > 0);
273 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
274 dmxr = mp->m_bmap_dmxr[0];
275 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
278 ASSERT(be64_to_cpu(prevp->br_startoff) <
279 be64_to_cpu(keyp->br_startoff));
284 * Compare the block numbers to see if there are dups.
287 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
289 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
291 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
293 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
295 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
296 if (*thispa == *pp) {
297 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
299 (unsigned long long)be64_to_cpu(*thispa));
300 xfs_err(mp, "%s: ptrs are equal in node\n",
302 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
309 * Check that the extents for the inode ip are in the right order in all
310 * btree leaves. THis becomes prohibitively expensive for large extent count
311 * files, so don't bother with inodes that have more than 10,000 extents in
312 * them. The btree record ordering checks will still be done, so for such large
313 * bmapbt constructs that is going to catch most corruptions.
316 xfs_bmap_check_leaf_extents(
317 struct xfs_btree_cur *cur, /* btree cursor or null */
318 xfs_inode_t *ip, /* incore inode pointer */
319 int whichfork) /* data or attr fork */
321 struct xfs_mount *mp = ip->i_mount;
322 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
323 struct xfs_btree_block *block; /* current btree block */
324 xfs_fsblock_t bno; /* block # of "block" */
325 struct xfs_buf *bp; /* buffer for "block" */
326 int error; /* error return value */
327 xfs_extnum_t i=0, j; /* index into the extents list */
328 int level; /* btree level, for checking */
329 __be64 *pp; /* pointer to block address */
330 xfs_bmbt_rec_t *ep; /* pointer to current extent */
331 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
332 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
335 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
338 /* skip large extent count inodes */
339 if (ip->i_df.if_nextents > 10000)
343 block = ifp->if_broot;
345 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
347 level = be16_to_cpu(block->bb_level);
349 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
350 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
351 bno = be64_to_cpu(*pp);
353 ASSERT(bno != NULLFSBLOCK);
354 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
355 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
358 * Go down the tree until leaf level is reached, following the first
359 * pointer (leftmost) at each level.
361 while (level-- > 0) {
362 /* See if buf is in cur first */
364 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
367 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
373 block = XFS_BUF_TO_BLOCK(bp);
378 * Check this block for basic sanity (increasing keys and
379 * no duplicate blocks).
382 xfs_check_block(block, mp, 0, 0);
383 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
384 bno = be64_to_cpu(*pp);
385 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
386 error = -EFSCORRUPTED;
391 xfs_trans_brelse(NULL, bp);
396 * Here with bp and block set to the leftmost leaf node in the tree.
401 * Loop over all leaf nodes checking that all extents are in the right order.
404 xfs_fsblock_t nextbno;
405 xfs_extnum_t num_recs;
408 num_recs = xfs_btree_get_numrecs(block);
411 * Read-ahead the next leaf block, if any.
414 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
417 * Check all the extents to make sure they are OK.
418 * If we had a previous block, the last entry should
419 * conform with the first entry in this one.
422 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
424 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
425 xfs_bmbt_disk_get_blockcount(&last) <=
426 xfs_bmbt_disk_get_startoff(ep));
428 for (j = 1; j < num_recs; j++) {
429 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
430 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
431 xfs_bmbt_disk_get_blockcount(ep) <=
432 xfs_bmbt_disk_get_startoff(nextp));
440 xfs_trans_brelse(NULL, bp);
444 * If we've reached the end, stop.
446 if (bno == NULLFSBLOCK)
450 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
453 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
459 block = XFS_BUF_TO_BLOCK(bp);
465 xfs_warn(mp, "%s: at error0", __func__);
467 xfs_trans_brelse(NULL, bp);
469 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
471 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
472 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
477 * Validate that the bmbt_irecs being returned from bmapi are valid
478 * given the caller's original parameters. Specifically check the
479 * ranges of the returned irecs to ensure that they only extend beyond
480 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
483 xfs_bmap_validate_ret(
487 xfs_bmbt_irec_t *mval,
491 int i; /* index to map values */
493 ASSERT(ret_nmap <= nmap);
495 for (i = 0; i < ret_nmap; i++) {
496 ASSERT(mval[i].br_blockcount > 0);
497 if (!(flags & XFS_BMAPI_ENTIRE)) {
498 ASSERT(mval[i].br_startoff >= bno);
499 ASSERT(mval[i].br_blockcount <= len);
500 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
503 ASSERT(mval[i].br_startoff < bno + len);
504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
508 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
509 mval[i].br_startoff);
510 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
511 mval[i].br_startblock != HOLESTARTBLOCK);
512 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
513 mval[i].br_state == XFS_EXT_UNWRITTEN);
518 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
519 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
523 * Inode fork format manipulation functions
527 * Convert the inode format to extent format if it currently is in btree format,
528 * but the extent list is small enough that it fits into the extent format.
530 * Since the extents are already in-core, all we have to do is give up the space
531 * for the btree root and pitch the leaf block.
533 STATIC int /* error */
534 xfs_bmap_btree_to_extents(
535 struct xfs_trans *tp, /* transaction pointer */
536 struct xfs_inode *ip, /* incore inode pointer */
537 struct xfs_btree_cur *cur, /* btree cursor */
538 int *logflagsp, /* inode logging flags */
539 int whichfork) /* data or attr fork */
541 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
542 struct xfs_mount *mp = ip->i_mount;
543 struct xfs_btree_block *rblock = ifp->if_broot;
544 struct xfs_btree_block *cblock;/* child btree block */
545 xfs_fsblock_t cbno; /* child block number */
546 struct xfs_buf *cbp; /* child block's buffer */
547 int error; /* error return value */
548 __be64 *pp; /* ptr to block address */
549 struct xfs_owner_info oinfo;
551 /* check if we actually need the extent format first: */
552 if (!xfs_bmap_wants_extents(ip, whichfork))
556 ASSERT(whichfork != XFS_COW_FORK);
557 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
558 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
559 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
560 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
562 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
563 cbno = be64_to_cpu(*pp);
565 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
566 return -EFSCORRUPTED;
568 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
572 cblock = XFS_BUF_TO_BLOCK(cbp);
573 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
576 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
577 error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
583 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
584 xfs_trans_binval(tp, cbp);
585 if (cur->bc_levels[0].bp == cbp)
586 cur->bc_levels[0].bp = NULL;
587 xfs_iroot_realloc(ip, -1, whichfork);
588 ASSERT(ifp->if_broot == NULL);
589 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
590 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
595 * Convert an extents-format file into a btree-format file.
596 * The new file will have a root block (in the inode) and a single child block.
598 STATIC int /* error */
599 xfs_bmap_extents_to_btree(
600 struct xfs_trans *tp, /* transaction pointer */
601 struct xfs_inode *ip, /* incore inode pointer */
602 struct xfs_btree_cur **curp, /* cursor returned to caller */
603 int wasdel, /* converting a delayed alloc */
604 int *logflagsp, /* inode logging flags */
605 int whichfork) /* data or attr fork */
607 struct xfs_btree_block *ablock; /* allocated (child) bt block */
608 struct xfs_buf *abp; /* buffer for ablock */
609 struct xfs_alloc_arg args; /* allocation arguments */
610 struct xfs_bmbt_rec *arp; /* child record pointer */
611 struct xfs_btree_block *block; /* btree root block */
612 struct xfs_btree_cur *cur; /* bmap btree cursor */
613 int error; /* error return value */
614 struct xfs_ifork *ifp; /* inode fork pointer */
615 struct xfs_bmbt_key *kp; /* root block key pointer */
616 struct xfs_mount *mp; /* mount structure */
617 xfs_bmbt_ptr_t *pp; /* root block address pointer */
618 struct xfs_iext_cursor icur;
619 struct xfs_bmbt_irec rec;
620 xfs_extnum_t cnt = 0;
623 ASSERT(whichfork != XFS_COW_FORK);
624 ifp = xfs_ifork_ptr(ip, whichfork);
625 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
628 * Make space in the inode incore. This needs to be undone if we fail
629 * to expand the root.
631 xfs_iroot_realloc(ip, 1, whichfork);
636 block = ifp->if_broot;
637 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
638 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
639 XFS_BTREE_LONG_PTRS);
641 * Need a cursor. Can't allocate until bb_level is filled in.
643 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
644 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
646 * Convert to a btree with two levels, one record in root.
648 ifp->if_format = XFS_DINODE_FMT_BTREE;
649 memset(&args, 0, sizeof(args));
652 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
654 args.minlen = args.maxlen = args.prod = 1;
655 args.wasdel = wasdel;
657 error = xfs_alloc_vextent_start_ag(&args,
658 XFS_INO_TO_FSB(mp, ip->i_ino));
660 goto out_root_realloc;
663 * Allocation can't fail, the space was reserved.
665 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
667 goto out_root_realloc;
670 cur->bc_ino.allocated++;
672 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
673 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
674 XFS_FSB_TO_DADDR(mp, args.fsbno),
675 mp->m_bsize, 0, &abp);
677 goto out_unreserve_dquot;
680 * Fill in the child block.
682 abp->b_ops = &xfs_bmbt_buf_ops;
683 ablock = XFS_BUF_TO_BLOCK(abp);
684 xfs_btree_init_block_int(mp, ablock, xfs_buf_daddr(abp),
685 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
686 XFS_BTREE_LONG_PTRS);
688 for_each_xfs_iext(ifp, &icur, &rec) {
689 if (isnullstartblock(rec.br_startblock))
691 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
692 xfs_bmbt_disk_set_all(arp, &rec);
695 ASSERT(cnt == ifp->if_nextents);
696 xfs_btree_set_numrecs(ablock, cnt);
699 * Fill in the root key and pointer.
701 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
702 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
703 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
704 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
705 be16_to_cpu(block->bb_level)));
706 *pp = cpu_to_be64(args.fsbno);
709 * Do all this logging at the end so that
710 * the root is at the right level.
712 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
713 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
714 ASSERT(*curp == NULL);
716 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
720 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
722 xfs_iroot_realloc(ip, -1, whichfork);
723 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
724 ASSERT(ifp->if_broot == NULL);
725 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
731 * Convert a local file to an extents file.
732 * This code is out of bounds for data forks of regular files,
733 * since the file data needs to get logged so things will stay consistent.
734 * (The bmap-level manipulations are ok, though).
737 xfs_bmap_local_to_extents_empty(
738 struct xfs_trans *tp,
739 struct xfs_inode *ip,
742 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
744 ASSERT(whichfork != XFS_COW_FORK);
745 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
746 ASSERT(ifp->if_bytes == 0);
747 ASSERT(ifp->if_nextents == 0);
749 xfs_bmap_forkoff_reset(ip, whichfork);
750 ifp->if_u1.if_root = NULL;
752 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
753 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
757 STATIC int /* error */
758 xfs_bmap_local_to_extents(
759 xfs_trans_t *tp, /* transaction pointer */
760 xfs_inode_t *ip, /* incore inode pointer */
761 xfs_extlen_t total, /* total blocks needed by transaction */
762 int *logflagsp, /* inode logging flags */
764 void (*init_fn)(struct xfs_trans *tp,
766 struct xfs_inode *ip,
767 struct xfs_ifork *ifp))
770 int flags; /* logging flags returned */
771 struct xfs_ifork *ifp; /* inode fork pointer */
772 xfs_alloc_arg_t args; /* allocation arguments */
773 struct xfs_buf *bp; /* buffer for extent block */
774 struct xfs_bmbt_irec rec;
775 struct xfs_iext_cursor icur;
778 * We don't want to deal with the case of keeping inode data inline yet.
779 * So sending the data fork of a regular inode is invalid.
781 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
782 ifp = xfs_ifork_ptr(ip, whichfork);
783 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
785 if (!ifp->if_bytes) {
786 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
787 flags = XFS_ILOG_CORE;
793 memset(&args, 0, sizeof(args));
795 args.mp = ip->i_mount;
797 args.minlen = args.maxlen = args.prod = 1;
798 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
801 * Allocate a block. We know we need only one, since the
802 * file currently fits in an inode.
805 args.minlen = args.maxlen = args.prod = 1;
806 error = xfs_alloc_vextent_start_ag(&args,
807 XFS_INO_TO_FSB(args.mp, ip->i_ino));
811 /* Can't fail, the space was reserved. */
812 ASSERT(args.fsbno != NULLFSBLOCK);
813 ASSERT(args.len == 1);
814 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
815 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
816 args.mp->m_bsize, 0, &bp);
821 * Initialize the block, copy the data and log the remote buffer.
823 * The callout is responsible for logging because the remote format
824 * might differ from the local format and thus we don't know how much to
825 * log here. Note that init_fn must also set the buffer log item type
828 init_fn(tp, bp, ip, ifp);
830 /* account for the change in fork size */
831 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
832 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
833 flags |= XFS_ILOG_CORE;
835 ifp->if_u1.if_root = NULL;
839 rec.br_startblock = args.fsbno;
840 rec.br_blockcount = 1;
841 rec.br_state = XFS_EXT_NORM;
842 xfs_iext_first(ifp, &icur);
843 xfs_iext_insert(ip, &icur, &rec, 0);
845 ifp->if_nextents = 1;
847 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
848 flags |= xfs_ilog_fext(whichfork);
856 * Called from xfs_bmap_add_attrfork to handle btree format files.
858 STATIC int /* error */
859 xfs_bmap_add_attrfork_btree(
860 xfs_trans_t *tp, /* transaction pointer */
861 xfs_inode_t *ip, /* incore inode pointer */
862 int *flags) /* inode logging flags */
864 struct xfs_btree_block *block = ip->i_df.if_broot;
865 struct xfs_btree_cur *cur; /* btree cursor */
866 int error; /* error return value */
867 xfs_mount_t *mp; /* file system mount struct */
868 int stat; /* newroot status */
872 if (XFS_BMAP_BMDR_SPACE(block) <= xfs_inode_data_fork_size(ip))
873 *flags |= XFS_ILOG_DBROOT;
875 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
876 error = xfs_bmbt_lookup_first(cur, &stat);
879 /* must be at least one entry */
880 if (XFS_IS_CORRUPT(mp, stat != 1)) {
881 error = -EFSCORRUPTED;
884 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
887 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
890 cur->bc_ino.allocated = 0;
891 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
895 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
900 * Called from xfs_bmap_add_attrfork to handle extents format files.
902 STATIC int /* error */
903 xfs_bmap_add_attrfork_extents(
904 struct xfs_trans *tp, /* transaction pointer */
905 struct xfs_inode *ip, /* incore inode pointer */
906 int *flags) /* inode logging flags */
908 struct xfs_btree_cur *cur; /* bmap btree cursor */
909 int error; /* error return value */
911 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
912 xfs_inode_data_fork_size(ip))
915 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
918 cur->bc_ino.allocated = 0;
919 xfs_btree_del_cursor(cur, error);
925 * Called from xfs_bmap_add_attrfork to handle local format files. Each
926 * different data fork content type needs a different callout to do the
927 * conversion. Some are basic and only require special block initialisation
928 * callouts for the data formating, others (directories) are so specialised they
929 * handle everything themselves.
931 * XXX (dgc): investigate whether directory conversion can use the generic
932 * formatting callout. It should be possible - it's just a very complex
935 STATIC int /* error */
936 xfs_bmap_add_attrfork_local(
937 struct xfs_trans *tp, /* transaction pointer */
938 struct xfs_inode *ip, /* incore inode pointer */
939 int *flags) /* inode logging flags */
941 struct xfs_da_args dargs; /* args for dir/attr code */
943 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
946 if (S_ISDIR(VFS_I(ip)->i_mode)) {
947 memset(&dargs, 0, sizeof(dargs));
948 dargs.geo = ip->i_mount->m_dir_geo;
950 dargs.total = dargs.geo->fsbcount;
951 dargs.whichfork = XFS_DATA_FORK;
953 return xfs_dir2_sf_to_block(&dargs);
956 if (S_ISLNK(VFS_I(ip)->i_mode))
957 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
959 xfs_symlink_local_to_remote);
961 /* should only be called for types that support local format data */
963 return -EFSCORRUPTED;
967 * Set an inode attr fork offset based on the format of the data fork.
970 xfs_bmap_set_attrforkoff(
971 struct xfs_inode *ip,
975 int default_size = xfs_default_attroffset(ip) >> 3;
977 switch (ip->i_df.if_format) {
978 case XFS_DINODE_FMT_DEV:
979 ip->i_forkoff = default_size;
981 case XFS_DINODE_FMT_LOCAL:
982 case XFS_DINODE_FMT_EXTENTS:
983 case XFS_DINODE_FMT_BTREE:
984 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
986 ip->i_forkoff = default_size;
987 else if (xfs_has_attr2(ip->i_mount) && version)
999 * Convert inode from non-attributed to attributed.
1000 * Must not be in a transaction, ip must not be locked.
1002 int /* error code */
1003 xfs_bmap_add_attrfork(
1004 xfs_inode_t *ip, /* incore inode pointer */
1005 int size, /* space new attribute needs */
1006 int rsvd) /* xact may use reserved blks */
1008 xfs_mount_t *mp; /* mount structure */
1009 xfs_trans_t *tp; /* transaction pointer */
1010 int blks; /* space reservation */
1011 int version = 1; /* superblock attr version */
1012 int logflags; /* logging flags */
1013 int error; /* error return value */
1015 ASSERT(xfs_inode_has_attr_fork(ip) == 0);
1018 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1020 blks = XFS_ADDAFORK_SPACE_RES(mp);
1022 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
1026 if (xfs_inode_has_attr_fork(ip))
1029 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1030 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1034 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
1036 switch (ip->i_df.if_format) {
1037 case XFS_DINODE_FMT_LOCAL:
1038 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1040 case XFS_DINODE_FMT_EXTENTS:
1041 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1043 case XFS_DINODE_FMT_BTREE:
1044 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1051 xfs_trans_log_inode(tp, ip, logflags);
1054 if (!xfs_has_attr(mp) ||
1055 (!xfs_has_attr2(mp) && version == 2)) {
1056 bool log_sb = false;
1058 spin_lock(&mp->m_sb_lock);
1059 if (!xfs_has_attr(mp)) {
1063 if (!xfs_has_attr2(mp) && version == 2) {
1067 spin_unlock(&mp->m_sb_lock);
1072 error = xfs_trans_commit(tp);
1073 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1077 xfs_trans_cancel(tp);
1078 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1083 * Internal and external extent tree search functions.
1086 struct xfs_iread_state {
1087 struct xfs_iext_cursor icur;
1088 xfs_extnum_t loaded;
1092 xfs_bmap_complain_bad_rec(
1093 struct xfs_inode *ip,
1096 const struct xfs_bmbt_irec *irec)
1098 struct xfs_mount *mp = ip->i_mount;
1099 const char *forkname;
1101 switch (whichfork) {
1102 case XFS_DATA_FORK: forkname = "data"; break;
1103 case XFS_ATTR_FORK: forkname = "attr"; break;
1104 case XFS_COW_FORK: forkname = "CoW"; break;
1105 default: forkname = "???"; break;
1109 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1110 ip->i_ino, forkname, fa);
1112 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1113 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1116 return -EFSCORRUPTED;
1119 /* Stuff every bmbt record from this block into the incore extent map. */
1121 xfs_iread_bmbt_block(
1122 struct xfs_btree_cur *cur,
1126 struct xfs_iread_state *ir = priv;
1127 struct xfs_mount *mp = cur->bc_mp;
1128 struct xfs_inode *ip = cur->bc_ino.ip;
1129 struct xfs_btree_block *block;
1131 struct xfs_bmbt_rec *frp;
1132 xfs_extnum_t num_recs;
1134 int whichfork = cur->bc_ino.whichfork;
1135 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1137 block = xfs_btree_get_block(cur, level, &bp);
1139 /* Abort if we find more records than nextents. */
1140 num_recs = xfs_btree_get_numrecs(block);
1141 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1142 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1143 (unsigned long long)ip->i_ino);
1144 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1145 sizeof(*block), __this_address);
1146 return -EFSCORRUPTED;
1149 /* Copy records into the incore cache. */
1150 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1151 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1152 struct xfs_bmbt_irec new;
1155 xfs_bmbt_disk_get_all(frp, &new);
1156 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1158 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1159 "xfs_iread_extents(2)", frp,
1161 return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1164 xfs_iext_insert(ip, &ir->icur, &new,
1165 xfs_bmap_fork_to_state(whichfork));
1166 trace_xfs_read_extent(ip, &ir->icur,
1167 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1168 xfs_iext_next(ifp, &ir->icur);
1175 * Read in extents from a btree-format inode.
1179 struct xfs_trans *tp,
1180 struct xfs_inode *ip,
1183 struct xfs_iread_state ir;
1184 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1185 struct xfs_mount *mp = ip->i_mount;
1186 struct xfs_btree_cur *cur;
1189 if (!xfs_need_iread_extents(ifp))
1192 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1195 xfs_iext_first(ifp, &ir.icur);
1196 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1197 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1198 XFS_BTREE_VISIT_RECORDS, &ir);
1199 xfs_btree_del_cursor(cur, error);
1203 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1204 error = -EFSCORRUPTED;
1207 ASSERT(ir.loaded == xfs_iext_count(ifp));
1209 * Use release semantics so that we can use acquire semantics in
1210 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1213 smp_store_release(&ifp->if_needextents, 0);
1216 xfs_iext_destroy(ifp);
1221 * Returns the relative block number of the first unused block(s) in the given
1222 * fork with at least "len" logically contiguous blocks free. This is the
1223 * lowest-address hole if the fork has holes, else the first block past the end
1224 * of fork. Return 0 if the fork is currently local (in-inode).
1227 xfs_bmap_first_unused(
1228 struct xfs_trans *tp, /* transaction pointer */
1229 struct xfs_inode *ip, /* incore inode */
1230 xfs_extlen_t len, /* size of hole to find */
1231 xfs_fileoff_t *first_unused, /* unused block */
1232 int whichfork) /* data or attr fork */
1234 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1235 struct xfs_bmbt_irec got;
1236 struct xfs_iext_cursor icur;
1237 xfs_fileoff_t lastaddr = 0;
1238 xfs_fileoff_t lowest, max;
1241 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1246 ASSERT(xfs_ifork_has_extents(ifp));
1248 error = xfs_iread_extents(tp, ip, whichfork);
1252 lowest = max = *first_unused;
1253 for_each_xfs_iext(ifp, &icur, &got) {
1255 * See if the hole before this extent will work.
1257 if (got.br_startoff >= lowest + len &&
1258 got.br_startoff - max >= len)
1260 lastaddr = got.br_startoff + got.br_blockcount;
1261 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1264 *first_unused = max;
1269 * Returns the file-relative block number of the last block - 1 before
1270 * last_block (input value) in the file.
1271 * This is not based on i_size, it is based on the extent records.
1272 * Returns 0 for local files, as they do not have extent records.
1275 xfs_bmap_last_before(
1276 struct xfs_trans *tp, /* transaction pointer */
1277 struct xfs_inode *ip, /* incore inode */
1278 xfs_fileoff_t *last_block, /* last block */
1279 int whichfork) /* data or attr fork */
1281 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1282 struct xfs_bmbt_irec got;
1283 struct xfs_iext_cursor icur;
1286 switch (ifp->if_format) {
1287 case XFS_DINODE_FMT_LOCAL:
1290 case XFS_DINODE_FMT_BTREE:
1291 case XFS_DINODE_FMT_EXTENTS:
1295 return -EFSCORRUPTED;
1298 error = xfs_iread_extents(tp, ip, whichfork);
1302 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1308 xfs_bmap_last_extent(
1309 struct xfs_trans *tp,
1310 struct xfs_inode *ip,
1312 struct xfs_bmbt_irec *rec,
1315 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1316 struct xfs_iext_cursor icur;
1319 error = xfs_iread_extents(tp, ip, whichfork);
1323 xfs_iext_last(ifp, &icur);
1324 if (!xfs_iext_get_extent(ifp, &icur, rec))
1332 * Check the last inode extent to determine whether this allocation will result
1333 * in blocks being allocated at the end of the file. When we allocate new data
1334 * blocks at the end of the file which do not start at the previous data block,
1335 * we will try to align the new blocks at stripe unit boundaries.
1337 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1338 * at, or past the EOF.
1342 struct xfs_bmalloca *bma,
1345 struct xfs_bmbt_irec rec;
1350 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1361 * Check if we are allocation or past the last extent, or at least into
1362 * the last delayed allocated extent.
1364 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1365 (bma->offset >= rec.br_startoff &&
1366 isnullstartblock(rec.br_startblock));
1371 * Returns the file-relative block number of the first block past eof in
1372 * the file. This is not based on i_size, it is based on the extent records.
1373 * Returns 0 for local files, as they do not have extent records.
1376 xfs_bmap_last_offset(
1377 struct xfs_inode *ip,
1378 xfs_fileoff_t *last_block,
1381 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1382 struct xfs_bmbt_irec rec;
1388 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1391 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
1392 return -EFSCORRUPTED;
1394 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1395 if (error || is_empty)
1398 *last_block = rec.br_startoff + rec.br_blockcount;
1403 * Extent tree manipulation functions used during allocation.
1407 * Convert a delayed allocation to a real allocation.
1409 STATIC int /* error */
1410 xfs_bmap_add_extent_delay_real(
1411 struct xfs_bmalloca *bma,
1414 struct xfs_mount *mp = bma->ip->i_mount;
1415 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
1416 struct xfs_bmbt_irec *new = &bma->got;
1417 int error; /* error return value */
1418 int i; /* temp state */
1419 xfs_fileoff_t new_endoff; /* end offset of new entry */
1420 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1421 /* left is 0, right is 1, prev is 2 */
1422 int rval=0; /* return value (logging flags) */
1423 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1424 xfs_filblks_t da_new; /* new count del alloc blocks used */
1425 xfs_filblks_t da_old; /* old count del alloc blocks used */
1426 xfs_filblks_t temp=0; /* value for da_new calculations */
1427 int tmp_rval; /* partial logging flags */
1428 struct xfs_bmbt_irec old;
1430 ASSERT(whichfork != XFS_ATTR_FORK);
1431 ASSERT(!isnullstartblock(new->br_startblock));
1433 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1435 XFS_STATS_INC(mp, xs_add_exlist);
1442 * Set up a bunch of variables to make the tests simpler.
1444 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1445 new_endoff = new->br_startoff + new->br_blockcount;
1446 ASSERT(isnullstartblock(PREV.br_startblock));
1447 ASSERT(PREV.br_startoff <= new->br_startoff);
1448 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1450 da_old = startblockval(PREV.br_startblock);
1454 * Set flags determining what part of the previous delayed allocation
1455 * extent is being replaced by a real allocation.
1457 if (PREV.br_startoff == new->br_startoff)
1458 state |= BMAP_LEFT_FILLING;
1459 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1460 state |= BMAP_RIGHT_FILLING;
1463 * Check and set flags if this segment has a left neighbor.
1464 * Don't set contiguous if the combined extent would be too large.
1466 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1467 state |= BMAP_LEFT_VALID;
1468 if (isnullstartblock(LEFT.br_startblock))
1469 state |= BMAP_LEFT_DELAY;
1472 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1473 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1474 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1475 LEFT.br_state == new->br_state &&
1476 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1477 state |= BMAP_LEFT_CONTIG;
1480 * Check and set flags if this segment has a right neighbor.
1481 * Don't set contiguous if the combined extent would be too large.
1482 * Also check for all-three-contiguous being too large.
1484 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1485 state |= BMAP_RIGHT_VALID;
1486 if (isnullstartblock(RIGHT.br_startblock))
1487 state |= BMAP_RIGHT_DELAY;
1490 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1491 new_endoff == RIGHT.br_startoff &&
1492 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1493 new->br_state == RIGHT.br_state &&
1494 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1495 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1496 BMAP_RIGHT_FILLING)) !=
1497 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1498 BMAP_RIGHT_FILLING) ||
1499 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1500 <= XFS_MAX_BMBT_EXTLEN))
1501 state |= BMAP_RIGHT_CONTIG;
1505 * Switch out based on the FILLING and CONTIG state bits.
1507 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1508 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1509 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1510 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1512 * Filling in all of a previously delayed allocation extent.
1513 * The left and right neighbors are both contiguous with new.
1515 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1517 xfs_iext_remove(bma->ip, &bma->icur, state);
1518 xfs_iext_remove(bma->ip, &bma->icur, state);
1519 xfs_iext_prev(ifp, &bma->icur);
1520 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1523 if (bma->cur == NULL)
1524 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1526 rval = XFS_ILOG_CORE;
1527 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1530 if (XFS_IS_CORRUPT(mp, i != 1)) {
1531 error = -EFSCORRUPTED;
1534 error = xfs_btree_delete(bma->cur, &i);
1537 if (XFS_IS_CORRUPT(mp, i != 1)) {
1538 error = -EFSCORRUPTED;
1541 error = xfs_btree_decrement(bma->cur, 0, &i);
1544 if (XFS_IS_CORRUPT(mp, i != 1)) {
1545 error = -EFSCORRUPTED;
1548 error = xfs_bmbt_update(bma->cur, &LEFT);
1554 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1556 * Filling in all of a previously delayed allocation extent.
1557 * The left neighbor is contiguous, the right is not.
1560 LEFT.br_blockcount += PREV.br_blockcount;
1562 xfs_iext_remove(bma->ip, &bma->icur, state);
1563 xfs_iext_prev(ifp, &bma->icur);
1564 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1566 if (bma->cur == NULL)
1567 rval = XFS_ILOG_DEXT;
1570 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1573 if (XFS_IS_CORRUPT(mp, i != 1)) {
1574 error = -EFSCORRUPTED;
1577 error = xfs_bmbt_update(bma->cur, &LEFT);
1583 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1585 * Filling in all of a previously delayed allocation extent.
1586 * The right neighbor is contiguous, the left is not. Take care
1587 * with delay -> unwritten extent allocation here because the
1588 * delalloc record we are overwriting is always written.
1590 PREV.br_startblock = new->br_startblock;
1591 PREV.br_blockcount += RIGHT.br_blockcount;
1592 PREV.br_state = new->br_state;
1594 xfs_iext_next(ifp, &bma->icur);
1595 xfs_iext_remove(bma->ip, &bma->icur, state);
1596 xfs_iext_prev(ifp, &bma->icur);
1597 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1599 if (bma->cur == NULL)
1600 rval = XFS_ILOG_DEXT;
1603 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1606 if (XFS_IS_CORRUPT(mp, i != 1)) {
1607 error = -EFSCORRUPTED;
1610 error = xfs_bmbt_update(bma->cur, &PREV);
1616 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1618 * Filling in all of a previously delayed allocation extent.
1619 * Neither the left nor right neighbors are contiguous with
1622 PREV.br_startblock = new->br_startblock;
1623 PREV.br_state = new->br_state;
1624 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1627 if (bma->cur == NULL)
1628 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1630 rval = XFS_ILOG_CORE;
1631 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1634 if (XFS_IS_CORRUPT(mp, i != 0)) {
1635 error = -EFSCORRUPTED;
1638 error = xfs_btree_insert(bma->cur, &i);
1641 if (XFS_IS_CORRUPT(mp, i != 1)) {
1642 error = -EFSCORRUPTED;
1648 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1650 * Filling in the first part of a previous delayed allocation.
1651 * The left neighbor is contiguous.
1654 temp = PREV.br_blockcount - new->br_blockcount;
1655 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1656 startblockval(PREV.br_startblock));
1658 LEFT.br_blockcount += new->br_blockcount;
1660 PREV.br_blockcount = temp;
1661 PREV.br_startoff += new->br_blockcount;
1662 PREV.br_startblock = nullstartblock(da_new);
1664 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1665 xfs_iext_prev(ifp, &bma->icur);
1666 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1668 if (bma->cur == NULL)
1669 rval = XFS_ILOG_DEXT;
1672 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1675 if (XFS_IS_CORRUPT(mp, i != 1)) {
1676 error = -EFSCORRUPTED;
1679 error = xfs_bmbt_update(bma->cur, &LEFT);
1685 case BMAP_LEFT_FILLING:
1687 * Filling in the first part of a previous delayed allocation.
1688 * The left neighbor is not contiguous.
1690 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1693 if (bma->cur == NULL)
1694 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1696 rval = XFS_ILOG_CORE;
1697 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1700 if (XFS_IS_CORRUPT(mp, i != 0)) {
1701 error = -EFSCORRUPTED;
1704 error = xfs_btree_insert(bma->cur, &i);
1707 if (XFS_IS_CORRUPT(mp, i != 1)) {
1708 error = -EFSCORRUPTED;
1713 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1714 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1715 &bma->cur, 1, &tmp_rval, whichfork);
1721 temp = PREV.br_blockcount - new->br_blockcount;
1722 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1723 startblockval(PREV.br_startblock) -
1724 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1726 PREV.br_startoff = new_endoff;
1727 PREV.br_blockcount = temp;
1728 PREV.br_startblock = nullstartblock(da_new);
1729 xfs_iext_next(ifp, &bma->icur);
1730 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1731 xfs_iext_prev(ifp, &bma->icur);
1734 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1736 * Filling in the last part of a previous delayed allocation.
1737 * The right neighbor is contiguous with the new allocation.
1740 RIGHT.br_startoff = new->br_startoff;
1741 RIGHT.br_startblock = new->br_startblock;
1742 RIGHT.br_blockcount += new->br_blockcount;
1744 if (bma->cur == NULL)
1745 rval = XFS_ILOG_DEXT;
1748 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1751 if (XFS_IS_CORRUPT(mp, i != 1)) {
1752 error = -EFSCORRUPTED;
1755 error = xfs_bmbt_update(bma->cur, &RIGHT);
1760 temp = PREV.br_blockcount - new->br_blockcount;
1761 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1762 startblockval(PREV.br_startblock));
1764 PREV.br_blockcount = temp;
1765 PREV.br_startblock = nullstartblock(da_new);
1767 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1768 xfs_iext_next(ifp, &bma->icur);
1769 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1772 case BMAP_RIGHT_FILLING:
1774 * Filling in the last part of a previous delayed allocation.
1775 * The right neighbor is not contiguous.
1777 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1780 if (bma->cur == NULL)
1781 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1783 rval = XFS_ILOG_CORE;
1784 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1787 if (XFS_IS_CORRUPT(mp, i != 0)) {
1788 error = -EFSCORRUPTED;
1791 error = xfs_btree_insert(bma->cur, &i);
1794 if (XFS_IS_CORRUPT(mp, i != 1)) {
1795 error = -EFSCORRUPTED;
1800 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1801 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1802 &bma->cur, 1, &tmp_rval, whichfork);
1808 temp = PREV.br_blockcount - new->br_blockcount;
1809 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1810 startblockval(PREV.br_startblock) -
1811 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1813 PREV.br_startblock = nullstartblock(da_new);
1814 PREV.br_blockcount = temp;
1815 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1816 xfs_iext_next(ifp, &bma->icur);
1821 * Filling in the middle part of a previous delayed allocation.
1822 * Contiguity is impossible here.
1823 * This case is avoided almost all the time.
1825 * We start with a delayed allocation:
1827 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1830 * and we are allocating:
1831 * +rrrrrrrrrrrrrrrrr+
1834 * and we set it up for insertion as:
1835 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1837 * PREV @ idx LEFT RIGHT
1838 * inserted at idx + 1
1842 /* LEFT is the new middle */
1845 /* RIGHT is the new right */
1846 RIGHT.br_state = PREV.br_state;
1847 RIGHT.br_startoff = new_endoff;
1848 RIGHT.br_blockcount =
1849 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1850 RIGHT.br_startblock =
1851 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1852 RIGHT.br_blockcount));
1855 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1856 PREV.br_startblock =
1857 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1858 PREV.br_blockcount));
1859 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1861 xfs_iext_next(ifp, &bma->icur);
1862 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1863 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1866 if (bma->cur == NULL)
1867 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1869 rval = XFS_ILOG_CORE;
1870 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1873 if (XFS_IS_CORRUPT(mp, i != 0)) {
1874 error = -EFSCORRUPTED;
1877 error = xfs_btree_insert(bma->cur, &i);
1880 if (XFS_IS_CORRUPT(mp, i != 1)) {
1881 error = -EFSCORRUPTED;
1886 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1887 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1888 &bma->cur, 1, &tmp_rval, whichfork);
1894 da_new = startblockval(PREV.br_startblock) +
1895 startblockval(RIGHT.br_startblock);
1898 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1899 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1900 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1901 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1902 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1903 case BMAP_LEFT_CONTIG:
1904 case BMAP_RIGHT_CONTIG:
1906 * These cases are all impossible.
1911 /* add reverse mapping unless caller opted out */
1912 if (!(bma->flags & XFS_BMAPI_NORMAP))
1913 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1915 /* convert to a btree if necessary */
1916 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1917 int tmp_logflags; /* partial log flag return val */
1919 ASSERT(bma->cur == NULL);
1920 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1921 &bma->cur, da_old > 0, &tmp_logflags,
1923 bma->logflags |= tmp_logflags;
1928 if (da_new != da_old)
1929 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
1932 da_new += bma->cur->bc_ino.allocated;
1933 bma->cur->bc_ino.allocated = 0;
1936 /* adjust for changes in reserved delayed indirect blocks */
1937 if (da_new != da_old) {
1938 ASSERT(state == 0 || da_new < da_old);
1939 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
1943 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1945 if (whichfork != XFS_COW_FORK)
1946 bma->logflags |= rval;
1954 * Convert an unwritten allocation to a real allocation or vice versa.
1957 xfs_bmap_add_extent_unwritten_real(
1958 struct xfs_trans *tp,
1959 xfs_inode_t *ip, /* incore inode pointer */
1961 struct xfs_iext_cursor *icur,
1962 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
1963 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1964 int *logflagsp) /* inode logging flags */
1966 struct xfs_btree_cur *cur; /* btree cursor */
1967 int error; /* error return value */
1968 int i; /* temp state */
1969 struct xfs_ifork *ifp; /* inode fork pointer */
1970 xfs_fileoff_t new_endoff; /* end offset of new entry */
1971 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1972 /* left is 0, right is 1, prev is 2 */
1973 int rval=0; /* return value (logging flags) */
1974 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1975 struct xfs_mount *mp = ip->i_mount;
1976 struct xfs_bmbt_irec old;
1981 ifp = xfs_ifork_ptr(ip, whichfork);
1983 ASSERT(!isnullstartblock(new->br_startblock));
1985 XFS_STATS_INC(mp, xs_add_exlist);
1992 * Set up a bunch of variables to make the tests simpler.
1995 xfs_iext_get_extent(ifp, icur, &PREV);
1996 ASSERT(new->br_state != PREV.br_state);
1997 new_endoff = new->br_startoff + new->br_blockcount;
1998 ASSERT(PREV.br_startoff <= new->br_startoff);
1999 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2002 * Set flags determining what part of the previous oldext allocation
2003 * extent is being replaced by a newext allocation.
2005 if (PREV.br_startoff == new->br_startoff)
2006 state |= BMAP_LEFT_FILLING;
2007 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2008 state |= BMAP_RIGHT_FILLING;
2011 * Check and set flags if this segment has a left neighbor.
2012 * Don't set contiguous if the combined extent would be too large.
2014 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2015 state |= BMAP_LEFT_VALID;
2016 if (isnullstartblock(LEFT.br_startblock))
2017 state |= BMAP_LEFT_DELAY;
2020 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2021 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2022 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2023 LEFT.br_state == new->br_state &&
2024 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2025 state |= BMAP_LEFT_CONTIG;
2028 * Check and set flags if this segment has a right neighbor.
2029 * Don't set contiguous if the combined extent would be too large.
2030 * Also check for all-three-contiguous being too large.
2032 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2033 state |= BMAP_RIGHT_VALID;
2034 if (isnullstartblock(RIGHT.br_startblock))
2035 state |= BMAP_RIGHT_DELAY;
2038 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2039 new_endoff == RIGHT.br_startoff &&
2040 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2041 new->br_state == RIGHT.br_state &&
2042 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2043 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2044 BMAP_RIGHT_FILLING)) !=
2045 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2046 BMAP_RIGHT_FILLING) ||
2047 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2048 <= XFS_MAX_BMBT_EXTLEN))
2049 state |= BMAP_RIGHT_CONTIG;
2052 * Switch out based on the FILLING and CONTIG state bits.
2054 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2055 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2056 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2057 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2059 * Setting all of a previous oldext extent to newext.
2060 * The left and right neighbors are both contiguous with new.
2062 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2064 xfs_iext_remove(ip, icur, state);
2065 xfs_iext_remove(ip, icur, state);
2066 xfs_iext_prev(ifp, icur);
2067 xfs_iext_update_extent(ip, state, icur, &LEFT);
2068 ifp->if_nextents -= 2;
2070 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2072 rval = XFS_ILOG_CORE;
2073 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2076 if (XFS_IS_CORRUPT(mp, i != 1)) {
2077 error = -EFSCORRUPTED;
2080 if ((error = xfs_btree_delete(cur, &i)))
2082 if (XFS_IS_CORRUPT(mp, i != 1)) {
2083 error = -EFSCORRUPTED;
2086 if ((error = xfs_btree_decrement(cur, 0, &i)))
2088 if (XFS_IS_CORRUPT(mp, i != 1)) {
2089 error = -EFSCORRUPTED;
2092 if ((error = xfs_btree_delete(cur, &i)))
2094 if (XFS_IS_CORRUPT(mp, i != 1)) {
2095 error = -EFSCORRUPTED;
2098 if ((error = xfs_btree_decrement(cur, 0, &i)))
2100 if (XFS_IS_CORRUPT(mp, i != 1)) {
2101 error = -EFSCORRUPTED;
2104 error = xfs_bmbt_update(cur, &LEFT);
2110 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2112 * Setting all of a previous oldext extent to newext.
2113 * The left neighbor is contiguous, the right is not.
2115 LEFT.br_blockcount += PREV.br_blockcount;
2117 xfs_iext_remove(ip, icur, state);
2118 xfs_iext_prev(ifp, icur);
2119 xfs_iext_update_extent(ip, state, icur, &LEFT);
2122 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2124 rval = XFS_ILOG_CORE;
2125 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2128 if (XFS_IS_CORRUPT(mp, i != 1)) {
2129 error = -EFSCORRUPTED;
2132 if ((error = xfs_btree_delete(cur, &i)))
2134 if (XFS_IS_CORRUPT(mp, i != 1)) {
2135 error = -EFSCORRUPTED;
2138 if ((error = xfs_btree_decrement(cur, 0, &i)))
2140 if (XFS_IS_CORRUPT(mp, i != 1)) {
2141 error = -EFSCORRUPTED;
2144 error = xfs_bmbt_update(cur, &LEFT);
2150 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2152 * Setting all of a previous oldext extent to newext.
2153 * The right neighbor is contiguous, the left is not.
2155 PREV.br_blockcount += RIGHT.br_blockcount;
2156 PREV.br_state = new->br_state;
2158 xfs_iext_next(ifp, icur);
2159 xfs_iext_remove(ip, icur, state);
2160 xfs_iext_prev(ifp, icur);
2161 xfs_iext_update_extent(ip, state, icur, &PREV);
2165 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2167 rval = XFS_ILOG_CORE;
2168 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2171 if (XFS_IS_CORRUPT(mp, i != 1)) {
2172 error = -EFSCORRUPTED;
2175 if ((error = xfs_btree_delete(cur, &i)))
2177 if (XFS_IS_CORRUPT(mp, i != 1)) {
2178 error = -EFSCORRUPTED;
2181 if ((error = xfs_btree_decrement(cur, 0, &i)))
2183 if (XFS_IS_CORRUPT(mp, i != 1)) {
2184 error = -EFSCORRUPTED;
2187 error = xfs_bmbt_update(cur, &PREV);
2193 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2195 * Setting all of a previous oldext extent to newext.
2196 * Neither the left nor right neighbors are contiguous with
2199 PREV.br_state = new->br_state;
2200 xfs_iext_update_extent(ip, state, icur, &PREV);
2203 rval = XFS_ILOG_DEXT;
2206 error = xfs_bmbt_lookup_eq(cur, new, &i);
2209 if (XFS_IS_CORRUPT(mp, i != 1)) {
2210 error = -EFSCORRUPTED;
2213 error = xfs_bmbt_update(cur, &PREV);
2219 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2221 * Setting the first part of a previous oldext extent to newext.
2222 * The left neighbor is contiguous.
2224 LEFT.br_blockcount += new->br_blockcount;
2227 PREV.br_startoff += new->br_blockcount;
2228 PREV.br_startblock += new->br_blockcount;
2229 PREV.br_blockcount -= new->br_blockcount;
2231 xfs_iext_update_extent(ip, state, icur, &PREV);
2232 xfs_iext_prev(ifp, icur);
2233 xfs_iext_update_extent(ip, state, icur, &LEFT);
2236 rval = XFS_ILOG_DEXT;
2239 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2242 if (XFS_IS_CORRUPT(mp, i != 1)) {
2243 error = -EFSCORRUPTED;
2246 error = xfs_bmbt_update(cur, &PREV);
2249 error = xfs_btree_decrement(cur, 0, &i);
2252 error = xfs_bmbt_update(cur, &LEFT);
2258 case BMAP_LEFT_FILLING:
2260 * Setting the first part of a previous oldext extent to newext.
2261 * The left neighbor is not contiguous.
2264 PREV.br_startoff += new->br_blockcount;
2265 PREV.br_startblock += new->br_blockcount;
2266 PREV.br_blockcount -= new->br_blockcount;
2268 xfs_iext_update_extent(ip, state, icur, &PREV);
2269 xfs_iext_insert(ip, icur, new, state);
2273 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2275 rval = XFS_ILOG_CORE;
2276 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2279 if (XFS_IS_CORRUPT(mp, i != 1)) {
2280 error = -EFSCORRUPTED;
2283 error = xfs_bmbt_update(cur, &PREV);
2286 cur->bc_rec.b = *new;
2287 if ((error = xfs_btree_insert(cur, &i)))
2289 if (XFS_IS_CORRUPT(mp, i != 1)) {
2290 error = -EFSCORRUPTED;
2296 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2298 * Setting the last part of a previous oldext extent to newext.
2299 * The right neighbor is contiguous with the new allocation.
2302 PREV.br_blockcount -= new->br_blockcount;
2304 RIGHT.br_startoff = new->br_startoff;
2305 RIGHT.br_startblock = new->br_startblock;
2306 RIGHT.br_blockcount += new->br_blockcount;
2308 xfs_iext_update_extent(ip, state, icur, &PREV);
2309 xfs_iext_next(ifp, icur);
2310 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2313 rval = XFS_ILOG_DEXT;
2316 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2319 if (XFS_IS_CORRUPT(mp, i != 1)) {
2320 error = -EFSCORRUPTED;
2323 error = xfs_bmbt_update(cur, &PREV);
2326 error = xfs_btree_increment(cur, 0, &i);
2329 error = xfs_bmbt_update(cur, &RIGHT);
2335 case BMAP_RIGHT_FILLING:
2337 * Setting the last part of a previous oldext extent to newext.
2338 * The right neighbor is not contiguous.
2341 PREV.br_blockcount -= new->br_blockcount;
2343 xfs_iext_update_extent(ip, state, icur, &PREV);
2344 xfs_iext_next(ifp, icur);
2345 xfs_iext_insert(ip, icur, new, state);
2349 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2351 rval = XFS_ILOG_CORE;
2352 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2355 if (XFS_IS_CORRUPT(mp, i != 1)) {
2356 error = -EFSCORRUPTED;
2359 error = xfs_bmbt_update(cur, &PREV);
2362 error = xfs_bmbt_lookup_eq(cur, new, &i);
2365 if (XFS_IS_CORRUPT(mp, i != 0)) {
2366 error = -EFSCORRUPTED;
2369 if ((error = xfs_btree_insert(cur, &i)))
2371 if (XFS_IS_CORRUPT(mp, i != 1)) {
2372 error = -EFSCORRUPTED;
2380 * Setting the middle part of a previous oldext extent to
2381 * newext. Contiguity is impossible here.
2382 * One extent becomes three extents.
2385 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2388 r[1].br_startoff = new_endoff;
2389 r[1].br_blockcount =
2390 old.br_startoff + old.br_blockcount - new_endoff;
2391 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2392 r[1].br_state = PREV.br_state;
2394 xfs_iext_update_extent(ip, state, icur, &PREV);
2395 xfs_iext_next(ifp, icur);
2396 xfs_iext_insert(ip, icur, &r[1], state);
2397 xfs_iext_insert(ip, icur, &r[0], state);
2398 ifp->if_nextents += 2;
2401 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2403 rval = XFS_ILOG_CORE;
2404 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2407 if (XFS_IS_CORRUPT(mp, i != 1)) {
2408 error = -EFSCORRUPTED;
2411 /* new right extent - oldext */
2412 error = xfs_bmbt_update(cur, &r[1]);
2415 /* new left extent - oldext */
2416 cur->bc_rec.b = PREV;
2417 if ((error = xfs_btree_insert(cur, &i)))
2419 if (XFS_IS_CORRUPT(mp, i != 1)) {
2420 error = -EFSCORRUPTED;
2424 * Reset the cursor to the position of the new extent
2425 * we are about to insert as we can't trust it after
2426 * the previous insert.
2428 error = xfs_bmbt_lookup_eq(cur, new, &i);
2431 if (XFS_IS_CORRUPT(mp, i != 0)) {
2432 error = -EFSCORRUPTED;
2435 /* new middle extent - newext */
2436 if ((error = xfs_btree_insert(cur, &i)))
2438 if (XFS_IS_CORRUPT(mp, i != 1)) {
2439 error = -EFSCORRUPTED;
2445 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2446 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2447 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2448 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2449 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2450 case BMAP_LEFT_CONTIG:
2451 case BMAP_RIGHT_CONTIG:
2453 * These cases are all impossible.
2458 /* update reverse mappings */
2459 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2461 /* convert to a btree if necessary */
2462 if (xfs_bmap_needs_btree(ip, whichfork)) {
2463 int tmp_logflags; /* partial log flag return val */
2465 ASSERT(cur == NULL);
2466 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2467 &tmp_logflags, whichfork);
2468 *logflagsp |= tmp_logflags;
2473 /* clear out the allocated field, done with it now in any case. */
2475 cur->bc_ino.allocated = 0;
2479 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2489 * Convert a hole to a delayed allocation.
2492 xfs_bmap_add_extent_hole_delay(
2493 xfs_inode_t *ip, /* incore inode pointer */
2495 struct xfs_iext_cursor *icur,
2496 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2498 struct xfs_ifork *ifp; /* inode fork pointer */
2499 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2500 xfs_filblks_t newlen=0; /* new indirect size */
2501 xfs_filblks_t oldlen=0; /* old indirect size */
2502 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2503 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2504 xfs_filblks_t temp; /* temp for indirect calculations */
2506 ifp = xfs_ifork_ptr(ip, whichfork);
2507 ASSERT(isnullstartblock(new->br_startblock));
2510 * Check and set flags if this segment has a left neighbor
2512 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2513 state |= BMAP_LEFT_VALID;
2514 if (isnullstartblock(left.br_startblock))
2515 state |= BMAP_LEFT_DELAY;
2519 * Check and set flags if the current (right) segment exists.
2520 * If it doesn't exist, we're converting the hole at end-of-file.
2522 if (xfs_iext_get_extent(ifp, icur, &right)) {
2523 state |= BMAP_RIGHT_VALID;
2524 if (isnullstartblock(right.br_startblock))
2525 state |= BMAP_RIGHT_DELAY;
2529 * Set contiguity flags on the left and right neighbors.
2530 * Don't let extents get too large, even if the pieces are contiguous.
2532 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2533 left.br_startoff + left.br_blockcount == new->br_startoff &&
2534 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2535 state |= BMAP_LEFT_CONTIG;
2537 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2538 new->br_startoff + new->br_blockcount == right.br_startoff &&
2539 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2540 (!(state & BMAP_LEFT_CONTIG) ||
2541 (left.br_blockcount + new->br_blockcount +
2542 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2543 state |= BMAP_RIGHT_CONTIG;
2546 * Switch out based on the contiguity flags.
2548 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2549 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2551 * New allocation is contiguous with delayed allocations
2552 * on the left and on the right.
2553 * Merge all three into a single extent record.
2555 temp = left.br_blockcount + new->br_blockcount +
2556 right.br_blockcount;
2558 oldlen = startblockval(left.br_startblock) +
2559 startblockval(new->br_startblock) +
2560 startblockval(right.br_startblock);
2561 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2563 left.br_startblock = nullstartblock(newlen);
2564 left.br_blockcount = temp;
2566 xfs_iext_remove(ip, icur, state);
2567 xfs_iext_prev(ifp, icur);
2568 xfs_iext_update_extent(ip, state, icur, &left);
2571 case BMAP_LEFT_CONTIG:
2573 * New allocation is contiguous with a delayed allocation
2575 * Merge the new allocation with the left neighbor.
2577 temp = left.br_blockcount + new->br_blockcount;
2579 oldlen = startblockval(left.br_startblock) +
2580 startblockval(new->br_startblock);
2581 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2583 left.br_blockcount = temp;
2584 left.br_startblock = nullstartblock(newlen);
2586 xfs_iext_prev(ifp, icur);
2587 xfs_iext_update_extent(ip, state, icur, &left);
2590 case BMAP_RIGHT_CONTIG:
2592 * New allocation is contiguous with a delayed allocation
2594 * Merge the new allocation with the right neighbor.
2596 temp = new->br_blockcount + right.br_blockcount;
2597 oldlen = startblockval(new->br_startblock) +
2598 startblockval(right.br_startblock);
2599 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2601 right.br_startoff = new->br_startoff;
2602 right.br_startblock = nullstartblock(newlen);
2603 right.br_blockcount = temp;
2604 xfs_iext_update_extent(ip, state, icur, &right);
2609 * New allocation is not contiguous with another
2610 * delayed allocation.
2611 * Insert a new entry.
2613 oldlen = newlen = 0;
2614 xfs_iext_insert(ip, icur, new, state);
2617 if (oldlen != newlen) {
2618 ASSERT(oldlen > newlen);
2619 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2622 * Nothing to do for disk quota accounting here.
2624 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2629 * Convert a hole to a real allocation.
2631 STATIC int /* error */
2632 xfs_bmap_add_extent_hole_real(
2633 struct xfs_trans *tp,
2634 struct xfs_inode *ip,
2636 struct xfs_iext_cursor *icur,
2637 struct xfs_btree_cur **curp,
2638 struct xfs_bmbt_irec *new,
2642 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2643 struct xfs_mount *mp = ip->i_mount;
2644 struct xfs_btree_cur *cur = *curp;
2645 int error; /* error return value */
2646 int i; /* temp state */
2647 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2648 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2649 int rval=0; /* return value (logging flags) */
2650 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2651 struct xfs_bmbt_irec old;
2653 ASSERT(!isnullstartblock(new->br_startblock));
2654 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2656 XFS_STATS_INC(mp, xs_add_exlist);
2659 * Check and set flags if this segment has a left neighbor.
2661 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2662 state |= BMAP_LEFT_VALID;
2663 if (isnullstartblock(left.br_startblock))
2664 state |= BMAP_LEFT_DELAY;
2668 * Check and set flags if this segment has a current value.
2669 * Not true if we're inserting into the "hole" at eof.
2671 if (xfs_iext_get_extent(ifp, icur, &right)) {
2672 state |= BMAP_RIGHT_VALID;
2673 if (isnullstartblock(right.br_startblock))
2674 state |= BMAP_RIGHT_DELAY;
2678 * We're inserting a real allocation between "left" and "right".
2679 * Set the contiguity flags. Don't let extents get too large.
2681 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2682 left.br_startoff + left.br_blockcount == new->br_startoff &&
2683 left.br_startblock + left.br_blockcount == new->br_startblock &&
2684 left.br_state == new->br_state &&
2685 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2686 state |= BMAP_LEFT_CONTIG;
2688 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2689 new->br_startoff + new->br_blockcount == right.br_startoff &&
2690 new->br_startblock + new->br_blockcount == right.br_startblock &&
2691 new->br_state == right.br_state &&
2692 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2693 (!(state & BMAP_LEFT_CONTIG) ||
2694 left.br_blockcount + new->br_blockcount +
2695 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
2696 state |= BMAP_RIGHT_CONTIG;
2700 * Select which case we're in here, and implement it.
2702 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2703 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2705 * New allocation is contiguous with real allocations on the
2706 * left and on the right.
2707 * Merge all three into a single extent record.
2709 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2711 xfs_iext_remove(ip, icur, state);
2712 xfs_iext_prev(ifp, icur);
2713 xfs_iext_update_extent(ip, state, icur, &left);
2717 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2719 rval = XFS_ILOG_CORE;
2720 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2723 if (XFS_IS_CORRUPT(mp, i != 1)) {
2724 error = -EFSCORRUPTED;
2727 error = xfs_btree_delete(cur, &i);
2730 if (XFS_IS_CORRUPT(mp, i != 1)) {
2731 error = -EFSCORRUPTED;
2734 error = xfs_btree_decrement(cur, 0, &i);
2737 if (XFS_IS_CORRUPT(mp, i != 1)) {
2738 error = -EFSCORRUPTED;
2741 error = xfs_bmbt_update(cur, &left);
2747 case BMAP_LEFT_CONTIG:
2749 * New allocation is contiguous with a real allocation
2751 * Merge the new allocation with the left neighbor.
2754 left.br_blockcount += new->br_blockcount;
2756 xfs_iext_prev(ifp, icur);
2757 xfs_iext_update_extent(ip, state, icur, &left);
2760 rval = xfs_ilog_fext(whichfork);
2763 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2766 if (XFS_IS_CORRUPT(mp, i != 1)) {
2767 error = -EFSCORRUPTED;
2770 error = xfs_bmbt_update(cur, &left);
2776 case BMAP_RIGHT_CONTIG:
2778 * New allocation is contiguous with a real allocation
2780 * Merge the new allocation with the right neighbor.
2784 right.br_startoff = new->br_startoff;
2785 right.br_startblock = new->br_startblock;
2786 right.br_blockcount += new->br_blockcount;
2787 xfs_iext_update_extent(ip, state, icur, &right);
2790 rval = xfs_ilog_fext(whichfork);
2793 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2796 if (XFS_IS_CORRUPT(mp, i != 1)) {
2797 error = -EFSCORRUPTED;
2800 error = xfs_bmbt_update(cur, &right);
2808 * New allocation is not contiguous with another
2810 * Insert a new entry.
2812 xfs_iext_insert(ip, icur, new, state);
2816 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2818 rval = XFS_ILOG_CORE;
2819 error = xfs_bmbt_lookup_eq(cur, new, &i);
2822 if (XFS_IS_CORRUPT(mp, i != 0)) {
2823 error = -EFSCORRUPTED;
2826 error = xfs_btree_insert(cur, &i);
2829 if (XFS_IS_CORRUPT(mp, i != 1)) {
2830 error = -EFSCORRUPTED;
2837 /* add reverse mapping unless caller opted out */
2838 if (!(flags & XFS_BMAPI_NORMAP))
2839 xfs_rmap_map_extent(tp, ip, whichfork, new);
2841 /* convert to a btree if necessary */
2842 if (xfs_bmap_needs_btree(ip, whichfork)) {
2843 int tmp_logflags; /* partial log flag return val */
2845 ASSERT(cur == NULL);
2846 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2847 &tmp_logflags, whichfork);
2848 *logflagsp |= tmp_logflags;
2854 /* clear out the allocated field, done with it now in any case. */
2856 cur->bc_ino.allocated = 0;
2858 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2865 * Functions used in the extent read, allocate and remove paths
2869 * Adjust the size of the new extent based on i_extsize and rt extsize.
2872 xfs_bmap_extsize_align(
2874 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2875 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2876 xfs_extlen_t extsz, /* align to this extent size */
2877 int rt, /* is this a realtime inode? */
2878 int eof, /* is extent at end-of-file? */
2879 int delay, /* creating delalloc extent? */
2880 int convert, /* overwriting unwritten extent? */
2881 xfs_fileoff_t *offp, /* in/out: aligned offset */
2882 xfs_extlen_t *lenp) /* in/out: aligned length */
2884 xfs_fileoff_t orig_off; /* original offset */
2885 xfs_extlen_t orig_alen; /* original length */
2886 xfs_fileoff_t orig_end; /* original off+len */
2887 xfs_fileoff_t nexto; /* next file offset */
2888 xfs_fileoff_t prevo; /* previous file offset */
2889 xfs_fileoff_t align_off; /* temp for offset */
2890 xfs_extlen_t align_alen; /* temp for length */
2891 xfs_extlen_t temp; /* temp for calculations */
2896 orig_off = align_off = *offp;
2897 orig_alen = align_alen = *lenp;
2898 orig_end = orig_off + orig_alen;
2901 * If this request overlaps an existing extent, then don't
2902 * attempt to perform any additional alignment.
2904 if (!delay && !eof &&
2905 (orig_off >= gotp->br_startoff) &&
2906 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2911 * If the file offset is unaligned vs. the extent size
2912 * we need to align it. This will be possible unless
2913 * the file was previously written with a kernel that didn't
2914 * perform this alignment, or if a truncate shot us in the
2917 div_u64_rem(orig_off, extsz, &temp);
2923 /* Same adjustment for the end of the requested area. */
2924 temp = (align_alen % extsz);
2926 align_alen += extsz - temp;
2929 * For large extent hint sizes, the aligned extent might be larger than
2930 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
2931 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
2932 * allocation loops handle short allocation just fine, so it is safe to
2933 * do this. We only want to do it when we are forced to, though, because
2934 * it means more allocation operations are required.
2936 while (align_alen > XFS_MAX_BMBT_EXTLEN)
2937 align_alen -= extsz;
2938 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
2941 * If the previous block overlaps with this proposed allocation
2942 * then move the start forward without adjusting the length.
2944 if (prevp->br_startoff != NULLFILEOFF) {
2945 if (prevp->br_startblock == HOLESTARTBLOCK)
2946 prevo = prevp->br_startoff;
2948 prevo = prevp->br_startoff + prevp->br_blockcount;
2951 if (align_off != orig_off && align_off < prevo)
2954 * If the next block overlaps with this proposed allocation
2955 * then move the start back without adjusting the length,
2956 * but not before offset 0.
2957 * This may of course make the start overlap previous block,
2958 * and if we hit the offset 0 limit then the next block
2959 * can still overlap too.
2961 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2962 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2963 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2964 nexto = gotp->br_startoff + gotp->br_blockcount;
2966 nexto = gotp->br_startoff;
2968 nexto = NULLFILEOFF;
2970 align_off + align_alen != orig_end &&
2971 align_off + align_alen > nexto)
2972 align_off = nexto > align_alen ? nexto - align_alen : 0;
2974 * If we're now overlapping the next or previous extent that
2975 * means we can't fit an extsz piece in this hole. Just move
2976 * the start forward to the first valid spot and set
2977 * the length so we hit the end.
2979 if (align_off != orig_off && align_off < prevo)
2981 if (align_off + align_alen != orig_end &&
2982 align_off + align_alen > nexto &&
2983 nexto != NULLFILEOFF) {
2984 ASSERT(nexto > prevo);
2985 align_alen = nexto - align_off;
2989 * If realtime, and the result isn't a multiple of the realtime
2990 * extent size we need to remove blocks until it is.
2992 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2994 * We're not covering the original request, or
2995 * we won't be able to once we fix the length.
2997 if (orig_off < align_off ||
2998 orig_end > align_off + align_alen ||
2999 align_alen - temp < orig_alen)
3002 * Try to fix it by moving the start up.
3004 if (align_off + temp <= orig_off) {
3009 * Try to fix it by moving the end in.
3011 else if (align_off + align_alen - temp >= orig_end)
3014 * Set the start to the minimum then trim the length.
3017 align_alen -= orig_off - align_off;
3018 align_off = orig_off;
3019 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3022 * Result doesn't cover the request, fail it.
3024 if (orig_off < align_off || orig_end > align_off + align_alen)
3027 ASSERT(orig_off >= align_off);
3028 /* see XFS_BMBT_MAX_EXTLEN handling above */
3029 ASSERT(orig_end <= align_off + align_alen ||
3030 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3034 if (!eof && gotp->br_startoff != NULLFILEOFF)
3035 ASSERT(align_off + align_alen <= gotp->br_startoff);
3036 if (prevp->br_startoff != NULLFILEOFF)
3037 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3045 #define XFS_ALLOC_GAP_UNITS 4
3049 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3051 xfs_fsblock_t adjust; /* adjustment to block numbers */
3052 xfs_mount_t *mp; /* mount point structure */
3053 int rt; /* true if inode is realtime */
3055 #define ISVALID(x,y) \
3057 (x) < mp->m_sb.sb_rblocks : \
3058 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3059 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3060 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3062 mp = ap->ip->i_mount;
3063 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3064 (ap->datatype & XFS_ALLOC_USERDATA);
3066 * If allocating at eof, and there's a previous real block,
3067 * try to use its last block as our starting point.
3069 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3070 !isnullstartblock(ap->prev.br_startblock) &&
3071 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3072 ap->prev.br_startblock)) {
3073 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3075 * Adjust for the gap between prevp and us.
3077 adjust = ap->offset -
3078 (ap->prev.br_startoff + ap->prev.br_blockcount);
3080 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3081 ap->blkno += adjust;
3084 * If not at eof, then compare the two neighbor blocks.
3085 * Figure out whether either one gives us a good starting point,
3086 * and pick the better one.
3088 else if (!ap->eof) {
3089 xfs_fsblock_t gotbno; /* right side block number */
3090 xfs_fsblock_t gotdiff=0; /* right side difference */
3091 xfs_fsblock_t prevbno; /* left side block number */
3092 xfs_fsblock_t prevdiff=0; /* left side difference */
3095 * If there's a previous (left) block, select a requested
3096 * start block based on it.
3098 if (ap->prev.br_startoff != NULLFILEOFF &&
3099 !isnullstartblock(ap->prev.br_startblock) &&
3100 (prevbno = ap->prev.br_startblock +
3101 ap->prev.br_blockcount) &&
3102 ISVALID(prevbno, ap->prev.br_startblock)) {
3104 * Calculate gap to end of previous block.
3106 adjust = prevdiff = ap->offset -
3107 (ap->prev.br_startoff +
3108 ap->prev.br_blockcount);
3110 * Figure the startblock based on the previous block's
3111 * end and the gap size.
3113 * If the gap is large relative to the piece we're
3114 * allocating, or using it gives us an invalid block
3115 * number, then just use the end of the previous block.
3117 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3118 ISVALID(prevbno + prevdiff,
3119 ap->prev.br_startblock))
3125 * No previous block or can't follow it, just default.
3128 prevbno = NULLFSBLOCK;
3130 * If there's a following (right) block, select a requested
3131 * start block based on it.
3133 if (!isnullstartblock(ap->got.br_startblock)) {
3135 * Calculate gap to start of next block.
3137 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3139 * Figure the startblock based on the next block's
3140 * start and the gap size.
3142 gotbno = ap->got.br_startblock;
3145 * If the gap is large relative to the piece we're
3146 * allocating, or using it gives us an invalid block
3147 * number, then just use the start of the next block
3148 * offset by our length.
3150 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3151 ISVALID(gotbno - gotdiff, gotbno))
3153 else if (ISVALID(gotbno - ap->length, gotbno)) {
3154 gotbno -= ap->length;
3155 gotdiff += adjust - ap->length;
3160 * No next block, just default.
3163 gotbno = NULLFSBLOCK;
3165 * If both valid, pick the better one, else the only good
3166 * one, else ap->blkno is already set (to 0 or the inode block).
3168 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3169 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3170 else if (prevbno != NULLFSBLOCK)
3171 ap->blkno = prevbno;
3172 else if (gotbno != NULLFSBLOCK)
3179 xfs_bmap_longest_free_extent(
3180 struct xfs_perag *pag,
3181 struct xfs_trans *tp,
3184 xfs_extlen_t longest;
3187 if (!xfs_perag_initialised_agf(pag)) {
3188 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3194 longest = xfs_alloc_longest_free_extent(pag,
3195 xfs_alloc_min_freelist(pag->pag_mount, pag),
3196 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3197 if (*blen < longest)
3204 xfs_bmap_select_minlen(
3205 struct xfs_bmalloca *ap,
3206 struct xfs_alloc_arg *args,
3211 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3212 * possible that there is enough contiguous free space for this request.
3214 if (blen < ap->minlen)
3218 * If the best seen length is less than the request length,
3219 * use the best as the minimum, otherwise we've got the maxlen we
3222 if (blen < args->maxlen)
3224 return args->maxlen;
3228 xfs_bmap_btalloc_select_lengths(
3229 struct xfs_bmalloca *ap,
3230 struct xfs_alloc_arg *args,
3233 struct xfs_mount *mp = args->mp;
3234 struct xfs_perag *pag;
3235 xfs_agnumber_t agno, startag;
3238 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3239 args->total = ap->minlen;
3240 args->minlen = ap->minlen;
3244 args->total = ap->total;
3245 startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
3246 if (startag == NULLAGNUMBER)
3250 for_each_perag_wrap(mp, startag, agno, pag) {
3251 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3252 if (error && error != -EAGAIN)
3255 if (*blen >= args->maxlen)
3259 xfs_perag_rele(pag);
3261 args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
3265 /* Update all inode and quota accounting for the allocation we just did. */
3267 xfs_bmap_btalloc_accounting(
3268 struct xfs_bmalloca *ap,
3269 struct xfs_alloc_arg *args)
3271 if (ap->flags & XFS_BMAPI_COWFORK) {
3273 * COW fork blocks are in-core only and thus are treated as
3274 * in-core quota reservation (like delalloc blocks) even when
3275 * converted to real blocks. The quota reservation is not
3276 * accounted to disk until blocks are remapped to the data
3277 * fork. So if these blocks were previously delalloc, we
3278 * already have quota reservation and there's nothing to do
3282 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3287 * Otherwise, we've allocated blocks in a hole. The transaction
3288 * has acquired in-core quota reservation for this extent.
3289 * Rather than account these as real blocks, however, we reduce
3290 * the transaction quota reservation based on the allocation.
3291 * This essentially transfers the transaction quota reservation
3292 * to that of a delalloc extent.
3294 ap->ip->i_delayed_blks += args->len;
3295 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3300 /* data/attr fork only */
3301 ap->ip->i_nblocks += args->len;
3302 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3304 ap->ip->i_delayed_blks -= args->len;
3305 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3307 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3308 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3313 xfs_bmap_compute_alignments(
3314 struct xfs_bmalloca *ap,
3315 struct xfs_alloc_arg *args)
3317 struct xfs_mount *mp = args->mp;
3318 xfs_extlen_t align = 0; /* minimum allocation alignment */
3319 int stripe_align = 0;
3321 /* stripe alignment for allocation is determined by mount parameters */
3322 if (mp->m_swidth && xfs_has_swalloc(mp))
3323 stripe_align = mp->m_swidth;
3324 else if (mp->m_dalign)
3325 stripe_align = mp->m_dalign;
3327 if (ap->flags & XFS_BMAPI_COWFORK)
3328 align = xfs_get_cowextsz_hint(ap->ip);
3329 else if (ap->datatype & XFS_ALLOC_USERDATA)
3330 align = xfs_get_extsz_hint(ap->ip);
3332 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3333 ap->eof, 0, ap->conv, &ap->offset,
3339 /* apply extent size hints if obtained earlier */
3342 div_u64_rem(ap->offset, args->prod, &args->mod);
3344 args->mod = args->prod - args->mod;
3345 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3349 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3350 div_u64_rem(ap->offset, args->prod, &args->mod);
3352 args->mod = args->prod - args->mod;
3355 return stripe_align;
3359 xfs_bmap_process_allocated_extent(
3360 struct xfs_bmalloca *ap,
3361 struct xfs_alloc_arg *args,
3362 xfs_fileoff_t orig_offset,
3363 xfs_extlen_t orig_length)
3365 ap->blkno = args->fsbno;
3366 ap->length = args->len;
3368 * If the extent size hint is active, we tried to round the
3369 * caller's allocation request offset down to extsz and the
3370 * length up to another extsz boundary. If we found a free
3371 * extent we mapped it in starting at this new offset. If the
3372 * newly mapped space isn't long enough to cover any of the
3373 * range of offsets that was originally requested, move the
3374 * mapping up so that we can fill as much of the caller's
3375 * original request as possible. Free space is apparently
3376 * very fragmented so we're unlikely to be able to satisfy the
3379 if (ap->length <= orig_length)
3380 ap->offset = orig_offset;
3381 else if (ap->offset + ap->length < orig_offset + orig_length)
3382 ap->offset = orig_offset + orig_length - ap->length;
3383 xfs_bmap_btalloc_accounting(ap, args);
3388 xfs_bmap_exact_minlen_extent_alloc(
3389 struct xfs_bmalloca *ap)
3391 struct xfs_mount *mp = ap->ip->i_mount;
3392 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3393 xfs_fileoff_t orig_offset;
3394 xfs_extlen_t orig_length;
3399 if (ap->minlen != 1) {
3400 ap->blkno = NULLFSBLOCK;
3405 orig_offset = ap->offset;
3406 orig_length = ap->length;
3408 args.alloc_minlen_only = 1;
3410 xfs_bmap_compute_alignments(ap, &args);
3413 * Unlike the longest extent available in an AG, we don't track
3414 * the length of an AG's shortest extent.
3415 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3416 * hence we can afford to start traversing from the 0th AG since
3417 * we need not be concerned about a drop in performance in
3418 * "debug only" code paths.
3420 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3422 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3423 args.minlen = args.maxlen = ap->minlen;
3424 args.total = ap->total;
3427 args.minalignslop = 0;
3429 args.minleft = ap->minleft;
3430 args.wasdel = ap->wasdel;
3431 args.resv = XFS_AG_RESV_NONE;
3432 args.datatype = ap->datatype;
3434 error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
3438 if (args.fsbno != NULLFSBLOCK) {
3439 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3442 ap->blkno = NULLFSBLOCK;
3450 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3455 * If we are not low on available data blocks and we are allocating at
3456 * EOF, optimise allocation for contiguous file extension and/or stripe
3457 * alignment of the new extent.
3459 * NOTE: ap->aeof is only set if the allocation length is >= the
3460 * stripe unit and the allocation offset is at the end of file.
3463 xfs_bmap_btalloc_at_eof(
3464 struct xfs_bmalloca *ap,
3465 struct xfs_alloc_arg *args,
3470 struct xfs_mount *mp = args->mp;
3471 struct xfs_perag *caller_pag = args->pag;
3475 * If there are already extents in the file, try an exact EOF block
3476 * allocation to extend the file as a contiguous extent. If that fails,
3477 * or it's the first allocation in a file, just try for a stripe aligned
3481 xfs_extlen_t nextminlen = 0;
3484 * Compute the minlen+alignment for the next case. Set slop so
3485 * that the value of minlen+alignment+slop doesn't go up between
3488 args->alignment = 1;
3489 if (blen > stripe_align && blen <= args->maxlen)
3490 nextminlen = blen - stripe_align;
3492 nextminlen = args->minlen;
3493 if (nextminlen + stripe_align > args->minlen + 1)
3494 args->minalignslop = nextminlen + stripe_align -
3497 args->minalignslop = 0;
3500 args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
3501 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
3503 xfs_perag_put(args->pag);
3509 if (args->fsbno != NULLFSBLOCK)
3512 * Exact allocation failed. Reset to try an aligned allocation
3513 * according to the original allocation specification.
3515 args->alignment = stripe_align;
3516 args->minlen = nextminlen;
3517 args->minalignslop = 0;
3520 * Adjust minlen to try and preserve alignment if we
3521 * can't guarantee an aligned maxlen extent.
3523 args->alignment = stripe_align;
3524 if (blen > args->alignment &&
3525 blen <= args->maxlen + args->alignment)
3526 args->minlen = blen - args->alignment;
3527 args->minalignslop = 0;
3531 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3534 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3535 ASSERT(args->pag == NULL);
3536 args->pag = caller_pag;
3541 if (args->fsbno != NULLFSBLOCK)
3545 * Allocation failed, so turn return the allocation args to their
3546 * original non-aligned state so the caller can proceed on allocation
3547 * failure as if this function was never called.
3549 args->alignment = 1;
3554 * We have failed multiple allocation attempts so now are in a low space
3555 * allocation situation. Try a locality first full filesystem minimum length
3556 * allocation whilst still maintaining necessary total block reservation
3559 * If that fails, we are now critically low on space, so perform a last resort
3560 * allocation attempt: no reserve, no locality, blocking, minimum length, full
3561 * filesystem free space scan. We also indicate to future allocations in this
3562 * transaction that we are critically low on space so they don't waste time on
3563 * allocation modes that are unlikely to succeed.
3566 xfs_bmap_btalloc_low_space(
3567 struct xfs_bmalloca *ap,
3568 struct xfs_alloc_arg *args)
3572 if (args->minlen > ap->minlen) {
3573 args->minlen = ap->minlen;
3574 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3575 if (error || args->fsbno != NULLFSBLOCK)
3579 /* Last ditch attempt before failure is declared. */
3580 args->total = ap->minlen;
3581 error = xfs_alloc_vextent_first_ag(args, 0);
3584 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3589 xfs_bmap_btalloc_filestreams(
3590 struct xfs_bmalloca *ap,
3591 struct xfs_alloc_arg *args,
3594 xfs_extlen_t blen = 0;
3598 error = xfs_filestream_select_ag(ap, args, &blen);
3604 * If we are in low space mode, then optimal allocation will fail so
3605 * prepare for minimal allocation and jump to the low space algorithm
3608 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3609 args->minlen = ap->minlen;
3610 ASSERT(args->fsbno == NULLFSBLOCK);
3614 args->minlen = xfs_bmap_select_minlen(ap, args, blen);
3616 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3619 if (!error && args->fsbno == NULLFSBLOCK)
3620 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3624 * We are now done with the perag reference for the filestreams
3625 * association provided by xfs_filestream_select_ag(). Release it now as
3626 * we've either succeeded, had a fatal error or we are out of space and
3627 * need to do a full filesystem scan for free space which will take it's
3630 xfs_perag_rele(args->pag);
3632 if (error || args->fsbno != NULLFSBLOCK)
3635 return xfs_bmap_btalloc_low_space(ap, args);
3639 xfs_bmap_btalloc_best_length(
3640 struct xfs_bmalloca *ap,
3641 struct xfs_alloc_arg *args,
3644 xfs_extlen_t blen = 0;
3647 ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
3648 xfs_bmap_adjacent(ap);
3651 * Search for an allocation group with a single extent large enough for
3652 * the request. If one isn't found, then adjust the minimum allocation
3653 * size to the largest space found.
3655 error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
3660 * Don't attempt optimal EOF allocation if previous allocations barely
3661 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3662 * optimal or even aligned allocations in this case, so don't waste time
3665 if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
3666 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3668 if (error || args->fsbno != NULLFSBLOCK)
3672 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3673 if (error || args->fsbno != NULLFSBLOCK)
3676 return xfs_bmap_btalloc_low_space(ap, args);
3681 struct xfs_bmalloca *ap)
3683 struct xfs_mount *mp = ap->ip->i_mount;
3684 struct xfs_alloc_arg args = {
3687 .fsbno = NULLFSBLOCK,
3688 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
3689 .minleft = ap->minleft,
3690 .wasdel = ap->wasdel,
3691 .resv = XFS_AG_RESV_NONE,
3692 .datatype = ap->datatype,
3696 xfs_fileoff_t orig_offset;
3697 xfs_extlen_t orig_length;
3702 orig_offset = ap->offset;
3703 orig_length = ap->length;
3705 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3707 /* Trim the allocation back to the maximum an AG can fit. */
3708 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3710 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3711 xfs_inode_is_filestream(ap->ip))
3712 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3714 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3718 if (args.fsbno != NULLFSBLOCK) {
3719 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3722 ap->blkno = NULLFSBLOCK;
3728 /* Trim extent to fit a logical block range. */
3731 struct xfs_bmbt_irec *irec,
3735 xfs_fileoff_t distance;
3736 xfs_fileoff_t end = bno + len;
3738 if (irec->br_startoff + irec->br_blockcount <= bno ||
3739 irec->br_startoff >= end) {
3740 irec->br_blockcount = 0;
3744 if (irec->br_startoff < bno) {
3745 distance = bno - irec->br_startoff;
3746 if (isnullstartblock(irec->br_startblock))
3747 irec->br_startblock = DELAYSTARTBLOCK;
3748 if (irec->br_startblock != DELAYSTARTBLOCK &&
3749 irec->br_startblock != HOLESTARTBLOCK)
3750 irec->br_startblock += distance;
3751 irec->br_startoff += distance;
3752 irec->br_blockcount -= distance;
3755 if (end < irec->br_startoff + irec->br_blockcount) {
3756 distance = irec->br_startoff + irec->br_blockcount - end;
3757 irec->br_blockcount -= distance;
3762 * Trim the returned map to the required bounds
3766 struct xfs_bmbt_irec *mval,
3767 struct xfs_bmbt_irec *got,
3775 if ((flags & XFS_BMAPI_ENTIRE) ||
3776 got->br_startoff + got->br_blockcount <= obno) {
3778 if (isnullstartblock(got->br_startblock))
3779 mval->br_startblock = DELAYSTARTBLOCK;
3785 ASSERT((*bno >= obno) || (n == 0));
3787 mval->br_startoff = *bno;
3788 if (isnullstartblock(got->br_startblock))
3789 mval->br_startblock = DELAYSTARTBLOCK;
3791 mval->br_startblock = got->br_startblock +
3792 (*bno - got->br_startoff);
3794 * Return the minimum of what we got and what we asked for for
3795 * the length. We can use the len variable here because it is
3796 * modified below and we could have been there before coming
3797 * here if the first part of the allocation didn't overlap what
3800 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3801 got->br_blockcount - (*bno - got->br_startoff));
3802 mval->br_state = got->br_state;
3803 ASSERT(mval->br_blockcount <= len);
3808 * Update and validate the extent map to return
3811 xfs_bmapi_update_map(
3812 struct xfs_bmbt_irec **map,
3820 xfs_bmbt_irec_t *mval = *map;
3822 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3823 ((mval->br_startoff + mval->br_blockcount) <= end));
3824 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3825 (mval->br_startoff < obno));
3827 *bno = mval->br_startoff + mval->br_blockcount;
3829 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3830 /* update previous map with new information */
3831 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3832 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3833 ASSERT(mval->br_state == mval[-1].br_state);
3834 mval[-1].br_blockcount = mval->br_blockcount;
3835 mval[-1].br_state = mval->br_state;
3836 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3837 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3838 mval[-1].br_startblock != HOLESTARTBLOCK &&
3839 mval->br_startblock == mval[-1].br_startblock +
3840 mval[-1].br_blockcount &&
3841 mval[-1].br_state == mval->br_state) {
3842 ASSERT(mval->br_startoff ==
3843 mval[-1].br_startoff + mval[-1].br_blockcount);
3844 mval[-1].br_blockcount += mval->br_blockcount;
3845 } else if (*n > 0 &&
3846 mval->br_startblock == DELAYSTARTBLOCK &&
3847 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3848 mval->br_startoff ==
3849 mval[-1].br_startoff + mval[-1].br_blockcount) {
3850 mval[-1].br_blockcount += mval->br_blockcount;
3851 mval[-1].br_state = mval->br_state;
3852 } else if (!((*n == 0) &&
3853 ((mval->br_startoff + mval->br_blockcount) <=
3862 * Map file blocks to filesystem blocks without allocation.
3866 struct xfs_inode *ip,
3869 struct xfs_bmbt_irec *mval,
3873 struct xfs_mount *mp = ip->i_mount;
3874 int whichfork = xfs_bmapi_whichfork(flags);
3875 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3876 struct xfs_bmbt_irec got;
3879 struct xfs_iext_cursor icur;
3885 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3886 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3888 if (WARN_ON_ONCE(!ifp))
3889 return -EFSCORRUPTED;
3891 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3892 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
3893 return -EFSCORRUPTED;
3895 if (xfs_is_shutdown(mp))
3898 XFS_STATS_INC(mp, xs_blk_mapr);
3900 error = xfs_iread_extents(NULL, ip, whichfork);
3904 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3909 while (bno < end && n < *nmap) {
3910 /* Reading past eof, act as though there's a hole up to end. */
3912 got.br_startoff = end;
3913 if (got.br_startoff > bno) {
3914 /* Reading in a hole. */
3915 mval->br_startoff = bno;
3916 mval->br_startblock = HOLESTARTBLOCK;
3917 mval->br_blockcount =
3918 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3919 mval->br_state = XFS_EXT_NORM;
3920 bno += mval->br_blockcount;
3921 len -= mval->br_blockcount;
3927 /* set up the extent map to return. */
3928 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3929 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3931 /* If we're done, stop now. */
3932 if (bno >= end || n >= *nmap)
3935 /* Else go on to the next record. */
3936 if (!xfs_iext_next_extent(ifp, &icur, &got))
3944 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3945 * global pool and the extent inserted into the inode in-core extent tree.
3947 * On entry, got refers to the first extent beyond the offset of the extent to
3948 * allocate or eof is specified if no such extent exists. On return, got refers
3949 * to the extent record that was inserted to the inode fork.
3951 * Note that the allocated extent may have been merged with contiguous extents
3952 * during insertion into the inode fork. Thus, got does not reflect the current
3953 * state of the inode fork on return. If necessary, the caller can use lastx to
3954 * look up the updated record in the inode fork.
3957 xfs_bmapi_reserve_delalloc(
3958 struct xfs_inode *ip,
3962 xfs_filblks_t prealloc,
3963 struct xfs_bmbt_irec *got,
3964 struct xfs_iext_cursor *icur,
3967 struct xfs_mount *mp = ip->i_mount;
3968 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3970 xfs_extlen_t indlen;
3972 xfs_fileoff_t aoff = off;
3975 * Cap the alloc length. Keep track of prealloc so we know whether to
3976 * tag the inode before we return.
3978 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
3980 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3981 if (prealloc && alen >= len)
3982 prealloc = alen - len;
3984 /* Figure out the extent size, adjust alen */
3985 if (whichfork == XFS_COW_FORK) {
3986 struct xfs_bmbt_irec prev;
3987 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3989 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3990 prev.br_startoff = NULLFILEOFF;
3992 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3993 1, 0, &aoff, &alen);
3998 * Make a transaction-less quota reservation for delayed allocation
3999 * blocks. This number gets adjusted later. We return if we haven't
4000 * allocated blocks already inside this loop.
4002 error = xfs_quota_reserve_blkres(ip, alen);
4007 * Split changing sb for alen and indlen since they could be coming
4008 * from different places.
4010 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4013 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4015 goto out_unreserve_quota;
4017 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4019 goto out_unreserve_blocks;
4022 ip->i_delayed_blks += alen;
4023 xfs_mod_delalloc(ip->i_mount, alen + indlen);
4025 got->br_startoff = aoff;
4026 got->br_startblock = nullstartblock(indlen);
4027 got->br_blockcount = alen;
4028 got->br_state = XFS_EXT_NORM;
4030 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4033 * Tag the inode if blocks were preallocated. Note that COW fork
4034 * preallocation can occur at the start or end of the extent, even when
4035 * prealloc == 0, so we must also check the aligned offset and length.
4037 if (whichfork == XFS_DATA_FORK && prealloc)
4038 xfs_inode_set_eofblocks_tag(ip);
4039 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4040 xfs_inode_set_cowblocks_tag(ip);
4044 out_unreserve_blocks:
4045 xfs_mod_fdblocks(mp, alen, false);
4046 out_unreserve_quota:
4047 if (XFS_IS_QUOTA_ON(mp))
4048 xfs_quota_unreserve_blkres(ip, alen);
4053 xfs_bmap_alloc_userdata(
4054 struct xfs_bmalloca *bma)
4056 struct xfs_mount *mp = bma->ip->i_mount;
4057 int whichfork = xfs_bmapi_whichfork(bma->flags);
4061 * Set the data type being allocated. For the data fork, the first data
4062 * in the file is treated differently to all other allocations. For the
4063 * attribute fork, we only need to ensure the allocated range is not on
4066 bma->datatype = XFS_ALLOC_NOBUSY;
4067 if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
4068 bma->datatype |= XFS_ALLOC_USERDATA;
4069 if (bma->offset == 0)
4070 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4072 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4073 error = xfs_bmap_isaeof(bma, whichfork);
4078 if (XFS_IS_REALTIME_INODE(bma->ip))
4079 return xfs_bmap_rtalloc(bma);
4082 if (unlikely(XFS_TEST_ERROR(false, mp,
4083 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4084 return xfs_bmap_exact_minlen_extent_alloc(bma);
4086 return xfs_bmap_btalloc(bma);
4091 struct xfs_bmalloca *bma)
4093 struct xfs_mount *mp = bma->ip->i_mount;
4094 int whichfork = xfs_bmapi_whichfork(bma->flags);
4095 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4096 int tmp_logflags = 0;
4099 ASSERT(bma->length > 0);
4102 * For the wasdelay case, we could also just allocate the stuff asked
4103 * for in this bmap call but that wouldn't be as good.
4106 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4107 bma->offset = bma->got.br_startoff;
4108 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4109 bma->prev.br_startoff = NULLFILEOFF;
4111 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
4113 bma->length = XFS_FILBLKS_MIN(bma->length,
4114 bma->got.br_startoff - bma->offset);
4117 if (bma->flags & XFS_BMAPI_CONTIG)
4118 bma->minlen = bma->length;
4122 if (bma->flags & XFS_BMAPI_METADATA) {
4123 if (unlikely(XFS_TEST_ERROR(false, mp,
4124 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4125 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4127 error = xfs_bmap_btalloc(bma);
4129 error = xfs_bmap_alloc_userdata(bma);
4131 if (error || bma->blkno == NULLFSBLOCK)
4134 if (bma->flags & XFS_BMAPI_ZERO) {
4135 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4140 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4141 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4143 * Bump the number of extents we've allocated
4149 bma->cur->bc_ino.flags =
4150 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4152 bma->got.br_startoff = bma->offset;
4153 bma->got.br_startblock = bma->blkno;
4154 bma->got.br_blockcount = bma->length;
4155 bma->got.br_state = XFS_EXT_NORM;
4157 if (bma->flags & XFS_BMAPI_PREALLOC)
4158 bma->got.br_state = XFS_EXT_UNWRITTEN;
4161 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4163 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4164 whichfork, &bma->icur, &bma->cur, &bma->got,
4165 &bma->logflags, bma->flags);
4167 bma->logflags |= tmp_logflags;
4172 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4173 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4174 * the neighbouring ones.
4176 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4178 ASSERT(bma->got.br_startoff <= bma->offset);
4179 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4180 bma->offset + bma->length);
4181 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4182 bma->got.br_state == XFS_EXT_UNWRITTEN);
4187 xfs_bmapi_convert_unwritten(
4188 struct xfs_bmalloca *bma,
4189 struct xfs_bmbt_irec *mval,
4193 int whichfork = xfs_bmapi_whichfork(flags);
4194 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4195 int tmp_logflags = 0;
4198 /* check if we need to do unwritten->real conversion */
4199 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4200 (flags & XFS_BMAPI_PREALLOC))
4203 /* check if we need to do real->unwritten conversion */
4204 if (mval->br_state == XFS_EXT_NORM &&
4205 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4206 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4210 * Modify (by adding) the state flag, if writing.
4212 ASSERT(mval->br_blockcount <= len);
4213 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4214 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4215 bma->ip, whichfork);
4217 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4218 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4221 * Before insertion into the bmbt, zero the range being converted
4224 if (flags & XFS_BMAPI_ZERO) {
4225 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4226 mval->br_blockcount);
4231 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4232 &bma->icur, &bma->cur, mval, &tmp_logflags);
4234 * Log the inode core unconditionally in the unwritten extent conversion
4235 * path because the conversion might not have done so (e.g., if the
4236 * extent count hasn't changed). We need to make sure the inode is dirty
4237 * in the transaction for the sake of fsync(), even if nothing has
4238 * changed, because fsync() will not force the log for this transaction
4239 * unless it sees the inode pinned.
4241 * Note: If we're only converting cow fork extents, there aren't
4242 * any on-disk updates to make, so we don't need to log anything.
4244 if (whichfork != XFS_COW_FORK)
4245 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4250 * Update our extent pointer, given that
4251 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4252 * of the neighbouring ones.
4254 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4257 * We may have combined previously unwritten space with written space,
4258 * so generate another request.
4260 if (mval->br_blockcount < len)
4267 struct xfs_trans *tp,
4268 struct xfs_inode *ip,
4271 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
4273 if (tp && tp->t_highest_agno != NULLAGNUMBER)
4275 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4277 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4281 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4282 * a case where the data is changed, there's an error, and it's not logged so we
4283 * don't shutdown when we should. Don't bother logging extents/btree changes if
4284 * we converted to the other format.
4288 struct xfs_bmalloca *bma,
4292 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4294 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4295 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4296 bma->logflags &= ~xfs_ilog_fext(whichfork);
4297 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4298 ifp->if_format != XFS_DINODE_FMT_BTREE)
4299 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4302 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4304 xfs_btree_del_cursor(bma->cur, error);
4308 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4309 * extent state if necessary. Details behaviour is controlled by the flags
4310 * parameter. Only allocates blocks from a single allocation group, to avoid
4315 struct xfs_trans *tp, /* transaction pointer */
4316 struct xfs_inode *ip, /* incore inode */
4317 xfs_fileoff_t bno, /* starting file offs. mapped */
4318 xfs_filblks_t len, /* length to map in file */
4319 uint32_t flags, /* XFS_BMAPI_... */
4320 xfs_extlen_t total, /* total blocks needed */
4321 struct xfs_bmbt_irec *mval, /* output: map values */
4322 int *nmap) /* i/o: mval size/count */
4324 struct xfs_bmalloca bma = {
4329 struct xfs_mount *mp = ip->i_mount;
4330 int whichfork = xfs_bmapi_whichfork(flags);
4331 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4332 xfs_fileoff_t end; /* end of mapped file region */
4333 bool eof = false; /* after the end of extents */
4334 int error; /* error return */
4335 int n; /* current extent index */
4336 xfs_fileoff_t obno; /* old block number (offset) */
4339 xfs_fileoff_t orig_bno; /* original block number value */
4340 int orig_flags; /* original flags arg value */
4341 xfs_filblks_t orig_len; /* original value of len arg */
4342 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4343 int orig_nmap; /* original value of *nmap */
4353 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4356 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4357 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4358 ASSERT(!(flags & XFS_BMAPI_REMAP));
4360 /* zeroing is for currently only for data extents, not metadata */
4361 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4362 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4364 * we can allocate unwritten extents or pre-zero allocated blocks,
4365 * but it makes no sense to do both at once. This would result in
4366 * zeroing the unwritten extent twice, but it still being an
4367 * unwritten extent....
4369 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4370 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4372 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4373 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4374 return -EFSCORRUPTED;
4377 if (xfs_is_shutdown(mp))
4380 XFS_STATS_INC(mp, xs_blk_mapw);
4382 error = xfs_iread_extents(tp, ip, whichfork);
4386 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4388 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4389 bma.prev.br_startoff = NULLFILEOFF;
4390 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4395 while (bno < end && n < *nmap) {
4396 bool need_alloc = false, wasdelay = false;
4398 /* in hole or beyond EOF? */
4399 if (eof || bma.got.br_startoff > bno) {
4401 * CoW fork conversions should /never/ hit EOF or
4402 * holes. There should always be something for us
4405 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4406 (flags & XFS_BMAPI_COWFORK)));
4409 } else if (isnullstartblock(bma.got.br_startblock)) {
4414 * First, deal with the hole before the allocated space
4415 * that we found, if any.
4417 if (need_alloc || wasdelay) {
4419 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4420 bma.wasdel = wasdelay;
4425 * There's a 32/64 bit type mismatch between the
4426 * allocation length request (which can be 64 bits in
4427 * length) and the bma length request, which is
4428 * xfs_extlen_t and therefore 32 bits. Hence we have to
4429 * check for 32-bit overflows and handle them here.
4431 if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
4432 bma.length = XFS_MAX_BMBT_EXTLEN;
4437 ASSERT(bma.length > 0);
4438 error = xfs_bmapi_allocate(&bma);
4441 if (bma.blkno == NULLFSBLOCK)
4445 * If this is a CoW allocation, record the data in
4446 * the refcount btree for orphan recovery.
4448 if (whichfork == XFS_COW_FORK)
4449 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4453 /* Deal with the allocated space we found. */
4454 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4457 /* Execute unwritten extent conversion if necessary */
4458 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4459 if (error == -EAGAIN)
4464 /* update the extent map to return */
4465 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4468 * If we're done, stop now. Stop when we've allocated
4469 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4470 * the transaction may get too big.
4472 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4475 /* Else go on to the next record. */
4477 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4482 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4487 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4488 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4489 xfs_bmapi_finish(&bma, whichfork, 0);
4490 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4494 xfs_bmapi_finish(&bma, whichfork, error);
4499 * Convert an existing delalloc extent to real blocks based on file offset. This
4500 * attempts to allocate the entire delalloc extent and may require multiple
4501 * invocations to allocate the target offset if a large enough physical extent
4505 xfs_bmapi_convert_delalloc(
4506 struct xfs_inode *ip,
4509 struct iomap *iomap,
4512 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4513 struct xfs_mount *mp = ip->i_mount;
4514 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4515 struct xfs_bmalloca bma = { NULL };
4517 struct xfs_trans *tp;
4520 if (whichfork == XFS_COW_FORK)
4521 flags |= IOMAP_F_SHARED;
4524 * Space for the extent and indirect blocks was reserved when the
4525 * delalloc extent was created so there's no need to do so here.
4527 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4528 XFS_TRANS_RESERVE, &tp);
4532 xfs_ilock(ip, XFS_ILOCK_EXCL);
4533 xfs_trans_ijoin(tp, ip, 0);
4535 error = xfs_iext_count_may_overflow(ip, whichfork,
4536 XFS_IEXT_ADD_NOSPLIT_CNT);
4537 if (error == -EFBIG)
4538 error = xfs_iext_count_upgrade(tp, ip,
4539 XFS_IEXT_ADD_NOSPLIT_CNT);
4541 goto out_trans_cancel;
4543 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4544 bma.got.br_startoff > offset_fsb) {
4546 * No extent found in the range we are trying to convert. This
4547 * should only happen for the COW fork, where another thread
4548 * might have moved the extent to the data fork in the meantime.
4550 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4552 goto out_trans_cancel;
4556 * If we find a real extent here we raced with another thread converting
4557 * the extent. Just return the real extent at this offset.
4559 if (!isnullstartblock(bma.got.br_startblock)) {
4560 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4561 xfs_iomap_inode_sequence(ip, flags));
4562 *seq = READ_ONCE(ifp->if_seq);
4563 goto out_trans_cancel;
4569 bma.offset = bma.got.br_startoff;
4570 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
4571 XFS_MAX_BMBT_EXTLEN);
4572 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4575 * When we're converting the delalloc reservations backing dirty pages
4576 * in the page cache, we must be careful about how we create the new
4579 * New CoW fork extents are created unwritten, turned into real extents
4580 * when we're about to write the data to disk, and mapped into the data
4581 * fork after the write finishes. End of story.
4583 * New data fork extents must be mapped in as unwritten and converted
4584 * to real extents after the write succeeds to avoid exposing stale
4585 * disk contents if we crash.
4587 bma.flags = XFS_BMAPI_PREALLOC;
4588 if (whichfork == XFS_COW_FORK)
4589 bma.flags |= XFS_BMAPI_COWFORK;
4591 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4592 bma.prev.br_startoff = NULLFILEOFF;
4594 error = xfs_bmapi_allocate(&bma);
4599 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4601 error = -EFSCORRUPTED;
4602 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
4605 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4606 XFS_STATS_INC(mp, xs_xstrat_quick);
4608 ASSERT(!isnullstartblock(bma.got.br_startblock));
4609 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4610 xfs_iomap_inode_sequence(ip, flags));
4611 *seq = READ_ONCE(ifp->if_seq);
4613 if (whichfork == XFS_COW_FORK)
4614 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4616 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4621 xfs_bmapi_finish(&bma, whichfork, 0);
4622 error = xfs_trans_commit(tp);
4623 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4627 xfs_bmapi_finish(&bma, whichfork, error);
4629 xfs_trans_cancel(tp);
4630 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4636 struct xfs_trans *tp,
4637 struct xfs_inode *ip,
4640 xfs_fsblock_t startblock,
4643 struct xfs_mount *mp = ip->i_mount;
4644 struct xfs_ifork *ifp;
4645 struct xfs_btree_cur *cur = NULL;
4646 struct xfs_bmbt_irec got;
4647 struct xfs_iext_cursor icur;
4648 int whichfork = xfs_bmapi_whichfork(flags);
4649 int logflags = 0, error;
4651 ifp = xfs_ifork_ptr(ip, whichfork);
4653 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4654 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4655 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4656 XFS_BMAPI_NORMAP)));
4657 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4658 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4660 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4661 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4662 return -EFSCORRUPTED;
4665 if (xfs_is_shutdown(mp))
4668 error = xfs_iread_extents(tp, ip, whichfork);
4672 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4673 /* make sure we only reflink into a hole. */
4674 ASSERT(got.br_startoff > bno);
4675 ASSERT(got.br_startoff - bno >= len);
4678 ip->i_nblocks += len;
4679 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4681 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
4682 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4683 cur->bc_ino.flags = 0;
4686 got.br_startoff = bno;
4687 got.br_startblock = startblock;
4688 got.br_blockcount = len;
4689 if (flags & XFS_BMAPI_PREALLOC)
4690 got.br_state = XFS_EXT_UNWRITTEN;
4692 got.br_state = XFS_EXT_NORM;
4694 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4695 &cur, &got, &logflags, flags);
4699 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4702 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4703 logflags &= ~XFS_ILOG_DEXT;
4704 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4705 logflags &= ~XFS_ILOG_DBROOT;
4708 xfs_trans_log_inode(tp, ip, logflags);
4710 xfs_btree_del_cursor(cur, error);
4715 * When a delalloc extent is split (e.g., due to a hole punch), the original
4716 * indlen reservation must be shared across the two new extents that are left
4719 * Given the original reservation and the worst case indlen for the two new
4720 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4721 * reservation fairly across the two new extents. If necessary, steal available
4722 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4723 * ores == 1). The number of stolen blocks is returned. The availability and
4724 * subsequent accounting of stolen blocks is the responsibility of the caller.
4726 static xfs_filblks_t
4727 xfs_bmap_split_indlen(
4728 xfs_filblks_t ores, /* original res. */
4729 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4730 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4731 xfs_filblks_t avail) /* stealable blocks */
4733 xfs_filblks_t len1 = *indlen1;
4734 xfs_filblks_t len2 = *indlen2;
4735 xfs_filblks_t nres = len1 + len2; /* new total res. */
4736 xfs_filblks_t stolen = 0;
4737 xfs_filblks_t resfactor;
4740 * Steal as many blocks as we can to try and satisfy the worst case
4741 * indlen for both new extents.
4743 if (ores < nres && avail)
4744 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4747 /* nothing else to do if we've satisfied the new reservation */
4752 * We can't meet the total required reservation for the two extents.
4753 * Calculate the percent of the overall shortage between both extents
4754 * and apply this percentage to each of the requested indlen values.
4755 * This distributes the shortage fairly and reduces the chances that one
4756 * of the two extents is left with nothing when extents are repeatedly
4759 resfactor = (ores * 100);
4760 do_div(resfactor, nres);
4765 ASSERT(len1 + len2 <= ores);
4766 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4769 * Hand out the remainder to each extent. If one of the two reservations
4770 * is zero, we want to make sure that one gets a block first. The loop
4771 * below starts with len1, so hand len2 a block right off the bat if it
4774 ores -= (len1 + len2);
4775 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4776 if (ores && !len2 && *indlen2) {
4781 if (len1 < *indlen1) {
4787 if (len2 < *indlen2) {
4800 xfs_bmap_del_extent_delay(
4801 struct xfs_inode *ip,
4803 struct xfs_iext_cursor *icur,
4804 struct xfs_bmbt_irec *got,
4805 struct xfs_bmbt_irec *del)
4807 struct xfs_mount *mp = ip->i_mount;
4808 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4809 struct xfs_bmbt_irec new;
4810 int64_t da_old, da_new, da_diff = 0;
4811 xfs_fileoff_t del_endoff, got_endoff;
4812 xfs_filblks_t got_indlen, new_indlen, stolen;
4813 uint32_t state = xfs_bmap_fork_to_state(whichfork);
4817 XFS_STATS_INC(mp, xs_del_exlist);
4819 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4820 del_endoff = del->br_startoff + del->br_blockcount;
4821 got_endoff = got->br_startoff + got->br_blockcount;
4822 da_old = startblockval(got->br_startblock);
4825 ASSERT(del->br_blockcount > 0);
4826 ASSERT(got->br_startoff <= del->br_startoff);
4827 ASSERT(got_endoff >= del_endoff);
4830 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4832 do_div(rtexts, mp->m_sb.sb_rextsize);
4833 xfs_mod_frextents(mp, rtexts);
4837 * Update the inode delalloc counter now and wait to update the
4838 * sb counters as we might have to borrow some blocks for the
4839 * indirect block accounting.
4842 error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4845 ip->i_delayed_blks -= del->br_blockcount;
4847 if (got->br_startoff == del->br_startoff)
4848 state |= BMAP_LEFT_FILLING;
4849 if (got_endoff == del_endoff)
4850 state |= BMAP_RIGHT_FILLING;
4852 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4853 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4855 * Matches the whole extent. Delete the entry.
4857 xfs_iext_remove(ip, icur, state);
4858 xfs_iext_prev(ifp, icur);
4860 case BMAP_LEFT_FILLING:
4862 * Deleting the first part of the extent.
4864 got->br_startoff = del_endoff;
4865 got->br_blockcount -= del->br_blockcount;
4866 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4867 got->br_blockcount), da_old);
4868 got->br_startblock = nullstartblock((int)da_new);
4869 xfs_iext_update_extent(ip, state, icur, got);
4871 case BMAP_RIGHT_FILLING:
4873 * Deleting the last part of the extent.
4875 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4876 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4877 got->br_blockcount), da_old);
4878 got->br_startblock = nullstartblock((int)da_new);
4879 xfs_iext_update_extent(ip, state, icur, got);
4883 * Deleting the middle of the extent.
4885 * Distribute the original indlen reservation across the two new
4886 * extents. Steal blocks from the deleted extent if necessary.
4887 * Stealing blocks simply fudges the fdblocks accounting below.
4888 * Warn if either of the new indlen reservations is zero as this
4889 * can lead to delalloc problems.
4891 got->br_blockcount = del->br_startoff - got->br_startoff;
4892 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4894 new.br_blockcount = got_endoff - del_endoff;
4895 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4897 WARN_ON_ONCE(!got_indlen || !new_indlen);
4898 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4899 del->br_blockcount);
4901 got->br_startblock = nullstartblock((int)got_indlen);
4903 new.br_startoff = del_endoff;
4904 new.br_state = got->br_state;
4905 new.br_startblock = nullstartblock((int)new_indlen);
4907 xfs_iext_update_extent(ip, state, icur, got);
4908 xfs_iext_next(ifp, icur);
4909 xfs_iext_insert(ip, icur, &new, state);
4911 da_new = got_indlen + new_indlen - stolen;
4912 del->br_blockcount -= stolen;
4916 ASSERT(da_old >= da_new);
4917 da_diff = da_old - da_new;
4919 da_diff += del->br_blockcount;
4921 xfs_mod_fdblocks(mp, da_diff, false);
4922 xfs_mod_delalloc(mp, -da_diff);
4928 xfs_bmap_del_extent_cow(
4929 struct xfs_inode *ip,
4930 struct xfs_iext_cursor *icur,
4931 struct xfs_bmbt_irec *got,
4932 struct xfs_bmbt_irec *del)
4934 struct xfs_mount *mp = ip->i_mount;
4935 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
4936 struct xfs_bmbt_irec new;
4937 xfs_fileoff_t del_endoff, got_endoff;
4938 uint32_t state = BMAP_COWFORK;
4940 XFS_STATS_INC(mp, xs_del_exlist);
4942 del_endoff = del->br_startoff + del->br_blockcount;
4943 got_endoff = got->br_startoff + got->br_blockcount;
4945 ASSERT(del->br_blockcount > 0);
4946 ASSERT(got->br_startoff <= del->br_startoff);
4947 ASSERT(got_endoff >= del_endoff);
4948 ASSERT(!isnullstartblock(got->br_startblock));
4950 if (got->br_startoff == del->br_startoff)
4951 state |= BMAP_LEFT_FILLING;
4952 if (got_endoff == del_endoff)
4953 state |= BMAP_RIGHT_FILLING;
4955 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4956 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4958 * Matches the whole extent. Delete the entry.
4960 xfs_iext_remove(ip, icur, state);
4961 xfs_iext_prev(ifp, icur);
4963 case BMAP_LEFT_FILLING:
4965 * Deleting the first part of the extent.
4967 got->br_startoff = del_endoff;
4968 got->br_blockcount -= del->br_blockcount;
4969 got->br_startblock = del->br_startblock + del->br_blockcount;
4970 xfs_iext_update_extent(ip, state, icur, got);
4972 case BMAP_RIGHT_FILLING:
4974 * Deleting the last part of the extent.
4976 got->br_blockcount -= del->br_blockcount;
4977 xfs_iext_update_extent(ip, state, icur, got);
4981 * Deleting the middle of the extent.
4983 got->br_blockcount = del->br_startoff - got->br_startoff;
4985 new.br_startoff = del_endoff;
4986 new.br_blockcount = got_endoff - del_endoff;
4987 new.br_state = got->br_state;
4988 new.br_startblock = del->br_startblock + del->br_blockcount;
4990 xfs_iext_update_extent(ip, state, icur, got);
4991 xfs_iext_next(ifp, icur);
4992 xfs_iext_insert(ip, icur, &new, state);
4995 ip->i_delayed_blks -= del->br_blockcount;
4999 * Called by xfs_bmapi to update file extent records and the btree
5000 * after removing space.
5002 STATIC int /* error */
5003 xfs_bmap_del_extent_real(
5004 xfs_inode_t *ip, /* incore inode pointer */
5005 xfs_trans_t *tp, /* current transaction pointer */
5006 struct xfs_iext_cursor *icur,
5007 struct xfs_btree_cur *cur, /* if null, not a btree */
5008 xfs_bmbt_irec_t *del, /* data to remove from extents */
5009 int *logflagsp, /* inode logging flags */
5010 int whichfork, /* data or attr fork */
5011 uint32_t bflags) /* bmapi flags */
5013 xfs_fsblock_t del_endblock=0; /* first block past del */
5014 xfs_fileoff_t del_endoff; /* first offset past del */
5015 int do_fx; /* free extent at end of routine */
5016 int error; /* error return value */
5017 int flags = 0;/* inode logging flags */
5018 struct xfs_bmbt_irec got; /* current extent entry */
5019 xfs_fileoff_t got_endoff; /* first offset past got */
5020 int i; /* temp state */
5021 struct xfs_ifork *ifp; /* inode fork pointer */
5022 xfs_mount_t *mp; /* mount structure */
5023 xfs_filblks_t nblks; /* quota/sb block count */
5024 xfs_bmbt_irec_t new; /* new record to be inserted */
5026 uint qfield; /* quota field to update */
5027 uint32_t state = xfs_bmap_fork_to_state(whichfork);
5028 struct xfs_bmbt_irec old;
5031 XFS_STATS_INC(mp, xs_del_exlist);
5033 ifp = xfs_ifork_ptr(ip, whichfork);
5034 ASSERT(del->br_blockcount > 0);
5035 xfs_iext_get_extent(ifp, icur, &got);
5036 ASSERT(got.br_startoff <= del->br_startoff);
5037 del_endoff = del->br_startoff + del->br_blockcount;
5038 got_endoff = got.br_startoff + got.br_blockcount;
5039 ASSERT(got_endoff >= del_endoff);
5040 ASSERT(!isnullstartblock(got.br_startblock));
5045 * If it's the case where the directory code is running with no block
5046 * reservation, and the deleted block is in the middle of its extent,
5047 * and the resulting insert of an extent would cause transformation to
5048 * btree format, then reject it. The calling code will then swap blocks
5049 * around instead. We have to do this now, rather than waiting for the
5050 * conversion to btree format, since the transaction will be dirty then.
5052 if (tp->t_blk_res == 0 &&
5053 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5054 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5055 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5058 flags = XFS_ILOG_CORE;
5059 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5063 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5067 if (!(bflags & XFS_BMAPI_REMAP)) {
5070 bno = div_u64_rem(del->br_startblock,
5071 mp->m_sb.sb_rextsize, &mod);
5074 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5080 nblks = len * mp->m_sb.sb_rextsize;
5081 qfield = XFS_TRANS_DQ_RTBCOUNT;
5084 nblks = del->br_blockcount;
5085 qfield = XFS_TRANS_DQ_BCOUNT;
5088 del_endblock = del->br_startblock + del->br_blockcount;
5090 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5093 if (XFS_IS_CORRUPT(mp, i != 1)) {
5094 error = -EFSCORRUPTED;
5099 if (got.br_startoff == del->br_startoff)
5100 state |= BMAP_LEFT_FILLING;
5101 if (got_endoff == del_endoff)
5102 state |= BMAP_RIGHT_FILLING;
5104 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5105 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5107 * Matches the whole extent. Delete the entry.
5109 xfs_iext_remove(ip, icur, state);
5110 xfs_iext_prev(ifp, icur);
5113 flags |= XFS_ILOG_CORE;
5115 flags |= xfs_ilog_fext(whichfork);
5118 if ((error = xfs_btree_delete(cur, &i)))
5120 if (XFS_IS_CORRUPT(mp, i != 1)) {
5121 error = -EFSCORRUPTED;
5125 case BMAP_LEFT_FILLING:
5127 * Deleting the first part of the extent.
5129 got.br_startoff = del_endoff;
5130 got.br_startblock = del_endblock;
5131 got.br_blockcount -= del->br_blockcount;
5132 xfs_iext_update_extent(ip, state, icur, &got);
5134 flags |= xfs_ilog_fext(whichfork);
5137 error = xfs_bmbt_update(cur, &got);
5141 case BMAP_RIGHT_FILLING:
5143 * Deleting the last part of the extent.
5145 got.br_blockcount -= del->br_blockcount;
5146 xfs_iext_update_extent(ip, state, icur, &got);
5148 flags |= xfs_ilog_fext(whichfork);
5151 error = xfs_bmbt_update(cur, &got);
5157 * Deleting the middle of the extent.
5162 got.br_blockcount = del->br_startoff - got.br_startoff;
5163 xfs_iext_update_extent(ip, state, icur, &got);
5165 new.br_startoff = del_endoff;
5166 new.br_blockcount = got_endoff - del_endoff;
5167 new.br_state = got.br_state;
5168 new.br_startblock = del_endblock;
5170 flags |= XFS_ILOG_CORE;
5172 error = xfs_bmbt_update(cur, &got);
5175 error = xfs_btree_increment(cur, 0, &i);
5178 cur->bc_rec.b = new;
5179 error = xfs_btree_insert(cur, &i);
5180 if (error && error != -ENOSPC)
5183 * If get no-space back from btree insert, it tried a
5184 * split, and we have a zero block reservation. Fix up
5185 * our state and return the error.
5187 if (error == -ENOSPC) {
5189 * Reset the cursor, don't trust it after any
5192 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5195 if (XFS_IS_CORRUPT(mp, i != 1)) {
5196 error = -EFSCORRUPTED;
5200 * Update the btree record back
5201 * to the original value.
5203 error = xfs_bmbt_update(cur, &old);
5207 * Reset the extent record back
5208 * to the original value.
5210 xfs_iext_update_extent(ip, state, icur, &old);
5215 if (XFS_IS_CORRUPT(mp, i != 1)) {
5216 error = -EFSCORRUPTED;
5220 flags |= xfs_ilog_fext(whichfork);
5223 xfs_iext_next(ifp, icur);
5224 xfs_iext_insert(ip, icur, &new, state);
5228 /* remove reverse mapping */
5229 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5232 * If we need to, add to list of extents to delete.
5234 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5235 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5236 xfs_refcount_decrease_extent(tp, del);
5238 error = __xfs_free_extent_later(tp, del->br_startblock,
5239 del->br_blockcount, NULL,
5241 ((bflags & XFS_BMAPI_NODISCARD) ||
5242 del->br_state == XFS_EXT_UNWRITTEN));
5249 * Adjust inode # blocks in the file.
5252 ip->i_nblocks -= nblks;
5254 * Adjust quota data.
5256 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5257 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5265 * Unmap (remove) blocks from a file.
5266 * If nexts is nonzero then the number of extents to remove is limited to
5267 * that value. If not all extents in the block range can be removed then
5272 struct xfs_trans *tp, /* transaction pointer */
5273 struct xfs_inode *ip, /* incore inode */
5274 xfs_fileoff_t start, /* first file offset deleted */
5275 xfs_filblks_t *rlen, /* i/o: amount remaining */
5276 uint32_t flags, /* misc flags */
5277 xfs_extnum_t nexts) /* number of extents max */
5279 struct xfs_btree_cur *cur; /* bmap btree cursor */
5280 struct xfs_bmbt_irec del; /* extent being deleted */
5281 int error; /* error return value */
5282 xfs_extnum_t extno; /* extent number in list */
5283 struct xfs_bmbt_irec got; /* current extent record */
5284 struct xfs_ifork *ifp; /* inode fork pointer */
5285 int isrt; /* freeing in rt area */
5286 int logflags; /* transaction logging flags */
5287 xfs_extlen_t mod; /* rt extent offset */
5288 struct xfs_mount *mp = ip->i_mount;
5289 int tmp_logflags; /* partial logging flags */
5290 int wasdel; /* was a delayed alloc extent */
5291 int whichfork; /* data or attribute fork */
5293 xfs_filblks_t len = *rlen; /* length to unmap in file */
5295 struct xfs_iext_cursor icur;
5298 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5300 whichfork = xfs_bmapi_whichfork(flags);
5301 ASSERT(whichfork != XFS_COW_FORK);
5302 ifp = xfs_ifork_ptr(ip, whichfork);
5303 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
5304 return -EFSCORRUPTED;
5305 if (xfs_is_shutdown(mp))
5308 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5312 error = xfs_iread_extents(tp, ip, whichfork);
5316 if (xfs_iext_count(ifp) == 0) {
5320 XFS_STATS_INC(mp, xs_blk_unmap);
5321 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5324 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5331 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5332 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5333 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5334 cur->bc_ino.flags = 0;
5340 * Synchronize by locking the bitmap inode.
5342 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5343 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5344 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5345 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5349 while (end != (xfs_fileoff_t)-1 && end >= start &&
5350 (nexts == 0 || extno < nexts)) {
5352 * Is the found extent after a hole in which end lives?
5353 * Just back up to the previous extent, if so.
5355 if (got.br_startoff > end &&
5356 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5361 * Is the last block of this extent before the range
5362 * we're supposed to delete? If so, we're done.
5364 end = XFS_FILEOFF_MIN(end,
5365 got.br_startoff + got.br_blockcount - 1);
5369 * Then deal with the (possibly delayed) allocated space
5373 wasdel = isnullstartblock(del.br_startblock);
5375 if (got.br_startoff < start) {
5376 del.br_startoff = start;
5377 del.br_blockcount -= start - got.br_startoff;
5379 del.br_startblock += start - got.br_startoff;
5381 if (del.br_startoff + del.br_blockcount > end + 1)
5382 del.br_blockcount = end + 1 - del.br_startoff;
5387 sum = del.br_startblock + del.br_blockcount;
5388 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5391 * Realtime extent not lined up at the end.
5392 * The extent could have been split into written
5393 * and unwritten pieces, or we could just be
5394 * unmapping part of it. But we can't really
5395 * get rid of part of a realtime extent.
5397 if (del.br_state == XFS_EXT_UNWRITTEN) {
5399 * This piece is unwritten, or we're not
5400 * using unwritten extents. Skip over it.
5403 end -= mod > del.br_blockcount ?
5404 del.br_blockcount : mod;
5405 if (end < got.br_startoff &&
5406 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5413 * It's written, turn it unwritten.
5414 * This is better than zeroing it.
5416 ASSERT(del.br_state == XFS_EXT_NORM);
5417 ASSERT(tp->t_blk_res > 0);
5419 * If this spans a realtime extent boundary,
5420 * chop it back to the start of the one we end at.
5422 if (del.br_blockcount > mod) {
5423 del.br_startoff += del.br_blockcount - mod;
5424 del.br_startblock += del.br_blockcount - mod;
5425 del.br_blockcount = mod;
5427 del.br_state = XFS_EXT_UNWRITTEN;
5428 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5429 whichfork, &icur, &cur, &del,
5435 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5437 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5440 * Realtime extent is lined up at the end but not
5441 * at the front. We'll get rid of full extents if
5444 if (del.br_blockcount > off) {
5445 del.br_blockcount -= off;
5446 del.br_startoff += off;
5447 del.br_startblock += off;
5448 } else if (del.br_startoff == start &&
5449 (del.br_state == XFS_EXT_UNWRITTEN ||
5450 tp->t_blk_res == 0)) {
5452 * Can't make it unwritten. There isn't
5453 * a full extent here so just skip it.
5455 ASSERT(end >= del.br_blockcount);
5456 end -= del.br_blockcount;
5457 if (got.br_startoff > end &&
5458 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5463 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5464 struct xfs_bmbt_irec prev;
5465 xfs_fileoff_t unwrite_start;
5468 * This one is already unwritten.
5469 * It must have a written left neighbor.
5470 * Unwrite the killed part of that one and
5473 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5475 ASSERT(prev.br_state == XFS_EXT_NORM);
5476 ASSERT(!isnullstartblock(prev.br_startblock));
5477 ASSERT(del.br_startblock ==
5478 prev.br_startblock + prev.br_blockcount);
5479 unwrite_start = max3(start,
5480 del.br_startoff - mod,
5482 mod = unwrite_start - prev.br_startoff;
5483 prev.br_startoff = unwrite_start;
5484 prev.br_startblock += mod;
5485 prev.br_blockcount -= mod;
5486 prev.br_state = XFS_EXT_UNWRITTEN;
5487 error = xfs_bmap_add_extent_unwritten_real(tp,
5488 ip, whichfork, &icur, &cur,
5494 ASSERT(del.br_state == XFS_EXT_NORM);
5495 del.br_state = XFS_EXT_UNWRITTEN;
5496 error = xfs_bmap_add_extent_unwritten_real(tp,
5497 ip, whichfork, &icur, &cur,
5507 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5510 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5511 &del, &tmp_logflags, whichfork,
5513 logflags |= tmp_logflags;
5519 end = del.br_startoff - 1;
5522 * If not done go on to the next (previous) record.
5524 if (end != (xfs_fileoff_t)-1 && end >= start) {
5525 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5526 (got.br_startoff > end &&
5527 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5534 if (done || end == (xfs_fileoff_t)-1 || end < start)
5537 *rlen = end - start + 1;
5540 * Convert to a btree if necessary.
5542 if (xfs_bmap_needs_btree(ip, whichfork)) {
5543 ASSERT(cur == NULL);
5544 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5545 &tmp_logflags, whichfork);
5546 logflags |= tmp_logflags;
5548 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5554 * Log everything. Do this after conversion, there's no point in
5555 * logging the extent records if we've converted to btree format.
5557 if ((logflags & xfs_ilog_fext(whichfork)) &&
5558 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5559 logflags &= ~xfs_ilog_fext(whichfork);
5560 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5561 ifp->if_format != XFS_DINODE_FMT_BTREE)
5562 logflags &= ~xfs_ilog_fbroot(whichfork);
5564 * Log inode even in the error case, if the transaction
5565 * is dirty we'll need to shut down the filesystem.
5568 xfs_trans_log_inode(tp, ip, logflags);
5571 cur->bc_ino.allocated = 0;
5572 xfs_btree_del_cursor(cur, error);
5577 /* Unmap a range of a file. */
5581 struct xfs_inode *ip,
5590 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5596 * Determine whether an extent shift can be accomplished by a merge with the
5597 * extent that precedes the target hole of the shift.
5601 struct xfs_bmbt_irec *left, /* preceding extent */
5602 struct xfs_bmbt_irec *got, /* current extent to shift */
5603 xfs_fileoff_t shift) /* shift fsb */
5605 xfs_fileoff_t startoff;
5607 startoff = got->br_startoff - shift;
5610 * The extent, once shifted, must be adjacent in-file and on-disk with
5611 * the preceding extent.
5613 if ((left->br_startoff + left->br_blockcount != startoff) ||
5614 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5615 (left->br_state != got->br_state) ||
5616 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5623 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5624 * hole in the file. If an extent shift would result in the extent being fully
5625 * adjacent to the extent that currently precedes the hole, we can merge with
5626 * the preceding extent rather than do the shift.
5628 * This function assumes the caller has verified a shift-by-merge is possible
5629 * with the provided extents via xfs_bmse_can_merge().
5633 struct xfs_trans *tp,
5634 struct xfs_inode *ip,
5636 xfs_fileoff_t shift, /* shift fsb */
5637 struct xfs_iext_cursor *icur,
5638 struct xfs_bmbt_irec *got, /* extent to shift */
5639 struct xfs_bmbt_irec *left, /* preceding extent */
5640 struct xfs_btree_cur *cur,
5641 int *logflags) /* output */
5643 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5644 struct xfs_bmbt_irec new;
5645 xfs_filblks_t blockcount;
5647 struct xfs_mount *mp = ip->i_mount;
5649 blockcount = left->br_blockcount + got->br_blockcount;
5651 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5652 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5653 ASSERT(xfs_bmse_can_merge(left, got, shift));
5656 new.br_blockcount = blockcount;
5659 * Update the on-disk extent count, the btree if necessary and log the
5663 *logflags |= XFS_ILOG_CORE;
5665 *logflags |= XFS_ILOG_DEXT;
5669 /* lookup and remove the extent to merge */
5670 error = xfs_bmbt_lookup_eq(cur, got, &i);
5673 if (XFS_IS_CORRUPT(mp, i != 1))
5674 return -EFSCORRUPTED;
5676 error = xfs_btree_delete(cur, &i);
5679 if (XFS_IS_CORRUPT(mp, i != 1))
5680 return -EFSCORRUPTED;
5682 /* lookup and update size of the previous extent */
5683 error = xfs_bmbt_lookup_eq(cur, left, &i);
5686 if (XFS_IS_CORRUPT(mp, i != 1))
5687 return -EFSCORRUPTED;
5689 error = xfs_bmbt_update(cur, &new);
5693 /* change to extent format if required after extent removal */
5694 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5699 xfs_iext_remove(ip, icur, 0);
5700 xfs_iext_prev(ifp, icur);
5701 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5704 /* update reverse mapping. rmap functions merge the rmaps for us */
5705 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5706 memcpy(&new, got, sizeof(new));
5707 new.br_startoff = left->br_startoff + left->br_blockcount;
5708 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5713 xfs_bmap_shift_update_extent(
5714 struct xfs_trans *tp,
5715 struct xfs_inode *ip,
5717 struct xfs_iext_cursor *icur,
5718 struct xfs_bmbt_irec *got,
5719 struct xfs_btree_cur *cur,
5721 xfs_fileoff_t startoff)
5723 struct xfs_mount *mp = ip->i_mount;
5724 struct xfs_bmbt_irec prev = *got;
5727 *logflags |= XFS_ILOG_CORE;
5729 got->br_startoff = startoff;
5732 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5735 if (XFS_IS_CORRUPT(mp, i != 1))
5736 return -EFSCORRUPTED;
5738 error = xfs_bmbt_update(cur, got);
5742 *logflags |= XFS_ILOG_DEXT;
5745 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5748 /* update reverse mapping */
5749 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5750 xfs_rmap_map_extent(tp, ip, whichfork, got);
5755 xfs_bmap_collapse_extents(
5756 struct xfs_trans *tp,
5757 struct xfs_inode *ip,
5758 xfs_fileoff_t *next_fsb,
5759 xfs_fileoff_t offset_shift_fsb,
5762 int whichfork = XFS_DATA_FORK;
5763 struct xfs_mount *mp = ip->i_mount;
5764 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5765 struct xfs_btree_cur *cur = NULL;
5766 struct xfs_bmbt_irec got, prev;
5767 struct xfs_iext_cursor icur;
5768 xfs_fileoff_t new_startoff;
5772 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5773 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5774 return -EFSCORRUPTED;
5777 if (xfs_is_shutdown(mp))
5780 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5782 error = xfs_iread_extents(tp, ip, whichfork);
5786 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5787 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5788 cur->bc_ino.flags = 0;
5791 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5795 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5796 error = -EFSCORRUPTED;
5800 new_startoff = got.br_startoff - offset_shift_fsb;
5801 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5802 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5807 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5808 error = xfs_bmse_merge(tp, ip, whichfork,
5809 offset_shift_fsb, &icur, &got, &prev,
5816 if (got.br_startoff < offset_shift_fsb) {
5822 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5823 cur, &logflags, new_startoff);
5828 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5833 *next_fsb = got.br_startoff;
5836 xfs_btree_del_cursor(cur, error);
5838 xfs_trans_log_inode(tp, ip, logflags);
5842 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5844 xfs_bmap_can_insert_extents(
5845 struct xfs_inode *ip,
5847 xfs_fileoff_t shift)
5849 struct xfs_bmbt_irec got;
5853 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5855 if (xfs_is_shutdown(ip->i_mount))
5858 xfs_ilock(ip, XFS_ILOCK_EXCL);
5859 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5860 if (!error && !is_empty && got.br_startoff >= off &&
5861 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5863 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5869 xfs_bmap_insert_extents(
5870 struct xfs_trans *tp,
5871 struct xfs_inode *ip,
5872 xfs_fileoff_t *next_fsb,
5873 xfs_fileoff_t offset_shift_fsb,
5875 xfs_fileoff_t stop_fsb)
5877 int whichfork = XFS_DATA_FORK;
5878 struct xfs_mount *mp = ip->i_mount;
5879 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5880 struct xfs_btree_cur *cur = NULL;
5881 struct xfs_bmbt_irec got, next;
5882 struct xfs_iext_cursor icur;
5883 xfs_fileoff_t new_startoff;
5887 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5888 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5889 return -EFSCORRUPTED;
5892 if (xfs_is_shutdown(mp))
5895 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5897 error = xfs_iread_extents(tp, ip, whichfork);
5901 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5902 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5903 cur->bc_ino.flags = 0;
5906 if (*next_fsb == NULLFSBLOCK) {
5907 xfs_iext_last(ifp, &icur);
5908 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5909 stop_fsb > got.br_startoff) {
5914 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5919 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5920 error = -EFSCORRUPTED;
5924 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
5925 error = -EFSCORRUPTED;
5929 new_startoff = got.br_startoff + offset_shift_fsb;
5930 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5931 if (new_startoff + got.br_blockcount > next.br_startoff) {
5937 * Unlike a left shift (which involves a hole punch), a right
5938 * shift does not modify extent neighbors in any way. We should
5939 * never find mergeable extents in this scenario. Check anyways
5940 * and warn if we encounter two extents that could be one.
5942 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5946 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5947 cur, &logflags, new_startoff);
5951 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5952 stop_fsb >= got.br_startoff + got.br_blockcount) {
5957 *next_fsb = got.br_startoff;
5960 xfs_btree_del_cursor(cur, error);
5962 xfs_trans_log_inode(tp, ip, logflags);
5967 * Splits an extent into two extents at split_fsb block such that it is the
5968 * first block of the current_ext. @ext is a target extent to be split.
5969 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5970 * hole or the first block of extents, just return 0.
5973 xfs_bmap_split_extent(
5974 struct xfs_trans *tp,
5975 struct xfs_inode *ip,
5976 xfs_fileoff_t split_fsb)
5978 int whichfork = XFS_DATA_FORK;
5979 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5980 struct xfs_btree_cur *cur = NULL;
5981 struct xfs_bmbt_irec got;
5982 struct xfs_bmbt_irec new; /* split extent */
5983 struct xfs_mount *mp = ip->i_mount;
5984 xfs_fsblock_t gotblkcnt; /* new block count for got */
5985 struct xfs_iext_cursor icur;
5990 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5991 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5992 return -EFSCORRUPTED;
5995 if (xfs_is_shutdown(mp))
5998 /* Read in all the extents */
5999 error = xfs_iread_extents(tp, ip, whichfork);
6004 * If there are not extents, or split_fsb lies in a hole we are done.
6006 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6007 got.br_startoff >= split_fsb)
6010 gotblkcnt = split_fsb - got.br_startoff;
6011 new.br_startoff = split_fsb;
6012 new.br_startblock = got.br_startblock + gotblkcnt;
6013 new.br_blockcount = got.br_blockcount - gotblkcnt;
6014 new.br_state = got.br_state;
6016 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6017 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6018 cur->bc_ino.flags = 0;
6019 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6022 if (XFS_IS_CORRUPT(mp, i != 1)) {
6023 error = -EFSCORRUPTED;
6028 got.br_blockcount = gotblkcnt;
6029 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6032 logflags = XFS_ILOG_CORE;
6034 error = xfs_bmbt_update(cur, &got);
6038 logflags |= XFS_ILOG_DEXT;
6040 /* Add new extent */
6041 xfs_iext_next(ifp, &icur);
6042 xfs_iext_insert(ip, &icur, &new, 0);
6046 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6049 if (XFS_IS_CORRUPT(mp, i != 0)) {
6050 error = -EFSCORRUPTED;
6053 error = xfs_btree_insert(cur, &i);
6056 if (XFS_IS_CORRUPT(mp, i != 1)) {
6057 error = -EFSCORRUPTED;
6063 * Convert to a btree if necessary.
6065 if (xfs_bmap_needs_btree(ip, whichfork)) {
6066 int tmp_logflags; /* partial log flag return val */
6068 ASSERT(cur == NULL);
6069 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6070 &tmp_logflags, whichfork);
6071 logflags |= tmp_logflags;
6076 cur->bc_ino.allocated = 0;
6077 xfs_btree_del_cursor(cur, error);
6081 xfs_trans_log_inode(tp, ip, logflags);
6085 /* Deferred mapping is only for real extents in the data fork. */
6087 xfs_bmap_is_update_needed(
6088 struct xfs_bmbt_irec *bmap)
6090 return bmap->br_startblock != HOLESTARTBLOCK &&
6091 bmap->br_startblock != DELAYSTARTBLOCK;
6094 /* Record a bmap intent. */
6097 struct xfs_trans *tp,
6098 enum xfs_bmap_intent_type type,
6099 struct xfs_inode *ip,
6101 struct xfs_bmbt_irec *bmap)
6103 struct xfs_bmap_intent *bi;
6105 trace_xfs_bmap_defer(tp->t_mountp,
6106 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6108 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6109 ip->i_ino, whichfork,
6111 bmap->br_blockcount,
6114 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
6115 INIT_LIST_HEAD(&bi->bi_list);
6118 bi->bi_whichfork = whichfork;
6119 bi->bi_bmap = *bmap;
6121 xfs_bmap_update_get_group(tp->t_mountp, bi);
6122 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6126 /* Map an extent into a file. */
6128 xfs_bmap_map_extent(
6129 struct xfs_trans *tp,
6130 struct xfs_inode *ip,
6131 struct xfs_bmbt_irec *PREV)
6133 if (!xfs_bmap_is_update_needed(PREV))
6136 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6139 /* Unmap an extent out of a file. */
6141 xfs_bmap_unmap_extent(
6142 struct xfs_trans *tp,
6143 struct xfs_inode *ip,
6144 struct xfs_bmbt_irec *PREV)
6146 if (!xfs_bmap_is_update_needed(PREV))
6149 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6153 * Process one of the deferred bmap operations. We pass back the
6154 * btree cursor to maintain our lock on the bmapbt between calls.
6157 xfs_bmap_finish_one(
6158 struct xfs_trans *tp,
6159 struct xfs_bmap_intent *bi)
6161 struct xfs_bmbt_irec *bmap = &bi->bi_bmap;
6164 ASSERT(tp->t_highest_agno == NULLAGNUMBER);
6166 trace_xfs_bmap_deferred(tp->t_mountp,
6167 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6169 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6170 bi->bi_owner->i_ino, bi->bi_whichfork,
6171 bmap->br_startoff, bmap->br_blockcount,
6174 if (WARN_ON_ONCE(bi->bi_whichfork != XFS_DATA_FORK))
6175 return -EFSCORRUPTED;
6177 if (XFS_TEST_ERROR(false, tp->t_mountp,
6178 XFS_ERRTAG_BMAP_FINISH_ONE))
6181 switch (bi->bi_type) {
6183 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
6184 bmap->br_blockcount, bmap->br_startblock, 0);
6185 bmap->br_blockcount = 0;
6187 case XFS_BMAP_UNMAP:
6188 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
6189 &bmap->br_blockcount, XFS_BMAPI_REMAP, 1);
6193 error = -EFSCORRUPTED;
6199 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6201 xfs_bmap_validate_extent(
6202 struct xfs_inode *ip,
6204 struct xfs_bmbt_irec *irec)
6206 struct xfs_mount *mp = ip->i_mount;
6208 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6209 return __this_address;
6211 if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) {
6212 if (!xfs_verify_rtext(mp, irec->br_startblock,
6213 irec->br_blockcount))
6214 return __this_address;
6216 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6217 irec->br_blockcount))
6218 return __this_address;
6220 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6221 return __this_address;
6226 xfs_bmap_intent_init_cache(void)
6228 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6229 sizeof(struct xfs_bmap_intent),
6232 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6236 xfs_bmap_intent_destroy_cache(void)
6238 kmem_cache_destroy(xfs_bmap_intent_cache);
6239 xfs_bmap_intent_cache = NULL;