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 struct xfs_buf *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 struct xfs_buf *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);
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++;
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 struct xfs_buf *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;
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;
1031 * Set an inode attr fork offset based on the format of the data fork.
1034 xfs_bmap_set_attrforkoff(
1035 struct xfs_inode *ip,
1039 switch (ip->i_df.if_format) {
1040 case XFS_DINODE_FMT_DEV:
1041 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1043 case XFS_DINODE_FMT_LOCAL:
1044 case XFS_DINODE_FMT_EXTENTS:
1045 case XFS_DINODE_FMT_BTREE:
1046 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1047 if (!ip->i_d.di_forkoff)
1048 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1049 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1061 * Convert inode from non-attributed to attributed.
1062 * Must not be in a transaction, ip must not be locked.
1064 int /* error code */
1065 xfs_bmap_add_attrfork(
1066 xfs_inode_t *ip, /* incore inode pointer */
1067 int size, /* space new attribute needs */
1068 int rsvd) /* xact may use reserved blks */
1070 xfs_mount_t *mp; /* mount structure */
1071 xfs_trans_t *tp; /* transaction pointer */
1072 int blks; /* space reservation */
1073 int version = 1; /* superblock attr version */
1074 int logflags; /* logging flags */
1075 int error; /* error return value */
1077 ASSERT(XFS_IFORK_Q(ip) == 0);
1080 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1082 blks = XFS_ADDAFORK_SPACE_RES(mp);
1084 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
1088 if (XFS_IFORK_Q(ip))
1091 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1092 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1095 ASSERT(ip->i_afp == NULL);
1097 ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
1098 ip->i_afp->if_flags = XFS_IFEXTENTS;
1100 switch (ip->i_df.if_format) {
1101 case XFS_DINODE_FMT_LOCAL:
1102 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1104 case XFS_DINODE_FMT_EXTENTS:
1105 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1107 case XFS_DINODE_FMT_BTREE:
1108 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1115 xfs_trans_log_inode(tp, ip, logflags);
1118 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1119 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1120 bool log_sb = false;
1122 spin_lock(&mp->m_sb_lock);
1123 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1124 xfs_sb_version_addattr(&mp->m_sb);
1127 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1128 xfs_sb_version_addattr2(&mp->m_sb);
1131 spin_unlock(&mp->m_sb_lock);
1136 error = xfs_trans_commit(tp);
1137 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1141 xfs_trans_cancel(tp);
1142 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1147 * Internal and external extent tree search functions.
1150 struct xfs_iread_state {
1151 struct xfs_iext_cursor icur;
1152 xfs_extnum_t loaded;
1155 /* Stuff every bmbt record from this block into the incore extent map. */
1157 xfs_iread_bmbt_block(
1158 struct xfs_btree_cur *cur,
1162 struct xfs_iread_state *ir = priv;
1163 struct xfs_mount *mp = cur->bc_mp;
1164 struct xfs_inode *ip = cur->bc_ino.ip;
1165 struct xfs_btree_block *block;
1167 struct xfs_bmbt_rec *frp;
1168 xfs_extnum_t num_recs;
1170 int whichfork = cur->bc_ino.whichfork;
1171 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1173 block = xfs_btree_get_block(cur, level, &bp);
1175 /* Abort if we find more records than nextents. */
1176 num_recs = xfs_btree_get_numrecs(block);
1177 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1178 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1179 (unsigned long long)ip->i_ino);
1180 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1181 sizeof(*block), __this_address);
1182 return -EFSCORRUPTED;
1185 /* Copy records into the incore cache. */
1186 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1187 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1188 struct xfs_bmbt_irec new;
1191 xfs_bmbt_disk_get_all(frp, &new);
1192 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1194 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1195 "xfs_iread_extents(2)", frp,
1197 return -EFSCORRUPTED;
1199 xfs_iext_insert(ip, &ir->icur, &new,
1200 xfs_bmap_fork_to_state(whichfork));
1201 trace_xfs_read_extent(ip, &ir->icur,
1202 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1203 xfs_iext_next(ifp, &ir->icur);
1210 * Read in extents from a btree-format inode.
1214 struct xfs_trans *tp,
1215 struct xfs_inode *ip,
1218 struct xfs_iread_state ir;
1219 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1220 struct xfs_mount *mp = ip->i_mount;
1221 struct xfs_btree_cur *cur;
1224 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1226 if (XFS_IS_CORRUPT(mp, ifp->if_format != XFS_DINODE_FMT_BTREE)) {
1227 error = -EFSCORRUPTED;
1232 xfs_iext_first(ifp, &ir.icur);
1233 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1234 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1235 XFS_BTREE_VISIT_RECORDS, &ir);
1236 xfs_btree_del_cursor(cur, error);
1240 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1241 error = -EFSCORRUPTED;
1244 ASSERT(ir.loaded == xfs_iext_count(ifp));
1246 ifp->if_flags |= XFS_IFEXTENTS;
1249 xfs_iext_destroy(ifp);
1254 * Returns the relative block number of the first unused block(s) in the given
1255 * fork with at least "len" logically contiguous blocks free. This is the
1256 * lowest-address hole if the fork has holes, else the first block past the end
1257 * of fork. Return 0 if the fork is currently local (in-inode).
1260 xfs_bmap_first_unused(
1261 struct xfs_trans *tp, /* transaction pointer */
1262 struct xfs_inode *ip, /* incore inode */
1263 xfs_extlen_t len, /* size of hole to find */
1264 xfs_fileoff_t *first_unused, /* unused block */
1265 int whichfork) /* data or attr fork */
1267 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1268 struct xfs_bmbt_irec got;
1269 struct xfs_iext_cursor icur;
1270 xfs_fileoff_t lastaddr = 0;
1271 xfs_fileoff_t lowest, max;
1274 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1279 ASSERT(xfs_ifork_has_extents(ifp));
1281 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1282 error = xfs_iread_extents(tp, ip, whichfork);
1287 lowest = max = *first_unused;
1288 for_each_xfs_iext(ifp, &icur, &got) {
1290 * See if the hole before this extent will work.
1292 if (got.br_startoff >= lowest + len &&
1293 got.br_startoff - max >= len)
1295 lastaddr = got.br_startoff + got.br_blockcount;
1296 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1299 *first_unused = max;
1304 * Returns the file-relative block number of the last block - 1 before
1305 * last_block (input value) in the file.
1306 * This is not based on i_size, it is based on the extent records.
1307 * Returns 0 for local files, as they do not have extent records.
1310 xfs_bmap_last_before(
1311 struct xfs_trans *tp, /* transaction pointer */
1312 struct xfs_inode *ip, /* incore inode */
1313 xfs_fileoff_t *last_block, /* last block */
1314 int whichfork) /* data or attr fork */
1316 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1317 struct xfs_bmbt_irec got;
1318 struct xfs_iext_cursor icur;
1321 switch (ifp->if_format) {
1322 case XFS_DINODE_FMT_LOCAL:
1325 case XFS_DINODE_FMT_BTREE:
1326 case XFS_DINODE_FMT_EXTENTS:
1330 return -EFSCORRUPTED;
1333 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1334 error = xfs_iread_extents(tp, ip, whichfork);
1339 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1345 xfs_bmap_last_extent(
1346 struct xfs_trans *tp,
1347 struct xfs_inode *ip,
1349 struct xfs_bmbt_irec *rec,
1352 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1353 struct xfs_iext_cursor icur;
1356 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1357 error = xfs_iread_extents(tp, ip, whichfork);
1362 xfs_iext_last(ifp, &icur);
1363 if (!xfs_iext_get_extent(ifp, &icur, rec))
1371 * Check the last inode extent to determine whether this allocation will result
1372 * in blocks being allocated at the end of the file. When we allocate new data
1373 * blocks at the end of the file which do not start at the previous data block,
1374 * we will try to align the new blocks at stripe unit boundaries.
1376 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1377 * at, or past the EOF.
1381 struct xfs_bmalloca *bma,
1384 struct xfs_bmbt_irec rec;
1389 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1400 * Check if we are allocation or past the last extent, or at least into
1401 * the last delayed allocated extent.
1403 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1404 (bma->offset >= rec.br_startoff &&
1405 isnullstartblock(rec.br_startblock));
1410 * Returns the file-relative block number of the first block past eof in
1411 * the file. This is not based on i_size, it is based on the extent records.
1412 * Returns 0 for local files, as they do not have extent records.
1415 xfs_bmap_last_offset(
1416 struct xfs_inode *ip,
1417 xfs_fileoff_t *last_block,
1420 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1421 struct xfs_bmbt_irec rec;
1427 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1430 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
1431 return -EFSCORRUPTED;
1433 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1434 if (error || is_empty)
1437 *last_block = rec.br_startoff + rec.br_blockcount;
1442 * Returns whether the selected fork of the inode has exactly one
1443 * block or not. For the data fork we check this matches i_disk_size,
1444 * implying the file's range is 0..bsize-1.
1446 int /* 1=>1 block, 0=>otherwise */
1448 struct xfs_inode *ip, /* incore inode */
1449 int whichfork) /* data or attr fork */
1451 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1452 int rval; /* return value */
1453 struct xfs_bmbt_irec s; /* internal version of extent */
1454 struct xfs_iext_cursor icur;
1457 if (whichfork == XFS_DATA_FORK)
1458 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1460 if (ifp->if_nextents != 1)
1462 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS)
1464 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1465 xfs_iext_first(ifp, &icur);
1466 xfs_iext_get_extent(ifp, &icur, &s);
1467 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1468 if (rval && whichfork == XFS_DATA_FORK)
1469 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1474 * Extent tree manipulation functions used during allocation.
1478 * Convert a delayed allocation to a real allocation.
1480 STATIC int /* error */
1481 xfs_bmap_add_extent_delay_real(
1482 struct xfs_bmalloca *bma,
1485 struct xfs_mount *mp = bma->ip->i_mount;
1486 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1487 struct xfs_bmbt_irec *new = &bma->got;
1488 int error; /* error return value */
1489 int i; /* temp state */
1490 xfs_fileoff_t new_endoff; /* end offset of new entry */
1491 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1492 /* left is 0, right is 1, prev is 2 */
1493 int rval=0; /* return value (logging flags) */
1494 int state = xfs_bmap_fork_to_state(whichfork);
1495 xfs_filblks_t da_new; /* new count del alloc blocks used */
1496 xfs_filblks_t da_old; /* old count del alloc blocks used */
1497 xfs_filblks_t temp=0; /* value for da_new calculations */
1498 int tmp_rval; /* partial logging flags */
1499 struct xfs_bmbt_irec old;
1501 ASSERT(whichfork != XFS_ATTR_FORK);
1502 ASSERT(!isnullstartblock(new->br_startblock));
1504 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1506 XFS_STATS_INC(mp, xs_add_exlist);
1513 * Set up a bunch of variables to make the tests simpler.
1515 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1516 new_endoff = new->br_startoff + new->br_blockcount;
1517 ASSERT(isnullstartblock(PREV.br_startblock));
1518 ASSERT(PREV.br_startoff <= new->br_startoff);
1519 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1521 da_old = startblockval(PREV.br_startblock);
1525 * Set flags determining what part of the previous delayed allocation
1526 * extent is being replaced by a real allocation.
1528 if (PREV.br_startoff == new->br_startoff)
1529 state |= BMAP_LEFT_FILLING;
1530 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1531 state |= BMAP_RIGHT_FILLING;
1534 * Check and set flags if this segment has a left neighbor.
1535 * Don't set contiguous if the combined extent would be too large.
1537 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1538 state |= BMAP_LEFT_VALID;
1539 if (isnullstartblock(LEFT.br_startblock))
1540 state |= BMAP_LEFT_DELAY;
1543 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1544 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1545 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1546 LEFT.br_state == new->br_state &&
1547 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1548 state |= BMAP_LEFT_CONTIG;
1551 * Check and set flags if this segment has a right neighbor.
1552 * Don't set contiguous if the combined extent would be too large.
1553 * Also check for all-three-contiguous being too large.
1555 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1556 state |= BMAP_RIGHT_VALID;
1557 if (isnullstartblock(RIGHT.br_startblock))
1558 state |= BMAP_RIGHT_DELAY;
1561 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1562 new_endoff == RIGHT.br_startoff &&
1563 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1564 new->br_state == RIGHT.br_state &&
1565 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1566 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1567 BMAP_RIGHT_FILLING)) !=
1568 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1569 BMAP_RIGHT_FILLING) ||
1570 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1572 state |= BMAP_RIGHT_CONTIG;
1576 * Switch out based on the FILLING and CONTIG state bits.
1578 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1579 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1580 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1581 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1583 * Filling in all of a previously delayed allocation extent.
1584 * The left and right neighbors are both contiguous with new.
1586 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1588 xfs_iext_remove(bma->ip, &bma->icur, state);
1589 xfs_iext_remove(bma->ip, &bma->icur, state);
1590 xfs_iext_prev(ifp, &bma->icur);
1591 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1594 if (bma->cur == NULL)
1595 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1597 rval = XFS_ILOG_CORE;
1598 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1601 if (XFS_IS_CORRUPT(mp, i != 1)) {
1602 error = -EFSCORRUPTED;
1605 error = xfs_btree_delete(bma->cur, &i);
1608 if (XFS_IS_CORRUPT(mp, i != 1)) {
1609 error = -EFSCORRUPTED;
1612 error = xfs_btree_decrement(bma->cur, 0, &i);
1615 if (XFS_IS_CORRUPT(mp, i != 1)) {
1616 error = -EFSCORRUPTED;
1619 error = xfs_bmbt_update(bma->cur, &LEFT);
1625 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1627 * Filling in all of a previously delayed allocation extent.
1628 * The left neighbor is contiguous, the right is not.
1631 LEFT.br_blockcount += PREV.br_blockcount;
1633 xfs_iext_remove(bma->ip, &bma->icur, state);
1634 xfs_iext_prev(ifp, &bma->icur);
1635 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1637 if (bma->cur == NULL)
1638 rval = XFS_ILOG_DEXT;
1641 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1644 if (XFS_IS_CORRUPT(mp, i != 1)) {
1645 error = -EFSCORRUPTED;
1648 error = xfs_bmbt_update(bma->cur, &LEFT);
1654 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1656 * Filling in all of a previously delayed allocation extent.
1657 * The right neighbor is contiguous, the left is not. Take care
1658 * with delay -> unwritten extent allocation here because the
1659 * delalloc record we are overwriting is always written.
1661 PREV.br_startblock = new->br_startblock;
1662 PREV.br_blockcount += RIGHT.br_blockcount;
1663 PREV.br_state = new->br_state;
1665 xfs_iext_next(ifp, &bma->icur);
1666 xfs_iext_remove(bma->ip, &bma->icur, state);
1667 xfs_iext_prev(ifp, &bma->icur);
1668 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1670 if (bma->cur == NULL)
1671 rval = XFS_ILOG_DEXT;
1674 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1677 if (XFS_IS_CORRUPT(mp, i != 1)) {
1678 error = -EFSCORRUPTED;
1681 error = xfs_bmbt_update(bma->cur, &PREV);
1687 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1689 * Filling in all of a previously delayed allocation extent.
1690 * Neither the left nor right neighbors are contiguous with
1693 PREV.br_startblock = new->br_startblock;
1694 PREV.br_state = new->br_state;
1695 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1698 if (bma->cur == NULL)
1699 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1701 rval = XFS_ILOG_CORE;
1702 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1705 if (XFS_IS_CORRUPT(mp, i != 0)) {
1706 error = -EFSCORRUPTED;
1709 error = xfs_btree_insert(bma->cur, &i);
1712 if (XFS_IS_CORRUPT(mp, i != 1)) {
1713 error = -EFSCORRUPTED;
1719 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1721 * Filling in the first part of a previous delayed allocation.
1722 * The left neighbor is contiguous.
1725 temp = PREV.br_blockcount - new->br_blockcount;
1726 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1727 startblockval(PREV.br_startblock));
1729 LEFT.br_blockcount += new->br_blockcount;
1731 PREV.br_blockcount = temp;
1732 PREV.br_startoff += new->br_blockcount;
1733 PREV.br_startblock = nullstartblock(da_new);
1735 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1736 xfs_iext_prev(ifp, &bma->icur);
1737 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1739 if (bma->cur == NULL)
1740 rval = XFS_ILOG_DEXT;
1743 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1746 if (XFS_IS_CORRUPT(mp, i != 1)) {
1747 error = -EFSCORRUPTED;
1750 error = xfs_bmbt_update(bma->cur, &LEFT);
1756 case BMAP_LEFT_FILLING:
1758 * Filling in the first part of a previous delayed allocation.
1759 * The left neighbor is not contiguous.
1761 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1764 if (bma->cur == NULL)
1765 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1767 rval = XFS_ILOG_CORE;
1768 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1771 if (XFS_IS_CORRUPT(mp, i != 0)) {
1772 error = -EFSCORRUPTED;
1775 error = xfs_btree_insert(bma->cur, &i);
1778 if (XFS_IS_CORRUPT(mp, i != 1)) {
1779 error = -EFSCORRUPTED;
1784 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1785 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1786 &bma->cur, 1, &tmp_rval, whichfork);
1792 temp = PREV.br_blockcount - new->br_blockcount;
1793 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1794 startblockval(PREV.br_startblock) -
1795 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1797 PREV.br_startoff = new_endoff;
1798 PREV.br_blockcount = temp;
1799 PREV.br_startblock = nullstartblock(da_new);
1800 xfs_iext_next(ifp, &bma->icur);
1801 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1802 xfs_iext_prev(ifp, &bma->icur);
1805 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1807 * Filling in the last part of a previous delayed allocation.
1808 * The right neighbor is contiguous with the new allocation.
1811 RIGHT.br_startoff = new->br_startoff;
1812 RIGHT.br_startblock = new->br_startblock;
1813 RIGHT.br_blockcount += new->br_blockcount;
1815 if (bma->cur == NULL)
1816 rval = XFS_ILOG_DEXT;
1819 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1822 if (XFS_IS_CORRUPT(mp, i != 1)) {
1823 error = -EFSCORRUPTED;
1826 error = xfs_bmbt_update(bma->cur, &RIGHT);
1831 temp = PREV.br_blockcount - new->br_blockcount;
1832 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1833 startblockval(PREV.br_startblock));
1835 PREV.br_blockcount = temp;
1836 PREV.br_startblock = nullstartblock(da_new);
1838 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1839 xfs_iext_next(ifp, &bma->icur);
1840 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1843 case BMAP_RIGHT_FILLING:
1845 * Filling in the last part of a previous delayed allocation.
1846 * The right neighbor is not contiguous.
1848 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1851 if (bma->cur == NULL)
1852 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1854 rval = XFS_ILOG_CORE;
1855 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1858 if (XFS_IS_CORRUPT(mp, i != 0)) {
1859 error = -EFSCORRUPTED;
1862 error = xfs_btree_insert(bma->cur, &i);
1865 if (XFS_IS_CORRUPT(mp, i != 1)) {
1866 error = -EFSCORRUPTED;
1871 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1872 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1873 &bma->cur, 1, &tmp_rval, whichfork);
1879 temp = PREV.br_blockcount - new->br_blockcount;
1880 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1881 startblockval(PREV.br_startblock) -
1882 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1884 PREV.br_startblock = nullstartblock(da_new);
1885 PREV.br_blockcount = temp;
1886 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1887 xfs_iext_next(ifp, &bma->icur);
1892 * Filling in the middle part of a previous delayed allocation.
1893 * Contiguity is impossible here.
1894 * This case is avoided almost all the time.
1896 * We start with a delayed allocation:
1898 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1901 * and we are allocating:
1902 * +rrrrrrrrrrrrrrrrr+
1905 * and we set it up for insertion as:
1906 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1908 * PREV @ idx LEFT RIGHT
1909 * inserted at idx + 1
1913 /* LEFT is the new middle */
1916 /* RIGHT is the new right */
1917 RIGHT.br_state = PREV.br_state;
1918 RIGHT.br_startoff = new_endoff;
1919 RIGHT.br_blockcount =
1920 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1921 RIGHT.br_startblock =
1922 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1923 RIGHT.br_blockcount));
1926 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1927 PREV.br_startblock =
1928 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1929 PREV.br_blockcount));
1930 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1932 xfs_iext_next(ifp, &bma->icur);
1933 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1934 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1937 if (bma->cur == NULL)
1938 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1940 rval = XFS_ILOG_CORE;
1941 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1944 if (XFS_IS_CORRUPT(mp, i != 0)) {
1945 error = -EFSCORRUPTED;
1948 error = xfs_btree_insert(bma->cur, &i);
1951 if (XFS_IS_CORRUPT(mp, i != 1)) {
1952 error = -EFSCORRUPTED;
1957 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1958 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1959 &bma->cur, 1, &tmp_rval, whichfork);
1965 da_new = startblockval(PREV.br_startblock) +
1966 startblockval(RIGHT.br_startblock);
1969 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1970 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1971 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1972 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1973 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1974 case BMAP_LEFT_CONTIG:
1975 case BMAP_RIGHT_CONTIG:
1977 * These cases are all impossible.
1982 /* add reverse mapping unless caller opted out */
1983 if (!(bma->flags & XFS_BMAPI_NORMAP))
1984 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1986 /* convert to a btree if necessary */
1987 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1988 int tmp_logflags; /* partial log flag return val */
1990 ASSERT(bma->cur == NULL);
1991 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1992 &bma->cur, da_old > 0, &tmp_logflags,
1994 bma->logflags |= tmp_logflags;
1999 if (da_new != da_old)
2000 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2003 da_new += bma->cur->bc_ino.allocated;
2004 bma->cur->bc_ino.allocated = 0;
2007 /* adjust for changes in reserved delayed indirect blocks */
2008 if (da_new != da_old) {
2009 ASSERT(state == 0 || da_new < da_old);
2010 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2014 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2016 if (whichfork != XFS_COW_FORK)
2017 bma->logflags |= rval;
2025 * Convert an unwritten allocation to a real allocation or vice versa.
2028 xfs_bmap_add_extent_unwritten_real(
2029 struct xfs_trans *tp,
2030 xfs_inode_t *ip, /* incore inode pointer */
2032 struct xfs_iext_cursor *icur,
2033 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2034 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2035 int *logflagsp) /* inode logging flags */
2037 xfs_btree_cur_t *cur; /* btree cursor */
2038 int error; /* error return value */
2039 int i; /* temp state */
2040 struct xfs_ifork *ifp; /* inode fork pointer */
2041 xfs_fileoff_t new_endoff; /* end offset of new entry */
2042 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2043 /* left is 0, right is 1, prev is 2 */
2044 int rval=0; /* return value (logging flags) */
2045 int state = xfs_bmap_fork_to_state(whichfork);
2046 struct xfs_mount *mp = ip->i_mount;
2047 struct xfs_bmbt_irec old;
2052 ifp = XFS_IFORK_PTR(ip, whichfork);
2054 ASSERT(!isnullstartblock(new->br_startblock));
2056 XFS_STATS_INC(mp, xs_add_exlist);
2063 * Set up a bunch of variables to make the tests simpler.
2066 xfs_iext_get_extent(ifp, icur, &PREV);
2067 ASSERT(new->br_state != PREV.br_state);
2068 new_endoff = new->br_startoff + new->br_blockcount;
2069 ASSERT(PREV.br_startoff <= new->br_startoff);
2070 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2073 * Set flags determining what part of the previous oldext allocation
2074 * extent is being replaced by a newext allocation.
2076 if (PREV.br_startoff == new->br_startoff)
2077 state |= BMAP_LEFT_FILLING;
2078 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2079 state |= BMAP_RIGHT_FILLING;
2082 * Check and set flags if this segment has a left neighbor.
2083 * Don't set contiguous if the combined extent would be too large.
2085 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2086 state |= BMAP_LEFT_VALID;
2087 if (isnullstartblock(LEFT.br_startblock))
2088 state |= BMAP_LEFT_DELAY;
2091 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2092 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2093 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2094 LEFT.br_state == new->br_state &&
2095 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2096 state |= BMAP_LEFT_CONTIG;
2099 * Check and set flags if this segment has a right neighbor.
2100 * Don't set contiguous if the combined extent would be too large.
2101 * Also check for all-three-contiguous being too large.
2103 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2104 state |= BMAP_RIGHT_VALID;
2105 if (isnullstartblock(RIGHT.br_startblock))
2106 state |= BMAP_RIGHT_DELAY;
2109 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2110 new_endoff == RIGHT.br_startoff &&
2111 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2112 new->br_state == RIGHT.br_state &&
2113 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2114 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2115 BMAP_RIGHT_FILLING)) !=
2116 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2117 BMAP_RIGHT_FILLING) ||
2118 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2120 state |= BMAP_RIGHT_CONTIG;
2123 * Switch out based on the FILLING and CONTIG state bits.
2125 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2126 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2127 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2128 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2130 * Setting all of a previous oldext extent to newext.
2131 * The left and right neighbors are both contiguous with new.
2133 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2135 xfs_iext_remove(ip, icur, state);
2136 xfs_iext_remove(ip, icur, state);
2137 xfs_iext_prev(ifp, icur);
2138 xfs_iext_update_extent(ip, state, icur, &LEFT);
2139 ifp->if_nextents -= 2;
2141 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2143 rval = XFS_ILOG_CORE;
2144 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2147 if (XFS_IS_CORRUPT(mp, i != 1)) {
2148 error = -EFSCORRUPTED;
2151 if ((error = xfs_btree_delete(cur, &i)))
2153 if (XFS_IS_CORRUPT(mp, i != 1)) {
2154 error = -EFSCORRUPTED;
2157 if ((error = xfs_btree_decrement(cur, 0, &i)))
2159 if (XFS_IS_CORRUPT(mp, i != 1)) {
2160 error = -EFSCORRUPTED;
2163 if ((error = xfs_btree_delete(cur, &i)))
2165 if (XFS_IS_CORRUPT(mp, i != 1)) {
2166 error = -EFSCORRUPTED;
2169 if ((error = xfs_btree_decrement(cur, 0, &i)))
2171 if (XFS_IS_CORRUPT(mp, i != 1)) {
2172 error = -EFSCORRUPTED;
2175 error = xfs_bmbt_update(cur, &LEFT);
2181 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2183 * Setting all of a previous oldext extent to newext.
2184 * The left neighbor is contiguous, the right is not.
2186 LEFT.br_blockcount += PREV.br_blockcount;
2188 xfs_iext_remove(ip, icur, state);
2189 xfs_iext_prev(ifp, icur);
2190 xfs_iext_update_extent(ip, state, icur, &LEFT);
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2195 rval = XFS_ILOG_CORE;
2196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2199 if (XFS_IS_CORRUPT(mp, i != 1)) {
2200 error = -EFSCORRUPTED;
2203 if ((error = xfs_btree_delete(cur, &i)))
2205 if (XFS_IS_CORRUPT(mp, i != 1)) {
2206 error = -EFSCORRUPTED;
2209 if ((error = xfs_btree_decrement(cur, 0, &i)))
2211 if (XFS_IS_CORRUPT(mp, i != 1)) {
2212 error = -EFSCORRUPTED;
2215 error = xfs_bmbt_update(cur, &LEFT);
2221 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2223 * Setting all of a previous oldext extent to newext.
2224 * The right neighbor is contiguous, the left is not.
2226 PREV.br_blockcount += RIGHT.br_blockcount;
2227 PREV.br_state = new->br_state;
2229 xfs_iext_next(ifp, icur);
2230 xfs_iext_remove(ip, icur, state);
2231 xfs_iext_prev(ifp, icur);
2232 xfs_iext_update_extent(ip, state, icur, &PREV);
2236 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2238 rval = XFS_ILOG_CORE;
2239 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2242 if (XFS_IS_CORRUPT(mp, i != 1)) {
2243 error = -EFSCORRUPTED;
2246 if ((error = xfs_btree_delete(cur, &i)))
2248 if (XFS_IS_CORRUPT(mp, i != 1)) {
2249 error = -EFSCORRUPTED;
2252 if ((error = xfs_btree_decrement(cur, 0, &i)))
2254 if (XFS_IS_CORRUPT(mp, i != 1)) {
2255 error = -EFSCORRUPTED;
2258 error = xfs_bmbt_update(cur, &PREV);
2264 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2266 * Setting all of a previous oldext extent to newext.
2267 * Neither the left nor right neighbors are contiguous with
2270 PREV.br_state = new->br_state;
2271 xfs_iext_update_extent(ip, state, icur, &PREV);
2274 rval = XFS_ILOG_DEXT;
2277 error = xfs_bmbt_lookup_eq(cur, new, &i);
2280 if (XFS_IS_CORRUPT(mp, i != 1)) {
2281 error = -EFSCORRUPTED;
2284 error = xfs_bmbt_update(cur, &PREV);
2290 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2292 * Setting the first part of a previous oldext extent to newext.
2293 * The left neighbor is contiguous.
2295 LEFT.br_blockcount += new->br_blockcount;
2298 PREV.br_startoff += new->br_blockcount;
2299 PREV.br_startblock += new->br_blockcount;
2300 PREV.br_blockcount -= new->br_blockcount;
2302 xfs_iext_update_extent(ip, state, icur, &PREV);
2303 xfs_iext_prev(ifp, icur);
2304 xfs_iext_update_extent(ip, state, icur, &LEFT);
2307 rval = XFS_ILOG_DEXT;
2310 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2313 if (XFS_IS_CORRUPT(mp, i != 1)) {
2314 error = -EFSCORRUPTED;
2317 error = xfs_bmbt_update(cur, &PREV);
2320 error = xfs_btree_decrement(cur, 0, &i);
2323 error = xfs_bmbt_update(cur, &LEFT);
2329 case BMAP_LEFT_FILLING:
2331 * Setting the first part of a previous oldext extent to newext.
2332 * The left neighbor is not contiguous.
2335 PREV.br_startoff += new->br_blockcount;
2336 PREV.br_startblock += new->br_blockcount;
2337 PREV.br_blockcount -= new->br_blockcount;
2339 xfs_iext_update_extent(ip, state, icur, &PREV);
2340 xfs_iext_insert(ip, icur, new, state);
2344 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2346 rval = XFS_ILOG_CORE;
2347 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2350 if (XFS_IS_CORRUPT(mp, i != 1)) {
2351 error = -EFSCORRUPTED;
2354 error = xfs_bmbt_update(cur, &PREV);
2357 cur->bc_rec.b = *new;
2358 if ((error = xfs_btree_insert(cur, &i)))
2360 if (XFS_IS_CORRUPT(mp, i != 1)) {
2361 error = -EFSCORRUPTED;
2367 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2369 * Setting the last part of a previous oldext extent to newext.
2370 * The right neighbor is contiguous with the new allocation.
2373 PREV.br_blockcount -= new->br_blockcount;
2375 RIGHT.br_startoff = new->br_startoff;
2376 RIGHT.br_startblock = new->br_startblock;
2377 RIGHT.br_blockcount += new->br_blockcount;
2379 xfs_iext_update_extent(ip, state, icur, &PREV);
2380 xfs_iext_next(ifp, icur);
2381 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2384 rval = XFS_ILOG_DEXT;
2387 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2390 if (XFS_IS_CORRUPT(mp, i != 1)) {
2391 error = -EFSCORRUPTED;
2394 error = xfs_bmbt_update(cur, &PREV);
2397 error = xfs_btree_increment(cur, 0, &i);
2400 error = xfs_bmbt_update(cur, &RIGHT);
2406 case BMAP_RIGHT_FILLING:
2408 * Setting the last part of a previous oldext extent to newext.
2409 * The right neighbor is not contiguous.
2412 PREV.br_blockcount -= new->br_blockcount;
2414 xfs_iext_update_extent(ip, state, icur, &PREV);
2415 xfs_iext_next(ifp, icur);
2416 xfs_iext_insert(ip, icur, new, state);
2420 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2422 rval = XFS_ILOG_CORE;
2423 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2426 if (XFS_IS_CORRUPT(mp, i != 1)) {
2427 error = -EFSCORRUPTED;
2430 error = xfs_bmbt_update(cur, &PREV);
2433 error = xfs_bmbt_lookup_eq(cur, new, &i);
2436 if (XFS_IS_CORRUPT(mp, i != 0)) {
2437 error = -EFSCORRUPTED;
2440 if ((error = xfs_btree_insert(cur, &i)))
2442 if (XFS_IS_CORRUPT(mp, i != 1)) {
2443 error = -EFSCORRUPTED;
2451 * Setting the middle part of a previous oldext extent to
2452 * newext. Contiguity is impossible here.
2453 * One extent becomes three extents.
2456 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2459 r[1].br_startoff = new_endoff;
2460 r[1].br_blockcount =
2461 old.br_startoff + old.br_blockcount - new_endoff;
2462 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2463 r[1].br_state = PREV.br_state;
2465 xfs_iext_update_extent(ip, state, icur, &PREV);
2466 xfs_iext_next(ifp, icur);
2467 xfs_iext_insert(ip, icur, &r[1], state);
2468 xfs_iext_insert(ip, icur, &r[0], state);
2469 ifp->if_nextents += 2;
2472 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2474 rval = XFS_ILOG_CORE;
2475 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2478 if (XFS_IS_CORRUPT(mp, i != 1)) {
2479 error = -EFSCORRUPTED;
2482 /* new right extent - oldext */
2483 error = xfs_bmbt_update(cur, &r[1]);
2486 /* new left extent - oldext */
2487 cur->bc_rec.b = PREV;
2488 if ((error = xfs_btree_insert(cur, &i)))
2490 if (XFS_IS_CORRUPT(mp, i != 1)) {
2491 error = -EFSCORRUPTED;
2495 * Reset the cursor to the position of the new extent
2496 * we are about to insert as we can't trust it after
2497 * the previous insert.
2499 error = xfs_bmbt_lookup_eq(cur, new, &i);
2502 if (XFS_IS_CORRUPT(mp, i != 0)) {
2503 error = -EFSCORRUPTED;
2506 /* new middle extent - newext */
2507 if ((error = xfs_btree_insert(cur, &i)))
2509 if (XFS_IS_CORRUPT(mp, i != 1)) {
2510 error = -EFSCORRUPTED;
2516 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2517 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2518 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2519 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2520 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2521 case BMAP_LEFT_CONTIG:
2522 case BMAP_RIGHT_CONTIG:
2524 * These cases are all impossible.
2529 /* update reverse mappings */
2530 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2532 /* convert to a btree if necessary */
2533 if (xfs_bmap_needs_btree(ip, whichfork)) {
2534 int tmp_logflags; /* partial log flag return val */
2536 ASSERT(cur == NULL);
2537 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2538 &tmp_logflags, whichfork);
2539 *logflagsp |= tmp_logflags;
2544 /* clear out the allocated field, done with it now in any case. */
2546 cur->bc_ino.allocated = 0;
2550 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2560 * Convert a hole to a delayed allocation.
2563 xfs_bmap_add_extent_hole_delay(
2564 xfs_inode_t *ip, /* incore inode pointer */
2566 struct xfs_iext_cursor *icur,
2567 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2569 struct xfs_ifork *ifp; /* inode fork pointer */
2570 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2571 xfs_filblks_t newlen=0; /* new indirect size */
2572 xfs_filblks_t oldlen=0; /* old indirect size */
2573 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2574 int state = xfs_bmap_fork_to_state(whichfork);
2575 xfs_filblks_t temp; /* temp for indirect calculations */
2577 ifp = XFS_IFORK_PTR(ip, whichfork);
2578 ASSERT(isnullstartblock(new->br_startblock));
2581 * Check and set flags if this segment has a left neighbor
2583 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2584 state |= BMAP_LEFT_VALID;
2585 if (isnullstartblock(left.br_startblock))
2586 state |= BMAP_LEFT_DELAY;
2590 * Check and set flags if the current (right) segment exists.
2591 * If it doesn't exist, we're converting the hole at end-of-file.
2593 if (xfs_iext_get_extent(ifp, icur, &right)) {
2594 state |= BMAP_RIGHT_VALID;
2595 if (isnullstartblock(right.br_startblock))
2596 state |= BMAP_RIGHT_DELAY;
2600 * Set contiguity flags on the left and right neighbors.
2601 * Don't let extents get too large, even if the pieces are contiguous.
2603 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2604 left.br_startoff + left.br_blockcount == new->br_startoff &&
2605 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2606 state |= BMAP_LEFT_CONTIG;
2608 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2609 new->br_startoff + new->br_blockcount == right.br_startoff &&
2610 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2611 (!(state & BMAP_LEFT_CONTIG) ||
2612 (left.br_blockcount + new->br_blockcount +
2613 right.br_blockcount <= MAXEXTLEN)))
2614 state |= BMAP_RIGHT_CONTIG;
2617 * Switch out based on the contiguity flags.
2619 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2620 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2622 * New allocation is contiguous with delayed allocations
2623 * on the left and on the right.
2624 * Merge all three into a single extent record.
2626 temp = left.br_blockcount + new->br_blockcount +
2627 right.br_blockcount;
2629 oldlen = startblockval(left.br_startblock) +
2630 startblockval(new->br_startblock) +
2631 startblockval(right.br_startblock);
2632 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2634 left.br_startblock = nullstartblock(newlen);
2635 left.br_blockcount = temp;
2637 xfs_iext_remove(ip, icur, state);
2638 xfs_iext_prev(ifp, icur);
2639 xfs_iext_update_extent(ip, state, icur, &left);
2642 case BMAP_LEFT_CONTIG:
2644 * New allocation is contiguous with a delayed allocation
2646 * Merge the new allocation with the left neighbor.
2648 temp = left.br_blockcount + new->br_blockcount;
2650 oldlen = startblockval(left.br_startblock) +
2651 startblockval(new->br_startblock);
2652 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2654 left.br_blockcount = temp;
2655 left.br_startblock = nullstartblock(newlen);
2657 xfs_iext_prev(ifp, icur);
2658 xfs_iext_update_extent(ip, state, icur, &left);
2661 case BMAP_RIGHT_CONTIG:
2663 * New allocation is contiguous with a delayed allocation
2665 * Merge the new allocation with the right neighbor.
2667 temp = new->br_blockcount + right.br_blockcount;
2668 oldlen = startblockval(new->br_startblock) +
2669 startblockval(right.br_startblock);
2670 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2672 right.br_startoff = new->br_startoff;
2673 right.br_startblock = nullstartblock(newlen);
2674 right.br_blockcount = temp;
2675 xfs_iext_update_extent(ip, state, icur, &right);
2680 * New allocation is not contiguous with another
2681 * delayed allocation.
2682 * Insert a new entry.
2684 oldlen = newlen = 0;
2685 xfs_iext_insert(ip, icur, new, state);
2688 if (oldlen != newlen) {
2689 ASSERT(oldlen > newlen);
2690 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2693 * Nothing to do for disk quota accounting here.
2695 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2700 * Convert a hole to a real allocation.
2702 STATIC int /* error */
2703 xfs_bmap_add_extent_hole_real(
2704 struct xfs_trans *tp,
2705 struct xfs_inode *ip,
2707 struct xfs_iext_cursor *icur,
2708 struct xfs_btree_cur **curp,
2709 struct xfs_bmbt_irec *new,
2713 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2714 struct xfs_mount *mp = ip->i_mount;
2715 struct xfs_btree_cur *cur = *curp;
2716 int error; /* error return value */
2717 int i; /* temp state */
2718 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2719 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2720 int rval=0; /* return value (logging flags) */
2721 int state = xfs_bmap_fork_to_state(whichfork);
2722 struct xfs_bmbt_irec old;
2724 ASSERT(!isnullstartblock(new->br_startblock));
2725 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2727 XFS_STATS_INC(mp, xs_add_exlist);
2730 * Check and set flags if this segment has a left neighbor.
2732 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2733 state |= BMAP_LEFT_VALID;
2734 if (isnullstartblock(left.br_startblock))
2735 state |= BMAP_LEFT_DELAY;
2739 * Check and set flags if this segment has a current value.
2740 * Not true if we're inserting into the "hole" at eof.
2742 if (xfs_iext_get_extent(ifp, icur, &right)) {
2743 state |= BMAP_RIGHT_VALID;
2744 if (isnullstartblock(right.br_startblock))
2745 state |= BMAP_RIGHT_DELAY;
2749 * We're inserting a real allocation between "left" and "right".
2750 * Set the contiguity flags. Don't let extents get too large.
2752 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2753 left.br_startoff + left.br_blockcount == new->br_startoff &&
2754 left.br_startblock + left.br_blockcount == new->br_startblock &&
2755 left.br_state == new->br_state &&
2756 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2757 state |= BMAP_LEFT_CONTIG;
2759 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2760 new->br_startoff + new->br_blockcount == right.br_startoff &&
2761 new->br_startblock + new->br_blockcount == right.br_startblock &&
2762 new->br_state == right.br_state &&
2763 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2764 (!(state & BMAP_LEFT_CONTIG) ||
2765 left.br_blockcount + new->br_blockcount +
2766 right.br_blockcount <= MAXEXTLEN))
2767 state |= BMAP_RIGHT_CONTIG;
2771 * Select which case we're in here, and implement it.
2773 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2774 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2776 * New allocation is contiguous with real allocations on the
2777 * left and on the right.
2778 * Merge all three into a single extent record.
2780 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2782 xfs_iext_remove(ip, icur, state);
2783 xfs_iext_prev(ifp, icur);
2784 xfs_iext_update_extent(ip, state, icur, &left);
2788 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2790 rval = XFS_ILOG_CORE;
2791 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2794 if (XFS_IS_CORRUPT(mp, i != 1)) {
2795 error = -EFSCORRUPTED;
2798 error = xfs_btree_delete(cur, &i);
2801 if (XFS_IS_CORRUPT(mp, i != 1)) {
2802 error = -EFSCORRUPTED;
2805 error = xfs_btree_decrement(cur, 0, &i);
2808 if (XFS_IS_CORRUPT(mp, i != 1)) {
2809 error = -EFSCORRUPTED;
2812 error = xfs_bmbt_update(cur, &left);
2818 case BMAP_LEFT_CONTIG:
2820 * New allocation is contiguous with a real allocation
2822 * Merge the new allocation with the left neighbor.
2825 left.br_blockcount += new->br_blockcount;
2827 xfs_iext_prev(ifp, icur);
2828 xfs_iext_update_extent(ip, state, icur, &left);
2831 rval = xfs_ilog_fext(whichfork);
2834 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2837 if (XFS_IS_CORRUPT(mp, i != 1)) {
2838 error = -EFSCORRUPTED;
2841 error = xfs_bmbt_update(cur, &left);
2847 case BMAP_RIGHT_CONTIG:
2849 * New allocation is contiguous with a real allocation
2851 * Merge the new allocation with the right neighbor.
2855 right.br_startoff = new->br_startoff;
2856 right.br_startblock = new->br_startblock;
2857 right.br_blockcount += new->br_blockcount;
2858 xfs_iext_update_extent(ip, state, icur, &right);
2861 rval = xfs_ilog_fext(whichfork);
2864 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2867 if (XFS_IS_CORRUPT(mp, i != 1)) {
2868 error = -EFSCORRUPTED;
2871 error = xfs_bmbt_update(cur, &right);
2879 * New allocation is not contiguous with another
2881 * Insert a new entry.
2883 xfs_iext_insert(ip, icur, new, state);
2887 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2889 rval = XFS_ILOG_CORE;
2890 error = xfs_bmbt_lookup_eq(cur, new, &i);
2893 if (XFS_IS_CORRUPT(mp, i != 0)) {
2894 error = -EFSCORRUPTED;
2897 error = xfs_btree_insert(cur, &i);
2900 if (XFS_IS_CORRUPT(mp, i != 1)) {
2901 error = -EFSCORRUPTED;
2908 /* add reverse mapping unless caller opted out */
2909 if (!(flags & XFS_BMAPI_NORMAP))
2910 xfs_rmap_map_extent(tp, ip, whichfork, new);
2912 /* convert to a btree if necessary */
2913 if (xfs_bmap_needs_btree(ip, whichfork)) {
2914 int tmp_logflags; /* partial log flag return val */
2916 ASSERT(cur == NULL);
2917 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2918 &tmp_logflags, whichfork);
2919 *logflagsp |= tmp_logflags;
2925 /* clear out the allocated field, done with it now in any case. */
2927 cur->bc_ino.allocated = 0;
2929 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2936 * Functions used in the extent read, allocate and remove paths
2940 * Adjust the size of the new extent based on di_extsize and rt extsize.
2943 xfs_bmap_extsize_align(
2945 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2946 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2947 xfs_extlen_t extsz, /* align to this extent size */
2948 int rt, /* is this a realtime inode? */
2949 int eof, /* is extent at end-of-file? */
2950 int delay, /* creating delalloc extent? */
2951 int convert, /* overwriting unwritten extent? */
2952 xfs_fileoff_t *offp, /* in/out: aligned offset */
2953 xfs_extlen_t *lenp) /* in/out: aligned length */
2955 xfs_fileoff_t orig_off; /* original offset */
2956 xfs_extlen_t orig_alen; /* original length */
2957 xfs_fileoff_t orig_end; /* original off+len */
2958 xfs_fileoff_t nexto; /* next file offset */
2959 xfs_fileoff_t prevo; /* previous file offset */
2960 xfs_fileoff_t align_off; /* temp for offset */
2961 xfs_extlen_t align_alen; /* temp for length */
2962 xfs_extlen_t temp; /* temp for calculations */
2967 orig_off = align_off = *offp;
2968 orig_alen = align_alen = *lenp;
2969 orig_end = orig_off + orig_alen;
2972 * If this request overlaps an existing extent, then don't
2973 * attempt to perform any additional alignment.
2975 if (!delay && !eof &&
2976 (orig_off >= gotp->br_startoff) &&
2977 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2982 * If the file offset is unaligned vs. the extent size
2983 * we need to align it. This will be possible unless
2984 * the file was previously written with a kernel that didn't
2985 * perform this alignment, or if a truncate shot us in the
2988 div_u64_rem(orig_off, extsz, &temp);
2994 /* Same adjustment for the end of the requested area. */
2995 temp = (align_alen % extsz);
2997 align_alen += extsz - temp;
3000 * For large extent hint sizes, the aligned extent might be larger than
3001 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3002 * the length back under MAXEXTLEN. The outer allocation loops handle
3003 * short allocation just fine, so it is safe to do this. We only want to
3004 * do it when we are forced to, though, because it means more allocation
3005 * operations are required.
3007 while (align_alen > MAXEXTLEN)
3008 align_alen -= extsz;
3009 ASSERT(align_alen <= MAXEXTLEN);
3012 * If the previous block overlaps with this proposed allocation
3013 * then move the start forward without adjusting the length.
3015 if (prevp->br_startoff != NULLFILEOFF) {
3016 if (prevp->br_startblock == HOLESTARTBLOCK)
3017 prevo = prevp->br_startoff;
3019 prevo = prevp->br_startoff + prevp->br_blockcount;
3022 if (align_off != orig_off && align_off < prevo)
3025 * If the next block overlaps with this proposed allocation
3026 * then move the start back without adjusting the length,
3027 * but not before offset 0.
3028 * This may of course make the start overlap previous block,
3029 * and if we hit the offset 0 limit then the next block
3030 * can still overlap too.
3032 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3033 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3034 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3035 nexto = gotp->br_startoff + gotp->br_blockcount;
3037 nexto = gotp->br_startoff;
3039 nexto = NULLFILEOFF;
3041 align_off + align_alen != orig_end &&
3042 align_off + align_alen > nexto)
3043 align_off = nexto > align_alen ? nexto - align_alen : 0;
3045 * If we're now overlapping the next or previous extent that
3046 * means we can't fit an extsz piece in this hole. Just move
3047 * the start forward to the first valid spot and set
3048 * the length so we hit the end.
3050 if (align_off != orig_off && align_off < prevo)
3052 if (align_off + align_alen != orig_end &&
3053 align_off + align_alen > nexto &&
3054 nexto != NULLFILEOFF) {
3055 ASSERT(nexto > prevo);
3056 align_alen = nexto - align_off;
3060 * If realtime, and the result isn't a multiple of the realtime
3061 * extent size we need to remove blocks until it is.
3063 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3065 * We're not covering the original request, or
3066 * we won't be able to once we fix the length.
3068 if (orig_off < align_off ||
3069 orig_end > align_off + align_alen ||
3070 align_alen - temp < orig_alen)
3073 * Try to fix it by moving the start up.
3075 if (align_off + temp <= orig_off) {
3080 * Try to fix it by moving the end in.
3082 else if (align_off + align_alen - temp >= orig_end)
3085 * Set the start to the minimum then trim the length.
3088 align_alen -= orig_off - align_off;
3089 align_off = orig_off;
3090 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3093 * Result doesn't cover the request, fail it.
3095 if (orig_off < align_off || orig_end > align_off + align_alen)
3098 ASSERT(orig_off >= align_off);
3099 /* see MAXEXTLEN handling above */
3100 ASSERT(orig_end <= align_off + align_alen ||
3101 align_alen + extsz > MAXEXTLEN);
3105 if (!eof && gotp->br_startoff != NULLFILEOFF)
3106 ASSERT(align_off + align_alen <= gotp->br_startoff);
3107 if (prevp->br_startoff != NULLFILEOFF)
3108 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3116 #define XFS_ALLOC_GAP_UNITS 4
3120 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3122 xfs_fsblock_t adjust; /* adjustment to block numbers */
3123 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3124 xfs_mount_t *mp; /* mount point structure */
3125 int nullfb; /* true if ap->firstblock isn't set */
3126 int rt; /* true if inode is realtime */
3128 #define ISVALID(x,y) \
3130 (x) < mp->m_sb.sb_rblocks : \
3131 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3132 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3133 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3135 mp = ap->ip->i_mount;
3136 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3137 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3138 (ap->datatype & XFS_ALLOC_USERDATA);
3139 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3140 ap->tp->t_firstblock);
3142 * If allocating at eof, and there's a previous real block,
3143 * try to use its last block as our starting point.
3145 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3146 !isnullstartblock(ap->prev.br_startblock) &&
3147 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3148 ap->prev.br_startblock)) {
3149 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3151 * Adjust for the gap between prevp and us.
3153 adjust = ap->offset -
3154 (ap->prev.br_startoff + ap->prev.br_blockcount);
3156 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3157 ap->blkno += adjust;
3160 * If not at eof, then compare the two neighbor blocks.
3161 * Figure out whether either one gives us a good starting point,
3162 * and pick the better one.
3164 else if (!ap->eof) {
3165 xfs_fsblock_t gotbno; /* right side block number */
3166 xfs_fsblock_t gotdiff=0; /* right side difference */
3167 xfs_fsblock_t prevbno; /* left side block number */
3168 xfs_fsblock_t prevdiff=0; /* left side difference */
3171 * If there's a previous (left) block, select a requested
3172 * start block based on it.
3174 if (ap->prev.br_startoff != NULLFILEOFF &&
3175 !isnullstartblock(ap->prev.br_startblock) &&
3176 (prevbno = ap->prev.br_startblock +
3177 ap->prev.br_blockcount) &&
3178 ISVALID(prevbno, ap->prev.br_startblock)) {
3180 * Calculate gap to end of previous block.
3182 adjust = prevdiff = ap->offset -
3183 (ap->prev.br_startoff +
3184 ap->prev.br_blockcount);
3186 * Figure the startblock based on the previous block's
3187 * end and the gap size.
3189 * If the gap is large relative to the piece we're
3190 * allocating, or using it gives us an invalid block
3191 * number, then just use the end of the previous block.
3193 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3194 ISVALID(prevbno + prevdiff,
3195 ap->prev.br_startblock))
3200 * If the firstblock forbids it, can't use it,
3203 if (!rt && !nullfb &&
3204 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3205 prevbno = NULLFSBLOCK;
3208 * No previous block or can't follow it, just default.
3211 prevbno = NULLFSBLOCK;
3213 * If there's a following (right) block, select a requested
3214 * start block based on it.
3216 if (!isnullstartblock(ap->got.br_startblock)) {
3218 * Calculate gap to start of next block.
3220 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3222 * Figure the startblock based on the next block's
3223 * start and the gap size.
3225 gotbno = ap->got.br_startblock;
3228 * If the gap is large relative to the piece we're
3229 * allocating, or using it gives us an invalid block
3230 * number, then just use the start of the next block
3231 * offset by our length.
3233 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3234 ISVALID(gotbno - gotdiff, gotbno))
3236 else if (ISVALID(gotbno - ap->length, gotbno)) {
3237 gotbno -= ap->length;
3238 gotdiff += adjust - ap->length;
3242 * If the firstblock forbids it, can't use it,
3245 if (!rt && !nullfb &&
3246 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3247 gotbno = NULLFSBLOCK;
3250 * No next block, just default.
3253 gotbno = NULLFSBLOCK;
3255 * If both valid, pick the better one, else the only good
3256 * one, else ap->blkno is already set (to 0 or the inode block).
3258 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3259 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3260 else if (prevbno != NULLFSBLOCK)
3261 ap->blkno = prevbno;
3262 else if (gotbno != NULLFSBLOCK)
3269 xfs_bmap_longest_free_extent(
3270 struct xfs_trans *tp,
3275 struct xfs_mount *mp = tp->t_mountp;
3276 struct xfs_perag *pag;
3277 xfs_extlen_t longest;
3280 pag = xfs_perag_get(mp, ag);
3281 if (!pag->pagf_init) {
3282 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3284 /* Couldn't lock the AGF, so skip this AG. */
3285 if (error == -EAGAIN) {
3293 longest = xfs_alloc_longest_free_extent(pag,
3294 xfs_alloc_min_freelist(mp, pag),
3295 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3296 if (*blen < longest)
3305 xfs_bmap_select_minlen(
3306 struct xfs_bmalloca *ap,
3307 struct xfs_alloc_arg *args,
3311 if (notinit || *blen < ap->minlen) {
3313 * Since we did a BUF_TRYLOCK above, it is possible that
3314 * there is space for this request.
3316 args->minlen = ap->minlen;
3317 } else if (*blen < args->maxlen) {
3319 * If the best seen length is less than the request length,
3320 * use the best as the minimum.
3322 args->minlen = *blen;
3325 * Otherwise we've seen an extent as big as maxlen, use that
3328 args->minlen = args->maxlen;
3333 xfs_bmap_btalloc_nullfb(
3334 struct xfs_bmalloca *ap,
3335 struct xfs_alloc_arg *args,
3338 struct xfs_mount *mp = ap->ip->i_mount;
3339 xfs_agnumber_t ag, startag;
3343 args->type = XFS_ALLOCTYPE_START_BNO;
3344 args->total = ap->total;
3346 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3347 if (startag == NULLAGNUMBER)
3350 while (*blen < args->maxlen) {
3351 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3356 if (++ag == mp->m_sb.sb_agcount)
3362 xfs_bmap_select_minlen(ap, args, blen, notinit);
3367 xfs_bmap_btalloc_filestreams(
3368 struct xfs_bmalloca *ap,
3369 struct xfs_alloc_arg *args,
3372 struct xfs_mount *mp = ap->ip->i_mount;
3377 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3378 args->total = ap->total;
3380 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3381 if (ag == NULLAGNUMBER)
3384 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3388 if (*blen < args->maxlen) {
3389 error = xfs_filestream_new_ag(ap, &ag);
3393 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3400 xfs_bmap_select_minlen(ap, args, blen, notinit);
3403 * Set the failure fallback case to look in the selected AG as stream
3406 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3410 /* Update all inode and quota accounting for the allocation we just did. */
3412 xfs_bmap_btalloc_accounting(
3413 struct xfs_bmalloca *ap,
3414 struct xfs_alloc_arg *args)
3416 if (ap->flags & XFS_BMAPI_COWFORK) {
3418 * COW fork blocks are in-core only and thus are treated as
3419 * in-core quota reservation (like delalloc blocks) even when
3420 * converted to real blocks. The quota reservation is not
3421 * accounted to disk until blocks are remapped to the data
3422 * fork. So if these blocks were previously delalloc, we
3423 * already have quota reservation and there's nothing to do
3427 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3432 * Otherwise, we've allocated blocks in a hole. The transaction
3433 * has acquired in-core quota reservation for this extent.
3434 * Rather than account these as real blocks, however, we reduce
3435 * the transaction quota reservation based on the allocation.
3436 * This essentially transfers the transaction quota reservation
3437 * to that of a delalloc extent.
3439 ap->ip->i_delayed_blks += args->len;
3440 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3445 /* data/attr fork only */
3446 ap->ip->i_nblocks += args->len;
3447 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3449 ap->ip->i_delayed_blks -= args->len;
3450 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3452 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3453 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3458 xfs_bmap_compute_alignments(
3459 struct xfs_bmalloca *ap,
3460 struct xfs_alloc_arg *args)
3462 struct xfs_mount *mp = args->mp;
3463 xfs_extlen_t align = 0; /* minimum allocation alignment */
3464 int stripe_align = 0;
3466 /* stripe alignment for allocation is determined by mount parameters */
3467 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3468 stripe_align = mp->m_swidth;
3469 else if (mp->m_dalign)
3470 stripe_align = mp->m_dalign;
3472 if (ap->flags & XFS_BMAPI_COWFORK)
3473 align = xfs_get_cowextsz_hint(ap->ip);
3474 else if (ap->datatype & XFS_ALLOC_USERDATA)
3475 align = xfs_get_extsz_hint(ap->ip);
3477 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3478 ap->eof, 0, ap->conv, &ap->offset,
3484 /* apply extent size hints if obtained earlier */
3487 div_u64_rem(ap->offset, args->prod, &args->mod);
3489 args->mod = args->prod - args->mod;
3490 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3494 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3495 div_u64_rem(ap->offset, args->prod, &args->mod);
3497 args->mod = args->prod - args->mod;
3500 return stripe_align;
3504 xfs_bmap_process_allocated_extent(
3505 struct xfs_bmalloca *ap,
3506 struct xfs_alloc_arg *args,
3507 xfs_fileoff_t orig_offset,
3508 xfs_extlen_t orig_length)
3512 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3515 * check the allocation happened at the same or higher AG than
3516 * the first block that was allocated.
3519 XFS_FSB_TO_AGNO(args->mp, ap->tp->t_firstblock) <=
3520 XFS_FSB_TO_AGNO(args->mp, args->fsbno));
3522 ap->blkno = args->fsbno;
3524 ap->tp->t_firstblock = args->fsbno;
3525 ap->length = args->len;
3527 * If the extent size hint is active, we tried to round the
3528 * caller's allocation request offset down to extsz and the
3529 * length up to another extsz boundary. If we found a free
3530 * extent we mapped it in starting at this new offset. If the
3531 * newly mapped space isn't long enough to cover any of the
3532 * range of offsets that was originally requested, move the
3533 * mapping up so that we can fill as much of the caller's
3534 * original request as possible. Free space is apparently
3535 * very fragmented so we're unlikely to be able to satisfy the
3538 if (ap->length <= orig_length)
3539 ap->offset = orig_offset;
3540 else if (ap->offset + ap->length < orig_offset + orig_length)
3541 ap->offset = orig_offset + orig_length - ap->length;
3542 xfs_bmap_btalloc_accounting(ap, args);
3547 xfs_bmap_exact_minlen_extent_alloc(
3548 struct xfs_bmalloca *ap)
3550 struct xfs_mount *mp = ap->ip->i_mount;
3551 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3552 xfs_fileoff_t orig_offset;
3553 xfs_extlen_t orig_length;
3558 if (ap->minlen != 1) {
3559 ap->blkno = NULLFSBLOCK;
3564 orig_offset = ap->offset;
3565 orig_length = ap->length;
3567 args.alloc_minlen_only = 1;
3569 xfs_bmap_compute_alignments(ap, &args);
3571 if (ap->tp->t_firstblock == NULLFSBLOCK) {
3573 * Unlike the longest extent available in an AG, we don't track
3574 * the length of an AG's shortest extent.
3575 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3576 * hence we can afford to start traversing from the 0th AG since
3577 * we need not be concerned about a drop in performance in
3578 * "debug only" code paths.
3580 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3582 ap->blkno = ap->tp->t_firstblock;
3585 args.fsbno = ap->blkno;
3586 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3587 args.type = XFS_ALLOCTYPE_FIRST_AG;
3588 args.minlen = args.maxlen = ap->minlen;
3589 args.total = ap->total;
3592 args.minalignslop = 0;
3594 args.minleft = ap->minleft;
3595 args.wasdel = ap->wasdel;
3596 args.resv = XFS_AG_RESV_NONE;
3597 args.datatype = ap->datatype;
3599 error = xfs_alloc_vextent(&args);
3603 if (args.fsbno != NULLFSBLOCK) {
3604 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3607 ap->blkno = NULLFSBLOCK;
3615 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3621 struct xfs_bmalloca *ap)
3623 struct xfs_mount *mp = ap->ip->i_mount;
3624 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3625 xfs_alloctype_t atype = 0;
3626 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3628 xfs_fileoff_t orig_offset;
3629 xfs_extlen_t orig_length;
3631 xfs_extlen_t nextminlen = 0;
3632 int nullfb; /* true if ap->firstblock isn't set */
3639 orig_offset = ap->offset;
3640 orig_length = ap->length;
3642 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3644 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3645 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3646 ap->tp->t_firstblock);
3648 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3649 xfs_inode_is_filestream(ap->ip)) {
3650 ag = xfs_filestream_lookup_ag(ap->ip);
3651 ag = (ag != NULLAGNUMBER) ? ag : 0;
3652 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3654 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3657 ap->blkno = ap->tp->t_firstblock;
3659 xfs_bmap_adjacent(ap);
3662 * If allowed, use ap->blkno; otherwise must use firstblock since
3663 * it's in the right allocation group.
3665 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3668 ap->blkno = ap->tp->t_firstblock;
3670 * Normal allocation, done through xfs_alloc_vextent.
3672 tryagain = isaligned = 0;
3673 args.fsbno = ap->blkno;
3674 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3676 /* Trim the allocation back to the maximum an AG can fit. */
3677 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3681 * Search for an allocation group with a single extent large
3682 * enough for the request. If one isn't found, then adjust
3683 * the minimum allocation size to the largest space found.
3685 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3686 xfs_inode_is_filestream(ap->ip))
3687 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3689 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3692 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3693 if (xfs_inode_is_filestream(ap->ip))
3694 args.type = XFS_ALLOCTYPE_FIRST_AG;
3696 args.type = XFS_ALLOCTYPE_START_BNO;
3697 args.total = args.minlen = ap->minlen;
3699 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3700 args.total = ap->total;
3701 args.minlen = ap->minlen;
3705 * If we are not low on available data blocks, and the underlying
3706 * logical volume manager is a stripe, and the file offset is zero then
3707 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3708 * is only set if the allocation length is >= the stripe unit and the
3709 * allocation offset is at the end of file.
3711 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3713 args.alignment = stripe_align;
3717 * Adjust minlen to try and preserve alignment if we
3718 * can't guarantee an aligned maxlen extent.
3720 if (blen > args.alignment &&
3721 blen <= args.maxlen + args.alignment)
3722 args.minlen = blen - args.alignment;
3723 args.minalignslop = 0;
3726 * First try an exact bno allocation.
3727 * If it fails then do a near or start bno
3728 * allocation with alignment turned on.
3732 args.type = XFS_ALLOCTYPE_THIS_BNO;
3735 * Compute the minlen+alignment for the
3736 * next case. Set slop so that the value
3737 * of minlen+alignment+slop doesn't go up
3738 * between the calls.
3740 if (blen > stripe_align && blen <= args.maxlen)
3741 nextminlen = blen - stripe_align;
3743 nextminlen = args.minlen;
3744 if (nextminlen + stripe_align > args.minlen + 1)
3746 nextminlen + stripe_align -
3749 args.minalignslop = 0;
3753 args.minalignslop = 0;
3755 args.minleft = ap->minleft;
3756 args.wasdel = ap->wasdel;
3757 args.resv = XFS_AG_RESV_NONE;
3758 args.datatype = ap->datatype;
3760 error = xfs_alloc_vextent(&args);
3764 if (tryagain && args.fsbno == NULLFSBLOCK) {
3766 * Exact allocation failed. Now try with alignment
3770 args.fsbno = ap->blkno;
3771 args.alignment = stripe_align;
3772 args.minlen = nextminlen;
3773 args.minalignslop = 0;
3775 if ((error = xfs_alloc_vextent(&args)))
3778 if (isaligned && args.fsbno == NULLFSBLOCK) {
3780 * allocation failed, so turn off alignment and
3784 args.fsbno = ap->blkno;
3786 if ((error = xfs_alloc_vextent(&args)))
3789 if (args.fsbno == NULLFSBLOCK && nullfb &&
3790 args.minlen > ap->minlen) {
3791 args.minlen = ap->minlen;
3792 args.type = XFS_ALLOCTYPE_START_BNO;
3793 args.fsbno = ap->blkno;
3794 if ((error = xfs_alloc_vextent(&args)))
3797 if (args.fsbno == NULLFSBLOCK && nullfb) {
3799 args.type = XFS_ALLOCTYPE_FIRST_AG;
3800 args.total = ap->minlen;
3801 if ((error = xfs_alloc_vextent(&args)))
3803 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3806 if (args.fsbno != NULLFSBLOCK) {
3807 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3810 ap->blkno = NULLFSBLOCK;
3816 /* Trim extent to fit a logical block range. */
3819 struct xfs_bmbt_irec *irec,
3823 xfs_fileoff_t distance;
3824 xfs_fileoff_t end = bno + len;
3826 if (irec->br_startoff + irec->br_blockcount <= bno ||
3827 irec->br_startoff >= end) {
3828 irec->br_blockcount = 0;
3832 if (irec->br_startoff < bno) {
3833 distance = bno - irec->br_startoff;
3834 if (isnullstartblock(irec->br_startblock))
3835 irec->br_startblock = DELAYSTARTBLOCK;
3836 if (irec->br_startblock != DELAYSTARTBLOCK &&
3837 irec->br_startblock != HOLESTARTBLOCK)
3838 irec->br_startblock += distance;
3839 irec->br_startoff += distance;
3840 irec->br_blockcount -= distance;
3843 if (end < irec->br_startoff + irec->br_blockcount) {
3844 distance = irec->br_startoff + irec->br_blockcount - end;
3845 irec->br_blockcount -= distance;
3850 * Trim the returned map to the required bounds
3854 struct xfs_bmbt_irec *mval,
3855 struct xfs_bmbt_irec *got,
3863 if ((flags & XFS_BMAPI_ENTIRE) ||
3864 got->br_startoff + got->br_blockcount <= obno) {
3866 if (isnullstartblock(got->br_startblock))
3867 mval->br_startblock = DELAYSTARTBLOCK;
3873 ASSERT((*bno >= obno) || (n == 0));
3875 mval->br_startoff = *bno;
3876 if (isnullstartblock(got->br_startblock))
3877 mval->br_startblock = DELAYSTARTBLOCK;
3879 mval->br_startblock = got->br_startblock +
3880 (*bno - got->br_startoff);
3882 * Return the minimum of what we got and what we asked for for
3883 * the length. We can use the len variable here because it is
3884 * modified below and we could have been there before coming
3885 * here if the first part of the allocation didn't overlap what
3888 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3889 got->br_blockcount - (*bno - got->br_startoff));
3890 mval->br_state = got->br_state;
3891 ASSERT(mval->br_blockcount <= len);
3896 * Update and validate the extent map to return
3899 xfs_bmapi_update_map(
3900 struct xfs_bmbt_irec **map,
3908 xfs_bmbt_irec_t *mval = *map;
3910 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3911 ((mval->br_startoff + mval->br_blockcount) <= end));
3912 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3913 (mval->br_startoff < obno));
3915 *bno = mval->br_startoff + mval->br_blockcount;
3917 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3918 /* update previous map with new information */
3919 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3920 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3921 ASSERT(mval->br_state == mval[-1].br_state);
3922 mval[-1].br_blockcount = mval->br_blockcount;
3923 mval[-1].br_state = mval->br_state;
3924 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3925 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3926 mval[-1].br_startblock != HOLESTARTBLOCK &&
3927 mval->br_startblock == mval[-1].br_startblock +
3928 mval[-1].br_blockcount &&
3929 mval[-1].br_state == mval->br_state) {
3930 ASSERT(mval->br_startoff ==
3931 mval[-1].br_startoff + mval[-1].br_blockcount);
3932 mval[-1].br_blockcount += mval->br_blockcount;
3933 } else if (*n > 0 &&
3934 mval->br_startblock == DELAYSTARTBLOCK &&
3935 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3936 mval->br_startoff ==
3937 mval[-1].br_startoff + mval[-1].br_blockcount) {
3938 mval[-1].br_blockcount += mval->br_blockcount;
3939 mval[-1].br_state = mval->br_state;
3940 } else if (!((*n == 0) &&
3941 ((mval->br_startoff + mval->br_blockcount) <=
3950 * Map file blocks to filesystem blocks without allocation.
3954 struct xfs_inode *ip,
3957 struct xfs_bmbt_irec *mval,
3961 struct xfs_mount *mp = ip->i_mount;
3962 int whichfork = xfs_bmapi_whichfork(flags);
3963 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3964 struct xfs_bmbt_irec got;
3967 struct xfs_iext_cursor icur;
3973 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3974 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3976 if (WARN_ON_ONCE(!ifp))
3977 return -EFSCORRUPTED;
3979 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3980 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
3981 return -EFSCORRUPTED;
3983 if (XFS_FORCED_SHUTDOWN(mp))
3986 XFS_STATS_INC(mp, xs_blk_mapr);
3988 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3989 error = xfs_iread_extents(NULL, ip, whichfork);
3994 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3999 while (bno < end && n < *nmap) {
4000 /* Reading past eof, act as though there's a hole up to end. */
4002 got.br_startoff = end;
4003 if (got.br_startoff > bno) {
4004 /* Reading in a hole. */
4005 mval->br_startoff = bno;
4006 mval->br_startblock = HOLESTARTBLOCK;
4007 mval->br_blockcount =
4008 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4009 mval->br_state = XFS_EXT_NORM;
4010 bno += mval->br_blockcount;
4011 len -= mval->br_blockcount;
4017 /* set up the extent map to return. */
4018 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4019 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4021 /* If we're done, stop now. */
4022 if (bno >= end || n >= *nmap)
4025 /* Else go on to the next record. */
4026 if (!xfs_iext_next_extent(ifp, &icur, &got))
4034 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4035 * global pool and the extent inserted into the inode in-core extent tree.
4037 * On entry, got refers to the first extent beyond the offset of the extent to
4038 * allocate or eof is specified if no such extent exists. On return, got refers
4039 * to the extent record that was inserted to the inode fork.
4041 * Note that the allocated extent may have been merged with contiguous extents
4042 * during insertion into the inode fork. Thus, got does not reflect the current
4043 * state of the inode fork on return. If necessary, the caller can use lastx to
4044 * look up the updated record in the inode fork.
4047 xfs_bmapi_reserve_delalloc(
4048 struct xfs_inode *ip,
4052 xfs_filblks_t prealloc,
4053 struct xfs_bmbt_irec *got,
4054 struct xfs_iext_cursor *icur,
4057 struct xfs_mount *mp = ip->i_mount;
4058 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4060 xfs_extlen_t indlen;
4062 xfs_fileoff_t aoff = off;
4065 * Cap the alloc length. Keep track of prealloc so we know whether to
4066 * tag the inode before we return.
4068 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
4070 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4071 if (prealloc && alen >= len)
4072 prealloc = alen - len;
4074 /* Figure out the extent size, adjust alen */
4075 if (whichfork == XFS_COW_FORK) {
4076 struct xfs_bmbt_irec prev;
4077 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
4079 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4080 prev.br_startoff = NULLFILEOFF;
4082 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4083 1, 0, &aoff, &alen);
4088 * Make a transaction-less quota reservation for delayed allocation
4089 * blocks. This number gets adjusted later. We return if we haven't
4090 * allocated blocks already inside this loop.
4092 error = xfs_quota_reserve_blkres(ip, alen);
4097 * Split changing sb for alen and indlen since they could be coming
4098 * from different places.
4100 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4103 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4105 goto out_unreserve_quota;
4107 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4109 goto out_unreserve_blocks;
4112 ip->i_delayed_blks += alen;
4113 xfs_mod_delalloc(ip->i_mount, alen + indlen);
4115 got->br_startoff = aoff;
4116 got->br_startblock = nullstartblock(indlen);
4117 got->br_blockcount = alen;
4118 got->br_state = XFS_EXT_NORM;
4120 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4123 * Tag the inode if blocks were preallocated. Note that COW fork
4124 * preallocation can occur at the start or end of the extent, even when
4125 * prealloc == 0, so we must also check the aligned offset and length.
4127 if (whichfork == XFS_DATA_FORK && prealloc)
4128 xfs_inode_set_eofblocks_tag(ip);
4129 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4130 xfs_inode_set_cowblocks_tag(ip);
4134 out_unreserve_blocks:
4135 xfs_mod_fdblocks(mp, alen, false);
4136 out_unreserve_quota:
4137 if (XFS_IS_QUOTA_ON(mp))
4138 xfs_quota_unreserve_blkres(ip, alen);
4143 xfs_bmap_alloc_userdata(
4144 struct xfs_bmalloca *bma)
4146 struct xfs_mount *mp = bma->ip->i_mount;
4147 int whichfork = xfs_bmapi_whichfork(bma->flags);
4151 * Set the data type being allocated. For the data fork, the first data
4152 * in the file is treated differently to all other allocations. For the
4153 * attribute fork, we only need to ensure the allocated range is not on
4156 bma->datatype = XFS_ALLOC_NOBUSY;
4157 if (whichfork == XFS_DATA_FORK) {
4158 bma->datatype |= XFS_ALLOC_USERDATA;
4159 if (bma->offset == 0)
4160 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4162 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4163 error = xfs_bmap_isaeof(bma, whichfork);
4168 if (XFS_IS_REALTIME_INODE(bma->ip))
4169 return xfs_bmap_rtalloc(bma);
4172 if (unlikely(XFS_TEST_ERROR(false, mp,
4173 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4174 return xfs_bmap_exact_minlen_extent_alloc(bma);
4176 return xfs_bmap_btalloc(bma);
4181 struct xfs_bmalloca *bma)
4183 struct xfs_mount *mp = bma->ip->i_mount;
4184 int whichfork = xfs_bmapi_whichfork(bma->flags);
4185 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4186 int tmp_logflags = 0;
4189 ASSERT(bma->length > 0);
4192 * For the wasdelay case, we could also just allocate the stuff asked
4193 * for in this bmap call but that wouldn't be as good.
4196 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4197 bma->offset = bma->got.br_startoff;
4198 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4199 bma->prev.br_startoff = NULLFILEOFF;
4201 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4203 bma->length = XFS_FILBLKS_MIN(bma->length,
4204 bma->got.br_startoff - bma->offset);
4207 if (bma->flags & XFS_BMAPI_CONTIG)
4208 bma->minlen = bma->length;
4212 if (bma->flags & XFS_BMAPI_METADATA) {
4213 if (unlikely(XFS_TEST_ERROR(false, mp,
4214 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4215 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4217 error = xfs_bmap_btalloc(bma);
4219 error = xfs_bmap_alloc_userdata(bma);
4221 if (error || bma->blkno == NULLFSBLOCK)
4224 if (bma->flags & XFS_BMAPI_ZERO) {
4225 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4230 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
4231 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4233 * Bump the number of extents we've allocated
4239 bma->cur->bc_ino.flags =
4240 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4242 bma->got.br_startoff = bma->offset;
4243 bma->got.br_startblock = bma->blkno;
4244 bma->got.br_blockcount = bma->length;
4245 bma->got.br_state = XFS_EXT_NORM;
4247 if (bma->flags & XFS_BMAPI_PREALLOC)
4248 bma->got.br_state = XFS_EXT_UNWRITTEN;
4251 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4253 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4254 whichfork, &bma->icur, &bma->cur, &bma->got,
4255 &bma->logflags, bma->flags);
4257 bma->logflags |= tmp_logflags;
4262 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4263 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4264 * the neighbouring ones.
4266 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4268 ASSERT(bma->got.br_startoff <= bma->offset);
4269 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4270 bma->offset + bma->length);
4271 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4272 bma->got.br_state == XFS_EXT_UNWRITTEN);
4277 xfs_bmapi_convert_unwritten(
4278 struct xfs_bmalloca *bma,
4279 struct xfs_bmbt_irec *mval,
4283 int whichfork = xfs_bmapi_whichfork(flags);
4284 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4285 int tmp_logflags = 0;
4288 /* check if we need to do unwritten->real conversion */
4289 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4290 (flags & XFS_BMAPI_PREALLOC))
4293 /* check if we need to do real->unwritten conversion */
4294 if (mval->br_state == XFS_EXT_NORM &&
4295 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4296 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4300 * Modify (by adding) the state flag, if writing.
4302 ASSERT(mval->br_blockcount <= len);
4303 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4304 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4305 bma->ip, whichfork);
4307 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4308 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4311 * Before insertion into the bmbt, zero the range being converted
4314 if (flags & XFS_BMAPI_ZERO) {
4315 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4316 mval->br_blockcount);
4321 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4322 &bma->icur, &bma->cur, mval, &tmp_logflags);
4324 * Log the inode core unconditionally in the unwritten extent conversion
4325 * path because the conversion might not have done so (e.g., if the
4326 * extent count hasn't changed). We need to make sure the inode is dirty
4327 * in the transaction for the sake of fsync(), even if nothing has
4328 * changed, because fsync() will not force the log for this transaction
4329 * unless it sees the inode pinned.
4331 * Note: If we're only converting cow fork extents, there aren't
4332 * any on-disk updates to make, so we don't need to log anything.
4334 if (whichfork != XFS_COW_FORK)
4335 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4340 * Update our extent pointer, given that
4341 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4342 * of the neighbouring ones.
4344 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4347 * We may have combined previously unwritten space with written space,
4348 * so generate another request.
4350 if (mval->br_blockcount < len)
4355 static inline xfs_extlen_t
4357 struct xfs_trans *tp,
4358 struct xfs_inode *ip,
4361 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork);
4363 if (tp && tp->t_firstblock != NULLFSBLOCK)
4365 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4367 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4371 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4372 * a case where the data is changed, there's an error, and it's not logged so we
4373 * don't shutdown when we should. Don't bother logging extents/btree changes if
4374 * we converted to the other format.
4378 struct xfs_bmalloca *bma,
4382 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4384 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4385 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4386 bma->logflags &= ~xfs_ilog_fext(whichfork);
4387 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4388 ifp->if_format != XFS_DINODE_FMT_BTREE)
4389 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4392 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4394 xfs_btree_del_cursor(bma->cur, error);
4398 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4399 * extent state if necessary. Details behaviour is controlled by the flags
4400 * parameter. Only allocates blocks from a single allocation group, to avoid
4405 struct xfs_trans *tp, /* transaction pointer */
4406 struct xfs_inode *ip, /* incore inode */
4407 xfs_fileoff_t bno, /* starting file offs. mapped */
4408 xfs_filblks_t len, /* length to map in file */
4409 int flags, /* XFS_BMAPI_... */
4410 xfs_extlen_t total, /* total blocks needed */
4411 struct xfs_bmbt_irec *mval, /* output: map values */
4412 int *nmap) /* i/o: mval size/count */
4414 struct xfs_bmalloca bma = {
4419 struct xfs_mount *mp = ip->i_mount;
4420 int whichfork = xfs_bmapi_whichfork(flags);
4421 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4422 xfs_fileoff_t end; /* end of mapped file region */
4423 bool eof = false; /* after the end of extents */
4424 int error; /* error return */
4425 int n; /* current extent index */
4426 xfs_fileoff_t obno; /* old block number (offset) */
4429 xfs_fileoff_t orig_bno; /* original block number value */
4430 int orig_flags; /* original flags arg value */
4431 xfs_filblks_t orig_len; /* original value of len arg */
4432 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4433 int orig_nmap; /* original value of *nmap */
4443 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4446 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4447 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4448 ASSERT(!(flags & XFS_BMAPI_REMAP));
4450 /* zeroing is for currently only for data extents, not metadata */
4451 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4452 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4454 * we can allocate unwritten extents or pre-zero allocated blocks,
4455 * but it makes no sense to do both at once. This would result in
4456 * zeroing the unwritten extent twice, but it still being an
4457 * unwritten extent....
4459 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4460 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4462 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4463 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4464 return -EFSCORRUPTED;
4467 if (XFS_FORCED_SHUTDOWN(mp))
4470 XFS_STATS_INC(mp, xs_blk_mapw);
4472 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4473 error = xfs_iread_extents(tp, ip, whichfork);
4478 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4480 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4481 bma.prev.br_startoff = NULLFILEOFF;
4482 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4487 while (bno < end && n < *nmap) {
4488 bool need_alloc = false, wasdelay = false;
4490 /* in hole or beyond EOF? */
4491 if (eof || bma.got.br_startoff > bno) {
4493 * CoW fork conversions should /never/ hit EOF or
4494 * holes. There should always be something for us
4497 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4498 (flags & XFS_BMAPI_COWFORK)));
4501 } else if (isnullstartblock(bma.got.br_startblock)) {
4506 * First, deal with the hole before the allocated space
4507 * that we found, if any.
4509 if (need_alloc || wasdelay) {
4511 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4512 bma.wasdel = wasdelay;
4517 * There's a 32/64 bit type mismatch between the
4518 * allocation length request (which can be 64 bits in
4519 * length) and the bma length request, which is
4520 * xfs_extlen_t and therefore 32 bits. Hence we have to
4521 * check for 32-bit overflows and handle them here.
4523 if (len > (xfs_filblks_t)MAXEXTLEN)
4524 bma.length = MAXEXTLEN;
4529 ASSERT(bma.length > 0);
4530 error = xfs_bmapi_allocate(&bma);
4533 if (bma.blkno == NULLFSBLOCK)
4537 * If this is a CoW allocation, record the data in
4538 * the refcount btree for orphan recovery.
4540 if (whichfork == XFS_COW_FORK)
4541 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4545 /* Deal with the allocated space we found. */
4546 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4549 /* Execute unwritten extent conversion if necessary */
4550 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4551 if (error == -EAGAIN)
4556 /* update the extent map to return */
4557 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4560 * If we're done, stop now. Stop when we've allocated
4561 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4562 * the transaction may get too big.
4564 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4567 /* Else go on to the next record. */
4569 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4574 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4579 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4580 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4581 xfs_bmapi_finish(&bma, whichfork, 0);
4582 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4586 xfs_bmapi_finish(&bma, whichfork, error);
4591 * Convert an existing delalloc extent to real blocks based on file offset. This
4592 * attempts to allocate the entire delalloc extent and may require multiple
4593 * invocations to allocate the target offset if a large enough physical extent
4597 xfs_bmapi_convert_delalloc(
4598 struct xfs_inode *ip,
4601 struct iomap *iomap,
4604 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4605 struct xfs_mount *mp = ip->i_mount;
4606 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4607 struct xfs_bmalloca bma = { NULL };
4609 struct xfs_trans *tp;
4612 if (whichfork == XFS_COW_FORK)
4613 flags |= IOMAP_F_SHARED;
4616 * Space for the extent and indirect blocks was reserved when the
4617 * delalloc extent was created so there's no need to do so here.
4619 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4620 XFS_TRANS_RESERVE, &tp);
4624 xfs_ilock(ip, XFS_ILOCK_EXCL);
4626 error = xfs_iext_count_may_overflow(ip, whichfork,
4627 XFS_IEXT_ADD_NOSPLIT_CNT);
4629 goto out_trans_cancel;
4631 xfs_trans_ijoin(tp, ip, 0);
4633 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4634 bma.got.br_startoff > offset_fsb) {
4636 * No extent found in the range we are trying to convert. This
4637 * should only happen for the COW fork, where another thread
4638 * might have moved the extent to the data fork in the meantime.
4640 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4642 goto out_trans_cancel;
4646 * If we find a real extent here we raced with another thread converting
4647 * the extent. Just return the real extent at this offset.
4649 if (!isnullstartblock(bma.got.br_startblock)) {
4650 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
4651 *seq = READ_ONCE(ifp->if_seq);
4652 goto out_trans_cancel;
4658 bma.offset = bma.got.br_startoff;
4659 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4660 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4663 * When we're converting the delalloc reservations backing dirty pages
4664 * in the page cache, we must be careful about how we create the new
4667 * New CoW fork extents are created unwritten, turned into real extents
4668 * when we're about to write the data to disk, and mapped into the data
4669 * fork after the write finishes. End of story.
4671 * New data fork extents must be mapped in as unwritten and converted
4672 * to real extents after the write succeeds to avoid exposing stale
4673 * disk contents if we crash.
4675 bma.flags = XFS_BMAPI_PREALLOC;
4676 if (whichfork == XFS_COW_FORK)
4677 bma.flags |= XFS_BMAPI_COWFORK;
4679 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4680 bma.prev.br_startoff = NULLFILEOFF;
4682 error = xfs_bmapi_allocate(&bma);
4687 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4689 error = -EFSCORRUPTED;
4690 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
4693 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4694 XFS_STATS_INC(mp, xs_xstrat_quick);
4696 ASSERT(!isnullstartblock(bma.got.br_startblock));
4697 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
4698 *seq = READ_ONCE(ifp->if_seq);
4700 if (whichfork == XFS_COW_FORK)
4701 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4703 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4708 xfs_bmapi_finish(&bma, whichfork, 0);
4709 error = xfs_trans_commit(tp);
4710 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4714 xfs_bmapi_finish(&bma, whichfork, error);
4716 xfs_trans_cancel(tp);
4717 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4723 struct xfs_trans *tp,
4724 struct xfs_inode *ip,
4727 xfs_fsblock_t startblock,
4730 struct xfs_mount *mp = ip->i_mount;
4731 struct xfs_ifork *ifp;
4732 struct xfs_btree_cur *cur = NULL;
4733 struct xfs_bmbt_irec got;
4734 struct xfs_iext_cursor icur;
4735 int whichfork = xfs_bmapi_whichfork(flags);
4736 int logflags = 0, error;
4738 ifp = XFS_IFORK_PTR(ip, whichfork);
4740 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4741 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4742 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4743 XFS_BMAPI_NORMAP)));
4744 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4745 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4747 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4748 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4749 return -EFSCORRUPTED;
4752 if (XFS_FORCED_SHUTDOWN(mp))
4755 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4756 error = xfs_iread_extents(tp, ip, whichfork);
4761 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4762 /* make sure we only reflink into a hole. */
4763 ASSERT(got.br_startoff > bno);
4764 ASSERT(got.br_startoff - bno >= len);
4767 ip->i_nblocks += len;
4768 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4770 if (ifp->if_flags & XFS_IFBROOT) {
4771 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4772 cur->bc_ino.flags = 0;
4775 got.br_startoff = bno;
4776 got.br_startblock = startblock;
4777 got.br_blockcount = len;
4778 if (flags & XFS_BMAPI_PREALLOC)
4779 got.br_state = XFS_EXT_UNWRITTEN;
4781 got.br_state = XFS_EXT_NORM;
4783 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4784 &cur, &got, &logflags, flags);
4788 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4791 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4792 logflags &= ~XFS_ILOG_DEXT;
4793 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4794 logflags &= ~XFS_ILOG_DBROOT;
4797 xfs_trans_log_inode(tp, ip, logflags);
4799 xfs_btree_del_cursor(cur, error);
4804 * When a delalloc extent is split (e.g., due to a hole punch), the original
4805 * indlen reservation must be shared across the two new extents that are left
4808 * Given the original reservation and the worst case indlen for the two new
4809 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4810 * reservation fairly across the two new extents. If necessary, steal available
4811 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4812 * ores == 1). The number of stolen blocks is returned. The availability and
4813 * subsequent accounting of stolen blocks is the responsibility of the caller.
4815 static xfs_filblks_t
4816 xfs_bmap_split_indlen(
4817 xfs_filblks_t ores, /* original res. */
4818 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4819 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4820 xfs_filblks_t avail) /* stealable blocks */
4822 xfs_filblks_t len1 = *indlen1;
4823 xfs_filblks_t len2 = *indlen2;
4824 xfs_filblks_t nres = len1 + len2; /* new total res. */
4825 xfs_filblks_t stolen = 0;
4826 xfs_filblks_t resfactor;
4829 * Steal as many blocks as we can to try and satisfy the worst case
4830 * indlen for both new extents.
4832 if (ores < nres && avail)
4833 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4836 /* nothing else to do if we've satisfied the new reservation */
4841 * We can't meet the total required reservation for the two extents.
4842 * Calculate the percent of the overall shortage between both extents
4843 * and apply this percentage to each of the requested indlen values.
4844 * This distributes the shortage fairly and reduces the chances that one
4845 * of the two extents is left with nothing when extents are repeatedly
4848 resfactor = (ores * 100);
4849 do_div(resfactor, nres);
4854 ASSERT(len1 + len2 <= ores);
4855 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4858 * Hand out the remainder to each extent. If one of the two reservations
4859 * is zero, we want to make sure that one gets a block first. The loop
4860 * below starts with len1, so hand len2 a block right off the bat if it
4863 ores -= (len1 + len2);
4864 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4865 if (ores && !len2 && *indlen2) {
4870 if (len1 < *indlen1) {
4876 if (len2 < *indlen2) {
4889 xfs_bmap_del_extent_delay(
4890 struct xfs_inode *ip,
4892 struct xfs_iext_cursor *icur,
4893 struct xfs_bmbt_irec *got,
4894 struct xfs_bmbt_irec *del)
4896 struct xfs_mount *mp = ip->i_mount;
4897 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4898 struct xfs_bmbt_irec new;
4899 int64_t da_old, da_new, da_diff = 0;
4900 xfs_fileoff_t del_endoff, got_endoff;
4901 xfs_filblks_t got_indlen, new_indlen, stolen;
4902 int state = xfs_bmap_fork_to_state(whichfork);
4906 XFS_STATS_INC(mp, xs_del_exlist);
4908 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4909 del_endoff = del->br_startoff + del->br_blockcount;
4910 got_endoff = got->br_startoff + got->br_blockcount;
4911 da_old = startblockval(got->br_startblock);
4914 ASSERT(del->br_blockcount > 0);
4915 ASSERT(got->br_startoff <= del->br_startoff);
4916 ASSERT(got_endoff >= del_endoff);
4919 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4921 do_div(rtexts, mp->m_sb.sb_rextsize);
4922 xfs_mod_frextents(mp, rtexts);
4926 * Update the inode delalloc counter now and wait to update the
4927 * sb counters as we might have to borrow some blocks for the
4928 * indirect block accounting.
4931 error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4934 ip->i_delayed_blks -= del->br_blockcount;
4936 if (got->br_startoff == del->br_startoff)
4937 state |= BMAP_LEFT_FILLING;
4938 if (got_endoff == del_endoff)
4939 state |= BMAP_RIGHT_FILLING;
4941 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4942 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4944 * Matches the whole extent. Delete the entry.
4946 xfs_iext_remove(ip, icur, state);
4947 xfs_iext_prev(ifp, icur);
4949 case BMAP_LEFT_FILLING:
4951 * Deleting the first part of the extent.
4953 got->br_startoff = del_endoff;
4954 got->br_blockcount -= del->br_blockcount;
4955 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4956 got->br_blockcount), da_old);
4957 got->br_startblock = nullstartblock((int)da_new);
4958 xfs_iext_update_extent(ip, state, icur, got);
4960 case BMAP_RIGHT_FILLING:
4962 * Deleting the last part of the extent.
4964 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4965 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4966 got->br_blockcount), da_old);
4967 got->br_startblock = nullstartblock((int)da_new);
4968 xfs_iext_update_extent(ip, state, icur, got);
4972 * Deleting the middle of the extent.
4974 * Distribute the original indlen reservation across the two new
4975 * extents. Steal blocks from the deleted extent if necessary.
4976 * Stealing blocks simply fudges the fdblocks accounting below.
4977 * Warn if either of the new indlen reservations is zero as this
4978 * can lead to delalloc problems.
4980 got->br_blockcount = del->br_startoff - got->br_startoff;
4981 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4983 new.br_blockcount = got_endoff - del_endoff;
4984 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4986 WARN_ON_ONCE(!got_indlen || !new_indlen);
4987 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4988 del->br_blockcount);
4990 got->br_startblock = nullstartblock((int)got_indlen);
4992 new.br_startoff = del_endoff;
4993 new.br_state = got->br_state;
4994 new.br_startblock = nullstartblock((int)new_indlen);
4996 xfs_iext_update_extent(ip, state, icur, got);
4997 xfs_iext_next(ifp, icur);
4998 xfs_iext_insert(ip, icur, &new, state);
5000 da_new = got_indlen + new_indlen - stolen;
5001 del->br_blockcount -= stolen;
5005 ASSERT(da_old >= da_new);
5006 da_diff = da_old - da_new;
5008 da_diff += del->br_blockcount;
5010 xfs_mod_fdblocks(mp, da_diff, false);
5011 xfs_mod_delalloc(mp, -da_diff);
5017 xfs_bmap_del_extent_cow(
5018 struct xfs_inode *ip,
5019 struct xfs_iext_cursor *icur,
5020 struct xfs_bmbt_irec *got,
5021 struct xfs_bmbt_irec *del)
5023 struct xfs_mount *mp = ip->i_mount;
5024 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
5025 struct xfs_bmbt_irec new;
5026 xfs_fileoff_t del_endoff, got_endoff;
5027 int state = BMAP_COWFORK;
5029 XFS_STATS_INC(mp, xs_del_exlist);
5031 del_endoff = del->br_startoff + del->br_blockcount;
5032 got_endoff = got->br_startoff + got->br_blockcount;
5034 ASSERT(del->br_blockcount > 0);
5035 ASSERT(got->br_startoff <= del->br_startoff);
5036 ASSERT(got_endoff >= del_endoff);
5037 ASSERT(!isnullstartblock(got->br_startblock));
5039 if (got->br_startoff == del->br_startoff)
5040 state |= BMAP_LEFT_FILLING;
5041 if (got_endoff == del_endoff)
5042 state |= BMAP_RIGHT_FILLING;
5044 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5045 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5047 * Matches the whole extent. Delete the entry.
5049 xfs_iext_remove(ip, icur, state);
5050 xfs_iext_prev(ifp, icur);
5052 case BMAP_LEFT_FILLING:
5054 * Deleting the first part of the extent.
5056 got->br_startoff = del_endoff;
5057 got->br_blockcount -= del->br_blockcount;
5058 got->br_startblock = del->br_startblock + del->br_blockcount;
5059 xfs_iext_update_extent(ip, state, icur, got);
5061 case BMAP_RIGHT_FILLING:
5063 * Deleting the last part of the extent.
5065 got->br_blockcount -= del->br_blockcount;
5066 xfs_iext_update_extent(ip, state, icur, got);
5070 * Deleting the middle of the extent.
5072 got->br_blockcount = del->br_startoff - got->br_startoff;
5074 new.br_startoff = del_endoff;
5075 new.br_blockcount = got_endoff - del_endoff;
5076 new.br_state = got->br_state;
5077 new.br_startblock = del->br_startblock + del->br_blockcount;
5079 xfs_iext_update_extent(ip, state, icur, got);
5080 xfs_iext_next(ifp, icur);
5081 xfs_iext_insert(ip, icur, &new, state);
5084 ip->i_delayed_blks -= del->br_blockcount;
5088 * Called by xfs_bmapi to update file extent records and the btree
5089 * after removing space.
5091 STATIC int /* error */
5092 xfs_bmap_del_extent_real(
5093 xfs_inode_t *ip, /* incore inode pointer */
5094 xfs_trans_t *tp, /* current transaction pointer */
5095 struct xfs_iext_cursor *icur,
5096 xfs_btree_cur_t *cur, /* if null, not a btree */
5097 xfs_bmbt_irec_t *del, /* data to remove from extents */
5098 int *logflagsp, /* inode logging flags */
5099 int whichfork, /* data or attr fork */
5100 int bflags) /* bmapi flags */
5102 xfs_fsblock_t del_endblock=0; /* first block past del */
5103 xfs_fileoff_t del_endoff; /* first offset past del */
5104 int do_fx; /* free extent at end of routine */
5105 int error; /* error return value */
5106 int flags = 0;/* inode logging flags */
5107 struct xfs_bmbt_irec got; /* current extent entry */
5108 xfs_fileoff_t got_endoff; /* first offset past got */
5109 int i; /* temp state */
5110 struct xfs_ifork *ifp; /* inode fork pointer */
5111 xfs_mount_t *mp; /* mount structure */
5112 xfs_filblks_t nblks; /* quota/sb block count */
5113 xfs_bmbt_irec_t new; /* new record to be inserted */
5115 uint qfield; /* quota field to update */
5116 int state = xfs_bmap_fork_to_state(whichfork);
5117 struct xfs_bmbt_irec old;
5120 XFS_STATS_INC(mp, xs_del_exlist);
5122 ifp = XFS_IFORK_PTR(ip, whichfork);
5123 ASSERT(del->br_blockcount > 0);
5124 xfs_iext_get_extent(ifp, icur, &got);
5125 ASSERT(got.br_startoff <= del->br_startoff);
5126 del_endoff = del->br_startoff + del->br_blockcount;
5127 got_endoff = got.br_startoff + got.br_blockcount;
5128 ASSERT(got_endoff >= del_endoff);
5129 ASSERT(!isnullstartblock(got.br_startblock));
5134 * If it's the case where the directory code is running with no block
5135 * reservation, and the deleted block is in the middle of its extent,
5136 * and the resulting insert of an extent would cause transformation to
5137 * btree format, then reject it. The calling code will then swap blocks
5138 * around instead. We have to do this now, rather than waiting for the
5139 * conversion to btree format, since the transaction will be dirty then.
5141 if (tp->t_blk_res == 0 &&
5142 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5143 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5144 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5147 flags = XFS_ILOG_CORE;
5148 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5152 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5156 if (!(bflags & XFS_BMAPI_REMAP)) {
5159 bno = div_u64_rem(del->br_startblock,
5160 mp->m_sb.sb_rextsize, &mod);
5163 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5169 nblks = len * mp->m_sb.sb_rextsize;
5170 qfield = XFS_TRANS_DQ_RTBCOUNT;
5173 nblks = del->br_blockcount;
5174 qfield = XFS_TRANS_DQ_BCOUNT;
5177 del_endblock = del->br_startblock + del->br_blockcount;
5179 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5182 if (XFS_IS_CORRUPT(mp, i != 1)) {
5183 error = -EFSCORRUPTED;
5188 if (got.br_startoff == del->br_startoff)
5189 state |= BMAP_LEFT_FILLING;
5190 if (got_endoff == del_endoff)
5191 state |= BMAP_RIGHT_FILLING;
5193 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5194 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5196 * Matches the whole extent. Delete the entry.
5198 xfs_iext_remove(ip, icur, state);
5199 xfs_iext_prev(ifp, icur);
5202 flags |= XFS_ILOG_CORE;
5204 flags |= xfs_ilog_fext(whichfork);
5207 if ((error = xfs_btree_delete(cur, &i)))
5209 if (XFS_IS_CORRUPT(mp, i != 1)) {
5210 error = -EFSCORRUPTED;
5214 case BMAP_LEFT_FILLING:
5216 * Deleting the first part of the extent.
5218 got.br_startoff = del_endoff;
5219 got.br_startblock = del_endblock;
5220 got.br_blockcount -= del->br_blockcount;
5221 xfs_iext_update_extent(ip, state, icur, &got);
5223 flags |= xfs_ilog_fext(whichfork);
5226 error = xfs_bmbt_update(cur, &got);
5230 case BMAP_RIGHT_FILLING:
5232 * Deleting the last part of the extent.
5234 got.br_blockcount -= del->br_blockcount;
5235 xfs_iext_update_extent(ip, state, icur, &got);
5237 flags |= xfs_ilog_fext(whichfork);
5240 error = xfs_bmbt_update(cur, &got);
5246 * Deleting the middle of the extent.
5250 * For directories, -ENOSPC is returned since a directory entry
5251 * remove operation must not fail due to low extent count
5252 * availability. -ENOSPC will be handled by higher layers of XFS
5253 * by letting the corresponding empty Data/Free blocks to linger
5254 * until a future remove operation. Dabtree blocks would be
5255 * swapped with the last block in the leaf space and then the
5256 * new last block will be unmapped.
5258 * The above logic also applies to the source directory entry of
5259 * a rename operation.
5261 error = xfs_iext_count_may_overflow(ip, whichfork, 1);
5263 ASSERT(S_ISDIR(VFS_I(ip)->i_mode) &&
5264 whichfork == XFS_DATA_FORK);
5271 got.br_blockcount = del->br_startoff - got.br_startoff;
5272 xfs_iext_update_extent(ip, state, icur, &got);
5274 new.br_startoff = del_endoff;
5275 new.br_blockcount = got_endoff - del_endoff;
5276 new.br_state = got.br_state;
5277 new.br_startblock = del_endblock;
5279 flags |= XFS_ILOG_CORE;
5281 error = xfs_bmbt_update(cur, &got);
5284 error = xfs_btree_increment(cur, 0, &i);
5287 cur->bc_rec.b = new;
5288 error = xfs_btree_insert(cur, &i);
5289 if (error && error != -ENOSPC)
5292 * If get no-space back from btree insert, it tried a
5293 * split, and we have a zero block reservation. Fix up
5294 * our state and return the error.
5296 if (error == -ENOSPC) {
5298 * Reset the cursor, don't trust it after any
5301 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5304 if (XFS_IS_CORRUPT(mp, i != 1)) {
5305 error = -EFSCORRUPTED;
5309 * Update the btree record back
5310 * to the original value.
5312 error = xfs_bmbt_update(cur, &old);
5316 * Reset the extent record back
5317 * to the original value.
5319 xfs_iext_update_extent(ip, state, icur, &old);
5324 if (XFS_IS_CORRUPT(mp, i != 1)) {
5325 error = -EFSCORRUPTED;
5329 flags |= xfs_ilog_fext(whichfork);
5332 xfs_iext_next(ifp, icur);
5333 xfs_iext_insert(ip, icur, &new, state);
5337 /* remove reverse mapping */
5338 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5341 * If we need to, add to list of extents to delete.
5343 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5344 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5345 xfs_refcount_decrease_extent(tp, del);
5347 __xfs_bmap_add_free(tp, del->br_startblock,
5348 del->br_blockcount, NULL,
5349 (bflags & XFS_BMAPI_NODISCARD) ||
5350 del->br_state == XFS_EXT_UNWRITTEN);
5355 * Adjust inode # blocks in the file.
5358 ip->i_nblocks -= nblks;
5360 * Adjust quota data.
5362 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5363 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5371 * Unmap (remove) blocks from a file.
5372 * If nexts is nonzero then the number of extents to remove is limited to
5373 * that value. If not all extents in the block range can be removed then
5378 struct xfs_trans *tp, /* transaction pointer */
5379 struct xfs_inode *ip, /* incore inode */
5380 xfs_fileoff_t start, /* first file offset deleted */
5381 xfs_filblks_t *rlen, /* i/o: amount remaining */
5382 int flags, /* misc flags */
5383 xfs_extnum_t nexts) /* number of extents max */
5385 struct xfs_btree_cur *cur; /* bmap btree cursor */
5386 struct xfs_bmbt_irec del; /* extent being deleted */
5387 int error; /* error return value */
5388 xfs_extnum_t extno; /* extent number in list */
5389 struct xfs_bmbt_irec got; /* current extent record */
5390 struct xfs_ifork *ifp; /* inode fork pointer */
5391 int isrt; /* freeing in rt area */
5392 int logflags; /* transaction logging flags */
5393 xfs_extlen_t mod; /* rt extent offset */
5394 struct xfs_mount *mp = ip->i_mount;
5395 int tmp_logflags; /* partial logging flags */
5396 int wasdel; /* was a delayed alloc extent */
5397 int whichfork; /* data or attribute fork */
5399 xfs_filblks_t len = *rlen; /* length to unmap in file */
5400 xfs_fileoff_t max_len;
5401 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5403 struct xfs_iext_cursor icur;
5406 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5408 whichfork = xfs_bmapi_whichfork(flags);
5409 ASSERT(whichfork != XFS_COW_FORK);
5410 ifp = XFS_IFORK_PTR(ip, whichfork);
5411 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
5412 return -EFSCORRUPTED;
5413 if (XFS_FORCED_SHUTDOWN(mp))
5416 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5421 * Guesstimate how many blocks we can unmap without running the risk of
5422 * blowing out the transaction with a mix of EFIs and reflink
5425 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5426 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5430 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5431 (error = xfs_iread_extents(tp, ip, whichfork)))
5433 if (xfs_iext_count(ifp) == 0) {
5437 XFS_STATS_INC(mp, xs_blk_unmap);
5438 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5441 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5448 if (ifp->if_flags & XFS_IFBROOT) {
5449 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5450 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5451 cur->bc_ino.flags = 0;
5457 * Synchronize by locking the bitmap inode.
5459 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5460 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5461 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5462 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5466 while (end != (xfs_fileoff_t)-1 && end >= start &&
5467 (nexts == 0 || extno < nexts) && max_len > 0) {
5469 * Is the found extent after a hole in which end lives?
5470 * Just back up to the previous extent, if so.
5472 if (got.br_startoff > end &&
5473 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5478 * Is the last block of this extent before the range
5479 * we're supposed to delete? If so, we're done.
5481 end = XFS_FILEOFF_MIN(end,
5482 got.br_startoff + got.br_blockcount - 1);
5486 * Then deal with the (possibly delayed) allocated space
5490 wasdel = isnullstartblock(del.br_startblock);
5493 * Make sure we don't touch multiple AGF headers out of order
5494 * in a single transaction, as that could cause AB-BA deadlocks.
5496 if (!wasdel && !isrt) {
5497 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5498 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5502 if (got.br_startoff < start) {
5503 del.br_startoff = start;
5504 del.br_blockcount -= start - got.br_startoff;
5506 del.br_startblock += start - got.br_startoff;
5508 if (del.br_startoff + del.br_blockcount > end + 1)
5509 del.br_blockcount = end + 1 - del.br_startoff;
5511 /* How much can we safely unmap? */
5512 if (max_len < del.br_blockcount) {
5513 del.br_startoff += del.br_blockcount - max_len;
5515 del.br_startblock += del.br_blockcount - max_len;
5516 del.br_blockcount = max_len;
5522 sum = del.br_startblock + del.br_blockcount;
5523 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5526 * Realtime extent not lined up at the end.
5527 * The extent could have been split into written
5528 * and unwritten pieces, or we could just be
5529 * unmapping part of it. But we can't really
5530 * get rid of part of a realtime extent.
5532 if (del.br_state == XFS_EXT_UNWRITTEN) {
5534 * This piece is unwritten, or we're not
5535 * using unwritten extents. Skip over it.
5538 end -= mod > del.br_blockcount ?
5539 del.br_blockcount : mod;
5540 if (end < got.br_startoff &&
5541 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5548 * It's written, turn it unwritten.
5549 * This is better than zeroing it.
5551 ASSERT(del.br_state == XFS_EXT_NORM);
5552 ASSERT(tp->t_blk_res > 0);
5554 * If this spans a realtime extent boundary,
5555 * chop it back to the start of the one we end at.
5557 if (del.br_blockcount > mod) {
5558 del.br_startoff += del.br_blockcount - mod;
5559 del.br_startblock += del.br_blockcount - mod;
5560 del.br_blockcount = mod;
5562 del.br_state = XFS_EXT_UNWRITTEN;
5563 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5564 whichfork, &icur, &cur, &del,
5570 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5572 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5575 * Realtime extent is lined up at the end but not
5576 * at the front. We'll get rid of full extents if
5579 if (del.br_blockcount > off) {
5580 del.br_blockcount -= off;
5581 del.br_startoff += off;
5582 del.br_startblock += off;
5583 } else if (del.br_startoff == start &&
5584 (del.br_state == XFS_EXT_UNWRITTEN ||
5585 tp->t_blk_res == 0)) {
5587 * Can't make it unwritten. There isn't
5588 * a full extent here so just skip it.
5590 ASSERT(end >= del.br_blockcount);
5591 end -= del.br_blockcount;
5592 if (got.br_startoff > end &&
5593 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5598 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5599 struct xfs_bmbt_irec prev;
5600 xfs_fileoff_t unwrite_start;
5603 * This one is already unwritten.
5604 * It must have a written left neighbor.
5605 * Unwrite the killed part of that one and
5608 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5610 ASSERT(prev.br_state == XFS_EXT_NORM);
5611 ASSERT(!isnullstartblock(prev.br_startblock));
5612 ASSERT(del.br_startblock ==
5613 prev.br_startblock + prev.br_blockcount);
5614 unwrite_start = max3(start,
5615 del.br_startoff - mod,
5617 mod = unwrite_start - prev.br_startoff;
5618 prev.br_startoff = unwrite_start;
5619 prev.br_startblock += mod;
5620 prev.br_blockcount -= mod;
5621 prev.br_state = XFS_EXT_UNWRITTEN;
5622 error = xfs_bmap_add_extent_unwritten_real(tp,
5623 ip, whichfork, &icur, &cur,
5629 ASSERT(del.br_state == XFS_EXT_NORM);
5630 del.br_state = XFS_EXT_UNWRITTEN;
5631 error = xfs_bmap_add_extent_unwritten_real(tp,
5632 ip, whichfork, &icur, &cur,
5642 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5645 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5646 &del, &tmp_logflags, whichfork,
5648 logflags |= tmp_logflags;
5654 max_len -= del.br_blockcount;
5655 end = del.br_startoff - 1;
5658 * If not done go on to the next (previous) record.
5660 if (end != (xfs_fileoff_t)-1 && end >= start) {
5661 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5662 (got.br_startoff > end &&
5663 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5670 if (done || end == (xfs_fileoff_t)-1 || end < start)
5673 *rlen = end - start + 1;
5676 * Convert to a btree if necessary.
5678 if (xfs_bmap_needs_btree(ip, whichfork)) {
5679 ASSERT(cur == NULL);
5680 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5681 &tmp_logflags, whichfork);
5682 logflags |= tmp_logflags;
5684 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5690 * Log everything. Do this after conversion, there's no point in
5691 * logging the extent records if we've converted to btree format.
5693 if ((logflags & xfs_ilog_fext(whichfork)) &&
5694 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5695 logflags &= ~xfs_ilog_fext(whichfork);
5696 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5697 ifp->if_format != XFS_DINODE_FMT_BTREE)
5698 logflags &= ~xfs_ilog_fbroot(whichfork);
5700 * Log inode even in the error case, if the transaction
5701 * is dirty we'll need to shut down the filesystem.
5704 xfs_trans_log_inode(tp, ip, logflags);
5707 cur->bc_ino.allocated = 0;
5708 xfs_btree_del_cursor(cur, error);
5713 /* Unmap a range of a file. */
5717 struct xfs_inode *ip,
5726 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5732 * Determine whether an extent shift can be accomplished by a merge with the
5733 * extent that precedes the target hole of the shift.
5737 struct xfs_bmbt_irec *left, /* preceding extent */
5738 struct xfs_bmbt_irec *got, /* current extent to shift */
5739 xfs_fileoff_t shift) /* shift fsb */
5741 xfs_fileoff_t startoff;
5743 startoff = got->br_startoff - shift;
5746 * The extent, once shifted, must be adjacent in-file and on-disk with
5747 * the preceding extent.
5749 if ((left->br_startoff + left->br_blockcount != startoff) ||
5750 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5751 (left->br_state != got->br_state) ||
5752 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5759 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5760 * hole in the file. If an extent shift would result in the extent being fully
5761 * adjacent to the extent that currently precedes the hole, we can merge with
5762 * the preceding extent rather than do the shift.
5764 * This function assumes the caller has verified a shift-by-merge is possible
5765 * with the provided extents via xfs_bmse_can_merge().
5769 struct xfs_trans *tp,
5770 struct xfs_inode *ip,
5772 xfs_fileoff_t shift, /* shift fsb */
5773 struct xfs_iext_cursor *icur,
5774 struct xfs_bmbt_irec *got, /* extent to shift */
5775 struct xfs_bmbt_irec *left, /* preceding extent */
5776 struct xfs_btree_cur *cur,
5777 int *logflags) /* output */
5779 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5780 struct xfs_bmbt_irec new;
5781 xfs_filblks_t blockcount;
5783 struct xfs_mount *mp = ip->i_mount;
5785 blockcount = left->br_blockcount + got->br_blockcount;
5787 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5788 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5789 ASSERT(xfs_bmse_can_merge(left, got, shift));
5792 new.br_blockcount = blockcount;
5795 * Update the on-disk extent count, the btree if necessary and log the
5799 *logflags |= XFS_ILOG_CORE;
5801 *logflags |= XFS_ILOG_DEXT;
5805 /* lookup and remove the extent to merge */
5806 error = xfs_bmbt_lookup_eq(cur, got, &i);
5809 if (XFS_IS_CORRUPT(mp, i != 1))
5810 return -EFSCORRUPTED;
5812 error = xfs_btree_delete(cur, &i);
5815 if (XFS_IS_CORRUPT(mp, i != 1))
5816 return -EFSCORRUPTED;
5818 /* lookup and update size of the previous extent */
5819 error = xfs_bmbt_lookup_eq(cur, left, &i);
5822 if (XFS_IS_CORRUPT(mp, i != 1))
5823 return -EFSCORRUPTED;
5825 error = xfs_bmbt_update(cur, &new);
5829 /* change to extent format if required after extent removal */
5830 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5835 xfs_iext_remove(ip, icur, 0);
5836 xfs_iext_prev(ifp, icur);
5837 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5840 /* update reverse mapping. rmap functions merge the rmaps for us */
5841 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5842 memcpy(&new, got, sizeof(new));
5843 new.br_startoff = left->br_startoff + left->br_blockcount;
5844 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5849 xfs_bmap_shift_update_extent(
5850 struct xfs_trans *tp,
5851 struct xfs_inode *ip,
5853 struct xfs_iext_cursor *icur,
5854 struct xfs_bmbt_irec *got,
5855 struct xfs_btree_cur *cur,
5857 xfs_fileoff_t startoff)
5859 struct xfs_mount *mp = ip->i_mount;
5860 struct xfs_bmbt_irec prev = *got;
5863 *logflags |= XFS_ILOG_CORE;
5865 got->br_startoff = startoff;
5868 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5871 if (XFS_IS_CORRUPT(mp, i != 1))
5872 return -EFSCORRUPTED;
5874 error = xfs_bmbt_update(cur, got);
5878 *logflags |= XFS_ILOG_DEXT;
5881 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5884 /* update reverse mapping */
5885 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5886 xfs_rmap_map_extent(tp, ip, whichfork, got);
5891 xfs_bmap_collapse_extents(
5892 struct xfs_trans *tp,
5893 struct xfs_inode *ip,
5894 xfs_fileoff_t *next_fsb,
5895 xfs_fileoff_t offset_shift_fsb,
5898 int whichfork = XFS_DATA_FORK;
5899 struct xfs_mount *mp = ip->i_mount;
5900 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5901 struct xfs_btree_cur *cur = NULL;
5902 struct xfs_bmbt_irec got, prev;
5903 struct xfs_iext_cursor icur;
5904 xfs_fileoff_t new_startoff;
5908 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5909 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5910 return -EFSCORRUPTED;
5913 if (XFS_FORCED_SHUTDOWN(mp))
5916 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5918 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5919 error = xfs_iread_extents(tp, ip, whichfork);
5924 if (ifp->if_flags & XFS_IFBROOT) {
5925 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5926 cur->bc_ino.flags = 0;
5929 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5933 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5934 error = -EFSCORRUPTED;
5938 new_startoff = got.br_startoff - offset_shift_fsb;
5939 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5940 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5945 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5946 error = xfs_bmse_merge(tp, ip, whichfork,
5947 offset_shift_fsb, &icur, &got, &prev,
5954 if (got.br_startoff < offset_shift_fsb) {
5960 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5961 cur, &logflags, new_startoff);
5966 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5971 *next_fsb = got.br_startoff;
5974 xfs_btree_del_cursor(cur, error);
5976 xfs_trans_log_inode(tp, ip, logflags);
5980 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5982 xfs_bmap_can_insert_extents(
5983 struct xfs_inode *ip,
5985 xfs_fileoff_t shift)
5987 struct xfs_bmbt_irec got;
5991 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5993 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5996 xfs_ilock(ip, XFS_ILOCK_EXCL);
5997 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5998 if (!error && !is_empty && got.br_startoff >= off &&
5999 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
6001 xfs_iunlock(ip, XFS_ILOCK_EXCL);
6007 xfs_bmap_insert_extents(
6008 struct xfs_trans *tp,
6009 struct xfs_inode *ip,
6010 xfs_fileoff_t *next_fsb,
6011 xfs_fileoff_t offset_shift_fsb,
6013 xfs_fileoff_t stop_fsb)
6015 int whichfork = XFS_DATA_FORK;
6016 struct xfs_mount *mp = ip->i_mount;
6017 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
6018 struct xfs_btree_cur *cur = NULL;
6019 struct xfs_bmbt_irec got, next;
6020 struct xfs_iext_cursor icur;
6021 xfs_fileoff_t new_startoff;
6025 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6026 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6027 return -EFSCORRUPTED;
6030 if (XFS_FORCED_SHUTDOWN(mp))
6033 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
6035 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6036 error = xfs_iread_extents(tp, ip, whichfork);
6041 if (ifp->if_flags & XFS_IFBROOT) {
6042 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6043 cur->bc_ino.flags = 0;
6046 if (*next_fsb == NULLFSBLOCK) {
6047 xfs_iext_last(ifp, &icur);
6048 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
6049 stop_fsb > got.br_startoff) {
6054 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
6059 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
6060 error = -EFSCORRUPTED;
6064 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
6065 error = -EFSCORRUPTED;
6069 new_startoff = got.br_startoff + offset_shift_fsb;
6070 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
6071 if (new_startoff + got.br_blockcount > next.br_startoff) {
6077 * Unlike a left shift (which involves a hole punch), a right
6078 * shift does not modify extent neighbors in any way. We should
6079 * never find mergeable extents in this scenario. Check anyways
6080 * and warn if we encounter two extents that could be one.
6082 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6086 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6087 cur, &logflags, new_startoff);
6091 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6092 stop_fsb >= got.br_startoff + got.br_blockcount) {
6097 *next_fsb = got.br_startoff;
6100 xfs_btree_del_cursor(cur, error);
6102 xfs_trans_log_inode(tp, ip, logflags);
6107 * Splits an extent into two extents at split_fsb block such that it is the
6108 * first block of the current_ext. @ext is a target extent to be split.
6109 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6110 * hole or the first block of extents, just return 0.
6113 xfs_bmap_split_extent(
6114 struct xfs_trans *tp,
6115 struct xfs_inode *ip,
6116 xfs_fileoff_t split_fsb)
6118 int whichfork = XFS_DATA_FORK;
6119 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
6120 struct xfs_btree_cur *cur = NULL;
6121 struct xfs_bmbt_irec got;
6122 struct xfs_bmbt_irec new; /* split extent */
6123 struct xfs_mount *mp = ip->i_mount;
6124 xfs_fsblock_t gotblkcnt; /* new block count for got */
6125 struct xfs_iext_cursor icur;
6130 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6131 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6132 return -EFSCORRUPTED;
6135 if (XFS_FORCED_SHUTDOWN(mp))
6138 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6139 /* Read in all the extents */
6140 error = xfs_iread_extents(tp, ip, whichfork);
6146 * If there are not extents, or split_fsb lies in a hole we are done.
6148 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6149 got.br_startoff >= split_fsb)
6152 gotblkcnt = split_fsb - got.br_startoff;
6153 new.br_startoff = split_fsb;
6154 new.br_startblock = got.br_startblock + gotblkcnt;
6155 new.br_blockcount = got.br_blockcount - gotblkcnt;
6156 new.br_state = got.br_state;
6158 if (ifp->if_flags & XFS_IFBROOT) {
6159 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6160 cur->bc_ino.flags = 0;
6161 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6164 if (XFS_IS_CORRUPT(mp, i != 1)) {
6165 error = -EFSCORRUPTED;
6170 got.br_blockcount = gotblkcnt;
6171 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6174 logflags = XFS_ILOG_CORE;
6176 error = xfs_bmbt_update(cur, &got);
6180 logflags |= XFS_ILOG_DEXT;
6182 /* Add new extent */
6183 xfs_iext_next(ifp, &icur);
6184 xfs_iext_insert(ip, &icur, &new, 0);
6188 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6191 if (XFS_IS_CORRUPT(mp, i != 0)) {
6192 error = -EFSCORRUPTED;
6195 error = xfs_btree_insert(cur, &i);
6198 if (XFS_IS_CORRUPT(mp, i != 1)) {
6199 error = -EFSCORRUPTED;
6205 * Convert to a btree if necessary.
6207 if (xfs_bmap_needs_btree(ip, whichfork)) {
6208 int tmp_logflags; /* partial log flag return val */
6210 ASSERT(cur == NULL);
6211 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6212 &tmp_logflags, whichfork);
6213 logflags |= tmp_logflags;
6218 cur->bc_ino.allocated = 0;
6219 xfs_btree_del_cursor(cur, error);
6223 xfs_trans_log_inode(tp, ip, logflags);
6227 /* Deferred mapping is only for real extents in the data fork. */
6229 xfs_bmap_is_update_needed(
6230 struct xfs_bmbt_irec *bmap)
6232 return bmap->br_startblock != HOLESTARTBLOCK &&
6233 bmap->br_startblock != DELAYSTARTBLOCK;
6236 /* Record a bmap intent. */
6239 struct xfs_trans *tp,
6240 enum xfs_bmap_intent_type type,
6241 struct xfs_inode *ip,
6243 struct xfs_bmbt_irec *bmap)
6245 struct xfs_bmap_intent *bi;
6247 trace_xfs_bmap_defer(tp->t_mountp,
6248 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6250 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6251 ip->i_ino, whichfork,
6253 bmap->br_blockcount,
6256 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
6257 INIT_LIST_HEAD(&bi->bi_list);
6260 bi->bi_whichfork = whichfork;
6261 bi->bi_bmap = *bmap;
6263 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6267 /* Map an extent into a file. */
6269 xfs_bmap_map_extent(
6270 struct xfs_trans *tp,
6271 struct xfs_inode *ip,
6272 struct xfs_bmbt_irec *PREV)
6274 if (!xfs_bmap_is_update_needed(PREV))
6277 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6280 /* Unmap an extent out of a file. */
6282 xfs_bmap_unmap_extent(
6283 struct xfs_trans *tp,
6284 struct xfs_inode *ip,
6285 struct xfs_bmbt_irec *PREV)
6287 if (!xfs_bmap_is_update_needed(PREV))
6290 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6294 * Process one of the deferred bmap operations. We pass back the
6295 * btree cursor to maintain our lock on the bmapbt between calls.
6298 xfs_bmap_finish_one(
6299 struct xfs_trans *tp,
6300 struct xfs_inode *ip,
6301 enum xfs_bmap_intent_type type,
6303 xfs_fileoff_t startoff,
6304 xfs_fsblock_t startblock,
6305 xfs_filblks_t *blockcount,
6310 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6312 trace_xfs_bmap_deferred(tp->t_mountp,
6313 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6314 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6315 ip->i_ino, whichfork, startoff, *blockcount, state);
6317 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6318 return -EFSCORRUPTED;
6320 if (XFS_TEST_ERROR(false, tp->t_mountp,
6321 XFS_ERRTAG_BMAP_FINISH_ONE))
6326 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6330 case XFS_BMAP_UNMAP:
6331 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6332 XFS_BMAPI_REMAP, 1);
6336 error = -EFSCORRUPTED;
6342 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6344 xfs_bmap_validate_extent(
6345 struct xfs_inode *ip,
6347 struct xfs_bmbt_irec *irec)
6349 struct xfs_mount *mp = ip->i_mount;
6351 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6352 return __this_address;
6354 if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) {
6355 if (!xfs_verify_rtext(mp, irec->br_startblock,
6356 irec->br_blockcount))
6357 return __this_address;
6359 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6360 irec->br_blockcount))
6361 return __this_address;
6363 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6364 return __this_address;