LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_bmap.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwa @ Mon Jul 31 20:08:17 PDT 2023 Lines: 2594 3088 84.0 %
Date: 2023-07-31 20:08:17 Functions: 79 81 97.5 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_sb.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_defer.h"
      16             : #include "xfs_dir2.h"
      17             : #include "xfs_inode.h"
      18             : #include "xfs_btree.h"
      19             : #include "xfs_trans.h"
      20             : #include "xfs_alloc.h"
      21             : #include "xfs_bmap.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"
      33             : #include "xfs_rmap.h"
      34             : #include "xfs_ag.h"
      35             : #include "xfs_ag_resv.h"
      36             : #include "xfs_refcount.h"
      37             : #include "xfs_icache.h"
      38             : #include "xfs_iomap.h"
      39             : 
      40             : struct kmem_cache               *xfs_bmap_intent_cache;
      41             : 
      42             : /*
      43             :  * Miscellaneous helper functions
      44             :  */
      45             : 
      46             : /*
      47             :  * Compute and fill in the value of the maximum depth of a bmap btree
      48             :  * in this filesystem.  Done once, during mount.
      49             :  */
      50             : void
      51       44990 : xfs_bmap_compute_maxlevels(
      52             :         xfs_mount_t     *mp,            /* file system mount structure */
      53             :         int             whichfork)      /* data or attr fork */
      54             : {
      55       44990 :         uint64_t        maxblocks;      /* max blocks at this level */
      56       44990 :         xfs_extnum_t    maxleafents;    /* max leaf entries possible */
      57       44990 :         int             level;          /* btree level */
      58       44990 :         int             maxrootrecs;    /* max records in root block */
      59       44990 :         int             minleafrecs;    /* min records in leaf block */
      60       44990 :         int             minnoderecs;    /* min records in node block */
      61       44990 :         int             sz;             /* root block size */
      62             : 
      63             :         /*
      64             :          * The maximum number of extents in a fork, hence the maximum number of
      65             :          * leaf entries, is controlled by the size of the on-disk extent count.
      66             :          *
      67             :          * Note that we can no longer assume that if we are in ATTR1 that the
      68             :          * fork offset of all the inodes will be
      69             :          * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
      70             :          * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
      71             :          * but probably at various positions. Therefore, for both ATTR1 and
      72             :          * ATTR2 we have to assume the worst case scenario of a minimum size
      73             :          * available.
      74             :          */
      75       44990 :         maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
      76             :                                 whichfork);
      77       44990 :         if (whichfork == XFS_DATA_FORK)
      78             :                 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
      79             :         else
      80       22495 :                 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
      81             : 
      82       44990 :         maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
      83       44990 :         minleafrecs = mp->m_bmap_dmnr[0];
      84       44990 :         minnoderecs = mp->m_bmap_dmnr[1];
      85       44990 :         maxblocks = howmany_64(maxleafents, minleafrecs);
      86      314492 :         for (level = 1; maxblocks > 1; level++) {
      87      224512 :                 if (maxblocks <= maxrootrecs)
      88             :                         maxblocks = 1;
      89             :                 else
      90      215452 :                         maxblocks = howmany_64(maxblocks, minnoderecs);
      91             :         }
      92       44990 :         mp->m_bm_maxlevels[whichfork] = level;
      93       44990 :         ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
      94       44990 : }
      95             : 
      96             : unsigned int
      97       22495 : xfs_bmap_compute_attr_offset(
      98             :         struct xfs_mount        *mp)
      99             : {
     100       22495 :         if (mp->m_sb.sb_inodesize == 256)
     101          80 :                 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
     102             :         return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
     103             : }
     104             : 
     105             : STATIC int                              /* error */
     106   117671736 : xfs_bmbt_lookup_eq(
     107             :         struct xfs_btree_cur    *cur,
     108             :         struct xfs_bmbt_irec    *irec,
     109             :         int                     *stat)  /* success/failure */
     110             : {
     111   117671736 :         cur->bc_rec.b = *irec;
     112   117671736 :         return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
     113             : }
     114             : 
     115             : STATIC int                              /* error */
     116           0 : xfs_bmbt_lookup_first(
     117             :         struct xfs_btree_cur    *cur,
     118             :         int                     *stat)  /* success/failure */
     119             : {
     120           0 :         cur->bc_rec.b.br_startoff = 0;
     121           0 :         cur->bc_rec.b.br_startblock = 0;
     122           0 :         cur->bc_rec.b.br_blockcount = 0;
     123           0 :         return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
     124             : }
     125             : 
     126             : /*
     127             :  * Check if the inode needs to be converted to btree format.
     128             :  */
     129   187935424 : static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
     130             : {
     131   187935424 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     132             : 
     133   373852052 :         return whichfork != XFS_COW_FORK &&
     134   187935481 :                 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
     135   103957564 :                 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
     136             : }
     137             : 
     138             : /*
     139             :  * Check if the inode should be converted to extent format.
     140             :  */
     141   188607045 : static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
     142             : {
     143   188607045 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     144             : 
     145   376886830 :         return whichfork != XFS_COW_FORK &&
     146   188605806 :                 ifp->if_format == XFS_DINODE_FMT_BTREE &&
     147    82232146 :                 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
     148             : }
     149             : 
     150             : /*
     151             :  * Update the record referred to by cur to the value given by irec
     152             :  * This either works (return 0) or gets an EFSCORRUPTED error.
     153             :  */
     154             : STATIC int
     155    28623152 : xfs_bmbt_update(
     156             :         struct xfs_btree_cur    *cur,
     157             :         struct xfs_bmbt_irec    *irec)
     158             : {
     159    28623152 :         union xfs_btree_rec     rec;
     160             : 
     161    28623152 :         xfs_bmbt_disk_set_all(&rec.bmbt, irec);
     162    28623151 :         return xfs_btree_update(cur, &rec);
     163             : }
     164             : 
     165             : /*
     166             :  * Compute the worst-case number of indirect blocks that will be used
     167             :  * for ip's delayed extent of length "len".
     168             :  */
     169             : STATIC xfs_filblks_t
     170    19121661 : xfs_bmap_worst_indlen(
     171             :         xfs_inode_t     *ip,            /* incore inode pointer */
     172             :         xfs_filblks_t   len)            /* delayed extent length */
     173             : {
     174    19121661 :         int             level;          /* btree level number */
     175    19121661 :         int             maxrecs;        /* maximum record count at this level */
     176    19121661 :         xfs_mount_t     *mp;            /* mount structure */
     177    19121661 :         xfs_filblks_t   rval;           /* return value */
     178             : 
     179    19121661 :         mp = ip->i_mount;
     180    19121661 :         maxrecs = mp->m_bmap_dmxr[0];
     181    19121661 :         for (level = 0, rval = 0;
     182    20611301 :              level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
     183     1489640 :              level++) {
     184    20613146 :                 len += maxrecs - 1;
     185    20613146 :                 do_div(len, maxrecs);
     186    20613146 :                 rval += len;
     187    20613146 :                 if (len == 1)
     188    19123506 :                         return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
     189    19123506 :                                 level - 1;
     190     1489640 :                 if (level == 0)
     191     1153831 :                         maxrecs = mp->m_bmap_dmxr[1];
     192             :         }
     193             :         return rval;
     194             : }
     195             : 
     196             : /*
     197             :  * Calculate the default attribute fork offset for newly created inodes.
     198             :  */
     199             : uint
     200     3010487 : xfs_default_attroffset(
     201             :         struct xfs_inode        *ip)
     202             : {
     203     3010487 :         if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
     204             :                 return roundup(sizeof(xfs_dev_t), 8);
     205     6442961 :         return M_IGEO(ip->i_mount)->attr_fork_offset;
     206             : }
     207             : 
     208             : /*
     209             :  * Helper routine to reset inode i_forkoff field when switching attribute fork
     210             :  * from local to extent format - we reset it where possible to make space
     211             :  * available for inline data fork extents.
     212             :  */
     213             : STATIC void
     214     2513657 : xfs_bmap_forkoff_reset(
     215             :         xfs_inode_t     *ip,
     216             :         int             whichfork)
     217             : {
     218     2513657 :         if (whichfork == XFS_ATTR_FORK &&
     219     2454685 :             ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
     220             :             ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
     221     2427566 :                 uint    dfl_forkoff = xfs_default_attroffset(ip) >> 3;
     222             : 
     223     2427566 :                 if (dfl_forkoff > ip->i_forkoff)
     224     2408856 :                         ip->i_forkoff = dfl_forkoff;
     225             :         }
     226     2513657 : }
     227             : 
     228             : #ifdef DEBUG
     229             : STATIC struct xfs_buf *
     230   442319941 : xfs_bmap_get_bp(
     231             :         struct xfs_btree_cur    *cur,
     232             :         xfs_fsblock_t           bno)
     233             : {
     234   442319941 :         struct xfs_log_item     *lip;
     235   442319941 :         int                     i;
     236             : 
     237   442319941 :         if (!cur)
     238             :                 return NULL;
     239             : 
     240  1147876041 :         for (i = 0; i < cur->bc_maxlevels; i++) {
     241  1147876037 :                 if (!cur->bc_levels[i].bp)
     242             :                         break;
     243   743615806 :                 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
     244    38059706 :                         return cur->bc_levels[i].bp;
     245             :         }
     246             : 
     247             :         /* Chase down all the log items to see if the bp is there */
     248  2159047933 :         list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
     249  1765809262 :                 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
     250             : 
     251  1765809262 :                 if (bip->bli_item.li_type == XFS_LI_BUF &&
     252   977938650 :                     xfs_buf_daddr(bip->bli_buf) == bno)
     253    11021564 :                         return bip->bli_buf;
     254             :         }
     255             : 
     256             :         return NULL;
     257             : }
     258             : 
     259             : STATIC void
     260    38306757 : xfs_check_block(
     261             :         struct xfs_btree_block  *block,
     262             :         xfs_mount_t             *mp,
     263             :         int                     root,
     264             :         short                   sz)
     265             : {
     266    38306757 :         int                     i, j, dmxr;
     267    38306757 :         __be64                  *pp, *thispa;   /* pointer to block address */
     268    38306757 :         xfs_bmbt_key_t          *prevp, *keyp;
     269             : 
     270    38306757 :         ASSERT(be16_to_cpu(block->bb_level) > 0);
     271             : 
     272             :         prevp = NULL;
     273   480627365 :         for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
     274   442320595 :                 dmxr = mp->m_bmap_dmxr[0];
     275   442320595 :                 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
     276             : 
     277   442320595 :                 if (prevp) {
     278   404013996 :                         ASSERT(be64_to_cpu(prevp->br_startoff) <
     279             :                                be64_to_cpu(keyp->br_startoff));
     280             :                 }
     281   442320595 :                 prevp = keyp;
     282             : 
     283             :                 /*
     284             :                  * Compare the block numbers to see if there are dups.
     285             :                  */
     286   442320595 :                 if (root)
     287   140731795 :                         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
     288             :                 else
     289   301588800 :                         pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
     290             : 
     291  5807478018 :                 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
     292  5365157410 :                         if (root)
     293   770752400 :                                 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
     294             :                         else
     295  4594405010 :                                 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
     296  5365157507 :                         if (*thispa == *pp) {
     297           0 :                                 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
     298             :                                         __func__, j, i,
     299             :                                         (unsigned long long)be64_to_cpu(*thispa));
     300           0 :                                 xfs_err(mp, "%s: ptrs are equal in node\n",
     301             :                                         __func__);
     302           0 :                                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     303             :                         }
     304             :                 }
     305             :         }
     306    38306770 : }
     307             : 
     308             : /*
     309             :  * Check that the extents for the inode ip are in the right order in all
     310             :  * btree leaves. THis becomes prohibitively expensive for large extent count
     311             :  * files, so don't bother with inodes that have more than 10,000 extents in
     312             :  * them. The btree record ordering checks will still be done, so for such large
     313             :  * bmapbt constructs that is going to catch most corruptions.
     314             :  */
     315             : STATIC void
     316   123328323 : xfs_bmap_check_leaf_extents(
     317             :         struct xfs_btree_cur    *cur,   /* btree cursor or null */
     318             :         xfs_inode_t             *ip,            /* incore inode pointer */
     319             :         int                     whichfork)      /* data or attr fork */
     320             : {
     321   123328323 :         struct xfs_mount        *mp = ip->i_mount;
     322   123328323 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     323   123328378 :         struct xfs_btree_block  *block; /* current btree block */
     324   123328378 :         xfs_fsblock_t           bno;    /* block # of "block" */
     325   123328378 :         struct xfs_buf          *bp;    /* buffer for "block" */
     326   123328378 :         int                     error;  /* error return value */
     327   123328378 :         xfs_extnum_t            i=0, j; /* index into the extents list */
     328   123328378 :         int                     level;  /* btree level, for checking */
     329   123328378 :         __be64                  *pp;    /* pointer to block address */
     330   123328378 :         xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
     331   123328378 :         xfs_bmbt_rec_t          last = {0, 0}; /* last extent in prev block */
     332   123328378 :         xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
     333   123328378 :         int                     bp_release = 0;
     334             : 
     335   123328378 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
     336             :                 return;
     337             : 
     338             :         /* skip large extent count inodes */
     339    56642800 :         if (ip->i_df.if_nextents > 10000)
     340             :                 return;
     341             : 
     342    28353678 :         bno = NULLFSBLOCK;
     343    28353678 :         block = ifp->if_broot;
     344             :         /*
     345             :          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
     346             :          */
     347    28353678 :         level = be16_to_cpu(block->bb_level);
     348    28353678 :         ASSERT(level > 0);
     349    28353678 :         xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
     350    28353656 :         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
     351    28353686 :         bno = be64_to_cpu(*pp);
     352             : 
     353    28353686 :         ASSERT(bno != NULLFSBLOCK);
     354    28353686 :         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
     355    28353686 :         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
     356             : 
     357             :         /*
     358             :          * Go down the tree until leaf level is reached, following the first
     359             :          * pointer (leftmost) at each level.
     360             :          */
     361    38306763 :         while (level-- > 0) {
     362             :                 /* See if buf is in cur first */
     363    38306763 :                 bp_release = 0;
     364    38306763 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     365    38306747 :                 if (!bp) {
     366    21347875 :                         bp_release = 1;
     367    21347875 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     368             :                                                 XFS_BMAP_BTREE_REF,
     369             :                                                 &xfs_bmbt_buf_ops);
     370    21347886 :                         if (error)
     371           7 :                                 goto error_norelse;
     372             :                 }
     373    38306751 :                 block = XFS_BUF_TO_BLOCK(bp);
     374    38306751 :                 if (level == 0)
     375             :                         break;
     376             : 
     377             :                 /*
     378             :                  * Check this block for basic sanity (increasing keys and
     379             :                  * no duplicate blocks).
     380             :                  */
     381             : 
     382     9953076 :                 xfs_check_block(block, mp, 0, 0);
     383     9953078 :                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
     384     9953078 :                 bno = be64_to_cpu(*pp);
     385     9953078 :                 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
     386           0 :                         error = -EFSCORRUPTED;
     387           0 :                         goto error0;
     388             :                 }
     389     9953077 :                 if (bp_release) {
     390           0 :                         bp_release = 0;
     391           0 :                         xfs_trans_brelse(NULL, bp);
     392             :                 }
     393             :         }
     394             : 
     395             :         /*
     396             :          * Here with bp and block set to the leftmost leaf node in the tree.
     397             :          */
     398             :         i = 0;
     399             : 
     400             :         /*
     401             :          * Loop over all leaf nodes checking that all extents are in the right order.
     402             :          */
     403   836378383 :         for (;;) {
     404   432366029 :                 xfs_fsblock_t   nextbno;
     405   432366029 :                 xfs_extnum_t    num_recs;
     406             : 
     407             : 
     408   432366029 :                 num_recs = xfs_btree_get_numrecs(block);
     409             : 
     410             :                 /*
     411             :                  * Read-ahead the next leaf block, if any.
     412             :                  */
     413             : 
     414   432366029 :                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
     415             : 
     416             :                 /*
     417             :                  * Check all the extents to make sure they are OK.
     418             :                  * If we had a previous block, the last entry should
     419             :                  * conform with the first entry in this one.
     420             :                  */
     421             : 
     422   432366029 :                 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
     423   432366029 :                 if (i) {
     424   404012908 :                         ASSERT(xfs_bmbt_disk_get_startoff(&last) +
     425             :                                xfs_bmbt_disk_get_blockcount(&last) <=
     426             :                                xfs_bmbt_disk_get_startoff(ep));
     427             :                 }
     428 >10423*10^7 :                 for (j = 1; j < num_recs; j++) {
     429 >10380*10^7 :                         nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
     430 >10380*10^7 :                         ASSERT(xfs_bmbt_disk_get_startoff(ep) +
     431             :                                xfs_bmbt_disk_get_blockcount(ep) <=
     432             :                                xfs_bmbt_disk_get_startoff(nextp));
     433 >10380*10^7 :                         ep = nextp;
     434             :                 }
     435             : 
     436   432366676 :                 last = *ep;
     437   432366676 :                 i += num_recs;
     438   432366676 :                 if (bp_release) {
     439   393239056 :                         bp_release = 0;
     440   393239056 :                         xfs_trans_brelse(NULL, bp);
     441             :                 }
     442   432367251 :                 bno = nextbno;
     443             :                 /*
     444             :                  * If we've reached the end, stop.
     445             :                  */
     446   432367251 :                 if (bno == NULLFSBLOCK)
     447             :                         break;
     448             : 
     449   404013613 :                 bp_release = 0;
     450   404013613 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     451   404012205 :                 if (!bp) {
     452   371891702 :                         bp_release = 1;
     453   371891702 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     454             :                                                 XFS_BMAP_BTREE_REF,
     455             :                                                 &xfs_bmbt_buf_ops);
     456   371891912 :                         if (error)
     457          61 :                                 goto error_norelse;
     458             :                 }
     459   404012354 :                 block = XFS_BUF_TO_BLOCK(bp);
     460             :         }
     461             : 
     462             :         return;
     463             : 
     464             : error0:
     465           0 :         xfs_warn(mp, "%s: at error0", __func__);
     466           0 :         if (bp_release)
     467           0 :                 xfs_trans_brelse(NULL, bp);
     468           0 : error_norelse:
     469          68 :         xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
     470             :                 __func__, i);
     471          68 :         xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
     472          68 :         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     473          68 :         return;
     474             : }
     475             : 
     476             : /*
     477             :  * Validate that the bmbt_irecs being returned from bmapi are valid
     478             :  * given the caller's original parameters.  Specifically check the
     479             :  * ranges of the returned irecs to ensure that they only extend beyond
     480             :  * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
     481             :  */
     482             : STATIC void
     483    60876541 : xfs_bmap_validate_ret(
     484             :         xfs_fileoff_t           bno,
     485             :         xfs_filblks_t           len,
     486             :         uint32_t                flags,
     487             :         xfs_bmbt_irec_t         *mval,
     488             :         int                     nmap,
     489             :         int                     ret_nmap)
     490             : {
     491    60876541 :         int                     i;              /* index to map values */
     492             : 
     493    60876541 :         ASSERT(ret_nmap <= nmap);
     494             : 
     495   121753178 :         for (i = 0; i < ret_nmap; i++) {
     496    60877036 :                 ASSERT(mval[i].br_blockcount > 0);
     497    60877036 :                 if (!(flags & XFS_BMAPI_ENTIRE)) {
     498    60877036 :                         ASSERT(mval[i].br_startoff >= bno);
     499    60877036 :                         ASSERT(mval[i].br_blockcount <= len);
     500    60877036 :                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
     501             :                                bno + len);
     502             :                 } else {
     503           0 :                         ASSERT(mval[i].br_startoff < bno + len);
     504           0 :                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
     505             :                                bno);
     506             :                 }
     507    60877036 :                 ASSERT(i == 0 ||
     508             :                        mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
     509             :                        mval[i].br_startoff);
     510    60877036 :                 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
     511             :                        mval[i].br_startblock != HOLESTARTBLOCK);
     512    60877036 :                 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
     513             :                        mval[i].br_state == XFS_EXT_UNWRITTEN);
     514             :         }
     515    60876142 : }
     516             : 
     517             : #else
     518             : #define xfs_bmap_check_leaf_extents(cur, ip, whichfork)         do { } while (0)
     519             : #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)    do { } while (0)
     520             : #endif /* DEBUG */
     521             : 
     522             : /*
     523             :  * Inode fork format manipulation functions
     524             :  */
     525             : 
     526             : /*
     527             :  * Convert the inode format to extent format if it currently is in btree format,
     528             :  * but the extent list is small enough that it fits into the extent format.
     529             :  *
     530             :  * Since the extents are already in-core, all we have to do is give up the space
     531             :  * for the btree root and pitch the leaf block.
     532             :  */
     533             : STATIC int                              /* error */
     534   188603579 : xfs_bmap_btree_to_extents(
     535             :         struct xfs_trans        *tp,    /* transaction pointer */
     536             :         struct xfs_inode        *ip,    /* incore inode pointer */
     537             :         struct xfs_btree_cur    *cur,   /* btree cursor */
     538             :         int                     *logflagsp, /* inode logging flags */
     539             :         int                     whichfork)  /* data or attr fork */
     540             : {
     541   188603579 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     542   188606983 :         struct xfs_mount        *mp = ip->i_mount;
     543   188606983 :         struct xfs_btree_block  *rblock = ifp->if_broot;
     544   188606983 :         struct xfs_btree_block  *cblock;/* child btree block */
     545   188606983 :         xfs_fsblock_t           cbno;   /* child block number */
     546   188606983 :         struct xfs_buf          *cbp;   /* child block's buffer */
     547   188606983 :         int                     error;  /* error return value */
     548   188606983 :         __be64                  *pp;    /* ptr to block address */
     549   188606983 :         struct xfs_owner_info   oinfo;
     550             : 
     551             :         /* check if we actually need the extent format first: */
     552   188606983 :         if (!xfs_bmap_wants_extents(ip, whichfork))
     553             :                 return 0;
     554             : 
     555      121694 :         ASSERT(cur);
     556      121694 :         ASSERT(whichfork != XFS_COW_FORK);
     557      121694 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
     558      121694 :         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
     559      121694 :         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
     560      121694 :         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
     561             : 
     562      121694 :         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
     563      121694 :         cbno = be64_to_cpu(*pp);
     564             : #ifdef DEBUG
     565      121694 :         if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
     566           0 :                 return -EFSCORRUPTED;
     567             : #endif
     568      121694 :         error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
     569             :                                 &xfs_bmbt_buf_ops);
     570      121694 :         if (error)
     571             :                 return error;
     572      121693 :         cblock = XFS_BUF_TO_BLOCK(cbp);
     573      121693 :         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
     574             :                 return error;
     575             : 
     576      121693 :         xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
     577      121693 :         error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
     578             :                         XFS_AG_RESV_NONE);
     579      121693 :         if (error)
     580             :                 return error;
     581             : 
     582      121693 :         ip->i_nblocks--;
     583      121693 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     584      121693 :         xfs_trans_binval(tp, cbp);
     585      121693 :         if (cur->bc_levels[0].bp == cbp)
     586      121693 :                 cur->bc_levels[0].bp = NULL;
     587      121693 :         xfs_iroot_realloc(ip, -1, whichfork);
     588      121693 :         ASSERT(ifp->if_broot == NULL);
     589      121693 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     590      121693 :         *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
     591      121693 :         return 0;
     592             : }
     593             : 
     594             : /*
     595             :  * Convert an extents-format file into a btree-format file.
     596             :  * The new file will have a root block (in the inode) and a single child block.
     597             :  */
     598             : STATIC int                                      /* error */
     599      275769 : xfs_bmap_extents_to_btree(
     600             :         struct xfs_trans        *tp,            /* transaction pointer */
     601             :         struct xfs_inode        *ip,            /* incore inode pointer */
     602             :         struct xfs_btree_cur    **curp,         /* cursor returned to caller */
     603             :         int                     wasdel,         /* converting a delayed alloc */
     604             :         int                     *logflagsp,     /* inode logging flags */
     605             :         int                     whichfork)      /* data or attr fork */
     606             : {
     607      275769 :         struct xfs_btree_block  *ablock;        /* allocated (child) bt block */
     608      275769 :         struct xfs_buf          *abp;           /* buffer for ablock */
     609      275769 :         struct xfs_alloc_arg    args;           /* allocation arguments */
     610      275769 :         struct xfs_bmbt_rec     *arp;           /* child record pointer */
     611      275769 :         struct xfs_btree_block  *block;         /* btree root block */
     612      275769 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     613      275769 :         int                     error;          /* error return value */
     614      275769 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
     615      275769 :         struct xfs_bmbt_key     *kp;            /* root block key pointer */
     616      275769 :         struct xfs_mount        *mp;            /* mount structure */
     617      275769 :         xfs_bmbt_ptr_t          *pp;            /* root block address pointer */
     618      275769 :         struct xfs_iext_cursor  icur;
     619      275769 :         struct xfs_bmbt_irec    rec;
     620      275769 :         xfs_extnum_t            cnt = 0;
     621             : 
     622      275769 :         mp = ip->i_mount;
     623      275769 :         ASSERT(whichfork != XFS_COW_FORK);
     624      275769 :         ifp = xfs_ifork_ptr(ip, whichfork);
     625      275769 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
     626             : 
     627             :         /*
     628             :          * Make space in the inode incore. This needs to be undone if we fail
     629             :          * to expand the root.
     630             :          */
     631      275769 :         xfs_iroot_realloc(ip, 1, whichfork);
     632             : 
     633             :         /*
     634             :          * Fill in the root.
     635             :          */
     636      275769 :         block = ifp->if_broot;
     637      275769 :         xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
     638             :                                  XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
     639             :                                  XFS_BTREE_LONG_PTRS);
     640             :         /*
     641             :          * Need a cursor.  Can't allocate until bb_level is filled in.
     642             :          */
     643      275769 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
     644      275769 :         cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
     645             :         /*
     646             :          * Convert to a btree with two levels, one record in root.
     647             :          */
     648      275769 :         ifp->if_format = XFS_DINODE_FMT_BTREE;
     649      275769 :         memset(&args, 0, sizeof(args));
     650      275769 :         args.tp = tp;
     651      275769 :         args.mp = mp;
     652      275769 :         xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
     653             : 
     654      275769 :         args.minlen = args.maxlen = args.prod = 1;
     655      275769 :         args.wasdel = wasdel;
     656      275769 :         *logflagsp = 0;
     657     1378845 :         error = xfs_alloc_vextent_start_ag(&args,
     658      275769 :                                 XFS_INO_TO_FSB(mp, ip->i_ino));
     659      275769 :         if (error)
     660          26 :                 goto out_root_realloc;
     661             : 
     662             :         /*
     663             :          * Allocation can't fail, the space was reserved.
     664             :          */
     665      275743 :         if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
     666           0 :                 error = -ENOSPC;
     667           0 :                 goto out_root_realloc;
     668             :         }
     669             : 
     670      275743 :         cur->bc_ino.allocated++;
     671      275743 :         ip->i_nblocks++;
     672      275743 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     673      827229 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
     674      275743 :                         XFS_FSB_TO_DADDR(mp, args.fsbno),
     675             :                         mp->m_bsize, 0, &abp);
     676      275743 :         if (error)
     677           0 :                 goto out_unreserve_dquot;
     678             : 
     679             :         /*
     680             :          * Fill in the child block.
     681             :          */
     682      275743 :         abp->b_ops = &xfs_bmbt_buf_ops;
     683      275743 :         ablock = XFS_BUF_TO_BLOCK(abp);
     684      275743 :         xfs_btree_init_block_int(mp, ablock, xfs_buf_daddr(abp),
     685             :                                 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
     686             :                                 XFS_BTREE_LONG_PTRS);
     687             : 
     688     5545331 :         for_each_xfs_iext(ifp, &icur, &rec) {
     689     5269588 :                 if (isnullstartblock(rec.br_startblock))
     690      171082 :                         continue;
     691     5098506 :                 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
     692     5098506 :                 xfs_bmbt_disk_set_all(arp, &rec);
     693     5098506 :                 cnt++;
     694             :         }
     695      275743 :         ASSERT(cnt == ifp->if_nextents);
     696      275743 :         xfs_btree_set_numrecs(ablock, cnt);
     697             : 
     698             :         /*
     699             :          * Fill in the root key and pointer.
     700             :          */
     701      275743 :         kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
     702      275743 :         arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
     703      275743 :         kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
     704      275742 :         pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
     705             :                                                 be16_to_cpu(block->bb_level)));
     706      275743 :         *pp = cpu_to_be64(args.fsbno);
     707             : 
     708             :         /*
     709             :          * Do all this logging at the end so that
     710             :          * the root is at the right level.
     711             :          */
     712      275743 :         xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
     713      275743 :         xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
     714      275743 :         ASSERT(*curp == NULL);
     715      275743 :         *curp = cur;
     716      275743 :         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
     717      275743 :         return 0;
     718             : 
     719             : out_unreserve_dquot:
     720           0 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     721          26 : out_root_realloc:
     722          26 :         xfs_iroot_realloc(ip, -1, whichfork);
     723          26 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     724          26 :         ASSERT(ifp->if_broot == NULL);
     725          26 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     726             : 
     727          26 :         return error;
     728             : }
     729             : 
     730             : /*
     731             :  * Convert a local file to an extents file.
     732             :  * This code is out of bounds for data forks of regular files,
     733             :  * since the file data needs to get logged so things will stay consistent.
     734             :  * (The bmap-level manipulations are ok, though).
     735             :  */
     736             : void
     737     2513614 : xfs_bmap_local_to_extents_empty(
     738             :         struct xfs_trans        *tp,
     739             :         struct xfs_inode        *ip,
     740             :         int                     whichfork)
     741             : {
     742     2513614 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     743             : 
     744     2513667 :         ASSERT(whichfork != XFS_COW_FORK);
     745     2513667 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     746     2513667 :         ASSERT(ifp->if_bytes == 0);
     747     2513667 :         ASSERT(ifp->if_nextents == 0);
     748             : 
     749     2513667 :         xfs_bmap_forkoff_reset(ip, whichfork);
     750     2513636 :         ifp->if_u1.if_root = NULL;
     751     2513636 :         ifp->if_height = 0;
     752     2513636 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     753     2513636 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     754     2513672 : }
     755             : 
     756             : 
     757             : STATIC int                              /* error */
     758           4 : xfs_bmap_local_to_extents(
     759             :         xfs_trans_t     *tp,            /* transaction pointer */
     760             :         xfs_inode_t     *ip,            /* incore inode pointer */
     761             :         xfs_extlen_t    total,          /* total blocks needed by transaction */
     762             :         int             *logflagsp,     /* inode logging flags */
     763             :         int             whichfork,
     764             :         void            (*init_fn)(struct xfs_trans *tp,
     765             :                                    struct xfs_buf *bp,
     766             :                                    struct xfs_inode *ip,
     767             :                                    struct xfs_ifork *ifp))
     768             : {
     769           4 :         int             error = 0;
     770           4 :         int             flags;          /* logging flags returned */
     771           4 :         struct xfs_ifork *ifp;          /* inode fork pointer */
     772           4 :         xfs_alloc_arg_t args;           /* allocation arguments */
     773           4 :         struct xfs_buf  *bp;            /* buffer for extent block */
     774           4 :         struct xfs_bmbt_irec rec;
     775           4 :         struct xfs_iext_cursor icur;
     776             : 
     777             :         /*
     778             :          * We don't want to deal with the case of keeping inode data inline yet.
     779             :          * So sending the data fork of a regular inode is invalid.
     780             :          */
     781           4 :         ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
     782           4 :         ifp = xfs_ifork_ptr(ip, whichfork);
     783           4 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     784             : 
     785           4 :         if (!ifp->if_bytes) {
     786           0 :                 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     787           0 :                 flags = XFS_ILOG_CORE;
     788           0 :                 goto done;
     789             :         }
     790             : 
     791           4 :         flags = 0;
     792           4 :         error = 0;
     793           4 :         memset(&args, 0, sizeof(args));
     794           4 :         args.tp = tp;
     795           4 :         args.mp = ip->i_mount;
     796           4 :         args.total = total;
     797           4 :         args.minlen = args.maxlen = args.prod = 1;
     798           4 :         xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
     799             : 
     800             :         /*
     801             :          * Allocate a block.  We know we need only one, since the
     802             :          * file currently fits in an inode.
     803             :          */
     804           4 :         args.total = total;
     805           4 :         args.minlen = args.maxlen = args.prod = 1;
     806          20 :         error = xfs_alloc_vextent_start_ag(&args,
     807           4 :                         XFS_INO_TO_FSB(args.mp, ip->i_ino));
     808           4 :         if (error)
     809           0 :                 goto done;
     810             : 
     811             :         /* Can't fail, the space was reserved. */
     812           4 :         ASSERT(args.fsbno != NULLFSBLOCK);
     813           4 :         ASSERT(args.len == 1);
     814          12 :         error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
     815           4 :                         XFS_FSB_TO_DADDR(args.mp, args.fsbno),
     816           4 :                         args.mp->m_bsize, 0, &bp);
     817           4 :         if (error)
     818           0 :                 goto done;
     819             : 
     820             :         /*
     821             :          * Initialize the block, copy the data and log the remote buffer.
     822             :          *
     823             :          * The callout is responsible for logging because the remote format
     824             :          * might differ from the local format and thus we don't know how much to
     825             :          * log here. Note that init_fn must also set the buffer log item type
     826             :          * correctly.
     827             :          */
     828           4 :         init_fn(tp, bp, ip, ifp);
     829             : 
     830             :         /* account for the change in fork size */
     831           4 :         xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
     832           4 :         xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     833           4 :         flags |= XFS_ILOG_CORE;
     834             : 
     835           4 :         ifp->if_u1.if_root = NULL;
     836           4 :         ifp->if_height = 0;
     837             : 
     838           4 :         rec.br_startoff = 0;
     839           4 :         rec.br_startblock = args.fsbno;
     840           4 :         rec.br_blockcount = 1;
     841           4 :         rec.br_state = XFS_EXT_NORM;
     842           4 :         xfs_iext_first(ifp, &icur);
     843           4 :         xfs_iext_insert(ip, &icur, &rec, 0);
     844             : 
     845           4 :         ifp->if_nextents = 1;
     846           4 :         ip->i_nblocks = 1;
     847           4 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     848           4 :         flags |= xfs_ilog_fext(whichfork);
     849             : 
     850           4 : done:
     851           4 :         *logflagsp = flags;
     852           4 :         return error;
     853             : }
     854             : 
     855             : /*
     856             :  * Called from xfs_bmap_add_attrfork to handle btree format files.
     857             :  */
     858             : STATIC int                                      /* error */
     859       17312 : xfs_bmap_add_attrfork_btree(
     860             :         xfs_trans_t             *tp,            /* transaction pointer */
     861             :         xfs_inode_t             *ip,            /* incore inode pointer */
     862             :         int                     *flags)         /* inode logging flags */
     863             : {
     864       17312 :         struct xfs_btree_block  *block = ip->i_df.if_broot;
     865       17312 :         struct xfs_btree_cur    *cur;           /* btree cursor */
     866       17312 :         int                     error;          /* error return value */
     867       17312 :         xfs_mount_t             *mp;            /* file system mount struct */
     868       17312 :         int                     stat;           /* newroot status */
     869             : 
     870       17312 :         mp = ip->i_mount;
     871             : 
     872       17312 :         if (XFS_BMAP_BMDR_SPACE(block) <= xfs_inode_data_fork_size(ip))
     873       17312 :                 *flags |= XFS_ILOG_DBROOT;
     874             :         else {
     875           0 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
     876           0 :                 error = xfs_bmbt_lookup_first(cur, &stat);
     877           0 :                 if (error)
     878           0 :                         goto error0;
     879             :                 /* must be at least one entry */
     880           0 :                 if (XFS_IS_CORRUPT(mp, stat != 1)) {
     881           0 :                         error = -EFSCORRUPTED;
     882           0 :                         goto error0;
     883             :                 }
     884           0 :                 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
     885           0 :                         goto error0;
     886           0 :                 if (stat == 0) {
     887           0 :                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     888           0 :                         return -ENOSPC;
     889             :                 }
     890           0 :                 cur->bc_ino.allocated = 0;
     891           0 :                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     892             :         }
     893             :         return 0;
     894           0 : error0:
     895           0 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     896           0 :         return error;
     897             : }
     898             : 
     899             : /*
     900             :  * Called from xfs_bmap_add_attrfork to handle extents format files.
     901             :  */
     902             : STATIC int                                      /* error */
     903     3007157 : xfs_bmap_add_attrfork_extents(
     904             :         struct xfs_trans        *tp,            /* transaction pointer */
     905             :         struct xfs_inode        *ip,            /* incore inode pointer */
     906             :         int                     *flags)         /* inode logging flags */
     907             : {
     908     3007157 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     909     3007157 :         int                     error;          /* error return value */
     910             : 
     911     3007157 :         if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
     912     3007157 :             xfs_inode_data_fork_size(ip))
     913             :                 return 0;
     914       17928 :         cur = NULL;
     915       17928 :         error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
     916             :                                           XFS_DATA_FORK);
     917       17928 :         if (cur) {
     918       17926 :                 cur->bc_ino.allocated = 0;
     919       17926 :                 xfs_btree_del_cursor(cur, error);
     920             :         }
     921             :         return error;
     922             : }
     923             : 
     924             : /*
     925             :  * Called from xfs_bmap_add_attrfork to handle local format files. Each
     926             :  * different data fork content type needs a different callout to do the
     927             :  * conversion. Some are basic and only require special block initialisation
     928             :  * callouts for the data formating, others (directories) are so specialised they
     929             :  * handle everything themselves.
     930             :  *
     931             :  * XXX (dgc): investigate whether directory conversion can use the generic
     932             :  * formatting callout. It should be possible - it's just a very complex
     933             :  * formatter.
     934             :  */
     935             : STATIC int                                      /* error */
     936      407973 : xfs_bmap_add_attrfork_local(
     937             :         struct xfs_trans        *tp,            /* transaction pointer */
     938             :         struct xfs_inode        *ip,            /* incore inode pointer */
     939             :         int                     *flags)         /* inode logging flags */
     940             : {
     941      407973 :         struct xfs_da_args      dargs;          /* args for dir/attr code */
     942             : 
     943      407973 :         if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
     944             :                 return 0;
     945             : 
     946        1022 :         if (S_ISDIR(VFS_I(ip)->i_mode)) {
     947        1018 :                 memset(&dargs, 0, sizeof(dargs));
     948        1018 :                 dargs.geo = ip->i_mount->m_dir_geo;
     949        1018 :                 dargs.dp = ip;
     950        1018 :                 dargs.total = dargs.geo->fsbcount;
     951        1018 :                 dargs.whichfork = XFS_DATA_FORK;
     952        1018 :                 dargs.trans = tp;
     953        1018 :                 return xfs_dir2_sf_to_block(&dargs);
     954             :         }
     955             : 
     956           4 :         if (S_ISLNK(VFS_I(ip)->i_mode))
     957           4 :                 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
     958             :                                                  XFS_DATA_FORK,
     959             :                                                  xfs_symlink_local_to_remote);
     960             : 
     961             :         /* should only be called for types that support local format data */
     962           0 :         ASSERT(0);
     963           0 :         return -EFSCORRUPTED;
     964             : }
     965             : 
     966             : /*
     967             :  * Set an inode attr fork offset based on the format of the data fork.
     968             :  */
     969             : static int
     970     3432486 : xfs_bmap_set_attrforkoff(
     971             :         struct xfs_inode        *ip,
     972             :         int                     size,
     973             :         int                     *version)
     974             : {
     975     3432486 :         int                     default_size = xfs_default_attroffset(ip) >> 3;
     976             : 
     977     3432486 :         switch (ip->i_df.if_format) {
     978          12 :         case XFS_DINODE_FMT_DEV:
     979          12 :                 ip->i_forkoff = default_size;
     980          12 :                 break;
     981     3432474 :         case XFS_DINODE_FMT_LOCAL:
     982             :         case XFS_DINODE_FMT_EXTENTS:
     983             :         case XFS_DINODE_FMT_BTREE:
     984     3432474 :                 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
     985     3432425 :                 if (!ip->i_forkoff)
     986         958 :                         ip->i_forkoff = default_size;
     987     3431467 :                 else if (xfs_has_attr2(ip->i_mount) && version)
     988     3431467 :                         *version = 2;
     989             :                 break;
     990           0 :         default:
     991           0 :                 ASSERT(0);
     992           0 :                 return -EINVAL;
     993             :         }
     994             : 
     995             :         return 0;
     996             : }
     997             : 
     998             : /*
     999             :  * Convert inode from non-attributed to attributed.
    1000             :  * Must not be in a transaction, ip must not be locked.
    1001             :  */
    1002             : int                                             /* error code */
    1003     3443460 : xfs_bmap_add_attrfork(
    1004             :         xfs_inode_t             *ip,            /* incore inode pointer */
    1005             :         int                     size,           /* space new attribute needs */
    1006             :         int                     rsvd)           /* xact may use reserved blks */
    1007             : {
    1008     3443460 :         xfs_mount_t             *mp;            /* mount structure */
    1009     3443460 :         xfs_trans_t             *tp;            /* transaction pointer */
    1010     3443460 :         int                     blks;           /* space reservation */
    1011     3443460 :         int                     version = 1;    /* superblock attr version */
    1012     3443460 :         int                     logflags;       /* logging flags */
    1013     3443460 :         int                     error;          /* error return value */
    1014             : 
    1015     3443460 :         ASSERT(xfs_inode_has_attr_fork(ip) == 0);
    1016             : 
    1017     3443460 :         mp = ip->i_mount;
    1018     3443460 :         ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
    1019             : 
    1020     3443460 :         blks = XFS_ADDAFORK_SPACE_RES(mp);
    1021             : 
    1022     3443460 :         error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
    1023             :                         rsvd, &tp);
    1024     3443439 :         if (error)
    1025             :                 return error;
    1026     3432430 :         if (xfs_inode_has_attr_fork(ip))
    1027           0 :                 goto trans_cancel;
    1028             : 
    1029     3432430 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    1030     3432473 :         error = xfs_bmap_set_attrforkoff(ip, size, &version);
    1031     3432484 :         if (error)
    1032           0 :                 goto trans_cancel;
    1033             : 
    1034     3432484 :         xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
    1035     3432481 :         logflags = 0;
    1036     3432481 :         switch (ip->i_df.if_format) {
    1037      407971 :         case XFS_DINODE_FMT_LOCAL:
    1038      407971 :                 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
    1039      407971 :                 break;
    1040     3007186 :         case XFS_DINODE_FMT_EXTENTS:
    1041     3007186 :                 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
    1042     3007186 :                 break;
    1043       17312 :         case XFS_DINODE_FMT_BTREE:
    1044       17312 :                 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
    1045       17312 :                 break;
    1046             :         default:
    1047             :                 error = 0;
    1048             :                 break;
    1049             :         }
    1050     3432476 :         if (logflags)
    1051       35242 :                 xfs_trans_log_inode(tp, ip, logflags);
    1052     3432478 :         if (error)
    1053           2 :                 goto trans_cancel;
    1054     3432476 :         if (!xfs_has_attr(mp) ||
    1055           0 :            (!xfs_has_attr2(mp) && version == 2)) {
    1056         389 :                 bool log_sb = false;
    1057             : 
    1058         389 :                 spin_lock(&mp->m_sb_lock);
    1059         389 :                 if (!xfs_has_attr(mp)) {
    1060         389 :                         xfs_add_attr(mp);
    1061         389 :                         log_sb = true;
    1062             :                 }
    1063         389 :                 if (!xfs_has_attr2(mp) && version == 2) {
    1064           0 :                         xfs_add_attr2(mp);
    1065           0 :                         log_sb = true;
    1066             :                 }
    1067         389 :                 spin_unlock(&mp->m_sb_lock);
    1068         389 :                 if (log_sb)
    1069         389 :                         xfs_log_sb(tp);
    1070             :         }
    1071             : 
    1072     3432476 :         error = xfs_trans_commit(tp);
    1073     3432485 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1074     3432485 :         return error;
    1075             : 
    1076           2 : trans_cancel:
    1077           2 :         xfs_trans_cancel(tp);
    1078           2 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1079           2 :         return error;
    1080             : }
    1081             : 
    1082             : /*
    1083             :  * Internal and external extent tree search functions.
    1084             :  */
    1085             : 
    1086             : struct xfs_iread_state {
    1087             :         struct xfs_iext_cursor  icur;
    1088             :         xfs_extnum_t            loaded;
    1089             : };
    1090             : 
    1091             : int
    1092           0 : xfs_bmap_complain_bad_rec(
    1093             :         struct xfs_inode                *ip,
    1094             :         int                             whichfork,
    1095             :         xfs_failaddr_t                  fa,
    1096             :         const struct xfs_bmbt_irec      *irec)
    1097             : {
    1098           0 :         struct xfs_mount                *mp = ip->i_mount;
    1099           0 :         const char                      *forkname;
    1100             : 
    1101           0 :         switch (whichfork) {
    1102             :         case XFS_DATA_FORK:     forkname = "data"; break;
    1103             :         case XFS_ATTR_FORK:     forkname = "attr"; break;
    1104             :         case XFS_COW_FORK:      forkname = "CoW"; break;
    1105             :         default:                forkname = "???"; break;
    1106             :         }
    1107             : 
    1108           0 :         xfs_warn(mp,
    1109             :  "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
    1110             :                                 ip->i_ino, forkname, fa);
    1111           0 :         xfs_warn(mp,
    1112             :                 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
    1113             :                 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
    1114             :                 irec->br_state);
    1115             : 
    1116           0 :         return -EFSCORRUPTED;
    1117             : }
    1118             : 
    1119             : /* Stuff every bmbt record from this block into the incore extent map. */
    1120             : static int
    1121     4117540 : xfs_iread_bmbt_block(
    1122             :         struct xfs_btree_cur    *cur,
    1123             :         int                     level,
    1124             :         void                    *priv)
    1125             : {
    1126     4117540 :         struct xfs_iread_state  *ir = priv;
    1127     4117540 :         struct xfs_mount        *mp = cur->bc_mp;
    1128     4117540 :         struct xfs_inode        *ip = cur->bc_ino.ip;
    1129     4117540 :         struct xfs_btree_block  *block;
    1130     4117540 :         struct xfs_buf          *bp;
    1131     4117540 :         struct xfs_bmbt_rec     *frp;
    1132     4117540 :         xfs_extnum_t            num_recs;
    1133     4117540 :         xfs_extnum_t            j;
    1134     4117540 :         int                     whichfork = cur->bc_ino.whichfork;
    1135     4117540 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1136             : 
    1137     4117541 :         block = xfs_btree_get_block(cur, level, &bp);
    1138             : 
    1139             :         /* Abort if we find more records than nextents. */
    1140     4117542 :         num_recs = xfs_btree_get_numrecs(block);
    1141     4117542 :         if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
    1142           0 :                 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
    1143             :                                 (unsigned long long)ip->i_ino);
    1144           0 :                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
    1145           0 :                                 sizeof(*block), __this_address);
    1146           0 :                 return -EFSCORRUPTED;
    1147             :         }
    1148             : 
    1149             :         /* Copy records into the incore cache. */
    1150     4117542 :         frp = XFS_BMBT_REC_ADDR(mp, block, 1);
    1151   789936242 :         for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
    1152   785818699 :                 struct xfs_bmbt_irec    new;
    1153   785818699 :                 xfs_failaddr_t          fa;
    1154             : 
    1155   785818699 :                 xfs_bmbt_disk_get_all(frp, &new);
    1156   785786381 :                 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
    1157   785821296 :                 if (fa) {
    1158           0 :                         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
    1159             :                                         "xfs_iread_extents(2)", frp,
    1160             :                                         sizeof(*frp), fa);
    1161           0 :                         return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
    1162             :                                         &new);
    1163             :                 }
    1164  1571642292 :                 xfs_iext_insert(ip, &ir->icur, &new,
    1165             :                                 xfs_bmap_fork_to_state(whichfork));
    1166  1571632242 :                 trace_xfs_read_extent(ip, &ir->icur,
    1167   785816271 :                                 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
    1168   785816318 :                 xfs_iext_next(ifp, &ir->icur);
    1169             :         }
    1170             : 
    1171             :         return 0;
    1172             : }
    1173             : 
    1174             : /*
    1175             :  * Read in extents from a btree-format inode.
    1176             :  */
    1177             : int
    1178  4072388716 : xfs_iread_extents(
    1179             :         struct xfs_trans        *tp,
    1180             :         struct xfs_inode        *ip,
    1181             :         int                     whichfork)
    1182             : {
    1183  4072388716 :         struct xfs_iread_state  ir;
    1184  4072388716 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1185  4074626305 :         struct xfs_mount        *mp = ip->i_mount;
    1186  4074626305 :         struct xfs_btree_cur    *cur;
    1187  4074626305 :         int                     error;
    1188             : 
    1189  4074626305 :         if (!xfs_need_iread_extents(ifp))
    1190             :                 return 0;
    1191             : 
    1192      382059 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1193             : 
    1194      382059 :         ir.loaded = 0;
    1195      382059 :         xfs_iext_first(ifp, &ir.icur);
    1196      382059 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    1197      382058 :         error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
    1198             :                         XFS_BTREE_VISIT_RECORDS, &ir);
    1199      382057 :         xfs_btree_del_cursor(cur, error);
    1200      382058 :         if (error)
    1201          69 :                 goto out;
    1202             : 
    1203      381989 :         if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
    1204           0 :                 error = -EFSCORRUPTED;
    1205           0 :                 goto out;
    1206             :         }
    1207      381989 :         ASSERT(ir.loaded == xfs_iext_count(ifp));
    1208             :         /*
    1209             :          * Use release semantics so that we can use acquire semantics in
    1210             :          * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
    1211             :          * after that load.
    1212             :          */
    1213      381990 :         smp_store_release(&ifp->if_needextents, 0);
    1214      381990 :         return 0;
    1215          69 : out:
    1216          69 :         xfs_iext_destroy(ifp);
    1217          69 :         return error;
    1218             : }
    1219             : 
    1220             : /*
    1221             :  * Returns the relative block number of the first unused block(s) in the given
    1222             :  * fork with at least "len" logically contiguous blocks free.  This is the
    1223             :  * lowest-address hole if the fork has holes, else the first block past the end
    1224             :  * of fork.  Return 0 if the fork is currently local (in-inode).
    1225             :  */
    1226             : int                                             /* error */
    1227     2648116 : xfs_bmap_first_unused(
    1228             :         struct xfs_trans        *tp,            /* transaction pointer */
    1229             :         struct xfs_inode        *ip,            /* incore inode */
    1230             :         xfs_extlen_t            len,            /* size of hole to find */
    1231             :         xfs_fileoff_t           *first_unused,  /* unused block */
    1232             :         int                     whichfork)      /* data or attr fork */
    1233             : {
    1234     2648116 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1235     2648164 :         struct xfs_bmbt_irec    got;
    1236     2648164 :         struct xfs_iext_cursor  icur;
    1237     2648164 :         xfs_fileoff_t           lastaddr = 0;
    1238     2648164 :         xfs_fileoff_t           lowest, max;
    1239     2648164 :         int                     error;
    1240             : 
    1241     2648164 :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
    1242           0 :                 *first_unused = 0;
    1243           0 :                 return 0;
    1244             :         }
    1245             : 
    1246     2648164 :         ASSERT(xfs_ifork_has_extents(ifp));
    1247             : 
    1248     2648164 :         error = xfs_iread_extents(tp, ip, whichfork);
    1249     2648116 :         if (error)
    1250             :                 return error;
    1251             : 
    1252     2648165 :         lowest = max = *first_unused;
    1253    13927532 :         for_each_xfs_iext(ifp, &icur, &got) {
    1254             :                 /*
    1255             :                  * See if the hole before this extent will work.
    1256             :                  */
    1257    11341209 :                 if (got.br_startoff >= lowest + len &&
    1258     8193899 :                     got.br_startoff - max >= len)
    1259             :                         break;
    1260    11279350 :                 lastaddr = got.br_startoff + got.br_blockcount;
    1261    11279350 :                 max = XFS_FILEOFF_MAX(lastaddr, lowest);
    1262             :         }
    1263             : 
    1264     2648136 :         *first_unused = max;
    1265     2648136 :         return 0;
    1266             : }
    1267             : 
    1268             : /*
    1269             :  * Returns the file-relative block number of the last block - 1 before
    1270             :  * last_block (input value) in the file.
    1271             :  * This is not based on i_size, it is based on the extent records.
    1272             :  * Returns 0 for local files, as they do not have extent records.
    1273             :  */
    1274             : int                                             /* error */
    1275       65466 : xfs_bmap_last_before(
    1276             :         struct xfs_trans        *tp,            /* transaction pointer */
    1277             :         struct xfs_inode        *ip,            /* incore inode */
    1278             :         xfs_fileoff_t           *last_block,    /* last block */
    1279             :         int                     whichfork)      /* data or attr fork */
    1280             : {
    1281       65466 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1282       65466 :         struct xfs_bmbt_irec    got;
    1283       65466 :         struct xfs_iext_cursor  icur;
    1284       65466 :         int                     error;
    1285             : 
    1286       65466 :         switch (ifp->if_format) {
    1287           0 :         case XFS_DINODE_FMT_LOCAL:
    1288           0 :                 *last_block = 0;
    1289           0 :                 return 0;
    1290             :         case XFS_DINODE_FMT_BTREE:
    1291             :         case XFS_DINODE_FMT_EXTENTS:
    1292       65466 :                 break;
    1293           0 :         default:
    1294           0 :                 ASSERT(0);
    1295           0 :                 return -EFSCORRUPTED;
    1296             :         }
    1297             : 
    1298       65466 :         error = xfs_iread_extents(tp, ip, whichfork);
    1299       65466 :         if (error)
    1300             :                 return error;
    1301             : 
    1302       65466 :         if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
    1303       45778 :                 *last_block = 0;
    1304             :         return 0;
    1305             : }
    1306             : 
    1307             : int
    1308   585537204 : xfs_bmap_last_extent(
    1309             :         struct xfs_trans        *tp,
    1310             :         struct xfs_inode        *ip,
    1311             :         int                     whichfork,
    1312             :         struct xfs_bmbt_irec    *rec,
    1313             :         int                     *is_empty)
    1314             : {
    1315   585537204 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1316   585596753 :         struct xfs_iext_cursor  icur;
    1317   585596753 :         int                     error;
    1318             : 
    1319   585596753 :         error = xfs_iread_extents(tp, ip, whichfork);
    1320   585710211 :         if (error)
    1321             :                 return error;
    1322             : 
    1323   585731329 :         xfs_iext_last(ifp, &icur);
    1324   585776735 :         if (!xfs_iext_get_extent(ifp, &icur, rec))
    1325    71392643 :                 *is_empty = 1;
    1326             :         else
    1327   514326139 :                 *is_empty = 0;
    1328             :         return 0;
    1329             : }
    1330             : 
    1331             : /*
    1332             :  * Check the last inode extent to determine whether this allocation will result
    1333             :  * in blocks being allocated at the end of the file. When we allocate new data
    1334             :  * blocks at the end of the file which do not start at the previous data block,
    1335             :  * we will try to align the new blocks at stripe unit boundaries.
    1336             :  *
    1337             :  * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
    1338             :  * at, or past the EOF.
    1339             :  */
    1340             : STATIC int
    1341        6416 : xfs_bmap_isaeof(
    1342             :         struct xfs_bmalloca     *bma,
    1343             :         int                     whichfork)
    1344             : {
    1345        6416 :         struct xfs_bmbt_irec    rec;
    1346        6416 :         int                     is_empty;
    1347        6416 :         int                     error;
    1348             : 
    1349        6416 :         bma->aeof = false;
    1350        6416 :         error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
    1351             :                                      &is_empty);
    1352        6416 :         if (error)
    1353             :                 return error;
    1354             : 
    1355        6416 :         if (is_empty) {
    1356        1180 :                 bma->aeof = true;
    1357        1180 :                 return 0;
    1358             :         }
    1359             : 
    1360             :         /*
    1361             :          * Check if we are allocation or past the last extent, or at least into
    1362             :          * the last delayed allocated extent.
    1363             :          */
    1364        5236 :         bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
    1365        1265 :                 (bma->offset >= rec.br_startoff &&
    1366        1265 :                  isnullstartblock(rec.br_startblock));
    1367        5236 :         return 0;
    1368             : }
    1369             : 
    1370             : /*
    1371             :  * Returns the file-relative block number of the first block past eof in
    1372             :  * the file.  This is not based on i_size, it is based on the extent records.
    1373             :  * Returns 0 for local files, as they do not have extent records.
    1374             :  */
    1375             : int
    1376   585181811 : xfs_bmap_last_offset(
    1377             :         struct xfs_inode        *ip,
    1378             :         xfs_fileoff_t           *last_block,
    1379             :         int                     whichfork)
    1380             : {
    1381   585181811 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1382   585281831 :         struct xfs_bmbt_irec    rec;
    1383   585281831 :         int                     is_empty;
    1384   585281831 :         int                     error;
    1385             : 
    1386   585281831 :         *last_block = 0;
    1387             : 
    1388   585281831 :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
    1389             :                 return 0;
    1390             : 
    1391   585281831 :         if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
    1392           0 :                 return -EFSCORRUPTED;
    1393             : 
    1394   585281831 :         error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
    1395   585436012 :         if (error || is_empty)
    1396             :                 return error;
    1397             : 
    1398   514053903 :         *last_block = rec.br_startoff + rec.br_blockcount;
    1399   514053903 :         return 0;
    1400             : }
    1401             : 
    1402             : /*
    1403             :  * Extent tree manipulation functions used during allocation.
    1404             :  */
    1405             : 
    1406             : /*
    1407             :  * Convert a delayed allocation to a real allocation.
    1408             :  */
    1409             : STATIC int                              /* error */
    1410    13186193 : xfs_bmap_add_extent_delay_real(
    1411             :         struct xfs_bmalloca     *bma,
    1412             :         int                     whichfork)
    1413             : {
    1414    13186193 :         struct xfs_mount        *mp = bma->ip->i_mount;
    1415    13186193 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    1416    13186193 :         struct xfs_bmbt_irec    *new = &bma->got;
    1417    13186193 :         int                     error;  /* error return value */
    1418    13186193 :         int                     i;      /* temp state */
    1419    13186193 :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    1420    13186193 :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    1421             :                                         /* left is 0, right is 1, prev is 2 */
    1422    13186193 :         int                     rval=0; /* return value (logging flags) */
    1423    13186193 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    1424    13186193 :         xfs_filblks_t           da_new; /* new count del alloc blocks used */
    1425    13186193 :         xfs_filblks_t           da_old; /* old count del alloc blocks used */
    1426    13186193 :         xfs_filblks_t           temp=0; /* value for da_new calculations */
    1427    13186193 :         int                     tmp_rval;       /* partial logging flags */
    1428    13186193 :         struct xfs_bmbt_irec    old;
    1429             : 
    1430    13186193 :         ASSERT(whichfork != XFS_ATTR_FORK);
    1431    13186193 :         ASSERT(!isnullstartblock(new->br_startblock));
    1432    13186193 :         ASSERT(!bma->cur ||
    1433             :                (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    1434             : 
    1435    13186193 :         XFS_STATS_INC(mp, xs_add_exlist);
    1436             : 
    1437             : #define LEFT            r[0]
    1438             : #define RIGHT           r[1]
    1439             : #define PREV            r[2]
    1440             : 
    1441             :         /*
    1442             :          * Set up a bunch of variables to make the tests simpler.
    1443             :          */
    1444    13186195 :         xfs_iext_get_extent(ifp, &bma->icur, &PREV);
    1445    13186193 :         new_endoff = new->br_startoff + new->br_blockcount;
    1446    13186193 :         ASSERT(isnullstartblock(PREV.br_startblock));
    1447    13186193 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    1448    13186193 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    1449             : 
    1450    13186193 :         da_old = startblockval(PREV.br_startblock);
    1451    13186193 :         da_new = 0;
    1452             : 
    1453             :         /*
    1454             :          * Set flags determining what part of the previous delayed allocation
    1455             :          * extent is being replaced by a real allocation.
    1456             :          */
    1457    13186193 :         if (PREV.br_startoff == new->br_startoff)
    1458    13186196 :                 state |= BMAP_LEFT_FILLING;
    1459    13186193 :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    1460    12979793 :                 state |= BMAP_RIGHT_FILLING;
    1461             : 
    1462             :         /*
    1463             :          * Check and set flags if this segment has a left neighbor.
    1464             :          * Don't set contiguous if the combined extent would be too large.
    1465             :          */
    1466    13186193 :         if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
    1467     8617871 :                 state |= BMAP_LEFT_VALID;
    1468     8617871 :                 if (isnullstartblock(LEFT.br_startblock))
    1469       44402 :                         state |= BMAP_LEFT_DELAY;
    1470             :         }
    1471             : 
    1472    13186184 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    1473     8573473 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    1474      849914 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    1475      225302 :             LEFT.br_state == new->br_state &&
    1476      111936 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    1477      107692 :                 state |= BMAP_LEFT_CONTIG;
    1478             : 
    1479             :         /*
    1480             :          * Check and set flags if this segment has a right neighbor.
    1481             :          * Don't set contiguous if the combined extent would be too large.
    1482             :          * Also check for all-three-contiguous being too large.
    1483             :          */
    1484    13186184 :         if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
    1485     6207296 :                 state |= BMAP_RIGHT_VALID;
    1486     6207296 :                 if (isnullstartblock(RIGHT.br_startblock))
    1487      774919 :                         state |= BMAP_RIGHT_DELAY;
    1488             :         }
    1489             : 
    1490    13186198 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    1491     5432378 :             new_endoff == RIGHT.br_startoff &&
    1492      566462 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    1493       61838 :             new->br_state == RIGHT.br_state &&
    1494       30724 :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    1495       30724 :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1496             :                        BMAP_RIGHT_FILLING)) !=
    1497             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1498       12738 :                        BMAP_RIGHT_FILLING) ||
    1499       12738 :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    1500             :                         <= XFS_MAX_BMBT_EXTLEN))
    1501       30724 :                 state |= BMAP_RIGHT_CONTIG;
    1502             : 
    1503    13186198 :         error = 0;
    1504             :         /*
    1505             :          * Switch out based on the FILLING and CONTIG state bits.
    1506             :          */
    1507    13186198 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1508             :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    1509       12738 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1510             :              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1511             :                 /*
    1512             :                  * Filling in all of a previously delayed allocation extent.
    1513             :                  * The left and right neighbors are both contiguous with new.
    1514             :                  */
    1515       12738 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    1516             : 
    1517       12738 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1518       12738 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1519       12738 :                 xfs_iext_prev(ifp, &bma->icur);
    1520       12738 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1521       12738 :                 ifp->if_nextents--;
    1522             : 
    1523       12738 :                 if (bma->cur == NULL)
    1524             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1525             :                 else {
    1526         269 :                         rval = XFS_ILOG_CORE;
    1527         269 :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1528         269 :                         if (error)
    1529           0 :                                 goto done;
    1530         269 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1531           0 :                                 error = -EFSCORRUPTED;
    1532           0 :                                 goto done;
    1533             :                         }
    1534         269 :                         error = xfs_btree_delete(bma->cur, &i);
    1535         269 :                         if (error)
    1536           0 :                                 goto done;
    1537         269 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1538           0 :                                 error = -EFSCORRUPTED;
    1539           0 :                                 goto done;
    1540             :                         }
    1541         269 :                         error = xfs_btree_decrement(bma->cur, 0, &i);
    1542         269 :                         if (error)
    1543           0 :                                 goto done;
    1544         269 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1545           0 :                                 error = -EFSCORRUPTED;
    1546           0 :                                 goto done;
    1547             :                         }
    1548         269 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1549         269 :                         if (error)
    1550           0 :                                 goto done;
    1551             :                 }
    1552             :                 break;
    1553             : 
    1554       82164 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    1555             :                 /*
    1556             :                  * Filling in all of a previously delayed allocation extent.
    1557             :                  * The left neighbor is contiguous, the right is not.
    1558             :                  */
    1559       82164 :                 old = LEFT;
    1560       82164 :                 LEFT.br_blockcount += PREV.br_blockcount;
    1561             : 
    1562       82164 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1563       82164 :                 xfs_iext_prev(ifp, &bma->icur);
    1564       82164 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1565             : 
    1566       82164 :                 if (bma->cur == NULL)
    1567             :                         rval = XFS_ILOG_DEXT;
    1568             :                 else {
    1569        1423 :                         rval = 0;
    1570        1423 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1571        1423 :                         if (error)
    1572           0 :                                 goto done;
    1573        1423 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1574           0 :                                 error = -EFSCORRUPTED;
    1575           0 :                                 goto done;
    1576             :                         }
    1577        1423 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1578        1423 :                         if (error)
    1579           0 :                                 goto done;
    1580             :                 }
    1581             :                 break;
    1582             : 
    1583       17986 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1584             :                 /*
    1585             :                  * Filling in all of a previously delayed allocation extent.
    1586             :                  * The right neighbor is contiguous, the left is not. Take care
    1587             :                  * with delay -> unwritten extent allocation here because the
    1588             :                  * delalloc record we are overwriting is always written.
    1589             :                  */
    1590       17986 :                 PREV.br_startblock = new->br_startblock;
    1591       17986 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    1592       17986 :                 PREV.br_state = new->br_state;
    1593             : 
    1594       17986 :                 xfs_iext_next(ifp, &bma->icur);
    1595       17986 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1596       17986 :                 xfs_iext_prev(ifp, &bma->icur);
    1597       17986 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1598             : 
    1599       17986 :                 if (bma->cur == NULL)
    1600             :                         rval = XFS_ILOG_DEXT;
    1601             :                 else {
    1602         192 :                         rval = 0;
    1603         192 :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1604         192 :                         if (error)
    1605           0 :                                 goto done;
    1606         192 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1607           0 :                                 error = -EFSCORRUPTED;
    1608           0 :                                 goto done;
    1609             :                         }
    1610         192 :                         error = xfs_bmbt_update(bma->cur, &PREV);
    1611         192 :                         if (error)
    1612           0 :                                 goto done;
    1613             :                 }
    1614             :                 break;
    1615             : 
    1616    12866908 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    1617             :                 /*
    1618             :                  * Filling in all of a previously delayed allocation extent.
    1619             :                  * Neither the left nor right neighbors are contiguous with
    1620             :                  * the new one.
    1621             :                  */
    1622    12866908 :                 PREV.br_startblock = new->br_startblock;
    1623    12866908 :                 PREV.br_state = new->br_state;
    1624    12866908 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1625    12866901 :                 ifp->if_nextents++;
    1626             : 
    1627    12866901 :                 if (bma->cur == NULL)
    1628             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1629             :                 else {
    1630      954634 :                         rval = XFS_ILOG_CORE;
    1631      954634 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1632      954634 :                         if (error)
    1633           0 :                                 goto done;
    1634      954634 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1635           0 :                                 error = -EFSCORRUPTED;
    1636           0 :                                 goto done;
    1637             :                         }
    1638      954634 :                         error = xfs_btree_insert(bma->cur, &i);
    1639      954633 :                         if (error)
    1640           0 :                                 goto done;
    1641      954633 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1642           0 :                                 error = -EFSCORRUPTED;
    1643           0 :                                 goto done;
    1644             :                         }
    1645             :                 }
    1646             :                 break;
    1647             : 
    1648       12790 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
    1649             :                 /*
    1650             :                  * Filling in the first part of a previous delayed allocation.
    1651             :                  * The left neighbor is contiguous.
    1652             :                  */
    1653       12790 :                 old = LEFT;
    1654       12790 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1655       12790 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1656             :                                 startblockval(PREV.br_startblock));
    1657             : 
    1658       12790 :                 LEFT.br_blockcount += new->br_blockcount;
    1659             : 
    1660       12790 :                 PREV.br_blockcount = temp;
    1661       12790 :                 PREV.br_startoff += new->br_blockcount;
    1662       12790 :                 PREV.br_startblock = nullstartblock(da_new);
    1663             : 
    1664       12790 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1665       12790 :                 xfs_iext_prev(ifp, &bma->icur);
    1666       12790 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1667             : 
    1668       12790 :                 if (bma->cur == NULL)
    1669             :                         rval = XFS_ILOG_DEXT;
    1670             :                 else {
    1671        8936 :                         rval = 0;
    1672        8936 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1673        8936 :                         if (error)
    1674           0 :                                 goto done;
    1675        8936 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1676           0 :                                 error = -EFSCORRUPTED;
    1677           0 :                                 goto done;
    1678             :                         }
    1679        8936 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1680        8936 :                         if (error)
    1681           0 :                                 goto done;
    1682             :                 }
    1683             :                 break;
    1684             : 
    1685      193612 :         case BMAP_LEFT_FILLING:
    1686             :                 /*
    1687             :                  * Filling in the first part of a previous delayed allocation.
    1688             :                  * The left neighbor is not contiguous.
    1689             :                  */
    1690      193612 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1691      193612 :                 ifp->if_nextents++;
    1692             : 
    1693      193612 :                 if (bma->cur == NULL)
    1694             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1695             :                 else {
    1696      149796 :                         rval = XFS_ILOG_CORE;
    1697      149796 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1698      149795 :                         if (error)
    1699           0 :                                 goto done;
    1700      149795 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1701           0 :                                 error = -EFSCORRUPTED;
    1702           0 :                                 goto done;
    1703             :                         }
    1704      149795 :                         error = xfs_btree_insert(bma->cur, &i);
    1705      149794 :                         if (error)
    1706           0 :                                 goto done;
    1707      149794 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1708           0 :                                 error = -EFSCORRUPTED;
    1709           0 :                                 goto done;
    1710             :                         }
    1711             :                 }
    1712             : 
    1713      193610 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1714        1549 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1715             :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1716        1549 :                         rval |= tmp_rval;
    1717        1549 :                         if (error)
    1718           0 :                                 goto done;
    1719             :                 }
    1720             : 
    1721      193611 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1722      193611 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1723             :                         startblockval(PREV.br_startblock) -
    1724             :                         (bma->cur ? bma->cur->bc_ino.allocated : 0));
    1725             : 
    1726      193612 :                 PREV.br_startoff = new_endoff;
    1727      193612 :                 PREV.br_blockcount = temp;
    1728      193612 :                 PREV.br_startblock = nullstartblock(da_new);
    1729      193611 :                 xfs_iext_next(ifp, &bma->icur);
    1730      193609 :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1731      193610 :                 xfs_iext_prev(ifp, &bma->icur);
    1732      193610 :                 break;
    1733             : 
    1734           0 :         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1735             :                 /*
    1736             :                  * Filling in the last part of a previous delayed allocation.
    1737             :                  * The right neighbor is contiguous with the new allocation.
    1738             :                  */
    1739           0 :                 old = RIGHT;
    1740           0 :                 RIGHT.br_startoff = new->br_startoff;
    1741           0 :                 RIGHT.br_startblock = new->br_startblock;
    1742           0 :                 RIGHT.br_blockcount += new->br_blockcount;
    1743             : 
    1744           0 :                 if (bma->cur == NULL)
    1745             :                         rval = XFS_ILOG_DEXT;
    1746             :                 else {
    1747           0 :                         rval = 0;
    1748           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1749           0 :                         if (error)
    1750           0 :                                 goto done;
    1751           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1752           0 :                                 error = -EFSCORRUPTED;
    1753           0 :                                 goto done;
    1754             :                         }
    1755           0 :                         error = xfs_bmbt_update(bma->cur, &RIGHT);
    1756           0 :                         if (error)
    1757           0 :                                 goto done;
    1758             :                 }
    1759             : 
    1760           0 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1761           0 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1762             :                         startblockval(PREV.br_startblock));
    1763             : 
    1764           0 :                 PREV.br_blockcount = temp;
    1765           0 :                 PREV.br_startblock = nullstartblock(da_new);
    1766             : 
    1767           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1768           0 :                 xfs_iext_next(ifp, &bma->icur);
    1769           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
    1770           0 :                 break;
    1771             : 
    1772           0 :         case BMAP_RIGHT_FILLING:
    1773             :                 /*
    1774             :                  * Filling in the last part of a previous delayed allocation.
    1775             :                  * The right neighbor is not contiguous.
    1776             :                  */
    1777           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1778           0 :                 ifp->if_nextents++;
    1779             : 
    1780           0 :                 if (bma->cur == NULL)
    1781             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1782             :                 else {
    1783           0 :                         rval = XFS_ILOG_CORE;
    1784           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1785           0 :                         if (error)
    1786           0 :                                 goto done;
    1787           0 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1788           0 :                                 error = -EFSCORRUPTED;
    1789           0 :                                 goto done;
    1790             :                         }
    1791           0 :                         error = xfs_btree_insert(bma->cur, &i);
    1792           0 :                         if (error)
    1793           0 :                                 goto done;
    1794           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1795           0 :                                 error = -EFSCORRUPTED;
    1796           0 :                                 goto done;
    1797             :                         }
    1798             :                 }
    1799             : 
    1800           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1801           0 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1802             :                                 &bma->cur, 1, &tmp_rval, whichfork);
    1803           0 :                         rval |= tmp_rval;
    1804           0 :                         if (error)
    1805           0 :                                 goto done;
    1806             :                 }
    1807             : 
    1808           0 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1809           0 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1810             :                         startblockval(PREV.br_startblock) -
    1811             :                         (bma->cur ? bma->cur->bc_ino.allocated : 0));
    1812             : 
    1813           0 :                 PREV.br_startblock = nullstartblock(da_new);
    1814           0 :                 PREV.br_blockcount = temp;
    1815           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1816           0 :                 xfs_iext_next(ifp, &bma->icur);
    1817           0 :                 break;
    1818             : 
    1819           0 :         case 0:
    1820             :                 /*
    1821             :                  * Filling in the middle part of a previous delayed allocation.
    1822             :                  * Contiguity is impossible here.
    1823             :                  * This case is avoided almost all the time.
    1824             :                  *
    1825             :                  * We start with a delayed allocation:
    1826             :                  *
    1827             :                  * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
    1828             :                  *  PREV @ idx
    1829             :                  *
    1830             :                  * and we are allocating:
    1831             :                  *                     +rrrrrrrrrrrrrrrrr+
    1832             :                  *                            new
    1833             :                  *
    1834             :                  * and we set it up for insertion as:
    1835             :                  * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
    1836             :                  *                            new
    1837             :                  *  PREV @ idx          LEFT              RIGHT
    1838             :                  *                      inserted at idx + 1
    1839             :                  */
    1840           0 :                 old = PREV;
    1841             : 
    1842             :                 /* LEFT is the new middle */
    1843           0 :                 LEFT = *new;
    1844             : 
    1845             :                 /* RIGHT is the new right */
    1846           0 :                 RIGHT.br_state = PREV.br_state;
    1847           0 :                 RIGHT.br_startoff = new_endoff;
    1848           0 :                 RIGHT.br_blockcount =
    1849           0 :                         PREV.br_startoff + PREV.br_blockcount - new_endoff;
    1850           0 :                 RIGHT.br_startblock =
    1851           0 :                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
    1852             :                                         RIGHT.br_blockcount));
    1853             : 
    1854             :                 /* truncate PREV */
    1855           0 :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    1856           0 :                 PREV.br_startblock =
    1857           0 :                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
    1858             :                                         PREV.br_blockcount));
    1859           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1860             : 
    1861           0 :                 xfs_iext_next(ifp, &bma->icur);
    1862           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
    1863           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
    1864           0 :                 ifp->if_nextents++;
    1865             : 
    1866           0 :                 if (bma->cur == NULL)
    1867             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1868             :                 else {
    1869           0 :                         rval = XFS_ILOG_CORE;
    1870           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1871           0 :                         if (error)
    1872           0 :                                 goto done;
    1873           0 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1874           0 :                                 error = -EFSCORRUPTED;
    1875           0 :                                 goto done;
    1876             :                         }
    1877           0 :                         error = xfs_btree_insert(bma->cur, &i);
    1878           0 :                         if (error)
    1879           0 :                                 goto done;
    1880           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1881           0 :                                 error = -EFSCORRUPTED;
    1882           0 :                                 goto done;
    1883             :                         }
    1884             :                 }
    1885             : 
    1886           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1887           0 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1888             :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1889           0 :                         rval |= tmp_rval;
    1890           0 :                         if (error)
    1891           0 :                                 goto done;
    1892             :                 }
    1893             : 
    1894           0 :                 da_new = startblockval(PREV.br_startblock) +
    1895           0 :                          startblockval(RIGHT.br_startblock);
    1896           0 :                 break;
    1897             : 
    1898           0 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1899             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1900             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
    1901             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    1902             :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1903             :         case BMAP_LEFT_CONTIG:
    1904             :         case BMAP_RIGHT_CONTIG:
    1905             :                 /*
    1906             :                  * These cases are all impossible.
    1907             :                  */
    1908           0 :                 ASSERT(0);
    1909             :         }
    1910             : 
    1911             :         /* add reverse mapping unless caller opted out */
    1912    13186188 :         if (!(bma->flags & XFS_BMAPI_NORMAP))
    1913    13186164 :                 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
    1914             : 
    1915             :         /* convert to a btree if necessary */
    1916    13186217 :         if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1917       95670 :                 int     tmp_logflags;   /* partial log flag return val */
    1918             : 
    1919       95670 :                 ASSERT(bma->cur == NULL);
    1920       95670 :                 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1921             :                                 &bma->cur, da_old > 0, &tmp_logflags,
    1922             :                                 whichfork);
    1923       95670 :                 bma->logflags |= tmp_logflags;
    1924       95670 :                 if (error)
    1925          18 :                         goto done;
    1926             :         }
    1927             : 
    1928    13186175 :         if (da_new != da_old)
    1929    12981929 :                 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
    1930             : 
    1931    13186172 :         if (bma->cur) {
    1932     1212446 :                 da_new += bma->cur->bc_ino.allocated;
    1933     1212446 :                 bma->cur->bc_ino.allocated = 0;
    1934             :         }
    1935             : 
    1936             :         /* adjust for changes in reserved delayed indirect blocks */
    1937    13186172 :         if (da_new != da_old) {
    1938    12980122 :                 ASSERT(state == 0 || da_new < da_old);
    1939    12980122 :                 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
    1940             :                                 false);
    1941             :         }
    1942             : 
    1943    13186174 :         xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
    1944    13186186 : done:
    1945    13186186 :         if (whichfork != XFS_COW_FORK)
    1946    13059185 :                 bma->logflags |= rval;
    1947    13186186 :         return error;
    1948             : #undef  LEFT
    1949             : #undef  RIGHT
    1950             : #undef  PREV
    1951             : }
    1952             : 
    1953             : /*
    1954             :  * Convert an unwritten allocation to a real allocation or vice versa.
    1955             :  */
    1956             : int                                     /* error */
    1957    22556668 : xfs_bmap_add_extent_unwritten_real(
    1958             :         struct xfs_trans        *tp,
    1959             :         xfs_inode_t             *ip,    /* incore inode pointer */
    1960             :         int                     whichfork,
    1961             :         struct xfs_iext_cursor  *icur,
    1962             :         struct xfs_btree_cur    **curp, /* if *curp is null, not a btree */
    1963             :         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
    1964             :         int                     *logflagsp) /* inode logging flags */
    1965             : {
    1966    22556668 :         struct xfs_btree_cur    *cur;   /* btree cursor */
    1967    22556668 :         int                     error;  /* error return value */
    1968    22556668 :         int                     i;      /* temp state */
    1969    22556668 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    1970    22556668 :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    1971    22556668 :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    1972             :                                         /* left is 0, right is 1, prev is 2 */
    1973    22556668 :         int                     rval=0; /* return value (logging flags) */
    1974    22556668 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    1975    22556668 :         struct xfs_mount        *mp = ip->i_mount;
    1976    22556668 :         struct xfs_bmbt_irec    old;
    1977             : 
    1978    22556668 :         *logflagsp = 0;
    1979             : 
    1980    22556668 :         cur = *curp;
    1981    22556668 :         ifp = xfs_ifork_ptr(ip, whichfork);
    1982             : 
    1983    22556576 :         ASSERT(!isnullstartblock(new->br_startblock));
    1984             : 
    1985    22556576 :         XFS_STATS_INC(mp, xs_add_exlist);
    1986             : 
    1987             : #define LEFT            r[0]
    1988             : #define RIGHT           r[1]
    1989             : #define PREV            r[2]
    1990             : 
    1991             :         /*
    1992             :          * Set up a bunch of variables to make the tests simpler.
    1993             :          */
    1994    22556565 :         error = 0;
    1995    22556565 :         xfs_iext_get_extent(ifp, icur, &PREV);
    1996    22556479 :         ASSERT(new->br_state != PREV.br_state);
    1997    22556479 :         new_endoff = new->br_startoff + new->br_blockcount;
    1998    22556479 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    1999    22556479 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    2000             : 
    2001             :         /*
    2002             :          * Set flags determining what part of the previous oldext allocation
    2003             :          * extent is being replaced by a newext allocation.
    2004             :          */
    2005    22556479 :         if (PREV.br_startoff == new->br_startoff)
    2006    20934285 :                 state |= BMAP_LEFT_FILLING;
    2007    22556479 :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    2008    18922331 :                 state |= BMAP_RIGHT_FILLING;
    2009             : 
    2010             :         /*
    2011             :          * Check and set flags if this segment has a left neighbor.
    2012             :          * Don't set contiguous if the combined extent would be too large.
    2013             :          */
    2014    22556479 :         if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
    2015    16123699 :                 state |= BMAP_LEFT_VALID;
    2016    16123699 :                 if (isnullstartblock(LEFT.br_startblock))
    2017      111518 :                         state |= BMAP_LEFT_DELAY;
    2018             :         }
    2019             : 
    2020    22556628 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2021    16012187 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    2022     3116813 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    2023      823220 :             LEFT.br_state == new->br_state &&
    2024      818976 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2025      818975 :                 state |= BMAP_LEFT_CONTIG;
    2026             : 
    2027             :         /*
    2028             :          * Check and set flags if this segment has a right neighbor.
    2029             :          * Don't set contiguous if the combined extent would be too large.
    2030             :          * Also check for all-three-contiguous being too large.
    2031             :          */
    2032    22556628 :         if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
    2033    12304519 :                 state |= BMAP_RIGHT_VALID;
    2034    12304519 :                 if (isnullstartblock(RIGHT.br_startblock))
    2035      125007 :                         state |= BMAP_RIGHT_DELAY;
    2036             :         }
    2037             : 
    2038    22556650 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2039    12179528 :             new_endoff == RIGHT.br_startoff &&
    2040     2616911 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    2041      297260 :             new->br_state == RIGHT.br_state &&
    2042      297259 :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2043      297259 :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2044             :                        BMAP_RIGHT_FILLING)) !=
    2045             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2046       76237 :                        BMAP_RIGHT_FILLING) ||
    2047       76237 :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    2048             :                         <= XFS_MAX_BMBT_EXTLEN))
    2049      297259 :                 state |= BMAP_RIGHT_CONTIG;
    2050             : 
    2051             :         /*
    2052             :          * Switch out based on the FILLING and CONTIG state bits.
    2053             :          */
    2054    22556650 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2055             :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    2056       76237 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2057             :              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2058             :                 /*
    2059             :                  * Setting all of a previous oldext extent to newext.
    2060             :                  * The left and right neighbors are both contiguous with new.
    2061             :                  */
    2062       76237 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    2063             : 
    2064       76237 :                 xfs_iext_remove(ip, icur, state);
    2065       76237 :                 xfs_iext_remove(ip, icur, state);
    2066       76237 :                 xfs_iext_prev(ifp, icur);
    2067       76237 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2068       76237 :                 ifp->if_nextents -= 2;
    2069       76237 :                 if (cur == NULL)
    2070             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2071             :                 else {
    2072       20292 :                         rval = XFS_ILOG_CORE;
    2073       20292 :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2074       20292 :                         if (error)
    2075           0 :                                 goto done;
    2076       20292 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2077           0 :                                 error = -EFSCORRUPTED;
    2078           0 :                                 goto done;
    2079             :                         }
    2080       20292 :                         if ((error = xfs_btree_delete(cur, &i)))
    2081           0 :                                 goto done;
    2082       20292 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2083           0 :                                 error = -EFSCORRUPTED;
    2084           0 :                                 goto done;
    2085             :                         }
    2086       20292 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2087           0 :                                 goto done;
    2088       20292 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2089           0 :                                 error = -EFSCORRUPTED;
    2090           0 :                                 goto done;
    2091             :                         }
    2092       20292 :                         if ((error = xfs_btree_delete(cur, &i)))
    2093           0 :                                 goto done;
    2094       20292 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2095           0 :                                 error = -EFSCORRUPTED;
    2096           0 :                                 goto done;
    2097             :                         }
    2098       20292 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2099           0 :                                 goto done;
    2100       20292 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2101           0 :                                 error = -EFSCORRUPTED;
    2102           0 :                                 goto done;
    2103             :                         }
    2104       20292 :                         error = xfs_bmbt_update(cur, &LEFT);
    2105       20292 :                         if (error)
    2106           0 :                                 goto done;
    2107             :                 }
    2108             :                 break;
    2109             : 
    2110      492820 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    2111             :                 /*
    2112             :                  * Setting all of a previous oldext extent to newext.
    2113             :                  * The left neighbor is contiguous, the right is not.
    2114             :                  */
    2115      492820 :                 LEFT.br_blockcount += PREV.br_blockcount;
    2116             : 
    2117      492820 :                 xfs_iext_remove(ip, icur, state);
    2118      492820 :                 xfs_iext_prev(ifp, icur);
    2119      492820 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2120      492820 :                 ifp->if_nextents--;
    2121      492820 :                 if (cur == NULL)
    2122             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2123             :                 else {
    2124       89398 :                         rval = XFS_ILOG_CORE;
    2125       89398 :                         error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
    2126       89398 :                         if (error)
    2127           0 :                                 goto done;
    2128       89398 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2129           0 :                                 error = -EFSCORRUPTED;
    2130           0 :                                 goto done;
    2131             :                         }
    2132       89398 :                         if ((error = xfs_btree_delete(cur, &i)))
    2133           0 :                                 goto done;
    2134       89398 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2135           0 :                                 error = -EFSCORRUPTED;
    2136           0 :                                 goto done;
    2137             :                         }
    2138       89398 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2139           0 :                                 goto done;
    2140       89398 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2141           0 :                                 error = -EFSCORRUPTED;
    2142           0 :                                 goto done;
    2143             :                         }
    2144       89398 :                         error = xfs_bmbt_update(cur, &LEFT);
    2145       89398 :                         if (error)
    2146           0 :                                 goto done;
    2147             :                 }
    2148             :                 break;
    2149             : 
    2150      152827 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2151             :                 /*
    2152             :                  * Setting all of a previous oldext extent to newext.
    2153             :                  * The right neighbor is contiguous, the left is not.
    2154             :                  */
    2155      152827 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    2156      152827 :                 PREV.br_state = new->br_state;
    2157             : 
    2158      152827 :                 xfs_iext_next(ifp, icur);
    2159      152827 :                 xfs_iext_remove(ip, icur, state);
    2160      152827 :                 xfs_iext_prev(ifp, icur);
    2161      152827 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2162      152827 :                 ifp->if_nextents--;
    2163             : 
    2164      152827 :                 if (cur == NULL)
    2165             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2166             :                 else {
    2167        5700 :                         rval = XFS_ILOG_CORE;
    2168        5700 :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2169        5700 :                         if (error)
    2170           0 :                                 goto done;
    2171        5700 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2172           0 :                                 error = -EFSCORRUPTED;
    2173           0 :                                 goto done;
    2174             :                         }
    2175        5700 :                         if ((error = xfs_btree_delete(cur, &i)))
    2176           0 :                                 goto done;
    2177        5700 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2178           0 :                                 error = -EFSCORRUPTED;
    2179           0 :                                 goto done;
    2180             :                         }
    2181        5700 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2182           0 :                                 goto done;
    2183        5700 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2184           0 :                                 error = -EFSCORRUPTED;
    2185           0 :                                 goto done;
    2186             :                         }
    2187        5700 :                         error = xfs_bmbt_update(cur, &PREV);
    2188        5700 :                         if (error)
    2189           0 :                                 goto done;
    2190             :                 }
    2191             :                 break;
    2192             : 
    2193    17624208 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    2194             :                 /*
    2195             :                  * Setting all of a previous oldext extent to newext.
    2196             :                  * Neither the left nor right neighbors are contiguous with
    2197             :                  * the new one.
    2198             :                  */
    2199    17624208 :                 PREV.br_state = new->br_state;
    2200    17624208 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2201             : 
    2202    17623930 :                 if (cur == NULL)
    2203             :                         rval = XFS_ILOG_DEXT;
    2204             :                 else {
    2205     2443348 :                         rval = 0;
    2206     2443348 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2207     2443347 :                         if (error)
    2208           0 :                                 goto done;
    2209     2443347 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2210           0 :                                 error = -EFSCORRUPTED;
    2211           0 :                                 goto done;
    2212             :                         }
    2213     2443347 :                         error = xfs_bmbt_update(cur, &PREV);
    2214     2443345 :                         if (error)
    2215           0 :                                 goto done;
    2216             :                 }
    2217             :                 break;
    2218             : 
    2219      249918 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
    2220             :                 /*
    2221             :                  * Setting the first part of a previous oldext extent to newext.
    2222             :                  * The left neighbor is contiguous.
    2223             :                  */
    2224      249918 :                 LEFT.br_blockcount += new->br_blockcount;
    2225             : 
    2226      249918 :                 old = PREV;
    2227      249918 :                 PREV.br_startoff += new->br_blockcount;
    2228      249918 :                 PREV.br_startblock += new->br_blockcount;
    2229      249918 :                 PREV.br_blockcount -= new->br_blockcount;
    2230             : 
    2231      249918 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2232      249918 :                 xfs_iext_prev(ifp, icur);
    2233      249918 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2234             : 
    2235      249918 :                 if (cur == NULL)
    2236             :                         rval = XFS_ILOG_DEXT;
    2237             :                 else {
    2238       55283 :                         rval = 0;
    2239       55283 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2240       55283 :                         if (error)
    2241           0 :                                 goto done;
    2242       55283 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2243           0 :                                 error = -EFSCORRUPTED;
    2244           0 :                                 goto done;
    2245             :                         }
    2246       55283 :                         error = xfs_bmbt_update(cur, &PREV);
    2247       55283 :                         if (error)
    2248           0 :                                 goto done;
    2249       55283 :                         error = xfs_btree_decrement(cur, 0, &i);
    2250       55283 :                         if (error)
    2251           0 :                                 goto done;
    2252       55283 :                         error = xfs_bmbt_update(cur, &LEFT);
    2253       55283 :                         if (error)
    2254           0 :                                 goto done;
    2255             :                 }
    2256             :                 break;
    2257             : 
    2258     2338299 :         case BMAP_LEFT_FILLING:
    2259             :                 /*
    2260             :                  * Setting the first part of a previous oldext extent to newext.
    2261             :                  * The left neighbor is not contiguous.
    2262             :                  */
    2263     2338299 :                 old = PREV;
    2264     2338299 :                 PREV.br_startoff += new->br_blockcount;
    2265     2338299 :                 PREV.br_startblock += new->br_blockcount;
    2266     2338299 :                 PREV.br_blockcount -= new->br_blockcount;
    2267             : 
    2268     2338299 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2269     2338300 :                 xfs_iext_insert(ip, icur, new, state);
    2270     2338300 :                 ifp->if_nextents++;
    2271             : 
    2272     2338300 :                 if (cur == NULL)
    2273             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2274             :                 else {
    2275      111603 :                         rval = XFS_ILOG_CORE;
    2276      111603 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2277      111603 :                         if (error)
    2278           0 :                                 goto done;
    2279      111603 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2280           0 :                                 error = -EFSCORRUPTED;
    2281           0 :                                 goto done;
    2282             :                         }
    2283      111603 :                         error = xfs_bmbt_update(cur, &PREV);
    2284      111603 :                         if (error)
    2285           0 :                                 goto done;
    2286      111603 :                         cur->bc_rec.b = *new;
    2287      111603 :                         if ((error = xfs_btree_insert(cur, &i)))
    2288           0 :                                 goto done;
    2289      111603 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2290           0 :                                 error = -EFSCORRUPTED;
    2291           0 :                                 goto done;
    2292             :                         }
    2293             :                 }
    2294             :                 break;
    2295             : 
    2296       68195 :         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2297             :                 /*
    2298             :                  * Setting the last part of a previous oldext extent to newext.
    2299             :                  * The right neighbor is contiguous with the new allocation.
    2300             :                  */
    2301       68195 :                 old = PREV;
    2302       68195 :                 PREV.br_blockcount -= new->br_blockcount;
    2303             : 
    2304       68195 :                 RIGHT.br_startoff = new->br_startoff;
    2305       68195 :                 RIGHT.br_startblock = new->br_startblock;
    2306       68195 :                 RIGHT.br_blockcount += new->br_blockcount;
    2307             : 
    2308       68195 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2309       68195 :                 xfs_iext_next(ifp, icur);
    2310       68195 :                 xfs_iext_update_extent(ip, state, icur, &RIGHT);
    2311             : 
    2312       68195 :                 if (cur == NULL)
    2313             :                         rval = XFS_ILOG_DEXT;
    2314             :                 else {
    2315       29401 :                         rval = 0;
    2316       29401 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2317       29401 :                         if (error)
    2318           0 :                                 goto done;
    2319       29401 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2320           0 :                                 error = -EFSCORRUPTED;
    2321           0 :                                 goto done;
    2322             :                         }
    2323       29401 :                         error = xfs_bmbt_update(cur, &PREV);
    2324       29401 :                         if (error)
    2325           0 :                                 goto done;
    2326       29401 :                         error = xfs_btree_increment(cur, 0, &i);
    2327       29401 :                         if (error)
    2328           0 :                                 goto done;
    2329       29401 :                         error = xfs_bmbt_update(cur, &RIGHT);
    2330       29401 :                         if (error)
    2331           0 :                                 goto done;
    2332             :                 }
    2333             :                 break;
    2334             : 
    2335      508040 :         case BMAP_RIGHT_FILLING:
    2336             :                 /*
    2337             :                  * Setting the last part of a previous oldext extent to newext.
    2338             :                  * The right neighbor is not contiguous.
    2339             :                  */
    2340      508040 :                 old = PREV;
    2341      508040 :                 PREV.br_blockcount -= new->br_blockcount;
    2342             : 
    2343      508040 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2344      508040 :                 xfs_iext_next(ifp, icur);
    2345      508040 :                 xfs_iext_insert(ip, icur, new, state);
    2346      508040 :                 ifp->if_nextents++;
    2347             : 
    2348      508040 :                 if (cur == NULL)
    2349             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2350             :                 else {
    2351       37327 :                         rval = XFS_ILOG_CORE;
    2352       37327 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2353       37327 :                         if (error)
    2354           0 :                                 goto done;
    2355       37327 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2356           0 :                                 error = -EFSCORRUPTED;
    2357           0 :                                 goto done;
    2358             :                         }
    2359       37327 :                         error = xfs_bmbt_update(cur, &PREV);
    2360       37327 :                         if (error)
    2361           0 :                                 goto done;
    2362       37327 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2363       37327 :                         if (error)
    2364           0 :                                 goto done;
    2365       37327 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2366           0 :                                 error = -EFSCORRUPTED;
    2367           0 :                                 goto done;
    2368             :                         }
    2369       37327 :                         if ((error = xfs_btree_insert(cur, &i)))
    2370           0 :                                 goto done;
    2371       37327 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2372           0 :                                 error = -EFSCORRUPTED;
    2373           0 :                                 goto done;
    2374             :                         }
    2375             :                 }
    2376             :                 break;
    2377             : 
    2378     1046106 :         case 0:
    2379             :                 /*
    2380             :                  * Setting the middle part of a previous oldext extent to
    2381             :                  * newext.  Contiguity is impossible here.
    2382             :                  * One extent becomes three extents.
    2383             :                  */
    2384     1046106 :                 old = PREV;
    2385     1046106 :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    2386             : 
    2387     1046106 :                 r[0] = *new;
    2388     1046106 :                 r[1].br_startoff = new_endoff;
    2389     1046106 :                 r[1].br_blockcount =
    2390     1046106 :                         old.br_startoff + old.br_blockcount - new_endoff;
    2391     1046106 :                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
    2392     1046106 :                 r[1].br_state = PREV.br_state;
    2393             : 
    2394     1046106 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2395     1046105 :                 xfs_iext_next(ifp, icur);
    2396     1046101 :                 xfs_iext_insert(ip, icur, &r[1], state);
    2397     1046089 :                 xfs_iext_insert(ip, icur, &r[0], state);
    2398     1046080 :                 ifp->if_nextents += 2;
    2399             : 
    2400     1046080 :                 if (cur == NULL)
    2401             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2402             :                 else {
    2403      134729 :                         rval = XFS_ILOG_CORE;
    2404      134729 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2405      134729 :                         if (error)
    2406           0 :                                 goto done;
    2407      134729 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2408           0 :                                 error = -EFSCORRUPTED;
    2409           0 :                                 goto done;
    2410             :                         }
    2411             :                         /* new right extent - oldext */
    2412      134729 :                         error = xfs_bmbt_update(cur, &r[1]);
    2413      134729 :                         if (error)
    2414           0 :                                 goto done;
    2415             :                         /* new left extent - oldext */
    2416      134729 :                         cur->bc_rec.b = PREV;
    2417      134729 :                         if ((error = xfs_btree_insert(cur, &i)))
    2418           0 :                                 goto done;
    2419      134729 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2420           0 :                                 error = -EFSCORRUPTED;
    2421           0 :                                 goto done;
    2422             :                         }
    2423             :                         /*
    2424             :                          * Reset the cursor to the position of the new extent
    2425             :                          * we are about to insert as we can't trust it after
    2426             :                          * the previous insert.
    2427             :                          */
    2428      134729 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2429      134729 :                         if (error)
    2430           0 :                                 goto done;
    2431      134729 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2432           0 :                                 error = -EFSCORRUPTED;
    2433           0 :                                 goto done;
    2434             :                         }
    2435             :                         /* new middle extent - newext */
    2436      134729 :                         if ((error = xfs_btree_insert(cur, &i)))
    2437           0 :                                 goto done;
    2438      134729 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2439           0 :                                 error = -EFSCORRUPTED;
    2440           0 :                                 goto done;
    2441             :                         }
    2442             :                 }
    2443             :                 break;
    2444             : 
    2445           0 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2446             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2447             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
    2448             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    2449             :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2450             :         case BMAP_LEFT_CONTIG:
    2451             :         case BMAP_RIGHT_CONTIG:
    2452             :                 /*
    2453             :                  * These cases are all impossible.
    2454             :                  */
    2455           0 :                 ASSERT(0);
    2456             :         }
    2457             : 
    2458             :         /* update reverse mappings */
    2459    22556344 :         xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
    2460             : 
    2461             :         /* convert to a btree if necessary */
    2462    22556423 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2463       26087 :                 int     tmp_logflags;   /* partial log flag return val */
    2464             : 
    2465       26087 :                 ASSERT(cur == NULL);
    2466       26087 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    2467             :                                 &tmp_logflags, whichfork);
    2468       26087 :                 *logflagsp |= tmp_logflags;
    2469       26087 :                 if (error)
    2470           3 :                         goto done;
    2471             :         }
    2472             : 
    2473             :         /* clear out the allocated field, done with it now in any case. */
    2474    22556404 :         if (cur) {
    2475     2953164 :                 cur->bc_ino.allocated = 0;
    2476     2953164 :                 *curp = cur;
    2477             :         }
    2478             : 
    2479    22556404 :         xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
    2480    22556326 : done:
    2481    22556326 :         *logflagsp |= rval;
    2482    22556326 :         return error;
    2483             : #undef  LEFT
    2484             : #undef  RIGHT
    2485             : #undef  PREV
    2486             : }
    2487             : 
    2488             : /*
    2489             :  * Convert a hole to a delayed allocation.
    2490             :  */
    2491             : STATIC void
    2492    15536363 : xfs_bmap_add_extent_hole_delay(
    2493             :         xfs_inode_t             *ip,    /* incore inode pointer */
    2494             :         int                     whichfork,
    2495             :         struct xfs_iext_cursor  *icur,
    2496             :         xfs_bmbt_irec_t         *new)   /* new data to add to file extents */
    2497             : {
    2498    15536363 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    2499    15536363 :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2500    15536363 :         xfs_filblks_t           newlen=0;       /* new indirect size */
    2501    15536363 :         xfs_filblks_t           oldlen=0;       /* old indirect size */
    2502    15536363 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2503    15536363 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2504    15536363 :         xfs_filblks_t           temp;    /* temp for indirect calculations */
    2505             : 
    2506    15536363 :         ifp = xfs_ifork_ptr(ip, whichfork);
    2507    15536513 :         ASSERT(isnullstartblock(new->br_startblock));
    2508             : 
    2509             :         /*
    2510             :          * Check and set flags if this segment has a left neighbor
    2511             :          */
    2512    15536513 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2513    10570357 :                 state |= BMAP_LEFT_VALID;
    2514    10570357 :                 if (isnullstartblock(left.br_startblock))
    2515     2461690 :                         state |= BMAP_LEFT_DELAY;
    2516             :         }
    2517             : 
    2518             :         /*
    2519             :          * Check and set flags if the current (right) segment exists.
    2520             :          * If it doesn't exist, we're converting the hole at end-of-file.
    2521             :          */
    2522    15536291 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2523     7112229 :                 state |= BMAP_RIGHT_VALID;
    2524     7112229 :                 if (isnullstartblock(right.br_startblock))
    2525     1412363 :                         state |= BMAP_RIGHT_DELAY;
    2526             :         }
    2527             : 
    2528             :         /*
    2529             :          * Set contiguity flags on the left and right neighbors.
    2530             :          * Don't let extents get too large, even if the pieces are contiguous.
    2531             :          */
    2532    15534174 :         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
    2533     2461691 :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2534     1836852 :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2535     1836850 :                 state |= BMAP_LEFT_CONTIG;
    2536             : 
    2537    15534174 :         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
    2538     1412363 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2539       89290 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2540       84994 :             (!(state & BMAP_LEFT_CONTIG) ||
    2541       18090 :              (left.br_blockcount + new->br_blockcount +
    2542             :               right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
    2543       84994 :                 state |= BMAP_RIGHT_CONTIG;
    2544             : 
    2545             :         /*
    2546             :          * Switch out based on the contiguity flags.
    2547             :          */
    2548    15534174 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2549       18090 :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2550             :                 /*
    2551             :                  * New allocation is contiguous with delayed allocations
    2552             :                  * on the left and on the right.
    2553             :                  * Merge all three into a single extent record.
    2554             :                  */
    2555       18090 :                 temp = left.br_blockcount + new->br_blockcount +
    2556       18090 :                         right.br_blockcount;
    2557             : 
    2558       18090 :                 oldlen = startblockval(left.br_startblock) +
    2559       18090 :                         startblockval(new->br_startblock) +
    2560       18090 :                         startblockval(right.br_startblock);
    2561       18090 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2562             :                                          oldlen);
    2563       18090 :                 left.br_startblock = nullstartblock(newlen);
    2564       18090 :                 left.br_blockcount = temp;
    2565             : 
    2566       18090 :                 xfs_iext_remove(ip, icur, state);
    2567       18090 :                 xfs_iext_prev(ifp, icur);
    2568       18090 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2569       18090 :                 break;
    2570             : 
    2571     1816539 :         case BMAP_LEFT_CONTIG:
    2572             :                 /*
    2573             :                  * New allocation is contiguous with a delayed allocation
    2574             :                  * on the left.
    2575             :                  * Merge the new allocation with the left neighbor.
    2576             :                  */
    2577     1816539 :                 temp = left.br_blockcount + new->br_blockcount;
    2578             : 
    2579     1816539 :                 oldlen = startblockval(left.br_startblock) +
    2580     1816539 :                         startblockval(new->br_startblock);
    2581     1816539 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2582             :                                          oldlen);
    2583     1818729 :                 left.br_blockcount = temp;
    2584     1818729 :                 left.br_startblock = nullstartblock(newlen);
    2585             : 
    2586     1818762 :                 xfs_iext_prev(ifp, icur);
    2587     1818761 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2588     1818761 :                 break;
    2589             : 
    2590       66904 :         case BMAP_RIGHT_CONTIG:
    2591             :                 /*
    2592             :                  * New allocation is contiguous with a delayed allocation
    2593             :                  * on the right.
    2594             :                  * Merge the new allocation with the right neighbor.
    2595             :                  */
    2596       66904 :                 temp = new->br_blockcount + right.br_blockcount;
    2597       66904 :                 oldlen = startblockval(new->br_startblock) +
    2598       66904 :                         startblockval(right.br_startblock);
    2599       66904 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2600             :                                          oldlen);
    2601       66904 :                 right.br_startoff = new->br_startoff;
    2602       66904 :                 right.br_startblock = nullstartblock(newlen);
    2603       66904 :                 right.br_blockcount = temp;
    2604       66904 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2605       66904 :                 break;
    2606             : 
    2607    13632641 :         case 0:
    2608             :                 /*
    2609             :                  * New allocation is not contiguous with another
    2610             :                  * delayed allocation.
    2611             :                  * Insert a new entry.
    2612             :                  */
    2613    13632641 :                 oldlen = newlen = 0;
    2614    13632641 :                 xfs_iext_insert(ip, icur, new, state);
    2615    13632641 :                 break;
    2616             :         }
    2617    15531099 :         if (oldlen != newlen) {
    2618     1903755 :                 ASSERT(oldlen > newlen);
    2619     1903755 :                 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
    2620             :                                  false);
    2621             :                 /*
    2622             :                  * Nothing to do for disk quota accounting here.
    2623             :                  */
    2624     1874909 :                 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
    2625             :         }
    2626    15531093 : }
    2627             : 
    2628             : /*
    2629             :  * Convert a hole to a real allocation.
    2630             :  */
    2631             : STATIC int                              /* error */
    2632    87590641 : xfs_bmap_add_extent_hole_real(
    2633             :         struct xfs_trans        *tp,
    2634             :         struct xfs_inode        *ip,
    2635             :         int                     whichfork,
    2636             :         struct xfs_iext_cursor  *icur,
    2637             :         struct xfs_btree_cur    **curp,
    2638             :         struct xfs_bmbt_irec    *new,
    2639             :         int                     *logflagsp,
    2640             :         uint32_t                flags)
    2641             : {
    2642    87590641 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    2643    87590764 :         struct xfs_mount        *mp = ip->i_mount;
    2644    87590764 :         struct xfs_btree_cur    *cur = *curp;
    2645    87590764 :         int                     error;  /* error return value */
    2646    87590764 :         int                     i;      /* temp state */
    2647    87590764 :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2648    87590764 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2649    87590764 :         int                     rval=0; /* return value (logging flags) */
    2650    87590764 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2651    87590764 :         struct xfs_bmbt_irec    old;
    2652             : 
    2653    87590764 :         ASSERT(!isnullstartblock(new->br_startblock));
    2654    87590764 :         ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    2655             : 
    2656    87590764 :         XFS_STATS_INC(mp, xs_add_exlist);
    2657             : 
    2658             :         /*
    2659             :          * Check and set flags if this segment has a left neighbor.
    2660             :          */
    2661    87591523 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2662    64134356 :                 state |= BMAP_LEFT_VALID;
    2663    64134356 :                 if (isnullstartblock(left.br_startblock))
    2664      128869 :                         state |= BMAP_LEFT_DELAY;
    2665             :         }
    2666             : 
    2667             :         /*
    2668             :          * Check and set flags if this segment has a current value.
    2669             :          * Not true if we're inserting into the "hole" at eof.
    2670             :          */
    2671    87591391 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2672    10816126 :                 state |= BMAP_RIGHT_VALID;
    2673    10816126 :                 if (isnullstartblock(right.br_startblock))
    2674      183945 :                         state |= BMAP_RIGHT_DELAY;
    2675             :         }
    2676             : 
    2677             :         /*
    2678             :          * We're inserting a real allocation between "left" and "right".
    2679             :          * Set the contiguity flags.  Don't let extents get too large.
    2680             :          */
    2681    87591060 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2682    64005542 :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2683    47788062 :             left.br_startblock + left.br_blockcount == new->br_startblock &&
    2684     5111435 :             left.br_state == new->br_state &&
    2685     4682702 :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2686     4682689 :                 state |= BMAP_LEFT_CONTIG;
    2687             : 
    2688    87591060 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2689    10632185 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2690     3681138 :             new->br_startblock + new->br_blockcount == right.br_startblock &&
    2691      649173 :             new->br_state == right.br_state &&
    2692      561359 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2693      561359 :             (!(state & BMAP_LEFT_CONTIG) ||
    2694      346765 :              left.br_blockcount + new->br_blockcount +
    2695             :              right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
    2696      561359 :                 state |= BMAP_RIGHT_CONTIG;
    2697             : 
    2698    87591060 :         error = 0;
    2699             :         /*
    2700             :          * Select which case we're in here, and implement it.
    2701             :          */
    2702    87591060 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2703      346765 :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2704             :                 /*
    2705             :                  * New allocation is contiguous with real allocations on the
    2706             :                  * left and on the right.
    2707             :                  * Merge all three into a single extent record.
    2708             :                  */
    2709      346765 :                 left.br_blockcount += new->br_blockcount + right.br_blockcount;
    2710             : 
    2711      346765 :                 xfs_iext_remove(ip, icur, state);
    2712      346765 :                 xfs_iext_prev(ifp, icur);
    2713      346765 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2714      346765 :                 ifp->if_nextents--;
    2715             : 
    2716      346765 :                 if (cur == NULL) {
    2717       67303 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2718             :                 } else {
    2719      279503 :                         rval = XFS_ILOG_CORE;
    2720      279503 :                         error = xfs_bmbt_lookup_eq(cur, &right, &i);
    2721      279503 :                         if (error)
    2722           0 :                                 goto done;
    2723      279503 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2724           0 :                                 error = -EFSCORRUPTED;
    2725           0 :                                 goto done;
    2726             :                         }
    2727      279503 :                         error = xfs_btree_delete(cur, &i);
    2728      279503 :                         if (error)
    2729           0 :                                 goto done;
    2730      279503 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2731           0 :                                 error = -EFSCORRUPTED;
    2732           0 :                                 goto done;
    2733             :                         }
    2734      279503 :                         error = xfs_btree_decrement(cur, 0, &i);
    2735      279503 :                         if (error)
    2736           0 :                                 goto done;
    2737      279503 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2738           0 :                                 error = -EFSCORRUPTED;
    2739           0 :                                 goto done;
    2740             :                         }
    2741      279503 :                         error = xfs_bmbt_update(cur, &left);
    2742      279503 :                         if (error)
    2743           0 :                                 goto done;
    2744             :                 }
    2745             :                 break;
    2746             : 
    2747     4335924 :         case BMAP_LEFT_CONTIG:
    2748             :                 /*
    2749             :                  * New allocation is contiguous with a real allocation
    2750             :                  * on the left.
    2751             :                  * Merge the new allocation with the left neighbor.
    2752             :                  */
    2753     4335924 :                 old = left;
    2754     4335924 :                 left.br_blockcount += new->br_blockcount;
    2755             : 
    2756     4335924 :                 xfs_iext_prev(ifp, icur);
    2757     4335924 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2758             : 
    2759     4335924 :                 if (cur == NULL) {
    2760     3683431 :                         rval = xfs_ilog_fext(whichfork);
    2761             :                 } else {
    2762      652493 :                         rval = 0;
    2763      652493 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2764      652493 :                         if (error)
    2765           0 :                                 goto done;
    2766      652493 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2767           0 :                                 error = -EFSCORRUPTED;
    2768           0 :                                 goto done;
    2769             :                         }
    2770      652493 :                         error = xfs_bmbt_update(cur, &left);
    2771      652493 :                         if (error)
    2772           0 :                                 goto done;
    2773             :                 }
    2774             :                 break;
    2775             : 
    2776      214594 :         case BMAP_RIGHT_CONTIG:
    2777             :                 /*
    2778             :                  * New allocation is contiguous with a real allocation
    2779             :                  * on the right.
    2780             :                  * Merge the new allocation with the right neighbor.
    2781             :                  */
    2782      214594 :                 old = right;
    2783             : 
    2784      214594 :                 right.br_startoff = new->br_startoff;
    2785      214594 :                 right.br_startblock = new->br_startblock;
    2786      214594 :                 right.br_blockcount += new->br_blockcount;
    2787      214594 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2788             : 
    2789      214594 :                 if (cur == NULL) {
    2790       81427 :                         rval = xfs_ilog_fext(whichfork);
    2791             :                 } else {
    2792      133167 :                         rval = 0;
    2793      133167 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2794      133167 :                         if (error)
    2795           0 :                                 goto done;
    2796      133167 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2797           0 :                                 error = -EFSCORRUPTED;
    2798           0 :                                 goto done;
    2799             :                         }
    2800      133167 :                         error = xfs_bmbt_update(cur, &right);
    2801      133167 :                         if (error)
    2802           0 :                                 goto done;
    2803             :                 }
    2804             :                 break;
    2805             : 
    2806    82693777 :         case 0:
    2807             :                 /*
    2808             :                  * New allocation is not contiguous with another
    2809             :                  * real allocation.
    2810             :                  * Insert a new entry.
    2811             :                  */
    2812    82693777 :                 xfs_iext_insert(ip, icur, new, state);
    2813    82692861 :                 ifp->if_nextents++;
    2814             : 
    2815    82692861 :                 if (cur == NULL) {
    2816    34094946 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2817             :                 } else {
    2818    51291727 :                         rval = XFS_ILOG_CORE;
    2819    51291727 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2820    51291687 :                         if (error)
    2821         976 :                                 goto done;
    2822    51290711 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2823           0 :                                 error = -EFSCORRUPTED;
    2824           0 :                                 goto done;
    2825             :                         }
    2826    51290711 :                         error = xfs_btree_insert(cur, &i);
    2827    51290724 :                         if (error)
    2828         153 :                                 goto done;
    2829    51290571 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2830           0 :                                 error = -EFSCORRUPTED;
    2831           0 :                                 goto done;
    2832             :                         }
    2833             :                 }
    2834             :                 break;
    2835             :         }
    2836             : 
    2837             :         /* add reverse mapping unless caller opted out */
    2838    87588988 :         if (!(flags & XFS_BMAPI_NORMAP))
    2839    87588908 :                 xfs_rmap_map_extent(tp, ip, whichfork, new);
    2840             : 
    2841             :         /* convert to a btree if necessary */
    2842    87590034 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2843      121523 :                 int     tmp_logflags;   /* partial log flag return val */
    2844             : 
    2845      121523 :                 ASSERT(cur == NULL);
    2846      121523 :                 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
    2847             :                                 &tmp_logflags, whichfork);
    2848      121523 :                 *logflagsp |= tmp_logflags;
    2849      121523 :                 cur = *curp;
    2850      121523 :                 if (error)
    2851           3 :                         goto done;
    2852             :         }
    2853             : 
    2854             :         /* clear out the allocated field, done with it now in any case. */
    2855    87589267 :         if (cur)
    2856    52477299 :                 cur->bc_ino.allocated = 0;
    2857             : 
    2858    87589267 :         xfs_bmap_check_leaf_extents(cur, ip, whichfork);
    2859    87589840 : done:
    2860    87589840 :         *logflagsp |= rval;
    2861    87589840 :         return error;
    2862             : }
    2863             : 
    2864             : /*
    2865             :  * Functions used in the extent read, allocate and remove paths
    2866             :  */
    2867             : 
    2868             : /*
    2869             :  * Adjust the size of the new extent based on i_extsize and rt extsize.
    2870             :  */
    2871             : int
    2872    10417302 : xfs_bmap_extsize_align(
    2873             :         xfs_mount_t     *mp,
    2874             :         xfs_bmbt_irec_t *gotp,          /* next extent pointer */
    2875             :         xfs_bmbt_irec_t *prevp,         /* previous extent pointer */
    2876             :         xfs_extlen_t    extsz,          /* align to this extent size */
    2877             :         int             rt,             /* is this a realtime inode? */
    2878             :         int             eof,            /* is extent at end-of-file? */
    2879             :         int             delay,          /* creating delalloc extent? */
    2880             :         int             convert,        /* overwriting unwritten extent? */
    2881             :         xfs_fileoff_t   *offp,          /* in/out: aligned offset */
    2882             :         xfs_extlen_t    *lenp)          /* in/out: aligned length */
    2883             : {
    2884    10417302 :         xfs_fileoff_t   orig_off;       /* original offset */
    2885    10417302 :         xfs_extlen_t    orig_alen;      /* original length */
    2886    10417302 :         xfs_fileoff_t   orig_end;       /* original off+len */
    2887    10417302 :         xfs_fileoff_t   nexto;          /* next file offset */
    2888    10417302 :         xfs_fileoff_t   prevo;          /* previous file offset */
    2889    10417302 :         xfs_fileoff_t   align_off;      /* temp for offset */
    2890    10417302 :         xfs_extlen_t    align_alen;     /* temp for length */
    2891    10417302 :         xfs_extlen_t    temp;           /* temp for calculations */
    2892             : 
    2893    10417302 :         if (convert)
    2894             :                 return 0;
    2895             : 
    2896    10417302 :         orig_off = align_off = *offp;
    2897    10417302 :         orig_alen = align_alen = *lenp;
    2898    10417302 :         orig_end = orig_off + orig_alen;
    2899             : 
    2900             :         /*
    2901             :          * If this request overlaps an existing extent, then don't
    2902             :          * attempt to perform any additional alignment.
    2903             :          */
    2904    10417302 :         if (!delay && !eof &&
    2905     3973034 :             (orig_off >= gotp->br_startoff) &&
    2906      127015 :             (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
    2907             :                 return 0;
    2908             :         }
    2909             : 
    2910             :         /*
    2911             :          * If the file offset is unaligned vs. the extent size
    2912             :          * we need to align it.  This will be possible unless
    2913             :          * the file was previously written with a kernel that didn't
    2914             :          * perform this alignment, or if a truncate shot us in the
    2915             :          * foot.
    2916             :          */
    2917    10290287 :         div_u64_rem(orig_off, extsz, &temp);
    2918    10290293 :         if (temp) {
    2919      296079 :                 align_alen += temp;
    2920      296079 :                 align_off -= temp;
    2921             :         }
    2922             : 
    2923             :         /* Same adjustment for the end of the requested area. */
    2924    10290293 :         temp = (align_alen % extsz);
    2925    10290293 :         if (temp)
    2926      327232 :                 align_alen += extsz - temp;
    2927             : 
    2928             :         /*
    2929             :          * For large extent hint sizes, the aligned extent might be larger than
    2930             :          * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
    2931             :          * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
    2932             :          * allocation loops handle short allocation just fine, so it is safe to
    2933             :          * do this. We only want to do it when we are forced to, though, because
    2934             :          * it means more allocation operations are required.
    2935             :          */
    2936    10290296 :         while (align_alen > XFS_MAX_BMBT_EXTLEN)
    2937           3 :                 align_alen -= extsz;
    2938    10290293 :         ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
    2939             : 
    2940             :         /*
    2941             :          * If the previous block overlaps with this proposed allocation
    2942             :          * then move the start forward without adjusting the length.
    2943             :          */
    2944    10290293 :         if (prevp->br_startoff != NULLFILEOFF) {
    2945     8898130 :                 if (prevp->br_startblock == HOLESTARTBLOCK)
    2946             :                         prevo = prevp->br_startoff;
    2947             :                 else
    2948     8898130 :                         prevo = prevp->br_startoff + prevp->br_blockcount;
    2949             :         } else
    2950             :                 prevo = 0;
    2951    10290293 :         if (align_off != orig_off && align_off < prevo)
    2952       11360 :                 align_off = prevo;
    2953             :         /*
    2954             :          * If the next block overlaps with this proposed allocation
    2955             :          * then move the start back without adjusting the length,
    2956             :          * but not before offset 0.
    2957             :          * This may of course make the start overlap previous block,
    2958             :          * and if we hit the offset 0 limit then the next block
    2959             :          * can still overlap too.
    2960             :          */
    2961    10290293 :         if (!eof && gotp->br_startoff != NULLFILEOFF) {
    2962     3902520 :                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
    2963     3846022 :                     (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
    2964           0 :                         nexto = gotp->br_startoff + gotp->br_blockcount;
    2965             :                 else
    2966             :                         nexto = gotp->br_startoff;
    2967             :         } else
    2968             :                 nexto = NULLFILEOFF;
    2969    10290293 :         if (!eof &&
    2970     3902519 :             align_off + align_alen != orig_end &&
    2971             :             align_off + align_alen > nexto)
    2972       11061 :                 align_off = nexto > align_alen ? nexto - align_alen : 0;
    2973             :         /*
    2974             :          * If we're now overlapping the next or previous extent that
    2975             :          * means we can't fit an extsz piece in this hole.  Just move
    2976             :          * the start forward to the first valid spot and set
    2977             :          * the length so we hit the end.
    2978             :          */
    2979    10290293 :         if (align_off != orig_off && align_off < prevo)
    2980        6408 :                 align_off = prevo;
    2981    10290293 :         if (align_off + align_alen != orig_end &&
    2982             :             align_off + align_alen > nexto &&
    2983             :             nexto != NULLFILEOFF) {
    2984        7012 :                 ASSERT(nexto > prevo);
    2985        7012 :                 align_alen = nexto - align_off;
    2986             :         }
    2987             : 
    2988             :         /*
    2989             :          * If realtime, and the result isn't a multiple of the realtime
    2990             :          * extent size we need to remove blocks until it is.
    2991             :          */
    2992    10290293 :         if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
    2993             :                 /*
    2994             :                  * We're not covering the original request, or
    2995             :                  * we won't be able to once we fix the length.
    2996             :                  */
    2997           0 :                 if (orig_off < align_off ||
    2998           0 :                     orig_end > align_off + align_alen ||
    2999           0 :                     align_alen - temp < orig_alen)
    3000             :                         return -EINVAL;
    3001             :                 /*
    3002             :                  * Try to fix it by moving the start up.
    3003             :                  */
    3004           0 :                 if (align_off + temp <= orig_off) {
    3005             :                         align_alen -= temp;
    3006             :                         align_off += temp;
    3007             :                 }
    3008             :                 /*
    3009             :                  * Try to fix it by moving the end in.
    3010             :                  */
    3011           0 :                 else if (align_off + align_alen - temp >= orig_end)
    3012             :                         align_alen -= temp;
    3013             :                 /*
    3014             :                  * Set the start to the minimum then trim the length.
    3015             :                  */
    3016             :                 else {
    3017           0 :                         align_alen -= orig_off - align_off;
    3018           0 :                         align_off = orig_off;
    3019           0 :                         align_alen -= align_alen % mp->m_sb.sb_rextsize;
    3020             :                 }
    3021             :                 /*
    3022             :                  * Result doesn't cover the request, fail it.
    3023             :                  */
    3024           0 :                 if (orig_off < align_off || orig_end > align_off + align_alen)
    3025             :                         return -EINVAL;
    3026             :         } else {
    3027    10290293 :                 ASSERT(orig_off >= align_off);
    3028             :                 /* see XFS_BMBT_MAX_EXTLEN handling above */
    3029    10290293 :                 ASSERT(orig_end <= align_off + align_alen ||
    3030             :                        align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
    3031             :         }
    3032             : 
    3033             : #ifdef DEBUG
    3034    10290293 :         if (!eof && gotp->br_startoff != NULLFILEOFF)
    3035     3902519 :                 ASSERT(align_off + align_alen <= gotp->br_startoff);
    3036    10290293 :         if (prevp->br_startoff != NULLFILEOFF)
    3037     8898159 :                 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
    3038             : #endif
    3039             : 
    3040    10290293 :         *lenp = align_alen;
    3041    10290293 :         *offp = align_off;
    3042    10290293 :         return 0;
    3043             : }
    3044             : 
    3045             : #define XFS_ALLOC_GAP_UNITS     4
    3046             : 
    3047             : void
    3048    50146221 : xfs_bmap_adjacent(
    3049             :         struct xfs_bmalloca     *ap)    /* bmap alloc argument struct */
    3050             : {
    3051    50146221 :         xfs_fsblock_t   adjust;         /* adjustment to block numbers */
    3052    50146221 :         xfs_mount_t     *mp;            /* mount point structure */
    3053    50146221 :         int             rt;             /* true if inode is realtime */
    3054             : 
    3055             : #define ISVALID(x,y)    \
    3056             :         (rt ? \
    3057             :                 (x) < mp->m_sb.sb_rblocks : \
    3058             :                 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
    3059             :                 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
    3060             :                 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
    3061             : 
    3062    50146221 :         mp = ap->ip->i_mount;
    3063    50146221 :         rt = XFS_IS_REALTIME_INODE(ap->ip) &&
    3064     7941618 :                 (ap->datatype & XFS_ALLOC_USERDATA);
    3065             :         /*
    3066             :          * If allocating at eof, and there's a previous real block,
    3067             :          * try to use its last block as our starting point.
    3068             :          */
    3069    50146221 :         if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
    3070    11179840 :             !isnullstartblock(ap->prev.br_startblock) &&
    3071     6755477 :             ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
    3072             :                     ap->prev.br_startblock)) {
    3073     6755281 :                 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
    3074             :                 /*
    3075             :                  * Adjust for the gap between prevp and us.
    3076             :                  */
    3077     6755281 :                 adjust = ap->offset -
    3078     6755281 :                         (ap->prev.br_startoff + ap->prev.br_blockcount);
    3079     9119272 :                 if (adjust &&
    3080     2363994 :                     ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
    3081     2348561 :                         ap->blkno += adjust;
    3082             :         }
    3083             :         /*
    3084             :          * If not at eof, then compare the two neighbor blocks.
    3085             :          * Figure out whether either one gives us a good starting point,
    3086             :          * and pick the better one.
    3087             :          */
    3088    43390941 :         else if (!ap->eof) {
    3089    20925879 :                 xfs_fsblock_t   gotbno;         /* right side block number */
    3090    20925879 :                 xfs_fsblock_t   gotdiff=0;      /* right side difference */
    3091    20925879 :                 xfs_fsblock_t   prevbno;        /* left side block number */
    3092    20925879 :                 xfs_fsblock_t   prevdiff=0;     /* left side difference */
    3093             : 
    3094             :                 /*
    3095             :                  * If there's a previous (left) block, select a requested
    3096             :                  * start block based on it.
    3097             :                  */
    3098    36311764 :                 if (ap->prev.br_startoff != NULLFILEOFF &&
    3099    15491014 :                     !isnullstartblock(ap->prev.br_startblock) &&
    3100    15385848 :                     (prevbno = ap->prev.br_startblock +
    3101    27612099 :                                ap->prev.br_blockcount) &&
    3102    15385885 :                     ISVALID(prevbno, ap->prev.br_startblock)) {
    3103             :                         /*
    3104             :                          * Calculate gap to end of previous block.
    3105             :                          */
    3106    15385749 :                         adjust = prevdiff = ap->offset -
    3107    15385749 :                                 (ap->prev.br_startoff +
    3108    15385749 :                                  ap->prev.br_blockcount);
    3109             :                         /*
    3110             :                          * Figure the startblock based on the previous block's
    3111             :                          * end and the gap size.
    3112             :                          * Heuristic!
    3113             :                          * If the gap is large relative to the piece we're
    3114             :                          * allocating, or using it gives us an invalid block
    3115             :                          * number, then just use the end of the previous block.
    3116             :                          */
    3117    18487867 :                         if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3118     5344793 :                             ISVALID(prevbno + prevdiff,
    3119             :                                     ap->prev.br_startblock))
    3120     5344759 :                                 prevbno += adjust;
    3121             :                         else
    3122    10040988 :                                 prevdiff += adjust;
    3123             :                 }
    3124             :                 /*
    3125             :                  * No previous block or can't follow it, just default.
    3126             :                  */
    3127             :                 else
    3128             :                         prevbno = NULLFSBLOCK;
    3129             :                 /*
    3130             :                  * If there's a following (right) block, select a requested
    3131             :                  * start block based on it.
    3132             :                  */
    3133    20925909 :                 if (!isnullstartblock(ap->got.br_startblock)) {
    3134             :                         /*
    3135             :                          * Calculate gap to start of next block.
    3136             :                          */
    3137     7791326 :                         adjust = gotdiff = ap->got.br_startoff - ap->offset;
    3138             :                         /*
    3139             :                          * Figure the startblock based on the next block's
    3140             :                          * start and the gap size.
    3141             :                          */
    3142     7791326 :                         gotbno = ap->got.br_startblock;
    3143             :                         /*
    3144             :                          * Heuristic!
    3145             :                          * If the gap is large relative to the piece we're
    3146             :                          * allocating, or using it gives us an invalid block
    3147             :                          * number, then just use the start of the next block
    3148             :                          * offset by our length.
    3149             :                          */
    3150     9149399 :                         if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3151     3357239 :                             ISVALID(gotbno - gotdiff, gotbno))
    3152     3334429 :                                 gotbno -= adjust;
    3153     4456893 :                         else if (ISVALID(gotbno - ap->length, gotbno)) {
    3154     4438256 :                                 gotbno -= ap->length;
    3155     4438256 :                                 gotdiff += adjust - ap->length;
    3156             :                         } else
    3157       18646 :                                 gotdiff += adjust;
    3158             :                 }
    3159             :                 /*
    3160             :                  * No next block, just default.
    3161             :                  */
    3162             :                 else
    3163             :                         gotbno = NULLFSBLOCK;
    3164             :                 /*
    3165             :                  * If both valid, pick the better one, else the only good
    3166             :                  * one, else ap->blkno is already set (to 0 or the inode block).
    3167             :                  */
    3168    20925914 :                 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
    3169     9124119 :                         ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
    3170    14012993 :                 else if (prevbno != NULLFSBLOCK)
    3171     8472882 :                         ap->blkno = prevbno;
    3172     5540111 :                 else if (gotbno != NULLFSBLOCK)
    3173      878399 :                         ap->blkno = gotbno;
    3174             :         }
    3175             : #undef ISVALID
    3176    50146254 : }
    3177             : 
    3178             : int
    3179    43601948 : xfs_bmap_longest_free_extent(
    3180             :         struct xfs_perag        *pag,
    3181             :         struct xfs_trans        *tp,
    3182             :         xfs_extlen_t            *blen)
    3183             : {
    3184    43601948 :         xfs_extlen_t            longest;
    3185    43601948 :         int                     error = 0;
    3186             : 
    3187    87203896 :         if (!xfs_perag_initialised_agf(pag)) {
    3188        1772 :                 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
    3189             :                                 NULL);
    3190        1772 :                 if (error)
    3191             :                         return error;
    3192             :         }
    3193             : 
    3194    43600190 :         longest = xfs_alloc_longest_free_extent(pag,
    3195             :                                 xfs_alloc_min_freelist(pag->pag_mount, pag),
    3196             :                                 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
    3197    43600384 :         if (*blen < longest)
    3198    43495732 :                 *blen = longest;
    3199             : 
    3200             :         return 0;
    3201             : }
    3202             : 
    3203             : static xfs_extlen_t
    3204             : xfs_bmap_select_minlen(
    3205             :         struct xfs_bmalloca     *ap,
    3206             :         struct xfs_alloc_arg    *args,
    3207             :         xfs_extlen_t            blen)
    3208             : {
    3209             : 
    3210             :         /*
    3211             :          * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
    3212             :          * possible that there is enough contiguous free space for this request.
    3213             :          */
    3214    43320610 :         if (blen < ap->minlen)
    3215             :                 return ap->minlen;
    3216             : 
    3217             :         /*
    3218             :          * If the best seen length is less than the request length,
    3219             :          * use the best as the minimum, otherwise we've got the maxlen we
    3220             :          * were asked for.
    3221             :          */
    3222    43320572 :         if (blen < args->maxlen)
    3223             :                 return blen;
    3224             :         return args->maxlen;
    3225             : }
    3226             : 
    3227             : static int
    3228    43309488 : xfs_bmap_btalloc_select_lengths(
    3229             :         struct xfs_bmalloca     *ap,
    3230             :         struct xfs_alloc_arg    *args,
    3231             :         xfs_extlen_t            *blen)
    3232             : {
    3233    43309488 :         struct xfs_mount        *mp = args->mp;
    3234    43309488 :         struct xfs_perag        *pag;
    3235    43309488 :         xfs_agnumber_t          agno, startag;
    3236    43309488 :         int                     error = 0;
    3237             : 
    3238    43309488 :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3239         122 :                 args->total = ap->minlen;
    3240         122 :                 args->minlen = ap->minlen;
    3241         122 :                 return 0;
    3242             :         }
    3243             : 
    3244    43309366 :         args->total = ap->total;
    3245    43309366 :         startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
    3246    43309366 :         if (startag == NULLAGNUMBER)
    3247           0 :                 startag = 0;
    3248             : 
    3249    43309366 :         *blen = 0;
    3250    43557575 :         for_each_perag_wrap(mp, startag, agno, pag) {
    3251    43516125 :                 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
    3252    43515405 :                 if (error && error != -EAGAIN)
    3253             :                         break;
    3254    43515405 :                 error = 0;
    3255    43515405 :                 if (*blen >= args->maxlen)
    3256             :                         break;
    3257             :         }
    3258    43308963 :         if (pag)
    3259    43267242 :                 xfs_perag_rele(pag);
    3260             : 
    3261    43309386 :         args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
    3262    43309386 :         return error;
    3263             : }
    3264             : 
    3265             : /* Update all inode and quota accounting for the allocation we just did. */
    3266             : static void
    3267    43653905 : xfs_bmap_btalloc_accounting(
    3268             :         struct xfs_bmalloca     *ap,
    3269             :         struct xfs_alloc_arg    *args)
    3270             : {
    3271    43653905 :         if (ap->flags & XFS_BMAPI_COWFORK) {
    3272             :                 /*
    3273             :                  * COW fork blocks are in-core only and thus are treated as
    3274             :                  * in-core quota reservation (like delalloc blocks) even when
    3275             :                  * converted to real blocks. The quota reservation is not
    3276             :                  * accounted to disk until blocks are remapped to the data
    3277             :                  * fork. So if these blocks were previously delalloc, we
    3278             :                  * already have quota reservation and there's nothing to do
    3279             :                  * yet.
    3280             :                  */
    3281      324782 :                 if (ap->wasdel) {
    3282      127004 :                         xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3283      127004 :                         return;
    3284             :                 }
    3285             : 
    3286             :                 /*
    3287             :                  * Otherwise, we've allocated blocks in a hole. The transaction
    3288             :                  * has acquired in-core quota reservation for this extent.
    3289             :                  * Rather than account these as real blocks, however, we reduce
    3290             :                  * the transaction quota reservation based on the allocation.
    3291             :                  * This essentially transfers the transaction quota reservation
    3292             :                  * to that of a delalloc extent.
    3293             :                  */
    3294      197778 :                 ap->ip->i_delayed_blks += args->len;
    3295      197778 :                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
    3296      197778 :                                 -(long)args->len);
    3297      197778 :                 return;
    3298             :         }
    3299             : 
    3300             :         /* data/attr fork only */
    3301    43329123 :         ap->ip->i_nblocks += args->len;
    3302    43329123 :         xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
    3303    43329292 :         if (ap->wasdel) {
    3304    13059195 :                 ap->ip->i_delayed_blks -= args->len;
    3305    13059195 :                 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3306             :         }
    3307   129987867 :         xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
    3308    43329289 :                 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
    3309    43329289 :                 args->len);
    3310             : }
    3311             : 
    3312             : static int
    3313    43654801 : xfs_bmap_compute_alignments(
    3314             :         struct xfs_bmalloca     *ap,
    3315             :         struct xfs_alloc_arg    *args)
    3316             : {
    3317    43654801 :         struct xfs_mount        *mp = args->mp;
    3318    43654801 :         xfs_extlen_t            align = 0; /* minimum allocation alignment */
    3319    43654801 :         int                     stripe_align = 0;
    3320             : 
    3321             :         /* stripe alignment for allocation is determined by mount parameters */
    3322    43654801 :         if (mp->m_swidth && xfs_has_swalloc(mp))
    3323             :                 stripe_align = mp->m_swidth;
    3324    43654801 :         else if (mp->m_dalign)
    3325       40693 :                 stripe_align = mp->m_dalign;
    3326             : 
    3327    43654801 :         if (ap->flags & XFS_BMAPI_COWFORK)
    3328      324807 :                 align = xfs_get_cowextsz_hint(ap->ip);
    3329    43329994 :         else if (ap->datatype & XFS_ALLOC_USERDATA)
    3330    20715557 :                 align = xfs_get_extsz_hint(ap->ip);
    3331    21040306 :         if (align) {
    3332     3422012 :                 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
    3333     3422010 :                                         ap->eof, 0, ap->conv, &ap->offset,
    3334             :                                         &ap->length))
    3335           0 :                         ASSERT(0);
    3336     3422012 :                 ASSERT(ap->length);
    3337             :         }
    3338             : 
    3339             :         /* apply extent size hints if obtained earlier */
    3340    43654745 :         if (align) {
    3341     3422009 :                 args->prod = align;
    3342     3422009 :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3343     3422009 :                 if (args->mod)
    3344       12104 :                         args->mod = args->prod - args->mod;
    3345    40232736 :         } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
    3346         303 :                 args->prod = 1;
    3347         303 :                 args->mod = 0;
    3348             :         } else {
    3349    40232433 :                 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
    3350    40232433 :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3351    40232989 :                 if (args->mod)
    3352    14803852 :                         args->mod = args->prod - args->mod;
    3353             :         }
    3354             : 
    3355    43655301 :         return stripe_align;
    3356             : }
    3357             : 
    3358             : static void
    3359    43653738 : xfs_bmap_process_allocated_extent(
    3360             :         struct xfs_bmalloca     *ap,
    3361             :         struct xfs_alloc_arg    *args,
    3362             :         xfs_fileoff_t           orig_offset,
    3363             :         xfs_extlen_t            orig_length)
    3364             : {
    3365    43653738 :         ap->blkno = args->fsbno;
    3366    43653738 :         ap->length = args->len;
    3367             :         /*
    3368             :          * If the extent size hint is active, we tried to round the
    3369             :          * caller's allocation request offset down to extsz and the
    3370             :          * length up to another extsz boundary.  If we found a free
    3371             :          * extent we mapped it in starting at this new offset.  If the
    3372             :          * newly mapped space isn't long enough to cover any of the
    3373             :          * range of offsets that was originally requested, move the
    3374             :          * mapping up so that we can fill as much of the caller's
    3375             :          * original request as possible.  Free space is apparently
    3376             :          * very fragmented so we're unlikely to be able to satisfy the
    3377             :          * hints anyway.
    3378             :          */
    3379    43653738 :         if (ap->length <= orig_length)
    3380    43440771 :                 ap->offset = orig_offset;
    3381      212967 :         else if (ap->offset + ap->length < orig_offset + orig_length)
    3382           0 :                 ap->offset = orig_offset + orig_length - ap->length;
    3383    43653738 :         xfs_bmap_btalloc_accounting(ap, args);
    3384    43654096 : }
    3385             : 
    3386             : #ifdef DEBUG
    3387             : static int
    3388      334342 : xfs_bmap_exact_minlen_extent_alloc(
    3389             :         struct xfs_bmalloca     *ap)
    3390             : {
    3391      334342 :         struct xfs_mount        *mp = ap->ip->i_mount;
    3392      334342 :         struct xfs_alloc_arg    args = { .tp = ap->tp, .mp = mp };
    3393      334342 :         xfs_fileoff_t           orig_offset;
    3394      334342 :         xfs_extlen_t            orig_length;
    3395      334342 :         int                     error;
    3396             : 
    3397      334342 :         ASSERT(ap->length);
    3398             : 
    3399      334342 :         if (ap->minlen != 1) {
    3400           0 :                 ap->blkno = NULLFSBLOCK;
    3401           0 :                 ap->length = 0;
    3402           0 :                 return 0;
    3403             :         }
    3404             : 
    3405      334342 :         orig_offset = ap->offset;
    3406      334342 :         orig_length = ap->length;
    3407             : 
    3408      334342 :         args.alloc_minlen_only = 1;
    3409             : 
    3410      334342 :         xfs_bmap_compute_alignments(ap, &args);
    3411             : 
    3412             :         /*
    3413             :          * Unlike the longest extent available in an AG, we don't track
    3414             :          * the length of an AG's shortest extent.
    3415             :          * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
    3416             :          * hence we can afford to start traversing from the 0th AG since
    3417             :          * we need not be concerned about a drop in performance in
    3418             :          * "debug only" code paths.
    3419             :          */
    3420      334344 :         ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
    3421             : 
    3422      334344 :         args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
    3423      334344 :         args.minlen = args.maxlen = ap->minlen;
    3424      334344 :         args.total = ap->total;
    3425             : 
    3426      334344 :         args.alignment = 1;
    3427      334344 :         args.minalignslop = 0;
    3428             : 
    3429      334344 :         args.minleft = ap->minleft;
    3430      334344 :         args.wasdel = ap->wasdel;
    3431      334344 :         args.resv = XFS_AG_RESV_NONE;
    3432      334344 :         args.datatype = ap->datatype;
    3433             : 
    3434      334344 :         error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
    3435      334347 :         if (error)
    3436             :                 return error;
    3437             : 
    3438      334347 :         if (args.fsbno != NULLFSBLOCK) {
    3439      334347 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3440             :                         orig_length);
    3441             :         } else {
    3442           0 :                 ap->blkno = NULLFSBLOCK;
    3443           0 :                 ap->length = 0;
    3444             :         }
    3445             : 
    3446             :         return 0;
    3447             : }
    3448             : #else
    3449             : 
    3450             : #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
    3451             : 
    3452             : #endif
    3453             : 
    3454             : /*
    3455             :  * If we are not low on available data blocks and we are allocating at
    3456             :  * EOF, optimise allocation for contiguous file extension and/or stripe
    3457             :  * alignment of the new extent.
    3458             :  *
    3459             :  * NOTE: ap->aeof is only set if the allocation length is >= the
    3460             :  * stripe unit and the allocation offset is at the end of file.
    3461             :  */
    3462             : static int
    3463        3707 : xfs_bmap_btalloc_at_eof(
    3464             :         struct xfs_bmalloca     *ap,
    3465             :         struct xfs_alloc_arg    *args,
    3466             :         xfs_extlen_t            blen,
    3467             :         int                     stripe_align,
    3468             :         bool                    ag_only)
    3469             : {
    3470        3707 :         struct xfs_mount        *mp = args->mp;
    3471        3707 :         struct xfs_perag        *caller_pag = args->pag;
    3472        3707 :         int                     error;
    3473             : 
    3474             :         /*
    3475             :          * If there are already extents in the file, try an exact EOF block
    3476             :          * allocation to extend the file as a contiguous extent. If that fails,
    3477             :          * or it's the first allocation in a file, just try for a stripe aligned
    3478             :          * allocation.
    3479             :          */
    3480        3707 :         if (ap->offset) {
    3481        3267 :                 xfs_extlen_t    nextminlen = 0;
    3482             : 
    3483             :                 /*
    3484             :                  * Compute the minlen+alignment for the next case.  Set slop so
    3485             :                  * that the value of minlen+alignment+slop doesn't go up between
    3486             :                  * the calls.
    3487             :                  */
    3488        3267 :                 args->alignment = 1;
    3489        3267 :                 if (blen > stripe_align && blen <= args->maxlen)
    3490           0 :                         nextminlen = blen - stripe_align;
    3491             :                 else
    3492        3267 :                         nextminlen = args->minlen;
    3493        3267 :                 if (nextminlen + stripe_align > args->minlen + 1)
    3494        3267 :                         args->minalignslop = nextminlen + stripe_align -
    3495        3267 :                                         args->minlen - 1;
    3496             :                 else
    3497           0 :                         args->minalignslop = 0;
    3498             : 
    3499        3267 :                 if (!caller_pag)
    3500        3267 :                         args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
    3501        3267 :                 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
    3502        3267 :                 if (!caller_pag) {
    3503        3267 :                         xfs_perag_put(args->pag);
    3504        3267 :                         args->pag = NULL;
    3505             :                 }
    3506        3267 :                 if (error)
    3507             :                         return error;
    3508             : 
    3509        3267 :                 if (args->fsbno != NULLFSBLOCK)
    3510             :                         return 0;
    3511             :                 /*
    3512             :                  * Exact allocation failed. Reset to try an aligned allocation
    3513             :                  * according to the original allocation specification.
    3514             :                  */
    3515        2826 :                 args->alignment = stripe_align;
    3516        2826 :                 args->minlen = nextminlen;
    3517        2826 :                 args->minalignslop = 0;
    3518             :         } else {
    3519             :                 /*
    3520             :                  * Adjust minlen to try and preserve alignment if we
    3521             :                  * can't guarantee an aligned maxlen extent.
    3522             :                  */
    3523         440 :                 args->alignment = stripe_align;
    3524         440 :                 if (blen > args->alignment &&
    3525         440 :                     blen <= args->maxlen + args->alignment)
    3526           0 :                         args->minlen = blen - args->alignment;
    3527         440 :                 args->minalignslop = 0;
    3528             :         }
    3529             : 
    3530        3266 :         if (ag_only) {
    3531           0 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3532             :         } else {
    3533        3266 :                 args->pag = NULL;
    3534        3266 :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3535        3266 :                 ASSERT(args->pag == NULL);
    3536        3266 :                 args->pag = caller_pag;
    3537             :         }
    3538        3266 :         if (error)
    3539             :                 return error;
    3540             : 
    3541        3266 :         if (args->fsbno != NULLFSBLOCK)
    3542             :                 return 0;
    3543             : 
    3544             :         /*
    3545             :          * Allocation failed, so turn return the allocation args to their
    3546             :          * original non-aligned state so the caller can proceed on allocation
    3547             :          * failure as if this function was never called.
    3548             :          */
    3549           0 :         args->alignment = 1;
    3550           0 :         return 0;
    3551             : }
    3552             : 
    3553             : /*
    3554             :  * We have failed multiple allocation attempts so now are in a low space
    3555             :  * allocation situation. Try a locality first full filesystem minimum length
    3556             :  * allocation whilst still maintaining necessary total block reservation
    3557             :  * requirements.
    3558             :  *
    3559             :  * If that fails, we are now critically low on space, so perform a last resort
    3560             :  * allocation attempt: no reserve, no locality, blocking, minimum length, full
    3561             :  * filesystem free space scan. We also indicate to future allocations in this
    3562             :  * transaction that we are critically low on space so they don't waste time on
    3563             :  * allocation modes that are unlikely to succeed.
    3564             :  */
    3565             : int
    3566       26430 : xfs_bmap_btalloc_low_space(
    3567             :         struct xfs_bmalloca     *ap,
    3568             :         struct xfs_alloc_arg    *args)
    3569             : {
    3570       26430 :         int                     error;
    3571             : 
    3572       26430 :         if (args->minlen > ap->minlen) {
    3573       26390 :                 args->minlen = ap->minlen;
    3574       26390 :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3575       26390 :                 if (error || args->fsbno != NULLFSBLOCK)
    3576             :                         return error;
    3577             :         }
    3578             : 
    3579             :         /* Last ditch attempt before failure is declared. */
    3580          40 :         args->total = ap->minlen;
    3581          40 :         error = xfs_alloc_vextent_first_ag(args, 0);
    3582          40 :         if (error)
    3583             :                 return error;
    3584          40 :         ap->tp->t_flags |= XFS_TRANS_LOWMODE;
    3585          40 :         return 0;
    3586             : }
    3587             : 
    3588             : static int
    3589       11224 : xfs_bmap_btalloc_filestreams(
    3590             :         struct xfs_bmalloca     *ap,
    3591             :         struct xfs_alloc_arg    *args,
    3592             :         int                     stripe_align)
    3593             : {
    3594       11224 :         xfs_extlen_t            blen = 0;
    3595       11224 :         int                     error = 0;
    3596             : 
    3597             : 
    3598       11224 :         error = xfs_filestream_select_ag(ap, args, &blen);
    3599       11224 :         if (error)
    3600             :                 return error;
    3601       11224 :         ASSERT(args->pag);
    3602             : 
    3603             :         /*
    3604             :          * If we are in low space mode, then optimal allocation will fail so
    3605             :          * prepare for minimal allocation and jump to the low space algorithm
    3606             :          * immediately.
    3607             :          */
    3608       11224 :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3609           0 :                 args->minlen = ap->minlen;
    3610           0 :                 ASSERT(args->fsbno == NULLFSBLOCK);
    3611           0 :                 goto out_low_space;
    3612             :         }
    3613             : 
    3614       11224 :         args->minlen = xfs_bmap_select_minlen(ap, args, blen);
    3615       11224 :         if (ap->aeof)
    3616           0 :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3617             :                                 true);
    3618             : 
    3619       11224 :         if (!error && args->fsbno == NULLFSBLOCK)
    3620       11224 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3621             : 
    3622           0 : out_low_space:
    3623             :         /*
    3624             :          * We are now done with the perag reference for the filestreams
    3625             :          * association provided by xfs_filestream_select_ag(). Release it now as
    3626             :          * we've either succeeded, had a fatal error or we are out of space and
    3627             :          * need to do a full filesystem scan for free space which will take it's
    3628             :          * own references.
    3629             :          */
    3630       11223 :         xfs_perag_rele(args->pag);
    3631       11223 :         args->pag = NULL;
    3632       11223 :         if (error || args->fsbno != NULLFSBLOCK)
    3633             :                 return error;
    3634             : 
    3635           1 :         return xfs_bmap_btalloc_low_space(ap, args);
    3636             : }
    3637             : 
    3638             : static int
    3639    43308867 : xfs_bmap_btalloc_best_length(
    3640             :         struct xfs_bmalloca     *ap,
    3641             :         struct xfs_alloc_arg    *args,
    3642             :         int                     stripe_align)
    3643             : {
    3644    43308867 :         xfs_extlen_t            blen = 0;
    3645    43308867 :         int                     error;
    3646             : 
    3647    43308867 :         ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
    3648    43308867 :         xfs_bmap_adjacent(ap);
    3649             : 
    3650             :         /*
    3651             :          * Search for an allocation group with a single extent large enough for
    3652             :          * the request.  If one isn't found, then adjust the minimum allocation
    3653             :          * size to the largest space found.
    3654             :          */
    3655    43309440 :         error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
    3656    43309486 :         if (error)
    3657             :                 return error;
    3658             : 
    3659             :         /*
    3660             :          * Don't attempt optimal EOF allocation if previous allocations barely
    3661             :          * succeeded due to being near ENOSPC. It is highly unlikely we'll get
    3662             :          * optimal or even aligned allocations in this case, so don't waste time
    3663             :          * trying.
    3664             :          */
    3665    43309486 :         if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
    3666        3707 :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3667             :                                 false);
    3668        3707 :                 if (error || args->fsbno != NULLFSBLOCK)
    3669             :                         return error;
    3670             :         }
    3671             : 
    3672    43305779 :         error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3673    43306351 :         if (error || args->fsbno != NULLFSBLOCK)
    3674             :                 return error;
    3675             : 
    3676       26429 :         return xfs_bmap_btalloc_low_space(ap, args);
    3677             : }
    3678             : 
    3679             : static int
    3680    43320658 : xfs_bmap_btalloc(
    3681             :         struct xfs_bmalloca     *ap)
    3682             : {
    3683    43320658 :         struct xfs_mount        *mp = ap->ip->i_mount;
    3684    86641316 :         struct xfs_alloc_arg    args = {
    3685    43320658 :                 .tp             = ap->tp,
    3686             :                 .mp             = mp,
    3687             :                 .fsbno          = NULLFSBLOCK,
    3688             :                 .oinfo          = XFS_RMAP_OINFO_SKIP_UPDATE,
    3689    43320658 :                 .minleft        = ap->minleft,
    3690           0 :                 .wasdel         = ap->wasdel,
    3691             :                 .resv           = XFS_AG_RESV_NONE,
    3692    43320658 :                 .datatype       = ap->datatype,
    3693             :                 .alignment      = 1,
    3694             :                 .minalignslop   = 0,
    3695             :         };
    3696    43320658 :         xfs_fileoff_t           orig_offset;
    3697    43320658 :         xfs_extlen_t            orig_length;
    3698    43320658 :         int                     error;
    3699    43320658 :         int                     stripe_align;
    3700             : 
    3701    43320658 :         ASSERT(ap->length);
    3702    43320658 :         orig_offset = ap->offset;
    3703    43320658 :         orig_length = ap->length;
    3704             : 
    3705    43320658 :         stripe_align = xfs_bmap_compute_alignments(ap, &args);
    3706             : 
    3707             :         /* Trim the allocation back to the maximum an AG can fit. */
    3708    43320388 :         args.maxlen = min(ap->length, mp->m_ag_max_usable);
    3709             : 
    3710    43320388 :         if ((ap->datatype & XFS_ALLOC_USERDATA) &&
    3711    20710960 :             xfs_inode_is_filestream(ap->ip))
    3712       11229 :                 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
    3713             :         else
    3714    43309229 :                 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
    3715    43321302 :         if (error)
    3716             :                 return error;
    3717             : 
    3718    43319638 :         if (args.fsbno != NULLFSBLOCK) {
    3719    43319598 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3720             :                         orig_length);
    3721             :         } else {
    3722          40 :                 ap->blkno = NULLFSBLOCK;
    3723          40 :                 ap->length = 0;
    3724             :         }
    3725             :         return 0;
    3726             : }
    3727             : 
    3728             : /* Trim extent to fit a logical block range. */
    3729             : void
    3730   205883664 : xfs_trim_extent(
    3731             :         struct xfs_bmbt_irec    *irec,
    3732             :         xfs_fileoff_t           bno,
    3733             :         xfs_filblks_t           len)
    3734             : {
    3735   205883664 :         xfs_fileoff_t           distance;
    3736   205883664 :         xfs_fileoff_t           end = bno + len;
    3737             : 
    3738   205883664 :         if (irec->br_startoff + irec->br_blockcount <= bno ||
    3739             :             irec->br_startoff >= end) {
    3740    70338519 :                 irec->br_blockcount = 0;
    3741    70338519 :                 return;
    3742             :         }
    3743             : 
    3744   135545145 :         if (irec->br_startoff < bno) {
    3745    37595472 :                 distance = bno - irec->br_startoff;
    3746    37595472 :                 if (isnullstartblock(irec->br_startblock))
    3747      282236 :                         irec->br_startblock = DELAYSTARTBLOCK;
    3748    37595472 :                 if (irec->br_startblock != DELAYSTARTBLOCK &&
    3749             :                     irec->br_startblock != HOLESTARTBLOCK)
    3750    37310854 :                         irec->br_startblock += distance;
    3751    37595472 :                 irec->br_startoff += distance;
    3752    37595472 :                 irec->br_blockcount -= distance;
    3753             :         }
    3754             : 
    3755   135545145 :         if (end < irec->br_startoff + irec->br_blockcount) {
    3756     3253132 :                 distance = irec->br_startoff + irec->br_blockcount - end;
    3757     3253132 :                 irec->br_blockcount -= distance;
    3758             :         }
    3759             : }
    3760             : 
    3761             : /*
    3762             :  * Trim the returned map to the required bounds
    3763             :  */
    3764             : STATIC void
    3765  2495067549 : xfs_bmapi_trim_map(
    3766             :         struct xfs_bmbt_irec    *mval,
    3767             :         struct xfs_bmbt_irec    *got,
    3768             :         xfs_fileoff_t           *bno,
    3769             :         xfs_filblks_t           len,
    3770             :         xfs_fileoff_t           obno,
    3771             :         xfs_fileoff_t           end,
    3772             :         int                     n,
    3773             :         uint32_t                flags)
    3774             : {
    3775  2495067549 :         if ((flags & XFS_BMAPI_ENTIRE) ||
    3776  2495067549 :             got->br_startoff + got->br_blockcount <= obno) {
    3777           7 :                 *mval = *got;
    3778           7 :                 if (isnullstartblock(got->br_startblock))
    3779           0 :                         mval->br_startblock = DELAYSTARTBLOCK;
    3780           7 :                 return;
    3781             :         }
    3782             : 
    3783  2495067542 :         if (obno > *bno)
    3784           0 :                 *bno = obno;
    3785  2495067542 :         ASSERT((*bno >= obno) || (n == 0));
    3786  2495067542 :         ASSERT(*bno < end);
    3787  2495067542 :         mval->br_startoff = *bno;
    3788  2495067542 :         if (isnullstartblock(got->br_startblock))
    3789     3622807 :                 mval->br_startblock = DELAYSTARTBLOCK;
    3790             :         else
    3791  2491444735 :                 mval->br_startblock = got->br_startblock +
    3792  2491444735 :                                         (*bno - got->br_startoff);
    3793             :         /*
    3794             :          * Return the minimum of what we got and what we asked for for
    3795             :          * the length.  We can use the len variable here because it is
    3796             :          * modified below and we could have been there before coming
    3797             :          * here if the first part of the allocation didn't overlap what
    3798             :          * was asked for.
    3799             :          */
    3800  2495067542 :         mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
    3801             :                         got->br_blockcount - (*bno - got->br_startoff));
    3802  2495067542 :         mval->br_state = got->br_state;
    3803  2495067542 :         ASSERT(mval->br_blockcount <= len);
    3804             :         return;
    3805             : }
    3806             : 
    3807             : /*
    3808             :  * Update and validate the extent map to return
    3809             :  */
    3810             : STATIC void
    3811  2495871324 : xfs_bmapi_update_map(
    3812             :         struct xfs_bmbt_irec    **map,
    3813             :         xfs_fileoff_t           *bno,
    3814             :         xfs_filblks_t           *len,
    3815             :         xfs_fileoff_t           obno,
    3816             :         xfs_fileoff_t           end,
    3817             :         int                     *n,
    3818             :         uint32_t                flags)
    3819             : {
    3820  2495871324 :         xfs_bmbt_irec_t *mval = *map;
    3821             : 
    3822  2495871324 :         ASSERT((flags & XFS_BMAPI_ENTIRE) ||
    3823             :                ((mval->br_startoff + mval->br_blockcount) <= end));
    3824  2495871324 :         ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
    3825             :                (mval->br_startoff < obno));
    3826             : 
    3827  2495871324 :         *bno = mval->br_startoff + mval->br_blockcount;
    3828  2495871324 :         *len = end - *bno;
    3829  2495871324 :         if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
    3830             :                 /* update previous map with new information */
    3831           0 :                 ASSERT(mval->br_startblock == mval[-1].br_startblock);
    3832           0 :                 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
    3833           0 :                 ASSERT(mval->br_state == mval[-1].br_state);
    3834           0 :                 mval[-1].br_blockcount = mval->br_blockcount;
    3835           0 :                 mval[-1].br_state = mval->br_state;
    3836  2495871324 :         } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
    3837      137889 :                    mval[-1].br_startblock != DELAYSTARTBLOCK &&
    3838      137442 :                    mval[-1].br_startblock != HOLESTARTBLOCK &&
    3839      137442 :                    mval->br_startblock == mval[-1].br_startblock +
    3840      137442 :                                           mval[-1].br_blockcount &&
    3841           0 :                    mval[-1].br_state == mval->br_state) {
    3842           0 :                 ASSERT(mval->br_startoff ==
    3843             :                        mval[-1].br_startoff + mval[-1].br_blockcount);
    3844           0 :                 mval[-1].br_blockcount += mval->br_blockcount;
    3845  2495871324 :         } else if (*n > 0 &&
    3846      137889 :                    mval->br_startblock == DELAYSTARTBLOCK &&
    3847           0 :                    mval[-1].br_startblock == DELAYSTARTBLOCK &&
    3848           0 :                    mval->br_startoff ==
    3849           0 :                    mval[-1].br_startoff + mval[-1].br_blockcount) {
    3850           0 :                 mval[-1].br_blockcount += mval->br_blockcount;
    3851           0 :                 mval[-1].br_state = mval->br_state;
    3852  2495871324 :         } else if (!((*n == 0) &&
    3853  2495709477 :                      ((mval->br_startoff + mval->br_blockcount) <=
    3854             :                       obno))) {
    3855  2495945745 :                 mval++;
    3856  2495945745 :                 (*n)++;
    3857             :         }
    3858  2495871324 :         *map = mval;
    3859  2495871324 : }
    3860             : 
    3861             : /*
    3862             :  * Map file blocks to filesystem blocks without allocation.
    3863             :  */
    3864             : int
    3865  2929896730 : xfs_bmapi_read(
    3866             :         struct xfs_inode        *ip,
    3867             :         xfs_fileoff_t           bno,
    3868             :         xfs_filblks_t           len,
    3869             :         struct xfs_bmbt_irec    *mval,
    3870             :         int                     *nmap,
    3871             :         uint32_t                flags)
    3872             : {
    3873  2929896730 :         struct xfs_mount        *mp = ip->i_mount;
    3874  2929896730 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    3875  2929896730 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    3876  2930068966 :         struct xfs_bmbt_irec    got;
    3877  2930068966 :         xfs_fileoff_t           obno;
    3878  2930068966 :         xfs_fileoff_t           end;
    3879  2930068966 :         struct xfs_iext_cursor  icur;
    3880  2930068966 :         int                     error;
    3881  2930068966 :         bool                    eof = false;
    3882  2930068966 :         int                     n = 0;
    3883             : 
    3884  2930068966 :         ASSERT(*nmap >= 1);
    3885  2930068966 :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
    3886  2930068966 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
    3887             : 
    3888  2930035988 :         if (WARN_ON_ONCE(!ifp))
    3889             :                 return -EFSCORRUPTED;
    3890             : 
    3891  5860305044 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    3892  2930035988 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
    3893           0 :                 return -EFSCORRUPTED;
    3894             : 
    3895  5860538112 :         if (xfs_is_shutdown(mp))
    3896             :                 return -EIO;
    3897             : 
    3898  2930263753 :         XFS_STATS_INC(mp, xs_blk_mapr);
    3899             : 
    3900  2930739627 :         error = xfs_iread_extents(NULL, ip, whichfork);
    3901  2930922540 :         if (error)
    3902             :                 return error;
    3903             : 
    3904  2930951315 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
    3905   277010779 :                 eof = true;
    3906  2930786626 :         end = bno + len;
    3907  2930786626 :         obno = bno;
    3908             : 
    3909  3428025451 :         while (bno < end && n < *nmap) {
    3910             :                 /* Reading past eof, act as though there's a hole up to end. */
    3911  2930916817 :                 if (eof)
    3912   277009656 :                         got.br_startoff = end;
    3913  2930916817 :                 if (got.br_startoff > bno) {
    3914             :                         /* Reading in a hole.  */
    3915   497100763 :                         mval->br_startoff = bno;
    3916   497100763 :                         mval->br_startblock = HOLESTARTBLOCK;
    3917   497100763 :                         mval->br_blockcount =
    3918   497100763 :                                 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
    3919   497100763 :                         mval->br_state = XFS_EXT_NORM;
    3920   497100763 :                         bno += mval->br_blockcount;
    3921   497100763 :                         len -= mval->br_blockcount;
    3922   497100763 :                         mval++;
    3923   497100763 :                         n++;
    3924   497100763 :                         continue;
    3925             :                 }
    3926             : 
    3927             :                 /* set up the extent map to return. */
    3928  2433816054 :                 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
    3929  2434699785 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    3930             : 
    3931             :                 /* If we're done, stop now. */
    3932  2435358234 :                 if (bno >= end || n >= *nmap)
    3933             :                         break;
    3934             : 
    3935             :                 /* Else go on to the next record. */
    3936      138062 :                 if (!xfs_iext_next_extent(ifp, &icur, &got))
    3937         190 :                         eof = true;
    3938             :         }
    3939  2932328806 :         *nmap = n;
    3940  2932328806 :         return 0;
    3941             : }
    3942             : 
    3943             : /*
    3944             :  * Add a delayed allocation extent to an inode. Blocks are reserved from the
    3945             :  * global pool and the extent inserted into the inode in-core extent tree.
    3946             :  *
    3947             :  * On entry, got refers to the first extent beyond the offset of the extent to
    3948             :  * allocate or eof is specified if no such extent exists. On return, got refers
    3949             :  * to the extent record that was inserted to the inode fork.
    3950             :  *
    3951             :  * Note that the allocated extent may have been merged with contiguous extents
    3952             :  * during insertion into the inode fork. Thus, got does not reflect the current
    3953             :  * state of the inode fork on return. If necessary, the caller can use lastx to
    3954             :  * look up the updated record in the inode fork.
    3955             :  */
    3956             : int
    3957    16521922 : xfs_bmapi_reserve_delalloc(
    3958             :         struct xfs_inode        *ip,
    3959             :         int                     whichfork,
    3960             :         xfs_fileoff_t           off,
    3961             :         xfs_filblks_t           len,
    3962             :         xfs_filblks_t           prealloc,
    3963             :         struct xfs_bmbt_irec    *got,
    3964             :         struct xfs_iext_cursor  *icur,
    3965             :         int                     eof)
    3966             : {
    3967    16521922 :         struct xfs_mount        *mp = ip->i_mount;
    3968    16521922 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    3969    16522162 :         xfs_extlen_t            alen;
    3970    16522162 :         xfs_extlen_t            indlen;
    3971    16522162 :         int                     error;
    3972    16522162 :         xfs_fileoff_t           aoff = off;
    3973             : 
    3974             :         /*
    3975             :          * Cap the alloc length. Keep track of prealloc so we know whether to
    3976             :          * tag the inode before we return.
    3977             :          */
    3978    16522162 :         alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
    3979    16522162 :         if (!eof)
    3980     7522885 :                 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
    3981    16522162 :         if (prealloc && alen >= len)
    3982     2616344 :                 prealloc = alen - len;
    3983             : 
    3984             :         /* Figure out the extent size, adjust alen */
    3985    16522162 :         if (whichfork == XFS_COW_FORK) {
    3986      170094 :                 struct xfs_bmbt_irec    prev;
    3987      170094 :                 xfs_extlen_t            extsz = xfs_get_cowextsz_hint(ip);
    3988             : 
    3989      170094 :                 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
    3990      101172 :                         prev.br_startoff = NULLFILEOFF;
    3991             : 
    3992      170094 :                 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
    3993             :                                                1, 0, &aoff, &alen);
    3994      170094 :                 ASSERT(!error);
    3995             :         }
    3996             : 
    3997             :         /*
    3998             :          * Make a transaction-less quota reservation for delayed allocation
    3999             :          * blocks.  This number gets adjusted later.  We return if we haven't
    4000             :          * allocated blocks already inside this loop.
    4001             :          */
    4002    16522162 :         error = xfs_quota_reserve_blkres(ip, alen);
    4003    16522752 :         if (error)
    4004             :                 return error;
    4005             : 
    4006             :         /*
    4007             :          * Split changing sb for alen and indlen since they could be coming
    4008             :          * from different places.
    4009             :          */
    4010    16521693 :         indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
    4011    16521752 :         ASSERT(indlen > 0);
    4012             : 
    4013    16521752 :         error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
    4014    16521807 :         if (error)
    4015      781859 :                 goto out_unreserve_quota;
    4016             : 
    4017    15739948 :         error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
    4018    15735289 :         if (error)
    4019      203416 :                 goto out_unreserve_blocks;
    4020             : 
    4021             : 
    4022    15531873 :         ip->i_delayed_blks += alen;
    4023    15531873 :         xfs_mod_delalloc(ip->i_mount, alen + indlen);
    4024             : 
    4025    15536499 :         got->br_startoff = aoff;
    4026    15536499 :         got->br_startblock = nullstartblock(indlen);
    4027    15536474 :         got->br_blockcount = alen;
    4028    15536474 :         got->br_state = XFS_EXT_NORM;
    4029             : 
    4030    15536474 :         xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
    4031             : 
    4032             :         /*
    4033             :          * Tag the inode if blocks were preallocated. Note that COW fork
    4034             :          * preallocation can occur at the start or end of the extent, even when
    4035             :          * prealloc == 0, so we must also check the aligned offset and length.
    4036             :          */
    4037    15536257 :         if (whichfork == XFS_DATA_FORK && prealloc)
    4038     2437180 :                 xfs_inode_set_eofblocks_tag(ip);
    4039    15536278 :         if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
    4040      159701 :                 xfs_inode_set_cowblocks_tag(ip);
    4041             : 
    4042             :         return 0;
    4043             : 
    4044             : out_unreserve_blocks:
    4045      203416 :         xfs_mod_fdblocks(mp, alen, false);
    4046      985276 : out_unreserve_quota:
    4047      985276 :         if (XFS_IS_QUOTA_ON(mp))
    4048      985260 :                 xfs_quota_unreserve_blkres(ip, alen);
    4049             :         return error;
    4050             : }
    4051             : 
    4052             : static int
    4053    27865837 : xfs_bmap_alloc_userdata(
    4054             :         struct xfs_bmalloca     *bma)
    4055             : {
    4056    27865837 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4057    27865837 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4058    27865837 :         int                     error;
    4059             : 
    4060             :         /*
    4061             :          * Set the data type being allocated. For the data fork, the first data
    4062             :          * in the file is treated differently to all other allocations. For the
    4063             :          * attribute fork, we only need to ensure the allocated range is not on
    4064             :          * the busy list.
    4065             :          */
    4066    27865837 :         bma->datatype = XFS_ALLOC_NOBUSY;
    4067    27865837 :         if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
    4068    27865385 :                 bma->datatype |= XFS_ALLOC_USERDATA;
    4069    27865385 :                 if (bma->offset == 0)
    4070     2514352 :                         bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
    4071             : 
    4072    27865385 :                 if (mp->m_dalign && bma->length >= mp->m_dalign) {
    4073        6416 :                         error = xfs_bmap_isaeof(bma, whichfork);
    4074        6416 :                         if (error)
    4075             :                                 return error;
    4076             :                 }
    4077             : 
    4078    27865385 :                 if (XFS_IS_REALTIME_INODE(bma->ip))
    4079     6825199 :                         return xfs_bmap_rtalloc(bma);
    4080             :         }
    4081             : 
    4082    21040650 :         if (unlikely(XFS_TEST_ERROR(false, mp,
    4083             :                         XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4084      329271 :                 return xfs_bmap_exact_minlen_extent_alloc(bma);
    4085             : 
    4086    20711394 :         return xfs_bmap_btalloc(bma);
    4087             : }
    4088             : 
    4089             : static int
    4090    50479639 : xfs_bmapi_allocate(
    4091             :         struct xfs_bmalloca     *bma)
    4092             : {
    4093    50479639 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4094    50479639 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4095    50479639 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4096    50480397 :         int                     tmp_logflags = 0;
    4097    50480397 :         int                     error;
    4098             : 
    4099    50480397 :         ASSERT(bma->length > 0);
    4100             : 
    4101             :         /*
    4102             :          * For the wasdelay case, we could also just allocate the stuff asked
    4103             :          * for in this bmap call but that wouldn't be as good.
    4104             :          */
    4105    50480397 :         if (bma->wasdel) {
    4106    13186955 :                 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
    4107    13186955 :                 bma->offset = bma->got.br_startoff;
    4108    13186955 :                 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
    4109     4569013 :                         bma->prev.br_startoff = NULLFILEOFF;
    4110             :         } else {
    4111    37293442 :                 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
    4112    37293442 :                 if (!bma->eof)
    4113     8003319 :                         bma->length = XFS_FILBLKS_MIN(bma->length,
    4114             :                                         bma->got.br_startoff - bma->offset);
    4115             :         }
    4116             : 
    4117    50480396 :         if (bma->flags & XFS_BMAPI_CONTIG)
    4118     2647637 :                 bma->minlen = bma->length;
    4119             :         else
    4120    47832759 :                 bma->minlen = 1;
    4121             : 
    4122    50480396 :         if (bma->flags & XFS_BMAPI_METADATA) {
    4123    22614771 :                 if (unlikely(XFS_TEST_ERROR(false, mp,
    4124             :                                 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4125        5070 :                         error = xfs_bmap_exact_minlen_extent_alloc(bma);
    4126             :                 else
    4127    22609033 :                         error = xfs_bmap_btalloc(bma);
    4128             :         } else {
    4129    27865625 :                 error = xfs_bmap_alloc_userdata(bma);
    4130             :         }
    4131    50480552 :         if (error || bma->blkno == NULLFSBLOCK)
    4132             :                 return error;
    4133             : 
    4134    50478848 :         if (bma->flags & XFS_BMAPI_ZERO) {
    4135           0 :                 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
    4136           0 :                 if (error)
    4137             :                         return error;
    4138             :         }
    4139             : 
    4140    50478848 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
    4141     4732216 :                 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
    4142             :         /*
    4143             :          * Bump the number of extents we've allocated
    4144             :          * in this call.
    4145             :          */
    4146    50478826 :         bma->nallocs++;
    4147             : 
    4148    50478826 :         if (bma->cur)
    4149     4732437 :                 bma->cur->bc_ino.flags =
    4150     4732437 :                         bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
    4151             : 
    4152    50478826 :         bma->got.br_startoff = bma->offset;
    4153    50478826 :         bma->got.br_startblock = bma->blkno;
    4154    50478826 :         bma->got.br_blockcount = bma->length;
    4155    50478826 :         bma->got.br_state = XFS_EXT_NORM;
    4156             : 
    4157    50478826 :         if (bma->flags & XFS_BMAPI_PREALLOC)
    4158    27864571 :                 bma->got.br_state = XFS_EXT_UNWRITTEN;
    4159             : 
    4160    50478826 :         if (bma->wasdel)
    4161    13186191 :                 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
    4162             :         else
    4163    37292635 :                 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
    4164             :                                 whichfork, &bma->icur, &bma->cur, &bma->got,
    4165             :                                 &bma->logflags, bma->flags);
    4166             : 
    4167    50477993 :         bma->logflags |= tmp_logflags;
    4168    50477993 :         if (error)
    4169             :                 return error;
    4170             : 
    4171             :         /*
    4172             :          * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
    4173             :          * or xfs_bmap_add_extent_hole_real might have merged it into one of
    4174             :          * the neighbouring ones.
    4175             :          */
    4176    50476711 :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4177             : 
    4178    50476554 :         ASSERT(bma->got.br_startoff <= bma->offset);
    4179    50476554 :         ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
    4180             :                bma->offset + bma->length);
    4181    50476554 :         ASSERT(bma->got.br_state == XFS_EXT_NORM ||
    4182             :                bma->got.br_state == XFS_EXT_UNWRITTEN);
    4183             :         return 0;
    4184             : }
    4185             : 
    4186             : STATIC int
    4187    61197378 : xfs_bmapi_convert_unwritten(
    4188             :         struct xfs_bmalloca     *bma,
    4189             :         struct xfs_bmbt_irec    *mval,
    4190             :         xfs_filblks_t           len,
    4191             :         uint32_t                flags)
    4192             : {
    4193    61197378 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4194    61197378 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4195    61196958 :         int                     tmp_logflags = 0;
    4196    61196958 :         int                     error;
    4197             : 
    4198             :         /* check if we need to do unwritten->real conversion */
    4199    61196958 :         if (mval->br_state == XFS_EXT_UNWRITTEN &&
    4200    36307262 :             (flags & XFS_BMAPI_PREALLOC))
    4201             :                 return 0;
    4202             : 
    4203             :         /* check if we need to do real->unwritten conversion */
    4204    45752266 :         if (mval->br_state == XFS_EXT_NORM &&
    4205    24889458 :             (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
    4206             :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
    4207             :                 return 0;
    4208             : 
    4209             :         /*
    4210             :          * Modify (by adding) the state flag, if writing.
    4211             :          */
    4212    20862808 :         ASSERT(mval->br_blockcount <= len);
    4213    20862808 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
    4214     2927082 :                 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
    4215             :                                         bma->ip, whichfork);
    4216             :         }
    4217    20862807 :         mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
    4218    20862807 :                                 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
    4219             : 
    4220             :         /*
    4221             :          * Before insertion into the bmbt, zero the range being converted
    4222             :          * if required.
    4223             :          */
    4224    20862807 :         if (flags & XFS_BMAPI_ZERO) {
    4225           0 :                 error = xfs_zero_extent(bma->ip, mval->br_startblock,
    4226           0 :                                         mval->br_blockcount);
    4227           0 :                 if (error)
    4228             :                         return error;
    4229             :         }
    4230             : 
    4231    20862807 :         error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
    4232             :                         &bma->icur, &bma->cur, mval, &tmp_logflags);
    4233             :         /*
    4234             :          * Log the inode core unconditionally in the unwritten extent conversion
    4235             :          * path because the conversion might not have done so (e.g., if the
    4236             :          * extent count hasn't changed). We need to make sure the inode is dirty
    4237             :          * in the transaction for the sake of fsync(), even if nothing has
    4238             :          * changed, because fsync() will not force the log for this transaction
    4239             :          * unless it sees the inode pinned.
    4240             :          *
    4241             :          * Note: If we're only converting cow fork extents, there aren't
    4242             :          * any on-disk updates to make, so we don't need to log anything.
    4243             :          */
    4244    20862835 :         if (whichfork != XFS_COW_FORK)
    4245    20862835 :                 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
    4246    20862835 :         if (error)
    4247             :                 return error;
    4248             : 
    4249             :         /*
    4250             :          * Update our extent pointer, given that
    4251             :          * xfs_bmap_add_extent_unwritten_real might have merged it into one
    4252             :          * of the neighbouring ones.
    4253             :          */
    4254    20862860 :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4255             : 
    4256             :         /*
    4257             :          * We may have combined previously unwritten space with written space,
    4258             :          * so generate another request.
    4259             :          */
    4260    20862629 :         if (mval->br_blockcount < len)
    4261      320459 :                 return -EAGAIN;
    4262             :         return 0;
    4263             : }
    4264             : 
    4265             : xfs_extlen_t
    4266    74212464 : xfs_bmapi_minleft(
    4267             :         struct xfs_trans        *tp,
    4268             :         struct xfs_inode        *ip,
    4269             :         int                     fork)
    4270             : {
    4271    74212464 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, fork);
    4272             : 
    4273    74213920 :         if (tp && tp->t_highest_agno != NULLAGNUMBER)
    4274             :                 return 0;
    4275    74184671 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
    4276             :                 return 1;
    4277     8055584 :         return be16_to_cpu(ifp->if_broot->bb_level) + 1;
    4278             : }
    4279             : 
    4280             : /*
    4281             :  * Log whatever the flags say, even if error.  Otherwise we might miss detecting
    4282             :  * a case where the data is changed, there's an error, and it's not logged so we
    4283             :  * don't shutdown when we should.  Don't bother logging extents/btree changes if
    4284             :  * we converted to the other format.
    4285             :  */
    4286             : static void
    4287    74015459 : xfs_bmapi_finish(
    4288             :         struct xfs_bmalloca     *bma,
    4289             :         int                     whichfork,
    4290             :         int                     error)
    4291             : {
    4292    74015459 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4293             : 
    4294    76856881 :         if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
    4295    63556834 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    4296      229230 :                 bma->logflags &= ~xfs_ilog_fext(whichfork);
    4297    76627614 :         else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
    4298           0 :                  ifp->if_format != XFS_DINODE_FMT_BTREE)
    4299           0 :                 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
    4300             : 
    4301    74017657 :         if (bma->logflags)
    4302    71080075 :                 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
    4303    74019577 :         if (bma->cur)
    4304     7888465 :                 xfs_btree_del_cursor(bma->cur, error);
    4305    74019554 : }
    4306             : 
    4307             : /*
    4308             :  * Map file blocks to filesystem blocks, and allocate blocks or convert the
    4309             :  * extent state if necessary.  Details behaviour is controlled by the flags
    4310             :  * parameter.  Only allocates blocks from a single allocation group, to avoid
    4311             :  * locking problems.
    4312             :  */
    4313             : int
    4314    60879004 : xfs_bmapi_write(
    4315             :         struct xfs_trans        *tp,            /* transaction pointer */
    4316             :         struct xfs_inode        *ip,            /* incore inode */
    4317             :         xfs_fileoff_t           bno,            /* starting file offs. mapped */
    4318             :         xfs_filblks_t           len,            /* length to map in file */
    4319             :         uint32_t                flags,          /* XFS_BMAPI_... */
    4320             :         xfs_extlen_t            total,          /* total blocks needed */
    4321             :         struct xfs_bmbt_irec    *mval,          /* output: map values */
    4322             :         int                     *nmap)          /* i/o: mval size/count */
    4323             : {
    4324    60879004 :         struct xfs_bmalloca     bma = {
    4325             :                 .tp             = tp,
    4326             :                 .ip             = ip,
    4327             :                 .total          = total,
    4328             :         };
    4329    60879004 :         struct xfs_mount        *mp = ip->i_mount;
    4330    60879004 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4331    60879004 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4332    60880309 :         xfs_fileoff_t           end;            /* end of mapped file region */
    4333    60880309 :         bool                    eof = false;    /* after the end of extents */
    4334    60880309 :         int                     error;          /* error return */
    4335    60880309 :         int                     n;              /* current extent index */
    4336    60880309 :         xfs_fileoff_t           obno;           /* old block number (offset) */
    4337             : 
    4338             : #ifdef DEBUG
    4339    60880309 :         xfs_fileoff_t           orig_bno;       /* original block number value */
    4340    60880309 :         int                     orig_flags;     /* original flags arg value */
    4341    60880309 :         xfs_filblks_t           orig_len;       /* original value of len arg */
    4342    60880309 :         struct xfs_bmbt_irec    *orig_mval;     /* original value of mval */
    4343    60880309 :         int                     orig_nmap;      /* original value of *nmap */
    4344             : 
    4345    60880309 :         orig_bno = bno;
    4346    60880309 :         orig_len = len;
    4347    60880309 :         orig_flags = flags;
    4348    60880309 :         orig_mval = mval;
    4349    60880309 :         orig_nmap = *nmap;
    4350             : #endif
    4351             : 
    4352    60880309 :         ASSERT(*nmap >= 1);
    4353    60880309 :         ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
    4354    60880309 :         ASSERT(tp != NULL);
    4355    60880309 :         ASSERT(len > 0);
    4356    60880309 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
    4357    60880309 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4358    60879715 :         ASSERT(!(flags & XFS_BMAPI_REMAP));
    4359             : 
    4360             :         /* zeroing is for currently only for data extents, not metadata */
    4361    60879715 :         ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
    4362             :                         (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
    4363             :         /*
    4364             :          * we can allocate unwritten extents or pre-zero allocated blocks,
    4365             :          * but it makes no sense to do both at once. This would result in
    4366             :          * zeroing the unwritten extent twice, but it still being an
    4367             :          * unwritten extent....
    4368             :          */
    4369    60879715 :         ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
    4370             :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
    4371             : 
    4372   121759527 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4373    60879715 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4374           0 :                 return -EFSCORRUPTED;
    4375             :         }
    4376             : 
    4377   121759624 :         if (xfs_is_shutdown(mp))
    4378             :                 return -EIO;
    4379             : 
    4380    60879777 :         XFS_STATS_INC(mp, xs_blk_mapw);
    4381             : 
    4382    60879289 :         error = xfs_iread_extents(tp, ip, whichfork);
    4383    60879717 :         if (error)
    4384           0 :                 goto error0;
    4385             : 
    4386    60879717 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
    4387    29290881 :                 eof = true;
    4388    60879677 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4389    29739723 :                 bma.prev.br_startoff = NULLFILEOFF;
    4390    60879441 :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4391             : 
    4392    60879435 :         n = 0;
    4393    60879435 :         end = bno + len;
    4394    60879435 :         obno = bno;
    4395    61199513 :         while (bno < end && n < *nmap) {
    4396    61199513 :                 bool                    need_alloc = false, wasdelay = false;
    4397             : 
    4398             :                 /* in hole or beyond EOF? */
    4399    61199513 :                 if (eof || bma.got.br_startoff > bno) {
    4400             :                         /*
    4401             :                          * CoW fork conversions should /never/ hit EOF or
    4402             :                          * holes.  There should always be something for us
    4403             :                          * to work on.
    4404             :                          */
    4405    37293116 :                         ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
    4406             :                                  (flags & XFS_BMAPI_COWFORK)));
    4407             : 
    4408             :                         need_alloc = true;
    4409    23906397 :                 } else if (isnullstartblock(bma.got.br_startblock)) {
    4410       46550 :                         wasdelay = true;
    4411             :                 }
    4412             : 
    4413             :                 /*
    4414             :                  * First, deal with the hole before the allocated space
    4415             :                  * that we found, if any.
    4416             :                  */
    4417    61199513 :                 if (need_alloc || wasdelay) {
    4418    37339835 :                         bma.eof = eof;
    4419    37339835 :                         bma.conv = !!(flags & XFS_BMAPI_CONVERT);
    4420    37339835 :                         bma.wasdel = wasdelay;
    4421    37339835 :                         bma.offset = bno;
    4422    37339835 :                         bma.flags = flags;
    4423             : 
    4424             :                         /*
    4425             :                          * There's a 32/64 bit type mismatch between the
    4426             :                          * allocation length request (which can be 64 bits in
    4427             :                          * length) and the bma length request, which is
    4428             :                          * xfs_extlen_t and therefore 32 bits. Hence we have to
    4429             :                          * check for 32-bit overflows and handle them here.
    4430             :                          */
    4431    37339835 :                         if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
    4432        6570 :                                 bma.length = XFS_MAX_BMBT_EXTLEN;
    4433             :                         else
    4434    37333265 :                                 bma.length = len;
    4435             : 
    4436    37339835 :                         ASSERT(len > 0);
    4437    37339835 :                         ASSERT(bma.length > 0);
    4438    37339835 :                         error = xfs_bmapi_allocate(&bma);
    4439    37338971 :                         if (error)
    4440        2032 :                                 goto error0;
    4441    37336939 :                         if (bma.blkno == NULLFSBLOCK)
    4442             :                                 break;
    4443             : 
    4444             :                         /*
    4445             :                          * If this is a CoW allocation, record the data in
    4446             :                          * the refcount btree for orphan recovery.
    4447             :                          */
    4448    37336899 :                         if (whichfork == XFS_COW_FORK)
    4449      198264 :                                 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
    4450             :                                                 bma.length);
    4451             :                 }
    4452             : 
    4453             :                 /* Deal with the allocated space we found.  */
    4454    61196577 :                 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
    4455             :                                                         end, n, flags);
    4456             : 
    4457             :                 /* Execute unwritten extent conversion if necessary */
    4458    61196758 :                 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
    4459    61195907 :                 if (error == -EAGAIN)
    4460      320458 :                         continue;
    4461    60875449 :                 if (error)
    4462           3 :                         goto error0;
    4463             : 
    4464             :                 /* update the extent map to return */
    4465    60875446 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    4466             : 
    4467             :                 /*
    4468             :                  * If we're done, stop now.  Stop when we've allocated
    4469             :                  * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise
    4470             :                  * the transaction may get too big.
    4471             :                  */
    4472    60876236 :                 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
    4473             :                         break;
    4474             : 
    4475             :                 /* Else go on to the next record. */
    4476          80 :                 bma.prev = bma.got;
    4477          80 :                 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
    4478          24 :                         eof = true;
    4479             :         }
    4480    60876196 :         *nmap = n;
    4481             : 
    4482    60876196 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4483             :                         whichfork);
    4484    60874677 :         if (error)
    4485           0 :                 goto error0;
    4486             : 
    4487    60874677 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
    4488             :                ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
    4489    60874677 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4490    60876207 :         xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
    4491             :                 orig_nmap, *nmap);
    4492    60876207 :         return 0;
    4493        2035 : error0:
    4494        2035 :         xfs_bmapi_finish(&bma, whichfork, error);
    4495        2035 :         return error;
    4496             : }
    4497             : 
    4498             : /*
    4499             :  * Convert an existing delalloc extent to real blocks based on file offset. This
    4500             :  * attempts to allocate the entire delalloc extent and may require multiple
    4501             :  * invocations to allocate the target offset if a large enough physical extent
    4502             :  * is not available.
    4503             :  */
    4504             : int
    4505    13326175 : xfs_bmapi_convert_delalloc(
    4506             :         struct xfs_inode        *ip,
    4507             :         int                     whichfork,
    4508             :         xfs_off_t               offset,
    4509             :         struct iomap            *iomap,
    4510             :         unsigned int            *seq)
    4511             : {
    4512    13326175 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4513    13326180 :         struct xfs_mount        *mp = ip->i_mount;
    4514    13326180 :         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
    4515    13326180 :         struct xfs_bmalloca     bma = { NULL };
    4516    13326180 :         uint16_t                flags = 0;
    4517    13326180 :         struct xfs_trans        *tp;
    4518    13326180 :         int                     error;
    4519             : 
    4520    13326180 :         if (whichfork == XFS_COW_FORK)
    4521      312201 :                 flags |= IOMAP_F_SHARED;
    4522             : 
    4523             :         /*
    4524             :          * Space for the extent and indirect blocks was reserved when the
    4525             :          * delalloc extent was created so there's no need to do so here.
    4526             :          */
    4527    13326180 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
    4528             :                                 XFS_TRANS_RESERVE, &tp);
    4529    13326195 :         if (error)
    4530             :                 return error;
    4531             : 
    4532    13326184 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    4533    13326182 :         xfs_trans_ijoin(tp, ip, 0);
    4534             : 
    4535    13326156 :         error = xfs_iext_count_may_overflow(ip, whichfork,
    4536             :                         XFS_IEXT_ADD_NOSPLIT_CNT);
    4537    13326172 :         if (error == -EFBIG)
    4538           0 :                 error = xfs_iext_count_upgrade(tp, ip,
    4539             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
    4540    13326172 :         if (error)
    4541           0 :                 goto out_trans_cancel;
    4542             : 
    4543    13326172 :         if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
    4544    13326180 :             bma.got.br_startoff > offset_fsb) {
    4545             :                 /*
    4546             :                  * No extent found in the range we are trying to convert.  This
    4547             :                  * should only happen for the COW fork, where another thread
    4548             :                  * might have moved the extent to the data fork in the meantime.
    4549             :                  */
    4550           0 :                 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
    4551           0 :                 error = -EAGAIN;
    4552           0 :                 goto out_trans_cancel;
    4553             :         }
    4554             : 
    4555             :         /*
    4556             :          * If we find a real extent here we raced with another thread converting
    4557             :          * the extent.  Just return the real extent at this offset.
    4558             :          */
    4559    13326180 :         if (!isnullstartblock(bma.got.br_startblock)) {
    4560      185770 :                 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4561             :                                 xfs_iomap_inode_sequence(ip, flags));
    4562      185739 :                 *seq = READ_ONCE(ifp->if_seq);
    4563      185739 :                 goto out_trans_cancel;
    4564             :         }
    4565             : 
    4566    13140410 :         bma.tp = tp;
    4567    13140410 :         bma.ip = ip;
    4568    13140410 :         bma.wasdel = true;
    4569    13140410 :         bma.offset = bma.got.br_startoff;
    4570    13140410 :         bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
    4571             :                         XFS_MAX_BMBT_EXTLEN);
    4572    13140410 :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4573             : 
    4574             :         /*
    4575             :          * When we're converting the delalloc reservations backing dirty pages
    4576             :          * in the page cache, we must be careful about how we create the new
    4577             :          * extents:
    4578             :          *
    4579             :          * New CoW fork extents are created unwritten, turned into real extents
    4580             :          * when we're about to write the data to disk, and mapped into the data
    4581             :          * fork after the write finishes.  End of story.
    4582             :          *
    4583             :          * New data fork extents must be mapped in as unwritten and converted
    4584             :          * to real extents after the write succeeds to avoid exposing stale
    4585             :          * disk contents if we crash.
    4586             :          */
    4587    13140405 :         bma.flags = XFS_BMAPI_PREALLOC;
    4588    13140405 :         if (whichfork == XFS_COW_FORK)
    4589      126532 :                 bma.flags |= XFS_BMAPI_COWFORK;
    4590             : 
    4591    13140405 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4592     4567397 :                 bma.prev.br_startoff = NULLFILEOFF;
    4593             : 
    4594    13140403 :         error = xfs_bmapi_allocate(&bma);
    4595    13140400 :         if (error)
    4596         780 :                 goto out_finish;
    4597             : 
    4598    13139620 :         error = -ENOSPC;
    4599    13139620 :         if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
    4600           0 :                 goto out_finish;
    4601    13139620 :         error = -EFSCORRUPTED;
    4602    26279240 :         if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
    4603           0 :                 goto out_finish;
    4604             : 
    4605    13139620 :         XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
    4606    13139619 :         XFS_STATS_INC(mp, xs_xstrat_quick);
    4607             : 
    4608    13139618 :         ASSERT(!isnullstartblock(bma.got.br_startblock));
    4609    13139618 :         xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4610             :                                 xfs_iomap_inode_sequence(ip, flags));
    4611    13139622 :         *seq = READ_ONCE(ifp->if_seq);
    4612             : 
    4613    13139622 :         if (whichfork == XFS_COW_FORK)
    4614      126516 :                 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
    4615             : 
    4616    13139625 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4617             :                         whichfork);
    4618    13139614 :         if (error)
    4619           0 :                 goto out_finish;
    4620             : 
    4621    13139614 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4622    13139624 :         error = xfs_trans_commit(tp);
    4623    13139632 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4624    13139632 :         return error;
    4625             : 
    4626         780 : out_finish:
    4627         780 :         xfs_bmapi_finish(&bma, whichfork, error);
    4628      186519 : out_trans_cancel:
    4629      186519 :         xfs_trans_cancel(tp);
    4630      186521 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4631      186521 :         return error;
    4632             : }
    4633             : 
    4634             : int
    4635    50298473 : xfs_bmapi_remap(
    4636             :         struct xfs_trans        *tp,
    4637             :         struct xfs_inode        *ip,
    4638             :         xfs_fileoff_t           bno,
    4639             :         xfs_filblks_t           len,
    4640             :         xfs_fsblock_t           startblock,
    4641             :         uint32_t                flags)
    4642             : {
    4643    50298473 :         struct xfs_mount        *mp = ip->i_mount;
    4644    50298473 :         struct xfs_ifork        *ifp;
    4645    50298473 :         struct xfs_btree_cur    *cur = NULL;
    4646    50298473 :         struct xfs_bmbt_irec    got;
    4647    50298473 :         struct xfs_iext_cursor  icur;
    4648    50298473 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4649    50298473 :         int                     logflags = 0, error;
    4650             : 
    4651    50298473 :         ifp = xfs_ifork_ptr(ip, whichfork);
    4652    50298473 :         ASSERT(len > 0);
    4653    50298473 :         ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
    4654    50298473 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4655    50298473 :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
    4656             :                            XFS_BMAPI_NORMAP)));
    4657    50298473 :         ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
    4658             :                         (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
    4659             : 
    4660   100596946 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4661    50298473 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4662           0 :                 return -EFSCORRUPTED;
    4663             :         }
    4664             : 
    4665   100596946 :         if (xfs_is_shutdown(mp))
    4666             :                 return -EIO;
    4667             : 
    4668    50298472 :         error = xfs_iread_extents(tp, ip, whichfork);
    4669    50298472 :         if (error)
    4670             :                 return error;
    4671             : 
    4672    50298472 :         if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
    4673             :                 /* make sure we only reflink into a hole. */
    4674     2813386 :                 ASSERT(got.br_startoff > bno);
    4675     2813386 :                 ASSERT(got.br_startoff - bno >= len);
    4676             :         }
    4677             : 
    4678    50298472 :         ip->i_nblocks += len;
    4679    50298472 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    4680             : 
    4681    50298472 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    4682    48739898 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    4683    48739898 :                 cur->bc_ino.flags = 0;
    4684             :         }
    4685             : 
    4686    50298472 :         got.br_startoff = bno;
    4687    50298472 :         got.br_startblock = startblock;
    4688    50298472 :         got.br_blockcount = len;
    4689    50298472 :         if (flags & XFS_BMAPI_PREALLOC)
    4690           0 :                 got.br_state = XFS_EXT_UNWRITTEN;
    4691             :         else
    4692    50298472 :                 got.br_state = XFS_EXT_NORM;
    4693             : 
    4694    50298472 :         error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
    4695             :                         &cur, &got, &logflags, flags);
    4696    50298472 :         if (error)
    4697           2 :                 goto error0;
    4698             : 
    4699    50298470 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
    4700             : 
    4701    50298472 : error0:
    4702    50298472 :         if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
    4703    48755375 :                 logflags &= ~XFS_ILOG_DEXT;
    4704     1543097 :         else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
    4705     1543097 :                 logflags &= ~XFS_ILOG_DBROOT;
    4706             : 
    4707    50298472 :         if (logflags)
    4708    49633777 :                 xfs_trans_log_inode(tp, ip, logflags);
    4709    50298472 :         if (cur)
    4710    48755473 :                 xfs_btree_del_cursor(cur, error);
    4711             :         return error;
    4712             : }
    4713             : 
    4714             : /*
    4715             :  * When a delalloc extent is split (e.g., due to a hole punch), the original
    4716             :  * indlen reservation must be shared across the two new extents that are left
    4717             :  * behind.
    4718             :  *
    4719             :  * Given the original reservation and the worst case indlen for the two new
    4720             :  * extents (as calculated by xfs_bmap_worst_indlen()), split the original
    4721             :  * reservation fairly across the two new extents. If necessary, steal available
    4722             :  * blocks from a deleted extent to make up a reservation deficiency (e.g., if
    4723             :  * ores == 1). The number of stolen blocks is returned. The availability and
    4724             :  * subsequent accounting of stolen blocks is the responsibility of the caller.
    4725             :  */
    4726             : static xfs_filblks_t
    4727         251 : xfs_bmap_split_indlen(
    4728             :         xfs_filblks_t                   ores,           /* original res. */
    4729             :         xfs_filblks_t                   *indlen1,       /* ext1 worst indlen */
    4730             :         xfs_filblks_t                   *indlen2,       /* ext2 worst indlen */
    4731             :         xfs_filblks_t                   avail)          /* stealable blocks */
    4732             : {
    4733         251 :         xfs_filblks_t                   len1 = *indlen1;
    4734         251 :         xfs_filblks_t                   len2 = *indlen2;
    4735         251 :         xfs_filblks_t                   nres = len1 + len2; /* new total res. */
    4736         251 :         xfs_filblks_t                   stolen = 0;
    4737         251 :         xfs_filblks_t                   resfactor;
    4738             : 
    4739             :         /*
    4740             :          * Steal as many blocks as we can to try and satisfy the worst case
    4741             :          * indlen for both new extents.
    4742             :          */
    4743         251 :         if (ores < nres && avail)
    4744         251 :                 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
    4745         251 :         ores += stolen;
    4746             : 
    4747             :          /* nothing else to do if we've satisfied the new reservation */
    4748         251 :         if (ores >= nres)
    4749             :                 return stolen;
    4750             : 
    4751             :         /*
    4752             :          * We can't meet the total required reservation for the two extents.
    4753             :          * Calculate the percent of the overall shortage between both extents
    4754             :          * and apply this percentage to each of the requested indlen values.
    4755             :          * This distributes the shortage fairly and reduces the chances that one
    4756             :          * of the two extents is left with nothing when extents are repeatedly
    4757             :          * split.
    4758             :          */
    4759         123 :         resfactor = (ores * 100);
    4760         123 :         do_div(resfactor, nres);
    4761         123 :         len1 *= resfactor;
    4762         123 :         do_div(len1, 100);
    4763         123 :         len2 *= resfactor;
    4764         123 :         do_div(len2, 100);
    4765         123 :         ASSERT(len1 + len2 <= ores);
    4766         123 :         ASSERT(len1 < *indlen1 && len2 < *indlen2);
    4767             : 
    4768             :         /*
    4769             :          * Hand out the remainder to each extent. If one of the two reservations
    4770             :          * is zero, we want to make sure that one gets a block first. The loop
    4771             :          * below starts with len1, so hand len2 a block right off the bat if it
    4772             :          * is zero.
    4773             :          */
    4774         123 :         ores -= (len1 + len2);
    4775         123 :         ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
    4776         123 :         if (ores && !len2 && *indlen2) {
    4777           0 :                 len2++;
    4778           0 :                 ores--;
    4779             :         }
    4780         190 :         while (ores) {
    4781         123 :                 if (len1 < *indlen1) {
    4782         123 :                         len1++;
    4783         123 :                         ores--;
    4784             :                 }
    4785         123 :                 if (!ores)
    4786             :                         break;
    4787          67 :                 if (len2 < *indlen2) {
    4788          67 :                         len2++;
    4789          67 :                         ores--;
    4790             :                 }
    4791             :         }
    4792             : 
    4793         123 :         *indlen1 = len1;
    4794         123 :         *indlen2 = len2;
    4795             : 
    4796         123 :         return stolen;
    4797             : }
    4798             : 
    4799             : int
    4800     1046050 : xfs_bmap_del_extent_delay(
    4801             :         struct xfs_inode        *ip,
    4802             :         int                     whichfork,
    4803             :         struct xfs_iext_cursor  *icur,
    4804             :         struct xfs_bmbt_irec    *got,
    4805             :         struct xfs_bmbt_irec    *del)
    4806             : {
    4807     1046050 :         struct xfs_mount        *mp = ip->i_mount;
    4808     1046050 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4809     1046076 :         struct xfs_bmbt_irec    new;
    4810     1046076 :         int64_t                 da_old, da_new, da_diff = 0;
    4811     1046076 :         xfs_fileoff_t           del_endoff, got_endoff;
    4812     1046076 :         xfs_filblks_t           got_indlen, new_indlen, stolen;
    4813     1046076 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    4814     1046076 :         int                     error = 0;
    4815     1046076 :         bool                    isrt;
    4816             : 
    4817     1046076 :         XFS_STATS_INC(mp, xs_del_exlist);
    4818             : 
    4819     1046084 :         isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
    4820     1046084 :         del_endoff = del->br_startoff + del->br_blockcount;
    4821     1046084 :         got_endoff = got->br_startoff + got->br_blockcount;
    4822     1046084 :         da_old = startblockval(got->br_startblock);
    4823     1046084 :         da_new = 0;
    4824             : 
    4825     1046084 :         ASSERT(del->br_blockcount > 0);
    4826     1046084 :         ASSERT(got->br_startoff <= del->br_startoff);
    4827     1046084 :         ASSERT(got_endoff >= del_endoff);
    4828             : 
    4829     1046084 :         if (isrt) {
    4830           0 :                 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
    4831             : 
    4832           0 :                 do_div(rtexts, mp->m_sb.sb_rextsize);
    4833           0 :                 xfs_mod_frextents(mp, rtexts);
    4834             :         }
    4835             : 
    4836             :         /*
    4837             :          * Update the inode delalloc counter now and wait to update the
    4838             :          * sb counters as we might have to borrow some blocks for the
    4839             :          * indirect block accounting.
    4840             :          */
    4841     1046084 :         ASSERT(!isrt);
    4842     1046084 :         error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
    4843     1046100 :         if (error)
    4844             :                 return error;
    4845     1046100 :         ip->i_delayed_blks -= del->br_blockcount;
    4846             : 
    4847     1046100 :         if (got->br_startoff == del->br_startoff)
    4848      554512 :                 state |= BMAP_LEFT_FILLING;
    4849     1046100 :         if (got_endoff == del_endoff)
    4850     1045069 :                 state |= BMAP_RIGHT_FILLING;
    4851             : 
    4852     1046100 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    4853      553732 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    4854             :                 /*
    4855             :                  * Matches the whole extent.  Delete the entry.
    4856             :                  */
    4857      553732 :                 xfs_iext_remove(ip, icur, state);
    4858      553714 :                 xfs_iext_prev(ifp, icur);
    4859      553714 :                 break;
    4860         780 :         case BMAP_LEFT_FILLING:
    4861             :                 /*
    4862             :                  * Deleting the first part of the extent.
    4863             :                  */
    4864         780 :                 got->br_startoff = del_endoff;
    4865         780 :                 got->br_blockcount -= del->br_blockcount;
    4866         780 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4867             :                                 got->br_blockcount), da_old);
    4868         780 :                 got->br_startblock = nullstartblock((int)da_new);
    4869         780 :                 xfs_iext_update_extent(ip, state, icur, got);
    4870         780 :                 break;
    4871      491337 :         case BMAP_RIGHT_FILLING:
    4872             :                 /*
    4873             :                  * Deleting the last part of the extent.
    4874             :                  */
    4875      491337 :                 got->br_blockcount = got->br_blockcount - del->br_blockcount;
    4876      491337 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4877             :                                 got->br_blockcount), da_old);
    4878      491337 :                 got->br_startblock = nullstartblock((int)da_new);
    4879      491337 :                 xfs_iext_update_extent(ip, state, icur, got);
    4880      491337 :                 break;
    4881         251 :         case 0:
    4882             :                 /*
    4883             :                  * Deleting the middle of the extent.
    4884             :                  *
    4885             :                  * Distribute the original indlen reservation across the two new
    4886             :                  * extents.  Steal blocks from the deleted extent if necessary.
    4887             :                  * Stealing blocks simply fudges the fdblocks accounting below.
    4888             :                  * Warn if either of the new indlen reservations is zero as this
    4889             :                  * can lead to delalloc problems.
    4890             :                  */
    4891         251 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    4892         251 :                 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
    4893             : 
    4894         251 :                 new.br_blockcount = got_endoff - del_endoff;
    4895         251 :                 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
    4896             : 
    4897         251 :                 WARN_ON_ONCE(!got_indlen || !new_indlen);
    4898         251 :                 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
    4899             :                                                        del->br_blockcount);
    4900             : 
    4901         251 :                 got->br_startblock = nullstartblock((int)got_indlen);
    4902             : 
    4903         251 :                 new.br_startoff = del_endoff;
    4904         251 :                 new.br_state = got->br_state;
    4905         251 :                 new.br_startblock = nullstartblock((int)new_indlen);
    4906             : 
    4907         251 :                 xfs_iext_update_extent(ip, state, icur, got);
    4908         251 :                 xfs_iext_next(ifp, icur);
    4909         251 :                 xfs_iext_insert(ip, icur, &new, state);
    4910             : 
    4911         251 :                 da_new = got_indlen + new_indlen - stolen;
    4912         251 :                 del->br_blockcount -= stolen;
    4913         251 :                 break;
    4914             :         }
    4915             : 
    4916     1046082 :         ASSERT(da_old >= da_new);
    4917     1046082 :         da_diff = da_old - da_new;
    4918     1046082 :         if (!isrt)
    4919     1046086 :                 da_diff += del->br_blockcount;
    4920     1046082 :         if (da_diff) {
    4921     1045947 :                 xfs_mod_fdblocks(mp, da_diff, false);
    4922     1045950 :                 xfs_mod_delalloc(mp, -da_diff);
    4923             :         }
    4924             :         return error;
    4925             : }
    4926             : 
    4927             : void
    4928     2059984 : xfs_bmap_del_extent_cow(
    4929             :         struct xfs_inode        *ip,
    4930             :         struct xfs_iext_cursor  *icur,
    4931             :         struct xfs_bmbt_irec    *got,
    4932             :         struct xfs_bmbt_irec    *del)
    4933             : {
    4934     2059984 :         struct xfs_mount        *mp = ip->i_mount;
    4935     2059984 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
    4936     2059984 :         struct xfs_bmbt_irec    new;
    4937     2059984 :         xfs_fileoff_t           del_endoff, got_endoff;
    4938     2059984 :         uint32_t                state = BMAP_COWFORK;
    4939             : 
    4940     2059984 :         XFS_STATS_INC(mp, xs_del_exlist);
    4941             : 
    4942     2059984 :         del_endoff = del->br_startoff + del->br_blockcount;
    4943     2059984 :         got_endoff = got->br_startoff + got->br_blockcount;
    4944             : 
    4945     2059984 :         ASSERT(del->br_blockcount > 0);
    4946     2059984 :         ASSERT(got->br_startoff <= del->br_startoff);
    4947     2059984 :         ASSERT(got_endoff >= del_endoff);
    4948     2059984 :         ASSERT(!isnullstartblock(got->br_startblock));
    4949             : 
    4950     2059984 :         if (got->br_startoff == del->br_startoff)
    4951     2038370 :                 state |= BMAP_LEFT_FILLING;
    4952     2059984 :         if (got_endoff == del_endoff)
    4953     1799143 :                 state |= BMAP_RIGHT_FILLING;
    4954             : 
    4955     2059984 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    4956     1777530 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    4957             :                 /*
    4958             :                  * Matches the whole extent.  Delete the entry.
    4959             :                  */
    4960     1777530 :                 xfs_iext_remove(ip, icur, state);
    4961     1777530 :                 xfs_iext_prev(ifp, icur);
    4962     1777530 :                 break;
    4963      260840 :         case BMAP_LEFT_FILLING:
    4964             :                 /*
    4965             :                  * Deleting the first part of the extent.
    4966             :                  */
    4967      260840 :                 got->br_startoff = del_endoff;
    4968      260840 :                 got->br_blockcount -= del->br_blockcount;
    4969      260840 :                 got->br_startblock = del->br_startblock + del->br_blockcount;
    4970      260840 :                 xfs_iext_update_extent(ip, state, icur, got);
    4971      260840 :                 break;
    4972       21613 :         case BMAP_RIGHT_FILLING:
    4973             :                 /*
    4974             :                  * Deleting the last part of the extent.
    4975             :                  */
    4976       21613 :                 got->br_blockcount -= del->br_blockcount;
    4977       21613 :                 xfs_iext_update_extent(ip, state, icur, got);
    4978       21613 :                 break;
    4979           1 :         case 0:
    4980             :                 /*
    4981             :                  * Deleting the middle of the extent.
    4982             :                  */
    4983           1 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    4984             : 
    4985           1 :                 new.br_startoff = del_endoff;
    4986           1 :                 new.br_blockcount = got_endoff - del_endoff;
    4987           1 :                 new.br_state = got->br_state;
    4988           1 :                 new.br_startblock = del->br_startblock + del->br_blockcount;
    4989             : 
    4990           1 :                 xfs_iext_update_extent(ip, state, icur, got);
    4991           1 :                 xfs_iext_next(ifp, icur);
    4992           1 :                 xfs_iext_insert(ip, icur, &new, state);
    4993           1 :                 break;
    4994             :         }
    4995     2059984 :         ip->i_delayed_blks -= del->br_blockcount;
    4996     2059984 : }
    4997             : 
    4998             : /*
    4999             :  * Called by xfs_bmapi to update file extent records and the btree
    5000             :  * after removing space.
    5001             :  */
    5002             : STATIC int                              /* error */
    5003    78919668 : xfs_bmap_del_extent_real(
    5004             :         xfs_inode_t             *ip,    /* incore inode pointer */
    5005             :         xfs_trans_t             *tp,    /* current transaction pointer */
    5006             :         struct xfs_iext_cursor  *icur,
    5007             :         struct xfs_btree_cur    *cur,   /* if null, not a btree */
    5008             :         xfs_bmbt_irec_t         *del,   /* data to remove from extents */
    5009             :         int                     *logflagsp, /* inode logging flags */
    5010             :         int                     whichfork, /* data or attr fork */
    5011             :         uint32_t                bflags) /* bmapi flags */
    5012             : {
    5013    78919668 :         xfs_fsblock_t           del_endblock=0; /* first block past del */
    5014    78919668 :         xfs_fileoff_t           del_endoff;     /* first offset past del */
    5015    78919668 :         int                     do_fx;  /* free extent at end of routine */
    5016    78919668 :         int                     error;  /* error return value */
    5017    78919668 :         int                     flags = 0;/* inode logging flags */
    5018    78919668 :         struct xfs_bmbt_irec    got;    /* current extent entry */
    5019    78919668 :         xfs_fileoff_t           got_endoff;     /* first offset past got */
    5020    78919668 :         int                     i;      /* temp state */
    5021    78919668 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    5022    78919668 :         xfs_mount_t             *mp;    /* mount structure */
    5023    78919668 :         xfs_filblks_t           nblks;  /* quota/sb block count */
    5024    78919668 :         xfs_bmbt_irec_t         new;    /* new record to be inserted */
    5025             :         /* REFERENCED */
    5026    78919668 :         uint                    qfield; /* quota field to update */
    5027    78919668 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    5028    78919668 :         struct xfs_bmbt_irec    old;
    5029             : 
    5030    78919668 :         mp = ip->i_mount;
    5031    78919668 :         XFS_STATS_INC(mp, xs_del_exlist);
    5032             : 
    5033    78920129 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5034    78919702 :         ASSERT(del->br_blockcount > 0);
    5035    78919702 :         xfs_iext_get_extent(ifp, icur, &got);
    5036    78918567 :         ASSERT(got.br_startoff <= del->br_startoff);
    5037    78918567 :         del_endoff = del->br_startoff + del->br_blockcount;
    5038    78918567 :         got_endoff = got.br_startoff + got.br_blockcount;
    5039    78918567 :         ASSERT(got_endoff >= del_endoff);
    5040    78918567 :         ASSERT(!isnullstartblock(got.br_startblock));
    5041    78918567 :         qfield = 0;
    5042    78918567 :         error = 0;
    5043             : 
    5044             :         /*
    5045             :          * If it's the case where the directory code is running with no block
    5046             :          * reservation, and the deleted block is in the middle of its extent,
    5047             :          * and the resulting insert of an extent would cause transformation to
    5048             :          * btree format, then reject it.  The calling code will then swap blocks
    5049             :          * around instead.  We have to do this now, rather than waiting for the
    5050             :          * conversion to btree format, since the transaction will be dirty then.
    5051             :          */
    5052    78918567 :         if (tp->t_blk_res == 0 &&
    5053    65016635 :             ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
    5054    29308826 :             ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
    5055       65703 :             del->br_startoff > got.br_startoff && del_endoff < got_endoff)
    5056             :                 return -ENOSPC;
    5057             : 
    5058    78918567 :         flags = XFS_ILOG_CORE;
    5059    78918567 :         if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
    5060     2716336 :                 xfs_filblks_t   len;
    5061     2716336 :                 xfs_extlen_t    mod;
    5062             : 
    5063     2716336 :                 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
    5064             :                                   &mod);
    5065     2716336 :                 ASSERT(mod == 0);
    5066             : 
    5067     2716336 :                 if (!(bflags & XFS_BMAPI_REMAP)) {
    5068     2716336 :                         xfs_fsblock_t   bno;
    5069             : 
    5070     2716336 :                         bno = div_u64_rem(del->br_startblock,
    5071             :                                         mp->m_sb.sb_rextsize, &mod);
    5072     2716336 :                         ASSERT(mod == 0);
    5073             : 
    5074     2716336 :                         error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
    5075     2716336 :                         if (error)
    5076           0 :                                 goto done;
    5077             :                 }
    5078             : 
    5079     2716336 :                 do_fx = 0;
    5080     2716336 :                 nblks = len * mp->m_sb.sb_rextsize;
    5081     2716336 :                 qfield = XFS_TRANS_DQ_RTBCOUNT;
    5082             :         } else {
    5083    76202231 :                 do_fx = 1;
    5084    76202231 :                 nblks = del->br_blockcount;
    5085    76202231 :                 qfield = XFS_TRANS_DQ_BCOUNT;
    5086             :         }
    5087             : 
    5088    78918567 :         del_endblock = del->br_startblock + del->br_blockcount;
    5089    78918567 :         if (cur) {
    5090    43075642 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5091    43075641 :                 if (error)
    5092           0 :                         goto done;
    5093    43075641 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5094           0 :                         error = -EFSCORRUPTED;
    5095           0 :                         goto done;
    5096             :                 }
    5097             :         }
    5098             : 
    5099    78918566 :         if (got.br_startoff == del->br_startoff)
    5100    70409434 :                 state |= BMAP_LEFT_FILLING;
    5101    78918566 :         if (got_endoff == del_endoff)
    5102    70067950 :                 state |= BMAP_RIGHT_FILLING;
    5103             : 
    5104    78918566 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5105    68607455 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5106             :                 /*
    5107             :                  * Matches the whole extent.  Delete the entry.
    5108             :                  */
    5109    68607455 :                 xfs_iext_remove(ip, icur, state);
    5110    68607938 :                 xfs_iext_prev(ifp, icur);
    5111    68607977 :                 ifp->if_nextents--;
    5112             : 
    5113    68607977 :                 flags |= XFS_ILOG_CORE;
    5114    68607977 :                 if (!cur) {
    5115    32044353 :                         flags |= xfs_ilog_fext(whichfork);
    5116    32044353 :                         break;
    5117             :                 }
    5118    36563624 :                 if ((error = xfs_btree_delete(cur, &i)))
    5119           1 :                         goto done;
    5120    36563665 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5121           0 :                         error = -EFSCORRUPTED;
    5122           0 :                         goto done;
    5123             :                 }
    5124             :                 break;
    5125     1801000 :         case BMAP_LEFT_FILLING:
    5126             :                 /*
    5127             :                  * Deleting the first part of the extent.
    5128             :                  */
    5129     1801000 :                 got.br_startoff = del_endoff;
    5130     1801000 :                 got.br_startblock = del_endblock;
    5131     1801000 :                 got.br_blockcount -= del->br_blockcount;
    5132     1801000 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5133     1801000 :                 if (!cur) {
    5134     1251735 :                         flags |= xfs_ilog_fext(whichfork);
    5135     1251735 :                         break;
    5136             :                 }
    5137      549265 :                 error = xfs_bmbt_update(cur, &got);
    5138      549256 :                 if (error)
    5139           0 :                         goto done;
    5140             :                 break;
    5141     1459437 :         case BMAP_RIGHT_FILLING:
    5142             :                 /*
    5143             :                  * Deleting the last part of the extent.
    5144             :                  */
    5145     1459437 :                 got.br_blockcount -= del->br_blockcount;
    5146     1459437 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5147     1459437 :                 if (!cur) {
    5148     1260135 :                         flags |= xfs_ilog_fext(whichfork);
    5149     1260135 :                         break;
    5150             :                 }
    5151      199302 :                 error = xfs_bmbt_update(cur, &got);
    5152      199302 :                 if (error)
    5153           0 :                         goto done;
    5154             :                 break;
    5155     7050674 :         case 0:
    5156             :                 /*
    5157             :                  * Deleting the middle of the extent.
    5158             :                  */
    5159             : 
    5160     7050674 :                 old = got;
    5161             : 
    5162     7050674 :                 got.br_blockcount = del->br_startoff - got.br_startoff;
    5163     7050674 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5164             : 
    5165     7050675 :                 new.br_startoff = del_endoff;
    5166     7050675 :                 new.br_blockcount = got_endoff - del_endoff;
    5167     7050675 :                 new.br_state = got.br_state;
    5168     7050675 :                 new.br_startblock = del_endblock;
    5169             : 
    5170     7050675 :                 flags |= XFS_ILOG_CORE;
    5171     7050675 :                 if (cur) {
    5172     5763418 :                         error = xfs_bmbt_update(cur, &got);
    5173     5763418 :                         if (error)
    5174           0 :                                 goto done;
    5175     5763418 :                         error = xfs_btree_increment(cur, 0, &i);
    5176     5763418 :                         if (error)
    5177           0 :                                 goto done;
    5178     5763418 :                         cur->bc_rec.b = new;
    5179     5763418 :                         error = xfs_btree_insert(cur, &i);
    5180     5763418 :                         if (error && error != -ENOSPC)
    5181           0 :                                 goto done;
    5182             :                         /*
    5183             :                          * If get no-space back from btree insert, it tried a
    5184             :                          * split, and we have a zero block reservation.  Fix up
    5185             :                          * our state and return the error.
    5186             :                          */
    5187     5763418 :                         if (error == -ENOSPC) {
    5188             :                                 /*
    5189             :                                  * Reset the cursor, don't trust it after any
    5190             :                                  * insert operation.
    5191             :                                  */
    5192           0 :                                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5193           0 :                                 if (error)
    5194           0 :                                         goto done;
    5195           0 :                                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5196           0 :                                         error = -EFSCORRUPTED;
    5197           0 :                                         goto done;
    5198             :                                 }
    5199             :                                 /*
    5200             :                                  * Update the btree record back
    5201             :                                  * to the original value.
    5202             :                                  */
    5203           0 :                                 error = xfs_bmbt_update(cur, &old);
    5204           0 :                                 if (error)
    5205           0 :                                         goto done;
    5206             :                                 /*
    5207             :                                  * Reset the extent record back
    5208             :                                  * to the original value.
    5209             :                                  */
    5210           0 :                                 xfs_iext_update_extent(ip, state, icur, &old);
    5211           0 :                                 flags = 0;
    5212           0 :                                 error = -ENOSPC;
    5213           0 :                                 goto done;
    5214             :                         }
    5215     5763418 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5216           0 :                                 error = -EFSCORRUPTED;
    5217           0 :                                 goto done;
    5218             :                         }
    5219             :                 } else
    5220     1287277 :                         flags |= xfs_ilog_fext(whichfork);
    5221             : 
    5222     7050675 :                 ifp->if_nextents++;
    5223     7050675 :                 xfs_iext_next(ifp, icur);
    5224     7050673 :                 xfs_iext_insert(ip, icur, &new, state);
    5225     7050673 :                 break;
    5226             :         }
    5227             : 
    5228             :         /* remove reverse mapping */
    5229    78919119 :         xfs_rmap_unmap_extent(tp, ip, whichfork, del);
    5230             : 
    5231             :         /*
    5232             :          * If we need to, add to list of extents to delete.
    5233             :          */
    5234    78920536 :         if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
    5235    71690442 :                 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
    5236    45544473 :                         xfs_refcount_decrease_extent(tp, del);
    5237             :                 } else {
    5238    26145969 :                         error = __xfs_free_extent_later(tp, del->br_startblock,
    5239             :                                         del->br_blockcount, NULL,
    5240             :                                         XFS_AG_RESV_NONE,
    5241    26145969 :                                         ((bflags & XFS_BMAPI_NODISCARD) ||
    5242    26132389 :                                         del->br_state == XFS_EXT_UNWRITTEN));
    5243    26145863 :                         if (error)
    5244           0 :                                 goto done;
    5245             :                 }
    5246             :         }
    5247             : 
    5248             :         /*
    5249             :          * Adjust inode # blocks in the file.
    5250             :          */
    5251    78920454 :         if (nblks)
    5252    78920375 :                 ip->i_nblocks -= nblks;
    5253             :         /*
    5254             :          * Adjust quota data.
    5255             :          */
    5256    78920454 :         if (qfield && !(bflags & XFS_BMAPI_REMAP))
    5257    74407216 :                 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
    5258             : 
    5259     4513238 : done:
    5260    78920040 :         *logflagsp = flags;
    5261    78920040 :         return error;
    5262             : }
    5263             : 
    5264             : /*
    5265             :  * Unmap (remove) blocks from a file.
    5266             :  * If nexts is nonzero then the number of extents to remove is limited to
    5267             :  * that value.  If not all extents in the block range can be removed then
    5268             :  * *done is set.
    5269             :  */
    5270             : int                                             /* error */
    5271    65147721 : __xfs_bunmapi(
    5272             :         struct xfs_trans        *tp,            /* transaction pointer */
    5273             :         struct xfs_inode        *ip,            /* incore inode */
    5274             :         xfs_fileoff_t           start,          /* first file offset deleted */
    5275             :         xfs_filblks_t           *rlen,          /* i/o: amount remaining */
    5276             :         uint32_t                flags,          /* misc flags */
    5277             :         xfs_extnum_t            nexts)          /* number of extents max */
    5278             : {
    5279    65147721 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
    5280    65147721 :         struct xfs_bmbt_irec    del;            /* extent being deleted */
    5281    65147721 :         int                     error;          /* error return value */
    5282    65147721 :         xfs_extnum_t            extno;          /* extent number in list */
    5283    65147721 :         struct xfs_bmbt_irec    got;            /* current extent record */
    5284    65147721 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
    5285    65147721 :         int                     isrt;           /* freeing in rt area */
    5286    65147721 :         int                     logflags;       /* transaction logging flags */
    5287    65147721 :         xfs_extlen_t            mod;            /* rt extent offset */
    5288    65147721 :         struct xfs_mount        *mp = ip->i_mount;
    5289    65147721 :         int                     tmp_logflags;   /* partial logging flags */
    5290    65147721 :         int                     wasdel;         /* was a delayed alloc extent */
    5291    65147721 :         int                     whichfork;      /* data or attribute fork */
    5292    65147721 :         xfs_fsblock_t           sum;
    5293    65147721 :         xfs_filblks_t           len = *rlen;    /* length to unmap in file */
    5294    65147721 :         xfs_fileoff_t           end;
    5295    65147721 :         struct xfs_iext_cursor  icur;
    5296    65147721 :         bool                    done = false;
    5297             : 
    5298    65147721 :         trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
    5299             : 
    5300    65147760 :         whichfork = xfs_bmapi_whichfork(flags);
    5301    65147760 :         ASSERT(whichfork != XFS_COW_FORK);
    5302    65147760 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5303    65147440 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
    5304           0 :                 return -EFSCORRUPTED;
    5305   130294880 :         if (xfs_is_shutdown(mp))
    5306             :                 return -EIO;
    5307             : 
    5308    65147043 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5309    65147461 :         ASSERT(len > 0);
    5310    65147461 :         ASSERT(nexts >= 0);
    5311             : 
    5312    65147461 :         error = xfs_iread_extents(tp, ip, whichfork);
    5313    65147323 :         if (error)
    5314             :                 return error;
    5315             : 
    5316    65147151 :         if (xfs_iext_count(ifp) == 0) {
    5317      630450 :                 *rlen = 0;
    5318      630450 :                 return 0;
    5319             :         }
    5320    64516612 :         XFS_STATS_INC(mp, xs_blk_unmap);
    5321    64516699 :         isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
    5322    64516699 :         end = start + len;
    5323             : 
    5324    64516699 :         if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
    5325      183652 :                 *rlen = 0;
    5326      183652 :                 return 0;
    5327             :         }
    5328    64331855 :         end--;
    5329             : 
    5330    64331855 :         logflags = 0;
    5331    64331855 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5332    25406696 :                 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
    5333    25406696 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5334    25406697 :                 cur->bc_ino.flags = 0;
    5335             :         } else
    5336    38925159 :                 cur = NULL;
    5337             : 
    5338    64331856 :         if (isrt) {
    5339             :                 /*
    5340             :                  * Synchronize by locking the bitmap inode.
    5341             :                  */
    5342     2119624 :                 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
    5343     2119636 :                 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
    5344     2119636 :                 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
    5345     2119636 :                 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
    5346             :         }
    5347             : 
    5348             :         extno = 0;
    5349   143317609 :         while (end != (xfs_fileoff_t)-1 && end >= start &&
    5350             :                (nexts == 0 || extno < nexts)) {
    5351             :                 /*
    5352             :                  * Is the found extent after a hole in which end lives?
    5353             :                  * Just back up to the previous extent, if so.
    5354             :                  */
    5355    80707084 :                 if (got.br_startoff > end &&
    5356           0 :                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5357             :                         done = true;
    5358             :                         break;
    5359             :                 }
    5360             :                 /*
    5361             :                  * Is the last block of this extent before the range
    5362             :                  * we're supposed to delete?  If so, we're done.
    5363             :                  */
    5364    80707084 :                 end = XFS_FILEOFF_MIN(end,
    5365             :                         got.br_startoff + got.br_blockcount - 1);
    5366    80707084 :                 if (end < start)
    5367             :                         break;
    5368             :                 /*
    5369             :                  * Then deal with the (possibly delayed) allocated space
    5370             :                  * we found.
    5371             :                  */
    5372    79944673 :                 del = got;
    5373    79944673 :                 wasdel = isnullstartblock(del.br_startblock);
    5374             : 
    5375    79944673 :                 if (got.br_startoff < start) {
    5376     8989756 :                         del.br_startoff = start;
    5377     8989756 :                         del.br_blockcount -= start - got.br_startoff;
    5378     8989756 :                         if (!wasdel)
    5379     8510111 :                                 del.br_startblock += start - got.br_startoff;
    5380             :                 }
    5381    79944673 :                 if (del.br_startoff + del.br_blockcount > end + 1)
    5382     8852372 :                         del.br_blockcount = end + 1 - del.br_startoff;
    5383             : 
    5384    79944673 :                 if (!isrt)
    5385    77228386 :                         goto delete;
    5386             : 
    5387     2716429 :                 sum = del.br_startblock + del.br_blockcount;
    5388     2716429 :                 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
    5389     2716338 :                 if (mod) {
    5390             :                         /*
    5391             :                          * Realtime extent not lined up at the end.
    5392             :                          * The extent could have been split into written
    5393             :                          * and unwritten pieces, or we could just be
    5394             :                          * unmapping part of it.  But we can't really
    5395             :                          * get rid of part of a realtime extent.
    5396             :                          */
    5397           0 :                         if (del.br_state == XFS_EXT_UNWRITTEN) {
    5398             :                                 /*
    5399             :                                  * This piece is unwritten, or we're not
    5400             :                                  * using unwritten extents.  Skip over it.
    5401             :                                  */
    5402           0 :                                 ASSERT(end >= mod);
    5403           0 :                                 end -= mod > del.br_blockcount ?
    5404           0 :                                         del.br_blockcount : mod;
    5405           0 :                                 if (end < got.br_startoff &&
    5406           0 :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5407             :                                         done = true;
    5408             :                                         break;
    5409             :                                 }
    5410           0 :                                 continue;
    5411             :                         }
    5412             :                         /*
    5413             :                          * It's written, turn it unwritten.
    5414             :                          * This is better than zeroing it.
    5415             :                          */
    5416           0 :                         ASSERT(del.br_state == XFS_EXT_NORM);
    5417           0 :                         ASSERT(tp->t_blk_res > 0);
    5418             :                         /*
    5419             :                          * If this spans a realtime extent boundary,
    5420             :                          * chop it back to the start of the one we end at.
    5421             :                          */
    5422           0 :                         if (del.br_blockcount > mod) {
    5423           0 :                                 del.br_startoff += del.br_blockcount - mod;
    5424           0 :                                 del.br_startblock += del.br_blockcount - mod;
    5425           0 :                                 del.br_blockcount = mod;
    5426             :                         }
    5427           0 :                         del.br_state = XFS_EXT_UNWRITTEN;
    5428           0 :                         error = xfs_bmap_add_extent_unwritten_real(tp, ip,
    5429             :                                         whichfork, &icur, &cur, &del,
    5430             :                                         &logflags);
    5431           0 :                         if (error)
    5432           0 :                                 goto error0;
    5433           0 :                         goto nodelete;
    5434             :                 }
    5435     2716338 :                 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
    5436     2716338 :                 if (mod) {
    5437           2 :                         xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
    5438             : 
    5439             :                         /*
    5440             :                          * Realtime extent is lined up at the end but not
    5441             :                          * at the front.  We'll get rid of full extents if
    5442             :                          * we can.
    5443             :                          */
    5444           2 :                         if (del.br_blockcount > off) {
    5445           0 :                                 del.br_blockcount -= off;
    5446           0 :                                 del.br_startoff += off;
    5447           0 :                                 del.br_startblock += off;
    5448           2 :                         } else if (del.br_startoff == start &&
    5449           0 :                                    (del.br_state == XFS_EXT_UNWRITTEN ||
    5450           0 :                                     tp->t_blk_res == 0)) {
    5451             :                                 /*
    5452             :                                  * Can't make it unwritten.  There isn't
    5453             :                                  * a full extent here so just skip it.
    5454             :                                  */
    5455           0 :                                 ASSERT(end >= del.br_blockcount);
    5456           0 :                                 end -= del.br_blockcount;
    5457           0 :                                 if (got.br_startoff > end &&
    5458           0 :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5459             :                                         done = true;
    5460             :                                         break;
    5461             :                                 }
    5462           0 :                                 continue;
    5463           2 :                         } else if (del.br_state == XFS_EXT_UNWRITTEN) {
    5464           2 :                                 struct xfs_bmbt_irec    prev;
    5465           2 :                                 xfs_fileoff_t           unwrite_start;
    5466             : 
    5467             :                                 /*
    5468             :                                  * This one is already unwritten.
    5469             :                                  * It must have a written left neighbor.
    5470             :                                  * Unwrite the killed part of that one and
    5471             :                                  * try again.
    5472             :                                  */
    5473           2 :                                 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
    5474           0 :                                         ASSERT(0);
    5475           2 :                                 ASSERT(prev.br_state == XFS_EXT_NORM);
    5476           2 :                                 ASSERT(!isnullstartblock(prev.br_startblock));
    5477           2 :                                 ASSERT(del.br_startblock ==
    5478             :                                        prev.br_startblock + prev.br_blockcount);
    5479           2 :                                 unwrite_start = max3(start,
    5480             :                                                      del.br_startoff - mod,
    5481             :                                                      prev.br_startoff);
    5482           2 :                                 mod = unwrite_start - prev.br_startoff;
    5483           2 :                                 prev.br_startoff = unwrite_start;
    5484           2 :                                 prev.br_startblock += mod;
    5485           2 :                                 prev.br_blockcount -= mod;
    5486           2 :                                 prev.br_state = XFS_EXT_UNWRITTEN;
    5487           2 :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5488             :                                                 ip, whichfork, &icur, &cur,
    5489             :                                                 &prev, &logflags);
    5490           2 :                                 if (error)
    5491           0 :                                         goto error0;
    5492           2 :                                 goto nodelete;
    5493             :                         } else {
    5494           0 :                                 ASSERT(del.br_state == XFS_EXT_NORM);
    5495           0 :                                 del.br_state = XFS_EXT_UNWRITTEN;
    5496           0 :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5497             :                                                 ip, whichfork, &icur, &cur,
    5498             :                                                 &del, &logflags);
    5499           0 :                                 if (error)
    5500           0 :                                         goto error0;
    5501           0 :                                 goto nodelete;
    5502             :                         }
    5503             :                 }
    5504             : 
    5505     2716336 : delete:
    5506    79944722 :                 if (wasdel) {
    5507     1025539 :                         error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
    5508             :                                         &got, &del);
    5509             :                 } else {
    5510    78919183 :                         error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
    5511             :                                         &del, &tmp_logflags, whichfork,
    5512             :                                         flags);
    5513    78920298 :                         logflags |= tmp_logflags;
    5514             :                 }
    5515             : 
    5516    79945862 :                 if (error)
    5517           1 :                         goto error0;
    5518             : 
    5519    79945861 :                 end = del.br_startoff - 1;
    5520    79945863 : nodelete:
    5521             :                 /*
    5522             :                  * If not done go on to the next (previous) record.
    5523             :                  */
    5524    79945863 :                 if (end != (xfs_fileoff_t)-1 && end >= start) {
    5525    45860084 :                         if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5526    45434934 :                             (got.br_startoff > end &&
    5527      505752 :                              !xfs_iext_prev_extent(ifp, &icur, &got))) {
    5528             :                                 done = true;
    5529             :                                 break;
    5530             :                         }
    5531    44899962 :                         extno++;
    5532             :                 }
    5533             :         }
    5534    63372936 :         if (done || end == (xfs_fileoff_t)-1 || end < start)
    5535    42712037 :                 *rlen = 0;
    5536             :         else
    5537    21621018 :                 *rlen = end - start + 1;
    5538             : 
    5539             :         /*
    5540             :          * Convert to a btree if necessary.
    5541             :          */
    5542    64333055 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    5543       12758 :                 ASSERT(cur == NULL);
    5544       12758 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    5545             :                                 &tmp_logflags, whichfork);
    5546       12758 :                 logflags |= tmp_logflags;
    5547             :         } else {
    5548    64319241 :                 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
    5549             :                         whichfork);
    5550             :         }
    5551             : 
    5552    64331185 : error0:
    5553             :         /*
    5554             :          * Log everything.  Do this after conversion, there's no point in
    5555             :          * logging the extent records if we've converted to btree format.
    5556             :          */
    5557    66188946 :         if ((logflags & xfs_ilog_fext(whichfork)) &&
    5558    31541684 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    5559       12758 :                 logflags &= ~xfs_ilog_fext(whichfork);
    5560    66176526 :         else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
    5561           0 :                  ifp->if_format != XFS_DINODE_FMT_BTREE)
    5562           0 :                 logflags &= ~xfs_ilog_fbroot(whichfork);
    5563             :         /*
    5564             :          * Log inode even in the error case, if the transaction
    5565             :          * is dirty we'll need to shut down the filesystem.
    5566             :          */
    5567    64331185 :         if (logflags)
    5568    56601987 :                 xfs_trans_log_inode(tp, ip, logflags);
    5569    64332237 :         if (cur) {
    5570    25418561 :                 if (!error)
    5571    25419460 :                         cur->bc_ino.allocated = 0;
    5572    25418561 :                 xfs_btree_del_cursor(cur, error);
    5573             :         }
    5574             :         return error;
    5575             : }
    5576             : 
    5577             : /* Unmap a range of a file. */
    5578             : int
    5579    28936078 : xfs_bunmapi(
    5580             :         xfs_trans_t             *tp,
    5581             :         struct xfs_inode        *ip,
    5582             :         xfs_fileoff_t           bno,
    5583             :         xfs_filblks_t           len,
    5584             :         uint32_t                flags,
    5585             :         xfs_extnum_t            nexts,
    5586             :         int                     *done)
    5587             : {
    5588    28936078 :         int                     error;
    5589             : 
    5590    28936078 :         error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
    5591    28936108 :         *done = (len == 0);
    5592    28936108 :         return error;
    5593             : }
    5594             : 
    5595             : /*
    5596             :  * Determine whether an extent shift can be accomplished by a merge with the
    5597             :  * extent that precedes the target hole of the shift.
    5598             :  */
    5599             : STATIC bool
    5600    19989142 : xfs_bmse_can_merge(
    5601             :         struct xfs_bmbt_irec    *left,  /* preceding extent */
    5602             :         struct xfs_bmbt_irec    *got,   /* current extent to shift */
    5603             :         xfs_fileoff_t           shift)  /* shift fsb */
    5604             : {
    5605    19989142 :         xfs_fileoff_t           startoff;
    5606             : 
    5607    19989142 :         startoff = got->br_startoff - shift;
    5608             : 
    5609             :         /*
    5610             :          * The extent, once shifted, must be adjacent in-file and on-disk with
    5611             :          * the preceding extent.
    5612             :          */
    5613    19989142 :         if ((left->br_startoff + left->br_blockcount != startoff) ||
    5614    18908606 :             (left->br_startblock + left->br_blockcount != got->br_startblock) ||
    5615      272600 :             (left->br_state != got->br_state) ||
    5616        4768 :             (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
    5617    19984374 :                 return false;
    5618             : 
    5619             :         return true;
    5620             : }
    5621             : 
    5622             : /*
    5623             :  * A bmap extent shift adjusts the file offset of an extent to fill a preceding
    5624             :  * hole in the file. If an extent shift would result in the extent being fully
    5625             :  * adjacent to the extent that currently precedes the hole, we can merge with
    5626             :  * the preceding extent rather than do the shift.
    5627             :  *
    5628             :  * This function assumes the caller has verified a shift-by-merge is possible
    5629             :  * with the provided extents via xfs_bmse_can_merge().
    5630             :  */
    5631             : STATIC int
    5632        2384 : xfs_bmse_merge(
    5633             :         struct xfs_trans                *tp,
    5634             :         struct xfs_inode                *ip,
    5635             :         int                             whichfork,
    5636             :         xfs_fileoff_t                   shift,          /* shift fsb */
    5637             :         struct xfs_iext_cursor          *icur,
    5638             :         struct xfs_bmbt_irec            *got,           /* extent to shift */
    5639             :         struct xfs_bmbt_irec            *left,          /* preceding extent */
    5640             :         struct xfs_btree_cur            *cur,
    5641             :         int                             *logflags)      /* output */
    5642             : {
    5643        2384 :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    5644        2384 :         struct xfs_bmbt_irec            new;
    5645        2384 :         xfs_filblks_t                   blockcount;
    5646        2384 :         int                             error, i;
    5647        2384 :         struct xfs_mount                *mp = ip->i_mount;
    5648             : 
    5649        2384 :         blockcount = left->br_blockcount + got->br_blockcount;
    5650             : 
    5651        2384 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5652        2384 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5653        2384 :         ASSERT(xfs_bmse_can_merge(left, got, shift));
    5654             : 
    5655        2384 :         new = *left;
    5656        2384 :         new.br_blockcount = blockcount;
    5657             : 
    5658             :         /*
    5659             :          * Update the on-disk extent count, the btree if necessary and log the
    5660             :          * inode.
    5661             :          */
    5662        2384 :         ifp->if_nextents--;
    5663        2384 :         *logflags |= XFS_ILOG_CORE;
    5664        2384 :         if (!cur) {
    5665        2318 :                 *logflags |= XFS_ILOG_DEXT;
    5666        2318 :                 goto done;
    5667             :         }
    5668             : 
    5669             :         /* lookup and remove the extent to merge */
    5670          66 :         error = xfs_bmbt_lookup_eq(cur, got, &i);
    5671          66 :         if (error)
    5672             :                 return error;
    5673          66 :         if (XFS_IS_CORRUPT(mp, i != 1))
    5674           0 :                 return -EFSCORRUPTED;
    5675             : 
    5676          66 :         error = xfs_btree_delete(cur, &i);
    5677          66 :         if (error)
    5678             :                 return error;
    5679          66 :         if (XFS_IS_CORRUPT(mp, i != 1))
    5680           0 :                 return -EFSCORRUPTED;
    5681             : 
    5682             :         /* lookup and update size of the previous extent */
    5683          66 :         error = xfs_bmbt_lookup_eq(cur, left, &i);
    5684          66 :         if (error)
    5685             :                 return error;
    5686          66 :         if (XFS_IS_CORRUPT(mp, i != 1))
    5687           0 :                 return -EFSCORRUPTED;
    5688             : 
    5689          66 :         error = xfs_bmbt_update(cur, &new);
    5690          66 :         if (error)
    5691             :                 return error;
    5692             : 
    5693             :         /* change to extent format if required after extent removal */
    5694          66 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
    5695          66 :         if (error)
    5696             :                 return error;
    5697             : 
    5698          66 : done:
    5699        2384 :         xfs_iext_remove(ip, icur, 0);
    5700        2384 :         xfs_iext_prev(ifp, icur);
    5701        4768 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5702             :                         &new);
    5703             : 
    5704             :         /* update reverse mapping. rmap functions merge the rmaps for us */
    5705        2384 :         xfs_rmap_unmap_extent(tp, ip, whichfork, got);
    5706        2384 :         memcpy(&new, got, sizeof(new));
    5707        2384 :         new.br_startoff = left->br_startoff + left->br_blockcount;
    5708        2384 :         xfs_rmap_map_extent(tp, ip, whichfork, &new);
    5709        2384 :         return 0;
    5710             : }
    5711             : 
    5712             : static int
    5713    20363953 : xfs_bmap_shift_update_extent(
    5714             :         struct xfs_trans        *tp,
    5715             :         struct xfs_inode        *ip,
    5716             :         int                     whichfork,
    5717             :         struct xfs_iext_cursor  *icur,
    5718             :         struct xfs_bmbt_irec    *got,
    5719             :         struct xfs_btree_cur    *cur,
    5720             :         int                     *logflags,
    5721             :         xfs_fileoff_t           startoff)
    5722             : {
    5723    20363953 :         struct xfs_mount        *mp = ip->i_mount;
    5724    20363953 :         struct xfs_bmbt_irec    prev = *got;
    5725    20363953 :         int                     error, i;
    5726             : 
    5727    20363953 :         *logflags |= XFS_ILOG_CORE;
    5728             : 
    5729    20363953 :         got->br_startoff = startoff;
    5730             : 
    5731    20363953 :         if (cur) {
    5732    18021577 :                 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
    5733    18021577 :                 if (error)
    5734             :                         return error;
    5735    18021576 :                 if (XFS_IS_CORRUPT(mp, i != 1))
    5736           0 :                         return -EFSCORRUPTED;
    5737             : 
    5738    18021576 :                 error = xfs_bmbt_update(cur, got);
    5739    18021576 :                 if (error)
    5740             :                         return error;
    5741             :         } else {
    5742     2342376 :                 *logflags |= XFS_ILOG_DEXT;
    5743             :         }
    5744             : 
    5745    40727904 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5746             :                         got);
    5747             : 
    5748             :         /* update reverse mapping */
    5749    20363952 :         xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
    5750    20363952 :         xfs_rmap_map_extent(tp, ip, whichfork, got);
    5751    20363952 :         return 0;
    5752             : }
    5753             : 
    5754             : int
    5755    18944237 : xfs_bmap_collapse_extents(
    5756             :         struct xfs_trans        *tp,
    5757             :         struct xfs_inode        *ip,
    5758             :         xfs_fileoff_t           *next_fsb,
    5759             :         xfs_fileoff_t           offset_shift_fsb,
    5760             :         bool                    *done)
    5761             : {
    5762    18944237 :         int                     whichfork = XFS_DATA_FORK;
    5763    18944237 :         struct xfs_mount        *mp = ip->i_mount;
    5764    18944237 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5765    18944237 :         struct xfs_btree_cur    *cur = NULL;
    5766    18944237 :         struct xfs_bmbt_irec    got, prev;
    5767    18944237 :         struct xfs_iext_cursor  icur;
    5768    18944237 :         xfs_fileoff_t           new_startoff;
    5769    18944237 :         int                     error = 0;
    5770    18944237 :         int                     logflags = 0;
    5771             : 
    5772    37888474 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5773    18944237 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5774           0 :                 return -EFSCORRUPTED;
    5775             :         }
    5776             : 
    5777    37888474 :         if (xfs_is_shutdown(mp))
    5778             :                 return -EIO;
    5779             : 
    5780    18944237 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5781             : 
    5782    18944237 :         error = xfs_iread_extents(tp, ip, whichfork);
    5783    18944237 :         if (error)
    5784             :                 return error;
    5785             : 
    5786    18944237 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5787    17619751 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5788    17619751 :                 cur->bc_ino.flags = 0;
    5789             :         }
    5790             : 
    5791    18944237 :         if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5792       40520 :                 *done = true;
    5793       40520 :                 goto del_cursor;
    5794             :         }
    5795    18903717 :         if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
    5796           0 :                 error = -EFSCORRUPTED;
    5797           0 :                 goto del_cursor;
    5798             :         }
    5799             : 
    5800    18903717 :         new_startoff = got.br_startoff - offset_shift_fsb;
    5801    18903717 :         if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
    5802    18839490 :                 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
    5803           0 :                         error = -EINVAL;
    5804           0 :                         goto del_cursor;
    5805             :                 }
    5806             : 
    5807    18839490 :                 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
    5808        2384 :                         error = xfs_bmse_merge(tp, ip, whichfork,
    5809             :                                         offset_shift_fsb, &icur, &got, &prev,
    5810             :                                         cur, &logflags);
    5811        2384 :                         if (error)
    5812           0 :                                 goto del_cursor;
    5813        2384 :                         goto done;
    5814             :                 }
    5815             :         } else {
    5816       64227 :                 if (got.br_startoff < offset_shift_fsb) {
    5817           0 :                         error = -EINVAL;
    5818           0 :                         goto del_cursor;
    5819             :                 }
    5820             :         }
    5821             : 
    5822    18901333 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    5823             :                         cur, &logflags, new_startoff);
    5824    18901333 :         if (error)
    5825           1 :                 goto del_cursor;
    5826             : 
    5827    18901332 : done:
    5828    18903716 :         if (!xfs_iext_next_extent(ifp, &icur, &got)) {
    5829      397503 :                 *done = true;
    5830      397503 :                 goto del_cursor;
    5831             :         }
    5832             : 
    5833    18506213 :         *next_fsb = got.br_startoff;
    5834    18944237 : del_cursor:
    5835    18944237 :         if (cur)
    5836    17619751 :                 xfs_btree_del_cursor(cur, error);
    5837    18944237 :         if (logflags)
    5838    18903717 :                 xfs_trans_log_inode(tp, ip, logflags);
    5839             :         return error;
    5840             : }
    5841             : 
    5842             : /* Make sure we won't be right-shifting an extent past the maximum bound. */
    5843             : int
    5844      352028 : xfs_bmap_can_insert_extents(
    5845             :         struct xfs_inode        *ip,
    5846             :         xfs_fileoff_t           off,
    5847             :         xfs_fileoff_t           shift)
    5848             : {
    5849      352028 :         struct xfs_bmbt_irec    got;
    5850      352028 :         int                     is_empty;
    5851      352028 :         int                     error = 0;
    5852             : 
    5853      352028 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5854             : 
    5855      704056 :         if (xfs_is_shutdown(ip->i_mount))
    5856             :                 return -EIO;
    5857             : 
    5858      352026 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    5859      352026 :         error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
    5860      352026 :         if (!error && !is_empty && got.br_startoff >= off &&
    5861      300036 :             ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
    5862           0 :                 error = -EINVAL;
    5863      352026 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    5864             : 
    5865      352026 :         return error;
    5866             : }
    5867             : 
    5868             : int
    5869     1497518 : xfs_bmap_insert_extents(
    5870             :         struct xfs_trans        *tp,
    5871             :         struct xfs_inode        *ip,
    5872             :         xfs_fileoff_t           *next_fsb,
    5873             :         xfs_fileoff_t           offset_shift_fsb,
    5874             :         bool                    *done,
    5875             :         xfs_fileoff_t           stop_fsb)
    5876             : {
    5877     1497518 :         int                     whichfork = XFS_DATA_FORK;
    5878     1497518 :         struct xfs_mount        *mp = ip->i_mount;
    5879     1497518 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5880     1497518 :         struct xfs_btree_cur    *cur = NULL;
    5881     1497518 :         struct xfs_bmbt_irec    got, next;
    5882     1497518 :         struct xfs_iext_cursor  icur;
    5883     1497518 :         xfs_fileoff_t           new_startoff;
    5884     1497518 :         int                     error = 0;
    5885     1497518 :         int                     logflags = 0;
    5886             : 
    5887     2995036 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5888     1497518 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5889           0 :                 return -EFSCORRUPTED;
    5890             :         }
    5891             : 
    5892     2995036 :         if (xfs_is_shutdown(mp))
    5893             :                 return -EIO;
    5894             : 
    5895     1497518 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5896             : 
    5897     1497518 :         error = xfs_iread_extents(tp, ip, whichfork);
    5898     1497518 :         if (error)
    5899             :                 return error;
    5900             : 
    5901     1497518 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5902      403588 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5903      403588 :                 cur->bc_ino.flags = 0;
    5904             :         }
    5905             : 
    5906     1497518 :         if (*next_fsb == NULLFSBLOCK) {
    5907      350250 :                 xfs_iext_last(ifp, &icur);
    5908      350250 :                 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5909      342780 :                     stop_fsb > got.br_startoff) {
    5910       34898 :                         *done = true;
    5911       34898 :                         goto del_cursor;
    5912             :                 }
    5913             :         } else {
    5914     1147268 :                 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5915           0 :                         *done = true;
    5916           0 :                         goto del_cursor;
    5917             :                 }
    5918             :         }
    5919     1462620 :         if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
    5920           0 :                 error = -EFSCORRUPTED;
    5921           0 :                 goto del_cursor;
    5922             :         }
    5923             : 
    5924     1462620 :         if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
    5925           0 :                 error = -EFSCORRUPTED;
    5926           0 :                 goto del_cursor;
    5927             :         }
    5928             : 
    5929     1462620 :         new_startoff = got.br_startoff + offset_shift_fsb;
    5930     1462620 :         if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
    5931     1147268 :                 if (new_startoff + got.br_blockcount > next.br_startoff) {
    5932           0 :                         error = -EINVAL;
    5933           0 :                         goto del_cursor;
    5934             :                 }
    5935             : 
    5936             :                 /*
    5937             :                  * Unlike a left shift (which involves a hole punch), a right
    5938             :                  * shift does not modify extent neighbors in any way.  We should
    5939             :                  * never find mergeable extents in this scenario.  Check anyways
    5940             :                  * and warn if we encounter two extents that could be one.
    5941             :                  */
    5942     1147268 :                 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
    5943           0 :                         WARN_ON_ONCE(1);
    5944             :         }
    5945             : 
    5946     1462620 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    5947             :                         cur, &logflags, new_startoff);
    5948     1462620 :         if (error)
    5949           0 :                 goto del_cursor;
    5950             : 
    5951     1462620 :         if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
    5952     1400660 :             stop_fsb >= got.br_startoff + got.br_blockcount) {
    5953      315349 :                 *done = true;
    5954      315349 :                 goto del_cursor;
    5955             :         }
    5956             : 
    5957     1147271 :         *next_fsb = got.br_startoff;
    5958     1497518 : del_cursor:
    5959     1497518 :         if (cur)
    5960      403588 :                 xfs_btree_del_cursor(cur, error);
    5961     1497518 :         if (logflags)
    5962     1462620 :                 xfs_trans_log_inode(tp, ip, logflags);
    5963             :         return error;
    5964             : }
    5965             : 
    5966             : /*
    5967             :  * Splits an extent into two extents at split_fsb block such that it is the
    5968             :  * first block of the current_ext. @ext is a target extent to be split.
    5969             :  * @split_fsb is a block where the extents is split.  If split_fsb lies in a
    5970             :  * hole or the first block of extents, just return 0.
    5971             :  */
    5972             : int
    5973      350250 : xfs_bmap_split_extent(
    5974             :         struct xfs_trans        *tp,
    5975             :         struct xfs_inode        *ip,
    5976             :         xfs_fileoff_t           split_fsb)
    5977             : {
    5978      350250 :         int                             whichfork = XFS_DATA_FORK;
    5979      350250 :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    5980      350250 :         struct xfs_btree_cur            *cur = NULL;
    5981      350250 :         struct xfs_bmbt_irec            got;
    5982      350250 :         struct xfs_bmbt_irec            new; /* split extent */
    5983      350250 :         struct xfs_mount                *mp = ip->i_mount;
    5984      350250 :         xfs_fsblock_t                   gotblkcnt; /* new block count for got */
    5985      350250 :         struct xfs_iext_cursor          icur;
    5986      350250 :         int                             error = 0;
    5987      350250 :         int                             logflags = 0;
    5988      350250 :         int                             i = 0;
    5989             : 
    5990      700500 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5991      350250 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5992           0 :                 return -EFSCORRUPTED;
    5993             :         }
    5994             : 
    5995      700500 :         if (xfs_is_shutdown(mp))
    5996             :                 return -EIO;
    5997             : 
    5998             :         /* Read in all the extents */
    5999      350250 :         error = xfs_iread_extents(tp, ip, whichfork);
    6000      350250 :         if (error)
    6001             :                 return error;
    6002             : 
    6003             :         /*
    6004             :          * If there are not extents, or split_fsb lies in a hole we are done.
    6005             :          */
    6006      350250 :         if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
    6007      315352 :             got.br_startoff >= split_fsb)
    6008             :                 return 0;
    6009             : 
    6010       81925 :         gotblkcnt = split_fsb - got.br_startoff;
    6011       81925 :         new.br_startoff = split_fsb;
    6012       81925 :         new.br_startblock = got.br_startblock + gotblkcnt;
    6013       81925 :         new.br_blockcount = got.br_blockcount - gotblkcnt;
    6014       81925 :         new.br_state = got.br_state;
    6015             : 
    6016       81925 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    6017        1783 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    6018        1783 :                 cur->bc_ino.flags = 0;
    6019        1783 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    6020        1783 :                 if (error)
    6021           0 :                         goto del_cursor;
    6022        1783 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    6023           0 :                         error = -EFSCORRUPTED;
    6024           0 :                         goto del_cursor;
    6025             :                 }
    6026             :         }
    6027             : 
    6028       81925 :         got.br_blockcount = gotblkcnt;
    6029       81925 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
    6030             :                         &got);
    6031             : 
    6032       81925 :         logflags = XFS_ILOG_CORE;
    6033       81925 :         if (cur) {
    6034        1783 :                 error = xfs_bmbt_update(cur, &got);
    6035        1783 :                 if (error)
    6036           0 :                         goto del_cursor;
    6037             :         } else
    6038             :                 logflags |= XFS_ILOG_DEXT;
    6039             : 
    6040             :         /* Add new extent */
    6041       81925 :         xfs_iext_next(ifp, &icur);
    6042       81925 :         xfs_iext_insert(ip, &icur, &new, 0);
    6043       81925 :         ifp->if_nextents++;
    6044             : 
    6045       81925 :         if (cur) {
    6046        1783 :                 error = xfs_bmbt_lookup_eq(cur, &new, &i);
    6047        1783 :                 if (error)
    6048           0 :                         goto del_cursor;
    6049        1783 :                 if (XFS_IS_CORRUPT(mp, i != 0)) {
    6050           0 :                         error = -EFSCORRUPTED;
    6051           0 :                         goto del_cursor;
    6052             :                 }
    6053        1783 :                 error = xfs_btree_insert(cur, &i);
    6054        1783 :                 if (error)
    6055           0 :                         goto del_cursor;
    6056        1783 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    6057           0 :                         error = -EFSCORRUPTED;
    6058           0 :                         goto del_cursor;
    6059             :                 }
    6060             :         }
    6061             : 
    6062             :         /*
    6063             :          * Convert to a btree if necessary.
    6064             :          */
    6065       81925 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    6066         254 :                 int tmp_logflags; /* partial log flag return val */
    6067             : 
    6068         254 :                 ASSERT(cur == NULL);
    6069         254 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    6070             :                                 &tmp_logflags, whichfork);
    6071         254 :                 logflags |= tmp_logflags;
    6072             :         }
    6073             : 
    6074       81671 : del_cursor:
    6075       81925 :         if (cur) {
    6076        2037 :                 cur->bc_ino.allocated = 0;
    6077        2037 :                 xfs_btree_del_cursor(cur, error);
    6078             :         }
    6079             : 
    6080       81925 :         if (logflags)
    6081       81925 :                 xfs_trans_log_inode(tp, ip, logflags);
    6082             :         return error;
    6083             : }
    6084             : 
    6085             : /* Deferred mapping is only for real extents in the data fork. */
    6086             : static bool
    6087             : xfs_bmap_is_update_needed(
    6088             :         struct xfs_bmbt_irec    *bmap)
    6089             : {
    6090    54978270 :         return  bmap->br_startblock != HOLESTARTBLOCK &&
    6091             :                 bmap->br_startblock != DELAYSTARTBLOCK;
    6092             : }
    6093             : 
    6094             : /* Record a bmap intent. */
    6095             : static int
    6096    54811579 : __xfs_bmap_add(
    6097             :         struct xfs_trans                *tp,
    6098             :         enum xfs_bmap_intent_type       type,
    6099             :         struct xfs_inode                *ip,
    6100             :         int                             whichfork,
    6101             :         struct xfs_bmbt_irec            *bmap)
    6102             : {
    6103    54811579 :         struct xfs_bmap_intent          *bi;
    6104             : 
    6105    54811587 :         trace_xfs_bmap_defer(tp->t_mountp,
    6106    54811579 :                         XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
    6107             :                         type,
    6108    54811579 :                         XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
    6109             :                         ip->i_ino, whichfork,
    6110             :                         bmap->br_startoff,
    6111             :                         bmap->br_blockcount,
    6112             :                         bmap->br_state);
    6113             : 
    6114    54811587 :         bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
    6115    54811582 :         INIT_LIST_HEAD(&bi->bi_list);
    6116    54811582 :         bi->bi_type = type;
    6117    54811582 :         bi->bi_owner = ip;
    6118    54811582 :         bi->bi_whichfork = whichfork;
    6119    54811582 :         bi->bi_bmap = *bmap;
    6120             : 
    6121    54811582 :         xfs_bmap_update_get_group(tp->t_mountp, bi);
    6122    54811586 :         xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
    6123    54811585 :         return 0;
    6124             : }
    6125             : 
    6126             : /* Map an extent into a file. */
    6127             : void
    6128    50381691 : xfs_bmap_map_extent(
    6129             :         struct xfs_trans        *tp,
    6130             :         struct xfs_inode        *ip,
    6131             :         struct xfs_bmbt_irec    *PREV)
    6132             : {
    6133    50381691 :         if (!xfs_bmap_is_update_needed(PREV))
    6134             :                 return;
    6135             : 
    6136    50298349 :         __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
    6137             : }
    6138             : 
    6139             : /* Unmap an extent out of a file. */
    6140             : void
    6141     4596579 : xfs_bmap_unmap_extent(
    6142             :         struct xfs_trans        *tp,
    6143             :         struct xfs_inode        *ip,
    6144             :         struct xfs_bmbt_irec    *PREV)
    6145             : {
    6146     4596579 :         if (!xfs_bmap_is_update_needed(PREV))
    6147             :                 return;
    6148             : 
    6149     4513239 :         __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
    6150             : }
    6151             : 
    6152             : /*
    6153             :  * Process one of the deferred bmap operations.  We pass back the
    6154             :  * btree cursor to maintain our lock on the bmapbt between calls.
    6155             :  */
    6156             : int
    6157    54811724 : xfs_bmap_finish_one(
    6158             :         struct xfs_trans                *tp,
    6159             :         struct xfs_bmap_intent          *bi)
    6160             : {
    6161    54811724 :         struct xfs_bmbt_irec            *bmap = &bi->bi_bmap;
    6162    54811724 :         int                             error = 0;
    6163             : 
    6164    54811724 :         ASSERT(tp->t_highest_agno == NULLAGNUMBER);
    6165             : 
    6166   164435168 :         trace_xfs_bmap_deferred(tp->t_mountp,
    6167    54811724 :                         XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
    6168    54811724 :                         bi->bi_type,
    6169    54811724 :                         XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
    6170    54811720 :                         bi->bi_owner->i_ino, bi->bi_whichfork,
    6171             :                         bmap->br_startoff, bmap->br_blockcount,
    6172             :                         bmap->br_state);
    6173             : 
    6174    54811724 :         if (WARN_ON_ONCE(bi->bi_whichfork != XFS_DATA_FORK))
    6175             :                 return -EFSCORRUPTED;
    6176             : 
    6177    54811724 :         if (XFS_TEST_ERROR(false, tp->t_mountp,
    6178             :                         XFS_ERRTAG_BMAP_FINISH_ONE))
    6179             :                 return -EIO;
    6180             : 
    6181    54811709 :         switch (bi->bi_type) {
    6182    50298473 :         case XFS_BMAP_MAP:
    6183    50298473 :                 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
    6184             :                                 bmap->br_blockcount, bmap->br_startblock, 0);
    6185    50298473 :                 bmap->br_blockcount = 0;
    6186    50298473 :                 break;
    6187     4513236 :         case XFS_BMAP_UNMAP:
    6188     4513236 :                 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
    6189             :                                 &bmap->br_blockcount, XFS_BMAPI_REMAP, 1);
    6190     4513236 :                 break;
    6191           0 :         default:
    6192           0 :                 ASSERT(0);
    6193           0 :                 error = -EFSCORRUPTED;
    6194             :         }
    6195             : 
    6196             :         return error;
    6197             : }
    6198             : 
    6199             : /* Check that an inode's extent does not have invalid flags or bad ranges. */
    6200             : xfs_failaddr_t
    6201  2384885471 : xfs_bmap_validate_extent(
    6202             :         struct xfs_inode        *ip,
    6203             :         int                     whichfork,
    6204             :         struct xfs_bmbt_irec    *irec)
    6205             : {
    6206  2384885471 :         struct xfs_mount        *mp = ip->i_mount;
    6207             : 
    6208  2384885471 :         if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
    6209           0 :                 return __this_address;
    6210             : 
    6211  2384923453 :         if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) {
    6212   207313780 :                 if (!xfs_verify_rtext(mp, irec->br_startblock,
    6213             :                                           irec->br_blockcount))
    6214           0 :                         return __this_address;
    6215             :         } else {
    6216  2177609673 :                 if (!xfs_verify_fsbext(mp, irec->br_startblock,
    6217             :                                            irec->br_blockcount))
    6218           0 :                         return __this_address;
    6219             :         }
    6220  2384984512 :         if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
    6221           0 :                 return __this_address;
    6222             :         return NULL;
    6223             : }
    6224             : 
    6225             : int __init
    6226          12 : xfs_bmap_intent_init_cache(void)
    6227             : {
    6228          12 :         xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
    6229             :                         sizeof(struct xfs_bmap_intent),
    6230             :                         0, 0, NULL);
    6231             : 
    6232          12 :         return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
    6233             : }
    6234             : 
    6235             : void
    6236          12 : xfs_bmap_intent_destroy_cache(void)
    6237             : {
    6238          12 :         kmem_cache_destroy(xfs_bmap_intent_cache);
    6239          12 :         xfs_bmap_intent_cache = NULL;
    6240          12 : }

Generated by: LCOV version 1.14