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"
42 #include "xfs_inode_util.h"
43 #include "xfs_rtgroup.h"
45 struct kmem_cache *xfs_bmap_intent_cache;
48 * Miscellaneous helper functions
52 * Compute and fill in the value of the maximum depth of a bmap btree
53 * in this filesystem. Done once, during mount.
56 xfs_bmap_compute_maxlevels(
57 xfs_mount_t *mp, /* file system mount structure */
58 int whichfork) /* data or attr fork */
60 uint64_t maxblocks; /* max blocks at this level */
61 xfs_extnum_t maxleafents; /* max leaf entries possible */
62 int level; /* btree level */
63 int maxrootrecs; /* max records in root block */
64 int minleafrecs; /* min records in leaf block */
65 int minnoderecs; /* min records in node block */
66 int sz; /* root block size */
69 * The maximum number of extents in a fork, hence the maximum number of
70 * leaf entries, is controlled by the size of the on-disk extent count.
72 * Note that we can no longer assume that if we are in ATTR1 that the
73 * fork offset of all the inodes will be
74 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
75 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
76 * but probably at various positions. Therefore, for both ATTR1 and
77 * ATTR2 we have to assume the worst case scenario of a minimum size
80 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
82 if (whichfork == XFS_DATA_FORK)
83 sz = xfs_bmdr_space_calc(MINDBTPTRS);
85 sz = xfs_bmdr_space_calc(MINABTPTRS);
87 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
88 minleafrecs = mp->m_bmap_dmnr[0];
89 minnoderecs = mp->m_bmap_dmnr[1];
90 maxblocks = howmany_64(maxleafents, minleafrecs);
91 for (level = 1; maxblocks > 1; level++) {
92 if (maxblocks <= maxrootrecs)
95 maxblocks = howmany_64(maxblocks, minnoderecs);
97 mp->m_bm_maxlevels[whichfork] = level;
98 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
102 xfs_bmap_compute_attr_offset(
103 struct xfs_mount *mp)
105 if (mp->m_sb.sb_inodesize == 256)
106 return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
107 return xfs_bmdr_space_calc(6 * MINABTPTRS);
110 STATIC int /* error */
112 struct xfs_btree_cur *cur,
113 struct xfs_bmbt_irec *irec,
114 int *stat) /* success/failure */
116 cur->bc_rec.b = *irec;
117 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
120 STATIC int /* error */
121 xfs_bmbt_lookup_first(
122 struct xfs_btree_cur *cur,
123 int *stat) /* success/failure */
125 cur->bc_rec.b.br_startoff = 0;
126 cur->bc_rec.b.br_startblock = 0;
127 cur->bc_rec.b.br_blockcount = 0;
128 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
132 * Check if the inode needs to be converted to btree format.
134 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
136 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
138 return whichfork != XFS_COW_FORK &&
139 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
140 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
144 * Check if the inode should be converted to extent format.
146 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
148 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
150 return whichfork != XFS_COW_FORK &&
151 ifp->if_format == XFS_DINODE_FMT_BTREE &&
152 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
156 * Update the record referred to by cur to the value given by irec
157 * This either works (return 0) or gets an EFSCORRUPTED error.
161 struct xfs_btree_cur *cur,
162 struct xfs_bmbt_irec *irec)
164 union xfs_btree_rec rec;
166 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
167 return xfs_btree_update(cur, &rec);
171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
175 xfs_bmap_worst_indlen(
176 xfs_inode_t *ip, /* incore inode pointer */
177 xfs_filblks_t len) /* delayed extent length */
179 int level; /* btree level number */
180 int maxrecs; /* maximum record count at this level */
181 xfs_mount_t *mp; /* mount structure */
182 xfs_filblks_t rval; /* return value */
185 maxrecs = mp->m_bmap_dmxr[0];
186 for (level = 0, rval = 0;
187 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
190 do_div(len, maxrecs);
193 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
196 maxrecs = mp->m_bmap_dmxr[1];
202 * Calculate the default attribute fork offset for newly created inodes.
205 xfs_default_attroffset(
206 struct xfs_inode *ip)
208 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
209 return roundup(sizeof(xfs_dev_t), 8);
210 return M_IGEO(ip->i_mount)->attr_fork_offset;
214 * Helper routine to reset inode i_forkoff field when switching attribute fork
215 * from local to extent format - we reset it where possible to make space
216 * available for inline data fork extents.
219 xfs_bmap_forkoff_reset(
223 if (whichfork == XFS_ATTR_FORK &&
224 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
225 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
226 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
228 if (dfl_forkoff > ip->i_forkoff)
229 ip->i_forkoff = dfl_forkoff;
235 struct xfs_mount *mp, /* file system mount point */
236 struct xfs_trans *tp, /* transaction pointer */
237 xfs_fsblock_t fsbno, /* file system block number */
238 struct xfs_buf **bpp) /* buffer for fsbno */
240 struct xfs_buf *bp; /* return value */
243 if (!xfs_verify_fsbno(mp, fsbno))
244 return -EFSCORRUPTED;
245 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
246 XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp,
249 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
256 STATIC struct xfs_buf *
258 struct xfs_btree_cur *cur,
261 struct xfs_log_item *lip;
267 for (i = 0; i < cur->bc_maxlevels; i++) {
268 if (!cur->bc_levels[i].bp)
270 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
271 return cur->bc_levels[i].bp;
274 /* Chase down all the log items to see if the bp is there */
275 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
276 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
278 if (bip->bli_item.li_type == XFS_LI_BUF &&
279 xfs_buf_daddr(bip->bli_buf) == bno)
288 struct xfs_btree_block *block,
294 __be64 *pp, *thispa; /* pointer to block address */
295 xfs_bmbt_key_t *prevp, *keyp;
297 ASSERT(be16_to_cpu(block->bb_level) > 0);
300 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
301 dmxr = mp->m_bmap_dmxr[0];
302 keyp = xfs_bmbt_key_addr(mp, block, i);
305 ASSERT(be64_to_cpu(prevp->br_startoff) <
306 be64_to_cpu(keyp->br_startoff));
311 * Compare the block numbers to see if there are dups.
314 pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz);
316 pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr);
318 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
320 thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz);
322 thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr);
323 if (*thispa == *pp) {
324 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
326 (unsigned long long)be64_to_cpu(*thispa));
327 xfs_err(mp, "%s: ptrs are equal in node\n",
329 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
336 * Check that the extents for the inode ip are in the right order in all
337 * btree leaves. THis becomes prohibitively expensive for large extent count
338 * files, so don't bother with inodes that have more than 10,000 extents in
339 * them. The btree record ordering checks will still be done, so for such large
340 * bmapbt constructs that is going to catch most corruptions.
343 xfs_bmap_check_leaf_extents(
344 struct xfs_btree_cur *cur, /* btree cursor or null */
345 xfs_inode_t *ip, /* incore inode pointer */
346 int whichfork) /* data or attr fork */
348 struct xfs_mount *mp = ip->i_mount;
349 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
350 struct xfs_btree_block *block; /* current btree block */
351 xfs_fsblock_t bno; /* block # of "block" */
352 struct xfs_buf *bp; /* buffer for "block" */
353 int error; /* error return value */
354 xfs_extnum_t i=0, j; /* index into the extents list */
355 int level; /* btree level, for checking */
356 __be64 *pp; /* pointer to block address */
357 xfs_bmbt_rec_t *ep; /* pointer to current extent */
358 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
359 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
362 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
365 /* skip large extent count inodes */
366 if (ip->i_df.if_nextents > 10000)
370 block = ifp->if_broot;
372 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
374 level = be16_to_cpu(block->bb_level);
376 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
377 pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes);
378 bno = be64_to_cpu(*pp);
380 ASSERT(bno != NULLFSBLOCK);
381 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
382 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
385 * Go down the tree until leaf level is reached, following the first
386 * pointer (leftmost) at each level.
388 while (level-- > 0) {
389 /* See if buf is in cur first */
391 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
394 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
395 if (xfs_metadata_is_sick(error))
396 xfs_btree_mark_sick(cur);
400 block = XFS_BUF_TO_BLOCK(bp);
405 * Check this block for basic sanity (increasing keys and
406 * no duplicate blocks).
409 xfs_check_block(block, mp, 0, 0);
410 pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]);
411 bno = be64_to_cpu(*pp);
412 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
413 xfs_btree_mark_sick(cur);
414 error = -EFSCORRUPTED;
419 xfs_trans_brelse(NULL, bp);
424 * Here with bp and block set to the leftmost leaf node in the tree.
429 * Loop over all leaf nodes checking that all extents are in the right order.
432 xfs_fsblock_t nextbno;
433 xfs_extnum_t num_recs;
436 num_recs = xfs_btree_get_numrecs(block);
439 * Read-ahead the next leaf block, if any.
442 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
445 * Check all the extents to make sure they are OK.
446 * If we had a previous block, the last entry should
447 * conform with the first entry in this one.
450 ep = xfs_bmbt_rec_addr(mp, block, 1);
452 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
453 xfs_bmbt_disk_get_blockcount(&last) <=
454 xfs_bmbt_disk_get_startoff(ep));
456 for (j = 1; j < num_recs; j++) {
457 nextp = xfs_bmbt_rec_addr(mp, block, j + 1);
458 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
459 xfs_bmbt_disk_get_blockcount(ep) <=
460 xfs_bmbt_disk_get_startoff(nextp));
468 xfs_trans_brelse(NULL, bp);
472 * If we've reached the end, stop.
474 if (bno == NULLFSBLOCK)
478 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
481 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
482 if (xfs_metadata_is_sick(error))
483 xfs_btree_mark_sick(cur);
487 block = XFS_BUF_TO_BLOCK(bp);
493 xfs_warn(mp, "%s: at error0", __func__);
495 xfs_trans_brelse(NULL, bp);
497 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
499 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
500 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
505 * Validate that the bmbt_irecs being returned from bmapi are valid
506 * given the caller's original parameters. Specifically check the
507 * ranges of the returned irecs to ensure that they only extend beyond
508 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
511 xfs_bmap_validate_ret(
515 xfs_bmbt_irec_t *mval,
519 int i; /* index to map values */
521 ASSERT(ret_nmap <= nmap);
523 for (i = 0; i < ret_nmap; i++) {
524 ASSERT(mval[i].br_blockcount > 0);
525 if (!(flags & XFS_BMAPI_ENTIRE)) {
526 ASSERT(mval[i].br_startoff >= bno);
527 ASSERT(mval[i].br_blockcount <= len);
528 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
531 ASSERT(mval[i].br_startoff < bno + len);
532 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
536 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
537 mval[i].br_startoff);
538 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
539 mval[i].br_startblock != HOLESTARTBLOCK);
540 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
541 mval[i].br_state == XFS_EXT_UNWRITTEN);
546 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
547 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
551 * Inode fork format manipulation functions
555 * Convert the inode format to extent format if it currently is in btree format,
556 * but the extent list is small enough that it fits into the extent format.
558 * Since the extents are already in-core, all we have to do is give up the space
559 * for the btree root and pitch the leaf block.
561 STATIC int /* error */
562 xfs_bmap_btree_to_extents(
563 struct xfs_trans *tp, /* transaction pointer */
564 struct xfs_inode *ip, /* incore inode pointer */
565 struct xfs_btree_cur *cur, /* btree cursor */
566 int *logflagsp, /* inode logging flags */
567 int whichfork) /* data or attr fork */
569 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
570 struct xfs_mount *mp = ip->i_mount;
571 struct xfs_btree_block *rblock = ifp->if_broot;
572 struct xfs_btree_block *cblock;/* child btree block */
573 xfs_fsblock_t cbno; /* child block number */
574 struct xfs_buf *cbp; /* child block's buffer */
575 int error; /* error return value */
576 __be64 *pp; /* ptr to block address */
577 struct xfs_owner_info oinfo;
579 /* check if we actually need the extent format first: */
580 if (!xfs_bmap_wants_extents(ip, whichfork))
584 ASSERT(whichfork != XFS_COW_FORK);
585 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
586 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
587 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
588 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1);
590 pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes);
591 cbno = be64_to_cpu(*pp);
593 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_verify_fsbno(mp, cbno))) {
594 xfs_btree_mark_sick(cur);
595 return -EFSCORRUPTED;
598 error = xfs_bmap_read_buf(mp, tp, cbno, &cbp);
599 if (xfs_metadata_is_sick(error))
600 xfs_btree_mark_sick(cur);
603 cblock = XFS_BUF_TO_BLOCK(cbp);
604 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
607 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
608 error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
609 XFS_AG_RESV_NONE, 0);
614 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
615 xfs_trans_binval(tp, cbp);
616 if (cur->bc_levels[0].bp == cbp)
617 cur->bc_levels[0].bp = NULL;
618 xfs_iroot_realloc(ip, -1, whichfork);
619 ASSERT(ifp->if_broot == NULL);
620 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
621 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
626 * Convert an extents-format file into a btree-format file.
627 * The new file will have a root block (in the inode) and a single child block.
629 STATIC int /* error */
630 xfs_bmap_extents_to_btree(
631 struct xfs_trans *tp, /* transaction pointer */
632 struct xfs_inode *ip, /* incore inode pointer */
633 struct xfs_btree_cur **curp, /* cursor returned to caller */
634 int wasdel, /* converting a delayed alloc */
635 int *logflagsp, /* inode logging flags */
636 int whichfork) /* data or attr fork */
638 struct xfs_btree_block *ablock; /* allocated (child) bt block */
639 struct xfs_buf *abp; /* buffer for ablock */
640 struct xfs_alloc_arg args; /* allocation arguments */
641 struct xfs_bmbt_rec *arp; /* child record pointer */
642 struct xfs_btree_block *block; /* btree root block */
643 struct xfs_btree_cur *cur; /* bmap btree cursor */
644 int error; /* error return value */
645 struct xfs_ifork *ifp; /* inode fork pointer */
646 struct xfs_bmbt_key *kp; /* root block key pointer */
647 struct xfs_mount *mp; /* mount structure */
648 xfs_bmbt_ptr_t *pp; /* root block address pointer */
649 struct xfs_iext_cursor icur;
650 struct xfs_bmbt_irec rec;
651 xfs_extnum_t cnt = 0;
654 ASSERT(whichfork != XFS_COW_FORK);
655 ifp = xfs_ifork_ptr(ip, whichfork);
656 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
659 * Make space in the inode incore. This needs to be undone if we fail
660 * to expand the root.
662 xfs_iroot_realloc(ip, 1, whichfork);
667 block = ifp->if_broot;
668 xfs_bmbt_init_block(ip, block, NULL, 1, 1);
670 * Need a cursor. Can't allocate until bb_level is filled in.
672 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
674 cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
676 * Convert to a btree with two levels, one record in root.
678 ifp->if_format = XFS_DINODE_FMT_BTREE;
679 memset(&args, 0, sizeof(args));
682 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
684 args.minlen = args.maxlen = args.prod = 1;
685 args.wasdel = wasdel;
687 error = xfs_alloc_vextent_start_ag(&args,
688 XFS_INO_TO_FSB(mp, ip->i_ino));
690 goto out_root_realloc;
693 * Allocation can't fail, the space was reserved.
695 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
697 goto out_root_realloc;
700 cur->bc_bmap.allocated++;
702 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
703 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
704 XFS_FSB_TO_DADDR(mp, args.fsbno),
705 mp->m_bsize, 0, &abp);
707 goto out_unreserve_dquot;
710 * Fill in the child block.
712 ablock = XFS_BUF_TO_BLOCK(abp);
713 xfs_bmbt_init_block(ip, ablock, abp, 0, 0);
715 for_each_xfs_iext(ifp, &icur, &rec) {
716 if (isnullstartblock(rec.br_startblock))
718 arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt);
719 xfs_bmbt_disk_set_all(arp, &rec);
722 ASSERT(cnt == ifp->if_nextents);
723 xfs_btree_set_numrecs(ablock, cnt);
726 * Fill in the root key and pointer.
728 kp = xfs_bmbt_key_addr(mp, block, 1);
729 arp = xfs_bmbt_rec_addr(mp, ablock, 1);
730 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
731 pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
732 be16_to_cpu(block->bb_level)));
733 *pp = cpu_to_be64(args.fsbno);
736 * Do all this logging at the end so that
737 * the root is at the right level.
739 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
740 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
741 ASSERT(*curp == NULL);
743 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
747 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
749 xfs_iroot_realloc(ip, -1, whichfork);
750 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
751 ASSERT(ifp->if_broot == NULL);
752 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
758 * Convert a local file to an extents file.
759 * This code is out of bounds for data forks of regular files,
760 * since the file data needs to get logged so things will stay consistent.
761 * (The bmap-level manipulations are ok, though).
764 xfs_bmap_local_to_extents_empty(
765 struct xfs_trans *tp,
766 struct xfs_inode *ip,
769 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
771 ASSERT(whichfork != XFS_COW_FORK);
772 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
773 ASSERT(ifp->if_bytes == 0);
774 ASSERT(ifp->if_nextents == 0);
776 xfs_bmap_forkoff_reset(ip, whichfork);
779 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
780 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
785 xfs_bmap_local_to_extents(
786 xfs_trans_t *tp, /* transaction pointer */
787 xfs_inode_t *ip, /* incore inode pointer */
788 xfs_extlen_t total, /* total blocks needed by transaction */
789 int *logflagsp, /* inode logging flags */
791 void (*init_fn)(struct xfs_trans *tp,
793 struct xfs_inode *ip,
794 struct xfs_ifork *ifp, void *priv),
798 int flags; /* logging flags returned */
799 struct xfs_ifork *ifp; /* inode fork pointer */
800 xfs_alloc_arg_t args; /* allocation arguments */
801 struct xfs_buf *bp; /* buffer for extent block */
802 struct xfs_bmbt_irec rec;
803 struct xfs_iext_cursor icur;
806 * We don't want to deal with the case of keeping inode data inline yet.
807 * So sending the data fork of a regular inode is invalid.
809 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
810 ifp = xfs_ifork_ptr(ip, whichfork);
811 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
813 if (!ifp->if_bytes) {
814 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
815 flags = XFS_ILOG_CORE;
821 memset(&args, 0, sizeof(args));
823 args.mp = ip->i_mount;
825 args.minlen = args.maxlen = args.prod = 1;
826 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
829 * Allocate a block. We know we need only one, since the
830 * file currently fits in an inode.
833 args.minlen = args.maxlen = args.prod = 1;
834 error = xfs_alloc_vextent_start_ag(&args,
835 XFS_INO_TO_FSB(args.mp, ip->i_ino));
839 /* Can't fail, the space was reserved. */
840 ASSERT(args.fsbno != NULLFSBLOCK);
841 ASSERT(args.len == 1);
842 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
843 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
844 args.mp->m_bsize, 0, &bp);
849 * Initialize the block, copy the data and log the remote buffer.
851 * The callout is responsible for logging because the remote format
852 * might differ from the local format and thus we don't know how much to
853 * log here. Note that init_fn must also set the buffer log item type
856 init_fn(tp, bp, ip, ifp, priv);
858 /* account for the change in fork size */
859 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
860 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
861 flags |= XFS_ILOG_CORE;
867 rec.br_startblock = args.fsbno;
868 rec.br_blockcount = 1;
869 rec.br_state = XFS_EXT_NORM;
870 xfs_iext_first(ifp, &icur);
871 xfs_iext_insert(ip, &icur, &rec, 0);
873 ifp->if_nextents = 1;
875 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
876 flags |= xfs_ilog_fext(whichfork);
884 * Called from xfs_bmap_add_attrfork to handle btree format files.
886 STATIC int /* error */
887 xfs_bmap_add_attrfork_btree(
888 xfs_trans_t *tp, /* transaction pointer */
889 xfs_inode_t *ip, /* incore inode pointer */
890 int *flags) /* inode logging flags */
892 struct xfs_btree_block *block = ip->i_df.if_broot;
893 struct xfs_btree_cur *cur; /* btree cursor */
894 int error; /* error return value */
895 xfs_mount_t *mp; /* file system mount struct */
896 int stat; /* newroot status */
900 if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip))
901 *flags |= XFS_ILOG_DBROOT;
903 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
904 error = xfs_bmbt_lookup_first(cur, &stat);
907 /* must be at least one entry */
908 if (XFS_IS_CORRUPT(mp, stat != 1)) {
909 xfs_btree_mark_sick(cur);
910 error = -EFSCORRUPTED;
913 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
916 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
919 cur->bc_bmap.allocated = 0;
920 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
924 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
929 * Called from xfs_bmap_add_attrfork to handle extents format files.
931 STATIC int /* error */
932 xfs_bmap_add_attrfork_extents(
933 struct xfs_trans *tp, /* transaction pointer */
934 struct xfs_inode *ip, /* incore inode pointer */
935 int *flags) /* inode logging flags */
937 struct xfs_btree_cur *cur; /* bmap btree cursor */
938 int error; /* error return value */
940 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
941 xfs_inode_data_fork_size(ip))
944 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
947 cur->bc_bmap.allocated = 0;
948 xfs_btree_del_cursor(cur, error);
954 * Called from xfs_bmap_add_attrfork to handle local format files. Each
955 * different data fork content type needs a different callout to do the
956 * conversion. Some are basic and only require special block initialisation
957 * callouts for the data formating, others (directories) are so specialised they
958 * handle everything themselves.
960 * XXX (dgc): investigate whether directory conversion can use the generic
961 * formatting callout. It should be possible - it's just a very complex
964 STATIC int /* error */
965 xfs_bmap_add_attrfork_local(
966 struct xfs_trans *tp, /* transaction pointer */
967 struct xfs_inode *ip, /* incore inode pointer */
968 int *flags) /* inode logging flags */
970 struct xfs_da_args dargs; /* args for dir/attr code */
972 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
975 if (S_ISDIR(VFS_I(ip)->i_mode)) {
976 memset(&dargs, 0, sizeof(dargs));
977 dargs.geo = ip->i_mount->m_dir_geo;
979 dargs.total = dargs.geo->fsbcount;
980 dargs.whichfork = XFS_DATA_FORK;
982 dargs.owner = ip->i_ino;
983 return xfs_dir2_sf_to_block(&dargs);
986 if (S_ISLNK(VFS_I(ip)->i_mode))
987 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
988 XFS_DATA_FORK, xfs_symlink_local_to_remote,
991 /* should only be called for types that support local format data */
993 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
994 return -EFSCORRUPTED;
998 * Set an inode attr fork offset based on the format of the data fork.
1001 xfs_bmap_set_attrforkoff(
1002 struct xfs_inode *ip,
1006 int default_size = xfs_default_attroffset(ip) >> 3;
1008 switch (ip->i_df.if_format) {
1009 case XFS_DINODE_FMT_DEV:
1010 ip->i_forkoff = default_size;
1012 case XFS_DINODE_FMT_LOCAL:
1013 case XFS_DINODE_FMT_EXTENTS:
1014 case XFS_DINODE_FMT_BTREE:
1015 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1017 ip->i_forkoff = default_size;
1018 else if (xfs_has_attr2(ip->i_mount) && version)
1030 * Convert inode from non-attributed to attributed. Caller must hold the
1031 * ILOCK_EXCL and the file cannot have an attr fork.
1033 int /* error code */
1034 xfs_bmap_add_attrfork(
1035 struct xfs_trans *tp,
1036 struct xfs_inode *ip, /* incore inode pointer */
1037 int size, /* space new attribute needs */
1038 int rsvd) /* xact may use reserved blks */
1040 struct xfs_mount *mp = tp->t_mountp;
1041 int version = 1; /* superblock attr version */
1042 int logflags; /* logging flags */
1043 int error; /* error return value */
1045 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1046 if (xfs_is_metadir_inode(ip))
1047 ASSERT(XFS_IS_DQDETACHED(ip));
1049 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1050 ASSERT(!xfs_inode_has_attr_fork(ip));
1052 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1053 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1057 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
1059 switch (ip->i_df.if_format) {
1060 case XFS_DINODE_FMT_LOCAL:
1061 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1063 case XFS_DINODE_FMT_EXTENTS:
1064 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1066 case XFS_DINODE_FMT_BTREE:
1067 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1074 xfs_trans_log_inode(tp, ip, logflags);
1077 if (!xfs_has_attr(mp) ||
1078 (!xfs_has_attr2(mp) && version == 2)) {
1079 bool log_sb = false;
1081 spin_lock(&mp->m_sb_lock);
1082 if (!xfs_has_attr(mp)) {
1086 if (!xfs_has_attr2(mp) && version == 2) {
1090 spin_unlock(&mp->m_sb_lock);
1099 * Internal and external extent tree search functions.
1102 struct xfs_iread_state {
1103 struct xfs_iext_cursor icur;
1104 xfs_extnum_t loaded;
1108 xfs_bmap_complain_bad_rec(
1109 struct xfs_inode *ip,
1112 const struct xfs_bmbt_irec *irec)
1114 struct xfs_mount *mp = ip->i_mount;
1115 const char *forkname;
1117 switch (whichfork) {
1118 case XFS_DATA_FORK: forkname = "data"; break;
1119 case XFS_ATTR_FORK: forkname = "attr"; break;
1120 case XFS_COW_FORK: forkname = "CoW"; break;
1121 default: forkname = "???"; break;
1125 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1126 ip->i_ino, forkname, fa);
1128 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1129 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1132 return -EFSCORRUPTED;
1135 /* Stuff every bmbt record from this block into the incore extent map. */
1137 xfs_iread_bmbt_block(
1138 struct xfs_btree_cur *cur,
1142 struct xfs_iread_state *ir = priv;
1143 struct xfs_mount *mp = cur->bc_mp;
1144 struct xfs_inode *ip = cur->bc_ino.ip;
1145 struct xfs_btree_block *block;
1147 struct xfs_bmbt_rec *frp;
1148 xfs_extnum_t num_recs;
1150 int whichfork = cur->bc_ino.whichfork;
1151 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1153 block = xfs_btree_get_block(cur, level, &bp);
1155 /* Abort if we find more records than nextents. */
1156 num_recs = xfs_btree_get_numrecs(block);
1157 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1158 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1159 (unsigned long long)ip->i_ino);
1160 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1161 sizeof(*block), __this_address);
1162 xfs_bmap_mark_sick(ip, whichfork);
1163 return -EFSCORRUPTED;
1166 /* Copy records into the incore cache. */
1167 frp = xfs_bmbt_rec_addr(mp, block, 1);
1168 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1169 struct xfs_bmbt_irec new;
1172 xfs_bmbt_disk_get_all(frp, &new);
1173 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1175 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1176 "xfs_iread_extents(2)", frp,
1178 xfs_bmap_mark_sick(ip, whichfork);
1179 return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1182 xfs_iext_insert(ip, &ir->icur, &new,
1183 xfs_bmap_fork_to_state(whichfork));
1184 trace_xfs_read_extent(ip, &ir->icur,
1185 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1186 xfs_iext_next(ifp, &ir->icur);
1193 * Read in extents from a btree-format inode.
1197 struct xfs_trans *tp,
1198 struct xfs_inode *ip,
1201 struct xfs_iread_state ir;
1202 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1203 struct xfs_mount *mp = ip->i_mount;
1204 struct xfs_btree_cur *cur;
1207 if (!xfs_need_iread_extents(ifp))
1210 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1213 xfs_iext_first(ifp, &ir.icur);
1214 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1215 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1216 XFS_BTREE_VISIT_RECORDS, &ir);
1217 xfs_btree_del_cursor(cur, error);
1221 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1222 xfs_bmap_mark_sick(ip, whichfork);
1223 error = -EFSCORRUPTED;
1226 ASSERT(ir.loaded == xfs_iext_count(ifp));
1228 * Use release semantics so that we can use acquire semantics in
1229 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1232 smp_store_release(&ifp->if_needextents, 0);
1235 if (xfs_metadata_is_sick(error))
1236 xfs_bmap_mark_sick(ip, whichfork);
1237 xfs_iext_destroy(ifp);
1242 * Returns the relative block number of the first unused block(s) in the given
1243 * fork with at least "len" logically contiguous blocks free. This is the
1244 * lowest-address hole if the fork has holes, else the first block past the end
1245 * of fork. Return 0 if the fork is currently local (in-inode).
1248 xfs_bmap_first_unused(
1249 struct xfs_trans *tp, /* transaction pointer */
1250 struct xfs_inode *ip, /* incore inode */
1251 xfs_extlen_t len, /* size of hole to find */
1252 xfs_fileoff_t *first_unused, /* unused block */
1253 int whichfork) /* data or attr fork */
1255 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1256 struct xfs_bmbt_irec got;
1257 struct xfs_iext_cursor icur;
1258 xfs_fileoff_t lastaddr = 0;
1259 xfs_fileoff_t lowest, max;
1262 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1267 ASSERT(xfs_ifork_has_extents(ifp));
1269 error = xfs_iread_extents(tp, ip, whichfork);
1273 lowest = max = *first_unused;
1274 for_each_xfs_iext(ifp, &icur, &got) {
1276 * See if the hole before this extent will work.
1278 if (got.br_startoff >= lowest + len &&
1279 got.br_startoff - max >= len)
1281 lastaddr = got.br_startoff + got.br_blockcount;
1282 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1285 *first_unused = max;
1290 * Returns the file-relative block number of the last block - 1 before
1291 * last_block (input value) in the file.
1292 * This is not based on i_size, it is based on the extent records.
1293 * Returns 0 for local files, as they do not have extent records.
1296 xfs_bmap_last_before(
1297 struct xfs_trans *tp, /* transaction pointer */
1298 struct xfs_inode *ip, /* incore inode */
1299 xfs_fileoff_t *last_block, /* last block */
1300 int whichfork) /* data or attr fork */
1302 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1303 struct xfs_bmbt_irec got;
1304 struct xfs_iext_cursor icur;
1307 switch (ifp->if_format) {
1308 case XFS_DINODE_FMT_LOCAL:
1311 case XFS_DINODE_FMT_BTREE:
1312 case XFS_DINODE_FMT_EXTENTS:
1316 xfs_bmap_mark_sick(ip, whichfork);
1317 return -EFSCORRUPTED;
1320 error = xfs_iread_extents(tp, ip, whichfork);
1324 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1330 xfs_bmap_last_extent(
1331 struct xfs_trans *tp,
1332 struct xfs_inode *ip,
1334 struct xfs_bmbt_irec *rec,
1337 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1338 struct xfs_iext_cursor icur;
1341 error = xfs_iread_extents(tp, ip, whichfork);
1345 xfs_iext_last(ifp, &icur);
1346 if (!xfs_iext_get_extent(ifp, &icur, rec))
1354 * Check the last inode extent to determine whether this allocation will result
1355 * in blocks being allocated at the end of the file. When we allocate new data
1356 * blocks at the end of the file which do not start at the previous data block,
1357 * we will try to align the new blocks at stripe unit boundaries.
1359 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1360 * at, or past the EOF.
1364 struct xfs_bmalloca *bma,
1367 struct xfs_bmbt_irec rec;
1372 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1383 * Check if we are allocation or past the last extent, or at least into
1384 * the last delayed allocated extent.
1386 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1387 (bma->offset >= rec.br_startoff &&
1388 isnullstartblock(rec.br_startblock));
1393 * Returns the file-relative block number of the first block past eof in
1394 * the file. This is not based on i_size, it is based on the extent records.
1395 * Returns 0 for local files, as they do not have extent records.
1398 xfs_bmap_last_offset(
1399 struct xfs_inode *ip,
1400 xfs_fileoff_t *last_block,
1403 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1404 struct xfs_bmbt_irec rec;
1410 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1413 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
1414 xfs_bmap_mark_sick(ip, whichfork);
1415 return -EFSCORRUPTED;
1418 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1419 if (error || is_empty)
1422 *last_block = rec.br_startoff + rec.br_blockcount;
1427 * Extent tree manipulation functions used during allocation.
1431 xfs_bmap_same_rtgroup(
1432 struct xfs_inode *ip,
1434 struct xfs_bmbt_irec *left,
1435 struct xfs_bmbt_irec *right)
1437 struct xfs_mount *mp = ip->i_mount;
1439 if (xfs_ifork_is_realtime(ip, whichfork) && xfs_has_rtgroups(mp)) {
1440 if (xfs_rtb_to_rgno(mp, left->br_startblock) !=
1441 xfs_rtb_to_rgno(mp, right->br_startblock))
1449 * Convert a delayed allocation to a real allocation.
1451 STATIC int /* error */
1452 xfs_bmap_add_extent_delay_real(
1453 struct xfs_bmalloca *bma,
1456 struct xfs_mount *mp = bma->ip->i_mount;
1457 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
1458 struct xfs_bmbt_irec *new = &bma->got;
1459 int error; /* error return value */
1460 int i; /* temp state */
1461 xfs_fileoff_t new_endoff; /* end offset of new entry */
1462 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1463 /* left is 0, right is 1, prev is 2 */
1464 int rval=0; /* return value (logging flags) */
1465 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1466 xfs_filblks_t da_new; /* new count del alloc blocks used */
1467 xfs_filblks_t da_old; /* old count del alloc blocks used */
1468 xfs_filblks_t temp=0; /* value for da_new calculations */
1469 int tmp_rval; /* partial logging flags */
1470 struct xfs_bmbt_irec old;
1472 ASSERT(whichfork != XFS_ATTR_FORK);
1473 ASSERT(!isnullstartblock(new->br_startblock));
1474 ASSERT(!bma->cur || (bma->cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
1476 XFS_STATS_INC(mp, xs_add_exlist);
1483 * Set up a bunch of variables to make the tests simpler.
1485 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1486 new_endoff = new->br_startoff + new->br_blockcount;
1487 ASSERT(isnullstartblock(PREV.br_startblock));
1488 ASSERT(PREV.br_startoff <= new->br_startoff);
1489 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1491 da_old = startblockval(PREV.br_startblock);
1495 * Set flags determining what part of the previous delayed allocation
1496 * extent is being replaced by a real allocation.
1498 if (PREV.br_startoff == new->br_startoff)
1499 state |= BMAP_LEFT_FILLING;
1500 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1501 state |= BMAP_RIGHT_FILLING;
1504 * Check and set flags if this segment has a left neighbor.
1505 * Don't set contiguous if the combined extent would be too large.
1507 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1508 state |= BMAP_LEFT_VALID;
1509 if (isnullstartblock(LEFT.br_startblock))
1510 state |= BMAP_LEFT_DELAY;
1513 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1514 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1515 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1516 LEFT.br_state == new->br_state &&
1517 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1518 xfs_bmap_same_rtgroup(bma->ip, whichfork, &LEFT, new))
1519 state |= BMAP_LEFT_CONTIG;
1522 * Check and set flags if this segment has a right neighbor.
1523 * Don't set contiguous if the combined extent would be too large.
1524 * Also check for all-three-contiguous being too large.
1526 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1527 state |= BMAP_RIGHT_VALID;
1528 if (isnullstartblock(RIGHT.br_startblock))
1529 state |= BMAP_RIGHT_DELAY;
1532 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1533 new_endoff == RIGHT.br_startoff &&
1534 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1535 new->br_state == RIGHT.br_state &&
1536 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1537 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1538 BMAP_RIGHT_FILLING)) !=
1539 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1540 BMAP_RIGHT_FILLING) ||
1541 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1542 <= XFS_MAX_BMBT_EXTLEN) &&
1543 xfs_bmap_same_rtgroup(bma->ip, whichfork, new, &RIGHT))
1544 state |= BMAP_RIGHT_CONTIG;
1548 * Switch out based on the FILLING and CONTIG state bits.
1550 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1551 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1552 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1553 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1555 * Filling in all of a previously delayed allocation extent.
1556 * The left and right neighbors are both contiguous with new.
1558 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1560 xfs_iext_remove(bma->ip, &bma->icur, state);
1561 xfs_iext_remove(bma->ip, &bma->icur, state);
1562 xfs_iext_prev(ifp, &bma->icur);
1563 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1566 if (bma->cur == NULL)
1567 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1569 rval = XFS_ILOG_CORE;
1570 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1573 if (XFS_IS_CORRUPT(mp, i != 1)) {
1574 xfs_btree_mark_sick(bma->cur);
1575 error = -EFSCORRUPTED;
1578 error = xfs_btree_delete(bma->cur, &i);
1581 if (XFS_IS_CORRUPT(mp, i != 1)) {
1582 xfs_btree_mark_sick(bma->cur);
1583 error = -EFSCORRUPTED;
1586 error = xfs_btree_decrement(bma->cur, 0, &i);
1589 if (XFS_IS_CORRUPT(mp, i != 1)) {
1590 xfs_btree_mark_sick(bma->cur);
1591 error = -EFSCORRUPTED;
1594 error = xfs_bmbt_update(bma->cur, &LEFT);
1598 ASSERT(da_new <= da_old);
1601 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1603 * Filling in all of a previously delayed allocation extent.
1604 * The left neighbor is contiguous, the right is not.
1607 LEFT.br_blockcount += PREV.br_blockcount;
1609 xfs_iext_remove(bma->ip, &bma->icur, state);
1610 xfs_iext_prev(ifp, &bma->icur);
1611 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1613 if (bma->cur == NULL)
1614 rval = XFS_ILOG_DEXT;
1617 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1620 if (XFS_IS_CORRUPT(mp, i != 1)) {
1621 xfs_btree_mark_sick(bma->cur);
1622 error = -EFSCORRUPTED;
1625 error = xfs_bmbt_update(bma->cur, &LEFT);
1629 ASSERT(da_new <= da_old);
1632 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1634 * Filling in all of a previously delayed allocation extent.
1635 * The right neighbor is contiguous, the left is not. Take care
1636 * with delay -> unwritten extent allocation here because the
1637 * delalloc record we are overwriting is always written.
1639 PREV.br_startblock = new->br_startblock;
1640 PREV.br_blockcount += RIGHT.br_blockcount;
1641 PREV.br_state = new->br_state;
1643 xfs_iext_next(ifp, &bma->icur);
1644 xfs_iext_remove(bma->ip, &bma->icur, state);
1645 xfs_iext_prev(ifp, &bma->icur);
1646 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1648 if (bma->cur == NULL)
1649 rval = XFS_ILOG_DEXT;
1652 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1655 if (XFS_IS_CORRUPT(mp, i != 1)) {
1656 xfs_btree_mark_sick(bma->cur);
1657 error = -EFSCORRUPTED;
1660 error = xfs_bmbt_update(bma->cur, &PREV);
1664 ASSERT(da_new <= da_old);
1667 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1669 * Filling in all of a previously delayed allocation extent.
1670 * Neither the left nor right neighbors are contiguous with
1673 PREV.br_startblock = new->br_startblock;
1674 PREV.br_state = new->br_state;
1675 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1678 if (bma->cur == NULL)
1679 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1681 rval = XFS_ILOG_CORE;
1682 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1685 if (XFS_IS_CORRUPT(mp, i != 0)) {
1686 xfs_btree_mark_sick(bma->cur);
1687 error = -EFSCORRUPTED;
1690 error = xfs_btree_insert(bma->cur, &i);
1693 if (XFS_IS_CORRUPT(mp, i != 1)) {
1694 xfs_btree_mark_sick(bma->cur);
1695 error = -EFSCORRUPTED;
1699 ASSERT(da_new <= da_old);
1702 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1704 * Filling in the first part of a previous delayed allocation.
1705 * The left neighbor is contiguous.
1708 temp = PREV.br_blockcount - new->br_blockcount;
1709 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1710 startblockval(PREV.br_startblock));
1712 LEFT.br_blockcount += new->br_blockcount;
1714 PREV.br_blockcount = temp;
1715 PREV.br_startoff += new->br_blockcount;
1716 PREV.br_startblock = nullstartblock(da_new);
1718 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1719 xfs_iext_prev(ifp, &bma->icur);
1720 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1722 if (bma->cur == NULL)
1723 rval = XFS_ILOG_DEXT;
1726 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1729 if (XFS_IS_CORRUPT(mp, i != 1)) {
1730 xfs_btree_mark_sick(bma->cur);
1731 error = -EFSCORRUPTED;
1734 error = xfs_bmbt_update(bma->cur, &LEFT);
1738 ASSERT(da_new <= da_old);
1741 case BMAP_LEFT_FILLING:
1743 * Filling in the first part of a previous delayed allocation.
1744 * The left neighbor is not contiguous.
1746 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1749 if (bma->cur == NULL)
1750 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1752 rval = XFS_ILOG_CORE;
1753 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1756 if (XFS_IS_CORRUPT(mp, i != 0)) {
1757 xfs_btree_mark_sick(bma->cur);
1758 error = -EFSCORRUPTED;
1761 error = xfs_btree_insert(bma->cur, &i);
1764 if (XFS_IS_CORRUPT(mp, i != 1)) {
1765 xfs_btree_mark_sick(bma->cur);
1766 error = -EFSCORRUPTED;
1771 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1772 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1773 &bma->cur, 1, &tmp_rval, whichfork);
1779 temp = PREV.br_blockcount - new->br_blockcount;
1780 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1781 startblockval(PREV.br_startblock) -
1782 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
1784 PREV.br_startoff = new_endoff;
1785 PREV.br_blockcount = temp;
1786 PREV.br_startblock = nullstartblock(da_new);
1787 xfs_iext_next(ifp, &bma->icur);
1788 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1789 xfs_iext_prev(ifp, &bma->icur);
1792 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1794 * Filling in the last part of a previous delayed allocation.
1795 * The right neighbor is contiguous with the new allocation.
1798 RIGHT.br_startoff = new->br_startoff;
1799 RIGHT.br_startblock = new->br_startblock;
1800 RIGHT.br_blockcount += new->br_blockcount;
1802 if (bma->cur == NULL)
1803 rval = XFS_ILOG_DEXT;
1806 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1809 if (XFS_IS_CORRUPT(mp, i != 1)) {
1810 xfs_btree_mark_sick(bma->cur);
1811 error = -EFSCORRUPTED;
1814 error = xfs_bmbt_update(bma->cur, &RIGHT);
1819 temp = PREV.br_blockcount - new->br_blockcount;
1820 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1821 startblockval(PREV.br_startblock));
1823 PREV.br_blockcount = temp;
1824 PREV.br_startblock = nullstartblock(da_new);
1826 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1827 xfs_iext_next(ifp, &bma->icur);
1828 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1829 ASSERT(da_new <= da_old);
1832 case BMAP_RIGHT_FILLING:
1834 * Filling in the last part of a previous delayed allocation.
1835 * The right neighbor is not contiguous.
1837 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1840 if (bma->cur == NULL)
1841 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1843 rval = XFS_ILOG_CORE;
1844 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1847 if (XFS_IS_CORRUPT(mp, i != 0)) {
1848 xfs_btree_mark_sick(bma->cur);
1849 error = -EFSCORRUPTED;
1852 error = xfs_btree_insert(bma->cur, &i);
1855 if (XFS_IS_CORRUPT(mp, i != 1)) {
1856 xfs_btree_mark_sick(bma->cur);
1857 error = -EFSCORRUPTED;
1862 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1863 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1864 &bma->cur, 1, &tmp_rval, whichfork);
1870 temp = PREV.br_blockcount - new->br_blockcount;
1871 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1872 startblockval(PREV.br_startblock) -
1873 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
1875 PREV.br_startblock = nullstartblock(da_new);
1876 PREV.br_blockcount = temp;
1877 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1878 xfs_iext_next(ifp, &bma->icur);
1879 ASSERT(da_new <= da_old);
1884 * Filling in the middle part of a previous delayed allocation.
1885 * Contiguity is impossible here.
1886 * This case is avoided almost all the time.
1888 * We start with a delayed allocation:
1890 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1893 * and we are allocating:
1894 * +rrrrrrrrrrrrrrrrr+
1897 * and we set it up for insertion as:
1898 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1900 * PREV @ idx LEFT RIGHT
1901 * inserted at idx + 1
1905 /* LEFT is the new middle */
1908 /* RIGHT is the new right */
1909 RIGHT.br_state = PREV.br_state;
1910 RIGHT.br_startoff = new_endoff;
1911 RIGHT.br_blockcount =
1912 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1913 RIGHT.br_startblock =
1914 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1915 RIGHT.br_blockcount));
1918 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1919 PREV.br_startblock =
1920 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1921 PREV.br_blockcount));
1922 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1924 xfs_iext_next(ifp, &bma->icur);
1925 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1926 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1929 if (bma->cur == NULL)
1930 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1932 rval = XFS_ILOG_CORE;
1933 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1936 if (XFS_IS_CORRUPT(mp, i != 0)) {
1937 xfs_btree_mark_sick(bma->cur);
1938 error = -EFSCORRUPTED;
1941 error = xfs_btree_insert(bma->cur, &i);
1944 if (XFS_IS_CORRUPT(mp, i != 1)) {
1945 xfs_btree_mark_sick(bma->cur);
1946 error = -EFSCORRUPTED;
1951 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1952 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1953 &bma->cur, 1, &tmp_rval, whichfork);
1959 da_new = startblockval(PREV.br_startblock) +
1960 startblockval(RIGHT.br_startblock);
1963 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1964 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1965 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1966 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1967 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1968 case BMAP_LEFT_CONTIG:
1969 case BMAP_RIGHT_CONTIG:
1971 * These cases are all impossible.
1976 /* add reverse mapping unless caller opted out */
1977 if (!(bma->flags & XFS_BMAPI_NORMAP))
1978 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1980 /* convert to a btree if necessary */
1981 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1982 int tmp_logflags; /* partial log flag return val */
1984 ASSERT(bma->cur == NULL);
1985 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1986 &bma->cur, da_old > 0, &tmp_logflags,
1988 bma->logflags |= tmp_logflags;
1993 if (da_new != da_old)
1994 xfs_mod_delalloc(bma->ip, 0, (int64_t)da_new - da_old);
1997 da_new += bma->cur->bc_bmap.allocated;
1998 bma->cur->bc_bmap.allocated = 0;
2001 /* adjust for changes in reserved delayed indirect blocks */
2002 if (da_new < da_old)
2003 xfs_add_fdblocks(mp, da_old - da_new);
2004 else if (da_new > da_old)
2005 error = xfs_dec_fdblocks(mp, da_new - da_old, true);
2007 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2009 if (whichfork != XFS_COW_FORK)
2010 bma->logflags |= rval;
2018 * Convert an unwritten allocation to a real allocation or vice versa.
2021 xfs_bmap_add_extent_unwritten_real(
2022 struct xfs_trans *tp,
2023 xfs_inode_t *ip, /* incore inode pointer */
2025 struct xfs_iext_cursor *icur,
2026 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
2027 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2028 int *logflagsp) /* inode logging flags */
2030 struct xfs_btree_cur *cur; /* btree cursor */
2031 int error; /* error return value */
2032 int i; /* temp state */
2033 struct xfs_ifork *ifp; /* inode fork pointer */
2034 xfs_fileoff_t new_endoff; /* end offset of new entry */
2035 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2036 /* left is 0, right is 1, prev is 2 */
2037 int rval=0; /* return value (logging flags) */
2038 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2039 struct xfs_mount *mp = ip->i_mount;
2040 struct xfs_bmbt_irec old;
2045 ifp = xfs_ifork_ptr(ip, whichfork);
2047 ASSERT(!isnullstartblock(new->br_startblock));
2049 XFS_STATS_INC(mp, xs_add_exlist);
2056 * Set up a bunch of variables to make the tests simpler.
2059 xfs_iext_get_extent(ifp, icur, &PREV);
2060 ASSERT(new->br_state != PREV.br_state);
2061 new_endoff = new->br_startoff + new->br_blockcount;
2062 ASSERT(PREV.br_startoff <= new->br_startoff);
2063 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2066 * Set flags determining what part of the previous oldext allocation
2067 * extent is being replaced by a newext allocation.
2069 if (PREV.br_startoff == new->br_startoff)
2070 state |= BMAP_LEFT_FILLING;
2071 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2072 state |= BMAP_RIGHT_FILLING;
2075 * Check and set flags if this segment has a left neighbor.
2076 * Don't set contiguous if the combined extent would be too large.
2078 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2079 state |= BMAP_LEFT_VALID;
2080 if (isnullstartblock(LEFT.br_startblock))
2081 state |= BMAP_LEFT_DELAY;
2084 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2085 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2086 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2087 LEFT.br_state == new->br_state &&
2088 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2089 xfs_bmap_same_rtgroup(ip, whichfork, &LEFT, new))
2090 state |= BMAP_LEFT_CONTIG;
2093 * Check and set flags if this segment has a right neighbor.
2094 * Don't set contiguous if the combined extent would be too large.
2095 * Also check for all-three-contiguous being too large.
2097 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2098 state |= BMAP_RIGHT_VALID;
2099 if (isnullstartblock(RIGHT.br_startblock))
2100 state |= BMAP_RIGHT_DELAY;
2103 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2104 new_endoff == RIGHT.br_startoff &&
2105 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2106 new->br_state == RIGHT.br_state &&
2107 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2108 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2109 BMAP_RIGHT_FILLING)) !=
2110 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2111 BMAP_RIGHT_FILLING) ||
2112 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2113 <= XFS_MAX_BMBT_EXTLEN) &&
2114 xfs_bmap_same_rtgroup(ip, whichfork, new, &RIGHT))
2115 state |= BMAP_RIGHT_CONTIG;
2118 * Switch out based on the FILLING and CONTIG state bits.
2120 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2121 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2122 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2123 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2125 * Setting all of a previous oldext extent to newext.
2126 * The left and right neighbors are both contiguous with new.
2128 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2130 xfs_iext_remove(ip, icur, state);
2131 xfs_iext_remove(ip, icur, state);
2132 xfs_iext_prev(ifp, icur);
2133 xfs_iext_update_extent(ip, state, icur, &LEFT);
2134 ifp->if_nextents -= 2;
2136 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2138 rval = XFS_ILOG_CORE;
2139 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2142 if (XFS_IS_CORRUPT(mp, i != 1)) {
2143 xfs_btree_mark_sick(cur);
2144 error = -EFSCORRUPTED;
2147 if ((error = xfs_btree_delete(cur, &i)))
2149 if (XFS_IS_CORRUPT(mp, i != 1)) {
2150 xfs_btree_mark_sick(cur);
2151 error = -EFSCORRUPTED;
2154 if ((error = xfs_btree_decrement(cur, 0, &i)))
2156 if (XFS_IS_CORRUPT(mp, i != 1)) {
2157 xfs_btree_mark_sick(cur);
2158 error = -EFSCORRUPTED;
2161 if ((error = xfs_btree_delete(cur, &i)))
2163 if (XFS_IS_CORRUPT(mp, i != 1)) {
2164 xfs_btree_mark_sick(cur);
2165 error = -EFSCORRUPTED;
2168 if ((error = xfs_btree_decrement(cur, 0, &i)))
2170 if (XFS_IS_CORRUPT(mp, i != 1)) {
2171 xfs_btree_mark_sick(cur);
2172 error = -EFSCORRUPTED;
2175 error = xfs_bmbt_update(cur, &LEFT);
2181 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2183 * Setting all of a previous oldext extent to newext.
2184 * The left neighbor is contiguous, the right is not.
2186 LEFT.br_blockcount += PREV.br_blockcount;
2188 xfs_iext_remove(ip, icur, state);
2189 xfs_iext_prev(ifp, icur);
2190 xfs_iext_update_extent(ip, state, icur, &LEFT);
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2195 rval = XFS_ILOG_CORE;
2196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2199 if (XFS_IS_CORRUPT(mp, i != 1)) {
2200 xfs_btree_mark_sick(cur);
2201 error = -EFSCORRUPTED;
2204 if ((error = xfs_btree_delete(cur, &i)))
2206 if (XFS_IS_CORRUPT(mp, i != 1)) {
2207 xfs_btree_mark_sick(cur);
2208 error = -EFSCORRUPTED;
2211 if ((error = xfs_btree_decrement(cur, 0, &i)))
2213 if (XFS_IS_CORRUPT(mp, i != 1)) {
2214 xfs_btree_mark_sick(cur);
2215 error = -EFSCORRUPTED;
2218 error = xfs_bmbt_update(cur, &LEFT);
2224 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2226 * Setting all of a previous oldext extent to newext.
2227 * The right neighbor is contiguous, the left is not.
2229 PREV.br_blockcount += RIGHT.br_blockcount;
2230 PREV.br_state = new->br_state;
2232 xfs_iext_next(ifp, icur);
2233 xfs_iext_remove(ip, icur, state);
2234 xfs_iext_prev(ifp, icur);
2235 xfs_iext_update_extent(ip, state, icur, &PREV);
2239 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2241 rval = XFS_ILOG_CORE;
2242 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2245 if (XFS_IS_CORRUPT(mp, i != 1)) {
2246 xfs_btree_mark_sick(cur);
2247 error = -EFSCORRUPTED;
2250 if ((error = xfs_btree_delete(cur, &i)))
2252 if (XFS_IS_CORRUPT(mp, i != 1)) {
2253 xfs_btree_mark_sick(cur);
2254 error = -EFSCORRUPTED;
2257 if ((error = xfs_btree_decrement(cur, 0, &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_RIGHT_FILLING:
2272 * Setting all of a previous oldext extent to newext.
2273 * Neither the left nor right neighbors are contiguous with
2276 PREV.br_state = new->br_state;
2277 xfs_iext_update_extent(ip, state, icur, &PREV);
2280 rval = XFS_ILOG_DEXT;
2283 error = xfs_bmbt_lookup_eq(cur, new, &i);
2286 if (XFS_IS_CORRUPT(mp, i != 1)) {
2287 xfs_btree_mark_sick(cur);
2288 error = -EFSCORRUPTED;
2291 error = xfs_bmbt_update(cur, &PREV);
2297 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2299 * Setting the first part of a previous oldext extent to newext.
2300 * The left neighbor is contiguous.
2302 LEFT.br_blockcount += new->br_blockcount;
2305 PREV.br_startoff += new->br_blockcount;
2306 PREV.br_startblock += new->br_blockcount;
2307 PREV.br_blockcount -= new->br_blockcount;
2309 xfs_iext_update_extent(ip, state, icur, &PREV);
2310 xfs_iext_prev(ifp, icur);
2311 xfs_iext_update_extent(ip, state, icur, &LEFT);
2314 rval = XFS_ILOG_DEXT;
2317 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2320 if (XFS_IS_CORRUPT(mp, i != 1)) {
2321 xfs_btree_mark_sick(cur);
2322 error = -EFSCORRUPTED;
2325 error = xfs_bmbt_update(cur, &PREV);
2328 error = xfs_btree_decrement(cur, 0, &i);
2331 error = xfs_bmbt_update(cur, &LEFT);
2337 case BMAP_LEFT_FILLING:
2339 * Setting the first part of a previous oldext extent to newext.
2340 * The left neighbor is not contiguous.
2343 PREV.br_startoff += new->br_blockcount;
2344 PREV.br_startblock += new->br_blockcount;
2345 PREV.br_blockcount -= new->br_blockcount;
2347 xfs_iext_update_extent(ip, state, icur, &PREV);
2348 xfs_iext_insert(ip, icur, new, state);
2352 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2354 rval = XFS_ILOG_CORE;
2355 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2358 if (XFS_IS_CORRUPT(mp, i != 1)) {
2359 xfs_btree_mark_sick(cur);
2360 error = -EFSCORRUPTED;
2363 error = xfs_bmbt_update(cur, &PREV);
2366 cur->bc_rec.b = *new;
2367 if ((error = xfs_btree_insert(cur, &i)))
2369 if (XFS_IS_CORRUPT(mp, i != 1)) {
2370 xfs_btree_mark_sick(cur);
2371 error = -EFSCORRUPTED;
2377 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2379 * Setting the last part of a previous oldext extent to newext.
2380 * The right neighbor is contiguous with the new allocation.
2383 PREV.br_blockcount -= new->br_blockcount;
2385 RIGHT.br_startoff = new->br_startoff;
2386 RIGHT.br_startblock = new->br_startblock;
2387 RIGHT.br_blockcount += new->br_blockcount;
2389 xfs_iext_update_extent(ip, state, icur, &PREV);
2390 xfs_iext_next(ifp, icur);
2391 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2394 rval = XFS_ILOG_DEXT;
2397 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2400 if (XFS_IS_CORRUPT(mp, i != 1)) {
2401 xfs_btree_mark_sick(cur);
2402 error = -EFSCORRUPTED;
2405 error = xfs_bmbt_update(cur, &PREV);
2408 error = xfs_btree_increment(cur, 0, &i);
2411 error = xfs_bmbt_update(cur, &RIGHT);
2417 case BMAP_RIGHT_FILLING:
2419 * Setting the last part of a previous oldext extent to newext.
2420 * The right neighbor is not contiguous.
2423 PREV.br_blockcount -= new->br_blockcount;
2425 xfs_iext_update_extent(ip, state, icur, &PREV);
2426 xfs_iext_next(ifp, icur);
2427 xfs_iext_insert(ip, icur, new, state);
2431 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2433 rval = XFS_ILOG_CORE;
2434 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2437 if (XFS_IS_CORRUPT(mp, i != 1)) {
2438 xfs_btree_mark_sick(cur);
2439 error = -EFSCORRUPTED;
2442 error = xfs_bmbt_update(cur, &PREV);
2445 error = xfs_bmbt_lookup_eq(cur, new, &i);
2448 if (XFS_IS_CORRUPT(mp, i != 0)) {
2449 xfs_btree_mark_sick(cur);
2450 error = -EFSCORRUPTED;
2453 if ((error = xfs_btree_insert(cur, &i)))
2455 if (XFS_IS_CORRUPT(mp, i != 1)) {
2456 xfs_btree_mark_sick(cur);
2457 error = -EFSCORRUPTED;
2465 * Setting the middle part of a previous oldext extent to
2466 * newext. Contiguity is impossible here.
2467 * One extent becomes three extents.
2470 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2473 r[1].br_startoff = new_endoff;
2474 r[1].br_blockcount =
2475 old.br_startoff + old.br_blockcount - new_endoff;
2476 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2477 r[1].br_state = PREV.br_state;
2479 xfs_iext_update_extent(ip, state, icur, &PREV);
2480 xfs_iext_next(ifp, icur);
2481 xfs_iext_insert(ip, icur, &r[1], state);
2482 xfs_iext_insert(ip, icur, &r[0], state);
2483 ifp->if_nextents += 2;
2486 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2488 rval = XFS_ILOG_CORE;
2489 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2492 if (XFS_IS_CORRUPT(mp, i != 1)) {
2493 xfs_btree_mark_sick(cur);
2494 error = -EFSCORRUPTED;
2497 /* new right extent - oldext */
2498 error = xfs_bmbt_update(cur, &r[1]);
2501 /* new left extent - oldext */
2502 cur->bc_rec.b = PREV;
2503 if ((error = xfs_btree_insert(cur, &i)))
2505 if (XFS_IS_CORRUPT(mp, i != 1)) {
2506 xfs_btree_mark_sick(cur);
2507 error = -EFSCORRUPTED;
2511 * Reset the cursor to the position of the new extent
2512 * we are about to insert as we can't trust it after
2513 * the previous insert.
2515 error = xfs_bmbt_lookup_eq(cur, new, &i);
2518 if (XFS_IS_CORRUPT(mp, i != 0)) {
2519 xfs_btree_mark_sick(cur);
2520 error = -EFSCORRUPTED;
2523 /* new middle extent - newext */
2524 if ((error = xfs_btree_insert(cur, &i)))
2526 if (XFS_IS_CORRUPT(mp, i != 1)) {
2527 xfs_btree_mark_sick(cur);
2528 error = -EFSCORRUPTED;
2534 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2535 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2536 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2537 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2538 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2539 case BMAP_LEFT_CONTIG:
2540 case BMAP_RIGHT_CONTIG:
2542 * These cases are all impossible.
2547 /* update reverse mappings */
2548 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2550 /* convert to a btree if necessary */
2551 if (xfs_bmap_needs_btree(ip, whichfork)) {
2552 int tmp_logflags; /* partial log flag return val */
2554 ASSERT(cur == NULL);
2555 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2556 &tmp_logflags, whichfork);
2557 *logflagsp |= tmp_logflags;
2562 /* clear out the allocated field, done with it now in any case. */
2564 cur->bc_bmap.allocated = 0;
2568 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2578 * Convert a hole to a delayed allocation.
2581 xfs_bmap_add_extent_hole_delay(
2582 xfs_inode_t *ip, /* incore inode pointer */
2584 struct xfs_iext_cursor *icur,
2585 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2587 struct xfs_ifork *ifp; /* inode fork pointer */
2588 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2589 xfs_filblks_t newlen=0; /* new indirect size */
2590 xfs_filblks_t oldlen=0; /* old indirect size */
2591 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2592 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2593 xfs_filblks_t temp; /* temp for indirect calculations */
2595 ifp = xfs_ifork_ptr(ip, whichfork);
2596 ASSERT(isnullstartblock(new->br_startblock));
2599 * Check and set flags if this segment has a left neighbor
2601 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2602 state |= BMAP_LEFT_VALID;
2603 if (isnullstartblock(left.br_startblock))
2604 state |= BMAP_LEFT_DELAY;
2608 * Check and set flags if the current (right) segment exists.
2609 * If it doesn't exist, we're converting the hole at end-of-file.
2611 if (xfs_iext_get_extent(ifp, icur, &right)) {
2612 state |= BMAP_RIGHT_VALID;
2613 if (isnullstartblock(right.br_startblock))
2614 state |= BMAP_RIGHT_DELAY;
2618 * Set contiguity flags on the left and right neighbors.
2619 * Don't let extents get too large, even if the pieces are contiguous.
2621 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2622 left.br_startoff + left.br_blockcount == new->br_startoff &&
2623 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2624 state |= BMAP_LEFT_CONTIG;
2626 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2627 new->br_startoff + new->br_blockcount == right.br_startoff &&
2628 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2629 (!(state & BMAP_LEFT_CONTIG) ||
2630 (left.br_blockcount + new->br_blockcount +
2631 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2632 state |= BMAP_RIGHT_CONTIG;
2635 * Switch out based on the contiguity flags.
2637 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2638 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2640 * New allocation is contiguous with delayed allocations
2641 * on the left and on the right.
2642 * Merge all three into a single extent record.
2644 temp = left.br_blockcount + new->br_blockcount +
2645 right.br_blockcount;
2647 oldlen = startblockval(left.br_startblock) +
2648 startblockval(new->br_startblock) +
2649 startblockval(right.br_startblock);
2650 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2652 left.br_startblock = nullstartblock(newlen);
2653 left.br_blockcount = temp;
2655 xfs_iext_remove(ip, icur, state);
2656 xfs_iext_prev(ifp, icur);
2657 xfs_iext_update_extent(ip, state, icur, &left);
2660 case BMAP_LEFT_CONTIG:
2662 * New allocation is contiguous with a delayed allocation
2664 * Merge the new allocation with the left neighbor.
2666 temp = left.br_blockcount + new->br_blockcount;
2668 oldlen = startblockval(left.br_startblock) +
2669 startblockval(new->br_startblock);
2670 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2672 left.br_blockcount = temp;
2673 left.br_startblock = nullstartblock(newlen);
2675 xfs_iext_prev(ifp, icur);
2676 xfs_iext_update_extent(ip, state, icur, &left);
2679 case BMAP_RIGHT_CONTIG:
2681 * New allocation is contiguous with a delayed allocation
2683 * Merge the new allocation with the right neighbor.
2685 temp = new->br_blockcount + right.br_blockcount;
2686 oldlen = startblockval(new->br_startblock) +
2687 startblockval(right.br_startblock);
2688 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2690 right.br_startoff = new->br_startoff;
2691 right.br_startblock = nullstartblock(newlen);
2692 right.br_blockcount = temp;
2693 xfs_iext_update_extent(ip, state, icur, &right);
2698 * New allocation is not contiguous with another
2699 * delayed allocation.
2700 * Insert a new entry.
2702 oldlen = newlen = 0;
2703 xfs_iext_insert(ip, icur, new, state);
2706 if (oldlen != newlen) {
2707 ASSERT(oldlen > newlen);
2708 xfs_add_fdblocks(ip->i_mount, oldlen - newlen);
2711 * Nothing to do for disk quota accounting here.
2713 xfs_mod_delalloc(ip, 0, (int64_t)newlen - oldlen);
2718 * Convert a hole to a real allocation.
2720 STATIC int /* error */
2721 xfs_bmap_add_extent_hole_real(
2722 struct xfs_trans *tp,
2723 struct xfs_inode *ip,
2725 struct xfs_iext_cursor *icur,
2726 struct xfs_btree_cur **curp,
2727 struct xfs_bmbt_irec *new,
2731 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2732 struct xfs_mount *mp = ip->i_mount;
2733 struct xfs_btree_cur *cur = *curp;
2734 int error; /* error return value */
2735 int i; /* temp state */
2736 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2737 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2738 int rval=0; /* return value (logging flags) */
2739 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2740 struct xfs_bmbt_irec old;
2742 ASSERT(!isnullstartblock(new->br_startblock));
2743 ASSERT(!cur || !(cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
2745 XFS_STATS_INC(mp, xs_add_exlist);
2748 * Check and set flags if this segment has a left neighbor.
2750 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2751 state |= BMAP_LEFT_VALID;
2752 if (isnullstartblock(left.br_startblock))
2753 state |= BMAP_LEFT_DELAY;
2757 * Check and set flags if this segment has a current value.
2758 * Not true if we're inserting into the "hole" at eof.
2760 if (xfs_iext_get_extent(ifp, icur, &right)) {
2761 state |= BMAP_RIGHT_VALID;
2762 if (isnullstartblock(right.br_startblock))
2763 state |= BMAP_RIGHT_DELAY;
2767 * We're inserting a real allocation between "left" and "right".
2768 * Set the contiguity flags. Don't let extents get too large.
2770 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2771 left.br_startoff + left.br_blockcount == new->br_startoff &&
2772 left.br_startblock + left.br_blockcount == new->br_startblock &&
2773 left.br_state == new->br_state &&
2774 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2775 xfs_bmap_same_rtgroup(ip, whichfork, &left, new))
2776 state |= BMAP_LEFT_CONTIG;
2778 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2779 new->br_startoff + new->br_blockcount == right.br_startoff &&
2780 new->br_startblock + new->br_blockcount == right.br_startblock &&
2781 new->br_state == right.br_state &&
2782 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2783 (!(state & BMAP_LEFT_CONTIG) ||
2784 left.br_blockcount + new->br_blockcount +
2785 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN) &&
2786 xfs_bmap_same_rtgroup(ip, whichfork, new, &right))
2787 state |= BMAP_RIGHT_CONTIG;
2791 * Select which case we're in here, and implement it.
2793 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2794 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2796 * New allocation is contiguous with real allocations on the
2797 * left and on the right.
2798 * Merge all three into a single extent record.
2800 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2802 xfs_iext_remove(ip, icur, state);
2803 xfs_iext_prev(ifp, icur);
2804 xfs_iext_update_extent(ip, state, icur, &left);
2808 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2810 rval = XFS_ILOG_CORE;
2811 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2814 if (XFS_IS_CORRUPT(mp, i != 1)) {
2815 xfs_btree_mark_sick(cur);
2816 error = -EFSCORRUPTED;
2819 error = xfs_btree_delete(cur, &i);
2822 if (XFS_IS_CORRUPT(mp, i != 1)) {
2823 xfs_btree_mark_sick(cur);
2824 error = -EFSCORRUPTED;
2827 error = xfs_btree_decrement(cur, 0, &i);
2830 if (XFS_IS_CORRUPT(mp, i != 1)) {
2831 xfs_btree_mark_sick(cur);
2832 error = -EFSCORRUPTED;
2835 error = xfs_bmbt_update(cur, &left);
2841 case BMAP_LEFT_CONTIG:
2843 * New allocation is contiguous with a real allocation
2845 * Merge the new allocation with the left neighbor.
2848 left.br_blockcount += new->br_blockcount;
2850 xfs_iext_prev(ifp, icur);
2851 xfs_iext_update_extent(ip, state, icur, &left);
2854 rval = xfs_ilog_fext(whichfork);
2857 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2860 if (XFS_IS_CORRUPT(mp, i != 1)) {
2861 xfs_btree_mark_sick(cur);
2862 error = -EFSCORRUPTED;
2865 error = xfs_bmbt_update(cur, &left);
2871 case BMAP_RIGHT_CONTIG:
2873 * New allocation is contiguous with a real allocation
2875 * Merge the new allocation with the right neighbor.
2879 right.br_startoff = new->br_startoff;
2880 right.br_startblock = new->br_startblock;
2881 right.br_blockcount += new->br_blockcount;
2882 xfs_iext_update_extent(ip, state, icur, &right);
2885 rval = xfs_ilog_fext(whichfork);
2888 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2891 if (XFS_IS_CORRUPT(mp, i != 1)) {
2892 xfs_btree_mark_sick(cur);
2893 error = -EFSCORRUPTED;
2896 error = xfs_bmbt_update(cur, &right);
2904 * New allocation is not contiguous with another
2906 * Insert a new entry.
2908 xfs_iext_insert(ip, icur, new, state);
2912 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2914 rval = XFS_ILOG_CORE;
2915 error = xfs_bmbt_lookup_eq(cur, new, &i);
2918 if (XFS_IS_CORRUPT(mp, i != 0)) {
2919 xfs_btree_mark_sick(cur);
2920 error = -EFSCORRUPTED;
2923 error = xfs_btree_insert(cur, &i);
2926 if (XFS_IS_CORRUPT(mp, i != 1)) {
2927 xfs_btree_mark_sick(cur);
2928 error = -EFSCORRUPTED;
2935 /* add reverse mapping unless caller opted out */
2936 if (!(flags & XFS_BMAPI_NORMAP))
2937 xfs_rmap_map_extent(tp, ip, whichfork, new);
2939 /* convert to a btree if necessary */
2940 if (xfs_bmap_needs_btree(ip, whichfork)) {
2941 int tmp_logflags; /* partial log flag return val */
2943 ASSERT(cur == NULL);
2944 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2945 &tmp_logflags, whichfork);
2946 *logflagsp |= tmp_logflags;
2952 /* clear out the allocated field, done with it now in any case. */
2954 cur->bc_bmap.allocated = 0;
2956 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2963 * Functions used in the extent read, allocate and remove paths
2967 * Adjust the size of the new extent based on i_extsize and rt extsize.
2970 xfs_bmap_extsize_align(
2972 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2973 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2974 xfs_extlen_t extsz, /* align to this extent size */
2975 int rt, /* is this a realtime inode? */
2976 int eof, /* is extent at end-of-file? */
2977 int delay, /* creating delalloc extent? */
2978 int convert, /* overwriting unwritten extent? */
2979 xfs_fileoff_t *offp, /* in/out: aligned offset */
2980 xfs_extlen_t *lenp) /* in/out: aligned length */
2982 xfs_fileoff_t orig_off; /* original offset */
2983 xfs_extlen_t orig_alen; /* original length */
2984 xfs_fileoff_t orig_end; /* original off+len */
2985 xfs_fileoff_t nexto; /* next file offset */
2986 xfs_fileoff_t prevo; /* previous file offset */
2987 xfs_fileoff_t align_off; /* temp for offset */
2988 xfs_extlen_t align_alen; /* temp for length */
2989 xfs_extlen_t temp; /* temp for calculations */
2994 orig_off = align_off = *offp;
2995 orig_alen = align_alen = *lenp;
2996 orig_end = orig_off + orig_alen;
2999 * If this request overlaps an existing extent, then don't
3000 * attempt to perform any additional alignment.
3002 if (!delay && !eof &&
3003 (orig_off >= gotp->br_startoff) &&
3004 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3009 * If the file offset is unaligned vs. the extent size
3010 * we need to align it. This will be possible unless
3011 * the file was previously written with a kernel that didn't
3012 * perform this alignment, or if a truncate shot us in the
3015 div_u64_rem(orig_off, extsz, &temp);
3021 /* Same adjustment for the end of the requested area. */
3022 temp = (align_alen % extsz);
3024 align_alen += extsz - temp;
3027 * For large extent hint sizes, the aligned extent might be larger than
3028 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
3029 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
3030 * allocation loops handle short allocation just fine, so it is safe to
3031 * do this. We only want to do it when we are forced to, though, because
3032 * it means more allocation operations are required.
3034 while (align_alen > XFS_MAX_BMBT_EXTLEN)
3035 align_alen -= extsz;
3036 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
3039 * If the previous block overlaps with this proposed allocation
3040 * then move the start forward without adjusting the length.
3042 if (prevp->br_startoff != NULLFILEOFF) {
3043 if (prevp->br_startblock == HOLESTARTBLOCK)
3044 prevo = prevp->br_startoff;
3046 prevo = prevp->br_startoff + prevp->br_blockcount;
3049 if (align_off != orig_off && align_off < prevo)
3052 * If the next block overlaps with this proposed allocation
3053 * then move the start back without adjusting the length,
3054 * but not before offset 0.
3055 * This may of course make the start overlap previous block,
3056 * and if we hit the offset 0 limit then the next block
3057 * can still overlap too.
3059 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3060 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3061 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3062 nexto = gotp->br_startoff + gotp->br_blockcount;
3064 nexto = gotp->br_startoff;
3066 nexto = NULLFILEOFF;
3068 align_off + align_alen != orig_end &&
3069 align_off + align_alen > nexto)
3070 align_off = nexto > align_alen ? nexto - align_alen : 0;
3072 * If we're now overlapping the next or previous extent that
3073 * means we can't fit an extsz piece in this hole. Just move
3074 * the start forward to the first valid spot and set
3075 * the length so we hit the end.
3077 if (align_off != orig_off && align_off < prevo)
3079 if (align_off + align_alen != orig_end &&
3080 align_off + align_alen > nexto &&
3081 nexto != NULLFILEOFF) {
3082 ASSERT(nexto > prevo);
3083 align_alen = nexto - align_off;
3087 * If realtime, and the result isn't a multiple of the realtime
3088 * extent size we need to remove blocks until it is.
3090 if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
3092 * We're not covering the original request, or
3093 * we won't be able to once we fix the length.
3095 if (orig_off < align_off ||
3096 orig_end > align_off + align_alen ||
3097 align_alen - temp < orig_alen)
3100 * Try to fix it by moving the start up.
3102 if (align_off + temp <= orig_off) {
3107 * Try to fix it by moving the end in.
3109 else if (align_off + align_alen - temp >= orig_end)
3112 * Set the start to the minimum then trim the length.
3115 align_alen -= orig_off - align_off;
3116 align_off = orig_off;
3117 align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
3120 * Result doesn't cover the request, fail it.
3122 if (orig_off < align_off || orig_end > align_off + align_alen)
3125 ASSERT(orig_off >= align_off);
3126 /* see XFS_BMBT_MAX_EXTLEN handling above */
3127 ASSERT(orig_end <= align_off + align_alen ||
3128 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3132 if (!eof && gotp->br_startoff != NULLFILEOFF)
3133 ASSERT(align_off + align_alen <= gotp->br_startoff);
3134 if (prevp->br_startoff != NULLFILEOFF)
3135 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3144 xfs_bmap_adjacent_valid(
3145 struct xfs_bmalloca *ap,
3149 struct xfs_mount *mp = ap->ip->i_mount;
3151 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3152 (ap->datatype & XFS_ALLOC_USERDATA)) {
3153 if (!xfs_has_rtgroups(mp))
3154 return x < mp->m_sb.sb_rblocks;
3156 return xfs_rtb_to_rgno(mp, x) == xfs_rtb_to_rgno(mp, y) &&
3157 xfs_rtb_to_rgno(mp, x) < mp->m_sb.sb_rgcount &&
3158 xfs_rtb_to_rtx(mp, x) < mp->m_sb.sb_rgextents;
3162 return XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) &&
3163 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount &&
3164 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks;
3167 #define XFS_ALLOC_GAP_UNITS 4
3169 /* returns true if ap->blkno was modified */
3172 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3174 xfs_fsblock_t adjust; /* adjustment to block numbers */
3177 * If allocating at eof, and there's a previous real block,
3178 * try to use its last block as our starting point.
3180 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3181 !isnullstartblock(ap->prev.br_startblock) &&
3182 xfs_bmap_adjacent_valid(ap,
3183 ap->prev.br_startblock + ap->prev.br_blockcount,
3184 ap->prev.br_startblock)) {
3185 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3187 * Adjust for the gap between prevp and us.
3189 adjust = ap->offset -
3190 (ap->prev.br_startoff + ap->prev.br_blockcount);
3191 if (adjust && xfs_bmap_adjacent_valid(ap, ap->blkno + adjust,
3192 ap->prev.br_startblock))
3193 ap->blkno += adjust;
3197 * If not at eof, then compare the two neighbor blocks.
3198 * Figure out whether either one gives us a good starting point,
3199 * and pick the better one.
3202 xfs_fsblock_t gotbno; /* right side block number */
3203 xfs_fsblock_t gotdiff=0; /* right side difference */
3204 xfs_fsblock_t prevbno; /* left side block number */
3205 xfs_fsblock_t prevdiff=0; /* left side difference */
3208 * If there's a previous (left) block, select a requested
3209 * start block based on it.
3211 if (ap->prev.br_startoff != NULLFILEOFF &&
3212 !isnullstartblock(ap->prev.br_startblock) &&
3213 (prevbno = ap->prev.br_startblock +
3214 ap->prev.br_blockcount) &&
3215 xfs_bmap_adjacent_valid(ap, prevbno,
3216 ap->prev.br_startblock)) {
3218 * Calculate gap to end of previous block.
3220 adjust = prevdiff = ap->offset -
3221 (ap->prev.br_startoff +
3222 ap->prev.br_blockcount);
3224 * Figure the startblock based on the previous block's
3225 * end and the gap size.
3227 * If the gap is large relative to the piece we're
3228 * allocating, or using it gives us an invalid block
3229 * number, then just use the end of the previous block.
3231 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3232 xfs_bmap_adjacent_valid(ap, prevbno + prevdiff,
3233 ap->prev.br_startblock))
3239 * No previous block or can't follow it, just default.
3242 prevbno = NULLFSBLOCK;
3244 * If there's a following (right) block, select a requested
3245 * start block based on it.
3247 if (!isnullstartblock(ap->got.br_startblock)) {
3249 * Calculate gap to start of next block.
3251 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3253 * Figure the startblock based on the next block's
3254 * start and the gap size.
3256 gotbno = ap->got.br_startblock;
3259 * If the gap is large relative to the piece we're
3260 * allocating, or using it gives us an invalid block
3261 * number, then just use the start of the next block
3262 * offset by our length.
3264 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3265 xfs_bmap_adjacent_valid(ap, gotbno - gotdiff,
3268 else if (xfs_bmap_adjacent_valid(ap, gotbno - ap->length,
3270 gotbno -= ap->length;
3271 gotdiff += adjust - ap->length;
3276 * No next block, just default.
3279 gotbno = NULLFSBLOCK;
3281 * If both valid, pick the better one, else the only good
3282 * one, else ap->blkno is already set (to 0 or the inode block).
3284 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) {
3285 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3288 if (prevbno != NULLFSBLOCK) {
3289 ap->blkno = prevbno;
3292 if (gotbno != NULLFSBLOCK) {
3302 xfs_bmap_longest_free_extent(
3303 struct xfs_perag *pag,
3304 struct xfs_trans *tp,
3307 xfs_extlen_t longest;
3310 if (!xfs_perag_initialised_agf(pag)) {
3311 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3317 longest = xfs_alloc_longest_free_extent(pag,
3318 xfs_alloc_min_freelist(pag_mount(pag), pag),
3319 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3320 if (*blen < longest)
3327 xfs_bmap_select_minlen(
3328 struct xfs_bmalloca *ap,
3329 struct xfs_alloc_arg *args,
3334 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3335 * possible that there is enough contiguous free space for this request.
3337 if (blen < ap->minlen)
3341 * If the best seen length is less than the request length,
3342 * use the best as the minimum, otherwise we've got the maxlen we
3345 if (blen < args->maxlen)
3347 return args->maxlen;
3351 xfs_bmap_btalloc_select_lengths(
3352 struct xfs_bmalloca *ap,
3353 struct xfs_alloc_arg *args,
3356 struct xfs_mount *mp = args->mp;
3357 struct xfs_perag *pag;
3358 xfs_agnumber_t agno, startag;
3361 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3362 args->total = ap->minlen;
3363 args->minlen = ap->minlen;
3367 args->total = ap->total;
3368 startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
3369 if (startag == NULLAGNUMBER)
3373 for_each_perag_wrap(mp, startag, agno, pag) {
3374 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3375 if (error && error != -EAGAIN)
3378 if (*blen >= args->maxlen)
3382 xfs_perag_rele(pag);
3384 args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
3388 /* Update all inode and quota accounting for the allocation we just did. */
3390 xfs_bmap_alloc_account(
3391 struct xfs_bmalloca *ap)
3393 bool isrt = XFS_IS_REALTIME_INODE(ap->ip) &&
3394 !(ap->flags & XFS_BMAPI_ATTRFORK);
3397 if (ap->flags & XFS_BMAPI_COWFORK) {
3399 * COW fork blocks are in-core only and thus are treated as
3400 * in-core quota reservation (like delalloc blocks) even when
3401 * converted to real blocks. The quota reservation is not
3402 * accounted to disk until blocks are remapped to the data
3403 * fork. So if these blocks were previously delalloc, we
3404 * already have quota reservation and there's nothing to do
3408 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3413 * Otherwise, we've allocated blocks in a hole. The transaction
3414 * has acquired in-core quota reservation for this extent.
3415 * Rather than account these as real blocks, however, we reduce
3416 * the transaction quota reservation based on the allocation.
3417 * This essentially transfers the transaction quota reservation
3418 * to that of a delalloc extent.
3420 ap->ip->i_delayed_blks += ap->length;
3421 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ?
3422 XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
3427 /* data/attr fork only */
3428 ap->ip->i_nblocks += ap->length;
3429 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3431 ap->ip->i_delayed_blks -= ap->length;
3432 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3433 fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT;
3435 fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
3438 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length);
3442 xfs_bmap_compute_alignments(
3443 struct xfs_bmalloca *ap,
3444 struct xfs_alloc_arg *args)
3446 struct xfs_mount *mp = args->mp;
3447 xfs_extlen_t align = 0; /* minimum allocation alignment */
3448 int stripe_align = 0;
3450 /* stripe alignment for allocation is determined by mount parameters */
3451 if (mp->m_swidth && xfs_has_swalloc(mp))
3452 stripe_align = mp->m_swidth;
3453 else if (mp->m_dalign)
3454 stripe_align = mp->m_dalign;
3456 if (ap->flags & XFS_BMAPI_COWFORK)
3457 align = xfs_get_cowextsz_hint(ap->ip);
3458 else if (ap->datatype & XFS_ALLOC_USERDATA)
3459 align = xfs_get_extsz_hint(ap->ip);
3461 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3462 ap->eof, 0, ap->conv, &ap->offset,
3468 /* apply extent size hints if obtained earlier */
3471 div_u64_rem(ap->offset, args->prod, &args->mod);
3473 args->mod = args->prod - args->mod;
3474 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3478 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3479 div_u64_rem(ap->offset, args->prod, &args->mod);
3481 args->mod = args->prod - args->mod;
3484 return stripe_align;
3488 xfs_bmap_process_allocated_extent(
3489 struct xfs_bmalloca *ap,
3490 struct xfs_alloc_arg *args,
3491 xfs_fileoff_t orig_offset,
3492 xfs_extlen_t orig_length)
3494 ap->blkno = args->fsbno;
3495 ap->length = args->len;
3497 * If the extent size hint is active, we tried to round the
3498 * caller's allocation request offset down to extsz and the
3499 * length up to another extsz boundary. If we found a free
3500 * extent we mapped it in starting at this new offset. If the
3501 * newly mapped space isn't long enough to cover any of the
3502 * range of offsets that was originally requested, move the
3503 * mapping up so that we can fill as much of the caller's
3504 * original request as possible. Free space is apparently
3505 * very fragmented so we're unlikely to be able to satisfy the
3508 if (ap->length <= orig_length)
3509 ap->offset = orig_offset;
3510 else if (ap->offset + ap->length < orig_offset + orig_length)
3511 ap->offset = orig_offset + orig_length - ap->length;
3512 xfs_bmap_alloc_account(ap);
3516 xfs_bmap_exact_minlen_extent_alloc(
3517 struct xfs_bmalloca *ap,
3518 struct xfs_alloc_arg *args)
3520 if (ap->minlen != 1) {
3521 args->fsbno = NULLFSBLOCK;
3525 args->alloc_minlen_only = 1;
3526 args->minlen = args->maxlen = ap->minlen;
3527 args->total = ap->total;
3530 * Unlike the longest extent available in an AG, we don't track
3531 * the length of an AG's shortest extent.
3532 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3533 * hence we can afford to start traversing from the 0th AG since
3534 * we need not be concerned about a drop in performance in
3535 * "debug only" code paths.
3537 ap->blkno = XFS_AGB_TO_FSB(ap->ip->i_mount, 0, 0);
3540 * Call xfs_bmap_btalloc_low_space here as it first does a "normal" AG
3541 * iteration and then drops args->total to args->minlen, which might be
3542 * required to find an allocation for the transaction reservation when
3543 * the file system is very full.
3545 return xfs_bmap_btalloc_low_space(ap, args);
3549 * If we are not low on available data blocks and we are allocating at
3550 * EOF, optimise allocation for contiguous file extension and/or stripe
3551 * alignment of the new extent.
3553 * NOTE: ap->aeof is only set if the allocation length is >= the
3554 * stripe unit and the allocation offset is at the end of file.
3557 xfs_bmap_btalloc_at_eof(
3558 struct xfs_bmalloca *ap,
3559 struct xfs_alloc_arg *args,
3564 struct xfs_mount *mp = args->mp;
3565 struct xfs_perag *caller_pag = args->pag;
3569 * If there are already extents in the file, try an exact EOF block
3570 * allocation to extend the file as a contiguous extent. If that fails,
3571 * or it's the first allocation in a file, just try for a stripe aligned
3575 xfs_extlen_t nextminlen = 0;
3578 * Compute the minlen+alignment for the next case. Set slop so
3579 * that the value of minlen+alignment+slop doesn't go up between
3582 args->alignment = 1;
3583 if (blen > stripe_align && blen <= args->maxlen)
3584 nextminlen = blen - stripe_align;
3586 nextminlen = args->minlen;
3587 if (nextminlen + stripe_align > args->minlen + 1)
3588 args->minalignslop = nextminlen + stripe_align -
3591 args->minalignslop = 0;
3594 args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
3595 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
3597 xfs_perag_put(args->pag);
3603 if (args->fsbno != NULLFSBLOCK)
3606 * Exact allocation failed. Reset to try an aligned allocation
3607 * according to the original allocation specification.
3609 args->alignment = stripe_align;
3610 args->minlen = nextminlen;
3611 args->minalignslop = 0;
3614 * Adjust minlen to try and preserve alignment if we
3615 * can't guarantee an aligned maxlen extent.
3617 args->alignment = stripe_align;
3618 if (blen > args->alignment &&
3619 blen <= args->maxlen + args->alignment)
3620 args->minlen = blen - args->alignment;
3621 args->minalignslop = 0;
3625 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3628 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3629 ASSERT(args->pag == NULL);
3630 args->pag = caller_pag;
3635 if (args->fsbno != NULLFSBLOCK)
3639 * Allocation failed, so turn return the allocation args to their
3640 * original non-aligned state so the caller can proceed on allocation
3641 * failure as if this function was never called.
3643 args->alignment = 1;
3648 * We have failed multiple allocation attempts so now are in a low space
3649 * allocation situation. Try a locality first full filesystem minimum length
3650 * allocation whilst still maintaining necessary total block reservation
3653 * If that fails, we are now critically low on space, so perform a last resort
3654 * allocation attempt: no reserve, no locality, blocking, minimum length, full
3655 * filesystem free space scan. We also indicate to future allocations in this
3656 * transaction that we are critically low on space so they don't waste time on
3657 * allocation modes that are unlikely to succeed.
3660 xfs_bmap_btalloc_low_space(
3661 struct xfs_bmalloca *ap,
3662 struct xfs_alloc_arg *args)
3666 if (args->minlen > ap->minlen) {
3667 args->minlen = ap->minlen;
3668 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3669 if (error || args->fsbno != NULLFSBLOCK)
3673 /* Last ditch attempt before failure is declared. */
3674 args->total = ap->minlen;
3675 error = xfs_alloc_vextent_first_ag(args, 0);
3678 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3683 xfs_bmap_btalloc_filestreams(
3684 struct xfs_bmalloca *ap,
3685 struct xfs_alloc_arg *args,
3688 xfs_extlen_t blen = 0;
3692 error = xfs_filestream_select_ag(ap, args, &blen);
3698 * If we are in low space mode, then optimal allocation will fail so
3699 * prepare for minimal allocation and jump to the low space algorithm
3702 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3703 args->minlen = ap->minlen;
3704 ASSERT(args->fsbno == NULLFSBLOCK);
3708 args->minlen = xfs_bmap_select_minlen(ap, args, blen);
3710 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3713 if (!error && args->fsbno == NULLFSBLOCK)
3714 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3718 * We are now done with the perag reference for the filestreams
3719 * association provided by xfs_filestream_select_ag(). Release it now as
3720 * we've either succeeded, had a fatal error or we are out of space and
3721 * need to do a full filesystem scan for free space which will take it's
3724 xfs_perag_rele(args->pag);
3726 if (error || args->fsbno != NULLFSBLOCK)
3729 return xfs_bmap_btalloc_low_space(ap, args);
3733 xfs_bmap_btalloc_best_length(
3734 struct xfs_bmalloca *ap,
3735 struct xfs_alloc_arg *args,
3738 xfs_extlen_t blen = 0;
3741 ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
3742 xfs_bmap_adjacent(ap);
3745 * Search for an allocation group with a single extent large enough for
3746 * the request. If one isn't found, then adjust the minimum allocation
3747 * size to the largest space found.
3749 error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
3754 * Don't attempt optimal EOF allocation if previous allocations barely
3755 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3756 * optimal or even aligned allocations in this case, so don't waste time
3759 if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
3760 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3762 if (error || args->fsbno != NULLFSBLOCK)
3766 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3767 if (error || args->fsbno != NULLFSBLOCK)
3770 return xfs_bmap_btalloc_low_space(ap, args);
3775 struct xfs_bmalloca *ap)
3777 struct xfs_mount *mp = ap->ip->i_mount;
3778 struct xfs_alloc_arg args = {
3781 .fsbno = NULLFSBLOCK,
3782 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
3783 .minleft = ap->minleft,
3784 .wasdel = ap->wasdel,
3785 .resv = XFS_AG_RESV_NONE,
3786 .datatype = ap->datatype,
3790 xfs_fileoff_t orig_offset;
3791 xfs_extlen_t orig_length;
3796 orig_offset = ap->offset;
3797 orig_length = ap->length;
3799 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3801 /* Trim the allocation back to the maximum an AG can fit. */
3802 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3804 if (unlikely(XFS_TEST_ERROR(false, mp,
3805 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
3806 error = xfs_bmap_exact_minlen_extent_alloc(ap, &args);
3807 else if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3808 xfs_inode_is_filestream(ap->ip))
3809 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3811 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3815 if (args.fsbno != NULLFSBLOCK) {
3816 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3819 ap->blkno = NULLFSBLOCK;
3825 /* Trim extent to fit a logical block range. */
3828 struct xfs_bmbt_irec *irec,
3832 xfs_fileoff_t distance;
3833 xfs_fileoff_t end = bno + len;
3835 if (irec->br_startoff + irec->br_blockcount <= bno ||
3836 irec->br_startoff >= end) {
3837 irec->br_blockcount = 0;
3841 if (irec->br_startoff < bno) {
3842 distance = bno - irec->br_startoff;
3843 if (isnullstartblock(irec->br_startblock))
3844 irec->br_startblock = DELAYSTARTBLOCK;
3845 if (irec->br_startblock != DELAYSTARTBLOCK &&
3846 irec->br_startblock != HOLESTARTBLOCK)
3847 irec->br_startblock += distance;
3848 irec->br_startoff += distance;
3849 irec->br_blockcount -= distance;
3852 if (end < irec->br_startoff + irec->br_blockcount) {
3853 distance = irec->br_startoff + irec->br_blockcount - end;
3854 irec->br_blockcount -= distance;
3859 * Trim the returned map to the required bounds
3863 struct xfs_bmbt_irec *mval,
3864 struct xfs_bmbt_irec *got,
3872 if ((flags & XFS_BMAPI_ENTIRE) ||
3873 got->br_startoff + got->br_blockcount <= obno) {
3875 if (isnullstartblock(got->br_startblock))
3876 mval->br_startblock = DELAYSTARTBLOCK;
3882 ASSERT((*bno >= obno) || (n == 0));
3884 mval->br_startoff = *bno;
3885 if (isnullstartblock(got->br_startblock))
3886 mval->br_startblock = DELAYSTARTBLOCK;
3888 mval->br_startblock = got->br_startblock +
3889 (*bno - got->br_startoff);
3891 * Return the minimum of what we got and what we asked for for
3892 * the length. We can use the len variable here because it is
3893 * modified below and we could have been there before coming
3894 * here if the first part of the allocation didn't overlap what
3897 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3898 got->br_blockcount - (*bno - got->br_startoff));
3899 mval->br_state = got->br_state;
3900 ASSERT(mval->br_blockcount <= len);
3905 * Update and validate the extent map to return
3908 xfs_bmapi_update_map(
3909 struct xfs_bmbt_irec **map,
3917 xfs_bmbt_irec_t *mval = *map;
3919 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3920 ((mval->br_startoff + mval->br_blockcount) <= end));
3921 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3922 (mval->br_startoff < obno));
3924 *bno = mval->br_startoff + mval->br_blockcount;
3926 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3927 /* update previous map with new information */
3928 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3929 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3930 ASSERT(mval->br_state == mval[-1].br_state);
3931 mval[-1].br_blockcount = mval->br_blockcount;
3932 mval[-1].br_state = mval->br_state;
3933 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3934 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3935 mval[-1].br_startblock != HOLESTARTBLOCK &&
3936 mval->br_startblock == mval[-1].br_startblock +
3937 mval[-1].br_blockcount &&
3938 mval[-1].br_state == mval->br_state) {
3939 ASSERT(mval->br_startoff ==
3940 mval[-1].br_startoff + mval[-1].br_blockcount);
3941 mval[-1].br_blockcount += mval->br_blockcount;
3942 } else if (*n > 0 &&
3943 mval->br_startblock == DELAYSTARTBLOCK &&
3944 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3945 mval->br_startoff ==
3946 mval[-1].br_startoff + mval[-1].br_blockcount) {
3947 mval[-1].br_blockcount += mval->br_blockcount;
3948 mval[-1].br_state = mval->br_state;
3949 } else if (!((*n == 0) &&
3950 ((mval->br_startoff + mval->br_blockcount) <=
3959 * Map file blocks to filesystem blocks without allocation.
3963 struct xfs_inode *ip,
3966 struct xfs_bmbt_irec *mval,
3970 struct xfs_mount *mp = ip->i_mount;
3971 int whichfork = xfs_bmapi_whichfork(flags);
3972 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3973 struct xfs_bmbt_irec got;
3976 struct xfs_iext_cursor icur;
3982 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3983 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
3985 if (WARN_ON_ONCE(!ifp)) {
3986 xfs_bmap_mark_sick(ip, whichfork);
3987 return -EFSCORRUPTED;
3990 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3991 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3992 xfs_bmap_mark_sick(ip, whichfork);
3993 return -EFSCORRUPTED;
3996 if (xfs_is_shutdown(mp))
3999 XFS_STATS_INC(mp, xs_blk_mapr);
4001 error = xfs_iread_extents(NULL, ip, whichfork);
4005 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
4010 while (bno < end && n < *nmap) {
4011 /* Reading past eof, act as though there's a hole up to end. */
4013 got.br_startoff = end;
4014 if (got.br_startoff > bno) {
4015 /* Reading in a hole. */
4016 mval->br_startoff = bno;
4017 mval->br_startblock = HOLESTARTBLOCK;
4018 mval->br_blockcount =
4019 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4020 mval->br_state = XFS_EXT_NORM;
4021 bno += mval->br_blockcount;
4022 len -= mval->br_blockcount;
4028 /* set up the extent map to return. */
4029 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4030 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4032 /* If we're done, stop now. */
4033 if (bno >= end || n >= *nmap)
4036 /* Else go on to the next record. */
4037 if (!xfs_iext_next_extent(ifp, &icur, &got))
4045 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4046 * global pool and the extent inserted into the inode in-core extent tree.
4048 * On entry, got refers to the first extent beyond the offset of the extent to
4049 * allocate or eof is specified if no such extent exists. On return, got refers
4050 * to the extent record that was inserted to the inode fork.
4052 * Note that the allocated extent may have been merged with contiguous extents
4053 * during insertion into the inode fork. Thus, got does not reflect the current
4054 * state of the inode fork on return. If necessary, the caller can use lastx to
4055 * look up the updated record in the inode fork.
4058 xfs_bmapi_reserve_delalloc(
4059 struct xfs_inode *ip,
4063 xfs_filblks_t prealloc,
4064 struct xfs_bmbt_irec *got,
4065 struct xfs_iext_cursor *icur,
4068 struct xfs_mount *mp = ip->i_mount;
4069 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4071 xfs_extlen_t indlen;
4075 bool use_cowextszhint =
4076 whichfork == XFS_COW_FORK && !prealloc;
4080 * Cap the alloc length. Keep track of prealloc so we know whether to
4081 * tag the inode before we return.
4084 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
4086 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4087 if (prealloc && alen >= len)
4088 prealloc = alen - len;
4091 * If we're targetting the COW fork but aren't creating a speculative
4092 * posteof preallocation, try to expand the reservation to align with
4093 * the COW extent size hint if there's sufficient free space.
4095 * Unlike the data fork, the CoW cancellation functions will free all
4096 * the reservations at inactivation, so we don't require that every
4097 * delalloc reservation have a dirty pagecache.
4099 if (use_cowextszhint) {
4100 struct xfs_bmbt_irec prev;
4101 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
4103 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4104 prev.br_startoff = NULLFILEOFF;
4106 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4107 1, 0, &aoff, &alen);
4112 * Make a transaction-less quota reservation for delayed allocation
4113 * blocks. This number gets adjusted later. We return if we haven't
4114 * allocated blocks already inside this loop.
4116 error = xfs_quota_reserve_blkres(ip, alen);
4121 * Split changing sb for alen and indlen since they could be coming
4122 * from different places.
4124 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4128 if (XFS_IS_REALTIME_INODE(ip)) {
4129 error = xfs_dec_frextents(mp, xfs_blen_to_rtbxlen(mp, alen));
4131 goto out_unreserve_quota;
4136 error = xfs_dec_fdblocks(mp, fdblocks, false);
4138 goto out_unreserve_frextents;
4140 ip->i_delayed_blks += alen;
4141 xfs_mod_delalloc(ip, alen, indlen);
4143 got->br_startoff = aoff;
4144 got->br_startblock = nullstartblock(indlen);
4145 got->br_blockcount = alen;
4146 got->br_state = XFS_EXT_NORM;
4148 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4151 * Tag the inode if blocks were preallocated. Note that COW fork
4152 * preallocation can occur at the start or end of the extent, even when
4153 * prealloc == 0, so we must also check the aligned offset and length.
4155 if (whichfork == XFS_DATA_FORK && prealloc)
4156 xfs_inode_set_eofblocks_tag(ip);
4157 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4158 xfs_inode_set_cowblocks_tag(ip);
4162 out_unreserve_frextents:
4163 if (XFS_IS_REALTIME_INODE(ip))
4164 xfs_add_frextents(mp, xfs_blen_to_rtbxlen(mp, alen));
4165 out_unreserve_quota:
4166 if (XFS_IS_QUOTA_ON(mp))
4167 xfs_quota_unreserve_blkres(ip, alen);
4169 if (error == -ENOSPC || error == -EDQUOT) {
4170 trace_xfs_delalloc_enospc(ip, off, len);
4172 if (prealloc || use_cowextszhint) {
4173 /* retry without any preallocation */
4174 use_cowextszhint = false;
4184 struct xfs_bmalloca *bma)
4186 struct xfs_mount *mp = bma->ip->i_mount;
4187 int whichfork = xfs_bmapi_whichfork(bma->flags);
4188 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4191 ASSERT(bma->length > 0);
4192 ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN);
4194 if (bma->flags & XFS_BMAPI_CONTIG)
4195 bma->minlen = bma->length;
4199 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4201 * For the data and COW fork, the first data in the file is
4202 * treated differently to all other allocations. For the
4203 * attribute fork, we only need to ensure the allocated range
4204 * is not on the busy list.
4206 bma->datatype = XFS_ALLOC_NOBUSY;
4207 if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
4208 bma->datatype |= XFS_ALLOC_USERDATA;
4209 if (bma->offset == 0)
4210 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4212 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4213 error = xfs_bmap_isaeof(bma, whichfork);
4220 if ((bma->datatype & XFS_ALLOC_USERDATA) &&
4221 XFS_IS_REALTIME_INODE(bma->ip))
4222 error = xfs_bmap_rtalloc(bma);
4224 error = xfs_bmap_btalloc(bma);
4227 if (bma->blkno == NULLFSBLOCK)
4230 if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) {
4231 xfs_bmap_mark_sick(bma->ip, whichfork);
4232 return -EFSCORRUPTED;
4235 if (bma->flags & XFS_BMAPI_ZERO) {
4236 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4241 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4242 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4244 * Bump the number of extents we've allocated
4249 if (bma->cur && bma->wasdel)
4250 bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
4252 bma->got.br_startoff = bma->offset;
4253 bma->got.br_startblock = bma->blkno;
4254 bma->got.br_blockcount = bma->length;
4255 bma->got.br_state = XFS_EXT_NORM;
4257 if (bma->flags & XFS_BMAPI_PREALLOC)
4258 bma->got.br_state = XFS_EXT_UNWRITTEN;
4261 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4263 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4264 whichfork, &bma->icur, &bma->cur, &bma->got,
4265 &bma->logflags, bma->flags);
4270 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4271 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4272 * the neighbouring ones.
4274 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4276 ASSERT(bma->got.br_startoff <= bma->offset);
4277 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4278 bma->offset + bma->length);
4279 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4280 bma->got.br_state == XFS_EXT_UNWRITTEN);
4285 xfs_bmapi_convert_unwritten(
4286 struct xfs_bmalloca *bma,
4287 struct xfs_bmbt_irec *mval,
4291 int whichfork = xfs_bmapi_whichfork(flags);
4292 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4293 int tmp_logflags = 0;
4296 /* check if we need to do unwritten->real conversion */
4297 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4298 (flags & XFS_BMAPI_PREALLOC))
4301 /* check if we need to do real->unwritten conversion */
4302 if (mval->br_state == XFS_EXT_NORM &&
4303 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4304 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4308 * Modify (by adding) the state flag, if writing.
4310 ASSERT(mval->br_blockcount <= len);
4311 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4312 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4313 bma->ip, whichfork);
4315 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4316 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4319 * Before insertion into the bmbt, zero the range being converted
4322 if (flags & XFS_BMAPI_ZERO) {
4323 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4324 mval->br_blockcount);
4329 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4330 &bma->icur, &bma->cur, mval, &tmp_logflags);
4332 * Log the inode core unconditionally in the unwritten extent conversion
4333 * path because the conversion might not have done so (e.g., if the
4334 * extent count hasn't changed). We need to make sure the inode is dirty
4335 * in the transaction for the sake of fsync(), even if nothing has
4336 * changed, because fsync() will not force the log for this transaction
4337 * unless it sees the inode pinned.
4339 * Note: If we're only converting cow fork extents, there aren't
4340 * any on-disk updates to make, so we don't need to log anything.
4342 if (whichfork != XFS_COW_FORK)
4343 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4348 * Update our extent pointer, given that
4349 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4350 * of the neighbouring ones.
4352 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4355 * We may have combined previously unwritten space with written space,
4356 * so generate another request.
4358 if (mval->br_blockcount < len)
4365 struct xfs_trans *tp,
4366 struct xfs_inode *ip,
4369 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
4371 if (tp && tp->t_highest_agno != NULLAGNUMBER)
4373 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4375 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4379 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4380 * a case where the data is changed, there's an error, and it's not logged so we
4381 * don't shutdown when we should. Don't bother logging extents/btree changes if
4382 * we converted to the other format.
4386 struct xfs_bmalloca *bma,
4390 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4392 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4393 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4394 bma->logflags &= ~xfs_ilog_fext(whichfork);
4395 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4396 ifp->if_format != XFS_DINODE_FMT_BTREE)
4397 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4400 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4402 xfs_btree_del_cursor(bma->cur, error);
4406 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4407 * extent state if necessary. Details behaviour is controlled by the flags
4408 * parameter. Only allocates blocks from a single allocation group, to avoid
4411 * Returns 0 on success and places the extent mappings in mval. nmaps is used
4412 * as an input/output parameter where the caller specifies the maximum number
4413 * of mappings that may be returned and xfs_bmapi_write passes back the number
4414 * of mappings (including existing mappings) it found.
4416 * Returns a negative error code on failure, including -ENOSPC when it could not
4417 * allocate any blocks and -ENOSR when it did allocate blocks to convert a
4418 * delalloc range, but those blocks were before the passed in range.
4422 struct xfs_trans *tp, /* transaction pointer */
4423 struct xfs_inode *ip, /* incore inode */
4424 xfs_fileoff_t bno, /* starting file offs. mapped */
4425 xfs_filblks_t len, /* length to map in file */
4426 uint32_t flags, /* XFS_BMAPI_... */
4427 xfs_extlen_t total, /* total blocks needed */
4428 struct xfs_bmbt_irec *mval, /* output: map values */
4429 int *nmap) /* i/o: mval size/count */
4431 struct xfs_bmalloca bma = {
4436 struct xfs_mount *mp = ip->i_mount;
4437 int whichfork = xfs_bmapi_whichfork(flags);
4438 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4439 xfs_fileoff_t end; /* end of mapped file region */
4440 bool eof = false; /* after the end of extents */
4441 int error; /* error return */
4442 int n; /* current extent index */
4443 xfs_fileoff_t obno; /* old block number (offset) */
4446 xfs_fileoff_t orig_bno; /* original block number value */
4447 int orig_flags; /* original flags arg value */
4448 xfs_filblks_t orig_len; /* original value of len arg */
4449 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4450 int orig_nmap; /* original value of *nmap */
4460 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4463 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4464 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4465 ASSERT(!(flags & XFS_BMAPI_REMAP));
4467 /* zeroing is for currently only for data extents, not metadata */
4468 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4469 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4471 * we can allocate unwritten extents or pre-zero allocated blocks,
4472 * but it makes no sense to do both at once. This would result in
4473 * zeroing the unwritten extent twice, but it still being an
4474 * unwritten extent....
4476 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4477 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4479 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4480 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4481 xfs_bmap_mark_sick(ip, whichfork);
4482 return -EFSCORRUPTED;
4485 if (xfs_is_shutdown(mp))
4488 XFS_STATS_INC(mp, xs_blk_mapw);
4490 error = xfs_iread_extents(tp, ip, whichfork);
4494 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4496 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4497 bma.prev.br_startoff = NULLFILEOFF;
4498 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4503 while (bno < end && n < *nmap) {
4504 bool need_alloc = false, wasdelay = false;
4506 /* in hole or beyond EOF? */
4507 if (eof || bma.got.br_startoff > bno) {
4509 * CoW fork conversions should /never/ hit EOF or
4510 * holes. There should always be something for us
4513 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4514 (flags & XFS_BMAPI_COWFORK)));
4517 } else if (isnullstartblock(bma.got.br_startblock)) {
4522 * First, deal with the hole before the allocated space
4523 * that we found, if any.
4525 if (need_alloc || wasdelay) {
4527 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4528 bma.wasdel = wasdelay;
4533 * There's a 32/64 bit type mismatch between the
4534 * allocation length request (which can be 64 bits in
4535 * length) and the bma length request, which is
4536 * xfs_extlen_t and therefore 32 bits. Hence we have to
4537 * be careful and do the min() using the larger type to
4540 bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN);
4543 bma.length = XFS_FILBLKS_MIN(bma.length,
4544 bma.got.br_blockcount -
4545 (bno - bma.got.br_startoff));
4548 bma.length = XFS_FILBLKS_MIN(bma.length,
4549 bma.got.br_startoff - bno);
4552 ASSERT(bma.length > 0);
4553 error = xfs_bmapi_allocate(&bma);
4556 * If we already allocated space in a previous
4557 * iteration return what we go so far when
4558 * running out of space.
4560 if (error == -ENOSPC && bma.nallocs)
4566 * If this is a CoW allocation, record the data in
4567 * the refcount btree for orphan recovery.
4569 if (whichfork == XFS_COW_FORK)
4570 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4574 /* Deal with the allocated space we found. */
4575 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4578 /* Execute unwritten extent conversion if necessary */
4579 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4580 if (error == -EAGAIN)
4585 /* update the extent map to return */
4586 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4589 * If we're done, stop now. Stop when we've allocated
4590 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4591 * the transaction may get too big.
4593 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4596 /* Else go on to the next record. */
4598 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4602 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4607 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4608 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4609 xfs_bmapi_finish(&bma, whichfork, 0);
4610 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4614 * When converting delayed allocations, xfs_bmapi_allocate ignores
4615 * the passed in bno and always converts from the start of the found
4618 * To avoid a successful return with *nmap set to 0, return the magic
4619 * -ENOSR error code for this particular case so that the caller can
4623 ASSERT(bma.nallocs >= *nmap);
4629 xfs_bmapi_finish(&bma, whichfork, error);
4634 * Convert an existing delalloc extent to real blocks based on file offset. This
4635 * attempts to allocate the entire delalloc extent and may require multiple
4636 * invocations to allocate the target offset if a large enough physical extent
4640 xfs_bmapi_convert_one_delalloc(
4641 struct xfs_inode *ip,
4644 struct iomap *iomap,
4647 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4648 struct xfs_mount *mp = ip->i_mount;
4649 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4650 struct xfs_bmalloca bma = { NULL };
4652 struct xfs_trans *tp;
4655 if (whichfork == XFS_COW_FORK)
4656 flags |= IOMAP_F_SHARED;
4659 * Space for the extent and indirect blocks was reserved when the
4660 * delalloc extent was created so there's no need to do so here.
4662 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4663 XFS_TRANS_RESERVE, &tp);
4667 xfs_ilock(ip, XFS_ILOCK_EXCL);
4668 xfs_trans_ijoin(tp, ip, 0);
4670 error = xfs_iext_count_extend(tp, ip, whichfork,
4671 XFS_IEXT_ADD_NOSPLIT_CNT);
4673 goto out_trans_cancel;
4675 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4676 bma.got.br_startoff > offset_fsb) {
4678 * No extent found in the range we are trying to convert. This
4679 * should only happen for the COW fork, where another thread
4680 * might have moved the extent to the data fork in the meantime.
4682 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4684 goto out_trans_cancel;
4688 * If we find a real extent here we raced with another thread converting
4689 * the extent. Just return the real extent at this offset.
4691 if (!isnullstartblock(bma.got.br_startblock)) {
4692 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4693 xfs_iomap_inode_sequence(ip, flags));
4695 *seq = READ_ONCE(ifp->if_seq);
4696 goto out_trans_cancel;
4702 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4705 * Always allocate convert from the start of the delalloc extent even if
4706 * that is outside the passed in range to create large contiguous
4709 bma.offset = bma.got.br_startoff;
4710 bma.length = bma.got.br_blockcount;
4713 * When we're converting the delalloc reservations backing dirty pages
4714 * in the page cache, we must be careful about how we create the new
4717 * New CoW fork extents are created unwritten, turned into real extents
4718 * when we're about to write the data to disk, and mapped into the data
4719 * fork after the write finishes. End of story.
4721 * New data fork extents must be mapped in as unwritten and converted
4722 * to real extents after the write succeeds to avoid exposing stale
4723 * disk contents if we crash.
4725 bma.flags = XFS_BMAPI_PREALLOC;
4726 if (whichfork == XFS_COW_FORK)
4727 bma.flags |= XFS_BMAPI_COWFORK;
4729 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4730 bma.prev.br_startoff = NULLFILEOFF;
4732 error = xfs_bmapi_allocate(&bma);
4736 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4737 XFS_STATS_INC(mp, xs_xstrat_quick);
4739 ASSERT(!isnullstartblock(bma.got.br_startblock));
4740 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4741 xfs_iomap_inode_sequence(ip, flags));
4743 *seq = READ_ONCE(ifp->if_seq);
4745 if (whichfork == XFS_COW_FORK)
4746 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4748 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4753 xfs_bmapi_finish(&bma, whichfork, 0);
4754 error = xfs_trans_commit(tp);
4755 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4759 xfs_bmapi_finish(&bma, whichfork, error);
4761 xfs_trans_cancel(tp);
4762 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4767 * Pass in a dellalloc extent and convert it to real extents, return the real
4768 * extent that maps offset_fsb in iomap.
4771 xfs_bmapi_convert_delalloc(
4772 struct xfs_inode *ip,
4775 struct iomap *iomap,
4781 * Attempt to allocate whatever delalloc extent currently backs offset
4782 * and put the result into iomap. Allocate in a loop because it may
4783 * take several attempts to allocate real blocks for a contiguous
4784 * delalloc extent if free space is sufficiently fragmented.
4787 error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset,
4791 } while (iomap->offset + iomap->length <= offset);
4798 struct xfs_trans *tp,
4799 struct xfs_inode *ip,
4802 xfs_fsblock_t startblock,
4805 struct xfs_mount *mp = ip->i_mount;
4806 struct xfs_ifork *ifp;
4807 struct xfs_btree_cur *cur = NULL;
4808 struct xfs_bmbt_irec got;
4809 struct xfs_iext_cursor icur;
4810 int whichfork = xfs_bmapi_whichfork(flags);
4811 int logflags = 0, error;
4813 ifp = xfs_ifork_ptr(ip, whichfork);
4815 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4816 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4817 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4818 XFS_BMAPI_NORMAP)));
4819 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4820 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4822 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4823 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4824 xfs_bmap_mark_sick(ip, whichfork);
4825 return -EFSCORRUPTED;
4828 if (xfs_is_shutdown(mp))
4831 error = xfs_iread_extents(tp, ip, whichfork);
4835 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4836 /* make sure we only reflink into a hole. */
4837 ASSERT(got.br_startoff > bno);
4838 ASSERT(got.br_startoff - bno >= len);
4841 ip->i_nblocks += len;
4842 ip->i_delayed_blks -= len; /* see xfs_bmap_defer_add */
4843 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4845 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
4846 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4848 got.br_startoff = bno;
4849 got.br_startblock = startblock;
4850 got.br_blockcount = len;
4851 if (flags & XFS_BMAPI_PREALLOC)
4852 got.br_state = XFS_EXT_UNWRITTEN;
4854 got.br_state = XFS_EXT_NORM;
4856 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4857 &cur, &got, &logflags, flags);
4861 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4864 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4865 logflags &= ~XFS_ILOG_DEXT;
4866 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4867 logflags &= ~XFS_ILOG_DBROOT;
4870 xfs_trans_log_inode(tp, ip, logflags);
4872 xfs_btree_del_cursor(cur, error);
4877 * When a delalloc extent is split (e.g., due to a hole punch), the original
4878 * indlen reservation must be shared across the two new extents that are left
4881 * Given the original reservation and the worst case indlen for the two new
4882 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4883 * reservation fairly across the two new extents. If necessary, steal available
4884 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4885 * ores == 1). The number of stolen blocks is returned. The availability and
4886 * subsequent accounting of stolen blocks is the responsibility of the caller.
4889 xfs_bmap_split_indlen(
4890 xfs_filblks_t ores, /* original res. */
4891 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4892 xfs_filblks_t *indlen2) /* ext2 worst indlen */
4894 xfs_filblks_t len1 = *indlen1;
4895 xfs_filblks_t len2 = *indlen2;
4896 xfs_filblks_t nres = len1 + len2; /* new total res. */
4897 xfs_filblks_t resfactor;
4900 * We can't meet the total required reservation for the two extents.
4901 * Calculate the percent of the overall shortage between both extents
4902 * and apply this percentage to each of the requested indlen values.
4903 * This distributes the shortage fairly and reduces the chances that one
4904 * of the two extents is left with nothing when extents are repeatedly
4907 resfactor = (ores * 100);
4908 do_div(resfactor, nres);
4913 ASSERT(len1 + len2 <= ores);
4914 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4917 * Hand out the remainder to each extent. If one of the two reservations
4918 * is zero, we want to make sure that one gets a block first. The loop
4919 * below starts with len1, so hand len2 a block right off the bat if it
4922 ores -= (len1 + len2);
4923 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4924 if (ores && !len2 && *indlen2) {
4929 if (len1 < *indlen1) {
4935 if (len2 < *indlen2) {
4946 xfs_bmap_del_extent_delay(
4947 struct xfs_inode *ip,
4949 struct xfs_iext_cursor *icur,
4950 struct xfs_bmbt_irec *got,
4951 struct xfs_bmbt_irec *del)
4953 struct xfs_mount *mp = ip->i_mount;
4954 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4955 struct xfs_bmbt_irec new;
4956 int64_t da_old, da_new, da_diff = 0;
4957 xfs_fileoff_t del_endoff, got_endoff;
4958 xfs_filblks_t got_indlen, new_indlen, stolen = 0;
4959 uint32_t state = xfs_bmap_fork_to_state(whichfork);
4963 XFS_STATS_INC(mp, xs_del_exlist);
4965 isrt = xfs_ifork_is_realtime(ip, whichfork);
4966 del_endoff = del->br_startoff + del->br_blockcount;
4967 got_endoff = got->br_startoff + got->br_blockcount;
4968 da_old = startblockval(got->br_startblock);
4971 ASSERT(del->br_blockcount > 0);
4972 ASSERT(got->br_startoff <= del->br_startoff);
4973 ASSERT(got_endoff >= del_endoff);
4976 * Update the inode delalloc counter now and wait to update the
4977 * sb counters as we might have to borrow some blocks for the
4978 * indirect block accounting.
4980 xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4981 ip->i_delayed_blks -= del->br_blockcount;
4983 if (got->br_startoff == del->br_startoff)
4984 state |= BMAP_LEFT_FILLING;
4985 if (got_endoff == del_endoff)
4986 state |= BMAP_RIGHT_FILLING;
4988 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4989 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4991 * Matches the whole extent. Delete the entry.
4993 xfs_iext_remove(ip, icur, state);
4994 xfs_iext_prev(ifp, icur);
4996 case BMAP_LEFT_FILLING:
4998 * Deleting the first part of the extent.
5000 got->br_startoff = del_endoff;
5001 got->br_blockcount -= del->br_blockcount;
5002 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5003 got->br_blockcount), da_old);
5004 got->br_startblock = nullstartblock((int)da_new);
5005 xfs_iext_update_extent(ip, state, icur, got);
5007 case BMAP_RIGHT_FILLING:
5009 * Deleting the last part of the extent.
5011 got->br_blockcount = got->br_blockcount - del->br_blockcount;
5012 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5013 got->br_blockcount), da_old);
5014 got->br_startblock = nullstartblock((int)da_new);
5015 xfs_iext_update_extent(ip, state, icur, got);
5019 * Deleting the middle of the extent.
5021 * Distribute the original indlen reservation across the two new
5022 * extents. Steal blocks from the deleted extent if necessary.
5023 * Stealing blocks simply fudges the fdblocks accounting below.
5024 * Warn if either of the new indlen reservations is zero as this
5025 * can lead to delalloc problems.
5027 got->br_blockcount = del->br_startoff - got->br_startoff;
5028 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
5030 new.br_blockcount = got_endoff - del_endoff;
5031 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5033 WARN_ON_ONCE(!got_indlen || !new_indlen);
5035 * Steal as many blocks as we can to try and satisfy the worst
5036 * case indlen for both new extents.
5038 * However, we can't just steal reservations from the data
5039 * blocks if this is an RT inodes as the data and metadata
5040 * blocks come from different pools. We'll have to live with
5041 * under-filled indirect reservation in this case.
5043 da_new = got_indlen + new_indlen;
5044 if (da_new > da_old && !isrt) {
5045 stolen = XFS_FILBLKS_MIN(da_new - da_old,
5046 del->br_blockcount);
5049 if (da_new > da_old)
5050 xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen);
5051 da_new = got_indlen + new_indlen;
5053 got->br_startblock = nullstartblock((int)got_indlen);
5055 new.br_startoff = del_endoff;
5056 new.br_state = got->br_state;
5057 new.br_startblock = nullstartblock((int)new_indlen);
5059 xfs_iext_update_extent(ip, state, icur, got);
5060 xfs_iext_next(ifp, icur);
5061 xfs_iext_insert(ip, icur, &new, state);
5063 del->br_blockcount -= stolen;
5067 ASSERT(da_old >= da_new);
5068 da_diff = da_old - da_new;
5072 xfs_add_frextents(mp, xfs_blen_to_rtbxlen(mp, del->br_blockcount));
5074 fdblocks += del->br_blockcount;
5076 xfs_add_fdblocks(mp, fdblocks);
5077 xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff);
5081 xfs_bmap_del_extent_cow(
5082 struct xfs_inode *ip,
5083 struct xfs_iext_cursor *icur,
5084 struct xfs_bmbt_irec *got,
5085 struct xfs_bmbt_irec *del)
5087 struct xfs_mount *mp = ip->i_mount;
5088 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
5089 struct xfs_bmbt_irec new;
5090 xfs_fileoff_t del_endoff, got_endoff;
5091 uint32_t state = BMAP_COWFORK;
5093 XFS_STATS_INC(mp, xs_del_exlist);
5095 del_endoff = del->br_startoff + del->br_blockcount;
5096 got_endoff = got->br_startoff + got->br_blockcount;
5098 ASSERT(del->br_blockcount > 0);
5099 ASSERT(got->br_startoff <= del->br_startoff);
5100 ASSERT(got_endoff >= del_endoff);
5101 ASSERT(!isnullstartblock(got->br_startblock));
5103 if (got->br_startoff == del->br_startoff)
5104 state |= BMAP_LEFT_FILLING;
5105 if (got_endoff == del_endoff)
5106 state |= BMAP_RIGHT_FILLING;
5108 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5109 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5111 * Matches the whole extent. Delete the entry.
5113 xfs_iext_remove(ip, icur, state);
5114 xfs_iext_prev(ifp, icur);
5116 case BMAP_LEFT_FILLING:
5118 * Deleting the first part of the extent.
5120 got->br_startoff = del_endoff;
5121 got->br_blockcount -= del->br_blockcount;
5122 got->br_startblock = del->br_startblock + del->br_blockcount;
5123 xfs_iext_update_extent(ip, state, icur, got);
5125 case BMAP_RIGHT_FILLING:
5127 * Deleting the last part of the extent.
5129 got->br_blockcount -= del->br_blockcount;
5130 xfs_iext_update_extent(ip, state, icur, got);
5134 * Deleting the middle of the extent.
5136 got->br_blockcount = del->br_startoff - got->br_startoff;
5138 new.br_startoff = del_endoff;
5139 new.br_blockcount = got_endoff - del_endoff;
5140 new.br_state = got->br_state;
5141 new.br_startblock = del->br_startblock + del->br_blockcount;
5143 xfs_iext_update_extent(ip, state, icur, got);
5144 xfs_iext_next(ifp, icur);
5145 xfs_iext_insert(ip, icur, &new, state);
5148 ip->i_delayed_blks -= del->br_blockcount;
5152 xfs_bmap_free_rtblocks(
5153 struct xfs_trans *tp,
5154 struct xfs_bmbt_irec *del)
5156 struct xfs_rtgroup *rtg;
5159 rtg = xfs_rtgroup_grab(tp->t_mountp, 0);
5164 * Ensure the bitmap and summary inodes are locked and joined to the
5165 * transaction before modifying them.
5167 if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) {
5168 tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED;
5169 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP);
5170 xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_BITMAP);
5173 error = xfs_rtfree_blocks(tp, rtg, del->br_startblock,
5174 del->br_blockcount);
5175 xfs_rtgroup_rele(rtg);
5180 * Called by xfs_bmapi to update file extent records and the btree
5181 * after removing space.
5183 STATIC int /* error */
5184 xfs_bmap_del_extent_real(
5185 xfs_inode_t *ip, /* incore inode pointer */
5186 xfs_trans_t *tp, /* current transaction pointer */
5187 struct xfs_iext_cursor *icur,
5188 struct xfs_btree_cur *cur, /* if null, not a btree */
5189 xfs_bmbt_irec_t *del, /* data to remove from extents */
5190 int *logflagsp, /* inode logging flags */
5191 int whichfork, /* data or attr fork */
5192 uint32_t bflags) /* bmapi flags */
5194 xfs_fsblock_t del_endblock=0; /* first block past del */
5195 xfs_fileoff_t del_endoff; /* first offset past del */
5196 int error = 0; /* error return value */
5197 struct xfs_bmbt_irec got; /* current extent entry */
5198 xfs_fileoff_t got_endoff; /* first offset past got */
5199 int i; /* temp state */
5200 struct xfs_ifork *ifp; /* inode fork pointer */
5201 xfs_mount_t *mp; /* mount structure */
5202 xfs_filblks_t nblks; /* quota/sb block count */
5203 xfs_bmbt_irec_t new; /* new record to be inserted */
5205 uint qfield; /* quota field to update */
5206 uint32_t state = xfs_bmap_fork_to_state(whichfork);
5207 struct xfs_bmbt_irec old;
5212 XFS_STATS_INC(mp, xs_del_exlist);
5214 ifp = xfs_ifork_ptr(ip, whichfork);
5215 ASSERT(del->br_blockcount > 0);
5216 xfs_iext_get_extent(ifp, icur, &got);
5217 ASSERT(got.br_startoff <= del->br_startoff);
5218 del_endoff = del->br_startoff + del->br_blockcount;
5219 got_endoff = got.br_startoff + got.br_blockcount;
5220 ASSERT(got_endoff >= del_endoff);
5221 ASSERT(!isnullstartblock(got.br_startblock));
5225 * If it's the case where the directory code is running with no block
5226 * reservation, and the deleted block is in the middle of its extent,
5227 * and the resulting insert of an extent would cause transformation to
5228 * btree format, then reject it. The calling code will then swap blocks
5229 * around instead. We have to do this now, rather than waiting for the
5230 * conversion to btree format, since the transaction will be dirty then.
5232 if (tp->t_blk_res == 0 &&
5233 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5234 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5235 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5238 *logflagsp = XFS_ILOG_CORE;
5239 if (xfs_ifork_is_realtime(ip, whichfork))
5240 qfield = XFS_TRANS_DQ_RTBCOUNT;
5242 qfield = XFS_TRANS_DQ_BCOUNT;
5243 nblks = del->br_blockcount;
5245 del_endblock = del->br_startblock + del->br_blockcount;
5247 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5250 if (XFS_IS_CORRUPT(mp, i != 1)) {
5251 xfs_btree_mark_sick(cur);
5252 return -EFSCORRUPTED;
5256 if (got.br_startoff == del->br_startoff)
5257 state |= BMAP_LEFT_FILLING;
5258 if (got_endoff == del_endoff)
5259 state |= BMAP_RIGHT_FILLING;
5261 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5262 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5264 * Matches the whole extent. Delete the entry.
5266 xfs_iext_remove(ip, icur, state);
5267 xfs_iext_prev(ifp, icur);
5270 *logflagsp |= XFS_ILOG_CORE;
5272 *logflagsp |= xfs_ilog_fext(whichfork);
5275 if ((error = xfs_btree_delete(cur, &i)))
5277 if (XFS_IS_CORRUPT(mp, i != 1)) {
5278 xfs_btree_mark_sick(cur);
5279 return -EFSCORRUPTED;
5282 case BMAP_LEFT_FILLING:
5284 * Deleting the first part of the extent.
5286 got.br_startoff = del_endoff;
5287 got.br_startblock = del_endblock;
5288 got.br_blockcount -= del->br_blockcount;
5289 xfs_iext_update_extent(ip, state, icur, &got);
5291 *logflagsp |= xfs_ilog_fext(whichfork);
5294 error = xfs_bmbt_update(cur, &got);
5298 case BMAP_RIGHT_FILLING:
5300 * Deleting the last part of the extent.
5302 got.br_blockcount -= del->br_blockcount;
5303 xfs_iext_update_extent(ip, state, icur, &got);
5305 *logflagsp |= xfs_ilog_fext(whichfork);
5308 error = xfs_bmbt_update(cur, &got);
5314 * Deleting the middle of the extent.
5319 got.br_blockcount = del->br_startoff - got.br_startoff;
5320 xfs_iext_update_extent(ip, state, icur, &got);
5322 new.br_startoff = del_endoff;
5323 new.br_blockcount = got_endoff - del_endoff;
5324 new.br_state = got.br_state;
5325 new.br_startblock = del_endblock;
5327 *logflagsp |= XFS_ILOG_CORE;
5329 error = xfs_bmbt_update(cur, &got);
5332 error = xfs_btree_increment(cur, 0, &i);
5335 cur->bc_rec.b = new;
5336 error = xfs_btree_insert(cur, &i);
5337 if (error && error != -ENOSPC)
5340 * If get no-space back from btree insert, it tried a
5341 * split, and we have a zero block reservation. Fix up
5342 * our state and return the error.
5344 if (error == -ENOSPC) {
5346 * Reset the cursor, don't trust it after any
5349 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5352 if (XFS_IS_CORRUPT(mp, i != 1)) {
5353 xfs_btree_mark_sick(cur);
5354 return -EFSCORRUPTED;
5357 * Update the btree record back
5358 * to the original value.
5360 error = xfs_bmbt_update(cur, &old);
5364 * Reset the extent record back
5365 * to the original value.
5367 xfs_iext_update_extent(ip, state, icur, &old);
5371 if (XFS_IS_CORRUPT(mp, i != 1)) {
5372 xfs_btree_mark_sick(cur);
5373 return -EFSCORRUPTED;
5376 *logflagsp |= xfs_ilog_fext(whichfork);
5379 xfs_iext_next(ifp, icur);
5380 xfs_iext_insert(ip, icur, &new, state);
5384 /* remove reverse mapping */
5385 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5388 * If we need to, add to list of extents to delete.
5390 if (!(bflags & XFS_BMAPI_REMAP)) {
5391 bool isrt = xfs_ifork_is_realtime(ip, whichfork);
5393 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5394 xfs_refcount_decrease_extent(tp, del);
5395 } else if (isrt && !xfs_has_rtgroups(mp)) {
5396 error = xfs_bmap_free_rtblocks(tp, del);
5398 unsigned int efi_flags = 0;
5400 if ((bflags & XFS_BMAPI_NODISCARD) ||
5401 del->br_state == XFS_EXT_UNWRITTEN)
5402 efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
5405 * Historically, we did not use EFIs to free realtime
5406 * extents. However, when reverse mapping is enabled,
5407 * we must maintain the same order of operations as the
5408 * data device, which is: Remove the file mapping,
5409 * remove the reverse mapping, and then free the
5410 * blocks. Reflink for realtime volumes requires the
5411 * same sort of ordering. Both features rely on
5412 * rtgroups, so let's gate rt EFI usage on rtgroups.
5415 efi_flags |= XFS_FREE_EXTENT_REALTIME;
5417 error = xfs_free_extent_later(tp, del->br_startblock,
5418 del->br_blockcount, NULL,
5419 XFS_AG_RESV_NONE, efi_flags);
5426 * Adjust inode # blocks in the file.
5429 ip->i_nblocks -= nblks;
5431 * Adjust quota data.
5433 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5434 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5440 * Unmap (remove) blocks from a file.
5441 * If nexts is nonzero then the number of extents to remove is limited to
5442 * that value. If not all extents in the block range can be removed then
5447 struct xfs_trans *tp, /* transaction pointer */
5448 struct xfs_inode *ip, /* incore inode */
5449 xfs_fileoff_t start, /* first file offset deleted */
5450 xfs_filblks_t *rlen, /* i/o: amount remaining */
5451 uint32_t flags, /* misc flags */
5452 xfs_extnum_t nexts) /* number of extents max */
5454 struct xfs_btree_cur *cur; /* bmap btree cursor */
5455 struct xfs_bmbt_irec del; /* extent being deleted */
5456 int error; /* error return value */
5457 xfs_extnum_t extno; /* extent number in list */
5458 struct xfs_bmbt_irec got; /* current extent record */
5459 struct xfs_ifork *ifp; /* inode fork pointer */
5460 int isrt; /* freeing in rt area */
5461 int logflags; /* transaction logging flags */
5462 xfs_extlen_t mod; /* rt extent offset */
5463 struct xfs_mount *mp = ip->i_mount;
5464 int tmp_logflags; /* partial logging flags */
5465 int wasdel; /* was a delayed alloc extent */
5466 int whichfork; /* data or attribute fork */
5467 xfs_filblks_t len = *rlen; /* length to unmap in file */
5469 struct xfs_iext_cursor icur;
5472 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5474 whichfork = xfs_bmapi_whichfork(flags);
5475 ASSERT(whichfork != XFS_COW_FORK);
5476 ifp = xfs_ifork_ptr(ip, whichfork);
5477 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5478 xfs_bmap_mark_sick(ip, whichfork);
5479 return -EFSCORRUPTED;
5481 if (xfs_is_shutdown(mp))
5484 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
5488 error = xfs_iread_extents(tp, ip, whichfork);
5492 if (xfs_iext_count(ifp) == 0) {
5496 XFS_STATS_INC(mp, xs_blk_unmap);
5497 isrt = xfs_ifork_is_realtime(ip, whichfork);
5500 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5507 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5508 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5509 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5514 while (end != (xfs_fileoff_t)-1 && end >= start &&
5515 (nexts == 0 || extno < nexts)) {
5517 * Is the found extent after a hole in which end lives?
5518 * Just back up to the previous extent, if so.
5520 if (got.br_startoff > end &&
5521 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5526 * Is the last block of this extent before the range
5527 * we're supposed to delete? If so, we're done.
5529 end = XFS_FILEOFF_MIN(end,
5530 got.br_startoff + got.br_blockcount - 1);
5534 * Then deal with the (possibly delayed) allocated space
5538 wasdel = isnullstartblock(del.br_startblock);
5540 if (got.br_startoff < start) {
5541 del.br_startoff = start;
5542 del.br_blockcount -= start - got.br_startoff;
5544 del.br_startblock += start - got.br_startoff;
5546 if (del.br_startoff + del.br_blockcount > end + 1)
5547 del.br_blockcount = end + 1 - del.br_startoff;
5549 if (!isrt || (flags & XFS_BMAPI_REMAP))
5552 mod = xfs_rtb_to_rtxoff(mp,
5553 del.br_startblock + del.br_blockcount);
5556 * Realtime extent not lined up at the end.
5557 * The extent could have been split into written
5558 * and unwritten pieces, or we could just be
5559 * unmapping part of it. But we can't really
5560 * get rid of part of a realtime extent.
5562 if (del.br_state == XFS_EXT_UNWRITTEN) {
5564 * This piece is unwritten, or we're not
5565 * using unwritten extents. Skip over it.
5567 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
5568 end -= mod > del.br_blockcount ?
5569 del.br_blockcount : mod;
5570 if (end < got.br_startoff &&
5571 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5578 * It's written, turn it unwritten.
5579 * This is better than zeroing it.
5581 ASSERT(del.br_state == XFS_EXT_NORM);
5582 ASSERT(tp->t_blk_res > 0);
5584 * If this spans a realtime extent boundary,
5585 * chop it back to the start of the one we end at.
5587 if (del.br_blockcount > mod) {
5588 del.br_startoff += del.br_blockcount - mod;
5589 del.br_startblock += del.br_blockcount - mod;
5590 del.br_blockcount = mod;
5592 del.br_state = XFS_EXT_UNWRITTEN;
5593 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5594 whichfork, &icur, &cur, &del,
5601 mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
5603 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5606 * Realtime extent is lined up at the end but not
5607 * at the front. We'll get rid of full extents if
5610 if (del.br_blockcount > off) {
5611 del.br_blockcount -= off;
5612 del.br_startoff += off;
5613 del.br_startblock += off;
5614 } else if (del.br_startoff == start &&
5615 (del.br_state == XFS_EXT_UNWRITTEN ||
5616 tp->t_blk_res == 0)) {
5618 * Can't make it unwritten. There isn't
5619 * a full extent here so just skip it.
5621 ASSERT(end >= del.br_blockcount);
5622 end -= del.br_blockcount;
5623 if (got.br_startoff > end &&
5624 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5629 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5630 struct xfs_bmbt_irec prev;
5631 xfs_fileoff_t unwrite_start;
5634 * This one is already unwritten.
5635 * It must have a written left neighbor.
5636 * Unwrite the killed part of that one and
5639 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5641 ASSERT(prev.br_state == XFS_EXT_NORM);
5642 ASSERT(!isnullstartblock(prev.br_startblock));
5643 ASSERT(del.br_startblock ==
5644 prev.br_startblock + prev.br_blockcount);
5645 unwrite_start = max3(start,
5646 del.br_startoff - mod,
5648 mod = unwrite_start - prev.br_startoff;
5649 prev.br_startoff = unwrite_start;
5650 prev.br_startblock += mod;
5651 prev.br_blockcount -= mod;
5652 prev.br_state = XFS_EXT_UNWRITTEN;
5653 error = xfs_bmap_add_extent_unwritten_real(tp,
5654 ip, whichfork, &icur, &cur,
5660 ASSERT(del.br_state == XFS_EXT_NORM);
5661 del.br_state = XFS_EXT_UNWRITTEN;
5662 error = xfs_bmap_add_extent_unwritten_real(tp,
5663 ip, whichfork, &icur, &cur,
5673 xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, &del);
5675 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5676 &del, &tmp_logflags, whichfork,
5678 logflags |= tmp_logflags;
5683 end = del.br_startoff - 1;
5686 * If not done go on to the next (previous) record.
5688 if (end != (xfs_fileoff_t)-1 && end >= start) {
5689 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5690 (got.br_startoff > end &&
5691 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5698 if (done || end == (xfs_fileoff_t)-1 || end < start)
5701 *rlen = end - start + 1;
5704 * Convert to a btree if necessary.
5706 if (xfs_bmap_needs_btree(ip, whichfork)) {
5707 ASSERT(cur == NULL);
5708 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5709 &tmp_logflags, whichfork);
5710 logflags |= tmp_logflags;
5712 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5718 * Log everything. Do this after conversion, there's no point in
5719 * logging the extent records if we've converted to btree format.
5721 if ((logflags & xfs_ilog_fext(whichfork)) &&
5722 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5723 logflags &= ~xfs_ilog_fext(whichfork);
5724 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5725 ifp->if_format != XFS_DINODE_FMT_BTREE)
5726 logflags &= ~xfs_ilog_fbroot(whichfork);
5728 * Log inode even in the error case, if the transaction
5729 * is dirty we'll need to shut down the filesystem.
5732 xfs_trans_log_inode(tp, ip, logflags);
5735 cur->bc_bmap.allocated = 0;
5736 xfs_btree_del_cursor(cur, error);
5741 /* Unmap a range of a file. */
5745 struct xfs_inode *ip,
5754 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5760 * Determine whether an extent shift can be accomplished by a merge with the
5761 * extent that precedes the target hole of the shift.
5765 struct xfs_inode *ip,
5767 struct xfs_bmbt_irec *left, /* preceding extent */
5768 struct xfs_bmbt_irec *got, /* current extent to shift */
5769 xfs_fileoff_t shift) /* shift fsb */
5771 xfs_fileoff_t startoff;
5773 startoff = got->br_startoff - shift;
5776 * The extent, once shifted, must be adjacent in-file and on-disk with
5777 * the preceding extent.
5779 if ((left->br_startoff + left->br_blockcount != startoff) ||
5780 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5781 (left->br_state != got->br_state) ||
5782 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN) ||
5783 !xfs_bmap_same_rtgroup(ip, whichfork, left, got))
5790 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5791 * hole in the file. If an extent shift would result in the extent being fully
5792 * adjacent to the extent that currently precedes the hole, we can merge with
5793 * the preceding extent rather than do the shift.
5795 * This function assumes the caller has verified a shift-by-merge is possible
5796 * with the provided extents via xfs_bmse_can_merge().
5800 struct xfs_trans *tp,
5801 struct xfs_inode *ip,
5803 xfs_fileoff_t shift, /* shift fsb */
5804 struct xfs_iext_cursor *icur,
5805 struct xfs_bmbt_irec *got, /* extent to shift */
5806 struct xfs_bmbt_irec *left, /* preceding extent */
5807 struct xfs_btree_cur *cur,
5808 int *logflags) /* output */
5810 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5811 struct xfs_bmbt_irec new;
5812 xfs_filblks_t blockcount;
5814 struct xfs_mount *mp = ip->i_mount;
5816 blockcount = left->br_blockcount + got->br_blockcount;
5818 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5819 ASSERT(xfs_bmse_can_merge(ip, whichfork, left, got, shift));
5822 new.br_blockcount = blockcount;
5825 * Update the on-disk extent count, the btree if necessary and log the
5829 *logflags |= XFS_ILOG_CORE;
5831 *logflags |= XFS_ILOG_DEXT;
5835 /* lookup and remove the extent to merge */
5836 error = xfs_bmbt_lookup_eq(cur, got, &i);
5839 if (XFS_IS_CORRUPT(mp, i != 1)) {
5840 xfs_btree_mark_sick(cur);
5841 return -EFSCORRUPTED;
5844 error = xfs_btree_delete(cur, &i);
5847 if (XFS_IS_CORRUPT(mp, i != 1)) {
5848 xfs_btree_mark_sick(cur);
5849 return -EFSCORRUPTED;
5852 /* lookup and update size of the previous extent */
5853 error = xfs_bmbt_lookup_eq(cur, left, &i);
5856 if (XFS_IS_CORRUPT(mp, i != 1)) {
5857 xfs_btree_mark_sick(cur);
5858 return -EFSCORRUPTED;
5861 error = xfs_bmbt_update(cur, &new);
5865 /* change to extent format if required after extent removal */
5866 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5871 xfs_iext_remove(ip, icur, 0);
5872 xfs_iext_prev(ifp, icur);
5873 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5876 /* update reverse mapping. rmap functions merge the rmaps for us */
5877 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5878 memcpy(&new, got, sizeof(new));
5879 new.br_startoff = left->br_startoff + left->br_blockcount;
5880 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5885 xfs_bmap_shift_update_extent(
5886 struct xfs_trans *tp,
5887 struct xfs_inode *ip,
5889 struct xfs_iext_cursor *icur,
5890 struct xfs_bmbt_irec *got,
5891 struct xfs_btree_cur *cur,
5893 xfs_fileoff_t startoff)
5895 struct xfs_mount *mp = ip->i_mount;
5896 struct xfs_bmbt_irec prev = *got;
5899 *logflags |= XFS_ILOG_CORE;
5901 got->br_startoff = startoff;
5904 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5907 if (XFS_IS_CORRUPT(mp, i != 1)) {
5908 xfs_btree_mark_sick(cur);
5909 return -EFSCORRUPTED;
5912 error = xfs_bmbt_update(cur, got);
5916 *logflags |= XFS_ILOG_DEXT;
5919 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5922 /* update reverse mapping */
5923 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5924 xfs_rmap_map_extent(tp, ip, whichfork, got);
5929 xfs_bmap_collapse_extents(
5930 struct xfs_trans *tp,
5931 struct xfs_inode *ip,
5932 xfs_fileoff_t *next_fsb,
5933 xfs_fileoff_t offset_shift_fsb,
5936 int whichfork = XFS_DATA_FORK;
5937 struct xfs_mount *mp = ip->i_mount;
5938 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5939 struct xfs_btree_cur *cur = NULL;
5940 struct xfs_bmbt_irec got, prev;
5941 struct xfs_iext_cursor icur;
5942 xfs_fileoff_t new_startoff;
5946 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5947 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5948 xfs_bmap_mark_sick(ip, whichfork);
5949 return -EFSCORRUPTED;
5952 if (xfs_is_shutdown(mp))
5955 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5957 error = xfs_iread_extents(tp, ip, whichfork);
5961 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
5962 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5964 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5968 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5969 xfs_bmap_mark_sick(ip, whichfork);
5970 error = -EFSCORRUPTED;
5974 new_startoff = got.br_startoff - offset_shift_fsb;
5975 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5976 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5981 if (xfs_bmse_can_merge(ip, whichfork, &prev, &got,
5982 offset_shift_fsb)) {
5983 error = xfs_bmse_merge(tp, ip, whichfork,
5984 offset_shift_fsb, &icur, &got, &prev,
5991 if (got.br_startoff < offset_shift_fsb) {
5997 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5998 cur, &logflags, new_startoff);
6003 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
6008 *next_fsb = got.br_startoff;
6011 xfs_btree_del_cursor(cur, error);
6013 xfs_trans_log_inode(tp, ip, logflags);
6017 /* Make sure we won't be right-shifting an extent past the maximum bound. */
6019 xfs_bmap_can_insert_extents(
6020 struct xfs_inode *ip,
6022 xfs_fileoff_t shift)
6024 struct xfs_bmbt_irec got;
6028 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
6030 if (xfs_is_shutdown(ip->i_mount))
6033 xfs_ilock(ip, XFS_ILOCK_EXCL);
6034 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
6035 if (!error && !is_empty && got.br_startoff >= off &&
6036 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
6038 xfs_iunlock(ip, XFS_ILOCK_EXCL);
6044 xfs_bmap_insert_extents(
6045 struct xfs_trans *tp,
6046 struct xfs_inode *ip,
6047 xfs_fileoff_t *next_fsb,
6048 xfs_fileoff_t offset_shift_fsb,
6050 xfs_fileoff_t stop_fsb)
6052 int whichfork = XFS_DATA_FORK;
6053 struct xfs_mount *mp = ip->i_mount;
6054 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
6055 struct xfs_btree_cur *cur = NULL;
6056 struct xfs_bmbt_irec got, next;
6057 struct xfs_iext_cursor icur;
6058 xfs_fileoff_t new_startoff;
6062 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6063 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6064 xfs_bmap_mark_sick(ip, whichfork);
6065 return -EFSCORRUPTED;
6068 if (xfs_is_shutdown(mp))
6071 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
6073 error = xfs_iread_extents(tp, ip, whichfork);
6077 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
6078 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6080 if (*next_fsb == NULLFSBLOCK) {
6081 xfs_iext_last(ifp, &icur);
6082 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
6083 stop_fsb > got.br_startoff) {
6088 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
6093 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
6094 xfs_bmap_mark_sick(ip, whichfork);
6095 error = -EFSCORRUPTED;
6099 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
6100 xfs_bmap_mark_sick(ip, whichfork);
6101 error = -EFSCORRUPTED;
6105 new_startoff = got.br_startoff + offset_shift_fsb;
6106 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
6107 if (new_startoff + got.br_blockcount > next.br_startoff) {
6113 * Unlike a left shift (which involves a hole punch), a right
6114 * shift does not modify extent neighbors in any way. We should
6115 * never find mergeable extents in this scenario. Check anyways
6116 * and warn if we encounter two extents that could be one.
6118 if (xfs_bmse_can_merge(ip, whichfork, &got, &next,
6123 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6124 cur, &logflags, new_startoff);
6128 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6129 stop_fsb >= got.br_startoff + got.br_blockcount) {
6134 *next_fsb = got.br_startoff;
6137 xfs_btree_del_cursor(cur, error);
6139 xfs_trans_log_inode(tp, ip, logflags);
6144 * Splits an extent into two extents at split_fsb block such that it is the
6145 * first block of the current_ext. @ext is a target extent to be split.
6146 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6147 * hole or the first block of extents, just return 0.
6150 xfs_bmap_split_extent(
6151 struct xfs_trans *tp,
6152 struct xfs_inode *ip,
6153 xfs_fileoff_t split_fsb)
6155 int whichfork = XFS_DATA_FORK;
6156 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
6157 struct xfs_btree_cur *cur = NULL;
6158 struct xfs_bmbt_irec got;
6159 struct xfs_bmbt_irec new; /* split extent */
6160 struct xfs_mount *mp = ip->i_mount;
6161 xfs_fsblock_t gotblkcnt; /* new block count for got */
6162 struct xfs_iext_cursor icur;
6167 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6168 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6169 xfs_bmap_mark_sick(ip, whichfork);
6170 return -EFSCORRUPTED;
6173 if (xfs_is_shutdown(mp))
6176 /* Read in all the extents */
6177 error = xfs_iread_extents(tp, ip, whichfork);
6182 * If there are not extents, or split_fsb lies in a hole we are done.
6184 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6185 got.br_startoff >= split_fsb)
6188 gotblkcnt = split_fsb - got.br_startoff;
6189 new.br_startoff = split_fsb;
6190 new.br_startblock = got.br_startblock + gotblkcnt;
6191 new.br_blockcount = got.br_blockcount - gotblkcnt;
6192 new.br_state = got.br_state;
6194 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6195 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6196 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6199 if (XFS_IS_CORRUPT(mp, i != 1)) {
6200 xfs_btree_mark_sick(cur);
6201 error = -EFSCORRUPTED;
6206 got.br_blockcount = gotblkcnt;
6207 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6210 logflags = XFS_ILOG_CORE;
6212 error = xfs_bmbt_update(cur, &got);
6216 logflags |= XFS_ILOG_DEXT;
6218 /* Add new extent */
6219 xfs_iext_next(ifp, &icur);
6220 xfs_iext_insert(ip, &icur, &new, 0);
6224 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6227 if (XFS_IS_CORRUPT(mp, i != 0)) {
6228 xfs_btree_mark_sick(cur);
6229 error = -EFSCORRUPTED;
6232 error = xfs_btree_insert(cur, &i);
6235 if (XFS_IS_CORRUPT(mp, i != 1)) {
6236 xfs_btree_mark_sick(cur);
6237 error = -EFSCORRUPTED;
6243 * Convert to a btree if necessary.
6245 if (xfs_bmap_needs_btree(ip, whichfork)) {
6246 int tmp_logflags; /* partial log flag return val */
6248 ASSERT(cur == NULL);
6249 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6250 &tmp_logflags, whichfork);
6251 logflags |= tmp_logflags;
6256 cur->bc_bmap.allocated = 0;
6257 xfs_btree_del_cursor(cur, error);
6261 xfs_trans_log_inode(tp, ip, logflags);
6265 /* Record a bmap intent. */
6268 struct xfs_trans *tp,
6269 enum xfs_bmap_intent_type type,
6270 struct xfs_inode *ip,
6272 struct xfs_bmbt_irec *bmap)
6274 struct xfs_bmap_intent *bi;
6276 if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6277 bmap->br_startblock == HOLESTARTBLOCK ||
6278 bmap->br_startblock == DELAYSTARTBLOCK)
6281 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
6282 INIT_LIST_HEAD(&bi->bi_list);
6285 bi->bi_whichfork = whichfork;
6286 bi->bi_bmap = *bmap;
6288 xfs_bmap_defer_add(tp, bi);
6291 /* Map an extent into a file. */
6293 xfs_bmap_map_extent(
6294 struct xfs_trans *tp,
6295 struct xfs_inode *ip,
6297 struct xfs_bmbt_irec *PREV)
6299 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
6302 /* Unmap an extent out of a file. */
6304 xfs_bmap_unmap_extent(
6305 struct xfs_trans *tp,
6306 struct xfs_inode *ip,
6308 struct xfs_bmbt_irec *PREV)
6310 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
6314 * Process one of the deferred bmap operations. We pass back the
6315 * btree cursor to maintain our lock on the bmapbt between calls.
6318 xfs_bmap_finish_one(
6319 struct xfs_trans *tp,
6320 struct xfs_bmap_intent *bi)
6322 struct xfs_bmbt_irec *bmap = &bi->bi_bmap;
6326 if (bi->bi_whichfork == XFS_ATTR_FORK)
6327 flags |= XFS_BMAPI_ATTRFORK;
6329 ASSERT(tp->t_highest_agno == NULLAGNUMBER);
6331 trace_xfs_bmap_deferred(bi);
6333 if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
6336 switch (bi->bi_type) {
6338 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6339 flags |= XFS_BMAPI_PREALLOC;
6340 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
6341 bmap->br_blockcount, bmap->br_startblock,
6343 bmap->br_blockcount = 0;
6345 case XFS_BMAP_UNMAP:
6346 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
6347 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6352 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
6353 error = -EFSCORRUPTED;
6359 /* Check that an extent does not have invalid flags or bad ranges. */
6361 xfs_bmap_validate_extent_raw(
6362 struct xfs_mount *mp,
6365 struct xfs_bmbt_irec *irec)
6367 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6368 return __this_address;
6370 if (rtfile && whichfork == XFS_DATA_FORK) {
6371 if (!xfs_verify_rtbext(mp, irec->br_startblock,
6372 irec->br_blockcount))
6373 return __this_address;
6375 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6376 irec->br_blockcount))
6377 return __this_address;
6379 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6380 return __this_address;
6385 xfs_bmap_intent_init_cache(void)
6387 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6388 sizeof(struct xfs_bmap_intent),
6391 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6395 xfs_bmap_intent_destroy_cache(void)
6397 kmem_cache_destroy(xfs_bmap_intent_cache);
6398 xfs_bmap_intent_cache = NULL;
6401 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6403 xfs_bmap_validate_extent(
6404 struct xfs_inode *ip,
6406 struct xfs_bmbt_irec *irec)
6408 return xfs_bmap_validate_extent_raw(ip->i_mount,
6409 XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6413 * Used in xfs_itruncate_extents(). This is the maximum number of extents
6414 * freed from a file in a single transaction.
6416 #define XFS_ITRUNC_MAX_EXTENTS 2
6419 * Unmap every extent in part of an inode's fork. We don't do any higher level
6420 * invalidation work at all.
6424 struct xfs_trans **tpp,
6425 struct xfs_inode *ip,
6427 xfs_fileoff_t startoff,
6428 xfs_fileoff_t endoff)
6430 xfs_filblks_t unmap_len = endoff - startoff + 1;
6433 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
6435 while (unmap_len > 0) {
6436 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6437 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6438 XFS_ITRUNC_MAX_EXTENTS);
6442 /* free the just unmapped extents */
6443 error = xfs_defer_finish(tpp);
6452 struct xfs_bmap_query_range {
6453 xfs_bmap_query_range_fn fn;
6457 /* Format btree record and pass to our callback. */
6459 xfs_bmap_query_range_helper(
6460 struct xfs_btree_cur *cur,
6461 const union xfs_btree_rec *rec,
6464 struct xfs_bmap_query_range *query = priv;
6465 struct xfs_bmbt_irec irec;
6468 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6469 fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6472 xfs_btree_mark_sick(cur);
6473 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6474 cur->bc_ino.whichfork, fa, &irec);
6477 return query->fn(cur, &irec, query->priv);
6480 /* Find all bmaps. */
6483 struct xfs_btree_cur *cur,
6484 xfs_bmap_query_range_fn fn,
6487 struct xfs_bmap_query_range query = {
6492 return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
6495 /* Helper function to extract extent size hint from inode */
6498 struct xfs_inode *ip)
6501 * No point in aligning allocations if we need to COW to actually
6504 if (xfs_is_always_cow_inode(ip))
6506 if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
6507 return ip->i_extsize;
6508 if (XFS_IS_REALTIME_INODE(ip) &&
6509 ip->i_mount->m_sb.sb_rextsize > 1)
6510 return ip->i_mount->m_sb.sb_rextsize;
6515 * Helper function to extract CoW extent size hint from inode.
6516 * Between the extent size hint and the CoW extent size hint, we
6517 * return the greater of the two. If the value is zero (automatic),
6518 * use the default size.
6521 xfs_get_cowextsz_hint(
6522 struct xfs_inode *ip)
6527 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6528 a = ip->i_cowextsize;
6529 b = xfs_get_extsz_hint(ip);
6533 return XFS_DEFAULT_COWEXTSZ_HINT;