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"
39 kmem_zone_t *xfs_bmap_free_item_zone;
42 * Miscellaneous helper functions
46 * Compute and fill in the value of the maximum depth of a bmap btree
47 * in this filesystem. Done once, during mount.
50 xfs_bmap_compute_maxlevels(
51 xfs_mount_t *mp, /* file system mount structure */
52 int whichfork) /* data or attr fork */
54 int level; /* btree level */
55 uint maxblocks; /* max blocks at this level */
56 uint maxleafents; /* max leaf entries possible */
57 int maxrootrecs; /* max records in root block */
58 int minleafrecs; /* min records in leaf block */
59 int minnoderecs; /* min records in node block */
60 int sz; /* root block size */
63 * The maximum number of extents in a file, hence the maximum
64 * number of leaf entries, is controlled by the type of di_nextents
65 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
66 * (a signed 16-bit number, xfs_aextnum_t).
68 * Note that we can no longer assume that if we are in ATTR1 that
69 * the fork offset of all the inodes will be
70 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
71 * with ATTR2 and then mounted back with ATTR1, keeping the
72 * di_forkoff's fixed but probably at various positions. Therefore,
73 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
74 * of a minimum size available.
76 if (whichfork == XFS_DATA_FORK) {
77 maxleafents = MAXEXTNUM;
78 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
80 maxleafents = MAXAEXTNUM;
81 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
83 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
84 minleafrecs = mp->m_bmap_dmnr[0];
85 minnoderecs = mp->m_bmap_dmnr[1];
86 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
87 for (level = 1; maxblocks > 1; level++) {
88 if (maxblocks <= maxrootrecs)
91 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
93 mp->m_bm_maxlevels[whichfork] = level;
96 STATIC int /* error */
98 struct xfs_btree_cur *cur,
99 struct xfs_bmbt_irec *irec,
100 int *stat) /* success/failure */
102 cur->bc_rec.b = *irec;
103 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
106 STATIC int /* error */
107 xfs_bmbt_lookup_first(
108 struct xfs_btree_cur *cur,
109 int *stat) /* success/failure */
111 cur->bc_rec.b.br_startoff = 0;
112 cur->bc_rec.b.br_startblock = 0;
113 cur->bc_rec.b.br_blockcount = 0;
114 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
118 * Check if the inode needs to be converted to btree format.
120 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
122 return whichfork != XFS_COW_FORK &&
123 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
124 XFS_IFORK_NEXTENTS(ip, whichfork) >
125 XFS_IFORK_MAXEXT(ip, whichfork);
129 * Check if the inode should be converted to extent format.
131 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
133 return whichfork != XFS_COW_FORK &&
134 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
135 XFS_IFORK_NEXTENTS(ip, whichfork) <=
136 XFS_IFORK_MAXEXT(ip, whichfork);
140 * Update the record referred to by cur to the value given by irec
141 * This either works (return 0) or gets an EFSCORRUPTED error.
145 struct xfs_btree_cur *cur,
146 struct xfs_bmbt_irec *irec)
148 union xfs_btree_rec rec;
150 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
151 return xfs_btree_update(cur, &rec);
155 * Compute the worst-case number of indirect blocks that will be used
156 * for ip's delayed extent of length "len".
159 xfs_bmap_worst_indlen(
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_filblks_t len) /* delayed extent length */
163 int level; /* btree level number */
164 int maxrecs; /* maximum record count at this level */
165 xfs_mount_t *mp; /* mount structure */
166 xfs_filblks_t rval; /* return value */
169 maxrecs = mp->m_bmap_dmxr[0];
170 for (level = 0, rval = 0;
171 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
174 do_div(len, maxrecs);
177 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
180 maxrecs = mp->m_bmap_dmxr[1];
186 * Calculate the default attribute fork offset for newly created inodes.
189 xfs_default_attroffset(
190 struct xfs_inode *ip)
192 struct xfs_mount *mp = ip->i_mount;
195 if (mp->m_sb.sb_inodesize == 256) {
196 offset = XFS_LITINO(mp, ip->i_d.di_version) -
197 XFS_BMDR_SPACE_CALC(MINABTPTRS);
199 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
202 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
207 * Helper routine to reset inode di_forkoff field when switching
208 * attribute fork from local to extent format - we reset it where
209 * possible to make space available for inline data fork extents.
212 xfs_bmap_forkoff_reset(
216 if (whichfork == XFS_ATTR_FORK &&
217 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
218 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
219 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
221 if (dfl_forkoff > ip->i_d.di_forkoff)
222 ip->i_d.di_forkoff = dfl_forkoff;
227 STATIC struct xfs_buf *
229 struct xfs_btree_cur *cur,
232 struct xfs_log_item *lip;
238 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
239 if (!cur->bc_bufs[i])
241 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
242 return cur->bc_bufs[i];
245 /* Chase down all the log items to see if the bp is there */
246 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
247 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
249 if (bip->bli_item.li_type == XFS_LI_BUF &&
250 XFS_BUF_ADDR(bip->bli_buf) == bno)
259 struct xfs_btree_block *block,
265 __be64 *pp, *thispa; /* pointer to block address */
266 xfs_bmbt_key_t *prevp, *keyp;
268 ASSERT(be16_to_cpu(block->bb_level) > 0);
271 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
272 dmxr = mp->m_bmap_dmxr[0];
273 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
276 ASSERT(be64_to_cpu(prevp->br_startoff) <
277 be64_to_cpu(keyp->br_startoff));
282 * Compare the block numbers to see if there are dups.
285 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
287 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
289 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
291 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
293 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
294 if (*thispa == *pp) {
295 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
297 (unsigned long long)be64_to_cpu(*thispa));
298 xfs_err(mp, "%s: ptrs are equal in node\n",
300 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
307 * Check that the extents for the inode ip are in the right order in all
308 * btree leaves. THis becomes prohibitively expensive for large extent count
309 * files, so don't bother with inodes that have more than 10,000 extents in
310 * them. The btree record ordering checks will still be done, so for such large
311 * bmapbt constructs that is going to catch most corruptions.
314 xfs_bmap_check_leaf_extents(
315 xfs_btree_cur_t *cur, /* btree cursor or null */
316 xfs_inode_t *ip, /* incore inode pointer */
317 int whichfork) /* data or attr fork */
319 struct xfs_btree_block *block; /* current btree block */
320 xfs_fsblock_t bno; /* block # of "block" */
321 xfs_buf_t *bp; /* buffer for "block" */
322 int error; /* error return value */
323 xfs_extnum_t i=0, j; /* index into the extents list */
324 struct xfs_ifork *ifp; /* fork structure */
325 int level; /* btree level, for checking */
326 xfs_mount_t *mp; /* file system mount structure */
327 __be64 *pp; /* pointer to block address */
328 xfs_bmbt_rec_t *ep; /* pointer to current extent */
329 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
330 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
333 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
337 /* skip large extent count inodes */
338 if (ip->i_d.di_nextents > 10000)
343 ifp = XFS_IFORK_PTR(ip, whichfork);
344 block = ifp->if_broot;
346 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
348 level = be16_to_cpu(block->bb_level);
350 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
351 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
352 bno = be64_to_cpu(*pp);
354 ASSERT(bno != NULLFSBLOCK);
355 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
356 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
359 * Go down the tree until leaf level is reached, following the first
360 * pointer (leftmost) at each level.
362 while (level-- > 0) {
363 /* See if buf is in cur first */
365 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
368 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
374 block = XFS_BUF_TO_BLOCK(bp);
379 * Check this block for basic sanity (increasing keys and
380 * no duplicate blocks).
383 xfs_check_block(block, mp, 0, 0);
384 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
385 bno = be64_to_cpu(*pp);
386 XFS_WANT_CORRUPTED_GOTO(mp,
387 xfs_verify_fsbno(mp, bno), error0);
390 xfs_trans_brelse(NULL, bp);
395 * Here with bp and block set to the leftmost leaf node in the tree.
400 * Loop over all leaf nodes checking that all extents are in the right order.
403 xfs_fsblock_t nextbno;
404 xfs_extnum_t num_recs;
407 num_recs = xfs_btree_get_numrecs(block);
410 * Read-ahead the next leaf block, if any.
413 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
416 * Check all the extents to make sure they are OK.
417 * If we had a previous block, the last entry should
418 * conform with the first entry in this one.
421 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
423 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
424 xfs_bmbt_disk_get_blockcount(&last) <=
425 xfs_bmbt_disk_get_startoff(ep));
427 for (j = 1; j < num_recs; j++) {
428 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
429 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
430 xfs_bmbt_disk_get_blockcount(ep) <=
431 xfs_bmbt_disk_get_startoff(nextp));
439 xfs_trans_brelse(NULL, bp);
443 * If we've reached the end, stop.
445 if (bno == NULLFSBLOCK)
449 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
452 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
458 block = XFS_BUF_TO_BLOCK(bp);
464 xfs_warn(mp, "%s: at error0", __func__);
466 xfs_trans_brelse(NULL, bp);
468 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
470 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
471 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
476 * Validate that the bmbt_irecs being returned from bmapi are valid
477 * given the caller's original parameters. Specifically check the
478 * ranges of the returned irecs to ensure that they only extend beyond
479 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
482 xfs_bmap_validate_ret(
486 xfs_bmbt_irec_t *mval,
490 int i; /* index to map values */
492 ASSERT(ret_nmap <= nmap);
494 for (i = 0; i < ret_nmap; i++) {
495 ASSERT(mval[i].br_blockcount > 0);
496 if (!(flags & XFS_BMAPI_ENTIRE)) {
497 ASSERT(mval[i].br_startoff >= bno);
498 ASSERT(mval[i].br_blockcount <= len);
499 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
502 ASSERT(mval[i].br_startoff < bno + len);
503 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
507 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
508 mval[i].br_startoff);
509 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
510 mval[i].br_startblock != HOLESTARTBLOCK);
511 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
512 mval[i].br_state == XFS_EXT_UNWRITTEN);
517 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
518 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
522 * bmap free list manipulation functions
526 * Add the extent to the list of extents to be free at transaction end.
527 * The list is maintained sorted (by block number).
531 struct xfs_trans *tp,
534 const struct xfs_owner_info *oinfo,
537 struct xfs_extent_free_item *new; /* new element */
539 struct xfs_mount *mp = tp->t_mountp;
543 ASSERT(bno != NULLFSBLOCK);
545 ASSERT(len <= MAXEXTLEN);
546 ASSERT(!isnullstartblock(bno));
547 agno = XFS_FSB_TO_AGNO(mp, bno);
548 agbno = XFS_FSB_TO_AGBNO(mp, bno);
549 ASSERT(agno < mp->m_sb.sb_agcount);
550 ASSERT(agbno < mp->m_sb.sb_agblocks);
551 ASSERT(len < mp->m_sb.sb_agblocks);
552 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
554 ASSERT(xfs_bmap_free_item_zone != NULL);
556 new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
557 new->xefi_startblock = bno;
558 new->xefi_blockcount = (xfs_extlen_t)len;
560 new->xefi_oinfo = *oinfo;
562 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
563 new->xefi_skip_discard = skip_discard;
564 trace_xfs_bmap_free_defer(tp->t_mountp,
565 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
566 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
567 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
571 * Inode fork format manipulation functions
575 * Convert the inode format to extent format if it currently is in btree format,
576 * but the extent list is small enough that it fits into the extent format.
578 * Since the extents are already in-core, all we have to do is give up the space
579 * for the btree root and pitch the leaf block.
581 STATIC int /* error */
582 xfs_bmap_btree_to_extents(
583 struct xfs_trans *tp, /* transaction pointer */
584 struct xfs_inode *ip, /* incore inode pointer */
585 struct xfs_btree_cur *cur, /* btree cursor */
586 int *logflagsp, /* inode logging flags */
587 int whichfork) /* data or attr fork */
589 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
590 struct xfs_mount *mp = ip->i_mount;
591 struct xfs_btree_block *rblock = ifp->if_broot;
592 struct xfs_btree_block *cblock;/* child btree block */
593 xfs_fsblock_t cbno; /* child block number */
594 xfs_buf_t *cbp; /* child block's buffer */
595 int error; /* error return value */
596 __be64 *pp; /* ptr to block address */
597 struct xfs_owner_info oinfo;
599 /* check if we actually need the extent format first: */
600 if (!xfs_bmap_wants_extents(ip, whichfork))
604 ASSERT(whichfork != XFS_COW_FORK);
605 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
606 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
607 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
608 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
609 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
611 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
612 cbno = be64_to_cpu(*pp);
614 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
615 xfs_btree_check_lptr(cur, cbno, 1));
617 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
621 cblock = XFS_BUF_TO_BLOCK(cbp);
622 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
624 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
625 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
626 ip->i_d.di_nblocks--;
627 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
628 xfs_trans_binval(tp, cbp);
629 if (cur->bc_bufs[0] == cbp)
630 cur->bc_bufs[0] = NULL;
631 xfs_iroot_realloc(ip, -1, whichfork);
632 ASSERT(ifp->if_broot == NULL);
633 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
634 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
635 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
640 * Convert an extents-format file into a btree-format file.
641 * The new file will have a root block (in the inode) and a single child block.
643 STATIC int /* error */
644 xfs_bmap_extents_to_btree(
645 struct xfs_trans *tp, /* transaction pointer */
646 struct xfs_inode *ip, /* incore inode pointer */
647 struct xfs_btree_cur **curp, /* cursor returned to caller */
648 int wasdel, /* converting a delayed alloc */
649 int *logflagsp, /* inode logging flags */
650 int whichfork) /* data or attr fork */
652 struct xfs_btree_block *ablock; /* allocated (child) bt block */
653 struct xfs_buf *abp; /* buffer for ablock */
654 struct xfs_alloc_arg args; /* allocation arguments */
655 struct xfs_bmbt_rec *arp; /* child record pointer */
656 struct xfs_btree_block *block; /* btree root block */
657 struct xfs_btree_cur *cur; /* bmap btree cursor */
658 int error; /* error return value */
659 struct xfs_ifork *ifp; /* inode fork pointer */
660 struct xfs_bmbt_key *kp; /* root block key pointer */
661 struct xfs_mount *mp; /* mount structure */
662 xfs_bmbt_ptr_t *pp; /* root block address pointer */
663 struct xfs_iext_cursor icur;
664 struct xfs_bmbt_irec rec;
665 xfs_extnum_t cnt = 0;
668 ASSERT(whichfork != XFS_COW_FORK);
669 ifp = XFS_IFORK_PTR(ip, whichfork);
670 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
673 * Make space in the inode incore. This needs to be undone if we fail
674 * to expand the root.
676 xfs_iroot_realloc(ip, 1, whichfork);
677 ifp->if_flags |= XFS_IFBROOT;
682 block = ifp->if_broot;
683 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
684 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
685 XFS_BTREE_LONG_PTRS);
687 * Need a cursor. Can't allocate until bb_level is filled in.
689 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
690 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
692 * Convert to a btree with two levels, one record in root.
694 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
695 memset(&args, 0, sizeof(args));
698 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
699 if (tp->t_firstblock == NULLFSBLOCK) {
700 args.type = XFS_ALLOCTYPE_START_BNO;
701 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
702 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
703 args.type = XFS_ALLOCTYPE_START_BNO;
704 args.fsbno = tp->t_firstblock;
706 args.type = XFS_ALLOCTYPE_NEAR_BNO;
707 args.fsbno = tp->t_firstblock;
709 args.minlen = args.maxlen = args.prod = 1;
710 args.wasdel = wasdel;
712 error = xfs_alloc_vextent(&args);
714 goto out_root_realloc;
716 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
718 goto out_root_realloc;
722 * Allocation can't fail, the space was reserved.
724 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
725 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
726 tp->t_firstblock = args.fsbno;
727 cur->bc_private.b.allocated++;
728 ip->i_d.di_nblocks++;
729 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
730 abp = xfs_btree_get_bufl(mp, tp, args.fsbno);
732 error = -EFSCORRUPTED;
733 goto out_unreserve_dquot;
737 * Fill in the child block.
739 abp->b_ops = &xfs_bmbt_buf_ops;
740 ablock = XFS_BUF_TO_BLOCK(abp);
741 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
742 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
743 XFS_BTREE_LONG_PTRS);
745 for_each_xfs_iext(ifp, &icur, &rec) {
746 if (isnullstartblock(rec.br_startblock))
748 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
749 xfs_bmbt_disk_set_all(arp, &rec);
752 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
753 xfs_btree_set_numrecs(ablock, cnt);
756 * Fill in the root key and pointer.
758 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
759 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
760 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
761 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
762 be16_to_cpu(block->bb_level)));
763 *pp = cpu_to_be64(args.fsbno);
766 * Do all this logging at the end so that
767 * the root is at the right level.
769 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
770 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
771 ASSERT(*curp == NULL);
773 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
777 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
779 xfs_iroot_realloc(ip, -1, whichfork);
780 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
781 ASSERT(ifp->if_broot == NULL);
782 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
788 * Convert a local file to an extents file.
789 * This code is out of bounds for data forks of regular files,
790 * since the file data needs to get logged so things will stay consistent.
791 * (The bmap-level manipulations are ok, though).
794 xfs_bmap_local_to_extents_empty(
795 struct xfs_inode *ip,
798 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
800 ASSERT(whichfork != XFS_COW_FORK);
801 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
802 ASSERT(ifp->if_bytes == 0);
803 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
805 xfs_bmap_forkoff_reset(ip, whichfork);
806 ifp->if_flags &= ~XFS_IFINLINE;
807 ifp->if_flags |= XFS_IFEXTENTS;
808 ifp->if_u1.if_root = NULL;
810 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
814 STATIC int /* error */
815 xfs_bmap_local_to_extents(
816 xfs_trans_t *tp, /* transaction pointer */
817 xfs_inode_t *ip, /* incore inode pointer */
818 xfs_extlen_t total, /* total blocks needed by transaction */
819 int *logflagsp, /* inode logging flags */
821 void (*init_fn)(struct xfs_trans *tp,
823 struct xfs_inode *ip,
824 struct xfs_ifork *ifp))
827 int flags; /* logging flags returned */
828 struct xfs_ifork *ifp; /* inode fork pointer */
829 xfs_alloc_arg_t args; /* allocation arguments */
830 xfs_buf_t *bp; /* buffer for extent block */
831 struct xfs_bmbt_irec rec;
832 struct xfs_iext_cursor icur;
835 * We don't want to deal with the case of keeping inode data inline yet.
836 * So sending the data fork of a regular inode is invalid.
838 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
839 ifp = XFS_IFORK_PTR(ip, whichfork);
840 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
842 if (!ifp->if_bytes) {
843 xfs_bmap_local_to_extents_empty(ip, whichfork);
844 flags = XFS_ILOG_CORE;
850 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
851 memset(&args, 0, sizeof(args));
853 args.mp = ip->i_mount;
854 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
856 * Allocate a block. We know we need only one, since the
857 * file currently fits in an inode.
859 if (tp->t_firstblock == NULLFSBLOCK) {
860 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
861 args.type = XFS_ALLOCTYPE_START_BNO;
863 args.fsbno = tp->t_firstblock;
864 args.type = XFS_ALLOCTYPE_NEAR_BNO;
867 args.minlen = args.maxlen = args.prod = 1;
868 error = xfs_alloc_vextent(&args);
872 /* Can't fail, the space was reserved. */
873 ASSERT(args.fsbno != NULLFSBLOCK);
874 ASSERT(args.len == 1);
875 tp->t_firstblock = args.fsbno;
876 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno);
879 * Initialize the block, copy the data and log the remote buffer.
881 * The callout is responsible for logging because the remote format
882 * might differ from the local format and thus we don't know how much to
883 * log here. Note that init_fn must also set the buffer log item type
886 init_fn(tp, bp, ip, ifp);
888 /* account for the change in fork size */
889 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
890 xfs_bmap_local_to_extents_empty(ip, whichfork);
891 flags |= XFS_ILOG_CORE;
893 ifp->if_u1.if_root = NULL;
897 rec.br_startblock = args.fsbno;
898 rec.br_blockcount = 1;
899 rec.br_state = XFS_EXT_NORM;
900 xfs_iext_first(ifp, &icur);
901 xfs_iext_insert(ip, &icur, &rec, 0);
903 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
904 ip->i_d.di_nblocks = 1;
905 xfs_trans_mod_dquot_byino(tp, ip,
906 XFS_TRANS_DQ_BCOUNT, 1L);
907 flags |= xfs_ilog_fext(whichfork);
915 * Called from xfs_bmap_add_attrfork to handle btree format files.
917 STATIC int /* error */
918 xfs_bmap_add_attrfork_btree(
919 xfs_trans_t *tp, /* transaction pointer */
920 xfs_inode_t *ip, /* incore inode pointer */
921 int *flags) /* inode logging flags */
923 xfs_btree_cur_t *cur; /* btree cursor */
924 int error; /* error return value */
925 xfs_mount_t *mp; /* file system mount struct */
926 int stat; /* newroot status */
929 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
930 *flags |= XFS_ILOG_DBROOT;
932 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
933 error = xfs_bmbt_lookup_first(cur, &stat);
936 /* must be at least one entry */
937 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
938 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
941 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
944 cur->bc_private.b.allocated = 0;
945 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
949 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
954 * Called from xfs_bmap_add_attrfork to handle extents format files.
956 STATIC int /* error */
957 xfs_bmap_add_attrfork_extents(
958 struct xfs_trans *tp, /* transaction pointer */
959 struct xfs_inode *ip, /* incore inode pointer */
960 int *flags) /* inode logging flags */
962 xfs_btree_cur_t *cur; /* bmap btree cursor */
963 int error; /* error return value */
965 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
968 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
971 cur->bc_private.b.allocated = 0;
972 xfs_btree_del_cursor(cur, error);
978 * Called from xfs_bmap_add_attrfork to handle local format files. Each
979 * different data fork content type needs a different callout to do the
980 * conversion. Some are basic and only require special block initialisation
981 * callouts for the data formating, others (directories) are so specialised they
982 * handle everything themselves.
984 * XXX (dgc): investigate whether directory conversion can use the generic
985 * formatting callout. It should be possible - it's just a very complex
988 STATIC int /* error */
989 xfs_bmap_add_attrfork_local(
990 struct xfs_trans *tp, /* transaction pointer */
991 struct xfs_inode *ip, /* incore inode pointer */
992 int *flags) /* inode logging flags */
994 struct xfs_da_args dargs; /* args for dir/attr code */
996 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
999 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1000 memset(&dargs, 0, sizeof(dargs));
1001 dargs.geo = ip->i_mount->m_dir_geo;
1003 dargs.total = dargs.geo->fsbcount;
1004 dargs.whichfork = XFS_DATA_FORK;
1006 return xfs_dir2_sf_to_block(&dargs);
1009 if (S_ISLNK(VFS_I(ip)->i_mode))
1010 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1012 xfs_symlink_local_to_remote);
1014 /* should only be called for types that support local format data */
1016 return -EFSCORRUPTED;
1019 /* Set an inode attr fork off based on the format */
1021 xfs_bmap_set_attrforkoff(
1022 struct xfs_inode *ip,
1026 switch (ip->i_d.di_format) {
1027 case XFS_DINODE_FMT_DEV:
1028 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1030 case XFS_DINODE_FMT_LOCAL:
1031 case XFS_DINODE_FMT_EXTENTS:
1032 case XFS_DINODE_FMT_BTREE:
1033 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1034 if (!ip->i_d.di_forkoff)
1035 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1036 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1048 * Convert inode from non-attributed to attributed.
1049 * Must not be in a transaction, ip must not be locked.
1051 int /* error code */
1052 xfs_bmap_add_attrfork(
1053 xfs_inode_t *ip, /* incore inode pointer */
1054 int size, /* space new attribute needs */
1055 int rsvd) /* xact may use reserved blks */
1057 xfs_mount_t *mp; /* mount structure */
1058 xfs_trans_t *tp; /* transaction pointer */
1059 int blks; /* space reservation */
1060 int version = 1; /* superblock attr version */
1061 int logflags; /* logging flags */
1062 int error; /* error return value */
1064 ASSERT(XFS_IFORK_Q(ip) == 0);
1067 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1069 blks = XFS_ADDAFORK_SPACE_RES(mp);
1071 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1072 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1076 xfs_ilock(ip, XFS_ILOCK_EXCL);
1077 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1078 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1079 XFS_QMOPT_RES_REGBLKS);
1082 if (XFS_IFORK_Q(ip))
1084 if (ip->i_d.di_anextents != 0) {
1085 error = -EFSCORRUPTED;
1088 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1090 * For inodes coming from pre-6.2 filesystems.
1092 ASSERT(ip->i_d.di_aformat == 0);
1093 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1096 xfs_trans_ijoin(tp, ip, 0);
1097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1098 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1101 ASSERT(ip->i_afp == NULL);
1102 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0);
1103 ip->i_afp->if_flags = XFS_IFEXTENTS;
1105 switch (ip->i_d.di_format) {
1106 case XFS_DINODE_FMT_LOCAL:
1107 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1109 case XFS_DINODE_FMT_EXTENTS:
1110 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1112 case XFS_DINODE_FMT_BTREE:
1113 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1120 xfs_trans_log_inode(tp, ip, logflags);
1123 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1124 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1125 bool log_sb = false;
1127 spin_lock(&mp->m_sb_lock);
1128 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1129 xfs_sb_version_addattr(&mp->m_sb);
1132 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1133 xfs_sb_version_addattr2(&mp->m_sb);
1136 spin_unlock(&mp->m_sb_lock);
1141 error = xfs_trans_commit(tp);
1142 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1146 xfs_trans_cancel(tp);
1147 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1152 * Internal and external extent tree search functions.
1156 * Read in extents from a btree-format inode.
1160 struct xfs_trans *tp,
1161 struct xfs_inode *ip,
1164 struct xfs_mount *mp = ip->i_mount;
1165 int state = xfs_bmap_fork_to_state(whichfork);
1166 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1167 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1168 struct xfs_btree_block *block = ifp->if_broot;
1169 struct xfs_iext_cursor icur;
1170 struct xfs_bmbt_irec new;
1178 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1180 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1181 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1182 return -EFSCORRUPTED;
1186 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1188 level = be16_to_cpu(block->bb_level);
1189 if (unlikely(level == 0)) {
1190 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1191 return -EFSCORRUPTED;
1193 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1194 bno = be64_to_cpu(*pp);
1197 * Go down the tree until leaf level is reached, following the first
1198 * pointer (leftmost) at each level.
1200 while (level-- > 0) {
1201 error = xfs_btree_read_bufl(mp, tp, bno, &bp,
1202 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1205 block = XFS_BUF_TO_BLOCK(bp);
1208 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1209 bno = be64_to_cpu(*pp);
1210 XFS_WANT_CORRUPTED_GOTO(mp,
1211 xfs_verify_fsbno(mp, bno), out_brelse);
1212 xfs_trans_brelse(tp, bp);
1216 * Here with bp and block set to the leftmost leaf node in the tree.
1219 xfs_iext_first(ifp, &icur);
1222 * Loop over all leaf nodes. Copy information to the extent records.
1225 xfs_bmbt_rec_t *frp;
1226 xfs_fsblock_t nextbno;
1227 xfs_extnum_t num_recs;
1229 num_recs = xfs_btree_get_numrecs(block);
1230 if (unlikely(i + num_recs > nextents)) {
1231 xfs_warn(ip->i_mount,
1232 "corrupt dinode %Lu, (btree extents).",
1233 (unsigned long long) ip->i_ino);
1234 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1235 __func__, block, sizeof(*block),
1237 error = -EFSCORRUPTED;
1241 * Read-ahead the next leaf block, if any.
1243 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1244 if (nextbno != NULLFSBLOCK)
1245 xfs_btree_reada_bufl(mp, nextbno, 1,
1248 * Copy records into the extent records.
1250 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1251 for (j = 0; j < num_recs; j++, frp++, i++) {
1254 xfs_bmbt_disk_get_all(frp, &new);
1255 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1257 error = -EFSCORRUPTED;
1258 xfs_inode_verifier_error(ip, error,
1259 "xfs_iread_extents(2)",
1260 frp, sizeof(*frp), fa);
1263 xfs_iext_insert(ip, &icur, &new, state);
1264 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1265 xfs_iext_next(ifp, &icur);
1267 xfs_trans_brelse(tp, bp);
1270 * If we've reached the end, stop.
1272 if (bno == NULLFSBLOCK)
1274 error = xfs_btree_read_bufl(mp, tp, bno, &bp,
1275 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1278 block = XFS_BUF_TO_BLOCK(bp);
1281 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1282 error = -EFSCORRUPTED;
1285 ASSERT(i == xfs_iext_count(ifp));
1287 ifp->if_flags |= XFS_IFEXTENTS;
1291 xfs_trans_brelse(tp, bp);
1293 xfs_iext_destroy(ifp);
1298 * Returns the relative block number of the first unused block(s) in the given
1299 * fork with at least "len" logically contiguous blocks free. This is the
1300 * lowest-address hole if the fork has holes, else the first block past the end
1301 * of fork. Return 0 if the fork is currently local (in-inode).
1304 xfs_bmap_first_unused(
1305 struct xfs_trans *tp, /* transaction pointer */
1306 struct xfs_inode *ip, /* incore inode */
1307 xfs_extlen_t len, /* size of hole to find */
1308 xfs_fileoff_t *first_unused, /* unused block */
1309 int whichfork) /* data or attr fork */
1311 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1312 struct xfs_bmbt_irec got;
1313 struct xfs_iext_cursor icur;
1314 xfs_fileoff_t lastaddr = 0;
1315 xfs_fileoff_t lowest, max;
1318 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1319 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1320 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1322 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1327 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1328 error = xfs_iread_extents(tp, ip, whichfork);
1333 lowest = max = *first_unused;
1334 for_each_xfs_iext(ifp, &icur, &got) {
1336 * See if the hole before this extent will work.
1338 if (got.br_startoff >= lowest + len &&
1339 got.br_startoff - max >= len)
1341 lastaddr = got.br_startoff + got.br_blockcount;
1342 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1345 *first_unused = max;
1350 * Returns the file-relative block number of the last block - 1 before
1351 * last_block (input value) in the file.
1352 * This is not based on i_size, it is based on the extent records.
1353 * Returns 0 for local files, as they do not have extent records.
1356 xfs_bmap_last_before(
1357 struct xfs_trans *tp, /* transaction pointer */
1358 struct xfs_inode *ip, /* incore inode */
1359 xfs_fileoff_t *last_block, /* last block */
1360 int whichfork) /* data or attr fork */
1362 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1363 struct xfs_bmbt_irec got;
1364 struct xfs_iext_cursor icur;
1367 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1368 case XFS_DINODE_FMT_LOCAL:
1371 case XFS_DINODE_FMT_BTREE:
1372 case XFS_DINODE_FMT_EXTENTS:
1378 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1379 error = xfs_iread_extents(tp, ip, whichfork);
1384 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1390 xfs_bmap_last_extent(
1391 struct xfs_trans *tp,
1392 struct xfs_inode *ip,
1394 struct xfs_bmbt_irec *rec,
1397 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1398 struct xfs_iext_cursor icur;
1401 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1402 error = xfs_iread_extents(tp, ip, whichfork);
1407 xfs_iext_last(ifp, &icur);
1408 if (!xfs_iext_get_extent(ifp, &icur, rec))
1416 * Check the last inode extent to determine whether this allocation will result
1417 * in blocks being allocated at the end of the file. When we allocate new data
1418 * blocks at the end of the file which do not start at the previous data block,
1419 * we will try to align the new blocks at stripe unit boundaries.
1421 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1422 * at, or past the EOF.
1426 struct xfs_bmalloca *bma,
1429 struct xfs_bmbt_irec rec;
1434 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1445 * Check if we are allocation or past the last extent, or at least into
1446 * the last delayed allocated extent.
1448 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1449 (bma->offset >= rec.br_startoff &&
1450 isnullstartblock(rec.br_startblock));
1455 * Returns the file-relative block number of the first block past eof in
1456 * the file. This is not based on i_size, it is based on the extent records.
1457 * Returns 0 for local files, as they do not have extent records.
1460 xfs_bmap_last_offset(
1461 struct xfs_inode *ip,
1462 xfs_fileoff_t *last_block,
1465 struct xfs_bmbt_irec rec;
1471 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1474 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1475 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1478 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1479 if (error || is_empty)
1482 *last_block = rec.br_startoff + rec.br_blockcount;
1487 * Returns whether the selected fork of the inode has exactly one
1488 * block or not. For the data fork we check this matches di_size,
1489 * implying the file's range is 0..bsize-1.
1491 int /* 1=>1 block, 0=>otherwise */
1493 xfs_inode_t *ip, /* incore inode */
1494 int whichfork) /* data or attr fork */
1496 struct xfs_ifork *ifp; /* inode fork pointer */
1497 int rval; /* return value */
1498 xfs_bmbt_irec_t s; /* internal version of extent */
1499 struct xfs_iext_cursor icur;
1502 if (whichfork == XFS_DATA_FORK)
1503 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1505 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1507 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1509 ifp = XFS_IFORK_PTR(ip, whichfork);
1510 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1511 xfs_iext_first(ifp, &icur);
1512 xfs_iext_get_extent(ifp, &icur, &s);
1513 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1514 if (rval && whichfork == XFS_DATA_FORK)
1515 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1520 * Extent tree manipulation functions used during allocation.
1524 * Convert a delayed allocation to a real allocation.
1526 STATIC int /* error */
1527 xfs_bmap_add_extent_delay_real(
1528 struct xfs_bmalloca *bma,
1531 struct xfs_bmbt_irec *new = &bma->got;
1532 int error; /* error return value */
1533 int i; /* temp state */
1534 struct xfs_ifork *ifp; /* inode fork pointer */
1535 xfs_fileoff_t new_endoff; /* end offset of new entry */
1536 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1537 /* left is 0, right is 1, prev is 2 */
1538 int rval=0; /* return value (logging flags) */
1539 int state = xfs_bmap_fork_to_state(whichfork);
1540 xfs_filblks_t da_new; /* new count del alloc blocks used */
1541 xfs_filblks_t da_old; /* old count del alloc blocks used */
1542 xfs_filblks_t temp=0; /* value for da_new calculations */
1543 int tmp_rval; /* partial logging flags */
1544 struct xfs_mount *mp;
1545 xfs_extnum_t *nextents;
1546 struct xfs_bmbt_irec old;
1548 mp = bma->ip->i_mount;
1549 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1550 ASSERT(whichfork != XFS_ATTR_FORK);
1551 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1552 &bma->ip->i_d.di_nextents);
1554 ASSERT(!isnullstartblock(new->br_startblock));
1556 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1558 XFS_STATS_INC(mp, xs_add_exlist);
1565 * Set up a bunch of variables to make the tests simpler.
1567 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1568 new_endoff = new->br_startoff + new->br_blockcount;
1569 ASSERT(isnullstartblock(PREV.br_startblock));
1570 ASSERT(PREV.br_startoff <= new->br_startoff);
1571 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1573 da_old = startblockval(PREV.br_startblock);
1577 * Set flags determining what part of the previous delayed allocation
1578 * extent is being replaced by a real allocation.
1580 if (PREV.br_startoff == new->br_startoff)
1581 state |= BMAP_LEFT_FILLING;
1582 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1583 state |= BMAP_RIGHT_FILLING;
1586 * Check and set flags if this segment has a left neighbor.
1587 * Don't set contiguous if the combined extent would be too large.
1589 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1590 state |= BMAP_LEFT_VALID;
1591 if (isnullstartblock(LEFT.br_startblock))
1592 state |= BMAP_LEFT_DELAY;
1595 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1596 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1597 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1598 LEFT.br_state == new->br_state &&
1599 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1600 state |= BMAP_LEFT_CONTIG;
1603 * Check and set flags if this segment has a right neighbor.
1604 * Don't set contiguous if the combined extent would be too large.
1605 * Also check for all-three-contiguous being too large.
1607 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1608 state |= BMAP_RIGHT_VALID;
1609 if (isnullstartblock(RIGHT.br_startblock))
1610 state |= BMAP_RIGHT_DELAY;
1613 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1614 new_endoff == RIGHT.br_startoff &&
1615 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1616 new->br_state == RIGHT.br_state &&
1617 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1618 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1619 BMAP_RIGHT_FILLING)) !=
1620 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1621 BMAP_RIGHT_FILLING) ||
1622 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1624 state |= BMAP_RIGHT_CONTIG;
1628 * Switch out based on the FILLING and CONTIG state bits.
1630 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1631 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1632 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1633 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1635 * Filling in all of a previously delayed allocation extent.
1636 * The left and right neighbors are both contiguous with new.
1638 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1640 xfs_iext_remove(bma->ip, &bma->icur, state);
1641 xfs_iext_remove(bma->ip, &bma->icur, state);
1642 xfs_iext_prev(ifp, &bma->icur);
1643 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1646 if (bma->cur == NULL)
1647 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1649 rval = XFS_ILOG_CORE;
1650 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1653 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1654 error = xfs_btree_delete(bma->cur, &i);
1657 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1658 error = xfs_btree_decrement(bma->cur, 0, &i);
1661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1662 error = xfs_bmbt_update(bma->cur, &LEFT);
1668 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1670 * Filling in all of a previously delayed allocation extent.
1671 * The left neighbor is contiguous, the right is not.
1674 LEFT.br_blockcount += PREV.br_blockcount;
1676 xfs_iext_remove(bma->ip, &bma->icur, state);
1677 xfs_iext_prev(ifp, &bma->icur);
1678 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1680 if (bma->cur == NULL)
1681 rval = XFS_ILOG_DEXT;
1684 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1687 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1688 error = xfs_bmbt_update(bma->cur, &LEFT);
1694 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1696 * Filling in all of a previously delayed allocation extent.
1697 * The right neighbor is contiguous, the left is not. Take care
1698 * with delay -> unwritten extent allocation here because the
1699 * delalloc record we are overwriting is always written.
1701 PREV.br_startblock = new->br_startblock;
1702 PREV.br_blockcount += RIGHT.br_blockcount;
1703 PREV.br_state = new->br_state;
1705 xfs_iext_next(ifp, &bma->icur);
1706 xfs_iext_remove(bma->ip, &bma->icur, state);
1707 xfs_iext_prev(ifp, &bma->icur);
1708 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1710 if (bma->cur == NULL)
1711 rval = XFS_ILOG_DEXT;
1714 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1717 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1718 error = xfs_bmbt_update(bma->cur, &PREV);
1724 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1726 * Filling in all of a previously delayed allocation extent.
1727 * Neither the left nor right neighbors are contiguous with
1730 PREV.br_startblock = new->br_startblock;
1731 PREV.br_state = new->br_state;
1732 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1735 if (bma->cur == NULL)
1736 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1738 rval = XFS_ILOG_CORE;
1739 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1742 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1743 error = xfs_btree_insert(bma->cur, &i);
1746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1750 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1752 * Filling in the first part of a previous delayed allocation.
1753 * The left neighbor is contiguous.
1756 temp = PREV.br_blockcount - new->br_blockcount;
1757 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1758 startblockval(PREV.br_startblock));
1760 LEFT.br_blockcount += new->br_blockcount;
1762 PREV.br_blockcount = temp;
1763 PREV.br_startoff += new->br_blockcount;
1764 PREV.br_startblock = nullstartblock(da_new);
1766 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1767 xfs_iext_prev(ifp, &bma->icur);
1768 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1770 if (bma->cur == NULL)
1771 rval = XFS_ILOG_DEXT;
1774 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1778 error = xfs_bmbt_update(bma->cur, &LEFT);
1784 case BMAP_LEFT_FILLING:
1786 * Filling in the first part of a previous delayed allocation.
1787 * The left neighbor is not contiguous.
1789 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1791 if (bma->cur == NULL)
1792 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1794 rval = XFS_ILOG_CORE;
1795 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1798 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1799 error = xfs_btree_insert(bma->cur, &i);
1802 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1805 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1806 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1807 &bma->cur, 1, &tmp_rval, whichfork);
1813 temp = PREV.br_blockcount - new->br_blockcount;
1814 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1815 startblockval(PREV.br_startblock) -
1816 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1818 PREV.br_startoff = new_endoff;
1819 PREV.br_blockcount = temp;
1820 PREV.br_startblock = nullstartblock(da_new);
1821 xfs_iext_next(ifp, &bma->icur);
1822 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1823 xfs_iext_prev(ifp, &bma->icur);
1826 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1828 * Filling in the last part of a previous delayed allocation.
1829 * The right neighbor is contiguous with the new allocation.
1832 RIGHT.br_startoff = new->br_startoff;
1833 RIGHT.br_startblock = new->br_startblock;
1834 RIGHT.br_blockcount += new->br_blockcount;
1836 if (bma->cur == NULL)
1837 rval = XFS_ILOG_DEXT;
1840 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1843 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1844 error = xfs_bmbt_update(bma->cur, &RIGHT);
1849 temp = PREV.br_blockcount - new->br_blockcount;
1850 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1851 startblockval(PREV.br_startblock));
1853 PREV.br_blockcount = temp;
1854 PREV.br_startblock = nullstartblock(da_new);
1856 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1857 xfs_iext_next(ifp, &bma->icur);
1858 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1861 case BMAP_RIGHT_FILLING:
1863 * Filling in the last part of a previous delayed allocation.
1864 * The right neighbor is not contiguous.
1866 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1868 if (bma->cur == NULL)
1869 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1871 rval = XFS_ILOG_CORE;
1872 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1875 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1876 error = xfs_btree_insert(bma->cur, &i);
1879 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1882 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1883 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1884 &bma->cur, 1, &tmp_rval, whichfork);
1890 temp = PREV.br_blockcount - new->br_blockcount;
1891 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1892 startblockval(PREV.br_startblock) -
1893 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1895 PREV.br_startblock = nullstartblock(da_new);
1896 PREV.br_blockcount = temp;
1897 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1898 xfs_iext_next(ifp, &bma->icur);
1903 * Filling in the middle part of a previous delayed allocation.
1904 * Contiguity is impossible here.
1905 * This case is avoided almost all the time.
1907 * We start with a delayed allocation:
1909 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1912 * and we are allocating:
1913 * +rrrrrrrrrrrrrrrrr+
1916 * and we set it up for insertion as:
1917 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1919 * PREV @ idx LEFT RIGHT
1920 * inserted at idx + 1
1924 /* LEFT is the new middle */
1927 /* RIGHT is the new right */
1928 RIGHT.br_state = PREV.br_state;
1929 RIGHT.br_startoff = new_endoff;
1930 RIGHT.br_blockcount =
1931 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1932 RIGHT.br_startblock =
1933 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1934 RIGHT.br_blockcount));
1937 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1938 PREV.br_startblock =
1939 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1940 PREV.br_blockcount));
1941 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1943 xfs_iext_next(ifp, &bma->icur);
1944 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1945 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1948 if (bma->cur == NULL)
1949 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1951 rval = XFS_ILOG_CORE;
1952 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1955 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1956 error = xfs_btree_insert(bma->cur, &i);
1959 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1962 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1963 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1964 &bma->cur, 1, &tmp_rval, whichfork);
1970 da_new = startblockval(PREV.br_startblock) +
1971 startblockval(RIGHT.br_startblock);
1974 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1975 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1976 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1977 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1978 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_LEFT_CONTIG:
1980 case BMAP_RIGHT_CONTIG:
1982 * These cases are all impossible.
1987 /* add reverse mapping unless caller opted out */
1988 if (!(bma->flags & XFS_BMAPI_NORMAP))
1989 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1991 /* convert to a btree if necessary */
1992 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1993 int tmp_logflags; /* partial log flag return val */
1995 ASSERT(bma->cur == NULL);
1996 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1997 &bma->cur, da_old > 0, &tmp_logflags,
1999 bma->logflags |= tmp_logflags;
2004 if (da_new != da_old)
2005 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2008 da_new += bma->cur->bc_private.b.allocated;
2009 bma->cur->bc_private.b.allocated = 0;
2012 /* adjust for changes in reserved delayed indirect blocks */
2013 if (da_new != da_old) {
2014 ASSERT(state == 0 || da_new < da_old);
2015 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2019 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2021 if (whichfork != XFS_COW_FORK)
2022 bma->logflags |= rval;
2030 * Convert an unwritten allocation to a real allocation or vice versa.
2033 xfs_bmap_add_extent_unwritten_real(
2034 struct xfs_trans *tp,
2035 xfs_inode_t *ip, /* incore inode pointer */
2037 struct xfs_iext_cursor *icur,
2038 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2039 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2040 int *logflagsp) /* inode logging flags */
2042 xfs_btree_cur_t *cur; /* btree cursor */
2043 int error; /* error return value */
2044 int i; /* temp state */
2045 struct xfs_ifork *ifp; /* inode fork pointer */
2046 xfs_fileoff_t new_endoff; /* end offset of new entry */
2047 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2048 /* left is 0, right is 1, prev is 2 */
2049 int rval=0; /* return value (logging flags) */
2050 int state = xfs_bmap_fork_to_state(whichfork);
2051 struct xfs_mount *mp = ip->i_mount;
2052 struct xfs_bmbt_irec old;
2057 ifp = XFS_IFORK_PTR(ip, whichfork);
2059 ASSERT(!isnullstartblock(new->br_startblock));
2061 XFS_STATS_INC(mp, xs_add_exlist);
2068 * Set up a bunch of variables to make the tests simpler.
2071 xfs_iext_get_extent(ifp, icur, &PREV);
2072 ASSERT(new->br_state != PREV.br_state);
2073 new_endoff = new->br_startoff + new->br_blockcount;
2074 ASSERT(PREV.br_startoff <= new->br_startoff);
2075 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2078 * Set flags determining what part of the previous oldext allocation
2079 * extent is being replaced by a newext allocation.
2081 if (PREV.br_startoff == new->br_startoff)
2082 state |= BMAP_LEFT_FILLING;
2083 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2084 state |= BMAP_RIGHT_FILLING;
2087 * Check and set flags if this segment has a left neighbor.
2088 * Don't set contiguous if the combined extent would be too large.
2090 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2091 state |= BMAP_LEFT_VALID;
2092 if (isnullstartblock(LEFT.br_startblock))
2093 state |= BMAP_LEFT_DELAY;
2096 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2097 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2098 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2099 LEFT.br_state == new->br_state &&
2100 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2101 state |= BMAP_LEFT_CONTIG;
2104 * Check and set flags if this segment has a right neighbor.
2105 * Don't set contiguous if the combined extent would be too large.
2106 * Also check for all-three-contiguous being too large.
2108 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2109 state |= BMAP_RIGHT_VALID;
2110 if (isnullstartblock(RIGHT.br_startblock))
2111 state |= BMAP_RIGHT_DELAY;
2114 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2115 new_endoff == RIGHT.br_startoff &&
2116 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2117 new->br_state == RIGHT.br_state &&
2118 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2119 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2120 BMAP_RIGHT_FILLING)) !=
2121 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2122 BMAP_RIGHT_FILLING) ||
2123 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2125 state |= BMAP_RIGHT_CONTIG;
2128 * Switch out based on the FILLING and CONTIG state bits.
2130 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2131 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2132 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2133 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2135 * Setting all of a previous oldext extent to newext.
2136 * The left and right neighbors are both contiguous with new.
2138 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2140 xfs_iext_remove(ip, icur, state);
2141 xfs_iext_remove(ip, icur, state);
2142 xfs_iext_prev(ifp, icur);
2143 xfs_iext_update_extent(ip, state, icur, &LEFT);
2144 XFS_IFORK_NEXT_SET(ip, whichfork,
2145 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2147 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2149 rval = XFS_ILOG_CORE;
2150 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2153 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2154 if ((error = xfs_btree_delete(cur, &i)))
2156 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2157 if ((error = xfs_btree_decrement(cur, 0, &i)))
2159 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2160 if ((error = xfs_btree_delete(cur, &i)))
2162 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2163 if ((error = xfs_btree_decrement(cur, 0, &i)))
2165 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2166 error = xfs_bmbt_update(cur, &LEFT);
2172 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2174 * Setting all of a previous oldext extent to newext.
2175 * The left neighbor is contiguous, the right is not.
2177 LEFT.br_blockcount += PREV.br_blockcount;
2179 xfs_iext_remove(ip, icur, state);
2180 xfs_iext_prev(ifp, icur);
2181 xfs_iext_update_extent(ip, state, icur, &LEFT);
2182 XFS_IFORK_NEXT_SET(ip, whichfork,
2183 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2185 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2187 rval = XFS_ILOG_CORE;
2188 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2191 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2192 if ((error = xfs_btree_delete(cur, &i)))
2194 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2195 if ((error = xfs_btree_decrement(cur, 0, &i)))
2197 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2198 error = xfs_bmbt_update(cur, &LEFT);
2204 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2206 * Setting all of a previous oldext extent to newext.
2207 * The right neighbor is contiguous, the left is not.
2209 PREV.br_blockcount += RIGHT.br_blockcount;
2210 PREV.br_state = new->br_state;
2212 xfs_iext_next(ifp, icur);
2213 xfs_iext_remove(ip, icur, state);
2214 xfs_iext_prev(ifp, icur);
2215 xfs_iext_update_extent(ip, state, icur, &PREV);
2217 XFS_IFORK_NEXT_SET(ip, whichfork,
2218 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2220 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2222 rval = XFS_ILOG_CORE;
2223 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2226 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2227 if ((error = xfs_btree_delete(cur, &i)))
2229 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2230 if ((error = xfs_btree_decrement(cur, 0, &i)))
2232 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2233 error = xfs_bmbt_update(cur, &PREV);
2239 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2241 * Setting all of a previous oldext extent to newext.
2242 * Neither the left nor right neighbors are contiguous with
2245 PREV.br_state = new->br_state;
2246 xfs_iext_update_extent(ip, state, icur, &PREV);
2249 rval = XFS_ILOG_DEXT;
2252 error = xfs_bmbt_lookup_eq(cur, new, &i);
2255 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2256 error = xfs_bmbt_update(cur, &PREV);
2262 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2264 * Setting the first part of a previous oldext extent to newext.
2265 * The left neighbor is contiguous.
2267 LEFT.br_blockcount += new->br_blockcount;
2270 PREV.br_startoff += new->br_blockcount;
2271 PREV.br_startblock += new->br_blockcount;
2272 PREV.br_blockcount -= new->br_blockcount;
2274 xfs_iext_update_extent(ip, state, icur, &PREV);
2275 xfs_iext_prev(ifp, icur);
2276 xfs_iext_update_extent(ip, state, icur, &LEFT);
2279 rval = XFS_ILOG_DEXT;
2282 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2285 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2286 error = xfs_bmbt_update(cur, &PREV);
2289 error = xfs_btree_decrement(cur, 0, &i);
2292 error = xfs_bmbt_update(cur, &LEFT);
2298 case BMAP_LEFT_FILLING:
2300 * Setting the first part of a previous oldext extent to newext.
2301 * The left neighbor is not contiguous.
2304 PREV.br_startoff += new->br_blockcount;
2305 PREV.br_startblock += new->br_blockcount;
2306 PREV.br_blockcount -= new->br_blockcount;
2308 xfs_iext_update_extent(ip, state, icur, &PREV);
2309 xfs_iext_insert(ip, icur, new, state);
2310 XFS_IFORK_NEXT_SET(ip, whichfork,
2311 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2313 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2315 rval = XFS_ILOG_CORE;
2316 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2319 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2320 error = xfs_bmbt_update(cur, &PREV);
2323 cur->bc_rec.b = *new;
2324 if ((error = xfs_btree_insert(cur, &i)))
2326 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2330 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2332 * Setting the last part of a previous oldext extent to newext.
2333 * The right neighbor is contiguous with the new allocation.
2336 PREV.br_blockcount -= new->br_blockcount;
2338 RIGHT.br_startoff = new->br_startoff;
2339 RIGHT.br_startblock = new->br_startblock;
2340 RIGHT.br_blockcount += new->br_blockcount;
2342 xfs_iext_update_extent(ip, state, icur, &PREV);
2343 xfs_iext_next(ifp, icur);
2344 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2347 rval = XFS_ILOG_DEXT;
2350 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2353 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2354 error = xfs_bmbt_update(cur, &PREV);
2357 error = xfs_btree_increment(cur, 0, &i);
2360 error = xfs_bmbt_update(cur, &RIGHT);
2366 case BMAP_RIGHT_FILLING:
2368 * Setting the last part of a previous oldext extent to newext.
2369 * The right neighbor is not contiguous.
2372 PREV.br_blockcount -= new->br_blockcount;
2374 xfs_iext_update_extent(ip, state, icur, &PREV);
2375 xfs_iext_next(ifp, icur);
2376 xfs_iext_insert(ip, icur, new, state);
2378 XFS_IFORK_NEXT_SET(ip, whichfork,
2379 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2381 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2383 rval = XFS_ILOG_CORE;
2384 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2387 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2388 error = xfs_bmbt_update(cur, &PREV);
2391 error = xfs_bmbt_lookup_eq(cur, new, &i);
2394 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2395 if ((error = xfs_btree_insert(cur, &i)))
2397 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2403 * Setting the middle part of a previous oldext extent to
2404 * newext. Contiguity is impossible here.
2405 * One extent becomes three extents.
2408 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2411 r[1].br_startoff = new_endoff;
2412 r[1].br_blockcount =
2413 old.br_startoff + old.br_blockcount - new_endoff;
2414 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2415 r[1].br_state = PREV.br_state;
2417 xfs_iext_update_extent(ip, state, icur, &PREV);
2418 xfs_iext_next(ifp, icur);
2419 xfs_iext_insert(ip, icur, &r[1], state);
2420 xfs_iext_insert(ip, icur, &r[0], state);
2422 XFS_IFORK_NEXT_SET(ip, whichfork,
2423 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2425 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2427 rval = XFS_ILOG_CORE;
2428 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2431 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2432 /* new right extent - oldext */
2433 error = xfs_bmbt_update(cur, &r[1]);
2436 /* new left extent - oldext */
2437 cur->bc_rec.b = PREV;
2438 if ((error = xfs_btree_insert(cur, &i)))
2440 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2442 * Reset the cursor to the position of the new extent
2443 * we are about to insert as we can't trust it after
2444 * the previous insert.
2446 error = xfs_bmbt_lookup_eq(cur, new, &i);
2449 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2450 /* new middle extent - newext */
2451 if ((error = xfs_btree_insert(cur, &i)))
2453 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2457 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2458 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2459 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2460 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2461 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2462 case BMAP_LEFT_CONTIG:
2463 case BMAP_RIGHT_CONTIG:
2465 * These cases are all impossible.
2470 /* update reverse mappings */
2471 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2473 /* convert to a btree if necessary */
2474 if (xfs_bmap_needs_btree(ip, whichfork)) {
2475 int tmp_logflags; /* partial log flag return val */
2477 ASSERT(cur == NULL);
2478 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2479 &tmp_logflags, whichfork);
2480 *logflagsp |= tmp_logflags;
2485 /* clear out the allocated field, done with it now in any case. */
2487 cur->bc_private.b.allocated = 0;
2491 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2501 * Convert a hole to a delayed allocation.
2504 xfs_bmap_add_extent_hole_delay(
2505 xfs_inode_t *ip, /* incore inode pointer */
2507 struct xfs_iext_cursor *icur,
2508 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2510 struct xfs_ifork *ifp; /* inode fork pointer */
2511 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2512 xfs_filblks_t newlen=0; /* new indirect size */
2513 xfs_filblks_t oldlen=0; /* old indirect size */
2514 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2515 int state = xfs_bmap_fork_to_state(whichfork);
2516 xfs_filblks_t temp; /* temp for indirect calculations */
2518 ifp = XFS_IFORK_PTR(ip, whichfork);
2519 ASSERT(isnullstartblock(new->br_startblock));
2522 * Check and set flags if this segment has a left neighbor
2524 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2525 state |= BMAP_LEFT_VALID;
2526 if (isnullstartblock(left.br_startblock))
2527 state |= BMAP_LEFT_DELAY;
2531 * Check and set flags if the current (right) segment exists.
2532 * If it doesn't exist, we're converting the hole at end-of-file.
2534 if (xfs_iext_get_extent(ifp, icur, &right)) {
2535 state |= BMAP_RIGHT_VALID;
2536 if (isnullstartblock(right.br_startblock))
2537 state |= BMAP_RIGHT_DELAY;
2541 * Set contiguity flags on the left and right neighbors.
2542 * Don't let extents get too large, even if the pieces are contiguous.
2544 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2545 left.br_startoff + left.br_blockcount == new->br_startoff &&
2546 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2547 state |= BMAP_LEFT_CONTIG;
2549 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2550 new->br_startoff + new->br_blockcount == right.br_startoff &&
2551 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2552 (!(state & BMAP_LEFT_CONTIG) ||
2553 (left.br_blockcount + new->br_blockcount +
2554 right.br_blockcount <= MAXEXTLEN)))
2555 state |= BMAP_RIGHT_CONTIG;
2558 * Switch out based on the contiguity flags.
2560 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2561 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2563 * New allocation is contiguous with delayed allocations
2564 * on the left and on the right.
2565 * Merge all three into a single extent record.
2567 temp = left.br_blockcount + new->br_blockcount +
2568 right.br_blockcount;
2570 oldlen = startblockval(left.br_startblock) +
2571 startblockval(new->br_startblock) +
2572 startblockval(right.br_startblock);
2573 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2575 left.br_startblock = nullstartblock(newlen);
2576 left.br_blockcount = temp;
2578 xfs_iext_remove(ip, icur, state);
2579 xfs_iext_prev(ifp, icur);
2580 xfs_iext_update_extent(ip, state, icur, &left);
2583 case BMAP_LEFT_CONTIG:
2585 * New allocation is contiguous with a delayed allocation
2587 * Merge the new allocation with the left neighbor.
2589 temp = left.br_blockcount + new->br_blockcount;
2591 oldlen = startblockval(left.br_startblock) +
2592 startblockval(new->br_startblock);
2593 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2595 left.br_blockcount = temp;
2596 left.br_startblock = nullstartblock(newlen);
2598 xfs_iext_prev(ifp, icur);
2599 xfs_iext_update_extent(ip, state, icur, &left);
2602 case BMAP_RIGHT_CONTIG:
2604 * New allocation is contiguous with a delayed allocation
2606 * Merge the new allocation with the right neighbor.
2608 temp = new->br_blockcount + right.br_blockcount;
2609 oldlen = startblockval(new->br_startblock) +
2610 startblockval(right.br_startblock);
2611 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2613 right.br_startoff = new->br_startoff;
2614 right.br_startblock = nullstartblock(newlen);
2615 right.br_blockcount = temp;
2616 xfs_iext_update_extent(ip, state, icur, &right);
2621 * New allocation is not contiguous with another
2622 * delayed allocation.
2623 * Insert a new entry.
2625 oldlen = newlen = 0;
2626 xfs_iext_insert(ip, icur, new, state);
2629 if (oldlen != newlen) {
2630 ASSERT(oldlen > newlen);
2631 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2634 * Nothing to do for disk quota accounting here.
2636 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2641 * Convert a hole to a real allocation.
2643 STATIC int /* error */
2644 xfs_bmap_add_extent_hole_real(
2645 struct xfs_trans *tp,
2646 struct xfs_inode *ip,
2648 struct xfs_iext_cursor *icur,
2649 struct xfs_btree_cur **curp,
2650 struct xfs_bmbt_irec *new,
2654 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2655 struct xfs_mount *mp = ip->i_mount;
2656 struct xfs_btree_cur *cur = *curp;
2657 int error; /* error return value */
2658 int i; /* temp state */
2659 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2660 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2661 int rval=0; /* return value (logging flags) */
2662 int state = xfs_bmap_fork_to_state(whichfork);
2663 struct xfs_bmbt_irec old;
2665 ASSERT(!isnullstartblock(new->br_startblock));
2666 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2668 XFS_STATS_INC(mp, xs_add_exlist);
2671 * Check and set flags if this segment has a left neighbor.
2673 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2674 state |= BMAP_LEFT_VALID;
2675 if (isnullstartblock(left.br_startblock))
2676 state |= BMAP_LEFT_DELAY;
2680 * Check and set flags if this segment has a current value.
2681 * Not true if we're inserting into the "hole" at eof.
2683 if (xfs_iext_get_extent(ifp, icur, &right)) {
2684 state |= BMAP_RIGHT_VALID;
2685 if (isnullstartblock(right.br_startblock))
2686 state |= BMAP_RIGHT_DELAY;
2690 * We're inserting a real allocation between "left" and "right".
2691 * Set the contiguity flags. Don't let extents get too large.
2693 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2694 left.br_startoff + left.br_blockcount == new->br_startoff &&
2695 left.br_startblock + left.br_blockcount == new->br_startblock &&
2696 left.br_state == new->br_state &&
2697 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2698 state |= BMAP_LEFT_CONTIG;
2700 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2701 new->br_startoff + new->br_blockcount == right.br_startoff &&
2702 new->br_startblock + new->br_blockcount == right.br_startblock &&
2703 new->br_state == right.br_state &&
2704 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2705 (!(state & BMAP_LEFT_CONTIG) ||
2706 left.br_blockcount + new->br_blockcount +
2707 right.br_blockcount <= MAXEXTLEN))
2708 state |= BMAP_RIGHT_CONTIG;
2712 * Select which case we're in here, and implement it.
2714 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2715 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2717 * New allocation is contiguous with real allocations on the
2718 * left and on the right.
2719 * Merge all three into a single extent record.
2721 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2723 xfs_iext_remove(ip, icur, state);
2724 xfs_iext_prev(ifp, icur);
2725 xfs_iext_update_extent(ip, state, icur, &left);
2727 XFS_IFORK_NEXT_SET(ip, whichfork,
2728 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2730 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2732 rval = XFS_ILOG_CORE;
2733 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2736 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2737 error = xfs_btree_delete(cur, &i);
2740 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2741 error = xfs_btree_decrement(cur, 0, &i);
2744 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2745 error = xfs_bmbt_update(cur, &left);
2751 case BMAP_LEFT_CONTIG:
2753 * New allocation is contiguous with a real allocation
2755 * Merge the new allocation with the left neighbor.
2758 left.br_blockcount += new->br_blockcount;
2760 xfs_iext_prev(ifp, icur);
2761 xfs_iext_update_extent(ip, state, icur, &left);
2764 rval = xfs_ilog_fext(whichfork);
2767 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2770 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2771 error = xfs_bmbt_update(cur, &left);
2777 case BMAP_RIGHT_CONTIG:
2779 * New allocation is contiguous with a real allocation
2781 * Merge the new allocation with the right neighbor.
2785 right.br_startoff = new->br_startoff;
2786 right.br_startblock = new->br_startblock;
2787 right.br_blockcount += new->br_blockcount;
2788 xfs_iext_update_extent(ip, state, icur, &right);
2791 rval = xfs_ilog_fext(whichfork);
2794 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2797 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2798 error = xfs_bmbt_update(cur, &right);
2806 * New allocation is not contiguous with another
2808 * Insert a new entry.
2810 xfs_iext_insert(ip, icur, new, state);
2811 XFS_IFORK_NEXT_SET(ip, whichfork,
2812 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2814 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2816 rval = XFS_ILOG_CORE;
2817 error = xfs_bmbt_lookup_eq(cur, new, &i);
2820 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2821 error = xfs_btree_insert(cur, &i);
2824 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2829 /* add reverse mapping unless caller opted out */
2830 if (!(flags & XFS_BMAPI_NORMAP))
2831 xfs_rmap_map_extent(tp, ip, whichfork, new);
2833 /* convert to a btree if necessary */
2834 if (xfs_bmap_needs_btree(ip, whichfork)) {
2835 int tmp_logflags; /* partial log flag return val */
2837 ASSERT(cur == NULL);
2838 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2839 &tmp_logflags, whichfork);
2840 *logflagsp |= tmp_logflags;
2846 /* clear out the allocated field, done with it now in any case. */
2848 cur->bc_private.b.allocated = 0;
2850 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2857 * Functions used in the extent read, allocate and remove paths
2861 * Adjust the size of the new extent based on di_extsize and rt extsize.
2864 xfs_bmap_extsize_align(
2866 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2867 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2868 xfs_extlen_t extsz, /* align to this extent size */
2869 int rt, /* is this a realtime inode? */
2870 int eof, /* is extent at end-of-file? */
2871 int delay, /* creating delalloc extent? */
2872 int convert, /* overwriting unwritten extent? */
2873 xfs_fileoff_t *offp, /* in/out: aligned offset */
2874 xfs_extlen_t *lenp) /* in/out: aligned length */
2876 xfs_fileoff_t orig_off; /* original offset */
2877 xfs_extlen_t orig_alen; /* original length */
2878 xfs_fileoff_t orig_end; /* original off+len */
2879 xfs_fileoff_t nexto; /* next file offset */
2880 xfs_fileoff_t prevo; /* previous file offset */
2881 xfs_fileoff_t align_off; /* temp for offset */
2882 xfs_extlen_t align_alen; /* temp for length */
2883 xfs_extlen_t temp; /* temp for calculations */
2888 orig_off = align_off = *offp;
2889 orig_alen = align_alen = *lenp;
2890 orig_end = orig_off + orig_alen;
2893 * If this request overlaps an existing extent, then don't
2894 * attempt to perform any additional alignment.
2896 if (!delay && !eof &&
2897 (orig_off >= gotp->br_startoff) &&
2898 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2903 * If the file offset is unaligned vs. the extent size
2904 * we need to align it. This will be possible unless
2905 * the file was previously written with a kernel that didn't
2906 * perform this alignment, or if a truncate shot us in the
2909 div_u64_rem(orig_off, extsz, &temp);
2915 /* Same adjustment for the end of the requested area. */
2916 temp = (align_alen % extsz);
2918 align_alen += extsz - temp;
2921 * For large extent hint sizes, the aligned extent might be larger than
2922 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2923 * the length back under MAXEXTLEN. The outer allocation loops handle
2924 * short allocation just fine, so it is safe to do this. We only want to
2925 * do it when we are forced to, though, because it means more allocation
2926 * operations are required.
2928 while (align_alen > MAXEXTLEN)
2929 align_alen -= extsz;
2930 ASSERT(align_alen <= MAXEXTLEN);
2933 * If the previous block overlaps with this proposed allocation
2934 * then move the start forward without adjusting the length.
2936 if (prevp->br_startoff != NULLFILEOFF) {
2937 if (prevp->br_startblock == HOLESTARTBLOCK)
2938 prevo = prevp->br_startoff;
2940 prevo = prevp->br_startoff + prevp->br_blockcount;
2943 if (align_off != orig_off && align_off < prevo)
2946 * If the next block overlaps with this proposed allocation
2947 * then move the start back without adjusting the length,
2948 * but not before offset 0.
2949 * This may of course make the start overlap previous block,
2950 * and if we hit the offset 0 limit then the next block
2951 * can still overlap too.
2953 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2954 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2955 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2956 nexto = gotp->br_startoff + gotp->br_blockcount;
2958 nexto = gotp->br_startoff;
2960 nexto = NULLFILEOFF;
2962 align_off + align_alen != orig_end &&
2963 align_off + align_alen > nexto)
2964 align_off = nexto > align_alen ? nexto - align_alen : 0;
2966 * If we're now overlapping the next or previous extent that
2967 * means we can't fit an extsz piece in this hole. Just move
2968 * the start forward to the first valid spot and set
2969 * the length so we hit the end.
2971 if (align_off != orig_off && align_off < prevo)
2973 if (align_off + align_alen != orig_end &&
2974 align_off + align_alen > nexto &&
2975 nexto != NULLFILEOFF) {
2976 ASSERT(nexto > prevo);
2977 align_alen = nexto - align_off;
2981 * If realtime, and the result isn't a multiple of the realtime
2982 * extent size we need to remove blocks until it is.
2984 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2986 * We're not covering the original request, or
2987 * we won't be able to once we fix the length.
2989 if (orig_off < align_off ||
2990 orig_end > align_off + align_alen ||
2991 align_alen - temp < orig_alen)
2994 * Try to fix it by moving the start up.
2996 if (align_off + temp <= orig_off) {
3001 * Try to fix it by moving the end in.
3003 else if (align_off + align_alen - temp >= orig_end)
3006 * Set the start to the minimum then trim the length.
3009 align_alen -= orig_off - align_off;
3010 align_off = orig_off;
3011 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3014 * Result doesn't cover the request, fail it.
3016 if (orig_off < align_off || orig_end > align_off + align_alen)
3019 ASSERT(orig_off >= align_off);
3020 /* see MAXEXTLEN handling above */
3021 ASSERT(orig_end <= align_off + align_alen ||
3022 align_alen + extsz > MAXEXTLEN);
3026 if (!eof && gotp->br_startoff != NULLFILEOFF)
3027 ASSERT(align_off + align_alen <= gotp->br_startoff);
3028 if (prevp->br_startoff != NULLFILEOFF)
3029 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3037 #define XFS_ALLOC_GAP_UNITS 4
3041 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3043 xfs_fsblock_t adjust; /* adjustment to block numbers */
3044 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3045 xfs_mount_t *mp; /* mount point structure */
3046 int nullfb; /* true if ap->firstblock isn't set */
3047 int rt; /* true if inode is realtime */
3049 #define ISVALID(x,y) \
3051 (x) < mp->m_sb.sb_rblocks : \
3052 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3053 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3054 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3056 mp = ap->ip->i_mount;
3057 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3058 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3059 xfs_alloc_is_userdata(ap->datatype);
3060 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3061 ap->tp->t_firstblock);
3063 * If allocating at eof, and there's a previous real block,
3064 * try to use its last block as our starting point.
3066 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3067 !isnullstartblock(ap->prev.br_startblock) &&
3068 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3069 ap->prev.br_startblock)) {
3070 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3072 * Adjust for the gap between prevp and us.
3074 adjust = ap->offset -
3075 (ap->prev.br_startoff + ap->prev.br_blockcount);
3077 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3078 ap->blkno += adjust;
3081 * If not at eof, then compare the two neighbor blocks.
3082 * Figure out whether either one gives us a good starting point,
3083 * and pick the better one.
3085 else if (!ap->eof) {
3086 xfs_fsblock_t gotbno; /* right side block number */
3087 xfs_fsblock_t gotdiff=0; /* right side difference */
3088 xfs_fsblock_t prevbno; /* left side block number */
3089 xfs_fsblock_t prevdiff=0; /* left side difference */
3092 * If there's a previous (left) block, select a requested
3093 * start block based on it.
3095 if (ap->prev.br_startoff != NULLFILEOFF &&
3096 !isnullstartblock(ap->prev.br_startblock) &&
3097 (prevbno = ap->prev.br_startblock +
3098 ap->prev.br_blockcount) &&
3099 ISVALID(prevbno, ap->prev.br_startblock)) {
3101 * Calculate gap to end of previous block.
3103 adjust = prevdiff = ap->offset -
3104 (ap->prev.br_startoff +
3105 ap->prev.br_blockcount);
3107 * Figure the startblock based on the previous block's
3108 * end and the gap size.
3110 * If the gap is large relative to the piece we're
3111 * allocating, or using it gives us an invalid block
3112 * number, then just use the end of the previous block.
3114 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3115 ISVALID(prevbno + prevdiff,
3116 ap->prev.br_startblock))
3121 * If the firstblock forbids it, can't use it,
3124 if (!rt && !nullfb &&
3125 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3126 prevbno = NULLFSBLOCK;
3129 * No previous block or can't follow it, just default.
3132 prevbno = NULLFSBLOCK;
3134 * If there's a following (right) block, select a requested
3135 * start block based on it.
3137 if (!isnullstartblock(ap->got.br_startblock)) {
3139 * Calculate gap to start of next block.
3141 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3143 * Figure the startblock based on the next block's
3144 * start and the gap size.
3146 gotbno = ap->got.br_startblock;
3149 * If the gap is large relative to the piece we're
3150 * allocating, or using it gives us an invalid block
3151 * number, then just use the start of the next block
3152 * offset by our length.
3154 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3155 ISVALID(gotbno - gotdiff, gotbno))
3157 else if (ISVALID(gotbno - ap->length, gotbno)) {
3158 gotbno -= ap->length;
3159 gotdiff += adjust - ap->length;
3163 * If the firstblock forbids it, can't use it,
3166 if (!rt && !nullfb &&
3167 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3168 gotbno = NULLFSBLOCK;
3171 * No next block, just default.
3174 gotbno = NULLFSBLOCK;
3176 * If both valid, pick the better one, else the only good
3177 * one, else ap->blkno is already set (to 0 or the inode block).
3179 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3180 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3181 else if (prevbno != NULLFSBLOCK)
3182 ap->blkno = prevbno;
3183 else if (gotbno != NULLFSBLOCK)
3190 xfs_bmap_longest_free_extent(
3191 struct xfs_trans *tp,
3196 struct xfs_mount *mp = tp->t_mountp;
3197 struct xfs_perag *pag;
3198 xfs_extlen_t longest;
3201 pag = xfs_perag_get(mp, ag);
3202 if (!pag->pagf_init) {
3203 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3207 if (!pag->pagf_init) {
3213 longest = xfs_alloc_longest_free_extent(pag,
3214 xfs_alloc_min_freelist(mp, pag),
3215 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3216 if (*blen < longest)
3225 xfs_bmap_select_minlen(
3226 struct xfs_bmalloca *ap,
3227 struct xfs_alloc_arg *args,
3231 if (notinit || *blen < ap->minlen) {
3233 * Since we did a BUF_TRYLOCK above, it is possible that
3234 * there is space for this request.
3236 args->minlen = ap->minlen;
3237 } else if (*blen < args->maxlen) {
3239 * If the best seen length is less than the request length,
3240 * use the best as the minimum.
3242 args->minlen = *blen;
3245 * Otherwise we've seen an extent as big as maxlen, use that
3248 args->minlen = args->maxlen;
3253 xfs_bmap_btalloc_nullfb(
3254 struct xfs_bmalloca *ap,
3255 struct xfs_alloc_arg *args,
3258 struct xfs_mount *mp = ap->ip->i_mount;
3259 xfs_agnumber_t ag, startag;
3263 args->type = XFS_ALLOCTYPE_START_BNO;
3264 args->total = ap->total;
3266 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3267 if (startag == NULLAGNUMBER)
3270 while (*blen < args->maxlen) {
3271 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3276 if (++ag == mp->m_sb.sb_agcount)
3282 xfs_bmap_select_minlen(ap, args, blen, notinit);
3287 xfs_bmap_btalloc_filestreams(
3288 struct xfs_bmalloca *ap,
3289 struct xfs_alloc_arg *args,
3292 struct xfs_mount *mp = ap->ip->i_mount;
3297 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3298 args->total = ap->total;
3300 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3301 if (ag == NULLAGNUMBER)
3304 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3308 if (*blen < args->maxlen) {
3309 error = xfs_filestream_new_ag(ap, &ag);
3313 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3320 xfs_bmap_select_minlen(ap, args, blen, notinit);
3323 * Set the failure fallback case to look in the selected AG as stream
3326 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3330 /* Update all inode and quota accounting for the allocation we just did. */
3332 xfs_bmap_btalloc_accounting(
3333 struct xfs_bmalloca *ap,
3334 struct xfs_alloc_arg *args)
3336 if (ap->flags & XFS_BMAPI_COWFORK) {
3338 * COW fork blocks are in-core only and thus are treated as
3339 * in-core quota reservation (like delalloc blocks) even when
3340 * converted to real blocks. The quota reservation is not
3341 * accounted to disk until blocks are remapped to the data
3342 * fork. So if these blocks were previously delalloc, we
3343 * already have quota reservation and there's nothing to do
3347 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3352 * Otherwise, we've allocated blocks in a hole. The transaction
3353 * has acquired in-core quota reservation for this extent.
3354 * Rather than account these as real blocks, however, we reduce
3355 * the transaction quota reservation based on the allocation.
3356 * This essentially transfers the transaction quota reservation
3357 * to that of a delalloc extent.
3359 ap->ip->i_delayed_blks += args->len;
3360 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3365 /* data/attr fork only */
3366 ap->ip->i_d.di_nblocks += args->len;
3367 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3369 ap->ip->i_delayed_blks -= args->len;
3370 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3372 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3373 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3379 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3381 xfs_mount_t *mp; /* mount point structure */
3382 xfs_alloctype_t atype = 0; /* type for allocation routines */
3383 xfs_extlen_t align = 0; /* minimum allocation alignment */
3384 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3386 xfs_alloc_arg_t args;
3387 xfs_fileoff_t orig_offset;
3388 xfs_extlen_t orig_length;
3390 xfs_extlen_t nextminlen = 0;
3391 int nullfb; /* true if ap->firstblock isn't set */
3398 orig_offset = ap->offset;
3399 orig_length = ap->length;
3401 mp = ap->ip->i_mount;
3403 /* stripe alignment for allocation is determined by mount parameters */
3405 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3406 stripe_align = mp->m_swidth;
3407 else if (mp->m_dalign)
3408 stripe_align = mp->m_dalign;
3410 if (ap->flags & XFS_BMAPI_COWFORK)
3411 align = xfs_get_cowextsz_hint(ap->ip);
3412 else if (xfs_alloc_is_userdata(ap->datatype))
3413 align = xfs_get_extsz_hint(ap->ip);
3415 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3416 align, 0, ap->eof, 0, ap->conv,
3417 &ap->offset, &ap->length);
3423 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3424 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3425 ap->tp->t_firstblock);
3427 if (xfs_alloc_is_userdata(ap->datatype) &&
3428 xfs_inode_is_filestream(ap->ip)) {
3429 ag = xfs_filestream_lookup_ag(ap->ip);
3430 ag = (ag != NULLAGNUMBER) ? ag : 0;
3431 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3433 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3436 ap->blkno = ap->tp->t_firstblock;
3438 xfs_bmap_adjacent(ap);
3441 * If allowed, use ap->blkno; otherwise must use firstblock since
3442 * it's in the right allocation group.
3444 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3447 ap->blkno = ap->tp->t_firstblock;
3449 * Normal allocation, done through xfs_alloc_vextent.
3451 tryagain = isaligned = 0;
3452 memset(&args, 0, sizeof(args));
3455 args.fsbno = ap->blkno;
3456 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3458 /* Trim the allocation back to the maximum an AG can fit. */
3459 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3463 * Search for an allocation group with a single extent large
3464 * enough for the request. If one isn't found, then adjust
3465 * the minimum allocation size to the largest space found.
3467 if (xfs_alloc_is_userdata(ap->datatype) &&
3468 xfs_inode_is_filestream(ap->ip))
3469 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3471 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3474 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3475 if (xfs_inode_is_filestream(ap->ip))
3476 args.type = XFS_ALLOCTYPE_FIRST_AG;
3478 args.type = XFS_ALLOCTYPE_START_BNO;
3479 args.total = args.minlen = ap->minlen;
3481 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3482 args.total = ap->total;
3483 args.minlen = ap->minlen;
3485 /* apply extent size hints if obtained earlier */
3488 div_u64_rem(ap->offset, args.prod, &args.mod);
3490 args.mod = args.prod - args.mod;
3491 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3495 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3496 div_u64_rem(ap->offset, args.prod, &args.mod);
3498 args.mod = args.prod - args.mod;
3501 * If we are not low on available data blocks, and the
3502 * underlying logical volume manager is a stripe, and
3503 * the file offset is zero then try to allocate data
3504 * blocks on stripe unit boundary.
3505 * NOTE: ap->aeof is only set if the allocation length
3506 * is >= the stripe unit and the allocation offset is
3507 * at the end of file.
3509 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3511 args.alignment = stripe_align;
3515 * Adjust for alignment
3517 if (blen > args.alignment && blen <= args.maxlen)
3518 args.minlen = blen - args.alignment;
3519 args.minalignslop = 0;
3522 * First try an exact bno allocation.
3523 * If it fails then do a near or start bno
3524 * allocation with alignment turned on.
3528 args.type = XFS_ALLOCTYPE_THIS_BNO;
3531 * Compute the minlen+alignment for the
3532 * next case. Set slop so that the value
3533 * of minlen+alignment+slop doesn't go up
3534 * between the calls.
3536 if (blen > stripe_align && blen <= args.maxlen)
3537 nextminlen = blen - stripe_align;
3539 nextminlen = args.minlen;
3540 if (nextminlen + stripe_align > args.minlen + 1)
3542 nextminlen + stripe_align -
3545 args.minalignslop = 0;
3549 args.minalignslop = 0;
3551 args.minleft = ap->minleft;
3552 args.wasdel = ap->wasdel;
3553 args.resv = XFS_AG_RESV_NONE;
3554 args.datatype = ap->datatype;
3555 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3558 error = xfs_alloc_vextent(&args);
3562 if (tryagain && args.fsbno == NULLFSBLOCK) {
3564 * Exact allocation failed. Now try with alignment
3568 args.fsbno = ap->blkno;
3569 args.alignment = stripe_align;
3570 args.minlen = nextminlen;
3571 args.minalignslop = 0;
3573 if ((error = xfs_alloc_vextent(&args)))
3576 if (isaligned && args.fsbno == NULLFSBLOCK) {
3578 * allocation failed, so turn off alignment and
3582 args.fsbno = ap->blkno;
3584 if ((error = xfs_alloc_vextent(&args)))
3587 if (args.fsbno == NULLFSBLOCK && nullfb &&
3588 args.minlen > ap->minlen) {
3589 args.minlen = ap->minlen;
3590 args.type = XFS_ALLOCTYPE_START_BNO;
3591 args.fsbno = ap->blkno;
3592 if ((error = xfs_alloc_vextent(&args)))
3595 if (args.fsbno == NULLFSBLOCK && nullfb) {
3597 args.type = XFS_ALLOCTYPE_FIRST_AG;
3598 args.total = ap->minlen;
3599 if ((error = xfs_alloc_vextent(&args)))
3601 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3603 if (args.fsbno != NULLFSBLOCK) {
3605 * check the allocation happened at the same or higher AG than
3606 * the first block that was allocated.
3608 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3609 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
3610 XFS_FSB_TO_AGNO(mp, args.fsbno));
3612 ap->blkno = args.fsbno;
3613 if (ap->tp->t_firstblock == NULLFSBLOCK)
3614 ap->tp->t_firstblock = args.fsbno;
3615 ASSERT(nullfb || fb_agno <= args.agno);
3616 ap->length = args.len;
3618 * If the extent size hint is active, we tried to round the
3619 * caller's allocation request offset down to extsz and the
3620 * length up to another extsz boundary. If we found a free
3621 * extent we mapped it in starting at this new offset. If the
3622 * newly mapped space isn't long enough to cover any of the
3623 * range of offsets that was originally requested, move the
3624 * mapping up so that we can fill as much of the caller's
3625 * original request as possible. Free space is apparently
3626 * very fragmented so we're unlikely to be able to satisfy the
3629 if (ap->length <= orig_length)
3630 ap->offset = orig_offset;
3631 else if (ap->offset + ap->length < orig_offset + orig_length)
3632 ap->offset = orig_offset + orig_length - ap->length;
3633 xfs_bmap_btalloc_accounting(ap, &args);
3635 ap->blkno = NULLFSBLOCK;
3642 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3643 * It figures out where to ask the underlying allocator to put the new extent.
3647 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3649 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3650 xfs_alloc_is_userdata(ap->datatype))
3651 return xfs_bmap_rtalloc(ap);
3652 return xfs_bmap_btalloc(ap);
3655 /* Trim extent to fit a logical block range. */
3658 struct xfs_bmbt_irec *irec,
3662 xfs_fileoff_t distance;
3663 xfs_fileoff_t end = bno + len;
3665 if (irec->br_startoff + irec->br_blockcount <= bno ||
3666 irec->br_startoff >= end) {
3667 irec->br_blockcount = 0;
3671 if (irec->br_startoff < bno) {
3672 distance = bno - irec->br_startoff;
3673 if (isnullstartblock(irec->br_startblock))
3674 irec->br_startblock = DELAYSTARTBLOCK;
3675 if (irec->br_startblock != DELAYSTARTBLOCK &&
3676 irec->br_startblock != HOLESTARTBLOCK)
3677 irec->br_startblock += distance;
3678 irec->br_startoff += distance;
3679 irec->br_blockcount -= distance;
3682 if (end < irec->br_startoff + irec->br_blockcount) {
3683 distance = irec->br_startoff + irec->br_blockcount - end;
3684 irec->br_blockcount -= distance;
3689 * Trim the returned map to the required bounds
3693 struct xfs_bmbt_irec *mval,
3694 struct xfs_bmbt_irec *got,
3702 if ((flags & XFS_BMAPI_ENTIRE) ||
3703 got->br_startoff + got->br_blockcount <= obno) {
3705 if (isnullstartblock(got->br_startblock))
3706 mval->br_startblock = DELAYSTARTBLOCK;
3712 ASSERT((*bno >= obno) || (n == 0));
3714 mval->br_startoff = *bno;
3715 if (isnullstartblock(got->br_startblock))
3716 mval->br_startblock = DELAYSTARTBLOCK;
3718 mval->br_startblock = got->br_startblock +
3719 (*bno - got->br_startoff);
3721 * Return the minimum of what we got and what we asked for for
3722 * the length. We can use the len variable here because it is
3723 * modified below and we could have been there before coming
3724 * here if the first part of the allocation didn't overlap what
3727 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3728 got->br_blockcount - (*bno - got->br_startoff));
3729 mval->br_state = got->br_state;
3730 ASSERT(mval->br_blockcount <= len);
3735 * Update and validate the extent map to return
3738 xfs_bmapi_update_map(
3739 struct xfs_bmbt_irec **map,
3747 xfs_bmbt_irec_t *mval = *map;
3749 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3750 ((mval->br_startoff + mval->br_blockcount) <= end));
3751 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3752 (mval->br_startoff < obno));
3754 *bno = mval->br_startoff + mval->br_blockcount;
3756 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3757 /* update previous map with new information */
3758 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3759 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3760 ASSERT(mval->br_state == mval[-1].br_state);
3761 mval[-1].br_blockcount = mval->br_blockcount;
3762 mval[-1].br_state = mval->br_state;
3763 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3764 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3765 mval[-1].br_startblock != HOLESTARTBLOCK &&
3766 mval->br_startblock == mval[-1].br_startblock +
3767 mval[-1].br_blockcount &&
3768 mval[-1].br_state == mval->br_state) {
3769 ASSERT(mval->br_startoff ==
3770 mval[-1].br_startoff + mval[-1].br_blockcount);
3771 mval[-1].br_blockcount += mval->br_blockcount;
3772 } else if (*n > 0 &&
3773 mval->br_startblock == DELAYSTARTBLOCK &&
3774 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3775 mval->br_startoff ==
3776 mval[-1].br_startoff + mval[-1].br_blockcount) {
3777 mval[-1].br_blockcount += mval->br_blockcount;
3778 mval[-1].br_state = mval->br_state;
3779 } else if (!((*n == 0) &&
3780 ((mval->br_startoff + mval->br_blockcount) <=
3789 * Map file blocks to filesystem blocks without allocation.
3793 struct xfs_inode *ip,
3796 struct xfs_bmbt_irec *mval,
3800 struct xfs_mount *mp = ip->i_mount;
3801 struct xfs_ifork *ifp;
3802 struct xfs_bmbt_irec got;
3805 struct xfs_iext_cursor icur;
3809 int whichfork = xfs_bmapi_whichfork(flags);
3812 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3813 XFS_BMAPI_COWFORK)));
3814 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3816 if (unlikely(XFS_TEST_ERROR(
3817 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3818 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3819 mp, XFS_ERRTAG_BMAPIFORMAT))) {
3820 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
3821 return -EFSCORRUPTED;
3824 if (XFS_FORCED_SHUTDOWN(mp))
3827 XFS_STATS_INC(mp, xs_blk_mapr);
3829 ifp = XFS_IFORK_PTR(ip, whichfork);
3831 /* No CoW fork? Return a hole. */
3832 if (whichfork == XFS_COW_FORK) {
3833 mval->br_startoff = bno;
3834 mval->br_startblock = HOLESTARTBLOCK;
3835 mval->br_blockcount = len;
3836 mval->br_state = XFS_EXT_NORM;
3842 * A missing attr ifork implies that the inode says we're in
3843 * extents or btree format but failed to pass the inode fork
3844 * verifier while trying to load it. Treat that as a file
3848 xfs_alert(mp, "%s: inode %llu missing fork %d",
3849 __func__, ip->i_ino, whichfork);
3851 return -EFSCORRUPTED;
3854 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3855 error = xfs_iread_extents(NULL, ip, whichfork);
3860 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3865 while (bno < end && n < *nmap) {
3866 /* Reading past eof, act as though there's a hole up to end. */
3868 got.br_startoff = end;
3869 if (got.br_startoff > bno) {
3870 /* Reading in a hole. */
3871 mval->br_startoff = bno;
3872 mval->br_startblock = HOLESTARTBLOCK;
3873 mval->br_blockcount =
3874 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3875 mval->br_state = XFS_EXT_NORM;
3876 bno += mval->br_blockcount;
3877 len -= mval->br_blockcount;
3883 /* set up the extent map to return. */
3884 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3885 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3887 /* If we're done, stop now. */
3888 if (bno >= end || n >= *nmap)
3891 /* Else go on to the next record. */
3892 if (!xfs_iext_next_extent(ifp, &icur, &got))
3900 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3901 * global pool and the extent inserted into the inode in-core extent tree.
3903 * On entry, got refers to the first extent beyond the offset of the extent to
3904 * allocate or eof is specified if no such extent exists. On return, got refers
3905 * to the extent record that was inserted to the inode fork.
3907 * Note that the allocated extent may have been merged with contiguous extents
3908 * during insertion into the inode fork. Thus, got does not reflect the current
3909 * state of the inode fork on return. If necessary, the caller can use lastx to
3910 * look up the updated record in the inode fork.
3913 xfs_bmapi_reserve_delalloc(
3914 struct xfs_inode *ip,
3918 xfs_filblks_t prealloc,
3919 struct xfs_bmbt_irec *got,
3920 struct xfs_iext_cursor *icur,
3923 struct xfs_mount *mp = ip->i_mount;
3924 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3926 xfs_extlen_t indlen;
3928 xfs_fileoff_t aoff = off;
3931 * Cap the alloc length. Keep track of prealloc so we know whether to
3932 * tag the inode before we return.
3934 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
3936 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3937 if (prealloc && alen >= len)
3938 prealloc = alen - len;
3940 /* Figure out the extent size, adjust alen */
3941 if (whichfork == XFS_COW_FORK) {
3942 struct xfs_bmbt_irec prev;
3943 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3945 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3946 prev.br_startoff = NULLFILEOFF;
3948 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3949 1, 0, &aoff, &alen);
3954 * Make a transaction-less quota reservation for delayed allocation
3955 * blocks. This number gets adjusted later. We return if we haven't
3956 * allocated blocks already inside this loop.
3958 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3959 XFS_QMOPT_RES_REGBLKS);
3964 * Split changing sb for alen and indlen since they could be coming
3965 * from different places.
3967 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3970 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
3972 goto out_unreserve_quota;
3974 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
3976 goto out_unreserve_blocks;
3979 ip->i_delayed_blks += alen;
3980 xfs_mod_delalloc(ip->i_mount, alen + indlen);
3982 got->br_startoff = aoff;
3983 got->br_startblock = nullstartblock(indlen);
3984 got->br_blockcount = alen;
3985 got->br_state = XFS_EXT_NORM;
3987 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
3990 * Tag the inode if blocks were preallocated. Note that COW fork
3991 * preallocation can occur at the start or end of the extent, even when
3992 * prealloc == 0, so we must also check the aligned offset and length.
3994 if (whichfork == XFS_DATA_FORK && prealloc)
3995 xfs_inode_set_eofblocks_tag(ip);
3996 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3997 xfs_inode_set_cowblocks_tag(ip);
4001 out_unreserve_blocks:
4002 xfs_mod_fdblocks(mp, alen, false);
4003 out_unreserve_quota:
4004 if (XFS_IS_QUOTA_ON(mp))
4005 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4006 XFS_QMOPT_RES_REGBLKS);
4012 struct xfs_bmalloca *bma)
4014 struct xfs_mount *mp = bma->ip->i_mount;
4015 int whichfork = xfs_bmapi_whichfork(bma->flags);
4016 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4017 int tmp_logflags = 0;
4020 ASSERT(bma->length > 0);
4023 * For the wasdelay case, we could also just allocate the stuff asked
4024 * for in this bmap call but that wouldn't be as good.
4027 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4028 bma->offset = bma->got.br_startoff;
4029 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
4031 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4033 bma->length = XFS_FILBLKS_MIN(bma->length,
4034 bma->got.br_startoff - bma->offset);
4038 * Set the data type being allocated. For the data fork, the first data
4039 * in the file is treated differently to all other allocations. For the
4040 * attribute fork, we only need to ensure the allocated range is not on
4043 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4044 bma->datatype = XFS_ALLOC_NOBUSY;
4045 if (whichfork == XFS_DATA_FORK && bma->offset == 0)
4046 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4047 if (bma->flags & XFS_BMAPI_ZERO)
4048 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
4051 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4054 * Only want to do the alignment at the eof if it is userdata and
4055 * allocation length is larger than a stripe unit.
4057 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4058 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4059 error = xfs_bmap_isaeof(bma, whichfork);
4064 error = xfs_bmap_alloc(bma);
4068 if (bma->blkno == NULLFSBLOCK)
4070 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
4071 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4073 * Bump the number of extents we've allocated
4079 bma->cur->bc_private.b.flags =
4080 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4082 bma->got.br_startoff = bma->offset;
4083 bma->got.br_startblock = bma->blkno;
4084 bma->got.br_blockcount = bma->length;
4085 bma->got.br_state = XFS_EXT_NORM;
4088 * In the data fork, a wasdelay extent has been initialized, so
4089 * shouldn't be flagged as unwritten.
4091 * For the cow fork, however, we convert delalloc reservations
4092 * (extents allocated for speculative preallocation) to
4093 * allocated unwritten extents, and only convert the unwritten
4094 * extents to real extents when we're about to write the data.
4096 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4097 (bma->flags & XFS_BMAPI_PREALLOC))
4098 bma->got.br_state = XFS_EXT_UNWRITTEN;
4101 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4103 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4104 whichfork, &bma->icur, &bma->cur, &bma->got,
4105 &bma->logflags, bma->flags);
4107 bma->logflags |= tmp_logflags;
4112 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4113 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4114 * the neighbouring ones.
4116 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4118 ASSERT(bma->got.br_startoff <= bma->offset);
4119 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4120 bma->offset + bma->length);
4121 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4122 bma->got.br_state == XFS_EXT_UNWRITTEN);
4127 xfs_bmapi_convert_unwritten(
4128 struct xfs_bmalloca *bma,
4129 struct xfs_bmbt_irec *mval,
4133 int whichfork = xfs_bmapi_whichfork(flags);
4134 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4135 int tmp_logflags = 0;
4138 /* check if we need to do unwritten->real conversion */
4139 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4140 (flags & XFS_BMAPI_PREALLOC))
4143 /* check if we need to do real->unwritten conversion */
4144 if (mval->br_state == XFS_EXT_NORM &&
4145 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4146 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4150 * Modify (by adding) the state flag, if writing.
4152 ASSERT(mval->br_blockcount <= len);
4153 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4154 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4155 bma->ip, whichfork);
4157 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4158 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4161 * Before insertion into the bmbt, zero the range being converted
4164 if (flags & XFS_BMAPI_ZERO) {
4165 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4166 mval->br_blockcount);
4171 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4172 &bma->icur, &bma->cur, mval, &tmp_logflags);
4174 * Log the inode core unconditionally in the unwritten extent conversion
4175 * path because the conversion might not have done so (e.g., if the
4176 * extent count hasn't changed). We need to make sure the inode is dirty
4177 * in the transaction for the sake of fsync(), even if nothing has
4178 * changed, because fsync() will not force the log for this transaction
4179 * unless it sees the inode pinned.
4181 * Note: If we're only converting cow fork extents, there aren't
4182 * any on-disk updates to make, so we don't need to log anything.
4184 if (whichfork != XFS_COW_FORK)
4185 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4190 * Update our extent pointer, given that
4191 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4192 * of the neighbouring ones.
4194 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4197 * We may have combined previously unwritten space with written space,
4198 * so generate another request.
4200 if (mval->br_blockcount < len)
4205 static inline xfs_extlen_t
4207 struct xfs_trans *tp,
4208 struct xfs_inode *ip,
4211 if (tp && tp->t_firstblock != NULLFSBLOCK)
4213 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4215 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4219 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4220 * a case where the data is changed, there's an error, and it's not logged so we
4221 * don't shutdown when we should. Don't bother logging extents/btree changes if
4222 * we converted to the other format.
4226 struct xfs_bmalloca *bma,
4230 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4231 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4232 bma->logflags &= ~xfs_ilog_fext(whichfork);
4233 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4234 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4235 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4238 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4240 xfs_btree_del_cursor(bma->cur, error);
4244 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4245 * extent state if necessary. Details behaviour is controlled by the flags
4246 * parameter. Only allocates blocks from a single allocation group, to avoid
4251 struct xfs_trans *tp, /* transaction pointer */
4252 struct xfs_inode *ip, /* incore inode */
4253 xfs_fileoff_t bno, /* starting file offs. mapped */
4254 xfs_filblks_t len, /* length to map in file */
4255 int flags, /* XFS_BMAPI_... */
4256 xfs_extlen_t total, /* total blocks needed */
4257 struct xfs_bmbt_irec *mval, /* output: map values */
4258 int *nmap) /* i/o: mval size/count */
4260 struct xfs_bmalloca bma = {
4265 struct xfs_mount *mp = ip->i_mount;
4266 struct xfs_ifork *ifp;
4267 xfs_fileoff_t end; /* end of mapped file region */
4268 bool eof = false; /* after the end of extents */
4269 int error; /* error return */
4270 int n; /* current extent index */
4271 xfs_fileoff_t obno; /* old block number (offset) */
4272 int whichfork; /* data or attr fork */
4275 xfs_fileoff_t orig_bno; /* original block number value */
4276 int orig_flags; /* original flags arg value */
4277 xfs_filblks_t orig_len; /* original value of len arg */
4278 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4279 int orig_nmap; /* original value of *nmap */
4287 whichfork = xfs_bmapi_whichfork(flags);
4290 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4293 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4294 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4295 ASSERT(!(flags & XFS_BMAPI_REMAP));
4297 /* zeroing is for currently only for data extents, not metadata */
4298 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4299 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4301 * we can allocate unwritten extents or pre-zero allocated blocks,
4302 * but it makes no sense to do both at once. This would result in
4303 * zeroing the unwritten extent twice, but it still being an
4304 * unwritten extent....
4306 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4307 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4309 if (unlikely(XFS_TEST_ERROR(
4310 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4311 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4312 mp, XFS_ERRTAG_BMAPIFORMAT))) {
4313 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4314 return -EFSCORRUPTED;
4317 if (XFS_FORCED_SHUTDOWN(mp))
4320 ifp = XFS_IFORK_PTR(ip, whichfork);
4322 XFS_STATS_INC(mp, xs_blk_mapw);
4324 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4325 error = xfs_iread_extents(tp, ip, whichfork);
4330 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4332 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4333 bma.prev.br_startoff = NULLFILEOFF;
4334 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4339 while (bno < end && n < *nmap) {
4340 bool need_alloc = false, wasdelay = false;
4342 /* in hole or beyond EOF? */
4343 if (eof || bma.got.br_startoff > bno) {
4345 * CoW fork conversions should /never/ hit EOF or
4346 * holes. There should always be something for us
4349 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4350 (flags & XFS_BMAPI_COWFORK)));
4353 } else if (isnullstartblock(bma.got.br_startblock)) {
4358 * First, deal with the hole before the allocated space
4359 * that we found, if any.
4361 if (need_alloc || wasdelay) {
4363 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4364 bma.wasdel = wasdelay;
4369 * There's a 32/64 bit type mismatch between the
4370 * allocation length request (which can be 64 bits in
4371 * length) and the bma length request, which is
4372 * xfs_extlen_t and therefore 32 bits. Hence we have to
4373 * check for 32-bit overflows and handle them here.
4375 if (len > (xfs_filblks_t)MAXEXTLEN)
4376 bma.length = MAXEXTLEN;
4381 ASSERT(bma.length > 0);
4382 error = xfs_bmapi_allocate(&bma);
4385 if (bma.blkno == NULLFSBLOCK)
4389 * If this is a CoW allocation, record the data in
4390 * the refcount btree for orphan recovery.
4392 if (whichfork == XFS_COW_FORK)
4393 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4397 /* Deal with the allocated space we found. */
4398 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4401 /* Execute unwritten extent conversion if necessary */
4402 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4403 if (error == -EAGAIN)
4408 /* update the extent map to return */
4409 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4412 * If we're done, stop now. Stop when we've allocated
4413 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4414 * the transaction may get too big.
4416 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4419 /* Else go on to the next record. */
4421 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4426 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4431 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4432 XFS_IFORK_NEXTENTS(ip, whichfork) >
4433 XFS_IFORK_MAXEXT(ip, whichfork));
4434 xfs_bmapi_finish(&bma, whichfork, 0);
4435 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4439 xfs_bmapi_finish(&bma, whichfork, error);
4444 * Convert an existing delalloc extent to real blocks based on file offset. This
4445 * attempts to allocate the entire delalloc extent and may require multiple
4446 * invocations to allocate the target offset if a large enough physical extent
4450 xfs_bmapi_convert_delalloc(
4451 struct xfs_inode *ip,
4453 xfs_fileoff_t offset_fsb,
4454 struct xfs_bmbt_irec *imap,
4457 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4458 struct xfs_mount *mp = ip->i_mount;
4459 struct xfs_bmalloca bma = { NULL };
4460 struct xfs_trans *tp;
4464 * Space for the extent and indirect blocks was reserved when the
4465 * delalloc extent was created so there's no need to do so here.
4467 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4468 XFS_TRANS_RESERVE, &tp);
4472 xfs_ilock(ip, XFS_ILOCK_EXCL);
4473 xfs_trans_ijoin(tp, ip, 0);
4475 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4476 bma.got.br_startoff > offset_fsb) {
4478 * No extent found in the range we are trying to convert. This
4479 * should only happen for the COW fork, where another thread
4480 * might have moved the extent to the data fork in the meantime.
4482 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4484 goto out_trans_cancel;
4488 * If we find a real extent here we raced with another thread converting
4489 * the extent. Just return the real extent at this offset.
4491 if (!isnullstartblock(bma.got.br_startblock)) {
4493 *seq = READ_ONCE(ifp->if_seq);
4494 goto out_trans_cancel;
4500 bma.offset = bma.got.br_startoff;
4501 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4502 bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
4503 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4504 if (whichfork == XFS_COW_FORK)
4505 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4507 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4508 bma.prev.br_startoff = NULLFILEOFF;
4510 error = xfs_bmapi_allocate(&bma);
4515 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4517 error = -EFSCORRUPTED;
4518 if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip)))
4521 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4522 XFS_STATS_INC(mp, xs_xstrat_quick);
4524 ASSERT(!isnullstartblock(bma.got.br_startblock));
4526 *seq = READ_ONCE(ifp->if_seq);
4528 if (whichfork == XFS_COW_FORK)
4529 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4531 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4536 xfs_bmapi_finish(&bma, whichfork, 0);
4537 error = xfs_trans_commit(tp);
4538 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4542 xfs_bmapi_finish(&bma, whichfork, error);
4544 xfs_trans_cancel(tp);
4545 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4551 struct xfs_trans *tp,
4552 struct xfs_inode *ip,
4555 xfs_fsblock_t startblock,
4558 struct xfs_mount *mp = ip->i_mount;
4559 struct xfs_ifork *ifp;
4560 struct xfs_btree_cur *cur = NULL;
4561 struct xfs_bmbt_irec got;
4562 struct xfs_iext_cursor icur;
4563 int whichfork = xfs_bmapi_whichfork(flags);
4564 int logflags = 0, error;
4566 ifp = XFS_IFORK_PTR(ip, whichfork);
4568 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4569 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4570 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4571 XFS_BMAPI_NORMAP)));
4572 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4573 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4575 if (unlikely(XFS_TEST_ERROR(
4576 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4577 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4578 mp, XFS_ERRTAG_BMAPIFORMAT))) {
4579 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4580 return -EFSCORRUPTED;
4583 if (XFS_FORCED_SHUTDOWN(mp))
4586 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4587 error = xfs_iread_extents(tp, ip, whichfork);
4592 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4593 /* make sure we only reflink into a hole. */
4594 ASSERT(got.br_startoff > bno);
4595 ASSERT(got.br_startoff - bno >= len);
4598 ip->i_d.di_nblocks += len;
4599 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4601 if (ifp->if_flags & XFS_IFBROOT) {
4602 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4603 cur->bc_private.b.flags = 0;
4606 got.br_startoff = bno;
4607 got.br_startblock = startblock;
4608 got.br_blockcount = len;
4609 if (flags & XFS_BMAPI_PREALLOC)
4610 got.br_state = XFS_EXT_UNWRITTEN;
4612 got.br_state = XFS_EXT_NORM;
4614 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4615 &cur, &got, &logflags, flags);
4619 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4622 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4623 logflags &= ~XFS_ILOG_DEXT;
4624 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4625 logflags &= ~XFS_ILOG_DBROOT;
4628 xfs_trans_log_inode(tp, ip, logflags);
4630 xfs_btree_del_cursor(cur, error);
4635 * When a delalloc extent is split (e.g., due to a hole punch), the original
4636 * indlen reservation must be shared across the two new extents that are left
4639 * Given the original reservation and the worst case indlen for the two new
4640 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4641 * reservation fairly across the two new extents. If necessary, steal available
4642 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4643 * ores == 1). The number of stolen blocks is returned. The availability and
4644 * subsequent accounting of stolen blocks is the responsibility of the caller.
4646 static xfs_filblks_t
4647 xfs_bmap_split_indlen(
4648 xfs_filblks_t ores, /* original res. */
4649 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4650 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4651 xfs_filblks_t avail) /* stealable blocks */
4653 xfs_filblks_t len1 = *indlen1;
4654 xfs_filblks_t len2 = *indlen2;
4655 xfs_filblks_t nres = len1 + len2; /* new total res. */
4656 xfs_filblks_t stolen = 0;
4657 xfs_filblks_t resfactor;
4660 * Steal as many blocks as we can to try and satisfy the worst case
4661 * indlen for both new extents.
4663 if (ores < nres && avail)
4664 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4667 /* nothing else to do if we've satisfied the new reservation */
4672 * We can't meet the total required reservation for the two extents.
4673 * Calculate the percent of the overall shortage between both extents
4674 * and apply this percentage to each of the requested indlen values.
4675 * This distributes the shortage fairly and reduces the chances that one
4676 * of the two extents is left with nothing when extents are repeatedly
4679 resfactor = (ores * 100);
4680 do_div(resfactor, nres);
4685 ASSERT(len1 + len2 <= ores);
4686 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4689 * Hand out the remainder to each extent. If one of the two reservations
4690 * is zero, we want to make sure that one gets a block first. The loop
4691 * below starts with len1, so hand len2 a block right off the bat if it
4694 ores -= (len1 + len2);
4695 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4696 if (ores && !len2 && *indlen2) {
4701 if (len1 < *indlen1) {
4707 if (len2 < *indlen2) {
4720 xfs_bmap_del_extent_delay(
4721 struct xfs_inode *ip,
4723 struct xfs_iext_cursor *icur,
4724 struct xfs_bmbt_irec *got,
4725 struct xfs_bmbt_irec *del)
4727 struct xfs_mount *mp = ip->i_mount;
4728 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4729 struct xfs_bmbt_irec new;
4730 int64_t da_old, da_new, da_diff = 0;
4731 xfs_fileoff_t del_endoff, got_endoff;
4732 xfs_filblks_t got_indlen, new_indlen, stolen;
4733 int state = xfs_bmap_fork_to_state(whichfork);
4737 XFS_STATS_INC(mp, xs_del_exlist);
4739 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4740 del_endoff = del->br_startoff + del->br_blockcount;
4741 got_endoff = got->br_startoff + got->br_blockcount;
4742 da_old = startblockval(got->br_startblock);
4745 ASSERT(del->br_blockcount > 0);
4746 ASSERT(got->br_startoff <= del->br_startoff);
4747 ASSERT(got_endoff >= del_endoff);
4750 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4752 do_div(rtexts, mp->m_sb.sb_rextsize);
4753 xfs_mod_frextents(mp, rtexts);
4757 * Update the inode delalloc counter now and wait to update the
4758 * sb counters as we might have to borrow some blocks for the
4759 * indirect block accounting.
4761 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4762 -((long)del->br_blockcount), 0,
4763 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4766 ip->i_delayed_blks -= del->br_blockcount;
4768 if (got->br_startoff == del->br_startoff)
4769 state |= BMAP_LEFT_FILLING;
4770 if (got_endoff == del_endoff)
4771 state |= BMAP_RIGHT_FILLING;
4773 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4774 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4776 * Matches the whole extent. Delete the entry.
4778 xfs_iext_remove(ip, icur, state);
4779 xfs_iext_prev(ifp, icur);
4781 case BMAP_LEFT_FILLING:
4783 * Deleting the first part of the extent.
4785 got->br_startoff = del_endoff;
4786 got->br_blockcount -= del->br_blockcount;
4787 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4788 got->br_blockcount), da_old);
4789 got->br_startblock = nullstartblock((int)da_new);
4790 xfs_iext_update_extent(ip, state, icur, got);
4792 case BMAP_RIGHT_FILLING:
4794 * Deleting the last part of the extent.
4796 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4797 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4798 got->br_blockcount), da_old);
4799 got->br_startblock = nullstartblock((int)da_new);
4800 xfs_iext_update_extent(ip, state, icur, got);
4804 * Deleting the middle of the extent.
4806 * Distribute the original indlen reservation across the two new
4807 * extents. Steal blocks from the deleted extent if necessary.
4808 * Stealing blocks simply fudges the fdblocks accounting below.
4809 * Warn if either of the new indlen reservations is zero as this
4810 * can lead to delalloc problems.
4812 got->br_blockcount = del->br_startoff - got->br_startoff;
4813 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4815 new.br_blockcount = got_endoff - del_endoff;
4816 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4818 WARN_ON_ONCE(!got_indlen || !new_indlen);
4819 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4820 del->br_blockcount);
4822 got->br_startblock = nullstartblock((int)got_indlen);
4824 new.br_startoff = del_endoff;
4825 new.br_state = got->br_state;
4826 new.br_startblock = nullstartblock((int)new_indlen);
4828 xfs_iext_update_extent(ip, state, icur, got);
4829 xfs_iext_next(ifp, icur);
4830 xfs_iext_insert(ip, icur, &new, state);
4832 da_new = got_indlen + new_indlen - stolen;
4833 del->br_blockcount -= stolen;
4837 ASSERT(da_old >= da_new);
4838 da_diff = da_old - da_new;
4840 da_diff += del->br_blockcount;
4842 xfs_mod_fdblocks(mp, da_diff, false);
4843 xfs_mod_delalloc(mp, -da_diff);
4849 xfs_bmap_del_extent_cow(
4850 struct xfs_inode *ip,
4851 struct xfs_iext_cursor *icur,
4852 struct xfs_bmbt_irec *got,
4853 struct xfs_bmbt_irec *del)
4855 struct xfs_mount *mp = ip->i_mount;
4856 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4857 struct xfs_bmbt_irec new;
4858 xfs_fileoff_t del_endoff, got_endoff;
4859 int state = BMAP_COWFORK;
4861 XFS_STATS_INC(mp, xs_del_exlist);
4863 del_endoff = del->br_startoff + del->br_blockcount;
4864 got_endoff = got->br_startoff + got->br_blockcount;
4866 ASSERT(del->br_blockcount > 0);
4867 ASSERT(got->br_startoff <= del->br_startoff);
4868 ASSERT(got_endoff >= del_endoff);
4869 ASSERT(!isnullstartblock(got->br_startblock));
4871 if (got->br_startoff == del->br_startoff)
4872 state |= BMAP_LEFT_FILLING;
4873 if (got_endoff == del_endoff)
4874 state |= BMAP_RIGHT_FILLING;
4876 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4877 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4879 * Matches the whole extent. Delete the entry.
4881 xfs_iext_remove(ip, icur, state);
4882 xfs_iext_prev(ifp, icur);
4884 case BMAP_LEFT_FILLING:
4886 * Deleting the first part of the extent.
4888 got->br_startoff = del_endoff;
4889 got->br_blockcount -= del->br_blockcount;
4890 got->br_startblock = del->br_startblock + del->br_blockcount;
4891 xfs_iext_update_extent(ip, state, icur, got);
4893 case BMAP_RIGHT_FILLING:
4895 * Deleting the last part of the extent.
4897 got->br_blockcount -= del->br_blockcount;
4898 xfs_iext_update_extent(ip, state, icur, got);
4902 * Deleting the middle of the extent.
4904 got->br_blockcount = del->br_startoff - got->br_startoff;
4906 new.br_startoff = del_endoff;
4907 new.br_blockcount = got_endoff - del_endoff;
4908 new.br_state = got->br_state;
4909 new.br_startblock = del->br_startblock + del->br_blockcount;
4911 xfs_iext_update_extent(ip, state, icur, got);
4912 xfs_iext_next(ifp, icur);
4913 xfs_iext_insert(ip, icur, &new, state);
4916 ip->i_delayed_blks -= del->br_blockcount;
4920 * Called by xfs_bmapi to update file extent records and the btree
4921 * after removing space.
4923 STATIC int /* error */
4924 xfs_bmap_del_extent_real(
4925 xfs_inode_t *ip, /* incore inode pointer */
4926 xfs_trans_t *tp, /* current transaction pointer */
4927 struct xfs_iext_cursor *icur,
4928 xfs_btree_cur_t *cur, /* if null, not a btree */
4929 xfs_bmbt_irec_t *del, /* data to remove from extents */
4930 int *logflagsp, /* inode logging flags */
4931 int whichfork, /* data or attr fork */
4932 int bflags) /* bmapi flags */
4934 xfs_fsblock_t del_endblock=0; /* first block past del */
4935 xfs_fileoff_t del_endoff; /* first offset past del */
4936 int do_fx; /* free extent at end of routine */
4937 int error; /* error return value */
4938 int flags = 0;/* inode logging flags */
4939 struct xfs_bmbt_irec got; /* current extent entry */
4940 xfs_fileoff_t got_endoff; /* first offset past got */
4941 int i; /* temp state */
4942 struct xfs_ifork *ifp; /* inode fork pointer */
4943 xfs_mount_t *mp; /* mount structure */
4944 xfs_filblks_t nblks; /* quota/sb block count */
4945 xfs_bmbt_irec_t new; /* new record to be inserted */
4947 uint qfield; /* quota field to update */
4948 int state = xfs_bmap_fork_to_state(whichfork);
4949 struct xfs_bmbt_irec old;
4952 XFS_STATS_INC(mp, xs_del_exlist);
4954 ifp = XFS_IFORK_PTR(ip, whichfork);
4955 ASSERT(del->br_blockcount > 0);
4956 xfs_iext_get_extent(ifp, icur, &got);
4957 ASSERT(got.br_startoff <= del->br_startoff);
4958 del_endoff = del->br_startoff + del->br_blockcount;
4959 got_endoff = got.br_startoff + got.br_blockcount;
4960 ASSERT(got_endoff >= del_endoff);
4961 ASSERT(!isnullstartblock(got.br_startblock));
4966 * If it's the case where the directory code is running with no block
4967 * reservation, and the deleted block is in the middle of its extent,
4968 * and the resulting insert of an extent would cause transformation to
4969 * btree format, then reject it. The calling code will then swap blocks
4970 * around instead. We have to do this now, rather than waiting for the
4971 * conversion to btree format, since the transaction will be dirty then.
4973 if (tp->t_blk_res == 0 &&
4974 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4975 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4976 XFS_IFORK_MAXEXT(ip, whichfork) &&
4977 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4980 flags = XFS_ILOG_CORE;
4981 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4986 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
4989 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
4993 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4997 nblks = len * mp->m_sb.sb_rextsize;
4998 qfield = XFS_TRANS_DQ_RTBCOUNT;
5001 nblks = del->br_blockcount;
5002 qfield = XFS_TRANS_DQ_BCOUNT;
5005 del_endblock = del->br_startblock + del->br_blockcount;
5007 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5010 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5013 if (got.br_startoff == del->br_startoff)
5014 state |= BMAP_LEFT_FILLING;
5015 if (got_endoff == del_endoff)
5016 state |= BMAP_RIGHT_FILLING;
5018 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5019 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5021 * Matches the whole extent. Delete the entry.
5023 xfs_iext_remove(ip, icur, state);
5024 xfs_iext_prev(ifp, icur);
5025 XFS_IFORK_NEXT_SET(ip, whichfork,
5026 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5027 flags |= XFS_ILOG_CORE;
5029 flags |= xfs_ilog_fext(whichfork);
5032 if ((error = xfs_btree_delete(cur, &i)))
5034 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5036 case BMAP_LEFT_FILLING:
5038 * Deleting the first part of the extent.
5040 got.br_startoff = del_endoff;
5041 got.br_startblock = del_endblock;
5042 got.br_blockcount -= del->br_blockcount;
5043 xfs_iext_update_extent(ip, state, icur, &got);
5045 flags |= xfs_ilog_fext(whichfork);
5048 error = xfs_bmbt_update(cur, &got);
5052 case BMAP_RIGHT_FILLING:
5054 * Deleting the last part of the extent.
5056 got.br_blockcount -= del->br_blockcount;
5057 xfs_iext_update_extent(ip, state, icur, &got);
5059 flags |= xfs_ilog_fext(whichfork);
5062 error = xfs_bmbt_update(cur, &got);
5068 * Deleting the middle of the extent.
5072 got.br_blockcount = del->br_startoff - got.br_startoff;
5073 xfs_iext_update_extent(ip, state, icur, &got);
5075 new.br_startoff = del_endoff;
5076 new.br_blockcount = got_endoff - del_endoff;
5077 new.br_state = got.br_state;
5078 new.br_startblock = del_endblock;
5080 flags |= XFS_ILOG_CORE;
5082 error = xfs_bmbt_update(cur, &got);
5085 error = xfs_btree_increment(cur, 0, &i);
5088 cur->bc_rec.b = new;
5089 error = xfs_btree_insert(cur, &i);
5090 if (error && error != -ENOSPC)
5093 * If get no-space back from btree insert, it tried a
5094 * split, and we have a zero block reservation. Fix up
5095 * our state and return the error.
5097 if (error == -ENOSPC) {
5099 * Reset the cursor, don't trust it after any
5102 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5105 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5107 * Update the btree record back
5108 * to the original value.
5110 error = xfs_bmbt_update(cur, &old);
5114 * Reset the extent record back
5115 * to the original value.
5117 xfs_iext_update_extent(ip, state, icur, &old);
5122 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5124 flags |= xfs_ilog_fext(whichfork);
5125 XFS_IFORK_NEXT_SET(ip, whichfork,
5126 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5127 xfs_iext_next(ifp, icur);
5128 xfs_iext_insert(ip, icur, &new, state);
5132 /* remove reverse mapping */
5133 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5136 * If we need to, add to list of extents to delete.
5138 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5139 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5140 xfs_refcount_decrease_extent(tp, del);
5142 __xfs_bmap_add_free(tp, del->br_startblock,
5143 del->br_blockcount, NULL,
5144 (bflags & XFS_BMAPI_NODISCARD) ||
5145 del->br_state == XFS_EXT_UNWRITTEN);
5150 * Adjust inode # blocks in the file.
5153 ip->i_d.di_nblocks -= nblks;
5155 * Adjust quota data.
5157 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5158 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5166 * Unmap (remove) blocks from a file.
5167 * If nexts is nonzero then the number of extents to remove is limited to
5168 * that value. If not all extents in the block range can be removed then
5173 struct xfs_trans *tp, /* transaction pointer */
5174 struct xfs_inode *ip, /* incore inode */
5175 xfs_fileoff_t start, /* first file offset deleted */
5176 xfs_filblks_t *rlen, /* i/o: amount remaining */
5177 int flags, /* misc flags */
5178 xfs_extnum_t nexts) /* number of extents max */
5180 struct xfs_btree_cur *cur; /* bmap btree cursor */
5181 struct xfs_bmbt_irec del; /* extent being deleted */
5182 int error; /* error return value */
5183 xfs_extnum_t extno; /* extent number in list */
5184 struct xfs_bmbt_irec got; /* current extent record */
5185 struct xfs_ifork *ifp; /* inode fork pointer */
5186 int isrt; /* freeing in rt area */
5187 int logflags; /* transaction logging flags */
5188 xfs_extlen_t mod; /* rt extent offset */
5189 struct xfs_mount *mp; /* mount structure */
5190 int tmp_logflags; /* partial logging flags */
5191 int wasdel; /* was a delayed alloc extent */
5192 int whichfork; /* data or attribute fork */
5194 xfs_filblks_t len = *rlen; /* length to unmap in file */
5195 xfs_fileoff_t max_len;
5196 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5198 struct xfs_iext_cursor icur;
5201 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5203 whichfork = xfs_bmapi_whichfork(flags);
5204 ASSERT(whichfork != XFS_COW_FORK);
5205 ifp = XFS_IFORK_PTR(ip, whichfork);
5207 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5208 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5209 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5211 return -EFSCORRUPTED;
5214 if (XFS_FORCED_SHUTDOWN(mp))
5217 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5222 * Guesstimate how many blocks we can unmap without running the risk of
5223 * blowing out the transaction with a mix of EFIs and reflink
5226 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5227 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5231 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5232 (error = xfs_iread_extents(tp, ip, whichfork)))
5234 if (xfs_iext_count(ifp) == 0) {
5238 XFS_STATS_INC(mp, xs_blk_unmap);
5239 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5242 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5249 if (ifp->if_flags & XFS_IFBROOT) {
5250 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5251 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5252 cur->bc_private.b.flags = 0;
5258 * Synchronize by locking the bitmap inode.
5260 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5261 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5262 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5263 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5267 while (end != (xfs_fileoff_t)-1 && end >= start &&
5268 (nexts == 0 || extno < nexts) && max_len > 0) {
5270 * Is the found extent after a hole in which end lives?
5271 * Just back up to the previous extent, if so.
5273 if (got.br_startoff > end &&
5274 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5279 * Is the last block of this extent before the range
5280 * we're supposed to delete? If so, we're done.
5282 end = XFS_FILEOFF_MIN(end,
5283 got.br_startoff + got.br_blockcount - 1);
5287 * Then deal with the (possibly delayed) allocated space
5291 wasdel = isnullstartblock(del.br_startblock);
5294 * Make sure we don't touch multiple AGF headers out of order
5295 * in a single transaction, as that could cause AB-BA deadlocks.
5298 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5299 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5303 if (got.br_startoff < start) {
5304 del.br_startoff = start;
5305 del.br_blockcount -= start - got.br_startoff;
5307 del.br_startblock += start - got.br_startoff;
5309 if (del.br_startoff + del.br_blockcount > end + 1)
5310 del.br_blockcount = end + 1 - del.br_startoff;
5312 /* How much can we safely unmap? */
5313 if (max_len < del.br_blockcount) {
5314 del.br_startoff += del.br_blockcount - max_len;
5316 del.br_startblock += del.br_blockcount - max_len;
5317 del.br_blockcount = max_len;
5323 sum = del.br_startblock + del.br_blockcount;
5324 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5327 * Realtime extent not lined up at the end.
5328 * The extent could have been split into written
5329 * and unwritten pieces, or we could just be
5330 * unmapping part of it. But we can't really
5331 * get rid of part of a realtime extent.
5333 if (del.br_state == XFS_EXT_UNWRITTEN) {
5335 * This piece is unwritten, or we're not
5336 * using unwritten extents. Skip over it.
5339 end -= mod > del.br_blockcount ?
5340 del.br_blockcount : mod;
5341 if (end < got.br_startoff &&
5342 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5349 * It's written, turn it unwritten.
5350 * This is better than zeroing it.
5352 ASSERT(del.br_state == XFS_EXT_NORM);
5353 ASSERT(tp->t_blk_res > 0);
5355 * If this spans a realtime extent boundary,
5356 * chop it back to the start of the one we end at.
5358 if (del.br_blockcount > mod) {
5359 del.br_startoff += del.br_blockcount - mod;
5360 del.br_startblock += del.br_blockcount - mod;
5361 del.br_blockcount = mod;
5363 del.br_state = XFS_EXT_UNWRITTEN;
5364 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5365 whichfork, &icur, &cur, &del,
5371 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5374 * Realtime extent is lined up at the end but not
5375 * at the front. We'll get rid of full extents if
5378 mod = mp->m_sb.sb_rextsize - mod;
5379 if (del.br_blockcount > mod) {
5380 del.br_blockcount -= mod;
5381 del.br_startoff += mod;
5382 del.br_startblock += mod;
5383 } else if (del.br_startoff == start &&
5384 (del.br_state == XFS_EXT_UNWRITTEN ||
5385 tp->t_blk_res == 0)) {
5387 * Can't make it unwritten. There isn't
5388 * a full extent here so just skip it.
5390 ASSERT(end >= del.br_blockcount);
5391 end -= del.br_blockcount;
5392 if (got.br_startoff > end &&
5393 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5398 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5399 struct xfs_bmbt_irec prev;
5402 * This one is already unwritten.
5403 * It must have a written left neighbor.
5404 * Unwrite the killed part of that one and
5407 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5409 ASSERT(prev.br_state == XFS_EXT_NORM);
5410 ASSERT(!isnullstartblock(prev.br_startblock));
5411 ASSERT(del.br_startblock ==
5412 prev.br_startblock + prev.br_blockcount);
5413 if (prev.br_startoff < start) {
5414 mod = start - prev.br_startoff;
5415 prev.br_blockcount -= mod;
5416 prev.br_startblock += mod;
5417 prev.br_startoff = start;
5419 prev.br_state = XFS_EXT_UNWRITTEN;
5420 error = xfs_bmap_add_extent_unwritten_real(tp,
5421 ip, whichfork, &icur, &cur,
5427 ASSERT(del.br_state == XFS_EXT_NORM);
5428 del.br_state = XFS_EXT_UNWRITTEN;
5429 error = xfs_bmap_add_extent_unwritten_real(tp,
5430 ip, whichfork, &icur, &cur,
5440 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5443 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5444 &del, &tmp_logflags, whichfork,
5446 logflags |= tmp_logflags;
5452 max_len -= del.br_blockcount;
5453 end = del.br_startoff - 1;
5456 * If not done go on to the next (previous) record.
5458 if (end != (xfs_fileoff_t)-1 && end >= start) {
5459 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5460 (got.br_startoff > end &&
5461 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5468 if (done || end == (xfs_fileoff_t)-1 || end < start)
5471 *rlen = end - start + 1;
5474 * Convert to a btree if necessary.
5476 if (xfs_bmap_needs_btree(ip, whichfork)) {
5477 ASSERT(cur == NULL);
5478 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5479 &tmp_logflags, whichfork);
5480 logflags |= tmp_logflags;
5482 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5488 * Log everything. Do this after conversion, there's no point in
5489 * logging the extent records if we've converted to btree format.
5491 if ((logflags & xfs_ilog_fext(whichfork)) &&
5492 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5493 logflags &= ~xfs_ilog_fext(whichfork);
5494 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5495 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5496 logflags &= ~xfs_ilog_fbroot(whichfork);
5498 * Log inode even in the error case, if the transaction
5499 * is dirty we'll need to shut down the filesystem.
5502 xfs_trans_log_inode(tp, ip, logflags);
5505 cur->bc_private.b.allocated = 0;
5506 xfs_btree_del_cursor(cur, error);
5511 /* Unmap a range of a file. */
5515 struct xfs_inode *ip,
5524 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5530 * Determine whether an extent shift can be accomplished by a merge with the
5531 * extent that precedes the target hole of the shift.
5535 struct xfs_bmbt_irec *left, /* preceding extent */
5536 struct xfs_bmbt_irec *got, /* current extent to shift */
5537 xfs_fileoff_t shift) /* shift fsb */
5539 xfs_fileoff_t startoff;
5541 startoff = got->br_startoff - shift;
5544 * The extent, once shifted, must be adjacent in-file and on-disk with
5545 * the preceding extent.
5547 if ((left->br_startoff + left->br_blockcount != startoff) ||
5548 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5549 (left->br_state != got->br_state) ||
5550 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5557 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5558 * hole in the file. If an extent shift would result in the extent being fully
5559 * adjacent to the extent that currently precedes the hole, we can merge with
5560 * the preceding extent rather than do the shift.
5562 * This function assumes the caller has verified a shift-by-merge is possible
5563 * with the provided extents via xfs_bmse_can_merge().
5567 struct xfs_trans *tp,
5568 struct xfs_inode *ip,
5570 xfs_fileoff_t shift, /* shift fsb */
5571 struct xfs_iext_cursor *icur,
5572 struct xfs_bmbt_irec *got, /* extent to shift */
5573 struct xfs_bmbt_irec *left, /* preceding extent */
5574 struct xfs_btree_cur *cur,
5575 int *logflags) /* output */
5577 struct xfs_bmbt_irec new;
5578 xfs_filblks_t blockcount;
5580 struct xfs_mount *mp = ip->i_mount;
5582 blockcount = left->br_blockcount + got->br_blockcount;
5584 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5585 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5586 ASSERT(xfs_bmse_can_merge(left, got, shift));
5589 new.br_blockcount = blockcount;
5592 * Update the on-disk extent count, the btree if necessary and log the
5595 XFS_IFORK_NEXT_SET(ip, whichfork,
5596 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5597 *logflags |= XFS_ILOG_CORE;
5599 *logflags |= XFS_ILOG_DEXT;
5603 /* lookup and remove the extent to merge */
5604 error = xfs_bmbt_lookup_eq(cur, got, &i);
5607 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5609 error = xfs_btree_delete(cur, &i);
5612 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5614 /* lookup and update size of the previous extent */
5615 error = xfs_bmbt_lookup_eq(cur, left, &i);
5618 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5620 error = xfs_bmbt_update(cur, &new);
5625 xfs_iext_remove(ip, icur, 0);
5626 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5627 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5630 /* update reverse mapping. rmap functions merge the rmaps for us */
5631 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5632 memcpy(&new, got, sizeof(new));
5633 new.br_startoff = left->br_startoff + left->br_blockcount;
5634 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5639 xfs_bmap_shift_update_extent(
5640 struct xfs_trans *tp,
5641 struct xfs_inode *ip,
5643 struct xfs_iext_cursor *icur,
5644 struct xfs_bmbt_irec *got,
5645 struct xfs_btree_cur *cur,
5647 xfs_fileoff_t startoff)
5649 struct xfs_mount *mp = ip->i_mount;
5650 struct xfs_bmbt_irec prev = *got;
5653 *logflags |= XFS_ILOG_CORE;
5655 got->br_startoff = startoff;
5658 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5661 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5663 error = xfs_bmbt_update(cur, got);
5667 *logflags |= XFS_ILOG_DEXT;
5670 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5673 /* update reverse mapping */
5674 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5675 xfs_rmap_map_extent(tp, ip, whichfork, got);
5680 xfs_bmap_collapse_extents(
5681 struct xfs_trans *tp,
5682 struct xfs_inode *ip,
5683 xfs_fileoff_t *next_fsb,
5684 xfs_fileoff_t offset_shift_fsb,
5687 int whichfork = XFS_DATA_FORK;
5688 struct xfs_mount *mp = ip->i_mount;
5689 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5690 struct xfs_btree_cur *cur = NULL;
5691 struct xfs_bmbt_irec got, prev;
5692 struct xfs_iext_cursor icur;
5693 xfs_fileoff_t new_startoff;
5697 if (unlikely(XFS_TEST_ERROR(
5698 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5699 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5700 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5701 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5702 return -EFSCORRUPTED;
5705 if (XFS_FORCED_SHUTDOWN(mp))
5708 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5710 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5711 error = xfs_iread_extents(tp, ip, whichfork);
5716 if (ifp->if_flags & XFS_IFBROOT) {
5717 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5718 cur->bc_private.b.flags = 0;
5721 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5725 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5728 new_startoff = got.br_startoff - offset_shift_fsb;
5729 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5730 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5735 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5736 error = xfs_bmse_merge(tp, ip, whichfork,
5737 offset_shift_fsb, &icur, &got, &prev,
5744 if (got.br_startoff < offset_shift_fsb) {
5750 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5751 cur, &logflags, new_startoff);
5756 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5761 *next_fsb = got.br_startoff;
5764 xfs_btree_del_cursor(cur, error);
5766 xfs_trans_log_inode(tp, ip, logflags);
5770 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5772 xfs_bmap_can_insert_extents(
5773 struct xfs_inode *ip,
5775 xfs_fileoff_t shift)
5777 struct xfs_bmbt_irec got;
5781 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5783 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5786 xfs_ilock(ip, XFS_ILOCK_EXCL);
5787 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5788 if (!error && !is_empty && got.br_startoff >= off &&
5789 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5791 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5797 xfs_bmap_insert_extents(
5798 struct xfs_trans *tp,
5799 struct xfs_inode *ip,
5800 xfs_fileoff_t *next_fsb,
5801 xfs_fileoff_t offset_shift_fsb,
5803 xfs_fileoff_t stop_fsb)
5805 int whichfork = XFS_DATA_FORK;
5806 struct xfs_mount *mp = ip->i_mount;
5807 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5808 struct xfs_btree_cur *cur = NULL;
5809 struct xfs_bmbt_irec got, next;
5810 struct xfs_iext_cursor icur;
5811 xfs_fileoff_t new_startoff;
5815 if (unlikely(XFS_TEST_ERROR(
5816 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5817 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5818 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5819 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5820 return -EFSCORRUPTED;
5823 if (XFS_FORCED_SHUTDOWN(mp))
5826 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5828 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5829 error = xfs_iread_extents(tp, ip, whichfork);
5834 if (ifp->if_flags & XFS_IFBROOT) {
5835 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5836 cur->bc_private.b.flags = 0;
5839 if (*next_fsb == NULLFSBLOCK) {
5840 xfs_iext_last(ifp, &icur);
5841 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5842 stop_fsb > got.br_startoff) {
5847 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5852 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5855 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
5860 new_startoff = got.br_startoff + offset_shift_fsb;
5861 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5862 if (new_startoff + got.br_blockcount > next.br_startoff) {
5868 * Unlike a left shift (which involves a hole punch), a right
5869 * shift does not modify extent neighbors in any way. We should
5870 * never find mergeable extents in this scenario. Check anyways
5871 * and warn if we encounter two extents that could be one.
5873 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5877 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5878 cur, &logflags, new_startoff);
5882 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5883 stop_fsb >= got.br_startoff + got.br_blockcount) {
5888 *next_fsb = got.br_startoff;
5891 xfs_btree_del_cursor(cur, error);
5893 xfs_trans_log_inode(tp, ip, logflags);
5898 * Splits an extent into two extents at split_fsb block such that it is the
5899 * first block of the current_ext. @ext is a target extent to be split.
5900 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5901 * hole or the first block of extents, just return 0.
5904 xfs_bmap_split_extent_at(
5905 struct xfs_trans *tp,
5906 struct xfs_inode *ip,
5907 xfs_fileoff_t split_fsb)
5909 int whichfork = XFS_DATA_FORK;
5910 struct xfs_btree_cur *cur = NULL;
5911 struct xfs_bmbt_irec got;
5912 struct xfs_bmbt_irec new; /* split extent */
5913 struct xfs_mount *mp = ip->i_mount;
5914 struct xfs_ifork *ifp;
5915 xfs_fsblock_t gotblkcnt; /* new block count for got */
5916 struct xfs_iext_cursor icur;
5921 if (unlikely(XFS_TEST_ERROR(
5922 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5923 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5924 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5925 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5926 XFS_ERRLEVEL_LOW, mp);
5927 return -EFSCORRUPTED;
5930 if (XFS_FORCED_SHUTDOWN(mp))
5933 ifp = XFS_IFORK_PTR(ip, whichfork);
5934 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5935 /* Read in all the extents */
5936 error = xfs_iread_extents(tp, ip, whichfork);
5942 * If there are not extents, or split_fsb lies in a hole we are done.
5944 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
5945 got.br_startoff >= split_fsb)
5948 gotblkcnt = split_fsb - got.br_startoff;
5949 new.br_startoff = split_fsb;
5950 new.br_startblock = got.br_startblock + gotblkcnt;
5951 new.br_blockcount = got.br_blockcount - gotblkcnt;
5952 new.br_state = got.br_state;
5954 if (ifp->if_flags & XFS_IFBROOT) {
5955 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5956 cur->bc_private.b.flags = 0;
5957 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5960 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5963 got.br_blockcount = gotblkcnt;
5964 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5967 logflags = XFS_ILOG_CORE;
5969 error = xfs_bmbt_update(cur, &got);
5973 logflags |= XFS_ILOG_DEXT;
5975 /* Add new extent */
5976 xfs_iext_next(ifp, &icur);
5977 xfs_iext_insert(ip, &icur, &new, 0);
5978 XFS_IFORK_NEXT_SET(ip, whichfork,
5979 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5982 error = xfs_bmbt_lookup_eq(cur, &new, &i);
5985 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5986 error = xfs_btree_insert(cur, &i);
5989 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5993 * Convert to a btree if necessary.
5995 if (xfs_bmap_needs_btree(ip, whichfork)) {
5996 int tmp_logflags; /* partial log flag return val */
5998 ASSERT(cur == NULL);
5999 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6000 &tmp_logflags, whichfork);
6001 logflags |= tmp_logflags;
6006 cur->bc_private.b.allocated = 0;
6007 xfs_btree_del_cursor(cur, error);
6011 xfs_trans_log_inode(tp, ip, logflags);
6016 xfs_bmap_split_extent(
6017 struct xfs_inode *ip,
6018 xfs_fileoff_t split_fsb)
6020 struct xfs_mount *mp = ip->i_mount;
6021 struct xfs_trans *tp;
6024 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6025 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6029 xfs_ilock(ip, XFS_ILOCK_EXCL);
6030 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6032 error = xfs_bmap_split_extent_at(tp, ip, split_fsb);
6036 return xfs_trans_commit(tp);
6039 xfs_trans_cancel(tp);
6043 /* Deferred mapping is only for real extents in the data fork. */
6045 xfs_bmap_is_update_needed(
6046 struct xfs_bmbt_irec *bmap)
6048 return bmap->br_startblock != HOLESTARTBLOCK &&
6049 bmap->br_startblock != DELAYSTARTBLOCK;
6052 /* Record a bmap intent. */
6055 struct xfs_trans *tp,
6056 enum xfs_bmap_intent_type type,
6057 struct xfs_inode *ip,
6059 struct xfs_bmbt_irec *bmap)
6061 struct xfs_bmap_intent *bi;
6063 trace_xfs_bmap_defer(tp->t_mountp,
6064 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6066 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6067 ip->i_ino, whichfork,
6069 bmap->br_blockcount,
6072 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
6073 INIT_LIST_HEAD(&bi->bi_list);
6076 bi->bi_whichfork = whichfork;
6077 bi->bi_bmap = *bmap;
6079 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6083 /* Map an extent into a file. */
6085 xfs_bmap_map_extent(
6086 struct xfs_trans *tp,
6087 struct xfs_inode *ip,
6088 struct xfs_bmbt_irec *PREV)
6090 if (!xfs_bmap_is_update_needed(PREV))
6093 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6096 /* Unmap an extent out of a file. */
6098 xfs_bmap_unmap_extent(
6099 struct xfs_trans *tp,
6100 struct xfs_inode *ip,
6101 struct xfs_bmbt_irec *PREV)
6103 if (!xfs_bmap_is_update_needed(PREV))
6106 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6110 * Process one of the deferred bmap operations. We pass back the
6111 * btree cursor to maintain our lock on the bmapbt between calls.
6114 xfs_bmap_finish_one(
6115 struct xfs_trans *tp,
6116 struct xfs_inode *ip,
6117 enum xfs_bmap_intent_type type,
6119 xfs_fileoff_t startoff,
6120 xfs_fsblock_t startblock,
6121 xfs_filblks_t *blockcount,
6126 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6128 trace_xfs_bmap_deferred(tp->t_mountp,
6129 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6130 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6131 ip->i_ino, whichfork, startoff, *blockcount, state);
6133 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6134 return -EFSCORRUPTED;
6136 if (XFS_TEST_ERROR(false, tp->t_mountp,
6137 XFS_ERRTAG_BMAP_FINISH_ONE))
6142 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6146 case XFS_BMAP_UNMAP:
6147 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6148 XFS_BMAPI_REMAP, 1);
6152 error = -EFSCORRUPTED;
6158 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6160 xfs_bmap_validate_extent(
6161 struct xfs_inode *ip,
6163 struct xfs_bmbt_irec *irec)
6165 struct xfs_mount *mp = ip->i_mount;
6166 xfs_fsblock_t endfsb;
6169 isrt = XFS_IS_REALTIME_INODE(ip);
6170 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6172 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6173 return __this_address;
6174 if (!xfs_verify_rtbno(mp, endfsb))
6175 return __this_address;
6177 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6178 return __this_address;
6179 if (!xfs_verify_fsbno(mp, endfsb))
6180 return __this_address;
6181 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6182 XFS_FSB_TO_AGNO(mp, endfsb))
6183 return __this_address;
6185 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6186 return __this_address;