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
65 * number of leaf entries, is controlled by the type of di_nextents
66 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
67 * (a signed 16-bit number, xfs_aextnum_t).
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 return whichfork != XFS_COW_FORK &&
124 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
125 XFS_IFORK_NEXTENTS(ip, whichfork) >
126 XFS_IFORK_MAXEXT(ip, whichfork);
130 * Check if the inode should be converted to extent format.
132 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
134 return whichfork != XFS_COW_FORK &&
135 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
136 XFS_IFORK_NEXTENTS(ip, whichfork) <=
137 XFS_IFORK_MAXEXT(ip, whichfork);
141 * Update the record referred to by cur to the value given by irec
142 * This either works (return 0) or gets an EFSCORRUPTED error.
146 struct xfs_btree_cur *cur,
147 struct xfs_bmbt_irec *irec)
149 union xfs_btree_rec rec;
151 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
152 return xfs_btree_update(cur, &rec);
156 * Compute the worst-case number of indirect blocks that will be used
157 * for ip's delayed extent of length "len".
160 xfs_bmap_worst_indlen(
161 xfs_inode_t *ip, /* incore inode pointer */
162 xfs_filblks_t len) /* delayed extent length */
164 int level; /* btree level number */
165 int maxrecs; /* maximum record count at this level */
166 xfs_mount_t *mp; /* mount structure */
167 xfs_filblks_t rval; /* return value */
170 maxrecs = mp->m_bmap_dmxr[0];
171 for (level = 0, rval = 0;
172 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
175 do_div(len, maxrecs);
178 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
181 maxrecs = mp->m_bmap_dmxr[1];
187 * Calculate the default attribute fork offset for newly created inodes.
190 xfs_default_attroffset(
191 struct xfs_inode *ip)
193 struct xfs_mount *mp = ip->i_mount;
196 if (mp->m_sb.sb_inodesize == 256)
197 offset = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
199 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
201 ASSERT(offset < XFS_LITINO(mp));
206 * Helper routine to reset inode di_forkoff field when switching
207 * attribute fork from local to extent format - we reset it where
208 * possible to make space available for inline data fork extents.
211 xfs_bmap_forkoff_reset(
215 if (whichfork == XFS_ATTR_FORK &&
216 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
217 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
218 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
220 if (dfl_forkoff > ip->i_d.di_forkoff)
221 ip->i_d.di_forkoff = dfl_forkoff;
226 STATIC struct xfs_buf *
228 struct xfs_btree_cur *cur,
231 struct xfs_log_item *lip;
237 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
238 if (!cur->bc_bufs[i])
240 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
241 return cur->bc_bufs[i];
244 /* Chase down all the log items to see if the bp is there */
245 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
246 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
248 if (bip->bli_item.li_type == XFS_LI_BUF &&
249 XFS_BUF_ADDR(bip->bli_buf) == bno)
258 struct xfs_btree_block *block,
264 __be64 *pp, *thispa; /* pointer to block address */
265 xfs_bmbt_key_t *prevp, *keyp;
267 ASSERT(be16_to_cpu(block->bb_level) > 0);
270 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
271 dmxr = mp->m_bmap_dmxr[0];
272 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
275 ASSERT(be64_to_cpu(prevp->br_startoff) <
276 be64_to_cpu(keyp->br_startoff));
281 * Compare the block numbers to see if there are dups.
284 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
286 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
288 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
290 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
292 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
293 if (*thispa == *pp) {
294 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
296 (unsigned long long)be64_to_cpu(*thispa));
297 xfs_err(mp, "%s: ptrs are equal in node\n",
299 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
306 * Check that the extents for the inode ip are in the right order in all
307 * btree leaves. THis becomes prohibitively expensive for large extent count
308 * files, so don't bother with inodes that have more than 10,000 extents in
309 * them. The btree record ordering checks will still be done, so for such large
310 * bmapbt constructs that is going to catch most corruptions.
313 xfs_bmap_check_leaf_extents(
314 xfs_btree_cur_t *cur, /* btree cursor or null */
315 xfs_inode_t *ip, /* incore inode pointer */
316 int whichfork) /* data or attr fork */
318 struct xfs_btree_block *block; /* current btree block */
319 xfs_fsblock_t bno; /* block # of "block" */
320 xfs_buf_t *bp; /* buffer for "block" */
321 int error; /* error return value */
322 xfs_extnum_t i=0, j; /* index into the extents list */
323 struct xfs_ifork *ifp; /* fork structure */
324 int level; /* btree level, for checking */
325 xfs_mount_t *mp; /* file system mount structure */
326 __be64 *pp; /* pointer to block address */
327 xfs_bmbt_rec_t *ep; /* pointer to current extent */
328 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
329 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
332 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
336 /* skip large extent count inodes */
337 if (ip->i_d.di_nextents > 10000)
342 ifp = XFS_IFORK_PTR(ip, whichfork);
343 block = ifp->if_broot;
345 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
347 level = be16_to_cpu(block->bb_level);
349 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
350 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
351 bno = be64_to_cpu(*pp);
353 ASSERT(bno != NULLFSBLOCK);
354 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
355 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
358 * Go down the tree until leaf level is reached, following the first
359 * pointer (leftmost) at each level.
361 while (level-- > 0) {
362 /* See if buf is in cur first */
364 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
367 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
373 block = XFS_BUF_TO_BLOCK(bp);
378 * Check this block for basic sanity (increasing keys and
379 * no duplicate blocks).
382 xfs_check_block(block, mp, 0, 0);
383 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
384 bno = be64_to_cpu(*pp);
385 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
386 error = -EFSCORRUPTED;
391 xfs_trans_brelse(NULL, bp);
396 * Here with bp and block set to the leftmost leaf node in the tree.
401 * Loop over all leaf nodes checking that all extents are in the right order.
404 xfs_fsblock_t nextbno;
405 xfs_extnum_t num_recs;
408 num_recs = xfs_btree_get_numrecs(block);
411 * Read-ahead the next leaf block, if any.
414 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
417 * Check all the extents to make sure they are OK.
418 * If we had a previous block, the last entry should
419 * conform with the first entry in this one.
422 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
424 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
425 xfs_bmbt_disk_get_blockcount(&last) <=
426 xfs_bmbt_disk_get_startoff(ep));
428 for (j = 1; j < num_recs; j++) {
429 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
430 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
431 xfs_bmbt_disk_get_blockcount(ep) <=
432 xfs_bmbt_disk_get_startoff(nextp));
440 xfs_trans_brelse(NULL, bp);
444 * If we've reached the end, stop.
446 if (bno == NULLFSBLOCK)
450 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
453 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
459 block = XFS_BUF_TO_BLOCK(bp);
465 xfs_warn(mp, "%s: at error0", __func__);
467 xfs_trans_brelse(NULL, bp);
469 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
471 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
472 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
477 * Validate that the bmbt_irecs being returned from bmapi are valid
478 * given the caller's original parameters. Specifically check the
479 * ranges of the returned irecs to ensure that they only extend beyond
480 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
483 xfs_bmap_validate_ret(
487 xfs_bmbt_irec_t *mval,
491 int i; /* index to map values */
493 ASSERT(ret_nmap <= nmap);
495 for (i = 0; i < ret_nmap; i++) {
496 ASSERT(mval[i].br_blockcount > 0);
497 if (!(flags & XFS_BMAPI_ENTIRE)) {
498 ASSERT(mval[i].br_startoff >= bno);
499 ASSERT(mval[i].br_blockcount <= len);
500 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
503 ASSERT(mval[i].br_startoff < bno + len);
504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
508 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
509 mval[i].br_startoff);
510 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
511 mval[i].br_startblock != HOLESTARTBLOCK);
512 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
513 mval[i].br_state == XFS_EXT_UNWRITTEN);
518 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
519 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
523 * bmap free list manipulation functions
527 * Add the extent to the list of extents to be free at transaction end.
528 * The list is maintained sorted (by block number).
532 struct xfs_trans *tp,
535 const struct xfs_owner_info *oinfo,
538 struct xfs_extent_free_item *new; /* new element */
540 struct xfs_mount *mp = tp->t_mountp;
544 ASSERT(bno != NULLFSBLOCK);
546 ASSERT(len <= MAXEXTLEN);
547 ASSERT(!isnullstartblock(bno));
548 agno = XFS_FSB_TO_AGNO(mp, bno);
549 agbno = XFS_FSB_TO_AGBNO(mp, bno);
550 ASSERT(agno < mp->m_sb.sb_agcount);
551 ASSERT(agbno < mp->m_sb.sb_agblocks);
552 ASSERT(len < mp->m_sb.sb_agblocks);
553 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
555 ASSERT(xfs_bmap_free_item_zone != NULL);
557 new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
558 new->xefi_startblock = bno;
559 new->xefi_blockcount = (xfs_extlen_t)len;
561 new->xefi_oinfo = *oinfo;
563 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
564 new->xefi_skip_discard = skip_discard;
565 trace_xfs_bmap_free_defer(tp->t_mountp,
566 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
567 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
568 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
572 * Inode fork format manipulation functions
576 * Convert the inode format to extent format if it currently is in btree format,
577 * but the extent list is small enough that it fits into the extent format.
579 * Since the extents are already in-core, all we have to do is give up the space
580 * for the btree root and pitch the leaf block.
582 STATIC int /* error */
583 xfs_bmap_btree_to_extents(
584 struct xfs_trans *tp, /* transaction pointer */
585 struct xfs_inode *ip, /* incore inode pointer */
586 struct xfs_btree_cur *cur, /* btree cursor */
587 int *logflagsp, /* inode logging flags */
588 int whichfork) /* data or attr fork */
590 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
591 struct xfs_mount *mp = ip->i_mount;
592 struct xfs_btree_block *rblock = ifp->if_broot;
593 struct xfs_btree_block *cblock;/* child btree block */
594 xfs_fsblock_t cbno; /* child block number */
595 xfs_buf_t *cbp; /* child block's buffer */
596 int error; /* error return value */
597 __be64 *pp; /* ptr to block address */
598 struct xfs_owner_info oinfo;
600 /* check if we actually need the extent format first: */
601 if (!xfs_bmap_wants_extents(ip, whichfork))
605 ASSERT(whichfork != XFS_COW_FORK);
606 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
607 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
608 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
609 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
610 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
612 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
613 cbno = be64_to_cpu(*pp);
615 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
616 return -EFSCORRUPTED;
618 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
622 cblock = XFS_BUF_TO_BLOCK(cbp);
623 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
625 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
626 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
627 ip->i_d.di_nblocks--;
628 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
629 xfs_trans_binval(tp, cbp);
630 if (cur->bc_bufs[0] == cbp)
631 cur->bc_bufs[0] = NULL;
632 xfs_iroot_realloc(ip, -1, whichfork);
633 ASSERT(ifp->if_broot == NULL);
634 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
635 XFS_IFORK_FMT_SET(ip, whichfork, 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(XFS_IFORK_FORMAT(ip, whichfork) == 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 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
696 memset(&args, 0, sizeof(args));
699 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
700 if (tp->t_firstblock == NULLFSBLOCK) {
701 args.type = XFS_ALLOCTYPE_START_BNO;
702 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
703 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
704 args.type = XFS_ALLOCTYPE_START_BNO;
705 args.fsbno = tp->t_firstblock;
707 args.type = XFS_ALLOCTYPE_NEAR_BNO;
708 args.fsbno = tp->t_firstblock;
710 args.minlen = args.maxlen = args.prod = 1;
711 args.wasdel = wasdel;
713 error = xfs_alloc_vextent(&args);
715 goto out_root_realloc;
717 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
719 goto out_root_realloc;
723 * Allocation can't fail, the space was reserved.
725 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
726 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
727 tp->t_firstblock = args.fsbno;
728 cur->bc_ino.allocated++;
729 ip->i_d.di_nblocks++;
730 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
731 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
732 XFS_FSB_TO_DADDR(mp, args.fsbno),
733 mp->m_bsize, 0, &abp);
735 goto out_unreserve_dquot;
738 * Fill in the child block.
740 abp->b_ops = &xfs_bmbt_buf_ops;
741 ablock = XFS_BUF_TO_BLOCK(abp);
742 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
743 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
744 XFS_BTREE_LONG_PTRS);
746 for_each_xfs_iext(ifp, &icur, &rec) {
747 if (isnullstartblock(rec.br_startblock))
749 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
750 xfs_bmbt_disk_set_all(arp, &rec);
753 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
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 XFS_IFORK_FMT_SET(ip, whichfork, 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(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
804 ASSERT(ifp->if_bytes == 0);
805 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 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 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
813 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
817 STATIC int /* error */
818 xfs_bmap_local_to_extents(
819 xfs_trans_t *tp, /* transaction pointer */
820 xfs_inode_t *ip, /* incore inode pointer */
821 xfs_extlen_t total, /* total blocks needed by transaction */
822 int *logflagsp, /* inode logging flags */
824 void (*init_fn)(struct xfs_trans *tp,
826 struct xfs_inode *ip,
827 struct xfs_ifork *ifp))
830 int flags; /* logging flags returned */
831 struct xfs_ifork *ifp; /* inode fork pointer */
832 xfs_alloc_arg_t args; /* allocation arguments */
833 xfs_buf_t *bp; /* buffer for extent block */
834 struct xfs_bmbt_irec rec;
835 struct xfs_iext_cursor icur;
838 * We don't want to deal with the case of keeping inode data inline yet.
839 * So sending the data fork of a regular inode is invalid.
841 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
842 ifp = XFS_IFORK_PTR(ip, whichfork);
843 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == 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 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
911 ip->i_d.di_nblocks = 1;
912 xfs_trans_mod_dquot_byino(tp, ip,
913 XFS_TRANS_DQ_BCOUNT, 1L);
914 flags |= xfs_ilog_fext(whichfork);
922 * Called from xfs_bmap_add_attrfork to handle btree format files.
924 STATIC int /* error */
925 xfs_bmap_add_attrfork_btree(
926 xfs_trans_t *tp, /* transaction pointer */
927 xfs_inode_t *ip, /* incore inode pointer */
928 int *flags) /* inode logging flags */
930 xfs_btree_cur_t *cur; /* btree cursor */
931 int error; /* error return value */
932 xfs_mount_t *mp; /* file system mount struct */
933 int stat; /* newroot status */
936 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
937 *flags |= XFS_ILOG_DBROOT;
939 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
940 error = xfs_bmbt_lookup_first(cur, &stat);
943 /* must be at least one entry */
944 if (XFS_IS_CORRUPT(mp, stat != 1)) {
945 error = -EFSCORRUPTED;
948 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
951 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
954 cur->bc_ino.allocated = 0;
955 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
959 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
964 * Called from xfs_bmap_add_attrfork to handle extents format files.
966 STATIC int /* error */
967 xfs_bmap_add_attrfork_extents(
968 struct xfs_trans *tp, /* transaction pointer */
969 struct xfs_inode *ip, /* incore inode pointer */
970 int *flags) /* inode logging flags */
972 xfs_btree_cur_t *cur; /* bmap btree cursor */
973 int error; /* error return value */
975 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
978 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
981 cur->bc_ino.allocated = 0;
982 xfs_btree_del_cursor(cur, error);
988 * Called from xfs_bmap_add_attrfork to handle local format files. Each
989 * different data fork content type needs a different callout to do the
990 * conversion. Some are basic and only require special block initialisation
991 * callouts for the data formating, others (directories) are so specialised they
992 * handle everything themselves.
994 * XXX (dgc): investigate whether directory conversion can use the generic
995 * formatting callout. It should be possible - it's just a very complex
998 STATIC int /* error */
999 xfs_bmap_add_attrfork_local(
1000 struct xfs_trans *tp, /* transaction pointer */
1001 struct xfs_inode *ip, /* incore inode pointer */
1002 int *flags) /* inode logging flags */
1004 struct xfs_da_args dargs; /* args for dir/attr code */
1006 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1009 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1010 memset(&dargs, 0, sizeof(dargs));
1011 dargs.geo = ip->i_mount->m_dir_geo;
1013 dargs.total = dargs.geo->fsbcount;
1014 dargs.whichfork = XFS_DATA_FORK;
1016 return xfs_dir2_sf_to_block(&dargs);
1019 if (S_ISLNK(VFS_I(ip)->i_mode))
1020 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1022 xfs_symlink_local_to_remote);
1024 /* should only be called for types that support local format data */
1026 return -EFSCORRUPTED;
1029 /* Set an inode attr fork off based on the format */
1031 xfs_bmap_set_attrforkoff(
1032 struct xfs_inode *ip,
1036 switch (ip->i_d.di_format) {
1037 case XFS_DINODE_FMT_DEV:
1038 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1040 case XFS_DINODE_FMT_LOCAL:
1041 case XFS_DINODE_FMT_EXTENTS:
1042 case XFS_DINODE_FMT_BTREE:
1043 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1044 if (!ip->i_d.di_forkoff)
1045 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1046 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1058 * Convert inode from non-attributed to attributed.
1059 * Must not be in a transaction, ip must not be locked.
1061 int /* error code */
1062 xfs_bmap_add_attrfork(
1063 xfs_inode_t *ip, /* incore inode pointer */
1064 int size, /* space new attribute needs */
1065 int rsvd) /* xact may use reserved blks */
1067 xfs_mount_t *mp; /* mount structure */
1068 xfs_trans_t *tp; /* transaction pointer */
1069 int blks; /* space reservation */
1070 int version = 1; /* superblock attr version */
1071 int logflags; /* logging flags */
1072 int error; /* error return value */
1074 ASSERT(XFS_IFORK_Q(ip) == 0);
1077 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1079 blks = XFS_ADDAFORK_SPACE_RES(mp);
1081 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1082 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1086 xfs_ilock(ip, XFS_ILOCK_EXCL);
1087 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1088 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1089 XFS_QMOPT_RES_REGBLKS);
1092 if (XFS_IFORK_Q(ip))
1094 if (XFS_IS_CORRUPT(mp, ip->i_d.di_anextents != 0)) {
1095 error = -EFSCORRUPTED;
1098 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1100 * For inodes coming from pre-6.2 filesystems.
1102 ASSERT(ip->i_d.di_aformat == 0);
1103 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1106 xfs_trans_ijoin(tp, ip, 0);
1107 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1108 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1111 ASSERT(ip->i_afp == NULL);
1112 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0);
1113 ip->i_afp->if_flags = XFS_IFEXTENTS;
1115 switch (ip->i_d.di_format) {
1116 case XFS_DINODE_FMT_LOCAL:
1117 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1119 case XFS_DINODE_FMT_EXTENTS:
1120 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1122 case XFS_DINODE_FMT_BTREE:
1123 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1130 xfs_trans_log_inode(tp, ip, logflags);
1133 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1134 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1135 bool log_sb = false;
1137 spin_lock(&mp->m_sb_lock);
1138 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1139 xfs_sb_version_addattr(&mp->m_sb);
1142 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1143 xfs_sb_version_addattr2(&mp->m_sb);
1146 spin_unlock(&mp->m_sb_lock);
1151 error = xfs_trans_commit(tp);
1152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1156 xfs_trans_cancel(tp);
1157 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1162 * Internal and external extent tree search functions.
1165 struct xfs_iread_state {
1166 struct xfs_iext_cursor icur;
1167 xfs_extnum_t loaded;
1170 /* Stuff every bmbt record from this block into the incore extent map. */
1172 xfs_iread_bmbt_block(
1173 struct xfs_btree_cur *cur,
1177 struct xfs_iread_state *ir = priv;
1178 struct xfs_mount *mp = cur->bc_mp;
1179 struct xfs_inode *ip = cur->bc_ino.ip;
1180 struct xfs_btree_block *block;
1182 struct xfs_bmbt_rec *frp;
1183 xfs_extnum_t num_recs;
1185 int whichfork = cur->bc_ino.whichfork;
1187 block = xfs_btree_get_block(cur, level, &bp);
1189 /* Abort if we find more records than nextents. */
1190 num_recs = xfs_btree_get_numrecs(block);
1191 if (unlikely(ir->loaded + num_recs >
1192 XFS_IFORK_NEXTENTS(ip, whichfork))) {
1193 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1194 (unsigned long long)ip->i_ino);
1195 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1196 sizeof(*block), __this_address);
1197 return -EFSCORRUPTED;
1200 /* Copy records into the incore cache. */
1201 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1202 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1203 struct xfs_bmbt_irec new;
1206 xfs_bmbt_disk_get_all(frp, &new);
1207 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1209 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1210 "xfs_iread_extents(2)", frp,
1212 return -EFSCORRUPTED;
1214 xfs_iext_insert(ip, &ir->icur, &new,
1215 xfs_bmap_fork_to_state(whichfork));
1216 trace_xfs_read_extent(ip, &ir->icur,
1217 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1218 xfs_iext_next(XFS_IFORK_PTR(ip, whichfork), &ir->icur);
1225 * Read in extents from a btree-format inode.
1229 struct xfs_trans *tp,
1230 struct xfs_inode *ip,
1233 struct xfs_iread_state ir;
1234 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1235 struct xfs_mount *mp = ip->i_mount;
1236 struct xfs_btree_cur *cur;
1239 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1241 if (XFS_IS_CORRUPT(mp,
1242 XFS_IFORK_FORMAT(ip, whichfork) !=
1243 XFS_DINODE_FMT_BTREE)) {
1244 error = -EFSCORRUPTED;
1249 xfs_iext_first(ifp, &ir.icur);
1250 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1251 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1252 XFS_BTREE_VISIT_RECORDS, &ir);
1253 xfs_btree_del_cursor(cur, error);
1257 if (XFS_IS_CORRUPT(mp,
1258 ir.loaded != XFS_IFORK_NEXTENTS(ip, whichfork))) {
1259 error = -EFSCORRUPTED;
1262 ASSERT(ir.loaded == xfs_iext_count(ifp));
1264 ifp->if_flags |= XFS_IFEXTENTS;
1267 xfs_iext_destroy(ifp);
1272 * Returns the relative block number of the first unused block(s) in the given
1273 * fork with at least "len" logically contiguous blocks free. This is the
1274 * lowest-address hole if the fork has holes, else the first block past the end
1275 * of fork. Return 0 if the fork is currently local (in-inode).
1278 xfs_bmap_first_unused(
1279 struct xfs_trans *tp, /* transaction pointer */
1280 struct xfs_inode *ip, /* incore inode */
1281 xfs_extlen_t len, /* size of hole to find */
1282 xfs_fileoff_t *first_unused, /* unused block */
1283 int whichfork) /* data or attr fork */
1285 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1286 struct xfs_bmbt_irec got;
1287 struct xfs_iext_cursor icur;
1288 xfs_fileoff_t lastaddr = 0;
1289 xfs_fileoff_t lowest, max;
1292 ASSERT(xfs_ifork_has_extents(ip, whichfork) ||
1293 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1295 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1300 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1301 error = xfs_iread_extents(tp, ip, whichfork);
1306 lowest = max = *first_unused;
1307 for_each_xfs_iext(ifp, &icur, &got) {
1309 * See if the hole before this extent will work.
1311 if (got.br_startoff >= lowest + len &&
1312 got.br_startoff - max >= len)
1314 lastaddr = got.br_startoff + got.br_blockcount;
1315 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1318 *first_unused = max;
1323 * Returns the file-relative block number of the last block - 1 before
1324 * last_block (input value) in the file.
1325 * This is not based on i_size, it is based on the extent records.
1326 * Returns 0 for local files, as they do not have extent records.
1329 xfs_bmap_last_before(
1330 struct xfs_trans *tp, /* transaction pointer */
1331 struct xfs_inode *ip, /* incore inode */
1332 xfs_fileoff_t *last_block, /* last block */
1333 int whichfork) /* data or attr fork */
1335 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1336 struct xfs_bmbt_irec got;
1337 struct xfs_iext_cursor icur;
1340 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1341 case XFS_DINODE_FMT_LOCAL:
1344 case XFS_DINODE_FMT_BTREE:
1345 case XFS_DINODE_FMT_EXTENTS:
1349 return -EFSCORRUPTED;
1352 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1353 error = xfs_iread_extents(tp, ip, whichfork);
1358 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1364 xfs_bmap_last_extent(
1365 struct xfs_trans *tp,
1366 struct xfs_inode *ip,
1368 struct xfs_bmbt_irec *rec,
1371 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1372 struct xfs_iext_cursor icur;
1375 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1376 error = xfs_iread_extents(tp, ip, whichfork);
1381 xfs_iext_last(ifp, &icur);
1382 if (!xfs_iext_get_extent(ifp, &icur, rec))
1390 * Check the last inode extent to determine whether this allocation will result
1391 * in blocks being allocated at the end of the file. When we allocate new data
1392 * blocks at the end of the file which do not start at the previous data block,
1393 * we will try to align the new blocks at stripe unit boundaries.
1395 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1396 * at, or past the EOF.
1400 struct xfs_bmalloca *bma,
1403 struct xfs_bmbt_irec rec;
1408 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1419 * Check if we are allocation or past the last extent, or at least into
1420 * the last delayed allocated extent.
1422 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1423 (bma->offset >= rec.br_startoff &&
1424 isnullstartblock(rec.br_startblock));
1429 * Returns the file-relative block number of the first block past eof in
1430 * the file. This is not based on i_size, it is based on the extent records.
1431 * Returns 0 for local files, as they do not have extent records.
1434 xfs_bmap_last_offset(
1435 struct xfs_inode *ip,
1436 xfs_fileoff_t *last_block,
1439 struct xfs_bmbt_irec rec;
1445 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1448 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ip, whichfork)))
1449 return -EFSCORRUPTED;
1451 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1452 if (error || is_empty)
1455 *last_block = rec.br_startoff + rec.br_blockcount;
1460 * Returns whether the selected fork of the inode has exactly one
1461 * block or not. For the data fork we check this matches di_size,
1462 * implying the file's range is 0..bsize-1.
1464 int /* 1=>1 block, 0=>otherwise */
1466 xfs_inode_t *ip, /* incore inode */
1467 int whichfork) /* data or attr fork */
1469 struct xfs_ifork *ifp; /* inode fork pointer */
1470 int rval; /* return value */
1471 xfs_bmbt_irec_t s; /* internal version of extent */
1472 struct xfs_iext_cursor icur;
1475 if (whichfork == XFS_DATA_FORK)
1476 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1478 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1480 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1482 ifp = XFS_IFORK_PTR(ip, whichfork);
1483 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1484 xfs_iext_first(ifp, &icur);
1485 xfs_iext_get_extent(ifp, &icur, &s);
1486 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1487 if (rval && whichfork == XFS_DATA_FORK)
1488 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1493 * Extent tree manipulation functions used during allocation.
1497 * Convert a delayed allocation to a real allocation.
1499 STATIC int /* error */
1500 xfs_bmap_add_extent_delay_real(
1501 struct xfs_bmalloca *bma,
1504 struct xfs_bmbt_irec *new = &bma->got;
1505 int error; /* error return value */
1506 int i; /* temp state */
1507 struct xfs_ifork *ifp; /* inode fork pointer */
1508 xfs_fileoff_t new_endoff; /* end offset of new entry */
1509 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1510 /* left is 0, right is 1, prev is 2 */
1511 int rval=0; /* return value (logging flags) */
1512 int state = xfs_bmap_fork_to_state(whichfork);
1513 xfs_filblks_t da_new; /* new count del alloc blocks used */
1514 xfs_filblks_t da_old; /* old count del alloc blocks used */
1515 xfs_filblks_t temp=0; /* value for da_new calculations */
1516 int tmp_rval; /* partial logging flags */
1517 struct xfs_mount *mp;
1518 xfs_extnum_t *nextents;
1519 struct xfs_bmbt_irec old;
1521 mp = bma->ip->i_mount;
1522 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1523 ASSERT(whichfork != XFS_ATTR_FORK);
1524 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1525 &bma->ip->i_d.di_nextents);
1527 ASSERT(!isnullstartblock(new->br_startblock));
1529 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1531 XFS_STATS_INC(mp, xs_add_exlist);
1538 * Set up a bunch of variables to make the tests simpler.
1540 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1541 new_endoff = new->br_startoff + new->br_blockcount;
1542 ASSERT(isnullstartblock(PREV.br_startblock));
1543 ASSERT(PREV.br_startoff <= new->br_startoff);
1544 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1546 da_old = startblockval(PREV.br_startblock);
1550 * Set flags determining what part of the previous delayed allocation
1551 * extent is being replaced by a real allocation.
1553 if (PREV.br_startoff == new->br_startoff)
1554 state |= BMAP_LEFT_FILLING;
1555 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1556 state |= BMAP_RIGHT_FILLING;
1559 * Check and set flags if this segment has a left neighbor.
1560 * Don't set contiguous if the combined extent would be too large.
1562 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1563 state |= BMAP_LEFT_VALID;
1564 if (isnullstartblock(LEFT.br_startblock))
1565 state |= BMAP_LEFT_DELAY;
1568 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1569 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1570 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1571 LEFT.br_state == new->br_state &&
1572 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1573 state |= BMAP_LEFT_CONTIG;
1576 * Check and set flags if this segment has a right neighbor.
1577 * Don't set contiguous if the combined extent would be too large.
1578 * Also check for all-three-contiguous being too large.
1580 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1581 state |= BMAP_RIGHT_VALID;
1582 if (isnullstartblock(RIGHT.br_startblock))
1583 state |= BMAP_RIGHT_DELAY;
1586 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1587 new_endoff == RIGHT.br_startoff &&
1588 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1589 new->br_state == RIGHT.br_state &&
1590 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1591 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1592 BMAP_RIGHT_FILLING)) !=
1593 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1594 BMAP_RIGHT_FILLING) ||
1595 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1597 state |= BMAP_RIGHT_CONTIG;
1601 * Switch out based on the FILLING and CONTIG state bits.
1603 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1604 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1605 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1606 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1608 * Filling in all of a previously delayed allocation extent.
1609 * The left and right neighbors are both contiguous with new.
1611 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1613 xfs_iext_remove(bma->ip, &bma->icur, state);
1614 xfs_iext_remove(bma->ip, &bma->icur, state);
1615 xfs_iext_prev(ifp, &bma->icur);
1616 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1619 if (bma->cur == NULL)
1620 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1622 rval = XFS_ILOG_CORE;
1623 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1626 if (XFS_IS_CORRUPT(mp, i != 1)) {
1627 error = -EFSCORRUPTED;
1630 error = xfs_btree_delete(bma->cur, &i);
1633 if (XFS_IS_CORRUPT(mp, i != 1)) {
1634 error = -EFSCORRUPTED;
1637 error = xfs_btree_decrement(bma->cur, 0, &i);
1640 if (XFS_IS_CORRUPT(mp, i != 1)) {
1641 error = -EFSCORRUPTED;
1644 error = xfs_bmbt_update(bma->cur, &LEFT);
1650 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1652 * Filling in all of a previously delayed allocation extent.
1653 * The left neighbor is contiguous, the right is not.
1656 LEFT.br_blockcount += PREV.br_blockcount;
1658 xfs_iext_remove(bma->ip, &bma->icur, state);
1659 xfs_iext_prev(ifp, &bma->icur);
1660 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1662 if (bma->cur == NULL)
1663 rval = XFS_ILOG_DEXT;
1666 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1669 if (XFS_IS_CORRUPT(mp, i != 1)) {
1670 error = -EFSCORRUPTED;
1673 error = xfs_bmbt_update(bma->cur, &LEFT);
1679 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1681 * Filling in all of a previously delayed allocation extent.
1682 * The right neighbor is contiguous, the left is not. Take care
1683 * with delay -> unwritten extent allocation here because the
1684 * delalloc record we are overwriting is always written.
1686 PREV.br_startblock = new->br_startblock;
1687 PREV.br_blockcount += RIGHT.br_blockcount;
1688 PREV.br_state = new->br_state;
1690 xfs_iext_next(ifp, &bma->icur);
1691 xfs_iext_remove(bma->ip, &bma->icur, state);
1692 xfs_iext_prev(ifp, &bma->icur);
1693 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1695 if (bma->cur == NULL)
1696 rval = XFS_ILOG_DEXT;
1699 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1702 if (XFS_IS_CORRUPT(mp, i != 1)) {
1703 error = -EFSCORRUPTED;
1706 error = xfs_bmbt_update(bma->cur, &PREV);
1712 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1714 * Filling in all of a previously delayed allocation extent.
1715 * Neither the left nor right neighbors are contiguous with
1718 PREV.br_startblock = new->br_startblock;
1719 PREV.br_state = new->br_state;
1720 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1723 if (bma->cur == NULL)
1724 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1726 rval = XFS_ILOG_CORE;
1727 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1730 if (XFS_IS_CORRUPT(mp, i != 0)) {
1731 error = -EFSCORRUPTED;
1734 error = xfs_btree_insert(bma->cur, &i);
1737 if (XFS_IS_CORRUPT(mp, i != 1)) {
1738 error = -EFSCORRUPTED;
1744 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1746 * Filling in the first part of a previous delayed allocation.
1747 * The left neighbor is contiguous.
1750 temp = PREV.br_blockcount - new->br_blockcount;
1751 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1752 startblockval(PREV.br_startblock));
1754 LEFT.br_blockcount += new->br_blockcount;
1756 PREV.br_blockcount = temp;
1757 PREV.br_startoff += new->br_blockcount;
1758 PREV.br_startblock = nullstartblock(da_new);
1760 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1761 xfs_iext_prev(ifp, &bma->icur);
1762 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1764 if (bma->cur == NULL)
1765 rval = XFS_ILOG_DEXT;
1768 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1771 if (XFS_IS_CORRUPT(mp, i != 1)) {
1772 error = -EFSCORRUPTED;
1775 error = xfs_bmbt_update(bma->cur, &LEFT);
1781 case BMAP_LEFT_FILLING:
1783 * Filling in the first part of a previous delayed allocation.
1784 * The left neighbor is not contiguous.
1786 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1788 if (bma->cur == NULL)
1789 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1791 rval = XFS_ILOG_CORE;
1792 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1795 if (XFS_IS_CORRUPT(mp, i != 0)) {
1796 error = -EFSCORRUPTED;
1799 error = xfs_btree_insert(bma->cur, &i);
1802 if (XFS_IS_CORRUPT(mp, i != 1)) {
1803 error = -EFSCORRUPTED;
1808 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1809 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1810 &bma->cur, 1, &tmp_rval, whichfork);
1816 temp = PREV.br_blockcount - new->br_blockcount;
1817 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1818 startblockval(PREV.br_startblock) -
1819 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1821 PREV.br_startoff = new_endoff;
1822 PREV.br_blockcount = temp;
1823 PREV.br_startblock = nullstartblock(da_new);
1824 xfs_iext_next(ifp, &bma->icur);
1825 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1826 xfs_iext_prev(ifp, &bma->icur);
1829 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1831 * Filling in the last part of a previous delayed allocation.
1832 * The right neighbor is contiguous with the new allocation.
1835 RIGHT.br_startoff = new->br_startoff;
1836 RIGHT.br_startblock = new->br_startblock;
1837 RIGHT.br_blockcount += new->br_blockcount;
1839 if (bma->cur == NULL)
1840 rval = XFS_ILOG_DEXT;
1843 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1846 if (XFS_IS_CORRUPT(mp, i != 1)) {
1847 error = -EFSCORRUPTED;
1850 error = xfs_bmbt_update(bma->cur, &RIGHT);
1855 temp = PREV.br_blockcount - new->br_blockcount;
1856 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1857 startblockval(PREV.br_startblock));
1859 PREV.br_blockcount = temp;
1860 PREV.br_startblock = nullstartblock(da_new);
1862 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1863 xfs_iext_next(ifp, &bma->icur);
1864 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1867 case BMAP_RIGHT_FILLING:
1869 * Filling in the last part of a previous delayed allocation.
1870 * The right neighbor is not contiguous.
1872 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1874 if (bma->cur == NULL)
1875 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1877 rval = XFS_ILOG_CORE;
1878 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1881 if (XFS_IS_CORRUPT(mp, i != 0)) {
1882 error = -EFSCORRUPTED;
1885 error = xfs_btree_insert(bma->cur, &i);
1888 if (XFS_IS_CORRUPT(mp, i != 1)) {
1889 error = -EFSCORRUPTED;
1894 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1895 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1896 &bma->cur, 1, &tmp_rval, whichfork);
1902 temp = PREV.br_blockcount - new->br_blockcount;
1903 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1904 startblockval(PREV.br_startblock) -
1905 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1907 PREV.br_startblock = nullstartblock(da_new);
1908 PREV.br_blockcount = temp;
1909 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1910 xfs_iext_next(ifp, &bma->icur);
1915 * Filling in the middle part of a previous delayed allocation.
1916 * Contiguity is impossible here.
1917 * This case is avoided almost all the time.
1919 * We start with a delayed allocation:
1921 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1924 * and we are allocating:
1925 * +rrrrrrrrrrrrrrrrr+
1928 * and we set it up for insertion as:
1929 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1931 * PREV @ idx LEFT RIGHT
1932 * inserted at idx + 1
1936 /* LEFT is the new middle */
1939 /* RIGHT is the new right */
1940 RIGHT.br_state = PREV.br_state;
1941 RIGHT.br_startoff = new_endoff;
1942 RIGHT.br_blockcount =
1943 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1944 RIGHT.br_startblock =
1945 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1946 RIGHT.br_blockcount));
1949 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1950 PREV.br_startblock =
1951 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1952 PREV.br_blockcount));
1953 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1955 xfs_iext_next(ifp, &bma->icur);
1956 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1957 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1960 if (bma->cur == NULL)
1961 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1963 rval = XFS_ILOG_CORE;
1964 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1967 if (XFS_IS_CORRUPT(mp, i != 0)) {
1968 error = -EFSCORRUPTED;
1971 error = xfs_btree_insert(bma->cur, &i);
1974 if (XFS_IS_CORRUPT(mp, i != 1)) {
1975 error = -EFSCORRUPTED;
1980 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1981 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1982 &bma->cur, 1, &tmp_rval, whichfork);
1988 da_new = startblockval(PREV.br_startblock) +
1989 startblockval(RIGHT.br_startblock);
1992 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1993 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1994 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1995 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1996 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1997 case BMAP_LEFT_CONTIG:
1998 case BMAP_RIGHT_CONTIG:
2000 * These cases are all impossible.
2005 /* add reverse mapping unless caller opted out */
2006 if (!(bma->flags & XFS_BMAPI_NORMAP))
2007 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
2009 /* convert to a btree if necessary */
2010 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2011 int tmp_logflags; /* partial log flag return val */
2013 ASSERT(bma->cur == NULL);
2014 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2015 &bma->cur, da_old > 0, &tmp_logflags,
2017 bma->logflags |= tmp_logflags;
2022 if (da_new != da_old)
2023 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2026 da_new += bma->cur->bc_ino.allocated;
2027 bma->cur->bc_ino.allocated = 0;
2030 /* adjust for changes in reserved delayed indirect blocks */
2031 if (da_new != da_old) {
2032 ASSERT(state == 0 || da_new < da_old);
2033 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2037 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2039 if (whichfork != XFS_COW_FORK)
2040 bma->logflags |= rval;
2048 * Convert an unwritten allocation to a real allocation or vice versa.
2051 xfs_bmap_add_extent_unwritten_real(
2052 struct xfs_trans *tp,
2053 xfs_inode_t *ip, /* incore inode pointer */
2055 struct xfs_iext_cursor *icur,
2056 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2057 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2058 int *logflagsp) /* inode logging flags */
2060 xfs_btree_cur_t *cur; /* btree cursor */
2061 int error; /* error return value */
2062 int i; /* temp state */
2063 struct xfs_ifork *ifp; /* inode fork pointer */
2064 xfs_fileoff_t new_endoff; /* end offset of new entry */
2065 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2066 /* left is 0, right is 1, prev is 2 */
2067 int rval=0; /* return value (logging flags) */
2068 int state = xfs_bmap_fork_to_state(whichfork);
2069 struct xfs_mount *mp = ip->i_mount;
2070 struct xfs_bmbt_irec old;
2075 ifp = XFS_IFORK_PTR(ip, whichfork);
2077 ASSERT(!isnullstartblock(new->br_startblock));
2079 XFS_STATS_INC(mp, xs_add_exlist);
2086 * Set up a bunch of variables to make the tests simpler.
2089 xfs_iext_get_extent(ifp, icur, &PREV);
2090 ASSERT(new->br_state != PREV.br_state);
2091 new_endoff = new->br_startoff + new->br_blockcount;
2092 ASSERT(PREV.br_startoff <= new->br_startoff);
2093 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2096 * Set flags determining what part of the previous oldext allocation
2097 * extent is being replaced by a newext allocation.
2099 if (PREV.br_startoff == new->br_startoff)
2100 state |= BMAP_LEFT_FILLING;
2101 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2102 state |= BMAP_RIGHT_FILLING;
2105 * Check and set flags if this segment has a left neighbor.
2106 * Don't set contiguous if the combined extent would be too large.
2108 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2109 state |= BMAP_LEFT_VALID;
2110 if (isnullstartblock(LEFT.br_startblock))
2111 state |= BMAP_LEFT_DELAY;
2114 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2115 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2116 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2117 LEFT.br_state == new->br_state &&
2118 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2119 state |= BMAP_LEFT_CONTIG;
2122 * Check and set flags if this segment has a right neighbor.
2123 * Don't set contiguous if the combined extent would be too large.
2124 * Also check for all-three-contiguous being too large.
2126 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2127 state |= BMAP_RIGHT_VALID;
2128 if (isnullstartblock(RIGHT.br_startblock))
2129 state |= BMAP_RIGHT_DELAY;
2132 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2133 new_endoff == RIGHT.br_startoff &&
2134 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2135 new->br_state == RIGHT.br_state &&
2136 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2137 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2138 BMAP_RIGHT_FILLING)) !=
2139 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2140 BMAP_RIGHT_FILLING) ||
2141 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2143 state |= BMAP_RIGHT_CONTIG;
2146 * Switch out based on the FILLING and CONTIG state bits.
2148 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2149 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2150 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2151 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2153 * Setting all of a previous oldext extent to newext.
2154 * The left and right neighbors are both contiguous with new.
2156 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2158 xfs_iext_remove(ip, icur, state);
2159 xfs_iext_remove(ip, icur, state);
2160 xfs_iext_prev(ifp, icur);
2161 xfs_iext_update_extent(ip, state, icur, &LEFT);
2162 XFS_IFORK_NEXT_SET(ip, whichfork,
2163 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2165 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2167 rval = XFS_ILOG_CORE;
2168 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2171 if (XFS_IS_CORRUPT(mp, i != 1)) {
2172 error = -EFSCORRUPTED;
2175 if ((error = xfs_btree_delete(cur, &i)))
2177 if (XFS_IS_CORRUPT(mp, i != 1)) {
2178 error = -EFSCORRUPTED;
2181 if ((error = xfs_btree_decrement(cur, 0, &i)))
2183 if (XFS_IS_CORRUPT(mp, i != 1)) {
2184 error = -EFSCORRUPTED;
2187 if ((error = xfs_btree_delete(cur, &i)))
2189 if (XFS_IS_CORRUPT(mp, i != 1)) {
2190 error = -EFSCORRUPTED;
2193 if ((error = xfs_btree_decrement(cur, 0, &i)))
2195 if (XFS_IS_CORRUPT(mp, i != 1)) {
2196 error = -EFSCORRUPTED;
2199 error = xfs_bmbt_update(cur, &LEFT);
2205 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2207 * Setting all of a previous oldext extent to newext.
2208 * The left neighbor is contiguous, the right is not.
2210 LEFT.br_blockcount += PREV.br_blockcount;
2212 xfs_iext_remove(ip, icur, state);
2213 xfs_iext_prev(ifp, icur);
2214 xfs_iext_update_extent(ip, state, icur, &LEFT);
2215 XFS_IFORK_NEXT_SET(ip, whichfork,
2216 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2218 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2220 rval = XFS_ILOG_CORE;
2221 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2224 if (XFS_IS_CORRUPT(mp, i != 1)) {
2225 error = -EFSCORRUPTED;
2228 if ((error = xfs_btree_delete(cur, &i)))
2230 if (XFS_IS_CORRUPT(mp, i != 1)) {
2231 error = -EFSCORRUPTED;
2234 if ((error = xfs_btree_decrement(cur, 0, &i)))
2236 if (XFS_IS_CORRUPT(mp, i != 1)) {
2237 error = -EFSCORRUPTED;
2240 error = xfs_bmbt_update(cur, &LEFT);
2246 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2248 * Setting all of a previous oldext extent to newext.
2249 * The right neighbor is contiguous, the left is not.
2251 PREV.br_blockcount += RIGHT.br_blockcount;
2252 PREV.br_state = new->br_state;
2254 xfs_iext_next(ifp, icur);
2255 xfs_iext_remove(ip, icur, state);
2256 xfs_iext_prev(ifp, icur);
2257 xfs_iext_update_extent(ip, state, icur, &PREV);
2259 XFS_IFORK_NEXT_SET(ip, whichfork,
2260 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2262 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2264 rval = XFS_ILOG_CORE;
2265 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2268 if (XFS_IS_CORRUPT(mp, i != 1)) {
2269 error = -EFSCORRUPTED;
2272 if ((error = xfs_btree_delete(cur, &i)))
2274 if (XFS_IS_CORRUPT(mp, i != 1)) {
2275 error = -EFSCORRUPTED;
2278 if ((error = xfs_btree_decrement(cur, 0, &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_RIGHT_FILLING:
2292 * Setting all of a previous oldext extent to newext.
2293 * Neither the left nor right neighbors are contiguous with
2296 PREV.br_state = new->br_state;
2297 xfs_iext_update_extent(ip, state, icur, &PREV);
2300 rval = XFS_ILOG_DEXT;
2303 error = xfs_bmbt_lookup_eq(cur, new, &i);
2306 if (XFS_IS_CORRUPT(mp, i != 1)) {
2307 error = -EFSCORRUPTED;
2310 error = xfs_bmbt_update(cur, &PREV);
2316 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2318 * Setting the first part of a previous oldext extent to newext.
2319 * The left neighbor is contiguous.
2321 LEFT.br_blockcount += new->br_blockcount;
2324 PREV.br_startoff += new->br_blockcount;
2325 PREV.br_startblock += new->br_blockcount;
2326 PREV.br_blockcount -= new->br_blockcount;
2328 xfs_iext_update_extent(ip, state, icur, &PREV);
2329 xfs_iext_prev(ifp, icur);
2330 xfs_iext_update_extent(ip, state, icur, &LEFT);
2333 rval = XFS_ILOG_DEXT;
2336 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2339 if (XFS_IS_CORRUPT(mp, i != 1)) {
2340 error = -EFSCORRUPTED;
2343 error = xfs_bmbt_update(cur, &PREV);
2346 error = xfs_btree_decrement(cur, 0, &i);
2349 error = xfs_bmbt_update(cur, &LEFT);
2355 case BMAP_LEFT_FILLING:
2357 * Setting the first part of a previous oldext extent to newext.
2358 * The left neighbor is not contiguous.
2361 PREV.br_startoff += new->br_blockcount;
2362 PREV.br_startblock += new->br_blockcount;
2363 PREV.br_blockcount -= new->br_blockcount;
2365 xfs_iext_update_extent(ip, state, icur, &PREV);
2366 xfs_iext_insert(ip, icur, new, state);
2367 XFS_IFORK_NEXT_SET(ip, whichfork,
2368 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2370 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2372 rval = XFS_ILOG_CORE;
2373 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2376 if (XFS_IS_CORRUPT(mp, i != 1)) {
2377 error = -EFSCORRUPTED;
2380 error = xfs_bmbt_update(cur, &PREV);
2383 cur->bc_rec.b = *new;
2384 if ((error = xfs_btree_insert(cur, &i)))
2386 if (XFS_IS_CORRUPT(mp, i != 1)) {
2387 error = -EFSCORRUPTED;
2393 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2395 * Setting the last part of a previous oldext extent to newext.
2396 * The right neighbor is contiguous with the new allocation.
2399 PREV.br_blockcount -= new->br_blockcount;
2401 RIGHT.br_startoff = new->br_startoff;
2402 RIGHT.br_startblock = new->br_startblock;
2403 RIGHT.br_blockcount += new->br_blockcount;
2405 xfs_iext_update_extent(ip, state, icur, &PREV);
2406 xfs_iext_next(ifp, icur);
2407 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2410 rval = XFS_ILOG_DEXT;
2413 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2416 if (XFS_IS_CORRUPT(mp, i != 1)) {
2417 error = -EFSCORRUPTED;
2420 error = xfs_bmbt_update(cur, &PREV);
2423 error = xfs_btree_increment(cur, 0, &i);
2426 error = xfs_bmbt_update(cur, &RIGHT);
2432 case BMAP_RIGHT_FILLING:
2434 * Setting the last part of a previous oldext extent to newext.
2435 * The right neighbor is not contiguous.
2438 PREV.br_blockcount -= new->br_blockcount;
2440 xfs_iext_update_extent(ip, state, icur, &PREV);
2441 xfs_iext_next(ifp, icur);
2442 xfs_iext_insert(ip, icur, new, state);
2444 XFS_IFORK_NEXT_SET(ip, whichfork,
2445 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2447 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2449 rval = XFS_ILOG_CORE;
2450 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2453 if (XFS_IS_CORRUPT(mp, i != 1)) {
2454 error = -EFSCORRUPTED;
2457 error = xfs_bmbt_update(cur, &PREV);
2460 error = xfs_bmbt_lookup_eq(cur, new, &i);
2463 if (XFS_IS_CORRUPT(mp, i != 0)) {
2464 error = -EFSCORRUPTED;
2467 if ((error = xfs_btree_insert(cur, &i)))
2469 if (XFS_IS_CORRUPT(mp, i != 1)) {
2470 error = -EFSCORRUPTED;
2478 * Setting the middle part of a previous oldext extent to
2479 * newext. Contiguity is impossible here.
2480 * One extent becomes three extents.
2483 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2486 r[1].br_startoff = new_endoff;
2487 r[1].br_blockcount =
2488 old.br_startoff + old.br_blockcount - new_endoff;
2489 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2490 r[1].br_state = PREV.br_state;
2492 xfs_iext_update_extent(ip, state, icur, &PREV);
2493 xfs_iext_next(ifp, icur);
2494 xfs_iext_insert(ip, icur, &r[1], state);
2495 xfs_iext_insert(ip, icur, &r[0], state);
2497 XFS_IFORK_NEXT_SET(ip, whichfork,
2498 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2500 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2502 rval = XFS_ILOG_CORE;
2503 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2506 if (XFS_IS_CORRUPT(mp, i != 1)) {
2507 error = -EFSCORRUPTED;
2510 /* new right extent - oldext */
2511 error = xfs_bmbt_update(cur, &r[1]);
2514 /* new left extent - oldext */
2515 cur->bc_rec.b = PREV;
2516 if ((error = xfs_btree_insert(cur, &i)))
2518 if (XFS_IS_CORRUPT(mp, i != 1)) {
2519 error = -EFSCORRUPTED;
2523 * Reset the cursor to the position of the new extent
2524 * we are about to insert as we can't trust it after
2525 * the previous insert.
2527 error = xfs_bmbt_lookup_eq(cur, new, &i);
2530 if (XFS_IS_CORRUPT(mp, i != 0)) {
2531 error = -EFSCORRUPTED;
2534 /* new middle extent - newext */
2535 if ((error = xfs_btree_insert(cur, &i)))
2537 if (XFS_IS_CORRUPT(mp, i != 1)) {
2538 error = -EFSCORRUPTED;
2544 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2545 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2546 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2547 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2548 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2549 case BMAP_LEFT_CONTIG:
2550 case BMAP_RIGHT_CONTIG:
2552 * These cases are all impossible.
2557 /* update reverse mappings */
2558 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2560 /* convert to a btree if necessary */
2561 if (xfs_bmap_needs_btree(ip, whichfork)) {
2562 int tmp_logflags; /* partial log flag return val */
2564 ASSERT(cur == NULL);
2565 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2566 &tmp_logflags, whichfork);
2567 *logflagsp |= tmp_logflags;
2572 /* clear out the allocated field, done with it now in any case. */
2574 cur->bc_ino.allocated = 0;
2578 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2588 * Convert a hole to a delayed allocation.
2591 xfs_bmap_add_extent_hole_delay(
2592 xfs_inode_t *ip, /* incore inode pointer */
2594 struct xfs_iext_cursor *icur,
2595 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2597 struct xfs_ifork *ifp; /* inode fork pointer */
2598 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2599 xfs_filblks_t newlen=0; /* new indirect size */
2600 xfs_filblks_t oldlen=0; /* old indirect size */
2601 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2602 int state = xfs_bmap_fork_to_state(whichfork);
2603 xfs_filblks_t temp; /* temp for indirect calculations */
2605 ifp = XFS_IFORK_PTR(ip, whichfork);
2606 ASSERT(isnullstartblock(new->br_startblock));
2609 * Check and set flags if this segment has a left neighbor
2611 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2612 state |= BMAP_LEFT_VALID;
2613 if (isnullstartblock(left.br_startblock))
2614 state |= BMAP_LEFT_DELAY;
2618 * Check and set flags if the current (right) segment exists.
2619 * If it doesn't exist, we're converting the hole at end-of-file.
2621 if (xfs_iext_get_extent(ifp, icur, &right)) {
2622 state |= BMAP_RIGHT_VALID;
2623 if (isnullstartblock(right.br_startblock))
2624 state |= BMAP_RIGHT_DELAY;
2628 * Set contiguity flags on the left and right neighbors.
2629 * Don't let extents get too large, even if the pieces are contiguous.
2631 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2632 left.br_startoff + left.br_blockcount == new->br_startoff &&
2633 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2634 state |= BMAP_LEFT_CONTIG;
2636 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2637 new->br_startoff + new->br_blockcount == right.br_startoff &&
2638 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2639 (!(state & BMAP_LEFT_CONTIG) ||
2640 (left.br_blockcount + new->br_blockcount +
2641 right.br_blockcount <= MAXEXTLEN)))
2642 state |= BMAP_RIGHT_CONTIG;
2645 * Switch out based on the contiguity flags.
2647 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2648 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2650 * New allocation is contiguous with delayed allocations
2651 * on the left and on the right.
2652 * Merge all three into a single extent record.
2654 temp = left.br_blockcount + new->br_blockcount +
2655 right.br_blockcount;
2657 oldlen = startblockval(left.br_startblock) +
2658 startblockval(new->br_startblock) +
2659 startblockval(right.br_startblock);
2660 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2662 left.br_startblock = nullstartblock(newlen);
2663 left.br_blockcount = temp;
2665 xfs_iext_remove(ip, icur, state);
2666 xfs_iext_prev(ifp, icur);
2667 xfs_iext_update_extent(ip, state, icur, &left);
2670 case BMAP_LEFT_CONTIG:
2672 * New allocation is contiguous with a delayed allocation
2674 * Merge the new allocation with the left neighbor.
2676 temp = left.br_blockcount + new->br_blockcount;
2678 oldlen = startblockval(left.br_startblock) +
2679 startblockval(new->br_startblock);
2680 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2682 left.br_blockcount = temp;
2683 left.br_startblock = nullstartblock(newlen);
2685 xfs_iext_prev(ifp, icur);
2686 xfs_iext_update_extent(ip, state, icur, &left);
2689 case BMAP_RIGHT_CONTIG:
2691 * New allocation is contiguous with a delayed allocation
2693 * Merge the new allocation with the right neighbor.
2695 temp = new->br_blockcount + right.br_blockcount;
2696 oldlen = startblockval(new->br_startblock) +
2697 startblockval(right.br_startblock);
2698 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2700 right.br_startoff = new->br_startoff;
2701 right.br_startblock = nullstartblock(newlen);
2702 right.br_blockcount = temp;
2703 xfs_iext_update_extent(ip, state, icur, &right);
2708 * New allocation is not contiguous with another
2709 * delayed allocation.
2710 * Insert a new entry.
2712 oldlen = newlen = 0;
2713 xfs_iext_insert(ip, icur, new, state);
2716 if (oldlen != newlen) {
2717 ASSERT(oldlen > newlen);
2718 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2721 * Nothing to do for disk quota accounting here.
2723 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2728 * Convert a hole to a real allocation.
2730 STATIC int /* error */
2731 xfs_bmap_add_extent_hole_real(
2732 struct xfs_trans *tp,
2733 struct xfs_inode *ip,
2735 struct xfs_iext_cursor *icur,
2736 struct xfs_btree_cur **curp,
2737 struct xfs_bmbt_irec *new,
2741 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2742 struct xfs_mount *mp = ip->i_mount;
2743 struct xfs_btree_cur *cur = *curp;
2744 int error; /* error return value */
2745 int i; /* temp state */
2746 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2747 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2748 int rval=0; /* return value (logging flags) */
2749 int state = xfs_bmap_fork_to_state(whichfork);
2750 struct xfs_bmbt_irec old;
2752 ASSERT(!isnullstartblock(new->br_startblock));
2753 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2755 XFS_STATS_INC(mp, xs_add_exlist);
2758 * Check and set flags if this segment has a left neighbor.
2760 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2761 state |= BMAP_LEFT_VALID;
2762 if (isnullstartblock(left.br_startblock))
2763 state |= BMAP_LEFT_DELAY;
2767 * Check and set flags if this segment has a current value.
2768 * Not true if we're inserting into the "hole" at eof.
2770 if (xfs_iext_get_extent(ifp, icur, &right)) {
2771 state |= BMAP_RIGHT_VALID;
2772 if (isnullstartblock(right.br_startblock))
2773 state |= BMAP_RIGHT_DELAY;
2777 * We're inserting a real allocation between "left" and "right".
2778 * Set the contiguity flags. Don't let extents get too large.
2780 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2781 left.br_startoff + left.br_blockcount == new->br_startoff &&
2782 left.br_startblock + left.br_blockcount == new->br_startblock &&
2783 left.br_state == new->br_state &&
2784 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2785 state |= BMAP_LEFT_CONTIG;
2787 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2788 new->br_startoff + new->br_blockcount == right.br_startoff &&
2789 new->br_startblock + new->br_blockcount == right.br_startblock &&
2790 new->br_state == right.br_state &&
2791 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2792 (!(state & BMAP_LEFT_CONTIG) ||
2793 left.br_blockcount + new->br_blockcount +
2794 right.br_blockcount <= MAXEXTLEN))
2795 state |= BMAP_RIGHT_CONTIG;
2799 * Select which case we're in here, and implement it.
2801 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2802 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2804 * New allocation is contiguous with real allocations on the
2805 * left and on the right.
2806 * Merge all three into a single extent record.
2808 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2810 xfs_iext_remove(ip, icur, state);
2811 xfs_iext_prev(ifp, icur);
2812 xfs_iext_update_extent(ip, state, icur, &left);
2814 XFS_IFORK_NEXT_SET(ip, whichfork,
2815 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2817 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2819 rval = XFS_ILOG_CORE;
2820 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2823 if (XFS_IS_CORRUPT(mp, i != 1)) {
2824 error = -EFSCORRUPTED;
2827 error = xfs_btree_delete(cur, &i);
2830 if (XFS_IS_CORRUPT(mp, i != 1)) {
2831 error = -EFSCORRUPTED;
2834 error = xfs_btree_decrement(cur, 0, &i);
2837 if (XFS_IS_CORRUPT(mp, i != 1)) {
2838 error = -EFSCORRUPTED;
2841 error = xfs_bmbt_update(cur, &left);
2847 case BMAP_LEFT_CONTIG:
2849 * New allocation is contiguous with a real allocation
2851 * Merge the new allocation with the left neighbor.
2854 left.br_blockcount += new->br_blockcount;
2856 xfs_iext_prev(ifp, icur);
2857 xfs_iext_update_extent(ip, state, icur, &left);
2860 rval = xfs_ilog_fext(whichfork);
2863 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2866 if (XFS_IS_CORRUPT(mp, i != 1)) {
2867 error = -EFSCORRUPTED;
2870 error = xfs_bmbt_update(cur, &left);
2876 case BMAP_RIGHT_CONTIG:
2878 * New allocation is contiguous with a real allocation
2880 * Merge the new allocation with the right neighbor.
2884 right.br_startoff = new->br_startoff;
2885 right.br_startblock = new->br_startblock;
2886 right.br_blockcount += new->br_blockcount;
2887 xfs_iext_update_extent(ip, state, icur, &right);
2890 rval = xfs_ilog_fext(whichfork);
2893 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2896 if (XFS_IS_CORRUPT(mp, i != 1)) {
2897 error = -EFSCORRUPTED;
2900 error = xfs_bmbt_update(cur, &right);
2908 * New allocation is not contiguous with another
2910 * Insert a new entry.
2912 xfs_iext_insert(ip, icur, new, state);
2913 XFS_IFORK_NEXT_SET(ip, whichfork,
2914 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2916 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2918 rval = XFS_ILOG_CORE;
2919 error = xfs_bmbt_lookup_eq(cur, new, &i);
2922 if (XFS_IS_CORRUPT(mp, i != 0)) {
2923 error = -EFSCORRUPTED;
2926 error = xfs_btree_insert(cur, &i);
2929 if (XFS_IS_CORRUPT(mp, i != 1)) {
2930 error = -EFSCORRUPTED;
2937 /* add reverse mapping unless caller opted out */
2938 if (!(flags & XFS_BMAPI_NORMAP))
2939 xfs_rmap_map_extent(tp, ip, whichfork, new);
2941 /* convert to a btree if necessary */
2942 if (xfs_bmap_needs_btree(ip, whichfork)) {
2943 int tmp_logflags; /* partial log flag return val */
2945 ASSERT(cur == NULL);
2946 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2947 &tmp_logflags, whichfork);
2948 *logflagsp |= tmp_logflags;
2954 /* clear out the allocated field, done with it now in any case. */
2956 cur->bc_ino.allocated = 0;
2958 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2965 * Functions used in the extent read, allocate and remove paths
2969 * Adjust the size of the new extent based on di_extsize and rt extsize.
2972 xfs_bmap_extsize_align(
2974 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2975 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2976 xfs_extlen_t extsz, /* align to this extent size */
2977 int rt, /* is this a realtime inode? */
2978 int eof, /* is extent at end-of-file? */
2979 int delay, /* creating delalloc extent? */
2980 int convert, /* overwriting unwritten extent? */
2981 xfs_fileoff_t *offp, /* in/out: aligned offset */
2982 xfs_extlen_t *lenp) /* in/out: aligned length */
2984 xfs_fileoff_t orig_off; /* original offset */
2985 xfs_extlen_t orig_alen; /* original length */
2986 xfs_fileoff_t orig_end; /* original off+len */
2987 xfs_fileoff_t nexto; /* next file offset */
2988 xfs_fileoff_t prevo; /* previous file offset */
2989 xfs_fileoff_t align_off; /* temp for offset */
2990 xfs_extlen_t align_alen; /* temp for length */
2991 xfs_extlen_t temp; /* temp for calculations */
2996 orig_off = align_off = *offp;
2997 orig_alen = align_alen = *lenp;
2998 orig_end = orig_off + orig_alen;
3001 * If this request overlaps an existing extent, then don't
3002 * attempt to perform any additional alignment.
3004 if (!delay && !eof &&
3005 (orig_off >= gotp->br_startoff) &&
3006 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3011 * If the file offset is unaligned vs. the extent size
3012 * we need to align it. This will be possible unless
3013 * the file was previously written with a kernel that didn't
3014 * perform this alignment, or if a truncate shot us in the
3017 div_u64_rem(orig_off, extsz, &temp);
3023 /* Same adjustment for the end of the requested area. */
3024 temp = (align_alen % extsz);
3026 align_alen += extsz - temp;
3029 * For large extent hint sizes, the aligned extent might be larger than
3030 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3031 * the length back under MAXEXTLEN. The outer allocation loops handle
3032 * short allocation just fine, so it is safe to do this. We only want to
3033 * do it when we are forced to, though, because it means more allocation
3034 * operations are required.
3036 while (align_alen > MAXEXTLEN)
3037 align_alen -= extsz;
3038 ASSERT(align_alen <= MAXEXTLEN);
3041 * If the previous block overlaps with this proposed allocation
3042 * then move the start forward without adjusting the length.
3044 if (prevp->br_startoff != NULLFILEOFF) {
3045 if (prevp->br_startblock == HOLESTARTBLOCK)
3046 prevo = prevp->br_startoff;
3048 prevo = prevp->br_startoff + prevp->br_blockcount;
3051 if (align_off != orig_off && align_off < prevo)
3054 * If the next block overlaps with this proposed allocation
3055 * then move the start back without adjusting the length,
3056 * but not before offset 0.
3057 * This may of course make the start overlap previous block,
3058 * and if we hit the offset 0 limit then the next block
3059 * can still overlap too.
3061 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3062 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3063 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3064 nexto = gotp->br_startoff + gotp->br_blockcount;
3066 nexto = gotp->br_startoff;
3068 nexto = NULLFILEOFF;
3070 align_off + align_alen != orig_end &&
3071 align_off + align_alen > nexto)
3072 align_off = nexto > align_alen ? nexto - align_alen : 0;
3074 * If we're now overlapping the next or previous extent that
3075 * means we can't fit an extsz piece in this hole. Just move
3076 * the start forward to the first valid spot and set
3077 * the length so we hit the end.
3079 if (align_off != orig_off && align_off < prevo)
3081 if (align_off + align_alen != orig_end &&
3082 align_off + align_alen > nexto &&
3083 nexto != NULLFILEOFF) {
3084 ASSERT(nexto > prevo);
3085 align_alen = nexto - align_off;
3089 * If realtime, and the result isn't a multiple of the realtime
3090 * extent size we need to remove blocks until it is.
3092 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3094 * We're not covering the original request, or
3095 * we won't be able to once we fix the length.
3097 if (orig_off < align_off ||
3098 orig_end > align_off + align_alen ||
3099 align_alen - temp < orig_alen)
3102 * Try to fix it by moving the start up.
3104 if (align_off + temp <= orig_off) {
3109 * Try to fix it by moving the end in.
3111 else if (align_off + align_alen - temp >= orig_end)
3114 * Set the start to the minimum then trim the length.
3117 align_alen -= orig_off - align_off;
3118 align_off = orig_off;
3119 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3122 * Result doesn't cover the request, fail it.
3124 if (orig_off < align_off || orig_end > align_off + align_alen)
3127 ASSERT(orig_off >= align_off);
3128 /* see MAXEXTLEN handling above */
3129 ASSERT(orig_end <= align_off + align_alen ||
3130 align_alen + extsz > MAXEXTLEN);
3134 if (!eof && gotp->br_startoff != NULLFILEOFF)
3135 ASSERT(align_off + align_alen <= gotp->br_startoff);
3136 if (prevp->br_startoff != NULLFILEOFF)
3137 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3145 #define XFS_ALLOC_GAP_UNITS 4
3149 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3151 xfs_fsblock_t adjust; /* adjustment to block numbers */
3152 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3153 xfs_mount_t *mp; /* mount point structure */
3154 int nullfb; /* true if ap->firstblock isn't set */
3155 int rt; /* true if inode is realtime */
3157 #define ISVALID(x,y) \
3159 (x) < mp->m_sb.sb_rblocks : \
3160 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3161 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3162 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3164 mp = ap->ip->i_mount;
3165 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3166 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3167 (ap->datatype & XFS_ALLOC_USERDATA);
3168 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3169 ap->tp->t_firstblock);
3171 * If allocating at eof, and there's a previous real block,
3172 * try to use its last block as our starting point.
3174 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3175 !isnullstartblock(ap->prev.br_startblock) &&
3176 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3177 ap->prev.br_startblock)) {
3178 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3180 * Adjust for the gap between prevp and us.
3182 adjust = ap->offset -
3183 (ap->prev.br_startoff + ap->prev.br_blockcount);
3185 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3186 ap->blkno += adjust;
3189 * If not at eof, then compare the two neighbor blocks.
3190 * Figure out whether either one gives us a good starting point,
3191 * and pick the better one.
3193 else if (!ap->eof) {
3194 xfs_fsblock_t gotbno; /* right side block number */
3195 xfs_fsblock_t gotdiff=0; /* right side difference */
3196 xfs_fsblock_t prevbno; /* left side block number */
3197 xfs_fsblock_t prevdiff=0; /* left side difference */
3200 * If there's a previous (left) block, select a requested
3201 * start block based on it.
3203 if (ap->prev.br_startoff != NULLFILEOFF &&
3204 !isnullstartblock(ap->prev.br_startblock) &&
3205 (prevbno = ap->prev.br_startblock +
3206 ap->prev.br_blockcount) &&
3207 ISVALID(prevbno, ap->prev.br_startblock)) {
3209 * Calculate gap to end of previous block.
3211 adjust = prevdiff = ap->offset -
3212 (ap->prev.br_startoff +
3213 ap->prev.br_blockcount);
3215 * Figure the startblock based on the previous block's
3216 * end and the gap size.
3218 * If the gap is large relative to the piece we're
3219 * allocating, or using it gives us an invalid block
3220 * number, then just use the end of the previous block.
3222 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3223 ISVALID(prevbno + prevdiff,
3224 ap->prev.br_startblock))
3229 * If the firstblock forbids it, can't use it,
3232 if (!rt && !nullfb &&
3233 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3234 prevbno = NULLFSBLOCK;
3237 * No previous block or can't follow it, just default.
3240 prevbno = NULLFSBLOCK;
3242 * If there's a following (right) block, select a requested
3243 * start block based on it.
3245 if (!isnullstartblock(ap->got.br_startblock)) {
3247 * Calculate gap to start of next block.
3249 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3251 * Figure the startblock based on the next block's
3252 * start and the gap size.
3254 gotbno = ap->got.br_startblock;
3257 * If the gap is large relative to the piece we're
3258 * allocating, or using it gives us an invalid block
3259 * number, then just use the start of the next block
3260 * offset by our length.
3262 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3263 ISVALID(gotbno - gotdiff, gotbno))
3265 else if (ISVALID(gotbno - ap->length, gotbno)) {
3266 gotbno -= ap->length;
3267 gotdiff += adjust - ap->length;
3271 * If the firstblock forbids it, can't use it,
3274 if (!rt && !nullfb &&
3275 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3276 gotbno = NULLFSBLOCK;
3279 * No next block, just default.
3282 gotbno = NULLFSBLOCK;
3284 * If both valid, pick the better one, else the only good
3285 * one, else ap->blkno is already set (to 0 or the inode block).
3287 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3288 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3289 else if (prevbno != NULLFSBLOCK)
3290 ap->blkno = prevbno;
3291 else if (gotbno != NULLFSBLOCK)
3298 xfs_bmap_longest_free_extent(
3299 struct xfs_trans *tp,
3304 struct xfs_mount *mp = tp->t_mountp;
3305 struct xfs_perag *pag;
3306 xfs_extlen_t longest;
3309 pag = xfs_perag_get(mp, ag);
3310 if (!pag->pagf_init) {
3311 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3313 /* Couldn't lock the AGF, so skip this AG. */
3314 if (error == -EAGAIN) {
3322 longest = xfs_alloc_longest_free_extent(pag,
3323 xfs_alloc_min_freelist(mp, pag),
3324 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3325 if (*blen < longest)
3334 xfs_bmap_select_minlen(
3335 struct xfs_bmalloca *ap,
3336 struct xfs_alloc_arg *args,
3340 if (notinit || *blen < ap->minlen) {
3342 * Since we did a BUF_TRYLOCK above, it is possible that
3343 * there is space for this request.
3345 args->minlen = ap->minlen;
3346 } else if (*blen < args->maxlen) {
3348 * If the best seen length is less than the request length,
3349 * use the best as the minimum.
3351 args->minlen = *blen;
3354 * Otherwise we've seen an extent as big as maxlen, use that
3357 args->minlen = args->maxlen;
3362 xfs_bmap_btalloc_nullfb(
3363 struct xfs_bmalloca *ap,
3364 struct xfs_alloc_arg *args,
3367 struct xfs_mount *mp = ap->ip->i_mount;
3368 xfs_agnumber_t ag, startag;
3372 args->type = XFS_ALLOCTYPE_START_BNO;
3373 args->total = ap->total;
3375 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3376 if (startag == NULLAGNUMBER)
3379 while (*blen < args->maxlen) {
3380 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3385 if (++ag == mp->m_sb.sb_agcount)
3391 xfs_bmap_select_minlen(ap, args, blen, notinit);
3396 xfs_bmap_btalloc_filestreams(
3397 struct xfs_bmalloca *ap,
3398 struct xfs_alloc_arg *args,
3401 struct xfs_mount *mp = ap->ip->i_mount;
3406 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3407 args->total = ap->total;
3409 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3410 if (ag == NULLAGNUMBER)
3413 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3417 if (*blen < args->maxlen) {
3418 error = xfs_filestream_new_ag(ap, &ag);
3422 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3429 xfs_bmap_select_minlen(ap, args, blen, notinit);
3432 * Set the failure fallback case to look in the selected AG as stream
3435 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3439 /* Update all inode and quota accounting for the allocation we just did. */
3441 xfs_bmap_btalloc_accounting(
3442 struct xfs_bmalloca *ap,
3443 struct xfs_alloc_arg *args)
3445 if (ap->flags & XFS_BMAPI_COWFORK) {
3447 * COW fork blocks are in-core only and thus are treated as
3448 * in-core quota reservation (like delalloc blocks) even when
3449 * converted to real blocks. The quota reservation is not
3450 * accounted to disk until blocks are remapped to the data
3451 * fork. So if these blocks were previously delalloc, we
3452 * already have quota reservation and there's nothing to do
3456 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3461 * Otherwise, we've allocated blocks in a hole. The transaction
3462 * has acquired in-core quota reservation for this extent.
3463 * Rather than account these as real blocks, however, we reduce
3464 * the transaction quota reservation based on the allocation.
3465 * This essentially transfers the transaction quota reservation
3466 * to that of a delalloc extent.
3468 ap->ip->i_delayed_blks += args->len;
3469 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3474 /* data/attr fork only */
3475 ap->ip->i_d.di_nblocks += args->len;
3476 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3478 ap->ip->i_delayed_blks -= args->len;
3479 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3481 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3482 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3488 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3490 xfs_mount_t *mp; /* mount point structure */
3491 xfs_alloctype_t atype = 0; /* type for allocation routines */
3492 xfs_extlen_t align = 0; /* minimum allocation alignment */
3493 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3495 xfs_alloc_arg_t args;
3496 xfs_fileoff_t orig_offset;
3497 xfs_extlen_t orig_length;
3499 xfs_extlen_t nextminlen = 0;
3500 int nullfb; /* true if ap->firstblock isn't set */
3507 orig_offset = ap->offset;
3508 orig_length = ap->length;
3510 mp = ap->ip->i_mount;
3512 /* stripe alignment for allocation is determined by mount parameters */
3514 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3515 stripe_align = mp->m_swidth;
3516 else if (mp->m_dalign)
3517 stripe_align = mp->m_dalign;
3519 if (ap->flags & XFS_BMAPI_COWFORK)
3520 align = xfs_get_cowextsz_hint(ap->ip);
3521 else if (ap->datatype & XFS_ALLOC_USERDATA)
3522 align = xfs_get_extsz_hint(ap->ip);
3524 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3525 align, 0, ap->eof, 0, ap->conv,
3526 &ap->offset, &ap->length);
3532 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3533 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3534 ap->tp->t_firstblock);
3536 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3537 xfs_inode_is_filestream(ap->ip)) {
3538 ag = xfs_filestream_lookup_ag(ap->ip);
3539 ag = (ag != NULLAGNUMBER) ? ag : 0;
3540 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3542 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3545 ap->blkno = ap->tp->t_firstblock;
3547 xfs_bmap_adjacent(ap);
3550 * If allowed, use ap->blkno; otherwise must use firstblock since
3551 * it's in the right allocation group.
3553 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3556 ap->blkno = ap->tp->t_firstblock;
3558 * Normal allocation, done through xfs_alloc_vextent.
3560 tryagain = isaligned = 0;
3561 memset(&args, 0, sizeof(args));
3564 args.fsbno = ap->blkno;
3565 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3567 /* Trim the allocation back to the maximum an AG can fit. */
3568 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3572 * Search for an allocation group with a single extent large
3573 * enough for the request. If one isn't found, then adjust
3574 * the minimum allocation size to the largest space found.
3576 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3577 xfs_inode_is_filestream(ap->ip))
3578 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3580 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3583 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3584 if (xfs_inode_is_filestream(ap->ip))
3585 args.type = XFS_ALLOCTYPE_FIRST_AG;
3587 args.type = XFS_ALLOCTYPE_START_BNO;
3588 args.total = args.minlen = ap->minlen;
3590 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3591 args.total = ap->total;
3592 args.minlen = ap->minlen;
3594 /* apply extent size hints if obtained earlier */
3597 div_u64_rem(ap->offset, args.prod, &args.mod);
3599 args.mod = args.prod - args.mod;
3600 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3604 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3605 div_u64_rem(ap->offset, args.prod, &args.mod);
3607 args.mod = args.prod - args.mod;
3610 * If we are not low on available data blocks, and the underlying
3611 * logical volume manager is a stripe, and the file offset is zero then
3612 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3613 * is only set if the allocation length is >= the stripe unit and the
3614 * allocation offset is at the end of file.
3616 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3618 args.alignment = stripe_align;
3622 * Adjust minlen to try and preserve alignment if we
3623 * can't guarantee an aligned maxlen extent.
3625 if (blen > args.alignment &&
3626 blen <= args.maxlen + args.alignment)
3627 args.minlen = blen - args.alignment;
3628 args.minalignslop = 0;
3631 * First try an exact bno allocation.
3632 * If it fails then do a near or start bno
3633 * allocation with alignment turned on.
3637 args.type = XFS_ALLOCTYPE_THIS_BNO;
3640 * Compute the minlen+alignment for the
3641 * next case. Set slop so that the value
3642 * of minlen+alignment+slop doesn't go up
3643 * between the calls.
3645 if (blen > stripe_align && blen <= args.maxlen)
3646 nextminlen = blen - stripe_align;
3648 nextminlen = args.minlen;
3649 if (nextminlen + stripe_align > args.minlen + 1)
3651 nextminlen + stripe_align -
3654 args.minalignslop = 0;
3658 args.minalignslop = 0;
3660 args.minleft = ap->minleft;
3661 args.wasdel = ap->wasdel;
3662 args.resv = XFS_AG_RESV_NONE;
3663 args.datatype = ap->datatype;
3665 error = xfs_alloc_vextent(&args);
3669 if (tryagain && args.fsbno == NULLFSBLOCK) {
3671 * Exact allocation failed. Now try with alignment
3675 args.fsbno = ap->blkno;
3676 args.alignment = stripe_align;
3677 args.minlen = nextminlen;
3678 args.minalignslop = 0;
3680 if ((error = xfs_alloc_vextent(&args)))
3683 if (isaligned && args.fsbno == NULLFSBLOCK) {
3685 * allocation failed, so turn off alignment and
3689 args.fsbno = ap->blkno;
3691 if ((error = xfs_alloc_vextent(&args)))
3694 if (args.fsbno == NULLFSBLOCK && nullfb &&
3695 args.minlen > ap->minlen) {
3696 args.minlen = ap->minlen;
3697 args.type = XFS_ALLOCTYPE_START_BNO;
3698 args.fsbno = ap->blkno;
3699 if ((error = xfs_alloc_vextent(&args)))
3702 if (args.fsbno == NULLFSBLOCK && nullfb) {
3704 args.type = XFS_ALLOCTYPE_FIRST_AG;
3705 args.total = ap->minlen;
3706 if ((error = xfs_alloc_vextent(&args)))
3708 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3710 if (args.fsbno != NULLFSBLOCK) {
3712 * check the allocation happened at the same or higher AG than
3713 * the first block that was allocated.
3715 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3716 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
3717 XFS_FSB_TO_AGNO(mp, args.fsbno));
3719 ap->blkno = args.fsbno;
3720 if (ap->tp->t_firstblock == NULLFSBLOCK)
3721 ap->tp->t_firstblock = args.fsbno;
3722 ASSERT(nullfb || fb_agno <= args.agno);
3723 ap->length = args.len;
3725 * If the extent size hint is active, we tried to round the
3726 * caller's allocation request offset down to extsz and the
3727 * length up to another extsz boundary. If we found a free
3728 * extent we mapped it in starting at this new offset. If the
3729 * newly mapped space isn't long enough to cover any of the
3730 * range of offsets that was originally requested, move the
3731 * mapping up so that we can fill as much of the caller's
3732 * original request as possible. Free space is apparently
3733 * very fragmented so we're unlikely to be able to satisfy the
3736 if (ap->length <= orig_length)
3737 ap->offset = orig_offset;
3738 else if (ap->offset + ap->length < orig_offset + orig_length)
3739 ap->offset = orig_offset + orig_length - ap->length;
3740 xfs_bmap_btalloc_accounting(ap, &args);
3742 ap->blkno = NULLFSBLOCK;
3748 /* Trim extent to fit a logical block range. */
3751 struct xfs_bmbt_irec *irec,
3755 xfs_fileoff_t distance;
3756 xfs_fileoff_t end = bno + len;
3758 if (irec->br_startoff + irec->br_blockcount <= bno ||
3759 irec->br_startoff >= end) {
3760 irec->br_blockcount = 0;
3764 if (irec->br_startoff < bno) {
3765 distance = bno - irec->br_startoff;
3766 if (isnullstartblock(irec->br_startblock))
3767 irec->br_startblock = DELAYSTARTBLOCK;
3768 if (irec->br_startblock != DELAYSTARTBLOCK &&
3769 irec->br_startblock != HOLESTARTBLOCK)
3770 irec->br_startblock += distance;
3771 irec->br_startoff += distance;
3772 irec->br_blockcount -= distance;
3775 if (end < irec->br_startoff + irec->br_blockcount) {
3776 distance = irec->br_startoff + irec->br_blockcount - end;
3777 irec->br_blockcount -= distance;
3782 * Trim the returned map to the required bounds
3786 struct xfs_bmbt_irec *mval,
3787 struct xfs_bmbt_irec *got,
3795 if ((flags & XFS_BMAPI_ENTIRE) ||
3796 got->br_startoff + got->br_blockcount <= obno) {
3798 if (isnullstartblock(got->br_startblock))
3799 mval->br_startblock = DELAYSTARTBLOCK;
3805 ASSERT((*bno >= obno) || (n == 0));
3807 mval->br_startoff = *bno;
3808 if (isnullstartblock(got->br_startblock))
3809 mval->br_startblock = DELAYSTARTBLOCK;
3811 mval->br_startblock = got->br_startblock +
3812 (*bno - got->br_startoff);
3814 * Return the minimum of what we got and what we asked for for
3815 * the length. We can use the len variable here because it is
3816 * modified below and we could have been there before coming
3817 * here if the first part of the allocation didn't overlap what
3820 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3821 got->br_blockcount - (*bno - got->br_startoff));
3822 mval->br_state = got->br_state;
3823 ASSERT(mval->br_blockcount <= len);
3828 * Update and validate the extent map to return
3831 xfs_bmapi_update_map(
3832 struct xfs_bmbt_irec **map,
3840 xfs_bmbt_irec_t *mval = *map;
3842 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3843 ((mval->br_startoff + mval->br_blockcount) <= end));
3844 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3845 (mval->br_startoff < obno));
3847 *bno = mval->br_startoff + mval->br_blockcount;
3849 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3850 /* update previous map with new information */
3851 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3852 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3853 ASSERT(mval->br_state == mval[-1].br_state);
3854 mval[-1].br_blockcount = mval->br_blockcount;
3855 mval[-1].br_state = mval->br_state;
3856 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3857 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3858 mval[-1].br_startblock != HOLESTARTBLOCK &&
3859 mval->br_startblock == mval[-1].br_startblock +
3860 mval[-1].br_blockcount &&
3861 mval[-1].br_state == mval->br_state) {
3862 ASSERT(mval->br_startoff ==
3863 mval[-1].br_startoff + mval[-1].br_blockcount);
3864 mval[-1].br_blockcount += mval->br_blockcount;
3865 } else if (*n > 0 &&
3866 mval->br_startblock == DELAYSTARTBLOCK &&
3867 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3868 mval->br_startoff ==
3869 mval[-1].br_startoff + mval[-1].br_blockcount) {
3870 mval[-1].br_blockcount += mval->br_blockcount;
3871 mval[-1].br_state = mval->br_state;
3872 } else if (!((*n == 0) &&
3873 ((mval->br_startoff + mval->br_blockcount) <=
3882 * Map file blocks to filesystem blocks without allocation.
3886 struct xfs_inode *ip,
3889 struct xfs_bmbt_irec *mval,
3893 struct xfs_mount *mp = ip->i_mount;
3894 struct xfs_ifork *ifp;
3895 struct xfs_bmbt_irec got;
3898 struct xfs_iext_cursor icur;
3902 int whichfork = xfs_bmapi_whichfork(flags);
3905 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3906 XFS_BMAPI_COWFORK)));
3907 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3909 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
3910 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3911 return -EFSCORRUPTED;
3914 if (XFS_FORCED_SHUTDOWN(mp))
3917 XFS_STATS_INC(mp, xs_blk_mapr);
3919 ifp = XFS_IFORK_PTR(ip, whichfork);
3921 /* No CoW fork? Return a hole. */
3922 if (whichfork == XFS_COW_FORK) {
3923 mval->br_startoff = bno;
3924 mval->br_startblock = HOLESTARTBLOCK;
3925 mval->br_blockcount = len;
3926 mval->br_state = XFS_EXT_NORM;
3932 * A missing attr ifork implies that the inode says we're in
3933 * extents or btree format but failed to pass the inode fork
3934 * verifier while trying to load it. Treat that as a file
3938 xfs_alert(mp, "%s: inode %llu missing fork %d",
3939 __func__, ip->i_ino, whichfork);
3941 return -EFSCORRUPTED;
3944 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3945 error = xfs_iread_extents(NULL, ip, whichfork);
3950 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3955 while (bno < end && n < *nmap) {
3956 /* Reading past eof, act as though there's a hole up to end. */
3958 got.br_startoff = end;
3959 if (got.br_startoff > bno) {
3960 /* Reading in a hole. */
3961 mval->br_startoff = bno;
3962 mval->br_startblock = HOLESTARTBLOCK;
3963 mval->br_blockcount =
3964 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3965 mval->br_state = XFS_EXT_NORM;
3966 bno += mval->br_blockcount;
3967 len -= mval->br_blockcount;
3973 /* set up the extent map to return. */
3974 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3975 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3977 /* If we're done, stop now. */
3978 if (bno >= end || n >= *nmap)
3981 /* Else go on to the next record. */
3982 if (!xfs_iext_next_extent(ifp, &icur, &got))
3990 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3991 * global pool and the extent inserted into the inode in-core extent tree.
3993 * On entry, got refers to the first extent beyond the offset of the extent to
3994 * allocate or eof is specified if no such extent exists. On return, got refers
3995 * to the extent record that was inserted to the inode fork.
3997 * Note that the allocated extent may have been merged with contiguous extents
3998 * during insertion into the inode fork. Thus, got does not reflect the current
3999 * state of the inode fork on return. If necessary, the caller can use lastx to
4000 * look up the updated record in the inode fork.
4003 xfs_bmapi_reserve_delalloc(
4004 struct xfs_inode *ip,
4008 xfs_filblks_t prealloc,
4009 struct xfs_bmbt_irec *got,
4010 struct xfs_iext_cursor *icur,
4013 struct xfs_mount *mp = ip->i_mount;
4014 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4016 xfs_extlen_t indlen;
4018 xfs_fileoff_t aoff = off;
4021 * Cap the alloc length. Keep track of prealloc so we know whether to
4022 * tag the inode before we return.
4024 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
4026 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4027 if (prealloc && alen >= len)
4028 prealloc = alen - len;
4030 /* Figure out the extent size, adjust alen */
4031 if (whichfork == XFS_COW_FORK) {
4032 struct xfs_bmbt_irec prev;
4033 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
4035 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4036 prev.br_startoff = NULLFILEOFF;
4038 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4039 1, 0, &aoff, &alen);
4044 * Make a transaction-less quota reservation for delayed allocation
4045 * blocks. This number gets adjusted later. We return if we haven't
4046 * allocated blocks already inside this loop.
4048 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4049 XFS_QMOPT_RES_REGBLKS);
4054 * Split changing sb for alen and indlen since they could be coming
4055 * from different places.
4057 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4060 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4062 goto out_unreserve_quota;
4064 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4066 goto out_unreserve_blocks;
4069 ip->i_delayed_blks += alen;
4070 xfs_mod_delalloc(ip->i_mount, alen + indlen);
4072 got->br_startoff = aoff;
4073 got->br_startblock = nullstartblock(indlen);
4074 got->br_blockcount = alen;
4075 got->br_state = XFS_EXT_NORM;
4077 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4080 * Tag the inode if blocks were preallocated. Note that COW fork
4081 * preallocation can occur at the start or end of the extent, even when
4082 * prealloc == 0, so we must also check the aligned offset and length.
4084 if (whichfork == XFS_DATA_FORK && prealloc)
4085 xfs_inode_set_eofblocks_tag(ip);
4086 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4087 xfs_inode_set_cowblocks_tag(ip);
4091 out_unreserve_blocks:
4092 xfs_mod_fdblocks(mp, alen, false);
4093 out_unreserve_quota:
4094 if (XFS_IS_QUOTA_ON(mp))
4095 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4096 XFS_QMOPT_RES_REGBLKS);
4101 xfs_bmap_alloc_userdata(
4102 struct xfs_bmalloca *bma)
4104 struct xfs_mount *mp = bma->ip->i_mount;
4105 int whichfork = xfs_bmapi_whichfork(bma->flags);
4109 * Set the data type being allocated. For the data fork, the first data
4110 * in the file is treated differently to all other allocations. For the
4111 * attribute fork, we only need to ensure the allocated range is not on
4114 bma->datatype = XFS_ALLOC_NOBUSY;
4115 if (whichfork == XFS_DATA_FORK) {
4116 bma->datatype |= XFS_ALLOC_USERDATA;
4117 if (bma->offset == 0)
4118 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4120 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4121 error = xfs_bmap_isaeof(bma, whichfork);
4126 if (XFS_IS_REALTIME_INODE(bma->ip))
4127 return xfs_bmap_rtalloc(bma);
4130 return xfs_bmap_btalloc(bma);
4135 struct xfs_bmalloca *bma)
4137 struct xfs_mount *mp = bma->ip->i_mount;
4138 int whichfork = xfs_bmapi_whichfork(bma->flags);
4139 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4140 int tmp_logflags = 0;
4143 ASSERT(bma->length > 0);
4146 * For the wasdelay case, we could also just allocate the stuff asked
4147 * for in this bmap call but that wouldn't be as good.
4150 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4151 bma->offset = bma->got.br_startoff;
4152 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4153 bma->prev.br_startoff = NULLFILEOFF;
4155 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4157 bma->length = XFS_FILBLKS_MIN(bma->length,
4158 bma->got.br_startoff - bma->offset);
4161 if (bma->flags & XFS_BMAPI_CONTIG)
4162 bma->minlen = bma->length;
4166 if (bma->flags & XFS_BMAPI_METADATA)
4167 error = xfs_bmap_btalloc(bma);
4169 error = xfs_bmap_alloc_userdata(bma);
4170 if (error || bma->blkno == NULLFSBLOCK)
4173 if (bma->flags & XFS_BMAPI_ZERO) {
4174 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4179 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
4180 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4182 * Bump the number of extents we've allocated
4188 bma->cur->bc_ino.flags =
4189 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4191 bma->got.br_startoff = bma->offset;
4192 bma->got.br_startblock = bma->blkno;
4193 bma->got.br_blockcount = bma->length;
4194 bma->got.br_state = XFS_EXT_NORM;
4197 * In the data fork, a wasdelay extent has been initialized, so
4198 * shouldn't be flagged as unwritten.
4200 * For the cow fork, however, we convert delalloc reservations
4201 * (extents allocated for speculative preallocation) to
4202 * allocated unwritten extents, and only convert the unwritten
4203 * extents to real extents when we're about to write the data.
4205 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4206 (bma->flags & XFS_BMAPI_PREALLOC))
4207 bma->got.br_state = XFS_EXT_UNWRITTEN;
4210 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4212 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4213 whichfork, &bma->icur, &bma->cur, &bma->got,
4214 &bma->logflags, bma->flags);
4216 bma->logflags |= tmp_logflags;
4221 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4222 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4223 * the neighbouring ones.
4225 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4227 ASSERT(bma->got.br_startoff <= bma->offset);
4228 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4229 bma->offset + bma->length);
4230 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4231 bma->got.br_state == XFS_EXT_UNWRITTEN);
4236 xfs_bmapi_convert_unwritten(
4237 struct xfs_bmalloca *bma,
4238 struct xfs_bmbt_irec *mval,
4242 int whichfork = xfs_bmapi_whichfork(flags);
4243 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4244 int tmp_logflags = 0;
4247 /* check if we need to do unwritten->real conversion */
4248 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4249 (flags & XFS_BMAPI_PREALLOC))
4252 /* check if we need to do real->unwritten conversion */
4253 if (mval->br_state == XFS_EXT_NORM &&
4254 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4255 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4259 * Modify (by adding) the state flag, if writing.
4261 ASSERT(mval->br_blockcount <= len);
4262 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4263 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4264 bma->ip, whichfork);
4266 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4267 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4270 * Before insertion into the bmbt, zero the range being converted
4273 if (flags & XFS_BMAPI_ZERO) {
4274 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4275 mval->br_blockcount);
4280 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4281 &bma->icur, &bma->cur, mval, &tmp_logflags);
4283 * Log the inode core unconditionally in the unwritten extent conversion
4284 * path because the conversion might not have done so (e.g., if the
4285 * extent count hasn't changed). We need to make sure the inode is dirty
4286 * in the transaction for the sake of fsync(), even if nothing has
4287 * changed, because fsync() will not force the log for this transaction
4288 * unless it sees the inode pinned.
4290 * Note: If we're only converting cow fork extents, there aren't
4291 * any on-disk updates to make, so we don't need to log anything.
4293 if (whichfork != XFS_COW_FORK)
4294 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4299 * Update our extent pointer, given that
4300 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4301 * of the neighbouring ones.
4303 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4306 * We may have combined previously unwritten space with written space,
4307 * so generate another request.
4309 if (mval->br_blockcount < len)
4314 static inline xfs_extlen_t
4316 struct xfs_trans *tp,
4317 struct xfs_inode *ip,
4320 if (tp && tp->t_firstblock != NULLFSBLOCK)
4322 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4324 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4328 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4329 * a case where the data is changed, there's an error, and it's not logged so we
4330 * don't shutdown when we should. Don't bother logging extents/btree changes if
4331 * we converted to the other format.
4335 struct xfs_bmalloca *bma,
4339 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4340 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4341 bma->logflags &= ~xfs_ilog_fext(whichfork);
4342 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4343 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4344 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4347 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4349 xfs_btree_del_cursor(bma->cur, error);
4353 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4354 * extent state if necessary. Details behaviour is controlled by the flags
4355 * parameter. Only allocates blocks from a single allocation group, to avoid
4360 struct xfs_trans *tp, /* transaction pointer */
4361 struct xfs_inode *ip, /* incore inode */
4362 xfs_fileoff_t bno, /* starting file offs. mapped */
4363 xfs_filblks_t len, /* length to map in file */
4364 int flags, /* XFS_BMAPI_... */
4365 xfs_extlen_t total, /* total blocks needed */
4366 struct xfs_bmbt_irec *mval, /* output: map values */
4367 int *nmap) /* i/o: mval size/count */
4369 struct xfs_bmalloca bma = {
4374 struct xfs_mount *mp = ip->i_mount;
4375 struct xfs_ifork *ifp;
4376 xfs_fileoff_t end; /* end of mapped file region */
4377 bool eof = false; /* after the end of extents */
4378 int error; /* error return */
4379 int n; /* current extent index */
4380 xfs_fileoff_t obno; /* old block number (offset) */
4381 int whichfork; /* data or attr fork */
4384 xfs_fileoff_t orig_bno; /* original block number value */
4385 int orig_flags; /* original flags arg value */
4386 xfs_filblks_t orig_len; /* original value of len arg */
4387 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4388 int orig_nmap; /* original value of *nmap */
4396 whichfork = xfs_bmapi_whichfork(flags);
4399 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4402 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4403 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4404 ASSERT(!(flags & XFS_BMAPI_REMAP));
4406 /* zeroing is for currently only for data extents, not metadata */
4407 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4408 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4410 * we can allocate unwritten extents or pre-zero allocated blocks,
4411 * but it makes no sense to do both at once. This would result in
4412 * zeroing the unwritten extent twice, but it still being an
4413 * unwritten extent....
4415 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4416 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4418 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
4419 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4420 return -EFSCORRUPTED;
4423 if (XFS_FORCED_SHUTDOWN(mp))
4426 ifp = XFS_IFORK_PTR(ip, whichfork);
4428 XFS_STATS_INC(mp, xs_blk_mapw);
4430 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4431 error = xfs_iread_extents(tp, ip, whichfork);
4436 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4438 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4439 bma.prev.br_startoff = NULLFILEOFF;
4440 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4445 while (bno < end && n < *nmap) {
4446 bool need_alloc = false, wasdelay = false;
4448 /* in hole or beyond EOF? */
4449 if (eof || bma.got.br_startoff > bno) {
4451 * CoW fork conversions should /never/ hit EOF or
4452 * holes. There should always be something for us
4455 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4456 (flags & XFS_BMAPI_COWFORK)));
4459 } else if (isnullstartblock(bma.got.br_startblock)) {
4464 * First, deal with the hole before the allocated space
4465 * that we found, if any.
4467 if (need_alloc || wasdelay) {
4469 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4470 bma.wasdel = wasdelay;
4475 * There's a 32/64 bit type mismatch between the
4476 * allocation length request (which can be 64 bits in
4477 * length) and the bma length request, which is
4478 * xfs_extlen_t and therefore 32 bits. Hence we have to
4479 * check for 32-bit overflows and handle them here.
4481 if (len > (xfs_filblks_t)MAXEXTLEN)
4482 bma.length = MAXEXTLEN;
4487 ASSERT(bma.length > 0);
4488 error = xfs_bmapi_allocate(&bma);
4491 if (bma.blkno == NULLFSBLOCK)
4495 * If this is a CoW allocation, record the data in
4496 * the refcount btree for orphan recovery.
4498 if (whichfork == XFS_COW_FORK)
4499 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4503 /* Deal with the allocated space we found. */
4504 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4507 /* Execute unwritten extent conversion if necessary */
4508 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4509 if (error == -EAGAIN)
4514 /* update the extent map to return */
4515 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4518 * If we're done, stop now. Stop when we've allocated
4519 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4520 * the transaction may get too big.
4522 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4525 /* Else go on to the next record. */
4527 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4532 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4537 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4538 XFS_IFORK_NEXTENTS(ip, whichfork) >
4539 XFS_IFORK_MAXEXT(ip, whichfork));
4540 xfs_bmapi_finish(&bma, whichfork, 0);
4541 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4545 xfs_bmapi_finish(&bma, whichfork, error);
4550 * Convert an existing delalloc extent to real blocks based on file offset. This
4551 * attempts to allocate the entire delalloc extent and may require multiple
4552 * invocations to allocate the target offset if a large enough physical extent
4556 xfs_bmapi_convert_delalloc(
4557 struct xfs_inode *ip,
4560 struct iomap *iomap,
4563 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4564 struct xfs_mount *mp = ip->i_mount;
4565 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4566 struct xfs_bmalloca bma = { NULL };
4568 struct xfs_trans *tp;
4571 if (whichfork == XFS_COW_FORK)
4572 flags |= IOMAP_F_SHARED;
4575 * Space for the extent and indirect blocks was reserved when the
4576 * delalloc extent was created so there's no need to do so here.
4578 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4579 XFS_TRANS_RESERVE, &tp);
4583 xfs_ilock(ip, XFS_ILOCK_EXCL);
4584 xfs_trans_ijoin(tp, ip, 0);
4586 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4587 bma.got.br_startoff > offset_fsb) {
4589 * No extent found in the range we are trying to convert. This
4590 * should only happen for the COW fork, where another thread
4591 * might have moved the extent to the data fork in the meantime.
4593 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4595 goto out_trans_cancel;
4599 * If we find a real extent here we raced with another thread converting
4600 * the extent. Just return the real extent at this offset.
4602 if (!isnullstartblock(bma.got.br_startblock)) {
4603 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
4604 *seq = READ_ONCE(ifp->if_seq);
4605 goto out_trans_cancel;
4611 bma.offset = bma.got.br_startoff;
4612 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4613 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4614 if (whichfork == XFS_COW_FORK)
4615 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4617 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4618 bma.prev.br_startoff = NULLFILEOFF;
4620 error = xfs_bmapi_allocate(&bma);
4625 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4627 error = -EFSCORRUPTED;
4628 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
4631 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4632 XFS_STATS_INC(mp, xs_xstrat_quick);
4634 ASSERT(!isnullstartblock(bma.got.br_startblock));
4635 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
4636 *seq = READ_ONCE(ifp->if_seq);
4638 if (whichfork == XFS_COW_FORK)
4639 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4641 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4646 xfs_bmapi_finish(&bma, whichfork, 0);
4647 error = xfs_trans_commit(tp);
4648 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4652 xfs_bmapi_finish(&bma, whichfork, error);
4654 xfs_trans_cancel(tp);
4655 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4661 struct xfs_trans *tp,
4662 struct xfs_inode *ip,
4665 xfs_fsblock_t startblock,
4668 struct xfs_mount *mp = ip->i_mount;
4669 struct xfs_ifork *ifp;
4670 struct xfs_btree_cur *cur = NULL;
4671 struct xfs_bmbt_irec got;
4672 struct xfs_iext_cursor icur;
4673 int whichfork = xfs_bmapi_whichfork(flags);
4674 int logflags = 0, error;
4676 ifp = XFS_IFORK_PTR(ip, whichfork);
4678 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4679 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4680 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4681 XFS_BMAPI_NORMAP)));
4682 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4683 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4685 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
4686 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4687 return -EFSCORRUPTED;
4690 if (XFS_FORCED_SHUTDOWN(mp))
4693 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4694 error = xfs_iread_extents(tp, ip, whichfork);
4699 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4700 /* make sure we only reflink into a hole. */
4701 ASSERT(got.br_startoff > bno);
4702 ASSERT(got.br_startoff - bno >= len);
4705 ip->i_d.di_nblocks += len;
4706 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4708 if (ifp->if_flags & XFS_IFBROOT) {
4709 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4710 cur->bc_ino.flags = 0;
4713 got.br_startoff = bno;
4714 got.br_startblock = startblock;
4715 got.br_blockcount = len;
4716 if (flags & XFS_BMAPI_PREALLOC)
4717 got.br_state = XFS_EXT_UNWRITTEN;
4719 got.br_state = XFS_EXT_NORM;
4721 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4722 &cur, &got, &logflags, flags);
4726 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4729 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4730 logflags &= ~XFS_ILOG_DEXT;
4731 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4732 logflags &= ~XFS_ILOG_DBROOT;
4735 xfs_trans_log_inode(tp, ip, logflags);
4737 xfs_btree_del_cursor(cur, error);
4742 * When a delalloc extent is split (e.g., due to a hole punch), the original
4743 * indlen reservation must be shared across the two new extents that are left
4746 * Given the original reservation and the worst case indlen for the two new
4747 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4748 * reservation fairly across the two new extents. If necessary, steal available
4749 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4750 * ores == 1). The number of stolen blocks is returned. The availability and
4751 * subsequent accounting of stolen blocks is the responsibility of the caller.
4753 static xfs_filblks_t
4754 xfs_bmap_split_indlen(
4755 xfs_filblks_t ores, /* original res. */
4756 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4757 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4758 xfs_filblks_t avail) /* stealable blocks */
4760 xfs_filblks_t len1 = *indlen1;
4761 xfs_filblks_t len2 = *indlen2;
4762 xfs_filblks_t nres = len1 + len2; /* new total res. */
4763 xfs_filblks_t stolen = 0;
4764 xfs_filblks_t resfactor;
4767 * Steal as many blocks as we can to try and satisfy the worst case
4768 * indlen for both new extents.
4770 if (ores < nres && avail)
4771 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4774 /* nothing else to do if we've satisfied the new reservation */
4779 * We can't meet the total required reservation for the two extents.
4780 * Calculate the percent of the overall shortage between both extents
4781 * and apply this percentage to each of the requested indlen values.
4782 * This distributes the shortage fairly and reduces the chances that one
4783 * of the two extents is left with nothing when extents are repeatedly
4786 resfactor = (ores * 100);
4787 do_div(resfactor, nres);
4792 ASSERT(len1 + len2 <= ores);
4793 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4796 * Hand out the remainder to each extent. If one of the two reservations
4797 * is zero, we want to make sure that one gets a block first. The loop
4798 * below starts with len1, so hand len2 a block right off the bat if it
4801 ores -= (len1 + len2);
4802 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4803 if (ores && !len2 && *indlen2) {
4808 if (len1 < *indlen1) {
4814 if (len2 < *indlen2) {
4827 xfs_bmap_del_extent_delay(
4828 struct xfs_inode *ip,
4830 struct xfs_iext_cursor *icur,
4831 struct xfs_bmbt_irec *got,
4832 struct xfs_bmbt_irec *del)
4834 struct xfs_mount *mp = ip->i_mount;
4835 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4836 struct xfs_bmbt_irec new;
4837 int64_t da_old, da_new, da_diff = 0;
4838 xfs_fileoff_t del_endoff, got_endoff;
4839 xfs_filblks_t got_indlen, new_indlen, stolen;
4840 int state = xfs_bmap_fork_to_state(whichfork);
4844 XFS_STATS_INC(mp, xs_del_exlist);
4846 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4847 del_endoff = del->br_startoff + del->br_blockcount;
4848 got_endoff = got->br_startoff + got->br_blockcount;
4849 da_old = startblockval(got->br_startblock);
4852 ASSERT(del->br_blockcount > 0);
4853 ASSERT(got->br_startoff <= del->br_startoff);
4854 ASSERT(got_endoff >= del_endoff);
4857 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4859 do_div(rtexts, mp->m_sb.sb_rextsize);
4860 xfs_mod_frextents(mp, rtexts);
4864 * Update the inode delalloc counter now and wait to update the
4865 * sb counters as we might have to borrow some blocks for the
4866 * indirect block accounting.
4868 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4869 -((long)del->br_blockcount), 0,
4870 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4873 ip->i_delayed_blks -= del->br_blockcount;
4875 if (got->br_startoff == del->br_startoff)
4876 state |= BMAP_LEFT_FILLING;
4877 if (got_endoff == del_endoff)
4878 state |= BMAP_RIGHT_FILLING;
4880 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4881 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4883 * Matches the whole extent. Delete the entry.
4885 xfs_iext_remove(ip, icur, state);
4886 xfs_iext_prev(ifp, icur);
4888 case BMAP_LEFT_FILLING:
4890 * Deleting the first part of the extent.
4892 got->br_startoff = del_endoff;
4893 got->br_blockcount -= del->br_blockcount;
4894 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4895 got->br_blockcount), da_old);
4896 got->br_startblock = nullstartblock((int)da_new);
4897 xfs_iext_update_extent(ip, state, icur, got);
4899 case BMAP_RIGHT_FILLING:
4901 * Deleting the last part of the extent.
4903 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4904 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4905 got->br_blockcount), da_old);
4906 got->br_startblock = nullstartblock((int)da_new);
4907 xfs_iext_update_extent(ip, state, icur, got);
4911 * Deleting the middle of the extent.
4913 * Distribute the original indlen reservation across the two new
4914 * extents. Steal blocks from the deleted extent if necessary.
4915 * Stealing blocks simply fudges the fdblocks accounting below.
4916 * Warn if either of the new indlen reservations is zero as this
4917 * can lead to delalloc problems.
4919 got->br_blockcount = del->br_startoff - got->br_startoff;
4920 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4922 new.br_blockcount = got_endoff - del_endoff;
4923 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4925 WARN_ON_ONCE(!got_indlen || !new_indlen);
4926 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4927 del->br_blockcount);
4929 got->br_startblock = nullstartblock((int)got_indlen);
4931 new.br_startoff = del_endoff;
4932 new.br_state = got->br_state;
4933 new.br_startblock = nullstartblock((int)new_indlen);
4935 xfs_iext_update_extent(ip, state, icur, got);
4936 xfs_iext_next(ifp, icur);
4937 xfs_iext_insert(ip, icur, &new, state);
4939 da_new = got_indlen + new_indlen - stolen;
4940 del->br_blockcount -= stolen;
4944 ASSERT(da_old >= da_new);
4945 da_diff = da_old - da_new;
4947 da_diff += del->br_blockcount;
4949 xfs_mod_fdblocks(mp, da_diff, false);
4950 xfs_mod_delalloc(mp, -da_diff);
4956 xfs_bmap_del_extent_cow(
4957 struct xfs_inode *ip,
4958 struct xfs_iext_cursor *icur,
4959 struct xfs_bmbt_irec *got,
4960 struct xfs_bmbt_irec *del)
4962 struct xfs_mount *mp = ip->i_mount;
4963 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4964 struct xfs_bmbt_irec new;
4965 xfs_fileoff_t del_endoff, got_endoff;
4966 int state = BMAP_COWFORK;
4968 XFS_STATS_INC(mp, xs_del_exlist);
4970 del_endoff = del->br_startoff + del->br_blockcount;
4971 got_endoff = got->br_startoff + got->br_blockcount;
4973 ASSERT(del->br_blockcount > 0);
4974 ASSERT(got->br_startoff <= del->br_startoff);
4975 ASSERT(got_endoff >= del_endoff);
4976 ASSERT(!isnullstartblock(got->br_startblock));
4978 if (got->br_startoff == del->br_startoff)
4979 state |= BMAP_LEFT_FILLING;
4980 if (got_endoff == del_endoff)
4981 state |= BMAP_RIGHT_FILLING;
4983 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4984 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4986 * Matches the whole extent. Delete the entry.
4988 xfs_iext_remove(ip, icur, state);
4989 xfs_iext_prev(ifp, icur);
4991 case BMAP_LEFT_FILLING:
4993 * Deleting the first part of the extent.
4995 got->br_startoff = del_endoff;
4996 got->br_blockcount -= del->br_blockcount;
4997 got->br_startblock = del->br_startblock + del->br_blockcount;
4998 xfs_iext_update_extent(ip, state, icur, got);
5000 case BMAP_RIGHT_FILLING:
5002 * Deleting the last part of the extent.
5004 got->br_blockcount -= del->br_blockcount;
5005 xfs_iext_update_extent(ip, state, icur, got);
5009 * Deleting the middle of the extent.
5011 got->br_blockcount = del->br_startoff - got->br_startoff;
5013 new.br_startoff = del_endoff;
5014 new.br_blockcount = got_endoff - del_endoff;
5015 new.br_state = got->br_state;
5016 new.br_startblock = del->br_startblock + del->br_blockcount;
5018 xfs_iext_update_extent(ip, state, icur, got);
5019 xfs_iext_next(ifp, icur);
5020 xfs_iext_insert(ip, icur, &new, state);
5023 ip->i_delayed_blks -= del->br_blockcount;
5027 * Called by xfs_bmapi to update file extent records and the btree
5028 * after removing space.
5030 STATIC int /* error */
5031 xfs_bmap_del_extent_real(
5032 xfs_inode_t *ip, /* incore inode pointer */
5033 xfs_trans_t *tp, /* current transaction pointer */
5034 struct xfs_iext_cursor *icur,
5035 xfs_btree_cur_t *cur, /* if null, not a btree */
5036 xfs_bmbt_irec_t *del, /* data to remove from extents */
5037 int *logflagsp, /* inode logging flags */
5038 int whichfork, /* data or attr fork */
5039 int bflags) /* bmapi flags */
5041 xfs_fsblock_t del_endblock=0; /* first block past del */
5042 xfs_fileoff_t del_endoff; /* first offset past del */
5043 int do_fx; /* free extent at end of routine */
5044 int error; /* error return value */
5045 int flags = 0;/* inode logging flags */
5046 struct xfs_bmbt_irec got; /* current extent entry */
5047 xfs_fileoff_t got_endoff; /* first offset past got */
5048 int i; /* temp state */
5049 struct xfs_ifork *ifp; /* inode fork pointer */
5050 xfs_mount_t *mp; /* mount structure */
5051 xfs_filblks_t nblks; /* quota/sb block count */
5052 xfs_bmbt_irec_t new; /* new record to be inserted */
5054 uint qfield; /* quota field to update */
5055 int state = xfs_bmap_fork_to_state(whichfork);
5056 struct xfs_bmbt_irec old;
5059 XFS_STATS_INC(mp, xs_del_exlist);
5061 ifp = XFS_IFORK_PTR(ip, whichfork);
5062 ASSERT(del->br_blockcount > 0);
5063 xfs_iext_get_extent(ifp, icur, &got);
5064 ASSERT(got.br_startoff <= del->br_startoff);
5065 del_endoff = del->br_startoff + del->br_blockcount;
5066 got_endoff = got.br_startoff + got.br_blockcount;
5067 ASSERT(got_endoff >= del_endoff);
5068 ASSERT(!isnullstartblock(got.br_startblock));
5073 * If it's the case where the directory code is running with no block
5074 * reservation, and the deleted block is in the middle of its extent,
5075 * and the resulting insert of an extent would cause transformation to
5076 * btree format, then reject it. The calling code will then swap blocks
5077 * around instead. We have to do this now, rather than waiting for the
5078 * conversion to btree format, since the transaction will be dirty then.
5080 if (tp->t_blk_res == 0 &&
5081 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5082 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5083 XFS_IFORK_MAXEXT(ip, whichfork) &&
5084 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5087 flags = XFS_ILOG_CORE;
5088 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5093 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
5096 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5100 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5104 nblks = len * mp->m_sb.sb_rextsize;
5105 qfield = XFS_TRANS_DQ_RTBCOUNT;
5108 nblks = del->br_blockcount;
5109 qfield = XFS_TRANS_DQ_BCOUNT;
5112 del_endblock = del->br_startblock + del->br_blockcount;
5114 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5117 if (XFS_IS_CORRUPT(mp, i != 1)) {
5118 error = -EFSCORRUPTED;
5123 if (got.br_startoff == del->br_startoff)
5124 state |= BMAP_LEFT_FILLING;
5125 if (got_endoff == del_endoff)
5126 state |= BMAP_RIGHT_FILLING;
5128 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5129 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5131 * Matches the whole extent. Delete the entry.
5133 xfs_iext_remove(ip, icur, state);
5134 xfs_iext_prev(ifp, icur);
5135 XFS_IFORK_NEXT_SET(ip, whichfork,
5136 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5137 flags |= XFS_ILOG_CORE;
5139 flags |= xfs_ilog_fext(whichfork);
5142 if ((error = xfs_btree_delete(cur, &i)))
5144 if (XFS_IS_CORRUPT(mp, i != 1)) {
5145 error = -EFSCORRUPTED;
5149 case BMAP_LEFT_FILLING:
5151 * Deleting the first part of the extent.
5153 got.br_startoff = del_endoff;
5154 got.br_startblock = del_endblock;
5155 got.br_blockcount -= del->br_blockcount;
5156 xfs_iext_update_extent(ip, state, icur, &got);
5158 flags |= xfs_ilog_fext(whichfork);
5161 error = xfs_bmbt_update(cur, &got);
5165 case BMAP_RIGHT_FILLING:
5167 * Deleting the last part of the extent.
5169 got.br_blockcount -= del->br_blockcount;
5170 xfs_iext_update_extent(ip, state, icur, &got);
5172 flags |= xfs_ilog_fext(whichfork);
5175 error = xfs_bmbt_update(cur, &got);
5181 * Deleting the middle of the extent.
5185 got.br_blockcount = del->br_startoff - got.br_startoff;
5186 xfs_iext_update_extent(ip, state, icur, &got);
5188 new.br_startoff = del_endoff;
5189 new.br_blockcount = got_endoff - del_endoff;
5190 new.br_state = got.br_state;
5191 new.br_startblock = del_endblock;
5193 flags |= XFS_ILOG_CORE;
5195 error = xfs_bmbt_update(cur, &got);
5198 error = xfs_btree_increment(cur, 0, &i);
5201 cur->bc_rec.b = new;
5202 error = xfs_btree_insert(cur, &i);
5203 if (error && error != -ENOSPC)
5206 * If get no-space back from btree insert, it tried a
5207 * split, and we have a zero block reservation. Fix up
5208 * our state and return the error.
5210 if (error == -ENOSPC) {
5212 * Reset the cursor, don't trust it after any
5215 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5218 if (XFS_IS_CORRUPT(mp, i != 1)) {
5219 error = -EFSCORRUPTED;
5223 * Update the btree record back
5224 * to the original value.
5226 error = xfs_bmbt_update(cur, &old);
5230 * Reset the extent record back
5231 * to the original value.
5233 xfs_iext_update_extent(ip, state, icur, &old);
5238 if (XFS_IS_CORRUPT(mp, i != 1)) {
5239 error = -EFSCORRUPTED;
5243 flags |= xfs_ilog_fext(whichfork);
5244 XFS_IFORK_NEXT_SET(ip, whichfork,
5245 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5246 xfs_iext_next(ifp, icur);
5247 xfs_iext_insert(ip, icur, &new, state);
5251 /* remove reverse mapping */
5252 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5255 * If we need to, add to list of extents to delete.
5257 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5258 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5259 xfs_refcount_decrease_extent(tp, del);
5261 __xfs_bmap_add_free(tp, del->br_startblock,
5262 del->br_blockcount, NULL,
5263 (bflags & XFS_BMAPI_NODISCARD) ||
5264 del->br_state == XFS_EXT_UNWRITTEN);
5269 * Adjust inode # blocks in the file.
5272 ip->i_d.di_nblocks -= nblks;
5274 * Adjust quota data.
5276 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5277 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5285 * Unmap (remove) blocks from a file.
5286 * If nexts is nonzero then the number of extents to remove is limited to
5287 * that value. If not all extents in the block range can be removed then
5292 struct xfs_trans *tp, /* transaction pointer */
5293 struct xfs_inode *ip, /* incore inode */
5294 xfs_fileoff_t start, /* first file offset deleted */
5295 xfs_filblks_t *rlen, /* i/o: amount remaining */
5296 int flags, /* misc flags */
5297 xfs_extnum_t nexts) /* number of extents max */
5299 struct xfs_btree_cur *cur; /* bmap btree cursor */
5300 struct xfs_bmbt_irec del; /* extent being deleted */
5301 int error; /* error return value */
5302 xfs_extnum_t extno; /* extent number in list */
5303 struct xfs_bmbt_irec got; /* current extent record */
5304 struct xfs_ifork *ifp; /* inode fork pointer */
5305 int isrt; /* freeing in rt area */
5306 int logflags; /* transaction logging flags */
5307 xfs_extlen_t mod; /* rt extent offset */
5308 struct xfs_mount *mp = ip->i_mount;
5309 int tmp_logflags; /* partial logging flags */
5310 int wasdel; /* was a delayed alloc extent */
5311 int whichfork; /* data or attribute fork */
5313 xfs_filblks_t len = *rlen; /* length to unmap in file */
5314 xfs_fileoff_t max_len;
5315 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5317 struct xfs_iext_cursor icur;
5320 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5322 whichfork = xfs_bmapi_whichfork(flags);
5323 ASSERT(whichfork != XFS_COW_FORK);
5324 ifp = XFS_IFORK_PTR(ip, whichfork);
5325 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)))
5326 return -EFSCORRUPTED;
5327 if (XFS_FORCED_SHUTDOWN(mp))
5330 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5335 * Guesstimate how many blocks we can unmap without running the risk of
5336 * blowing out the transaction with a mix of EFIs and reflink
5339 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5340 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5344 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5345 (error = xfs_iread_extents(tp, ip, whichfork)))
5347 if (xfs_iext_count(ifp) == 0) {
5351 XFS_STATS_INC(mp, xs_blk_unmap);
5352 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5355 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5362 if (ifp->if_flags & XFS_IFBROOT) {
5363 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5364 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5365 cur->bc_ino.flags = 0;
5371 * Synchronize by locking the bitmap inode.
5373 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5374 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5375 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5376 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5380 while (end != (xfs_fileoff_t)-1 && end >= start &&
5381 (nexts == 0 || extno < nexts) && max_len > 0) {
5383 * Is the found extent after a hole in which end lives?
5384 * Just back up to the previous extent, if so.
5386 if (got.br_startoff > end &&
5387 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5392 * Is the last block of this extent before the range
5393 * we're supposed to delete? If so, we're done.
5395 end = XFS_FILEOFF_MIN(end,
5396 got.br_startoff + got.br_blockcount - 1);
5400 * Then deal with the (possibly delayed) allocated space
5404 wasdel = isnullstartblock(del.br_startblock);
5407 * Make sure we don't touch multiple AGF headers out of order
5408 * in a single transaction, as that could cause AB-BA deadlocks.
5410 if (!wasdel && !isrt) {
5411 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5412 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5416 if (got.br_startoff < start) {
5417 del.br_startoff = start;
5418 del.br_blockcount -= start - got.br_startoff;
5420 del.br_startblock += start - got.br_startoff;
5422 if (del.br_startoff + del.br_blockcount > end + 1)
5423 del.br_blockcount = end + 1 - del.br_startoff;
5425 /* How much can we safely unmap? */
5426 if (max_len < del.br_blockcount) {
5427 del.br_startoff += del.br_blockcount - max_len;
5429 del.br_startblock += del.br_blockcount - max_len;
5430 del.br_blockcount = max_len;
5436 sum = del.br_startblock + del.br_blockcount;
5437 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5440 * Realtime extent not lined up at the end.
5441 * The extent could have been split into written
5442 * and unwritten pieces, or we could just be
5443 * unmapping part of it. But we can't really
5444 * get rid of part of a realtime extent.
5446 if (del.br_state == XFS_EXT_UNWRITTEN) {
5448 * This piece is unwritten, or we're not
5449 * using unwritten extents. Skip over it.
5452 end -= mod > del.br_blockcount ?
5453 del.br_blockcount : mod;
5454 if (end < got.br_startoff &&
5455 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5462 * It's written, turn it unwritten.
5463 * This is better than zeroing it.
5465 ASSERT(del.br_state == XFS_EXT_NORM);
5466 ASSERT(tp->t_blk_res > 0);
5468 * If this spans a realtime extent boundary,
5469 * chop it back to the start of the one we end at.
5471 if (del.br_blockcount > mod) {
5472 del.br_startoff += del.br_blockcount - mod;
5473 del.br_startblock += del.br_blockcount - mod;
5474 del.br_blockcount = mod;
5476 del.br_state = XFS_EXT_UNWRITTEN;
5477 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5478 whichfork, &icur, &cur, &del,
5484 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5486 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5489 * Realtime extent is lined up at the end but not
5490 * at the front. We'll get rid of full extents if
5493 if (del.br_blockcount > off) {
5494 del.br_blockcount -= off;
5495 del.br_startoff += off;
5496 del.br_startblock += off;
5497 } else if (del.br_startoff == start &&
5498 (del.br_state == XFS_EXT_UNWRITTEN ||
5499 tp->t_blk_res == 0)) {
5501 * Can't make it unwritten. There isn't
5502 * a full extent here so just skip it.
5504 ASSERT(end >= del.br_blockcount);
5505 end -= del.br_blockcount;
5506 if (got.br_startoff > end &&
5507 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5512 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5513 struct xfs_bmbt_irec prev;
5514 xfs_fileoff_t unwrite_start;
5517 * This one is already unwritten.
5518 * It must have a written left neighbor.
5519 * Unwrite the killed part of that one and
5522 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5524 ASSERT(prev.br_state == XFS_EXT_NORM);
5525 ASSERT(!isnullstartblock(prev.br_startblock));
5526 ASSERT(del.br_startblock ==
5527 prev.br_startblock + prev.br_blockcount);
5528 unwrite_start = max3(start,
5529 del.br_startoff - mod,
5531 mod = unwrite_start - prev.br_startoff;
5532 prev.br_startoff = unwrite_start;
5533 prev.br_startblock += mod;
5534 prev.br_blockcount -= mod;
5535 prev.br_state = XFS_EXT_UNWRITTEN;
5536 error = xfs_bmap_add_extent_unwritten_real(tp,
5537 ip, whichfork, &icur, &cur,
5543 ASSERT(del.br_state == XFS_EXT_NORM);
5544 del.br_state = XFS_EXT_UNWRITTEN;
5545 error = xfs_bmap_add_extent_unwritten_real(tp,
5546 ip, whichfork, &icur, &cur,
5556 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5559 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5560 &del, &tmp_logflags, whichfork,
5562 logflags |= tmp_logflags;
5568 max_len -= del.br_blockcount;
5569 end = del.br_startoff - 1;
5572 * If not done go on to the next (previous) record.
5574 if (end != (xfs_fileoff_t)-1 && end >= start) {
5575 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5576 (got.br_startoff > end &&
5577 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5584 if (done || end == (xfs_fileoff_t)-1 || end < start)
5587 *rlen = end - start + 1;
5590 * Convert to a btree if necessary.
5592 if (xfs_bmap_needs_btree(ip, whichfork)) {
5593 ASSERT(cur == NULL);
5594 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5595 &tmp_logflags, whichfork);
5596 logflags |= tmp_logflags;
5598 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5604 * Log everything. Do this after conversion, there's no point in
5605 * logging the extent records if we've converted to btree format.
5607 if ((logflags & xfs_ilog_fext(whichfork)) &&
5608 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5609 logflags &= ~xfs_ilog_fext(whichfork);
5610 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5611 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5612 logflags &= ~xfs_ilog_fbroot(whichfork);
5614 * Log inode even in the error case, if the transaction
5615 * is dirty we'll need to shut down the filesystem.
5618 xfs_trans_log_inode(tp, ip, logflags);
5621 cur->bc_ino.allocated = 0;
5622 xfs_btree_del_cursor(cur, error);
5627 /* Unmap a range of a file. */
5631 struct xfs_inode *ip,
5640 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5646 * Determine whether an extent shift can be accomplished by a merge with the
5647 * extent that precedes the target hole of the shift.
5651 struct xfs_bmbt_irec *left, /* preceding extent */
5652 struct xfs_bmbt_irec *got, /* current extent to shift */
5653 xfs_fileoff_t shift) /* shift fsb */
5655 xfs_fileoff_t startoff;
5657 startoff = got->br_startoff - shift;
5660 * The extent, once shifted, must be adjacent in-file and on-disk with
5661 * the preceding extent.
5663 if ((left->br_startoff + left->br_blockcount != startoff) ||
5664 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5665 (left->br_state != got->br_state) ||
5666 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5673 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5674 * hole in the file. If an extent shift would result in the extent being fully
5675 * adjacent to the extent that currently precedes the hole, we can merge with
5676 * the preceding extent rather than do the shift.
5678 * This function assumes the caller has verified a shift-by-merge is possible
5679 * with the provided extents via xfs_bmse_can_merge().
5683 struct xfs_trans *tp,
5684 struct xfs_inode *ip,
5686 xfs_fileoff_t shift, /* shift fsb */
5687 struct xfs_iext_cursor *icur,
5688 struct xfs_bmbt_irec *got, /* extent to shift */
5689 struct xfs_bmbt_irec *left, /* preceding extent */
5690 struct xfs_btree_cur *cur,
5691 int *logflags) /* output */
5693 struct xfs_bmbt_irec new;
5694 xfs_filblks_t blockcount;
5696 struct xfs_mount *mp = ip->i_mount;
5698 blockcount = left->br_blockcount + got->br_blockcount;
5700 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5701 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5702 ASSERT(xfs_bmse_can_merge(left, got, shift));
5705 new.br_blockcount = blockcount;
5708 * Update the on-disk extent count, the btree if necessary and log the
5711 XFS_IFORK_NEXT_SET(ip, whichfork,
5712 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5713 *logflags |= XFS_ILOG_CORE;
5715 *logflags |= XFS_ILOG_DEXT;
5719 /* lookup and remove the extent to merge */
5720 error = xfs_bmbt_lookup_eq(cur, got, &i);
5723 if (XFS_IS_CORRUPT(mp, i != 1))
5724 return -EFSCORRUPTED;
5726 error = xfs_btree_delete(cur, &i);
5729 if (XFS_IS_CORRUPT(mp, i != 1))
5730 return -EFSCORRUPTED;
5732 /* lookup and update size of the previous extent */
5733 error = xfs_bmbt_lookup_eq(cur, left, &i);
5736 if (XFS_IS_CORRUPT(mp, i != 1))
5737 return -EFSCORRUPTED;
5739 error = xfs_bmbt_update(cur, &new);
5743 /* change to extent format if required after extent removal */
5744 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5749 xfs_iext_remove(ip, icur, 0);
5750 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5751 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5754 /* update reverse mapping. rmap functions merge the rmaps for us */
5755 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5756 memcpy(&new, got, sizeof(new));
5757 new.br_startoff = left->br_startoff + left->br_blockcount;
5758 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5763 xfs_bmap_shift_update_extent(
5764 struct xfs_trans *tp,
5765 struct xfs_inode *ip,
5767 struct xfs_iext_cursor *icur,
5768 struct xfs_bmbt_irec *got,
5769 struct xfs_btree_cur *cur,
5771 xfs_fileoff_t startoff)
5773 struct xfs_mount *mp = ip->i_mount;
5774 struct xfs_bmbt_irec prev = *got;
5777 *logflags |= XFS_ILOG_CORE;
5779 got->br_startoff = startoff;
5782 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5785 if (XFS_IS_CORRUPT(mp, i != 1))
5786 return -EFSCORRUPTED;
5788 error = xfs_bmbt_update(cur, got);
5792 *logflags |= XFS_ILOG_DEXT;
5795 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5798 /* update reverse mapping */
5799 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5800 xfs_rmap_map_extent(tp, ip, whichfork, got);
5805 xfs_bmap_collapse_extents(
5806 struct xfs_trans *tp,
5807 struct xfs_inode *ip,
5808 xfs_fileoff_t *next_fsb,
5809 xfs_fileoff_t offset_shift_fsb,
5812 int whichfork = XFS_DATA_FORK;
5813 struct xfs_mount *mp = ip->i_mount;
5814 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5815 struct xfs_btree_cur *cur = NULL;
5816 struct xfs_bmbt_irec got, prev;
5817 struct xfs_iext_cursor icur;
5818 xfs_fileoff_t new_startoff;
5822 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
5823 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5824 return -EFSCORRUPTED;
5827 if (XFS_FORCED_SHUTDOWN(mp))
5830 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5832 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5833 error = xfs_iread_extents(tp, ip, whichfork);
5838 if (ifp->if_flags & XFS_IFBROOT) {
5839 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5840 cur->bc_ino.flags = 0;
5843 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5847 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5848 error = -EFSCORRUPTED;
5852 new_startoff = got.br_startoff - offset_shift_fsb;
5853 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5854 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5859 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5860 error = xfs_bmse_merge(tp, ip, whichfork,
5861 offset_shift_fsb, &icur, &got, &prev,
5868 if (got.br_startoff < offset_shift_fsb) {
5874 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5875 cur, &logflags, new_startoff);
5880 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5885 *next_fsb = got.br_startoff;
5888 xfs_btree_del_cursor(cur, error);
5890 xfs_trans_log_inode(tp, ip, logflags);
5894 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5896 xfs_bmap_can_insert_extents(
5897 struct xfs_inode *ip,
5899 xfs_fileoff_t shift)
5901 struct xfs_bmbt_irec got;
5905 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5907 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5910 xfs_ilock(ip, XFS_ILOCK_EXCL);
5911 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5912 if (!error && !is_empty && got.br_startoff >= off &&
5913 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5915 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5921 xfs_bmap_insert_extents(
5922 struct xfs_trans *tp,
5923 struct xfs_inode *ip,
5924 xfs_fileoff_t *next_fsb,
5925 xfs_fileoff_t offset_shift_fsb,
5927 xfs_fileoff_t stop_fsb)
5929 int whichfork = XFS_DATA_FORK;
5930 struct xfs_mount *mp = ip->i_mount;
5931 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5932 struct xfs_btree_cur *cur = NULL;
5933 struct xfs_bmbt_irec got, next;
5934 struct xfs_iext_cursor icur;
5935 xfs_fileoff_t new_startoff;
5939 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
5940 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5941 return -EFSCORRUPTED;
5944 if (XFS_FORCED_SHUTDOWN(mp))
5947 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5949 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5950 error = xfs_iread_extents(tp, ip, whichfork);
5955 if (ifp->if_flags & XFS_IFBROOT) {
5956 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5957 cur->bc_ino.flags = 0;
5960 if (*next_fsb == NULLFSBLOCK) {
5961 xfs_iext_last(ifp, &icur);
5962 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5963 stop_fsb > got.br_startoff) {
5968 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5973 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5974 error = -EFSCORRUPTED;
5978 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
5979 error = -EFSCORRUPTED;
5983 new_startoff = got.br_startoff + offset_shift_fsb;
5984 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5985 if (new_startoff + got.br_blockcount > next.br_startoff) {
5991 * Unlike a left shift (which involves a hole punch), a right
5992 * shift does not modify extent neighbors in any way. We should
5993 * never find mergeable extents in this scenario. Check anyways
5994 * and warn if we encounter two extents that could be one.
5996 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6000 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6001 cur, &logflags, new_startoff);
6005 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6006 stop_fsb >= got.br_startoff + got.br_blockcount) {
6011 *next_fsb = got.br_startoff;
6014 xfs_btree_del_cursor(cur, error);
6016 xfs_trans_log_inode(tp, ip, logflags);
6021 * Splits an extent into two extents at split_fsb block such that it is the
6022 * first block of the current_ext. @ext is a target extent to be split.
6023 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6024 * hole or the first block of extents, just return 0.
6027 xfs_bmap_split_extent(
6028 struct xfs_trans *tp,
6029 struct xfs_inode *ip,
6030 xfs_fileoff_t split_fsb)
6032 int whichfork = XFS_DATA_FORK;
6033 struct xfs_btree_cur *cur = NULL;
6034 struct xfs_bmbt_irec got;
6035 struct xfs_bmbt_irec new; /* split extent */
6036 struct xfs_mount *mp = ip->i_mount;
6037 struct xfs_ifork *ifp;
6038 xfs_fsblock_t gotblkcnt; /* new block count for got */
6039 struct xfs_iext_cursor icur;
6044 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
6045 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6046 return -EFSCORRUPTED;
6049 if (XFS_FORCED_SHUTDOWN(mp))
6052 ifp = XFS_IFORK_PTR(ip, whichfork);
6053 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6054 /* Read in all the extents */
6055 error = xfs_iread_extents(tp, ip, whichfork);
6061 * If there are not extents, or split_fsb lies in a hole we are done.
6063 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6064 got.br_startoff >= split_fsb)
6067 gotblkcnt = split_fsb - got.br_startoff;
6068 new.br_startoff = split_fsb;
6069 new.br_startblock = got.br_startblock + gotblkcnt;
6070 new.br_blockcount = got.br_blockcount - gotblkcnt;
6071 new.br_state = got.br_state;
6073 if (ifp->if_flags & XFS_IFBROOT) {
6074 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6075 cur->bc_ino.flags = 0;
6076 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6079 if (XFS_IS_CORRUPT(mp, i != 1)) {
6080 error = -EFSCORRUPTED;
6085 got.br_blockcount = gotblkcnt;
6086 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6089 logflags = XFS_ILOG_CORE;
6091 error = xfs_bmbt_update(cur, &got);
6095 logflags |= XFS_ILOG_DEXT;
6097 /* Add new extent */
6098 xfs_iext_next(ifp, &icur);
6099 xfs_iext_insert(ip, &icur, &new, 0);
6100 XFS_IFORK_NEXT_SET(ip, whichfork,
6101 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6104 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6107 if (XFS_IS_CORRUPT(mp, i != 0)) {
6108 error = -EFSCORRUPTED;
6111 error = xfs_btree_insert(cur, &i);
6114 if (XFS_IS_CORRUPT(mp, i != 1)) {
6115 error = -EFSCORRUPTED;
6121 * Convert to a btree if necessary.
6123 if (xfs_bmap_needs_btree(ip, whichfork)) {
6124 int tmp_logflags; /* partial log flag return val */
6126 ASSERT(cur == NULL);
6127 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6128 &tmp_logflags, whichfork);
6129 logflags |= tmp_logflags;
6134 cur->bc_ino.allocated = 0;
6135 xfs_btree_del_cursor(cur, error);
6139 xfs_trans_log_inode(tp, ip, logflags);
6143 /* Deferred mapping is only for real extents in the data fork. */
6145 xfs_bmap_is_update_needed(
6146 struct xfs_bmbt_irec *bmap)
6148 return bmap->br_startblock != HOLESTARTBLOCK &&
6149 bmap->br_startblock != DELAYSTARTBLOCK;
6152 /* Record a bmap intent. */
6155 struct xfs_trans *tp,
6156 enum xfs_bmap_intent_type type,
6157 struct xfs_inode *ip,
6159 struct xfs_bmbt_irec *bmap)
6161 struct xfs_bmap_intent *bi;
6163 trace_xfs_bmap_defer(tp->t_mountp,
6164 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6166 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6167 ip->i_ino, whichfork,
6169 bmap->br_blockcount,
6172 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
6173 INIT_LIST_HEAD(&bi->bi_list);
6176 bi->bi_whichfork = whichfork;
6177 bi->bi_bmap = *bmap;
6179 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6183 /* Map an extent into a file. */
6185 xfs_bmap_map_extent(
6186 struct xfs_trans *tp,
6187 struct xfs_inode *ip,
6188 struct xfs_bmbt_irec *PREV)
6190 if (!xfs_bmap_is_update_needed(PREV))
6193 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6196 /* Unmap an extent out of a file. */
6198 xfs_bmap_unmap_extent(
6199 struct xfs_trans *tp,
6200 struct xfs_inode *ip,
6201 struct xfs_bmbt_irec *PREV)
6203 if (!xfs_bmap_is_update_needed(PREV))
6206 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6210 * Process one of the deferred bmap operations. We pass back the
6211 * btree cursor to maintain our lock on the bmapbt between calls.
6214 xfs_bmap_finish_one(
6215 struct xfs_trans *tp,
6216 struct xfs_inode *ip,
6217 enum xfs_bmap_intent_type type,
6219 xfs_fileoff_t startoff,
6220 xfs_fsblock_t startblock,
6221 xfs_filblks_t *blockcount,
6226 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6228 trace_xfs_bmap_deferred(tp->t_mountp,
6229 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6230 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6231 ip->i_ino, whichfork, startoff, *blockcount, state);
6233 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6234 return -EFSCORRUPTED;
6236 if (XFS_TEST_ERROR(false, tp->t_mountp,
6237 XFS_ERRTAG_BMAP_FINISH_ONE))
6242 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6246 case XFS_BMAP_UNMAP:
6247 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6248 XFS_BMAPI_REMAP, 1);
6252 error = -EFSCORRUPTED;
6258 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6260 xfs_bmap_validate_extent(
6261 struct xfs_inode *ip,
6263 struct xfs_bmbt_irec *irec)
6265 struct xfs_mount *mp = ip->i_mount;
6266 xfs_fsblock_t endfsb;
6269 isrt = XFS_IS_REALTIME_INODE(ip);
6270 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6272 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6273 return __this_address;
6274 if (!xfs_verify_rtbno(mp, endfsb))
6275 return __this_address;
6277 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6278 return __this_address;
6279 if (!xfs_verify_fsbno(mp, endfsb))
6280 return __this_address;
6281 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6282 XFS_FSB_TO_AGNO(mp, endfsb))
6283 return __this_address;
6285 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6286 return __this_address;