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_rtbitmap.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"
39 #include "xfs_health.h"
40 #include "xfs_bmap_item.h"
41 #include "xfs_symlink_remote.h"
43 struct kmem_cache *xfs_bmap_intent_cache;
46 * Miscellaneous helper functions
50 * Compute and fill in the value of the maximum depth of a bmap btree
51 * in this filesystem. Done once, during mount.
54 xfs_bmap_compute_maxlevels(
55 xfs_mount_t *mp, /* file system mount structure */
56 int whichfork) /* data or attr fork */
58 uint64_t maxblocks; /* max blocks at this level */
59 xfs_extnum_t maxleafents; /* max leaf entries possible */
60 int level; /* btree level */
61 int maxrootrecs; /* max records in root block */
62 int minleafrecs; /* min records in leaf block */
63 int minnoderecs; /* min records in node block */
64 int sz; /* root block size */
67 * The maximum number of extents in a fork, hence the maximum number of
68 * leaf entries, is controlled by the size of the on-disk extent count.
70 * Note that we can no longer assume that if we are in ATTR1 that the
71 * fork offset of all the inodes will be
72 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
73 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
74 * but probably at various positions. Therefore, for both ATTR1 and
75 * ATTR2 we have to assume the worst case scenario of a minimum size
78 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
80 if (whichfork == XFS_DATA_FORK)
81 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
83 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
85 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
86 minleafrecs = mp->m_bmap_dmnr[0];
87 minnoderecs = mp->m_bmap_dmnr[1];
88 maxblocks = howmany_64(maxleafents, minleafrecs);
89 for (level = 1; maxblocks > 1; level++) {
90 if (maxblocks <= maxrootrecs)
93 maxblocks = howmany_64(maxblocks, minnoderecs);
95 mp->m_bm_maxlevels[whichfork] = level;
96 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
100 xfs_bmap_compute_attr_offset(
101 struct xfs_mount *mp)
103 if (mp->m_sb.sb_inodesize == 256)
104 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
105 return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
108 STATIC int /* error */
110 struct xfs_btree_cur *cur,
111 struct xfs_bmbt_irec *irec,
112 int *stat) /* success/failure */
114 cur->bc_rec.b = *irec;
115 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
118 STATIC int /* error */
119 xfs_bmbt_lookup_first(
120 struct xfs_btree_cur *cur,
121 int *stat) /* success/failure */
123 cur->bc_rec.b.br_startoff = 0;
124 cur->bc_rec.b.br_startblock = 0;
125 cur->bc_rec.b.br_blockcount = 0;
126 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
130 * Check if the inode needs to be converted to btree format.
132 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
134 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
136 return whichfork != XFS_COW_FORK &&
137 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
138 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
142 * Check if the inode should be converted to extent format.
144 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
146 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
148 return whichfork != XFS_COW_FORK &&
149 ifp->if_format == XFS_DINODE_FMT_BTREE &&
150 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
154 * Update the record referred to by cur to the value given by irec
155 * This either works (return 0) or gets an EFSCORRUPTED error.
159 struct xfs_btree_cur *cur,
160 struct xfs_bmbt_irec *irec)
162 union xfs_btree_rec rec;
164 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
165 return xfs_btree_update(cur, &rec);
169 * Compute the worst-case number of indirect blocks that will be used
170 * for ip's delayed extent of length "len".
173 xfs_bmap_worst_indlen(
174 xfs_inode_t *ip, /* incore inode pointer */
175 xfs_filblks_t len) /* delayed extent length */
177 int level; /* btree level number */
178 int maxrecs; /* maximum record count at this level */
179 xfs_mount_t *mp; /* mount structure */
180 xfs_filblks_t rval; /* return value */
183 maxrecs = mp->m_bmap_dmxr[0];
184 for (level = 0, rval = 0;
185 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
188 do_div(len, maxrecs);
191 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
194 maxrecs = mp->m_bmap_dmxr[1];
200 * Calculate the default attribute fork offset for newly created inodes.
203 xfs_default_attroffset(
204 struct xfs_inode *ip)
206 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
207 return roundup(sizeof(xfs_dev_t), 8);
208 return M_IGEO(ip->i_mount)->attr_fork_offset;
212 * Helper routine to reset inode i_forkoff field when switching attribute fork
213 * from local to extent format - we reset it where possible to make space
214 * available for inline data fork extents.
217 xfs_bmap_forkoff_reset(
221 if (whichfork == XFS_ATTR_FORK &&
222 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
223 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
224 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
226 if (dfl_forkoff > ip->i_forkoff)
227 ip->i_forkoff = dfl_forkoff;
233 struct xfs_mount *mp, /* file system mount point */
234 struct xfs_trans *tp, /* transaction pointer */
235 xfs_fsblock_t fsbno, /* file system block number */
236 struct xfs_buf **bpp) /* buffer for fsbno */
238 struct xfs_buf *bp; /* return value */
241 if (!xfs_verify_fsbno(mp, fsbno))
242 return -EFSCORRUPTED;
243 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
244 XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp,
247 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
254 STATIC struct xfs_buf *
256 struct xfs_btree_cur *cur,
259 struct xfs_log_item *lip;
265 for (i = 0; i < cur->bc_maxlevels; i++) {
266 if (!cur->bc_levels[i].bp)
268 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
269 return cur->bc_levels[i].bp;
272 /* Chase down all the log items to see if the bp is there */
273 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
274 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
276 if (bip->bli_item.li_type == XFS_LI_BUF &&
277 xfs_buf_daddr(bip->bli_buf) == bno)
286 struct xfs_btree_block *block,
292 __be64 *pp, *thispa; /* pointer to block address */
293 xfs_bmbt_key_t *prevp, *keyp;
295 ASSERT(be16_to_cpu(block->bb_level) > 0);
298 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
299 dmxr = mp->m_bmap_dmxr[0];
300 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
303 ASSERT(be64_to_cpu(prevp->br_startoff) <
304 be64_to_cpu(keyp->br_startoff));
309 * Compare the block numbers to see if there are dups.
312 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
314 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
316 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
318 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
320 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
321 if (*thispa == *pp) {
322 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
324 (unsigned long long)be64_to_cpu(*thispa));
325 xfs_err(mp, "%s: ptrs are equal in node\n",
327 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
334 * Check that the extents for the inode ip are in the right order in all
335 * btree leaves. THis becomes prohibitively expensive for large extent count
336 * files, so don't bother with inodes that have more than 10,000 extents in
337 * them. The btree record ordering checks will still be done, so for such large
338 * bmapbt constructs that is going to catch most corruptions.
341 xfs_bmap_check_leaf_extents(
342 struct xfs_btree_cur *cur, /* btree cursor or null */
343 xfs_inode_t *ip, /* incore inode pointer */
344 int whichfork) /* data or attr fork */
346 struct xfs_mount *mp = ip->i_mount;
347 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
348 struct xfs_btree_block *block; /* current btree block */
349 xfs_fsblock_t bno; /* block # of "block" */
350 struct xfs_buf *bp; /* buffer for "block" */
351 int error; /* error return value */
352 xfs_extnum_t i=0, j; /* index into the extents list */
353 int level; /* btree level, for checking */
354 __be64 *pp; /* pointer to block address */
355 xfs_bmbt_rec_t *ep; /* pointer to current extent */
356 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
357 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
360 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
363 /* skip large extent count inodes */
364 if (ip->i_df.if_nextents > 10000)
368 block = ifp->if_broot;
370 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
372 level = be16_to_cpu(block->bb_level);
374 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
375 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
376 bno = be64_to_cpu(*pp);
378 ASSERT(bno != NULLFSBLOCK);
379 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
380 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
383 * Go down the tree until leaf level is reached, following the first
384 * pointer (leftmost) at each level.
386 while (level-- > 0) {
387 /* See if buf is in cur first */
389 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
392 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
393 if (xfs_metadata_is_sick(error))
394 xfs_btree_mark_sick(cur);
398 block = XFS_BUF_TO_BLOCK(bp);
403 * Check this block for basic sanity (increasing keys and
404 * no duplicate blocks).
407 xfs_check_block(block, mp, 0, 0);
408 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
409 bno = be64_to_cpu(*pp);
410 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
411 xfs_btree_mark_sick(cur);
412 error = -EFSCORRUPTED;
417 xfs_trans_brelse(NULL, bp);
422 * Here with bp and block set to the leftmost leaf node in the tree.
427 * Loop over all leaf nodes checking that all extents are in the right order.
430 xfs_fsblock_t nextbno;
431 xfs_extnum_t num_recs;
434 num_recs = xfs_btree_get_numrecs(block);
437 * Read-ahead the next leaf block, if any.
440 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
443 * Check all the extents to make sure they are OK.
444 * If we had a previous block, the last entry should
445 * conform with the first entry in this one.
448 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
450 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
451 xfs_bmbt_disk_get_blockcount(&last) <=
452 xfs_bmbt_disk_get_startoff(ep));
454 for (j = 1; j < num_recs; j++) {
455 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
456 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
457 xfs_bmbt_disk_get_blockcount(ep) <=
458 xfs_bmbt_disk_get_startoff(nextp));
466 xfs_trans_brelse(NULL, bp);
470 * If we've reached the end, stop.
472 if (bno == NULLFSBLOCK)
476 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
479 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
480 if (xfs_metadata_is_sick(error))
481 xfs_btree_mark_sick(cur);
485 block = XFS_BUF_TO_BLOCK(bp);
491 xfs_warn(mp, "%s: at error0", __func__);
493 xfs_trans_brelse(NULL, bp);
495 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
497 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
498 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
503 * Validate that the bmbt_irecs being returned from bmapi are valid
504 * given the caller's original parameters. Specifically check the
505 * ranges of the returned irecs to ensure that they only extend beyond
506 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
509 xfs_bmap_validate_ret(
513 xfs_bmbt_irec_t *mval,
517 int i; /* index to map values */
519 ASSERT(ret_nmap <= nmap);
521 for (i = 0; i < ret_nmap; i++) {
522 ASSERT(mval[i].br_blockcount > 0);
523 if (!(flags & XFS_BMAPI_ENTIRE)) {
524 ASSERT(mval[i].br_startoff >= bno);
525 ASSERT(mval[i].br_blockcount <= len);
526 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
529 ASSERT(mval[i].br_startoff < bno + len);
530 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
534 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
535 mval[i].br_startoff);
536 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
537 mval[i].br_startblock != HOLESTARTBLOCK);
538 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
539 mval[i].br_state == XFS_EXT_UNWRITTEN);
544 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
545 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
549 * Inode fork format manipulation functions
553 * Convert the inode format to extent format if it currently is in btree format,
554 * but the extent list is small enough that it fits into the extent format.
556 * Since the extents are already in-core, all we have to do is give up the space
557 * for the btree root and pitch the leaf block.
559 STATIC int /* error */
560 xfs_bmap_btree_to_extents(
561 struct xfs_trans *tp, /* transaction pointer */
562 struct xfs_inode *ip, /* incore inode pointer */
563 struct xfs_btree_cur *cur, /* btree cursor */
564 int *logflagsp, /* inode logging flags */
565 int whichfork) /* data or attr fork */
567 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
568 struct xfs_mount *mp = ip->i_mount;
569 struct xfs_btree_block *rblock = ifp->if_broot;
570 struct xfs_btree_block *cblock;/* child btree block */
571 xfs_fsblock_t cbno; /* child block number */
572 struct xfs_buf *cbp; /* child block's buffer */
573 int error; /* error return value */
574 __be64 *pp; /* ptr to block address */
575 struct xfs_owner_info oinfo;
577 /* check if we actually need the extent format first: */
578 if (!xfs_bmap_wants_extents(ip, whichfork))
582 ASSERT(whichfork != XFS_COW_FORK);
583 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
584 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
585 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
586 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
588 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
589 cbno = be64_to_cpu(*pp);
591 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_verify_fsbno(mp, cbno))) {
592 xfs_btree_mark_sick(cur);
593 return -EFSCORRUPTED;
596 error = xfs_bmap_read_buf(mp, tp, cbno, &cbp);
597 if (xfs_metadata_is_sick(error))
598 xfs_btree_mark_sick(cur);
601 cblock = XFS_BUF_TO_BLOCK(cbp);
602 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
605 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
606 error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
607 XFS_AG_RESV_NONE, false);
612 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
613 xfs_trans_binval(tp, cbp);
614 if (cur->bc_levels[0].bp == cbp)
615 cur->bc_levels[0].bp = NULL;
616 xfs_iroot_realloc(ip, -1, whichfork);
617 ASSERT(ifp->if_broot == NULL);
618 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
619 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
624 * Convert an extents-format file into a btree-format file.
625 * The new file will have a root block (in the inode) and a single child block.
627 STATIC int /* error */
628 xfs_bmap_extents_to_btree(
629 struct xfs_trans *tp, /* transaction pointer */
630 struct xfs_inode *ip, /* incore inode pointer */
631 struct xfs_btree_cur **curp, /* cursor returned to caller */
632 int wasdel, /* converting a delayed alloc */
633 int *logflagsp, /* inode logging flags */
634 int whichfork) /* data or attr fork */
636 struct xfs_btree_block *ablock; /* allocated (child) bt block */
637 struct xfs_buf *abp; /* buffer for ablock */
638 struct xfs_alloc_arg args; /* allocation arguments */
639 struct xfs_bmbt_rec *arp; /* child record pointer */
640 struct xfs_btree_block *block; /* btree root block */
641 struct xfs_btree_cur *cur; /* bmap btree cursor */
642 int error; /* error return value */
643 struct xfs_ifork *ifp; /* inode fork pointer */
644 struct xfs_bmbt_key *kp; /* root block key pointer */
645 struct xfs_mount *mp; /* mount structure */
646 xfs_bmbt_ptr_t *pp; /* root block address pointer */
647 struct xfs_iext_cursor icur;
648 struct xfs_bmbt_irec rec;
649 xfs_extnum_t cnt = 0;
652 ASSERT(whichfork != XFS_COW_FORK);
653 ifp = xfs_ifork_ptr(ip, whichfork);
654 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
657 * Make space in the inode incore. This needs to be undone if we fail
658 * to expand the root.
660 xfs_iroot_realloc(ip, 1, whichfork);
665 block = ifp->if_broot;
666 xfs_bmbt_init_block(ip, block, NULL, 1, 1);
668 * Need a cursor. Can't allocate until bb_level is filled in.
670 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
672 cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
674 * Convert to a btree with two levels, one record in root.
676 ifp->if_format = XFS_DINODE_FMT_BTREE;
677 memset(&args, 0, sizeof(args));
680 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
682 args.minlen = args.maxlen = args.prod = 1;
683 args.wasdel = wasdel;
685 error = xfs_alloc_vextent_start_ag(&args,
686 XFS_INO_TO_FSB(mp, ip->i_ino));
688 goto out_root_realloc;
691 * Allocation can't fail, the space was reserved.
693 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
695 goto out_root_realloc;
698 cur->bc_bmap.allocated++;
700 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
701 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
702 XFS_FSB_TO_DADDR(mp, args.fsbno),
703 mp->m_bsize, 0, &abp);
705 goto out_unreserve_dquot;
708 * Fill in the child block.
710 ablock = XFS_BUF_TO_BLOCK(abp);
711 xfs_bmbt_init_block(ip, ablock, abp, 0, 0);
713 for_each_xfs_iext(ifp, &icur, &rec) {
714 if (isnullstartblock(rec.br_startblock))
716 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
717 xfs_bmbt_disk_set_all(arp, &rec);
720 ASSERT(cnt == ifp->if_nextents);
721 xfs_btree_set_numrecs(ablock, cnt);
724 * Fill in the root key and pointer.
726 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
727 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
728 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
729 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
730 be16_to_cpu(block->bb_level)));
731 *pp = cpu_to_be64(args.fsbno);
734 * Do all this logging at the end so that
735 * the root is at the right level.
737 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
738 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
739 ASSERT(*curp == NULL);
741 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
745 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
747 xfs_iroot_realloc(ip, -1, whichfork);
748 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
749 ASSERT(ifp->if_broot == NULL);
750 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
756 * Convert a local file to an extents file.
757 * This code is out of bounds for data forks of regular files,
758 * since the file data needs to get logged so things will stay consistent.
759 * (The bmap-level manipulations are ok, though).
762 xfs_bmap_local_to_extents_empty(
763 struct xfs_trans *tp,
764 struct xfs_inode *ip,
767 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
769 ASSERT(whichfork != XFS_COW_FORK);
770 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
771 ASSERT(ifp->if_bytes == 0);
772 ASSERT(ifp->if_nextents == 0);
774 xfs_bmap_forkoff_reset(ip, whichfork);
777 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
778 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
783 xfs_bmap_local_to_extents(
784 xfs_trans_t *tp, /* transaction pointer */
785 xfs_inode_t *ip, /* incore inode pointer */
786 xfs_extlen_t total, /* total blocks needed by transaction */
787 int *logflagsp, /* inode logging flags */
789 void (*init_fn)(struct xfs_trans *tp,
791 struct xfs_inode *ip,
792 struct xfs_ifork *ifp, void *priv),
796 int flags; /* logging flags returned */
797 struct xfs_ifork *ifp; /* inode fork pointer */
798 xfs_alloc_arg_t args; /* allocation arguments */
799 struct xfs_buf *bp; /* buffer for extent block */
800 struct xfs_bmbt_irec rec;
801 struct xfs_iext_cursor icur;
804 * We don't want to deal with the case of keeping inode data inline yet.
805 * So sending the data fork of a regular inode is invalid.
807 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
808 ifp = xfs_ifork_ptr(ip, whichfork);
809 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
811 if (!ifp->if_bytes) {
812 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
813 flags = XFS_ILOG_CORE;
819 memset(&args, 0, sizeof(args));
821 args.mp = ip->i_mount;
823 args.minlen = args.maxlen = args.prod = 1;
824 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
827 * Allocate a block. We know we need only one, since the
828 * file currently fits in an inode.
831 args.minlen = args.maxlen = args.prod = 1;
832 error = xfs_alloc_vextent_start_ag(&args,
833 XFS_INO_TO_FSB(args.mp, ip->i_ino));
837 /* Can't fail, the space was reserved. */
838 ASSERT(args.fsbno != NULLFSBLOCK);
839 ASSERT(args.len == 1);
840 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
841 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
842 args.mp->m_bsize, 0, &bp);
847 * Initialize the block, copy the data and log the remote buffer.
849 * The callout is responsible for logging because the remote format
850 * might differ from the local format and thus we don't know how much to
851 * log here. Note that init_fn must also set the buffer log item type
854 init_fn(tp, bp, ip, ifp, priv);
856 /* account for the change in fork size */
857 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
858 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
859 flags |= XFS_ILOG_CORE;
865 rec.br_startblock = args.fsbno;
866 rec.br_blockcount = 1;
867 rec.br_state = XFS_EXT_NORM;
868 xfs_iext_first(ifp, &icur);
869 xfs_iext_insert(ip, &icur, &rec, 0);
871 ifp->if_nextents = 1;
873 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
874 flags |= xfs_ilog_fext(whichfork);
882 * Called from xfs_bmap_add_attrfork to handle btree format files.
884 STATIC int /* error */
885 xfs_bmap_add_attrfork_btree(
886 xfs_trans_t *tp, /* transaction pointer */
887 xfs_inode_t *ip, /* incore inode pointer */
888 int *flags) /* inode logging flags */
890 struct xfs_btree_block *block = ip->i_df.if_broot;
891 struct xfs_btree_cur *cur; /* btree cursor */
892 int error; /* error return value */
893 xfs_mount_t *mp; /* file system mount struct */
894 int stat; /* newroot status */
898 if (XFS_BMAP_BMDR_SPACE(block) <= xfs_inode_data_fork_size(ip))
899 *flags |= XFS_ILOG_DBROOT;
901 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
902 error = xfs_bmbt_lookup_first(cur, &stat);
905 /* must be at least one entry */
906 if (XFS_IS_CORRUPT(mp, stat != 1)) {
907 xfs_btree_mark_sick(cur);
908 error = -EFSCORRUPTED;
911 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
914 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
917 cur->bc_bmap.allocated = 0;
918 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
922 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
927 * Called from xfs_bmap_add_attrfork to handle extents format files.
929 STATIC int /* error */
930 xfs_bmap_add_attrfork_extents(
931 struct xfs_trans *tp, /* transaction pointer */
932 struct xfs_inode *ip, /* incore inode pointer */
933 int *flags) /* inode logging flags */
935 struct xfs_btree_cur *cur; /* bmap btree cursor */
936 int error; /* error return value */
938 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
939 xfs_inode_data_fork_size(ip))
942 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
945 cur->bc_bmap.allocated = 0;
946 xfs_btree_del_cursor(cur, error);
952 * Called from xfs_bmap_add_attrfork to handle local format files. Each
953 * different data fork content type needs a different callout to do the
954 * conversion. Some are basic and only require special block initialisation
955 * callouts for the data formating, others (directories) are so specialised they
956 * handle everything themselves.
958 * XXX (dgc): investigate whether directory conversion can use the generic
959 * formatting callout. It should be possible - it's just a very complex
962 STATIC int /* error */
963 xfs_bmap_add_attrfork_local(
964 struct xfs_trans *tp, /* transaction pointer */
965 struct xfs_inode *ip, /* incore inode pointer */
966 int *flags) /* inode logging flags */
968 struct xfs_da_args dargs; /* args for dir/attr code */
970 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
973 if (S_ISDIR(VFS_I(ip)->i_mode)) {
974 memset(&dargs, 0, sizeof(dargs));
975 dargs.geo = ip->i_mount->m_dir_geo;
977 dargs.total = dargs.geo->fsbcount;
978 dargs.whichfork = XFS_DATA_FORK;
980 dargs.owner = ip->i_ino;
981 return xfs_dir2_sf_to_block(&dargs);
984 if (S_ISLNK(VFS_I(ip)->i_mode))
985 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
986 XFS_DATA_FORK, xfs_symlink_local_to_remote,
989 /* should only be called for types that support local format data */
991 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
992 return -EFSCORRUPTED;
996 * Set an inode attr fork offset based on the format of the data fork.
999 xfs_bmap_set_attrforkoff(
1000 struct xfs_inode *ip,
1004 int default_size = xfs_default_attroffset(ip) >> 3;
1006 switch (ip->i_df.if_format) {
1007 case XFS_DINODE_FMT_DEV:
1008 ip->i_forkoff = default_size;
1010 case XFS_DINODE_FMT_LOCAL:
1011 case XFS_DINODE_FMT_EXTENTS:
1012 case XFS_DINODE_FMT_BTREE:
1013 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1015 ip->i_forkoff = default_size;
1016 else if (xfs_has_attr2(ip->i_mount) && version)
1028 * Convert inode from non-attributed to attributed. Caller must hold the
1029 * ILOCK_EXCL and the file cannot have an attr fork.
1031 int /* error code */
1032 xfs_bmap_add_attrfork(
1033 struct xfs_trans *tp,
1034 struct xfs_inode *ip, /* incore inode pointer */
1035 int size, /* space new attribute needs */
1036 int rsvd) /* xact may use reserved blks */
1038 struct xfs_mount *mp = tp->t_mountp;
1039 int version = 1; /* superblock attr version */
1040 int logflags; /* logging flags */
1041 int error; /* error return value */
1043 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1044 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1045 ASSERT(!xfs_inode_has_attr_fork(ip));
1047 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1048 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1052 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
1054 switch (ip->i_df.if_format) {
1055 case XFS_DINODE_FMT_LOCAL:
1056 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1058 case XFS_DINODE_FMT_EXTENTS:
1059 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1061 case XFS_DINODE_FMT_BTREE:
1062 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1069 xfs_trans_log_inode(tp, ip, logflags);
1072 if (!xfs_has_attr(mp) ||
1073 (!xfs_has_attr2(mp) && version == 2)) {
1074 bool log_sb = false;
1076 spin_lock(&mp->m_sb_lock);
1077 if (!xfs_has_attr(mp)) {
1081 if (!xfs_has_attr2(mp) && version == 2) {
1085 spin_unlock(&mp->m_sb_lock);
1094 * Internal and external extent tree search functions.
1097 struct xfs_iread_state {
1098 struct xfs_iext_cursor icur;
1099 xfs_extnum_t loaded;
1103 xfs_bmap_complain_bad_rec(
1104 struct xfs_inode *ip,
1107 const struct xfs_bmbt_irec *irec)
1109 struct xfs_mount *mp = ip->i_mount;
1110 const char *forkname;
1112 switch (whichfork) {
1113 case XFS_DATA_FORK: forkname = "data"; break;
1114 case XFS_ATTR_FORK: forkname = "attr"; break;
1115 case XFS_COW_FORK: forkname = "CoW"; break;
1116 default: forkname = "???"; break;
1120 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1121 ip->i_ino, forkname, fa);
1123 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1124 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1127 return -EFSCORRUPTED;
1130 /* Stuff every bmbt record from this block into the incore extent map. */
1132 xfs_iread_bmbt_block(
1133 struct xfs_btree_cur *cur,
1137 struct xfs_iread_state *ir = priv;
1138 struct xfs_mount *mp = cur->bc_mp;
1139 struct xfs_inode *ip = cur->bc_ino.ip;
1140 struct xfs_btree_block *block;
1142 struct xfs_bmbt_rec *frp;
1143 xfs_extnum_t num_recs;
1145 int whichfork = cur->bc_ino.whichfork;
1146 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1148 block = xfs_btree_get_block(cur, level, &bp);
1150 /* Abort if we find more records than nextents. */
1151 num_recs = xfs_btree_get_numrecs(block);
1152 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1153 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1154 (unsigned long long)ip->i_ino);
1155 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1156 sizeof(*block), __this_address);
1157 xfs_bmap_mark_sick(ip, whichfork);
1158 return -EFSCORRUPTED;
1161 /* Copy records into the incore cache. */
1162 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1163 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1164 struct xfs_bmbt_irec new;
1167 xfs_bmbt_disk_get_all(frp, &new);
1168 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1170 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1171 "xfs_iread_extents(2)", frp,
1173 xfs_bmap_mark_sick(ip, whichfork);
1174 return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1177 xfs_iext_insert(ip, &ir->icur, &new,
1178 xfs_bmap_fork_to_state(whichfork));
1179 trace_xfs_read_extent(ip, &ir->icur,
1180 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1181 xfs_iext_next(ifp, &ir->icur);
1188 * Read in extents from a btree-format inode.
1192 struct xfs_trans *tp,
1193 struct xfs_inode *ip,
1196 struct xfs_iread_state ir;
1197 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1198 struct xfs_mount *mp = ip->i_mount;
1199 struct xfs_btree_cur *cur;
1202 if (!xfs_need_iread_extents(ifp))
1205 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1208 xfs_iext_first(ifp, &ir.icur);
1209 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1210 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1211 XFS_BTREE_VISIT_RECORDS, &ir);
1212 xfs_btree_del_cursor(cur, error);
1216 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1217 xfs_bmap_mark_sick(ip, whichfork);
1218 error = -EFSCORRUPTED;
1221 ASSERT(ir.loaded == xfs_iext_count(ifp));
1223 * Use release semantics so that we can use acquire semantics in
1224 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1227 smp_store_release(&ifp->if_needextents, 0);
1230 if (xfs_metadata_is_sick(error))
1231 xfs_bmap_mark_sick(ip, whichfork);
1232 xfs_iext_destroy(ifp);
1237 * Returns the relative block number of the first unused block(s) in the given
1238 * fork with at least "len" logically contiguous blocks free. This is the
1239 * lowest-address hole if the fork has holes, else the first block past the end
1240 * of fork. Return 0 if the fork is currently local (in-inode).
1243 xfs_bmap_first_unused(
1244 struct xfs_trans *tp, /* transaction pointer */
1245 struct xfs_inode *ip, /* incore inode */
1246 xfs_extlen_t len, /* size of hole to find */
1247 xfs_fileoff_t *first_unused, /* unused block */
1248 int whichfork) /* data or attr fork */
1250 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1251 struct xfs_bmbt_irec got;
1252 struct xfs_iext_cursor icur;
1253 xfs_fileoff_t lastaddr = 0;
1254 xfs_fileoff_t lowest, max;
1257 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1262 ASSERT(xfs_ifork_has_extents(ifp));
1264 error = xfs_iread_extents(tp, ip, whichfork);
1268 lowest = max = *first_unused;
1269 for_each_xfs_iext(ifp, &icur, &got) {
1271 * See if the hole before this extent will work.
1273 if (got.br_startoff >= lowest + len &&
1274 got.br_startoff - max >= len)
1276 lastaddr = got.br_startoff + got.br_blockcount;
1277 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1280 *first_unused = max;
1285 * Returns the file-relative block number of the last block - 1 before
1286 * last_block (input value) in the file.
1287 * This is not based on i_size, it is based on the extent records.
1288 * Returns 0 for local files, as they do not have extent records.
1291 xfs_bmap_last_before(
1292 struct xfs_trans *tp, /* transaction pointer */
1293 struct xfs_inode *ip, /* incore inode */
1294 xfs_fileoff_t *last_block, /* last block */
1295 int whichfork) /* data or attr fork */
1297 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1298 struct xfs_bmbt_irec got;
1299 struct xfs_iext_cursor icur;
1302 switch (ifp->if_format) {
1303 case XFS_DINODE_FMT_LOCAL:
1306 case XFS_DINODE_FMT_BTREE:
1307 case XFS_DINODE_FMT_EXTENTS:
1311 xfs_bmap_mark_sick(ip, whichfork);
1312 return -EFSCORRUPTED;
1315 error = xfs_iread_extents(tp, ip, whichfork);
1319 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1325 xfs_bmap_last_extent(
1326 struct xfs_trans *tp,
1327 struct xfs_inode *ip,
1329 struct xfs_bmbt_irec *rec,
1332 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1333 struct xfs_iext_cursor icur;
1336 error = xfs_iread_extents(tp, ip, whichfork);
1340 xfs_iext_last(ifp, &icur);
1341 if (!xfs_iext_get_extent(ifp, &icur, rec))
1349 * Check the last inode extent to determine whether this allocation will result
1350 * in blocks being allocated at the end of the file. When we allocate new data
1351 * blocks at the end of the file which do not start at the previous data block,
1352 * we will try to align the new blocks at stripe unit boundaries.
1354 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1355 * at, or past the EOF.
1359 struct xfs_bmalloca *bma,
1362 struct xfs_bmbt_irec rec;
1367 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1378 * Check if we are allocation or past the last extent, or at least into
1379 * the last delayed allocated extent.
1381 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1382 (bma->offset >= rec.br_startoff &&
1383 isnullstartblock(rec.br_startblock));
1388 * Returns the file-relative block number of the first block past eof in
1389 * the file. This is not based on i_size, it is based on the extent records.
1390 * Returns 0 for local files, as they do not have extent records.
1393 xfs_bmap_last_offset(
1394 struct xfs_inode *ip,
1395 xfs_fileoff_t *last_block,
1398 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1399 struct xfs_bmbt_irec rec;
1405 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1408 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
1409 xfs_bmap_mark_sick(ip, whichfork);
1410 return -EFSCORRUPTED;
1413 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1414 if (error || is_empty)
1417 *last_block = rec.br_startoff + rec.br_blockcount;
1422 * Extent tree manipulation functions used during allocation.
1426 * Convert a delayed allocation to a real allocation.
1428 STATIC int /* error */
1429 xfs_bmap_add_extent_delay_real(
1430 struct xfs_bmalloca *bma,
1433 struct xfs_mount *mp = bma->ip->i_mount;
1434 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
1435 struct xfs_bmbt_irec *new = &bma->got;
1436 int error; /* error return value */
1437 int i; /* temp state */
1438 xfs_fileoff_t new_endoff; /* end offset of new entry */
1439 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1440 /* left is 0, right is 1, prev is 2 */
1441 int rval=0; /* return value (logging flags) */
1442 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1443 xfs_filblks_t da_new; /* new count del alloc blocks used */
1444 xfs_filblks_t da_old; /* old count del alloc blocks used */
1445 xfs_filblks_t temp=0; /* value for da_new calculations */
1446 int tmp_rval; /* partial logging flags */
1447 struct xfs_bmbt_irec old;
1449 ASSERT(whichfork != XFS_ATTR_FORK);
1450 ASSERT(!isnullstartblock(new->br_startblock));
1451 ASSERT(!bma->cur || (bma->cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
1453 XFS_STATS_INC(mp, xs_add_exlist);
1460 * Set up a bunch of variables to make the tests simpler.
1462 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1463 new_endoff = new->br_startoff + new->br_blockcount;
1464 ASSERT(isnullstartblock(PREV.br_startblock));
1465 ASSERT(PREV.br_startoff <= new->br_startoff);
1466 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1468 da_old = startblockval(PREV.br_startblock);
1472 * Set flags determining what part of the previous delayed allocation
1473 * extent is being replaced by a real allocation.
1475 if (PREV.br_startoff == new->br_startoff)
1476 state |= BMAP_LEFT_FILLING;
1477 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1478 state |= BMAP_RIGHT_FILLING;
1481 * Check and set flags if this segment has a left neighbor.
1482 * Don't set contiguous if the combined extent would be too large.
1484 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1485 state |= BMAP_LEFT_VALID;
1486 if (isnullstartblock(LEFT.br_startblock))
1487 state |= BMAP_LEFT_DELAY;
1490 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1491 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1492 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1493 LEFT.br_state == new->br_state &&
1494 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1495 state |= BMAP_LEFT_CONTIG;
1498 * Check and set flags if this segment has a right neighbor.
1499 * Don't set contiguous if the combined extent would be too large.
1500 * Also check for all-three-contiguous being too large.
1502 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1503 state |= BMAP_RIGHT_VALID;
1504 if (isnullstartblock(RIGHT.br_startblock))
1505 state |= BMAP_RIGHT_DELAY;
1508 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1509 new_endoff == RIGHT.br_startoff &&
1510 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1511 new->br_state == RIGHT.br_state &&
1512 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1513 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1514 BMAP_RIGHT_FILLING)) !=
1515 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1516 BMAP_RIGHT_FILLING) ||
1517 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1518 <= XFS_MAX_BMBT_EXTLEN))
1519 state |= BMAP_RIGHT_CONTIG;
1523 * Switch out based on the FILLING and CONTIG state bits.
1525 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1526 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1527 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1528 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1530 * Filling in all of a previously delayed allocation extent.
1531 * The left and right neighbors are both contiguous with new.
1533 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1535 xfs_iext_remove(bma->ip, &bma->icur, state);
1536 xfs_iext_remove(bma->ip, &bma->icur, state);
1537 xfs_iext_prev(ifp, &bma->icur);
1538 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1541 if (bma->cur == NULL)
1542 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1544 rval = XFS_ILOG_CORE;
1545 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1548 if (XFS_IS_CORRUPT(mp, i != 1)) {
1549 xfs_btree_mark_sick(bma->cur);
1550 error = -EFSCORRUPTED;
1553 error = xfs_btree_delete(bma->cur, &i);
1556 if (XFS_IS_CORRUPT(mp, i != 1)) {
1557 xfs_btree_mark_sick(bma->cur);
1558 error = -EFSCORRUPTED;
1561 error = xfs_btree_decrement(bma->cur, 0, &i);
1564 if (XFS_IS_CORRUPT(mp, i != 1)) {
1565 xfs_btree_mark_sick(bma->cur);
1566 error = -EFSCORRUPTED;
1569 error = xfs_bmbt_update(bma->cur, &LEFT);
1573 ASSERT(da_new <= da_old);
1576 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1578 * Filling in all of a previously delayed allocation extent.
1579 * The left neighbor is contiguous, the right is not.
1582 LEFT.br_blockcount += PREV.br_blockcount;
1584 xfs_iext_remove(bma->ip, &bma->icur, state);
1585 xfs_iext_prev(ifp, &bma->icur);
1586 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1588 if (bma->cur == NULL)
1589 rval = XFS_ILOG_DEXT;
1592 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1595 if (XFS_IS_CORRUPT(mp, i != 1)) {
1596 xfs_btree_mark_sick(bma->cur);
1597 error = -EFSCORRUPTED;
1600 error = xfs_bmbt_update(bma->cur, &LEFT);
1604 ASSERT(da_new <= da_old);
1607 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1609 * Filling in all of a previously delayed allocation extent.
1610 * The right neighbor is contiguous, the left is not. Take care
1611 * with delay -> unwritten extent allocation here because the
1612 * delalloc record we are overwriting is always written.
1614 PREV.br_startblock = new->br_startblock;
1615 PREV.br_blockcount += RIGHT.br_blockcount;
1616 PREV.br_state = new->br_state;
1618 xfs_iext_next(ifp, &bma->icur);
1619 xfs_iext_remove(bma->ip, &bma->icur, state);
1620 xfs_iext_prev(ifp, &bma->icur);
1621 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1623 if (bma->cur == NULL)
1624 rval = XFS_ILOG_DEXT;
1627 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1630 if (XFS_IS_CORRUPT(mp, i != 1)) {
1631 xfs_btree_mark_sick(bma->cur);
1632 error = -EFSCORRUPTED;
1635 error = xfs_bmbt_update(bma->cur, &PREV);
1639 ASSERT(da_new <= da_old);
1642 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1644 * Filling in all of a previously delayed allocation extent.
1645 * Neither the left nor right neighbors are contiguous with
1648 PREV.br_startblock = new->br_startblock;
1649 PREV.br_state = new->br_state;
1650 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1653 if (bma->cur == NULL)
1654 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1656 rval = XFS_ILOG_CORE;
1657 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1660 if (XFS_IS_CORRUPT(mp, i != 0)) {
1661 xfs_btree_mark_sick(bma->cur);
1662 error = -EFSCORRUPTED;
1665 error = xfs_btree_insert(bma->cur, &i);
1668 if (XFS_IS_CORRUPT(mp, i != 1)) {
1669 xfs_btree_mark_sick(bma->cur);
1670 error = -EFSCORRUPTED;
1674 ASSERT(da_new <= da_old);
1677 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1679 * Filling in the first part of a previous delayed allocation.
1680 * The left neighbor is contiguous.
1683 temp = PREV.br_blockcount - new->br_blockcount;
1684 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1685 startblockval(PREV.br_startblock));
1687 LEFT.br_blockcount += new->br_blockcount;
1689 PREV.br_blockcount = temp;
1690 PREV.br_startoff += new->br_blockcount;
1691 PREV.br_startblock = nullstartblock(da_new);
1693 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1694 xfs_iext_prev(ifp, &bma->icur);
1695 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1697 if (bma->cur == NULL)
1698 rval = XFS_ILOG_DEXT;
1701 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1704 if (XFS_IS_CORRUPT(mp, i != 1)) {
1705 xfs_btree_mark_sick(bma->cur);
1706 error = -EFSCORRUPTED;
1709 error = xfs_bmbt_update(bma->cur, &LEFT);
1713 ASSERT(da_new <= da_old);
1716 case BMAP_LEFT_FILLING:
1718 * Filling in the first part of a previous delayed allocation.
1719 * The left neighbor is not contiguous.
1721 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1724 if (bma->cur == NULL)
1725 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1727 rval = XFS_ILOG_CORE;
1728 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1731 if (XFS_IS_CORRUPT(mp, i != 0)) {
1732 xfs_btree_mark_sick(bma->cur);
1733 error = -EFSCORRUPTED;
1736 error = xfs_btree_insert(bma->cur, &i);
1739 if (XFS_IS_CORRUPT(mp, i != 1)) {
1740 xfs_btree_mark_sick(bma->cur);
1741 error = -EFSCORRUPTED;
1746 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1747 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1748 &bma->cur, 1, &tmp_rval, whichfork);
1754 temp = PREV.br_blockcount - new->br_blockcount;
1755 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1756 startblockval(PREV.br_startblock) -
1757 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
1759 PREV.br_startoff = new_endoff;
1760 PREV.br_blockcount = temp;
1761 PREV.br_startblock = nullstartblock(da_new);
1762 xfs_iext_next(ifp, &bma->icur);
1763 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1764 xfs_iext_prev(ifp, &bma->icur);
1767 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1769 * Filling in the last part of a previous delayed allocation.
1770 * The right neighbor is contiguous with the new allocation.
1773 RIGHT.br_startoff = new->br_startoff;
1774 RIGHT.br_startblock = new->br_startblock;
1775 RIGHT.br_blockcount += new->br_blockcount;
1777 if (bma->cur == NULL)
1778 rval = XFS_ILOG_DEXT;
1781 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1784 if (XFS_IS_CORRUPT(mp, i != 1)) {
1785 xfs_btree_mark_sick(bma->cur);
1786 error = -EFSCORRUPTED;
1789 error = xfs_bmbt_update(bma->cur, &RIGHT);
1794 temp = PREV.br_blockcount - new->br_blockcount;
1795 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1796 startblockval(PREV.br_startblock));
1798 PREV.br_blockcount = temp;
1799 PREV.br_startblock = nullstartblock(da_new);
1801 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1802 xfs_iext_next(ifp, &bma->icur);
1803 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1804 ASSERT(da_new <= da_old);
1807 case BMAP_RIGHT_FILLING:
1809 * Filling in the last part of a previous delayed allocation.
1810 * The right neighbor is not contiguous.
1812 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1815 if (bma->cur == NULL)
1816 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1818 rval = XFS_ILOG_CORE;
1819 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1822 if (XFS_IS_CORRUPT(mp, i != 0)) {
1823 xfs_btree_mark_sick(bma->cur);
1824 error = -EFSCORRUPTED;
1827 error = xfs_btree_insert(bma->cur, &i);
1830 if (XFS_IS_CORRUPT(mp, i != 1)) {
1831 xfs_btree_mark_sick(bma->cur);
1832 error = -EFSCORRUPTED;
1837 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1838 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1839 &bma->cur, 1, &tmp_rval, whichfork);
1845 temp = PREV.br_blockcount - new->br_blockcount;
1846 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1847 startblockval(PREV.br_startblock) -
1848 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
1850 PREV.br_startblock = nullstartblock(da_new);
1851 PREV.br_blockcount = temp;
1852 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1853 xfs_iext_next(ifp, &bma->icur);
1854 ASSERT(da_new <= da_old);
1859 * Filling in the middle part of a previous delayed allocation.
1860 * Contiguity is impossible here.
1861 * This case is avoided almost all the time.
1863 * We start with a delayed allocation:
1865 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1868 * and we are allocating:
1869 * +rrrrrrrrrrrrrrrrr+
1872 * and we set it up for insertion as:
1873 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1875 * PREV @ idx LEFT RIGHT
1876 * inserted at idx + 1
1880 /* LEFT is the new middle */
1883 /* RIGHT is the new right */
1884 RIGHT.br_state = PREV.br_state;
1885 RIGHT.br_startoff = new_endoff;
1886 RIGHT.br_blockcount =
1887 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1888 RIGHT.br_startblock =
1889 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1890 RIGHT.br_blockcount));
1893 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1894 PREV.br_startblock =
1895 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1896 PREV.br_blockcount));
1897 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1899 xfs_iext_next(ifp, &bma->icur);
1900 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1901 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1904 if (bma->cur == NULL)
1905 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1907 rval = XFS_ILOG_CORE;
1908 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1911 if (XFS_IS_CORRUPT(mp, i != 0)) {
1912 xfs_btree_mark_sick(bma->cur);
1913 error = -EFSCORRUPTED;
1916 error = xfs_btree_insert(bma->cur, &i);
1919 if (XFS_IS_CORRUPT(mp, i != 1)) {
1920 xfs_btree_mark_sick(bma->cur);
1921 error = -EFSCORRUPTED;
1926 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1927 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1928 &bma->cur, 1, &tmp_rval, whichfork);
1934 da_new = startblockval(PREV.br_startblock) +
1935 startblockval(RIGHT.br_startblock);
1938 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1939 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1940 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1941 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1942 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1943 case BMAP_LEFT_CONTIG:
1944 case BMAP_RIGHT_CONTIG:
1946 * These cases are all impossible.
1951 /* add reverse mapping unless caller opted out */
1952 if (!(bma->flags & XFS_BMAPI_NORMAP))
1953 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1955 /* convert to a btree if necessary */
1956 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1957 int tmp_logflags; /* partial log flag return val */
1959 ASSERT(bma->cur == NULL);
1960 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1961 &bma->cur, da_old > 0, &tmp_logflags,
1963 bma->logflags |= tmp_logflags;
1968 if (da_new != da_old)
1969 xfs_mod_delalloc(bma->ip, 0, (int64_t)da_new - da_old);
1972 da_new += bma->cur->bc_bmap.allocated;
1973 bma->cur->bc_bmap.allocated = 0;
1976 /* adjust for changes in reserved delayed indirect blocks */
1977 if (da_new < da_old)
1978 xfs_add_fdblocks(mp, da_old - da_new);
1979 else if (da_new > da_old)
1980 error = xfs_dec_fdblocks(mp, da_new - da_old, true);
1982 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1984 if (whichfork != XFS_COW_FORK)
1985 bma->logflags |= rval;
1993 * Convert an unwritten allocation to a real allocation or vice versa.
1996 xfs_bmap_add_extent_unwritten_real(
1997 struct xfs_trans *tp,
1998 xfs_inode_t *ip, /* incore inode pointer */
2000 struct xfs_iext_cursor *icur,
2001 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
2002 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2003 int *logflagsp) /* inode logging flags */
2005 struct xfs_btree_cur *cur; /* btree cursor */
2006 int error; /* error return value */
2007 int i; /* temp state */
2008 struct xfs_ifork *ifp; /* inode fork pointer */
2009 xfs_fileoff_t new_endoff; /* end offset of new entry */
2010 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2011 /* left is 0, right is 1, prev is 2 */
2012 int rval=0; /* return value (logging flags) */
2013 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2014 struct xfs_mount *mp = ip->i_mount;
2015 struct xfs_bmbt_irec old;
2020 ifp = xfs_ifork_ptr(ip, whichfork);
2022 ASSERT(!isnullstartblock(new->br_startblock));
2024 XFS_STATS_INC(mp, xs_add_exlist);
2031 * Set up a bunch of variables to make the tests simpler.
2034 xfs_iext_get_extent(ifp, icur, &PREV);
2035 ASSERT(new->br_state != PREV.br_state);
2036 new_endoff = new->br_startoff + new->br_blockcount;
2037 ASSERT(PREV.br_startoff <= new->br_startoff);
2038 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2041 * Set flags determining what part of the previous oldext allocation
2042 * extent is being replaced by a newext allocation.
2044 if (PREV.br_startoff == new->br_startoff)
2045 state |= BMAP_LEFT_FILLING;
2046 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2047 state |= BMAP_RIGHT_FILLING;
2050 * Check and set flags if this segment has a left neighbor.
2051 * Don't set contiguous if the combined extent would be too large.
2053 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2054 state |= BMAP_LEFT_VALID;
2055 if (isnullstartblock(LEFT.br_startblock))
2056 state |= BMAP_LEFT_DELAY;
2059 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2060 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2061 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2062 LEFT.br_state == new->br_state &&
2063 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2064 state |= BMAP_LEFT_CONTIG;
2067 * Check and set flags if this segment has a right neighbor.
2068 * Don't set contiguous if the combined extent would be too large.
2069 * Also check for all-three-contiguous being too large.
2071 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2072 state |= BMAP_RIGHT_VALID;
2073 if (isnullstartblock(RIGHT.br_startblock))
2074 state |= BMAP_RIGHT_DELAY;
2077 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2078 new_endoff == RIGHT.br_startoff &&
2079 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2080 new->br_state == RIGHT.br_state &&
2081 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2082 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2083 BMAP_RIGHT_FILLING)) !=
2084 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2085 BMAP_RIGHT_FILLING) ||
2086 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2087 <= XFS_MAX_BMBT_EXTLEN))
2088 state |= BMAP_RIGHT_CONTIG;
2091 * Switch out based on the FILLING and CONTIG state bits.
2093 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2094 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2095 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2096 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2098 * Setting all of a previous oldext extent to newext.
2099 * The left and right neighbors are both contiguous with new.
2101 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2103 xfs_iext_remove(ip, icur, state);
2104 xfs_iext_remove(ip, icur, state);
2105 xfs_iext_prev(ifp, icur);
2106 xfs_iext_update_extent(ip, state, icur, &LEFT);
2107 ifp->if_nextents -= 2;
2109 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2111 rval = XFS_ILOG_CORE;
2112 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2115 if (XFS_IS_CORRUPT(mp, i != 1)) {
2116 xfs_btree_mark_sick(cur);
2117 error = -EFSCORRUPTED;
2120 if ((error = xfs_btree_delete(cur, &i)))
2122 if (XFS_IS_CORRUPT(mp, i != 1)) {
2123 xfs_btree_mark_sick(cur);
2124 error = -EFSCORRUPTED;
2127 if ((error = xfs_btree_decrement(cur, 0, &i)))
2129 if (XFS_IS_CORRUPT(mp, i != 1)) {
2130 xfs_btree_mark_sick(cur);
2131 error = -EFSCORRUPTED;
2134 if ((error = xfs_btree_delete(cur, &i)))
2136 if (XFS_IS_CORRUPT(mp, i != 1)) {
2137 xfs_btree_mark_sick(cur);
2138 error = -EFSCORRUPTED;
2141 if ((error = xfs_btree_decrement(cur, 0, &i)))
2143 if (XFS_IS_CORRUPT(mp, i != 1)) {
2144 xfs_btree_mark_sick(cur);
2145 error = -EFSCORRUPTED;
2148 error = xfs_bmbt_update(cur, &LEFT);
2154 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2156 * Setting all of a previous oldext extent to newext.
2157 * The left neighbor is contiguous, the right is not.
2159 LEFT.br_blockcount += PREV.br_blockcount;
2161 xfs_iext_remove(ip, icur, state);
2162 xfs_iext_prev(ifp, icur);
2163 xfs_iext_update_extent(ip, state, icur, &LEFT);
2166 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2168 rval = XFS_ILOG_CORE;
2169 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2172 if (XFS_IS_CORRUPT(mp, i != 1)) {
2173 xfs_btree_mark_sick(cur);
2174 error = -EFSCORRUPTED;
2177 if ((error = xfs_btree_delete(cur, &i)))
2179 if (XFS_IS_CORRUPT(mp, i != 1)) {
2180 xfs_btree_mark_sick(cur);
2181 error = -EFSCORRUPTED;
2184 if ((error = xfs_btree_decrement(cur, 0, &i)))
2186 if (XFS_IS_CORRUPT(mp, i != 1)) {
2187 xfs_btree_mark_sick(cur);
2188 error = -EFSCORRUPTED;
2191 error = xfs_bmbt_update(cur, &LEFT);
2197 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2199 * Setting all of a previous oldext extent to newext.
2200 * The right neighbor is contiguous, the left is not.
2202 PREV.br_blockcount += RIGHT.br_blockcount;
2203 PREV.br_state = new->br_state;
2205 xfs_iext_next(ifp, icur);
2206 xfs_iext_remove(ip, icur, state);
2207 xfs_iext_prev(ifp, icur);
2208 xfs_iext_update_extent(ip, state, icur, &PREV);
2212 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2214 rval = XFS_ILOG_CORE;
2215 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2218 if (XFS_IS_CORRUPT(mp, i != 1)) {
2219 xfs_btree_mark_sick(cur);
2220 error = -EFSCORRUPTED;
2223 if ((error = xfs_btree_delete(cur, &i)))
2225 if (XFS_IS_CORRUPT(mp, i != 1)) {
2226 xfs_btree_mark_sick(cur);
2227 error = -EFSCORRUPTED;
2230 if ((error = xfs_btree_decrement(cur, 0, &i)))
2232 if (XFS_IS_CORRUPT(mp, i != 1)) {
2233 xfs_btree_mark_sick(cur);
2234 error = -EFSCORRUPTED;
2237 error = xfs_bmbt_update(cur, &PREV);
2243 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2245 * Setting all of a previous oldext extent to newext.
2246 * Neither the left nor right neighbors are contiguous with
2249 PREV.br_state = new->br_state;
2250 xfs_iext_update_extent(ip, state, icur, &PREV);
2253 rval = XFS_ILOG_DEXT;
2256 error = xfs_bmbt_lookup_eq(cur, new, &i);
2259 if (XFS_IS_CORRUPT(mp, i != 1)) {
2260 xfs_btree_mark_sick(cur);
2261 error = -EFSCORRUPTED;
2264 error = xfs_bmbt_update(cur, &PREV);
2270 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2272 * Setting the first part of a previous oldext extent to newext.
2273 * The left neighbor is contiguous.
2275 LEFT.br_blockcount += new->br_blockcount;
2278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
2282 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 xfs_iext_prev(ifp, icur);
2284 xfs_iext_update_extent(ip, state, icur, &LEFT);
2287 rval = XFS_ILOG_DEXT;
2290 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2293 if (XFS_IS_CORRUPT(mp, i != 1)) {
2294 xfs_btree_mark_sick(cur);
2295 error = -EFSCORRUPTED;
2298 error = xfs_bmbt_update(cur, &PREV);
2301 error = xfs_btree_decrement(cur, 0, &i);
2304 error = xfs_bmbt_update(cur, &LEFT);
2310 case BMAP_LEFT_FILLING:
2312 * Setting the first part of a previous oldext extent to newext.
2313 * The left neighbor is not contiguous.
2316 PREV.br_startoff += new->br_blockcount;
2317 PREV.br_startblock += new->br_blockcount;
2318 PREV.br_blockcount -= new->br_blockcount;
2320 xfs_iext_update_extent(ip, state, icur, &PREV);
2321 xfs_iext_insert(ip, icur, new, state);
2325 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2327 rval = XFS_ILOG_CORE;
2328 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2331 if (XFS_IS_CORRUPT(mp, i != 1)) {
2332 xfs_btree_mark_sick(cur);
2333 error = -EFSCORRUPTED;
2336 error = xfs_bmbt_update(cur, &PREV);
2339 cur->bc_rec.b = *new;
2340 if ((error = xfs_btree_insert(cur, &i)))
2342 if (XFS_IS_CORRUPT(mp, i != 1)) {
2343 xfs_btree_mark_sick(cur);
2344 error = -EFSCORRUPTED;
2350 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2352 * Setting the last part of a previous oldext extent to newext.
2353 * The right neighbor is contiguous with the new allocation.
2356 PREV.br_blockcount -= new->br_blockcount;
2358 RIGHT.br_startoff = new->br_startoff;
2359 RIGHT.br_startblock = new->br_startblock;
2360 RIGHT.br_blockcount += new->br_blockcount;
2362 xfs_iext_update_extent(ip, state, icur, &PREV);
2363 xfs_iext_next(ifp, icur);
2364 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2367 rval = XFS_ILOG_DEXT;
2370 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2373 if (XFS_IS_CORRUPT(mp, i != 1)) {
2374 xfs_btree_mark_sick(cur);
2375 error = -EFSCORRUPTED;
2378 error = xfs_bmbt_update(cur, &PREV);
2381 error = xfs_btree_increment(cur, 0, &i);
2384 error = xfs_bmbt_update(cur, &RIGHT);
2390 case BMAP_RIGHT_FILLING:
2392 * Setting the last part of a previous oldext extent to newext.
2393 * The right neighbor is not contiguous.
2396 PREV.br_blockcount -= new->br_blockcount;
2398 xfs_iext_update_extent(ip, state, icur, &PREV);
2399 xfs_iext_next(ifp, icur);
2400 xfs_iext_insert(ip, icur, new, state);
2404 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 rval = XFS_ILOG_CORE;
2407 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2410 if (XFS_IS_CORRUPT(mp, i != 1)) {
2411 xfs_btree_mark_sick(cur);
2412 error = -EFSCORRUPTED;
2415 error = xfs_bmbt_update(cur, &PREV);
2418 error = xfs_bmbt_lookup_eq(cur, new, &i);
2421 if (XFS_IS_CORRUPT(mp, i != 0)) {
2422 xfs_btree_mark_sick(cur);
2423 error = -EFSCORRUPTED;
2426 if ((error = xfs_btree_insert(cur, &i)))
2428 if (XFS_IS_CORRUPT(mp, i != 1)) {
2429 xfs_btree_mark_sick(cur);
2430 error = -EFSCORRUPTED;
2438 * Setting the middle part of a previous oldext extent to
2439 * newext. Contiguity is impossible here.
2440 * One extent becomes three extents.
2443 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2446 r[1].br_startoff = new_endoff;
2447 r[1].br_blockcount =
2448 old.br_startoff + old.br_blockcount - new_endoff;
2449 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2450 r[1].br_state = PREV.br_state;
2452 xfs_iext_update_extent(ip, state, icur, &PREV);
2453 xfs_iext_next(ifp, icur);
2454 xfs_iext_insert(ip, icur, &r[1], state);
2455 xfs_iext_insert(ip, icur, &r[0], state);
2456 ifp->if_nextents += 2;
2459 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2461 rval = XFS_ILOG_CORE;
2462 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2465 if (XFS_IS_CORRUPT(mp, i != 1)) {
2466 xfs_btree_mark_sick(cur);
2467 error = -EFSCORRUPTED;
2470 /* new right extent - oldext */
2471 error = xfs_bmbt_update(cur, &r[1]);
2474 /* new left extent - oldext */
2475 cur->bc_rec.b = PREV;
2476 if ((error = xfs_btree_insert(cur, &i)))
2478 if (XFS_IS_CORRUPT(mp, i != 1)) {
2479 xfs_btree_mark_sick(cur);
2480 error = -EFSCORRUPTED;
2484 * Reset the cursor to the position of the new extent
2485 * we are about to insert as we can't trust it after
2486 * the previous insert.
2488 error = xfs_bmbt_lookup_eq(cur, new, &i);
2491 if (XFS_IS_CORRUPT(mp, i != 0)) {
2492 xfs_btree_mark_sick(cur);
2493 error = -EFSCORRUPTED;
2496 /* new middle extent - newext */
2497 if ((error = xfs_btree_insert(cur, &i)))
2499 if (XFS_IS_CORRUPT(mp, i != 1)) {
2500 xfs_btree_mark_sick(cur);
2501 error = -EFSCORRUPTED;
2507 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2508 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2509 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2510 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2511 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2512 case BMAP_LEFT_CONTIG:
2513 case BMAP_RIGHT_CONTIG:
2515 * These cases are all impossible.
2520 /* update reverse mappings */
2521 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2523 /* convert to a btree if necessary */
2524 if (xfs_bmap_needs_btree(ip, whichfork)) {
2525 int tmp_logflags; /* partial log flag return val */
2527 ASSERT(cur == NULL);
2528 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2529 &tmp_logflags, whichfork);
2530 *logflagsp |= tmp_logflags;
2535 /* clear out the allocated field, done with it now in any case. */
2537 cur->bc_bmap.allocated = 0;
2541 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2551 * Convert a hole to a delayed allocation.
2554 xfs_bmap_add_extent_hole_delay(
2555 xfs_inode_t *ip, /* incore inode pointer */
2557 struct xfs_iext_cursor *icur,
2558 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2560 struct xfs_ifork *ifp; /* inode fork pointer */
2561 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2562 xfs_filblks_t newlen=0; /* new indirect size */
2563 xfs_filblks_t oldlen=0; /* old indirect size */
2564 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2565 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2566 xfs_filblks_t temp; /* temp for indirect calculations */
2568 ifp = xfs_ifork_ptr(ip, whichfork);
2569 ASSERT(isnullstartblock(new->br_startblock));
2572 * Check and set flags if this segment has a left neighbor
2574 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2575 state |= BMAP_LEFT_VALID;
2576 if (isnullstartblock(left.br_startblock))
2577 state |= BMAP_LEFT_DELAY;
2581 * Check and set flags if the current (right) segment exists.
2582 * If it doesn't exist, we're converting the hole at end-of-file.
2584 if (xfs_iext_get_extent(ifp, icur, &right)) {
2585 state |= BMAP_RIGHT_VALID;
2586 if (isnullstartblock(right.br_startblock))
2587 state |= BMAP_RIGHT_DELAY;
2591 * Set contiguity flags on the left and right neighbors.
2592 * Don't let extents get too large, even if the pieces are contiguous.
2594 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2595 left.br_startoff + left.br_blockcount == new->br_startoff &&
2596 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2597 state |= BMAP_LEFT_CONTIG;
2599 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2600 new->br_startoff + new->br_blockcount == right.br_startoff &&
2601 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2602 (!(state & BMAP_LEFT_CONTIG) ||
2603 (left.br_blockcount + new->br_blockcount +
2604 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2605 state |= BMAP_RIGHT_CONTIG;
2608 * Switch out based on the contiguity flags.
2610 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2611 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2613 * New allocation is contiguous with delayed allocations
2614 * on the left and on the right.
2615 * Merge all three into a single extent record.
2617 temp = left.br_blockcount + new->br_blockcount +
2618 right.br_blockcount;
2620 oldlen = startblockval(left.br_startblock) +
2621 startblockval(new->br_startblock) +
2622 startblockval(right.br_startblock);
2623 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2625 left.br_startblock = nullstartblock(newlen);
2626 left.br_blockcount = temp;
2628 xfs_iext_remove(ip, icur, state);
2629 xfs_iext_prev(ifp, icur);
2630 xfs_iext_update_extent(ip, state, icur, &left);
2633 case BMAP_LEFT_CONTIG:
2635 * New allocation is contiguous with a delayed allocation
2637 * Merge the new allocation with the left neighbor.
2639 temp = left.br_blockcount + new->br_blockcount;
2641 oldlen = startblockval(left.br_startblock) +
2642 startblockval(new->br_startblock);
2643 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2645 left.br_blockcount = temp;
2646 left.br_startblock = nullstartblock(newlen);
2648 xfs_iext_prev(ifp, icur);
2649 xfs_iext_update_extent(ip, state, icur, &left);
2652 case BMAP_RIGHT_CONTIG:
2654 * New allocation is contiguous with a delayed allocation
2656 * Merge the new allocation with the right neighbor.
2658 temp = new->br_blockcount + right.br_blockcount;
2659 oldlen = startblockval(new->br_startblock) +
2660 startblockval(right.br_startblock);
2661 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2663 right.br_startoff = new->br_startoff;
2664 right.br_startblock = nullstartblock(newlen);
2665 right.br_blockcount = temp;
2666 xfs_iext_update_extent(ip, state, icur, &right);
2671 * New allocation is not contiguous with another
2672 * delayed allocation.
2673 * Insert a new entry.
2675 oldlen = newlen = 0;
2676 xfs_iext_insert(ip, icur, new, state);
2679 if (oldlen != newlen) {
2680 ASSERT(oldlen > newlen);
2681 xfs_add_fdblocks(ip->i_mount, oldlen - newlen);
2684 * Nothing to do for disk quota accounting here.
2686 xfs_mod_delalloc(ip, 0, (int64_t)newlen - oldlen);
2691 * Convert a hole to a real allocation.
2693 STATIC int /* error */
2694 xfs_bmap_add_extent_hole_real(
2695 struct xfs_trans *tp,
2696 struct xfs_inode *ip,
2698 struct xfs_iext_cursor *icur,
2699 struct xfs_btree_cur **curp,
2700 struct xfs_bmbt_irec *new,
2704 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2705 struct xfs_mount *mp = ip->i_mount;
2706 struct xfs_btree_cur *cur = *curp;
2707 int error; /* error return value */
2708 int i; /* temp state */
2709 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2710 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2711 int rval=0; /* return value (logging flags) */
2712 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2713 struct xfs_bmbt_irec old;
2715 ASSERT(!isnullstartblock(new->br_startblock));
2716 ASSERT(!cur || !(cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
2718 XFS_STATS_INC(mp, xs_add_exlist);
2721 * Check and set flags if this segment has a left neighbor.
2723 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2724 state |= BMAP_LEFT_VALID;
2725 if (isnullstartblock(left.br_startblock))
2726 state |= BMAP_LEFT_DELAY;
2730 * Check and set flags if this segment has a current value.
2731 * Not true if we're inserting into the "hole" at eof.
2733 if (xfs_iext_get_extent(ifp, icur, &right)) {
2734 state |= BMAP_RIGHT_VALID;
2735 if (isnullstartblock(right.br_startblock))
2736 state |= BMAP_RIGHT_DELAY;
2740 * We're inserting a real allocation between "left" and "right".
2741 * Set the contiguity flags. Don't let extents get too large.
2743 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2744 left.br_startoff + left.br_blockcount == new->br_startoff &&
2745 left.br_startblock + left.br_blockcount == new->br_startblock &&
2746 left.br_state == new->br_state &&
2747 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2748 state |= BMAP_LEFT_CONTIG;
2750 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2751 new->br_startoff + new->br_blockcount == right.br_startoff &&
2752 new->br_startblock + new->br_blockcount == right.br_startblock &&
2753 new->br_state == right.br_state &&
2754 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2755 (!(state & BMAP_LEFT_CONTIG) ||
2756 left.br_blockcount + new->br_blockcount +
2757 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
2758 state |= BMAP_RIGHT_CONTIG;
2762 * Select which case we're in here, and implement it.
2764 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2765 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2767 * New allocation is contiguous with real allocations on the
2768 * left and on the right.
2769 * Merge all three into a single extent record.
2771 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2773 xfs_iext_remove(ip, icur, state);
2774 xfs_iext_prev(ifp, icur);
2775 xfs_iext_update_extent(ip, state, icur, &left);
2779 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2781 rval = XFS_ILOG_CORE;
2782 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2785 if (XFS_IS_CORRUPT(mp, i != 1)) {
2786 xfs_btree_mark_sick(cur);
2787 error = -EFSCORRUPTED;
2790 error = xfs_btree_delete(cur, &i);
2793 if (XFS_IS_CORRUPT(mp, i != 1)) {
2794 xfs_btree_mark_sick(cur);
2795 error = -EFSCORRUPTED;
2798 error = xfs_btree_decrement(cur, 0, &i);
2801 if (XFS_IS_CORRUPT(mp, i != 1)) {
2802 xfs_btree_mark_sick(cur);
2803 error = -EFSCORRUPTED;
2806 error = xfs_bmbt_update(cur, &left);
2812 case BMAP_LEFT_CONTIG:
2814 * New allocation is contiguous with a real allocation
2816 * Merge the new allocation with the left neighbor.
2819 left.br_blockcount += new->br_blockcount;
2821 xfs_iext_prev(ifp, icur);
2822 xfs_iext_update_extent(ip, state, icur, &left);
2825 rval = xfs_ilog_fext(whichfork);
2828 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2831 if (XFS_IS_CORRUPT(mp, i != 1)) {
2832 xfs_btree_mark_sick(cur);
2833 error = -EFSCORRUPTED;
2836 error = xfs_bmbt_update(cur, &left);
2842 case BMAP_RIGHT_CONTIG:
2844 * New allocation is contiguous with a real allocation
2846 * Merge the new allocation with the right neighbor.
2850 right.br_startoff = new->br_startoff;
2851 right.br_startblock = new->br_startblock;
2852 right.br_blockcount += new->br_blockcount;
2853 xfs_iext_update_extent(ip, state, icur, &right);
2856 rval = xfs_ilog_fext(whichfork);
2859 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2862 if (XFS_IS_CORRUPT(mp, i != 1)) {
2863 xfs_btree_mark_sick(cur);
2864 error = -EFSCORRUPTED;
2867 error = xfs_bmbt_update(cur, &right);
2875 * New allocation is not contiguous with another
2877 * Insert a new entry.
2879 xfs_iext_insert(ip, icur, new, state);
2883 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2885 rval = XFS_ILOG_CORE;
2886 error = xfs_bmbt_lookup_eq(cur, new, &i);
2889 if (XFS_IS_CORRUPT(mp, i != 0)) {
2890 xfs_btree_mark_sick(cur);
2891 error = -EFSCORRUPTED;
2894 error = xfs_btree_insert(cur, &i);
2897 if (XFS_IS_CORRUPT(mp, i != 1)) {
2898 xfs_btree_mark_sick(cur);
2899 error = -EFSCORRUPTED;
2906 /* add reverse mapping unless caller opted out */
2907 if (!(flags & XFS_BMAPI_NORMAP))
2908 xfs_rmap_map_extent(tp, ip, whichfork, new);
2910 /* convert to a btree if necessary */
2911 if (xfs_bmap_needs_btree(ip, whichfork)) {
2912 int tmp_logflags; /* partial log flag return val */
2914 ASSERT(cur == NULL);
2915 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2916 &tmp_logflags, whichfork);
2917 *logflagsp |= tmp_logflags;
2923 /* clear out the allocated field, done with it now in any case. */
2925 cur->bc_bmap.allocated = 0;
2927 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2934 * Functions used in the extent read, allocate and remove paths
2938 * Adjust the size of the new extent based on i_extsize and rt extsize.
2941 xfs_bmap_extsize_align(
2943 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2944 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2945 xfs_extlen_t extsz, /* align to this extent size */
2946 int rt, /* is this a realtime inode? */
2947 int eof, /* is extent at end-of-file? */
2948 int delay, /* creating delalloc extent? */
2949 int convert, /* overwriting unwritten extent? */
2950 xfs_fileoff_t *offp, /* in/out: aligned offset */
2951 xfs_extlen_t *lenp) /* in/out: aligned length */
2953 xfs_fileoff_t orig_off; /* original offset */
2954 xfs_extlen_t orig_alen; /* original length */
2955 xfs_fileoff_t orig_end; /* original off+len */
2956 xfs_fileoff_t nexto; /* next file offset */
2957 xfs_fileoff_t prevo; /* previous file offset */
2958 xfs_fileoff_t align_off; /* temp for offset */
2959 xfs_extlen_t align_alen; /* temp for length */
2960 xfs_extlen_t temp; /* temp for calculations */
2965 orig_off = align_off = *offp;
2966 orig_alen = align_alen = *lenp;
2967 orig_end = orig_off + orig_alen;
2970 * If this request overlaps an existing extent, then don't
2971 * attempt to perform any additional alignment.
2973 if (!delay && !eof &&
2974 (orig_off >= gotp->br_startoff) &&
2975 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2980 * If the file offset is unaligned vs. the extent size
2981 * we need to align it. This will be possible unless
2982 * the file was previously written with a kernel that didn't
2983 * perform this alignment, or if a truncate shot us in the
2986 div_u64_rem(orig_off, extsz, &temp);
2992 /* Same adjustment for the end of the requested area. */
2993 temp = (align_alen % extsz);
2995 align_alen += extsz - temp;
2998 * For large extent hint sizes, the aligned extent might be larger than
2999 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
3000 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
3001 * allocation loops handle short allocation just fine, so it is safe to
3002 * do this. We only want to do it when we are forced to, though, because
3003 * it means more allocation operations are required.
3005 while (align_alen > XFS_MAX_BMBT_EXTLEN)
3006 align_alen -= extsz;
3007 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
3010 * If the previous block overlaps with this proposed allocation
3011 * then move the start forward without adjusting the length.
3013 if (prevp->br_startoff != NULLFILEOFF) {
3014 if (prevp->br_startblock == HOLESTARTBLOCK)
3015 prevo = prevp->br_startoff;
3017 prevo = prevp->br_startoff + prevp->br_blockcount;
3020 if (align_off != orig_off && align_off < prevo)
3023 * If the next block overlaps with this proposed allocation
3024 * then move the start back without adjusting the length,
3025 * but not before offset 0.
3026 * This may of course make the start overlap previous block,
3027 * and if we hit the offset 0 limit then the next block
3028 * can still overlap too.
3030 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3031 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3032 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3033 nexto = gotp->br_startoff + gotp->br_blockcount;
3035 nexto = gotp->br_startoff;
3037 nexto = NULLFILEOFF;
3039 align_off + align_alen != orig_end &&
3040 align_off + align_alen > nexto)
3041 align_off = nexto > align_alen ? nexto - align_alen : 0;
3043 * If we're now overlapping the next or previous extent that
3044 * means we can't fit an extsz piece in this hole. Just move
3045 * the start forward to the first valid spot and set
3046 * the length so we hit the end.
3048 if (align_off != orig_off && align_off < prevo)
3050 if (align_off + align_alen != orig_end &&
3051 align_off + align_alen > nexto &&
3052 nexto != NULLFILEOFF) {
3053 ASSERT(nexto > prevo);
3054 align_alen = nexto - align_off;
3058 * If realtime, and the result isn't a multiple of the realtime
3059 * extent size we need to remove blocks until it is.
3061 if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
3063 * We're not covering the original request, or
3064 * we won't be able to once we fix the length.
3066 if (orig_off < align_off ||
3067 orig_end > align_off + align_alen ||
3068 align_alen - temp < orig_alen)
3071 * Try to fix it by moving the start up.
3073 if (align_off + temp <= orig_off) {
3078 * Try to fix it by moving the end in.
3080 else if (align_off + align_alen - temp >= orig_end)
3083 * Set the start to the minimum then trim the length.
3086 align_alen -= orig_off - align_off;
3087 align_off = orig_off;
3088 align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
3091 * Result doesn't cover the request, fail it.
3093 if (orig_off < align_off || orig_end > align_off + align_alen)
3096 ASSERT(orig_off >= align_off);
3097 /* see XFS_BMBT_MAX_EXTLEN handling above */
3098 ASSERT(orig_end <= align_off + align_alen ||
3099 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3103 if (!eof && gotp->br_startoff != NULLFILEOFF)
3104 ASSERT(align_off + align_alen <= gotp->br_startoff);
3105 if (prevp->br_startoff != NULLFILEOFF)
3106 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3114 #define XFS_ALLOC_GAP_UNITS 4
3116 /* returns true if ap->blkno was modified */
3119 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3121 xfs_fsblock_t adjust; /* adjustment to block numbers */
3122 xfs_mount_t *mp; /* mount point structure */
3123 int rt; /* true if inode is realtime */
3125 #define ISVALID(x,y) \
3127 (x) < mp->m_sb.sb_rblocks : \
3128 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3129 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3130 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3132 mp = ap->ip->i_mount;
3133 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3134 (ap->datatype & XFS_ALLOC_USERDATA);
3136 * If allocating at eof, and there's a previous real block,
3137 * try to use its last block as our starting point.
3139 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3140 !isnullstartblock(ap->prev.br_startblock) &&
3141 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3142 ap->prev.br_startblock)) {
3143 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3145 * Adjust for the gap between prevp and us.
3147 adjust = ap->offset -
3148 (ap->prev.br_startoff + ap->prev.br_blockcount);
3150 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3151 ap->blkno += adjust;
3155 * If not at eof, then compare the two neighbor blocks.
3156 * Figure out whether either one gives us a good starting point,
3157 * and pick the better one.
3160 xfs_fsblock_t gotbno; /* right side block number */
3161 xfs_fsblock_t gotdiff=0; /* right side difference */
3162 xfs_fsblock_t prevbno; /* left side block number */
3163 xfs_fsblock_t prevdiff=0; /* left side difference */
3166 * If there's a previous (left) block, select a requested
3167 * start block based on it.
3169 if (ap->prev.br_startoff != NULLFILEOFF &&
3170 !isnullstartblock(ap->prev.br_startblock) &&
3171 (prevbno = ap->prev.br_startblock +
3172 ap->prev.br_blockcount) &&
3173 ISVALID(prevbno, ap->prev.br_startblock)) {
3175 * Calculate gap to end of previous block.
3177 adjust = prevdiff = ap->offset -
3178 (ap->prev.br_startoff +
3179 ap->prev.br_blockcount);
3181 * Figure the startblock based on the previous block's
3182 * end and the gap size.
3184 * If the gap is large relative to the piece we're
3185 * allocating, or using it gives us an invalid block
3186 * number, then just use the end of the previous block.
3188 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3189 ISVALID(prevbno + prevdiff,
3190 ap->prev.br_startblock))
3196 * No previous block or can't follow it, just default.
3199 prevbno = NULLFSBLOCK;
3201 * If there's a following (right) block, select a requested
3202 * start block based on it.
3204 if (!isnullstartblock(ap->got.br_startblock)) {
3206 * Calculate gap to start of next block.
3208 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3210 * Figure the startblock based on the next block's
3211 * start and the gap size.
3213 gotbno = ap->got.br_startblock;
3216 * If the gap is large relative to the piece we're
3217 * allocating, or using it gives us an invalid block
3218 * number, then just use the start of the next block
3219 * offset by our length.
3221 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3222 ISVALID(gotbno - gotdiff, gotbno))
3224 else if (ISVALID(gotbno - ap->length, gotbno)) {
3225 gotbno -= ap->length;
3226 gotdiff += adjust - ap->length;
3231 * No next block, just default.
3234 gotbno = NULLFSBLOCK;
3236 * If both valid, pick the better one, else the only good
3237 * one, else ap->blkno is already set (to 0 or the inode block).
3239 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) {
3240 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3243 if (prevbno != NULLFSBLOCK) {
3244 ap->blkno = prevbno;
3247 if (gotbno != NULLFSBLOCK) {
3257 xfs_bmap_longest_free_extent(
3258 struct xfs_perag *pag,
3259 struct xfs_trans *tp,
3262 xfs_extlen_t longest;
3265 if (!xfs_perag_initialised_agf(pag)) {
3266 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3272 longest = xfs_alloc_longest_free_extent(pag,
3273 xfs_alloc_min_freelist(pag->pag_mount, pag),
3274 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3275 if (*blen < longest)
3282 xfs_bmap_select_minlen(
3283 struct xfs_bmalloca *ap,
3284 struct xfs_alloc_arg *args,
3289 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3290 * possible that there is enough contiguous free space for this request.
3292 if (blen < ap->minlen)
3296 * If the best seen length is less than the request length,
3297 * use the best as the minimum, otherwise we've got the maxlen we
3300 if (blen < args->maxlen)
3302 return args->maxlen;
3306 xfs_bmap_btalloc_select_lengths(
3307 struct xfs_bmalloca *ap,
3308 struct xfs_alloc_arg *args,
3311 struct xfs_mount *mp = args->mp;
3312 struct xfs_perag *pag;
3313 xfs_agnumber_t agno, startag;
3316 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3317 args->total = ap->minlen;
3318 args->minlen = ap->minlen;
3322 args->total = ap->total;
3323 startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
3324 if (startag == NULLAGNUMBER)
3328 for_each_perag_wrap(mp, startag, agno, pag) {
3329 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3330 if (error && error != -EAGAIN)
3333 if (*blen >= args->maxlen)
3337 xfs_perag_rele(pag);
3339 args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
3343 /* Update all inode and quota accounting for the allocation we just did. */
3345 xfs_bmap_alloc_account(
3346 struct xfs_bmalloca *ap)
3348 bool isrt = XFS_IS_REALTIME_INODE(ap->ip) &&
3349 !(ap->flags & XFS_BMAPI_ATTRFORK);
3352 if (ap->flags & XFS_BMAPI_COWFORK) {
3354 * COW fork blocks are in-core only and thus are treated as
3355 * in-core quota reservation (like delalloc blocks) even when
3356 * converted to real blocks. The quota reservation is not
3357 * accounted to disk until blocks are remapped to the data
3358 * fork. So if these blocks were previously delalloc, we
3359 * already have quota reservation and there's nothing to do
3363 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3368 * Otherwise, we've allocated blocks in a hole. The transaction
3369 * has acquired in-core quota reservation for this extent.
3370 * Rather than account these as real blocks, however, we reduce
3371 * the transaction quota reservation based on the allocation.
3372 * This essentially transfers the transaction quota reservation
3373 * to that of a delalloc extent.
3375 ap->ip->i_delayed_blks += ap->length;
3376 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ?
3377 XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
3382 /* data/attr fork only */
3383 ap->ip->i_nblocks += ap->length;
3384 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3386 ap->ip->i_delayed_blks -= ap->length;
3387 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3388 fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT;
3390 fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
3393 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length);
3397 xfs_bmap_compute_alignments(
3398 struct xfs_bmalloca *ap,
3399 struct xfs_alloc_arg *args)
3401 struct xfs_mount *mp = args->mp;
3402 xfs_extlen_t align = 0; /* minimum allocation alignment */
3403 int stripe_align = 0;
3405 /* stripe alignment for allocation is determined by mount parameters */
3406 if (mp->m_swidth && xfs_has_swalloc(mp))
3407 stripe_align = mp->m_swidth;
3408 else if (mp->m_dalign)
3409 stripe_align = mp->m_dalign;
3411 if (ap->flags & XFS_BMAPI_COWFORK)
3412 align = xfs_get_cowextsz_hint(ap->ip);
3413 else if (ap->datatype & XFS_ALLOC_USERDATA)
3414 align = xfs_get_extsz_hint(ap->ip);
3416 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3417 ap->eof, 0, ap->conv, &ap->offset,
3423 /* apply extent size hints if obtained earlier */
3426 div_u64_rem(ap->offset, args->prod, &args->mod);
3428 args->mod = args->prod - args->mod;
3429 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3433 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3434 div_u64_rem(ap->offset, args->prod, &args->mod);
3436 args->mod = args->prod - args->mod;
3439 return stripe_align;
3443 xfs_bmap_process_allocated_extent(
3444 struct xfs_bmalloca *ap,
3445 struct xfs_alloc_arg *args,
3446 xfs_fileoff_t orig_offset,
3447 xfs_extlen_t orig_length)
3449 ap->blkno = args->fsbno;
3450 ap->length = args->len;
3452 * If the extent size hint is active, we tried to round the
3453 * caller's allocation request offset down to extsz and the
3454 * length up to another extsz boundary. If we found a free
3455 * extent we mapped it in starting at this new offset. If the
3456 * newly mapped space isn't long enough to cover any of the
3457 * range of offsets that was originally requested, move the
3458 * mapping up so that we can fill as much of the caller's
3459 * original request as possible. Free space is apparently
3460 * very fragmented so we're unlikely to be able to satisfy the
3463 if (ap->length <= orig_length)
3464 ap->offset = orig_offset;
3465 else if (ap->offset + ap->length < orig_offset + orig_length)
3466 ap->offset = orig_offset + orig_length - ap->length;
3467 xfs_bmap_alloc_account(ap);
3472 xfs_bmap_exact_minlen_extent_alloc(
3473 struct xfs_bmalloca *ap)
3475 struct xfs_mount *mp = ap->ip->i_mount;
3476 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3477 xfs_fileoff_t orig_offset;
3478 xfs_extlen_t orig_length;
3483 if (ap->minlen != 1) {
3484 ap->blkno = NULLFSBLOCK;
3489 orig_offset = ap->offset;
3490 orig_length = ap->length;
3492 args.alloc_minlen_only = 1;
3494 xfs_bmap_compute_alignments(ap, &args);
3497 * Unlike the longest extent available in an AG, we don't track
3498 * the length of an AG's shortest extent.
3499 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3500 * hence we can afford to start traversing from the 0th AG since
3501 * we need not be concerned about a drop in performance in
3502 * "debug only" code paths.
3504 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3506 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3507 args.minlen = args.maxlen = ap->minlen;
3508 args.total = ap->total;
3511 args.minalignslop = 0;
3513 args.minleft = ap->minleft;
3514 args.wasdel = ap->wasdel;
3515 args.resv = XFS_AG_RESV_NONE;
3516 args.datatype = ap->datatype;
3518 error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
3522 if (args.fsbno != NULLFSBLOCK) {
3523 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3526 ap->blkno = NULLFSBLOCK;
3534 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3539 * If we are not low on available data blocks and we are allocating at
3540 * EOF, optimise allocation for contiguous file extension and/or stripe
3541 * alignment of the new extent.
3543 * NOTE: ap->aeof is only set if the allocation length is >= the
3544 * stripe unit and the allocation offset is at the end of file.
3547 xfs_bmap_btalloc_at_eof(
3548 struct xfs_bmalloca *ap,
3549 struct xfs_alloc_arg *args,
3554 struct xfs_mount *mp = args->mp;
3555 struct xfs_perag *caller_pag = args->pag;
3559 * If there are already extents in the file, try an exact EOF block
3560 * allocation to extend the file as a contiguous extent. If that fails,
3561 * or it's the first allocation in a file, just try for a stripe aligned
3565 xfs_extlen_t nextminlen = 0;
3568 * Compute the minlen+alignment for the next case. Set slop so
3569 * that the value of minlen+alignment+slop doesn't go up between
3572 args->alignment = 1;
3573 if (blen > stripe_align && blen <= args->maxlen)
3574 nextminlen = blen - stripe_align;
3576 nextminlen = args->minlen;
3577 if (nextminlen + stripe_align > args->minlen + 1)
3578 args->minalignslop = nextminlen + stripe_align -
3581 args->minalignslop = 0;
3584 args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
3585 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
3587 xfs_perag_put(args->pag);
3593 if (args->fsbno != NULLFSBLOCK)
3596 * Exact allocation failed. Reset to try an aligned allocation
3597 * according to the original allocation specification.
3599 args->alignment = stripe_align;
3600 args->minlen = nextminlen;
3601 args->minalignslop = 0;
3604 * Adjust minlen to try and preserve alignment if we
3605 * can't guarantee an aligned maxlen extent.
3607 args->alignment = stripe_align;
3608 if (blen > args->alignment &&
3609 blen <= args->maxlen + args->alignment)
3610 args->minlen = blen - args->alignment;
3611 args->minalignslop = 0;
3615 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3618 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3619 ASSERT(args->pag == NULL);
3620 args->pag = caller_pag;
3625 if (args->fsbno != NULLFSBLOCK)
3629 * Allocation failed, so turn return the allocation args to their
3630 * original non-aligned state so the caller can proceed on allocation
3631 * failure as if this function was never called.
3633 args->alignment = 1;
3638 * We have failed multiple allocation attempts so now are in a low space
3639 * allocation situation. Try a locality first full filesystem minimum length
3640 * allocation whilst still maintaining necessary total block reservation
3643 * If that fails, we are now critically low on space, so perform a last resort
3644 * allocation attempt: no reserve, no locality, blocking, minimum length, full
3645 * filesystem free space scan. We also indicate to future allocations in this
3646 * transaction that we are critically low on space so they don't waste time on
3647 * allocation modes that are unlikely to succeed.
3650 xfs_bmap_btalloc_low_space(
3651 struct xfs_bmalloca *ap,
3652 struct xfs_alloc_arg *args)
3656 if (args->minlen > ap->minlen) {
3657 args->minlen = ap->minlen;
3658 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3659 if (error || args->fsbno != NULLFSBLOCK)
3663 /* Last ditch attempt before failure is declared. */
3664 args->total = ap->minlen;
3665 error = xfs_alloc_vextent_first_ag(args, 0);
3668 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3673 xfs_bmap_btalloc_filestreams(
3674 struct xfs_bmalloca *ap,
3675 struct xfs_alloc_arg *args,
3678 xfs_extlen_t blen = 0;
3682 error = xfs_filestream_select_ag(ap, args, &blen);
3688 * If we are in low space mode, then optimal allocation will fail so
3689 * prepare for minimal allocation and jump to the low space algorithm
3692 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3693 args->minlen = ap->minlen;
3694 ASSERT(args->fsbno == NULLFSBLOCK);
3698 args->minlen = xfs_bmap_select_minlen(ap, args, blen);
3700 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3703 if (!error && args->fsbno == NULLFSBLOCK)
3704 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3708 * We are now done with the perag reference for the filestreams
3709 * association provided by xfs_filestream_select_ag(). Release it now as
3710 * we've either succeeded, had a fatal error or we are out of space and
3711 * need to do a full filesystem scan for free space which will take it's
3714 xfs_perag_rele(args->pag);
3716 if (error || args->fsbno != NULLFSBLOCK)
3719 return xfs_bmap_btalloc_low_space(ap, args);
3723 xfs_bmap_btalloc_best_length(
3724 struct xfs_bmalloca *ap,
3725 struct xfs_alloc_arg *args,
3728 xfs_extlen_t blen = 0;
3731 ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
3732 xfs_bmap_adjacent(ap);
3735 * Search for an allocation group with a single extent large enough for
3736 * the request. If one isn't found, then adjust the minimum allocation
3737 * size to the largest space found.
3739 error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
3744 * Don't attempt optimal EOF allocation if previous allocations barely
3745 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3746 * optimal or even aligned allocations in this case, so don't waste time
3749 if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
3750 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3752 if (error || args->fsbno != NULLFSBLOCK)
3756 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3757 if (error || args->fsbno != NULLFSBLOCK)
3760 return xfs_bmap_btalloc_low_space(ap, args);
3765 struct xfs_bmalloca *ap)
3767 struct xfs_mount *mp = ap->ip->i_mount;
3768 struct xfs_alloc_arg args = {
3771 .fsbno = NULLFSBLOCK,
3772 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
3773 .minleft = ap->minleft,
3774 .wasdel = ap->wasdel,
3775 .resv = XFS_AG_RESV_NONE,
3776 .datatype = ap->datatype,
3780 xfs_fileoff_t orig_offset;
3781 xfs_extlen_t orig_length;
3786 orig_offset = ap->offset;
3787 orig_length = ap->length;
3789 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3791 /* Trim the allocation back to the maximum an AG can fit. */
3792 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3794 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3795 xfs_inode_is_filestream(ap->ip))
3796 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3798 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3802 if (args.fsbno != NULLFSBLOCK) {
3803 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3806 ap->blkno = NULLFSBLOCK;
3812 /* Trim extent to fit a logical block range. */
3815 struct xfs_bmbt_irec *irec,
3819 xfs_fileoff_t distance;
3820 xfs_fileoff_t end = bno + len;
3822 if (irec->br_startoff + irec->br_blockcount <= bno ||
3823 irec->br_startoff >= end) {
3824 irec->br_blockcount = 0;
3828 if (irec->br_startoff < bno) {
3829 distance = bno - irec->br_startoff;
3830 if (isnullstartblock(irec->br_startblock))
3831 irec->br_startblock = DELAYSTARTBLOCK;
3832 if (irec->br_startblock != DELAYSTARTBLOCK &&
3833 irec->br_startblock != HOLESTARTBLOCK)
3834 irec->br_startblock += distance;
3835 irec->br_startoff += distance;
3836 irec->br_blockcount -= distance;
3839 if (end < irec->br_startoff + irec->br_blockcount) {
3840 distance = irec->br_startoff + irec->br_blockcount - end;
3841 irec->br_blockcount -= distance;
3846 * Trim the returned map to the required bounds
3850 struct xfs_bmbt_irec *mval,
3851 struct xfs_bmbt_irec *got,
3859 if ((flags & XFS_BMAPI_ENTIRE) ||
3860 got->br_startoff + got->br_blockcount <= obno) {
3862 if (isnullstartblock(got->br_startblock))
3863 mval->br_startblock = DELAYSTARTBLOCK;
3869 ASSERT((*bno >= obno) || (n == 0));
3871 mval->br_startoff = *bno;
3872 if (isnullstartblock(got->br_startblock))
3873 mval->br_startblock = DELAYSTARTBLOCK;
3875 mval->br_startblock = got->br_startblock +
3876 (*bno - got->br_startoff);
3878 * Return the minimum of what we got and what we asked for for
3879 * the length. We can use the len variable here because it is
3880 * modified below and we could have been there before coming
3881 * here if the first part of the allocation didn't overlap what
3884 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3885 got->br_blockcount - (*bno - got->br_startoff));
3886 mval->br_state = got->br_state;
3887 ASSERT(mval->br_blockcount <= len);
3892 * Update and validate the extent map to return
3895 xfs_bmapi_update_map(
3896 struct xfs_bmbt_irec **map,
3904 xfs_bmbt_irec_t *mval = *map;
3906 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3907 ((mval->br_startoff + mval->br_blockcount) <= end));
3908 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3909 (mval->br_startoff < obno));
3911 *bno = mval->br_startoff + mval->br_blockcount;
3913 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3914 /* update previous map with new information */
3915 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3916 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3917 ASSERT(mval->br_state == mval[-1].br_state);
3918 mval[-1].br_blockcount = mval->br_blockcount;
3919 mval[-1].br_state = mval->br_state;
3920 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3921 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3922 mval[-1].br_startblock != HOLESTARTBLOCK &&
3923 mval->br_startblock == mval[-1].br_startblock +
3924 mval[-1].br_blockcount &&
3925 mval[-1].br_state == mval->br_state) {
3926 ASSERT(mval->br_startoff ==
3927 mval[-1].br_startoff + mval[-1].br_blockcount);
3928 mval[-1].br_blockcount += mval->br_blockcount;
3929 } else if (*n > 0 &&
3930 mval->br_startblock == DELAYSTARTBLOCK &&
3931 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3932 mval->br_startoff ==
3933 mval[-1].br_startoff + mval[-1].br_blockcount) {
3934 mval[-1].br_blockcount += mval->br_blockcount;
3935 mval[-1].br_state = mval->br_state;
3936 } else if (!((*n == 0) &&
3937 ((mval->br_startoff + mval->br_blockcount) <=
3946 * Map file blocks to filesystem blocks without allocation.
3950 struct xfs_inode *ip,
3953 struct xfs_bmbt_irec *mval,
3957 struct xfs_mount *mp = ip->i_mount;
3958 int whichfork = xfs_bmapi_whichfork(flags);
3959 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3960 struct xfs_bmbt_irec got;
3963 struct xfs_iext_cursor icur;
3969 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3970 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
3972 if (WARN_ON_ONCE(!ifp)) {
3973 xfs_bmap_mark_sick(ip, whichfork);
3974 return -EFSCORRUPTED;
3977 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3978 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3979 xfs_bmap_mark_sick(ip, whichfork);
3980 return -EFSCORRUPTED;
3983 if (xfs_is_shutdown(mp))
3986 XFS_STATS_INC(mp, xs_blk_mapr);
3988 error = xfs_iread_extents(NULL, ip, whichfork);
3992 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3997 while (bno < end && n < *nmap) {
3998 /* Reading past eof, act as though there's a hole up to end. */
4000 got.br_startoff = end;
4001 if (got.br_startoff > bno) {
4002 /* Reading in a hole. */
4003 mval->br_startoff = bno;
4004 mval->br_startblock = HOLESTARTBLOCK;
4005 mval->br_blockcount =
4006 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4007 mval->br_state = XFS_EXT_NORM;
4008 bno += mval->br_blockcount;
4009 len -= mval->br_blockcount;
4015 /* set up the extent map to return. */
4016 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4017 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4019 /* If we're done, stop now. */
4020 if (bno >= end || n >= *nmap)
4023 /* Else go on to the next record. */
4024 if (!xfs_iext_next_extent(ifp, &icur, &got))
4032 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4033 * global pool and the extent inserted into the inode in-core extent tree.
4035 * On entry, got refers to the first extent beyond the offset of the extent to
4036 * allocate or eof is specified if no such extent exists. On return, got refers
4037 * to the extent record that was inserted to the inode fork.
4039 * Note that the allocated extent may have been merged with contiguous extents
4040 * during insertion into the inode fork. Thus, got does not reflect the current
4041 * state of the inode fork on return. If necessary, the caller can use lastx to
4042 * look up the updated record in the inode fork.
4045 xfs_bmapi_reserve_delalloc(
4046 struct xfs_inode *ip,
4050 xfs_filblks_t prealloc,
4051 struct xfs_bmbt_irec *got,
4052 struct xfs_iext_cursor *icur,
4055 struct xfs_mount *mp = ip->i_mount;
4056 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4058 xfs_extlen_t indlen;
4061 xfs_fileoff_t aoff = off;
4064 * Cap the alloc length. Keep track of prealloc so we know whether to
4065 * tag the inode before we return.
4067 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
4069 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4070 if (prealloc && alen >= len)
4071 prealloc = alen - len;
4073 /* Figure out the extent size, adjust alen */
4074 if (whichfork == XFS_COW_FORK) {
4075 struct xfs_bmbt_irec prev;
4076 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
4078 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4079 prev.br_startoff = NULLFILEOFF;
4081 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4082 1, 0, &aoff, &alen);
4087 * Make a transaction-less quota reservation for delayed allocation
4088 * blocks. This number gets adjusted later. We return if we haven't
4089 * allocated blocks already inside this loop.
4091 error = xfs_quota_reserve_blkres(ip, alen);
4096 * Split changing sb for alen and indlen since they could be coming
4097 * from different places.
4099 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4103 if (XFS_IS_REALTIME_INODE(ip)) {
4104 error = xfs_dec_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4106 goto out_unreserve_quota;
4111 error = xfs_dec_fdblocks(mp, fdblocks, false);
4113 goto out_unreserve_frextents;
4115 ip->i_delayed_blks += alen;
4116 xfs_mod_delalloc(ip, alen, indlen);
4118 got->br_startoff = aoff;
4119 got->br_startblock = nullstartblock(indlen);
4120 got->br_blockcount = alen;
4121 got->br_state = XFS_EXT_NORM;
4123 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4126 * Tag the inode if blocks were preallocated. Note that COW fork
4127 * preallocation can occur at the start or end of the extent, even when
4128 * prealloc == 0, so we must also check the aligned offset and length.
4130 if (whichfork == XFS_DATA_FORK && prealloc)
4131 xfs_inode_set_eofblocks_tag(ip);
4132 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4133 xfs_inode_set_cowblocks_tag(ip);
4137 out_unreserve_frextents:
4138 if (XFS_IS_REALTIME_INODE(ip))
4139 xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4140 out_unreserve_quota:
4141 if (XFS_IS_QUOTA_ON(mp))
4142 xfs_quota_unreserve_blkres(ip, alen);
4147 xfs_bmap_alloc_userdata(
4148 struct xfs_bmalloca *bma)
4150 struct xfs_mount *mp = bma->ip->i_mount;
4151 int whichfork = xfs_bmapi_whichfork(bma->flags);
4155 * Set the data type being allocated. For the data fork, the first data
4156 * in the file is treated differently to all other allocations. For the
4157 * attribute fork, we only need to ensure the allocated range is not on
4160 bma->datatype = XFS_ALLOC_NOBUSY;
4161 if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
4162 bma->datatype |= XFS_ALLOC_USERDATA;
4163 if (bma->offset == 0)
4164 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4166 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4167 error = xfs_bmap_isaeof(bma, whichfork);
4172 if (XFS_IS_REALTIME_INODE(bma->ip))
4173 return xfs_bmap_rtalloc(bma);
4176 if (unlikely(XFS_TEST_ERROR(false, mp,
4177 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4178 return xfs_bmap_exact_minlen_extent_alloc(bma);
4180 return xfs_bmap_btalloc(bma);
4185 struct xfs_bmalloca *bma)
4187 struct xfs_mount *mp = bma->ip->i_mount;
4188 int whichfork = xfs_bmapi_whichfork(bma->flags);
4189 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4192 ASSERT(bma->length > 0);
4193 ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN);
4195 if (bma->flags & XFS_BMAPI_CONTIG)
4196 bma->minlen = bma->length;
4200 if (bma->flags & XFS_BMAPI_METADATA) {
4201 if (unlikely(XFS_TEST_ERROR(false, mp,
4202 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4203 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4205 error = xfs_bmap_btalloc(bma);
4207 error = xfs_bmap_alloc_userdata(bma);
4211 if (bma->blkno == NULLFSBLOCK)
4214 if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) {
4215 xfs_bmap_mark_sick(bma->ip, whichfork);
4216 return -EFSCORRUPTED;
4219 if (bma->flags & XFS_BMAPI_ZERO) {
4220 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4225 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4226 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4228 * Bump the number of extents we've allocated
4233 if (bma->cur && bma->wasdel)
4234 bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
4236 bma->got.br_startoff = bma->offset;
4237 bma->got.br_startblock = bma->blkno;
4238 bma->got.br_blockcount = bma->length;
4239 bma->got.br_state = XFS_EXT_NORM;
4241 if (bma->flags & XFS_BMAPI_PREALLOC)
4242 bma->got.br_state = XFS_EXT_UNWRITTEN;
4245 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4247 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4248 whichfork, &bma->icur, &bma->cur, &bma->got,
4249 &bma->logflags, bma->flags);
4254 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4255 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4256 * the neighbouring ones.
4258 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4260 ASSERT(bma->got.br_startoff <= bma->offset);
4261 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4262 bma->offset + bma->length);
4263 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4264 bma->got.br_state == XFS_EXT_UNWRITTEN);
4269 xfs_bmapi_convert_unwritten(
4270 struct xfs_bmalloca *bma,
4271 struct xfs_bmbt_irec *mval,
4275 int whichfork = xfs_bmapi_whichfork(flags);
4276 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4277 int tmp_logflags = 0;
4280 /* check if we need to do unwritten->real conversion */
4281 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4282 (flags & XFS_BMAPI_PREALLOC))
4285 /* check if we need to do real->unwritten conversion */
4286 if (mval->br_state == XFS_EXT_NORM &&
4287 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4288 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4292 * Modify (by adding) the state flag, if writing.
4294 ASSERT(mval->br_blockcount <= len);
4295 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4296 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4297 bma->ip, whichfork);
4299 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4300 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4303 * Before insertion into the bmbt, zero the range being converted
4306 if (flags & XFS_BMAPI_ZERO) {
4307 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4308 mval->br_blockcount);
4313 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4314 &bma->icur, &bma->cur, mval, &tmp_logflags);
4316 * Log the inode core unconditionally in the unwritten extent conversion
4317 * path because the conversion might not have done so (e.g., if the
4318 * extent count hasn't changed). We need to make sure the inode is dirty
4319 * in the transaction for the sake of fsync(), even if nothing has
4320 * changed, because fsync() will not force the log for this transaction
4321 * unless it sees the inode pinned.
4323 * Note: If we're only converting cow fork extents, there aren't
4324 * any on-disk updates to make, so we don't need to log anything.
4326 if (whichfork != XFS_COW_FORK)
4327 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4332 * Update our extent pointer, given that
4333 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4334 * of the neighbouring ones.
4336 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4339 * We may have combined previously unwritten space with written space,
4340 * so generate another request.
4342 if (mval->br_blockcount < len)
4349 struct xfs_trans *tp,
4350 struct xfs_inode *ip,
4353 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
4355 if (tp && tp->t_highest_agno != NULLAGNUMBER)
4357 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4359 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4363 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4364 * a case where the data is changed, there's an error, and it's not logged so we
4365 * don't shutdown when we should. Don't bother logging extents/btree changes if
4366 * we converted to the other format.
4370 struct xfs_bmalloca *bma,
4374 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4376 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4377 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4378 bma->logflags &= ~xfs_ilog_fext(whichfork);
4379 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4380 ifp->if_format != XFS_DINODE_FMT_BTREE)
4381 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4384 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4386 xfs_btree_del_cursor(bma->cur, error);
4390 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4391 * extent state if necessary. Details behaviour is controlled by the flags
4392 * parameter. Only allocates blocks from a single allocation group, to avoid
4395 * Returns 0 on success and places the extent mappings in mval. nmaps is used
4396 * as an input/output parameter where the caller specifies the maximum number
4397 * of mappings that may be returned and xfs_bmapi_write passes back the number
4398 * of mappings (including existing mappings) it found.
4400 * Returns a negative error code on failure, including -ENOSPC when it could not
4401 * allocate any blocks and -ENOSR when it did allocate blocks to convert a
4402 * delalloc range, but those blocks were before the passed in range.
4406 struct xfs_trans *tp, /* transaction pointer */
4407 struct xfs_inode *ip, /* incore inode */
4408 xfs_fileoff_t bno, /* starting file offs. mapped */
4409 xfs_filblks_t len, /* length to map in file */
4410 uint32_t flags, /* XFS_BMAPI_... */
4411 xfs_extlen_t total, /* total blocks needed */
4412 struct xfs_bmbt_irec *mval, /* output: map values */
4413 int *nmap) /* i/o: mval size/count */
4415 struct xfs_bmalloca bma = {
4420 struct xfs_mount *mp = ip->i_mount;
4421 int whichfork = xfs_bmapi_whichfork(flags);
4422 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4423 xfs_fileoff_t end; /* end of mapped file region */
4424 bool eof = false; /* after the end of extents */
4425 int error; /* error return */
4426 int n; /* current extent index */
4427 xfs_fileoff_t obno; /* old block number (offset) */
4430 xfs_fileoff_t orig_bno; /* original block number value */
4431 int orig_flags; /* original flags arg value */
4432 xfs_filblks_t orig_len; /* original value of len arg */
4433 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4434 int orig_nmap; /* original value of *nmap */
4444 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4447 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4448 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4449 ASSERT(!(flags & XFS_BMAPI_REMAP));
4451 /* zeroing is for currently only for data extents, not metadata */
4452 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4453 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4455 * we can allocate unwritten extents or pre-zero allocated blocks,
4456 * but it makes no sense to do both at once. This would result in
4457 * zeroing the unwritten extent twice, but it still being an
4458 * unwritten extent....
4460 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4461 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4463 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4464 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4465 xfs_bmap_mark_sick(ip, whichfork);
4466 return -EFSCORRUPTED;
4469 if (xfs_is_shutdown(mp))
4472 XFS_STATS_INC(mp, xs_blk_mapw);
4474 error = xfs_iread_extents(tp, ip, whichfork);
4478 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4480 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4481 bma.prev.br_startoff = NULLFILEOFF;
4482 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4487 while (bno < end && n < *nmap) {
4488 bool need_alloc = false, wasdelay = false;
4490 /* in hole or beyond EOF? */
4491 if (eof || bma.got.br_startoff > bno) {
4493 * CoW fork conversions should /never/ hit EOF or
4494 * holes. There should always be something for us
4497 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4498 (flags & XFS_BMAPI_COWFORK)));
4501 } else if (isnullstartblock(bma.got.br_startblock)) {
4506 * First, deal with the hole before the allocated space
4507 * that we found, if any.
4509 if (need_alloc || wasdelay) {
4511 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4512 bma.wasdel = wasdelay;
4517 * There's a 32/64 bit type mismatch between the
4518 * allocation length request (which can be 64 bits in
4519 * length) and the bma length request, which is
4520 * xfs_extlen_t and therefore 32 bits. Hence we have to
4521 * be careful and do the min() using the larger type to
4524 bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN);
4527 bma.length = XFS_FILBLKS_MIN(bma.length,
4528 bma.got.br_blockcount -
4529 (bno - bma.got.br_startoff));
4532 bma.length = XFS_FILBLKS_MIN(bma.length,
4533 bma.got.br_startoff - bno);
4536 ASSERT(bma.length > 0);
4537 error = xfs_bmapi_allocate(&bma);
4540 * If we already allocated space in a previous
4541 * iteration return what we go so far when
4542 * running out of space.
4544 if (error == -ENOSPC && bma.nallocs)
4550 * If this is a CoW allocation, record the data in
4551 * the refcount btree for orphan recovery.
4553 if (whichfork == XFS_COW_FORK)
4554 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4558 /* Deal with the allocated space we found. */
4559 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4562 /* Execute unwritten extent conversion if necessary */
4563 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4564 if (error == -EAGAIN)
4569 /* update the extent map to return */
4570 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4573 * If we're done, stop now. Stop when we've allocated
4574 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4575 * the transaction may get too big.
4577 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4580 /* Else go on to the next record. */
4582 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4586 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4591 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4592 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4593 xfs_bmapi_finish(&bma, whichfork, 0);
4594 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4598 * When converting delayed allocations, xfs_bmapi_allocate ignores
4599 * the passed in bno and always converts from the start of the found
4602 * To avoid a successful return with *nmap set to 0, return the magic
4603 * -ENOSR error code for this particular case so that the caller can
4607 ASSERT(bma.nallocs >= *nmap);
4613 xfs_bmapi_finish(&bma, whichfork, error);
4618 * Convert an existing delalloc extent to real blocks based on file offset. This
4619 * attempts to allocate the entire delalloc extent and may require multiple
4620 * invocations to allocate the target offset if a large enough physical extent
4624 xfs_bmapi_convert_one_delalloc(
4625 struct xfs_inode *ip,
4628 struct iomap *iomap,
4631 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4632 struct xfs_mount *mp = ip->i_mount;
4633 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4634 struct xfs_bmalloca bma = { NULL };
4636 struct xfs_trans *tp;
4639 if (whichfork == XFS_COW_FORK)
4640 flags |= IOMAP_F_SHARED;
4643 * Space for the extent and indirect blocks was reserved when the
4644 * delalloc extent was created so there's no need to do so here.
4646 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4647 XFS_TRANS_RESERVE, &tp);
4651 xfs_ilock(ip, XFS_ILOCK_EXCL);
4652 xfs_trans_ijoin(tp, ip, 0);
4654 error = xfs_iext_count_extend(tp, ip, whichfork,
4655 XFS_IEXT_ADD_NOSPLIT_CNT);
4657 goto out_trans_cancel;
4659 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4660 bma.got.br_startoff > offset_fsb) {
4662 * No extent found in the range we are trying to convert. This
4663 * should only happen for the COW fork, where another thread
4664 * might have moved the extent to the data fork in the meantime.
4666 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4668 goto out_trans_cancel;
4672 * If we find a real extent here we raced with another thread converting
4673 * the extent. Just return the real extent at this offset.
4675 if (!isnullstartblock(bma.got.br_startblock)) {
4676 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4677 xfs_iomap_inode_sequence(ip, flags));
4679 *seq = READ_ONCE(ifp->if_seq);
4680 goto out_trans_cancel;
4686 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4689 * Always allocate convert from the start of the delalloc extent even if
4690 * that is outside the passed in range to create large contiguous
4693 bma.offset = bma.got.br_startoff;
4694 bma.length = bma.got.br_blockcount;
4697 * When we're converting the delalloc reservations backing dirty pages
4698 * in the page cache, we must be careful about how we create the new
4701 * New CoW fork extents are created unwritten, turned into real extents
4702 * when we're about to write the data to disk, and mapped into the data
4703 * fork after the write finishes. End of story.
4705 * New data fork extents must be mapped in as unwritten and converted
4706 * to real extents after the write succeeds to avoid exposing stale
4707 * disk contents if we crash.
4709 bma.flags = XFS_BMAPI_PREALLOC;
4710 if (whichfork == XFS_COW_FORK)
4711 bma.flags |= XFS_BMAPI_COWFORK;
4713 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4714 bma.prev.br_startoff = NULLFILEOFF;
4716 error = xfs_bmapi_allocate(&bma);
4720 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4721 XFS_STATS_INC(mp, xs_xstrat_quick);
4723 ASSERT(!isnullstartblock(bma.got.br_startblock));
4724 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4725 xfs_iomap_inode_sequence(ip, flags));
4727 *seq = READ_ONCE(ifp->if_seq);
4729 if (whichfork == XFS_COW_FORK)
4730 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4732 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4737 xfs_bmapi_finish(&bma, whichfork, 0);
4738 error = xfs_trans_commit(tp);
4739 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4743 xfs_bmapi_finish(&bma, whichfork, error);
4745 xfs_trans_cancel(tp);
4746 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4751 * Pass in a dellalloc extent and convert it to real extents, return the real
4752 * extent that maps offset_fsb in iomap.
4755 xfs_bmapi_convert_delalloc(
4756 struct xfs_inode *ip,
4759 struct iomap *iomap,
4765 * Attempt to allocate whatever delalloc extent currently backs offset
4766 * and put the result into iomap. Allocate in a loop because it may
4767 * take several attempts to allocate real blocks for a contiguous
4768 * delalloc extent if free space is sufficiently fragmented.
4771 error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset,
4775 } while (iomap->offset + iomap->length <= offset);
4782 struct xfs_trans *tp,
4783 struct xfs_inode *ip,
4786 xfs_fsblock_t startblock,
4789 struct xfs_mount *mp = ip->i_mount;
4790 struct xfs_ifork *ifp;
4791 struct xfs_btree_cur *cur = NULL;
4792 struct xfs_bmbt_irec got;
4793 struct xfs_iext_cursor icur;
4794 int whichfork = xfs_bmapi_whichfork(flags);
4795 int logflags = 0, error;
4797 ifp = xfs_ifork_ptr(ip, whichfork);
4799 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4800 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4801 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4802 XFS_BMAPI_NORMAP)));
4803 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4804 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4806 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4807 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4808 xfs_bmap_mark_sick(ip, whichfork);
4809 return -EFSCORRUPTED;
4812 if (xfs_is_shutdown(mp))
4815 error = xfs_iread_extents(tp, ip, whichfork);
4819 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4820 /* make sure we only reflink into a hole. */
4821 ASSERT(got.br_startoff > bno);
4822 ASSERT(got.br_startoff - bno >= len);
4825 ip->i_nblocks += len;
4826 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4828 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
4829 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4831 got.br_startoff = bno;
4832 got.br_startblock = startblock;
4833 got.br_blockcount = len;
4834 if (flags & XFS_BMAPI_PREALLOC)
4835 got.br_state = XFS_EXT_UNWRITTEN;
4837 got.br_state = XFS_EXT_NORM;
4839 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4840 &cur, &got, &logflags, flags);
4844 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4847 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4848 logflags &= ~XFS_ILOG_DEXT;
4849 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4850 logflags &= ~XFS_ILOG_DBROOT;
4853 xfs_trans_log_inode(tp, ip, logflags);
4855 xfs_btree_del_cursor(cur, error);
4860 * When a delalloc extent is split (e.g., due to a hole punch), the original
4861 * indlen reservation must be shared across the two new extents that are left
4864 * Given the original reservation and the worst case indlen for the two new
4865 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4866 * reservation fairly across the two new extents. If necessary, steal available
4867 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4868 * ores == 1). The number of stolen blocks is returned. The availability and
4869 * subsequent accounting of stolen blocks is the responsibility of the caller.
4872 xfs_bmap_split_indlen(
4873 xfs_filblks_t ores, /* original res. */
4874 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4875 xfs_filblks_t *indlen2) /* ext2 worst indlen */
4877 xfs_filblks_t len1 = *indlen1;
4878 xfs_filblks_t len2 = *indlen2;
4879 xfs_filblks_t nres = len1 + len2; /* new total res. */
4880 xfs_filblks_t resfactor;
4883 * We can't meet the total required reservation for the two extents.
4884 * Calculate the percent of the overall shortage between both extents
4885 * and apply this percentage to each of the requested indlen values.
4886 * This distributes the shortage fairly and reduces the chances that one
4887 * of the two extents is left with nothing when extents are repeatedly
4890 resfactor = (ores * 100);
4891 do_div(resfactor, nres);
4896 ASSERT(len1 + len2 <= ores);
4897 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4900 * Hand out the remainder to each extent. If one of the two reservations
4901 * is zero, we want to make sure that one gets a block first. The loop
4902 * below starts with len1, so hand len2 a block right off the bat if it
4905 ores -= (len1 + len2);
4906 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4907 if (ores && !len2 && *indlen2) {
4912 if (len1 < *indlen1) {
4918 if (len2 < *indlen2) {
4929 xfs_bmap_del_extent_delay(
4930 struct xfs_inode *ip,
4932 struct xfs_iext_cursor *icur,
4933 struct xfs_bmbt_irec *got,
4934 struct xfs_bmbt_irec *del)
4936 struct xfs_mount *mp = ip->i_mount;
4937 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4938 struct xfs_bmbt_irec new;
4939 int64_t da_old, da_new, da_diff = 0;
4940 xfs_fileoff_t del_endoff, got_endoff;
4941 xfs_filblks_t got_indlen, new_indlen, stolen = 0;
4942 uint32_t state = xfs_bmap_fork_to_state(whichfork);
4946 XFS_STATS_INC(mp, xs_del_exlist);
4948 isrt = xfs_ifork_is_realtime(ip, whichfork);
4949 del_endoff = del->br_startoff + del->br_blockcount;
4950 got_endoff = got->br_startoff + got->br_blockcount;
4951 da_old = startblockval(got->br_startblock);
4954 ASSERT(del->br_blockcount > 0);
4955 ASSERT(got->br_startoff <= del->br_startoff);
4956 ASSERT(got_endoff >= del_endoff);
4959 * Update the inode delalloc counter now and wait to update the
4960 * sb counters as we might have to borrow some blocks for the
4961 * indirect block accounting.
4963 xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4964 ip->i_delayed_blks -= del->br_blockcount;
4966 if (got->br_startoff == del->br_startoff)
4967 state |= BMAP_LEFT_FILLING;
4968 if (got_endoff == del_endoff)
4969 state |= BMAP_RIGHT_FILLING;
4971 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4972 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4974 * Matches the whole extent. Delete the entry.
4976 xfs_iext_remove(ip, icur, state);
4977 xfs_iext_prev(ifp, icur);
4979 case BMAP_LEFT_FILLING:
4981 * Deleting the first part of the extent.
4983 got->br_startoff = del_endoff;
4984 got->br_blockcount -= del->br_blockcount;
4985 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4986 got->br_blockcount), da_old);
4987 got->br_startblock = nullstartblock((int)da_new);
4988 xfs_iext_update_extent(ip, state, icur, got);
4990 case BMAP_RIGHT_FILLING:
4992 * Deleting the last part of the extent.
4994 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4995 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4996 got->br_blockcount), da_old);
4997 got->br_startblock = nullstartblock((int)da_new);
4998 xfs_iext_update_extent(ip, state, icur, got);
5002 * Deleting the middle of the extent.
5004 * Distribute the original indlen reservation across the two new
5005 * extents. Steal blocks from the deleted extent if necessary.
5006 * Stealing blocks simply fudges the fdblocks accounting below.
5007 * Warn if either of the new indlen reservations is zero as this
5008 * can lead to delalloc problems.
5010 got->br_blockcount = del->br_startoff - got->br_startoff;
5011 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
5013 new.br_blockcount = got_endoff - del_endoff;
5014 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5016 WARN_ON_ONCE(!got_indlen || !new_indlen);
5018 * Steal as many blocks as we can to try and satisfy the worst
5019 * case indlen for both new extents.
5021 * However, we can't just steal reservations from the data
5022 * blocks if this is an RT inodes as the data and metadata
5023 * blocks come from different pools. We'll have to live with
5024 * under-filled indirect reservation in this case.
5026 da_new = got_indlen + new_indlen;
5027 if (da_new > da_old && !isrt) {
5028 stolen = XFS_FILBLKS_MIN(da_new - da_old,
5029 del->br_blockcount);
5032 if (da_new > da_old)
5033 xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen);
5034 da_new = got_indlen + new_indlen;
5036 got->br_startblock = nullstartblock((int)got_indlen);
5038 new.br_startoff = del_endoff;
5039 new.br_state = got->br_state;
5040 new.br_startblock = nullstartblock((int)new_indlen);
5042 xfs_iext_update_extent(ip, state, icur, got);
5043 xfs_iext_next(ifp, icur);
5044 xfs_iext_insert(ip, icur, &new, state);
5046 del->br_blockcount -= stolen;
5050 ASSERT(da_old >= da_new);
5051 da_diff = da_old - da_new;
5055 xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, del->br_blockcount));
5057 fdblocks += del->br_blockcount;
5059 xfs_add_fdblocks(mp, fdblocks);
5060 xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff);
5064 xfs_bmap_del_extent_cow(
5065 struct xfs_inode *ip,
5066 struct xfs_iext_cursor *icur,
5067 struct xfs_bmbt_irec *got,
5068 struct xfs_bmbt_irec *del)
5070 struct xfs_mount *mp = ip->i_mount;
5071 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
5072 struct xfs_bmbt_irec new;
5073 xfs_fileoff_t del_endoff, got_endoff;
5074 uint32_t state = BMAP_COWFORK;
5076 XFS_STATS_INC(mp, xs_del_exlist);
5078 del_endoff = del->br_startoff + del->br_blockcount;
5079 got_endoff = got->br_startoff + got->br_blockcount;
5081 ASSERT(del->br_blockcount > 0);
5082 ASSERT(got->br_startoff <= del->br_startoff);
5083 ASSERT(got_endoff >= del_endoff);
5084 ASSERT(!isnullstartblock(got->br_startblock));
5086 if (got->br_startoff == del->br_startoff)
5087 state |= BMAP_LEFT_FILLING;
5088 if (got_endoff == del_endoff)
5089 state |= BMAP_RIGHT_FILLING;
5091 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5092 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5094 * Matches the whole extent. Delete the entry.
5096 xfs_iext_remove(ip, icur, state);
5097 xfs_iext_prev(ifp, icur);
5099 case BMAP_LEFT_FILLING:
5101 * Deleting the first part of the extent.
5103 got->br_startoff = del_endoff;
5104 got->br_blockcount -= del->br_blockcount;
5105 got->br_startblock = del->br_startblock + del->br_blockcount;
5106 xfs_iext_update_extent(ip, state, icur, got);
5108 case BMAP_RIGHT_FILLING:
5110 * Deleting the last part of the extent.
5112 got->br_blockcount -= del->br_blockcount;
5113 xfs_iext_update_extent(ip, state, icur, got);
5117 * Deleting the middle of the extent.
5119 got->br_blockcount = del->br_startoff - got->br_startoff;
5121 new.br_startoff = del_endoff;
5122 new.br_blockcount = got_endoff - del_endoff;
5123 new.br_state = got->br_state;
5124 new.br_startblock = del->br_startblock + del->br_blockcount;
5126 xfs_iext_update_extent(ip, state, icur, got);
5127 xfs_iext_next(ifp, icur);
5128 xfs_iext_insert(ip, icur, &new, state);
5131 ip->i_delayed_blks -= del->br_blockcount;
5135 * Called by xfs_bmapi to update file extent records and the btree
5136 * after removing space.
5138 STATIC int /* error */
5139 xfs_bmap_del_extent_real(
5140 xfs_inode_t *ip, /* incore inode pointer */
5141 xfs_trans_t *tp, /* current transaction pointer */
5142 struct xfs_iext_cursor *icur,
5143 struct xfs_btree_cur *cur, /* if null, not a btree */
5144 xfs_bmbt_irec_t *del, /* data to remove from extents */
5145 int *logflagsp, /* inode logging flags */
5146 int whichfork, /* data or attr fork */
5147 uint32_t bflags) /* bmapi flags */
5149 xfs_fsblock_t del_endblock=0; /* first block past del */
5150 xfs_fileoff_t del_endoff; /* first offset past del */
5151 int error = 0; /* error return value */
5152 struct xfs_bmbt_irec got; /* current extent entry */
5153 xfs_fileoff_t got_endoff; /* first offset past got */
5154 int i; /* temp state */
5155 struct xfs_ifork *ifp; /* inode fork pointer */
5156 xfs_mount_t *mp; /* mount structure */
5157 xfs_filblks_t nblks; /* quota/sb block count */
5158 xfs_bmbt_irec_t new; /* new record to be inserted */
5160 uint qfield; /* quota field to update */
5161 uint32_t state = xfs_bmap_fork_to_state(whichfork);
5162 struct xfs_bmbt_irec old;
5167 XFS_STATS_INC(mp, xs_del_exlist);
5169 ifp = xfs_ifork_ptr(ip, whichfork);
5170 ASSERT(del->br_blockcount > 0);
5171 xfs_iext_get_extent(ifp, icur, &got);
5172 ASSERT(got.br_startoff <= del->br_startoff);
5173 del_endoff = del->br_startoff + del->br_blockcount;
5174 got_endoff = got.br_startoff + got.br_blockcount;
5175 ASSERT(got_endoff >= del_endoff);
5176 ASSERT(!isnullstartblock(got.br_startblock));
5180 * If it's the case where the directory code is running with no block
5181 * reservation, and the deleted block is in the middle of its extent,
5182 * and the resulting insert of an extent would cause transformation to
5183 * btree format, then reject it. The calling code will then swap blocks
5184 * around instead. We have to do this now, rather than waiting for the
5185 * conversion to btree format, since the transaction will be dirty then.
5187 if (tp->t_blk_res == 0 &&
5188 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5189 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5190 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5193 *logflagsp = XFS_ILOG_CORE;
5194 if (xfs_ifork_is_realtime(ip, whichfork))
5195 qfield = XFS_TRANS_DQ_RTBCOUNT;
5197 qfield = XFS_TRANS_DQ_BCOUNT;
5198 nblks = del->br_blockcount;
5200 del_endblock = del->br_startblock + del->br_blockcount;
5202 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5205 if (XFS_IS_CORRUPT(mp, i != 1)) {
5206 xfs_btree_mark_sick(cur);
5207 return -EFSCORRUPTED;
5211 if (got.br_startoff == del->br_startoff)
5212 state |= BMAP_LEFT_FILLING;
5213 if (got_endoff == del_endoff)
5214 state |= BMAP_RIGHT_FILLING;
5216 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5217 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5219 * Matches the whole extent. Delete the entry.
5221 xfs_iext_remove(ip, icur, state);
5222 xfs_iext_prev(ifp, icur);
5225 *logflagsp |= XFS_ILOG_CORE;
5227 *logflagsp |= xfs_ilog_fext(whichfork);
5230 if ((error = xfs_btree_delete(cur, &i)))
5232 if (XFS_IS_CORRUPT(mp, i != 1)) {
5233 xfs_btree_mark_sick(cur);
5234 return -EFSCORRUPTED;
5237 case BMAP_LEFT_FILLING:
5239 * Deleting the first part of the extent.
5241 got.br_startoff = del_endoff;
5242 got.br_startblock = del_endblock;
5243 got.br_blockcount -= del->br_blockcount;
5244 xfs_iext_update_extent(ip, state, icur, &got);
5246 *logflagsp |= xfs_ilog_fext(whichfork);
5249 error = xfs_bmbt_update(cur, &got);
5253 case BMAP_RIGHT_FILLING:
5255 * Deleting the last part of the extent.
5257 got.br_blockcount -= del->br_blockcount;
5258 xfs_iext_update_extent(ip, state, icur, &got);
5260 *logflagsp |= xfs_ilog_fext(whichfork);
5263 error = xfs_bmbt_update(cur, &got);
5269 * Deleting the middle of the extent.
5274 got.br_blockcount = del->br_startoff - got.br_startoff;
5275 xfs_iext_update_extent(ip, state, icur, &got);
5277 new.br_startoff = del_endoff;
5278 new.br_blockcount = got_endoff - del_endoff;
5279 new.br_state = got.br_state;
5280 new.br_startblock = del_endblock;
5282 *logflagsp |= XFS_ILOG_CORE;
5284 error = xfs_bmbt_update(cur, &got);
5287 error = xfs_btree_increment(cur, 0, &i);
5290 cur->bc_rec.b = new;
5291 error = xfs_btree_insert(cur, &i);
5292 if (error && error != -ENOSPC)
5295 * If get no-space back from btree insert, it tried a
5296 * split, and we have a zero block reservation. Fix up
5297 * our state and return the error.
5299 if (error == -ENOSPC) {
5301 * Reset the cursor, don't trust it after any
5304 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5307 if (XFS_IS_CORRUPT(mp, i != 1)) {
5308 xfs_btree_mark_sick(cur);
5309 return -EFSCORRUPTED;
5312 * Update the btree record back
5313 * to the original value.
5315 error = xfs_bmbt_update(cur, &old);
5319 * Reset the extent record back
5320 * to the original value.
5322 xfs_iext_update_extent(ip, state, icur, &old);
5326 if (XFS_IS_CORRUPT(mp, i != 1)) {
5327 xfs_btree_mark_sick(cur);
5328 return -EFSCORRUPTED;
5331 *logflagsp |= xfs_ilog_fext(whichfork);
5334 xfs_iext_next(ifp, icur);
5335 xfs_iext_insert(ip, icur, &new, state);
5339 /* remove reverse mapping */
5340 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5343 * If we need to, add to list of extents to delete.
5345 if (!(bflags & XFS_BMAPI_REMAP)) {
5346 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5347 xfs_refcount_decrease_extent(tp, del);
5348 } else if (xfs_ifork_is_realtime(ip, whichfork)) {
5350 * Ensure the bitmap and summary inodes are locked
5351 * and joined to the transaction before modifying them.
5353 if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) {
5354 tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED;
5355 xfs_rtbitmap_lock(tp, mp);
5357 error = xfs_rtfree_blocks(tp, del->br_startblock,
5358 del->br_blockcount);
5360 error = xfs_free_extent_later(tp, del->br_startblock,
5361 del->br_blockcount, NULL,
5363 ((bflags & XFS_BMAPI_NODISCARD) ||
5364 del->br_state == XFS_EXT_UNWRITTEN));
5371 * Adjust inode # blocks in the file.
5374 ip->i_nblocks -= nblks;
5376 * Adjust quota data.
5378 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5379 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5385 * Unmap (remove) blocks from a file.
5386 * If nexts is nonzero then the number of extents to remove is limited to
5387 * that value. If not all extents in the block range can be removed then
5392 struct xfs_trans *tp, /* transaction pointer */
5393 struct xfs_inode *ip, /* incore inode */
5394 xfs_fileoff_t start, /* first file offset deleted */
5395 xfs_filblks_t *rlen, /* i/o: amount remaining */
5396 uint32_t flags, /* misc flags */
5397 xfs_extnum_t nexts) /* number of extents max */
5399 struct xfs_btree_cur *cur; /* bmap btree cursor */
5400 struct xfs_bmbt_irec del; /* extent being deleted */
5401 int error; /* error return value */
5402 xfs_extnum_t extno; /* extent number in list */
5403 struct xfs_bmbt_irec got; /* current extent record */
5404 struct xfs_ifork *ifp; /* inode fork pointer */
5405 int isrt; /* freeing in rt area */
5406 int logflags; /* transaction logging flags */
5407 xfs_extlen_t mod; /* rt extent offset */
5408 struct xfs_mount *mp = ip->i_mount;
5409 int tmp_logflags; /* partial logging flags */
5410 int wasdel; /* was a delayed alloc extent */
5411 int whichfork; /* data or attribute fork */
5412 xfs_filblks_t len = *rlen; /* length to unmap in file */
5414 struct xfs_iext_cursor icur;
5417 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5419 whichfork = xfs_bmapi_whichfork(flags);
5420 ASSERT(whichfork != XFS_COW_FORK);
5421 ifp = xfs_ifork_ptr(ip, whichfork);
5422 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5423 xfs_bmap_mark_sick(ip, whichfork);
5424 return -EFSCORRUPTED;
5426 if (xfs_is_shutdown(mp))
5429 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
5433 error = xfs_iread_extents(tp, ip, whichfork);
5437 if (xfs_iext_count(ifp) == 0) {
5441 XFS_STATS_INC(mp, xs_blk_unmap);
5442 isrt = xfs_ifork_is_realtime(ip, whichfork);
5445 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5452 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5453 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5454 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5459 while (end != (xfs_fileoff_t)-1 && end >= start &&
5460 (nexts == 0 || extno < nexts)) {
5462 * Is the found extent after a hole in which end lives?
5463 * Just back up to the previous extent, if so.
5465 if (got.br_startoff > end &&
5466 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5471 * Is the last block of this extent before the range
5472 * we're supposed to delete? If so, we're done.
5474 end = XFS_FILEOFF_MIN(end,
5475 got.br_startoff + got.br_blockcount - 1);
5479 * Then deal with the (possibly delayed) allocated space
5483 wasdel = isnullstartblock(del.br_startblock);
5485 if (got.br_startoff < start) {
5486 del.br_startoff = start;
5487 del.br_blockcount -= start - got.br_startoff;
5489 del.br_startblock += start - got.br_startoff;
5491 if (del.br_startoff + del.br_blockcount > end + 1)
5492 del.br_blockcount = end + 1 - del.br_startoff;
5494 if (!isrt || (flags & XFS_BMAPI_REMAP))
5497 mod = xfs_rtb_to_rtxoff(mp,
5498 del.br_startblock + del.br_blockcount);
5501 * Realtime extent not lined up at the end.
5502 * The extent could have been split into written
5503 * and unwritten pieces, or we could just be
5504 * unmapping part of it. But we can't really
5505 * get rid of part of a realtime extent.
5507 if (del.br_state == XFS_EXT_UNWRITTEN) {
5509 * This piece is unwritten, or we're not
5510 * using unwritten extents. Skip over it.
5512 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
5513 end -= mod > del.br_blockcount ?
5514 del.br_blockcount : mod;
5515 if (end < got.br_startoff &&
5516 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5523 * It's written, turn it unwritten.
5524 * This is better than zeroing it.
5526 ASSERT(del.br_state == XFS_EXT_NORM);
5527 ASSERT(tp->t_blk_res > 0);
5529 * If this spans a realtime extent boundary,
5530 * chop it back to the start of the one we end at.
5532 if (del.br_blockcount > mod) {
5533 del.br_startoff += del.br_blockcount - mod;
5534 del.br_startblock += del.br_blockcount - mod;
5535 del.br_blockcount = mod;
5537 del.br_state = XFS_EXT_UNWRITTEN;
5538 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5539 whichfork, &icur, &cur, &del,
5546 mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
5548 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5551 * Realtime extent is lined up at the end but not
5552 * at the front. We'll get rid of full extents if
5555 if (del.br_blockcount > off) {
5556 del.br_blockcount -= off;
5557 del.br_startoff += off;
5558 del.br_startblock += off;
5559 } else if (del.br_startoff == start &&
5560 (del.br_state == XFS_EXT_UNWRITTEN ||
5561 tp->t_blk_res == 0)) {
5563 * Can't make it unwritten. There isn't
5564 * a full extent here so just skip it.
5566 ASSERT(end >= del.br_blockcount);
5567 end -= del.br_blockcount;
5568 if (got.br_startoff > end &&
5569 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5574 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5575 struct xfs_bmbt_irec prev;
5576 xfs_fileoff_t unwrite_start;
5579 * This one is already unwritten.
5580 * It must have a written left neighbor.
5581 * Unwrite the killed part of that one and
5584 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5586 ASSERT(prev.br_state == XFS_EXT_NORM);
5587 ASSERT(!isnullstartblock(prev.br_startblock));
5588 ASSERT(del.br_startblock ==
5589 prev.br_startblock + prev.br_blockcount);
5590 unwrite_start = max3(start,
5591 del.br_startoff - mod,
5593 mod = unwrite_start - prev.br_startoff;
5594 prev.br_startoff = unwrite_start;
5595 prev.br_startblock += mod;
5596 prev.br_blockcount -= mod;
5597 prev.br_state = XFS_EXT_UNWRITTEN;
5598 error = xfs_bmap_add_extent_unwritten_real(tp,
5599 ip, whichfork, &icur, &cur,
5605 ASSERT(del.br_state == XFS_EXT_NORM);
5606 del.br_state = XFS_EXT_UNWRITTEN;
5607 error = xfs_bmap_add_extent_unwritten_real(tp,
5608 ip, whichfork, &icur, &cur,
5618 xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, &del);
5620 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5621 &del, &tmp_logflags, whichfork,
5623 logflags |= tmp_logflags;
5628 end = del.br_startoff - 1;
5631 * If not done go on to the next (previous) record.
5633 if (end != (xfs_fileoff_t)-1 && end >= start) {
5634 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5635 (got.br_startoff > end &&
5636 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5643 if (done || end == (xfs_fileoff_t)-1 || end < start)
5646 *rlen = end - start + 1;
5649 * Convert to a btree if necessary.
5651 if (xfs_bmap_needs_btree(ip, whichfork)) {
5652 ASSERT(cur == NULL);
5653 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5654 &tmp_logflags, whichfork);
5655 logflags |= tmp_logflags;
5657 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5663 * Log everything. Do this after conversion, there's no point in
5664 * logging the extent records if we've converted to btree format.
5666 if ((logflags & xfs_ilog_fext(whichfork)) &&
5667 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5668 logflags &= ~xfs_ilog_fext(whichfork);
5669 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5670 ifp->if_format != XFS_DINODE_FMT_BTREE)
5671 logflags &= ~xfs_ilog_fbroot(whichfork);
5673 * Log inode even in the error case, if the transaction
5674 * is dirty we'll need to shut down the filesystem.
5677 xfs_trans_log_inode(tp, ip, logflags);
5680 cur->bc_bmap.allocated = 0;
5681 xfs_btree_del_cursor(cur, error);
5686 /* Unmap a range of a file. */
5690 struct xfs_inode *ip,
5699 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5705 * Determine whether an extent shift can be accomplished by a merge with the
5706 * extent that precedes the target hole of the shift.
5710 struct xfs_bmbt_irec *left, /* preceding extent */
5711 struct xfs_bmbt_irec *got, /* current extent to shift */
5712 xfs_fileoff_t shift) /* shift fsb */
5714 xfs_fileoff_t startoff;
5716 startoff = got->br_startoff - shift;
5719 * The extent, once shifted, must be adjacent in-file and on-disk with
5720 * the preceding extent.
5722 if ((left->br_startoff + left->br_blockcount != startoff) ||
5723 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5724 (left->br_state != got->br_state) ||
5725 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5732 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5733 * hole in the file. If an extent shift would result in the extent being fully
5734 * adjacent to the extent that currently precedes the hole, we can merge with
5735 * the preceding extent rather than do the shift.
5737 * This function assumes the caller has verified a shift-by-merge is possible
5738 * with the provided extents via xfs_bmse_can_merge().
5742 struct xfs_trans *tp,
5743 struct xfs_inode *ip,
5745 xfs_fileoff_t shift, /* shift fsb */
5746 struct xfs_iext_cursor *icur,
5747 struct xfs_bmbt_irec *got, /* extent to shift */
5748 struct xfs_bmbt_irec *left, /* preceding extent */
5749 struct xfs_btree_cur *cur,
5750 int *logflags) /* output */
5752 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5753 struct xfs_bmbt_irec new;
5754 xfs_filblks_t blockcount;
5756 struct xfs_mount *mp = ip->i_mount;
5758 blockcount = left->br_blockcount + got->br_blockcount;
5760 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5761 ASSERT(xfs_bmse_can_merge(left, got, shift));
5764 new.br_blockcount = blockcount;
5767 * Update the on-disk extent count, the btree if necessary and log the
5771 *logflags |= XFS_ILOG_CORE;
5773 *logflags |= XFS_ILOG_DEXT;
5777 /* lookup and remove the extent to merge */
5778 error = xfs_bmbt_lookup_eq(cur, got, &i);
5781 if (XFS_IS_CORRUPT(mp, i != 1)) {
5782 xfs_btree_mark_sick(cur);
5783 return -EFSCORRUPTED;
5786 error = xfs_btree_delete(cur, &i);
5789 if (XFS_IS_CORRUPT(mp, i != 1)) {
5790 xfs_btree_mark_sick(cur);
5791 return -EFSCORRUPTED;
5794 /* lookup and update size of the previous extent */
5795 error = xfs_bmbt_lookup_eq(cur, left, &i);
5798 if (XFS_IS_CORRUPT(mp, i != 1)) {
5799 xfs_btree_mark_sick(cur);
5800 return -EFSCORRUPTED;
5803 error = xfs_bmbt_update(cur, &new);
5807 /* change to extent format if required after extent removal */
5808 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5813 xfs_iext_remove(ip, icur, 0);
5814 xfs_iext_prev(ifp, icur);
5815 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5818 /* update reverse mapping. rmap functions merge the rmaps for us */
5819 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5820 memcpy(&new, got, sizeof(new));
5821 new.br_startoff = left->br_startoff + left->br_blockcount;
5822 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5827 xfs_bmap_shift_update_extent(
5828 struct xfs_trans *tp,
5829 struct xfs_inode *ip,
5831 struct xfs_iext_cursor *icur,
5832 struct xfs_bmbt_irec *got,
5833 struct xfs_btree_cur *cur,
5835 xfs_fileoff_t startoff)
5837 struct xfs_mount *mp = ip->i_mount;
5838 struct xfs_bmbt_irec prev = *got;
5841 *logflags |= XFS_ILOG_CORE;
5843 got->br_startoff = startoff;
5846 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5849 if (XFS_IS_CORRUPT(mp, i != 1)) {
5850 xfs_btree_mark_sick(cur);
5851 return -EFSCORRUPTED;
5854 error = xfs_bmbt_update(cur, got);
5858 *logflags |= XFS_ILOG_DEXT;
5861 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5864 /* update reverse mapping */
5865 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5866 xfs_rmap_map_extent(tp, ip, whichfork, got);
5871 xfs_bmap_collapse_extents(
5872 struct xfs_trans *tp,
5873 struct xfs_inode *ip,
5874 xfs_fileoff_t *next_fsb,
5875 xfs_fileoff_t offset_shift_fsb,
5878 int whichfork = XFS_DATA_FORK;
5879 struct xfs_mount *mp = ip->i_mount;
5880 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5881 struct xfs_btree_cur *cur = NULL;
5882 struct xfs_bmbt_irec got, prev;
5883 struct xfs_iext_cursor icur;
5884 xfs_fileoff_t new_startoff;
5888 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5889 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5890 xfs_bmap_mark_sick(ip, whichfork);
5891 return -EFSCORRUPTED;
5894 if (xfs_is_shutdown(mp))
5897 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5899 error = xfs_iread_extents(tp, ip, whichfork);
5903 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
5904 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5906 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5910 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5911 xfs_bmap_mark_sick(ip, whichfork);
5912 error = -EFSCORRUPTED;
5916 new_startoff = got.br_startoff - offset_shift_fsb;
5917 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5918 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5923 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5924 error = xfs_bmse_merge(tp, ip, whichfork,
5925 offset_shift_fsb, &icur, &got, &prev,
5932 if (got.br_startoff < offset_shift_fsb) {
5938 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5939 cur, &logflags, new_startoff);
5944 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5949 *next_fsb = got.br_startoff;
5952 xfs_btree_del_cursor(cur, error);
5954 xfs_trans_log_inode(tp, ip, logflags);
5958 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5960 xfs_bmap_can_insert_extents(
5961 struct xfs_inode *ip,
5963 xfs_fileoff_t shift)
5965 struct xfs_bmbt_irec got;
5969 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
5971 if (xfs_is_shutdown(ip->i_mount))
5974 xfs_ilock(ip, XFS_ILOCK_EXCL);
5975 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5976 if (!error && !is_empty && got.br_startoff >= off &&
5977 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5979 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5985 xfs_bmap_insert_extents(
5986 struct xfs_trans *tp,
5987 struct xfs_inode *ip,
5988 xfs_fileoff_t *next_fsb,
5989 xfs_fileoff_t offset_shift_fsb,
5991 xfs_fileoff_t stop_fsb)
5993 int whichfork = XFS_DATA_FORK;
5994 struct xfs_mount *mp = ip->i_mount;
5995 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5996 struct xfs_btree_cur *cur = NULL;
5997 struct xfs_bmbt_irec got, next;
5998 struct xfs_iext_cursor icur;
5999 xfs_fileoff_t new_startoff;
6003 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6004 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6005 xfs_bmap_mark_sick(ip, whichfork);
6006 return -EFSCORRUPTED;
6009 if (xfs_is_shutdown(mp))
6012 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
6014 error = xfs_iread_extents(tp, ip, whichfork);
6018 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
6019 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6021 if (*next_fsb == NULLFSBLOCK) {
6022 xfs_iext_last(ifp, &icur);
6023 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
6024 stop_fsb > got.br_startoff) {
6029 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
6034 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
6035 xfs_bmap_mark_sick(ip, whichfork);
6036 error = -EFSCORRUPTED;
6040 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
6041 xfs_bmap_mark_sick(ip, whichfork);
6042 error = -EFSCORRUPTED;
6046 new_startoff = got.br_startoff + offset_shift_fsb;
6047 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
6048 if (new_startoff + got.br_blockcount > next.br_startoff) {
6054 * Unlike a left shift (which involves a hole punch), a right
6055 * shift does not modify extent neighbors in any way. We should
6056 * never find mergeable extents in this scenario. Check anyways
6057 * and warn if we encounter two extents that could be one.
6059 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6063 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6064 cur, &logflags, new_startoff);
6068 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6069 stop_fsb >= got.br_startoff + got.br_blockcount) {
6074 *next_fsb = got.br_startoff;
6077 xfs_btree_del_cursor(cur, error);
6079 xfs_trans_log_inode(tp, ip, logflags);
6084 * Splits an extent into two extents at split_fsb block such that it is the
6085 * first block of the current_ext. @ext is a target extent to be split.
6086 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6087 * hole or the first block of extents, just return 0.
6090 xfs_bmap_split_extent(
6091 struct xfs_trans *tp,
6092 struct xfs_inode *ip,
6093 xfs_fileoff_t split_fsb)
6095 int whichfork = XFS_DATA_FORK;
6096 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
6097 struct xfs_btree_cur *cur = NULL;
6098 struct xfs_bmbt_irec got;
6099 struct xfs_bmbt_irec new; /* split extent */
6100 struct xfs_mount *mp = ip->i_mount;
6101 xfs_fsblock_t gotblkcnt; /* new block count for got */
6102 struct xfs_iext_cursor icur;
6107 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6108 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6109 xfs_bmap_mark_sick(ip, whichfork);
6110 return -EFSCORRUPTED;
6113 if (xfs_is_shutdown(mp))
6116 /* Read in all the extents */
6117 error = xfs_iread_extents(tp, ip, whichfork);
6122 * If there are not extents, or split_fsb lies in a hole we are done.
6124 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6125 got.br_startoff >= split_fsb)
6128 gotblkcnt = split_fsb - got.br_startoff;
6129 new.br_startoff = split_fsb;
6130 new.br_startblock = got.br_startblock + gotblkcnt;
6131 new.br_blockcount = got.br_blockcount - gotblkcnt;
6132 new.br_state = got.br_state;
6134 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6135 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6136 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6139 if (XFS_IS_CORRUPT(mp, i != 1)) {
6140 xfs_btree_mark_sick(cur);
6141 error = -EFSCORRUPTED;
6146 got.br_blockcount = gotblkcnt;
6147 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6150 logflags = XFS_ILOG_CORE;
6152 error = xfs_bmbt_update(cur, &got);
6156 logflags |= XFS_ILOG_DEXT;
6158 /* Add new extent */
6159 xfs_iext_next(ifp, &icur);
6160 xfs_iext_insert(ip, &icur, &new, 0);
6164 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6167 if (XFS_IS_CORRUPT(mp, i != 0)) {
6168 xfs_btree_mark_sick(cur);
6169 error = -EFSCORRUPTED;
6172 error = xfs_btree_insert(cur, &i);
6175 if (XFS_IS_CORRUPT(mp, i != 1)) {
6176 xfs_btree_mark_sick(cur);
6177 error = -EFSCORRUPTED;
6183 * Convert to a btree if necessary.
6185 if (xfs_bmap_needs_btree(ip, whichfork)) {
6186 int tmp_logflags; /* partial log flag return val */
6188 ASSERT(cur == NULL);
6189 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6190 &tmp_logflags, whichfork);
6191 logflags |= tmp_logflags;
6196 cur->bc_bmap.allocated = 0;
6197 xfs_btree_del_cursor(cur, error);
6201 xfs_trans_log_inode(tp, ip, logflags);
6205 /* Record a bmap intent. */
6208 struct xfs_trans *tp,
6209 enum xfs_bmap_intent_type type,
6210 struct xfs_inode *ip,
6212 struct xfs_bmbt_irec *bmap)
6214 struct xfs_bmap_intent *bi;
6216 if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6217 bmap->br_startblock == HOLESTARTBLOCK ||
6218 bmap->br_startblock == DELAYSTARTBLOCK)
6221 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
6222 INIT_LIST_HEAD(&bi->bi_list);
6225 bi->bi_whichfork = whichfork;
6226 bi->bi_bmap = *bmap;
6228 xfs_bmap_defer_add(tp, bi);
6231 /* Map an extent into a file. */
6233 xfs_bmap_map_extent(
6234 struct xfs_trans *tp,
6235 struct xfs_inode *ip,
6237 struct xfs_bmbt_irec *PREV)
6239 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
6242 /* Unmap an extent out of a file. */
6244 xfs_bmap_unmap_extent(
6245 struct xfs_trans *tp,
6246 struct xfs_inode *ip,
6248 struct xfs_bmbt_irec *PREV)
6250 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
6254 * Process one of the deferred bmap operations. We pass back the
6255 * btree cursor to maintain our lock on the bmapbt between calls.
6258 xfs_bmap_finish_one(
6259 struct xfs_trans *tp,
6260 struct xfs_bmap_intent *bi)
6262 struct xfs_bmbt_irec *bmap = &bi->bi_bmap;
6266 if (bi->bi_whichfork == XFS_ATTR_FORK)
6267 flags |= XFS_BMAPI_ATTRFORK;
6269 ASSERT(tp->t_highest_agno == NULLAGNUMBER);
6271 trace_xfs_bmap_deferred(bi);
6273 if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
6276 switch (bi->bi_type) {
6278 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6279 flags |= XFS_BMAPI_PREALLOC;
6280 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
6281 bmap->br_blockcount, bmap->br_startblock,
6283 bmap->br_blockcount = 0;
6285 case XFS_BMAP_UNMAP:
6286 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
6287 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6292 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
6293 error = -EFSCORRUPTED;
6299 /* Check that an extent does not have invalid flags or bad ranges. */
6301 xfs_bmap_validate_extent_raw(
6302 struct xfs_mount *mp,
6305 struct xfs_bmbt_irec *irec)
6307 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6308 return __this_address;
6310 if (rtfile && whichfork == XFS_DATA_FORK) {
6311 if (!xfs_verify_rtbext(mp, irec->br_startblock,
6312 irec->br_blockcount))
6313 return __this_address;
6315 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6316 irec->br_blockcount))
6317 return __this_address;
6319 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6320 return __this_address;
6325 xfs_bmap_intent_init_cache(void)
6327 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6328 sizeof(struct xfs_bmap_intent),
6331 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6335 xfs_bmap_intent_destroy_cache(void)
6337 kmem_cache_destroy(xfs_bmap_intent_cache);
6338 xfs_bmap_intent_cache = NULL;
6341 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6343 xfs_bmap_validate_extent(
6344 struct xfs_inode *ip,
6346 struct xfs_bmbt_irec *irec)
6348 return xfs_bmap_validate_extent_raw(ip->i_mount,
6349 XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6353 * Used in xfs_itruncate_extents(). This is the maximum number of extents
6354 * freed from a file in a single transaction.
6356 #define XFS_ITRUNC_MAX_EXTENTS 2
6359 * Unmap every extent in part of an inode's fork. We don't do any higher level
6360 * invalidation work at all.
6364 struct xfs_trans **tpp,
6365 struct xfs_inode *ip,
6367 xfs_fileoff_t startoff,
6368 xfs_fileoff_t endoff)
6370 xfs_filblks_t unmap_len = endoff - startoff + 1;
6373 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
6375 while (unmap_len > 0) {
6376 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6377 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6378 XFS_ITRUNC_MAX_EXTENTS);
6382 /* free the just unmapped extents */
6383 error = xfs_defer_finish(tpp);
6391 struct xfs_bmap_query_range {
6392 xfs_bmap_query_range_fn fn;
6396 /* Format btree record and pass to our callback. */
6398 xfs_bmap_query_range_helper(
6399 struct xfs_btree_cur *cur,
6400 const union xfs_btree_rec *rec,
6403 struct xfs_bmap_query_range *query = priv;
6404 struct xfs_bmbt_irec irec;
6407 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6408 fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6411 xfs_btree_mark_sick(cur);
6412 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6413 cur->bc_ino.whichfork, fa, &irec);
6416 return query->fn(cur, &irec, query->priv);
6419 /* Find all bmaps. */
6422 struct xfs_btree_cur *cur,
6423 xfs_bmap_query_range_fn fn,
6426 struct xfs_bmap_query_range query = {
6431 return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);