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) %Ld",
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)))
575 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
576 xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo);
578 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
579 xfs_trans_binval(tp, cbp);
580 if (cur->bc_levels[0].bp == cbp)
581 cur->bc_levels[0].bp = NULL;
582 xfs_iroot_realloc(ip, -1, whichfork);
583 ASSERT(ifp->if_broot == NULL);
584 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
585 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
590 * Convert an extents-format file into a btree-format file.
591 * The new file will have a root block (in the inode) and a single child block.
593 STATIC int /* error */
594 xfs_bmap_extents_to_btree(
595 struct xfs_trans *tp, /* transaction pointer */
596 struct xfs_inode *ip, /* incore inode pointer */
597 struct xfs_btree_cur **curp, /* cursor returned to caller */
598 int wasdel, /* converting a delayed alloc */
599 int *logflagsp, /* inode logging flags */
600 int whichfork) /* data or attr fork */
602 struct xfs_btree_block *ablock; /* allocated (child) bt block */
603 struct xfs_buf *abp; /* buffer for ablock */
604 struct xfs_alloc_arg args; /* allocation arguments */
605 struct xfs_bmbt_rec *arp; /* child record pointer */
606 struct xfs_btree_block *block; /* btree root block */
607 struct xfs_btree_cur *cur; /* bmap btree cursor */
608 int error; /* error return value */
609 struct xfs_ifork *ifp; /* inode fork pointer */
610 struct xfs_bmbt_key *kp; /* root block key pointer */
611 struct xfs_mount *mp; /* mount structure */
612 xfs_bmbt_ptr_t *pp; /* root block address pointer */
613 struct xfs_iext_cursor icur;
614 struct xfs_bmbt_irec rec;
615 xfs_extnum_t cnt = 0;
618 ASSERT(whichfork != XFS_COW_FORK);
619 ifp = XFS_IFORK_PTR(ip, whichfork);
620 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
623 * Make space in the inode incore. This needs to be undone if we fail
624 * to expand the root.
626 xfs_iroot_realloc(ip, 1, whichfork);
631 block = ifp->if_broot;
632 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
633 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
634 XFS_BTREE_LONG_PTRS);
636 * Need a cursor. Can't allocate until bb_level is filled in.
638 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
639 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
641 * Convert to a btree with two levels, one record in root.
643 ifp->if_format = XFS_DINODE_FMT_BTREE;
644 memset(&args, 0, sizeof(args));
647 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
648 if (tp->t_firstblock == NULLFSBLOCK) {
649 args.type = XFS_ALLOCTYPE_START_BNO;
650 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
651 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
652 args.type = XFS_ALLOCTYPE_START_BNO;
653 args.fsbno = tp->t_firstblock;
655 args.type = XFS_ALLOCTYPE_NEAR_BNO;
656 args.fsbno = tp->t_firstblock;
658 args.minlen = args.maxlen = args.prod = 1;
659 args.wasdel = wasdel;
661 error = xfs_alloc_vextent(&args);
663 goto out_root_realloc;
665 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
667 goto out_root_realloc;
671 * Allocation can't fail, the space was reserved.
673 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
674 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
675 tp->t_firstblock = args.fsbno;
676 cur->bc_ino.allocated++;
678 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
679 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
680 XFS_FSB_TO_DADDR(mp, args.fsbno),
681 mp->m_bsize, 0, &abp);
683 goto out_unreserve_dquot;
686 * Fill in the child block.
688 abp->b_ops = &xfs_bmbt_buf_ops;
689 ablock = XFS_BUF_TO_BLOCK(abp);
690 xfs_btree_init_block_int(mp, ablock, xfs_buf_daddr(abp),
691 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
692 XFS_BTREE_LONG_PTRS);
694 for_each_xfs_iext(ifp, &icur, &rec) {
695 if (isnullstartblock(rec.br_startblock))
697 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
698 xfs_bmbt_disk_set_all(arp, &rec);
701 ASSERT(cnt == ifp->if_nextents);
702 xfs_btree_set_numrecs(ablock, cnt);
705 * Fill in the root key and pointer.
707 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
708 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
709 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
710 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
711 be16_to_cpu(block->bb_level)));
712 *pp = cpu_to_be64(args.fsbno);
715 * Do all this logging at the end so that
716 * the root is at the right level.
718 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
719 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
720 ASSERT(*curp == NULL);
722 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
726 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
728 xfs_iroot_realloc(ip, -1, whichfork);
729 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
730 ASSERT(ifp->if_broot == NULL);
731 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
737 * Convert a local file to an extents file.
738 * This code is out of bounds for data forks of regular files,
739 * since the file data needs to get logged so things will stay consistent.
740 * (The bmap-level manipulations are ok, though).
743 xfs_bmap_local_to_extents_empty(
744 struct xfs_trans *tp,
745 struct xfs_inode *ip,
748 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
750 ASSERT(whichfork != XFS_COW_FORK);
751 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
752 ASSERT(ifp->if_bytes == 0);
753 ASSERT(ifp->if_nextents == 0);
755 xfs_bmap_forkoff_reset(ip, whichfork);
756 ifp->if_u1.if_root = NULL;
758 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
759 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
763 STATIC int /* error */
764 xfs_bmap_local_to_extents(
765 xfs_trans_t *tp, /* transaction pointer */
766 xfs_inode_t *ip, /* incore inode pointer */
767 xfs_extlen_t total, /* total blocks needed by transaction */
768 int *logflagsp, /* inode logging flags */
770 void (*init_fn)(struct xfs_trans *tp,
772 struct xfs_inode *ip,
773 struct xfs_ifork *ifp))
776 int flags; /* logging flags returned */
777 struct xfs_ifork *ifp; /* inode fork pointer */
778 xfs_alloc_arg_t args; /* allocation arguments */
779 struct xfs_buf *bp; /* buffer for extent block */
780 struct xfs_bmbt_irec rec;
781 struct xfs_iext_cursor icur;
784 * We don't want to deal with the case of keeping inode data inline yet.
785 * So sending the data fork of a regular inode is invalid.
787 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
788 ifp = XFS_IFORK_PTR(ip, whichfork);
789 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
791 if (!ifp->if_bytes) {
792 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
793 flags = XFS_ILOG_CORE;
799 memset(&args, 0, sizeof(args));
801 args.mp = ip->i_mount;
802 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
804 * Allocate a block. We know we need only one, since the
805 * file currently fits in an inode.
807 if (tp->t_firstblock == NULLFSBLOCK) {
808 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
809 args.type = XFS_ALLOCTYPE_START_BNO;
811 args.fsbno = tp->t_firstblock;
812 args.type = XFS_ALLOCTYPE_NEAR_BNO;
815 args.minlen = args.maxlen = args.prod = 1;
816 error = xfs_alloc_vextent(&args);
820 /* Can't fail, the space was reserved. */
821 ASSERT(args.fsbno != NULLFSBLOCK);
822 ASSERT(args.len == 1);
823 tp->t_firstblock = args.fsbno;
824 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
825 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
826 args.mp->m_bsize, 0, &bp);
831 * Initialize the block, copy the data and log the remote buffer.
833 * The callout is responsible for logging because the remote format
834 * might differ from the local format and thus we don't know how much to
835 * log here. Note that init_fn must also set the buffer log item type
838 init_fn(tp, bp, ip, ifp);
840 /* account for the change in fork size */
841 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
842 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
843 flags |= XFS_ILOG_CORE;
845 ifp->if_u1.if_root = NULL;
849 rec.br_startblock = args.fsbno;
850 rec.br_blockcount = 1;
851 rec.br_state = XFS_EXT_NORM;
852 xfs_iext_first(ifp, &icur);
853 xfs_iext_insert(ip, &icur, &rec, 0);
855 ifp->if_nextents = 1;
857 xfs_trans_mod_dquot_byino(tp, ip,
858 XFS_TRANS_DQ_BCOUNT, 1L);
859 flags |= xfs_ilog_fext(whichfork);
867 * Called from xfs_bmap_add_attrfork to handle btree format files.
869 STATIC int /* error */
870 xfs_bmap_add_attrfork_btree(
871 xfs_trans_t *tp, /* transaction pointer */
872 xfs_inode_t *ip, /* incore inode pointer */
873 int *flags) /* inode logging flags */
875 struct xfs_btree_block *block = ip->i_df.if_broot;
876 struct xfs_btree_cur *cur; /* btree cursor */
877 int error; /* error return value */
878 xfs_mount_t *mp; /* file system mount struct */
879 int stat; /* newroot status */
883 if (XFS_BMAP_BMDR_SPACE(block) <= XFS_IFORK_DSIZE(ip))
884 *flags |= XFS_ILOG_DBROOT;
886 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
887 error = xfs_bmbt_lookup_first(cur, &stat);
890 /* must be at least one entry */
891 if (XFS_IS_CORRUPT(mp, stat != 1)) {
892 error = -EFSCORRUPTED;
895 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
898 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
901 cur->bc_ino.allocated = 0;
902 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
906 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
911 * Called from xfs_bmap_add_attrfork to handle extents format files.
913 STATIC int /* error */
914 xfs_bmap_add_attrfork_extents(
915 struct xfs_trans *tp, /* transaction pointer */
916 struct xfs_inode *ip, /* incore inode pointer */
917 int *flags) /* inode logging flags */
919 struct xfs_btree_cur *cur; /* bmap btree cursor */
920 int error; /* error return value */
922 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
926 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
929 cur->bc_ino.allocated = 0;
930 xfs_btree_del_cursor(cur, error);
936 * Called from xfs_bmap_add_attrfork to handle local format files. Each
937 * different data fork content type needs a different callout to do the
938 * conversion. Some are basic and only require special block initialisation
939 * callouts for the data formating, others (directories) are so specialised they
940 * handle everything themselves.
942 * XXX (dgc): investigate whether directory conversion can use the generic
943 * formatting callout. It should be possible - it's just a very complex
946 STATIC int /* error */
947 xfs_bmap_add_attrfork_local(
948 struct xfs_trans *tp, /* transaction pointer */
949 struct xfs_inode *ip, /* incore inode pointer */
950 int *flags) /* inode logging flags */
952 struct xfs_da_args dargs; /* args for dir/attr code */
954 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
957 if (S_ISDIR(VFS_I(ip)->i_mode)) {
958 memset(&dargs, 0, sizeof(dargs));
959 dargs.geo = ip->i_mount->m_dir_geo;
961 dargs.total = dargs.geo->fsbcount;
962 dargs.whichfork = XFS_DATA_FORK;
964 return xfs_dir2_sf_to_block(&dargs);
967 if (S_ISLNK(VFS_I(ip)->i_mode))
968 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
970 xfs_symlink_local_to_remote);
972 /* should only be called for types that support local format data */
974 return -EFSCORRUPTED;
978 * Set an inode attr fork offset based on the format of the data fork.
981 xfs_bmap_set_attrforkoff(
982 struct xfs_inode *ip,
986 int default_size = xfs_default_attroffset(ip) >> 3;
988 switch (ip->i_df.if_format) {
989 case XFS_DINODE_FMT_DEV:
990 ip->i_forkoff = default_size;
992 case XFS_DINODE_FMT_LOCAL:
993 case XFS_DINODE_FMT_EXTENTS:
994 case XFS_DINODE_FMT_BTREE:
995 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
997 ip->i_forkoff = default_size;
998 else if (xfs_has_attr2(ip->i_mount) && version)
1010 * Convert inode from non-attributed to attributed.
1011 * Must not be in a transaction, ip must not be locked.
1013 int /* error code */
1014 xfs_bmap_add_attrfork(
1015 xfs_inode_t *ip, /* incore inode pointer */
1016 int size, /* space new attribute needs */
1017 int rsvd) /* xact may use reserved blks */
1019 xfs_mount_t *mp; /* mount structure */
1020 xfs_trans_t *tp; /* transaction pointer */
1021 int blks; /* space reservation */
1022 int version = 1; /* superblock attr version */
1023 int logflags; /* logging flags */
1024 int error; /* error return value */
1026 ASSERT(XFS_IFORK_Q(ip) == 0);
1029 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1031 blks = XFS_ADDAFORK_SPACE_RES(mp);
1033 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
1037 if (XFS_IFORK_Q(ip))
1040 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1041 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1044 ASSERT(ip->i_afp == NULL);
1046 ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
1048 switch (ip->i_df.if_format) {
1049 case XFS_DINODE_FMT_LOCAL:
1050 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1052 case XFS_DINODE_FMT_EXTENTS:
1053 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1055 case XFS_DINODE_FMT_BTREE:
1056 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1063 xfs_trans_log_inode(tp, ip, logflags);
1066 if (!xfs_has_attr(mp) ||
1067 (!xfs_has_attr2(mp) && version == 2)) {
1068 bool log_sb = false;
1070 spin_lock(&mp->m_sb_lock);
1071 if (!xfs_has_attr(mp)) {
1075 if (!xfs_has_attr2(mp) && version == 2) {
1079 spin_unlock(&mp->m_sb_lock);
1084 error = xfs_trans_commit(tp);
1085 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1089 xfs_trans_cancel(tp);
1090 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1095 * Internal and external extent tree search functions.
1098 struct xfs_iread_state {
1099 struct xfs_iext_cursor icur;
1100 xfs_extnum_t loaded;
1103 /* Stuff every bmbt record from this block into the incore extent map. */
1105 xfs_iread_bmbt_block(
1106 struct xfs_btree_cur *cur,
1110 struct xfs_iread_state *ir = priv;
1111 struct xfs_mount *mp = cur->bc_mp;
1112 struct xfs_inode *ip = cur->bc_ino.ip;
1113 struct xfs_btree_block *block;
1115 struct xfs_bmbt_rec *frp;
1116 xfs_extnum_t num_recs;
1118 int whichfork = cur->bc_ino.whichfork;
1119 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1121 block = xfs_btree_get_block(cur, level, &bp);
1123 /* Abort if we find more records than nextents. */
1124 num_recs = xfs_btree_get_numrecs(block);
1125 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1126 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1127 (unsigned long long)ip->i_ino);
1128 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1129 sizeof(*block), __this_address);
1130 return -EFSCORRUPTED;
1133 /* Copy records into the incore cache. */
1134 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1135 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1136 struct xfs_bmbt_irec new;
1139 xfs_bmbt_disk_get_all(frp, &new);
1140 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1142 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1143 "xfs_iread_extents(2)", frp,
1145 return -EFSCORRUPTED;
1147 xfs_iext_insert(ip, &ir->icur, &new,
1148 xfs_bmap_fork_to_state(whichfork));
1149 trace_xfs_read_extent(ip, &ir->icur,
1150 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1151 xfs_iext_next(ifp, &ir->icur);
1158 * Read in extents from a btree-format inode.
1162 struct xfs_trans *tp,
1163 struct xfs_inode *ip,
1166 struct xfs_iread_state ir;
1167 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1168 struct xfs_mount *mp = ip->i_mount;
1169 struct xfs_btree_cur *cur;
1172 if (!xfs_need_iread_extents(ifp))
1175 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1178 xfs_iext_first(ifp, &ir.icur);
1179 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1180 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1181 XFS_BTREE_VISIT_RECORDS, &ir);
1182 xfs_btree_del_cursor(cur, error);
1186 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1187 error = -EFSCORRUPTED;
1190 ASSERT(ir.loaded == xfs_iext_count(ifp));
1193 xfs_iext_destroy(ifp);
1198 * Returns the relative block number of the first unused block(s) in the given
1199 * fork with at least "len" logically contiguous blocks free. This is the
1200 * lowest-address hole if the fork has holes, else the first block past the end
1201 * of fork. Return 0 if the fork is currently local (in-inode).
1204 xfs_bmap_first_unused(
1205 struct xfs_trans *tp, /* transaction pointer */
1206 struct xfs_inode *ip, /* incore inode */
1207 xfs_extlen_t len, /* size of hole to find */
1208 xfs_fileoff_t *first_unused, /* unused block */
1209 int whichfork) /* data or attr fork */
1211 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1212 struct xfs_bmbt_irec got;
1213 struct xfs_iext_cursor icur;
1214 xfs_fileoff_t lastaddr = 0;
1215 xfs_fileoff_t lowest, max;
1218 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1223 ASSERT(xfs_ifork_has_extents(ifp));
1225 error = xfs_iread_extents(tp, ip, whichfork);
1229 lowest = max = *first_unused;
1230 for_each_xfs_iext(ifp, &icur, &got) {
1232 * See if the hole before this extent will work.
1234 if (got.br_startoff >= lowest + len &&
1235 got.br_startoff - max >= len)
1237 lastaddr = got.br_startoff + got.br_blockcount;
1238 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1241 *first_unused = max;
1246 * Returns the file-relative block number of the last block - 1 before
1247 * last_block (input value) in the file.
1248 * This is not based on i_size, it is based on the extent records.
1249 * Returns 0 for local files, as they do not have extent records.
1252 xfs_bmap_last_before(
1253 struct xfs_trans *tp, /* transaction pointer */
1254 struct xfs_inode *ip, /* incore inode */
1255 xfs_fileoff_t *last_block, /* last block */
1256 int whichfork) /* data or attr fork */
1258 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1259 struct xfs_bmbt_irec got;
1260 struct xfs_iext_cursor icur;
1263 switch (ifp->if_format) {
1264 case XFS_DINODE_FMT_LOCAL:
1267 case XFS_DINODE_FMT_BTREE:
1268 case XFS_DINODE_FMT_EXTENTS:
1272 return -EFSCORRUPTED;
1275 error = xfs_iread_extents(tp, ip, whichfork);
1279 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1285 xfs_bmap_last_extent(
1286 struct xfs_trans *tp,
1287 struct xfs_inode *ip,
1289 struct xfs_bmbt_irec *rec,
1292 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1293 struct xfs_iext_cursor icur;
1296 error = xfs_iread_extents(tp, ip, whichfork);
1300 xfs_iext_last(ifp, &icur);
1301 if (!xfs_iext_get_extent(ifp, &icur, rec))
1309 * Check the last inode extent to determine whether this allocation will result
1310 * in blocks being allocated at the end of the file. When we allocate new data
1311 * blocks at the end of the file which do not start at the previous data block,
1312 * we will try to align the new blocks at stripe unit boundaries.
1314 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1315 * at, or past the EOF.
1319 struct xfs_bmalloca *bma,
1322 struct xfs_bmbt_irec rec;
1327 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1338 * Check if we are allocation or past the last extent, or at least into
1339 * the last delayed allocated extent.
1341 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1342 (bma->offset >= rec.br_startoff &&
1343 isnullstartblock(rec.br_startblock));
1348 * Returns the file-relative block number of the first block past eof in
1349 * the file. This is not based on i_size, it is based on the extent records.
1350 * Returns 0 for local files, as they do not have extent records.
1353 xfs_bmap_last_offset(
1354 struct xfs_inode *ip,
1355 xfs_fileoff_t *last_block,
1358 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1359 struct xfs_bmbt_irec rec;
1365 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1368 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
1369 return -EFSCORRUPTED;
1371 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1372 if (error || is_empty)
1375 *last_block = rec.br_startoff + rec.br_blockcount;
1380 * Extent tree manipulation functions used during allocation.
1384 * Convert a delayed allocation to a real allocation.
1386 STATIC int /* error */
1387 xfs_bmap_add_extent_delay_real(
1388 struct xfs_bmalloca *bma,
1391 struct xfs_mount *mp = bma->ip->i_mount;
1392 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1393 struct xfs_bmbt_irec *new = &bma->got;
1394 int error; /* error return value */
1395 int i; /* temp state */
1396 xfs_fileoff_t new_endoff; /* end offset of new entry */
1397 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1398 /* left is 0, right is 1, prev is 2 */
1399 int rval=0; /* return value (logging flags) */
1400 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1401 xfs_filblks_t da_new; /* new count del alloc blocks used */
1402 xfs_filblks_t da_old; /* old count del alloc blocks used */
1403 xfs_filblks_t temp=0; /* value for da_new calculations */
1404 int tmp_rval; /* partial logging flags */
1405 struct xfs_bmbt_irec old;
1407 ASSERT(whichfork != XFS_ATTR_FORK);
1408 ASSERT(!isnullstartblock(new->br_startblock));
1410 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1412 XFS_STATS_INC(mp, xs_add_exlist);
1419 * Set up a bunch of variables to make the tests simpler.
1421 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1422 new_endoff = new->br_startoff + new->br_blockcount;
1423 ASSERT(isnullstartblock(PREV.br_startblock));
1424 ASSERT(PREV.br_startoff <= new->br_startoff);
1425 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1427 da_old = startblockval(PREV.br_startblock);
1431 * Set flags determining what part of the previous delayed allocation
1432 * extent is being replaced by a real allocation.
1434 if (PREV.br_startoff == new->br_startoff)
1435 state |= BMAP_LEFT_FILLING;
1436 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1437 state |= BMAP_RIGHT_FILLING;
1440 * Check and set flags if this segment has a left neighbor.
1441 * Don't set contiguous if the combined extent would be too large.
1443 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1444 state |= BMAP_LEFT_VALID;
1445 if (isnullstartblock(LEFT.br_startblock))
1446 state |= BMAP_LEFT_DELAY;
1449 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1450 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1451 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1452 LEFT.br_state == new->br_state &&
1453 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1454 state |= BMAP_LEFT_CONTIG;
1457 * Check and set flags if this segment has a right neighbor.
1458 * Don't set contiguous if the combined extent would be too large.
1459 * Also check for all-three-contiguous being too large.
1461 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1462 state |= BMAP_RIGHT_VALID;
1463 if (isnullstartblock(RIGHT.br_startblock))
1464 state |= BMAP_RIGHT_DELAY;
1467 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1468 new_endoff == RIGHT.br_startoff &&
1469 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1470 new->br_state == RIGHT.br_state &&
1471 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1472 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1473 BMAP_RIGHT_FILLING)) !=
1474 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1475 BMAP_RIGHT_FILLING) ||
1476 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1477 <= XFS_MAX_BMBT_EXTLEN))
1478 state |= BMAP_RIGHT_CONTIG;
1482 * Switch out based on the FILLING and CONTIG state bits.
1484 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1485 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1486 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1487 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1489 * Filling in all of a previously delayed allocation extent.
1490 * The left and right neighbors are both contiguous with new.
1492 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1494 xfs_iext_remove(bma->ip, &bma->icur, state);
1495 xfs_iext_remove(bma->ip, &bma->icur, state);
1496 xfs_iext_prev(ifp, &bma->icur);
1497 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1500 if (bma->cur == NULL)
1501 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1503 rval = XFS_ILOG_CORE;
1504 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1507 if (XFS_IS_CORRUPT(mp, i != 1)) {
1508 error = -EFSCORRUPTED;
1511 error = xfs_btree_delete(bma->cur, &i);
1514 if (XFS_IS_CORRUPT(mp, i != 1)) {
1515 error = -EFSCORRUPTED;
1518 error = xfs_btree_decrement(bma->cur, 0, &i);
1521 if (XFS_IS_CORRUPT(mp, i != 1)) {
1522 error = -EFSCORRUPTED;
1525 error = xfs_bmbt_update(bma->cur, &LEFT);
1531 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1533 * Filling in all of a previously delayed allocation extent.
1534 * The left neighbor is contiguous, the right is not.
1537 LEFT.br_blockcount += PREV.br_blockcount;
1539 xfs_iext_remove(bma->ip, &bma->icur, state);
1540 xfs_iext_prev(ifp, &bma->icur);
1541 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1543 if (bma->cur == NULL)
1544 rval = XFS_ILOG_DEXT;
1547 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1550 if (XFS_IS_CORRUPT(mp, i != 1)) {
1551 error = -EFSCORRUPTED;
1554 error = xfs_bmbt_update(bma->cur, &LEFT);
1560 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1562 * Filling in all of a previously delayed allocation extent.
1563 * The right neighbor is contiguous, the left is not. Take care
1564 * with delay -> unwritten extent allocation here because the
1565 * delalloc record we are overwriting is always written.
1567 PREV.br_startblock = new->br_startblock;
1568 PREV.br_blockcount += RIGHT.br_blockcount;
1569 PREV.br_state = new->br_state;
1571 xfs_iext_next(ifp, &bma->icur);
1572 xfs_iext_remove(bma->ip, &bma->icur, state);
1573 xfs_iext_prev(ifp, &bma->icur);
1574 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1576 if (bma->cur == NULL)
1577 rval = XFS_ILOG_DEXT;
1580 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1583 if (XFS_IS_CORRUPT(mp, i != 1)) {
1584 error = -EFSCORRUPTED;
1587 error = xfs_bmbt_update(bma->cur, &PREV);
1593 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1595 * Filling in all of a previously delayed allocation extent.
1596 * Neither the left nor right neighbors are contiguous with
1599 PREV.br_startblock = new->br_startblock;
1600 PREV.br_state = new->br_state;
1601 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1604 if (bma->cur == NULL)
1605 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1607 rval = XFS_ILOG_CORE;
1608 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1611 if (XFS_IS_CORRUPT(mp, i != 0)) {
1612 error = -EFSCORRUPTED;
1615 error = xfs_btree_insert(bma->cur, &i);
1618 if (XFS_IS_CORRUPT(mp, i != 1)) {
1619 error = -EFSCORRUPTED;
1625 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1627 * Filling in the first part of a previous delayed allocation.
1628 * The left neighbor is contiguous.
1631 temp = PREV.br_blockcount - new->br_blockcount;
1632 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1633 startblockval(PREV.br_startblock));
1635 LEFT.br_blockcount += new->br_blockcount;
1637 PREV.br_blockcount = temp;
1638 PREV.br_startoff += new->br_blockcount;
1639 PREV.br_startblock = nullstartblock(da_new);
1641 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1642 xfs_iext_prev(ifp, &bma->icur);
1643 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1645 if (bma->cur == NULL)
1646 rval = XFS_ILOG_DEXT;
1649 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1652 if (XFS_IS_CORRUPT(mp, i != 1)) {
1653 error = -EFSCORRUPTED;
1656 error = xfs_bmbt_update(bma->cur, &LEFT);
1662 case BMAP_LEFT_FILLING:
1664 * Filling in the first part of a previous delayed allocation.
1665 * The left neighbor is not contiguous.
1667 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1670 if (bma->cur == NULL)
1671 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1673 rval = XFS_ILOG_CORE;
1674 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1677 if (XFS_IS_CORRUPT(mp, i != 0)) {
1678 error = -EFSCORRUPTED;
1681 error = xfs_btree_insert(bma->cur, &i);
1684 if (XFS_IS_CORRUPT(mp, i != 1)) {
1685 error = -EFSCORRUPTED;
1690 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1691 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1692 &bma->cur, 1, &tmp_rval, whichfork);
1698 temp = PREV.br_blockcount - new->br_blockcount;
1699 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1700 startblockval(PREV.br_startblock) -
1701 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1703 PREV.br_startoff = new_endoff;
1704 PREV.br_blockcount = temp;
1705 PREV.br_startblock = nullstartblock(da_new);
1706 xfs_iext_next(ifp, &bma->icur);
1707 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1708 xfs_iext_prev(ifp, &bma->icur);
1711 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1713 * Filling in the last part of a previous delayed allocation.
1714 * The right neighbor is contiguous with the new allocation.
1717 RIGHT.br_startoff = new->br_startoff;
1718 RIGHT.br_startblock = new->br_startblock;
1719 RIGHT.br_blockcount += new->br_blockcount;
1721 if (bma->cur == NULL)
1722 rval = XFS_ILOG_DEXT;
1725 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1728 if (XFS_IS_CORRUPT(mp, i != 1)) {
1729 error = -EFSCORRUPTED;
1732 error = xfs_bmbt_update(bma->cur, &RIGHT);
1737 temp = PREV.br_blockcount - new->br_blockcount;
1738 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1739 startblockval(PREV.br_startblock));
1741 PREV.br_blockcount = temp;
1742 PREV.br_startblock = nullstartblock(da_new);
1744 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1745 xfs_iext_next(ifp, &bma->icur);
1746 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1749 case BMAP_RIGHT_FILLING:
1751 * Filling in the last part of a previous delayed allocation.
1752 * The right neighbor is not contiguous.
1754 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1757 if (bma->cur == NULL)
1758 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1760 rval = XFS_ILOG_CORE;
1761 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1764 if (XFS_IS_CORRUPT(mp, i != 0)) {
1765 error = -EFSCORRUPTED;
1768 error = xfs_btree_insert(bma->cur, &i);
1771 if (XFS_IS_CORRUPT(mp, i != 1)) {
1772 error = -EFSCORRUPTED;
1777 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1778 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1779 &bma->cur, 1, &tmp_rval, whichfork);
1785 temp = PREV.br_blockcount - new->br_blockcount;
1786 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1787 startblockval(PREV.br_startblock) -
1788 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1790 PREV.br_startblock = nullstartblock(da_new);
1791 PREV.br_blockcount = temp;
1792 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1793 xfs_iext_next(ifp, &bma->icur);
1798 * Filling in the middle part of a previous delayed allocation.
1799 * Contiguity is impossible here.
1800 * This case is avoided almost all the time.
1802 * We start with a delayed allocation:
1804 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1807 * and we are allocating:
1808 * +rrrrrrrrrrrrrrrrr+
1811 * and we set it up for insertion as:
1812 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1814 * PREV @ idx LEFT RIGHT
1815 * inserted at idx + 1
1819 /* LEFT is the new middle */
1822 /* RIGHT is the new right */
1823 RIGHT.br_state = PREV.br_state;
1824 RIGHT.br_startoff = new_endoff;
1825 RIGHT.br_blockcount =
1826 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1827 RIGHT.br_startblock =
1828 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1829 RIGHT.br_blockcount));
1832 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1833 PREV.br_startblock =
1834 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1835 PREV.br_blockcount));
1836 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1838 xfs_iext_next(ifp, &bma->icur);
1839 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1840 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1843 if (bma->cur == NULL)
1844 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1846 rval = XFS_ILOG_CORE;
1847 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1850 if (XFS_IS_CORRUPT(mp, i != 0)) {
1851 error = -EFSCORRUPTED;
1854 error = xfs_btree_insert(bma->cur, &i);
1857 if (XFS_IS_CORRUPT(mp, i != 1)) {
1858 error = -EFSCORRUPTED;
1863 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1864 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1865 &bma->cur, 1, &tmp_rval, whichfork);
1871 da_new = startblockval(PREV.br_startblock) +
1872 startblockval(RIGHT.br_startblock);
1875 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1876 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1877 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1878 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1879 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1880 case BMAP_LEFT_CONTIG:
1881 case BMAP_RIGHT_CONTIG:
1883 * These cases are all impossible.
1888 /* add reverse mapping unless caller opted out */
1889 if (!(bma->flags & XFS_BMAPI_NORMAP))
1890 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1892 /* convert to a btree if necessary */
1893 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1894 int tmp_logflags; /* partial log flag return val */
1896 ASSERT(bma->cur == NULL);
1897 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1898 &bma->cur, da_old > 0, &tmp_logflags,
1900 bma->logflags |= tmp_logflags;
1905 if (da_new != da_old)
1906 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
1909 da_new += bma->cur->bc_ino.allocated;
1910 bma->cur->bc_ino.allocated = 0;
1913 /* adjust for changes in reserved delayed indirect blocks */
1914 if (da_new != da_old) {
1915 ASSERT(state == 0 || da_new < da_old);
1916 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
1920 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1922 if (whichfork != XFS_COW_FORK)
1923 bma->logflags |= rval;
1931 * Convert an unwritten allocation to a real allocation or vice versa.
1934 xfs_bmap_add_extent_unwritten_real(
1935 struct xfs_trans *tp,
1936 xfs_inode_t *ip, /* incore inode pointer */
1938 struct xfs_iext_cursor *icur,
1939 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
1940 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1941 int *logflagsp) /* inode logging flags */
1943 struct xfs_btree_cur *cur; /* btree cursor */
1944 int error; /* error return value */
1945 int i; /* temp state */
1946 struct xfs_ifork *ifp; /* inode fork pointer */
1947 xfs_fileoff_t new_endoff; /* end offset of new entry */
1948 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1949 /* left is 0, right is 1, prev is 2 */
1950 int rval=0; /* return value (logging flags) */
1951 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1952 struct xfs_mount *mp = ip->i_mount;
1953 struct xfs_bmbt_irec old;
1958 ifp = XFS_IFORK_PTR(ip, whichfork);
1960 ASSERT(!isnullstartblock(new->br_startblock));
1962 XFS_STATS_INC(mp, xs_add_exlist);
1969 * Set up a bunch of variables to make the tests simpler.
1972 xfs_iext_get_extent(ifp, icur, &PREV);
1973 ASSERT(new->br_state != PREV.br_state);
1974 new_endoff = new->br_startoff + new->br_blockcount;
1975 ASSERT(PREV.br_startoff <= new->br_startoff);
1976 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1979 * Set flags determining what part of the previous oldext allocation
1980 * extent is being replaced by a newext allocation.
1982 if (PREV.br_startoff == new->br_startoff)
1983 state |= BMAP_LEFT_FILLING;
1984 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1985 state |= BMAP_RIGHT_FILLING;
1988 * Check and set flags if this segment has a left neighbor.
1989 * Don't set contiguous if the combined extent would be too large.
1991 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
1992 state |= BMAP_LEFT_VALID;
1993 if (isnullstartblock(LEFT.br_startblock))
1994 state |= BMAP_LEFT_DELAY;
1997 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1998 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1999 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2000 LEFT.br_state == new->br_state &&
2001 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2002 state |= BMAP_LEFT_CONTIG;
2005 * Check and set flags if this segment has a right neighbor.
2006 * Don't set contiguous if the combined extent would be too large.
2007 * Also check for all-three-contiguous being too large.
2009 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2010 state |= BMAP_RIGHT_VALID;
2011 if (isnullstartblock(RIGHT.br_startblock))
2012 state |= BMAP_RIGHT_DELAY;
2015 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2016 new_endoff == RIGHT.br_startoff &&
2017 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2018 new->br_state == RIGHT.br_state &&
2019 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2020 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2021 BMAP_RIGHT_FILLING)) !=
2022 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2023 BMAP_RIGHT_FILLING) ||
2024 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2025 <= XFS_MAX_BMBT_EXTLEN))
2026 state |= BMAP_RIGHT_CONTIG;
2029 * Switch out based on the FILLING and CONTIG state bits.
2031 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2032 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2033 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2034 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2036 * Setting all of a previous oldext extent to newext.
2037 * The left and right neighbors are both contiguous with new.
2039 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2041 xfs_iext_remove(ip, icur, state);
2042 xfs_iext_remove(ip, icur, state);
2043 xfs_iext_prev(ifp, icur);
2044 xfs_iext_update_extent(ip, state, icur, &LEFT);
2045 ifp->if_nextents -= 2;
2047 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2049 rval = XFS_ILOG_CORE;
2050 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2053 if (XFS_IS_CORRUPT(mp, i != 1)) {
2054 error = -EFSCORRUPTED;
2057 if ((error = xfs_btree_delete(cur, &i)))
2059 if (XFS_IS_CORRUPT(mp, i != 1)) {
2060 error = -EFSCORRUPTED;
2063 if ((error = xfs_btree_decrement(cur, 0, &i)))
2065 if (XFS_IS_CORRUPT(mp, i != 1)) {
2066 error = -EFSCORRUPTED;
2069 if ((error = xfs_btree_delete(cur, &i)))
2071 if (XFS_IS_CORRUPT(mp, i != 1)) {
2072 error = -EFSCORRUPTED;
2075 if ((error = xfs_btree_decrement(cur, 0, &i)))
2077 if (XFS_IS_CORRUPT(mp, i != 1)) {
2078 error = -EFSCORRUPTED;
2081 error = xfs_bmbt_update(cur, &LEFT);
2087 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2089 * Setting all of a previous oldext extent to newext.
2090 * The left neighbor is contiguous, the right is not.
2092 LEFT.br_blockcount += PREV.br_blockcount;
2094 xfs_iext_remove(ip, icur, state);
2095 xfs_iext_prev(ifp, icur);
2096 xfs_iext_update_extent(ip, state, icur, &LEFT);
2099 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2101 rval = XFS_ILOG_CORE;
2102 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2105 if (XFS_IS_CORRUPT(mp, i != 1)) {
2106 error = -EFSCORRUPTED;
2109 if ((error = xfs_btree_delete(cur, &i)))
2111 if (XFS_IS_CORRUPT(mp, i != 1)) {
2112 error = -EFSCORRUPTED;
2115 if ((error = xfs_btree_decrement(cur, 0, &i)))
2117 if (XFS_IS_CORRUPT(mp, i != 1)) {
2118 error = -EFSCORRUPTED;
2121 error = xfs_bmbt_update(cur, &LEFT);
2127 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2129 * Setting all of a previous oldext extent to newext.
2130 * The right neighbor is contiguous, the left is not.
2132 PREV.br_blockcount += RIGHT.br_blockcount;
2133 PREV.br_state = new->br_state;
2135 xfs_iext_next(ifp, icur);
2136 xfs_iext_remove(ip, icur, state);
2137 xfs_iext_prev(ifp, icur);
2138 xfs_iext_update_extent(ip, state, icur, &PREV);
2142 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2144 rval = XFS_ILOG_CORE;
2145 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2148 if (XFS_IS_CORRUPT(mp, i != 1)) {
2149 error = -EFSCORRUPTED;
2152 if ((error = xfs_btree_delete(cur, &i)))
2154 if (XFS_IS_CORRUPT(mp, i != 1)) {
2155 error = -EFSCORRUPTED;
2158 if ((error = xfs_btree_decrement(cur, 0, &i)))
2160 if (XFS_IS_CORRUPT(mp, i != 1)) {
2161 error = -EFSCORRUPTED;
2164 error = xfs_bmbt_update(cur, &PREV);
2170 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2172 * Setting all of a previous oldext extent to newext.
2173 * Neither the left nor right neighbors are contiguous with
2176 PREV.br_state = new->br_state;
2177 xfs_iext_update_extent(ip, state, icur, &PREV);
2180 rval = XFS_ILOG_DEXT;
2183 error = xfs_bmbt_lookup_eq(cur, new, &i);
2186 if (XFS_IS_CORRUPT(mp, i != 1)) {
2187 error = -EFSCORRUPTED;
2190 error = xfs_bmbt_update(cur, &PREV);
2196 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2198 * Setting the first part of a previous oldext extent to newext.
2199 * The left neighbor is contiguous.
2201 LEFT.br_blockcount += new->br_blockcount;
2204 PREV.br_startoff += new->br_blockcount;
2205 PREV.br_startblock += new->br_blockcount;
2206 PREV.br_blockcount -= new->br_blockcount;
2208 xfs_iext_update_extent(ip, state, icur, &PREV);
2209 xfs_iext_prev(ifp, icur);
2210 xfs_iext_update_extent(ip, state, icur, &LEFT);
2213 rval = XFS_ILOG_DEXT;
2216 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2219 if (XFS_IS_CORRUPT(mp, i != 1)) {
2220 error = -EFSCORRUPTED;
2223 error = xfs_bmbt_update(cur, &PREV);
2226 error = xfs_btree_decrement(cur, 0, &i);
2229 error = xfs_bmbt_update(cur, &LEFT);
2235 case BMAP_LEFT_FILLING:
2237 * Setting the first part of a previous oldext extent to newext.
2238 * The left neighbor is not contiguous.
2241 PREV.br_startoff += new->br_blockcount;
2242 PREV.br_startblock += new->br_blockcount;
2243 PREV.br_blockcount -= new->br_blockcount;
2245 xfs_iext_update_extent(ip, state, icur, &PREV);
2246 xfs_iext_insert(ip, icur, new, state);
2250 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2252 rval = XFS_ILOG_CORE;
2253 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2256 if (XFS_IS_CORRUPT(mp, i != 1)) {
2257 error = -EFSCORRUPTED;
2260 error = xfs_bmbt_update(cur, &PREV);
2263 cur->bc_rec.b = *new;
2264 if ((error = xfs_btree_insert(cur, &i)))
2266 if (XFS_IS_CORRUPT(mp, i != 1)) {
2267 error = -EFSCORRUPTED;
2273 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2275 * Setting the last part of a previous oldext extent to newext.
2276 * The right neighbor is contiguous with the new allocation.
2279 PREV.br_blockcount -= new->br_blockcount;
2281 RIGHT.br_startoff = new->br_startoff;
2282 RIGHT.br_startblock = new->br_startblock;
2283 RIGHT.br_blockcount += new->br_blockcount;
2285 xfs_iext_update_extent(ip, state, icur, &PREV);
2286 xfs_iext_next(ifp, icur);
2287 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2290 rval = XFS_ILOG_DEXT;
2293 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2296 if (XFS_IS_CORRUPT(mp, i != 1)) {
2297 error = -EFSCORRUPTED;
2300 error = xfs_bmbt_update(cur, &PREV);
2303 error = xfs_btree_increment(cur, 0, &i);
2306 error = xfs_bmbt_update(cur, &RIGHT);
2312 case BMAP_RIGHT_FILLING:
2314 * Setting the last part of a previous oldext extent to newext.
2315 * The right neighbor is not contiguous.
2318 PREV.br_blockcount -= new->br_blockcount;
2320 xfs_iext_update_extent(ip, state, icur, &PREV);
2321 xfs_iext_next(ifp, icur);
2322 xfs_iext_insert(ip, icur, new, state);
2326 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2328 rval = XFS_ILOG_CORE;
2329 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2332 if (XFS_IS_CORRUPT(mp, i != 1)) {
2333 error = -EFSCORRUPTED;
2336 error = xfs_bmbt_update(cur, &PREV);
2339 error = xfs_bmbt_lookup_eq(cur, new, &i);
2342 if (XFS_IS_CORRUPT(mp, i != 0)) {
2343 error = -EFSCORRUPTED;
2346 if ((error = xfs_btree_insert(cur, &i)))
2348 if (XFS_IS_CORRUPT(mp, i != 1)) {
2349 error = -EFSCORRUPTED;
2357 * Setting the middle part of a previous oldext extent to
2358 * newext. Contiguity is impossible here.
2359 * One extent becomes three extents.
2362 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2365 r[1].br_startoff = new_endoff;
2366 r[1].br_blockcount =
2367 old.br_startoff + old.br_blockcount - new_endoff;
2368 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2369 r[1].br_state = PREV.br_state;
2371 xfs_iext_update_extent(ip, state, icur, &PREV);
2372 xfs_iext_next(ifp, icur);
2373 xfs_iext_insert(ip, icur, &r[1], state);
2374 xfs_iext_insert(ip, icur, &r[0], state);
2375 ifp->if_nextents += 2;
2378 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2380 rval = XFS_ILOG_CORE;
2381 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2384 if (XFS_IS_CORRUPT(mp, i != 1)) {
2385 error = -EFSCORRUPTED;
2388 /* new right extent - oldext */
2389 error = xfs_bmbt_update(cur, &r[1]);
2392 /* new left extent - oldext */
2393 cur->bc_rec.b = PREV;
2394 if ((error = xfs_btree_insert(cur, &i)))
2396 if (XFS_IS_CORRUPT(mp, i != 1)) {
2397 error = -EFSCORRUPTED;
2401 * Reset the cursor to the position of the new extent
2402 * we are about to insert as we can't trust it after
2403 * the previous insert.
2405 error = xfs_bmbt_lookup_eq(cur, new, &i);
2408 if (XFS_IS_CORRUPT(mp, i != 0)) {
2409 error = -EFSCORRUPTED;
2412 /* new middle extent - newext */
2413 if ((error = xfs_btree_insert(cur, &i)))
2415 if (XFS_IS_CORRUPT(mp, i != 1)) {
2416 error = -EFSCORRUPTED;
2422 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2423 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2424 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2425 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2426 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2427 case BMAP_LEFT_CONTIG:
2428 case BMAP_RIGHT_CONTIG:
2430 * These cases are all impossible.
2435 /* update reverse mappings */
2436 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2438 /* convert to a btree if necessary */
2439 if (xfs_bmap_needs_btree(ip, whichfork)) {
2440 int tmp_logflags; /* partial log flag return val */
2442 ASSERT(cur == NULL);
2443 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2444 &tmp_logflags, whichfork);
2445 *logflagsp |= tmp_logflags;
2450 /* clear out the allocated field, done with it now in any case. */
2452 cur->bc_ino.allocated = 0;
2456 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2466 * Convert a hole to a delayed allocation.
2469 xfs_bmap_add_extent_hole_delay(
2470 xfs_inode_t *ip, /* incore inode pointer */
2472 struct xfs_iext_cursor *icur,
2473 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2475 struct xfs_ifork *ifp; /* inode fork pointer */
2476 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2477 xfs_filblks_t newlen=0; /* new indirect size */
2478 xfs_filblks_t oldlen=0; /* old indirect size */
2479 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2480 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2481 xfs_filblks_t temp; /* temp for indirect calculations */
2483 ifp = XFS_IFORK_PTR(ip, whichfork);
2484 ASSERT(isnullstartblock(new->br_startblock));
2487 * Check and set flags if this segment has a left neighbor
2489 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2490 state |= BMAP_LEFT_VALID;
2491 if (isnullstartblock(left.br_startblock))
2492 state |= BMAP_LEFT_DELAY;
2496 * Check and set flags if the current (right) segment exists.
2497 * If it doesn't exist, we're converting the hole at end-of-file.
2499 if (xfs_iext_get_extent(ifp, icur, &right)) {
2500 state |= BMAP_RIGHT_VALID;
2501 if (isnullstartblock(right.br_startblock))
2502 state |= BMAP_RIGHT_DELAY;
2506 * Set contiguity flags on the left and right neighbors.
2507 * Don't let extents get too large, even if the pieces are contiguous.
2509 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2510 left.br_startoff + left.br_blockcount == new->br_startoff &&
2511 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2512 state |= BMAP_LEFT_CONTIG;
2514 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2515 new->br_startoff + new->br_blockcount == right.br_startoff &&
2516 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2517 (!(state & BMAP_LEFT_CONTIG) ||
2518 (left.br_blockcount + new->br_blockcount +
2519 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2520 state |= BMAP_RIGHT_CONTIG;
2523 * Switch out based on the contiguity flags.
2525 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2526 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2528 * New allocation is contiguous with delayed allocations
2529 * on the left and on the right.
2530 * Merge all three into a single extent record.
2532 temp = left.br_blockcount + new->br_blockcount +
2533 right.br_blockcount;
2535 oldlen = startblockval(left.br_startblock) +
2536 startblockval(new->br_startblock) +
2537 startblockval(right.br_startblock);
2538 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2540 left.br_startblock = nullstartblock(newlen);
2541 left.br_blockcount = temp;
2543 xfs_iext_remove(ip, icur, state);
2544 xfs_iext_prev(ifp, icur);
2545 xfs_iext_update_extent(ip, state, icur, &left);
2548 case BMAP_LEFT_CONTIG:
2550 * New allocation is contiguous with a delayed allocation
2552 * Merge the new allocation with the left neighbor.
2554 temp = left.br_blockcount + new->br_blockcount;
2556 oldlen = startblockval(left.br_startblock) +
2557 startblockval(new->br_startblock);
2558 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2560 left.br_blockcount = temp;
2561 left.br_startblock = nullstartblock(newlen);
2563 xfs_iext_prev(ifp, icur);
2564 xfs_iext_update_extent(ip, state, icur, &left);
2567 case BMAP_RIGHT_CONTIG:
2569 * New allocation is contiguous with a delayed allocation
2571 * Merge the new allocation with the right neighbor.
2573 temp = new->br_blockcount + right.br_blockcount;
2574 oldlen = startblockval(new->br_startblock) +
2575 startblockval(right.br_startblock);
2576 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2578 right.br_startoff = new->br_startoff;
2579 right.br_startblock = nullstartblock(newlen);
2580 right.br_blockcount = temp;
2581 xfs_iext_update_extent(ip, state, icur, &right);
2586 * New allocation is not contiguous with another
2587 * delayed allocation.
2588 * Insert a new entry.
2590 oldlen = newlen = 0;
2591 xfs_iext_insert(ip, icur, new, state);
2594 if (oldlen != newlen) {
2595 ASSERT(oldlen > newlen);
2596 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2599 * Nothing to do for disk quota accounting here.
2601 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2606 * Convert a hole to a real allocation.
2608 STATIC int /* error */
2609 xfs_bmap_add_extent_hole_real(
2610 struct xfs_trans *tp,
2611 struct xfs_inode *ip,
2613 struct xfs_iext_cursor *icur,
2614 struct xfs_btree_cur **curp,
2615 struct xfs_bmbt_irec *new,
2619 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2620 struct xfs_mount *mp = ip->i_mount;
2621 struct xfs_btree_cur *cur = *curp;
2622 int error; /* error return value */
2623 int i; /* temp state */
2624 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2625 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2626 int rval=0; /* return value (logging flags) */
2627 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2628 struct xfs_bmbt_irec old;
2630 ASSERT(!isnullstartblock(new->br_startblock));
2631 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2633 XFS_STATS_INC(mp, xs_add_exlist);
2636 * Check and set flags if this segment has a left neighbor.
2638 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2639 state |= BMAP_LEFT_VALID;
2640 if (isnullstartblock(left.br_startblock))
2641 state |= BMAP_LEFT_DELAY;
2645 * Check and set flags if this segment has a current value.
2646 * Not true if we're inserting into the "hole" at eof.
2648 if (xfs_iext_get_extent(ifp, icur, &right)) {
2649 state |= BMAP_RIGHT_VALID;
2650 if (isnullstartblock(right.br_startblock))
2651 state |= BMAP_RIGHT_DELAY;
2655 * We're inserting a real allocation between "left" and "right".
2656 * Set the contiguity flags. Don't let extents get too large.
2658 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2659 left.br_startoff + left.br_blockcount == new->br_startoff &&
2660 left.br_startblock + left.br_blockcount == new->br_startblock &&
2661 left.br_state == new->br_state &&
2662 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2663 state |= BMAP_LEFT_CONTIG;
2665 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2666 new->br_startoff + new->br_blockcount == right.br_startoff &&
2667 new->br_startblock + new->br_blockcount == right.br_startblock &&
2668 new->br_state == right.br_state &&
2669 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2670 (!(state & BMAP_LEFT_CONTIG) ||
2671 left.br_blockcount + new->br_blockcount +
2672 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
2673 state |= BMAP_RIGHT_CONTIG;
2677 * Select which case we're in here, and implement it.
2679 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2680 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2682 * New allocation is contiguous with real allocations on the
2683 * left and on the right.
2684 * Merge all three into a single extent record.
2686 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2688 xfs_iext_remove(ip, icur, state);
2689 xfs_iext_prev(ifp, icur);
2690 xfs_iext_update_extent(ip, state, icur, &left);
2694 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2696 rval = XFS_ILOG_CORE;
2697 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2700 if (XFS_IS_CORRUPT(mp, i != 1)) {
2701 error = -EFSCORRUPTED;
2704 error = xfs_btree_delete(cur, &i);
2707 if (XFS_IS_CORRUPT(mp, i != 1)) {
2708 error = -EFSCORRUPTED;
2711 error = xfs_btree_decrement(cur, 0, &i);
2714 if (XFS_IS_CORRUPT(mp, i != 1)) {
2715 error = -EFSCORRUPTED;
2718 error = xfs_bmbt_update(cur, &left);
2724 case BMAP_LEFT_CONTIG:
2726 * New allocation is contiguous with a real allocation
2728 * Merge the new allocation with the left neighbor.
2731 left.br_blockcount += new->br_blockcount;
2733 xfs_iext_prev(ifp, icur);
2734 xfs_iext_update_extent(ip, state, icur, &left);
2737 rval = xfs_ilog_fext(whichfork);
2740 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2743 if (XFS_IS_CORRUPT(mp, i != 1)) {
2744 error = -EFSCORRUPTED;
2747 error = xfs_bmbt_update(cur, &left);
2753 case BMAP_RIGHT_CONTIG:
2755 * New allocation is contiguous with a real allocation
2757 * Merge the new allocation with the right neighbor.
2761 right.br_startoff = new->br_startoff;
2762 right.br_startblock = new->br_startblock;
2763 right.br_blockcount += new->br_blockcount;
2764 xfs_iext_update_extent(ip, state, icur, &right);
2767 rval = xfs_ilog_fext(whichfork);
2770 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2773 if (XFS_IS_CORRUPT(mp, i != 1)) {
2774 error = -EFSCORRUPTED;
2777 error = xfs_bmbt_update(cur, &right);
2785 * New allocation is not contiguous with another
2787 * Insert a new entry.
2789 xfs_iext_insert(ip, icur, new, state);
2793 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2795 rval = XFS_ILOG_CORE;
2796 error = xfs_bmbt_lookup_eq(cur, new, &i);
2799 if (XFS_IS_CORRUPT(mp, i != 0)) {
2800 error = -EFSCORRUPTED;
2803 error = xfs_btree_insert(cur, &i);
2806 if (XFS_IS_CORRUPT(mp, i != 1)) {
2807 error = -EFSCORRUPTED;
2814 /* add reverse mapping unless caller opted out */
2815 if (!(flags & XFS_BMAPI_NORMAP))
2816 xfs_rmap_map_extent(tp, ip, whichfork, new);
2818 /* convert to a btree if necessary */
2819 if (xfs_bmap_needs_btree(ip, whichfork)) {
2820 int tmp_logflags; /* partial log flag return val */
2822 ASSERT(cur == NULL);
2823 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2824 &tmp_logflags, whichfork);
2825 *logflagsp |= tmp_logflags;
2831 /* clear out the allocated field, done with it now in any case. */
2833 cur->bc_ino.allocated = 0;
2835 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2842 * Functions used in the extent read, allocate and remove paths
2846 * Adjust the size of the new extent based on i_extsize and rt extsize.
2849 xfs_bmap_extsize_align(
2851 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2852 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2853 xfs_extlen_t extsz, /* align to this extent size */
2854 int rt, /* is this a realtime inode? */
2855 int eof, /* is extent at end-of-file? */
2856 int delay, /* creating delalloc extent? */
2857 int convert, /* overwriting unwritten extent? */
2858 xfs_fileoff_t *offp, /* in/out: aligned offset */
2859 xfs_extlen_t *lenp) /* in/out: aligned length */
2861 xfs_fileoff_t orig_off; /* original offset */
2862 xfs_extlen_t orig_alen; /* original length */
2863 xfs_fileoff_t orig_end; /* original off+len */
2864 xfs_fileoff_t nexto; /* next file offset */
2865 xfs_fileoff_t prevo; /* previous file offset */
2866 xfs_fileoff_t align_off; /* temp for offset */
2867 xfs_extlen_t align_alen; /* temp for length */
2868 xfs_extlen_t temp; /* temp for calculations */
2873 orig_off = align_off = *offp;
2874 orig_alen = align_alen = *lenp;
2875 orig_end = orig_off + orig_alen;
2878 * If this request overlaps an existing extent, then don't
2879 * attempt to perform any additional alignment.
2881 if (!delay && !eof &&
2882 (orig_off >= gotp->br_startoff) &&
2883 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2888 * If the file offset is unaligned vs. the extent size
2889 * we need to align it. This will be possible unless
2890 * the file was previously written with a kernel that didn't
2891 * perform this alignment, or if a truncate shot us in the
2894 div_u64_rem(orig_off, extsz, &temp);
2900 /* Same adjustment for the end of the requested area. */
2901 temp = (align_alen % extsz);
2903 align_alen += extsz - temp;
2906 * For large extent hint sizes, the aligned extent might be larger than
2907 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
2908 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
2909 * allocation loops handle short allocation just fine, so it is safe to
2910 * do this. We only want to do it when we are forced to, though, because
2911 * it means more allocation operations are required.
2913 while (align_alen > XFS_MAX_BMBT_EXTLEN)
2914 align_alen -= extsz;
2915 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
2918 * If the previous block overlaps with this proposed allocation
2919 * then move the start forward without adjusting the length.
2921 if (prevp->br_startoff != NULLFILEOFF) {
2922 if (prevp->br_startblock == HOLESTARTBLOCK)
2923 prevo = prevp->br_startoff;
2925 prevo = prevp->br_startoff + prevp->br_blockcount;
2928 if (align_off != orig_off && align_off < prevo)
2931 * If the next block overlaps with this proposed allocation
2932 * then move the start back without adjusting the length,
2933 * but not before offset 0.
2934 * This may of course make the start overlap previous block,
2935 * and if we hit the offset 0 limit then the next block
2936 * can still overlap too.
2938 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2939 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2940 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2941 nexto = gotp->br_startoff + gotp->br_blockcount;
2943 nexto = gotp->br_startoff;
2945 nexto = NULLFILEOFF;
2947 align_off + align_alen != orig_end &&
2948 align_off + align_alen > nexto)
2949 align_off = nexto > align_alen ? nexto - align_alen : 0;
2951 * If we're now overlapping the next or previous extent that
2952 * means we can't fit an extsz piece in this hole. Just move
2953 * the start forward to the first valid spot and set
2954 * the length so we hit the end.
2956 if (align_off != orig_off && align_off < prevo)
2958 if (align_off + align_alen != orig_end &&
2959 align_off + align_alen > nexto &&
2960 nexto != NULLFILEOFF) {
2961 ASSERT(nexto > prevo);
2962 align_alen = nexto - align_off;
2966 * If realtime, and the result isn't a multiple of the realtime
2967 * extent size we need to remove blocks until it is.
2969 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2971 * We're not covering the original request, or
2972 * we won't be able to once we fix the length.
2974 if (orig_off < align_off ||
2975 orig_end > align_off + align_alen ||
2976 align_alen - temp < orig_alen)
2979 * Try to fix it by moving the start up.
2981 if (align_off + temp <= orig_off) {
2986 * Try to fix it by moving the end in.
2988 else if (align_off + align_alen - temp >= orig_end)
2991 * Set the start to the minimum then trim the length.
2994 align_alen -= orig_off - align_off;
2995 align_off = orig_off;
2996 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2999 * Result doesn't cover the request, fail it.
3001 if (orig_off < align_off || orig_end > align_off + align_alen)
3004 ASSERT(orig_off >= align_off);
3005 /* see XFS_BMBT_MAX_EXTLEN handling above */
3006 ASSERT(orig_end <= align_off + align_alen ||
3007 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3011 if (!eof && gotp->br_startoff != NULLFILEOFF)
3012 ASSERT(align_off + align_alen <= gotp->br_startoff);
3013 if (prevp->br_startoff != NULLFILEOFF)
3014 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3022 #define XFS_ALLOC_GAP_UNITS 4
3026 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3028 xfs_fsblock_t adjust; /* adjustment to block numbers */
3029 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3030 xfs_mount_t *mp; /* mount point structure */
3031 int nullfb; /* true if ap->firstblock isn't set */
3032 int rt; /* true if inode is realtime */
3034 #define ISVALID(x,y) \
3036 (x) < mp->m_sb.sb_rblocks : \
3037 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3038 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3039 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3041 mp = ap->ip->i_mount;
3042 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3043 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3044 (ap->datatype & XFS_ALLOC_USERDATA);
3045 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3046 ap->tp->t_firstblock);
3048 * If allocating at eof, and there's a previous real block,
3049 * try to use its last block as our starting point.
3051 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3052 !isnullstartblock(ap->prev.br_startblock) &&
3053 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3054 ap->prev.br_startblock)) {
3055 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3057 * Adjust for the gap between prevp and us.
3059 adjust = ap->offset -
3060 (ap->prev.br_startoff + ap->prev.br_blockcount);
3062 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3063 ap->blkno += adjust;
3066 * If not at eof, then compare the two neighbor blocks.
3067 * Figure out whether either one gives us a good starting point,
3068 * and pick the better one.
3070 else if (!ap->eof) {
3071 xfs_fsblock_t gotbno; /* right side block number */
3072 xfs_fsblock_t gotdiff=0; /* right side difference */
3073 xfs_fsblock_t prevbno; /* left side block number */
3074 xfs_fsblock_t prevdiff=0; /* left side difference */
3077 * If there's a previous (left) block, select a requested
3078 * start block based on it.
3080 if (ap->prev.br_startoff != NULLFILEOFF &&
3081 !isnullstartblock(ap->prev.br_startblock) &&
3082 (prevbno = ap->prev.br_startblock +
3083 ap->prev.br_blockcount) &&
3084 ISVALID(prevbno, ap->prev.br_startblock)) {
3086 * Calculate gap to end of previous block.
3088 adjust = prevdiff = ap->offset -
3089 (ap->prev.br_startoff +
3090 ap->prev.br_blockcount);
3092 * Figure the startblock based on the previous block's
3093 * end and the gap size.
3095 * If the gap is large relative to the piece we're
3096 * allocating, or using it gives us an invalid block
3097 * number, then just use the end of the previous block.
3099 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3100 ISVALID(prevbno + prevdiff,
3101 ap->prev.br_startblock))
3106 * If the firstblock forbids it, can't use it,
3109 if (!rt && !nullfb &&
3110 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3111 prevbno = NULLFSBLOCK;
3114 * No previous block or can't follow it, just default.
3117 prevbno = NULLFSBLOCK;
3119 * If there's a following (right) block, select a requested
3120 * start block based on it.
3122 if (!isnullstartblock(ap->got.br_startblock)) {
3124 * Calculate gap to start of next block.
3126 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3128 * Figure the startblock based on the next block's
3129 * start and the gap size.
3131 gotbno = ap->got.br_startblock;
3134 * If the gap is large relative to the piece we're
3135 * allocating, or using it gives us an invalid block
3136 * number, then just use the start of the next block
3137 * offset by our length.
3139 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3140 ISVALID(gotbno - gotdiff, gotbno))
3142 else if (ISVALID(gotbno - ap->length, gotbno)) {
3143 gotbno -= ap->length;
3144 gotdiff += adjust - ap->length;
3148 * If the firstblock forbids it, can't use it,
3151 if (!rt && !nullfb &&
3152 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3153 gotbno = NULLFSBLOCK;
3156 * No next block, just default.
3159 gotbno = NULLFSBLOCK;
3161 * If both valid, pick the better one, else the only good
3162 * one, else ap->blkno is already set (to 0 or the inode block).
3164 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3165 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3166 else if (prevbno != NULLFSBLOCK)
3167 ap->blkno = prevbno;
3168 else if (gotbno != NULLFSBLOCK)
3175 xfs_bmap_longest_free_extent(
3176 struct xfs_trans *tp,
3181 struct xfs_mount *mp = tp->t_mountp;
3182 struct xfs_perag *pag;
3183 xfs_extlen_t longest;
3186 pag = xfs_perag_get(mp, ag);
3187 if (!pag->pagf_init) {
3188 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3190 /* Couldn't lock the AGF, so skip this AG. */
3191 if (error == -EAGAIN) {
3199 longest = xfs_alloc_longest_free_extent(pag,
3200 xfs_alloc_min_freelist(mp, pag),
3201 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3202 if (*blen < longest)
3211 xfs_bmap_select_minlen(
3212 struct xfs_bmalloca *ap,
3213 struct xfs_alloc_arg *args,
3217 if (notinit || *blen < ap->minlen) {
3219 * Since we did a BUF_TRYLOCK above, it is possible that
3220 * there is space for this request.
3222 args->minlen = ap->minlen;
3223 } else if (*blen < args->maxlen) {
3225 * If the best seen length is less than the request length,
3226 * use the best as the minimum.
3228 args->minlen = *blen;
3231 * Otherwise we've seen an extent as big as maxlen, use that
3234 args->minlen = args->maxlen;
3239 xfs_bmap_btalloc_nullfb(
3240 struct xfs_bmalloca *ap,
3241 struct xfs_alloc_arg *args,
3244 struct xfs_mount *mp = ap->ip->i_mount;
3245 xfs_agnumber_t ag, startag;
3249 args->type = XFS_ALLOCTYPE_START_BNO;
3250 args->total = ap->total;
3252 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3253 if (startag == NULLAGNUMBER)
3256 while (*blen < args->maxlen) {
3257 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3262 if (++ag == mp->m_sb.sb_agcount)
3268 xfs_bmap_select_minlen(ap, args, blen, notinit);
3273 xfs_bmap_btalloc_filestreams(
3274 struct xfs_bmalloca *ap,
3275 struct xfs_alloc_arg *args,
3278 struct xfs_mount *mp = ap->ip->i_mount;
3283 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3284 args->total = ap->total;
3286 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3287 if (ag == NULLAGNUMBER)
3290 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3294 if (*blen < args->maxlen) {
3295 error = xfs_filestream_new_ag(ap, &ag);
3299 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3306 xfs_bmap_select_minlen(ap, args, blen, notinit);
3309 * Set the failure fallback case to look in the selected AG as stream
3312 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3316 /* Update all inode and quota accounting for the allocation we just did. */
3318 xfs_bmap_btalloc_accounting(
3319 struct xfs_bmalloca *ap,
3320 struct xfs_alloc_arg *args)
3322 if (ap->flags & XFS_BMAPI_COWFORK) {
3324 * COW fork blocks are in-core only and thus are treated as
3325 * in-core quota reservation (like delalloc blocks) even when
3326 * converted to real blocks. The quota reservation is not
3327 * accounted to disk until blocks are remapped to the data
3328 * fork. So if these blocks were previously delalloc, we
3329 * already have quota reservation and there's nothing to do
3333 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3338 * Otherwise, we've allocated blocks in a hole. The transaction
3339 * has acquired in-core quota reservation for this extent.
3340 * Rather than account these as real blocks, however, we reduce
3341 * the transaction quota reservation based on the allocation.
3342 * This essentially transfers the transaction quota reservation
3343 * to that of a delalloc extent.
3345 ap->ip->i_delayed_blks += args->len;
3346 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3351 /* data/attr fork only */
3352 ap->ip->i_nblocks += args->len;
3353 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3355 ap->ip->i_delayed_blks -= args->len;
3356 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3358 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3359 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3364 xfs_bmap_compute_alignments(
3365 struct xfs_bmalloca *ap,
3366 struct xfs_alloc_arg *args)
3368 struct xfs_mount *mp = args->mp;
3369 xfs_extlen_t align = 0; /* minimum allocation alignment */
3370 int stripe_align = 0;
3372 /* stripe alignment for allocation is determined by mount parameters */
3373 if (mp->m_swidth && xfs_has_swalloc(mp))
3374 stripe_align = mp->m_swidth;
3375 else if (mp->m_dalign)
3376 stripe_align = mp->m_dalign;
3378 if (ap->flags & XFS_BMAPI_COWFORK)
3379 align = xfs_get_cowextsz_hint(ap->ip);
3380 else if (ap->datatype & XFS_ALLOC_USERDATA)
3381 align = xfs_get_extsz_hint(ap->ip);
3383 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3384 ap->eof, 0, ap->conv, &ap->offset,
3390 /* apply extent size hints if obtained earlier */
3393 div_u64_rem(ap->offset, args->prod, &args->mod);
3395 args->mod = args->prod - args->mod;
3396 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3400 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3401 div_u64_rem(ap->offset, args->prod, &args->mod);
3403 args->mod = args->prod - args->mod;
3406 return stripe_align;
3410 xfs_bmap_process_allocated_extent(
3411 struct xfs_bmalloca *ap,
3412 struct xfs_alloc_arg *args,
3413 xfs_fileoff_t orig_offset,
3414 xfs_extlen_t orig_length)
3418 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3421 * check the allocation happened at the same or higher AG than
3422 * the first block that was allocated.
3425 XFS_FSB_TO_AGNO(args->mp, ap->tp->t_firstblock) <=
3426 XFS_FSB_TO_AGNO(args->mp, args->fsbno));
3428 ap->blkno = args->fsbno;
3430 ap->tp->t_firstblock = args->fsbno;
3431 ap->length = args->len;
3433 * If the extent size hint is active, we tried to round the
3434 * caller's allocation request offset down to extsz and the
3435 * length up to another extsz boundary. If we found a free
3436 * extent we mapped it in starting at this new offset. If the
3437 * newly mapped space isn't long enough to cover any of the
3438 * range of offsets that was originally requested, move the
3439 * mapping up so that we can fill as much of the caller's
3440 * original request as possible. Free space is apparently
3441 * very fragmented so we're unlikely to be able to satisfy the
3444 if (ap->length <= orig_length)
3445 ap->offset = orig_offset;
3446 else if (ap->offset + ap->length < orig_offset + orig_length)
3447 ap->offset = orig_offset + orig_length - ap->length;
3448 xfs_bmap_btalloc_accounting(ap, args);
3453 xfs_bmap_exact_minlen_extent_alloc(
3454 struct xfs_bmalloca *ap)
3456 struct xfs_mount *mp = ap->ip->i_mount;
3457 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3458 xfs_fileoff_t orig_offset;
3459 xfs_extlen_t orig_length;
3464 if (ap->minlen != 1) {
3465 ap->blkno = NULLFSBLOCK;
3470 orig_offset = ap->offset;
3471 orig_length = ap->length;
3473 args.alloc_minlen_only = 1;
3475 xfs_bmap_compute_alignments(ap, &args);
3477 if (ap->tp->t_firstblock == NULLFSBLOCK) {
3479 * Unlike the longest extent available in an AG, we don't track
3480 * the length of an AG's shortest extent.
3481 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3482 * hence we can afford to start traversing from the 0th AG since
3483 * we need not be concerned about a drop in performance in
3484 * "debug only" code paths.
3486 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3488 ap->blkno = ap->tp->t_firstblock;
3491 args.fsbno = ap->blkno;
3492 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3493 args.type = XFS_ALLOCTYPE_FIRST_AG;
3494 args.minlen = args.maxlen = ap->minlen;
3495 args.total = ap->total;
3498 args.minalignslop = 0;
3500 args.minleft = ap->minleft;
3501 args.wasdel = ap->wasdel;
3502 args.resv = XFS_AG_RESV_NONE;
3503 args.datatype = ap->datatype;
3505 error = xfs_alloc_vextent(&args);
3509 if (args.fsbno != NULLFSBLOCK) {
3510 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3513 ap->blkno = NULLFSBLOCK;
3521 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3527 struct xfs_bmalloca *ap)
3529 struct xfs_mount *mp = ap->ip->i_mount;
3530 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3531 xfs_alloctype_t atype = 0;
3532 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3534 xfs_fileoff_t orig_offset;
3535 xfs_extlen_t orig_length;
3537 xfs_extlen_t nextminlen = 0;
3538 int nullfb; /* true if ap->firstblock isn't set */
3545 orig_offset = ap->offset;
3546 orig_length = ap->length;
3548 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3550 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3551 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3552 ap->tp->t_firstblock);
3554 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3555 xfs_inode_is_filestream(ap->ip)) {
3556 ag = xfs_filestream_lookup_ag(ap->ip);
3557 ag = (ag != NULLAGNUMBER) ? ag : 0;
3558 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3560 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3563 ap->blkno = ap->tp->t_firstblock;
3565 xfs_bmap_adjacent(ap);
3568 * If allowed, use ap->blkno; otherwise must use firstblock since
3569 * it's in the right allocation group.
3571 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3574 ap->blkno = ap->tp->t_firstblock;
3576 * Normal allocation, done through xfs_alloc_vextent.
3578 tryagain = isaligned = 0;
3579 args.fsbno = ap->blkno;
3580 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3582 /* Trim the allocation back to the maximum an AG can fit. */
3583 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3587 * Search for an allocation group with a single extent large
3588 * enough for the request. If one isn't found, then adjust
3589 * the minimum allocation size to the largest space found.
3591 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3592 xfs_inode_is_filestream(ap->ip))
3593 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3595 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3598 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3599 if (xfs_inode_is_filestream(ap->ip))
3600 args.type = XFS_ALLOCTYPE_FIRST_AG;
3602 args.type = XFS_ALLOCTYPE_START_BNO;
3603 args.total = args.minlen = ap->minlen;
3605 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3606 args.total = ap->total;
3607 args.minlen = ap->minlen;
3611 * If we are not low on available data blocks, and the underlying
3612 * logical volume manager is a stripe, and the file offset is zero then
3613 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3614 * is only set if the allocation length is >= the stripe unit and the
3615 * allocation offset is at the end of file.
3617 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3619 args.alignment = stripe_align;
3623 * Adjust minlen to try and preserve alignment if we
3624 * can't guarantee an aligned maxlen extent.
3626 if (blen > args.alignment &&
3627 blen <= args.maxlen + args.alignment)
3628 args.minlen = blen - args.alignment;
3629 args.minalignslop = 0;
3632 * First try an exact bno allocation.
3633 * If it fails then do a near or start bno
3634 * allocation with alignment turned on.
3638 args.type = XFS_ALLOCTYPE_THIS_BNO;
3641 * Compute the minlen+alignment for the
3642 * next case. Set slop so that the value
3643 * of minlen+alignment+slop doesn't go up
3644 * between the calls.
3646 if (blen > stripe_align && blen <= args.maxlen)
3647 nextminlen = blen - stripe_align;
3649 nextminlen = args.minlen;
3650 if (nextminlen + stripe_align > args.minlen + 1)
3652 nextminlen + stripe_align -
3655 args.minalignslop = 0;
3659 args.minalignslop = 0;
3661 args.minleft = ap->minleft;
3662 args.wasdel = ap->wasdel;
3663 args.resv = XFS_AG_RESV_NONE;
3664 args.datatype = ap->datatype;
3666 error = xfs_alloc_vextent(&args);
3670 if (tryagain && args.fsbno == NULLFSBLOCK) {
3672 * Exact allocation failed. Now try with alignment
3676 args.fsbno = ap->blkno;
3677 args.alignment = stripe_align;
3678 args.minlen = nextminlen;
3679 args.minalignslop = 0;
3681 if ((error = xfs_alloc_vextent(&args)))
3684 if (isaligned && args.fsbno == NULLFSBLOCK) {
3686 * allocation failed, so turn off alignment and
3690 args.fsbno = ap->blkno;
3692 if ((error = xfs_alloc_vextent(&args)))
3695 if (args.fsbno == NULLFSBLOCK && nullfb &&
3696 args.minlen > ap->minlen) {
3697 args.minlen = ap->minlen;
3698 args.type = XFS_ALLOCTYPE_START_BNO;
3699 args.fsbno = ap->blkno;
3700 if ((error = xfs_alloc_vextent(&args)))
3703 if (args.fsbno == NULLFSBLOCK && nullfb) {
3705 args.type = XFS_ALLOCTYPE_FIRST_AG;
3706 args.total = ap->minlen;
3707 if ((error = xfs_alloc_vextent(&args)))
3709 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3712 if (args.fsbno != NULLFSBLOCK) {
3713 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3716 ap->blkno = NULLFSBLOCK;
3722 /* Trim extent to fit a logical block range. */
3725 struct xfs_bmbt_irec *irec,
3729 xfs_fileoff_t distance;
3730 xfs_fileoff_t end = bno + len;
3732 if (irec->br_startoff + irec->br_blockcount <= bno ||
3733 irec->br_startoff >= end) {
3734 irec->br_blockcount = 0;
3738 if (irec->br_startoff < bno) {
3739 distance = bno - irec->br_startoff;
3740 if (isnullstartblock(irec->br_startblock))
3741 irec->br_startblock = DELAYSTARTBLOCK;
3742 if (irec->br_startblock != DELAYSTARTBLOCK &&
3743 irec->br_startblock != HOLESTARTBLOCK)
3744 irec->br_startblock += distance;
3745 irec->br_startoff += distance;
3746 irec->br_blockcount -= distance;
3749 if (end < irec->br_startoff + irec->br_blockcount) {
3750 distance = irec->br_startoff + irec->br_blockcount - end;
3751 irec->br_blockcount -= distance;
3756 * Trim the returned map to the required bounds
3760 struct xfs_bmbt_irec *mval,
3761 struct xfs_bmbt_irec *got,
3769 if ((flags & XFS_BMAPI_ENTIRE) ||
3770 got->br_startoff + got->br_blockcount <= obno) {
3772 if (isnullstartblock(got->br_startblock))
3773 mval->br_startblock = DELAYSTARTBLOCK;
3779 ASSERT((*bno >= obno) || (n == 0));
3781 mval->br_startoff = *bno;
3782 if (isnullstartblock(got->br_startblock))
3783 mval->br_startblock = DELAYSTARTBLOCK;
3785 mval->br_startblock = got->br_startblock +
3786 (*bno - got->br_startoff);
3788 * Return the minimum of what we got and what we asked for for
3789 * the length. We can use the len variable here because it is
3790 * modified below and we could have been there before coming
3791 * here if the first part of the allocation didn't overlap what
3794 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3795 got->br_blockcount - (*bno - got->br_startoff));
3796 mval->br_state = got->br_state;
3797 ASSERT(mval->br_blockcount <= len);
3802 * Update and validate the extent map to return
3805 xfs_bmapi_update_map(
3806 struct xfs_bmbt_irec **map,
3814 xfs_bmbt_irec_t *mval = *map;
3816 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3817 ((mval->br_startoff + mval->br_blockcount) <= end));
3818 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3819 (mval->br_startoff < obno));
3821 *bno = mval->br_startoff + mval->br_blockcount;
3823 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3824 /* update previous map with new information */
3825 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3826 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3827 ASSERT(mval->br_state == mval[-1].br_state);
3828 mval[-1].br_blockcount = mval->br_blockcount;
3829 mval[-1].br_state = mval->br_state;
3830 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3831 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3832 mval[-1].br_startblock != HOLESTARTBLOCK &&
3833 mval->br_startblock == mval[-1].br_startblock +
3834 mval[-1].br_blockcount &&
3835 mval[-1].br_state == mval->br_state) {
3836 ASSERT(mval->br_startoff ==
3837 mval[-1].br_startoff + mval[-1].br_blockcount);
3838 mval[-1].br_blockcount += mval->br_blockcount;
3839 } else if (*n > 0 &&
3840 mval->br_startblock == DELAYSTARTBLOCK &&
3841 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3842 mval->br_startoff ==
3843 mval[-1].br_startoff + mval[-1].br_blockcount) {
3844 mval[-1].br_blockcount += mval->br_blockcount;
3845 mval[-1].br_state = mval->br_state;
3846 } else if (!((*n == 0) &&
3847 ((mval->br_startoff + mval->br_blockcount) <=
3856 * Map file blocks to filesystem blocks without allocation.
3860 struct xfs_inode *ip,
3863 struct xfs_bmbt_irec *mval,
3867 struct xfs_mount *mp = ip->i_mount;
3868 int whichfork = xfs_bmapi_whichfork(flags);
3869 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3870 struct xfs_bmbt_irec got;
3873 struct xfs_iext_cursor icur;
3879 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3880 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3882 if (WARN_ON_ONCE(!ifp))
3883 return -EFSCORRUPTED;
3885 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3886 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
3887 return -EFSCORRUPTED;
3889 if (xfs_is_shutdown(mp))
3892 XFS_STATS_INC(mp, xs_blk_mapr);
3894 error = xfs_iread_extents(NULL, ip, whichfork);
3898 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3903 while (bno < end && n < *nmap) {
3904 /* Reading past eof, act as though there's a hole up to end. */
3906 got.br_startoff = end;
3907 if (got.br_startoff > bno) {
3908 /* Reading in a hole. */
3909 mval->br_startoff = bno;
3910 mval->br_startblock = HOLESTARTBLOCK;
3911 mval->br_blockcount =
3912 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3913 mval->br_state = XFS_EXT_NORM;
3914 bno += mval->br_blockcount;
3915 len -= mval->br_blockcount;
3921 /* set up the extent map to return. */
3922 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3923 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3925 /* If we're done, stop now. */
3926 if (bno >= end || n >= *nmap)
3929 /* Else go on to the next record. */
3930 if (!xfs_iext_next_extent(ifp, &icur, &got))
3938 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3939 * global pool and the extent inserted into the inode in-core extent tree.
3941 * On entry, got refers to the first extent beyond the offset of the extent to
3942 * allocate or eof is specified if no such extent exists. On return, got refers
3943 * to the extent record that was inserted to the inode fork.
3945 * Note that the allocated extent may have been merged with contiguous extents
3946 * during insertion into the inode fork. Thus, got does not reflect the current
3947 * state of the inode fork on return. If necessary, the caller can use lastx to
3948 * look up the updated record in the inode fork.
3951 xfs_bmapi_reserve_delalloc(
3952 struct xfs_inode *ip,
3956 xfs_filblks_t prealloc,
3957 struct xfs_bmbt_irec *got,
3958 struct xfs_iext_cursor *icur,
3961 struct xfs_mount *mp = ip->i_mount;
3962 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3964 xfs_extlen_t indlen;
3966 xfs_fileoff_t aoff = off;
3969 * Cap the alloc length. Keep track of prealloc so we know whether to
3970 * tag the inode before we return.
3972 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
3974 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3975 if (prealloc && alen >= len)
3976 prealloc = alen - len;
3978 /* Figure out the extent size, adjust alen */
3979 if (whichfork == XFS_COW_FORK) {
3980 struct xfs_bmbt_irec prev;
3981 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3983 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3984 prev.br_startoff = NULLFILEOFF;
3986 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3987 1, 0, &aoff, &alen);
3992 * Make a transaction-less quota reservation for delayed allocation
3993 * blocks. This number gets adjusted later. We return if we haven't
3994 * allocated blocks already inside this loop.
3996 error = xfs_quota_reserve_blkres(ip, alen);
4001 * Split changing sb for alen and indlen since they could be coming
4002 * from different places.
4004 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4007 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4009 goto out_unreserve_quota;
4011 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4013 goto out_unreserve_blocks;
4016 ip->i_delayed_blks += alen;
4017 xfs_mod_delalloc(ip->i_mount, alen + indlen);
4019 got->br_startoff = aoff;
4020 got->br_startblock = nullstartblock(indlen);
4021 got->br_blockcount = alen;
4022 got->br_state = XFS_EXT_NORM;
4024 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4027 * Tag the inode if blocks were preallocated. Note that COW fork
4028 * preallocation can occur at the start or end of the extent, even when
4029 * prealloc == 0, so we must also check the aligned offset and length.
4031 if (whichfork == XFS_DATA_FORK && prealloc)
4032 xfs_inode_set_eofblocks_tag(ip);
4033 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4034 xfs_inode_set_cowblocks_tag(ip);
4038 out_unreserve_blocks:
4039 xfs_mod_fdblocks(mp, alen, false);
4040 out_unreserve_quota:
4041 if (XFS_IS_QUOTA_ON(mp))
4042 xfs_quota_unreserve_blkres(ip, alen);
4047 xfs_bmap_alloc_userdata(
4048 struct xfs_bmalloca *bma)
4050 struct xfs_mount *mp = bma->ip->i_mount;
4051 int whichfork = xfs_bmapi_whichfork(bma->flags);
4055 * Set the data type being allocated. For the data fork, the first data
4056 * in the file is treated differently to all other allocations. For the
4057 * attribute fork, we only need to ensure the allocated range is not on
4060 bma->datatype = XFS_ALLOC_NOBUSY;
4061 if (whichfork == XFS_DATA_FORK) {
4062 bma->datatype |= XFS_ALLOC_USERDATA;
4063 if (bma->offset == 0)
4064 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4066 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4067 error = xfs_bmap_isaeof(bma, whichfork);
4072 if (XFS_IS_REALTIME_INODE(bma->ip))
4073 return xfs_bmap_rtalloc(bma);
4076 if (unlikely(XFS_TEST_ERROR(false, mp,
4077 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4078 return xfs_bmap_exact_minlen_extent_alloc(bma);
4080 return xfs_bmap_btalloc(bma);
4085 struct xfs_bmalloca *bma)
4087 struct xfs_mount *mp = bma->ip->i_mount;
4088 int whichfork = xfs_bmapi_whichfork(bma->flags);
4089 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4090 int tmp_logflags = 0;
4093 ASSERT(bma->length > 0);
4096 * For the wasdelay case, we could also just allocate the stuff asked
4097 * for in this bmap call but that wouldn't be as good.
4100 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4101 bma->offset = bma->got.br_startoff;
4102 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4103 bma->prev.br_startoff = NULLFILEOFF;
4105 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
4107 bma->length = XFS_FILBLKS_MIN(bma->length,
4108 bma->got.br_startoff - bma->offset);
4111 if (bma->flags & XFS_BMAPI_CONTIG)
4112 bma->minlen = bma->length;
4116 if (bma->flags & XFS_BMAPI_METADATA) {
4117 if (unlikely(XFS_TEST_ERROR(false, mp,
4118 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4119 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4121 error = xfs_bmap_btalloc(bma);
4123 error = xfs_bmap_alloc_userdata(bma);
4125 if (error || bma->blkno == NULLFSBLOCK)
4128 if (bma->flags & XFS_BMAPI_ZERO) {
4129 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4134 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4135 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4137 * Bump the number of extents we've allocated
4143 bma->cur->bc_ino.flags =
4144 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4146 bma->got.br_startoff = bma->offset;
4147 bma->got.br_startblock = bma->blkno;
4148 bma->got.br_blockcount = bma->length;
4149 bma->got.br_state = XFS_EXT_NORM;
4151 if (bma->flags & XFS_BMAPI_PREALLOC)
4152 bma->got.br_state = XFS_EXT_UNWRITTEN;
4155 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4157 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4158 whichfork, &bma->icur, &bma->cur, &bma->got,
4159 &bma->logflags, bma->flags);
4161 bma->logflags |= tmp_logflags;
4166 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4167 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4168 * the neighbouring ones.
4170 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4172 ASSERT(bma->got.br_startoff <= bma->offset);
4173 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4174 bma->offset + bma->length);
4175 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4176 bma->got.br_state == XFS_EXT_UNWRITTEN);
4181 xfs_bmapi_convert_unwritten(
4182 struct xfs_bmalloca *bma,
4183 struct xfs_bmbt_irec *mval,
4187 int whichfork = xfs_bmapi_whichfork(flags);
4188 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4189 int tmp_logflags = 0;
4192 /* check if we need to do unwritten->real conversion */
4193 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4194 (flags & XFS_BMAPI_PREALLOC))
4197 /* check if we need to do real->unwritten conversion */
4198 if (mval->br_state == XFS_EXT_NORM &&
4199 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4200 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4204 * Modify (by adding) the state flag, if writing.
4206 ASSERT(mval->br_blockcount <= len);
4207 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4208 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4209 bma->ip, whichfork);
4211 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4212 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4215 * Before insertion into the bmbt, zero the range being converted
4218 if (flags & XFS_BMAPI_ZERO) {
4219 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4220 mval->br_blockcount);
4225 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4226 &bma->icur, &bma->cur, mval, &tmp_logflags);
4228 * Log the inode core unconditionally in the unwritten extent conversion
4229 * path because the conversion might not have done so (e.g., if the
4230 * extent count hasn't changed). We need to make sure the inode is dirty
4231 * in the transaction for the sake of fsync(), even if nothing has
4232 * changed, because fsync() will not force the log for this transaction
4233 * unless it sees the inode pinned.
4235 * Note: If we're only converting cow fork extents, there aren't
4236 * any on-disk updates to make, so we don't need to log anything.
4238 if (whichfork != XFS_COW_FORK)
4239 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4244 * Update our extent pointer, given that
4245 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4246 * of the neighbouring ones.
4248 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4251 * We may have combined previously unwritten space with written space,
4252 * so generate another request.
4254 if (mval->br_blockcount < len)
4259 static inline xfs_extlen_t
4261 struct xfs_trans *tp,
4262 struct xfs_inode *ip,
4265 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork);
4267 if (tp && tp->t_firstblock != NULLFSBLOCK)
4269 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4271 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4275 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4276 * a case where the data is changed, there's an error, and it's not logged so we
4277 * don't shutdown when we should. Don't bother logging extents/btree changes if
4278 * we converted to the other format.
4282 struct xfs_bmalloca *bma,
4286 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4288 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4289 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4290 bma->logflags &= ~xfs_ilog_fext(whichfork);
4291 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4292 ifp->if_format != XFS_DINODE_FMT_BTREE)
4293 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4296 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4298 xfs_btree_del_cursor(bma->cur, error);
4302 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4303 * extent state if necessary. Details behaviour is controlled by the flags
4304 * parameter. Only allocates blocks from a single allocation group, to avoid
4309 struct xfs_trans *tp, /* transaction pointer */
4310 struct xfs_inode *ip, /* incore inode */
4311 xfs_fileoff_t bno, /* starting file offs. mapped */
4312 xfs_filblks_t len, /* length to map in file */
4313 uint32_t flags, /* XFS_BMAPI_... */
4314 xfs_extlen_t total, /* total blocks needed */
4315 struct xfs_bmbt_irec *mval, /* output: map values */
4316 int *nmap) /* i/o: mval size/count */
4318 struct xfs_bmalloca bma = {
4323 struct xfs_mount *mp = ip->i_mount;
4324 int whichfork = xfs_bmapi_whichfork(flags);
4325 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4326 xfs_fileoff_t end; /* end of mapped file region */
4327 bool eof = false; /* after the end of extents */
4328 int error; /* error return */
4329 int n; /* current extent index */
4330 xfs_fileoff_t obno; /* old block number (offset) */
4333 xfs_fileoff_t orig_bno; /* original block number value */
4334 int orig_flags; /* original flags arg value */
4335 xfs_filblks_t orig_len; /* original value of len arg */
4336 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4337 int orig_nmap; /* original value of *nmap */
4347 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4350 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4351 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4352 ASSERT(!(flags & XFS_BMAPI_REMAP));
4354 /* zeroing is for currently only for data extents, not metadata */
4355 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4356 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4358 * we can allocate unwritten extents or pre-zero allocated blocks,
4359 * but it makes no sense to do both at once. This would result in
4360 * zeroing the unwritten extent twice, but it still being an
4361 * unwritten extent....
4363 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4364 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4366 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4367 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4368 return -EFSCORRUPTED;
4371 if (xfs_is_shutdown(mp))
4374 XFS_STATS_INC(mp, xs_blk_mapw);
4376 error = xfs_iread_extents(tp, ip, whichfork);
4380 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4382 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4383 bma.prev.br_startoff = NULLFILEOFF;
4384 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4389 while (bno < end && n < *nmap) {
4390 bool need_alloc = false, wasdelay = false;
4392 /* in hole or beyond EOF? */
4393 if (eof || bma.got.br_startoff > bno) {
4395 * CoW fork conversions should /never/ hit EOF or
4396 * holes. There should always be something for us
4399 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4400 (flags & XFS_BMAPI_COWFORK)));
4403 } else if (isnullstartblock(bma.got.br_startblock)) {
4408 * First, deal with the hole before the allocated space
4409 * that we found, if any.
4411 if (need_alloc || wasdelay) {
4413 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4414 bma.wasdel = wasdelay;
4419 * There's a 32/64 bit type mismatch between the
4420 * allocation length request (which can be 64 bits in
4421 * length) and the bma length request, which is
4422 * xfs_extlen_t and therefore 32 bits. Hence we have to
4423 * check for 32-bit overflows and handle them here.
4425 if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
4426 bma.length = XFS_MAX_BMBT_EXTLEN;
4431 ASSERT(bma.length > 0);
4432 error = xfs_bmapi_allocate(&bma);
4435 if (bma.blkno == NULLFSBLOCK)
4439 * If this is a CoW allocation, record the data in
4440 * the refcount btree for orphan recovery.
4442 if (whichfork == XFS_COW_FORK)
4443 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4447 /* Deal with the allocated space we found. */
4448 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4451 /* Execute unwritten extent conversion if necessary */
4452 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4453 if (error == -EAGAIN)
4458 /* update the extent map to return */
4459 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4462 * If we're done, stop now. Stop when we've allocated
4463 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4464 * the transaction may get too big.
4466 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4469 /* Else go on to the next record. */
4471 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4476 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4481 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4482 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4483 xfs_bmapi_finish(&bma, whichfork, 0);
4484 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4488 xfs_bmapi_finish(&bma, whichfork, error);
4493 * Convert an existing delalloc extent to real blocks based on file offset. This
4494 * attempts to allocate the entire delalloc extent and may require multiple
4495 * invocations to allocate the target offset if a large enough physical extent
4499 xfs_bmapi_convert_delalloc(
4500 struct xfs_inode *ip,
4503 struct iomap *iomap,
4506 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4507 struct xfs_mount *mp = ip->i_mount;
4508 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4509 struct xfs_bmalloca bma = { NULL };
4511 struct xfs_trans *tp;
4514 if (whichfork == XFS_COW_FORK)
4515 flags |= IOMAP_F_SHARED;
4518 * Space for the extent and indirect blocks was reserved when the
4519 * delalloc extent was created so there's no need to do so here.
4521 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4522 XFS_TRANS_RESERVE, &tp);
4526 xfs_ilock(ip, XFS_ILOCK_EXCL);
4527 xfs_trans_ijoin(tp, ip, 0);
4529 error = xfs_iext_count_may_overflow(ip, whichfork,
4530 XFS_IEXT_ADD_NOSPLIT_CNT);
4531 if (error == -EFBIG)
4532 error = xfs_iext_count_upgrade(tp, ip,
4533 XFS_IEXT_ADD_NOSPLIT_CNT);
4535 goto out_trans_cancel;
4537 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4538 bma.got.br_startoff > offset_fsb) {
4540 * No extent found in the range we are trying to convert. This
4541 * should only happen for the COW fork, where another thread
4542 * might have moved the extent to the data fork in the meantime.
4544 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4546 goto out_trans_cancel;
4550 * If we find a real extent here we raced with another thread converting
4551 * the extent. Just return the real extent at this offset.
4553 if (!isnullstartblock(bma.got.br_startblock)) {
4554 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags);
4555 *seq = READ_ONCE(ifp->if_seq);
4556 goto out_trans_cancel;
4562 bma.offset = bma.got.br_startoff;
4563 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
4564 XFS_MAX_BMBT_EXTLEN);
4565 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4568 * When we're converting the delalloc reservations backing dirty pages
4569 * in the page cache, we must be careful about how we create the new
4572 * New CoW fork extents are created unwritten, turned into real extents
4573 * when we're about to write the data to disk, and mapped into the data
4574 * fork after the write finishes. End of story.
4576 * New data fork extents must be mapped in as unwritten and converted
4577 * to real extents after the write succeeds to avoid exposing stale
4578 * disk contents if we crash.
4580 bma.flags = XFS_BMAPI_PREALLOC;
4581 if (whichfork == XFS_COW_FORK)
4582 bma.flags |= XFS_BMAPI_COWFORK;
4584 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4585 bma.prev.br_startoff = NULLFILEOFF;
4587 error = xfs_bmapi_allocate(&bma);
4592 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4594 error = -EFSCORRUPTED;
4595 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
4598 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4599 XFS_STATS_INC(mp, xs_xstrat_quick);
4601 ASSERT(!isnullstartblock(bma.got.br_startblock));
4602 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags);
4603 *seq = READ_ONCE(ifp->if_seq);
4605 if (whichfork == XFS_COW_FORK)
4606 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4608 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4613 xfs_bmapi_finish(&bma, whichfork, 0);
4614 error = xfs_trans_commit(tp);
4615 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4619 xfs_bmapi_finish(&bma, whichfork, error);
4621 xfs_trans_cancel(tp);
4622 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4628 struct xfs_trans *tp,
4629 struct xfs_inode *ip,
4632 xfs_fsblock_t startblock,
4635 struct xfs_mount *mp = ip->i_mount;
4636 struct xfs_ifork *ifp;
4637 struct xfs_btree_cur *cur = NULL;
4638 struct xfs_bmbt_irec got;
4639 struct xfs_iext_cursor icur;
4640 int whichfork = xfs_bmapi_whichfork(flags);
4641 int logflags = 0, error;
4643 ifp = XFS_IFORK_PTR(ip, whichfork);
4645 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4646 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4647 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4648 XFS_BMAPI_NORMAP)));
4649 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4650 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4652 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4653 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4654 return -EFSCORRUPTED;
4657 if (xfs_is_shutdown(mp))
4660 error = xfs_iread_extents(tp, ip, whichfork);
4664 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4665 /* make sure we only reflink into a hole. */
4666 ASSERT(got.br_startoff > bno);
4667 ASSERT(got.br_startoff - bno >= len);
4670 ip->i_nblocks += len;
4671 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4673 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
4674 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4675 cur->bc_ino.flags = 0;
4678 got.br_startoff = bno;
4679 got.br_startblock = startblock;
4680 got.br_blockcount = len;
4681 if (flags & XFS_BMAPI_PREALLOC)
4682 got.br_state = XFS_EXT_UNWRITTEN;
4684 got.br_state = XFS_EXT_NORM;
4686 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4687 &cur, &got, &logflags, flags);
4691 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4694 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4695 logflags &= ~XFS_ILOG_DEXT;
4696 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4697 logflags &= ~XFS_ILOG_DBROOT;
4700 xfs_trans_log_inode(tp, ip, logflags);
4702 xfs_btree_del_cursor(cur, error);
4707 * When a delalloc extent is split (e.g., due to a hole punch), the original
4708 * indlen reservation must be shared across the two new extents that are left
4711 * Given the original reservation and the worst case indlen for the two new
4712 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4713 * reservation fairly across the two new extents. If necessary, steal available
4714 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4715 * ores == 1). The number of stolen blocks is returned. The availability and
4716 * subsequent accounting of stolen blocks is the responsibility of the caller.
4718 static xfs_filblks_t
4719 xfs_bmap_split_indlen(
4720 xfs_filblks_t ores, /* original res. */
4721 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4722 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4723 xfs_filblks_t avail) /* stealable blocks */
4725 xfs_filblks_t len1 = *indlen1;
4726 xfs_filblks_t len2 = *indlen2;
4727 xfs_filblks_t nres = len1 + len2; /* new total res. */
4728 xfs_filblks_t stolen = 0;
4729 xfs_filblks_t resfactor;
4732 * Steal as many blocks as we can to try and satisfy the worst case
4733 * indlen for both new extents.
4735 if (ores < nres && avail)
4736 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4739 /* nothing else to do if we've satisfied the new reservation */
4744 * We can't meet the total required reservation for the two extents.
4745 * Calculate the percent of the overall shortage between both extents
4746 * and apply this percentage to each of the requested indlen values.
4747 * This distributes the shortage fairly and reduces the chances that one
4748 * of the two extents is left with nothing when extents are repeatedly
4751 resfactor = (ores * 100);
4752 do_div(resfactor, nres);
4757 ASSERT(len1 + len2 <= ores);
4758 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4761 * Hand out the remainder to each extent. If one of the two reservations
4762 * is zero, we want to make sure that one gets a block first. The loop
4763 * below starts with len1, so hand len2 a block right off the bat if it
4766 ores -= (len1 + len2);
4767 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4768 if (ores && !len2 && *indlen2) {
4773 if (len1 < *indlen1) {
4779 if (len2 < *indlen2) {
4792 xfs_bmap_del_extent_delay(
4793 struct xfs_inode *ip,
4795 struct xfs_iext_cursor *icur,
4796 struct xfs_bmbt_irec *got,
4797 struct xfs_bmbt_irec *del)
4799 struct xfs_mount *mp = ip->i_mount;
4800 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4801 struct xfs_bmbt_irec new;
4802 int64_t da_old, da_new, da_diff = 0;
4803 xfs_fileoff_t del_endoff, got_endoff;
4804 xfs_filblks_t got_indlen, new_indlen, stolen;
4805 uint32_t state = xfs_bmap_fork_to_state(whichfork);
4809 XFS_STATS_INC(mp, xs_del_exlist);
4811 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4812 del_endoff = del->br_startoff + del->br_blockcount;
4813 got_endoff = got->br_startoff + got->br_blockcount;
4814 da_old = startblockval(got->br_startblock);
4817 ASSERT(del->br_blockcount > 0);
4818 ASSERT(got->br_startoff <= del->br_startoff);
4819 ASSERT(got_endoff >= del_endoff);
4822 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4824 do_div(rtexts, mp->m_sb.sb_rextsize);
4825 xfs_mod_frextents(mp, rtexts);
4829 * Update the inode delalloc counter now and wait to update the
4830 * sb counters as we might have to borrow some blocks for the
4831 * indirect block accounting.
4834 error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4837 ip->i_delayed_blks -= del->br_blockcount;
4839 if (got->br_startoff == del->br_startoff)
4840 state |= BMAP_LEFT_FILLING;
4841 if (got_endoff == del_endoff)
4842 state |= BMAP_RIGHT_FILLING;
4844 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4845 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4847 * Matches the whole extent. Delete the entry.
4849 xfs_iext_remove(ip, icur, state);
4850 xfs_iext_prev(ifp, icur);
4852 case BMAP_LEFT_FILLING:
4854 * Deleting the first part of the extent.
4856 got->br_startoff = del_endoff;
4857 got->br_blockcount -= del->br_blockcount;
4858 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4859 got->br_blockcount), da_old);
4860 got->br_startblock = nullstartblock((int)da_new);
4861 xfs_iext_update_extent(ip, state, icur, got);
4863 case BMAP_RIGHT_FILLING:
4865 * Deleting the last part of the extent.
4867 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4868 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4869 got->br_blockcount), da_old);
4870 got->br_startblock = nullstartblock((int)da_new);
4871 xfs_iext_update_extent(ip, state, icur, got);
4875 * Deleting the middle of the extent.
4877 * Distribute the original indlen reservation across the two new
4878 * extents. Steal blocks from the deleted extent if necessary.
4879 * Stealing blocks simply fudges the fdblocks accounting below.
4880 * Warn if either of the new indlen reservations is zero as this
4881 * can lead to delalloc problems.
4883 got->br_blockcount = del->br_startoff - got->br_startoff;
4884 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4886 new.br_blockcount = got_endoff - del_endoff;
4887 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4889 WARN_ON_ONCE(!got_indlen || !new_indlen);
4890 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4891 del->br_blockcount);
4893 got->br_startblock = nullstartblock((int)got_indlen);
4895 new.br_startoff = del_endoff;
4896 new.br_state = got->br_state;
4897 new.br_startblock = nullstartblock((int)new_indlen);
4899 xfs_iext_update_extent(ip, state, icur, got);
4900 xfs_iext_next(ifp, icur);
4901 xfs_iext_insert(ip, icur, &new, state);
4903 da_new = got_indlen + new_indlen - stolen;
4904 del->br_blockcount -= stolen;
4908 ASSERT(da_old >= da_new);
4909 da_diff = da_old - da_new;
4911 da_diff += del->br_blockcount;
4913 xfs_mod_fdblocks(mp, da_diff, false);
4914 xfs_mod_delalloc(mp, -da_diff);
4920 xfs_bmap_del_extent_cow(
4921 struct xfs_inode *ip,
4922 struct xfs_iext_cursor *icur,
4923 struct xfs_bmbt_irec *got,
4924 struct xfs_bmbt_irec *del)
4926 struct xfs_mount *mp = ip->i_mount;
4927 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4928 struct xfs_bmbt_irec new;
4929 xfs_fileoff_t del_endoff, got_endoff;
4930 uint32_t state = BMAP_COWFORK;
4932 XFS_STATS_INC(mp, xs_del_exlist);
4934 del_endoff = del->br_startoff + del->br_blockcount;
4935 got_endoff = got->br_startoff + got->br_blockcount;
4937 ASSERT(del->br_blockcount > 0);
4938 ASSERT(got->br_startoff <= del->br_startoff);
4939 ASSERT(got_endoff >= del_endoff);
4940 ASSERT(!isnullstartblock(got->br_startblock));
4942 if (got->br_startoff == del->br_startoff)
4943 state |= BMAP_LEFT_FILLING;
4944 if (got_endoff == del_endoff)
4945 state |= BMAP_RIGHT_FILLING;
4947 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4948 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4950 * Matches the whole extent. Delete the entry.
4952 xfs_iext_remove(ip, icur, state);
4953 xfs_iext_prev(ifp, icur);
4955 case BMAP_LEFT_FILLING:
4957 * Deleting the first part of the extent.
4959 got->br_startoff = del_endoff;
4960 got->br_blockcount -= del->br_blockcount;
4961 got->br_startblock = del->br_startblock + del->br_blockcount;
4962 xfs_iext_update_extent(ip, state, icur, got);
4964 case BMAP_RIGHT_FILLING:
4966 * Deleting the last part of the extent.
4968 got->br_blockcount -= del->br_blockcount;
4969 xfs_iext_update_extent(ip, state, icur, got);
4973 * Deleting the middle of the extent.
4975 got->br_blockcount = del->br_startoff - got->br_startoff;
4977 new.br_startoff = del_endoff;
4978 new.br_blockcount = got_endoff - del_endoff;
4979 new.br_state = got->br_state;
4980 new.br_startblock = del->br_startblock + del->br_blockcount;
4982 xfs_iext_update_extent(ip, state, icur, got);
4983 xfs_iext_next(ifp, icur);
4984 xfs_iext_insert(ip, icur, &new, state);
4987 ip->i_delayed_blks -= del->br_blockcount;
4991 * Called by xfs_bmapi to update file extent records and the btree
4992 * after removing space.
4994 STATIC int /* error */
4995 xfs_bmap_del_extent_real(
4996 xfs_inode_t *ip, /* incore inode pointer */
4997 xfs_trans_t *tp, /* current transaction pointer */
4998 struct xfs_iext_cursor *icur,
4999 struct xfs_btree_cur *cur, /* if null, not a btree */
5000 xfs_bmbt_irec_t *del, /* data to remove from extents */
5001 int *logflagsp, /* inode logging flags */
5002 int whichfork, /* data or attr fork */
5003 uint32_t bflags) /* bmapi flags */
5005 xfs_fsblock_t del_endblock=0; /* first block past del */
5006 xfs_fileoff_t del_endoff; /* first offset past del */
5007 int do_fx; /* free extent at end of routine */
5008 int error; /* error return value */
5009 int flags = 0;/* inode logging flags */
5010 struct xfs_bmbt_irec got; /* current extent entry */
5011 xfs_fileoff_t got_endoff; /* first offset past got */
5012 int i; /* temp state */
5013 struct xfs_ifork *ifp; /* inode fork pointer */
5014 xfs_mount_t *mp; /* mount structure */
5015 xfs_filblks_t nblks; /* quota/sb block count */
5016 xfs_bmbt_irec_t new; /* new record to be inserted */
5018 uint qfield; /* quota field to update */
5019 uint32_t state = xfs_bmap_fork_to_state(whichfork);
5020 struct xfs_bmbt_irec old;
5023 XFS_STATS_INC(mp, xs_del_exlist);
5025 ifp = XFS_IFORK_PTR(ip, whichfork);
5026 ASSERT(del->br_blockcount > 0);
5027 xfs_iext_get_extent(ifp, icur, &got);
5028 ASSERT(got.br_startoff <= del->br_startoff);
5029 del_endoff = del->br_startoff + del->br_blockcount;
5030 got_endoff = got.br_startoff + got.br_blockcount;
5031 ASSERT(got_endoff >= del_endoff);
5032 ASSERT(!isnullstartblock(got.br_startblock));
5037 * If it's the case where the directory code is running with no block
5038 * reservation, and the deleted block is in the middle of its extent,
5039 * and the resulting insert of an extent would cause transformation to
5040 * btree format, then reject it. The calling code will then swap blocks
5041 * around instead. We have to do this now, rather than waiting for the
5042 * conversion to btree format, since the transaction will be dirty then.
5044 if (tp->t_blk_res == 0 &&
5045 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5046 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5047 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5050 flags = XFS_ILOG_CORE;
5051 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5055 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5059 if (!(bflags & XFS_BMAPI_REMAP)) {
5062 bno = div_u64_rem(del->br_startblock,
5063 mp->m_sb.sb_rextsize, &mod);
5066 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5072 nblks = len * mp->m_sb.sb_rextsize;
5073 qfield = XFS_TRANS_DQ_RTBCOUNT;
5076 nblks = del->br_blockcount;
5077 qfield = XFS_TRANS_DQ_BCOUNT;
5080 del_endblock = del->br_startblock + del->br_blockcount;
5082 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5085 if (XFS_IS_CORRUPT(mp, i != 1)) {
5086 error = -EFSCORRUPTED;
5091 if (got.br_startoff == del->br_startoff)
5092 state |= BMAP_LEFT_FILLING;
5093 if (got_endoff == del_endoff)
5094 state |= BMAP_RIGHT_FILLING;
5096 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5097 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5099 * Matches the whole extent. Delete the entry.
5101 xfs_iext_remove(ip, icur, state);
5102 xfs_iext_prev(ifp, icur);
5105 flags |= XFS_ILOG_CORE;
5107 flags |= xfs_ilog_fext(whichfork);
5110 if ((error = xfs_btree_delete(cur, &i)))
5112 if (XFS_IS_CORRUPT(mp, i != 1)) {
5113 error = -EFSCORRUPTED;
5117 case BMAP_LEFT_FILLING:
5119 * Deleting the first part of the extent.
5121 got.br_startoff = del_endoff;
5122 got.br_startblock = del_endblock;
5123 got.br_blockcount -= del->br_blockcount;
5124 xfs_iext_update_extent(ip, state, icur, &got);
5126 flags |= xfs_ilog_fext(whichfork);
5129 error = xfs_bmbt_update(cur, &got);
5133 case BMAP_RIGHT_FILLING:
5135 * Deleting the last part of the extent.
5137 got.br_blockcount -= del->br_blockcount;
5138 xfs_iext_update_extent(ip, state, icur, &got);
5140 flags |= xfs_ilog_fext(whichfork);
5143 error = xfs_bmbt_update(cur, &got);
5149 * Deleting the middle of the extent.
5154 got.br_blockcount = del->br_startoff - got.br_startoff;
5155 xfs_iext_update_extent(ip, state, icur, &got);
5157 new.br_startoff = del_endoff;
5158 new.br_blockcount = got_endoff - del_endoff;
5159 new.br_state = got.br_state;
5160 new.br_startblock = del_endblock;
5162 flags |= XFS_ILOG_CORE;
5164 error = xfs_bmbt_update(cur, &got);
5167 error = xfs_btree_increment(cur, 0, &i);
5170 cur->bc_rec.b = new;
5171 error = xfs_btree_insert(cur, &i);
5172 if (error && error != -ENOSPC)
5175 * If get no-space back from btree insert, it tried a
5176 * split, and we have a zero block reservation. Fix up
5177 * our state and return the error.
5179 if (error == -ENOSPC) {
5181 * Reset the cursor, don't trust it after any
5184 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5187 if (XFS_IS_CORRUPT(mp, i != 1)) {
5188 error = -EFSCORRUPTED;
5192 * Update the btree record back
5193 * to the original value.
5195 error = xfs_bmbt_update(cur, &old);
5199 * Reset the extent record back
5200 * to the original value.
5202 xfs_iext_update_extent(ip, state, icur, &old);
5207 if (XFS_IS_CORRUPT(mp, i != 1)) {
5208 error = -EFSCORRUPTED;
5212 flags |= xfs_ilog_fext(whichfork);
5215 xfs_iext_next(ifp, icur);
5216 xfs_iext_insert(ip, icur, &new, state);
5220 /* remove reverse mapping */
5221 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5224 * If we need to, add to list of extents to delete.
5226 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5227 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5228 xfs_refcount_decrease_extent(tp, del);
5230 __xfs_free_extent_later(tp, del->br_startblock,
5231 del->br_blockcount, NULL,
5232 (bflags & XFS_BMAPI_NODISCARD) ||
5233 del->br_state == XFS_EXT_UNWRITTEN);
5238 * Adjust inode # blocks in the file.
5241 ip->i_nblocks -= nblks;
5243 * Adjust quota data.
5245 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5246 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5254 * Unmap (remove) blocks from a file.
5255 * If nexts is nonzero then the number of extents to remove is limited to
5256 * that value. If not all extents in the block range can be removed then
5261 struct xfs_trans *tp, /* transaction pointer */
5262 struct xfs_inode *ip, /* incore inode */
5263 xfs_fileoff_t start, /* first file offset deleted */
5264 xfs_filblks_t *rlen, /* i/o: amount remaining */
5265 uint32_t flags, /* misc flags */
5266 xfs_extnum_t nexts) /* number of extents max */
5268 struct xfs_btree_cur *cur; /* bmap btree cursor */
5269 struct xfs_bmbt_irec del; /* extent being deleted */
5270 int error; /* error return value */
5271 xfs_extnum_t extno; /* extent number in list */
5272 struct xfs_bmbt_irec got; /* current extent record */
5273 struct xfs_ifork *ifp; /* inode fork pointer */
5274 int isrt; /* freeing in rt area */
5275 int logflags; /* transaction logging flags */
5276 xfs_extlen_t mod; /* rt extent offset */
5277 struct xfs_mount *mp = ip->i_mount;
5278 int tmp_logflags; /* partial logging flags */
5279 int wasdel; /* was a delayed alloc extent */
5280 int whichfork; /* data or attribute fork */
5282 xfs_filblks_t len = *rlen; /* length to unmap in file */
5284 struct xfs_iext_cursor icur;
5287 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5289 whichfork = xfs_bmapi_whichfork(flags);
5290 ASSERT(whichfork != XFS_COW_FORK);
5291 ifp = XFS_IFORK_PTR(ip, whichfork);
5292 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
5293 return -EFSCORRUPTED;
5294 if (xfs_is_shutdown(mp))
5297 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5301 error = xfs_iread_extents(tp, ip, whichfork);
5305 if (xfs_iext_count(ifp) == 0) {
5309 XFS_STATS_INC(mp, xs_blk_unmap);
5310 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5313 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5320 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5321 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5322 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5323 cur->bc_ino.flags = 0;
5329 * Synchronize by locking the bitmap inode.
5331 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5332 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5333 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5334 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5338 while (end != (xfs_fileoff_t)-1 && end >= start &&
5339 (nexts == 0 || extno < nexts)) {
5341 * Is the found extent after a hole in which end lives?
5342 * Just back up to the previous extent, if so.
5344 if (got.br_startoff > end &&
5345 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5350 * Is the last block of this extent before the range
5351 * we're supposed to delete? If so, we're done.
5353 end = XFS_FILEOFF_MIN(end,
5354 got.br_startoff + got.br_blockcount - 1);
5358 * Then deal with the (possibly delayed) allocated space
5362 wasdel = isnullstartblock(del.br_startblock);
5364 if (got.br_startoff < start) {
5365 del.br_startoff = start;
5366 del.br_blockcount -= start - got.br_startoff;
5368 del.br_startblock += start - got.br_startoff;
5370 if (del.br_startoff + del.br_blockcount > end + 1)
5371 del.br_blockcount = end + 1 - del.br_startoff;
5376 sum = del.br_startblock + del.br_blockcount;
5377 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5380 * Realtime extent not lined up at the end.
5381 * The extent could have been split into written
5382 * and unwritten pieces, or we could just be
5383 * unmapping part of it. But we can't really
5384 * get rid of part of a realtime extent.
5386 if (del.br_state == XFS_EXT_UNWRITTEN) {
5388 * This piece is unwritten, or we're not
5389 * using unwritten extents. Skip over it.
5392 end -= mod > del.br_blockcount ?
5393 del.br_blockcount : mod;
5394 if (end < got.br_startoff &&
5395 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5402 * It's written, turn it unwritten.
5403 * This is better than zeroing it.
5405 ASSERT(del.br_state == XFS_EXT_NORM);
5406 ASSERT(tp->t_blk_res > 0);
5408 * If this spans a realtime extent boundary,
5409 * chop it back to the start of the one we end at.
5411 if (del.br_blockcount > mod) {
5412 del.br_startoff += del.br_blockcount - mod;
5413 del.br_startblock += del.br_blockcount - mod;
5414 del.br_blockcount = mod;
5416 del.br_state = XFS_EXT_UNWRITTEN;
5417 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5418 whichfork, &icur, &cur, &del,
5424 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5426 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5429 * Realtime extent is lined up at the end but not
5430 * at the front. We'll get rid of full extents if
5433 if (del.br_blockcount > off) {
5434 del.br_blockcount -= off;
5435 del.br_startoff += off;
5436 del.br_startblock += off;
5437 } else if (del.br_startoff == start &&
5438 (del.br_state == XFS_EXT_UNWRITTEN ||
5439 tp->t_blk_res == 0)) {
5441 * Can't make it unwritten. There isn't
5442 * a full extent here so just skip it.
5444 ASSERT(end >= del.br_blockcount);
5445 end -= del.br_blockcount;
5446 if (got.br_startoff > end &&
5447 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5452 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5453 struct xfs_bmbt_irec prev;
5454 xfs_fileoff_t unwrite_start;
5457 * This one is already unwritten.
5458 * It must have a written left neighbor.
5459 * Unwrite the killed part of that one and
5462 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5464 ASSERT(prev.br_state == XFS_EXT_NORM);
5465 ASSERT(!isnullstartblock(prev.br_startblock));
5466 ASSERT(del.br_startblock ==
5467 prev.br_startblock + prev.br_blockcount);
5468 unwrite_start = max3(start,
5469 del.br_startoff - mod,
5471 mod = unwrite_start - prev.br_startoff;
5472 prev.br_startoff = unwrite_start;
5473 prev.br_startblock += mod;
5474 prev.br_blockcount -= mod;
5475 prev.br_state = XFS_EXT_UNWRITTEN;
5476 error = xfs_bmap_add_extent_unwritten_real(tp,
5477 ip, whichfork, &icur, &cur,
5483 ASSERT(del.br_state == XFS_EXT_NORM);
5484 del.br_state = XFS_EXT_UNWRITTEN;
5485 error = xfs_bmap_add_extent_unwritten_real(tp,
5486 ip, whichfork, &icur, &cur,
5496 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5499 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5500 &del, &tmp_logflags, whichfork,
5502 logflags |= tmp_logflags;
5508 end = del.br_startoff - 1;
5511 * If not done go on to the next (previous) record.
5513 if (end != (xfs_fileoff_t)-1 && end >= start) {
5514 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5515 (got.br_startoff > end &&
5516 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5523 if (done || end == (xfs_fileoff_t)-1 || end < start)
5526 *rlen = end - start + 1;
5529 * Convert to a btree if necessary.
5531 if (xfs_bmap_needs_btree(ip, whichfork)) {
5532 ASSERT(cur == NULL);
5533 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5534 &tmp_logflags, whichfork);
5535 logflags |= tmp_logflags;
5537 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5543 * Log everything. Do this after conversion, there's no point in
5544 * logging the extent records if we've converted to btree format.
5546 if ((logflags & xfs_ilog_fext(whichfork)) &&
5547 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5548 logflags &= ~xfs_ilog_fext(whichfork);
5549 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5550 ifp->if_format != XFS_DINODE_FMT_BTREE)
5551 logflags &= ~xfs_ilog_fbroot(whichfork);
5553 * Log inode even in the error case, if the transaction
5554 * is dirty we'll need to shut down the filesystem.
5557 xfs_trans_log_inode(tp, ip, logflags);
5560 cur->bc_ino.allocated = 0;
5561 xfs_btree_del_cursor(cur, error);
5566 /* Unmap a range of a file. */
5570 struct xfs_inode *ip,
5579 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5585 * Determine whether an extent shift can be accomplished by a merge with the
5586 * extent that precedes the target hole of the shift.
5590 struct xfs_bmbt_irec *left, /* preceding extent */
5591 struct xfs_bmbt_irec *got, /* current extent to shift */
5592 xfs_fileoff_t shift) /* shift fsb */
5594 xfs_fileoff_t startoff;
5596 startoff = got->br_startoff - shift;
5599 * The extent, once shifted, must be adjacent in-file and on-disk with
5600 * the preceding extent.
5602 if ((left->br_startoff + left->br_blockcount != startoff) ||
5603 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5604 (left->br_state != got->br_state) ||
5605 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5612 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5613 * hole in the file. If an extent shift would result in the extent being fully
5614 * adjacent to the extent that currently precedes the hole, we can merge with
5615 * the preceding extent rather than do the shift.
5617 * This function assumes the caller has verified a shift-by-merge is possible
5618 * with the provided extents via xfs_bmse_can_merge().
5622 struct xfs_trans *tp,
5623 struct xfs_inode *ip,
5625 xfs_fileoff_t shift, /* shift fsb */
5626 struct xfs_iext_cursor *icur,
5627 struct xfs_bmbt_irec *got, /* extent to shift */
5628 struct xfs_bmbt_irec *left, /* preceding extent */
5629 struct xfs_btree_cur *cur,
5630 int *logflags) /* output */
5632 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5633 struct xfs_bmbt_irec new;
5634 xfs_filblks_t blockcount;
5636 struct xfs_mount *mp = ip->i_mount;
5638 blockcount = left->br_blockcount + got->br_blockcount;
5640 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5641 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5642 ASSERT(xfs_bmse_can_merge(left, got, shift));
5645 new.br_blockcount = blockcount;
5648 * Update the on-disk extent count, the btree if necessary and log the
5652 *logflags |= XFS_ILOG_CORE;
5654 *logflags |= XFS_ILOG_DEXT;
5658 /* lookup and remove the extent to merge */
5659 error = xfs_bmbt_lookup_eq(cur, got, &i);
5662 if (XFS_IS_CORRUPT(mp, i != 1))
5663 return -EFSCORRUPTED;
5665 error = xfs_btree_delete(cur, &i);
5668 if (XFS_IS_CORRUPT(mp, i != 1))
5669 return -EFSCORRUPTED;
5671 /* lookup and update size of the previous extent */
5672 error = xfs_bmbt_lookup_eq(cur, left, &i);
5675 if (XFS_IS_CORRUPT(mp, i != 1))
5676 return -EFSCORRUPTED;
5678 error = xfs_bmbt_update(cur, &new);
5682 /* change to extent format if required after extent removal */
5683 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5688 xfs_iext_remove(ip, icur, 0);
5689 xfs_iext_prev(ifp, icur);
5690 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5693 /* update reverse mapping. rmap functions merge the rmaps for us */
5694 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5695 memcpy(&new, got, sizeof(new));
5696 new.br_startoff = left->br_startoff + left->br_blockcount;
5697 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5702 xfs_bmap_shift_update_extent(
5703 struct xfs_trans *tp,
5704 struct xfs_inode *ip,
5706 struct xfs_iext_cursor *icur,
5707 struct xfs_bmbt_irec *got,
5708 struct xfs_btree_cur *cur,
5710 xfs_fileoff_t startoff)
5712 struct xfs_mount *mp = ip->i_mount;
5713 struct xfs_bmbt_irec prev = *got;
5716 *logflags |= XFS_ILOG_CORE;
5718 got->br_startoff = startoff;
5721 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5724 if (XFS_IS_CORRUPT(mp, i != 1))
5725 return -EFSCORRUPTED;
5727 error = xfs_bmbt_update(cur, got);
5731 *logflags |= XFS_ILOG_DEXT;
5734 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5737 /* update reverse mapping */
5738 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5739 xfs_rmap_map_extent(tp, ip, whichfork, got);
5744 xfs_bmap_collapse_extents(
5745 struct xfs_trans *tp,
5746 struct xfs_inode *ip,
5747 xfs_fileoff_t *next_fsb,
5748 xfs_fileoff_t offset_shift_fsb,
5751 int whichfork = XFS_DATA_FORK;
5752 struct xfs_mount *mp = ip->i_mount;
5753 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5754 struct xfs_btree_cur *cur = NULL;
5755 struct xfs_bmbt_irec got, prev;
5756 struct xfs_iext_cursor icur;
5757 xfs_fileoff_t new_startoff;
5761 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5762 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5763 return -EFSCORRUPTED;
5766 if (xfs_is_shutdown(mp))
5769 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5771 error = xfs_iread_extents(tp, ip, whichfork);
5775 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5776 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5777 cur->bc_ino.flags = 0;
5780 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5784 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5785 error = -EFSCORRUPTED;
5789 new_startoff = got.br_startoff - offset_shift_fsb;
5790 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5791 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5796 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5797 error = xfs_bmse_merge(tp, ip, whichfork,
5798 offset_shift_fsb, &icur, &got, &prev,
5805 if (got.br_startoff < offset_shift_fsb) {
5811 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5812 cur, &logflags, new_startoff);
5817 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5822 *next_fsb = got.br_startoff;
5825 xfs_btree_del_cursor(cur, error);
5827 xfs_trans_log_inode(tp, ip, logflags);
5831 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5833 xfs_bmap_can_insert_extents(
5834 struct xfs_inode *ip,
5836 xfs_fileoff_t shift)
5838 struct xfs_bmbt_irec got;
5842 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5844 if (xfs_is_shutdown(ip->i_mount))
5847 xfs_ilock(ip, XFS_ILOCK_EXCL);
5848 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5849 if (!error && !is_empty && got.br_startoff >= off &&
5850 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5852 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5858 xfs_bmap_insert_extents(
5859 struct xfs_trans *tp,
5860 struct xfs_inode *ip,
5861 xfs_fileoff_t *next_fsb,
5862 xfs_fileoff_t offset_shift_fsb,
5864 xfs_fileoff_t stop_fsb)
5866 int whichfork = XFS_DATA_FORK;
5867 struct xfs_mount *mp = ip->i_mount;
5868 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5869 struct xfs_btree_cur *cur = NULL;
5870 struct xfs_bmbt_irec got, next;
5871 struct xfs_iext_cursor icur;
5872 xfs_fileoff_t new_startoff;
5876 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5877 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5878 return -EFSCORRUPTED;
5881 if (xfs_is_shutdown(mp))
5884 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5886 error = xfs_iread_extents(tp, ip, whichfork);
5890 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5891 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5892 cur->bc_ino.flags = 0;
5895 if (*next_fsb == NULLFSBLOCK) {
5896 xfs_iext_last(ifp, &icur);
5897 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5898 stop_fsb > got.br_startoff) {
5903 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5908 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5909 error = -EFSCORRUPTED;
5913 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
5914 error = -EFSCORRUPTED;
5918 new_startoff = got.br_startoff + offset_shift_fsb;
5919 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5920 if (new_startoff + got.br_blockcount > next.br_startoff) {
5926 * Unlike a left shift (which involves a hole punch), a right
5927 * shift does not modify extent neighbors in any way. We should
5928 * never find mergeable extents in this scenario. Check anyways
5929 * and warn if we encounter two extents that could be one.
5931 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5935 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5936 cur, &logflags, new_startoff);
5940 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5941 stop_fsb >= got.br_startoff + got.br_blockcount) {
5946 *next_fsb = got.br_startoff;
5949 xfs_btree_del_cursor(cur, error);
5951 xfs_trans_log_inode(tp, ip, logflags);
5956 * Splits an extent into two extents at split_fsb block such that it is the
5957 * first block of the current_ext. @ext is a target extent to be split.
5958 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5959 * hole or the first block of extents, just return 0.
5962 xfs_bmap_split_extent(
5963 struct xfs_trans *tp,
5964 struct xfs_inode *ip,
5965 xfs_fileoff_t split_fsb)
5967 int whichfork = XFS_DATA_FORK;
5968 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5969 struct xfs_btree_cur *cur = NULL;
5970 struct xfs_bmbt_irec got;
5971 struct xfs_bmbt_irec new; /* split extent */
5972 struct xfs_mount *mp = ip->i_mount;
5973 xfs_fsblock_t gotblkcnt; /* new block count for got */
5974 struct xfs_iext_cursor icur;
5979 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5980 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5981 return -EFSCORRUPTED;
5984 if (xfs_is_shutdown(mp))
5987 /* Read in all the extents */
5988 error = xfs_iread_extents(tp, ip, whichfork);
5993 * If there are not extents, or split_fsb lies in a hole we are done.
5995 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
5996 got.br_startoff >= split_fsb)
5999 gotblkcnt = split_fsb - got.br_startoff;
6000 new.br_startoff = split_fsb;
6001 new.br_startblock = got.br_startblock + gotblkcnt;
6002 new.br_blockcount = got.br_blockcount - gotblkcnt;
6003 new.br_state = got.br_state;
6005 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6006 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6007 cur->bc_ino.flags = 0;
6008 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6011 if (XFS_IS_CORRUPT(mp, i != 1)) {
6012 error = -EFSCORRUPTED;
6017 got.br_blockcount = gotblkcnt;
6018 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6021 logflags = XFS_ILOG_CORE;
6023 error = xfs_bmbt_update(cur, &got);
6027 logflags |= XFS_ILOG_DEXT;
6029 /* Add new extent */
6030 xfs_iext_next(ifp, &icur);
6031 xfs_iext_insert(ip, &icur, &new, 0);
6035 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6038 if (XFS_IS_CORRUPT(mp, i != 0)) {
6039 error = -EFSCORRUPTED;
6042 error = xfs_btree_insert(cur, &i);
6045 if (XFS_IS_CORRUPT(mp, i != 1)) {
6046 error = -EFSCORRUPTED;
6052 * Convert to a btree if necessary.
6054 if (xfs_bmap_needs_btree(ip, whichfork)) {
6055 int tmp_logflags; /* partial log flag return val */
6057 ASSERT(cur == NULL);
6058 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6059 &tmp_logflags, whichfork);
6060 logflags |= tmp_logflags;
6065 cur->bc_ino.allocated = 0;
6066 xfs_btree_del_cursor(cur, error);
6070 xfs_trans_log_inode(tp, ip, logflags);
6074 /* Deferred mapping is only for real extents in the data fork. */
6076 xfs_bmap_is_update_needed(
6077 struct xfs_bmbt_irec *bmap)
6079 return bmap->br_startblock != HOLESTARTBLOCK &&
6080 bmap->br_startblock != DELAYSTARTBLOCK;
6083 /* Record a bmap intent. */
6086 struct xfs_trans *tp,
6087 enum xfs_bmap_intent_type type,
6088 struct xfs_inode *ip,
6090 struct xfs_bmbt_irec *bmap)
6092 struct xfs_bmap_intent *bi;
6094 trace_xfs_bmap_defer(tp->t_mountp,
6095 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6097 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6098 ip->i_ino, whichfork,
6100 bmap->br_blockcount,
6103 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
6104 INIT_LIST_HEAD(&bi->bi_list);
6107 bi->bi_whichfork = whichfork;
6108 bi->bi_bmap = *bmap;
6110 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6114 /* Map an extent into a file. */
6116 xfs_bmap_map_extent(
6117 struct xfs_trans *tp,
6118 struct xfs_inode *ip,
6119 struct xfs_bmbt_irec *PREV)
6121 if (!xfs_bmap_is_update_needed(PREV))
6124 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6127 /* Unmap an extent out of a file. */
6129 xfs_bmap_unmap_extent(
6130 struct xfs_trans *tp,
6131 struct xfs_inode *ip,
6132 struct xfs_bmbt_irec *PREV)
6134 if (!xfs_bmap_is_update_needed(PREV))
6137 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6141 * Process one of the deferred bmap operations. We pass back the
6142 * btree cursor to maintain our lock on the bmapbt between calls.
6145 xfs_bmap_finish_one(
6146 struct xfs_trans *tp,
6147 struct xfs_inode *ip,
6148 enum xfs_bmap_intent_type type,
6150 xfs_fileoff_t startoff,
6151 xfs_fsblock_t startblock,
6152 xfs_filblks_t *blockcount,
6157 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6159 trace_xfs_bmap_deferred(tp->t_mountp,
6160 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6161 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6162 ip->i_ino, whichfork, startoff, *blockcount, state);
6164 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6165 return -EFSCORRUPTED;
6167 if (XFS_TEST_ERROR(false, tp->t_mountp,
6168 XFS_ERRTAG_BMAP_FINISH_ONE))
6173 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6177 case XFS_BMAP_UNMAP:
6178 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6179 XFS_BMAPI_REMAP, 1);
6183 error = -EFSCORRUPTED;
6189 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6191 xfs_bmap_validate_extent(
6192 struct xfs_inode *ip,
6194 struct xfs_bmbt_irec *irec)
6196 struct xfs_mount *mp = ip->i_mount;
6198 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6199 return __this_address;
6201 if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) {
6202 if (!xfs_verify_rtext(mp, irec->br_startblock,
6203 irec->br_blockcount))
6204 return __this_address;
6206 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6207 irec->br_blockcount))
6208 return __this_address;
6210 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6211 return __this_address;
6216 xfs_bmap_intent_init_cache(void)
6218 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6219 sizeof(struct xfs_bmap_intent),
6222 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6226 xfs_bmap_intent_destroy_cache(void)
6228 kmem_cache_destroy(xfs_bmap_intent_cache);
6229 xfs_bmap_intent_cache = NULL;