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-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 2631 3190 82.5 %
Date: 2023-07-31 20:08:12 Functions: 83 85 97.6 %

          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             : #include "xfs_health.h"
      40             : #include "xfs_symlink_remote.h"
      41             : 
      42             : struct kmem_cache               *xfs_bmap_intent_cache;
      43             : 
      44             : /*
      45             :  * Miscellaneous helper functions
      46             :  */
      47             : 
      48             : /*
      49             :  * Compute and fill in the value of the maximum depth of a bmap btree
      50             :  * in this filesystem.  Done once, during mount.
      51             :  */
      52             : void
      53      121760 : xfs_bmap_compute_maxlevels(
      54             :         xfs_mount_t     *mp,            /* file system mount structure */
      55             :         int             whichfork)      /* data or attr fork */
      56             : {
      57      121760 :         uint64_t        maxblocks;      /* max blocks at this level */
      58      121760 :         xfs_extnum_t    maxleafents;    /* max leaf entries possible */
      59      121760 :         int             level;          /* btree level */
      60      121760 :         int             maxrootrecs;    /* max records in root block */
      61      121760 :         int             minleafrecs;    /* min records in leaf block */
      62      121760 :         int             minnoderecs;    /* min records in node block */
      63      121760 :         int             sz;             /* root block size */
      64             : 
      65             :         /*
      66             :          * The maximum number of extents in a fork, hence the maximum number of
      67             :          * leaf entries, is controlled by the size of the on-disk extent count.
      68             :          *
      69             :          * Note that we can no longer assume that if we are in ATTR1 that the
      70             :          * fork offset of all the inodes will be
      71             :          * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
      72             :          * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
      73             :          * but probably at various positions. Therefore, for both ATTR1 and
      74             :          * ATTR2 we have to assume the worst case scenario of a minimum size
      75             :          * available.
      76             :          */
      77      121760 :         maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
      78             :                                 whichfork);
      79      121760 :         if (whichfork == XFS_DATA_FORK)
      80             :                 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
      81             :         else
      82       60880 :                 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
      83             : 
      84      121760 :         maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
      85      121760 :         minleafrecs = mp->m_bmap_dmnr[0];
      86      121760 :         minnoderecs = mp->m_bmap_dmnr[1];
      87      121760 :         maxblocks = howmany_64(maxleafents, minleafrecs);
      88      856658 :         for (level = 1; maxblocks > 1; level++) {
      89      613138 :                 if (maxblocks <= maxrootrecs)
      90             :                         maxblocks = 1;
      91             :                 else
      92      613077 :                         maxblocks = howmany_64(maxblocks, minnoderecs);
      93             :         }
      94      121760 :         mp->m_bm_maxlevels[whichfork] = level;
      95      121760 :         ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
      96      121760 : }
      97             : 
      98             : unsigned int
      99       60880 : xfs_bmap_compute_attr_offset(
     100             :         struct xfs_mount        *mp)
     101             : {
     102       60880 :         if (mp->m_sb.sb_inodesize == 256)
     103         368 :                 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
     104             :         return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
     105             : }
     106             : 
     107             : STATIC int                              /* error */
     108   362806633 : xfs_bmbt_lookup_eq(
     109             :         struct xfs_btree_cur    *cur,
     110             :         struct xfs_bmbt_irec    *irec,
     111             :         int                     *stat)  /* success/failure */
     112             : {
     113   362806633 :         cur->bc_rec.b = *irec;
     114   362806633 :         return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
     115             : }
     116             : 
     117             : STATIC int                              /* error */
     118           0 : xfs_bmbt_lookup_first(
     119             :         struct xfs_btree_cur    *cur,
     120             :         int                     *stat)  /* success/failure */
     121             : {
     122           0 :         cur->bc_rec.b.br_startoff = 0;
     123           0 :         cur->bc_rec.b.br_startblock = 0;
     124           0 :         cur->bc_rec.b.br_blockcount = 0;
     125           0 :         return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
     126             : }
     127             : 
     128             : /*
     129             :  * Check if the inode needs to be converted to btree format.
     130             :  */
     131   470675414 : static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
     132             : {
     133   470675414 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     134             : 
     135   927447298 :         return whichfork != XFS_COW_FORK &&
     136   470488076 :                 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
     137   229323805 :                 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
     138             : }
     139             : 
     140             : /*
     141             :  * Check if the inode should be converted to extent format.
     142             :  */
     143   466854785 : static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
     144             : {
     145   466854785 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     146             : 
     147   929968652 :         return whichfork != XFS_COW_FORK &&
     148   466859523 :                 ifp->if_format == XFS_DINODE_FMT_BTREE &&
     149   231352544 :                 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
     150             : }
     151             : 
     152             : /*
     153             :  * Update the record referred to by cur to the value given by irec
     154             :  * This either works (return 0) or gets an EFSCORRUPTED error.
     155             :  */
     156             : STATIC int
     157   188116156 : xfs_bmbt_update(
     158             :         struct xfs_btree_cur    *cur,
     159             :         struct xfs_bmbt_irec    *irec)
     160             : {
     161   188116156 :         union xfs_btree_rec     rec;
     162             : 
     163   188116156 :         xfs_bmbt_disk_set_all(&rec.bmbt, irec);
     164   188115773 :         return xfs_btree_update(cur, &rec);
     165             : }
     166             : 
     167             : /*
     168             :  * Compute the worst-case number of indirect blocks that will be used
     169             :  * for ip's delayed extent of length "len".
     170             :  */
     171             : STATIC xfs_filblks_t
     172    80345121 : xfs_bmap_worst_indlen(
     173             :         xfs_inode_t     *ip,            /* incore inode pointer */
     174             :         xfs_filblks_t   len)            /* delayed extent length */
     175             : {
     176    80345121 :         int             level;          /* btree level number */
     177    80345121 :         int             maxrecs;        /* maximum record count at this level */
     178    80345121 :         xfs_mount_t     *mp;            /* mount structure */
     179    80345121 :         xfs_filblks_t   rval;           /* return value */
     180             : 
     181    80345121 :         mp = ip->i_mount;
     182    80345121 :         maxrecs = mp->m_bmap_dmxr[0];
     183    80345121 :         for (level = 0, rval = 0;
     184    83807123 :              level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
     185     3462002 :              level++) {
     186    83801372 :                 len += maxrecs - 1;
     187    83801372 :                 do_div(len, maxrecs);
     188    83801372 :                 rval += len;
     189    83801372 :                 if (len == 1)
     190    80339370 :                         return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
     191    80339370 :                                 level - 1;
     192     3462002 :                 if (level == 0)
     193     3084880 :                         maxrecs = mp->m_bmap_dmxr[1];
     194             :         }
     195             :         return rval;
     196             : }
     197             : 
     198             : /*
     199             :  * Calculate the default attribute fork offset for newly created inodes.
     200             :  */
     201             : uint
     202   105708096 : xfs_default_attroffset(
     203             :         struct xfs_inode        *ip)
     204             : {
     205   105708096 :         if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
     206             :                 return roundup(sizeof(xfs_dev_t), 8);
     207    97278653 :         return M_IGEO(ip->i_mount)->attr_fork_offset;
     208             : }
     209             : 
     210             : /*
     211             :  * Helper routine to reset inode i_forkoff field when switching attribute fork
     212             :  * from local to extent format - we reset it where possible to make space
     213             :  * available for inline data fork extents.
     214             :  */
     215             : STATIC void
     216     5208589 : xfs_bmap_forkoff_reset(
     217             :         xfs_inode_t     *ip,
     218             :         int             whichfork)
     219             : {
     220     5208589 :         if (whichfork == XFS_ATTR_FORK &&
     221     4682651 :             ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
     222             :             ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
     223     4569009 :                 uint    dfl_forkoff = xfs_default_attroffset(ip) >> 3;
     224             : 
     225     4569009 :                 if (dfl_forkoff > ip->i_forkoff)
     226     4536365 :                         ip->i_forkoff = dfl_forkoff;
     227             :         }
     228     5208589 : }
     229             : 
     230             : #ifdef DEBUG
     231             : STATIC struct xfs_buf *
     232   545697342 : xfs_bmap_get_bp(
     233             :         struct xfs_btree_cur    *cur,
     234             :         xfs_fsblock_t           bno)
     235             : {
     236   545697342 :         struct xfs_log_item     *lip;
     237   545697342 :         int                     i;
     238             : 
     239   545697342 :         if (!cur)
     240             :                 return NULL;
     241             : 
     242  1419179358 :         for (i = 0; i < cur->bc_maxlevels; i++) {
     243  1419179415 :                 if (!cur->bc_levels[i].bp)
     244             :                         break;
     245   961921213 :                 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
     246    88439197 :                         return cur->bc_levels[i].bp;
     247             :         }
     248             : 
     249             :         /* Chase down all the log items to see if the bp is there */
     250  2625087985 :         list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
     251  2182147572 :                 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
     252             : 
     253  2182147572 :                 if (bip->bli_item.li_type == XFS_LI_BUF &&
     254  1299969414 :                     xfs_buf_daddr(bip->bli_buf) == bno)
     255    14317732 :                         return bip->bli_buf;
     256             :         }
     257             : 
     258             :         return NULL;
     259             : }
     260             : 
     261             : STATIC void
     262    90719762 : xfs_check_block(
     263             :         struct xfs_btree_block  *block,
     264             :         xfs_mount_t             *mp,
     265             :         int                     root,
     266             :         short                   sz)
     267             : {
     268    90719762 :         int                     i, j, dmxr;
     269    90719762 :         __be64                  *pp, *thispa;   /* pointer to block address */
     270    90719762 :         xfs_bmbt_key_t          *prevp, *keyp;
     271             : 
     272    90719762 :         ASSERT(be16_to_cpu(block->bb_level) > 0);
     273             : 
     274             :         prevp = NULL;
     275   636404800 :         for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
     276   545686037 :                 dmxr = mp->m_bmap_dmxr[0];
     277   545686037 :                 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
     278             : 
     279   545686037 :                 if (prevp) {
     280   454969912 :                         ASSERT(be64_to_cpu(prevp->br_startoff) <
     281             :                                be64_to_cpu(keyp->br_startoff));
     282             :                 }
     283   545686037 :                 prevp = keyp;
     284             : 
     285             :                 /*
     286             :                  * Compare the block numbers to see if there are dups.
     287             :                  */
     288   545686037 :                 if (root)
     289   127152970 :                         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
     290             :                 else
     291   418533067 :                         pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
     292             : 
     293  6437247850 :                 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
     294  5891562812 :                         if (root)
     295   213936836 :                                 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
     296             :                         else
     297  5677625976 :                                 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
     298  5891563159 :                         if (*thispa == *pp) {
     299           0 :                                 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
     300             :                                         __func__, j, i,
     301             :                                         (unsigned long long)be64_to_cpu(*thispa));
     302           0 :                                 xfs_err(mp, "%s: ptrs are equal in node\n",
     303             :                                         __func__);
     304           0 :                                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     305             :                         }
     306             :                 }
     307             :         }
     308    90718763 : }
     309             : 
     310             : /*
     311             :  * Check that the extents for the inode ip are in the right order in all
     312             :  * btree leaves. THis becomes prohibitively expensive for large extent count
     313             :  * files, so don't bother with inodes that have more than 10,000 extents in
     314             :  * them. The btree record ordering checks will still be done, so for such large
     315             :  * bmapbt constructs that is going to catch most corruptions.
     316             :  */
     317             : STATIC void
     318   300504608 : xfs_bmap_check_leaf_extents(
     319             :         struct xfs_btree_cur    *cur,   /* btree cursor or null */
     320             :         xfs_inode_t             *ip,            /* incore inode pointer */
     321             :         int                     whichfork)      /* data or attr fork */
     322             : {
     323   300504608 :         struct xfs_mount        *mp = ip->i_mount;
     324   300504608 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     325   300455547 :         struct xfs_btree_block  *block; /* current btree block */
     326   300455547 :         xfs_fsblock_t           bno;    /* block # of "block" */
     327   300455547 :         struct xfs_buf          *bp;    /* buffer for "block" */
     328   300455547 :         int                     error;  /* error return value */
     329   300455547 :         xfs_extnum_t            i=0, j; /* index into the extents list */
     330   300455547 :         int                     level;  /* btree level, for checking */
     331   300455547 :         __be64                  *pp;    /* pointer to block address */
     332   300455547 :         xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
     333   300455547 :         xfs_bmbt_rec_t          last = {0, 0}; /* last extent in prev block */
     334   300455547 :         xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
     335   300455547 :         int                     bp_release = 0;
     336             : 
     337   300455547 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
     338             :                 return;
     339             : 
     340             :         /* skip large extent count inodes */
     341   143624843 :         if (ip->i_df.if_nextents > 10000)
     342             :                 return;
     343             : 
     344    74164975 :         bno = NULLFSBLOCK;
     345    74164975 :         block = ifp->if_broot;
     346             :         /*
     347             :          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
     348             :          */
     349    74164975 :         level = be16_to_cpu(block->bb_level);
     350    74164975 :         ASSERT(level > 0);
     351    74164975 :         xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
     352    74165717 :         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
     353    74165897 :         bno = be64_to_cpu(*pp);
     354             : 
     355    74165897 :         ASSERT(bno != NULLFSBLOCK);
     356    74165897 :         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
     357    74165897 :         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
     358             : 
     359             :         /*
     360             :          * Go down the tree until leaf level is reached, following the first
     361             :          * pointer (leftmost) at each level.
     362             :          */
     363    90718867 :         while (level-- > 0) {
     364             :                 /* See if buf is in cur first */
     365    90719644 :                 bp_release = 0;
     366    90719644 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     367    90719163 :                 if (!bp) {
     368    26362271 :                         bp_release = 1;
     369    26362271 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     370             :                                                 XFS_BMAP_BTREE_REF,
     371             :                                                 &xfs_bmbt_buf_ops);
     372    26362215 :                         if (xfs_metadata_is_sick(error))
     373           0 :                                 xfs_btree_mark_sick(cur);
     374    26362215 :                         if (error)
     375           6 :                                 goto error_norelse;
     376             :                 }
     377    90719101 :                 block = XFS_BUF_TO_BLOCK(bp);
     378    90719101 :                 if (level == 0)
     379             :                         break;
     380             : 
     381             :                 /*
     382             :                  * Check this block for basic sanity (increasing keys and
     383             :                  * no duplicate blocks).
     384             :                  */
     385             : 
     386    16552668 :                 xfs_check_block(block, mp, 0, 0);
     387    16553117 :                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
     388    16553117 :                 bno = be64_to_cpu(*pp);
     389    16553117 :                 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
     390           0 :                         xfs_btree_mark_sick(cur);
     391           0 :                         error = -EFSCORRUPTED;
     392           0 :                         goto error0;
     393             :                 }
     394    16553089 :                 if (bp_release) {
     395           0 :                         bp_release = 0;
     396           0 :                         xfs_trans_brelse(NULL, bp);
     397             :                 }
     398             :         }
     399             : 
     400             :         /*
     401             :          * Here with bp and block set to the leftmost leaf node in the tree.
     402             :          */
     403             :         i = 0;
     404             : 
     405             :         /*
     406             :          * Loop over all leaf nodes checking that all extents are in the right order.
     407             :          */
     408   984103926 :         for (;;) {
     409   529134791 :                 xfs_fsblock_t   nextbno;
     410   529134791 :                 xfs_extnum_t    num_recs;
     411             : 
     412             : 
     413   529134791 :                 num_recs = xfs_btree_get_numrecs(block);
     414             : 
     415             :                 /*
     416             :                  * Read-ahead the next leaf block, if any.
     417             :                  */
     418             : 
     419   529134791 :                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
     420             : 
     421             :                 /*
     422             :                  * Check all the extents to make sure they are OK.
     423             :                  * If we had a previous block, the last entry should
     424             :                  * conform with the first entry in this one.
     425             :                  */
     426             : 
     427   529134791 :                 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
     428   529134791 :                 if (i) {
     429   454967099 :                         ASSERT(xfs_bmbt_disk_get_startoff(&last) +
     430             :                                xfs_bmbt_disk_get_blockcount(&last) <=
     431             :                                xfs_bmbt_disk_get_startoff(ep));
     432             :                 }
     433 >11656*10^7 :                 for (j = 1; j < num_recs; j++) {
     434 >11603*10^7 :                         nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
     435 >11603*10^7 :                         ASSERT(xfs_bmbt_disk_get_startoff(ep) +
     436             :                                xfs_bmbt_disk_get_blockcount(ep) <=
     437             :                                xfs_bmbt_disk_get_startoff(nextp));
     438 >11603*10^7 :                         ep = nextp;
     439             :                 }
     440             : 
     441   529141983 :                 last = *ep;
     442   529141983 :                 i += num_recs;
     443   529141983 :                 if (bp_release) {
     444   442933120 :                         bp_release = 0;
     445   442933120 :                         xfs_trans_brelse(NULL, bp);
     446             :                 }
     447   529154585 :                 bno = nextbno;
     448             :                 /*
     449             :                  * If we've reached the end, stop.
     450             :                  */
     451   529154585 :                 if (bno == NULLFSBLOCK)
     452             :                         break;
     453             : 
     454   454981134 :                 bp_release = 0;
     455   454981134 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     456   454975666 :                 if (!bp) {
     457   416581431 :                         bp_release = 1;
     458   416581431 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     459             :                                                 XFS_BMAP_BTREE_REF,
     460             :                                                 &xfs_bmbt_buf_ops);
     461   416574960 :                         if (xfs_metadata_is_sick(error))
     462           0 :                                 xfs_btree_mark_sick(cur);
     463   416574960 :                         if (error)
     464          60 :                                 goto error_norelse;
     465             :                 }
     466   454969135 :                 block = XFS_BUF_TO_BLOCK(bp);
     467             :         }
     468             : 
     469             :         return;
     470             : 
     471             : error0:
     472           0 :         xfs_warn(mp, "%s: at error0", __func__);
     473           0 :         if (bp_release)
     474           0 :                 xfs_trans_brelse(NULL, bp);
     475           0 : error_norelse:
     476          66 :         xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
     477             :                 __func__, i);
     478          66 :         xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
     479          66 :         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     480          66 :         return;
     481             : }
     482             : 
     483             : /*
     484             :  * Validate that the bmbt_irecs being returned from bmapi are valid
     485             :  * given the caller's original parameters.  Specifically check the
     486             :  * ranges of the returned irecs to ensure that they only extend beyond
     487             :  * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
     488             :  */
     489             : STATIC void
     490   177084892 : xfs_bmap_validate_ret(
     491             :         xfs_fileoff_t           bno,
     492             :         xfs_filblks_t           len,
     493             :         uint32_t                flags,
     494             :         xfs_bmbt_irec_t         *mval,
     495             :         int                     nmap,
     496             :         int                     ret_nmap)
     497             : {
     498   177084892 :         int                     i;              /* index to map values */
     499             : 
     500   177084892 :         ASSERT(ret_nmap <= nmap);
     501             : 
     502   354146837 :         for (i = 0; i < ret_nmap; i++) {
     503   177074579 :                 ASSERT(mval[i].br_blockcount > 0);
     504   177074579 :                 if (!(flags & XFS_BMAPI_ENTIRE)) {
     505   177074579 :                         ASSERT(mval[i].br_startoff >= bno);
     506   177074579 :                         ASSERT(mval[i].br_blockcount <= len);
     507   177074579 :                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
     508             :                                bno + len);
     509             :                 } else {
     510           0 :                         ASSERT(mval[i].br_startoff < bno + len);
     511           0 :                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
     512             :                                bno);
     513             :                 }
     514   177074579 :                 ASSERT(i == 0 ||
     515             :                        mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
     516             :                        mval[i].br_startoff);
     517   177074579 :                 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
     518             :                        mval[i].br_startblock != HOLESTARTBLOCK);
     519   177074579 :                 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
     520             :                        mval[i].br_state == XFS_EXT_UNWRITTEN);
     521             :         }
     522   177072258 : }
     523             : 
     524             : #else
     525             : #define xfs_bmap_check_leaf_extents(cur, ip, whichfork)         do { } while (0)
     526             : #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)    do { } while (0)
     527             : #endif /* DEBUG */
     528             : 
     529             : /*
     530             :  * Inode fork format manipulation functions
     531             :  */
     532             : 
     533             : /*
     534             :  * Convert the inode format to extent format if it currently is in btree format,
     535             :  * but the extent list is small enough that it fits into the extent format.
     536             :  *
     537             :  * Since the extents are already in-core, all we have to do is give up the space
     538             :  * for the btree root and pitch the leaf block.
     539             :  */
     540             : STATIC int                              /* error */
     541   466955854 : xfs_bmap_btree_to_extents(
     542             :         struct xfs_trans        *tp,    /* transaction pointer */
     543             :         struct xfs_inode        *ip,    /* incore inode pointer */
     544             :         struct xfs_btree_cur    *cur,   /* btree cursor */
     545             :         int                     *logflagsp, /* inode logging flags */
     546             :         int                     whichfork)  /* data or attr fork */
     547             : {
     548   466955854 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     549   466965080 :         struct xfs_mount        *mp = ip->i_mount;
     550   466965080 :         struct xfs_btree_block  *rblock = ifp->if_broot;
     551   466965080 :         struct xfs_btree_block  *cblock;/* child btree block */
     552   466965080 :         xfs_fsblock_t           cbno;   /* child block number */
     553   466965080 :         struct xfs_buf          *cbp;   /* child block's buffer */
     554   466965080 :         int                     error;  /* error return value */
     555   466965080 :         __be64                  *pp;    /* ptr to block address */
     556   466965080 :         struct xfs_owner_info   oinfo;
     557             : 
     558             :         /* check if we actually need the extent format first: */
     559   466965080 :         if (!xfs_bmap_wants_extents(ip, whichfork))
     560             :                 return 0;
     561             : 
     562     1776475 :         ASSERT(cur);
     563     1776475 :         ASSERT(whichfork != XFS_COW_FORK);
     564     1776475 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
     565     1776475 :         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
     566     1776475 :         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
     567     1776475 :         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
     568             : 
     569     1776471 :         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
     570     1776475 :         cbno = be64_to_cpu(*pp);
     571             : #ifdef DEBUG
     572     1776475 :         if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1))) {
     573           0 :                 xfs_btree_mark_sick(cur);
     574           0 :                 return -EFSCORRUPTED;
     575             :         }
     576             : #endif
     577     1776474 :         error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
     578             :                                 &xfs_bmbt_buf_ops);
     579     1776479 :         if (xfs_metadata_is_sick(error))
     580           0 :                 xfs_btree_mark_sick(cur);
     581     1776479 :         if (error)
     582             :                 return error;
     583     1776479 :         cblock = XFS_BUF_TO_BLOCK(cbp);
     584     1776479 :         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
     585             :                 return error;
     586             : 
     587     1776481 :         xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
     588     1776481 :         error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
     589             :                         XFS_AG_RESV_NONE);
     590     1776482 :         if (error)
     591             :                 return error;
     592             : 
     593     1776482 :         ip->i_nblocks--;
     594     1776482 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     595     1776483 :         xfs_trans_binval(tp, cbp);
     596     1776480 :         if (cur->bc_levels[0].bp == cbp)
     597     1776482 :                 cur->bc_levels[0].bp = NULL;
     598     1776480 :         xfs_iroot_realloc(ip, -1, whichfork);
     599     1776479 :         ASSERT(ifp->if_broot == NULL);
     600     1776479 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     601     1776479 :         *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
     602     1776479 :         return 0;
     603             : }
     604             : 
     605             : /*
     606             :  * Convert an extents-format file into a btree-format file.
     607             :  * The new file will have a root block (in the inode) and a single child block.
     608             :  */
     609             : STATIC int                                      /* error */
     610     2553657 : xfs_bmap_extents_to_btree(
     611             :         struct xfs_trans        *tp,            /* transaction pointer */
     612             :         struct xfs_inode        *ip,            /* incore inode pointer */
     613             :         struct xfs_btree_cur    **curp,         /* cursor returned to caller */
     614             :         int                     wasdel,         /* converting a delayed alloc */
     615             :         int                     *logflagsp,     /* inode logging flags */
     616             :         int                     whichfork)      /* data or attr fork */
     617             : {
     618     2553657 :         struct xfs_btree_block  *ablock;        /* allocated (child) bt block */
     619     2553657 :         struct xfs_buf          *abp;           /* buffer for ablock */
     620     2553657 :         struct xfs_alloc_arg    args;           /* allocation arguments */
     621     2553657 :         struct xfs_bmbt_rec     *arp;           /* child record pointer */
     622     2553657 :         struct xfs_btree_block  *block;         /* btree root block */
     623     2553657 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     624     2553657 :         int                     error;          /* error return value */
     625     2553657 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
     626     2553657 :         struct xfs_bmbt_key     *kp;            /* root block key pointer */
     627     2553657 :         struct xfs_mount        *mp;            /* mount structure */
     628     2553657 :         xfs_bmbt_ptr_t          *pp;            /* root block address pointer */
     629     2553657 :         struct xfs_iext_cursor  icur;
     630     2553657 :         struct xfs_bmbt_irec    rec;
     631     2553657 :         xfs_extnum_t            cnt = 0;
     632             : 
     633     2553657 :         mp = ip->i_mount;
     634     2553657 :         ASSERT(whichfork != XFS_COW_FORK);
     635     2553657 :         ifp = xfs_ifork_ptr(ip, whichfork);
     636     2553655 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
     637             : 
     638             :         /*
     639             :          * Make space in the inode incore. This needs to be undone if we fail
     640             :          * to expand the root.
     641             :          */
     642     2553655 :         xfs_iroot_realloc(ip, 1, whichfork);
     643             : 
     644             :         /*
     645             :          * Fill in the root.
     646             :          */
     647     2553661 :         block = ifp->if_broot;
     648     2553661 :         xfs_btree_init_block(mp, block, &xfs_bmbt_ops, 1, 1, ip->i_ino);
     649             :         /*
     650             :          * Need a cursor.  Can't allocate until bb_level is filled in.
     651             :          */
     652     2553654 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
     653     2553656 :         cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
     654             :         /*
     655             :          * Convert to a btree with two levels, one record in root.
     656             :          */
     657     2553656 :         ifp->if_format = XFS_DINODE_FMT_BTREE;
     658     2553656 :         memset(&args, 0, sizeof(args));
     659     2553656 :         args.tp = tp;
     660     2553656 :         args.mp = mp;
     661     2553656 :         xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
     662             : 
     663     2553656 :         args.minlen = args.maxlen = args.prod = 1;
     664     2553656 :         args.wasdel = wasdel;
     665     2553656 :         *logflagsp = 0;
     666    12768280 :         error = xfs_alloc_vextent_start_ag(&args,
     667     2553656 :                                 XFS_INO_TO_FSB(mp, ip->i_ino));
     668     2553660 :         if (error)
     669          54 :                 goto out_root_realloc;
     670             : 
     671             :         /*
     672             :          * Allocation can't fail, the space was reserved.
     673             :          */
     674     2553606 :         if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
     675           0 :                 error = -ENOSPC;
     676           0 :                 goto out_root_realloc;
     677             :         }
     678             : 
     679     2553606 :         cur->bc_ino.allocated++;
     680     2553606 :         ip->i_nblocks++;
     681     2553606 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     682     7660808 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
     683     2553602 :                         XFS_FSB_TO_DADDR(mp, args.fsbno),
     684             :                         mp->m_bsize, 0, &abp);
     685     2553598 :         if (error)
     686           0 :                 goto out_unreserve_dquot;
     687             : 
     688             :         /*
     689             :          * Fill in the child block.
     690             :          */
     691     2553598 :         ablock = XFS_BUF_TO_BLOCK(abp);
     692     2553598 :         xfs_btree_init_buf(mp, abp, &xfs_bmbt_ops, 0, 0, ip->i_ino);
     693             : 
     694    37526332 :         for_each_xfs_iext(ifp, &icur, &rec) {
     695    34972737 :                 if (isnullstartblock(rec.br_startblock))
     696     1525310 :                         continue;
     697    33447427 :                 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
     698    33447427 :                 xfs_bmbt_disk_set_all(arp, &rec);
     699    33447431 :                 cnt++;
     700             :         }
     701     2553602 :         ASSERT(cnt == ifp->if_nextents);
     702     2553602 :         xfs_btree_set_numrecs(ablock, cnt);
     703             : 
     704             :         /*
     705             :          * Fill in the root key and pointer.
     706             :          */
     707     2553602 :         kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
     708     2553602 :         arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
     709     2553602 :         kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
     710     2553599 :         pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
     711             :                                                 be16_to_cpu(block->bb_level)));
     712     2553586 :         *pp = cpu_to_be64(args.fsbno);
     713             : 
     714             :         /*
     715             :          * Do all this logging at the end so that
     716             :          * the root is at the right level.
     717             :          */
     718     2553586 :         xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
     719     2553603 :         xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
     720     2553606 :         ASSERT(*curp == NULL);
     721     2553606 :         *curp = cur;
     722     2553606 :         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
     723     2553606 :         return 0;
     724             : 
     725             : out_unreserve_dquot:
     726           0 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     727          54 : out_root_realloc:
     728          54 :         xfs_iroot_realloc(ip, -1, whichfork);
     729          54 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     730          54 :         ASSERT(ifp->if_broot == NULL);
     731          54 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     732             : 
     733          54 :         return error;
     734             : }
     735             : 
     736             : /*
     737             :  * Convert a local file to an extents file.
     738             :  * This code is out of bounds for data forks of regular files,
     739             :  * since the file data needs to get logged so things will stay consistent.
     740             :  * (The bmap-level manipulations are ok, though).
     741             :  */
     742             : void
     743     5208881 : xfs_bmap_local_to_extents_empty(
     744             :         struct xfs_trans        *tp,
     745             :         struct xfs_inode        *ip,
     746             :         int                     whichfork)
     747             : {
     748     5208881 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     749             : 
     750     5208670 :         ASSERT(whichfork != XFS_COW_FORK);
     751     5208670 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     752     5208670 :         ASSERT(ifp->if_bytes == 0);
     753     5208670 :         ASSERT(ifp->if_nextents == 0);
     754             : 
     755     5208670 :         xfs_bmap_forkoff_reset(ip, whichfork);
     756     5208598 :         ifp->if_u1.if_root = NULL;
     757     5208598 :         ifp->if_height = 0;
     758     5208598 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     759     5208598 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     760     5208984 : }
     761             : 
     762             : 
     763             : int                                     /* error */
     764        1694 : xfs_bmap_local_to_extents(
     765             :         xfs_trans_t     *tp,            /* transaction pointer */
     766             :         xfs_inode_t     *ip,            /* incore inode pointer */
     767             :         xfs_extlen_t    total,          /* total blocks needed by transaction */
     768             :         int             *logflagsp,     /* inode logging flags */
     769             :         int             whichfork,
     770             :         void            (*init_fn)(struct xfs_trans *tp,
     771             :                                    struct xfs_buf *bp,
     772             :                                    struct xfs_inode *ip,
     773             :                                    struct xfs_ifork *ifp, void *priv),
     774             :         void            *priv)
     775             : {
     776        1694 :         int             error = 0;
     777        1694 :         int             flags;          /* logging flags returned */
     778        1694 :         struct xfs_ifork *ifp;          /* inode fork pointer */
     779        1694 :         xfs_alloc_arg_t args;           /* allocation arguments */
     780        1694 :         struct xfs_buf  *bp;            /* buffer for extent block */
     781        1694 :         struct xfs_bmbt_irec rec;
     782        1694 :         struct xfs_iext_cursor icur;
     783             : 
     784             :         /*
     785             :          * We don't want to deal with the case of keeping inode data inline yet.
     786             :          * So sending the data fork of a regular inode is invalid.
     787             :          */
     788        1694 :         ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
     789        1694 :         ifp = xfs_ifork_ptr(ip, whichfork);
     790        1694 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     791             : 
     792        1694 :         if (!ifp->if_bytes) {
     793           0 :                 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     794           0 :                 flags = XFS_ILOG_CORE;
     795           0 :                 goto done;
     796             :         }
     797             : 
     798        1694 :         flags = 0;
     799        1694 :         error = 0;
     800        1694 :         memset(&args, 0, sizeof(args));
     801        1694 :         args.tp = tp;
     802        1694 :         args.mp = ip->i_mount;
     803        1694 :         args.total = total;
     804        1694 :         args.minlen = args.maxlen = args.prod = 1;
     805        1694 :         xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
     806             : 
     807             :         /*
     808             :          * Allocate a block.  We know we need only one, since the
     809             :          * file currently fits in an inode.
     810             :          */
     811        1694 :         args.total = total;
     812        1694 :         args.minlen = args.maxlen = args.prod = 1;
     813        8470 :         error = xfs_alloc_vextent_start_ag(&args,
     814        1694 :                         XFS_INO_TO_FSB(args.mp, ip->i_ino));
     815        1694 :         if (error)
     816           0 :                 goto done;
     817             : 
     818             :         /* Can't fail, the space was reserved. */
     819        1694 :         ASSERT(args.fsbno != NULLFSBLOCK);
     820        1694 :         ASSERT(args.len == 1);
     821        5082 :         error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
     822        1694 :                         XFS_FSB_TO_DADDR(args.mp, args.fsbno),
     823        1694 :                         args.mp->m_bsize, 0, &bp);
     824        1694 :         if (error)
     825           0 :                 goto done;
     826             : 
     827             :         /*
     828             :          * Initialize the block, copy the data and log the remote buffer.
     829             :          *
     830             :          * The callout is responsible for logging because the remote format
     831             :          * might differ from the local format and thus we don't know how much to
     832             :          * log here. Note that init_fn must also set the buffer log item type
     833             :          * correctly.
     834             :          */
     835        1694 :         init_fn(tp, bp, ip, ifp, priv);
     836             : 
     837             :         /* account for the change in fork size */
     838        1694 :         xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
     839        1694 :         xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     840        1694 :         flags |= XFS_ILOG_CORE;
     841             : 
     842        1694 :         ifp->if_u1.if_root = NULL;
     843        1694 :         ifp->if_height = 0;
     844             : 
     845        1694 :         rec.br_startoff = 0;
     846        1694 :         rec.br_startblock = args.fsbno;
     847        1694 :         rec.br_blockcount = 1;
     848        1694 :         rec.br_state = XFS_EXT_NORM;
     849        1694 :         xfs_iext_first(ifp, &icur);
     850        1694 :         xfs_iext_insert(ip, &icur, &rec, 0);
     851             : 
     852        1694 :         ifp->if_nextents = 1;
     853        1694 :         ip->i_nblocks = 1;
     854        1694 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     855        1694 :         flags |= xfs_ilog_fext(whichfork);
     856             : 
     857        1694 : done:
     858        1694 :         *logflagsp = flags;
     859        1694 :         return error;
     860             : }
     861             : 
     862             : /*
     863             :  * Called from xfs_bmap_add_attrfork to handle btree format files.
     864             :  */
     865             : STATIC int                                      /* error */
     866        1908 : xfs_bmap_add_attrfork_btree(
     867             :         xfs_trans_t             *tp,            /* transaction pointer */
     868             :         xfs_inode_t             *ip,            /* incore inode pointer */
     869             :         int                     *flags)         /* inode logging flags */
     870             : {
     871        1908 :         struct xfs_btree_block  *block = ip->i_df.if_broot;
     872        1908 :         struct xfs_btree_cur    *cur;           /* btree cursor */
     873        1908 :         int                     error;          /* error return value */
     874        1908 :         xfs_mount_t             *mp;            /* file system mount struct */
     875        1908 :         int                     stat;           /* newroot status */
     876             : 
     877        1908 :         mp = ip->i_mount;
     878             : 
     879        1908 :         if (XFS_BMAP_BMDR_SPACE(block) <= xfs_inode_data_fork_size(ip))
     880        1908 :                 *flags |= XFS_ILOG_DBROOT;
     881             :         else {
     882           0 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
     883           0 :                 error = xfs_bmbt_lookup_first(cur, &stat);
     884           0 :                 if (error)
     885           0 :                         goto error0;
     886             :                 /* must be at least one entry */
     887           0 :                 if (XFS_IS_CORRUPT(mp, stat != 1)) {
     888           0 :                         xfs_btree_mark_sick(cur);
     889           0 :                         error = -EFSCORRUPTED;
     890           0 :                         goto error0;
     891             :                 }
     892           0 :                 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
     893           0 :                         goto error0;
     894           0 :                 if (stat == 0) {
     895           0 :                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     896           0 :                         return -ENOSPC;
     897             :                 }
     898           0 :                 cur->bc_ino.allocated = 0;
     899           0 :                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     900             :         }
     901             :         return 0;
     902           0 : error0:
     903           0 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     904           0 :         return error;
     905             : }
     906             : 
     907             : /*
     908             :  * Called from xfs_bmap_add_attrfork to handle extents format files.
     909             :  */
     910             : STATIC int                                      /* error */
     911     4436184 : xfs_bmap_add_attrfork_extents(
     912             :         struct xfs_trans        *tp,            /* transaction pointer */
     913             :         struct xfs_inode        *ip,            /* incore inode pointer */
     914             :         int                     *flags)         /* inode logging flags */
     915             : {
     916     4436184 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     917     4436184 :         int                     error;          /* error return value */
     918             : 
     919     4436184 :         if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
     920     4436184 :             xfs_inode_data_fork_size(ip))
     921             :                 return 0;
     922        1440 :         cur = NULL;
     923        1440 :         error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
     924             :                                           XFS_DATA_FORK);
     925        1440 :         if (cur) {
     926        1440 :                 cur->bc_ino.allocated = 0;
     927        1440 :                 xfs_btree_del_cursor(cur, error);
     928             :         }
     929             :         return error;
     930             : }
     931             : 
     932             : /*
     933             :  * Called from xfs_bmap_add_attrfork to handle local format files. Each
     934             :  * different data fork content type needs a different callout to do the
     935             :  * conversion. Some are basic and only require special block initialisation
     936             :  * callouts for the data formating, others (directories) are so specialised they
     937             :  * handle everything themselves.
     938             :  *
     939             :  * XXX (dgc): investigate whether directory conversion can use the generic
     940             :  * formatting callout. It should be possible - it's just a very complex
     941             :  * formatter.
     942             :  */
     943             : STATIC int                                      /* error */
     944       57864 : xfs_bmap_add_attrfork_local(
     945             :         struct xfs_trans        *tp,            /* transaction pointer */
     946             :         struct xfs_inode        *ip,            /* incore inode pointer */
     947             :         int                     *flags)         /* inode logging flags */
     948             : {
     949       57864 :         struct xfs_da_args      dargs;          /* args for dir/attr code */
     950             : 
     951       57864 :         if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
     952             :                 return 0;
     953             : 
     954          76 :         if (S_ISDIR(VFS_I(ip)->i_mode)) {
     955          74 :                 memset(&dargs, 0, sizeof(dargs));
     956          74 :                 dargs.geo = ip->i_mount->m_dir_geo;
     957          74 :                 dargs.dp = ip;
     958          74 :                 dargs.total = dargs.geo->fsbcount;
     959          74 :                 dargs.whichfork = XFS_DATA_FORK;
     960          74 :                 dargs.trans = tp;
     961          74 :                 dargs.owner = ip->i_ino;
     962          74 :                 return xfs_dir2_sf_to_block(&dargs);
     963             :         }
     964             : 
     965           2 :         if (S_ISLNK(VFS_I(ip)->i_mode))
     966           2 :                 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
     967             :                                 XFS_DATA_FORK, xfs_symlink_local_to_remote,
     968             :                                 NULL);
     969             : 
     970             :         /* should only be called for types that support local format data */
     971           0 :         ASSERT(0);
     972           0 :         xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
     973           0 :         return -EFSCORRUPTED;
     974             : }
     975             : 
     976             : /*
     977             :  * Set an inode attr fork offset based on the format of the data fork.
     978             :  */
     979             : static int
     980     4496344 : xfs_bmap_set_attrforkoff(
     981             :         struct xfs_inode        *ip,
     982             :         int                     size,
     983             :         int                     *version)
     984             : {
     985     4496344 :         int                     default_size = xfs_default_attroffset(ip) >> 3;
     986             : 
     987     4496344 :         switch (ip->i_df.if_format) {
     988           6 :         case XFS_DINODE_FMT_DEV:
     989           6 :                 ip->i_forkoff = default_size;
     990           6 :                 break;
     991     4496338 :         case XFS_DINODE_FMT_LOCAL:
     992             :         case XFS_DINODE_FMT_EXTENTS:
     993             :         case XFS_DINODE_FMT_BTREE:
     994     4496338 :                 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
     995     4495284 :                 if (!ip->i_forkoff)
     996          63 :                         ip->i_forkoff = default_size;
     997     4495221 :                 else if (xfs_has_attr2(ip->i_mount) && version)
     998     4495221 :                         *version = 2;
     999             :                 break;
    1000           0 :         default:
    1001           0 :                 ASSERT(0);
    1002           0 :                 return -EINVAL;
    1003             :         }
    1004             : 
    1005             :         return 0;
    1006             : }
    1007             : 
    1008             : /*
    1009             :  * Convert inode from non-attributed to attributed.
    1010             :  * Must not be in a transaction, ip must not be locked.
    1011             :  */
    1012             : int                                             /* error code */
    1013     8784622 : xfs_bmap_add_attrfork(
    1014             :         xfs_inode_t             *ip,            /* incore inode pointer */
    1015             :         int                     size,           /* space new attribute needs */
    1016             :         int                     rsvd)           /* xact may use reserved blks */
    1017             : {
    1018     8784622 :         xfs_mount_t             *mp;            /* mount structure */
    1019     8784622 :         xfs_trans_t             *tp;            /* transaction pointer */
    1020     8784622 :         int                     blks;           /* space reservation */
    1021     8784622 :         int                     version = 1;    /* superblock attr version */
    1022     8784622 :         int                     logflags;       /* logging flags */
    1023     8784622 :         int                     error;          /* error return value */
    1024             : 
    1025     8784622 :         mp = ip->i_mount;
    1026     8784622 :         ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
    1027             : 
    1028     8784622 :         blks = XFS_ADDAFORK_SPACE_RES(mp);
    1029             : 
    1030     8784622 :         error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
    1031             :                         rsvd, &tp);
    1032     8785625 :         if (error)
    1033             :                 return error;
    1034     8785312 :         if (xfs_inode_has_attr_fork(ip))
    1035     4288824 :                 goto trans_cancel;
    1036             : 
    1037     4496488 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    1038     4496344 :         error = xfs_bmap_set_attrforkoff(ip, size, &version);
    1039     4495346 :         if (error)
    1040           0 :                 goto trans_cancel;
    1041             : 
    1042     4495346 :         xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
    1043     4495519 :         logflags = 0;
    1044     4495519 :         switch (ip->i_df.if_format) {
    1045       57871 :         case XFS_DINODE_FMT_LOCAL:
    1046       57871 :                 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
    1047       57871 :                 break;
    1048     4435734 :         case XFS_DINODE_FMT_EXTENTS:
    1049     4435734 :                 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
    1050     4435734 :                 break;
    1051        1908 :         case XFS_DINODE_FMT_BTREE:
    1052        1908 :                 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
    1053        1908 :                 break;
    1054             :         default:
    1055             :                 error = 0;
    1056             :                 break;
    1057             :         }
    1058     4495262 :         if (logflags)
    1059        3350 :                 xfs_trans_log_inode(tp, ip, logflags);
    1060     4495229 :         if (error)
    1061           0 :                 goto trans_cancel;
    1062     4495229 :         if (!xfs_has_attr(mp) ||
    1063           0 :            (!xfs_has_attr2(mp) && version == 2)) {
    1064           0 :                 bool log_sb = false;
    1065             : 
    1066           0 :                 spin_lock(&mp->m_sb_lock);
    1067           0 :                 if (!xfs_has_attr(mp)) {
    1068           0 :                         xfs_add_attr(mp);
    1069           0 :                         log_sb = true;
    1070             :                 }
    1071           0 :                 if (!xfs_has_attr2(mp) && version == 2) {
    1072           0 :                         xfs_add_attr2(mp);
    1073           0 :                         log_sb = true;
    1074             :                 }
    1075           0 :                 spin_unlock(&mp->m_sb_lock);
    1076           0 :                 if (log_sb)
    1077           0 :                         xfs_log_sb(tp);
    1078             :         }
    1079             : 
    1080     4495229 :         error = xfs_trans_commit(tp);
    1081     4496487 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1082     4496487 :         return error;
    1083             : 
    1084     4288824 : trans_cancel:
    1085     4288824 :         xfs_trans_cancel(tp);
    1086     4288266 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1087     4288266 :         return error;
    1088             : }
    1089             : 
    1090             : /*
    1091             :  * Internal and external extent tree search functions.
    1092             :  */
    1093             : 
    1094             : struct xfs_iread_state {
    1095             :         struct xfs_iext_cursor  icur;
    1096             :         xfs_extnum_t            loaded;
    1097             : };
    1098             : 
    1099             : int
    1100           0 : xfs_bmap_complain_bad_rec(
    1101             :         struct xfs_inode                *ip,
    1102             :         int                             whichfork,
    1103             :         xfs_failaddr_t                  fa,
    1104             :         const struct xfs_bmbt_irec      *irec)
    1105             : {
    1106           0 :         struct xfs_mount                *mp = ip->i_mount;
    1107           0 :         const char                      *forkname;
    1108             : 
    1109           0 :         switch (whichfork) {
    1110             :         case XFS_DATA_FORK:     forkname = "data"; break;
    1111             :         case XFS_ATTR_FORK:     forkname = "attr"; break;
    1112             :         case XFS_COW_FORK:      forkname = "CoW"; break;
    1113             :         default:                forkname = "???"; break;
    1114             :         }
    1115             : 
    1116           0 :         xfs_warn(mp,
    1117             :  "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
    1118             :                                 ip->i_ino, forkname, fa);
    1119           0 :         xfs_warn(mp,
    1120             :                 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
    1121             :                 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
    1122             :                 irec->br_state);
    1123             : 
    1124           0 :         return -EFSCORRUPTED;
    1125             : }
    1126             : 
    1127             : /* Stuff every bmbt record from this block into the incore extent map. */
    1128             : static int
    1129     5322555 : xfs_iread_bmbt_block(
    1130             :         struct xfs_btree_cur    *cur,
    1131             :         int                     level,
    1132             :         void                    *priv)
    1133             : {
    1134     5322555 :         struct xfs_iread_state  *ir = priv;
    1135     5322555 :         struct xfs_mount        *mp = cur->bc_mp;
    1136     5322555 :         struct xfs_inode        *ip = cur->bc_ino.ip;
    1137     5322555 :         struct xfs_btree_block  *block;
    1138     5322555 :         struct xfs_buf          *bp;
    1139     5322555 :         struct xfs_bmbt_rec     *frp;
    1140     5322555 :         xfs_extnum_t            num_recs;
    1141     5322555 :         xfs_extnum_t            j;
    1142     5322555 :         int                     whichfork = cur->bc_ino.whichfork;
    1143     5322555 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1144             : 
    1145     5322537 :         block = xfs_btree_get_block(cur, level, &bp);
    1146             : 
    1147             :         /* Abort if we find more records than nextents. */
    1148     5322536 :         num_recs = xfs_btree_get_numrecs(block);
    1149     5322536 :         if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
    1150           0 :                 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
    1151             :                                 (unsigned long long)ip->i_ino);
    1152           0 :                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
    1153           0 :                                 sizeof(*block), __this_address);
    1154           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    1155           0 :                 return -EFSCORRUPTED;
    1156             :         }
    1157             : 
    1158             :         /* Copy records into the incore cache. */
    1159     5322536 :         frp = XFS_BMBT_REC_ADDR(mp, block, 1);
    1160  1082058364 :         for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
    1161  1076735785 :                 struct xfs_bmbt_irec    new;
    1162  1076735785 :                 xfs_failaddr_t          fa;
    1163             : 
    1164  1076735785 :                 xfs_bmbt_disk_get_all(frp, &new);
    1165  1076737732 :                 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
    1166  1076742931 :                 if (fa) {
    1167           0 :                         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
    1168             :                                         "xfs_iread_extents(2)", frp,
    1169             :                                         sizeof(*frp), fa);
    1170           0 :                         xfs_bmap_mark_sick(ip, whichfork);
    1171           0 :                         return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
    1172             :                                         &new);
    1173             :                 }
    1174  2153470882 :                 xfs_iext_insert(ip, &ir->icur, &new,
    1175             :                                 xfs_bmap_fork_to_state(whichfork));
    1176  1076738813 :                 trace_xfs_read_extent(ip, &ir->icur,
    1177  1076738813 :                                 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
    1178  1076738068 :                 xfs_iext_next(ifp, &ir->icur);
    1179             :         }
    1180             : 
    1181             :         return 0;
    1182             : }
    1183             : 
    1184             : /*
    1185             :  * Read in extents from a btree-format inode.
    1186             :  */
    1187             : int
    1188 11103877242 : xfs_iread_extents(
    1189             :         struct xfs_trans        *tp,
    1190             :         struct xfs_inode        *ip,
    1191             :         int                     whichfork)
    1192             : {
    1193 11103877242 :         struct xfs_iread_state  ir;
    1194 11103877242 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1195 11098836505 :         struct xfs_mount        *mp = ip->i_mount;
    1196 11098836505 :         struct xfs_btree_cur    *cur;
    1197 11098836505 :         int                     error;
    1198             : 
    1199 11098836505 :         if (!xfs_need_iread_extents(ifp))
    1200             :                 return 0;
    1201             : 
    1202      182119 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1203             : 
    1204      182111 :         ir.loaded = 0;
    1205      182111 :         xfs_iext_first(ifp, &ir.icur);
    1206      182110 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    1207      182147 :         error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
    1208             :                         XFS_BTREE_VISIT_RECORDS, &ir);
    1209      182141 :         xfs_btree_del_cursor(cur, error);
    1210      182141 :         if (error)
    1211         266 :                 goto out;
    1212             : 
    1213      181875 :         if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
    1214           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    1215           0 :                 error = -EFSCORRUPTED;
    1216           0 :                 goto out;
    1217             :         }
    1218      181875 :         ASSERT(ir.loaded == xfs_iext_count(ifp));
    1219             :         /*
    1220             :          * Use release semantics so that we can use acquire semantics in
    1221             :          * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
    1222             :          * after that load.
    1223             :          */
    1224      181867 :         smp_store_release(&ifp->if_needextents, 0);
    1225      181867 :         return 0;
    1226         266 : out:
    1227         266 :         if (xfs_metadata_is_sick(error))
    1228          10 :                 xfs_bmap_mark_sick(ip, whichfork);
    1229         266 :         xfs_iext_destroy(ifp);
    1230         266 :         return error;
    1231             : }
    1232             : 
    1233             : /*
    1234             :  * Returns the relative block number of the first unused block(s) in the given
    1235             :  * fork with at least "len" logically contiguous blocks free.  This is the
    1236             :  * lowest-address hole if the fork has holes, else the first block past the end
    1237             :  * of fork.  Return 0 if the fork is currently local (in-inode).
    1238             :  */
    1239             : int                                             /* error */
    1240     5710796 : xfs_bmap_first_unused(
    1241             :         struct xfs_trans        *tp,            /* transaction pointer */
    1242             :         struct xfs_inode        *ip,            /* incore inode */
    1243             :         xfs_extlen_t            len,            /* size of hole to find */
    1244             :         xfs_fileoff_t           *first_unused,  /* unused block */
    1245             :         int                     whichfork)      /* data or attr fork */
    1246             : {
    1247     5710796 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1248     5710615 :         struct xfs_bmbt_irec    got;
    1249     5710615 :         struct xfs_iext_cursor  icur;
    1250     5710615 :         xfs_fileoff_t           lastaddr = 0;
    1251     5710615 :         xfs_fileoff_t           lowest, max;
    1252     5710615 :         int                     error;
    1253             : 
    1254     5710615 :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
    1255           0 :                 *first_unused = 0;
    1256           0 :                 return 0;
    1257             :         }
    1258             : 
    1259     5710615 :         ASSERT(xfs_ifork_has_extents(ifp));
    1260             : 
    1261     5710615 :         error = xfs_iread_extents(tp, ip, whichfork);
    1262     5710804 :         if (error)
    1263             :                 return error;
    1264             : 
    1265     5710852 :         lowest = max = *first_unused;
    1266    70692074 :         for_each_xfs_iext(ifp, &icur, &got) {
    1267             :                 /*
    1268             :                  * See if the hole before this extent will work.
    1269             :                  */
    1270    65261656 :                 if (got.br_startoff >= lowest + len &&
    1271    47005279 :                     got.br_startoff - max >= len)
    1272             :                         break;
    1273    64982155 :                 lastaddr = got.br_startoff + got.br_blockcount;
    1274    64982155 :                 max = XFS_FILEOFF_MAX(lastaddr, lowest);
    1275             :         }
    1276             : 
    1277     5710179 :         *first_unused = max;
    1278     5710179 :         return 0;
    1279             : }
    1280             : 
    1281             : /*
    1282             :  * Returns the file-relative block number of the last block - 1 before
    1283             :  * last_block (input value) in the file.
    1284             :  * This is not based on i_size, it is based on the extent records.
    1285             :  * Returns 0 for local files, as they do not have extent records.
    1286             :  */
    1287             : int                                             /* error */
    1288      424785 : xfs_bmap_last_before(
    1289             :         struct xfs_trans        *tp,            /* transaction pointer */
    1290             :         struct xfs_inode        *ip,            /* incore inode */
    1291             :         xfs_fileoff_t           *last_block,    /* last block */
    1292             :         int                     whichfork)      /* data or attr fork */
    1293             : {
    1294      424785 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1295      424785 :         struct xfs_bmbt_irec    got;
    1296      424785 :         struct xfs_iext_cursor  icur;
    1297      424785 :         int                     error;
    1298             : 
    1299      424785 :         switch (ifp->if_format) {
    1300           0 :         case XFS_DINODE_FMT_LOCAL:
    1301           0 :                 *last_block = 0;
    1302           0 :                 return 0;
    1303             :         case XFS_DINODE_FMT_BTREE:
    1304             :         case XFS_DINODE_FMT_EXTENTS:
    1305      424785 :                 break;
    1306           0 :         default:
    1307           0 :                 ASSERT(0);
    1308           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    1309           0 :                 return -EFSCORRUPTED;
    1310             :         }
    1311             : 
    1312      424785 :         error = xfs_iread_extents(tp, ip, whichfork);
    1313      424786 :         if (error)
    1314             :                 return error;
    1315             : 
    1316      424787 :         if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
    1317      369549 :                 *last_block = 0;
    1318             :         return 0;
    1319             : }
    1320             : 
    1321             : int
    1322   908034213 : xfs_bmap_last_extent(
    1323             :         struct xfs_trans        *tp,
    1324             :         struct xfs_inode        *ip,
    1325             :         int                     whichfork,
    1326             :         struct xfs_bmbt_irec    *rec,
    1327             :         int                     *is_empty)
    1328             : {
    1329   908034213 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1330   907972616 :         struct xfs_iext_cursor  icur;
    1331   907972616 :         int                     error;
    1332             : 
    1333   907972616 :         error = xfs_iread_extents(tp, ip, whichfork);
    1334   908851845 :         if (error)
    1335             :                 return error;
    1336             : 
    1337   908983272 :         xfs_iext_last(ifp, &icur);
    1338   910494961 :         if (!xfs_iext_get_extent(ifp, &icur, rec))
    1339    54034497 :                 *is_empty = 1;
    1340             :         else
    1341   856083591 :                 *is_empty = 0;
    1342             :         return 0;
    1343             : }
    1344             : 
    1345             : /*
    1346             :  * Check the last inode extent to determine whether this allocation will result
    1347             :  * in blocks being allocated at the end of the file. When we allocate new data
    1348             :  * blocks at the end of the file which do not start at the previous data block,
    1349             :  * we will try to align the new blocks at stripe unit boundaries.
    1350             :  *
    1351             :  * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
    1352             :  * at, or past the EOF.
    1353             :  */
    1354             : STATIC int
    1355     1025800 : xfs_bmap_isaeof(
    1356             :         struct xfs_bmalloca     *bma,
    1357             :         int                     whichfork)
    1358             : {
    1359     1025800 :         struct xfs_bmbt_irec    rec;
    1360     1025800 :         int                     is_empty;
    1361     1025800 :         int                     error;
    1362             : 
    1363     1025800 :         bma->aeof = false;
    1364     1025800 :         error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
    1365             :                                      &is_empty);
    1366     1026265 :         if (error)
    1367             :                 return error;
    1368             : 
    1369     1026265 :         if (is_empty) {
    1370       42230 :                 bma->aeof = true;
    1371       42230 :                 return 0;
    1372             :         }
    1373             : 
    1374             :         /*
    1375             :          * Check if we are allocation or past the last extent, or at least into
    1376             :          * the last delayed allocated extent.
    1377             :          */
    1378      984035 :         bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
    1379      234618 :                 (bma->offset >= rec.br_startoff &&
    1380      234618 :                  isnullstartblock(rec.br_startblock));
    1381      984035 :         return 0;
    1382             : }
    1383             : 
    1384             : /*
    1385             :  * Returns the file-relative block number of the first block past eof in
    1386             :  * the file.  This is not based on i_size, it is based on the extent records.
    1387             :  * Returns 0 for local files, as they do not have extent records.
    1388             :  */
    1389             : int
    1390   905337871 : xfs_bmap_last_offset(
    1391             :         struct xfs_inode        *ip,
    1392             :         xfs_fileoff_t           *last_block,
    1393             :         int                     whichfork)
    1394             : {
    1395   905337871 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1396   904686705 :         struct xfs_bmbt_irec    rec;
    1397   904686705 :         int                     is_empty;
    1398   904686705 :         int                     error;
    1399             : 
    1400   904686705 :         *last_block = 0;
    1401             : 
    1402   904686705 :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
    1403             :                 return 0;
    1404             : 
    1405   904686705 :         if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
    1406           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    1407           0 :                 return -EFSCORRUPTED;
    1408             :         }
    1409             : 
    1410   904686705 :         error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
    1411   907552108 :         if (error || is_empty)
    1412             :                 return error;
    1413             : 
    1414   853545994 :         *last_block = rec.br_startoff + rec.br_blockcount;
    1415   853545994 :         return 0;
    1416             : }
    1417             : 
    1418             : /*
    1419             :  * Extent tree manipulation functions used during allocation.
    1420             :  */
    1421             : 
    1422             : /*
    1423             :  * Convert a delayed allocation to a real allocation.
    1424             :  */
    1425             : STATIC int                              /* error */
    1426    27680633 : xfs_bmap_add_extent_delay_real(
    1427             :         struct xfs_bmalloca     *bma,
    1428             :         int                     whichfork)
    1429             : {
    1430    27680633 :         struct xfs_mount        *mp = bma->ip->i_mount;
    1431    27680633 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    1432    27680232 :         struct xfs_bmbt_irec    *new = &bma->got;
    1433    27680232 :         int                     error;  /* error return value */
    1434    27680232 :         int                     i;      /* temp state */
    1435    27680232 :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    1436    27680232 :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    1437             :                                         /* left is 0, right is 1, prev is 2 */
    1438    27680232 :         int                     rval=0; /* return value (logging flags) */
    1439    27680232 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    1440    27680232 :         xfs_filblks_t           da_new; /* new count del alloc blocks used */
    1441    27680232 :         xfs_filblks_t           da_old; /* old count del alloc blocks used */
    1442    27680232 :         xfs_filblks_t           temp=0; /* value for da_new calculations */
    1443    27680232 :         int                     tmp_rval;       /* partial logging flags */
    1444    27680232 :         struct xfs_bmbt_irec    old;
    1445             : 
    1446    27680232 :         ASSERT(whichfork != XFS_ATTR_FORK);
    1447    27680232 :         ASSERT(!isnullstartblock(new->br_startblock));
    1448    27680232 :         ASSERT(!bma->cur ||
    1449             :                (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    1450             : 
    1451    27680232 :         XFS_STATS_INC(mp, xs_add_exlist);
    1452             : 
    1453             : #define LEFT            r[0]
    1454             : #define RIGHT           r[1]
    1455             : #define PREV            r[2]
    1456             : 
    1457             :         /*
    1458             :          * Set up a bunch of variables to make the tests simpler.
    1459             :          */
    1460    27680396 :         xfs_iext_get_extent(ifp, &bma->icur, &PREV);
    1461    27680605 :         new_endoff = new->br_startoff + new->br_blockcount;
    1462    27680605 :         ASSERT(isnullstartblock(PREV.br_startblock));
    1463    27680605 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    1464    27680605 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    1465             : 
    1466    27680605 :         da_old = startblockval(PREV.br_startblock);
    1467    27680605 :         da_new = 0;
    1468             : 
    1469             :         /*
    1470             :          * Set flags determining what part of the previous delayed allocation
    1471             :          * extent is being replaced by a real allocation.
    1472             :          */
    1473    27680605 :         if (PREV.br_startoff == new->br_startoff)
    1474    27680669 :                 state |= BMAP_LEFT_FILLING;
    1475    27680605 :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    1476    25388894 :                 state |= BMAP_RIGHT_FILLING;
    1477             : 
    1478             :         /*
    1479             :          * Check and set flags if this segment has a left neighbor.
    1480             :          * Don't set contiguous if the combined extent would be too large.
    1481             :          */
    1482    27680605 :         if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
    1483    20437205 :                 state |= BMAP_LEFT_VALID;
    1484    20437205 :                 if (isnullstartblock(LEFT.br_startblock))
    1485      153405 :                         state |= BMAP_LEFT_DELAY;
    1486             :         }
    1487             : 
    1488    27680630 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    1489    20283845 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    1490     4666480 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    1491      530565 :             LEFT.br_state == new->br_state &&
    1492      324104 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    1493      311338 :                 state |= BMAP_LEFT_CONTIG;
    1494             : 
    1495             :         /*
    1496             :          * Check and set flags if this segment has a right neighbor.
    1497             :          * Don't set contiguous if the combined extent would be too large.
    1498             :          * Also check for all-three-contiguous being too large.
    1499             :          */
    1500    27680630 :         if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
    1501    14566559 :                 state |= BMAP_RIGHT_VALID;
    1502    14566559 :                 if (isnullstartblock(RIGHT.br_startblock))
    1503     4443062 :                         state |= BMAP_RIGHT_DELAY;
    1504             :         }
    1505             : 
    1506    27680347 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    1507    10123563 :             new_endoff == RIGHT.br_startoff &&
    1508     2160814 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    1509      129876 :             new->br_state == RIGHT.br_state &&
    1510       77697 :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    1511       77697 :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1512             :                        BMAP_RIGHT_FILLING)) !=
    1513             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1514       46519 :                        BMAP_RIGHT_FILLING) ||
    1515       46519 :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    1516             :                         <= XFS_MAX_BMBT_EXTLEN))
    1517       77697 :                 state |= BMAP_RIGHT_CONTIG;
    1518             : 
    1519    27680347 :         error = 0;
    1520             :         /*
    1521             :          * Switch out based on the FILLING and CONTIG state bits.
    1522             :          */
    1523    27680347 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1524             :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    1525       46519 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1526             :              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1527             :                 /*
    1528             :                  * Filling in all of a previously delayed allocation extent.
    1529             :                  * The left and right neighbors are both contiguous with new.
    1530             :                  */
    1531       46519 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    1532             : 
    1533       46519 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1534       46519 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1535       46519 :                 xfs_iext_prev(ifp, &bma->icur);
    1536       46519 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1537       46519 :                 ifp->if_nextents--;
    1538             : 
    1539       46519 :                 if (bma->cur == NULL)
    1540             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1541             :                 else {
    1542       21130 :                         rval = XFS_ILOG_CORE;
    1543       21130 :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1544       21130 :                         if (error)
    1545           0 :                                 goto done;
    1546       21130 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1547           0 :                                 xfs_btree_mark_sick(bma->cur);
    1548           0 :                                 error = -EFSCORRUPTED;
    1549           0 :                                 goto done;
    1550             :                         }
    1551       21130 :                         error = xfs_btree_delete(bma->cur, &i);
    1552       21130 :                         if (error)
    1553           0 :                                 goto done;
    1554       21130 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1555           0 :                                 xfs_btree_mark_sick(bma->cur);
    1556           0 :                                 error = -EFSCORRUPTED;
    1557           0 :                                 goto done;
    1558             :                         }
    1559       21130 :                         error = xfs_btree_decrement(bma->cur, 0, &i);
    1560       21130 :                         if (error)
    1561           0 :                                 goto done;
    1562       21130 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1563           0 :                                 xfs_btree_mark_sick(bma->cur);
    1564           0 :                                 error = -EFSCORRUPTED;
    1565           0 :                                 goto done;
    1566             :                         }
    1567       21130 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1568       21130 :                         if (error)
    1569           0 :                                 goto done;
    1570             :                 }
    1571             :                 break;
    1572             : 
    1573      193538 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    1574             :                 /*
    1575             :                  * Filling in all of a previously delayed allocation extent.
    1576             :                  * The left neighbor is contiguous, the right is not.
    1577             :                  */
    1578      193538 :                 old = LEFT;
    1579      193538 :                 LEFT.br_blockcount += PREV.br_blockcount;
    1580             : 
    1581      193538 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1582      193538 :                 xfs_iext_prev(ifp, &bma->icur);
    1583      193538 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1584             : 
    1585      193538 :                 if (bma->cur == NULL)
    1586             :                         rval = XFS_ILOG_DEXT;
    1587             :                 else {
    1588       67194 :                         rval = 0;
    1589       67194 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1590       67194 :                         if (error)
    1591           0 :                                 goto done;
    1592       67194 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1593           0 :                                 xfs_btree_mark_sick(bma->cur);
    1594           0 :                                 error = -EFSCORRUPTED;
    1595           0 :                                 goto done;
    1596             :                         }
    1597       67194 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1598       67194 :                         if (error)
    1599           0 :                                 goto done;
    1600             :                 }
    1601             :                 break;
    1602             : 
    1603       31178 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1604             :                 /*
    1605             :                  * Filling in all of a previously delayed allocation extent.
    1606             :                  * The right neighbor is contiguous, the left is not. Take care
    1607             :                  * with delay -> unwritten extent allocation here because the
    1608             :                  * delalloc record we are overwriting is always written.
    1609             :                  */
    1610       31178 :                 PREV.br_startblock = new->br_startblock;
    1611       31178 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    1612       31178 :                 PREV.br_state = new->br_state;
    1613             : 
    1614       31178 :                 xfs_iext_next(ifp, &bma->icur);
    1615       31178 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1616       31178 :                 xfs_iext_prev(ifp, &bma->icur);
    1617       31178 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1618             : 
    1619       31178 :                 if (bma->cur == NULL)
    1620             :                         rval = XFS_ILOG_DEXT;
    1621             :                 else {
    1622       13825 :                         rval = 0;
    1623       13825 :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1624       13825 :                         if (error)
    1625           0 :                                 goto done;
    1626       13825 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1627           0 :                                 xfs_btree_mark_sick(bma->cur);
    1628           0 :                                 error = -EFSCORRUPTED;
    1629           0 :                                 goto done;
    1630             :                         }
    1631       13825 :                         error = xfs_bmbt_update(bma->cur, &PREV);
    1632       13825 :                         if (error)
    1633           0 :                                 goto done;
    1634             :                 }
    1635             :                 break;
    1636             : 
    1637    25117522 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    1638             :                 /*
    1639             :                  * Filling in all of a previously delayed allocation extent.
    1640             :                  * Neither the left nor right neighbors are contiguous with
    1641             :                  * the new one.
    1642             :                  */
    1643    25117522 :                 PREV.br_startblock = new->br_startblock;
    1644    25117522 :                 PREV.br_state = new->br_state;
    1645    25117522 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1646    25117553 :                 ifp->if_nextents++;
    1647             : 
    1648    25117553 :                 if (bma->cur == NULL)
    1649             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1650             :                 else {
    1651     6954810 :                         rval = XFS_ILOG_CORE;
    1652     6954810 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1653     6954845 :                         if (error)
    1654           0 :                                 goto done;
    1655     6954845 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1656           0 :                                 xfs_btree_mark_sick(bma->cur);
    1657           0 :                                 error = -EFSCORRUPTED;
    1658           0 :                                 goto done;
    1659             :                         }
    1660     6954845 :                         error = xfs_btree_insert(bma->cur, &i);
    1661     6954822 :                         if (error)
    1662           0 :                                 goto done;
    1663     6954822 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1664           0 :                                 xfs_btree_mark_sick(bma->cur);
    1665           0 :                                 error = -EFSCORRUPTED;
    1666           0 :                                 goto done;
    1667             :                         }
    1668             :                 }
    1669             :                 break;
    1670             : 
    1671       71281 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
    1672             :                 /*
    1673             :                  * Filling in the first part of a previous delayed allocation.
    1674             :                  * The left neighbor is contiguous.
    1675             :                  */
    1676       71281 :                 old = LEFT;
    1677       71281 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1678       71281 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1679             :                                 startblockval(PREV.br_startblock));
    1680             : 
    1681       71281 :                 LEFT.br_blockcount += new->br_blockcount;
    1682             : 
    1683       71281 :                 PREV.br_blockcount = temp;
    1684       71281 :                 PREV.br_startoff += new->br_blockcount;
    1685       71281 :                 PREV.br_startblock = nullstartblock(da_new);
    1686             : 
    1687       71281 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1688       71281 :                 xfs_iext_prev(ifp, &bma->icur);
    1689       71281 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1690             : 
    1691       71281 :                 if (bma->cur == NULL)
    1692             :                         rval = XFS_ILOG_DEXT;
    1693             :                 else {
    1694       42107 :                         rval = 0;
    1695       42107 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1696       42107 :                         if (error)
    1697           0 :                                 goto done;
    1698       42107 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1699           0 :                                 xfs_btree_mark_sick(bma->cur);
    1700           0 :                                 error = -EFSCORRUPTED;
    1701           0 :                                 goto done;
    1702             :                         }
    1703       42107 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1704       42107 :                         if (error)
    1705           0 :                                 goto done;
    1706             :                 }
    1707             :                 break;
    1708             : 
    1709     2220309 :         case BMAP_LEFT_FILLING:
    1710             :                 /*
    1711             :                  * Filling in the first part of a previous delayed allocation.
    1712             :                  * The left neighbor is not contiguous.
    1713             :                  */
    1714     2220309 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1715     2220314 :                 ifp->if_nextents++;
    1716             : 
    1717     2220314 :                 if (bma->cur == NULL)
    1718             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1719             :                 else {
    1720     1383421 :                         rval = XFS_ILOG_CORE;
    1721     1383421 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1722     1383576 :                         if (error)
    1723           0 :                                 goto done;
    1724     1383576 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1725           0 :                                 xfs_btree_mark_sick(bma->cur);
    1726           0 :                                 error = -EFSCORRUPTED;
    1727           0 :                                 goto done;
    1728             :                         }
    1729     1383576 :                         error = xfs_btree_insert(bma->cur, &i);
    1730     1383408 :                         if (error)
    1731           0 :                                 goto done;
    1732     1383408 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1733           0 :                                 xfs_btree_mark_sick(bma->cur);
    1734           0 :                                 error = -EFSCORRUPTED;
    1735           0 :                                 goto done;
    1736             :                         }
    1737             :                 }
    1738             : 
    1739     2220301 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1740       12554 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1741             :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1742       12554 :                         rval |= tmp_rval;
    1743       12554 :                         if (error)
    1744           0 :                                 goto done;
    1745             :                 }
    1746             : 
    1747     2220172 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1748     2220172 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1749             :                         startblockval(PREV.br_startblock) -
    1750             :                         (bma->cur ? bma->cur->bc_ino.allocated : 0));
    1751             : 
    1752     2220096 :                 PREV.br_startoff = new_endoff;
    1753     2220096 :                 PREV.br_blockcount = temp;
    1754     2220096 :                 PREV.br_startblock = nullstartblock(da_new);
    1755     2220018 :                 xfs_iext_next(ifp, &bma->icur);
    1756     2220097 :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1757     2220281 :                 xfs_iext_prev(ifp, &bma->icur);
    1758     2220281 :                 break;
    1759             : 
    1760           0 :         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1761             :                 /*
    1762             :                  * Filling in the last part of a previous delayed allocation.
    1763             :                  * The right neighbor is contiguous with the new allocation.
    1764             :                  */
    1765           0 :                 old = RIGHT;
    1766           0 :                 RIGHT.br_startoff = new->br_startoff;
    1767           0 :                 RIGHT.br_startblock = new->br_startblock;
    1768           0 :                 RIGHT.br_blockcount += new->br_blockcount;
    1769             : 
    1770           0 :                 if (bma->cur == NULL)
    1771             :                         rval = XFS_ILOG_DEXT;
    1772             :                 else {
    1773           0 :                         rval = 0;
    1774           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1775           0 :                         if (error)
    1776           0 :                                 goto done;
    1777           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1778           0 :                                 xfs_btree_mark_sick(bma->cur);
    1779           0 :                                 error = -EFSCORRUPTED;
    1780           0 :                                 goto done;
    1781             :                         }
    1782           0 :                         error = xfs_bmbt_update(bma->cur, &RIGHT);
    1783           0 :                         if (error)
    1784           0 :                                 goto done;
    1785             :                 }
    1786             : 
    1787           0 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1788           0 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1789             :                         startblockval(PREV.br_startblock));
    1790             : 
    1791           0 :                 PREV.br_blockcount = temp;
    1792           0 :                 PREV.br_startblock = nullstartblock(da_new);
    1793             : 
    1794           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1795           0 :                 xfs_iext_next(ifp, &bma->icur);
    1796           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
    1797           0 :                 break;
    1798             : 
    1799           0 :         case BMAP_RIGHT_FILLING:
    1800             :                 /*
    1801             :                  * Filling in the last part of a previous delayed allocation.
    1802             :                  * The right neighbor is not contiguous.
    1803             :                  */
    1804           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1805           0 :                 ifp->if_nextents++;
    1806             : 
    1807           0 :                 if (bma->cur == NULL)
    1808             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1809             :                 else {
    1810           0 :                         rval = XFS_ILOG_CORE;
    1811           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1812           0 :                         if (error)
    1813           0 :                                 goto done;
    1814           0 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1815           0 :                                 xfs_btree_mark_sick(bma->cur);
    1816           0 :                                 error = -EFSCORRUPTED;
    1817           0 :                                 goto done;
    1818             :                         }
    1819           0 :                         error = xfs_btree_insert(bma->cur, &i);
    1820           0 :                         if (error)
    1821           0 :                                 goto done;
    1822           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1823           0 :                                 xfs_btree_mark_sick(bma->cur);
    1824           0 :                                 error = -EFSCORRUPTED;
    1825           0 :                                 goto done;
    1826             :                         }
    1827             :                 }
    1828             : 
    1829           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1830           0 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1831             :                                 &bma->cur, 1, &tmp_rval, whichfork);
    1832           0 :                         rval |= tmp_rval;
    1833           0 :                         if (error)
    1834           0 :                                 goto done;
    1835             :                 }
    1836             : 
    1837           0 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1838           0 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1839             :                         startblockval(PREV.br_startblock) -
    1840             :                         (bma->cur ? bma->cur->bc_ino.allocated : 0));
    1841             : 
    1842           0 :                 PREV.br_startblock = nullstartblock(da_new);
    1843           0 :                 PREV.br_blockcount = temp;
    1844           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1845           0 :                 xfs_iext_next(ifp, &bma->icur);
    1846           0 :                 break;
    1847             : 
    1848           0 :         case 0:
    1849             :                 /*
    1850             :                  * Filling in the middle part of a previous delayed allocation.
    1851             :                  * Contiguity is impossible here.
    1852             :                  * This case is avoided almost all the time.
    1853             :                  *
    1854             :                  * We start with a delayed allocation:
    1855             :                  *
    1856             :                  * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
    1857             :                  *  PREV @ idx
    1858             :                  *
    1859             :                  * and we are allocating:
    1860             :                  *                     +rrrrrrrrrrrrrrrrr+
    1861             :                  *                            new
    1862             :                  *
    1863             :                  * and we set it up for insertion as:
    1864             :                  * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
    1865             :                  *                            new
    1866             :                  *  PREV @ idx          LEFT              RIGHT
    1867             :                  *                      inserted at idx + 1
    1868             :                  */
    1869           0 :                 old = PREV;
    1870             : 
    1871             :                 /* LEFT is the new middle */
    1872           0 :                 LEFT = *new;
    1873             : 
    1874             :                 /* RIGHT is the new right */
    1875           0 :                 RIGHT.br_state = PREV.br_state;
    1876           0 :                 RIGHT.br_startoff = new_endoff;
    1877           0 :                 RIGHT.br_blockcount =
    1878           0 :                         PREV.br_startoff + PREV.br_blockcount - new_endoff;
    1879           0 :                 RIGHT.br_startblock =
    1880           0 :                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
    1881             :                                         RIGHT.br_blockcount));
    1882             : 
    1883             :                 /* truncate PREV */
    1884           0 :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    1885           0 :                 PREV.br_startblock =
    1886           0 :                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
    1887             :                                         PREV.br_blockcount));
    1888           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1889             : 
    1890           0 :                 xfs_iext_next(ifp, &bma->icur);
    1891           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
    1892           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
    1893           0 :                 ifp->if_nextents++;
    1894             : 
    1895           0 :                 if (bma->cur == NULL)
    1896             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1897             :                 else {
    1898           0 :                         rval = XFS_ILOG_CORE;
    1899           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1900           0 :                         if (error)
    1901           0 :                                 goto done;
    1902           0 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1903           0 :                                 xfs_btree_mark_sick(bma->cur);
    1904           0 :                                 error = -EFSCORRUPTED;
    1905           0 :                                 goto done;
    1906             :                         }
    1907           0 :                         error = xfs_btree_insert(bma->cur, &i);
    1908           0 :                         if (error)
    1909           0 :                                 goto done;
    1910           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1911           0 :                                 xfs_btree_mark_sick(bma->cur);
    1912           0 :                                 error = -EFSCORRUPTED;
    1913           0 :                                 goto done;
    1914             :                         }
    1915             :                 }
    1916             : 
    1917           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1918           0 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1919             :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1920           0 :                         rval |= tmp_rval;
    1921           0 :                         if (error)
    1922           0 :                                 goto done;
    1923             :                 }
    1924             : 
    1925           0 :                 da_new = startblockval(PREV.br_startblock) +
    1926           0 :                          startblockval(RIGHT.br_startblock);
    1927           0 :                 break;
    1928             : 
    1929           0 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1930             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1931             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
    1932             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    1933             :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1934             :         case BMAP_LEFT_CONTIG:
    1935             :         case BMAP_RIGHT_CONTIG:
    1936             :                 /*
    1937             :                  * These cases are all impossible.
    1938             :                  */
    1939           0 :                 ASSERT(0);
    1940             :         }
    1941             : 
    1942             :         /* add reverse mapping unless caller opted out */
    1943    27680365 :         if (!(bma->flags & XFS_BMAPI_NORMAP))
    1944    27680411 :                 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
    1945             : 
    1946             :         /* convert to a btree if necessary */
    1947    27680273 :         if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1948      473806 :                 int     tmp_logflags;   /* partial log flag return val */
    1949             : 
    1950      473806 :                 ASSERT(bma->cur == NULL);
    1951      473806 :                 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1952             :                                 &bma->cur, da_old > 0, &tmp_logflags,
    1953             :                                 whichfork);
    1954      473806 :                 bma->logflags |= tmp_logflags;
    1955      473806 :                 if (error)
    1956          35 :                         goto done;
    1957             :         }
    1958             : 
    1959    27680118 :         if (da_new != da_old)
    1960    25405624 :                 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
    1961             : 
    1962    27680202 :         if (bma->cur) {
    1963     8968600 :                 da_new += bma->cur->bc_ino.allocated;
    1964     8968600 :                 bma->cur->bc_ino.allocated = 0;
    1965             :         }
    1966             : 
    1967             :         /* adjust for changes in reserved delayed indirect blocks */
    1968    27680202 :         if (da_new != da_old) {
    1969    25390277 :                 ASSERT(state == 0 || da_new < da_old);
    1970    25390277 :                 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
    1971             :                                 false);
    1972             :         }
    1973             : 
    1974    27680283 :         xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
    1975    27680631 : done:
    1976    27680631 :         if (whichfork != XFS_COW_FORK)
    1977    24290221 :                 bma->logflags |= rval;
    1978    27680631 :         return error;
    1979             : #undef  LEFT
    1980             : #undef  RIGHT
    1981             : #undef  PREV
    1982             : }
    1983             : 
    1984             : /*
    1985             :  * Convert an unwritten allocation to a real allocation or vice versa.
    1986             :  */
    1987             : int                                     /* error */
    1988    67529383 : xfs_bmap_add_extent_unwritten_real(
    1989             :         struct xfs_trans        *tp,
    1990             :         xfs_inode_t             *ip,    /* incore inode pointer */
    1991             :         int                     whichfork,
    1992             :         struct xfs_iext_cursor  *icur,
    1993             :         struct xfs_btree_cur    **curp, /* if *curp is null, not a btree */
    1994             :         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
    1995             :         int                     *logflagsp) /* inode logging flags */
    1996             : {
    1997    67529383 :         struct xfs_btree_cur    *cur;   /* btree cursor */
    1998    67529383 :         int                     error;  /* error return value */
    1999    67529383 :         int                     i;      /* temp state */
    2000    67529383 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    2001    67529383 :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    2002    67529383 :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    2003             :                                         /* left is 0, right is 1, prev is 2 */
    2004    67529383 :         int                     rval=0; /* return value (logging flags) */
    2005    67529383 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2006    67529383 :         struct xfs_mount        *mp = ip->i_mount;
    2007    67529383 :         struct xfs_bmbt_irec    old;
    2008             : 
    2009    67529383 :         *logflagsp = 0;
    2010             : 
    2011    67529383 :         cur = *curp;
    2012    67529383 :         ifp = xfs_ifork_ptr(ip, whichfork);
    2013             : 
    2014    67526374 :         ASSERT(!isnullstartblock(new->br_startblock));
    2015             : 
    2016    67526374 :         XFS_STATS_INC(mp, xs_add_exlist);
    2017             : 
    2018             : #define LEFT            r[0]
    2019             : #define RIGHT           r[1]
    2020             : #define PREV            r[2]
    2021             : 
    2022             :         /*
    2023             :          * Set up a bunch of variables to make the tests simpler.
    2024             :          */
    2025    67526782 :         error = 0;
    2026    67526782 :         xfs_iext_get_extent(ifp, icur, &PREV);
    2027    67528030 :         ASSERT(new->br_state != PREV.br_state);
    2028    67528030 :         new_endoff = new->br_startoff + new->br_blockcount;
    2029    67528030 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    2030    67528030 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    2031             : 
    2032             :         /*
    2033             :          * Set flags determining what part of the previous oldext allocation
    2034             :          * extent is being replaced by a newext allocation.
    2035             :          */
    2036    67528030 :         if (PREV.br_startoff == new->br_startoff)
    2037    55663850 :                 state |= BMAP_LEFT_FILLING;
    2038    67528030 :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    2039    50552585 :                 state |= BMAP_RIGHT_FILLING;
    2040             : 
    2041             :         /*
    2042             :          * Check and set flags if this segment has a left neighbor.
    2043             :          * Don't set contiguous if the combined extent would be too large.
    2044             :          */
    2045    67528030 :         if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
    2046    53713948 :                 state |= BMAP_LEFT_VALID;
    2047    53713948 :                 if (isnullstartblock(LEFT.br_startblock))
    2048      369394 :                         state |= BMAP_LEFT_DELAY;
    2049             :         }
    2050             : 
    2051    67528988 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2052    53344622 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    2053    14993018 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    2054     4497092 :             LEFT.br_state == new->br_state &&
    2055     4484318 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2056     4484317 :                 state |= BMAP_LEFT_CONTIG;
    2057             : 
    2058             :         /*
    2059             :          * Check and set flags if this segment has a right neighbor.
    2060             :          * Don't set contiguous if the combined extent would be too large.
    2061             :          * Also check for all-three-contiguous being too large.
    2062             :          */
    2063    67528988 :         if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
    2064    41900490 :                 state |= BMAP_RIGHT_VALID;
    2065    41900490 :                 if (isnullstartblock(RIGHT.br_startblock))
    2066      572299 :                         state |= BMAP_RIGHT_DELAY;
    2067             :         }
    2068             : 
    2069    67529235 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2070    41328282 :             new_endoff == RIGHT.br_startoff &&
    2071    13248802 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    2072     2622508 :             new->br_state == RIGHT.br_state &&
    2073     2622438 :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2074     2622438 :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2075             :                        BMAP_RIGHT_FILLING)) !=
    2076             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2077      662732 :                        BMAP_RIGHT_FILLING) ||
    2078      662732 :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    2079             :                         <= XFS_MAX_BMBT_EXTLEN))
    2080     2622436 :                 state |= BMAP_RIGHT_CONTIG;
    2081             : 
    2082             :         /*
    2083             :          * Switch out based on the FILLING and CONTIG state bits.
    2084             :          */
    2085    67529235 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2086             :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    2087      662730 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2088             :              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2089             :                 /*
    2090             :                  * Setting all of a previous oldext extent to newext.
    2091             :                  * The left and right neighbors are both contiguous with new.
    2092             :                  */
    2093      662730 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    2094             : 
    2095      662730 :                 xfs_iext_remove(ip, icur, state);
    2096      662730 :                 xfs_iext_remove(ip, icur, state);
    2097      662730 :                 xfs_iext_prev(ifp, icur);
    2098      662730 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2099      662730 :                 ifp->if_nextents -= 2;
    2100      662730 :                 if (cur == NULL)
    2101             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2102             :                 else {
    2103      418699 :                         rval = XFS_ILOG_CORE;
    2104      418699 :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2105      418699 :                         if (error)
    2106           0 :                                 goto done;
    2107      418699 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2108           0 :                                 xfs_btree_mark_sick(cur);
    2109           0 :                                 error = -EFSCORRUPTED;
    2110           0 :                                 goto done;
    2111             :                         }
    2112      418699 :                         if ((error = xfs_btree_delete(cur, &i)))
    2113           0 :                                 goto done;
    2114      418699 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2115           0 :                                 xfs_btree_mark_sick(cur);
    2116           0 :                                 error = -EFSCORRUPTED;
    2117           0 :                                 goto done;
    2118             :                         }
    2119      418699 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2120           0 :                                 goto done;
    2121      418699 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2122           0 :                                 xfs_btree_mark_sick(cur);
    2123           0 :                                 error = -EFSCORRUPTED;
    2124           0 :                                 goto done;
    2125             :                         }
    2126      418699 :                         if ((error = xfs_btree_delete(cur, &i)))
    2127           0 :                                 goto done;
    2128      418699 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2129           0 :                                 xfs_btree_mark_sick(cur);
    2130           0 :                                 error = -EFSCORRUPTED;
    2131           0 :                                 goto done;
    2132             :                         }
    2133      418699 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2134           0 :                                 goto done;
    2135      418699 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2136           0 :                                 xfs_btree_mark_sick(cur);
    2137           0 :                                 error = -EFSCORRUPTED;
    2138           0 :                                 goto done;
    2139             :                         }
    2140      418699 :                         error = xfs_bmbt_update(cur, &LEFT);
    2141      418699 :                         if (error)
    2142           0 :                                 goto done;
    2143             :                 }
    2144             :                 break;
    2145             : 
    2146     2535431 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    2147             :                 /*
    2148             :                  * Setting all of a previous oldext extent to newext.
    2149             :                  * The left neighbor is contiguous, the right is not.
    2150             :                  */
    2151     2535431 :                 LEFT.br_blockcount += PREV.br_blockcount;
    2152             : 
    2153     2535431 :                 xfs_iext_remove(ip, icur, state);
    2154     2535430 :                 xfs_iext_prev(ifp, icur);
    2155     2535430 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2156     2535431 :                 ifp->if_nextents--;
    2157     2535431 :                 if (cur == NULL)
    2158             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2159             :                 else {
    2160     1119874 :                         rval = XFS_ILOG_CORE;
    2161     1119874 :                         error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
    2162     1119874 :                         if (error)
    2163           0 :                                 goto done;
    2164     1119874 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2165           0 :                                 xfs_btree_mark_sick(cur);
    2166           0 :                                 error = -EFSCORRUPTED;
    2167           0 :                                 goto done;
    2168             :                         }
    2169     1119874 :                         if ((error = xfs_btree_delete(cur, &i)))
    2170           0 :                                 goto done;
    2171     1119874 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2172           0 :                                 xfs_btree_mark_sick(cur);
    2173           0 :                                 error = -EFSCORRUPTED;
    2174           0 :                                 goto done;
    2175             :                         }
    2176     1119874 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2177           0 :                                 goto done;
    2178     1119874 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2179           0 :                                 xfs_btree_mark_sick(cur);
    2180           0 :                                 error = -EFSCORRUPTED;
    2181           0 :                                 goto done;
    2182             :                         }
    2183     1119874 :                         error = xfs_bmbt_update(cur, &LEFT);
    2184     1119874 :                         if (error)
    2185           0 :                                 goto done;
    2186             :                 }
    2187             :                 break;
    2188             : 
    2189      894113 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2190             :                 /*
    2191             :                  * Setting all of a previous oldext extent to newext.
    2192             :                  * The right neighbor is contiguous, the left is not.
    2193             :                  */
    2194      894113 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    2195      894113 :                 PREV.br_state = new->br_state;
    2196             : 
    2197      894113 :                 xfs_iext_next(ifp, icur);
    2198      894113 :                 xfs_iext_remove(ip, icur, state);
    2199      894113 :                 xfs_iext_prev(ifp, icur);
    2200      894113 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2201      894113 :                 ifp->if_nextents--;
    2202             : 
    2203      894113 :                 if (cur == NULL)
    2204             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2205             :                 else {
    2206      287950 :                         rval = XFS_ILOG_CORE;
    2207      287950 :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2208      287950 :                         if (error)
    2209           0 :                                 goto done;
    2210      287950 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2211           0 :                                 xfs_btree_mark_sick(cur);
    2212           0 :                                 error = -EFSCORRUPTED;
    2213           0 :                                 goto done;
    2214             :                         }
    2215      287950 :                         if ((error = xfs_btree_delete(cur, &i)))
    2216           0 :                                 goto done;
    2217      287950 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2218           0 :                                 xfs_btree_mark_sick(cur);
    2219           0 :                                 error = -EFSCORRUPTED;
    2220           0 :                                 goto done;
    2221             :                         }
    2222      287950 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2223           0 :                                 goto done;
    2224      287950 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2225           0 :                                 xfs_btree_mark_sick(cur);
    2226           0 :                                 error = -EFSCORRUPTED;
    2227           0 :                                 goto done;
    2228             :                         }
    2229      287950 :                         error = xfs_bmbt_update(cur, &PREV);
    2230      287950 :                         if (error)
    2231           0 :                                 goto done;
    2232             :                 }
    2233             :                 break;
    2234             : 
    2235    42395423 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    2236             :                 /*
    2237             :                  * Setting all of a previous oldext extent to newext.
    2238             :                  * Neither the left nor right neighbors are contiguous with
    2239             :                  * the new one.
    2240             :                  */
    2241    42395423 :                 PREV.br_state = new->br_state;
    2242    42395423 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2243             : 
    2244    42394481 :                 if (cur == NULL)
    2245             :                         rval = XFS_ILOG_DEXT;
    2246             :                 else {
    2247    18762759 :                         rval = 0;
    2248    18762759 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2249    18762933 :                         if (error)
    2250           1 :                                 goto done;
    2251    18762932 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2252           0 :                                 xfs_btree_mark_sick(cur);
    2253           0 :                                 error = -EFSCORRUPTED;
    2254           0 :                                 goto done;
    2255             :                         }
    2256    18762932 :                         error = xfs_bmbt_update(cur, &PREV);
    2257    18762539 :                         if (error)
    2258           0 :                                 goto done;
    2259             :                 }
    2260             :                 break;
    2261             : 
    2262     1286155 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
    2263             :                 /*
    2264             :                  * Setting the first part of a previous oldext extent to newext.
    2265             :                  * The left neighbor is contiguous.
    2266             :                  */
    2267     1286155 :                 LEFT.br_blockcount += new->br_blockcount;
    2268             : 
    2269     1286155 :                 old = PREV;
    2270     1286155 :                 PREV.br_startoff += new->br_blockcount;
    2271     1286155 :                 PREV.br_startblock += new->br_blockcount;
    2272     1286155 :                 PREV.br_blockcount -= new->br_blockcount;
    2273             : 
    2274     1286155 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2275     1286155 :                 xfs_iext_prev(ifp, icur);
    2276     1286155 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2277             : 
    2278     1286154 :                 if (cur == NULL)
    2279             :                         rval = XFS_ILOG_DEXT;
    2280             :                 else {
    2281      501235 :                         rval = 0;
    2282      501235 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2283      501235 :                         if (error)
    2284           0 :                                 goto done;
    2285      501235 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2286           0 :                                 xfs_btree_mark_sick(cur);
    2287           0 :                                 error = -EFSCORRUPTED;
    2288           0 :                                 goto done;
    2289             :                         }
    2290      501235 :                         error = xfs_bmbt_update(cur, &PREV);
    2291      501235 :                         if (error)
    2292           0 :                                 goto done;
    2293      501235 :                         error = xfs_btree_decrement(cur, 0, &i);
    2294      501235 :                         if (error)
    2295           0 :                                 goto done;
    2296      501235 :                         error = xfs_bmbt_update(cur, &LEFT);
    2297      501235 :                         if (error)
    2298           0 :                                 goto done;
    2299             :                 }
    2300             :                 break;
    2301             : 
    2302     7889812 :         case BMAP_LEFT_FILLING:
    2303             :                 /*
    2304             :                  * Setting the first part of a previous oldext extent to newext.
    2305             :                  * The left neighbor is not contiguous.
    2306             :                  */
    2307     7889812 :                 old = PREV;
    2308     7889812 :                 PREV.br_startoff += new->br_blockcount;
    2309     7889812 :                 PREV.br_startblock += new->br_blockcount;
    2310     7889812 :                 PREV.br_blockcount -= new->br_blockcount;
    2311             : 
    2312     7889812 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2313     7889814 :                 xfs_iext_insert(ip, icur, new, state);
    2314     7889806 :                 ifp->if_nextents++;
    2315             : 
    2316     7889806 :                 if (cur == NULL)
    2317             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2318             :                 else {
    2319     1343422 :                         rval = XFS_ILOG_CORE;
    2320     1343422 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2321     1343422 :                         if (error)
    2322           0 :                                 goto done;
    2323     1343422 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2324           0 :                                 xfs_btree_mark_sick(cur);
    2325           0 :                                 error = -EFSCORRUPTED;
    2326           0 :                                 goto done;
    2327             :                         }
    2328     1343422 :                         error = xfs_bmbt_update(cur, &PREV);
    2329     1343422 :                         if (error)
    2330           0 :                                 goto done;
    2331     1343422 :                         cur->bc_rec.b = *new;
    2332     1343422 :                         if ((error = xfs_btree_insert(cur, &i)))
    2333           0 :                                 goto done;
    2334     1343421 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2335           0 :                                 xfs_btree_mark_sick(cur);
    2336           0 :                                 error = -EFSCORRUPTED;
    2337           0 :                                 goto done;
    2338             :                         }
    2339             :                 }
    2340             :                 break;
    2341             : 
    2342     1065594 :         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2343             :                 /*
    2344             :                  * Setting the last part of a previous oldext extent to newext.
    2345             :                  * The right neighbor is contiguous with the new allocation.
    2346             :                  */
    2347     1065594 :                 old = PREV;
    2348     1065594 :                 PREV.br_blockcount -= new->br_blockcount;
    2349             : 
    2350     1065594 :                 RIGHT.br_startoff = new->br_startoff;
    2351     1065594 :                 RIGHT.br_startblock = new->br_startblock;
    2352     1065594 :                 RIGHT.br_blockcount += new->br_blockcount;
    2353             : 
    2354     1065594 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2355     1065594 :                 xfs_iext_next(ifp, icur);
    2356     1065594 :                 xfs_iext_update_extent(ip, state, icur, &RIGHT);
    2357             : 
    2358     1065593 :                 if (cur == NULL)
    2359             :                         rval = XFS_ILOG_DEXT;
    2360             :                 else {
    2361      530922 :                         rval = 0;
    2362      530922 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2363      530922 :                         if (error)
    2364           1 :                                 goto done;
    2365      530921 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2366           0 :                                 xfs_btree_mark_sick(cur);
    2367           0 :                                 error = -EFSCORRUPTED;
    2368           0 :                                 goto done;
    2369             :                         }
    2370      530921 :                         error = xfs_bmbt_update(cur, &PREV);
    2371      530921 :                         if (error)
    2372           0 :                                 goto done;
    2373      530921 :                         error = xfs_btree_increment(cur, 0, &i);
    2374      530921 :                         if (error)
    2375           0 :                                 goto done;
    2376      530921 :                         error = xfs_bmbt_update(cur, &RIGHT);
    2377      530921 :                         if (error)
    2378           0 :                                 goto done;
    2379             :                 }
    2380             :                 break;
    2381             : 
    2382     2999411 :         case BMAP_RIGHT_FILLING:
    2383             :                 /*
    2384             :                  * Setting the last part of a previous oldext extent to newext.
    2385             :                  * The right neighbor is not contiguous.
    2386             :                  */
    2387     2999411 :                 old = PREV;
    2388     2999411 :                 PREV.br_blockcount -= new->br_blockcount;
    2389             : 
    2390     2999411 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2391     2999415 :                 xfs_iext_next(ifp, icur);
    2392     2999406 :                 xfs_iext_insert(ip, icur, new, state);
    2393     2999414 :                 ifp->if_nextents++;
    2394             : 
    2395     2999414 :                 if (cur == NULL)
    2396             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2397             :                 else {
    2398      705877 :                         rval = XFS_ILOG_CORE;
    2399      705877 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2400      705876 :                         if (error)
    2401           0 :                                 goto done;
    2402      705876 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2403           0 :                                 xfs_btree_mark_sick(cur);
    2404           0 :                                 error = -EFSCORRUPTED;
    2405           0 :                                 goto done;
    2406             :                         }
    2407      705876 :                         error = xfs_bmbt_update(cur, &PREV);
    2408      705875 :                         if (error)
    2409           0 :                                 goto done;
    2410      705875 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2411      705877 :                         if (error)
    2412           0 :                                 goto done;
    2413      705877 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2414           0 :                                 xfs_btree_mark_sick(cur);
    2415           0 :                                 error = -EFSCORRUPTED;
    2416           0 :                                 goto done;
    2417             :                         }
    2418      705877 :                         if ((error = xfs_btree_insert(cur, &i)))
    2419           0 :                                 goto done;
    2420      705875 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2421           0 :                                 xfs_btree_mark_sick(cur);
    2422           0 :                                 error = -EFSCORRUPTED;
    2423           0 :                                 goto done;
    2424             :                         }
    2425             :                 }
    2426             :                 break;
    2427             : 
    2428     7800566 :         case 0:
    2429             :                 /*
    2430             :                  * Setting the middle part of a previous oldext extent to
    2431             :                  * newext.  Contiguity is impossible here.
    2432             :                  * One extent becomes three extents.
    2433             :                  */
    2434     7800566 :                 old = PREV;
    2435     7800566 :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    2436             : 
    2437     7800566 :                 r[0] = *new;
    2438     7800566 :                 r[1].br_startoff = new_endoff;
    2439     7800566 :                 r[1].br_blockcount =
    2440     7800566 :                         old.br_startoff + old.br_blockcount - new_endoff;
    2441     7800566 :                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
    2442     7800566 :                 r[1].br_state = PREV.br_state;
    2443             : 
    2444     7800566 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2445     7800564 :                 xfs_iext_next(ifp, icur);
    2446     7800536 :                 xfs_iext_insert(ip, icur, &r[1], state);
    2447     7800596 :                 xfs_iext_insert(ip, icur, &r[0], state);
    2448     7800644 :                 ifp->if_nextents += 2;
    2449             : 
    2450     7800644 :                 if (cur == NULL)
    2451             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2452             :                 else {
    2453     3069904 :                         rval = XFS_ILOG_CORE;
    2454     3069904 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2455     3069894 :                         if (error)
    2456           0 :                                 goto done;
    2457     3069894 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2458           0 :                                 xfs_btree_mark_sick(cur);
    2459           0 :                                 error = -EFSCORRUPTED;
    2460           0 :                                 goto done;
    2461             :                         }
    2462             :                         /* new right extent - oldext */
    2463     3069894 :                         error = xfs_bmbt_update(cur, &r[1]);
    2464     3069834 :                         if (error)
    2465           0 :                                 goto done;
    2466             :                         /* new left extent - oldext */
    2467     3069834 :                         cur->bc_rec.b = PREV;
    2468     3069834 :                         if ((error = xfs_btree_insert(cur, &i)))
    2469           0 :                                 goto done;
    2470     3069870 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2471           0 :                                 xfs_btree_mark_sick(cur);
    2472           0 :                                 error = -EFSCORRUPTED;
    2473           0 :                                 goto done;
    2474             :                         }
    2475             :                         /*
    2476             :                          * Reset the cursor to the position of the new extent
    2477             :                          * we are about to insert as we can't trust it after
    2478             :                          * the previous insert.
    2479             :                          */
    2480     3069870 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2481     3069881 :                         if (error)
    2482           0 :                                 goto done;
    2483     3069881 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2484           0 :                                 xfs_btree_mark_sick(cur);
    2485           0 :                                 error = -EFSCORRUPTED;
    2486           0 :                                 goto done;
    2487             :                         }
    2488             :                         /* new middle extent - newext */
    2489     3069881 :                         if ((error = xfs_btree_insert(cur, &i)))
    2490           0 :                                 goto done;
    2491     3069902 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2492           0 :                                 xfs_btree_mark_sick(cur);
    2493           0 :                                 error = -EFSCORRUPTED;
    2494           0 :                                 goto done;
    2495             :                         }
    2496             :                 }
    2497             :                 break;
    2498             : 
    2499           0 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2500             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2501             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
    2502             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    2503             :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2504             :         case BMAP_LEFT_CONTIG:
    2505             :         case BMAP_RIGHT_CONTIG:
    2506             :                 /*
    2507             :                  * These cases are all impossible.
    2508             :                  */
    2509           0 :                 ASSERT(0);
    2510             :         }
    2511             : 
    2512             :         /* update reverse mappings */
    2513    67528140 :         xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
    2514             : 
    2515             :         /* convert to a btree if necessary */
    2516    67527869 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2517      410685 :                 int     tmp_logflags;   /* partial log flag return val */
    2518             : 
    2519      410685 :                 ASSERT(cur == NULL);
    2520      410685 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    2521             :                                 &tmp_logflags, whichfork);
    2522      410685 :                 *logflagsp |= tmp_logflags;
    2523      410685 :                 if (error)
    2524           7 :                         goto done;
    2525             :         }
    2526             : 
    2527             :         /* clear out the allocated field, done with it now in any case. */
    2528    67526824 :         if (cur) {
    2529    27150944 :                 cur->bc_ino.allocated = 0;
    2530    27150944 :                 *curp = cur;
    2531             :         }
    2532             : 
    2533    67526824 :         xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
    2534    67528506 : done:
    2535    67528506 :         *logflagsp |= rval;
    2536    67528506 :         return error;
    2537             : #undef  LEFT
    2538             : #undef  RIGHT
    2539             : #undef  PREV
    2540             : }
    2541             : 
    2542             : /*
    2543             :  * Convert a hole to a delayed allocation.
    2544             :  */
    2545             : STATIC void
    2546    52633289 : xfs_bmap_add_extent_hole_delay(
    2547             :         xfs_inode_t             *ip,    /* incore inode pointer */
    2548             :         int                     whichfork,
    2549             :         struct xfs_iext_cursor  *icur,
    2550             :         xfs_bmbt_irec_t         *new)   /* new data to add to file extents */
    2551             : {
    2552    52633289 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    2553    52633289 :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2554    52633289 :         xfs_filblks_t           newlen=0;       /* new indirect size */
    2555    52633289 :         xfs_filblks_t           oldlen=0;       /* old indirect size */
    2556    52633289 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2557    52633289 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2558    52633289 :         xfs_filblks_t           temp;    /* temp for indirect calculations */
    2559             : 
    2560    52633289 :         ifp = xfs_ifork_ptr(ip, whichfork);
    2561    52523624 :         ASSERT(isnullstartblock(new->br_startblock));
    2562             : 
    2563             :         /*
    2564             :          * Check and set flags if this segment has a left neighbor
    2565             :          */
    2566    52523624 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2567    39654977 :                 state |= BMAP_LEFT_VALID;
    2568    39654977 :                 if (isnullstartblock(left.br_startblock))
    2569    21842023 :                         state |= BMAP_LEFT_DELAY;
    2570             :         }
    2571             : 
    2572             :         /*
    2573             :          * Check and set flags if the current (right) segment exists.
    2574             :          * If it doesn't exist, we're converting the hole at end-of-file.
    2575             :          */
    2576    52549556 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2577    26439171 :                 state |= BMAP_RIGHT_VALID;
    2578    26439171 :                 if (isnullstartblock(right.br_startblock))
    2579     7843556 :                         state |= BMAP_RIGHT_DELAY;
    2580             :         }
    2581             : 
    2582             :         /*
    2583             :          * Set contiguity flags on the left and right neighbors.
    2584             :          * Don't let extents get too large, even if the pieces are contiguous.
    2585             :          */
    2586    52579636 :         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
    2587    21843925 :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2588    17904818 :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2589    17904516 :                 state |= BMAP_LEFT_CONTIG;
    2590             : 
    2591    52579636 :         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
    2592     7843557 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2593     3159007 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2594     3146119 :             (!(state & BMAP_LEFT_CONTIG) ||
    2595       63911 :              (left.br_blockcount + new->br_blockcount +
    2596             :               right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
    2597     3146119 :                 state |= BMAP_RIGHT_CONTIG;
    2598             : 
    2599             :         /*
    2600             :          * Switch out based on the contiguity flags.
    2601             :          */
    2602    52579636 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2603       63911 :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2604             :                 /*
    2605             :                  * New allocation is contiguous with delayed allocations
    2606             :                  * on the left and on the right.
    2607             :                  * Merge all three into a single extent record.
    2608             :                  */
    2609       63911 :                 temp = left.br_blockcount + new->br_blockcount +
    2610       63911 :                         right.br_blockcount;
    2611             : 
    2612       63911 :                 oldlen = startblockval(left.br_startblock) +
    2613       63911 :                         startblockval(new->br_startblock) +
    2614       63911 :                         startblockval(right.br_startblock);
    2615       63911 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2616             :                                          oldlen);
    2617       63912 :                 left.br_startblock = nullstartblock(newlen);
    2618       63911 :                 left.br_blockcount = temp;
    2619             : 
    2620       63911 :                 xfs_iext_remove(ip, icur, state);
    2621       63912 :                 xfs_iext_prev(ifp, icur);
    2622       63912 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2623       63912 :                 break;
    2624             : 
    2625    17838676 :         case BMAP_LEFT_CONTIG:
    2626             :                 /*
    2627             :                  * New allocation is contiguous with a delayed allocation
    2628             :                  * on the left.
    2629             :                  * Merge the new allocation with the left neighbor.
    2630             :                  */
    2631    17838676 :                 temp = left.br_blockcount + new->br_blockcount;
    2632             : 
    2633    17838676 :                 oldlen = startblockval(left.br_startblock) +
    2634    17838676 :                         startblockval(new->br_startblock);
    2635    17838676 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2636             :                                          oldlen);
    2637    17837189 :                 left.br_blockcount = temp;
    2638    17837189 :                 left.br_startblock = nullstartblock(newlen);
    2639             : 
    2640    17832441 :                 xfs_iext_prev(ifp, icur);
    2641    17835762 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2642    17835762 :                 break;
    2643             : 
    2644     3082208 :         case BMAP_RIGHT_CONTIG:
    2645             :                 /*
    2646             :                  * New allocation is contiguous with a delayed allocation
    2647             :                  * on the right.
    2648             :                  * Merge the new allocation with the right neighbor.
    2649             :                  */
    2650     3082208 :                 temp = new->br_blockcount + right.br_blockcount;
    2651     3082208 :                 oldlen = startblockval(new->br_startblock) +
    2652     3082208 :                         startblockval(right.br_startblock);
    2653     3082208 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2654             :                                          oldlen);
    2655     3082206 :                 right.br_startoff = new->br_startoff;
    2656     3082206 :                 right.br_startblock = nullstartblock(newlen);
    2657     3082206 :                 right.br_blockcount = temp;
    2658     3082206 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2659     3082206 :                 break;
    2660             : 
    2661    31594841 :         case 0:
    2662             :                 /*
    2663             :                  * New allocation is not contiguous with another
    2664             :                  * delayed allocation.
    2665             :                  * Insert a new entry.
    2666             :                  */
    2667    31594841 :                 oldlen = newlen = 0;
    2668    31594841 :                 xfs_iext_insert(ip, icur, new, state);
    2669    31594841 :                 break;
    2670             :         }
    2671    52549074 :         if (oldlen != newlen) {
    2672    20972464 :                 ASSERT(oldlen > newlen);
    2673    20972464 :                 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
    2674             :                                  false);
    2675             :                 /*
    2676             :                  * Nothing to do for disk quota accounting here.
    2677             :                  */
    2678    20986221 :                 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
    2679             :         }
    2680    52564184 : }
    2681             : 
    2682             : /*
    2683             :  * Convert a hole to a real allocation.
    2684             :  */
    2685             : STATIC int                              /* error */
    2686   205319100 : xfs_bmap_add_extent_hole_real(
    2687             :         struct xfs_trans        *tp,
    2688             :         struct xfs_inode        *ip,
    2689             :         int                     whichfork,
    2690             :         struct xfs_iext_cursor  *icur,
    2691             :         struct xfs_btree_cur    **curp,
    2692             :         struct xfs_bmbt_irec    *new,
    2693             :         int                     *logflagsp,
    2694             :         uint32_t                flags)
    2695             : {
    2696   205319100 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    2697   205293856 :         struct xfs_mount        *mp = ip->i_mount;
    2698   205293856 :         struct xfs_btree_cur    *cur = *curp;
    2699   205293856 :         int                     error;  /* error return value */
    2700   205293856 :         int                     i;      /* temp state */
    2701   205293856 :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2702   205293856 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2703   205293856 :         int                     rval=0; /* return value (logging flags) */
    2704   205293856 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2705   205293856 :         struct xfs_bmbt_irec    old;
    2706             : 
    2707   205293856 :         ASSERT(!isnullstartblock(new->br_startblock));
    2708   205293856 :         ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    2709             : 
    2710   205293856 :         XFS_STATS_INC(mp, xs_add_exlist);
    2711             : 
    2712             :         /*
    2713             :          * Check and set flags if this segment has a left neighbor.
    2714             :          */
    2715   205300522 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2716   161335840 :                 state |= BMAP_LEFT_VALID;
    2717   161335840 :                 if (isnullstartblock(left.br_startblock))
    2718      255162 :                         state |= BMAP_LEFT_DELAY;
    2719             :         }
    2720             : 
    2721             :         /*
    2722             :          * Check and set flags if this segment has a current value.
    2723             :          * Not true if we're inserting into the "hole" at eof.
    2724             :          */
    2725   205302671 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2726    49615299 :                 state |= BMAP_RIGHT_VALID;
    2727    49615299 :                 if (isnullstartblock(right.br_startblock))
    2728      491210 :                         state |= BMAP_RIGHT_DELAY;
    2729             :         }
    2730             : 
    2731             :         /*
    2732             :          * We're inserting a real allocation between "left" and "right".
    2733             :          * Set the contiguity flags.  Don't let extents get too large.
    2734             :          */
    2735   205309596 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2736   161080695 :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2737   116894403 :             left.br_startblock + left.br_blockcount == new->br_startblock &&
    2738    43039962 :             left.br_state == new->br_state &&
    2739    40974882 :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2740    40974695 :                 state |= BMAP_LEFT_CONTIG;
    2741             : 
    2742   205309596 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2743    49123753 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2744    19682543 :             new->br_startblock + new->br_blockcount == right.br_startblock &&
    2745     3768379 :             new->br_state == right.br_state &&
    2746     3376662 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2747     3376662 :             (!(state & BMAP_LEFT_CONTIG) ||
    2748     1324958 :              left.br_blockcount + new->br_blockcount +
    2749             :              right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
    2750     3376662 :                 state |= BMAP_RIGHT_CONTIG;
    2751             : 
    2752   205309596 :         error = 0;
    2753             :         /*
    2754             :          * Select which case we're in here, and implement it.
    2755             :          */
    2756   205309596 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2757     1324958 :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2758             :                 /*
    2759             :                  * New allocation is contiguous with real allocations on the
    2760             :                  * left and on the right.
    2761             :                  * Merge all three into a single extent record.
    2762             :                  */
    2763     1324958 :                 left.br_blockcount += new->br_blockcount + right.br_blockcount;
    2764             : 
    2765     1324958 :                 xfs_iext_remove(ip, icur, state);
    2766     1324958 :                 xfs_iext_prev(ifp, icur);
    2767     1324958 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2768     1324958 :                 ifp->if_nextents--;
    2769             : 
    2770     1324958 :                 if (cur == NULL) {
    2771      287830 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2772             :                 } else {
    2773     1037524 :                         rval = XFS_ILOG_CORE;
    2774     1037524 :                         error = xfs_bmbt_lookup_eq(cur, &right, &i);
    2775     1037524 :                         if (error)
    2776           0 :                                 goto done;
    2777     1037524 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2778           0 :                                 xfs_btree_mark_sick(cur);
    2779           0 :                                 error = -EFSCORRUPTED;
    2780           0 :                                 goto done;
    2781             :                         }
    2782     1037524 :                         error = xfs_btree_delete(cur, &i);
    2783     1037524 :                         if (error)
    2784           0 :                                 goto done;
    2785     1037524 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2786           0 :                                 xfs_btree_mark_sick(cur);
    2787           0 :                                 error = -EFSCORRUPTED;
    2788           0 :                                 goto done;
    2789             :                         }
    2790     1037524 :                         error = xfs_btree_decrement(cur, 0, &i);
    2791     1037524 :                         if (error)
    2792           0 :                                 goto done;
    2793     1037524 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2794           0 :                                 xfs_btree_mark_sick(cur);
    2795           0 :                                 error = -EFSCORRUPTED;
    2796           0 :                                 goto done;
    2797             :                         }
    2798     1037524 :                         error = xfs_bmbt_update(cur, &left);
    2799     1037524 :                         if (error)
    2800           0 :                                 goto done;
    2801             :                 }
    2802             :                 break;
    2803             : 
    2804    39649735 :         case BMAP_LEFT_CONTIG:
    2805             :                 /*
    2806             :                  * New allocation is contiguous with a real allocation
    2807             :                  * on the left.
    2808             :                  * Merge the new allocation with the left neighbor.
    2809             :                  */
    2810    39649735 :                 old = left;
    2811    39649735 :                 left.br_blockcount += new->br_blockcount;
    2812             : 
    2813    39649735 :                 xfs_iext_prev(ifp, icur);
    2814    39649734 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2815             : 
    2816    39649731 :                 if (cur == NULL) {
    2817    32240902 :                         rval = xfs_ilog_fext(whichfork);
    2818             :                 } else {
    2819     7408829 :                         rval = 0;
    2820     7408829 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2821     7408835 :                         if (error)
    2822           0 :                                 goto done;
    2823     7408835 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2824           0 :                                 xfs_btree_mark_sick(cur);
    2825           0 :                                 error = -EFSCORRUPTED;
    2826           0 :                                 goto done;
    2827             :                         }
    2828     7408835 :                         error = xfs_bmbt_update(cur, &left);
    2829     7408831 :                         if (error)
    2830           0 :                                 goto done;
    2831             :                 }
    2832             :                 break;
    2833             : 
    2834     2051704 :         case BMAP_RIGHT_CONTIG:
    2835             :                 /*
    2836             :                  * New allocation is contiguous with a real allocation
    2837             :                  * on the right.
    2838             :                  * Merge the new allocation with the right neighbor.
    2839             :                  */
    2840     2051704 :                 old = right;
    2841             : 
    2842     2051704 :                 right.br_startoff = new->br_startoff;
    2843     2051704 :                 right.br_startblock = new->br_startblock;
    2844     2051704 :                 right.br_blockcount += new->br_blockcount;
    2845     2051704 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2846             : 
    2847     2051704 :                 if (cur == NULL) {
    2848     1327197 :                         rval = xfs_ilog_fext(whichfork);
    2849             :                 } else {
    2850      724507 :                         rval = 0;
    2851      724507 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2852      724507 :                         if (error)
    2853           0 :                                 goto done;
    2854      724507 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2855           0 :                                 xfs_btree_mark_sick(cur);
    2856           0 :                                 error = -EFSCORRUPTED;
    2857           0 :                                 goto done;
    2858             :                         }
    2859      724507 :                         error = xfs_bmbt_update(cur, &right);
    2860      724507 :                         if (error)
    2861           0 :                                 goto done;
    2862             :                 }
    2863             :                 break;
    2864             : 
    2865   162283199 :         case 0:
    2866             :                 /*
    2867             :                  * New allocation is not contiguous with another
    2868             :                  * real allocation.
    2869             :                  * Insert a new entry.
    2870             :                  */
    2871   162283199 :                 xfs_iext_insert(ip, icur, new, state);
    2872   162259916 :                 ifp->if_nextents++;
    2873             : 
    2874   162259916 :                 if (cur == NULL) {
    2875    70523080 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2876             :                 } else {
    2877    96958390 :                         rval = XFS_ILOG_CORE;
    2878    96958390 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2879    96961604 :                         if (error)
    2880        1247 :                                 goto done;
    2881    96960357 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2882           0 :                                 xfs_btree_mark_sick(cur);
    2883           0 :                                 error = -EFSCORRUPTED;
    2884           0 :                                 goto done;
    2885             :                         }
    2886    96960357 :                         error = xfs_btree_insert(cur, &i);
    2887    96958674 :                         if (error)
    2888         179 :                                 goto done;
    2889    96958495 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2890           0 :                                 xfs_btree_mark_sick(cur);
    2891           0 :                                 error = -EFSCORRUPTED;
    2892           0 :                                 goto done;
    2893             :                         }
    2894             :                 }
    2895             :                 break;
    2896             :         }
    2897             : 
    2898             :         /* add reverse mapping unless caller opted out */
    2899   205286416 :         if (!(flags & XFS_BMAPI_NORMAP))
    2900   205288387 :                 xfs_rmap_map_extent(tp, ip, whichfork, new);
    2901             : 
    2902             :         /* convert to a btree if necessary */
    2903   205294897 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2904     1382215 :                 int     tmp_logflags;   /* partial log flag return val */
    2905             : 
    2906     1382215 :                 ASSERT(cur == NULL);
    2907     1382215 :                 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
    2908             :                                 &tmp_logflags, whichfork);
    2909     1382216 :                 *logflagsp |= tmp_logflags;
    2910     1382216 :                 cur = *curp;
    2911     1382216 :                 if (error)
    2912           9 :                         goto done;
    2913             :         }
    2914             : 
    2915             :         /* clear out the allocated field, done with it now in any case. */
    2916   205266760 :         if (cur)
    2917   107509221 :                 cur->bc_ino.allocated = 0;
    2918             : 
    2919   205266760 :         xfs_bmap_check_leaf_extents(cur, ip, whichfork);
    2920   205286091 : done:
    2921   205286091 :         *logflagsp |= rval;
    2922   205286091 :         return error;
    2923             : }
    2924             : 
    2925             : /*
    2926             :  * Functions used in the extent read, allocate and remove paths
    2927             :  */
    2928             : 
    2929             : /*
    2930             :  * Adjust the size of the new extent based on i_extsize and rt extsize.
    2931             :  */
    2932             : int
    2933    73860141 : xfs_bmap_extsize_align(
    2934             :         xfs_mount_t     *mp,
    2935             :         xfs_bmbt_irec_t *gotp,          /* next extent pointer */
    2936             :         xfs_bmbt_irec_t *prevp,         /* previous extent pointer */
    2937             :         xfs_extlen_t    extsz,          /* align to this extent size */
    2938             :         int             rt,             /* is this a realtime inode? */
    2939             :         int             eof,            /* is extent at end-of-file? */
    2940             :         int             delay,          /* creating delalloc extent? */
    2941             :         int             convert,        /* overwriting unwritten extent? */
    2942             :         xfs_fileoff_t   *offp,          /* in/out: aligned offset */
    2943             :         xfs_extlen_t    *lenp)          /* in/out: aligned length */
    2944             : {
    2945    73860141 :         xfs_fileoff_t   orig_off;       /* original offset */
    2946    73860141 :         xfs_extlen_t    orig_alen;      /* original length */
    2947    73860141 :         xfs_fileoff_t   orig_end;       /* original off+len */
    2948    73860141 :         xfs_fileoff_t   nexto;          /* next file offset */
    2949    73860141 :         xfs_fileoff_t   prevo;          /* previous file offset */
    2950    73860141 :         xfs_fileoff_t   align_off;      /* temp for offset */
    2951    73860141 :         xfs_extlen_t    align_alen;     /* temp for length */
    2952    73860141 :         xfs_extlen_t    temp;           /* temp for calculations */
    2953             : 
    2954    73860141 :         if (convert)
    2955             :                 return 0;
    2956             : 
    2957    73860141 :         orig_off = align_off = *offp;
    2958    73860141 :         orig_alen = align_alen = *lenp;
    2959    73860141 :         orig_end = orig_off + orig_alen;
    2960             : 
    2961             :         /*
    2962             :          * If this request overlaps an existing extent, then don't
    2963             :          * attempt to perform any additional alignment.
    2964             :          */
    2965    73860141 :         if (!delay && !eof &&
    2966    22592084 :             (orig_off >= gotp->br_startoff) &&
    2967     3390304 :             (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
    2968             :                 return 0;
    2969             :         }
    2970             : 
    2971             :         /*
    2972             :          * If the file offset is unaligned vs. the extent size
    2973             :          * we need to align it.  This will be possible unless
    2974             :          * the file was previously written with a kernel that didn't
    2975             :          * perform this alignment, or if a truncate shot us in the
    2976             :          * foot.
    2977             :          */
    2978    70469896 :         div_u64_rem(orig_off, extsz, &temp);
    2979    70424260 :         if (temp) {
    2980     5561112 :                 align_alen += temp;
    2981     5561112 :                 align_off -= temp;
    2982             :         }
    2983             : 
    2984             :         /* Same adjustment for the end of the requested area. */
    2985    70424260 :         temp = (align_alen % extsz);
    2986    70424260 :         if (temp)
    2987     6876013 :                 align_alen += extsz - temp;
    2988             : 
    2989             :         /*
    2990             :          * For large extent hint sizes, the aligned extent might be larger than
    2991             :          * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
    2992             :          * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
    2993             :          * allocation loops handle short allocation just fine, so it is safe to
    2994             :          * do this. We only want to do it when we are forced to, though, because
    2995             :          * it means more allocation operations are required.
    2996             :          */
    2997    70424566 :         while (align_alen > XFS_MAX_BMBT_EXTLEN)
    2998         306 :                 align_alen -= extsz;
    2999    70424260 :         ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
    3000             : 
    3001             :         /*
    3002             :          * If the previous block overlaps with this proposed allocation
    3003             :          * then move the start forward without adjusting the length.
    3004             :          */
    3005    70424260 :         if (prevp->br_startoff != NULLFILEOFF) {
    3006    58783776 :                 if (prevp->br_startblock == HOLESTARTBLOCK)
    3007             :                         prevo = prevp->br_startoff;
    3008             :                 else
    3009    58783776 :                         prevo = prevp->br_startoff + prevp->br_blockcount;
    3010             :         } else
    3011             :                 prevo = 0;
    3012    70424260 :         if (align_off != orig_off && align_off < prevo)
    3013      636189 :                 align_off = prevo;
    3014             :         /*
    3015             :          * If the next block overlaps with this proposed allocation
    3016             :          * then move the start back without adjusting the length,
    3017             :          * but not before offset 0.
    3018             :          * This may of course make the start overlap previous block,
    3019             :          * and if we hit the offset 0 limit then the next block
    3020             :          * can still overlap too.
    3021             :          */
    3022    70424260 :         if (!eof && gotp->br_startoff != NULLFILEOFF) {
    3023    20342285 :                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
    3024    19200818 :                     (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
    3025           0 :                         nexto = gotp->br_startoff + gotp->br_blockcount;
    3026             :                 else
    3027             :                         nexto = gotp->br_startoff;
    3028             :         } else
    3029             :                 nexto = NULLFILEOFF;
    3030    70424260 :         if (!eof &&
    3031    20342310 :             align_off + align_alen != orig_end &&
    3032             :             align_off + align_alen > nexto)
    3033      559795 :                 align_off = nexto > align_alen ? nexto - align_alen : 0;
    3034             :         /*
    3035             :          * If we're now overlapping the next or previous extent that
    3036             :          * means we can't fit an extsz piece in this hole.  Just move
    3037             :          * the start forward to the first valid spot and set
    3038             :          * the length so we hit the end.
    3039             :          */
    3040    70424260 :         if (align_off != orig_off && align_off < prevo)
    3041      501113 :                 align_off = prevo;
    3042    70424260 :         if (align_off + align_alen != orig_end &&
    3043             :             align_off + align_alen > nexto &&
    3044             :             nexto != NULLFILEOFF) {
    3045      523334 :                 ASSERT(nexto > prevo);
    3046      523334 :                 align_alen = nexto - align_off;
    3047             :         }
    3048             : 
    3049             :         /*
    3050             :          * If realtime, and the result isn't a multiple of the realtime
    3051             :          * extent size we need to remove blocks until it is.
    3052             :          */
    3053    70424260 :         if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
    3054             :                 /*
    3055             :                  * We're not covering the original request, or
    3056             :                  * we won't be able to once we fix the length.
    3057             :                  */
    3058           0 :                 if (orig_off < align_off ||
    3059           0 :                     orig_end > align_off + align_alen ||
    3060           0 :                     align_alen - temp < orig_alen)
    3061             :                         return -EINVAL;
    3062             :                 /*
    3063             :                  * Try to fix it by moving the start up.
    3064             :                  */
    3065           0 :                 if (align_off + temp <= orig_off) {
    3066             :                         align_alen -= temp;
    3067             :                         align_off += temp;
    3068             :                 }
    3069             :                 /*
    3070             :                  * Try to fix it by moving the end in.
    3071             :                  */
    3072           0 :                 else if (align_off + align_alen - temp >= orig_end)
    3073             :                         align_alen -= temp;
    3074             :                 /*
    3075             :                  * Set the start to the minimum then trim the length.
    3076             :                  */
    3077             :                 else {
    3078           0 :                         align_alen -= orig_off - align_off;
    3079           0 :                         align_off = orig_off;
    3080           0 :                         align_alen -= align_alen % mp->m_sb.sb_rextsize;
    3081             :                 }
    3082             :                 /*
    3083             :                  * Result doesn't cover the request, fail it.
    3084             :                  */
    3085           0 :                 if (orig_off < align_off || orig_end > align_off + align_alen)
    3086             :                         return -EINVAL;
    3087             :         } else {
    3088    70424260 :                 ASSERT(orig_off >= align_off);
    3089             :                 /* see XFS_BMBT_MAX_EXTLEN handling above */
    3090    70424260 :                 ASSERT(orig_end <= align_off + align_alen ||
    3091             :                        align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
    3092             :         }
    3093             : 
    3094             : #ifdef DEBUG
    3095    70424260 :         if (!eof && gotp->br_startoff != NULLFILEOFF)
    3096    20342613 :                 ASSERT(align_off + align_alen <= gotp->br_startoff);
    3097    70424260 :         if (prevp->br_startoff != NULLFILEOFF)
    3098    58797979 :                 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
    3099             : #endif
    3100             : 
    3101    70424260 :         *lenp = align_alen;
    3102    70424260 :         *offp = align_off;
    3103    70424260 :         return 0;
    3104             : }
    3105             : 
    3106             : #define XFS_ALLOC_GAP_UNITS     4
    3107             : 
    3108             : void
    3109   134941728 : xfs_bmap_adjacent(
    3110             :         struct xfs_bmalloca     *ap)    /* bmap alloc argument struct */
    3111             : {
    3112   134941728 :         xfs_fsblock_t   adjust;         /* adjustment to block numbers */
    3113   134941728 :         xfs_mount_t     *mp;            /* mount point structure */
    3114   134941728 :         int             rt;             /* true if inode is realtime */
    3115             : 
    3116             : #define ISVALID(x,y)    \
    3117             :         (rt ? \
    3118             :                 (x) < mp->m_sb.sb_rblocks : \
    3119             :                 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
    3120             :                 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
    3121             :                 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
    3122             : 
    3123   134941728 :         mp = ap->ip->i_mount;
    3124   134941728 :         rt = XFS_IS_REALTIME_INODE(ap->ip) &&
    3125    58730903 :                 (ap->datatype & XFS_ALLOC_USERDATA);
    3126             :         /*
    3127             :          * If allocating at eof, and there's a previous real block,
    3128             :          * try to use its last block as our starting point.
    3129             :          */
    3130   134941728 :         if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
    3131    53918116 :             !isnullstartblock(ap->prev.br_startblock) &&
    3132    42132768 :             ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
    3133             :                     ap->prev.br_startblock)) {
    3134    42132289 :                 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
    3135             :                 /*
    3136             :                  * Adjust for the gap between prevp and us.
    3137             :                  */
    3138    42132289 :                 adjust = ap->offset -
    3139    42132289 :                         (ap->prev.br_startoff + ap->prev.br_blockcount);
    3140    48162108 :                 if (adjust &&
    3141     6029820 :                     ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
    3142     5983684 :                         ap->blkno += adjust;
    3143             :         }
    3144             :         /*
    3145             :          * If not at eof, then compare the two neighbor blocks.
    3146             :          * Figure out whether either one gives us a good starting point,
    3147             :          * and pick the better one.
    3148             :          */
    3149    92809560 :         else if (!ap->eof) {
    3150    53481932 :                 xfs_fsblock_t   gotbno;         /* right side block number */
    3151    53481932 :                 xfs_fsblock_t   gotdiff=0;      /* right side difference */
    3152    53481932 :                 xfs_fsblock_t   prevbno;        /* left side block number */
    3153    53481932 :                 xfs_fsblock_t   prevdiff=0;     /* left side difference */
    3154             : 
    3155             :                 /*
    3156             :                  * If there's a previous (left) block, select a requested
    3157             :                  * start block based on it.
    3158             :                  */
    3159    97540622 :                 if (ap->prev.br_startoff != NULLFILEOFF &&
    3160    44278701 :                     !isnullstartblock(ap->prev.br_startblock) &&
    3161    44059357 :                     (prevbno = ap->prev.br_startblock +
    3162    70463593 :                                ap->prev.br_blockcount) &&
    3163    44058690 :                     ISVALID(prevbno, ap->prev.br_startblock)) {
    3164             :                         /*
    3165             :                          * Calculate gap to end of previous block.
    3166             :                          */
    3167    44059246 :                         adjust = prevdiff = ap->offset -
    3168    44059246 :                                 (ap->prev.br_startoff +
    3169    44059246 :                                  ap->prev.br_blockcount);
    3170             :                         /*
    3171             :                          * Figure the startblock based on the previous block's
    3172             :                          * end and the gap size.
    3173             :                          * Heuristic!
    3174             :                          * If the gap is large relative to the piece we're
    3175             :                          * allocating, or using it gives us an invalid block
    3176             :                          * number, then just use the end of the previous block.
    3177             :                          */
    3178    57171111 :                         if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3179    24123297 :                             ISVALID(prevbno + prevdiff,
    3180             :                                     ap->prev.br_startblock))
    3181    24123126 :                                 prevbno += adjust;
    3182             :                         else
    3183    19936123 :                                 prevdiff += adjust;
    3184             :                 }
    3185             :                 /*
    3186             :                  * No previous block or can't follow it, just default.
    3187             :                  */
    3188             :                 else
    3189             :                         prevbno = NULLFSBLOCK;
    3190             :                 /*
    3191             :                  * If there's a following (right) block, select a requested
    3192             :                  * start block based on it.
    3193             :                  */
    3194    53481570 :                 if (!isnullstartblock(ap->got.br_startblock)) {
    3195             :                         /*
    3196             :                          * Calculate gap to start of next block.
    3197             :                          */
    3198    27135380 :                         adjust = gotdiff = ap->got.br_startoff - ap->offset;
    3199             :                         /*
    3200             :                          * Figure the startblock based on the next block's
    3201             :                          * start and the gap size.
    3202             :                          */
    3203    27135380 :                         gotbno = ap->got.br_startblock;
    3204             :                         /*
    3205             :                          * Heuristic!
    3206             :                          * If the gap is large relative to the piece we're
    3207             :                          * allocating, or using it gives us an invalid block
    3208             :                          * number, then just use the start of the next block
    3209             :                          * offset by our length.
    3210             :                          */
    3211    31135601 :                         if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3212    12469337 :                             ISVALID(gotbno - gotdiff, gotbno))
    3213    12408924 :                                 gotbno -= adjust;
    3214    14726451 :                         else if (ISVALID(gotbno - ap->length, gotbno)) {
    3215    14660657 :                                 gotbno -= ap->length;
    3216    14660657 :                                 gotdiff += adjust - ap->length;
    3217             :                         } else
    3218       65790 :                                 gotdiff += adjust;
    3219             :                 }
    3220             :                 /*
    3221             :                  * No next block, just default.
    3222             :                  */
    3223             :                 else
    3224             :                         gotbno = NULLFSBLOCK;
    3225             :                 /*
    3226             :                  * If both valid, pick the better one, else the only good
    3227             :                  * one, else ap->blkno is already set (to 0 or the inode block).
    3228             :                  */
    3229    53481561 :                 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
    3230    32835091 :                         ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
    3231    28341417 :                 else if (prevbno != NULLFSBLOCK)
    3232    18919244 :                         ap->blkno = prevbno;
    3233     9422173 :                 else if (gotbno != NULLFSBLOCK)
    3234     1994545 :                         ap->blkno = gotbno;
    3235             :         }
    3236             : #undef ISVALID
    3237   134941477 : }
    3238             : 
    3239             : int
    3240    80999793 : xfs_bmap_longest_free_extent(
    3241             :         struct xfs_perag        *pag,
    3242             :         struct xfs_trans        *tp,
    3243             :         xfs_extlen_t            *blen)
    3244             : {
    3245    80999793 :         xfs_extlen_t            longest;
    3246    80999793 :         int                     error = 0;
    3247             : 
    3248   161999586 :         if (!xfs_perag_initialised_agf(pag)) {
    3249        2358 :                 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
    3250             :                                 NULL);
    3251        2358 :                 if (error)
    3252             :                         return error;
    3253             :         }
    3254             : 
    3255    80997502 :         longest = xfs_alloc_longest_free_extent(pag,
    3256             :                                 xfs_alloc_min_freelist(pag->pag_mount, pag),
    3257             :                                 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
    3258    80928441 :         if (*blen < longest)
    3259    79030029 :                 *blen = longest;
    3260             : 
    3261             :         return 0;
    3262             : }
    3263             : 
    3264             : static xfs_extlen_t
    3265             : xfs_bmap_select_minlen(
    3266             :         struct xfs_bmalloca     *ap,
    3267             :         struct xfs_alloc_arg    *args,
    3268             :         xfs_extlen_t            blen)
    3269             : {
    3270             : 
    3271             :         /*
    3272             :          * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
    3273             :          * possible that there is enough contiguous free space for this request.
    3274             :          */
    3275    77635147 :         if (blen < ap->minlen)
    3276             :                 return ap->minlen;
    3277             : 
    3278             :         /*
    3279             :          * If the best seen length is less than the request length,
    3280             :          * use the best as the minimum, otherwise we've got the maxlen we
    3281             :          * were asked for.
    3282             :          */
    3283    77635016 :         if (blen < args->maxlen)
    3284             :                 return blen;
    3285             :         return args->maxlen;
    3286             : }
    3287             : 
    3288             : static int
    3289    77500192 : xfs_bmap_btalloc_select_lengths(
    3290             :         struct xfs_bmalloca     *ap,
    3291             :         struct xfs_alloc_arg    *args,
    3292             :         xfs_extlen_t            *blen)
    3293             : {
    3294    77500192 :         struct xfs_mount        *mp = args->mp;
    3295    77500192 :         struct xfs_perag        *pag;
    3296    77500192 :         xfs_agnumber_t          agno, startag;
    3297    77500192 :         int                     error = 0;
    3298             : 
    3299    77500192 :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3300         548 :                 args->total = ap->minlen;
    3301         548 :                 args->minlen = ap->minlen;
    3302         548 :                 return 0;
    3303             :         }
    3304             : 
    3305    77499644 :         args->total = ap->total;
    3306    77499644 :         startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
    3307    77499644 :         if (startag == NULLAGNUMBER)
    3308           0 :                 startag = 0;
    3309             : 
    3310    77499644 :         *blen = 0;
    3311    81108696 :         for_each_perag_wrap(mp, startag, agno, pag) {
    3312    80555237 :                 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
    3313    80458923 :                 if (error && error != -EAGAIN)
    3314             :                         break;
    3315    80458923 :                 error = 0;
    3316    80458923 :                 if (*blen >= args->maxlen)
    3317             :                         break;
    3318             :         }
    3319    77488553 :         if (pag)
    3320    76859449 :                 xfs_perag_rele(pag);
    3321             : 
    3322    77573611 :         args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
    3323    77573611 :         return error;
    3324             : }
    3325             : 
    3326             : /* Update all inode and quota accounting for the allocation we just did. */
    3327             : static void
    3328    80356386 : xfs_bmap_btalloc_accounting(
    3329             :         struct xfs_bmalloca     *ap,
    3330             :         struct xfs_alloc_arg    *args)
    3331             : {
    3332    80356386 :         if (ap->flags & XFS_BMAPI_COWFORK) {
    3333             :                 /*
    3334             :                  * COW fork blocks are in-core only and thus are treated as
    3335             :                  * in-core quota reservation (like delalloc blocks) even when
    3336             :                  * converted to real blocks. The quota reservation is not
    3337             :                  * accounted to disk until blocks are remapped to the data
    3338             :                  * fork. So if these blocks were previously delalloc, we
    3339             :                  * already have quota reservation and there's nothing to do
    3340             :                  * yet.
    3341             :                  */
    3342     3750570 :                 if (ap->wasdel) {
    3343     3390438 :                         xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3344     3390438 :                         return;
    3345             :                 }
    3346             : 
    3347             :                 /*
    3348             :                  * Otherwise, we've allocated blocks in a hole. The transaction
    3349             :                  * has acquired in-core quota reservation for this extent.
    3350             :                  * Rather than account these as real blocks, however, we reduce
    3351             :                  * the transaction quota reservation based on the allocation.
    3352             :                  * This essentially transfers the transaction quota reservation
    3353             :                  * to that of a delalloc extent.
    3354             :                  */
    3355      360132 :                 ap->ip->i_delayed_blks += args->len;
    3356      360132 :                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
    3357      360132 :                                 -(long)args->len);
    3358      360132 :                 return;
    3359             :         }
    3360             : 
    3361             :         /* data/attr fork only */
    3362    76605816 :         ap->ip->i_nblocks += args->len;
    3363    76605816 :         xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
    3364    76633086 :         if (ap->wasdel) {
    3365    24290340 :                 ap->ip->i_delayed_blks -= args->len;
    3366    24290340 :                 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3367             :         }
    3368    76632942 :         xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
    3369    76632942 :                 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
    3370    76632942 :                 args->len);
    3371             : }
    3372             : 
    3373             : static int
    3374    80311805 : xfs_bmap_compute_alignments(
    3375             :         struct xfs_bmalloca     *ap,
    3376             :         struct xfs_alloc_arg    *args)
    3377             : {
    3378    80311805 :         struct xfs_mount        *mp = args->mp;
    3379    80311805 :         xfs_extlen_t            align = 0; /* minimum allocation alignment */
    3380    80311805 :         int                     stripe_align = 0;
    3381             : 
    3382             :         /* stripe alignment for allocation is determined by mount parameters */
    3383    80311805 :         if (mp->m_swidth && xfs_has_swalloc(mp))
    3384             :                 stripe_align = mp->m_swidth;
    3385    80311379 :         else if (mp->m_dalign)
    3386     5278778 :                 stripe_align = mp->m_dalign;
    3387             : 
    3388    80311805 :         if (ap->flags & XFS_BMAPI_COWFORK)
    3389     3750505 :                 align = xfs_get_cowextsz_hint(ap->ip);
    3390    76561300 :         else if (ap->datatype & XFS_ALLOC_USERDATA)
    3391    43333906 :                 align = xfs_get_extsz_hint(ap->ip);
    3392    47082602 :         if (align) {
    3393    12134563 :                 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
    3394    12134688 :                                         ap->eof, 0, ap->conv, &ap->offset,
    3395             :                                         &ap->length))
    3396           0 :                         ASSERT(0);
    3397    12134563 :                 ASSERT(ap->length);
    3398             :         }
    3399             : 
    3400             :         /* apply extent size hints if obtained earlier */
    3401    80309871 :         if (align) {
    3402    12134575 :                 args->prod = align;
    3403    12134575 :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3404    12134554 :                 if (args->mod)
    3405     1287987 :                         args->mod = args->prod - args->mod;
    3406    68175296 :         } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
    3407    68153673 :                 args->prod = 1;
    3408    68153673 :                 args->mod = 0;
    3409             :         } else {
    3410       21623 :                 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
    3411       21623 :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3412       21623 :                 if (args->mod)
    3413       15564 :                         args->mod = args->prod - args->mod;
    3414             :         }
    3415             : 
    3416    80309850 :         return stripe_align;
    3417             : }
    3418             : 
    3419             : static void
    3420    80366454 : xfs_bmap_process_allocated_extent(
    3421             :         struct xfs_bmalloca     *ap,
    3422             :         struct xfs_alloc_arg    *args,
    3423             :         xfs_fileoff_t           orig_offset,
    3424             :         xfs_extlen_t            orig_length)
    3425             : {
    3426    80366454 :         ap->blkno = args->fsbno;
    3427    80366454 :         ap->length = args->len;
    3428             :         /*
    3429             :          * If the extent size hint is active, we tried to round the
    3430             :          * caller's allocation request offset down to extsz and the
    3431             :          * length up to another extsz boundary.  If we found a free
    3432             :          * extent we mapped it in starting at this new offset.  If the
    3433             :          * newly mapped space isn't long enough to cover any of the
    3434             :          * range of offsets that was originally requested, move the
    3435             :          * mapping up so that we can fill as much of the caller's
    3436             :          * original request as possible.  Free space is apparently
    3437             :          * very fragmented so we're unlikely to be able to satisfy the
    3438             :          * hints anyway.
    3439             :          */
    3440    80366454 :         if (ap->length <= orig_length)
    3441    79927369 :                 ap->offset = orig_offset;
    3442      439085 :         else if (ap->offset + ap->length < orig_offset + orig_length)
    3443          20 :                 ap->offset = orig_offset + orig_length - ap->length;
    3444    80366454 :         xfs_bmap_btalloc_accounting(ap, args);
    3445    80369533 : }
    3446             : 
    3447             : #ifdef DEBUG
    3448             : static int
    3449     2733352 : xfs_bmap_exact_minlen_extent_alloc(
    3450             :         struct xfs_bmalloca     *ap)
    3451             : {
    3452     2733352 :         struct xfs_mount        *mp = ap->ip->i_mount;
    3453     2733352 :         struct xfs_alloc_arg    args = { .tp = ap->tp, .mp = mp };
    3454     2733352 :         xfs_fileoff_t           orig_offset;
    3455     2733352 :         xfs_extlen_t            orig_length;
    3456     2733352 :         int                     error;
    3457             : 
    3458     2733352 :         ASSERT(ap->length);
    3459             : 
    3460     2733352 :         if (ap->minlen != 1) {
    3461           0 :                 ap->blkno = NULLFSBLOCK;
    3462           0 :                 ap->length = 0;
    3463           0 :                 return 0;
    3464             :         }
    3465             : 
    3466     2733352 :         orig_offset = ap->offset;
    3467     2733352 :         orig_length = ap->length;
    3468             : 
    3469     2733352 :         args.alloc_minlen_only = 1;
    3470             : 
    3471     2733352 :         xfs_bmap_compute_alignments(ap, &args);
    3472             : 
    3473             :         /*
    3474             :          * Unlike the longest extent available in an AG, we don't track
    3475             :          * the length of an AG's shortest extent.
    3476             :          * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
    3477             :          * hence we can afford to start traversing from the 0th AG since
    3478             :          * we need not be concerned about a drop in performance in
    3479             :          * "debug only" code paths.
    3480             :          */
    3481     2730639 :         ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
    3482             : 
    3483     2730639 :         args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
    3484     2730639 :         args.minlen = args.maxlen = ap->minlen;
    3485     2730639 :         args.total = ap->total;
    3486             : 
    3487     2730639 :         args.alignment = 1;
    3488     2730639 :         args.minalignslop = 0;
    3489             : 
    3490     2730639 :         args.minleft = ap->minleft;
    3491     2730639 :         args.wasdel = ap->wasdel;
    3492     2730639 :         args.resv = XFS_AG_RESV_NONE;
    3493     2730639 :         args.datatype = ap->datatype;
    3494             : 
    3495     2730639 :         error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
    3496     2734049 :         if (error)
    3497             :                 return error;
    3498             : 
    3499     2734049 :         if (args.fsbno != NULLFSBLOCK) {
    3500     2734041 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3501             :                         orig_length);
    3502             :         } else {
    3503           8 :                 ap->blkno = NULLFSBLOCK;
    3504           8 :                 ap->length = 0;
    3505             :         }
    3506             : 
    3507             :         return 0;
    3508             : }
    3509             : #else
    3510             : 
    3511             : #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
    3512             : 
    3513             : #endif
    3514             : 
    3515             : /*
    3516             :  * If we are not low on available data blocks and we are allocating at
    3517             :  * EOF, optimise allocation for contiguous file extension and/or stripe
    3518             :  * alignment of the new extent.
    3519             :  *
    3520             :  * NOTE: ap->aeof is only set if the allocation length is >= the
    3521             :  * stripe unit and the allocation offset is at the end of file.
    3522             :  */
    3523             : static int
    3524      435381 : xfs_bmap_btalloc_at_eof(
    3525             :         struct xfs_bmalloca     *ap,
    3526             :         struct xfs_alloc_arg    *args,
    3527             :         xfs_extlen_t            blen,
    3528             :         int                     stripe_align,
    3529             :         bool                    ag_only)
    3530             : {
    3531      435381 :         struct xfs_mount        *mp = args->mp;
    3532      435381 :         struct xfs_perag        *caller_pag = args->pag;
    3533      435381 :         int                     error;
    3534             : 
    3535             :         /*
    3536             :          * If there are already extents in the file, try an exact EOF block
    3537             :          * allocation to extend the file as a contiguous extent. If that fails,
    3538             :          * or it's the first allocation in a file, just try for a stripe aligned
    3539             :          * allocation.
    3540             :          */
    3541      435381 :         if (ap->offset) {
    3542      407912 :                 xfs_extlen_t    nextminlen = 0;
    3543             : 
    3544             :                 /*
    3545             :                  * Compute the minlen+alignment for the next case.  Set slop so
    3546             :                  * that the value of minlen+alignment+slop doesn't go up between
    3547             :                  * the calls.
    3548             :                  */
    3549      407912 :                 args->alignment = 1;
    3550      407912 :                 if (blen > stripe_align && blen <= args->maxlen)
    3551        5518 :                         nextminlen = blen - stripe_align;
    3552             :                 else
    3553      402394 :                         nextminlen = args->minlen;
    3554      407912 :                 if (nextminlen + stripe_align > args->minlen + 1)
    3555      402394 :                         args->minalignslop = nextminlen + stripe_align -
    3556      402394 :                                         args->minlen - 1;
    3557             :                 else
    3558        5518 :                         args->minalignslop = 0;
    3559             : 
    3560      407912 :                 if (!caller_pag)
    3561      407069 :                         args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
    3562      407963 :                 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
    3563      407942 :                 if (!caller_pag) {
    3564      407107 :                         xfs_perag_put(args->pag);
    3565      407129 :                         args->pag = NULL;
    3566             :                 }
    3567      407964 :                 if (error)
    3568             :                         return error;
    3569             : 
    3570      407964 :                 if (args->fsbno != NULLFSBLOCK)
    3571             :                         return 0;
    3572             :                 /*
    3573             :                  * Exact allocation failed. Reset to try an aligned allocation
    3574             :                  * according to the original allocation specification.
    3575             :                  */
    3576      289851 :                 args->alignment = stripe_align;
    3577      289851 :                 args->minlen = nextminlen;
    3578      289851 :                 args->minalignslop = 0;
    3579             :         } else {
    3580             :                 /*
    3581             :                  * Adjust minlen to try and preserve alignment if we
    3582             :                  * can't guarantee an aligned maxlen extent.
    3583             :                  */
    3584       27469 :                 args->alignment = stripe_align;
    3585       27469 :                 if (blen > args->alignment &&
    3586       27387 :                     blen <= args->maxlen + args->alignment)
    3587        1164 :                         args->minlen = blen - args->alignment;
    3588       27469 :                 args->minalignslop = 0;
    3589             :         }
    3590             : 
    3591      317320 :         if (ag_only) {
    3592        5610 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3593             :         } else {
    3594      311710 :                 args->pag = NULL;
    3595      311710 :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3596      311729 :                 ASSERT(args->pag == NULL);
    3597      311729 :                 args->pag = caller_pag;
    3598             :         }
    3599      317334 :         if (error)
    3600             :                 return error;
    3601             : 
    3602      317334 :         if (args->fsbno != NULLFSBLOCK)
    3603             :                 return 0;
    3604             : 
    3605             :         /*
    3606             :          * Allocation failed, so turn return the allocation args to their
    3607             :          * original non-aligned state so the caller can proceed on allocation
    3608             :          * failure as if this function was never called.
    3609             :          */
    3610       17585 :         args->alignment = 1;
    3611       17585 :         return 0;
    3612             : }
    3613             : 
    3614             : /*
    3615             :  * We have failed multiple allocation attempts so now are in a low space
    3616             :  * allocation situation. Try a locality first full filesystem minimum length
    3617             :  * allocation whilst still maintaining necessary total block reservation
    3618             :  * requirements.
    3619             :  *
    3620             :  * If that fails, we are now critically low on space, so perform a last resort
    3621             :  * allocation attempt: no reserve, no locality, blocking, minimum length, full
    3622             :  * filesystem free space scan. We also indicate to future allocations in this
    3623             :  * transaction that we are critically low on space so they don't waste time on
    3624             :  * allocation modes that are unlikely to succeed.
    3625             :  */
    3626             : int
    3627      571394 : xfs_bmap_btalloc_low_space(
    3628             :         struct xfs_bmalloca     *ap,
    3629             :         struct xfs_alloc_arg    *args)
    3630             : {
    3631      571394 :         int                     error;
    3632             : 
    3633      571394 :         if (args->minlen > ap->minlen) {
    3634      571234 :                 args->minlen = ap->minlen;
    3635      571234 :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3636      571234 :                 if (error || args->fsbno != NULLFSBLOCK)
    3637             :                         return error;
    3638             :         }
    3639             : 
    3640             :         /* Last ditch attempt before failure is declared. */
    3641         160 :         args->total = ap->minlen;
    3642         160 :         error = xfs_alloc_vextent_first_ag(args, 0);
    3643         160 :         if (error)
    3644             :                 return error;
    3645         160 :         ap->tp->t_flags |= XFS_TRANS_LOWMODE;
    3646         160 :         return 0;
    3647             : }
    3648             : 
    3649             : static int
    3650       61517 : xfs_bmap_btalloc_filestreams(
    3651             :         struct xfs_bmalloca     *ap,
    3652             :         struct xfs_alloc_arg    *args,
    3653             :         int                     stripe_align)
    3654             : {
    3655       61517 :         xfs_extlen_t            blen = 0;
    3656       61517 :         int                     error = 0;
    3657             : 
    3658             : 
    3659       61517 :         error = xfs_filestream_select_ag(ap, args, &blen);
    3660       61536 :         if (error)
    3661             :                 return error;
    3662       61536 :         ASSERT(args->pag);
    3663             : 
    3664             :         /*
    3665             :          * If we are in low space mode, then optimal allocation will fail so
    3666             :          * prepare for minimal allocation and jump to the low space algorithm
    3667             :          * immediately.
    3668             :          */
    3669       61536 :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3670           0 :                 args->minlen = ap->minlen;
    3671           0 :                 ASSERT(args->fsbno == NULLFSBLOCK);
    3672           0 :                 goto out_low_space;
    3673             :         }
    3674             : 
    3675       61536 :         args->minlen = xfs_bmap_select_minlen(ap, args, blen);
    3676       61536 :         if (ap->aeof)
    3677        5611 :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3678             :                                 true);
    3679             : 
    3680       61536 :         if (!error && args->fsbno == NULLFSBLOCK)
    3681       55925 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3682             : 
    3683        5611 : out_low_space:
    3684             :         /*
    3685             :          * We are now done with the perag reference for the filestreams
    3686             :          * association provided by xfs_filestream_select_ag(). Release it now as
    3687             :          * we've either succeeded, had a fatal error or we are out of space and
    3688             :          * need to do a full filesystem scan for free space which will take it's
    3689             :          * own references.
    3690             :          */
    3691       61533 :         xfs_perag_rele(args->pag);
    3692       61542 :         args->pag = NULL;
    3693       61542 :         if (error || args->fsbno != NULLFSBLOCK)
    3694             :                 return error;
    3695             : 
    3696           0 :         return xfs_bmap_btalloc_low_space(ap, args);
    3697             : }
    3698             : 
    3699             : static int
    3700    77535142 : xfs_bmap_btalloc_best_length(
    3701             :         struct xfs_bmalloca     *ap,
    3702             :         struct xfs_alloc_arg    *args,
    3703             :         int                     stripe_align)
    3704             : {
    3705    77535142 :         xfs_extlen_t            blen = 0;
    3706    77535142 :         int                     error;
    3707             : 
    3708    77535142 :         ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
    3709    77535142 :         xfs_bmap_adjacent(ap);
    3710             : 
    3711             :         /*
    3712             :          * Search for an allocation group with a single extent large enough for
    3713             :          * the request.  If one isn't found, then adjust the minimum allocation
    3714             :          * size to the largest space found.
    3715             :          */
    3716    77497675 :         error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
    3717    77568523 :         if (error)
    3718             :                 return error;
    3719             : 
    3720             :         /*
    3721             :          * Don't attempt optimal EOF allocation if previous allocations barely
    3722             :          * succeeded due to being near ENOSPC. It is highly unlikely we'll get
    3723             :          * optimal or even aligned allocations in this case, so don't waste time
    3724             :          * trying.
    3725             :          */
    3726    77568523 :         if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
    3727      429831 :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3728             :                                 false);
    3729      429830 :                 if (error || args->fsbno != NULLFSBLOCK)
    3730             :                         return error;
    3731             :         }
    3732             : 
    3733    77156276 :         error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3734    77178025 :         if (error || args->fsbno != NULLFSBLOCK)
    3735             :                 return error;
    3736             : 
    3737      571394 :         return xfs_bmap_btalloc_low_space(ap, args);
    3738             : }
    3739             : 
    3740             : static int
    3741    77589629 : xfs_bmap_btalloc(
    3742             :         struct xfs_bmalloca     *ap)
    3743             : {
    3744    77589629 :         struct xfs_mount        *mp = ap->ip->i_mount;
    3745   155179258 :         struct xfs_alloc_arg    args = {
    3746    77589629 :                 .tp             = ap->tp,
    3747             :                 .mp             = mp,
    3748             :                 .fsbno          = NULLFSBLOCK,
    3749             :                 .oinfo          = XFS_RMAP_OINFO_SKIP_UPDATE,
    3750    77589629 :                 .minleft        = ap->minleft,
    3751           0 :                 .wasdel         = ap->wasdel,
    3752             :                 .resv           = XFS_AG_RESV_NONE,
    3753    77589629 :                 .datatype       = ap->datatype,
    3754             :                 .alignment      = 1,
    3755             :                 .minalignslop   = 0,
    3756             :         };
    3757    77589629 :         xfs_fileoff_t           orig_offset;
    3758    77589629 :         xfs_extlen_t            orig_length;
    3759    77589629 :         int                     error;
    3760    77589629 :         int                     stripe_align;
    3761             : 
    3762    77589629 :         ASSERT(ap->length);
    3763    77589629 :         orig_offset = ap->offset;
    3764    77589629 :         orig_length = ap->length;
    3765             : 
    3766    77589629 :         stripe_align = xfs_bmap_compute_alignments(ap, &args);
    3767             : 
    3768             :         /* Trim the allocation back to the maximum an AG can fit. */
    3769    77569644 :         args.maxlen = min(ap->length, mp->m_ag_max_usable);
    3770             : 
    3771    77569644 :         if ((ap->datatype & XFS_ALLOC_USERDATA) &&
    3772    44367860 :             xfs_inode_is_filestream(ap->ip))
    3773       61884 :                 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
    3774             :         else
    3775    77507760 :                 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
    3776    77646914 :         if (error)
    3777             :                 return error;
    3778             : 
    3779    77641947 :         if (args.fsbno != NULLFSBLOCK) {
    3780    77641787 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3781             :                         orig_length);
    3782             :         } else {
    3783         160 :                 ap->blkno = NULLFSBLOCK;
    3784         160 :                 ap->length = 0;
    3785             :         }
    3786             :         return 0;
    3787             : }
    3788             : 
    3789             : /* Trim extent to fit a logical block range. */
    3790             : void
    3791   469136861 : xfs_trim_extent(
    3792             :         struct xfs_bmbt_irec    *irec,
    3793             :         xfs_fileoff_t           bno,
    3794             :         xfs_filblks_t           len)
    3795             : {
    3796   469136861 :         xfs_fileoff_t           distance;
    3797   469136861 :         xfs_fileoff_t           end = bno + len;
    3798             : 
    3799   469136861 :         if (irec->br_startoff + irec->br_blockcount <= bno ||
    3800             :             irec->br_startoff >= end) {
    3801    95492504 :                 irec->br_blockcount = 0;
    3802    95492504 :                 return;
    3803             :         }
    3804             : 
    3805   373644357 :         if (irec->br_startoff < bno) {
    3806   130332630 :                 distance = bno - irec->br_startoff;
    3807   130332630 :                 if (isnullstartblock(irec->br_startblock))
    3808    29285025 :                         irec->br_startblock = DELAYSTARTBLOCK;
    3809   130332630 :                 if (irec->br_startblock != DELAYSTARTBLOCK &&
    3810             :                     irec->br_startblock != HOLESTARTBLOCK)
    3811   101021302 :                         irec->br_startblock += distance;
    3812   130332630 :                 irec->br_startoff += distance;
    3813   130332630 :                 irec->br_blockcount -= distance;
    3814             :         }
    3815             : 
    3816   373644357 :         if (end < irec->br_startoff + irec->br_blockcount) {
    3817    48206950 :                 distance = irec->br_startoff + irec->br_blockcount - end;
    3818    48206950 :                 irec->br_blockcount -= distance;
    3819             :         }
    3820             : }
    3821             : 
    3822             : /*
    3823             :  * Trim the returned map to the required bounds
    3824             :  */
    3825             : STATIC void
    3826  6685136705 : xfs_bmapi_trim_map(
    3827             :         struct xfs_bmbt_irec    *mval,
    3828             :         struct xfs_bmbt_irec    *got,
    3829             :         xfs_fileoff_t           *bno,
    3830             :         xfs_filblks_t           len,
    3831             :         xfs_fileoff_t           obno,
    3832             :         xfs_fileoff_t           end,
    3833             :         int                     n,
    3834             :         uint32_t                flags)
    3835             : {
    3836  6685136705 :         if ((flags & XFS_BMAPI_ENTIRE) ||
    3837  6685136705 :             got->br_startoff + got->br_blockcount <= obno) {
    3838          83 :                 *mval = *got;
    3839          83 :                 if (isnullstartblock(got->br_startblock))
    3840           0 :                         mval->br_startblock = DELAYSTARTBLOCK;
    3841          83 :                 return;
    3842             :         }
    3843             : 
    3844  6685136622 :         if (obno > *bno)
    3845           0 :                 *bno = obno;
    3846  6685136622 :         ASSERT((*bno >= obno) || (n == 0));
    3847  6685136622 :         ASSERT(*bno < end);
    3848  6685136622 :         mval->br_startoff = *bno;
    3849  6685136622 :         if (isnullstartblock(got->br_startblock))
    3850     3955945 :                 mval->br_startblock = DELAYSTARTBLOCK;
    3851             :         else
    3852  6681180677 :                 mval->br_startblock = got->br_startblock +
    3853  6681180677 :                                         (*bno - got->br_startoff);
    3854             :         /*
    3855             :          * Return the minimum of what we got and what we asked for for
    3856             :          * the length.  We can use the len variable here because it is
    3857             :          * modified below and we could have been there before coming
    3858             :          * here if the first part of the allocation didn't overlap what
    3859             :          * was asked for.
    3860             :          */
    3861  6685136622 :         mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
    3862             :                         got->br_blockcount - (*bno - got->br_startoff));
    3863  6685136622 :         mval->br_state = got->br_state;
    3864  6685136622 :         ASSERT(mval->br_blockcount <= len);
    3865             :         return;
    3866             : }
    3867             : 
    3868             : /*
    3869             :  * Update and validate the extent map to return
    3870             :  */
    3871             : STATIC void
    3872  6682748490 : xfs_bmapi_update_map(
    3873             :         struct xfs_bmbt_irec    **map,
    3874             :         xfs_fileoff_t           *bno,
    3875             :         xfs_filblks_t           *len,
    3876             :         xfs_fileoff_t           obno,
    3877             :         xfs_fileoff_t           end,
    3878             :         int                     *n,
    3879             :         uint32_t                flags)
    3880             : {
    3881  6682748490 :         xfs_bmbt_irec_t *mval = *map;
    3882             : 
    3883  6682748490 :         ASSERT((flags & XFS_BMAPI_ENTIRE) ||
    3884             :                ((mval->br_startoff + mval->br_blockcount) <= end));
    3885  6682748490 :         ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
    3886             :                (mval->br_startoff < obno));
    3887             : 
    3888  6682748490 :         *bno = mval->br_startoff + mval->br_blockcount;
    3889  6682748490 :         *len = end - *bno;
    3890  6682748490 :         if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
    3891             :                 /* update previous map with new information */
    3892           0 :                 ASSERT(mval->br_startblock == mval[-1].br_startblock);
    3893           0 :                 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
    3894           0 :                 ASSERT(mval->br_state == mval[-1].br_state);
    3895           0 :                 mval[-1].br_blockcount = mval->br_blockcount;
    3896           0 :                 mval[-1].br_state = mval->br_state;
    3897  6682748490 :         } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
    3898      636127 :                    mval[-1].br_startblock != DELAYSTARTBLOCK &&
    3899      635015 :                    mval[-1].br_startblock != HOLESTARTBLOCK &&
    3900      635015 :                    mval->br_startblock == mval[-1].br_startblock +
    3901      635015 :                                           mval[-1].br_blockcount &&
    3902           0 :                    mval[-1].br_state == mval->br_state) {
    3903           0 :                 ASSERT(mval->br_startoff ==
    3904             :                        mval[-1].br_startoff + mval[-1].br_blockcount);
    3905           0 :                 mval[-1].br_blockcount += mval->br_blockcount;
    3906  6682748490 :         } else if (*n > 0 &&
    3907      636127 :                    mval->br_startblock == DELAYSTARTBLOCK &&
    3908           0 :                    mval[-1].br_startblock == DELAYSTARTBLOCK &&
    3909           0 :                    mval->br_startoff ==
    3910           0 :                    mval[-1].br_startoff + mval[-1].br_blockcount) {
    3911           0 :                 mval[-1].br_blockcount += mval->br_blockcount;
    3912           0 :                 mval[-1].br_state = mval->br_state;
    3913  6682748490 :         } else if (!((*n == 0) &&
    3914  6681874544 :                      ((mval->br_startoff + mval->br_blockcount) <=
    3915             :                       obno))) {
    3916  6680990972 :                 mval++;
    3917  6680990972 :                 (*n)++;
    3918             :         }
    3919  6682748490 :         *map = mval;
    3920  6682748490 : }
    3921             : 
    3922             : /*
    3923             :  * Map file blocks to filesystem blocks without allocation.
    3924             :  */
    3925             : int
    3926  8749126463 : xfs_bmapi_read(
    3927             :         struct xfs_inode        *ip,
    3928             :         xfs_fileoff_t           bno,
    3929             :         xfs_filblks_t           len,
    3930             :         struct xfs_bmbt_irec    *mval,
    3931             :         int                     *nmap,
    3932             :         uint32_t                flags)
    3933             : {
    3934  8749126463 :         struct xfs_mount        *mp = ip->i_mount;
    3935  8749126463 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    3936  8749126463 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    3937  8745153375 :         struct xfs_bmbt_irec    got;
    3938  8745153375 :         xfs_fileoff_t           obno;
    3939  8745153375 :         xfs_fileoff_t           end;
    3940  8745153375 :         struct xfs_iext_cursor  icur;
    3941  8745153375 :         int                     error;
    3942  8745153375 :         bool                    eof = false;
    3943  8745153375 :         int                     n = 0;
    3944             : 
    3945  8745153375 :         ASSERT(*nmap >= 1);
    3946  8745153375 :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
    3947  8745153375 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
    3948             : 
    3949  8745375257 :         if (WARN_ON_ONCE(!ifp)) {
    3950           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    3951           0 :                 return -EFSCORRUPTED;
    3952             :         }
    3953             : 
    3954 17489321572 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    3955  8745375257 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    3956           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    3957           0 :                 return -EFSCORRUPTED;
    3958             :         }
    3959             : 
    3960 17487892630 :         if (xfs_is_shutdown(mp))
    3961             :                 return -EIO;
    3962             : 
    3963  8743937389 :         XFS_STATS_INC(mp, xs_blk_mapr);
    3964             : 
    3965  8746184624 :         error = xfs_iread_extents(NULL, ip, whichfork);
    3966  8745877595 :         if (error)
    3967             :                 return error;
    3968             : 
    3969  8746172133 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
    3970   574518361 :                 eof = true;
    3971  8747756190 :         end = bno + len;
    3972  8747756190 :         obno = bno;
    3973             : 
    3974 11036662994 :         while (bno < end && n < *nmap) {
    3975             :                 /* Reading past eof, act as though there's a hole up to end. */
    3976  8771367021 :                 if (eof)
    3977   597467348 :                         got.br_startoff = end;
    3978  8771367021 :                 if (got.br_startoff > bno) {
    3979             :                         /* Reading in a hole.  */
    3980  2265294581 :                         mval->br_startoff = bno;
    3981  2265294581 :                         mval->br_startblock = HOLESTARTBLOCK;
    3982  2265294581 :                         mval->br_blockcount =
    3983  2265294581 :                                 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
    3984  2265294581 :                         mval->br_state = XFS_EXT_NORM;
    3985  2265294581 :                         bno += mval->br_blockcount;
    3986  2265294581 :                         len -= mval->br_blockcount;
    3987  2265294581 :                         mval++;
    3988  2265294581 :                         n++;
    3989  2265294581 :                         continue;
    3990             :                 }
    3991             : 
    3992             :                 /* set up the extent map to return. */
    3993  6506072440 :                 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
    3994  6504943149 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    3995             : 
    3996             :                 /* If we're done, stop now. */
    3997  6504769708 :                 if (bno >= end || n >= *nmap)
    3998             :                         break;
    3999             : 
    4000             :                 /* Else go on to the next record. */
    4001    23607825 :                 if (!xfs_iext_next_extent(ifp, &icur, &got))
    4002    22978911 :                         eof = true;
    4003             :         }
    4004  8746457856 :         *nmap = n;
    4005  8746457856 :         return 0;
    4006             : }
    4007             : 
    4008             : /*
    4009             :  * Add a delayed allocation extent to an inode. Blocks are reserved from the
    4010             :  * global pool and the extent inserted into the inode in-core extent tree.
    4011             :  *
    4012             :  * On entry, got refers to the first extent beyond the offset of the extent to
    4013             :  * allocate or eof is specified if no such extent exists. On return, got refers
    4014             :  * to the extent record that was inserted to the inode fork.
    4015             :  *
    4016             :  * Note that the allocated extent may have been merged with contiguous extents
    4017             :  * during insertion into the inode fork. Thus, got does not reflect the current
    4018             :  * state of the inode fork on return. If necessary, the caller can use lastx to
    4019             :  * look up the updated record in the inode fork.
    4020             :  */
    4021             : int
    4022    54231165 : xfs_bmapi_reserve_delalloc(
    4023             :         struct xfs_inode        *ip,
    4024             :         int                     whichfork,
    4025             :         xfs_fileoff_t           off,
    4026             :         xfs_filblks_t           len,
    4027             :         xfs_filblks_t           prealloc,
    4028             :         struct xfs_bmbt_irec    *got,
    4029             :         struct xfs_iext_cursor  *icur,
    4030             :         int                     eof)
    4031             : {
    4032    54231165 :         struct xfs_mount        *mp = ip->i_mount;
    4033    54231165 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4034    54165375 :         xfs_extlen_t            alen;
    4035    54165375 :         xfs_extlen_t            indlen;
    4036    54165375 :         int                     error;
    4037    54165375 :         xfs_fileoff_t           aoff = off;
    4038             : 
    4039             :         /*
    4040             :          * Cap the alloc length. Keep track of prealloc so we know whether to
    4041             :          * tag the inode before we return.
    4042             :          */
    4043    54165375 :         alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
    4044    54165375 :         if (!eof)
    4045    26986563 :                 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
    4046    54165375 :         if (prealloc && alen >= len)
    4047     6955936 :                 prealloc = alen - len;
    4048             : 
    4049             :         /* Figure out the extent size, adjust alen */
    4050    54165375 :         if (whichfork == XFS_COW_FORK) {
    4051     4419709 :                 struct xfs_bmbt_irec    prev;
    4052     4419709 :                 xfs_extlen_t            extsz = xfs_get_cowextsz_hint(ip);
    4053             : 
    4054     4422791 :                 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
    4055     2354097 :                         prev.br_startoff = NULLFILEOFF;
    4056             : 
    4057     4423044 :                 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
    4058             :                                                1, 0, &aoff, &alen);
    4059     4421280 :                 ASSERT(!error);
    4060             :         }
    4061             : 
    4062             :         /*
    4063             :          * Make a transaction-less quota reservation for delayed allocation
    4064             :          * blocks.  This number gets adjusted later.  We return if we haven't
    4065             :          * allocated blocks already inside this loop.
    4066             :          */
    4067    54166946 :         error = xfs_quota_reserve_blkres(ip, alen);
    4068    54339379 :         if (error)
    4069             :                 return error;
    4070             : 
    4071             :         /*
    4072             :          * Split changing sb for alen and indlen since they could be coming
    4073             :          * from different places.
    4074             :          */
    4075    54333134 :         indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
    4076    54326025 :         ASSERT(indlen > 0);
    4077             : 
    4078    54326025 :         error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
    4079    54265148 :         if (error)
    4080     1342530 :                 goto out_unreserve_quota;
    4081             : 
    4082    52922618 :         error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
    4083    52947749 :         if (error)
    4084      314491 :                 goto out_unreserve_blocks;
    4085             : 
    4086             : 
    4087    52633258 :         ip->i_delayed_blks += alen;
    4088    52633258 :         xfs_mod_delalloc(ip->i_mount, alen + indlen);
    4089             : 
    4090    52600377 :         got->br_startoff = aoff;
    4091    52600377 :         got->br_startblock = nullstartblock(indlen);
    4092    52609162 :         got->br_blockcount = alen;
    4093    52609162 :         got->br_state = XFS_EXT_NORM;
    4094             : 
    4095    52609162 :         xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
    4096             : 
    4097             :         /*
    4098             :          * Tag the inode if blocks were preallocated. Note that COW fork
    4099             :          * preallocation can occur at the start or end of the extent, even when
    4100             :          * prealloc == 0, so we must also check the aligned offset and length.
    4101             :          */
    4102    52548409 :         if (whichfork == XFS_DATA_FORK && prealloc)
    4103     6140836 :                 xfs_inode_set_eofblocks_tag(ip);
    4104    52548591 :         if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
    4105     4082238 :                 xfs_inode_set_cowblocks_tag(ip);
    4106             : 
    4107             :         return 0;
    4108             : 
    4109             : out_unreserve_blocks:
    4110      314491 :         xfs_mod_fdblocks(mp, alen, false);
    4111     1656962 : out_unreserve_quota:
    4112     1656962 :         if (XFS_IS_QUOTA_ON(mp))
    4113     1487507 :                 xfs_quota_unreserve_blkres(ip, alen);
    4114             :         return error;
    4115             : }
    4116             : 
    4117             : static int
    4118   104338940 : xfs_bmap_alloc_userdata(
    4119             :         struct xfs_bmalloca     *bma)
    4120             : {
    4121   104338940 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4122   104338940 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4123   104338940 :         int                     error;
    4124             : 
    4125             :         /*
    4126             :          * Set the data type being allocated. For the data fork, the first data
    4127             :          * in the file is treated differently to all other allocations. For the
    4128             :          * attribute fork, we only need to ensure the allocated range is not on
    4129             :          * the busy list.
    4130             :          */
    4131   104338940 :         bma->datatype = XFS_ALLOC_NOBUSY;
    4132   104338940 :         if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
    4133   104335977 :                 bma->datatype |= XFS_ALLOC_USERDATA;
    4134   104335977 :                 if (bma->offset == 0)
    4135     9473372 :                         bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
    4136             : 
    4137   104335977 :                 if (mp->m_dalign && bma->length >= mp->m_dalign) {
    4138     1025761 :                         error = xfs_bmap_isaeof(bma, whichfork);
    4139     1026230 :                         if (error)
    4140             :                                 return error;
    4141             :                 }
    4142             : 
    4143   104336446 :                 if (XFS_IS_REALTIME_INODE(bma->ip))
    4144    57251805 :                         return xfs_bmap_rtalloc(bma);
    4145             :         }
    4146             : 
    4147    47087628 :         if (unlikely(XFS_TEST_ERROR(false, mp,
    4148             :                         XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4149     2718810 :                 return xfs_bmap_exact_minlen_extent_alloc(bma);
    4150             : 
    4151    44370187 :         return xfs_bmap_btalloc(bma);
    4152             : }
    4153             : 
    4154             : static int
    4155   137590411 : xfs_bmapi_allocate(
    4156             :         struct xfs_bmalloca     *bma)
    4157             : {
    4158   137590411 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4159   137590411 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4160   137590411 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4161   137541862 :         int                     tmp_logflags = 0;
    4162   137541862 :         int                     error;
    4163             : 
    4164   137541862 :         ASSERT(bma->length > 0);
    4165             : 
    4166             :         /*
    4167             :          * For the wasdelay case, we could also just allocate the stuff asked
    4168             :          * for in this bmap call but that wouldn't be as good.
    4169             :          */
    4170   137541862 :         if (bma->wasdel) {
    4171    27684880 :                 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
    4172    27684880 :                 bma->offset = bma->got.br_startoff;
    4173    27684880 :                 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
    4174     7247211 :                         bma->prev.br_startoff = NULLFILEOFF;
    4175             :         } else {
    4176   109856982 :                 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
    4177   109856982 :                 if (!bma->eof)
    4178    27744567 :                         bma->length = XFS_FILBLKS_MIN(bma->length,
    4179             :                                         bma->got.br_startoff - bma->offset);
    4180             :         }
    4181             : 
    4182   137542040 :         if (bma->flags & XFS_BMAPI_CONTIG)
    4183     5706516 :                 bma->minlen = bma->length;
    4184             :         else
    4185   131835524 :                 bma->minlen = 1;
    4186             : 
    4187   137542040 :         if (bma->flags & XFS_BMAPI_METADATA) {
    4188    33210024 :                 if (unlikely(XFS_TEST_ERROR(false, mp,
    4189             :                                 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4190       15377 :                         error = xfs_bmap_exact_minlen_extent_alloc(bma);
    4191             :                 else
    4192    33216888 :                         error = xfs_bmap_btalloc(bma);
    4193             :         } else {
    4194   104332016 :                 error = xfs_bmap_alloc_userdata(bma);
    4195             :         }
    4196   137698525 :         if (error || bma->blkno == NULLFSBLOCK)
    4197             :                 return error;
    4198             : 
    4199   137693378 :         if (bma->flags & XFS_BMAPI_ZERO) {
    4200           0 :                 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
    4201           0 :                 if (error)
    4202             :                         return error;
    4203             :         }
    4204             : 
    4205   137693378 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
    4206    31214925 :                 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
    4207             :         /*
    4208             :          * Bump the number of extents we've allocated
    4209             :          * in this call.
    4210             :          */
    4211   137695702 :         bma->nallocs++;
    4212             : 
    4213   137695702 :         if (bma->cur)
    4214    31209489 :                 bma->cur->bc_ino.flags =
    4215    31209489 :                         bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
    4216             : 
    4217   137695702 :         bma->got.br_startoff = bma->offset;
    4218   137695702 :         bma->got.br_startblock = bma->blkno;
    4219   137695702 :         bma->got.br_blockcount = bma->length;
    4220   137695702 :         bma->got.br_state = XFS_EXT_NORM;
    4221             : 
    4222   137695702 :         if (bma->flags & XFS_BMAPI_PREALLOC)
    4223   104436941 :                 bma->got.br_state = XFS_EXT_UNWRITTEN;
    4224             : 
    4225   137695702 :         if (bma->wasdel)
    4226    27680486 :                 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
    4227             :         else
    4228   110015216 :                 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
    4229             :                                 whichfork, &bma->icur, &bma->cur, &bma->got,
    4230             :                                 &bma->logflags, bma->flags);
    4231             : 
    4232   137703146 :         bma->logflags |= tmp_logflags;
    4233   137703146 :         if (error)
    4234             :                 return error;
    4235             : 
    4236             :         /*
    4237             :          * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
    4238             :          * or xfs_bmap_add_extent_hole_real might have merged it into one of
    4239             :          * the neighbouring ones.
    4240             :          */
    4241   137681209 :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4242             : 
    4243   137696915 :         ASSERT(bma->got.br_startoff <= bma->offset);
    4244   137696915 :         ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
    4245             :                bma->offset + bma->length);
    4246   137696915 :         ASSERT(bma->got.br_state == XFS_EXT_NORM ||
    4247             :                bma->got.br_state == XFS_EXT_UNWRITTEN);
    4248             :         return 0;
    4249             : }
    4250             : 
    4251             : STATIC int
    4252   178423567 : xfs_bmapi_convert_unwritten(
    4253             :         struct xfs_bmalloca     *bma,
    4254             :         struct xfs_bmbt_irec    *mval,
    4255             :         xfs_filblks_t           len,
    4256             :         uint32_t                flags)
    4257             : {
    4258   178423567 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4259   178423567 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4260   178398968 :         int                     tmp_logflags = 0;
    4261   178398968 :         int                     error;
    4262             : 
    4263             :         /* check if we need to do unwritten->real conversion */
    4264   178398968 :         if (mval->br_state == XFS_EXT_UNWRITTEN &&
    4265   136355028 :             (flags & XFS_BMAPI_PREALLOC))
    4266             :                 return 0;
    4267             : 
    4268             :         /* check if we need to do real->unwritten conversion */
    4269    98573607 :         if (mval->br_state == XFS_EXT_NORM &&
    4270    42040670 :             (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
    4271             :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
    4272             :                 return 0;
    4273             : 
    4274             :         /*
    4275             :          * Modify (by adding) the state flag, if writing.
    4276             :          */
    4277    56532937 :         ASSERT(mval->br_blockcount <= len);
    4278    56532937 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
    4279    25993638 :                 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
    4280             :                                         bma->ip, whichfork);
    4281             :         }
    4282    56533164 :         mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
    4283    56533164 :                                 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
    4284             : 
    4285             :         /*
    4286             :          * Before insertion into the bmbt, zero the range being converted
    4287             :          * if required.
    4288             :          */
    4289    56533164 :         if (flags & XFS_BMAPI_ZERO) {
    4290           0 :                 error = xfs_zero_extent(bma->ip, mval->br_startblock,
    4291           0 :                                         mval->br_blockcount);
    4292           0 :                 if (error)
    4293             :                         return error;
    4294             :         }
    4295             : 
    4296    56533164 :         error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
    4297             :                         &bma->icur, &bma->cur, mval, &tmp_logflags);
    4298             :         /*
    4299             :          * Log the inode core unconditionally in the unwritten extent conversion
    4300             :          * path because the conversion might not have done so (e.g., if the
    4301             :          * extent count hasn't changed). We need to make sure the inode is dirty
    4302             :          * in the transaction for the sake of fsync(), even if nothing has
    4303             :          * changed, because fsync() will not force the log for this transaction
    4304             :          * unless it sees the inode pinned.
    4305             :          *
    4306             :          * Note: If we're only converting cow fork extents, there aren't
    4307             :          * any on-disk updates to make, so we don't need to log anything.
    4308             :          */
    4309    56534042 :         if (whichfork != XFS_COW_FORK)
    4310    56534042 :                 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
    4311    56534042 :         if (error)
    4312             :                 return error;
    4313             : 
    4314             :         /*
    4315             :          * Update our extent pointer, given that
    4316             :          * xfs_bmap_add_extent_unwritten_real might have merged it into one
    4317             :          * of the neighbouring ones.
    4318             :          */
    4319    56533578 :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4320             : 
    4321             :         /*
    4322             :          * We may have combined previously unwritten space with written space,
    4323             :          * so generate another request.
    4324             :          */
    4325    56532708 :         if (mval->br_blockcount < len)
    4326     1362798 :                 return -EAGAIN;
    4327             :         return 0;
    4328             : }
    4329             : 
    4330             : xfs_extlen_t
    4331   205033061 : xfs_bmapi_minleft(
    4332             :         struct xfs_trans        *tp,
    4333             :         struct xfs_inode        *ip,
    4334             :         int                     fork)
    4335             : {
    4336   205033061 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, fork);
    4337             : 
    4338   205007510 :         if (tp && tp->t_highest_agno != NULLAGNUMBER)
    4339             :                 return 0;
    4340   204950214 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
    4341             :                 return 1;
    4342    61414722 :         return be16_to_cpu(ifp->if_broot->bb_level) + 1;
    4343             : }
    4344             : 
    4345             : /*
    4346             :  * Log whatever the flags say, even if error.  Otherwise we might miss detecting
    4347             :  * a case where the data is changed, there's an error, and it's not logged so we
    4348             :  * don't shutdown when we should.  Don't bother logging extents/btree changes if
    4349             :  * we converted to the other format.
    4350             :  */
    4351             : static void
    4352   204632824 : xfs_bmapi_finish(
    4353             :         struct xfs_bmalloca     *bma,
    4354             :         int                     whichfork,
    4355             :         int                     error)
    4356             : {
    4357   204632824 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4358             : 
    4359   213229700 :         if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
    4360   133709986 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    4361     1644087 :                 bma->logflags &= ~xfs_ilog_fext(whichfork);
    4362   211585652 :         else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
    4363           0 :                  ifp->if_format != XFS_DINODE_FMT_BTREE)
    4364           0 :                 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
    4365             : 
    4366   204615091 :         if (bma->logflags)
    4367   187325456 :                 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
    4368   204691319 :         if (bma->cur)
    4369    58857526 :                 xfs_btree_del_cursor(bma->cur, error);
    4370   204688386 : }
    4371             : 
    4372             : /*
    4373             :  * Map file blocks to filesystem blocks, and allocate blocks or convert the
    4374             :  * extent state if necessary.  Details behaviour is controlled by the flags
    4375             :  * parameter.  Only allocates blocks from a single allocation group, to avoid
    4376             :  * locking problems.
    4377             :  */
    4378             : int
    4379   177065444 : xfs_bmapi_write(
    4380             :         struct xfs_trans        *tp,            /* transaction pointer */
    4381             :         struct xfs_inode        *ip,            /* incore inode */
    4382             :         xfs_fileoff_t           bno,            /* starting file offs. mapped */
    4383             :         xfs_filblks_t           len,            /* length to map in file */
    4384             :         uint32_t                flags,          /* XFS_BMAPI_... */
    4385             :         xfs_extlen_t            total,          /* total blocks needed */
    4386             :         struct xfs_bmbt_irec    *mval,          /* output: map values */
    4387             :         int                     *nmap)          /* i/o: mval size/count */
    4388             : {
    4389   177065444 :         struct xfs_bmalloca     bma = {
    4390             :                 .tp             = tp,
    4391             :                 .ip             = ip,
    4392             :                 .total          = total,
    4393             :         };
    4394   177065444 :         struct xfs_mount        *mp = ip->i_mount;
    4395   177065444 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4396   177065444 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4397   176977399 :         xfs_fileoff_t           end;            /* end of mapped file region */
    4398   176977399 :         bool                    eof = false;    /* after the end of extents */
    4399   176977399 :         int                     error;          /* error return */
    4400   176977399 :         int                     n;              /* current extent index */
    4401   176977399 :         xfs_fileoff_t           obno;           /* old block number (offset) */
    4402             : 
    4403             : #ifdef DEBUG
    4404   176977399 :         xfs_fileoff_t           orig_bno;       /* original block number value */
    4405   176977399 :         int                     orig_flags;     /* original flags arg value */
    4406   176977399 :         xfs_filblks_t           orig_len;       /* original value of len arg */
    4407   176977399 :         struct xfs_bmbt_irec    *orig_mval;     /* original value of mval */
    4408   176977399 :         int                     orig_nmap;      /* original value of *nmap */
    4409             : 
    4410   176977399 :         orig_bno = bno;
    4411   176977399 :         orig_len = len;
    4412   176977399 :         orig_flags = flags;
    4413   176977399 :         orig_mval = mval;
    4414   176977399 :         orig_nmap = *nmap;
    4415             : #endif
    4416             : 
    4417   176977399 :         ASSERT(*nmap >= 1);
    4418   176977399 :         ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
    4419   176977399 :         ASSERT(tp != NULL);
    4420   176977399 :         ASSERT(len > 0);
    4421   176977399 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
    4422   176977399 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4423   177008770 :         ASSERT(!(flags & XFS_BMAPI_REMAP));
    4424             : 
    4425             :         /* zeroing is for currently only for data extents, not metadata */
    4426   177008770 :         ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
    4427             :                         (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
    4428             :         /*
    4429             :          * we can allocate unwritten extents or pre-zero allocated blocks,
    4430             :          * but it makes no sense to do both at once. This would result in
    4431             :          * zeroing the unwritten extent twice, but it still being an
    4432             :          * unwritten extent....
    4433             :          */
    4434   177008770 :         ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
    4435             :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
    4436             : 
    4437   354012870 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4438   177008770 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4439           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    4440           0 :                 return -EFSCORRUPTED;
    4441             :         }
    4442             : 
    4443   354008200 :         if (xfs_is_shutdown(mp))
    4444             :                 return -EIO;
    4445             : 
    4446   177004023 :         XFS_STATS_INC(mp, xs_blk_mapw);
    4447             : 
    4448   177054414 :         error = xfs_iread_extents(tp, ip, whichfork);
    4449   177017918 :         if (error)
    4450           0 :                 goto error0;
    4451             : 
    4452   177017918 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
    4453    82233876 :                 eof = true;
    4454   177040018 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4455    54305831 :                 bma.prev.br_startoff = NULLFILEOFF;
    4456   177010247 :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4457             : 
    4458   176991400 :         n = 0;
    4459   176991400 :         end = bno + len;
    4460   176991400 :         obno = bno;
    4461   178335322 :         while (bno < end && n < *nmap) {
    4462   178335322 :                 bool                    need_alloc = false, wasdelay = false;
    4463             : 
    4464             :                 /* in hole or beyond EOF? */
    4465   178335322 :                 if (eof || bma.got.br_startoff > bno) {
    4466             :                         /*
    4467             :                          * CoW fork conversions should /never/ hit EOF or
    4468             :                          * holes.  There should always be something for us
    4469             :                          * to work on.
    4470             :                          */
    4471   109914314 :                         ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
    4472             :                                  (flags & XFS_BMAPI_COWFORK)));
    4473             : 
    4474             :                         need_alloc = true;
    4475    68421008 :                 } else if (isnullstartblock(bma.got.br_startblock)) {
    4476      105974 :                         wasdelay = true;
    4477             :                 }
    4478             : 
    4479             :                 /*
    4480             :                  * First, deal with the hole before the allocated space
    4481             :                  * that we found, if any.
    4482             :                  */
    4483   178335322 :                 if (need_alloc || wasdelay) {
    4484   110020322 :                         bma.eof = eof;
    4485   110020322 :                         bma.conv = !!(flags & XFS_BMAPI_CONVERT);
    4486   110020322 :                         bma.wasdel = wasdelay;
    4487   110020322 :                         bma.offset = bno;
    4488   110020322 :                         bma.flags = flags;
    4489             : 
    4490             :                         /*
    4491             :                          * There's a 32/64 bit type mismatch between the
    4492             :                          * allocation length request (which can be 64 bits in
    4493             :                          * length) and the bma length request, which is
    4494             :                          * xfs_extlen_t and therefore 32 bits. Hence we have to
    4495             :                          * check for 32-bit overflows and handle them here.
    4496             :                          */
    4497   110020322 :                         if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
    4498       82435 :                                 bma.length = XFS_MAX_BMBT_EXTLEN;
    4499             :                         else
    4500   109937887 :                                 bma.length = len;
    4501             : 
    4502   110020322 :                         ASSERT(len > 0);
    4503   110020322 :                         ASSERT(bma.length > 0);
    4504   110020322 :                         error = xfs_bmapi_allocate(&bma);
    4505   110142767 :                         if (error)
    4506        2021 :                                 goto error0;
    4507   110140746 :                         if (bma.blkno == NULLFSBLOCK)
    4508             :                                 break;
    4509             : 
    4510             :                         /*
    4511             :                          * If this is a CoW allocation, record the data in
    4512             :                          * the refcount btree for orphan recovery.
    4513             :                          */
    4514   110140578 :                         if (whichfork == XFS_COW_FORK)
    4515      365790 :                                 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
    4516             :                                                 bma.length);
    4517             :                 }
    4518             : 
    4519             :                 /* Deal with the allocated space we found.  */
    4520   178455582 :                 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
    4521             :                                                         end, n, flags);
    4522             : 
    4523             :                 /* Execute unwritten extent conversion if necessary */
    4524   178414790 :                 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
    4525   178413332 :                 if (error == -EAGAIN)
    4526     1362806 :                         continue;
    4527   177050526 :                 if (error)
    4528           9 :                         goto error0;
    4529             : 
    4530             :                 /* update the extent map to return */
    4531   177050517 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    4532             : 
    4533             :                 /*
    4534             :                  * If we're done, stop now.  Stop when we've allocated
    4535             :                  * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise
    4536             :                  * the transaction may get too big.
    4537             :                  */
    4538   177047803 :                 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
    4539             :                         break;
    4540             : 
    4541             :                 /* Else go on to the next record. */
    4542         372 :                 bma.prev = bma.got;
    4543         372 :                 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
    4544          99 :                         eof = true;
    4545             :         }
    4546   177047599 :         *nmap = n;
    4547             : 
    4548   177047599 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4549             :                         whichfork);
    4550   177033466 :         if (error)
    4551           0 :                 goto error0;
    4552             : 
    4553   177033466 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
    4554             :                ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
    4555   177033466 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4556   177101941 :         xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
    4557             :                 orig_nmap, *nmap);
    4558   177101941 :         return 0;
    4559        2030 : error0:
    4560        2030 :         xfs_bmapi_finish(&bma, whichfork, error);
    4561        2030 :         return error;
    4562             : }
    4563             : 
    4564             : /*
    4565             :  * Convert an existing delalloc extent to real blocks based on file offset. This
    4566             :  * attempts to allocate the entire delalloc extent and may require multiple
    4567             :  * invocations to allocate the target offset if a large enough physical extent
    4568             :  * is not available.
    4569             :  */
    4570             : int
    4571    29155104 : xfs_bmapi_convert_delalloc(
    4572             :         struct xfs_inode        *ip,
    4573             :         int                     whichfork,
    4574             :         xfs_off_t               offset,
    4575             :         struct iomap            *iomap,
    4576             :         unsigned int            *seq)
    4577             : {
    4578    29155104 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4579    29154951 :         struct xfs_mount        *mp = ip->i_mount;
    4580    29154951 :         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
    4581    29154951 :         struct xfs_bmalloca     bma = { NULL };
    4582    29154951 :         uint16_t                flags = 0;
    4583    29154951 :         struct xfs_trans        *tp;
    4584    29154951 :         int                     error;
    4585             : 
    4586    29154951 :         if (whichfork == XFS_COW_FORK)
    4587     4960492 :                 flags |= IOMAP_F_SHARED;
    4588             : 
    4589             :         /*
    4590             :          * Space for the extent and indirect blocks was reserved when the
    4591             :          * delalloc extent was created so there's no need to do so here.
    4592             :          */
    4593    29154951 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
    4594             :                                 XFS_TRANS_RESERVE, &tp);
    4595    29154951 :         if (error)
    4596             :                 return error;
    4597             : 
    4598    29154959 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    4599    29154958 :         xfs_trans_ijoin(tp, ip, 0);
    4600             : 
    4601    29155100 :         error = xfs_iext_count_may_overflow(ip, whichfork,
    4602             :                         XFS_IEXT_ADD_NOSPLIT_CNT);
    4603    29154931 :         if (error == -EFBIG)
    4604           0 :                 error = xfs_iext_count_upgrade(tp, ip,
    4605             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
    4606    29154931 :         if (error)
    4607           0 :                 goto out_trans_cancel;
    4608             : 
    4609    29154931 :         if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
    4610    29155099 :             bma.got.br_startoff > offset_fsb) {
    4611             :                 /*
    4612             :                  * No extent found in the range we are trying to convert.  This
    4613             :                  * should only happen for the COW fork, where another thread
    4614             :                  * might have moved the extent to the data fork in the meantime.
    4615             :                  */
    4616           0 :                 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
    4617           0 :                 error = -EAGAIN;
    4618           0 :                 goto out_trans_cancel;
    4619             :         }
    4620             : 
    4621             :         /*
    4622             :          * If we find a real extent here we raced with another thread converting
    4623             :          * the extent.  Just return the real extent at this offset.
    4624             :          */
    4625    29155099 :         if (!isnullstartblock(bma.got.br_startblock)) {
    4626     1575813 :                 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4627             :                                 xfs_iomap_inode_sequence(ip, flags));
    4628     1575813 :                 *seq = READ_ONCE(ifp->if_seq);
    4629     1575813 :                 goto out_trans_cancel;
    4630             :         }
    4631             : 
    4632    27579286 :         bma.tp = tp;
    4633    27579286 :         bma.ip = ip;
    4634    27579286 :         bma.wasdel = true;
    4635    27579286 :         bma.offset = bma.got.br_startoff;
    4636    27579286 :         bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
    4637             :                         XFS_MAX_BMBT_EXTLEN);
    4638    27579286 :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4639             : 
    4640             :         /*
    4641             :          * When we're converting the delalloc reservations backing dirty pages
    4642             :          * in the page cache, we must be careful about how we create the new
    4643             :          * extents:
    4644             :          *
    4645             :          * New CoW fork extents are created unwritten, turned into real extents
    4646             :          * when we're about to write the data to disk, and mapped into the data
    4647             :          * fork after the write finishes.  End of story.
    4648             :          *
    4649             :          * New data fork extents must be mapped in as unwritten and converted
    4650             :          * to real extents after the write succeeds to avoid exposing stale
    4651             :          * disk contents if we crash.
    4652             :          */
    4653    27579148 :         bma.flags = XFS_BMAPI_PREALLOC;
    4654    27579148 :         if (whichfork == XFS_COW_FORK)
    4655     3384807 :                 bma.flags |= XFS_BMAPI_COWFORK;
    4656             : 
    4657    27579148 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4658     7243528 :                 bma.prev.br_startoff = NULLFILEOFF;
    4659             : 
    4660    27579111 :         error = xfs_bmapi_allocate(&bma);
    4661    27579168 :         if (error)
    4662        4422 :                 goto out_finish;
    4663             : 
    4664    27574746 :         error = -ENOSPC;
    4665    27574746 :         if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
    4666           0 :                 goto out_finish;
    4667    55149492 :         if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock))) {
    4668           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    4669           0 :                 error = -EFSCORRUPTED;
    4670           0 :                 goto out_finish;
    4671             :         }
    4672             : 
    4673    27574746 :         XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
    4674    27574694 :         XFS_STATS_INC(mp, xs_xstrat_quick);
    4675             : 
    4676    27574796 :         ASSERT(!isnullstartblock(bma.got.br_startblock));
    4677    27574796 :         xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4678             :                                 xfs_iomap_inode_sequence(ip, flags));
    4679    27574545 :         *seq = READ_ONCE(ifp->if_seq);
    4680             : 
    4681    27574545 :         if (whichfork == XFS_COW_FORK)
    4682     3384739 :                 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
    4683             : 
    4684    27574620 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4685             :                         whichfork);
    4686    27574218 :         if (error)
    4687           0 :                 goto out_finish;
    4688             : 
    4689    27574218 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4690    27574497 :         error = xfs_trans_commit(tp);
    4691    27574818 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4692    27574818 :         return error;
    4693             : 
    4694        4422 : out_finish:
    4695        4422 :         xfs_bmapi_finish(&bma, whichfork, error);
    4696     1580235 : out_trans_cancel:
    4697     1580235 :         xfs_trans_cancel(tp);
    4698     1580235 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4699     1580235 :         return error;
    4700             : }
    4701             : 
    4702             : int
    4703    95275952 : xfs_bmapi_remap(
    4704             :         struct xfs_trans        *tp,
    4705             :         struct xfs_inode        *ip,
    4706             :         xfs_fileoff_t           bno,
    4707             :         xfs_filblks_t           len,
    4708             :         xfs_fsblock_t           startblock,
    4709             :         uint32_t                flags)
    4710             : {
    4711    95275952 :         struct xfs_mount        *mp = ip->i_mount;
    4712    95275952 :         struct xfs_ifork        *ifp;
    4713    95275952 :         struct xfs_btree_cur    *cur = NULL;
    4714    95275952 :         struct xfs_bmbt_irec    got;
    4715    95275952 :         struct xfs_iext_cursor  icur;
    4716    95275952 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4717    95275952 :         int                     logflags = 0, error;
    4718             : 
    4719    95275952 :         ifp = xfs_ifork_ptr(ip, whichfork);
    4720    95275926 :         ASSERT(len > 0);
    4721    95275926 :         ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
    4722    95275926 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4723    95275940 :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
    4724             :                            XFS_BMAPI_NORMAP)));
    4725    95275940 :         ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
    4726             :                         (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
    4727             : 
    4728   190551874 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4729    95275940 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4730           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    4731           0 :                 return -EFSCORRUPTED;
    4732             :         }
    4733             : 
    4734   190551868 :         if (xfs_is_shutdown(mp))
    4735             :                 return -EIO;
    4736             : 
    4737    95275930 :         error = xfs_iread_extents(tp, ip, whichfork);
    4738    95275939 :         if (error)
    4739             :                 return error;
    4740             : 
    4741    95275944 :         if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
    4742             :                 /* make sure we only reflink into a hole. */
    4743    21866799 :                 ASSERT(got.br_startoff > bno);
    4744    21866799 :                 ASSERT(got.br_startoff - bno >= len);
    4745             :         }
    4746             : 
    4747    95275970 :         ip->i_nblocks += len;
    4748    95275970 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    4749             : 
    4750    95275983 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    4751    83394849 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    4752    83394845 :                 cur->bc_ino.flags = 0;
    4753             :         }
    4754             : 
    4755    95275979 :         got.br_startoff = bno;
    4756    95275979 :         got.br_startblock = startblock;
    4757    95275979 :         got.br_blockcount = len;
    4758    95275979 :         if (flags & XFS_BMAPI_PREALLOC)
    4759     1735944 :                 got.br_state = XFS_EXT_UNWRITTEN;
    4760             :         else
    4761    93540035 :                 got.br_state = XFS_EXT_NORM;
    4762             : 
    4763    95275979 :         error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
    4764             :                         &cur, &got, &logflags, flags);
    4765    95275938 :         if (error)
    4766           6 :                 goto error0;
    4767             : 
    4768    95275932 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
    4769             : 
    4770    95275903 : error0:
    4771    95275903 :         if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
    4772    84025300 :                 logflags &= ~XFS_ILOG_DEXT;
    4773    11250603 :         else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
    4774    11250603 :                 logflags &= ~XFS_ILOG_DBROOT;
    4775             : 
    4776    95275903 :         if (logflags)
    4777    90494033 :                 xfs_trans_log_inode(tp, ip, logflags);
    4778    95275984 :         if (cur)
    4779    84029854 :                 xfs_btree_del_cursor(cur, error);
    4780             :         return error;
    4781             : }
    4782             : 
    4783             : /*
    4784             :  * When a delalloc extent is split (e.g., due to a hole punch), the original
    4785             :  * indlen reservation must be shared across the two new extents that are left
    4786             :  * behind.
    4787             :  *
    4788             :  * Given the original reservation and the worst case indlen for the two new
    4789             :  * extents (as calculated by xfs_bmap_worst_indlen()), split the original
    4790             :  * reservation fairly across the two new extents. If necessary, steal available
    4791             :  * blocks from a deleted extent to make up a reservation deficiency (e.g., if
    4792             :  * ores == 1). The number of stolen blocks is returned. The availability and
    4793             :  * subsequent accounting of stolen blocks is the responsibility of the caller.
    4794             :  */
    4795             : static xfs_filblks_t
    4796       26631 : xfs_bmap_split_indlen(
    4797             :         xfs_filblks_t                   ores,           /* original res. */
    4798             :         xfs_filblks_t                   *indlen1,       /* ext1 worst indlen */
    4799             :         xfs_filblks_t                   *indlen2,       /* ext2 worst indlen */
    4800             :         xfs_filblks_t                   avail)          /* stealable blocks */
    4801             : {
    4802       26631 :         xfs_filblks_t                   len1 = *indlen1;
    4803       26631 :         xfs_filblks_t                   len2 = *indlen2;
    4804       26631 :         xfs_filblks_t                   nres = len1 + len2; /* new total res. */
    4805       26631 :         xfs_filblks_t                   stolen = 0;
    4806       26631 :         xfs_filblks_t                   resfactor;
    4807             : 
    4808             :         /*
    4809             :          * Steal as many blocks as we can to try and satisfy the worst case
    4810             :          * indlen for both new extents.
    4811             :          */
    4812       26631 :         if (ores < nres && avail)
    4813       26631 :                 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
    4814       26631 :         ores += stolen;
    4815             : 
    4816             :          /* nothing else to do if we've satisfied the new reservation */
    4817       26631 :         if (ores >= nres)
    4818             :                 return stolen;
    4819             : 
    4820             :         /*
    4821             :          * We can't meet the total required reservation for the two extents.
    4822             :          * Calculate the percent of the overall shortage between both extents
    4823             :          * and apply this percentage to each of the requested indlen values.
    4824             :          * This distributes the shortage fairly and reduces the chances that one
    4825             :          * of the two extents is left with nothing when extents are repeatedly
    4826             :          * split.
    4827             :          */
    4828       17682 :         resfactor = (ores * 100);
    4829       17682 :         do_div(resfactor, nres);
    4830       17682 :         len1 *= resfactor;
    4831       17682 :         do_div(len1, 100);
    4832       17682 :         len2 *= resfactor;
    4833       17682 :         do_div(len2, 100);
    4834       17682 :         ASSERT(len1 + len2 <= ores);
    4835       17682 :         ASSERT(len1 < *indlen1 && len2 < *indlen2);
    4836             : 
    4837             :         /*
    4838             :          * Hand out the remainder to each extent. If one of the two reservations
    4839             :          * is zero, we want to make sure that one gets a block first. The loop
    4840             :          * below starts with len1, so hand len2 a block right off the bat if it
    4841             :          * is zero.
    4842             :          */
    4843       17682 :         ores -= (len1 + len2);
    4844       17682 :         ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
    4845       17682 :         if (ores && !len2 && *indlen2) {
    4846           2 :                 len2++;
    4847           2 :                 ores--;
    4848             :         }
    4849       27310 :         while (ores) {
    4850       17682 :                 if (len1 < *indlen1) {
    4851       17682 :                         len1++;
    4852       17682 :                         ores--;
    4853             :                 }
    4854       17682 :                 if (!ores)
    4855             :                         break;
    4856        9628 :                 if (len2 < *indlen2) {
    4857        9628 :                         len2++;
    4858        9628 :                         ores--;
    4859             :                 }
    4860             :         }
    4861             : 
    4862       17682 :         *indlen1 = len1;
    4863       17682 :         *indlen2 = len2;
    4864             : 
    4865       17682 :         return stolen;
    4866             : }
    4867             : 
    4868             : int
    4869     8878452 : xfs_bmap_del_extent_delay(
    4870             :         struct xfs_inode        *ip,
    4871             :         int                     whichfork,
    4872             :         struct xfs_iext_cursor  *icur,
    4873             :         struct xfs_bmbt_irec    *got,
    4874             :         struct xfs_bmbt_irec    *del)
    4875             : {
    4876     8878452 :         struct xfs_mount        *mp = ip->i_mount;
    4877     8878452 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4878     8849148 :         struct xfs_bmbt_irec    new;
    4879     8849148 :         int64_t                 da_old, da_new, da_diff = 0;
    4880     8849148 :         xfs_fileoff_t           del_endoff, got_endoff;
    4881     8849148 :         xfs_filblks_t           got_indlen, new_indlen, stolen;
    4882     8849148 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    4883     8849148 :         int                     error = 0;
    4884     8849148 :         bool                    isrt;
    4885             : 
    4886     8849148 :         XFS_STATS_INC(mp, xs_del_exlist);
    4887             : 
    4888     8852594 :         isrt = xfs_ifork_is_realtime(ip, whichfork);
    4889     8858099 :         del_endoff = del->br_startoff + del->br_blockcount;
    4890     8858099 :         got_endoff = got->br_startoff + got->br_blockcount;
    4891     8858099 :         da_old = startblockval(got->br_startblock);
    4892     8858099 :         da_new = 0;
    4893             : 
    4894     8858099 :         ASSERT(del->br_blockcount > 0);
    4895     8858099 :         ASSERT(got->br_startoff <= del->br_startoff);
    4896     8858099 :         ASSERT(got_endoff >= del_endoff);
    4897             : 
    4898     8858099 :         if (isrt) {
    4899           0 :                 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
    4900             : 
    4901           0 :                 do_div(rtexts, mp->m_sb.sb_rextsize);
    4902           0 :                 xfs_mod_frextents(mp, rtexts);
    4903             :         }
    4904             : 
    4905             :         /*
    4906             :          * Update the inode delalloc counter now and wait to update the
    4907             :          * sb counters as we might have to borrow some blocks for the
    4908             :          * indirect block accounting.
    4909             :          */
    4910     8858099 :         ASSERT(!isrt);
    4911     8858099 :         error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
    4912     8910526 :         if (error)
    4913             :                 return error;
    4914     8910526 :         ip->i_delayed_blks -= del->br_blockcount;
    4915             : 
    4916     8910526 :         if (got->br_startoff == del->br_startoff)
    4917     6234119 :                 state |= BMAP_LEFT_FILLING;
    4918     8910526 :         if (got_endoff == del_endoff)
    4919     8856686 :                 state |= BMAP_RIGHT_FILLING;
    4920             : 
    4921     8910526 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    4922     6204913 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    4923             :                 /*
    4924             :                  * Matches the whole extent.  Delete the entry.
    4925             :                  */
    4926     6204913 :                 xfs_iext_remove(ip, icur, state);
    4927     6194528 :                 xfs_iext_prev(ifp, icur);
    4928     6194528 :                 break;
    4929       28176 :         case BMAP_LEFT_FILLING:
    4930             :                 /*
    4931             :                  * Deleting the first part of the extent.
    4932             :                  */
    4933       28176 :                 got->br_startoff = del_endoff;
    4934       28176 :                 got->br_blockcount -= del->br_blockcount;
    4935       28176 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4936             :                                 got->br_blockcount), da_old);
    4937       28176 :                 got->br_startblock = nullstartblock((int)da_new);
    4938       28176 :                 xfs_iext_update_extent(ip, state, icur, got);
    4939       28176 :                 break;
    4940     2650806 :         case BMAP_RIGHT_FILLING:
    4941             :                 /*
    4942             :                  * Deleting the last part of the extent.
    4943             :                  */
    4944     2650806 :                 got->br_blockcount = got->br_blockcount - del->br_blockcount;
    4945     2650806 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4946             :                                 got->br_blockcount), da_old);
    4947     2648647 :                 got->br_startblock = nullstartblock((int)da_new);
    4948     2646823 :                 xfs_iext_update_extent(ip, state, icur, got);
    4949     2646823 :                 break;
    4950       26631 :         case 0:
    4951             :                 /*
    4952             :                  * Deleting the middle of the extent.
    4953             :                  *
    4954             :                  * Distribute the original indlen reservation across the two new
    4955             :                  * extents.  Steal blocks from the deleted extent if necessary.
    4956             :                  * Stealing blocks simply fudges the fdblocks accounting below.
    4957             :                  * Warn if either of the new indlen reservations is zero as this
    4958             :                  * can lead to delalloc problems.
    4959             :                  */
    4960       26631 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    4961       26631 :                 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
    4962             : 
    4963       26631 :                 new.br_blockcount = got_endoff - del_endoff;
    4964       26631 :                 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
    4965             : 
    4966       26631 :                 WARN_ON_ONCE(!got_indlen || !new_indlen);
    4967       26631 :                 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
    4968             :                                                        del->br_blockcount);
    4969             : 
    4970       26631 :                 got->br_startblock = nullstartblock((int)got_indlen);
    4971             : 
    4972       26631 :                 new.br_startoff = del_endoff;
    4973       26631 :                 new.br_state = got->br_state;
    4974       26631 :                 new.br_startblock = nullstartblock((int)new_indlen);
    4975             : 
    4976       26631 :                 xfs_iext_update_extent(ip, state, icur, got);
    4977       26631 :                 xfs_iext_next(ifp, icur);
    4978       26631 :                 xfs_iext_insert(ip, icur, &new, state);
    4979             : 
    4980       26631 :                 da_new = got_indlen + new_indlen - stolen;
    4981       26631 :                 del->br_blockcount -= stolen;
    4982       26631 :                 break;
    4983             :         }
    4984             : 
    4985     8864519 :         ASSERT(da_old >= da_new);
    4986     8864519 :         da_diff = da_old - da_new;
    4987     8864519 :         if (!isrt)
    4988     8866729 :                 da_diff += del->br_blockcount;
    4989     8864519 :         if (da_diff) {
    4990     8845671 :                 xfs_mod_fdblocks(mp, da_diff, false);
    4991     8871174 :                 xfs_mod_delalloc(mp, -da_diff);
    4992             :         }
    4993             :         return error;
    4994             : }
    4995             : 
    4996             : void
    4997    15254732 : xfs_bmap_del_extent_cow(
    4998             :         struct xfs_inode        *ip,
    4999             :         struct xfs_iext_cursor  *icur,
    5000             :         struct xfs_bmbt_irec    *got,
    5001             :         struct xfs_bmbt_irec    *del)
    5002             : {
    5003    15254732 :         struct xfs_mount        *mp = ip->i_mount;
    5004    15254732 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
    5005    15254732 :         struct xfs_bmbt_irec    new;
    5006    15254732 :         xfs_fileoff_t           del_endoff, got_endoff;
    5007    15254732 :         uint32_t                state = BMAP_COWFORK;
    5008             : 
    5009    15254732 :         XFS_STATS_INC(mp, xs_del_exlist);
    5010             : 
    5011    15252985 :         del_endoff = del->br_startoff + del->br_blockcount;
    5012    15252985 :         got_endoff = got->br_startoff + got->br_blockcount;
    5013             : 
    5014    15252985 :         ASSERT(del->br_blockcount > 0);
    5015    15252985 :         ASSERT(got->br_startoff <= del->br_startoff);
    5016    15252985 :         ASSERT(got_endoff >= del_endoff);
    5017    15252985 :         ASSERT(!isnullstartblock(got->br_startblock));
    5018             : 
    5019    15252985 :         if (got->br_startoff == del->br_startoff)
    5020    14780154 :                 state |= BMAP_LEFT_FILLING;
    5021    15252985 :         if (got_endoff == del_endoff)
    5022    13319050 :                 state |= BMAP_RIGHT_FILLING;
    5023             : 
    5024    15252985 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5025    12889057 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5026             :                 /*
    5027             :                  * Matches the whole extent.  Delete the entry.
    5028             :                  */
    5029    12889057 :                 xfs_iext_remove(ip, icur, state);
    5030    12890837 :                 xfs_iext_prev(ifp, icur);
    5031    12890837 :                 break;
    5032     1890820 :         case BMAP_LEFT_FILLING:
    5033             :                 /*
    5034             :                  * Deleting the first part of the extent.
    5035             :                  */
    5036     1890820 :                 got->br_startoff = del_endoff;
    5037     1890820 :                 got->br_blockcount -= del->br_blockcount;
    5038     1890820 :                 got->br_startblock = del->br_startblock + del->br_blockcount;
    5039     1890820 :                 xfs_iext_update_extent(ip, state, icur, got);
    5040     1890820 :                 break;
    5041      429860 :         case BMAP_RIGHT_FILLING:
    5042             :                 /*
    5043             :                  * Deleting the last part of the extent.
    5044             :                  */
    5045      429860 :                 got->br_blockcount -= del->br_blockcount;
    5046      429860 :                 xfs_iext_update_extent(ip, state, icur, got);
    5047      429860 :                 break;
    5048       43248 :         case 0:
    5049             :                 /*
    5050             :                  * Deleting the middle of the extent.
    5051             :                  */
    5052       43248 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    5053             : 
    5054       43248 :                 new.br_startoff = del_endoff;
    5055       43248 :                 new.br_blockcount = got_endoff - del_endoff;
    5056       43248 :                 new.br_state = got->br_state;
    5057       43248 :                 new.br_startblock = del->br_startblock + del->br_blockcount;
    5058             : 
    5059       43248 :                 xfs_iext_update_extent(ip, state, icur, got);
    5060       43248 :                 xfs_iext_next(ifp, icur);
    5061       43248 :                 xfs_iext_insert(ip, icur, &new, state);
    5062       43248 :                 break;
    5063             :         }
    5064    15256192 :         ip->i_delayed_blks -= del->br_blockcount;
    5065    15256192 : }
    5066             : 
    5067             : /*
    5068             :  * Called by xfs_bmapi to update file extent records and the btree
    5069             :  * after removing space.
    5070             :  */
    5071             : STATIC int                              /* error */
    5072   181938508 : xfs_bmap_del_extent_real(
    5073             :         xfs_inode_t             *ip,    /* incore inode pointer */
    5074             :         xfs_trans_t             *tp,    /* current transaction pointer */
    5075             :         struct xfs_iext_cursor  *icur,
    5076             :         struct xfs_btree_cur    *cur,   /* if null, not a btree */
    5077             :         xfs_bmbt_irec_t         *del,   /* data to remove from extents */
    5078             :         int                     *logflagsp, /* inode logging flags */
    5079             :         int                     whichfork, /* data or attr fork */
    5080             :         uint32_t                bflags) /* bmapi flags */
    5081             : {
    5082   181938508 :         xfs_fsblock_t           del_endblock=0; /* first block past del */
    5083   181938508 :         xfs_fileoff_t           del_endoff;     /* first offset past del */
    5084   181938508 :         int                     do_fx;  /* free extent at end of routine */
    5085   181938508 :         int                     error;  /* error return value */
    5086   181938508 :         int                     flags = 0;/* inode logging flags */
    5087   181938508 :         struct xfs_bmbt_irec    got;    /* current extent entry */
    5088   181938508 :         xfs_fileoff_t           got_endoff;     /* first offset past got */
    5089   181938508 :         int                     i;      /* temp state */
    5090   181938508 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    5091   181938508 :         xfs_mount_t             *mp;    /* mount structure */
    5092   181938508 :         xfs_filblks_t           nblks;  /* quota/sb block count */
    5093   181938508 :         xfs_bmbt_irec_t         new;    /* new record to be inserted */
    5094             :         /* REFERENCED */
    5095   181938508 :         uint                    qfield; /* quota field to update */
    5096   181938508 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    5097   181938508 :         struct xfs_bmbt_irec    old;
    5098             : 
    5099   181938508 :         mp = ip->i_mount;
    5100   181938508 :         XFS_STATS_INC(mp, xs_del_exlist);
    5101             : 
    5102   181916350 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5103   181926321 :         ASSERT(del->br_blockcount > 0);
    5104   181926321 :         xfs_iext_get_extent(ifp, icur, &got);
    5105   181949584 :         ASSERT(got.br_startoff <= del->br_startoff);
    5106   181949584 :         del_endoff = del->br_startoff + del->br_blockcount;
    5107   181949584 :         got_endoff = got.br_startoff + got.br_blockcount;
    5108   181949584 :         ASSERT(got_endoff >= del_endoff);
    5109   181949584 :         ASSERT(!isnullstartblock(got.br_startblock));
    5110   181949584 :         qfield = 0;
    5111   181949584 :         error = 0;
    5112             : 
    5113             :         /*
    5114             :          * If it's the case where the directory code is running with no block
    5115             :          * reservation, and the deleted block is in the middle of its extent,
    5116             :          * and the resulting insert of an extent would cause transformation to
    5117             :          * btree format, then reject it.  The calling code will then swap blocks
    5118             :          * around instead.  We have to do this now, rather than waiting for the
    5119             :          * conversion to btree format, since the transaction will be dirty then.
    5120             :          */
    5121   181949584 :         if (tp->t_blk_res == 0 &&
    5122   102670639 :             ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
    5123    48386540 :             ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
    5124      587393 :             del->br_startoff > got.br_startoff && del_endoff < got_endoff)
    5125             :                 return -ENOSPC;
    5126             : 
    5127   181949584 :         flags = XFS_ILOG_CORE;
    5128   181949584 :         if (xfs_ifork_is_realtime(ip, whichfork)) {
    5129    34843418 :                 if (!(bflags & XFS_BMAPI_REMAP)) {
    5130    34843418 :                         error = xfs_rtfree_blocks(tp, del->br_startblock,
    5131             :                                         del->br_blockcount);
    5132    34843418 :                         if (error)
    5133           1 :                                 goto done;
    5134             :                 }
    5135             :                 do_fx = 0;
    5136             :                 qfield = XFS_TRANS_DQ_RTBCOUNT;
    5137             :         } else {
    5138             :                 do_fx = 1;
    5139             :                 qfield = XFS_TRANS_DQ_BCOUNT;
    5140             :         }
    5141   181898067 :         nblks = del->br_blockcount;
    5142             : 
    5143   181898067 :         del_endblock = del->br_startblock + del->br_blockcount;
    5144   181898067 :         if (cur) {
    5145   110732054 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5146   110732657 :                 if (error)
    5147           4 :                         goto done;
    5148   110732653 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5149           0 :                         xfs_btree_mark_sick(cur);
    5150           0 :                         error = -EFSCORRUPTED;
    5151           0 :                         goto done;
    5152             :                 }
    5153             :         }
    5154             : 
    5155   181898666 :         if (got.br_startoff == del->br_startoff)
    5156   135006648 :                 state |= BMAP_LEFT_FILLING;
    5157   181898666 :         if (got_endoff == del_endoff)
    5158   132758610 :                 state |= BMAP_RIGHT_FILLING;
    5159             : 
    5160   181898666 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5161   124514751 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5162             :                 /*
    5163             :                  * Matches the whole extent.  Delete the entry.
    5164             :                  */
    5165   124514751 :                 xfs_iext_remove(ip, icur, state);
    5166   124587939 :                 xfs_iext_prev(ifp, icur);
    5167   124558956 :                 ifp->if_nextents--;
    5168             : 
    5169   124558956 :                 flags |= XFS_ILOG_CORE;
    5170   124558956 :                 if (!cur) {
    5171    58108160 :                         flags |= xfs_ilog_fext(whichfork);
    5172    58108160 :                         break;
    5173             :                 }
    5174    66450796 :                 if ((error = xfs_btree_delete(cur, &i)))
    5175           0 :                         goto done;
    5176    66450326 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5177           0 :                         xfs_btree_mark_sick(cur);
    5178           0 :                         error = -EFSCORRUPTED;
    5179           0 :                         goto done;
    5180             :                 }
    5181             :                 break;
    5182    10465759 :         case BMAP_LEFT_FILLING:
    5183             :                 /*
    5184             :                  * Deleting the first part of the extent.
    5185             :                  */
    5186    10465759 :                 got.br_startoff = del_endoff;
    5187    10465759 :                 got.br_startblock = del_endblock;
    5188    10465759 :                 got.br_blockcount -= del->br_blockcount;
    5189    10465759 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5190    10465766 :                 if (!cur) {
    5191     4770451 :                         flags |= xfs_ilog_fext(whichfork);
    5192     4770451 :                         break;
    5193             :                 }
    5194     5695315 :                 error = xfs_bmbt_update(cur, &got);
    5195     5695300 :                 if (error)
    5196           0 :                         goto done;
    5197             :                 break;
    5198     8231443 :         case BMAP_RIGHT_FILLING:
    5199             :                 /*
    5200             :                  * Deleting the last part of the extent.
    5201             :                  */
    5202     8231443 :                 got.br_blockcount -= del->br_blockcount;
    5203     8231443 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5204     8231439 :                 if (!cur) {
    5205     4753504 :                         flags |= xfs_ilog_fext(whichfork);
    5206     4753504 :                         break;
    5207             :                 }
    5208     3477935 :                 error = xfs_bmbt_update(cur, &got);
    5209     3477908 :                 if (error)
    5210           0 :                         goto done;
    5211             :                 break;
    5212    38686713 :         case 0:
    5213             :                 /*
    5214             :                  * Deleting the middle of the extent.
    5215             :                  */
    5216             : 
    5217    38686713 :                 old = got;
    5218             : 
    5219    38686713 :                 got.br_blockcount = del->br_startoff - got.br_startoff;
    5220    38686713 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5221             : 
    5222    38686616 :                 new.br_startoff = del_endoff;
    5223    38686616 :                 new.br_blockcount = got_endoff - del_endoff;
    5224    38686616 :                 new.br_state = got.br_state;
    5225    38686616 :                 new.br_startblock = del_endblock;
    5226             : 
    5227    38686616 :                 flags |= XFS_ILOG_CORE;
    5228    38686616 :                 if (cur) {
    5229    35108360 :                         error = xfs_bmbt_update(cur, &got);
    5230    35108034 :                         if (error)
    5231           0 :                                 goto done;
    5232    35108034 :                         error = xfs_btree_increment(cur, 0, &i);
    5233    35108128 :                         if (error)
    5234           0 :                                 goto done;
    5235    35108128 :                         cur->bc_rec.b = new;
    5236    35108128 :                         error = xfs_btree_insert(cur, &i);
    5237    35108061 :                         if (error && error != -ENOSPC)
    5238           0 :                                 goto done;
    5239             :                         /*
    5240             :                          * If get no-space back from btree insert, it tried a
    5241             :                          * split, and we have a zero block reservation.  Fix up
    5242             :                          * our state and return the error.
    5243             :                          */
    5244    35108061 :                         if (error == -ENOSPC) {
    5245             :                                 /*
    5246             :                                  * Reset the cursor, don't trust it after any
    5247             :                                  * insert operation.
    5248             :                                  */
    5249           0 :                                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5250           0 :                                 if (error)
    5251           0 :                                         goto done;
    5252           0 :                                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5253           0 :                                         xfs_btree_mark_sick(cur);
    5254           0 :                                         error = -EFSCORRUPTED;
    5255           0 :                                         goto done;
    5256             :                                 }
    5257             :                                 /*
    5258             :                                  * Update the btree record back
    5259             :                                  * to the original value.
    5260             :                                  */
    5261           0 :                                 error = xfs_bmbt_update(cur, &old);
    5262           0 :                                 if (error)
    5263           0 :                                         goto done;
    5264             :                                 /*
    5265             :                                  * Reset the extent record back
    5266             :                                  * to the original value.
    5267             :                                  */
    5268           0 :                                 xfs_iext_update_extent(ip, state, icur, &old);
    5269           0 :                                 flags = 0;
    5270           0 :                                 error = -ENOSPC;
    5271           0 :                                 goto done;
    5272             :                         }
    5273    35108061 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5274           0 :                                 xfs_btree_mark_sick(cur);
    5275           0 :                                 error = -EFSCORRUPTED;
    5276           0 :                                 goto done;
    5277             :                         }
    5278             :                 } else
    5279     3578314 :                         flags |= xfs_ilog_fext(whichfork);
    5280             : 
    5281    38686317 :                 ifp->if_nextents++;
    5282    38686317 :                 xfs_iext_next(ifp, icur);
    5283    38686515 :                 xfs_iext_insert(ip, icur, &new, state);
    5284    38686515 :                 break;
    5285             :         }
    5286             : 
    5287             :         /* remove reverse mapping */
    5288   181942215 :         xfs_rmap_unmap_extent(tp, ip, whichfork, del);
    5289             : 
    5290             :         /*
    5291             :          * If we need to, add to list of extents to delete.
    5292             :          */
    5293   181909707 :         if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
    5294   115917760 :                 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
    5295    71228679 :                         xfs_refcount_decrease_extent(tp, del);
    5296             :                 } else {
    5297    44689081 :                         error = __xfs_free_extent_later(tp, del->br_startblock,
    5298             :                                         del->br_blockcount, NULL,
    5299             :                                         XFS_AG_RESV_NONE,
    5300    44689081 :                                         ((bflags & XFS_BMAPI_NODISCARD) ||
    5301    44653557 :                                         del->br_state == XFS_EXT_UNWRITTEN));
    5302    44760815 :                         if (error)
    5303           0 :                                 goto done;
    5304             :                 }
    5305             :         }
    5306             : 
    5307             :         /*
    5308             :          * Adjust inode # blocks in the file.
    5309             :          */
    5310   181983103 :         if (nblks)
    5311   181990184 :                 ip->i_nblocks -= nblks;
    5312             :         /*
    5313             :          * Adjust quota data.
    5314             :          */
    5315   181983103 :         if (qfield && !(bflags & XFS_BMAPI_REMAP))
    5316   150827383 :                 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
    5317             : 
    5318    31155720 : done:
    5319   181948359 :         *logflagsp = flags;
    5320   181948359 :         return error;
    5321             : }
    5322             : 
    5323             : /*
    5324             :  * Unmap (remove) blocks from a file.
    5325             :  * If nexts is nonzero then the number of extents to remove is limited to
    5326             :  * that value.  If not all extents in the block range can be removed then
    5327             :  * *done is set.
    5328             :  */
    5329             : int                                             /* error */
    5330   171792244 : __xfs_bunmapi(
    5331             :         struct xfs_trans        *tp,            /* transaction pointer */
    5332             :         struct xfs_inode        *ip,            /* incore inode */
    5333             :         xfs_fileoff_t           start,          /* first file offset deleted */
    5334             :         xfs_filblks_t           *rlen,          /* i/o: amount remaining */
    5335             :         uint32_t                flags,          /* misc flags */
    5336             :         xfs_extnum_t            nexts)          /* number of extents max */
    5337             : {
    5338   171792244 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
    5339   171792244 :         struct xfs_bmbt_irec    del;            /* extent being deleted */
    5340   171792244 :         int                     error;          /* error return value */
    5341   171792244 :         xfs_extnum_t            extno;          /* extent number in list */
    5342   171792244 :         struct xfs_bmbt_irec    got;            /* current extent record */
    5343   171792244 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
    5344   171792244 :         int                     isrt;           /* freeing in rt area */
    5345   171792244 :         int                     logflags;       /* transaction logging flags */
    5346   171792244 :         xfs_extlen_t            mod;            /* rt extent offset */
    5347   171792244 :         struct xfs_mount        *mp = ip->i_mount;
    5348   171792244 :         int                     tmp_logflags;   /* partial logging flags */
    5349   171792244 :         int                     wasdel;         /* was a delayed alloc extent */
    5350   171792244 :         int                     whichfork;      /* data or attribute fork */
    5351   171792244 :         xfs_fsblock_t           sum;
    5352   171792244 :         xfs_filblks_t           len = *rlen;    /* length to unmap in file */
    5353   171792244 :         xfs_fileoff_t           end;
    5354   171792244 :         struct xfs_iext_cursor  icur;
    5355   171792244 :         bool                    done = false;
    5356             : 
    5357   171792244 :         trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
    5358             : 
    5359   171655807 :         whichfork = xfs_bmapi_whichfork(flags);
    5360   171655807 :         ASSERT(whichfork != XFS_COW_FORK);
    5361   171655807 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5362   171712480 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
    5363           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5364           0 :                 return -EFSCORRUPTED;
    5365             :         }
    5366   343424960 :         if (xfs_is_shutdown(mp))
    5367             :                 return -EIO;
    5368             : 
    5369   171712223 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5370   171700356 :         ASSERT(len > 0);
    5371   171700356 :         ASSERT(nexts >= 0);
    5372             : 
    5373   171700356 :         error = xfs_iread_extents(tp, ip, whichfork);
    5374   171770987 :         if (error)
    5375             :                 return error;
    5376             : 
    5377   171772488 :         if (xfs_iext_count(ifp) == 0) {
    5378     4075118 :                 *rlen = 0;
    5379     4075118 :                 return 0;
    5380             :         }
    5381   167658650 :         XFS_STATS_INC(mp, xs_blk_unmap);
    5382   167704513 :         isrt = xfs_ifork_is_realtime(ip, whichfork);
    5383   167636706 :         end = start + len;
    5384             : 
    5385   167636706 :         if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
    5386      282200 :                 *rlen = 0;
    5387      282200 :                 return 0;
    5388             :         }
    5389   167308150 :         end--;
    5390             : 
    5391   167308150 :         logflags = 0;
    5392   167308150 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5393    84703061 :                 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
    5394    84703061 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5395    84703487 :                 cur->bc_ino.flags = 0;
    5396             :         } else
    5397    82605089 :                 cur = NULL;
    5398             : 
    5399   167308576 :         if (isrt) {
    5400             :                 /*
    5401             :                  * Synchronize by locking the bitmap inode.
    5402             :                  */
    5403    31414441 :                 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
    5404    31419878 :                 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
    5405    31419878 :                 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
    5406    31419878 :                 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
    5407             :         }
    5408             : 
    5409             :         extno = 0;
    5410   355704709 :         while (end != (xfs_fileoff_t)-1 && end >= start &&
    5411             :                (nexts == 0 || extno < nexts)) {
    5412             :                 /*
    5413             :                  * Is the found extent after a hole in which end lives?
    5414             :                  * Just back up to the previous extent, if so.
    5415             :                  */
    5416   192477694 :                 if (got.br_startoff > end &&
    5417           0 :                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5418             :                         done = true;
    5419             :                         break;
    5420             :                 }
    5421             :                 /*
    5422             :                  * Is the last block of this extent before the range
    5423             :                  * we're supposed to delete?  If so, we're done.
    5424             :                  */
    5425   192477694 :                 end = XFS_FILEOFF_MIN(end,
    5426             :                         got.br_startoff + got.br_blockcount - 1);
    5427   192477694 :                 if (end < start)
    5428             :                         break;
    5429             :                 /*
    5430             :                  * Then deal with the (possibly delayed) allocated space
    5431             :                  * we found.
    5432             :                  */
    5433   190235757 :                 del = got;
    5434   190235757 :                 wasdel = isnullstartblock(del.br_startblock);
    5435             : 
    5436   190235757 :                 if (got.br_startoff < start) {
    5437    47669768 :                         del.br_startoff = start;
    5438    47669768 :                         del.br_blockcount -= start - got.br_startoff;
    5439    47669768 :                         if (!wasdel)
    5440    46689845 :                                 del.br_startblock += start - got.br_startoff;
    5441             :                 }
    5442   190235757 :                 if (del.br_startoff + del.br_blockcount > end + 1)
    5443    49213174 :                         del.br_blockcount = end + 1 - del.br_startoff;
    5444             : 
    5445   190235757 :                 if (!isrt || (flags & XFS_BMAPI_REMAP))
    5446   153196019 :                         goto delete;
    5447             : 
    5448    37039738 :                 sum = del.br_startblock + del.br_blockcount;
    5449    37039738 :                 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
    5450    37039738 :                 if (mod) {
    5451             :                         /*
    5452             :                          * Realtime extent not lined up at the end.
    5453             :                          * The extent could have been split into written
    5454             :                          * and unwritten pieces, or we could just be
    5455             :                          * unmapping part of it.  But we can't really
    5456             :                          * get rid of part of a realtime extent.
    5457             :                          */
    5458           0 :                         if (del.br_state == XFS_EXT_UNWRITTEN) {
    5459             :                                 /*
    5460             :                                  * This piece is unwritten, or we're not
    5461             :                                  * using unwritten extents.  Skip over it.
    5462             :                                  */
    5463           0 :                                 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
    5464           0 :                                 end -= mod > del.br_blockcount ?
    5465           0 :                                         del.br_blockcount : mod;
    5466           0 :                                 if (end < got.br_startoff &&
    5467           0 :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5468             :                                         done = true;
    5469             :                                         break;
    5470             :                                 }
    5471           0 :                                 continue;
    5472             :                         }
    5473             :                         /*
    5474             :                          * It's written, turn it unwritten.
    5475             :                          * This is better than zeroing it.
    5476             :                          */
    5477           0 :                         ASSERT(del.br_state == XFS_EXT_NORM);
    5478           0 :                         ASSERT(tp->t_blk_res > 0);
    5479             :                         /*
    5480             :                          * If this spans a realtime extent boundary,
    5481             :                          * chop it back to the start of the one we end at.
    5482             :                          */
    5483           0 :                         if (del.br_blockcount > mod) {
    5484           0 :                                 del.br_startoff += del.br_blockcount - mod;
    5485           0 :                                 del.br_startblock += del.br_blockcount - mod;
    5486           0 :                                 del.br_blockcount = mod;
    5487             :                         }
    5488           0 :                         del.br_state = XFS_EXT_UNWRITTEN;
    5489           0 :                         error = xfs_bmap_add_extent_unwritten_real(tp, ip,
    5490             :                                         whichfork, &icur, &cur, &del,
    5491             :                                         &logflags);
    5492           0 :                         if (error)
    5493           0 :                                 goto error0;
    5494           0 :                         goto nodelete;
    5495             :                 }
    5496    37039738 :                 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
    5497    37039738 :                 if (mod) {
    5498     2699317 :                         xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
    5499             : 
    5500             :                         /*
    5501             :                          * Realtime extent is lined up at the end but not
    5502             :                          * at the front.  We'll get rid of full extents if
    5503             :                          * we can.
    5504             :                          */
    5505     2699317 :                         if (del.br_blockcount > off) {
    5506      502997 :                                 del.br_blockcount -= off;
    5507      502997 :                                 del.br_startoff += off;
    5508      502997 :                                 del.br_startblock += off;
    5509     2196320 :                         } else if (del.br_startoff == start &&
    5510      350141 :                                    (del.br_state == XFS_EXT_UNWRITTEN ||
    5511      102944 :                                     tp->t_blk_res == 0)) {
    5512             :                                 /*
    5513             :                                  * Can't make it unwritten.  There isn't
    5514             :                                  * a full extent here so just skip it.
    5515             :                                  */
    5516      350141 :                                 ASSERT(end >= del.br_blockcount);
    5517      350141 :                                 end -= del.br_blockcount;
    5518      484292 :                                 if (got.br_startoff > end &&
    5519      134151 :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5520             :                                         done = true;
    5521             :                                         break;
    5522             :                                 }
    5523      350141 :                                 continue;
    5524     1846179 :                         } else if (del.br_state == XFS_EXT_UNWRITTEN) {
    5525     1160254 :                                 struct xfs_bmbt_irec    prev;
    5526     1160254 :                                 xfs_fileoff_t           unwrite_start;
    5527             : 
    5528             :                                 /*
    5529             :                                  * This one is already unwritten.
    5530             :                                  * It must have a written left neighbor.
    5531             :                                  * Unwrite the killed part of that one and
    5532             :                                  * try again.
    5533             :                                  */
    5534     1160254 :                                 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
    5535           0 :                                         ASSERT(0);
    5536     1160254 :                                 ASSERT(prev.br_state == XFS_EXT_NORM);
    5537     1160254 :                                 ASSERT(!isnullstartblock(prev.br_startblock));
    5538     1160254 :                                 ASSERT(del.br_startblock ==
    5539             :                                        prev.br_startblock + prev.br_blockcount);
    5540     1160254 :                                 unwrite_start = max3(start,
    5541             :                                                      del.br_startoff - mod,
    5542             :                                                      prev.br_startoff);
    5543     1160254 :                                 mod = unwrite_start - prev.br_startoff;
    5544     1160254 :                                 prev.br_startoff = unwrite_start;
    5545     1160254 :                                 prev.br_startblock += mod;
    5546     1160254 :                                 prev.br_blockcount -= mod;
    5547     1160254 :                                 prev.br_state = XFS_EXT_UNWRITTEN;
    5548     1160254 :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5549             :                                                 ip, whichfork, &icur, &cur,
    5550             :                                                 &prev, &logflags);
    5551     1160254 :                                 if (error)
    5552           0 :                                         goto error0;
    5553     1160254 :                                 goto nodelete;
    5554             :                         } else {
    5555      685925 :                                 ASSERT(del.br_state == XFS_EXT_NORM);
    5556      685925 :                                 del.br_state = XFS_EXT_UNWRITTEN;
    5557      685925 :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5558             :                                                 ip, whichfork, &icur, &cur,
    5559             :                                                 &del, &logflags);
    5560      685925 :                                 if (error)
    5561           0 :                                         goto error0;
    5562      685925 :                                 goto nodelete;
    5563             :                         }
    5564             :                 }
    5565             : 
    5566    34340421 : delete:
    5567   188039437 :                 if (wasdel) {
    5568     6096746 :                         error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
    5569             :                                         &got, &del);
    5570             :                 } else {
    5571   181942691 :                         error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
    5572             :                                         &del, &tmp_logflags, whichfork,
    5573             :                                         flags);
    5574   181955043 :                         logflags |= tmp_logflags;
    5575             :                 }
    5576             : 
    5577   188116317 :                 if (error)
    5578           5 :                         goto error0;
    5579             : 
    5580   188116312 :                 end = del.br_startoff - 1;
    5581   189962491 : nodelete:
    5582             :                 /*
    5583             :                  * If not done go on to the next (previous) record.
    5584             :                  */
    5585   189962491 :                 if (end != (xfs_fileoff_t)-1 && end >= start) {
    5586    80931252 :                         if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5587    80754283 :                             (got.br_startoff > end &&
    5588     1684904 :                              !xfs_iext_prev_extent(ifp, &icur, &got))) {
    5589             :                                 done = true;
    5590             :                                 break;
    5591             :                         }
    5592    79009316 :                         extno++;
    5593             :                 }
    5594             :         }
    5595   165468952 :         if (done || end == (xfs_fileoff_t)-1 || end < start)
    5596   130372612 :                 *rlen = 0;
    5597             :         else
    5598    37018530 :                 *rlen = end - start + 1;
    5599             : 
    5600             :         /*
    5601             :          * Convert to a btree if necessary.
    5602             :          */
    5603   167391142 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    5604      240543 :                 ASSERT(cur == NULL);
    5605      240543 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    5606             :                                 &tmp_logflags, whichfork);
    5607      240543 :                 logflags |= tmp_logflags;
    5608             :         } else {
    5609   167050365 :                 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
    5610             :                         whichfork);
    5611             :         }
    5612             : 
    5613   167273521 : error0:
    5614             :         /*
    5615             :          * Log everything.  Do this after conversion, there's no point in
    5616             :          * logging the extent records if we've converted to btree format.
    5617             :          */
    5618   170961257 :         if ((logflags & xfs_ilog_fext(whichfork)) &&
    5619    63946930 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    5620      240644 :                 logflags &= ~xfs_ilog_fext(whichfork);
    5621   170718032 :         else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
    5622           0 :                  ifp->if_format != XFS_DINODE_FMT_BTREE)
    5623           0 :                 logflags &= ~xfs_ilog_fbroot(whichfork);
    5624             :         /*
    5625             :          * Log inode even in the error case, if the transaction
    5626             :          * is dirty we'll need to shut down the filesystem.
    5627             :          */
    5628   167273521 :         if (logflags)
    5629   144349611 :                 xfs_trans_log_inode(tp, ip, logflags);
    5630   167363572 :         if (cur) {
    5631    84979747 :                 if (!error)
    5632    84944216 :                         cur->bc_ino.allocated = 0;
    5633    84979747 :                 xfs_btree_del_cursor(cur, error);
    5634             :         }
    5635             :         return error;
    5636             : }
    5637             : 
    5638             : /* Unmap a range of a file. */
    5639             : int
    5640    73282883 : xfs_bunmapi(
    5641             :         xfs_trans_t             *tp,
    5642             :         struct xfs_inode        *ip,
    5643             :         xfs_fileoff_t           bno,
    5644             :         xfs_filblks_t           len,
    5645             :         uint32_t                flags,
    5646             :         xfs_extnum_t            nexts,
    5647             :         int                     *done)
    5648             : {
    5649    73282883 :         int                     error;
    5650             : 
    5651    73282883 :         error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
    5652    73265671 :         *done = (len == 0);
    5653    73265671 :         return error;
    5654             : }
    5655             : 
    5656             : /*
    5657             :  * Determine whether an extent shift can be accomplished by a merge with the
    5658             :  * extent that precedes the target hole of the shift.
    5659             :  */
    5660             : STATIC bool
    5661   113000242 : xfs_bmse_can_merge(
    5662             :         struct xfs_bmbt_irec    *left,  /* preceding extent */
    5663             :         struct xfs_bmbt_irec    *got,   /* current extent to shift */
    5664             :         xfs_fileoff_t           shift)  /* shift fsb */
    5665             : {
    5666   113000242 :         xfs_fileoff_t           startoff;
    5667             : 
    5668   113000242 :         startoff = got->br_startoff - shift;
    5669             : 
    5670             :         /*
    5671             :          * The extent, once shifted, must be adjacent in-file and on-disk with
    5672             :          * the preceding extent.
    5673             :          */
    5674   113000242 :         if ((left->br_startoff + left->br_blockcount != startoff) ||
    5675   106664763 :             (left->br_startblock + left->br_blockcount != got->br_startblock) ||
    5676     1904085 :             (left->br_state != got->br_state) ||
    5677        7174 :             (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
    5678   112993068 :                 return false;
    5679             : 
    5680             :         return true;
    5681             : }
    5682             : 
    5683             : /*
    5684             :  * A bmap extent shift adjusts the file offset of an extent to fill a preceding
    5685             :  * hole in the file. If an extent shift would result in the extent being fully
    5686             :  * adjacent to the extent that currently precedes the hole, we can merge with
    5687             :  * the preceding extent rather than do the shift.
    5688             :  *
    5689             :  * This function assumes the caller has verified a shift-by-merge is possible
    5690             :  * with the provided extents via xfs_bmse_can_merge().
    5691             :  */
    5692             : STATIC int
    5693        3587 : xfs_bmse_merge(
    5694             :         struct xfs_trans                *tp,
    5695             :         struct xfs_inode                *ip,
    5696             :         int                             whichfork,
    5697             :         xfs_fileoff_t                   shift,          /* shift fsb */
    5698             :         struct xfs_iext_cursor          *icur,
    5699             :         struct xfs_bmbt_irec            *got,           /* extent to shift */
    5700             :         struct xfs_bmbt_irec            *left,          /* preceding extent */
    5701             :         struct xfs_btree_cur            *cur,
    5702             :         int                             *logflags)      /* output */
    5703             : {
    5704        3587 :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    5705        3587 :         struct xfs_bmbt_irec            new;
    5706        3587 :         xfs_filblks_t                   blockcount;
    5707        3587 :         int                             error, i;
    5708        3587 :         struct xfs_mount                *mp = ip->i_mount;
    5709             : 
    5710        3587 :         blockcount = left->br_blockcount + got->br_blockcount;
    5711             : 
    5712        3587 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5713        3587 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5714        3587 :         ASSERT(xfs_bmse_can_merge(left, got, shift));
    5715             : 
    5716        3587 :         new = *left;
    5717        3587 :         new.br_blockcount = blockcount;
    5718             : 
    5719             :         /*
    5720             :          * Update the on-disk extent count, the btree if necessary and log the
    5721             :          * inode.
    5722             :          */
    5723        3587 :         ifp->if_nextents--;
    5724        3587 :         *logflags |= XFS_ILOG_CORE;
    5725        3587 :         if (!cur) {
    5726        2425 :                 *logflags |= XFS_ILOG_DEXT;
    5727        2425 :                 goto done;
    5728             :         }
    5729             : 
    5730             :         /* lookup and remove the extent to merge */
    5731        1162 :         error = xfs_bmbt_lookup_eq(cur, got, &i);
    5732        1162 :         if (error)
    5733             :                 return error;
    5734        1162 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5735           0 :                 xfs_btree_mark_sick(cur);
    5736           0 :                 return -EFSCORRUPTED;
    5737             :         }
    5738             : 
    5739        1162 :         error = xfs_btree_delete(cur, &i);
    5740        1162 :         if (error)
    5741             :                 return error;
    5742        1162 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5743           0 :                 xfs_btree_mark_sick(cur);
    5744           0 :                 return -EFSCORRUPTED;
    5745             :         }
    5746             : 
    5747             :         /* lookup and update size of the previous extent */
    5748        1162 :         error = xfs_bmbt_lookup_eq(cur, left, &i);
    5749        1162 :         if (error)
    5750             :                 return error;
    5751        1162 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5752           0 :                 xfs_btree_mark_sick(cur);
    5753           0 :                 return -EFSCORRUPTED;
    5754             :         }
    5755             : 
    5756        1162 :         error = xfs_bmbt_update(cur, &new);
    5757        1162 :         if (error)
    5758             :                 return error;
    5759             : 
    5760             :         /* change to extent format if required after extent removal */
    5761        1162 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
    5762        1162 :         if (error)
    5763             :                 return error;
    5764             : 
    5765        1162 : done:
    5766        3587 :         xfs_iext_remove(ip, icur, 0);
    5767        3587 :         xfs_iext_prev(ifp, icur);
    5768        7174 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5769             :                         &new);
    5770             : 
    5771             :         /* update reverse mapping. rmap functions merge the rmaps for us */
    5772        3587 :         xfs_rmap_unmap_extent(tp, ip, whichfork, got);
    5773        3587 :         memcpy(&new, got, sizeof(new));
    5774        3587 :         new.br_startoff = left->br_startoff + left->br_blockcount;
    5775        3587 :         xfs_rmap_map_extent(tp, ip, whichfork, &new);
    5776        3587 :         return 0;
    5777             : }
    5778             : 
    5779             : static int
    5780   114786553 : xfs_bmap_shift_update_extent(
    5781             :         struct xfs_trans        *tp,
    5782             :         struct xfs_inode        *ip,
    5783             :         int                     whichfork,
    5784             :         struct xfs_iext_cursor  *icur,
    5785             :         struct xfs_bmbt_irec    *got,
    5786             :         struct xfs_btree_cur    *cur,
    5787             :         int                     *logflags,
    5788             :         xfs_fileoff_t           startoff)
    5789             : {
    5790   114786553 :         struct xfs_mount        *mp = ip->i_mount;
    5791   114786553 :         struct xfs_bmbt_irec    prev = *got;
    5792   114786553 :         int                     error, i;
    5793             : 
    5794   114786553 :         *logflags |= XFS_ILOG_CORE;
    5795             : 
    5796   114786553 :         got->br_startoff = startoff;
    5797             : 
    5798   114786553 :         if (cur) {
    5799   106545473 :                 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
    5800   106545484 :                 if (error)
    5801             :                         return error;
    5802   106545484 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5803           0 :                         xfs_btree_mark_sick(cur);
    5804           0 :                         return -EFSCORRUPTED;
    5805             :                 }
    5806             : 
    5807   106545484 :                 error = xfs_bmbt_update(cur, got);
    5808   106545463 :                 if (error)
    5809             :                         return error;
    5810             :         } else {
    5811     8241080 :                 *logflags |= XFS_ILOG_DEXT;
    5812             :         }
    5813             : 
    5814   229573086 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5815             :                         got);
    5816             : 
    5817             :         /* update reverse mapping */
    5818   114786521 :         xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
    5819   114786555 :         xfs_rmap_map_extent(tp, ip, whichfork, got);
    5820   114786555 :         return 0;
    5821             : }
    5822             : 
    5823             : int
    5824   105502790 : xfs_bmap_collapse_extents(
    5825             :         struct xfs_trans        *tp,
    5826             :         struct xfs_inode        *ip,
    5827             :         xfs_fileoff_t           *next_fsb,
    5828             :         xfs_fileoff_t           offset_shift_fsb,
    5829             :         bool                    *done)
    5830             : {
    5831   105502790 :         int                     whichfork = XFS_DATA_FORK;
    5832   105502790 :         struct xfs_mount        *mp = ip->i_mount;
    5833   105502790 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5834   105502785 :         struct xfs_btree_cur    *cur = NULL;
    5835   105502785 :         struct xfs_bmbt_irec    got, prev;
    5836   105502785 :         struct xfs_iext_cursor  icur;
    5837   105502785 :         xfs_fileoff_t           new_startoff;
    5838   105502785 :         int                     error = 0;
    5839   105502785 :         int                     logflags = 0;
    5840             : 
    5841   211005574 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5842   105502785 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5843           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5844           0 :                 return -EFSCORRUPTED;
    5845             :         }
    5846             : 
    5847   211005578 :         if (xfs_is_shutdown(mp))
    5848             :                 return -EIO;
    5849             : 
    5850   105502786 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5851             : 
    5852   105502784 :         error = xfs_iread_extents(tp, ip, whichfork);
    5853   105502783 :         if (error)
    5854             :                 return error;
    5855             : 
    5856   105502783 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5857   100820592 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5858   100820591 :                 cur->bc_ino.flags = 0;
    5859             :         }
    5860             : 
    5861   105502782 :         if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5862      298274 :                 *done = true;
    5863      298274 :                 goto del_cursor;
    5864             :         }
    5865   105204513 :         if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
    5866           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5867           0 :                 error = -EFSCORRUPTED;
    5868           0 :                 goto del_cursor;
    5869             :         }
    5870             : 
    5871   105204513 :         new_startoff = got.br_startoff - offset_shift_fsb;
    5872   105204513 :         if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
    5873   105051957 :                 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
    5874           0 :                         error = -EINVAL;
    5875           0 :                         goto del_cursor;
    5876             :                 }
    5877             : 
    5878   105051957 :                 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
    5879        3587 :                         error = xfs_bmse_merge(tp, ip, whichfork,
    5880             :                                         offset_shift_fsb, &icur, &got, &prev,
    5881             :                                         cur, &logflags);
    5882        3587 :                         if (error)
    5883           0 :                                 goto del_cursor;
    5884        3587 :                         goto done;
    5885             :                 }
    5886             :         } else {
    5887      152553 :                 if (got.br_startoff < offset_shift_fsb) {
    5888           0 :                         error = -EINVAL;
    5889           0 :                         goto del_cursor;
    5890             :                 }
    5891             :         }
    5892             : 
    5893   105200923 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    5894             :                         cur, &logflags, new_startoff);
    5895   105200925 :         if (error)
    5896           0 :                 goto del_cursor;
    5897             : 
    5898   105200925 : done:
    5899   105204512 :         if (!xfs_iext_next_extent(ifp, &icur, &got)) {
    5900     2105906 :                 *done = true;
    5901     2105906 :                 goto del_cursor;
    5902             :         }
    5903             : 
    5904   103098603 :         *next_fsb = got.br_startoff;
    5905   105502783 : del_cursor:
    5906   105502783 :         if (cur)
    5907   100820593 :                 xfs_btree_del_cursor(cur, error);
    5908   105502776 :         if (logflags)
    5909   105204506 :                 xfs_trans_log_inode(tp, ip, logflags);
    5910             :         return error;
    5911             : }
    5912             : 
    5913             : /* Make sure we won't be right-shifting an extent past the maximum bound. */
    5914             : int
    5915     1795515 : xfs_bmap_can_insert_extents(
    5916             :         struct xfs_inode        *ip,
    5917             :         xfs_fileoff_t           off,
    5918             :         xfs_fileoff_t           shift)
    5919             : {
    5920     1795515 :         struct xfs_bmbt_irec    got;
    5921     1795515 :         int                     is_empty;
    5922     1795515 :         int                     error = 0;
    5923             : 
    5924     1795515 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5925             : 
    5926     3591018 :         if (xfs_is_shutdown(ip->i_mount))
    5927             :                 return -EIO;
    5928             : 
    5929     1795509 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    5930     1795513 :         error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
    5931     1795511 :         if (!error && !is_empty && got.br_startoff >= off &&
    5932     1546591 :             ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
    5933           0 :                 error = -EINVAL;
    5934     1795511 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    5935             : 
    5936     1795511 :         return error;
    5937             : }
    5938             : 
    5939             : int
    5940     9738222 : xfs_bmap_insert_extents(
    5941             :         struct xfs_trans        *tp,
    5942             :         struct xfs_inode        *ip,
    5943             :         xfs_fileoff_t           *next_fsb,
    5944             :         xfs_fileoff_t           offset_shift_fsb,
    5945             :         bool                    *done,
    5946             :         xfs_fileoff_t           stop_fsb)
    5947             : {
    5948     9738222 :         int                     whichfork = XFS_DATA_FORK;
    5949     9738222 :         struct xfs_mount        *mp = ip->i_mount;
    5950     9738222 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5951     9738220 :         struct xfs_btree_cur    *cur = NULL;
    5952     9738220 :         struct xfs_bmbt_irec    got, next;
    5953     9738220 :         struct xfs_iext_cursor  icur;
    5954     9738220 :         xfs_fileoff_t           new_startoff;
    5955     9738220 :         int                     error = 0;
    5956     9738220 :         int                     logflags = 0;
    5957             : 
    5958    19476439 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5959     9738220 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5960           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5961           0 :                 return -EFSCORRUPTED;
    5962             :         }
    5963             : 
    5964    19476438 :         if (xfs_is_shutdown(mp))
    5965             :                 return -EIO;
    5966             : 
    5967     9738219 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5968             : 
    5969     9738221 :         error = xfs_iread_extents(tp, ip, whichfork);
    5970     9738224 :         if (error)
    5971             :                 return error;
    5972             : 
    5973     9738224 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5974     5764773 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5975     5764774 :                 cur->bc_ino.flags = 0;
    5976             :         }
    5977             : 
    5978     9738225 :         if (*next_fsb == NULLFSBLOCK) {
    5979     1793514 :                 xfs_iext_last(ifp, &icur);
    5980     1793515 :                 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5981     1770741 :                     stop_fsb > got.br_startoff) {
    5982      152579 :                         *done = true;
    5983      152579 :                         goto del_cursor;
    5984             :                 }
    5985             :         } else {
    5986     7944711 :                 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5987           0 :                         *done = true;
    5988           0 :                         goto del_cursor;
    5989             :                 }
    5990             :         }
    5991     9585648 :         if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
    5992           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5993           0 :                 error = -EFSCORRUPTED;
    5994           0 :                 goto del_cursor;
    5995             :         }
    5996             : 
    5997     9585648 :         if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
    5998           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5999           0 :                 error = -EFSCORRUPTED;
    6000           0 :                 goto del_cursor;
    6001             :         }
    6002             : 
    6003     9585648 :         new_startoff = got.br_startoff + offset_shift_fsb;
    6004     9585648 :         if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
    6005     7944706 :                 if (new_startoff + got.br_blockcount > next.br_startoff) {
    6006           0 :                         error = -EINVAL;
    6007           0 :                         goto del_cursor;
    6008             :                 }
    6009             : 
    6010             :                 /*
    6011             :                  * Unlike a left shift (which involves a hole punch), a right
    6012             :                  * shift does not modify extent neighbors in any way.  We should
    6013             :                  * never find mergeable extents in this scenario.  Check anyways
    6014             :                  * and warn if we encounter two extents that could be one.
    6015             :                  */
    6016     7944706 :                 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
    6017           0 :                         WARN_ON_ONCE(1);
    6018             :         }
    6019             : 
    6020     9585640 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    6021             :                         cur, &logflags, new_startoff);
    6022     9585652 :         if (error)
    6023           0 :                 goto del_cursor;
    6024             : 
    6025     9585652 :         if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
    6026     9437626 :             stop_fsb >= got.br_startoff + got.br_blockcount) {
    6027     1640934 :                 *done = true;
    6028     1640934 :                 goto del_cursor;
    6029             :         }
    6030             : 
    6031     7944710 :         *next_fsb = got.br_startoff;
    6032     9738223 : del_cursor:
    6033     9738223 :         if (cur)
    6034     5764772 :                 xfs_btree_del_cursor(cur, error);
    6035     9738220 :         if (logflags)
    6036     9585642 :                 xfs_trans_log_inode(tp, ip, logflags);
    6037             :         return error;
    6038             : }
    6039             : 
    6040             : /*
    6041             :  * Splits an extent into two extents at split_fsb block such that it is the
    6042             :  * first block of the current_ext. @ext is a target extent to be split.
    6043             :  * @split_fsb is a block where the extents is split.  If split_fsb lies in a
    6044             :  * hole or the first block of extents, just return 0.
    6045             :  */
    6046             : int
    6047     1793516 : xfs_bmap_split_extent(
    6048             :         struct xfs_trans        *tp,
    6049             :         struct xfs_inode        *ip,
    6050             :         xfs_fileoff_t           split_fsb)
    6051             : {
    6052     1793516 :         int                             whichfork = XFS_DATA_FORK;
    6053     1793516 :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    6054     1793514 :         struct xfs_btree_cur            *cur = NULL;
    6055     1793514 :         struct xfs_bmbt_irec            got;
    6056     1793514 :         struct xfs_bmbt_irec            new; /* split extent */
    6057     1793514 :         struct xfs_mount                *mp = ip->i_mount;
    6058     1793514 :         xfs_fsblock_t                   gotblkcnt; /* new block count for got */
    6059     1793514 :         struct xfs_iext_cursor          icur;
    6060     1793514 :         int                             error = 0;
    6061     1793514 :         int                             logflags = 0;
    6062     1793514 :         int                             i = 0;
    6063             : 
    6064     3587028 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    6065     1793514 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    6066           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    6067           0 :                 return -EFSCORRUPTED;
    6068             :         }
    6069             : 
    6070     3587028 :         if (xfs_is_shutdown(mp))
    6071             :                 return -EIO;
    6072             : 
    6073             :         /* Read in all the extents */
    6074     1793513 :         error = xfs_iread_extents(tp, ip, whichfork);
    6075     1793514 :         if (error)
    6076             :                 return error;
    6077             : 
    6078             :         /*
    6079             :          * If there are not extents, or split_fsb lies in a hole we are done.
    6080             :          */
    6081     1793514 :         if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
    6082     1640935 :             got.br_startoff >= split_fsb)
    6083             :                 return 0;
    6084             : 
    6085      605696 :         gotblkcnt = split_fsb - got.br_startoff;
    6086      605696 :         new.br_startoff = split_fsb;
    6087      605696 :         new.br_startblock = got.br_startblock + gotblkcnt;
    6088      605696 :         new.br_blockcount = got.br_blockcount - gotblkcnt;
    6089      605696 :         new.br_state = got.br_state;
    6090             : 
    6091      605696 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    6092      200307 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    6093      200307 :                 cur->bc_ino.flags = 0;
    6094      200307 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    6095      200307 :                 if (error)
    6096           0 :                         goto del_cursor;
    6097      200307 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    6098           0 :                         xfs_btree_mark_sick(cur);
    6099           0 :                         error = -EFSCORRUPTED;
    6100           0 :                         goto del_cursor;
    6101             :                 }
    6102             :         }
    6103             : 
    6104      605696 :         got.br_blockcount = gotblkcnt;
    6105      605696 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
    6106             :                         &got);
    6107             : 
    6108      605696 :         logflags = XFS_ILOG_CORE;
    6109      605696 :         if (cur) {
    6110      200307 :                 error = xfs_bmbt_update(cur, &got);
    6111      200307 :                 if (error)
    6112           0 :                         goto del_cursor;
    6113             :         } else
    6114             :                 logflags |= XFS_ILOG_DEXT;
    6115             : 
    6116             :         /* Add new extent */
    6117      605696 :         xfs_iext_next(ifp, &icur);
    6118      605696 :         xfs_iext_insert(ip, &icur, &new, 0);
    6119      605696 :         ifp->if_nextents++;
    6120             : 
    6121      605696 :         if (cur) {
    6122      200307 :                 error = xfs_bmbt_lookup_eq(cur, &new, &i);
    6123      200307 :                 if (error)
    6124           0 :                         goto del_cursor;
    6125      200307 :                 if (XFS_IS_CORRUPT(mp, i != 0)) {
    6126           0 :                         xfs_btree_mark_sick(cur);
    6127           0 :                         error = -EFSCORRUPTED;
    6128           0 :                         goto del_cursor;
    6129             :                 }
    6130      200307 :                 error = xfs_btree_insert(cur, &i);
    6131      200307 :                 if (error)
    6132           0 :                         goto del_cursor;
    6133      200307 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    6134           0 :                         xfs_btree_mark_sick(cur);
    6135           0 :                         error = -EFSCORRUPTED;
    6136           0 :                         goto del_cursor;
    6137             :                 }
    6138             :         }
    6139             : 
    6140             :         /*
    6141             :          * Convert to a btree if necessary.
    6142             :          */
    6143      605696 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    6144       32418 :                 int tmp_logflags; /* partial log flag return val */
    6145             : 
    6146       32418 :                 ASSERT(cur == NULL);
    6147       32418 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    6148             :                                 &tmp_logflags, whichfork);
    6149       32418 :                 logflags |= tmp_logflags;
    6150             :         }
    6151             : 
    6152      573278 : del_cursor:
    6153      605696 :         if (cur) {
    6154      232725 :                 cur->bc_ino.allocated = 0;
    6155      232725 :                 xfs_btree_del_cursor(cur, error);
    6156             :         }
    6157             : 
    6158      605696 :         if (logflags)
    6159      605696 :                 xfs_trans_log_inode(tp, ip, logflags);
    6160             :         return error;
    6161             : }
    6162             : 
    6163             : /* Record a bmap intent. */
    6164             : static void
    6165   131598748 : __xfs_bmap_add(
    6166             :         struct xfs_trans                *tp,
    6167             :         enum xfs_bmap_intent_type       type,
    6168             :         struct xfs_inode                *ip,
    6169             :         int                             whichfork,
    6170             :         struct xfs_bmbt_irec            *bmap)
    6171             : {
    6172   131598748 :         struct xfs_bmap_intent          *bi;
    6173             : 
    6174   131598748 :         if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
    6175   131598748 :             bmap->br_startblock == HOLESTARTBLOCK ||
    6176             :             bmap->br_startblock == DELAYSTARTBLOCK)
    6177             :                 return;
    6178             : 
    6179   126431356 :         bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
    6180   126431719 :         INIT_LIST_HEAD(&bi->bi_list);
    6181   126431719 :         bi->bi_type = type;
    6182   126431719 :         bi->bi_owner = ip;
    6183   126431719 :         bi->bi_whichfork = whichfork;
    6184   126431719 :         bi->bi_bmap = *bmap;
    6185             : 
    6186   126431719 :         trace_xfs_bmap_defer(bi);
    6187             : 
    6188   126431457 :         xfs_bmap_update_get_group(tp->t_mountp, bi);
    6189   126431925 :         xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
    6190             : }
    6191             : 
    6192             : /* Map an extent into a file. */
    6193             : void
    6194    97859600 : xfs_bmap_map_extent(
    6195             :         struct xfs_trans        *tp,
    6196             :         struct xfs_inode        *ip,
    6197             :         int                     whichfork,
    6198             :         struct xfs_bmbt_irec    *PREV)
    6199             : {
    6200    97859600 :         __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
    6201    97859675 : }
    6202             : 
    6203             : /* Unmap an extent out of a file. */
    6204             : void
    6205    33739419 : xfs_bmap_unmap_extent(
    6206             :         struct xfs_trans        *tp,
    6207             :         struct xfs_inode        *ip,
    6208             :         int                     whichfork,
    6209             :         struct xfs_bmbt_irec    *PREV)
    6210             : {
    6211    33739419 :         __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
    6212    33739437 : }
    6213             : 
    6214             : /*
    6215             :  * Process one of the deferred bmap operations.  We pass back the
    6216             :  * btree cursor to maintain our lock on the bmapbt between calls.
    6217             :  */
    6218             : int
    6219   126431838 : xfs_bmap_finish_one(
    6220             :         struct xfs_trans                *tp,
    6221             :         struct xfs_bmap_intent          *bi)
    6222             : {
    6223   126431838 :         struct xfs_bmbt_irec            *bmap = &bi->bi_bmap;
    6224   126431838 :         int                             error = 0;
    6225   126431838 :         int                             flags = 0;
    6226             : 
    6227   126431838 :         if (bi->bi_whichfork == XFS_ATTR_FORK)
    6228      293684 :                 flags |= XFS_BMAPI_ATTRFORK;
    6229             : 
    6230   126431838 :         ASSERT(tp->t_highest_agno == NULLAGNUMBER);
    6231             : 
    6232   126431838 :         trace_xfs_bmap_deferred(bi);
    6233             : 
    6234   126431733 :         if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
    6235             :                 return -EIO;
    6236             : 
    6237   126431673 :         switch (bi->bi_type) {
    6238    95275938 :         case XFS_BMAP_MAP:
    6239    95275938 :                 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
    6240     1735944 :                         flags |= XFS_BMAPI_PREALLOC;
    6241    95275938 :                 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
    6242             :                                 bmap->br_blockcount, bmap->br_startblock,
    6243             :                                 flags);
    6244    95275964 :                 bmap->br_blockcount = 0;
    6245    95275964 :                 break;
    6246    31155735 :         case XFS_BMAP_UNMAP:
    6247    31155735 :                 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
    6248             :                                 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
    6249             :                                 1);
    6250    31155735 :                 break;
    6251           0 :         default:
    6252           0 :                 ASSERT(0);
    6253           0 :                 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
    6254           0 :                 error = -EFSCORRUPTED;
    6255             :         }
    6256             : 
    6257             :         return error;
    6258             : }
    6259             : 
    6260             : /* Check that an extent does not have invalid flags or bad ranges. */
    6261             : xfs_failaddr_t
    6262  4048007979 : xfs_bmap_validate_extent_raw(
    6263             :         struct xfs_mount        *mp,
    6264             :         bool                    rtfile,
    6265             :         int                     whichfork,
    6266             :         struct xfs_bmbt_irec    *irec)
    6267             : {
    6268  4048007979 :         if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
    6269           0 :                 return __this_address;
    6270             : 
    6271  4048015475 :         if (rtfile && whichfork == XFS_DATA_FORK) {
    6272   498907245 :                 if (!xfs_verify_rtext(mp, irec->br_startblock,
    6273             :                                           irec->br_blockcount))
    6274           0 :                         return __this_address;
    6275             :         } else {
    6276  3549108230 :                 if (!xfs_verify_fsbext(mp, irec->br_startblock,
    6277             :                                            irec->br_blockcount))
    6278           0 :                         return __this_address;
    6279             :         }
    6280  4048129998 :         if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
    6281           0 :                 return __this_address;
    6282             :         return NULL;
    6283             : }
    6284             : 
    6285             : int __init
    6286          50 : xfs_bmap_intent_init_cache(void)
    6287             : {
    6288          50 :         xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
    6289             :                         sizeof(struct xfs_bmap_intent),
    6290             :                         0, 0, NULL);
    6291             : 
    6292          50 :         return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
    6293             : }
    6294             : 
    6295             : void
    6296          49 : xfs_bmap_intent_destroy_cache(void)
    6297             : {
    6298          49 :         kmem_cache_destroy(xfs_bmap_intent_cache);
    6299          49 :         xfs_bmap_intent_cache = NULL;
    6300          49 : }
    6301             : 
    6302             : /* Check that an inode's extent does not have invalid flags or bad ranges. */
    6303             : xfs_failaddr_t
    6304  4048073935 : xfs_bmap_validate_extent(
    6305             :         struct xfs_inode        *ip,
    6306             :         int                     whichfork,
    6307             :         struct xfs_bmbt_irec    *irec)
    6308             : {
    6309  4048073935 :         return xfs_bmap_validate_extent_raw(ip->i_mount,
    6310  4048073935 :                         XFS_IS_REALTIME_INODE(ip), whichfork, irec);
    6311             : }
    6312             : 
    6313             : /*
    6314             :  * Used in xfs_itruncate_extents().  This is the maximum number of extents
    6315             :  * freed from a file in a single transaction.
    6316             :  */
    6317             : #define XFS_ITRUNC_MAX_EXTENTS  2
    6318             : 
    6319             : /*
    6320             :  * Unmap every extent in part of an inode's fork.  We don't do any higher level
    6321             :  * invalidation work at all.
    6322             :  */
    6323             : int
    6324    33064603 : xfs_bunmapi_range(
    6325             :         struct xfs_trans        **tpp,
    6326             :         struct xfs_inode        *ip,
    6327             :         uint32_t                flags,
    6328             :         xfs_fileoff_t           startoff,
    6329             :         xfs_fileoff_t           endoff)
    6330             : {
    6331    33064603 :         xfs_filblks_t           unmap_len = endoff - startoff + 1;
    6332    33064603 :         int                     error = 0;
    6333             : 
    6334    33064603 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    6335             : 
    6336   100241479 :         while (unmap_len > 0) {
    6337    67237427 :                 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
    6338    67237427 :                 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
    6339             :                                 XFS_ITRUNC_MAX_EXTENTS);
    6340    67242400 :                 if (error)
    6341         501 :                         goto out;
    6342             : 
    6343             :                 /* free the just unmapped extents */
    6344    67241899 :                 error = xfs_defer_finish(tpp);
    6345    67285138 :                 if (error)
    6346        1575 :                         goto out;
    6347             :         }
    6348    33004052 : out:
    6349    33006128 :         return error;
    6350             : }
    6351             : 
    6352             : struct xfs_bmap_query_range {
    6353             :         xfs_bmap_query_range_fn fn;
    6354             :         void                    *priv;
    6355             : };
    6356             : 
    6357             : /* Format btree record and pass to our callback. */
    6358             : STATIC int
    6359         120 : xfs_bmap_query_range_helper(
    6360             :         struct xfs_btree_cur            *cur,
    6361             :         const union xfs_btree_rec       *rec,
    6362             :         void                            *priv)
    6363             : {
    6364         120 :         struct xfs_bmap_query_range     *query = priv;
    6365         120 :         struct xfs_bmbt_irec            irec;
    6366         120 :         xfs_failaddr_t                  fa;
    6367             : 
    6368         120 :         xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
    6369         120 :         fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
    6370             :                         &irec);
    6371         120 :         if (fa) {
    6372           0 :                 xfs_btree_mark_sick(cur);
    6373           0 :                 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
    6374           0 :                                 cur->bc_ino.whichfork, fa, &irec);
    6375             :         }
    6376             : 
    6377         120 :         return query->fn(cur, &irec, query->priv);
    6378             : }
    6379             : 
    6380             : /* Find all bmaps. */
    6381             : int
    6382           8 : xfs_bmap_query_all(
    6383             :         struct xfs_btree_cur            *cur,
    6384             :         xfs_bmap_query_range_fn         fn,
    6385             :         void                            *priv)
    6386             : {
    6387           8 :         struct xfs_bmap_query_range     query = {
    6388             :                 .priv                   = priv,
    6389             :                 .fn                     = fn,
    6390             :         };
    6391             : 
    6392           8 :         return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
    6393             : }

Generated by: LCOV version 1.14