1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
17 #include "xfs_inode.h"
18 #include "xfs_btree.h"
19 #include "xfs_trans.h"
20 #include "xfs_alloc.h"
22 #include "xfs_bmap_util.h"
23 #include "xfs_bmap_btree.h"
24 #include "xfs_rtalloc.h"
25 #include "xfs_errortag.h"
26 #include "xfs_error.h"
27 #include "xfs_quota.h"
28 #include "xfs_trans_space.h"
29 #include "xfs_buf_item.h"
30 #include "xfs_trace.h"
31 #include "xfs_attr_leaf.h"
32 #include "xfs_filestream.h"
34 #include "xfs_ag_resv.h"
35 #include "xfs_refcount.h"
36 #include "xfs_icache.h"
37 #include "xfs_iomap.h"
40 kmem_zone_t *xfs_bmap_free_item_zone;
43 * Miscellaneous helper functions
47 * Compute and fill in the value of the maximum depth of a bmap btree
48 * in this filesystem. Done once, during mount.
51 xfs_bmap_compute_maxlevels(
52 xfs_mount_t *mp, /* file system mount structure */
53 int whichfork) /* data or attr fork */
55 int level; /* btree level */
56 uint maxblocks; /* max blocks at this level */
57 uint maxleafents; /* max leaf entries possible */
58 int maxrootrecs; /* max records in root block */
59 int minleafrecs; /* min records in leaf block */
60 int minnoderecs; /* min records in node block */
61 int sz; /* root block size */
64 * The maximum number of extents in a file, hence the maximum number of
65 * leaf entries, is controlled by the size of the on-disk extent count,
66 * either a signed 32-bit number for the data fork, or a signed 16-bit
67 * number for the attr fork.
69 * Note that we can no longer assume that if we are in ATTR1 that
70 * the fork offset of all the inodes will be
71 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
72 * with ATTR2 and then mounted back with ATTR1, keeping the
73 * di_forkoff's fixed but probably at various positions. Therefore,
74 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
75 * of a minimum size available.
77 if (whichfork == XFS_DATA_FORK) {
78 maxleafents = MAXEXTNUM;
79 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
81 maxleafents = MAXAEXTNUM;
82 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
84 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
85 minleafrecs = mp->m_bmap_dmnr[0];
86 minnoderecs = mp->m_bmap_dmnr[1];
87 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
88 for (level = 1; maxblocks > 1; level++) {
89 if (maxblocks <= maxrootrecs)
92 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
94 mp->m_bm_maxlevels[whichfork] = level;
97 STATIC int /* error */
99 struct xfs_btree_cur *cur,
100 struct xfs_bmbt_irec *irec,
101 int *stat) /* success/failure */
103 cur->bc_rec.b = *irec;
104 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
107 STATIC int /* error */
108 xfs_bmbt_lookup_first(
109 struct xfs_btree_cur *cur,
110 int *stat) /* success/failure */
112 cur->bc_rec.b.br_startoff = 0;
113 cur->bc_rec.b.br_startblock = 0;
114 cur->bc_rec.b.br_blockcount = 0;
115 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
119 * Check if the inode needs to be converted to btree format.
121 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
123 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
125 return whichfork != XFS_COW_FORK &&
126 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
127 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
131 * Check if the inode should be converted to extent format.
133 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
135 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
137 return whichfork != XFS_COW_FORK &&
138 ifp->if_format == XFS_DINODE_FMT_BTREE &&
139 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
143 * Update the record referred to by cur to the value given by irec
144 * This either works (return 0) or gets an EFSCORRUPTED error.
148 struct xfs_btree_cur *cur,
149 struct xfs_bmbt_irec *irec)
151 union xfs_btree_rec rec;
153 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
154 return xfs_btree_update(cur, &rec);
158 * Compute the worst-case number of indirect blocks that will be used
159 * for ip's delayed extent of length "len".
162 xfs_bmap_worst_indlen(
163 xfs_inode_t *ip, /* incore inode pointer */
164 xfs_filblks_t len) /* delayed extent length */
166 int level; /* btree level number */
167 int maxrecs; /* maximum record count at this level */
168 xfs_mount_t *mp; /* mount structure */
169 xfs_filblks_t rval; /* return value */
172 maxrecs = mp->m_bmap_dmxr[0];
173 for (level = 0, rval = 0;
174 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
177 do_div(len, maxrecs);
180 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
183 maxrecs = mp->m_bmap_dmxr[1];
189 * Calculate the default attribute fork offset for newly created inodes.
192 xfs_default_attroffset(
193 struct xfs_inode *ip)
195 struct xfs_mount *mp = ip->i_mount;
198 if (mp->m_sb.sb_inodesize == 256)
199 offset = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
201 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
203 ASSERT(offset < XFS_LITINO(mp));
208 * Helper routine to reset inode di_forkoff field when switching
209 * attribute fork from local to extent format - we reset it where
210 * possible to make space available for inline data fork extents.
213 xfs_bmap_forkoff_reset(
217 if (whichfork == XFS_ATTR_FORK &&
218 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
219 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
220 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
222 if (dfl_forkoff > ip->i_d.di_forkoff)
223 ip->i_d.di_forkoff = dfl_forkoff;
228 STATIC struct xfs_buf *
230 struct xfs_btree_cur *cur,
233 struct xfs_log_item *lip;
239 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
240 if (!cur->bc_bufs[i])
242 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
243 return cur->bc_bufs[i];
246 /* Chase down all the log items to see if the bp is there */
247 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
248 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
250 if (bip->bli_item.li_type == XFS_LI_BUF &&
251 XFS_BUF_ADDR(bip->bli_buf) == bno)
260 struct xfs_btree_block *block,
266 __be64 *pp, *thispa; /* pointer to block address */
267 xfs_bmbt_key_t *prevp, *keyp;
269 ASSERT(be16_to_cpu(block->bb_level) > 0);
272 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
273 dmxr = mp->m_bmap_dmxr[0];
274 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
277 ASSERT(be64_to_cpu(prevp->br_startoff) <
278 be64_to_cpu(keyp->br_startoff));
283 * Compare the block numbers to see if there are dups.
286 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
288 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
290 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
292 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
294 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
295 if (*thispa == *pp) {
296 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
298 (unsigned long long)be64_to_cpu(*thispa));
299 xfs_err(mp, "%s: ptrs are equal in node\n",
301 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
308 * Check that the extents for the inode ip are in the right order in all
309 * btree leaves. THis becomes prohibitively expensive for large extent count
310 * files, so don't bother with inodes that have more than 10,000 extents in
311 * them. The btree record ordering checks will still be done, so for such large
312 * bmapbt constructs that is going to catch most corruptions.
315 xfs_bmap_check_leaf_extents(
316 xfs_btree_cur_t *cur, /* btree cursor or null */
317 xfs_inode_t *ip, /* incore inode pointer */
318 int whichfork) /* data or attr fork */
320 struct xfs_mount *mp = ip->i_mount;
321 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
322 struct xfs_btree_block *block; /* current btree block */
323 xfs_fsblock_t bno; /* block # of "block" */
324 xfs_buf_t *bp; /* buffer for "block" */
325 int error; /* error return value */
326 xfs_extnum_t i=0, j; /* index into the extents list */
327 int level; /* btree level, for checking */
328 __be64 *pp; /* pointer to block address */
329 xfs_bmbt_rec_t *ep; /* pointer to current extent */
330 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
331 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
334 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
337 /* skip large extent count inodes */
338 if (ip->i_df.if_nextents > 10000)
342 block = ifp->if_broot;
344 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
346 level = be16_to_cpu(block->bb_level);
348 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
349 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
350 bno = be64_to_cpu(*pp);
352 ASSERT(bno != NULLFSBLOCK);
353 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
354 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
357 * Go down the tree until leaf level is reached, following the first
358 * pointer (leftmost) at each level.
360 while (level-- > 0) {
361 /* See if buf is in cur first */
363 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
366 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
372 block = XFS_BUF_TO_BLOCK(bp);
377 * Check this block for basic sanity (increasing keys and
378 * no duplicate blocks).
381 xfs_check_block(block, mp, 0, 0);
382 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
383 bno = be64_to_cpu(*pp);
384 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
385 error = -EFSCORRUPTED;
390 xfs_trans_brelse(NULL, bp);
395 * Here with bp and block set to the leftmost leaf node in the tree.
400 * Loop over all leaf nodes checking that all extents are in the right order.
403 xfs_fsblock_t nextbno;
404 xfs_extnum_t num_recs;
407 num_recs = xfs_btree_get_numrecs(block);
410 * Read-ahead the next leaf block, if any.
413 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
416 * Check all the extents to make sure they are OK.
417 * If we had a previous block, the last entry should
418 * conform with the first entry in this one.
421 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
423 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
424 xfs_bmbt_disk_get_blockcount(&last) <=
425 xfs_bmbt_disk_get_startoff(ep));
427 for (j = 1; j < num_recs; j++) {
428 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
429 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
430 xfs_bmbt_disk_get_blockcount(ep) <=
431 xfs_bmbt_disk_get_startoff(nextp));
439 xfs_trans_brelse(NULL, bp);
443 * If we've reached the end, stop.
445 if (bno == NULLFSBLOCK)
449 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
452 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
458 block = XFS_BUF_TO_BLOCK(bp);
464 xfs_warn(mp, "%s: at error0", __func__);
466 xfs_trans_brelse(NULL, bp);
468 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
470 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
471 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
476 * Validate that the bmbt_irecs being returned from bmapi are valid
477 * given the caller's original parameters. Specifically check the
478 * ranges of the returned irecs to ensure that they only extend beyond
479 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
482 xfs_bmap_validate_ret(
486 xfs_bmbt_irec_t *mval,
490 int i; /* index to map values */
492 ASSERT(ret_nmap <= nmap);
494 for (i = 0; i < ret_nmap; i++) {
495 ASSERT(mval[i].br_blockcount > 0);
496 if (!(flags & XFS_BMAPI_ENTIRE)) {
497 ASSERT(mval[i].br_startoff >= bno);
498 ASSERT(mval[i].br_blockcount <= len);
499 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
502 ASSERT(mval[i].br_startoff < bno + len);
503 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
507 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
508 mval[i].br_startoff);
509 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
510 mval[i].br_startblock != HOLESTARTBLOCK);
511 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
512 mval[i].br_state == XFS_EXT_UNWRITTEN);
517 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
518 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
522 * bmap free list manipulation functions
526 * Add the extent to the list of extents to be free at transaction end.
527 * The list is maintained sorted (by block number).
531 struct xfs_trans *tp,
534 const struct xfs_owner_info *oinfo,
537 struct xfs_extent_free_item *new; /* new element */
539 struct xfs_mount *mp = tp->t_mountp;
543 ASSERT(bno != NULLFSBLOCK);
545 ASSERT(len <= MAXEXTLEN);
546 ASSERT(!isnullstartblock(bno));
547 agno = XFS_FSB_TO_AGNO(mp, bno);
548 agbno = XFS_FSB_TO_AGBNO(mp, bno);
549 ASSERT(agno < mp->m_sb.sb_agcount);
550 ASSERT(agbno < mp->m_sb.sb_agblocks);
551 ASSERT(len < mp->m_sb.sb_agblocks);
552 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
554 ASSERT(xfs_bmap_free_item_zone != NULL);
556 new = kmem_cache_alloc(xfs_bmap_free_item_zone,
557 GFP_KERNEL | __GFP_NOFAIL);
558 new->xefi_startblock = bno;
559 new->xefi_blockcount = (xfs_extlen_t)len;
561 new->xefi_oinfo = *oinfo;
563 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
564 new->xefi_skip_discard = skip_discard;
565 trace_xfs_bmap_free_defer(tp->t_mountp,
566 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
567 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
568 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
572 * Inode fork format manipulation functions
576 * Convert the inode format to extent format if it currently is in btree format,
577 * but the extent list is small enough that it fits into the extent format.
579 * Since the extents are already in-core, all we have to do is give up the space
580 * for the btree root and pitch the leaf block.
582 STATIC int /* error */
583 xfs_bmap_btree_to_extents(
584 struct xfs_trans *tp, /* transaction pointer */
585 struct xfs_inode *ip, /* incore inode pointer */
586 struct xfs_btree_cur *cur, /* btree cursor */
587 int *logflagsp, /* inode logging flags */
588 int whichfork) /* data or attr fork */
590 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
591 struct xfs_mount *mp = ip->i_mount;
592 struct xfs_btree_block *rblock = ifp->if_broot;
593 struct xfs_btree_block *cblock;/* child btree block */
594 xfs_fsblock_t cbno; /* child block number */
595 xfs_buf_t *cbp; /* child block's buffer */
596 int error; /* error return value */
597 __be64 *pp; /* ptr to block address */
598 struct xfs_owner_info oinfo;
600 /* check if we actually need the extent format first: */
601 if (!xfs_bmap_wants_extents(ip, whichfork))
605 ASSERT(whichfork != XFS_COW_FORK);
606 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
607 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
608 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
609 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
610 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
612 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
613 cbno = be64_to_cpu(*pp);
615 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
616 return -EFSCORRUPTED;
618 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
622 cblock = XFS_BUF_TO_BLOCK(cbp);
623 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
625 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
626 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
627 ip->i_d.di_nblocks--;
628 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
629 xfs_trans_binval(tp, cbp);
630 if (cur->bc_bufs[0] == cbp)
631 cur->bc_bufs[0] = NULL;
632 xfs_iroot_realloc(ip, -1, whichfork);
633 ASSERT(ifp->if_broot == NULL);
634 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
635 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
636 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
641 * Convert an extents-format file into a btree-format file.
642 * The new file will have a root block (in the inode) and a single child block.
644 STATIC int /* error */
645 xfs_bmap_extents_to_btree(
646 struct xfs_trans *tp, /* transaction pointer */
647 struct xfs_inode *ip, /* incore inode pointer */
648 struct xfs_btree_cur **curp, /* cursor returned to caller */
649 int wasdel, /* converting a delayed alloc */
650 int *logflagsp, /* inode logging flags */
651 int whichfork) /* data or attr fork */
653 struct xfs_btree_block *ablock; /* allocated (child) bt block */
654 struct xfs_buf *abp; /* buffer for ablock */
655 struct xfs_alloc_arg args; /* allocation arguments */
656 struct xfs_bmbt_rec *arp; /* child record pointer */
657 struct xfs_btree_block *block; /* btree root block */
658 struct xfs_btree_cur *cur; /* bmap btree cursor */
659 int error; /* error return value */
660 struct xfs_ifork *ifp; /* inode fork pointer */
661 struct xfs_bmbt_key *kp; /* root block key pointer */
662 struct xfs_mount *mp; /* mount structure */
663 xfs_bmbt_ptr_t *pp; /* root block address pointer */
664 struct xfs_iext_cursor icur;
665 struct xfs_bmbt_irec rec;
666 xfs_extnum_t cnt = 0;
669 ASSERT(whichfork != XFS_COW_FORK);
670 ifp = XFS_IFORK_PTR(ip, whichfork);
671 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
674 * Make space in the inode incore. This needs to be undone if we fail
675 * to expand the root.
677 xfs_iroot_realloc(ip, 1, whichfork);
678 ifp->if_flags |= XFS_IFBROOT;
683 block = ifp->if_broot;
684 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
685 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
686 XFS_BTREE_LONG_PTRS);
688 * Need a cursor. Can't allocate until bb_level is filled in.
690 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
691 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
693 * Convert to a btree with two levels, one record in root.
695 ifp->if_format = XFS_DINODE_FMT_BTREE;
696 memset(&args, 0, sizeof(args));
699 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
700 if (tp->t_firstblock == NULLFSBLOCK) {
701 args.type = XFS_ALLOCTYPE_START_BNO;
702 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
703 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
704 args.type = XFS_ALLOCTYPE_START_BNO;
705 args.fsbno = tp->t_firstblock;
707 args.type = XFS_ALLOCTYPE_NEAR_BNO;
708 args.fsbno = tp->t_firstblock;
710 args.minlen = args.maxlen = args.prod = 1;
711 args.wasdel = wasdel;
713 error = xfs_alloc_vextent(&args);
715 goto out_root_realloc;
717 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
719 goto out_root_realloc;
723 * Allocation can't fail, the space was reserved.
725 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
726 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
727 tp->t_firstblock = args.fsbno;
728 cur->bc_ino.allocated++;
729 ip->i_d.di_nblocks++;
730 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
731 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
732 XFS_FSB_TO_DADDR(mp, args.fsbno),
733 mp->m_bsize, 0, &abp);
735 goto out_unreserve_dquot;
738 * Fill in the child block.
740 abp->b_ops = &xfs_bmbt_buf_ops;
741 ablock = XFS_BUF_TO_BLOCK(abp);
742 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
743 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
744 XFS_BTREE_LONG_PTRS);
746 for_each_xfs_iext(ifp, &icur, &rec) {
747 if (isnullstartblock(rec.br_startblock))
749 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
750 xfs_bmbt_disk_set_all(arp, &rec);
753 ASSERT(cnt == ifp->if_nextents);
754 xfs_btree_set_numrecs(ablock, cnt);
757 * Fill in the root key and pointer.
759 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
760 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
761 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
762 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
763 be16_to_cpu(block->bb_level)));
764 *pp = cpu_to_be64(args.fsbno);
767 * Do all this logging at the end so that
768 * the root is at the right level.
770 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
771 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
772 ASSERT(*curp == NULL);
774 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
778 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
780 xfs_iroot_realloc(ip, -1, whichfork);
781 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
782 ASSERT(ifp->if_broot == NULL);
783 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
789 * Convert a local file to an extents file.
790 * This code is out of bounds for data forks of regular files,
791 * since the file data needs to get logged so things will stay consistent.
792 * (The bmap-level manipulations are ok, though).
795 xfs_bmap_local_to_extents_empty(
796 struct xfs_trans *tp,
797 struct xfs_inode *ip,
800 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
802 ASSERT(whichfork != XFS_COW_FORK);
803 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
804 ASSERT(ifp->if_bytes == 0);
805 ASSERT(ifp->if_nextents == 0);
807 xfs_bmap_forkoff_reset(ip, whichfork);
808 ifp->if_flags &= ~XFS_IFINLINE;
809 ifp->if_flags |= XFS_IFEXTENTS;
810 ifp->if_u1.if_root = NULL;
812 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
813 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
817 STATIC int /* error */
818 xfs_bmap_local_to_extents(
819 xfs_trans_t *tp, /* transaction pointer */
820 xfs_inode_t *ip, /* incore inode pointer */
821 xfs_extlen_t total, /* total blocks needed by transaction */
822 int *logflagsp, /* inode logging flags */
824 void (*init_fn)(struct xfs_trans *tp,
826 struct xfs_inode *ip,
827 struct xfs_ifork *ifp))
830 int flags; /* logging flags returned */
831 struct xfs_ifork *ifp; /* inode fork pointer */
832 xfs_alloc_arg_t args; /* allocation arguments */
833 xfs_buf_t *bp; /* buffer for extent block */
834 struct xfs_bmbt_irec rec;
835 struct xfs_iext_cursor icur;
838 * We don't want to deal with the case of keeping inode data inline yet.
839 * So sending the data fork of a regular inode is invalid.
841 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
842 ifp = XFS_IFORK_PTR(ip, whichfork);
843 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
845 if (!ifp->if_bytes) {
846 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
847 flags = XFS_ILOG_CORE;
853 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
854 memset(&args, 0, sizeof(args));
856 args.mp = ip->i_mount;
857 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
862 if (tp->t_firstblock == NULLFSBLOCK) {
863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
866 args.fsbno = tp->t_firstblock;
867 args.type = XFS_ALLOCTYPE_NEAR_BNO;
870 args.minlen = args.maxlen = args.prod = 1;
871 error = xfs_alloc_vextent(&args);
875 /* Can't fail, the space was reserved. */
876 ASSERT(args.fsbno != NULLFSBLOCK);
877 ASSERT(args.len == 1);
878 tp->t_firstblock = args.fsbno;
879 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
880 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
881 args.mp->m_bsize, 0, &bp);
886 * Initialize the block, copy the data and log the remote buffer.
888 * The callout is responsible for logging because the remote format
889 * might differ from the local format and thus we don't know how much to
890 * log here. Note that init_fn must also set the buffer log item type
893 init_fn(tp, bp, ip, ifp);
895 /* account for the change in fork size */
896 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
897 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
898 flags |= XFS_ILOG_CORE;
900 ifp->if_u1.if_root = NULL;
904 rec.br_startblock = args.fsbno;
905 rec.br_blockcount = 1;
906 rec.br_state = XFS_EXT_NORM;
907 xfs_iext_first(ifp, &icur);
908 xfs_iext_insert(ip, &icur, &rec, 0);
910 ifp->if_nextents = 1;
911 ip->i_d.di_nblocks = 1;
912 xfs_trans_mod_dquot_byino(tp, ip,
913 XFS_TRANS_DQ_BCOUNT, 1L);
914 flags |= xfs_ilog_fext(whichfork);
922 * Called from xfs_bmap_add_attrfork to handle btree format files.
924 STATIC int /* error */
925 xfs_bmap_add_attrfork_btree(
926 xfs_trans_t *tp, /* transaction pointer */
927 xfs_inode_t *ip, /* incore inode pointer */
928 int *flags) /* inode logging flags */
930 xfs_btree_cur_t *cur; /* btree cursor */
931 int error; /* error return value */
932 xfs_mount_t *mp; /* file system mount struct */
933 int stat; /* newroot status */
936 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
937 *flags |= XFS_ILOG_DBROOT;
939 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
940 error = xfs_bmbt_lookup_first(cur, &stat);
943 /* must be at least one entry */
944 if (XFS_IS_CORRUPT(mp, stat != 1)) {
945 error = -EFSCORRUPTED;
948 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
951 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
954 cur->bc_ino.allocated = 0;
955 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
959 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
964 * Called from xfs_bmap_add_attrfork to handle extents format files.
966 STATIC int /* error */
967 xfs_bmap_add_attrfork_extents(
968 struct xfs_trans *tp, /* transaction pointer */
969 struct xfs_inode *ip, /* incore inode pointer */
970 int *flags) /* inode logging flags */
972 xfs_btree_cur_t *cur; /* bmap btree cursor */
973 int error; /* error return value */
975 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
979 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
982 cur->bc_ino.allocated = 0;
983 xfs_btree_del_cursor(cur, error);
989 * Called from xfs_bmap_add_attrfork to handle local format files. Each
990 * different data fork content type needs a different callout to do the
991 * conversion. Some are basic and only require special block initialisation
992 * callouts for the data formating, others (directories) are so specialised they
993 * handle everything themselves.
995 * XXX (dgc): investigate whether directory conversion can use the generic
996 * formatting callout. It should be possible - it's just a very complex
999 STATIC int /* error */
1000 xfs_bmap_add_attrfork_local(
1001 struct xfs_trans *tp, /* transaction pointer */
1002 struct xfs_inode *ip, /* incore inode pointer */
1003 int *flags) /* inode logging flags */
1005 struct xfs_da_args dargs; /* args for dir/attr code */
1007 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1010 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1011 memset(&dargs, 0, sizeof(dargs));
1012 dargs.geo = ip->i_mount->m_dir_geo;
1014 dargs.total = dargs.geo->fsbcount;
1015 dargs.whichfork = XFS_DATA_FORK;
1017 return xfs_dir2_sf_to_block(&dargs);
1020 if (S_ISLNK(VFS_I(ip)->i_mode))
1021 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1023 xfs_symlink_local_to_remote);
1025 /* should only be called for types that support local format data */
1027 return -EFSCORRUPTED;
1030 /* Set an inode attr fork off based on the format */
1032 xfs_bmap_set_attrforkoff(
1033 struct xfs_inode *ip,
1037 switch (ip->i_df.if_format) {
1038 case XFS_DINODE_FMT_DEV:
1039 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1041 case XFS_DINODE_FMT_LOCAL:
1042 case XFS_DINODE_FMT_EXTENTS:
1043 case XFS_DINODE_FMT_BTREE:
1044 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1045 if (!ip->i_d.di_forkoff)
1046 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1047 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1059 * Convert inode from non-attributed to attributed.
1060 * Must not be in a transaction, ip must not be locked.
1062 int /* error code */
1063 xfs_bmap_add_attrfork(
1064 xfs_inode_t *ip, /* incore inode pointer */
1065 int size, /* space new attribute needs */
1066 int rsvd) /* xact may use reserved blks */
1068 xfs_mount_t *mp; /* mount structure */
1069 xfs_trans_t *tp; /* transaction pointer */
1070 int blks; /* space reservation */
1071 int version = 1; /* superblock attr version */
1072 int logflags; /* logging flags */
1073 int error; /* error return value */
1075 ASSERT(XFS_IFORK_Q(ip) == 0);
1078 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1080 blks = XFS_ADDAFORK_SPACE_RES(mp);
1082 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1083 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1087 xfs_ilock(ip, XFS_ILOCK_EXCL);
1088 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1089 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1090 XFS_QMOPT_RES_REGBLKS);
1093 if (XFS_IFORK_Q(ip))
1096 xfs_trans_ijoin(tp, ip, 0);
1097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1098 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1101 ASSERT(ip->i_afp == NULL);
1103 ip->i_afp = kmem_cache_zalloc(xfs_ifork_zone,
1104 GFP_KERNEL | __GFP_NOFAIL);
1106 ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS;
1107 ip->i_afp->if_flags = XFS_IFEXTENTS;
1109 switch (ip->i_df.if_format) {
1110 case XFS_DINODE_FMT_LOCAL:
1111 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1113 case XFS_DINODE_FMT_EXTENTS:
1114 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1116 case XFS_DINODE_FMT_BTREE:
1117 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1124 xfs_trans_log_inode(tp, ip, logflags);
1127 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1128 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1129 bool log_sb = false;
1131 spin_lock(&mp->m_sb_lock);
1132 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1133 xfs_sb_version_addattr(&mp->m_sb);
1136 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1137 xfs_sb_version_addattr2(&mp->m_sb);
1140 spin_unlock(&mp->m_sb_lock);
1145 error = xfs_trans_commit(tp);
1146 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1150 xfs_trans_cancel(tp);
1151 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1156 * Internal and external extent tree search functions.
1159 struct xfs_iread_state {
1160 struct xfs_iext_cursor icur;
1161 xfs_extnum_t loaded;
1164 /* Stuff every bmbt record from this block into the incore extent map. */
1166 xfs_iread_bmbt_block(
1167 struct xfs_btree_cur *cur,
1171 struct xfs_iread_state *ir = priv;
1172 struct xfs_mount *mp = cur->bc_mp;
1173 struct xfs_inode *ip = cur->bc_ino.ip;
1174 struct xfs_btree_block *block;
1176 struct xfs_bmbt_rec *frp;
1177 xfs_extnum_t num_recs;
1179 int whichfork = cur->bc_ino.whichfork;
1180 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1182 block = xfs_btree_get_block(cur, level, &bp);
1184 /* Abort if we find more records than nextents. */
1185 num_recs = xfs_btree_get_numrecs(block);
1186 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1187 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1188 (unsigned long long)ip->i_ino);
1189 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1190 sizeof(*block), __this_address);
1191 return -EFSCORRUPTED;
1194 /* Copy records into the incore cache. */
1195 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1196 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1197 struct xfs_bmbt_irec new;
1200 xfs_bmbt_disk_get_all(frp, &new);
1201 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1203 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1204 "xfs_iread_extents(2)", frp,
1206 return -EFSCORRUPTED;
1208 xfs_iext_insert(ip, &ir->icur, &new,
1209 xfs_bmap_fork_to_state(whichfork));
1210 trace_xfs_read_extent(ip, &ir->icur,
1211 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1212 xfs_iext_next(ifp, &ir->icur);
1219 * Read in extents from a btree-format inode.
1223 struct xfs_trans *tp,
1224 struct xfs_inode *ip,
1227 struct xfs_iread_state ir;
1228 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1229 struct xfs_mount *mp = ip->i_mount;
1230 struct xfs_btree_cur *cur;
1233 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1235 if (XFS_IS_CORRUPT(mp, ifp->if_format != XFS_DINODE_FMT_BTREE)) {
1236 error = -EFSCORRUPTED;
1241 xfs_iext_first(ifp, &ir.icur);
1242 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1243 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1244 XFS_BTREE_VISIT_RECORDS, &ir);
1245 xfs_btree_del_cursor(cur, error);
1249 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1250 error = -EFSCORRUPTED;
1253 ASSERT(ir.loaded == xfs_iext_count(ifp));
1255 ifp->if_flags |= XFS_IFEXTENTS;
1258 xfs_iext_destroy(ifp);
1263 * Returns the relative block number of the first unused block(s) in the given
1264 * fork with at least "len" logically contiguous blocks free. This is the
1265 * lowest-address hole if the fork has holes, else the first block past the end
1266 * of fork. Return 0 if the fork is currently local (in-inode).
1269 xfs_bmap_first_unused(
1270 struct xfs_trans *tp, /* transaction pointer */
1271 struct xfs_inode *ip, /* incore inode */
1272 xfs_extlen_t len, /* size of hole to find */
1273 xfs_fileoff_t *first_unused, /* unused block */
1274 int whichfork) /* data or attr fork */
1276 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1277 struct xfs_bmbt_irec got;
1278 struct xfs_iext_cursor icur;
1279 xfs_fileoff_t lastaddr = 0;
1280 xfs_fileoff_t lowest, max;
1283 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1288 ASSERT(xfs_ifork_has_extents(ifp));
1290 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1291 error = xfs_iread_extents(tp, ip, whichfork);
1296 lowest = max = *first_unused;
1297 for_each_xfs_iext(ifp, &icur, &got) {
1299 * See if the hole before this extent will work.
1301 if (got.br_startoff >= lowest + len &&
1302 got.br_startoff - max >= len)
1304 lastaddr = got.br_startoff + got.br_blockcount;
1305 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1308 *first_unused = max;
1313 * Returns the file-relative block number of the last block - 1 before
1314 * last_block (input value) in the file.
1315 * This is not based on i_size, it is based on the extent records.
1316 * Returns 0 for local files, as they do not have extent records.
1319 xfs_bmap_last_before(
1320 struct xfs_trans *tp, /* transaction pointer */
1321 struct xfs_inode *ip, /* incore inode */
1322 xfs_fileoff_t *last_block, /* last block */
1323 int whichfork) /* data or attr fork */
1325 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1326 struct xfs_bmbt_irec got;
1327 struct xfs_iext_cursor icur;
1330 switch (ifp->if_format) {
1331 case XFS_DINODE_FMT_LOCAL:
1334 case XFS_DINODE_FMT_BTREE:
1335 case XFS_DINODE_FMT_EXTENTS:
1339 return -EFSCORRUPTED;
1342 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1343 error = xfs_iread_extents(tp, ip, whichfork);
1348 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1354 xfs_bmap_last_extent(
1355 struct xfs_trans *tp,
1356 struct xfs_inode *ip,
1358 struct xfs_bmbt_irec *rec,
1361 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1362 struct xfs_iext_cursor icur;
1365 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1366 error = xfs_iread_extents(tp, ip, whichfork);
1371 xfs_iext_last(ifp, &icur);
1372 if (!xfs_iext_get_extent(ifp, &icur, rec))
1380 * Check the last inode extent to determine whether this allocation will result
1381 * in blocks being allocated at the end of the file. When we allocate new data
1382 * blocks at the end of the file which do not start at the previous data block,
1383 * we will try to align the new blocks at stripe unit boundaries.
1385 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1386 * at, or past the EOF.
1390 struct xfs_bmalloca *bma,
1393 struct xfs_bmbt_irec rec;
1398 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1409 * Check if we are allocation or past the last extent, or at least into
1410 * the last delayed allocated extent.
1412 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1413 (bma->offset >= rec.br_startoff &&
1414 isnullstartblock(rec.br_startblock));
1419 * Returns the file-relative block number of the first block past eof in
1420 * the file. This is not based on i_size, it is based on the extent records.
1421 * Returns 0 for local files, as they do not have extent records.
1424 xfs_bmap_last_offset(
1425 struct xfs_inode *ip,
1426 xfs_fileoff_t *last_block,
1429 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1430 struct xfs_bmbt_irec rec;
1436 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1439 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
1440 return -EFSCORRUPTED;
1442 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1443 if (error || is_empty)
1446 *last_block = rec.br_startoff + rec.br_blockcount;
1451 * Returns whether the selected fork of the inode has exactly one
1452 * block or not. For the data fork we check this matches di_size,
1453 * implying the file's range is 0..bsize-1.
1455 int /* 1=>1 block, 0=>otherwise */
1457 struct xfs_inode *ip, /* incore inode */
1458 int whichfork) /* data or attr fork */
1460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1461 int rval; /* return value */
1462 struct xfs_bmbt_irec s; /* internal version of extent */
1463 struct xfs_iext_cursor icur;
1466 if (whichfork == XFS_DATA_FORK)
1467 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1469 if (ifp->if_nextents != 1)
1471 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS)
1473 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1474 xfs_iext_first(ifp, &icur);
1475 xfs_iext_get_extent(ifp, &icur, &s);
1476 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1477 if (rval && whichfork == XFS_DATA_FORK)
1478 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1483 * Extent tree manipulation functions used during allocation.
1487 * Convert a delayed allocation to a real allocation.
1489 STATIC int /* error */
1490 xfs_bmap_add_extent_delay_real(
1491 struct xfs_bmalloca *bma,
1494 struct xfs_mount *mp = bma->ip->i_mount;
1495 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1496 struct xfs_bmbt_irec *new = &bma->got;
1497 int error; /* error return value */
1498 int i; /* temp state */
1499 xfs_fileoff_t new_endoff; /* end offset of new entry */
1500 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1501 /* left is 0, right is 1, prev is 2 */
1502 int rval=0; /* return value (logging flags) */
1503 int state = xfs_bmap_fork_to_state(whichfork);
1504 xfs_filblks_t da_new; /* new count del alloc blocks used */
1505 xfs_filblks_t da_old; /* old count del alloc blocks used */
1506 xfs_filblks_t temp=0; /* value for da_new calculations */
1507 int tmp_rval; /* partial logging flags */
1508 struct xfs_bmbt_irec old;
1510 ASSERT(whichfork != XFS_ATTR_FORK);
1511 ASSERT(!isnullstartblock(new->br_startblock));
1513 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1515 XFS_STATS_INC(mp, xs_add_exlist);
1522 * Set up a bunch of variables to make the tests simpler.
1524 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1525 new_endoff = new->br_startoff + new->br_blockcount;
1526 ASSERT(isnullstartblock(PREV.br_startblock));
1527 ASSERT(PREV.br_startoff <= new->br_startoff);
1528 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1530 da_old = startblockval(PREV.br_startblock);
1534 * Set flags determining what part of the previous delayed allocation
1535 * extent is being replaced by a real allocation.
1537 if (PREV.br_startoff == new->br_startoff)
1538 state |= BMAP_LEFT_FILLING;
1539 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1540 state |= BMAP_RIGHT_FILLING;
1543 * Check and set flags if this segment has a left neighbor.
1544 * Don't set contiguous if the combined extent would be too large.
1546 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1547 state |= BMAP_LEFT_VALID;
1548 if (isnullstartblock(LEFT.br_startblock))
1549 state |= BMAP_LEFT_DELAY;
1552 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1553 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1554 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1555 LEFT.br_state == new->br_state &&
1556 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1557 state |= BMAP_LEFT_CONTIG;
1560 * Check and set flags if this segment has a right neighbor.
1561 * Don't set contiguous if the combined extent would be too large.
1562 * Also check for all-three-contiguous being too large.
1564 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1565 state |= BMAP_RIGHT_VALID;
1566 if (isnullstartblock(RIGHT.br_startblock))
1567 state |= BMAP_RIGHT_DELAY;
1570 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1571 new_endoff == RIGHT.br_startoff &&
1572 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1573 new->br_state == RIGHT.br_state &&
1574 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1575 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1576 BMAP_RIGHT_FILLING)) !=
1577 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1578 BMAP_RIGHT_FILLING) ||
1579 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1581 state |= BMAP_RIGHT_CONTIG;
1585 * Switch out based on the FILLING and CONTIG state bits.
1587 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1588 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1589 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1590 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1592 * Filling in all of a previously delayed allocation extent.
1593 * The left and right neighbors are both contiguous with new.
1595 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1597 xfs_iext_remove(bma->ip, &bma->icur, state);
1598 xfs_iext_remove(bma->ip, &bma->icur, state);
1599 xfs_iext_prev(ifp, &bma->icur);
1600 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1603 if (bma->cur == NULL)
1604 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1606 rval = XFS_ILOG_CORE;
1607 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1610 if (XFS_IS_CORRUPT(mp, i != 1)) {
1611 error = -EFSCORRUPTED;
1614 error = xfs_btree_delete(bma->cur, &i);
1617 if (XFS_IS_CORRUPT(mp, i != 1)) {
1618 error = -EFSCORRUPTED;
1621 error = xfs_btree_decrement(bma->cur, 0, &i);
1624 if (XFS_IS_CORRUPT(mp, i != 1)) {
1625 error = -EFSCORRUPTED;
1628 error = xfs_bmbt_update(bma->cur, &LEFT);
1634 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1636 * Filling in all of a previously delayed allocation extent.
1637 * The left neighbor is contiguous, the right is not.
1640 LEFT.br_blockcount += PREV.br_blockcount;
1642 xfs_iext_remove(bma->ip, &bma->icur, state);
1643 xfs_iext_prev(ifp, &bma->icur);
1644 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1646 if (bma->cur == NULL)
1647 rval = XFS_ILOG_DEXT;
1650 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1653 if (XFS_IS_CORRUPT(mp, i != 1)) {
1654 error = -EFSCORRUPTED;
1657 error = xfs_bmbt_update(bma->cur, &LEFT);
1663 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1665 * Filling in all of a previously delayed allocation extent.
1666 * The right neighbor is contiguous, the left is not. Take care
1667 * with delay -> unwritten extent allocation here because the
1668 * delalloc record we are overwriting is always written.
1670 PREV.br_startblock = new->br_startblock;
1671 PREV.br_blockcount += RIGHT.br_blockcount;
1672 PREV.br_state = new->br_state;
1674 xfs_iext_next(ifp, &bma->icur);
1675 xfs_iext_remove(bma->ip, &bma->icur, state);
1676 xfs_iext_prev(ifp, &bma->icur);
1677 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1679 if (bma->cur == NULL)
1680 rval = XFS_ILOG_DEXT;
1683 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1686 if (XFS_IS_CORRUPT(mp, i != 1)) {
1687 error = -EFSCORRUPTED;
1690 error = xfs_bmbt_update(bma->cur, &PREV);
1696 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1698 * Filling in all of a previously delayed allocation extent.
1699 * Neither the left nor right neighbors are contiguous with
1702 PREV.br_startblock = new->br_startblock;
1703 PREV.br_state = new->br_state;
1704 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1707 if (bma->cur == NULL)
1708 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1710 rval = XFS_ILOG_CORE;
1711 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1714 if (XFS_IS_CORRUPT(mp, i != 0)) {
1715 error = -EFSCORRUPTED;
1718 error = xfs_btree_insert(bma->cur, &i);
1721 if (XFS_IS_CORRUPT(mp, i != 1)) {
1722 error = -EFSCORRUPTED;
1728 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1730 * Filling in the first part of a previous delayed allocation.
1731 * The left neighbor is contiguous.
1734 temp = PREV.br_blockcount - new->br_blockcount;
1735 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1736 startblockval(PREV.br_startblock));
1738 LEFT.br_blockcount += new->br_blockcount;
1740 PREV.br_blockcount = temp;
1741 PREV.br_startoff += new->br_blockcount;
1742 PREV.br_startblock = nullstartblock(da_new);
1744 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1745 xfs_iext_prev(ifp, &bma->icur);
1746 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1748 if (bma->cur == NULL)
1749 rval = XFS_ILOG_DEXT;
1752 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1755 if (XFS_IS_CORRUPT(mp, i != 1)) {
1756 error = -EFSCORRUPTED;
1759 error = xfs_bmbt_update(bma->cur, &LEFT);
1765 case BMAP_LEFT_FILLING:
1767 * Filling in the first part of a previous delayed allocation.
1768 * The left neighbor is not contiguous.
1770 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1773 if (bma->cur == NULL)
1774 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1776 rval = XFS_ILOG_CORE;
1777 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1780 if (XFS_IS_CORRUPT(mp, i != 0)) {
1781 error = -EFSCORRUPTED;
1784 error = xfs_btree_insert(bma->cur, &i);
1787 if (XFS_IS_CORRUPT(mp, i != 1)) {
1788 error = -EFSCORRUPTED;
1793 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1794 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1795 &bma->cur, 1, &tmp_rval, whichfork);
1801 temp = PREV.br_blockcount - new->br_blockcount;
1802 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1803 startblockval(PREV.br_startblock) -
1804 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1806 PREV.br_startoff = new_endoff;
1807 PREV.br_blockcount = temp;
1808 PREV.br_startblock = nullstartblock(da_new);
1809 xfs_iext_next(ifp, &bma->icur);
1810 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1811 xfs_iext_prev(ifp, &bma->icur);
1814 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1816 * Filling in the last part of a previous delayed allocation.
1817 * The right neighbor is contiguous with the new allocation.
1820 RIGHT.br_startoff = new->br_startoff;
1821 RIGHT.br_startblock = new->br_startblock;
1822 RIGHT.br_blockcount += new->br_blockcount;
1824 if (bma->cur == NULL)
1825 rval = XFS_ILOG_DEXT;
1828 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1831 if (XFS_IS_CORRUPT(mp, i != 1)) {
1832 error = -EFSCORRUPTED;
1835 error = xfs_bmbt_update(bma->cur, &RIGHT);
1840 temp = PREV.br_blockcount - new->br_blockcount;
1841 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1842 startblockval(PREV.br_startblock));
1844 PREV.br_blockcount = temp;
1845 PREV.br_startblock = nullstartblock(da_new);
1847 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1848 xfs_iext_next(ifp, &bma->icur);
1849 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1852 case BMAP_RIGHT_FILLING:
1854 * Filling in the last part of a previous delayed allocation.
1855 * The right neighbor is not contiguous.
1857 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1860 if (bma->cur == NULL)
1861 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1863 rval = XFS_ILOG_CORE;
1864 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1867 if (XFS_IS_CORRUPT(mp, i != 0)) {
1868 error = -EFSCORRUPTED;
1871 error = xfs_btree_insert(bma->cur, &i);
1874 if (XFS_IS_CORRUPT(mp, i != 1)) {
1875 error = -EFSCORRUPTED;
1880 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1881 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1882 &bma->cur, 1, &tmp_rval, whichfork);
1888 temp = PREV.br_blockcount - new->br_blockcount;
1889 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1890 startblockval(PREV.br_startblock) -
1891 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1893 PREV.br_startblock = nullstartblock(da_new);
1894 PREV.br_blockcount = temp;
1895 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1896 xfs_iext_next(ifp, &bma->icur);
1901 * Filling in the middle part of a previous delayed allocation.
1902 * Contiguity is impossible here.
1903 * This case is avoided almost all the time.
1905 * We start with a delayed allocation:
1907 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1910 * and we are allocating:
1911 * +rrrrrrrrrrrrrrrrr+
1914 * and we set it up for insertion as:
1915 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1917 * PREV @ idx LEFT RIGHT
1918 * inserted at idx + 1
1922 /* LEFT is the new middle */
1925 /* RIGHT is the new right */
1926 RIGHT.br_state = PREV.br_state;
1927 RIGHT.br_startoff = new_endoff;
1928 RIGHT.br_blockcount =
1929 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1930 RIGHT.br_startblock =
1931 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1932 RIGHT.br_blockcount));
1935 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1936 PREV.br_startblock =
1937 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1938 PREV.br_blockcount));
1939 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1941 xfs_iext_next(ifp, &bma->icur);
1942 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1943 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1946 if (bma->cur == NULL)
1947 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1949 rval = XFS_ILOG_CORE;
1950 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1953 if (XFS_IS_CORRUPT(mp, i != 0)) {
1954 error = -EFSCORRUPTED;
1957 error = xfs_btree_insert(bma->cur, &i);
1960 if (XFS_IS_CORRUPT(mp, i != 1)) {
1961 error = -EFSCORRUPTED;
1966 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1967 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1968 &bma->cur, 1, &tmp_rval, whichfork);
1974 da_new = startblockval(PREV.br_startblock) +
1975 startblockval(RIGHT.br_startblock);
1978 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1981 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1982 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1983 case BMAP_LEFT_CONTIG:
1984 case BMAP_RIGHT_CONTIG:
1986 * These cases are all impossible.
1991 /* add reverse mapping unless caller opted out */
1992 if (!(bma->flags & XFS_BMAPI_NORMAP))
1993 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1995 /* convert to a btree if necessary */
1996 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1997 int tmp_logflags; /* partial log flag return val */
1999 ASSERT(bma->cur == NULL);
2000 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2001 &bma->cur, da_old > 0, &tmp_logflags,
2003 bma->logflags |= tmp_logflags;
2008 if (da_new != da_old)
2009 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2012 da_new += bma->cur->bc_ino.allocated;
2013 bma->cur->bc_ino.allocated = 0;
2016 /* adjust for changes in reserved delayed indirect blocks */
2017 if (da_new != da_old) {
2018 ASSERT(state == 0 || da_new < da_old);
2019 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2023 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2025 if (whichfork != XFS_COW_FORK)
2026 bma->logflags |= rval;
2034 * Convert an unwritten allocation to a real allocation or vice versa.
2037 xfs_bmap_add_extent_unwritten_real(
2038 struct xfs_trans *tp,
2039 xfs_inode_t *ip, /* incore inode pointer */
2041 struct xfs_iext_cursor *icur,
2042 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2043 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2044 int *logflagsp) /* inode logging flags */
2046 xfs_btree_cur_t *cur; /* btree cursor */
2047 int error; /* error return value */
2048 int i; /* temp state */
2049 struct xfs_ifork *ifp; /* inode fork pointer */
2050 xfs_fileoff_t new_endoff; /* end offset of new entry */
2051 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2052 /* left is 0, right is 1, prev is 2 */
2053 int rval=0; /* return value (logging flags) */
2054 int state = xfs_bmap_fork_to_state(whichfork);
2055 struct xfs_mount *mp = ip->i_mount;
2056 struct xfs_bmbt_irec old;
2061 ifp = XFS_IFORK_PTR(ip, whichfork);
2063 ASSERT(!isnullstartblock(new->br_startblock));
2065 XFS_STATS_INC(mp, xs_add_exlist);
2072 * Set up a bunch of variables to make the tests simpler.
2075 xfs_iext_get_extent(ifp, icur, &PREV);
2076 ASSERT(new->br_state != PREV.br_state);
2077 new_endoff = new->br_startoff + new->br_blockcount;
2078 ASSERT(PREV.br_startoff <= new->br_startoff);
2079 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2082 * Set flags determining what part of the previous oldext allocation
2083 * extent is being replaced by a newext allocation.
2085 if (PREV.br_startoff == new->br_startoff)
2086 state |= BMAP_LEFT_FILLING;
2087 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2088 state |= BMAP_RIGHT_FILLING;
2091 * Check and set flags if this segment has a left neighbor.
2092 * Don't set contiguous if the combined extent would be too large.
2094 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2095 state |= BMAP_LEFT_VALID;
2096 if (isnullstartblock(LEFT.br_startblock))
2097 state |= BMAP_LEFT_DELAY;
2100 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2101 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2102 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2103 LEFT.br_state == new->br_state &&
2104 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2105 state |= BMAP_LEFT_CONTIG;
2108 * Check and set flags if this segment has a right neighbor.
2109 * Don't set contiguous if the combined extent would be too large.
2110 * Also check for all-three-contiguous being too large.
2112 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2113 state |= BMAP_RIGHT_VALID;
2114 if (isnullstartblock(RIGHT.br_startblock))
2115 state |= BMAP_RIGHT_DELAY;
2118 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2119 new_endoff == RIGHT.br_startoff &&
2120 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2121 new->br_state == RIGHT.br_state &&
2122 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2123 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2124 BMAP_RIGHT_FILLING)) !=
2125 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2126 BMAP_RIGHT_FILLING) ||
2127 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2129 state |= BMAP_RIGHT_CONTIG;
2132 * Switch out based on the FILLING and CONTIG state bits.
2134 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2135 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2136 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2137 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2139 * Setting all of a previous oldext extent to newext.
2140 * The left and right neighbors are both contiguous with new.
2142 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2144 xfs_iext_remove(ip, icur, state);
2145 xfs_iext_remove(ip, icur, state);
2146 xfs_iext_prev(ifp, icur);
2147 xfs_iext_update_extent(ip, state, icur, &LEFT);
2148 ifp->if_nextents -= 2;
2150 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2152 rval = XFS_ILOG_CORE;
2153 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2156 if (XFS_IS_CORRUPT(mp, i != 1)) {
2157 error = -EFSCORRUPTED;
2160 if ((error = xfs_btree_delete(cur, &i)))
2162 if (XFS_IS_CORRUPT(mp, i != 1)) {
2163 error = -EFSCORRUPTED;
2166 if ((error = xfs_btree_decrement(cur, 0, &i)))
2168 if (XFS_IS_CORRUPT(mp, i != 1)) {
2169 error = -EFSCORRUPTED;
2172 if ((error = xfs_btree_delete(cur, &i)))
2174 if (XFS_IS_CORRUPT(mp, i != 1)) {
2175 error = -EFSCORRUPTED;
2178 if ((error = xfs_btree_decrement(cur, 0, &i)))
2180 if (XFS_IS_CORRUPT(mp, i != 1)) {
2181 error = -EFSCORRUPTED;
2184 error = xfs_bmbt_update(cur, &LEFT);
2190 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2192 * Setting all of a previous oldext extent to newext.
2193 * The left neighbor is contiguous, the right is not.
2195 LEFT.br_blockcount += PREV.br_blockcount;
2197 xfs_iext_remove(ip, icur, state);
2198 xfs_iext_prev(ifp, icur);
2199 xfs_iext_update_extent(ip, state, icur, &LEFT);
2202 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2204 rval = XFS_ILOG_CORE;
2205 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2208 if (XFS_IS_CORRUPT(mp, i != 1)) {
2209 error = -EFSCORRUPTED;
2212 if ((error = xfs_btree_delete(cur, &i)))
2214 if (XFS_IS_CORRUPT(mp, i != 1)) {
2215 error = -EFSCORRUPTED;
2218 if ((error = xfs_btree_decrement(cur, 0, &i)))
2220 if (XFS_IS_CORRUPT(mp, i != 1)) {
2221 error = -EFSCORRUPTED;
2224 error = xfs_bmbt_update(cur, &LEFT);
2230 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2232 * Setting all of a previous oldext extent to newext.
2233 * The right neighbor is contiguous, the left is not.
2235 PREV.br_blockcount += RIGHT.br_blockcount;
2236 PREV.br_state = new->br_state;
2238 xfs_iext_next(ifp, icur);
2239 xfs_iext_remove(ip, icur, state);
2240 xfs_iext_prev(ifp, icur);
2241 xfs_iext_update_extent(ip, state, icur, &PREV);
2245 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2247 rval = XFS_ILOG_CORE;
2248 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2251 if (XFS_IS_CORRUPT(mp, i != 1)) {
2252 error = -EFSCORRUPTED;
2255 if ((error = xfs_btree_delete(cur, &i)))
2257 if (XFS_IS_CORRUPT(mp, i != 1)) {
2258 error = -EFSCORRUPTED;
2261 if ((error = xfs_btree_decrement(cur, 0, &i)))
2263 if (XFS_IS_CORRUPT(mp, i != 1)) {
2264 error = -EFSCORRUPTED;
2267 error = xfs_bmbt_update(cur, &PREV);
2273 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2275 * Setting all of a previous oldext extent to newext.
2276 * Neither the left nor right neighbors are contiguous with
2279 PREV.br_state = new->br_state;
2280 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 rval = XFS_ILOG_DEXT;
2286 error = xfs_bmbt_lookup_eq(cur, new, &i);
2289 if (XFS_IS_CORRUPT(mp, i != 1)) {
2290 error = -EFSCORRUPTED;
2293 error = xfs_bmbt_update(cur, &PREV);
2299 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2301 * Setting the first part of a previous oldext extent to newext.
2302 * The left neighbor is contiguous.
2304 LEFT.br_blockcount += new->br_blockcount;
2307 PREV.br_startoff += new->br_blockcount;
2308 PREV.br_startblock += new->br_blockcount;
2309 PREV.br_blockcount -= new->br_blockcount;
2311 xfs_iext_update_extent(ip, state, icur, &PREV);
2312 xfs_iext_prev(ifp, icur);
2313 xfs_iext_update_extent(ip, state, icur, &LEFT);
2316 rval = XFS_ILOG_DEXT;
2319 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2322 if (XFS_IS_CORRUPT(mp, i != 1)) {
2323 error = -EFSCORRUPTED;
2326 error = xfs_bmbt_update(cur, &PREV);
2329 error = xfs_btree_decrement(cur, 0, &i);
2332 error = xfs_bmbt_update(cur, &LEFT);
2338 case BMAP_LEFT_FILLING:
2340 * Setting the first part of a previous oldext extent to newext.
2341 * The left neighbor is not contiguous.
2344 PREV.br_startoff += new->br_blockcount;
2345 PREV.br_startblock += new->br_blockcount;
2346 PREV.br_blockcount -= new->br_blockcount;
2348 xfs_iext_update_extent(ip, state, icur, &PREV);
2349 xfs_iext_insert(ip, icur, new, state);
2353 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2355 rval = XFS_ILOG_CORE;
2356 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2359 if (XFS_IS_CORRUPT(mp, i != 1)) {
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 error = -EFSCORRUPTED;
2376 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2378 * Setting the last part of a previous oldext extent to newext.
2379 * The right neighbor is contiguous with the new allocation.
2382 PREV.br_blockcount -= new->br_blockcount;
2384 RIGHT.br_startoff = new->br_startoff;
2385 RIGHT.br_startblock = new->br_startblock;
2386 RIGHT.br_blockcount += new->br_blockcount;
2388 xfs_iext_update_extent(ip, state, icur, &PREV);
2389 xfs_iext_next(ifp, icur);
2390 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2393 rval = XFS_ILOG_DEXT;
2396 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2399 if (XFS_IS_CORRUPT(mp, i != 1)) {
2400 error = -EFSCORRUPTED;
2403 error = xfs_bmbt_update(cur, &PREV);
2406 error = xfs_btree_increment(cur, 0, &i);
2409 error = xfs_bmbt_update(cur, &RIGHT);
2415 case BMAP_RIGHT_FILLING:
2417 * Setting the last part of a previous oldext extent to newext.
2418 * The right neighbor is not contiguous.
2421 PREV.br_blockcount -= new->br_blockcount;
2423 xfs_iext_update_extent(ip, state, icur, &PREV);
2424 xfs_iext_next(ifp, icur);
2425 xfs_iext_insert(ip, icur, new, state);
2429 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2431 rval = XFS_ILOG_CORE;
2432 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2435 if (XFS_IS_CORRUPT(mp, i != 1)) {
2436 error = -EFSCORRUPTED;
2439 error = xfs_bmbt_update(cur, &PREV);
2442 error = xfs_bmbt_lookup_eq(cur, new, &i);
2445 if (XFS_IS_CORRUPT(mp, i != 0)) {
2446 error = -EFSCORRUPTED;
2449 if ((error = xfs_btree_insert(cur, &i)))
2451 if (XFS_IS_CORRUPT(mp, i != 1)) {
2452 error = -EFSCORRUPTED;
2460 * Setting the middle part of a previous oldext extent to
2461 * newext. Contiguity is impossible here.
2462 * One extent becomes three extents.
2465 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2468 r[1].br_startoff = new_endoff;
2469 r[1].br_blockcount =
2470 old.br_startoff + old.br_blockcount - new_endoff;
2471 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2472 r[1].br_state = PREV.br_state;
2474 xfs_iext_update_extent(ip, state, icur, &PREV);
2475 xfs_iext_next(ifp, icur);
2476 xfs_iext_insert(ip, icur, &r[1], state);
2477 xfs_iext_insert(ip, icur, &r[0], state);
2478 ifp->if_nextents += 2;
2481 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2483 rval = XFS_ILOG_CORE;
2484 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2487 if (XFS_IS_CORRUPT(mp, i != 1)) {
2488 error = -EFSCORRUPTED;
2491 /* new right extent - oldext */
2492 error = xfs_bmbt_update(cur, &r[1]);
2495 /* new left extent - oldext */
2496 cur->bc_rec.b = PREV;
2497 if ((error = xfs_btree_insert(cur, &i)))
2499 if (XFS_IS_CORRUPT(mp, i != 1)) {
2500 error = -EFSCORRUPTED;
2504 * Reset the cursor to the position of the new extent
2505 * we are about to insert as we can't trust it after
2506 * the previous insert.
2508 error = xfs_bmbt_lookup_eq(cur, new, &i);
2511 if (XFS_IS_CORRUPT(mp, i != 0)) {
2512 error = -EFSCORRUPTED;
2515 /* new middle extent - newext */
2516 if ((error = xfs_btree_insert(cur, &i)))
2518 if (XFS_IS_CORRUPT(mp, i != 1)) {
2519 error = -EFSCORRUPTED;
2525 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2526 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2527 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2528 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2529 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2530 case BMAP_LEFT_CONTIG:
2531 case BMAP_RIGHT_CONTIG:
2533 * These cases are all impossible.
2538 /* update reverse mappings */
2539 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2541 /* convert to a btree if necessary */
2542 if (xfs_bmap_needs_btree(ip, whichfork)) {
2543 int tmp_logflags; /* partial log flag return val */
2545 ASSERT(cur == NULL);
2546 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2547 &tmp_logflags, whichfork);
2548 *logflagsp |= tmp_logflags;
2553 /* clear out the allocated field, done with it now in any case. */
2555 cur->bc_ino.allocated = 0;
2559 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2569 * Convert a hole to a delayed allocation.
2572 xfs_bmap_add_extent_hole_delay(
2573 xfs_inode_t *ip, /* incore inode pointer */
2575 struct xfs_iext_cursor *icur,
2576 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2578 struct xfs_ifork *ifp; /* inode fork pointer */
2579 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2580 xfs_filblks_t newlen=0; /* new indirect size */
2581 xfs_filblks_t oldlen=0; /* old indirect size */
2582 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2583 int state = xfs_bmap_fork_to_state(whichfork);
2584 xfs_filblks_t temp; /* temp for indirect calculations */
2586 ifp = XFS_IFORK_PTR(ip, whichfork);
2587 ASSERT(isnullstartblock(new->br_startblock));
2590 * Check and set flags if this segment has a left neighbor
2592 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2593 state |= BMAP_LEFT_VALID;
2594 if (isnullstartblock(left.br_startblock))
2595 state |= BMAP_LEFT_DELAY;
2599 * Check and set flags if the current (right) segment exists.
2600 * If it doesn't exist, we're converting the hole at end-of-file.
2602 if (xfs_iext_get_extent(ifp, icur, &right)) {
2603 state |= BMAP_RIGHT_VALID;
2604 if (isnullstartblock(right.br_startblock))
2605 state |= BMAP_RIGHT_DELAY;
2609 * Set contiguity flags on the left and right neighbors.
2610 * Don't let extents get too large, even if the pieces are contiguous.
2612 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2613 left.br_startoff + left.br_blockcount == new->br_startoff &&
2614 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2615 state |= BMAP_LEFT_CONTIG;
2617 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2618 new->br_startoff + new->br_blockcount == right.br_startoff &&
2619 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2620 (!(state & BMAP_LEFT_CONTIG) ||
2621 (left.br_blockcount + new->br_blockcount +
2622 right.br_blockcount <= MAXEXTLEN)))
2623 state |= BMAP_RIGHT_CONTIG;
2626 * Switch out based on the contiguity flags.
2628 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2629 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2631 * New allocation is contiguous with delayed allocations
2632 * on the left and on the right.
2633 * Merge all three into a single extent record.
2635 temp = left.br_blockcount + new->br_blockcount +
2636 right.br_blockcount;
2638 oldlen = startblockval(left.br_startblock) +
2639 startblockval(new->br_startblock) +
2640 startblockval(right.br_startblock);
2641 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2643 left.br_startblock = nullstartblock(newlen);
2644 left.br_blockcount = temp;
2646 xfs_iext_remove(ip, icur, state);
2647 xfs_iext_prev(ifp, icur);
2648 xfs_iext_update_extent(ip, state, icur, &left);
2651 case BMAP_LEFT_CONTIG:
2653 * New allocation is contiguous with a delayed allocation
2655 * Merge the new allocation with the left neighbor.
2657 temp = left.br_blockcount + new->br_blockcount;
2659 oldlen = startblockval(left.br_startblock) +
2660 startblockval(new->br_startblock);
2661 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2663 left.br_blockcount = temp;
2664 left.br_startblock = nullstartblock(newlen);
2666 xfs_iext_prev(ifp, icur);
2667 xfs_iext_update_extent(ip, state, icur, &left);
2670 case BMAP_RIGHT_CONTIG:
2672 * New allocation is contiguous with a delayed allocation
2674 * Merge the new allocation with the right neighbor.
2676 temp = new->br_blockcount + right.br_blockcount;
2677 oldlen = startblockval(new->br_startblock) +
2678 startblockval(right.br_startblock);
2679 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2681 right.br_startoff = new->br_startoff;
2682 right.br_startblock = nullstartblock(newlen);
2683 right.br_blockcount = temp;
2684 xfs_iext_update_extent(ip, state, icur, &right);
2689 * New allocation is not contiguous with another
2690 * delayed allocation.
2691 * Insert a new entry.
2693 oldlen = newlen = 0;
2694 xfs_iext_insert(ip, icur, new, state);
2697 if (oldlen != newlen) {
2698 ASSERT(oldlen > newlen);
2699 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2702 * Nothing to do for disk quota accounting here.
2704 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2709 * Convert a hole to a real allocation.
2711 STATIC int /* error */
2712 xfs_bmap_add_extent_hole_real(
2713 struct xfs_trans *tp,
2714 struct xfs_inode *ip,
2716 struct xfs_iext_cursor *icur,
2717 struct xfs_btree_cur **curp,
2718 struct xfs_bmbt_irec *new,
2722 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2723 struct xfs_mount *mp = ip->i_mount;
2724 struct xfs_btree_cur *cur = *curp;
2725 int error; /* error return value */
2726 int i; /* temp state */
2727 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2728 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2729 int rval=0; /* return value (logging flags) */
2730 int state = xfs_bmap_fork_to_state(whichfork);
2731 struct xfs_bmbt_irec old;
2733 ASSERT(!isnullstartblock(new->br_startblock));
2734 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2736 XFS_STATS_INC(mp, xs_add_exlist);
2739 * Check and set flags if this segment has a left neighbor.
2741 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2742 state |= BMAP_LEFT_VALID;
2743 if (isnullstartblock(left.br_startblock))
2744 state |= BMAP_LEFT_DELAY;
2748 * Check and set flags if this segment has a current value.
2749 * Not true if we're inserting into the "hole" at eof.
2751 if (xfs_iext_get_extent(ifp, icur, &right)) {
2752 state |= BMAP_RIGHT_VALID;
2753 if (isnullstartblock(right.br_startblock))
2754 state |= BMAP_RIGHT_DELAY;
2758 * We're inserting a real allocation between "left" and "right".
2759 * Set the contiguity flags. Don't let extents get too large.
2761 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2762 left.br_startoff + left.br_blockcount == new->br_startoff &&
2763 left.br_startblock + left.br_blockcount == new->br_startblock &&
2764 left.br_state == new->br_state &&
2765 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2766 state |= BMAP_LEFT_CONTIG;
2768 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2769 new->br_startoff + new->br_blockcount == right.br_startoff &&
2770 new->br_startblock + new->br_blockcount == right.br_startblock &&
2771 new->br_state == right.br_state &&
2772 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2773 (!(state & BMAP_LEFT_CONTIG) ||
2774 left.br_blockcount + new->br_blockcount +
2775 right.br_blockcount <= MAXEXTLEN))
2776 state |= BMAP_RIGHT_CONTIG;
2780 * Select which case we're in here, and implement it.
2782 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2783 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2785 * New allocation is contiguous with real allocations on the
2786 * left and on the right.
2787 * Merge all three into a single extent record.
2789 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2791 xfs_iext_remove(ip, icur, state);
2792 xfs_iext_prev(ifp, icur);
2793 xfs_iext_update_extent(ip, state, icur, &left);
2797 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2799 rval = XFS_ILOG_CORE;
2800 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2803 if (XFS_IS_CORRUPT(mp, i != 1)) {
2804 error = -EFSCORRUPTED;
2807 error = xfs_btree_delete(cur, &i);
2810 if (XFS_IS_CORRUPT(mp, i != 1)) {
2811 error = -EFSCORRUPTED;
2814 error = xfs_btree_decrement(cur, 0, &i);
2817 if (XFS_IS_CORRUPT(mp, i != 1)) {
2818 error = -EFSCORRUPTED;
2821 error = xfs_bmbt_update(cur, &left);
2827 case BMAP_LEFT_CONTIG:
2829 * New allocation is contiguous with a real allocation
2831 * Merge the new allocation with the left neighbor.
2834 left.br_blockcount += new->br_blockcount;
2836 xfs_iext_prev(ifp, icur);
2837 xfs_iext_update_extent(ip, state, icur, &left);
2840 rval = xfs_ilog_fext(whichfork);
2843 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2846 if (XFS_IS_CORRUPT(mp, i != 1)) {
2847 error = -EFSCORRUPTED;
2850 error = xfs_bmbt_update(cur, &left);
2856 case BMAP_RIGHT_CONTIG:
2858 * New allocation is contiguous with a real allocation
2860 * Merge the new allocation with the right neighbor.
2864 right.br_startoff = new->br_startoff;
2865 right.br_startblock = new->br_startblock;
2866 right.br_blockcount += new->br_blockcount;
2867 xfs_iext_update_extent(ip, state, icur, &right);
2870 rval = xfs_ilog_fext(whichfork);
2873 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2876 if (XFS_IS_CORRUPT(mp, i != 1)) {
2877 error = -EFSCORRUPTED;
2880 error = xfs_bmbt_update(cur, &right);
2888 * New allocation is not contiguous with another
2890 * Insert a new entry.
2892 xfs_iext_insert(ip, icur, new, state);
2896 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2898 rval = XFS_ILOG_CORE;
2899 error = xfs_bmbt_lookup_eq(cur, new, &i);
2902 if (XFS_IS_CORRUPT(mp, i != 0)) {
2903 error = -EFSCORRUPTED;
2906 error = xfs_btree_insert(cur, &i);
2909 if (XFS_IS_CORRUPT(mp, i != 1)) {
2910 error = -EFSCORRUPTED;
2917 /* add reverse mapping unless caller opted out */
2918 if (!(flags & XFS_BMAPI_NORMAP))
2919 xfs_rmap_map_extent(tp, ip, whichfork, new);
2921 /* convert to a btree if necessary */
2922 if (xfs_bmap_needs_btree(ip, whichfork)) {
2923 int tmp_logflags; /* partial log flag return val */
2925 ASSERT(cur == NULL);
2926 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2927 &tmp_logflags, whichfork);
2928 *logflagsp |= tmp_logflags;
2934 /* clear out the allocated field, done with it now in any case. */
2936 cur->bc_ino.allocated = 0;
2938 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2945 * Functions used in the extent read, allocate and remove paths
2949 * Adjust the size of the new extent based on di_extsize and rt extsize.
2952 xfs_bmap_extsize_align(
2954 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2955 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2956 xfs_extlen_t extsz, /* align to this extent size */
2957 int rt, /* is this a realtime inode? */
2958 int eof, /* is extent at end-of-file? */
2959 int delay, /* creating delalloc extent? */
2960 int convert, /* overwriting unwritten extent? */
2961 xfs_fileoff_t *offp, /* in/out: aligned offset */
2962 xfs_extlen_t *lenp) /* in/out: aligned length */
2964 xfs_fileoff_t orig_off; /* original offset */
2965 xfs_extlen_t orig_alen; /* original length */
2966 xfs_fileoff_t orig_end; /* original off+len */
2967 xfs_fileoff_t nexto; /* next file offset */
2968 xfs_fileoff_t prevo; /* previous file offset */
2969 xfs_fileoff_t align_off; /* temp for offset */
2970 xfs_extlen_t align_alen; /* temp for length */
2971 xfs_extlen_t temp; /* temp for calculations */
2976 orig_off = align_off = *offp;
2977 orig_alen = align_alen = *lenp;
2978 orig_end = orig_off + orig_alen;
2981 * If this request overlaps an existing extent, then don't
2982 * attempt to perform any additional alignment.
2984 if (!delay && !eof &&
2985 (orig_off >= gotp->br_startoff) &&
2986 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2991 * If the file offset is unaligned vs. the extent size
2992 * we need to align it. This will be possible unless
2993 * the file was previously written with a kernel that didn't
2994 * perform this alignment, or if a truncate shot us in the
2997 div_u64_rem(orig_off, extsz, &temp);
3003 /* Same adjustment for the end of the requested area. */
3004 temp = (align_alen % extsz);
3006 align_alen += extsz - temp;
3009 * For large extent hint sizes, the aligned extent might be larger than
3010 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3011 * the length back under MAXEXTLEN. The outer allocation loops handle
3012 * short allocation just fine, so it is safe to do this. We only want to
3013 * do it when we are forced to, though, because it means more allocation
3014 * operations are required.
3016 while (align_alen > MAXEXTLEN)
3017 align_alen -= extsz;
3018 ASSERT(align_alen <= MAXEXTLEN);
3021 * If the previous block overlaps with this proposed allocation
3022 * then move the start forward without adjusting the length.
3024 if (prevp->br_startoff != NULLFILEOFF) {
3025 if (prevp->br_startblock == HOLESTARTBLOCK)
3026 prevo = prevp->br_startoff;
3028 prevo = prevp->br_startoff + prevp->br_blockcount;
3031 if (align_off != orig_off && align_off < prevo)
3034 * If the next block overlaps with this proposed allocation
3035 * then move the start back without adjusting the length,
3036 * but not before offset 0.
3037 * This may of course make the start overlap previous block,
3038 * and if we hit the offset 0 limit then the next block
3039 * can still overlap too.
3041 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3042 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3043 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3044 nexto = gotp->br_startoff + gotp->br_blockcount;
3046 nexto = gotp->br_startoff;
3048 nexto = NULLFILEOFF;
3050 align_off + align_alen != orig_end &&
3051 align_off + align_alen > nexto)
3052 align_off = nexto > align_alen ? nexto - align_alen : 0;
3054 * If we're now overlapping the next or previous extent that
3055 * means we can't fit an extsz piece in this hole. Just move
3056 * the start forward to the first valid spot and set
3057 * the length so we hit the end.
3059 if (align_off != orig_off && align_off < prevo)
3061 if (align_off + align_alen != orig_end &&
3062 align_off + align_alen > nexto &&
3063 nexto != NULLFILEOFF) {
3064 ASSERT(nexto > prevo);
3065 align_alen = nexto - align_off;
3069 * If realtime, and the result isn't a multiple of the realtime
3070 * extent size we need to remove blocks until it is.
3072 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3074 * We're not covering the original request, or
3075 * we won't be able to once we fix the length.
3077 if (orig_off < align_off ||
3078 orig_end > align_off + align_alen ||
3079 align_alen - temp < orig_alen)
3082 * Try to fix it by moving the start up.
3084 if (align_off + temp <= orig_off) {
3089 * Try to fix it by moving the end in.
3091 else if (align_off + align_alen - temp >= orig_end)
3094 * Set the start to the minimum then trim the length.
3097 align_alen -= orig_off - align_off;
3098 align_off = orig_off;
3099 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3102 * Result doesn't cover the request, fail it.
3104 if (orig_off < align_off || orig_end > align_off + align_alen)
3107 ASSERT(orig_off >= align_off);
3108 /* see MAXEXTLEN handling above */
3109 ASSERT(orig_end <= align_off + align_alen ||
3110 align_alen + extsz > MAXEXTLEN);
3114 if (!eof && gotp->br_startoff != NULLFILEOFF)
3115 ASSERT(align_off + align_alen <= gotp->br_startoff);
3116 if (prevp->br_startoff != NULLFILEOFF)
3117 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3125 #define XFS_ALLOC_GAP_UNITS 4
3129 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3131 xfs_fsblock_t adjust; /* adjustment to block numbers */
3132 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3133 xfs_mount_t *mp; /* mount point structure */
3134 int nullfb; /* true if ap->firstblock isn't set */
3135 int rt; /* true if inode is realtime */
3137 #define ISVALID(x,y) \
3139 (x) < mp->m_sb.sb_rblocks : \
3140 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3141 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3142 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3144 mp = ap->ip->i_mount;
3145 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3146 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3147 (ap->datatype & XFS_ALLOC_USERDATA);
3148 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3149 ap->tp->t_firstblock);
3151 * If allocating at eof, and there's a previous real block,
3152 * try to use its last block as our starting point.
3154 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3155 !isnullstartblock(ap->prev.br_startblock) &&
3156 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3157 ap->prev.br_startblock)) {
3158 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3160 * Adjust for the gap between prevp and us.
3162 adjust = ap->offset -
3163 (ap->prev.br_startoff + ap->prev.br_blockcount);
3165 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3166 ap->blkno += adjust;
3169 * If not at eof, then compare the two neighbor blocks.
3170 * Figure out whether either one gives us a good starting point,
3171 * and pick the better one.
3173 else if (!ap->eof) {
3174 xfs_fsblock_t gotbno; /* right side block number */
3175 xfs_fsblock_t gotdiff=0; /* right side difference */
3176 xfs_fsblock_t prevbno; /* left side block number */
3177 xfs_fsblock_t prevdiff=0; /* left side difference */
3180 * If there's a previous (left) block, select a requested
3181 * start block based on it.
3183 if (ap->prev.br_startoff != NULLFILEOFF &&
3184 !isnullstartblock(ap->prev.br_startblock) &&
3185 (prevbno = ap->prev.br_startblock +
3186 ap->prev.br_blockcount) &&
3187 ISVALID(prevbno, ap->prev.br_startblock)) {
3189 * Calculate gap to end of previous block.
3191 adjust = prevdiff = ap->offset -
3192 (ap->prev.br_startoff +
3193 ap->prev.br_blockcount);
3195 * Figure the startblock based on the previous block's
3196 * end and the gap size.
3198 * If the gap is large relative to the piece we're
3199 * allocating, or using it gives us an invalid block
3200 * number, then just use the end of the previous block.
3202 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3203 ISVALID(prevbno + prevdiff,
3204 ap->prev.br_startblock))
3209 * If the firstblock forbids it, can't use it,
3212 if (!rt && !nullfb &&
3213 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3214 prevbno = NULLFSBLOCK;
3217 * No previous block or can't follow it, just default.
3220 prevbno = NULLFSBLOCK;
3222 * If there's a following (right) block, select a requested
3223 * start block based on it.
3225 if (!isnullstartblock(ap->got.br_startblock)) {
3227 * Calculate gap to start of next block.
3229 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3231 * Figure the startblock based on the next block's
3232 * start and the gap size.
3234 gotbno = ap->got.br_startblock;
3237 * If the gap is large relative to the piece we're
3238 * allocating, or using it gives us an invalid block
3239 * number, then just use the start of the next block
3240 * offset by our length.
3242 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3243 ISVALID(gotbno - gotdiff, gotbno))
3245 else if (ISVALID(gotbno - ap->length, gotbno)) {
3246 gotbno -= ap->length;
3247 gotdiff += adjust - ap->length;
3251 * If the firstblock forbids it, can't use it,
3254 if (!rt && !nullfb &&
3255 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3256 gotbno = NULLFSBLOCK;
3259 * No next block, just default.
3262 gotbno = NULLFSBLOCK;
3264 * If both valid, pick the better one, else the only good
3265 * one, else ap->blkno is already set (to 0 or the inode block).
3267 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3268 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3269 else if (prevbno != NULLFSBLOCK)
3270 ap->blkno = prevbno;
3271 else if (gotbno != NULLFSBLOCK)
3278 xfs_bmap_longest_free_extent(
3279 struct xfs_trans *tp,
3284 struct xfs_mount *mp = tp->t_mountp;
3285 struct xfs_perag *pag;
3286 xfs_extlen_t longest;
3289 pag = xfs_perag_get(mp, ag);
3290 if (!pag->pagf_init) {
3291 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3293 /* Couldn't lock the AGF, so skip this AG. */
3294 if (error == -EAGAIN) {
3302 longest = xfs_alloc_longest_free_extent(pag,
3303 xfs_alloc_min_freelist(mp, pag),
3304 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3305 if (*blen < longest)
3314 xfs_bmap_select_minlen(
3315 struct xfs_bmalloca *ap,
3316 struct xfs_alloc_arg *args,
3320 if (notinit || *blen < ap->minlen) {
3322 * Since we did a BUF_TRYLOCK above, it is possible that
3323 * there is space for this request.
3325 args->minlen = ap->minlen;
3326 } else if (*blen < args->maxlen) {
3328 * If the best seen length is less than the request length,
3329 * use the best as the minimum.
3331 args->minlen = *blen;
3334 * Otherwise we've seen an extent as big as maxlen, use that
3337 args->minlen = args->maxlen;
3342 xfs_bmap_btalloc_nullfb(
3343 struct xfs_bmalloca *ap,
3344 struct xfs_alloc_arg *args,
3347 struct xfs_mount *mp = ap->ip->i_mount;
3348 xfs_agnumber_t ag, startag;
3352 args->type = XFS_ALLOCTYPE_START_BNO;
3353 args->total = ap->total;
3355 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3356 if (startag == NULLAGNUMBER)
3359 while (*blen < args->maxlen) {
3360 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3365 if (++ag == mp->m_sb.sb_agcount)
3371 xfs_bmap_select_minlen(ap, args, blen, notinit);
3376 xfs_bmap_btalloc_filestreams(
3377 struct xfs_bmalloca *ap,
3378 struct xfs_alloc_arg *args,
3381 struct xfs_mount *mp = ap->ip->i_mount;
3386 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3387 args->total = ap->total;
3389 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3390 if (ag == NULLAGNUMBER)
3393 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3397 if (*blen < args->maxlen) {
3398 error = xfs_filestream_new_ag(ap, &ag);
3402 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3409 xfs_bmap_select_minlen(ap, args, blen, notinit);
3412 * Set the failure fallback case to look in the selected AG as stream
3415 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3419 /* Update all inode and quota accounting for the allocation we just did. */
3421 xfs_bmap_btalloc_accounting(
3422 struct xfs_bmalloca *ap,
3423 struct xfs_alloc_arg *args)
3425 if (ap->flags & XFS_BMAPI_COWFORK) {
3427 * COW fork blocks are in-core only and thus are treated as
3428 * in-core quota reservation (like delalloc blocks) even when
3429 * converted to real blocks. The quota reservation is not
3430 * accounted to disk until blocks are remapped to the data
3431 * fork. So if these blocks were previously delalloc, we
3432 * already have quota reservation and there's nothing to do
3436 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3441 * Otherwise, we've allocated blocks in a hole. The transaction
3442 * has acquired in-core quota reservation for this extent.
3443 * Rather than account these as real blocks, however, we reduce
3444 * the transaction quota reservation based on the allocation.
3445 * This essentially transfers the transaction quota reservation
3446 * to that of a delalloc extent.
3448 ap->ip->i_delayed_blks += args->len;
3449 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3454 /* data/attr fork only */
3455 ap->ip->i_d.di_nblocks += args->len;
3456 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3458 ap->ip->i_delayed_blks -= args->len;
3459 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3461 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3462 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3468 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3470 xfs_mount_t *mp; /* mount point structure */
3471 xfs_alloctype_t atype = 0; /* type for allocation routines */
3472 xfs_extlen_t align = 0; /* minimum allocation alignment */
3473 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3475 xfs_alloc_arg_t args;
3476 xfs_fileoff_t orig_offset;
3477 xfs_extlen_t orig_length;
3479 xfs_extlen_t nextminlen = 0;
3480 int nullfb; /* true if ap->firstblock isn't set */
3487 orig_offset = ap->offset;
3488 orig_length = ap->length;
3490 mp = ap->ip->i_mount;
3492 /* stripe alignment for allocation is determined by mount parameters */
3494 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3495 stripe_align = mp->m_swidth;
3496 else if (mp->m_dalign)
3497 stripe_align = mp->m_dalign;
3499 if (ap->flags & XFS_BMAPI_COWFORK)
3500 align = xfs_get_cowextsz_hint(ap->ip);
3501 else if (ap->datatype & XFS_ALLOC_USERDATA)
3502 align = xfs_get_extsz_hint(ap->ip);
3504 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3505 align, 0, ap->eof, 0, ap->conv,
3506 &ap->offset, &ap->length);
3512 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3513 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3514 ap->tp->t_firstblock);
3516 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3517 xfs_inode_is_filestream(ap->ip)) {
3518 ag = xfs_filestream_lookup_ag(ap->ip);
3519 ag = (ag != NULLAGNUMBER) ? ag : 0;
3520 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3522 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3525 ap->blkno = ap->tp->t_firstblock;
3527 xfs_bmap_adjacent(ap);
3530 * If allowed, use ap->blkno; otherwise must use firstblock since
3531 * it's in the right allocation group.
3533 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3536 ap->blkno = ap->tp->t_firstblock;
3538 * Normal allocation, done through xfs_alloc_vextent.
3540 tryagain = isaligned = 0;
3541 memset(&args, 0, sizeof(args));
3544 args.fsbno = ap->blkno;
3545 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3547 /* Trim the allocation back to the maximum an AG can fit. */
3548 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3552 * Search for an allocation group with a single extent large
3553 * enough for the request. If one isn't found, then adjust
3554 * the minimum allocation size to the largest space found.
3556 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3557 xfs_inode_is_filestream(ap->ip))
3558 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3560 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3563 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3564 if (xfs_inode_is_filestream(ap->ip))
3565 args.type = XFS_ALLOCTYPE_FIRST_AG;
3567 args.type = XFS_ALLOCTYPE_START_BNO;
3568 args.total = args.minlen = ap->minlen;
3570 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3571 args.total = ap->total;
3572 args.minlen = ap->minlen;
3574 /* apply extent size hints if obtained earlier */
3577 div_u64_rem(ap->offset, args.prod, &args.mod);
3579 args.mod = args.prod - args.mod;
3580 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3584 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3585 div_u64_rem(ap->offset, args.prod, &args.mod);
3587 args.mod = args.prod - args.mod;
3590 * If we are not low on available data blocks, and the underlying
3591 * logical volume manager is a stripe, and the file offset is zero then
3592 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3593 * is only set if the allocation length is >= the stripe unit and the
3594 * allocation offset is at the end of file.
3596 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3598 args.alignment = stripe_align;
3602 * Adjust minlen to try and preserve alignment if we
3603 * can't guarantee an aligned maxlen extent.
3605 if (blen > args.alignment &&
3606 blen <= args.maxlen + args.alignment)
3607 args.minlen = blen - args.alignment;
3608 args.minalignslop = 0;
3611 * First try an exact bno allocation.
3612 * If it fails then do a near or start bno
3613 * allocation with alignment turned on.
3617 args.type = XFS_ALLOCTYPE_THIS_BNO;
3620 * Compute the minlen+alignment for the
3621 * next case. Set slop so that the value
3622 * of minlen+alignment+slop doesn't go up
3623 * between the calls.
3625 if (blen > stripe_align && blen <= args.maxlen)
3626 nextminlen = blen - stripe_align;
3628 nextminlen = args.minlen;
3629 if (nextminlen + stripe_align > args.minlen + 1)
3631 nextminlen + stripe_align -
3634 args.minalignslop = 0;
3638 args.minalignslop = 0;
3640 args.minleft = ap->minleft;
3641 args.wasdel = ap->wasdel;
3642 args.resv = XFS_AG_RESV_NONE;
3643 args.datatype = ap->datatype;
3645 error = xfs_alloc_vextent(&args);
3649 if (tryagain && args.fsbno == NULLFSBLOCK) {
3651 * Exact allocation failed. Now try with alignment
3655 args.fsbno = ap->blkno;
3656 args.alignment = stripe_align;
3657 args.minlen = nextminlen;
3658 args.minalignslop = 0;
3660 if ((error = xfs_alloc_vextent(&args)))
3663 if (isaligned && args.fsbno == NULLFSBLOCK) {
3665 * allocation failed, so turn off alignment and
3669 args.fsbno = ap->blkno;
3671 if ((error = xfs_alloc_vextent(&args)))
3674 if (args.fsbno == NULLFSBLOCK && nullfb &&
3675 args.minlen > ap->minlen) {
3676 args.minlen = ap->minlen;
3677 args.type = XFS_ALLOCTYPE_START_BNO;
3678 args.fsbno = ap->blkno;
3679 if ((error = xfs_alloc_vextent(&args)))
3682 if (args.fsbno == NULLFSBLOCK && nullfb) {
3684 args.type = XFS_ALLOCTYPE_FIRST_AG;
3685 args.total = ap->minlen;
3686 if ((error = xfs_alloc_vextent(&args)))
3688 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3690 if (args.fsbno != NULLFSBLOCK) {
3692 * check the allocation happened at the same or higher AG than
3693 * the first block that was allocated.
3695 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3696 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
3697 XFS_FSB_TO_AGNO(mp, args.fsbno));
3699 ap->blkno = args.fsbno;
3700 if (ap->tp->t_firstblock == NULLFSBLOCK)
3701 ap->tp->t_firstblock = args.fsbno;
3702 ASSERT(nullfb || fb_agno <= args.agno);
3703 ap->length = args.len;
3705 * If the extent size hint is active, we tried to round the
3706 * caller's allocation request offset down to extsz and the
3707 * length up to another extsz boundary. If we found a free
3708 * extent we mapped it in starting at this new offset. If the
3709 * newly mapped space isn't long enough to cover any of the
3710 * range of offsets that was originally requested, move the
3711 * mapping up so that we can fill as much of the caller's
3712 * original request as possible. Free space is apparently
3713 * very fragmented so we're unlikely to be able to satisfy the
3716 if (ap->length <= orig_length)
3717 ap->offset = orig_offset;
3718 else if (ap->offset + ap->length < orig_offset + orig_length)
3719 ap->offset = orig_offset + orig_length - ap->length;
3720 xfs_bmap_btalloc_accounting(ap, &args);
3722 ap->blkno = NULLFSBLOCK;
3728 /* Trim extent to fit a logical block range. */
3731 struct xfs_bmbt_irec *irec,
3735 xfs_fileoff_t distance;
3736 xfs_fileoff_t end = bno + len;
3738 if (irec->br_startoff + irec->br_blockcount <= bno ||
3739 irec->br_startoff >= end) {
3740 irec->br_blockcount = 0;
3744 if (irec->br_startoff < bno) {
3745 distance = bno - irec->br_startoff;
3746 if (isnullstartblock(irec->br_startblock))
3747 irec->br_startblock = DELAYSTARTBLOCK;
3748 if (irec->br_startblock != DELAYSTARTBLOCK &&
3749 irec->br_startblock != HOLESTARTBLOCK)
3750 irec->br_startblock += distance;
3751 irec->br_startoff += distance;
3752 irec->br_blockcount -= distance;
3755 if (end < irec->br_startoff + irec->br_blockcount) {
3756 distance = irec->br_startoff + irec->br_blockcount - end;
3757 irec->br_blockcount -= distance;
3762 * Trim the returned map to the required bounds
3766 struct xfs_bmbt_irec *mval,
3767 struct xfs_bmbt_irec *got,
3775 if ((flags & XFS_BMAPI_ENTIRE) ||
3776 got->br_startoff + got->br_blockcount <= obno) {
3778 if (isnullstartblock(got->br_startblock))
3779 mval->br_startblock = DELAYSTARTBLOCK;
3785 ASSERT((*bno >= obno) || (n == 0));
3787 mval->br_startoff = *bno;
3788 if (isnullstartblock(got->br_startblock))
3789 mval->br_startblock = DELAYSTARTBLOCK;
3791 mval->br_startblock = got->br_startblock +
3792 (*bno - got->br_startoff);
3794 * Return the minimum of what we got and what we asked for for
3795 * the length. We can use the len variable here because it is
3796 * modified below and we could have been there before coming
3797 * here if the first part of the allocation didn't overlap what
3800 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3801 got->br_blockcount - (*bno - got->br_startoff));
3802 mval->br_state = got->br_state;
3803 ASSERT(mval->br_blockcount <= len);
3808 * Update and validate the extent map to return
3811 xfs_bmapi_update_map(
3812 struct xfs_bmbt_irec **map,
3820 xfs_bmbt_irec_t *mval = *map;
3822 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3823 ((mval->br_startoff + mval->br_blockcount) <= end));
3824 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3825 (mval->br_startoff < obno));
3827 *bno = mval->br_startoff + mval->br_blockcount;
3829 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3830 /* update previous map with new information */
3831 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3832 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3833 ASSERT(mval->br_state == mval[-1].br_state);
3834 mval[-1].br_blockcount = mval->br_blockcount;
3835 mval[-1].br_state = mval->br_state;
3836 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3837 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3838 mval[-1].br_startblock != HOLESTARTBLOCK &&
3839 mval->br_startblock == mval[-1].br_startblock +
3840 mval[-1].br_blockcount &&
3841 mval[-1].br_state == mval->br_state) {
3842 ASSERT(mval->br_startoff ==
3843 mval[-1].br_startoff + mval[-1].br_blockcount);
3844 mval[-1].br_blockcount += mval->br_blockcount;
3845 } else if (*n > 0 &&
3846 mval->br_startblock == DELAYSTARTBLOCK &&
3847 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3848 mval->br_startoff ==
3849 mval[-1].br_startoff + mval[-1].br_blockcount) {
3850 mval[-1].br_blockcount += mval->br_blockcount;
3851 mval[-1].br_state = mval->br_state;
3852 } else if (!((*n == 0) &&
3853 ((mval->br_startoff + mval->br_blockcount) <=
3862 * Map file blocks to filesystem blocks without allocation.
3866 struct xfs_inode *ip,
3869 struct xfs_bmbt_irec *mval,
3873 struct xfs_mount *mp = ip->i_mount;
3874 int whichfork = xfs_bmapi_whichfork(flags);
3875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3876 struct xfs_bmbt_irec got;
3879 struct xfs_iext_cursor icur;
3885 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3886 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3888 if (WARN_ON_ONCE(!ifp))
3889 return -EFSCORRUPTED;
3891 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3892 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
3893 return -EFSCORRUPTED;
3895 if (XFS_FORCED_SHUTDOWN(mp))
3898 XFS_STATS_INC(mp, xs_blk_mapr);
3900 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3901 error = xfs_iread_extents(NULL, ip, whichfork);
3906 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3911 while (bno < end && n < *nmap) {
3912 /* Reading past eof, act as though there's a hole up to end. */
3914 got.br_startoff = end;
3915 if (got.br_startoff > bno) {
3916 /* Reading in a hole. */
3917 mval->br_startoff = bno;
3918 mval->br_startblock = HOLESTARTBLOCK;
3919 mval->br_blockcount =
3920 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3921 mval->br_state = XFS_EXT_NORM;
3922 bno += mval->br_blockcount;
3923 len -= mval->br_blockcount;
3929 /* set up the extent map to return. */
3930 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3931 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3933 /* If we're done, stop now. */
3934 if (bno >= end || n >= *nmap)
3937 /* Else go on to the next record. */
3938 if (!xfs_iext_next_extent(ifp, &icur, &got))
3946 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3947 * global pool and the extent inserted into the inode in-core extent tree.
3949 * On entry, got refers to the first extent beyond the offset of the extent to
3950 * allocate or eof is specified if no such extent exists. On return, got refers
3951 * to the extent record that was inserted to the inode fork.
3953 * Note that the allocated extent may have been merged with contiguous extents
3954 * during insertion into the inode fork. Thus, got does not reflect the current
3955 * state of the inode fork on return. If necessary, the caller can use lastx to
3956 * look up the updated record in the inode fork.
3959 xfs_bmapi_reserve_delalloc(
3960 struct xfs_inode *ip,
3964 xfs_filblks_t prealloc,
3965 struct xfs_bmbt_irec *got,
3966 struct xfs_iext_cursor *icur,
3969 struct xfs_mount *mp = ip->i_mount;
3970 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3972 xfs_extlen_t indlen;
3974 xfs_fileoff_t aoff = off;
3977 * Cap the alloc length. Keep track of prealloc so we know whether to
3978 * tag the inode before we return.
3980 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
3982 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3983 if (prealloc && alen >= len)
3984 prealloc = alen - len;
3986 /* Figure out the extent size, adjust alen */
3987 if (whichfork == XFS_COW_FORK) {
3988 struct xfs_bmbt_irec prev;
3989 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3991 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3992 prev.br_startoff = NULLFILEOFF;
3994 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3995 1, 0, &aoff, &alen);
4000 * Make a transaction-less quota reservation for delayed allocation
4001 * blocks. This number gets adjusted later. We return if we haven't
4002 * allocated blocks already inside this loop.
4004 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4005 XFS_QMOPT_RES_REGBLKS);
4010 * Split changing sb for alen and indlen since they could be coming
4011 * from different places.
4013 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4016 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4018 goto out_unreserve_quota;
4020 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4022 goto out_unreserve_blocks;
4025 ip->i_delayed_blks += alen;
4026 xfs_mod_delalloc(ip->i_mount, alen + indlen);
4028 got->br_startoff = aoff;
4029 got->br_startblock = nullstartblock(indlen);
4030 got->br_blockcount = alen;
4031 got->br_state = XFS_EXT_NORM;
4033 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4036 * Tag the inode if blocks were preallocated. Note that COW fork
4037 * preallocation can occur at the start or end of the extent, even when
4038 * prealloc == 0, so we must also check the aligned offset and length.
4040 if (whichfork == XFS_DATA_FORK && prealloc)
4041 xfs_inode_set_eofblocks_tag(ip);
4042 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4043 xfs_inode_set_cowblocks_tag(ip);
4047 out_unreserve_blocks:
4048 xfs_mod_fdblocks(mp, alen, false);
4049 out_unreserve_quota:
4050 if (XFS_IS_QUOTA_ON(mp))
4051 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4052 XFS_QMOPT_RES_REGBLKS);
4057 xfs_bmap_alloc_userdata(
4058 struct xfs_bmalloca *bma)
4060 struct xfs_mount *mp = bma->ip->i_mount;
4061 int whichfork = xfs_bmapi_whichfork(bma->flags);
4065 * Set the data type being allocated. For the data fork, the first data
4066 * in the file is treated differently to all other allocations. For the
4067 * attribute fork, we only need to ensure the allocated range is not on
4070 bma->datatype = XFS_ALLOC_NOBUSY;
4071 if (whichfork == XFS_DATA_FORK) {
4072 bma->datatype |= XFS_ALLOC_USERDATA;
4073 if (bma->offset == 0)
4074 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4076 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4077 error = xfs_bmap_isaeof(bma, whichfork);
4082 if (XFS_IS_REALTIME_INODE(bma->ip))
4083 return xfs_bmap_rtalloc(bma);
4086 return xfs_bmap_btalloc(bma);
4091 struct xfs_bmalloca *bma)
4093 struct xfs_mount *mp = bma->ip->i_mount;
4094 int whichfork = xfs_bmapi_whichfork(bma->flags);
4095 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4096 int tmp_logflags = 0;
4099 ASSERT(bma->length > 0);
4102 * For the wasdelay case, we could also just allocate the stuff asked
4103 * for in this bmap call but that wouldn't be as good.
4106 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4107 bma->offset = bma->got.br_startoff;
4108 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4109 bma->prev.br_startoff = NULLFILEOFF;
4111 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4113 bma->length = XFS_FILBLKS_MIN(bma->length,
4114 bma->got.br_startoff - bma->offset);
4117 if (bma->flags & XFS_BMAPI_CONTIG)
4118 bma->minlen = bma->length;
4122 if (bma->flags & XFS_BMAPI_METADATA)
4123 error = xfs_bmap_btalloc(bma);
4125 error = xfs_bmap_alloc_userdata(bma);
4126 if (error || bma->blkno == NULLFSBLOCK)
4129 if (bma->flags & XFS_BMAPI_ZERO) {
4130 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4135 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
4136 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4138 * Bump the number of extents we've allocated
4144 bma->cur->bc_ino.flags =
4145 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4147 bma->got.br_startoff = bma->offset;
4148 bma->got.br_startblock = bma->blkno;
4149 bma->got.br_blockcount = bma->length;
4150 bma->got.br_state = XFS_EXT_NORM;
4152 if (bma->flags & XFS_BMAPI_PREALLOC)
4153 bma->got.br_state = XFS_EXT_UNWRITTEN;
4156 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4158 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4159 whichfork, &bma->icur, &bma->cur, &bma->got,
4160 &bma->logflags, bma->flags);
4162 bma->logflags |= tmp_logflags;
4167 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4168 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4169 * the neighbouring ones.
4171 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4173 ASSERT(bma->got.br_startoff <= bma->offset);
4174 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4175 bma->offset + bma->length);
4176 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4177 bma->got.br_state == XFS_EXT_UNWRITTEN);
4182 xfs_bmapi_convert_unwritten(
4183 struct xfs_bmalloca *bma,
4184 struct xfs_bmbt_irec *mval,
4188 int whichfork = xfs_bmapi_whichfork(flags);
4189 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4190 int tmp_logflags = 0;
4193 /* check if we need to do unwritten->real conversion */
4194 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4195 (flags & XFS_BMAPI_PREALLOC))
4198 /* check if we need to do real->unwritten conversion */
4199 if (mval->br_state == XFS_EXT_NORM &&
4200 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4201 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4205 * Modify (by adding) the state flag, if writing.
4207 ASSERT(mval->br_blockcount <= len);
4208 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4209 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4210 bma->ip, whichfork);
4212 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4213 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4216 * Before insertion into the bmbt, zero the range being converted
4219 if (flags & XFS_BMAPI_ZERO) {
4220 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4221 mval->br_blockcount);
4226 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4227 &bma->icur, &bma->cur, mval, &tmp_logflags);
4229 * Log the inode core unconditionally in the unwritten extent conversion
4230 * path because the conversion might not have done so (e.g., if the
4231 * extent count hasn't changed). We need to make sure the inode is dirty
4232 * in the transaction for the sake of fsync(), even if nothing has
4233 * changed, because fsync() will not force the log for this transaction
4234 * unless it sees the inode pinned.
4236 * Note: If we're only converting cow fork extents, there aren't
4237 * any on-disk updates to make, so we don't need to log anything.
4239 if (whichfork != XFS_COW_FORK)
4240 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4245 * Update our extent pointer, given that
4246 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4247 * of the neighbouring ones.
4249 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4252 * We may have combined previously unwritten space with written space,
4253 * so generate another request.
4255 if (mval->br_blockcount < len)
4260 static inline xfs_extlen_t
4262 struct xfs_trans *tp,
4263 struct xfs_inode *ip,
4266 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork);
4268 if (tp && tp->t_firstblock != NULLFSBLOCK)
4270 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4272 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4276 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4277 * a case where the data is changed, there's an error, and it's not logged so we
4278 * don't shutdown when we should. Don't bother logging extents/btree changes if
4279 * we converted to the other format.
4283 struct xfs_bmalloca *bma,
4287 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4289 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4290 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4291 bma->logflags &= ~xfs_ilog_fext(whichfork);
4292 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4293 ifp->if_format != XFS_DINODE_FMT_BTREE)
4294 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4297 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4299 xfs_btree_del_cursor(bma->cur, error);
4303 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4304 * extent state if necessary. Details behaviour is controlled by the flags
4305 * parameter. Only allocates blocks from a single allocation group, to avoid
4310 struct xfs_trans *tp, /* transaction pointer */
4311 struct xfs_inode *ip, /* incore inode */
4312 xfs_fileoff_t bno, /* starting file offs. mapped */
4313 xfs_filblks_t len, /* length to map in file */
4314 int flags, /* XFS_BMAPI_... */
4315 xfs_extlen_t total, /* total blocks needed */
4316 struct xfs_bmbt_irec *mval, /* output: map values */
4317 int *nmap) /* i/o: mval size/count */
4319 struct xfs_bmalloca bma = {
4324 struct xfs_mount *mp = ip->i_mount;
4325 int whichfork = xfs_bmapi_whichfork(flags);
4326 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4327 xfs_fileoff_t end; /* end of mapped file region */
4328 bool eof = false; /* after the end of extents */
4329 int error; /* error return */
4330 int n; /* current extent index */
4331 xfs_fileoff_t obno; /* old block number (offset) */
4334 xfs_fileoff_t orig_bno; /* original block number value */
4335 int orig_flags; /* original flags arg value */
4336 xfs_filblks_t orig_len; /* original value of len arg */
4337 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4338 int orig_nmap; /* original value of *nmap */
4348 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4351 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4352 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4353 ASSERT(!(flags & XFS_BMAPI_REMAP));
4355 /* zeroing is for currently only for data extents, not metadata */
4356 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4357 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4359 * we can allocate unwritten extents or pre-zero allocated blocks,
4360 * but it makes no sense to do both at once. This would result in
4361 * zeroing the unwritten extent twice, but it still being an
4362 * unwritten extent....
4364 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4365 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4367 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4368 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4369 return -EFSCORRUPTED;
4372 if (XFS_FORCED_SHUTDOWN(mp))
4375 XFS_STATS_INC(mp, xs_blk_mapw);
4377 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4378 error = xfs_iread_extents(tp, ip, whichfork);
4383 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4385 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4386 bma.prev.br_startoff = NULLFILEOFF;
4387 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4392 while (bno < end && n < *nmap) {
4393 bool need_alloc = false, wasdelay = false;
4395 /* in hole or beyond EOF? */
4396 if (eof || bma.got.br_startoff > bno) {
4398 * CoW fork conversions should /never/ hit EOF or
4399 * holes. There should always be something for us
4402 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4403 (flags & XFS_BMAPI_COWFORK)));
4406 } else if (isnullstartblock(bma.got.br_startblock)) {
4411 * First, deal with the hole before the allocated space
4412 * that we found, if any.
4414 if (need_alloc || wasdelay) {
4416 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4417 bma.wasdel = wasdelay;
4422 * There's a 32/64 bit type mismatch between the
4423 * allocation length request (which can be 64 bits in
4424 * length) and the bma length request, which is
4425 * xfs_extlen_t and therefore 32 bits. Hence we have to
4426 * check for 32-bit overflows and handle them here.
4428 if (len > (xfs_filblks_t)MAXEXTLEN)
4429 bma.length = MAXEXTLEN;
4434 ASSERT(bma.length > 0);
4435 error = xfs_bmapi_allocate(&bma);
4438 if (bma.blkno == NULLFSBLOCK)
4442 * If this is a CoW allocation, record the data in
4443 * the refcount btree for orphan recovery.
4445 if (whichfork == XFS_COW_FORK)
4446 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4450 /* Deal with the allocated space we found. */
4451 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4454 /* Execute unwritten extent conversion if necessary */
4455 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4456 if (error == -EAGAIN)
4461 /* update the extent map to return */
4462 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4465 * If we're done, stop now. Stop when we've allocated
4466 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4467 * the transaction may get too big.
4469 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4472 /* Else go on to the next record. */
4474 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4479 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4484 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4485 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4486 xfs_bmapi_finish(&bma, whichfork, 0);
4487 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4491 xfs_bmapi_finish(&bma, whichfork, error);
4496 * Convert an existing delalloc extent to real blocks based on file offset. This
4497 * attempts to allocate the entire delalloc extent and may require multiple
4498 * invocations to allocate the target offset if a large enough physical extent
4502 xfs_bmapi_convert_delalloc(
4503 struct xfs_inode *ip,
4506 struct iomap *iomap,
4509 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4510 struct xfs_mount *mp = ip->i_mount;
4511 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4512 struct xfs_bmalloca bma = { NULL };
4514 struct xfs_trans *tp;
4517 if (whichfork == XFS_COW_FORK)
4518 flags |= IOMAP_F_SHARED;
4521 * Space for the extent and indirect blocks was reserved when the
4522 * delalloc extent was created so there's no need to do so here.
4524 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4525 XFS_TRANS_RESERVE, &tp);
4529 xfs_ilock(ip, XFS_ILOCK_EXCL);
4530 xfs_trans_ijoin(tp, ip, 0);
4532 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4533 bma.got.br_startoff > offset_fsb) {
4535 * No extent found in the range we are trying to convert. This
4536 * should only happen for the COW fork, where another thread
4537 * might have moved the extent to the data fork in the meantime.
4539 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4541 goto out_trans_cancel;
4545 * If we find a real extent here we raced with another thread converting
4546 * the extent. Just return the real extent at this offset.
4548 if (!isnullstartblock(bma.got.br_startblock)) {
4549 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
4550 *seq = READ_ONCE(ifp->if_seq);
4551 goto out_trans_cancel;
4557 bma.offset = bma.got.br_startoff;
4558 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4559 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4562 * When we're converting the delalloc reservations backing dirty pages
4563 * in the page cache, we must be careful about how we create the new
4566 * New CoW fork extents are created unwritten, turned into real extents
4567 * when we're about to write the data to disk, and mapped into the data
4568 * fork after the write finishes. End of story.
4570 * New data fork extents must be mapped in as unwritten and converted
4571 * to real extents after the write succeeds to avoid exposing stale
4572 * disk contents if we crash.
4574 bma.flags = XFS_BMAPI_PREALLOC;
4575 if (whichfork == XFS_COW_FORK)
4576 bma.flags |= XFS_BMAPI_COWFORK;
4578 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4579 bma.prev.br_startoff = NULLFILEOFF;
4581 error = xfs_bmapi_allocate(&bma);
4586 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4588 error = -EFSCORRUPTED;
4589 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
4592 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4593 XFS_STATS_INC(mp, xs_xstrat_quick);
4595 ASSERT(!isnullstartblock(bma.got.br_startblock));
4596 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
4597 *seq = READ_ONCE(ifp->if_seq);
4599 if (whichfork == XFS_COW_FORK)
4600 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4602 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4607 xfs_bmapi_finish(&bma, whichfork, 0);
4608 error = xfs_trans_commit(tp);
4609 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4613 xfs_bmapi_finish(&bma, whichfork, error);
4615 xfs_trans_cancel(tp);
4616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4622 struct xfs_trans *tp,
4623 struct xfs_inode *ip,
4626 xfs_fsblock_t startblock,
4629 struct xfs_mount *mp = ip->i_mount;
4630 struct xfs_ifork *ifp;
4631 struct xfs_btree_cur *cur = NULL;
4632 struct xfs_bmbt_irec got;
4633 struct xfs_iext_cursor icur;
4634 int whichfork = xfs_bmapi_whichfork(flags);
4635 int logflags = 0, error;
4637 ifp = XFS_IFORK_PTR(ip, whichfork);
4639 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4640 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4641 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4642 XFS_BMAPI_NORMAP)));
4643 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4644 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4646 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4647 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4648 return -EFSCORRUPTED;
4651 if (XFS_FORCED_SHUTDOWN(mp))
4654 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4655 error = xfs_iread_extents(tp, ip, whichfork);
4660 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4661 /* make sure we only reflink into a hole. */
4662 ASSERT(got.br_startoff > bno);
4663 ASSERT(got.br_startoff - bno >= len);
4666 ip->i_d.di_nblocks += len;
4667 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4669 if (ifp->if_flags & XFS_IFBROOT) {
4670 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4671 cur->bc_ino.flags = 0;
4674 got.br_startoff = bno;
4675 got.br_startblock = startblock;
4676 got.br_blockcount = len;
4677 if (flags & XFS_BMAPI_PREALLOC)
4678 got.br_state = XFS_EXT_UNWRITTEN;
4680 got.br_state = XFS_EXT_NORM;
4682 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4683 &cur, &got, &logflags, flags);
4687 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4690 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4691 logflags &= ~XFS_ILOG_DEXT;
4692 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4693 logflags &= ~XFS_ILOG_DBROOT;
4696 xfs_trans_log_inode(tp, ip, logflags);
4698 xfs_btree_del_cursor(cur, error);
4703 * When a delalloc extent is split (e.g., due to a hole punch), the original
4704 * indlen reservation must be shared across the two new extents that are left
4707 * Given the original reservation and the worst case indlen for the two new
4708 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4709 * reservation fairly across the two new extents. If necessary, steal available
4710 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4711 * ores == 1). The number of stolen blocks is returned. The availability and
4712 * subsequent accounting of stolen blocks is the responsibility of the caller.
4714 static xfs_filblks_t
4715 xfs_bmap_split_indlen(
4716 xfs_filblks_t ores, /* original res. */
4717 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4718 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4719 xfs_filblks_t avail) /* stealable blocks */
4721 xfs_filblks_t len1 = *indlen1;
4722 xfs_filblks_t len2 = *indlen2;
4723 xfs_filblks_t nres = len1 + len2; /* new total res. */
4724 xfs_filblks_t stolen = 0;
4725 xfs_filblks_t resfactor;
4728 * Steal as many blocks as we can to try and satisfy the worst case
4729 * indlen for both new extents.
4731 if (ores < nres && avail)
4732 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4735 /* nothing else to do if we've satisfied the new reservation */
4740 * We can't meet the total required reservation for the two extents.
4741 * Calculate the percent of the overall shortage between both extents
4742 * and apply this percentage to each of the requested indlen values.
4743 * This distributes the shortage fairly and reduces the chances that one
4744 * of the two extents is left with nothing when extents are repeatedly
4747 resfactor = (ores * 100);
4748 do_div(resfactor, nres);
4753 ASSERT(len1 + len2 <= ores);
4754 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4757 * Hand out the remainder to each extent. If one of the two reservations
4758 * is zero, we want to make sure that one gets a block first. The loop
4759 * below starts with len1, so hand len2 a block right off the bat if it
4762 ores -= (len1 + len2);
4763 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4764 if (ores && !len2 && *indlen2) {
4769 if (len1 < *indlen1) {
4775 if (len2 < *indlen2) {
4788 xfs_bmap_del_extent_delay(
4789 struct xfs_inode *ip,
4791 struct xfs_iext_cursor *icur,
4792 struct xfs_bmbt_irec *got,
4793 struct xfs_bmbt_irec *del)
4795 struct xfs_mount *mp = ip->i_mount;
4796 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4797 struct xfs_bmbt_irec new;
4798 int64_t da_old, da_new, da_diff = 0;
4799 xfs_fileoff_t del_endoff, got_endoff;
4800 xfs_filblks_t got_indlen, new_indlen, stolen;
4801 int state = xfs_bmap_fork_to_state(whichfork);
4805 XFS_STATS_INC(mp, xs_del_exlist);
4807 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4808 del_endoff = del->br_startoff + del->br_blockcount;
4809 got_endoff = got->br_startoff + got->br_blockcount;
4810 da_old = startblockval(got->br_startblock);
4813 ASSERT(del->br_blockcount > 0);
4814 ASSERT(got->br_startoff <= del->br_startoff);
4815 ASSERT(got_endoff >= del_endoff);
4818 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4820 do_div(rtexts, mp->m_sb.sb_rextsize);
4821 xfs_mod_frextents(mp, rtexts);
4825 * Update the inode delalloc counter now and wait to update the
4826 * sb counters as we might have to borrow some blocks for the
4827 * indirect block accounting.
4829 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4830 -((long)del->br_blockcount), 0,
4831 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4834 ip->i_delayed_blks -= del->br_blockcount;
4836 if (got->br_startoff == del->br_startoff)
4837 state |= BMAP_LEFT_FILLING;
4838 if (got_endoff == del_endoff)
4839 state |= BMAP_RIGHT_FILLING;
4841 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4842 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4844 * Matches the whole extent. Delete the entry.
4846 xfs_iext_remove(ip, icur, state);
4847 xfs_iext_prev(ifp, icur);
4849 case BMAP_LEFT_FILLING:
4851 * Deleting the first part of the extent.
4853 got->br_startoff = del_endoff;
4854 got->br_blockcount -= del->br_blockcount;
4855 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4856 got->br_blockcount), da_old);
4857 got->br_startblock = nullstartblock((int)da_new);
4858 xfs_iext_update_extent(ip, state, icur, got);
4860 case BMAP_RIGHT_FILLING:
4862 * Deleting the last part of the extent.
4864 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4865 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4866 got->br_blockcount), da_old);
4867 got->br_startblock = nullstartblock((int)da_new);
4868 xfs_iext_update_extent(ip, state, icur, got);
4872 * Deleting the middle of the extent.
4874 * Distribute the original indlen reservation across the two new
4875 * extents. Steal blocks from the deleted extent if necessary.
4876 * Stealing blocks simply fudges the fdblocks accounting below.
4877 * Warn if either of the new indlen reservations is zero as this
4878 * can lead to delalloc problems.
4880 got->br_blockcount = del->br_startoff - got->br_startoff;
4881 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4883 new.br_blockcount = got_endoff - del_endoff;
4884 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4886 WARN_ON_ONCE(!got_indlen || !new_indlen);
4887 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4888 del->br_blockcount);
4890 got->br_startblock = nullstartblock((int)got_indlen);
4892 new.br_startoff = del_endoff;
4893 new.br_state = got->br_state;
4894 new.br_startblock = nullstartblock((int)new_indlen);
4896 xfs_iext_update_extent(ip, state, icur, got);
4897 xfs_iext_next(ifp, icur);
4898 xfs_iext_insert(ip, icur, &new, state);
4900 da_new = got_indlen + new_indlen - stolen;
4901 del->br_blockcount -= stolen;
4905 ASSERT(da_old >= da_new);
4906 da_diff = da_old - da_new;
4908 da_diff += del->br_blockcount;
4910 xfs_mod_fdblocks(mp, da_diff, false);
4911 xfs_mod_delalloc(mp, -da_diff);
4917 xfs_bmap_del_extent_cow(
4918 struct xfs_inode *ip,
4919 struct xfs_iext_cursor *icur,
4920 struct xfs_bmbt_irec *got,
4921 struct xfs_bmbt_irec *del)
4923 struct xfs_mount *mp = ip->i_mount;
4924 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4925 struct xfs_bmbt_irec new;
4926 xfs_fileoff_t del_endoff, got_endoff;
4927 int state = BMAP_COWFORK;
4929 XFS_STATS_INC(mp, xs_del_exlist);
4931 del_endoff = del->br_startoff + del->br_blockcount;
4932 got_endoff = got->br_startoff + got->br_blockcount;
4934 ASSERT(del->br_blockcount > 0);
4935 ASSERT(got->br_startoff <= del->br_startoff);
4936 ASSERT(got_endoff >= del_endoff);
4937 ASSERT(!isnullstartblock(got->br_startblock));
4939 if (got->br_startoff == del->br_startoff)
4940 state |= BMAP_LEFT_FILLING;
4941 if (got_endoff == del_endoff)
4942 state |= BMAP_RIGHT_FILLING;
4944 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4945 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4947 * Matches the whole extent. Delete the entry.
4949 xfs_iext_remove(ip, icur, state);
4950 xfs_iext_prev(ifp, icur);
4952 case BMAP_LEFT_FILLING:
4954 * Deleting the first part of the extent.
4956 got->br_startoff = del_endoff;
4957 got->br_blockcount -= del->br_blockcount;
4958 got->br_startblock = del->br_startblock + del->br_blockcount;
4959 xfs_iext_update_extent(ip, state, icur, got);
4961 case BMAP_RIGHT_FILLING:
4963 * Deleting the last part of the extent.
4965 got->br_blockcount -= del->br_blockcount;
4966 xfs_iext_update_extent(ip, state, icur, got);
4970 * Deleting the middle of the extent.
4972 got->br_blockcount = del->br_startoff - got->br_startoff;
4974 new.br_startoff = del_endoff;
4975 new.br_blockcount = got_endoff - del_endoff;
4976 new.br_state = got->br_state;
4977 new.br_startblock = del->br_startblock + del->br_blockcount;
4979 xfs_iext_update_extent(ip, state, icur, got);
4980 xfs_iext_next(ifp, icur);
4981 xfs_iext_insert(ip, icur, &new, state);
4984 ip->i_delayed_blks -= del->br_blockcount;
4988 * Called by xfs_bmapi to update file extent records and the btree
4989 * after removing space.
4991 STATIC int /* error */
4992 xfs_bmap_del_extent_real(
4993 xfs_inode_t *ip, /* incore inode pointer */
4994 xfs_trans_t *tp, /* current transaction pointer */
4995 struct xfs_iext_cursor *icur,
4996 xfs_btree_cur_t *cur, /* if null, not a btree */
4997 xfs_bmbt_irec_t *del, /* data to remove from extents */
4998 int *logflagsp, /* inode logging flags */
4999 int whichfork, /* data or attr fork */
5000 int bflags) /* bmapi flags */
5002 xfs_fsblock_t del_endblock=0; /* first block past del */
5003 xfs_fileoff_t del_endoff; /* first offset past del */
5004 int do_fx; /* free extent at end of routine */
5005 int error; /* error return value */
5006 int flags = 0;/* inode logging flags */
5007 struct xfs_bmbt_irec got; /* current extent entry */
5008 xfs_fileoff_t got_endoff; /* first offset past got */
5009 int i; /* temp state */
5010 struct xfs_ifork *ifp; /* inode fork pointer */
5011 xfs_mount_t *mp; /* mount structure */
5012 xfs_filblks_t nblks; /* quota/sb block count */
5013 xfs_bmbt_irec_t new; /* new record to be inserted */
5015 uint qfield; /* quota field to update */
5016 int state = xfs_bmap_fork_to_state(whichfork);
5017 struct xfs_bmbt_irec old;
5020 XFS_STATS_INC(mp, xs_del_exlist);
5022 ifp = XFS_IFORK_PTR(ip, whichfork);
5023 ASSERT(del->br_blockcount > 0);
5024 xfs_iext_get_extent(ifp, icur, &got);
5025 ASSERT(got.br_startoff <= del->br_startoff);
5026 del_endoff = del->br_startoff + del->br_blockcount;
5027 got_endoff = got.br_startoff + got.br_blockcount;
5028 ASSERT(got_endoff >= del_endoff);
5029 ASSERT(!isnullstartblock(got.br_startblock));
5034 * If it's the case where the directory code is running with no block
5035 * reservation, and the deleted block is in the middle of its extent,
5036 * and the resulting insert of an extent would cause transformation to
5037 * btree format, then reject it. The calling code will then swap blocks
5038 * around instead. We have to do this now, rather than waiting for the
5039 * conversion to btree format, since the transaction will be dirty then.
5041 if (tp->t_blk_res == 0 &&
5042 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5043 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5044 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5047 flags = XFS_ILOG_CORE;
5048 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5052 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5056 if (!(bflags & XFS_BMAPI_REMAP)) {
5059 bno = div_u64_rem(del->br_startblock,
5060 mp->m_sb.sb_rextsize, &mod);
5063 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5069 nblks = len * mp->m_sb.sb_rextsize;
5070 qfield = XFS_TRANS_DQ_RTBCOUNT;
5073 nblks = del->br_blockcount;
5074 qfield = XFS_TRANS_DQ_BCOUNT;
5077 del_endblock = del->br_startblock + del->br_blockcount;
5079 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5082 if (XFS_IS_CORRUPT(mp, i != 1)) {
5083 error = -EFSCORRUPTED;
5088 if (got.br_startoff == del->br_startoff)
5089 state |= BMAP_LEFT_FILLING;
5090 if (got_endoff == del_endoff)
5091 state |= BMAP_RIGHT_FILLING;
5093 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5094 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5096 * Matches the whole extent. Delete the entry.
5098 xfs_iext_remove(ip, icur, state);
5099 xfs_iext_prev(ifp, icur);
5102 flags |= XFS_ILOG_CORE;
5104 flags |= xfs_ilog_fext(whichfork);
5107 if ((error = xfs_btree_delete(cur, &i)))
5109 if (XFS_IS_CORRUPT(mp, i != 1)) {
5110 error = -EFSCORRUPTED;
5114 case BMAP_LEFT_FILLING:
5116 * Deleting the first part of the extent.
5118 got.br_startoff = del_endoff;
5119 got.br_startblock = del_endblock;
5120 got.br_blockcount -= del->br_blockcount;
5121 xfs_iext_update_extent(ip, state, icur, &got);
5123 flags |= xfs_ilog_fext(whichfork);
5126 error = xfs_bmbt_update(cur, &got);
5130 case BMAP_RIGHT_FILLING:
5132 * Deleting the last part of the extent.
5134 got.br_blockcount -= del->br_blockcount;
5135 xfs_iext_update_extent(ip, state, icur, &got);
5137 flags |= xfs_ilog_fext(whichfork);
5140 error = xfs_bmbt_update(cur, &got);
5146 * Deleting the middle of the extent.
5150 got.br_blockcount = del->br_startoff - got.br_startoff;
5151 xfs_iext_update_extent(ip, state, icur, &got);
5153 new.br_startoff = del_endoff;
5154 new.br_blockcount = got_endoff - del_endoff;
5155 new.br_state = got.br_state;
5156 new.br_startblock = del_endblock;
5158 flags |= XFS_ILOG_CORE;
5160 error = xfs_bmbt_update(cur, &got);
5163 error = xfs_btree_increment(cur, 0, &i);
5166 cur->bc_rec.b = new;
5167 error = xfs_btree_insert(cur, &i);
5168 if (error && error != -ENOSPC)
5171 * If get no-space back from btree insert, it tried a
5172 * split, and we have a zero block reservation. Fix up
5173 * our state and return the error.
5175 if (error == -ENOSPC) {
5177 * Reset the cursor, don't trust it after any
5180 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5183 if (XFS_IS_CORRUPT(mp, i != 1)) {
5184 error = -EFSCORRUPTED;
5188 * Update the btree record back
5189 * to the original value.
5191 error = xfs_bmbt_update(cur, &old);
5195 * Reset the extent record back
5196 * to the original value.
5198 xfs_iext_update_extent(ip, state, icur, &old);
5203 if (XFS_IS_CORRUPT(mp, i != 1)) {
5204 error = -EFSCORRUPTED;
5208 flags |= xfs_ilog_fext(whichfork);
5211 xfs_iext_next(ifp, icur);
5212 xfs_iext_insert(ip, icur, &new, state);
5216 /* remove reverse mapping */
5217 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5220 * If we need to, add to list of extents to delete.
5222 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5223 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5224 xfs_refcount_decrease_extent(tp, del);
5226 __xfs_bmap_add_free(tp, del->br_startblock,
5227 del->br_blockcount, NULL,
5228 (bflags & XFS_BMAPI_NODISCARD) ||
5229 del->br_state == XFS_EXT_UNWRITTEN);
5234 * Adjust inode # blocks in the file.
5237 ip->i_d.di_nblocks -= nblks;
5239 * Adjust quota data.
5241 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5242 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5250 * Unmap (remove) blocks from a file.
5251 * If nexts is nonzero then the number of extents to remove is limited to
5252 * that value. If not all extents in the block range can be removed then
5257 struct xfs_trans *tp, /* transaction pointer */
5258 struct xfs_inode *ip, /* incore inode */
5259 xfs_fileoff_t start, /* first file offset deleted */
5260 xfs_filblks_t *rlen, /* i/o: amount remaining */
5261 int flags, /* misc flags */
5262 xfs_extnum_t nexts) /* number of extents max */
5264 struct xfs_btree_cur *cur; /* bmap btree cursor */
5265 struct xfs_bmbt_irec del; /* extent being deleted */
5266 int error; /* error return value */
5267 xfs_extnum_t extno; /* extent number in list */
5268 struct xfs_bmbt_irec got; /* current extent record */
5269 struct xfs_ifork *ifp; /* inode fork pointer */
5270 int isrt; /* freeing in rt area */
5271 int logflags; /* transaction logging flags */
5272 xfs_extlen_t mod; /* rt extent offset */
5273 struct xfs_mount *mp = ip->i_mount;
5274 int tmp_logflags; /* partial logging flags */
5275 int wasdel; /* was a delayed alloc extent */
5276 int whichfork; /* data or attribute fork */
5278 xfs_filblks_t len = *rlen; /* length to unmap in file */
5279 xfs_fileoff_t max_len;
5280 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5282 struct xfs_iext_cursor icur;
5285 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5287 whichfork = xfs_bmapi_whichfork(flags);
5288 ASSERT(whichfork != XFS_COW_FORK);
5289 ifp = XFS_IFORK_PTR(ip, whichfork);
5290 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
5291 return -EFSCORRUPTED;
5292 if (XFS_FORCED_SHUTDOWN(mp))
5295 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5300 * Guesstimate how many blocks we can unmap without running the risk of
5301 * blowing out the transaction with a mix of EFIs and reflink
5304 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5305 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5309 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5310 (error = xfs_iread_extents(tp, ip, whichfork)))
5312 if (xfs_iext_count(ifp) == 0) {
5316 XFS_STATS_INC(mp, xs_blk_unmap);
5317 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5320 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5327 if (ifp->if_flags & XFS_IFBROOT) {
5328 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5329 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5330 cur->bc_ino.flags = 0;
5336 * Synchronize by locking the bitmap inode.
5338 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5339 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5340 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5341 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5345 while (end != (xfs_fileoff_t)-1 && end >= start &&
5346 (nexts == 0 || extno < nexts) && max_len > 0) {
5348 * Is the found extent after a hole in which end lives?
5349 * Just back up to the previous extent, if so.
5351 if (got.br_startoff > end &&
5352 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5357 * Is the last block of this extent before the range
5358 * we're supposed to delete? If so, we're done.
5360 end = XFS_FILEOFF_MIN(end,
5361 got.br_startoff + got.br_blockcount - 1);
5365 * Then deal with the (possibly delayed) allocated space
5369 wasdel = isnullstartblock(del.br_startblock);
5372 * Make sure we don't touch multiple AGF headers out of order
5373 * in a single transaction, as that could cause AB-BA deadlocks.
5375 if (!wasdel && !isrt) {
5376 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5377 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5381 if (got.br_startoff < start) {
5382 del.br_startoff = start;
5383 del.br_blockcount -= start - got.br_startoff;
5385 del.br_startblock += start - got.br_startoff;
5387 if (del.br_startoff + del.br_blockcount > end + 1)
5388 del.br_blockcount = end + 1 - del.br_startoff;
5390 /* How much can we safely unmap? */
5391 if (max_len < del.br_blockcount) {
5392 del.br_startoff += del.br_blockcount - max_len;
5394 del.br_startblock += del.br_blockcount - max_len;
5395 del.br_blockcount = max_len;
5401 sum = del.br_startblock + del.br_blockcount;
5402 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5405 * Realtime extent not lined up at the end.
5406 * The extent could have been split into written
5407 * and unwritten pieces, or we could just be
5408 * unmapping part of it. But we can't really
5409 * get rid of part of a realtime extent.
5411 if (del.br_state == XFS_EXT_UNWRITTEN) {
5413 * This piece is unwritten, or we're not
5414 * using unwritten extents. Skip over it.
5417 end -= mod > del.br_blockcount ?
5418 del.br_blockcount : mod;
5419 if (end < got.br_startoff &&
5420 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5427 * It's written, turn it unwritten.
5428 * This is better than zeroing it.
5430 ASSERT(del.br_state == XFS_EXT_NORM);
5431 ASSERT(tp->t_blk_res > 0);
5433 * If this spans a realtime extent boundary,
5434 * chop it back to the start of the one we end at.
5436 if (del.br_blockcount > mod) {
5437 del.br_startoff += del.br_blockcount - mod;
5438 del.br_startblock += del.br_blockcount - mod;
5439 del.br_blockcount = mod;
5441 del.br_state = XFS_EXT_UNWRITTEN;
5442 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5443 whichfork, &icur, &cur, &del,
5449 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5451 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5454 * Realtime extent is lined up at the end but not
5455 * at the front. We'll get rid of full extents if
5458 if (del.br_blockcount > off) {
5459 del.br_blockcount -= off;
5460 del.br_startoff += off;
5461 del.br_startblock += off;
5462 } else if (del.br_startoff == start &&
5463 (del.br_state == XFS_EXT_UNWRITTEN ||
5464 tp->t_blk_res == 0)) {
5466 * Can't make it unwritten. There isn't
5467 * a full extent here so just skip it.
5469 ASSERT(end >= del.br_blockcount);
5470 end -= del.br_blockcount;
5471 if (got.br_startoff > end &&
5472 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5477 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5478 struct xfs_bmbt_irec prev;
5479 xfs_fileoff_t unwrite_start;
5482 * This one is already unwritten.
5483 * It must have a written left neighbor.
5484 * Unwrite the killed part of that one and
5487 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5489 ASSERT(prev.br_state == XFS_EXT_NORM);
5490 ASSERT(!isnullstartblock(prev.br_startblock));
5491 ASSERT(del.br_startblock ==
5492 prev.br_startblock + prev.br_blockcount);
5493 unwrite_start = max3(start,
5494 del.br_startoff - mod,
5496 mod = unwrite_start - prev.br_startoff;
5497 prev.br_startoff = unwrite_start;
5498 prev.br_startblock += mod;
5499 prev.br_blockcount -= mod;
5500 prev.br_state = XFS_EXT_UNWRITTEN;
5501 error = xfs_bmap_add_extent_unwritten_real(tp,
5502 ip, whichfork, &icur, &cur,
5508 ASSERT(del.br_state == XFS_EXT_NORM);
5509 del.br_state = XFS_EXT_UNWRITTEN;
5510 error = xfs_bmap_add_extent_unwritten_real(tp,
5511 ip, whichfork, &icur, &cur,
5521 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5524 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5525 &del, &tmp_logflags, whichfork,
5527 logflags |= tmp_logflags;
5533 max_len -= del.br_blockcount;
5534 end = del.br_startoff - 1;
5537 * If not done go on to the next (previous) record.
5539 if (end != (xfs_fileoff_t)-1 && end >= start) {
5540 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5541 (got.br_startoff > end &&
5542 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5549 if (done || end == (xfs_fileoff_t)-1 || end < start)
5552 *rlen = end - start + 1;
5555 * Convert to a btree if necessary.
5557 if (xfs_bmap_needs_btree(ip, whichfork)) {
5558 ASSERT(cur == NULL);
5559 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5560 &tmp_logflags, whichfork);
5561 logflags |= tmp_logflags;
5563 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5569 * Log everything. Do this after conversion, there's no point in
5570 * logging the extent records if we've converted to btree format.
5572 if ((logflags & xfs_ilog_fext(whichfork)) &&
5573 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5574 logflags &= ~xfs_ilog_fext(whichfork);
5575 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5576 ifp->if_format != XFS_DINODE_FMT_BTREE)
5577 logflags &= ~xfs_ilog_fbroot(whichfork);
5579 * Log inode even in the error case, if the transaction
5580 * is dirty we'll need to shut down the filesystem.
5583 xfs_trans_log_inode(tp, ip, logflags);
5586 cur->bc_ino.allocated = 0;
5587 xfs_btree_del_cursor(cur, error);
5592 /* Unmap a range of a file. */
5596 struct xfs_inode *ip,
5605 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5611 * Determine whether an extent shift can be accomplished by a merge with the
5612 * extent that precedes the target hole of the shift.
5616 struct xfs_bmbt_irec *left, /* preceding extent */
5617 struct xfs_bmbt_irec *got, /* current extent to shift */
5618 xfs_fileoff_t shift) /* shift fsb */
5620 xfs_fileoff_t startoff;
5622 startoff = got->br_startoff - shift;
5625 * The extent, once shifted, must be adjacent in-file and on-disk with
5626 * the preceding extent.
5628 if ((left->br_startoff + left->br_blockcount != startoff) ||
5629 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5630 (left->br_state != got->br_state) ||
5631 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5638 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5639 * hole in the file. If an extent shift would result in the extent being fully
5640 * adjacent to the extent that currently precedes the hole, we can merge with
5641 * the preceding extent rather than do the shift.
5643 * This function assumes the caller has verified a shift-by-merge is possible
5644 * with the provided extents via xfs_bmse_can_merge().
5648 struct xfs_trans *tp,
5649 struct xfs_inode *ip,
5651 xfs_fileoff_t shift, /* shift fsb */
5652 struct xfs_iext_cursor *icur,
5653 struct xfs_bmbt_irec *got, /* extent to shift */
5654 struct xfs_bmbt_irec *left, /* preceding extent */
5655 struct xfs_btree_cur *cur,
5656 int *logflags) /* output */
5658 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5659 struct xfs_bmbt_irec new;
5660 xfs_filblks_t blockcount;
5662 struct xfs_mount *mp = ip->i_mount;
5664 blockcount = left->br_blockcount + got->br_blockcount;
5666 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5667 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5668 ASSERT(xfs_bmse_can_merge(left, got, shift));
5671 new.br_blockcount = blockcount;
5674 * Update the on-disk extent count, the btree if necessary and log the
5678 *logflags |= XFS_ILOG_CORE;
5680 *logflags |= XFS_ILOG_DEXT;
5684 /* lookup and remove the extent to merge */
5685 error = xfs_bmbt_lookup_eq(cur, got, &i);
5688 if (XFS_IS_CORRUPT(mp, i != 1))
5689 return -EFSCORRUPTED;
5691 error = xfs_btree_delete(cur, &i);
5694 if (XFS_IS_CORRUPT(mp, i != 1))
5695 return -EFSCORRUPTED;
5697 /* lookup and update size of the previous extent */
5698 error = xfs_bmbt_lookup_eq(cur, left, &i);
5701 if (XFS_IS_CORRUPT(mp, i != 1))
5702 return -EFSCORRUPTED;
5704 error = xfs_bmbt_update(cur, &new);
5708 /* change to extent format if required after extent removal */
5709 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5714 xfs_iext_remove(ip, icur, 0);
5715 xfs_iext_prev(ifp, icur);
5716 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5719 /* update reverse mapping. rmap functions merge the rmaps for us */
5720 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5721 memcpy(&new, got, sizeof(new));
5722 new.br_startoff = left->br_startoff + left->br_blockcount;
5723 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5728 xfs_bmap_shift_update_extent(
5729 struct xfs_trans *tp,
5730 struct xfs_inode *ip,
5732 struct xfs_iext_cursor *icur,
5733 struct xfs_bmbt_irec *got,
5734 struct xfs_btree_cur *cur,
5736 xfs_fileoff_t startoff)
5738 struct xfs_mount *mp = ip->i_mount;
5739 struct xfs_bmbt_irec prev = *got;
5742 *logflags |= XFS_ILOG_CORE;
5744 got->br_startoff = startoff;
5747 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5750 if (XFS_IS_CORRUPT(mp, i != 1))
5751 return -EFSCORRUPTED;
5753 error = xfs_bmbt_update(cur, got);
5757 *logflags |= XFS_ILOG_DEXT;
5760 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5763 /* update reverse mapping */
5764 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5765 xfs_rmap_map_extent(tp, ip, whichfork, got);
5770 xfs_bmap_collapse_extents(
5771 struct xfs_trans *tp,
5772 struct xfs_inode *ip,
5773 xfs_fileoff_t *next_fsb,
5774 xfs_fileoff_t offset_shift_fsb,
5777 int whichfork = XFS_DATA_FORK;
5778 struct xfs_mount *mp = ip->i_mount;
5779 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5780 struct xfs_btree_cur *cur = NULL;
5781 struct xfs_bmbt_irec got, prev;
5782 struct xfs_iext_cursor icur;
5783 xfs_fileoff_t new_startoff;
5787 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5788 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5789 return -EFSCORRUPTED;
5792 if (XFS_FORCED_SHUTDOWN(mp))
5795 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5797 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5798 error = xfs_iread_extents(tp, ip, whichfork);
5803 if (ifp->if_flags & XFS_IFBROOT) {
5804 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5805 cur->bc_ino.flags = 0;
5808 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5812 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5813 error = -EFSCORRUPTED;
5817 new_startoff = got.br_startoff - offset_shift_fsb;
5818 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5819 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5824 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5825 error = xfs_bmse_merge(tp, ip, whichfork,
5826 offset_shift_fsb, &icur, &got, &prev,
5833 if (got.br_startoff < offset_shift_fsb) {
5839 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5840 cur, &logflags, new_startoff);
5845 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5850 *next_fsb = got.br_startoff;
5853 xfs_btree_del_cursor(cur, error);
5855 xfs_trans_log_inode(tp, ip, logflags);
5859 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5861 xfs_bmap_can_insert_extents(
5862 struct xfs_inode *ip,
5864 xfs_fileoff_t shift)
5866 struct xfs_bmbt_irec got;
5870 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5872 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5875 xfs_ilock(ip, XFS_ILOCK_EXCL);
5876 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5877 if (!error && !is_empty && got.br_startoff >= off &&
5878 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5880 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5886 xfs_bmap_insert_extents(
5887 struct xfs_trans *tp,
5888 struct xfs_inode *ip,
5889 xfs_fileoff_t *next_fsb,
5890 xfs_fileoff_t offset_shift_fsb,
5892 xfs_fileoff_t stop_fsb)
5894 int whichfork = XFS_DATA_FORK;
5895 struct xfs_mount *mp = ip->i_mount;
5896 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5897 struct xfs_btree_cur *cur = NULL;
5898 struct xfs_bmbt_irec got, next;
5899 struct xfs_iext_cursor icur;
5900 xfs_fileoff_t new_startoff;
5904 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5905 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5906 return -EFSCORRUPTED;
5909 if (XFS_FORCED_SHUTDOWN(mp))
5912 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5914 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5915 error = xfs_iread_extents(tp, ip, whichfork);
5920 if (ifp->if_flags & XFS_IFBROOT) {
5921 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5922 cur->bc_ino.flags = 0;
5925 if (*next_fsb == NULLFSBLOCK) {
5926 xfs_iext_last(ifp, &icur);
5927 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5928 stop_fsb > got.br_startoff) {
5933 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5938 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5939 error = -EFSCORRUPTED;
5943 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
5944 error = -EFSCORRUPTED;
5948 new_startoff = got.br_startoff + offset_shift_fsb;
5949 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5950 if (new_startoff + got.br_blockcount > next.br_startoff) {
5956 * Unlike a left shift (which involves a hole punch), a right
5957 * shift does not modify extent neighbors in any way. We should
5958 * never find mergeable extents in this scenario. Check anyways
5959 * and warn if we encounter two extents that could be one.
5961 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5965 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5966 cur, &logflags, new_startoff);
5970 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5971 stop_fsb >= got.br_startoff + got.br_blockcount) {
5976 *next_fsb = got.br_startoff;
5979 xfs_btree_del_cursor(cur, error);
5981 xfs_trans_log_inode(tp, ip, logflags);
5986 * Splits an extent into two extents at split_fsb block such that it is the
5987 * first block of the current_ext. @ext is a target extent to be split.
5988 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5989 * hole or the first block of extents, just return 0.
5992 xfs_bmap_split_extent(
5993 struct xfs_trans *tp,
5994 struct xfs_inode *ip,
5995 xfs_fileoff_t split_fsb)
5997 int whichfork = XFS_DATA_FORK;
5998 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5999 struct xfs_btree_cur *cur = NULL;
6000 struct xfs_bmbt_irec got;
6001 struct xfs_bmbt_irec new; /* split extent */
6002 struct xfs_mount *mp = ip->i_mount;
6003 xfs_fsblock_t gotblkcnt; /* new block count for got */
6004 struct xfs_iext_cursor icur;
6009 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6010 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6011 return -EFSCORRUPTED;
6014 if (XFS_FORCED_SHUTDOWN(mp))
6017 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6018 /* Read in all the extents */
6019 error = xfs_iread_extents(tp, ip, whichfork);
6025 * If there are not extents, or split_fsb lies in a hole we are done.
6027 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6028 got.br_startoff >= split_fsb)
6031 gotblkcnt = split_fsb - got.br_startoff;
6032 new.br_startoff = split_fsb;
6033 new.br_startblock = got.br_startblock + gotblkcnt;
6034 new.br_blockcount = got.br_blockcount - gotblkcnt;
6035 new.br_state = got.br_state;
6037 if (ifp->if_flags & XFS_IFBROOT) {
6038 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6039 cur->bc_ino.flags = 0;
6040 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6043 if (XFS_IS_CORRUPT(mp, i != 1)) {
6044 error = -EFSCORRUPTED;
6049 got.br_blockcount = gotblkcnt;
6050 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6053 logflags = XFS_ILOG_CORE;
6055 error = xfs_bmbt_update(cur, &got);
6059 logflags |= XFS_ILOG_DEXT;
6061 /* Add new extent */
6062 xfs_iext_next(ifp, &icur);
6063 xfs_iext_insert(ip, &icur, &new, 0);
6067 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6070 if (XFS_IS_CORRUPT(mp, i != 0)) {
6071 error = -EFSCORRUPTED;
6074 error = xfs_btree_insert(cur, &i);
6077 if (XFS_IS_CORRUPT(mp, i != 1)) {
6078 error = -EFSCORRUPTED;
6084 * Convert to a btree if necessary.
6086 if (xfs_bmap_needs_btree(ip, whichfork)) {
6087 int tmp_logflags; /* partial log flag return val */
6089 ASSERT(cur == NULL);
6090 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6091 &tmp_logflags, whichfork);
6092 logflags |= tmp_logflags;
6097 cur->bc_ino.allocated = 0;
6098 xfs_btree_del_cursor(cur, error);
6102 xfs_trans_log_inode(tp, ip, logflags);
6106 /* Deferred mapping is only for real extents in the data fork. */
6108 xfs_bmap_is_update_needed(
6109 struct xfs_bmbt_irec *bmap)
6111 return bmap->br_startblock != HOLESTARTBLOCK &&
6112 bmap->br_startblock != DELAYSTARTBLOCK;
6115 /* Record a bmap intent. */
6118 struct xfs_trans *tp,
6119 enum xfs_bmap_intent_type type,
6120 struct xfs_inode *ip,
6122 struct xfs_bmbt_irec *bmap)
6124 struct xfs_bmap_intent *bi;
6126 trace_xfs_bmap_defer(tp->t_mountp,
6127 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6129 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6130 ip->i_ino, whichfork,
6132 bmap->br_blockcount,
6135 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
6136 INIT_LIST_HEAD(&bi->bi_list);
6139 bi->bi_whichfork = whichfork;
6140 bi->bi_bmap = *bmap;
6142 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6146 /* Map an extent into a file. */
6148 xfs_bmap_map_extent(
6149 struct xfs_trans *tp,
6150 struct xfs_inode *ip,
6151 struct xfs_bmbt_irec *PREV)
6153 if (!xfs_bmap_is_update_needed(PREV))
6156 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6159 /* Unmap an extent out of a file. */
6161 xfs_bmap_unmap_extent(
6162 struct xfs_trans *tp,
6163 struct xfs_inode *ip,
6164 struct xfs_bmbt_irec *PREV)
6166 if (!xfs_bmap_is_update_needed(PREV))
6169 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6173 * Process one of the deferred bmap operations. We pass back the
6174 * btree cursor to maintain our lock on the bmapbt between calls.
6177 xfs_bmap_finish_one(
6178 struct xfs_trans *tp,
6179 struct xfs_inode *ip,
6180 enum xfs_bmap_intent_type type,
6182 xfs_fileoff_t startoff,
6183 xfs_fsblock_t startblock,
6184 xfs_filblks_t *blockcount,
6189 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6191 trace_xfs_bmap_deferred(tp->t_mountp,
6192 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6193 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6194 ip->i_ino, whichfork, startoff, *blockcount, state);
6196 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6197 return -EFSCORRUPTED;
6199 if (XFS_TEST_ERROR(false, tp->t_mountp,
6200 XFS_ERRTAG_BMAP_FINISH_ONE))
6205 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6209 case XFS_BMAP_UNMAP:
6210 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6211 XFS_BMAPI_REMAP, 1);
6215 error = -EFSCORRUPTED;
6221 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6223 xfs_bmap_validate_extent(
6224 struct xfs_inode *ip,
6226 struct xfs_bmbt_irec *irec)
6228 struct xfs_mount *mp = ip->i_mount;
6229 xfs_fsblock_t endfsb;
6232 if (irec->br_startblock + irec->br_blockcount <= irec->br_startblock)
6233 return __this_address;
6234 if (irec->br_startoff + irec->br_blockcount <= irec->br_startoff)
6235 return __this_address;
6237 isrt = XFS_IS_REALTIME_INODE(ip);
6238 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6239 if (isrt && whichfork == XFS_DATA_FORK) {
6240 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6241 return __this_address;
6242 if (!xfs_verify_rtbno(mp, endfsb))
6243 return __this_address;
6245 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6246 return __this_address;
6247 if (!xfs_verify_fsbno(mp, endfsb))
6248 return __this_address;
6249 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6250 XFS_FSB_TO_AGNO(mp, endfsb))
6251 return __this_address;
6253 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6254 return __this_address;