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-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 2586 3188 81.1 %
Date: 2023-07-31 20:08:07 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       48238 : xfs_bmap_compute_maxlevels(
      54             :         xfs_mount_t     *mp,            /* file system mount structure */
      55             :         int             whichfork)      /* data or attr fork */
      56             : {
      57       48238 :         uint64_t        maxblocks;      /* max blocks at this level */
      58       48238 :         xfs_extnum_t    maxleafents;    /* max leaf entries possible */
      59       48238 :         int             level;          /* btree level */
      60       48238 :         int             maxrootrecs;    /* max records in root block */
      61       48238 :         int             minleafrecs;    /* min records in leaf block */
      62       48238 :         int             minnoderecs;    /* min records in node block */
      63       48238 :         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       48238 :         maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
      78             :                                 whichfork);
      79       48238 :         if (whichfork == XFS_DATA_FORK)
      80             :                 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
      81             :         else
      82       24119 :                 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
      83             : 
      84       48238 :         maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
      85       48238 :         minleafrecs = mp->m_bmap_dmnr[0];
      86       48238 :         minnoderecs = mp->m_bmap_dmnr[1];
      87       48238 :         maxblocks = howmany_64(maxleafents, minleafrecs);
      88      288990 :         for (level = 1; maxblocks > 1; level++) {
      89      240752 :                 if (maxblocks <= maxrootrecs)
      90             :                         maxblocks = 1;
      91             :                 else
      92      231252 :                         maxblocks = howmany_64(maxblocks, minnoderecs);
      93             :         }
      94       48238 :         mp->m_bm_maxlevels[whichfork] = level;
      95       48238 :         ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
      96       48238 : }
      97             : 
      98             : unsigned int
      99       24119 : xfs_bmap_compute_attr_offset(
     100             :         struct xfs_mount        *mp)
     101             : {
     102       24119 :         if (mp->m_sb.sb_inodesize == 256)
     103          80 :                 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
     104             :         return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
     105             : }
     106             : 
     107             : STATIC int                              /* error */
     108   134524845 : xfs_bmbt_lookup_eq(
     109             :         struct xfs_btree_cur    *cur,
     110             :         struct xfs_bmbt_irec    *irec,
     111             :         int                     *stat)  /* success/failure */
     112             : {
     113   134524845 :         cur->bc_rec.b = *irec;
     114   134524845 :         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   222041515 : static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
     132             : {
     133   222041515 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     134             : 
     135   442084487 :         return whichfork != XFS_COW_FORK &&
     136   222040679 :                 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
     137   124347752 :                 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
     138             : }
     139             : 
     140             : /*
     141             :  * Check if the inode should be converted to extent format.
     142             :  */
     143   222751944 : static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
     144             : {
     145   222751944 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     146             : 
     147   445168935 :         return whichfork != XFS_COW_FORK &&
     148   222752812 :                 ifp->if_format == XFS_DINODE_FMT_BTREE &&
     149    96900427 :                 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    33315799 : xfs_bmbt_update(
     158             :         struct xfs_btree_cur    *cur,
     159             :         struct xfs_bmbt_irec    *irec)
     160             : {
     161    33315799 :         union xfs_btree_rec     rec;
     162             : 
     163    33315799 :         xfs_bmbt_disk_set_all(&rec.bmbt, irec);
     164    33315765 :         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    21581822 : xfs_bmap_worst_indlen(
     173             :         xfs_inode_t     *ip,            /* incore inode pointer */
     174             :         xfs_filblks_t   len)            /* delayed extent length */
     175             : {
     176    21581822 :         int             level;          /* btree level number */
     177    21581822 :         int             maxrecs;        /* maximum record count at this level */
     178    21581822 :         xfs_mount_t     *mp;            /* mount structure */
     179    21581822 :         xfs_filblks_t   rval;           /* return value */
     180             : 
     181    21581822 :         mp = ip->i_mount;
     182    21581822 :         maxrecs = mp->m_bmap_dmxr[0];
     183    21581822 :         for (level = 0, rval = 0;
     184    22953556 :              level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
     185     1371734 :              level++) {
     186    22953662 :                 len += maxrecs - 1;
     187    22953662 :                 do_div(len, maxrecs);
     188    22953662 :                 rval += len;
     189    22953662 :                 if (len == 1)
     190    21581928 :                         return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
     191    21581928 :                                 level - 1;
     192     1371734 :                 if (level == 0)
     193     1155942 :                         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    61494997 : xfs_default_attroffset(
     203             :         struct xfs_inode        *ip)
     204             : {
     205    61494997 :         if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
     206             :                 return roundup(sizeof(xfs_dev_t), 8);
     207    53973115 :         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     4575688 : xfs_bmap_forkoff_reset(
     217             :         xfs_inode_t     *ip,
     218             :         int             whichfork)
     219             : {
     220     4575688 :         if (whichfork == XFS_ATTR_FORK &&
     221     4263445 :             ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
     222             :             ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
     223     4194251 :                 uint    dfl_forkoff = xfs_default_attroffset(ip) >> 3;
     224             : 
     225     4194251 :                 if (dfl_forkoff > ip->i_forkoff)
     226     4177122 :                         ip->i_forkoff = dfl_forkoff;
     227             :         }
     228     4575688 : }
     229             : 
     230             : #ifdef DEBUG
     231             : STATIC struct xfs_buf *
     232   451357795 : xfs_bmap_get_bp(
     233             :         struct xfs_btree_cur    *cur,
     234             :         xfs_fsblock_t           bno)
     235             : {
     236   451357795 :         struct xfs_log_item     *lip;
     237   451357795 :         int                     i;
     238             : 
     239   451357795 :         if (!cur)
     240             :                 return NULL;
     241             : 
     242  1230384791 :         for (i = 0; i < cur->bc_maxlevels; i++) {
     243  1230384600 :                 if (!cur->bc_levels[i].bp)
     244             :                         break;
     245   828250135 :                 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
     246    49223139 :                         return cur->bc_levels[i].bp;
     247             :         }
     248             : 
     249             :         /* Chase down all the log items to see if the bp is there */
     250  2235621287 :         list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
     251  1845045178 :                 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
     252             : 
     253  1845045178 :                 if (bip->bli_item.li_type == XFS_LI_BUF &&
     254  1067027401 :                     xfs_buf_daddr(bip->bli_buf) == bno)
     255    11558547 :                         return bip->bli_buf;
     256             :         }
     257             : 
     258             :         return NULL;
     259             : }
     260             : 
     261             : STATIC void
     262    50024793 : xfs_check_block(
     263             :         struct xfs_btree_block  *block,
     264             :         xfs_mount_t             *mp,
     265             :         int                     root,
     266             :         short                   sz)
     267             : {
     268    50024793 :         int                     i, j, dmxr;
     269    50024793 :         __be64                  *pp, *thispa;   /* pointer to block address */
     270    50024793 :         xfs_bmbt_key_t          *prevp, *keyp;
     271             : 
     272    50024793 :         ASSERT(be16_to_cpu(block->bb_level) > 0);
     273             : 
     274             :         prevp = NULL;
     275  1002763738 :         for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
     276   451357130 :                 dmxr = mp->m_bmap_dmxr[0];
     277   451357130 :                 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
     278             : 
     279   451357130 :                 if (prevp) {
     280   401333052 :                         ASSERT(be64_to_cpu(prevp->br_startoff) <
     281             :                                be64_to_cpu(keyp->br_startoff));
     282             :                 }
     283   451357130 :                 prevp = keyp;
     284             : 
     285             :                 /*
     286             :                  * Compare the block numbers to see if there are dups.
     287             :                  */
     288   451357130 :                 if (root)
     289    73635398 :                         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
     290             :                 else
     291   377721732 :                         pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
     292             : 
     293  5772904120 :                 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
     294  5321547044 :                         if (root)
     295   162001672 :                                 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
     296             :                         else
     297  5159545372 :                                 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
     298  5321547167 :                         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    50024739 : }
     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   144090399 : 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   144090399 :         struct xfs_mount        *mp = ip->i_mount;
     324   144090399 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     325   144088127 :         struct xfs_btree_block  *block; /* current btree block */
     326   144088127 :         xfs_fsblock_t           bno;    /* block # of "block" */
     327   144088127 :         struct xfs_buf          *bp;    /* buffer for "block" */
     328   144088127 :         int                     error;  /* error return value */
     329   144088127 :         xfs_extnum_t            i=0, j; /* index into the extents list */
     330   144088127 :         int                     level;  /* btree level, for checking */
     331   144088127 :         __be64                  *pp;    /* pointer to block address */
     332   144088127 :         xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
     333   144088127 :         xfs_bmbt_rec_t          last = {0, 0}; /* last extent in prev block */
     334   144088127 :         xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
     335   144088127 :         int                     bp_release = 0;
     336             : 
     337   144088127 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
     338             :                 return;
     339             : 
     340             :         /* skip large extent count inodes */
     341    66364219 :         if (ip->i_df.if_nextents > 10000)
     342             :                 return;
     343             : 
     344    35192750 :         bno = NULLFSBLOCK;
     345    35192750 :         block = ifp->if_broot;
     346             :         /*
     347             :          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
     348             :          */
     349    35192750 :         level = be16_to_cpu(block->bb_level);
     350    35192750 :         ASSERT(level > 0);
     351    35192750 :         xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
     352    35192697 :         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
     353    35192606 :         bno = be64_to_cpu(*pp);
     354             : 
     355    35192606 :         ASSERT(bno != NULLFSBLOCK);
     356    35192606 :         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
     357    35192606 :         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    50024360 :         while (level-- > 0) {
     364             :                 /* See if buf is in cur first */
     365    50024220 :                 bp_release = 0;
     366    50024220 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     367    50024693 :                 if (!bp) {
     368    21344150 :                         bp_release = 1;
     369    21344150 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     370             :                                                 XFS_BMAP_BTREE_REF,
     371             :                                                 &xfs_bmbt_buf_ops);
     372    21344158 :                         if (xfs_metadata_is_sick(error))
     373           0 :                                 xfs_btree_mark_sick(cur);
     374    21344158 :                         if (error)
     375           5 :                                 goto error_norelse;
     376             :                 }
     377    50024696 :                 block = XFS_BUF_TO_BLOCK(bp);
     378    50024696 :                 if (level == 0)
     379             :                         break;
     380             : 
     381             :                 /*
     382             :                  * Check this block for basic sanity (increasing keys and
     383             :                  * no duplicate blocks).
     384             :                  */
     385             : 
     386    14831822 :                 xfs_check_block(block, mp, 0, 0);
     387    14831817 :                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
     388    14831817 :                 bno = be64_to_cpu(*pp);
     389    14831817 :                 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    14831754 :                 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   837856540 :         for (;;) {
     409   436524777 :                 xfs_fsblock_t   nextbno;
     410   436524777 :                 xfs_extnum_t    num_recs;
     411             : 
     412             : 
     413   436524777 :                 num_recs = xfs_btree_get_numrecs(block);
     414             : 
     415             :                 /*
     416             :                  * Read-ahead the next leaf block, if any.
     417             :                  */
     418             : 
     419   436524777 :                 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   436524777 :                 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
     428   436524777 :                 if (i) {
     429   401332413 :                         ASSERT(xfs_bmbt_disk_get_startoff(&last) +
     430             :                                xfs_bmbt_disk_get_blockcount(&last) <=
     431             :                                xfs_bmbt_disk_get_startoff(ep));
     432             :                 }
     433 >10349*10^7 :                 for (j = 1; j < num_recs; j++) {
     434 >10306*10^7 :                         nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
     435 >10306*10^7 :                         ASSERT(xfs_bmbt_disk_get_startoff(ep) +
     436             :                                xfs_bmbt_disk_get_blockcount(ep) <=
     437             :                                xfs_bmbt_disk_get_startoff(nextp));
     438 >10306*10^7 :                         ep = nextp;
     439             :                 }
     440             : 
     441   436525213 :                 last = *ep;
     442   436525213 :                 i += num_recs;
     443   436525213 :                 if (bp_release) {
     444   390576155 :                         bp_release = 0;
     445   390576155 :                         xfs_trans_brelse(NULL, bp);
     446             :                 }
     447   436526161 :                 bno = nextbno;
     448             :                 /*
     449             :                  * If we've reached the end, stop.
     450             :                  */
     451   436526161 :                 if (bno == NULLFSBLOCK)
     452             :                         break;
     453             : 
     454   401333168 :                 bp_release = 0;
     455   401333168 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     456   401331354 :                 if (!bp) {
     457   369232422 :                         bp_release = 1;
     458   369232422 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     459             :                                                 XFS_BMAP_BTREE_REF,
     460             :                                                 &xfs_bmbt_buf_ops);
     461   369232886 :                         if (xfs_metadata_is_sick(error))
     462           0 :                                 xfs_btree_mark_sick(cur);
     463   369232886 :                         if (error)
     464          42 :                                 goto error_norelse;
     465             :                 }
     466   401331776 :                 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          47 :         xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
     477             :                 __func__, i);
     478          47 :         xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
     479          47 :         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     480          47 :         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    76199331 : 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    76199331 :         int                     i;              /* index to map values */
     499             : 
     500    76199331 :         ASSERT(ret_nmap <= nmap);
     501             : 
     502   152398202 :         for (i = 0; i < ret_nmap; i++) {
     503    76198860 :                 ASSERT(mval[i].br_blockcount > 0);
     504    76198860 :                 if (!(flags & XFS_BMAPI_ENTIRE)) {
     505    76198860 :                         ASSERT(mval[i].br_startoff >= bno);
     506    76198860 :                         ASSERT(mval[i].br_blockcount <= len);
     507    76198860 :                         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    76198860 :                 ASSERT(i == 0 ||
     515             :                        mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
     516             :                        mval[i].br_startoff);
     517    76198860 :                 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
     518             :                        mval[i].br_startblock != HOLESTARTBLOCK);
     519    76198860 :                 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
     520             :                        mval[i].br_state == XFS_EXT_UNWRITTEN);
     521             :         }
     522    76199342 : }
     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   222739390 : 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   222739390 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     549   222755499 :         struct xfs_mount        *mp = ip->i_mount;
     550   222755499 :         struct xfs_btree_block  *rblock = ifp->if_broot;
     551   222755499 :         struct xfs_btree_block  *cblock;/* child btree block */
     552   222755499 :         xfs_fsblock_t           cbno;   /* child block number */
     553   222755499 :         struct xfs_buf          *cbp;   /* child block's buffer */
     554   222755499 :         int                     error;  /* error return value */
     555   222755499 :         __be64                  *pp;    /* ptr to block address */
     556   222755499 :         struct xfs_owner_info   oinfo;
     557             : 
     558             :         /* check if we actually need the extent format first: */
     559   222755499 :         if (!xfs_bmap_wants_extents(ip, whichfork))
     560             :                 return 0;
     561             : 
     562      446626 :         ASSERT(cur);
     563      446626 :         ASSERT(whichfork != XFS_COW_FORK);
     564      446626 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
     565      446626 :         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
     566      446626 :         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
     567      446626 :         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
     568             : 
     569      446626 :         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
     570      446626 :         cbno = be64_to_cpu(*pp);
     571             : #ifdef DEBUG
     572      446626 :         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      446626 :         error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
     578             :                                 &xfs_bmbt_buf_ops);
     579      446626 :         if (xfs_metadata_is_sick(error))
     580           0 :                 xfs_btree_mark_sick(cur);
     581      446626 :         if (error)
     582             :                 return error;
     583      446626 :         cblock = XFS_BUF_TO_BLOCK(cbp);
     584      446626 :         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
     585             :                 return error;
     586             : 
     587      446626 :         xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
     588      446626 :         error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
     589             :                         XFS_AG_RESV_NONE);
     590      446626 :         if (error)
     591             :                 return error;
     592             : 
     593      446626 :         ip->i_nblocks--;
     594      446626 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     595      446626 :         xfs_trans_binval(tp, cbp);
     596      446626 :         if (cur->bc_levels[0].bp == cbp)
     597      446626 :                 cur->bc_levels[0].bp = NULL;
     598      446626 :         xfs_iroot_realloc(ip, -1, whichfork);
     599      446626 :         ASSERT(ifp->if_broot == NULL);
     600      446626 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     601      446626 :         *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
     602      446626 :         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      858297 : 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      858297 :         struct xfs_btree_block  *ablock;        /* allocated (child) bt block */
     619      858297 :         struct xfs_buf          *abp;           /* buffer for ablock */
     620      858297 :         struct xfs_alloc_arg    args;           /* allocation arguments */
     621      858297 :         struct xfs_bmbt_rec     *arp;           /* child record pointer */
     622      858297 :         struct xfs_btree_block  *block;         /* btree root block */
     623      858297 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     624      858297 :         int                     error;          /* error return value */
     625      858297 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
     626      858297 :         struct xfs_bmbt_key     *kp;            /* root block key pointer */
     627      858297 :         struct xfs_mount        *mp;            /* mount structure */
     628      858297 :         xfs_bmbt_ptr_t          *pp;            /* root block address pointer */
     629      858297 :         struct xfs_iext_cursor  icur;
     630      858297 :         struct xfs_bmbt_irec    rec;
     631      858297 :         xfs_extnum_t            cnt = 0;
     632             : 
     633      858297 :         mp = ip->i_mount;
     634      858297 :         ASSERT(whichfork != XFS_COW_FORK);
     635      858297 :         ifp = xfs_ifork_ptr(ip, whichfork);
     636      858297 :         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      858297 :         xfs_iroot_realloc(ip, 1, whichfork);
     643             : 
     644             :         /*
     645             :          * Fill in the root.
     646             :          */
     647      858298 :         block = ifp->if_broot;
     648      858298 :         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      858296 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
     653      858297 :         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      858297 :         ifp->if_format = XFS_DINODE_FMT_BTREE;
     658      858297 :         memset(&args, 0, sizeof(args));
     659      858297 :         args.tp = tp;
     660      858297 :         args.mp = mp;
     661      858297 :         xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
     662             : 
     663      858297 :         args.minlen = args.maxlen = args.prod = 1;
     664      858297 :         args.wasdel = wasdel;
     665      858297 :         *logflagsp = 0;
     666      858297 :         error = xfs_alloc_vextent_start_ag(&args,
     667      858297 :                                 XFS_INO_TO_FSB(mp, ip->i_ino));
     668      858299 :         if (error)
     669          53 :                 goto out_root_realloc;
     670             : 
     671             :         /*
     672             :          * Allocation can't fail, the space was reserved.
     673             :          */
     674      858246 :         if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
     675           0 :                 error = -ENOSPC;
     676           0 :                 goto out_root_realloc;
     677             :         }
     678             : 
     679      858246 :         cur->bc_ino.allocated++;
     680      858246 :         ip->i_nblocks++;
     681      858246 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     682      858246 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
     683      858246 :                         XFS_FSB_TO_DADDR(mp, args.fsbno),
     684             :                         mp->m_bsize, 0, &abp);
     685      858246 :         if (error)
     686           0 :                 goto out_unreserve_dquot;
     687             : 
     688             :         /*
     689             :          * Fill in the child block.
     690             :          */
     691      858246 :         ablock = XFS_BUF_TO_BLOCK(abp);
     692      858246 :         xfs_btree_init_buf(mp, abp, &xfs_bmbt_ops, 0, 0, ip->i_ino);
     693             : 
     694    12114812 :         for_each_xfs_iext(ifp, &icur, &rec) {
     695    11256566 :                 if (isnullstartblock(rec.br_startblock))
     696      206132 :                         continue;
     697    11050434 :                 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
     698    11050434 :                 xfs_bmbt_disk_set_all(arp, &rec);
     699    11050434 :                 cnt++;
     700             :         }
     701      858246 :         ASSERT(cnt == ifp->if_nextents);
     702      858246 :         xfs_btree_set_numrecs(ablock, cnt);
     703             : 
     704             :         /*
     705             :          * Fill in the root key and pointer.
     706             :          */
     707      858246 :         kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
     708      858246 :         arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
     709      858246 :         kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
     710      858246 :         pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
     711             :                                                 be16_to_cpu(block->bb_level)));
     712      858246 :         *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      858246 :         xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
     719      858246 :         xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
     720      858246 :         ASSERT(*curp == NULL);
     721      858246 :         *curp = cur;
     722      858246 :         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
     723      858246 :         return 0;
     724             : 
     725             : out_unreserve_dquot:
     726           0 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     727          53 : out_root_realloc:
     728          53 :         xfs_iroot_realloc(ip, -1, whichfork);
     729          53 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     730          53 :         ASSERT(ifp->if_broot == NULL);
     731          53 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     732             : 
     733          53 :         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     4575666 : xfs_bmap_local_to_extents_empty(
     744             :         struct xfs_trans        *tp,
     745             :         struct xfs_inode        *ip,
     746             :         int                     whichfork)
     747             : {
     748     4575666 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     749             : 
     750     4575710 :         ASSERT(whichfork != XFS_COW_FORK);
     751     4575710 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     752     4575710 :         ASSERT(ifp->if_bytes == 0);
     753     4575710 :         ASSERT(ifp->if_nextents == 0);
     754             : 
     755     4575710 :         xfs_bmap_forkoff_reset(ip, whichfork);
     756     4575662 :         ifp->if_u1.if_root = NULL;
     757     4575662 :         ifp->if_height = 0;
     758     4575662 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     759     4575662 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     760     4575687 : }
     761             : 
     762             : 
     763             : int                                     /* error */
     764         559 : 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         559 :         int             error = 0;
     777         559 :         int             flags;          /* logging flags returned */
     778         559 :         struct xfs_ifork *ifp;          /* inode fork pointer */
     779         559 :         xfs_alloc_arg_t args;           /* allocation arguments */
     780         559 :         struct xfs_buf  *bp;            /* buffer for extent block */
     781         559 :         struct xfs_bmbt_irec rec;
     782         559 :         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         559 :         ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
     789         559 :         ifp = xfs_ifork_ptr(ip, whichfork);
     790         559 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     791             : 
     792         559 :         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         559 :         flags = 0;
     799         559 :         error = 0;
     800         559 :         memset(&args, 0, sizeof(args));
     801         559 :         args.tp = tp;
     802         559 :         args.mp = ip->i_mount;
     803         559 :         args.total = total;
     804         559 :         args.minlen = args.maxlen = args.prod = 1;
     805         559 :         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         559 :         args.total = total;
     812         559 :         args.minlen = args.maxlen = args.prod = 1;
     813         559 :         error = xfs_alloc_vextent_start_ag(&args,
     814         559 :                         XFS_INO_TO_FSB(args.mp, ip->i_ino));
     815         559 :         if (error)
     816           0 :                 goto done;
     817             : 
     818             :         /* Can't fail, the space was reserved. */
     819         559 :         ASSERT(args.fsbno != NULLFSBLOCK);
     820         559 :         ASSERT(args.len == 1);
     821         559 :         error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
     822         559 :                         XFS_FSB_TO_DADDR(args.mp, args.fsbno),
     823             :                         args.mp->m_bsize, 0, &bp);
     824         559 :         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         559 :         init_fn(tp, bp, ip, ifp, priv);
     836             : 
     837             :         /* account for the change in fork size */
     838         559 :         xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
     839         559 :         xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     840         559 :         flags |= XFS_ILOG_CORE;
     841             : 
     842         559 :         ifp->if_u1.if_root = NULL;
     843         559 :         ifp->if_height = 0;
     844             : 
     845         559 :         rec.br_startoff = 0;
     846         559 :         rec.br_startblock = args.fsbno;
     847         559 :         rec.br_blockcount = 1;
     848         559 :         rec.br_state = XFS_EXT_NORM;
     849         559 :         xfs_iext_first(ifp, &icur);
     850         559 :         xfs_iext_insert(ip, &icur, &rec, 0);
     851             : 
     852         559 :         ifp->if_nextents = 1;
     853         559 :         ip->i_nblocks = 1;
     854         559 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     855         559 :         flags |= xfs_ilog_fext(whichfork);
     856             : 
     857         559 : done:
     858         559 :         *logflagsp = flags;
     859         559 :         return error;
     860             : }
     861             : 
     862             : /*
     863             :  * Called from xfs_bmap_add_attrfork to handle btree format files.
     864             :  */
     865             : STATIC int                                      /* error */
     866          11 : 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          11 :         struct xfs_btree_block  *block = ip->i_df.if_broot;
     872          11 :         struct xfs_btree_cur    *cur;           /* btree cursor */
     873          11 :         int                     error;          /* error return value */
     874          11 :         xfs_mount_t             *mp;            /* file system mount struct */
     875          11 :         int                     stat;           /* newroot status */
     876             : 
     877          11 :         mp = ip->i_mount;
     878             : 
     879          11 :         if (XFS_BMAP_BMDR_SPACE(block) <= xfs_inode_data_fork_size(ip))
     880          11 :                 *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      197692 : 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      197692 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     917      197692 :         int                     error;          /* error return value */
     918             : 
     919      197692 :         if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
     920      197692 :             xfs_inode_data_fork_size(ip))
     921             :                 return 0;
     922           9 :         cur = NULL;
     923           9 :         error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
     924             :                                           XFS_DATA_FORK);
     925           9 :         if (cur) {
     926           9 :                 cur->bc_ino.allocated = 0;
     927           9 :                 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         367 : 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         367 :         struct xfs_da_args      dargs;          /* args for dir/attr code */
     950             : 
     951         367 :         if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
     952             :                 return 0;
     953             : 
     954           0 :         if (S_ISDIR(VFS_I(ip)->i_mode)) {
     955           0 :                 memset(&dargs, 0, sizeof(dargs));
     956           0 :                 dargs.geo = ip->i_mount->m_dir_geo;
     957           0 :                 dargs.dp = ip;
     958           0 :                 dargs.total = dargs.geo->fsbcount;
     959           0 :                 dargs.whichfork = XFS_DATA_FORK;
     960           0 :                 dargs.trans = tp;
     961           0 :                 dargs.owner = ip->i_ino;
     962           0 :                 return xfs_dir2_sf_to_block(&dargs);
     963             :         }
     964             : 
     965           0 :         if (S_ISLNK(VFS_I(ip)->i_mode))
     966           0 :                 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      198073 : xfs_bmap_set_attrforkoff(
     981             :         struct xfs_inode        *ip,
     982             :         int                     size,
     983             :         int                     *version)
     984             : {
     985      198073 :         int                     default_size = xfs_default_attroffset(ip) >> 3;
     986             : 
     987      198073 :         switch (ip->i_df.if_format) {
     988           0 :         case XFS_DINODE_FMT_DEV:
     989           0 :                 ip->i_forkoff = default_size;
     990           0 :                 break;
     991      198073 :         case XFS_DINODE_FMT_LOCAL:
     992             :         case XFS_DINODE_FMT_EXTENTS:
     993             :         case XFS_DINODE_FMT_BTREE:
     994      198073 :                 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
     995      198063 :                 if (!ip->i_forkoff)
     996           0 :                         ip->i_forkoff = default_size;
     997      198063 :                 else if (xfs_has_attr2(ip->i_mount) && version)
     998      198063 :                         *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      352372 : 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      352372 :         xfs_mount_t             *mp;            /* mount structure */
    1019      352372 :         xfs_trans_t             *tp;            /* transaction pointer */
    1020      352372 :         int                     blks;           /* space reservation */
    1021      352372 :         int                     version = 1;    /* superblock attr version */
    1022      352372 :         int                     logflags;       /* logging flags */
    1023      352372 :         int                     error;          /* error return value */
    1024             : 
    1025      352372 :         mp = ip->i_mount;
    1026      352372 :         ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
    1027             : 
    1028      352372 :         blks = XFS_ADDAFORK_SPACE_RES(mp);
    1029             : 
    1030      352372 :         error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
    1031             :                         rsvd, &tp);
    1032      352375 :         if (error)
    1033             :                 return error;
    1034      352375 :         if (xfs_inode_has_attr_fork(ip))
    1035      154302 :                 goto trans_cancel;
    1036             : 
    1037      198073 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    1038      198073 :         error = xfs_bmap_set_attrforkoff(ip, size, &version);
    1039      198063 :         if (error)
    1040           0 :                 goto trans_cancel;
    1041             : 
    1042      198063 :         xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
    1043      198064 :         logflags = 0;
    1044      198064 :         switch (ip->i_df.if_format) {
    1045         367 :         case XFS_DINODE_FMT_LOCAL:
    1046         367 :                 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
    1047         367 :                 break;
    1048      197686 :         case XFS_DINODE_FMT_EXTENTS:
    1049      197686 :                 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
    1050      197686 :                 break;
    1051          11 :         case XFS_DINODE_FMT_BTREE:
    1052          11 :                 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
    1053          11 :                 break;
    1054             :         default:
    1055             :                 error = 0;
    1056             :                 break;
    1057             :         }
    1058      198064 :         if (logflags)
    1059          20 :                 xfs_trans_log_inode(tp, ip, logflags);
    1060      198063 :         if (error)
    1061           0 :                 goto trans_cancel;
    1062      198063 :         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      198063 :         error = xfs_trans_commit(tp);
    1081      198073 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1082      198073 :         return error;
    1083             : 
    1084      154302 : trans_cancel:
    1085      154302 :         xfs_trans_cancel(tp);
    1086      154302 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1087      154302 :         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     5773545 : xfs_iread_bmbt_block(
    1130             :         struct xfs_btree_cur    *cur,
    1131             :         int                     level,
    1132             :         void                    *priv)
    1133             : {
    1134     5773545 :         struct xfs_iread_state  *ir = priv;
    1135     5773545 :         struct xfs_mount        *mp = cur->bc_mp;
    1136     5773545 :         struct xfs_inode        *ip = cur->bc_ino.ip;
    1137     5773545 :         struct xfs_btree_block  *block;
    1138     5773545 :         struct xfs_buf          *bp;
    1139     5773545 :         struct xfs_bmbt_rec     *frp;
    1140     5773545 :         xfs_extnum_t            num_recs;
    1141     5773545 :         xfs_extnum_t            j;
    1142     5773545 :         int                     whichfork = cur->bc_ino.whichfork;
    1143     5773545 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1144             : 
    1145     5773541 :         block = xfs_btree_get_block(cur, level, &bp);
    1146             : 
    1147             :         /* Abort if we find more records than nextents. */
    1148     5773537 :         num_recs = xfs_btree_get_numrecs(block);
    1149     5773537 :         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     5773537 :         frp = XFS_BMBT_REC_ADDR(mp, block, 1);
    1160   972772572 :         for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
    1161   966999032 :                 struct xfs_bmbt_irec    new;
    1162   966999032 :                 xfs_failaddr_t          fa;
    1163             : 
    1164   966999032 :                 xfs_bmbt_disk_get_all(frp, &new);
    1165   966998143 :                 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
    1166   966999258 :                 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  1933993546 :                 xfs_iext_insert(ip, &ir->icur, &new,
    1175             :                                 xfs_bmap_fork_to_state(whichfork));
    1176  1933991094 :                 trace_xfs_read_extent(ip, &ir->icur,
    1177   966998032 :                                 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
    1178   966998566 :                 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  4451562865 : xfs_iread_extents(
    1189             :         struct xfs_trans        *tp,
    1190             :         struct xfs_inode        *ip,
    1191             :         int                     whichfork)
    1192             : {
    1193  4451562865 :         struct xfs_iread_state  ir;
    1194  4451562865 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1195  4453142112 :         struct xfs_mount        *mp = ip->i_mount;
    1196  4453142112 :         struct xfs_btree_cur    *cur;
    1197  4453142112 :         int                     error;
    1198             : 
    1199  4453142112 :         if (!xfs_need_iread_extents(ifp))
    1200             :                 return 0;
    1201             : 
    1202     1252869 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1203             : 
    1204     1252869 :         ir.loaded = 0;
    1205     1252869 :         xfs_iext_first(ifp, &ir.icur);
    1206     1252869 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    1207     1252867 :         error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
    1208             :                         XFS_BTREE_VISIT_RECORDS, &ir);
    1209     1252855 :         xfs_btree_del_cursor(cur, error);
    1210     1252867 :         if (error)
    1211         198 :                 goto out;
    1212             : 
    1213     1252669 :         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     1252669 :         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     1252669 :         smp_store_release(&ifp->if_needextents, 0);
    1225     1252669 :         return 0;
    1226         198 : out:
    1227         198 :         if (xfs_metadata_is_sick(error))
    1228           2 :                 xfs_bmap_mark_sick(ip, whichfork);
    1229         198 :         xfs_iext_destroy(ifp);
    1230         198 :         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     4750991 : 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     4750991 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1248     4751042 :         struct xfs_bmbt_irec    got;
    1249     4751042 :         struct xfs_iext_cursor  icur;
    1250     4751042 :         xfs_fileoff_t           lastaddr = 0;
    1251     4751042 :         xfs_fileoff_t           lowest, max;
    1252     4751042 :         int                     error;
    1253             : 
    1254     4751042 :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
    1255           0 :                 *first_unused = 0;
    1256           0 :                 return 0;
    1257             :         }
    1258             : 
    1259     4751042 :         ASSERT(xfs_ifork_has_extents(ifp));
    1260             : 
    1261     4751042 :         error = xfs_iread_extents(tp, ip, whichfork);
    1262     4751010 :         if (error)
    1263             :                 return error;
    1264             : 
    1265     4751102 :         lowest = max = *first_unused;
    1266    16779225 :         for_each_xfs_iext(ifp, &icur, &got) {
    1267             :                 /*
    1268             :                  * See if the hole before this extent will work.
    1269             :                  */
    1270    12098478 :                 if (got.br_startoff >= lowest + len &&
    1271     8763621 :                     got.br_startoff - max >= len)
    1272             :                         break;
    1273    12028283 :                 lastaddr = got.br_startoff + got.br_blockcount;
    1274    12028283 :                 max = XFS_FILEOFF_MAX(lastaddr, lowest);
    1275             :         }
    1276             : 
    1277     4751003 :         *first_unused = max;
    1278     4751003 :         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      239655 : 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      239655 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1295      239655 :         struct xfs_bmbt_irec    got;
    1296      239655 :         struct xfs_iext_cursor  icur;
    1297      239655 :         int                     error;
    1298             : 
    1299      239655 :         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      239655 :                 break;
    1306           0 :         default:
    1307           0 :                 ASSERT(0);
    1308           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    1309           0 :                 return -EFSCORRUPTED;
    1310             :         }
    1311             : 
    1312      239655 :         error = xfs_iread_extents(tp, ip, whichfork);
    1313      239655 :         if (error)
    1314             :                 return error;
    1315             : 
    1316      239655 :         if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
    1317      217278 :                 *last_block = 0;
    1318             :         return 0;
    1319             : }
    1320             : 
    1321             : int
    1322   475199889 : 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   475199889 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1330   475233301 :         struct xfs_iext_cursor  icur;
    1331   475233301 :         int                     error;
    1332             : 
    1333   475233301 :         error = xfs_iread_extents(tp, ip, whichfork);
    1334   475322702 :         if (error)
    1335             :                 return error;
    1336             : 
    1337   475305742 :         xfs_iext_last(ifp, &icur);
    1338   475314394 :         if (!xfs_iext_get_extent(ifp, &icur, rec))
    1339    18061586 :                 *is_empty = 1;
    1340             :         else
    1341   457260922 :                 *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        6443 : xfs_bmap_isaeof(
    1356             :         struct xfs_bmalloca     *bma,
    1357             :         int                     whichfork)
    1358             : {
    1359        6443 :         struct xfs_bmbt_irec    rec;
    1360        6443 :         int                     is_empty;
    1361        6443 :         int                     error;
    1362             : 
    1363        6443 :         bma->aeof = false;
    1364        6443 :         error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
    1365             :                                      &is_empty);
    1366        6444 :         if (error)
    1367             :                 return error;
    1368             : 
    1369        6444 :         if (is_empty) {
    1370        1231 :                 bma->aeof = true;
    1371        1231 :                 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        5213 :         bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
    1379        1274 :                 (bma->offset >= rec.br_startoff &&
    1380        1274 :                  isnullstartblock(rec.br_startblock));
    1381        5213 :         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   474798102 : xfs_bmap_last_offset(
    1391             :         struct xfs_inode        *ip,
    1392             :         xfs_fileoff_t           *last_block,
    1393             :         int                     whichfork)
    1394             : {
    1395   474798102 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1396   474839686 :         struct xfs_bmbt_irec    rec;
    1397   474839686 :         int                     is_empty;
    1398   474839686 :         int                     error;
    1399             : 
    1400   474839686 :         *last_block = 0;
    1401             : 
    1402   474839686 :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
    1403             :                 return 0;
    1404             : 
    1405   474839686 :         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   474839686 :         error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
    1411   474964766 :         if (error || is_empty)
    1412             :                 return error;
    1413             : 
    1414   456912650 :         *last_block = rec.br_startoff + rec.br_blockcount;
    1415   456912650 :         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    15729650 : xfs_bmap_add_extent_delay_real(
    1427             :         struct xfs_bmalloca     *bma,
    1428             :         int                     whichfork)
    1429             : {
    1430    15729650 :         struct xfs_mount        *mp = bma->ip->i_mount;
    1431    15729650 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    1432    15729656 :         struct xfs_bmbt_irec    *new = &bma->got;
    1433    15729656 :         int                     error;  /* error return value */
    1434    15729656 :         int                     i;      /* temp state */
    1435    15729656 :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    1436    15729656 :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    1437             :                                         /* left is 0, right is 1, prev is 2 */
    1438    15729656 :         int                     rval=0; /* return value (logging flags) */
    1439    15729656 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    1440    15729656 :         xfs_filblks_t           da_new; /* new count del alloc blocks used */
    1441    15729656 :         xfs_filblks_t           da_old; /* old count del alloc blocks used */
    1442    15729656 :         xfs_filblks_t           temp=0; /* value for da_new calculations */
    1443    15729656 :         int                     tmp_rval;       /* partial logging flags */
    1444    15729656 :         struct xfs_bmbt_irec    old;
    1445             : 
    1446    15729656 :         ASSERT(whichfork != XFS_ATTR_FORK);
    1447    15729656 :         ASSERT(!isnullstartblock(new->br_startblock));
    1448    15729656 :         ASSERT(!bma->cur ||
    1449             :                (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    1450             : 
    1451    15729656 :         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    15729656 :         xfs_iext_get_extent(ifp, &bma->icur, &PREV);
    1461    15729657 :         new_endoff = new->br_startoff + new->br_blockcount;
    1462    15729657 :         ASSERT(isnullstartblock(PREV.br_startblock));
    1463    15729657 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    1464    15729657 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    1465             : 
    1466    15729657 :         da_old = startblockval(PREV.br_startblock);
    1467    15729657 :         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    15729657 :         if (PREV.br_startoff == new->br_startoff)
    1474    15729664 :                 state |= BMAP_LEFT_FILLING;
    1475    15729657 :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    1476    15497896 :                 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    15729657 :         if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
    1483    10557601 :                 state |= BMAP_LEFT_VALID;
    1484    10557601 :                 if (isnullstartblock(LEFT.br_startblock))
    1485       44518 :                         state |= BMAP_LEFT_DELAY;
    1486             :         }
    1487             : 
    1488    15729637 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    1489    10513075 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    1490      872128 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    1491      196792 :             LEFT.br_state == new->br_state &&
    1492       93771 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    1493       89543 :                 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    15729637 :         if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
    1501     7599912 :                 state |= BMAP_RIGHT_VALID;
    1502     7599912 :                 if (isnullstartblock(RIGHT.br_startblock))
    1503      787161 :                         state |= BMAP_RIGHT_DELAY;
    1504             :         }
    1505             : 
    1506    15729628 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    1507     6812752 :             new_endoff == RIGHT.br_startoff &&
    1508      582262 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    1509       54026 :             new->br_state == RIGHT.br_state &&
    1510       25410 :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    1511       25410 :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1512             :                        BMAP_RIGHT_FILLING)) !=
    1513             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1514       11517 :                        BMAP_RIGHT_FILLING) ||
    1515       11517 :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    1516             :                         <= XFS_MAX_BMBT_EXTLEN))
    1517       25410 :                 state |= BMAP_RIGHT_CONTIG;
    1518             : 
    1519    15729628 :         error = 0;
    1520             :         /*
    1521             :          * Switch out based on the FILLING and CONTIG state bits.
    1522             :          */
    1523    15729628 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1524             :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    1525       11517 :         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       11517 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    1532             : 
    1533       11517 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1534       11517 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1535       11517 :                 xfs_iext_prev(ifp, &bma->icur);
    1536       11517 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1537       11517 :                 ifp->if_nextents--;
    1538             : 
    1539       11517 :                 if (bma->cur == NULL)
    1540             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1541             :                 else {
    1542        1837 :                         rval = XFS_ILOG_CORE;
    1543        1837 :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1544        1837 :                         if (error)
    1545           0 :                                 goto done;
    1546        1837 :                         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        1837 :                         error = xfs_btree_delete(bma->cur, &i);
    1552        1837 :                         if (error)
    1553           0 :                                 goto done;
    1554        1837 :                         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        1837 :                         error = xfs_btree_decrement(bma->cur, 0, &i);
    1560        1837 :                         if (error)
    1561           0 :                                 goto done;
    1562        1837 :                         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        1837 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1568        1837 :                         if (error)
    1569           0 :                                 goto done;
    1570             :                 }
    1571             :                 break;
    1572             : 
    1573       63754 :         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       63754 :                 old = LEFT;
    1579       63754 :                 LEFT.br_blockcount += PREV.br_blockcount;
    1580             : 
    1581       63754 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1582       63754 :                 xfs_iext_prev(ifp, &bma->icur);
    1583       63754 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1584             : 
    1585       63754 :                 if (bma->cur == NULL)
    1586             :                         rval = XFS_ILOG_DEXT;
    1587             :                 else {
    1588        7787 :                         rval = 0;
    1589        7787 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1590        7787 :                         if (error)
    1591           0 :                                 goto done;
    1592        7787 :                         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        7787 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1598        7787 :                         if (error)
    1599           0 :                                 goto done;
    1600             :                 }
    1601             :                 break;
    1602             : 
    1603       13893 :         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       13893 :                 PREV.br_startblock = new->br_startblock;
    1611       13893 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    1612       13893 :                 PREV.br_state = new->br_state;
    1613             : 
    1614       13893 :                 xfs_iext_next(ifp, &bma->icur);
    1615       13893 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1616       13893 :                 xfs_iext_prev(ifp, &bma->icur);
    1617       13893 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1618             : 
    1619       13893 :                 if (bma->cur == NULL)
    1620             :                         rval = XFS_ILOG_DEXT;
    1621             :                 else {
    1622        1769 :                         rval = 0;
    1623        1769 :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1624        1769 :                         if (error)
    1625           0 :                                 goto done;
    1626        1769 :                         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        1769 :                         error = xfs_bmbt_update(bma->cur, &PREV);
    1632        1769 :                         if (error)
    1633           0 :                                 goto done;
    1634             :                 }
    1635             :                 break;
    1636             : 
    1637    15408700 :         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    15408700 :                 PREV.br_startblock = new->br_startblock;
    1644    15408700 :                 PREV.br_state = new->br_state;
    1645    15408700 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1646    15408702 :                 ifp->if_nextents++;
    1647             : 
    1648    15408702 :                 if (bma->cur == NULL)
    1649             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1650             :                 else {
    1651     2279366 :                         rval = XFS_ILOG_CORE;
    1652     2279366 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1653     2279366 :                         if (error)
    1654           1 :                                 goto done;
    1655     2279365 :                         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     2279365 :                         error = xfs_btree_insert(bma->cur, &i);
    1661     2279365 :                         if (error)
    1662           0 :                                 goto done;
    1663     2279365 :                         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       14272 :         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       14272 :                 old = LEFT;
    1677       14272 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1678       14272 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1679             :                                 startblockval(PREV.br_startblock));
    1680             : 
    1681       14272 :                 LEFT.br_blockcount += new->br_blockcount;
    1682             : 
    1683       14272 :                 PREV.br_blockcount = temp;
    1684       14272 :                 PREV.br_startoff += new->br_blockcount;
    1685       14272 :                 PREV.br_startblock = nullstartblock(da_new);
    1686             : 
    1687       14272 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1688       14272 :                 xfs_iext_prev(ifp, &bma->icur);
    1689       14272 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1690             : 
    1691       14272 :                 if (bma->cur == NULL)
    1692             :                         rval = XFS_ILOG_DEXT;
    1693             :                 else {
    1694       11873 :                         rval = 0;
    1695       11873 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1696       11873 :                         if (error)
    1697           0 :                                 goto done;
    1698       11873 :                         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       11873 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1704       11873 :                         if (error)
    1705           0 :                                 goto done;
    1706             :                 }
    1707             :                 break;
    1708             : 
    1709      217492 :         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      217492 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1715      217495 :                 ifp->if_nextents++;
    1716             : 
    1717      217495 :                 if (bma->cur == NULL)
    1718             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1719             :                 else {
    1720      183567 :                         rval = XFS_ILOG_CORE;
    1721      183567 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1722      183565 :                         if (error)
    1723           0 :                                 goto done;
    1724      183565 :                         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      183565 :                         error = xfs_btree_insert(bma->cur, &i);
    1730      183566 :                         if (error)
    1731           0 :                                 goto done;
    1732      183566 :                         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      217494 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1740        2057 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1741             :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1742        2057 :                         rval |= tmp_rval;
    1743        2057 :                         if (error)
    1744           0 :                                 goto done;
    1745             :                 }
    1746             : 
    1747      217495 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1748      217495 :                 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      217496 :                 PREV.br_startoff = new_endoff;
    1753      217496 :                 PREV.br_blockcount = temp;
    1754      217496 :                 PREV.br_startblock = nullstartblock(da_new);
    1755      217496 :                 xfs_iext_next(ifp, &bma->icur);
    1756      217495 :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1757      217496 :                 xfs_iext_prev(ifp, &bma->icur);
    1758      217496 :                 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    15729633 :         if (!(bma->flags & XFS_BMAPI_NORMAP))
    1944    15729618 :                 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
    1945             : 
    1946             :         /* convert to a btree if necessary */
    1947    15729636 :         if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1948      315975 :                 int     tmp_logflags;   /* partial log flag return val */
    1949             : 
    1950      315975 :                 ASSERT(bma->cur == NULL);
    1951      315975 :                 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1952             :                                 &bma->cur, da_old > 0, &tmp_logflags,
    1953             :                                 whichfork);
    1954      315975 :                 bma->logflags |= tmp_logflags;
    1955      315975 :                 if (error)
    1956          39 :                         goto done;
    1957             :         }
    1958             : 
    1959    15729603 :         if (da_new != da_old)
    1960    15500538 :                 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
    1961             : 
    1962    15729556 :         if (bma->cur) {
    1963     2804161 :                 da_new += bma->cur->bc_ino.allocated;
    1964     2804161 :                 bma->cur->bc_ino.allocated = 0;
    1965             :         }
    1966             : 
    1967             :         /* adjust for changes in reserved delayed indirect blocks */
    1968    15729556 :         if (da_new != da_old) {
    1969    15498176 :                 ASSERT(state == 0 || da_new < da_old);
    1970    15498176 :                 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
    1971             :                                 false);
    1972             :         }
    1973             : 
    1974    15729589 :         xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
    1975    15729600 : done:
    1976    15729600 :         if (whichfork != XFS_COW_FORK)
    1977    15612263 :                 bma->logflags |= rval;
    1978    15729600 :         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    26537967 : 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    26537967 :         struct xfs_btree_cur    *cur;   /* btree cursor */
    1998    26537967 :         int                     error;  /* error return value */
    1999    26537967 :         int                     i;      /* temp state */
    2000    26537967 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    2001    26537967 :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    2002    26537967 :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    2003             :                                         /* left is 0, right is 1, prev is 2 */
    2004    26537967 :         int                     rval=0; /* return value (logging flags) */
    2005    26537967 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2006    26537967 :         struct xfs_mount        *mp = ip->i_mount;
    2007    26537967 :         struct xfs_bmbt_irec    old;
    2008             : 
    2009    26537967 :         *logflagsp = 0;
    2010             : 
    2011    26537967 :         cur = *curp;
    2012    26537967 :         ifp = xfs_ifork_ptr(ip, whichfork);
    2013             : 
    2014    26537814 :         ASSERT(!isnullstartblock(new->br_startblock));
    2015             : 
    2016    26537814 :         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    26537814 :         error = 0;
    2026    26537814 :         xfs_iext_get_extent(ifp, icur, &PREV);
    2027    26537949 :         ASSERT(new->br_state != PREV.br_state);
    2028    26537949 :         new_endoff = new->br_startoff + new->br_blockcount;
    2029    26537949 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    2030    26537949 :         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    26537949 :         if (PREV.br_startoff == new->br_startoff)
    2037    24918245 :                 state |= BMAP_LEFT_FILLING;
    2038    26537949 :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    2039    22336822 :                 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    26537949 :         if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
    2046    19172722 :                 state |= BMAP_LEFT_VALID;
    2047    19172722 :                 if (isnullstartblock(LEFT.br_startblock))
    2048      115786 :                         state |= BMAP_LEFT_DELAY;
    2049             :         }
    2050             : 
    2051    26536204 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2052    19057245 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    2053     3634284 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    2054      953095 :             LEFT.br_state == new->br_state &&
    2055      948867 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2056      948867 :                 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    26536204 :         if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
    2064    14564357 :                 state |= BMAP_RIGHT_VALID;
    2065    14564357 :                 if (isnullstartblock(RIGHT.br_startblock))
    2066      143701 :                         state |= BMAP_RIGHT_DELAY;
    2067             :         }
    2068             : 
    2069    26537231 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2070    14420602 :             new_endoff == RIGHT.br_startoff &&
    2071     3036595 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    2072      325374 :             new->br_state == RIGHT.br_state &&
    2073      325373 :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2074      325373 :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2075             :                        BMAP_RIGHT_FILLING)) !=
    2076             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2077       97624 :                        BMAP_RIGHT_FILLING) ||
    2078       97624 :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    2079             :                         <= XFS_MAX_BMBT_EXTLEN))
    2080      325372 :                 state |= BMAP_RIGHT_CONTIG;
    2081             : 
    2082             :         /*
    2083             :          * Switch out based on the FILLING and CONTIG state bits.
    2084             :          */
    2085    26537231 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2086             :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    2087       97623 :         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       97623 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    2094             : 
    2095       97623 :                 xfs_iext_remove(ip, icur, state);
    2096       97623 :                 xfs_iext_remove(ip, icur, state);
    2097       97623 :                 xfs_iext_prev(ifp, icur);
    2098       97623 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2099       97623 :                 ifp->if_nextents -= 2;
    2100       97623 :                 if (cur == NULL)
    2101             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2102             :                 else {
    2103       45980 :                         rval = XFS_ILOG_CORE;
    2104       45980 :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2105       45980 :                         if (error)
    2106           0 :                                 goto done;
    2107       45980 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2108           0 :                                 xfs_btree_mark_sick(cur);
    2109           0 :                                 error = -EFSCORRUPTED;
    2110           0 :                                 goto done;
    2111             :                         }
    2112       45980 :                         if ((error = xfs_btree_delete(cur, &i)))
    2113           0 :                                 goto done;
    2114       45980 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2115           0 :                                 xfs_btree_mark_sick(cur);
    2116           0 :                                 error = -EFSCORRUPTED;
    2117           0 :                                 goto done;
    2118             :                         }
    2119       45980 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2120           0 :                                 goto done;
    2121       45980 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2122           0 :                                 xfs_btree_mark_sick(cur);
    2123           0 :                                 error = -EFSCORRUPTED;
    2124           0 :                                 goto done;
    2125             :                         }
    2126       45980 :                         if ((error = xfs_btree_delete(cur, &i)))
    2127           0 :                                 goto done;
    2128       45980 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2129           0 :                                 xfs_btree_mark_sick(cur);
    2130           0 :                                 error = -EFSCORRUPTED;
    2131           0 :                                 goto done;
    2132             :                         }
    2133       45980 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2134           0 :                                 goto done;
    2135       45980 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2136           0 :                                 xfs_btree_mark_sick(cur);
    2137           0 :                                 error = -EFSCORRUPTED;
    2138           0 :                                 goto done;
    2139             :                         }
    2140       45980 :                         error = xfs_bmbt_update(cur, &LEFT);
    2141       45980 :                         if (error)
    2142           0 :                                 goto done;
    2143             :                 }
    2144             :                 break;
    2145             : 
    2146      531586 :         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      531586 :                 LEFT.br_blockcount += PREV.br_blockcount;
    2152             : 
    2153      531586 :                 xfs_iext_remove(ip, icur, state);
    2154      531586 :                 xfs_iext_prev(ifp, icur);
    2155      531586 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2156      531586 :                 ifp->if_nextents--;
    2157      531586 :                 if (cur == NULL)
    2158             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2159             :                 else {
    2160      165921 :                         rval = XFS_ILOG_CORE;
    2161      165921 :                         error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
    2162      165921 :                         if (error)
    2163           0 :                                 goto done;
    2164      165921 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2165           0 :                                 xfs_btree_mark_sick(cur);
    2166           0 :                                 error = -EFSCORRUPTED;
    2167           0 :                                 goto done;
    2168             :                         }
    2169      165921 :                         if ((error = xfs_btree_delete(cur, &i)))
    2170           0 :                                 goto done;
    2171      165921 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2172           0 :                                 xfs_btree_mark_sick(cur);
    2173           0 :                                 error = -EFSCORRUPTED;
    2174           0 :                                 goto done;
    2175             :                         }
    2176      165921 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2177           0 :                                 goto done;
    2178      165921 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2179           0 :                                 xfs_btree_mark_sick(cur);
    2180           0 :                                 error = -EFSCORRUPTED;
    2181           0 :                                 goto done;
    2182             :                         }
    2183      165921 :                         error = xfs_bmbt_update(cur, &LEFT);
    2184      165921 :                         if (error)
    2185           0 :                                 goto done;
    2186             :                 }
    2187             :                 break;
    2188             : 
    2189      162514 :         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      162514 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    2195      162514 :                 PREV.br_state = new->br_state;
    2196             : 
    2197      162514 :                 xfs_iext_next(ifp, icur);
    2198      162514 :                 xfs_iext_remove(ip, icur, state);
    2199      162514 :                 xfs_iext_prev(ifp, icur);
    2200      162514 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2201      162514 :                 ifp->if_nextents--;
    2202             : 
    2203      162514 :                 if (cur == NULL)
    2204             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2205             :                 else {
    2206       32596 :                         rval = XFS_ILOG_CORE;
    2207       32596 :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2208       32596 :                         if (error)
    2209           0 :                                 goto done;
    2210       32596 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2211           0 :                                 xfs_btree_mark_sick(cur);
    2212           0 :                                 error = -EFSCORRUPTED;
    2213           0 :                                 goto done;
    2214             :                         }
    2215       32596 :                         if ((error = xfs_btree_delete(cur, &i)))
    2216           0 :                                 goto done;
    2217       32596 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2218           0 :                                 xfs_btree_mark_sick(cur);
    2219           0 :                                 error = -EFSCORRUPTED;
    2220           0 :                                 goto done;
    2221             :                         }
    2222       32596 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2223           0 :                                 goto done;
    2224       32596 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2225           0 :                                 xfs_btree_mark_sick(cur);
    2226           0 :                                 error = -EFSCORRUPTED;
    2227           0 :                                 goto done;
    2228             :                         }
    2229       32596 :                         error = xfs_bmbt_update(cur, &PREV);
    2230       32596 :                         if (error)
    2231           0 :                                 goto done;
    2232             :                 }
    2233             :                 break;
    2234             : 
    2235    20958502 :         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    20958502 :                 PREV.br_state = new->br_state;
    2242    20958502 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2243             : 
    2244    20957970 :                 if (cur == NULL)
    2245             :                         rval = XFS_ILOG_DEXT;
    2246             :                 else {
    2247     4923217 :                         rval = 0;
    2248     4923217 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2249     4923240 :                         if (error)
    2250           0 :                                 goto done;
    2251     4923240 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2252           0 :                                 xfs_btree_mark_sick(cur);
    2253           0 :                                 error = -EFSCORRUPTED;
    2254           0 :                                 goto done;
    2255             :                         }
    2256     4923240 :                         error = xfs_bmbt_update(cur, &PREV);
    2257     4923166 :                         if (error)
    2258           0 :                                 goto done;
    2259             :                 }
    2260             :                 break;
    2261             : 
    2262      319658 :         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      319658 :                 LEFT.br_blockcount += new->br_blockcount;
    2268             : 
    2269      319658 :                 old = PREV;
    2270      319658 :                 PREV.br_startoff += new->br_blockcount;
    2271      319658 :                 PREV.br_startblock += new->br_blockcount;
    2272      319658 :                 PREV.br_blockcount -= new->br_blockcount;
    2273             : 
    2274      319658 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2275      319658 :                 xfs_iext_prev(ifp, icur);
    2276      319658 :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2277             : 
    2278      319658 :                 if (cur == NULL)
    2279             :                         rval = XFS_ILOG_DEXT;
    2280             :                 else {
    2281      107168 :                         rval = 0;
    2282      107168 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2283      107168 :                         if (error)
    2284           0 :                                 goto done;
    2285      107168 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2286           0 :                                 xfs_btree_mark_sick(cur);
    2287           0 :                                 error = -EFSCORRUPTED;
    2288           0 :                                 goto done;
    2289             :                         }
    2290      107168 :                         error = xfs_bmbt_update(cur, &PREV);
    2291      107168 :                         if (error)
    2292           0 :                                 goto done;
    2293      107168 :                         error = xfs_btree_decrement(cur, 0, &i);
    2294      107168 :                         if (error)
    2295           0 :                                 goto done;
    2296      107168 :                         error = xfs_bmbt_update(cur, &LEFT);
    2297      107168 :                         if (error)
    2298           0 :                                 goto done;
    2299             :                 }
    2300             :                 break;
    2301             : 
    2302     2847647 :         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     2847647 :                 old = PREV;
    2308     2847647 :                 PREV.br_startoff += new->br_blockcount;
    2309     2847647 :                 PREV.br_startblock += new->br_blockcount;
    2310     2847647 :                 PREV.br_blockcount -= new->br_blockcount;
    2311             : 
    2312     2847647 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2313     2847631 :                 xfs_iext_insert(ip, icur, new, state);
    2314     2847631 :                 ifp->if_nextents++;
    2315             : 
    2316     2847631 :                 if (cur == NULL)
    2317             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2318             :                 else {
    2319      364572 :                         rval = XFS_ILOG_CORE;
    2320      364572 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2321      364572 :                         if (error)
    2322           0 :                                 goto done;
    2323      364572 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2324           0 :                                 xfs_btree_mark_sick(cur);
    2325           0 :                                 error = -EFSCORRUPTED;
    2326           0 :                                 goto done;
    2327             :                         }
    2328      364572 :                         error = xfs_bmbt_update(cur, &PREV);
    2329      364572 :                         if (error)
    2330           0 :                                 goto done;
    2331      364572 :                         cur->bc_rec.b = *new;
    2332      364572 :                         if ((error = xfs_btree_insert(cur, &i)))
    2333           0 :                                 goto done;
    2334      364572 :                         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       65235 :         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       65235 :                 old = PREV;
    2348       65235 :                 PREV.br_blockcount -= new->br_blockcount;
    2349             : 
    2350       65235 :                 RIGHT.br_startoff = new->br_startoff;
    2351       65235 :                 RIGHT.br_startblock = new->br_startblock;
    2352       65235 :                 RIGHT.br_blockcount += new->br_blockcount;
    2353             : 
    2354       65235 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2355       65235 :                 xfs_iext_next(ifp, icur);
    2356       65235 :                 xfs_iext_update_extent(ip, state, icur, &RIGHT);
    2357             : 
    2358       65235 :                 if (cur == NULL)
    2359             :                         rval = XFS_ILOG_DEXT;
    2360             :                 else {
    2361       35222 :                         rval = 0;
    2362       35222 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2363       35222 :                         if (error)
    2364           0 :                                 goto done;
    2365       35222 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2366           0 :                                 xfs_btree_mark_sick(cur);
    2367           0 :                                 error = -EFSCORRUPTED;
    2368           0 :                                 goto done;
    2369             :                         }
    2370       35222 :                         error = xfs_bmbt_update(cur, &PREV);
    2371       35222 :                         if (error)
    2372           0 :                                 goto done;
    2373       35222 :                         error = xfs_btree_increment(cur, 0, &i);
    2374       35222 :                         if (error)
    2375           0 :                                 goto done;
    2376       35222 :                         error = xfs_bmbt_update(cur, &RIGHT);
    2377       35222 :                         if (error)
    2378           0 :                                 goto done;
    2379             :                 }
    2380             :                 break;
    2381             : 
    2382      520623 :         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      520623 :                 old = PREV;
    2388      520623 :                 PREV.br_blockcount -= new->br_blockcount;
    2389             : 
    2390      520623 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2391      520622 :                 xfs_iext_next(ifp, icur);
    2392      520623 :                 xfs_iext_insert(ip, icur, new, state);
    2393      520622 :                 ifp->if_nextents++;
    2394             : 
    2395      520622 :                 if (cur == NULL)
    2396             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2397             :                 else {
    2398       89708 :                         rval = XFS_ILOG_CORE;
    2399       89708 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2400       89708 :                         if (error)
    2401           0 :                                 goto done;
    2402       89708 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2403           0 :                                 xfs_btree_mark_sick(cur);
    2404           0 :                                 error = -EFSCORRUPTED;
    2405           0 :                                 goto done;
    2406             :                         }
    2407       89708 :                         error = xfs_bmbt_update(cur, &PREV);
    2408       89708 :                         if (error)
    2409           0 :                                 goto done;
    2410       89708 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2411       89708 :                         if (error)
    2412           0 :                                 goto done;
    2413       89708 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2414           0 :                                 xfs_btree_mark_sick(cur);
    2415           0 :                                 error = -EFSCORRUPTED;
    2416           0 :                                 goto done;
    2417             :                         }
    2418       89708 :                         if ((error = xfs_btree_insert(cur, &i)))
    2419           0 :                                 goto done;
    2420       89708 :                         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     1033843 :         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     1033843 :                 old = PREV;
    2435     1033843 :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    2436             : 
    2437     1033843 :                 r[0] = *new;
    2438     1033843 :                 r[1].br_startoff = new_endoff;
    2439     1033843 :                 r[1].br_blockcount =
    2440     1033843 :                         old.br_startoff + old.br_blockcount - new_endoff;
    2441     1033843 :                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
    2442     1033843 :                 r[1].br_state = PREV.br_state;
    2443             : 
    2444     1033843 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2445     1033808 :                 xfs_iext_next(ifp, icur);
    2446     1033545 :                 xfs_iext_insert(ip, icur, &r[1], state);
    2447     1033903 :                 xfs_iext_insert(ip, icur, &r[0], state);
    2448     1033372 :                 ifp->if_nextents += 2;
    2449             : 
    2450     1033372 :                 if (cur == NULL)
    2451             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2452             :                 else {
    2453      185135 :                         rval = XFS_ILOG_CORE;
    2454      185135 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2455      185135 :                         if (error)
    2456           0 :                                 goto done;
    2457      185135 :                         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      185135 :                         error = xfs_bmbt_update(cur, &r[1]);
    2464      185135 :                         if (error)
    2465           0 :                                 goto done;
    2466             :                         /* new left extent - oldext */
    2467      185135 :                         cur->bc_rec.b = PREV;
    2468      185135 :                         if ((error = xfs_btree_insert(cur, &i)))
    2469           0 :                                 goto done;
    2470      185135 :                         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      185135 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2481      185135 :                         if (error)
    2482           0 :                                 goto done;
    2483      185135 :                         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      185135 :                         if ((error = xfs_btree_insert(cur, &i)))
    2490           0 :                                 goto done;
    2491      185135 :                         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    26536160 :         xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
    2514             : 
    2515             :         /* convert to a btree if necessary */
    2516    26536154 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2517      101820 :                 int     tmp_logflags;   /* partial log flag return val */
    2518             : 
    2519      101820 :                 ASSERT(cur == NULL);
    2520      101820 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    2521             :                                 &tmp_logflags, whichfork);
    2522      101820 :                 *logflagsp |= tmp_logflags;
    2523      101820 :                 if (error)
    2524           7 :                         goto done;
    2525             :         }
    2526             : 
    2527             :         /* clear out the allocated field, done with it now in any case. */
    2528    26535843 :         if (cur) {
    2529     6051252 :                 cur->bc_ino.allocated = 0;
    2530     6051252 :                 *curp = cur;
    2531             :         }
    2532             : 
    2533    26535843 :         xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
    2534    26535600 : done:
    2535    26535600 :         *logflagsp |= rval;
    2536    26535600 :         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    18061755 : 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    18061755 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    2553    18061755 :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2554    18061755 :         xfs_filblks_t           newlen=0;       /* new indirect size */
    2555    18061755 :         xfs_filblks_t           oldlen=0;       /* old indirect size */
    2556    18061755 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2557    18061755 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2558    18061755 :         xfs_filblks_t           temp;    /* temp for indirect calculations */
    2559             : 
    2560    18061755 :         ifp = xfs_ifork_ptr(ip, whichfork);
    2561    18076645 :         ASSERT(isnullstartblock(new->br_startblock));
    2562             : 
    2563             :         /*
    2564             :          * Check and set flags if this segment has a left neighbor
    2565             :          */
    2566    18076645 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2567    12502527 :                 state |= BMAP_LEFT_VALID;
    2568    12502527 :                 if (isnullstartblock(left.br_startblock))
    2569     2465336 :                         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    18074613 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2577     8475871 :                 state |= BMAP_RIGHT_VALID;
    2578     8475871 :                 if (isnullstartblock(right.br_startblock))
    2579     1383623 :                         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    18074765 :         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
    2587     2465325 :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2588     1847404 :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2589     1847403 :                 state |= BMAP_LEFT_CONTIG;
    2590             : 
    2591    18074765 :         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
    2592     1383623 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2593       89421 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2594       85125 :             (!(state & BMAP_LEFT_CONTIG) ||
    2595       19661 :              (left.br_blockcount + new->br_blockcount +
    2596             :               right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
    2597       85125 :                 state |= BMAP_RIGHT_CONTIG;
    2598             : 
    2599             :         /*
    2600             :          * Switch out based on the contiguity flags.
    2601             :          */
    2602    18074765 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2603       19661 :         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       19661 :                 temp = left.br_blockcount + new->br_blockcount +
    2610       19661 :                         right.br_blockcount;
    2611             : 
    2612       19661 :                 oldlen = startblockval(left.br_startblock) +
    2613       19661 :                         startblockval(new->br_startblock) +
    2614       19661 :                         startblockval(right.br_startblock);
    2615       19661 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2616             :                                          oldlen);
    2617       19661 :                 left.br_startblock = nullstartblock(newlen);
    2618       19661 :                 left.br_blockcount = temp;
    2619             : 
    2620       19661 :                 xfs_iext_remove(ip, icur, state);
    2621       19661 :                 xfs_iext_prev(ifp, icur);
    2622       19661 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2623       19661 :                 break;
    2624             : 
    2625     1827726 :         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     1827726 :                 temp = left.br_blockcount + new->br_blockcount;
    2632             : 
    2633     1827726 :                 oldlen = startblockval(left.br_startblock) +
    2634     1827726 :                         startblockval(new->br_startblock);
    2635     1827726 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2636             :                                          oldlen);
    2637     1827741 :                 left.br_blockcount = temp;
    2638     1827741 :                 left.br_startblock = nullstartblock(newlen);
    2639             : 
    2640     1827741 :                 xfs_iext_prev(ifp, icur);
    2641     1827703 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2642     1827703 :                 break;
    2643             : 
    2644       65464 :         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       65464 :                 temp = new->br_blockcount + right.br_blockcount;
    2651       65464 :                 oldlen = startblockval(new->br_startblock) +
    2652       65464 :                         startblockval(right.br_startblock);
    2653       65464 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2654             :                                          oldlen);
    2655       65464 :                 right.br_startoff = new->br_startoff;
    2656       65464 :                 right.br_startblock = nullstartblock(newlen);
    2657       65464 :                 right.br_blockcount = temp;
    2658       65464 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2659       65464 :                 break;
    2660             : 
    2661    16161914 :         case 0:
    2662             :                 /*
    2663             :                  * New allocation is not contiguous with another
    2664             :                  * delayed allocation.
    2665             :                  * Insert a new entry.
    2666             :                  */
    2667    16161914 :                 oldlen = newlen = 0;
    2668    16161914 :                 xfs_iext_insert(ip, icur, new, state);
    2669    16161914 :                 break;
    2670             :         }
    2671    18074807 :         if (oldlen != newlen) {
    2672     1912811 :                 ASSERT(oldlen > newlen);
    2673     1912811 :                 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
    2674             :                                  false);
    2675             :                 /*
    2676             :                  * Nothing to do for disk quota accounting here.
    2677             :                  */
    2678     1912812 :                 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
    2679             :         }
    2680    18055938 : }
    2681             : 
    2682             : /*
    2683             :  * Convert a hole to a real allocation.
    2684             :  */
    2685             : STATIC int                              /* error */
    2686   101831260 : 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   101831260 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    2697   101831611 :         struct xfs_mount        *mp = ip->i_mount;
    2698   101831611 :         struct xfs_btree_cur    *cur = *curp;
    2699   101831611 :         int                     error;  /* error return value */
    2700   101831611 :         int                     i;      /* temp state */
    2701   101831611 :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2702   101831611 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2703   101831611 :         int                     rval=0; /* return value (logging flags) */
    2704   101831611 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2705   101831611 :         struct xfs_bmbt_irec    old;
    2706             : 
    2707   101831611 :         ASSERT(!isnullstartblock(new->br_startblock));
    2708   101831611 :         ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    2709             : 
    2710   101831611 :         XFS_STATS_INC(mp, xs_add_exlist);
    2711             : 
    2712             :         /*
    2713             :          * Check and set flags if this segment has a left neighbor.
    2714             :          */
    2715   101831611 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2716    68850869 :                 state |= BMAP_LEFT_VALID;
    2717    68850869 :                 if (isnullstartblock(left.br_startblock))
    2718      138357 :                         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   101832260 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2726    12996301 :                 state |= BMAP_RIGHT_VALID;
    2727    12996301 :                 if (isnullstartblock(right.br_startblock))
    2728      205596 :                         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   101830671 :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2736    68712466 :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2737    50480494 :             left.br_startblock + left.br_blockcount == new->br_startblock &&
    2738     5322312 :             left.br_state == new->br_state &&
    2739     4856345 :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2740     4856332 :                 state |= BMAP_LEFT_CONTIG;
    2741             : 
    2742   101830671 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2743    12790647 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2744     4368886 :             new->br_startblock + new->br_blockcount == right.br_startblock &&
    2745      653036 :             new->br_state == right.br_state &&
    2746      554557 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2747      554557 :             (!(state & BMAP_LEFT_CONTIG) ||
    2748      344063 :              left.br_blockcount + new->br_blockcount +
    2749             :              right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
    2750      554557 :                 state |= BMAP_RIGHT_CONTIG;
    2751             : 
    2752   101830671 :         error = 0;
    2753             :         /*
    2754             :          * Select which case we're in here, and implement it.
    2755             :          */
    2756   101830671 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2757      344063 :         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      344063 :                 left.br_blockcount += new->br_blockcount + right.br_blockcount;
    2764             : 
    2765      344063 :                 xfs_iext_remove(ip, icur, state);
    2766      344063 :                 xfs_iext_prev(ifp, icur);
    2767      344063 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2768      344063 :                 ifp->if_nextents--;
    2769             : 
    2770      344063 :                 if (cur == NULL) {
    2771       60244 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2772             :                 } else {
    2773      283849 :                         rval = XFS_ILOG_CORE;
    2774      283849 :                         error = xfs_bmbt_lookup_eq(cur, &right, &i);
    2775      283849 :                         if (error)
    2776           0 :                                 goto done;
    2777      283849 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2778           0 :                                 xfs_btree_mark_sick(cur);
    2779           0 :                                 error = -EFSCORRUPTED;
    2780           0 :                                 goto done;
    2781             :                         }
    2782      283849 :                         error = xfs_btree_delete(cur, &i);
    2783      283849 :                         if (error)
    2784           0 :                                 goto done;
    2785      283849 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2786           0 :                                 xfs_btree_mark_sick(cur);
    2787           0 :                                 error = -EFSCORRUPTED;
    2788           0 :                                 goto done;
    2789             :                         }
    2790      283849 :                         error = xfs_btree_decrement(cur, 0, &i);
    2791      283849 :                         if (error)
    2792           0 :                                 goto done;
    2793      283849 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2794           0 :                                 xfs_btree_mark_sick(cur);
    2795           0 :                                 error = -EFSCORRUPTED;
    2796           0 :                                 goto done;
    2797             :                         }
    2798      283849 :                         error = xfs_bmbt_update(cur, &left);
    2799      283849 :                         if (error)
    2800           0 :                                 goto done;
    2801             :                 }
    2802             :                 break;
    2803             : 
    2804     4512269 :         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     4512269 :                 old = left;
    2811     4512269 :                 left.br_blockcount += new->br_blockcount;
    2812             : 
    2813     4512269 :                 xfs_iext_prev(ifp, icur);
    2814     4512269 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2815             : 
    2816     4512269 :                 if (cur == NULL) {
    2817     3676273 :                         rval = xfs_ilog_fext(whichfork);
    2818             :                 } else {
    2819      835996 :                         rval = 0;
    2820      835996 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2821      835996 :                         if (error)
    2822           0 :                                 goto done;
    2823      835996 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2824           0 :                                 xfs_btree_mark_sick(cur);
    2825           0 :                                 error = -EFSCORRUPTED;
    2826           0 :                                 goto done;
    2827             :                         }
    2828      835996 :                         error = xfs_bmbt_update(cur, &left);
    2829      835996 :                         if (error)
    2830           0 :                                 goto done;
    2831             :                 }
    2832             :                 break;
    2833             : 
    2834      210494 :         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      210494 :                 old = right;
    2841             : 
    2842      210494 :                 right.br_startoff = new->br_startoff;
    2843      210494 :                 right.br_startblock = new->br_startblock;
    2844      210494 :                 right.br_blockcount += new->br_blockcount;
    2845      210494 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2846             : 
    2847      210494 :                 if (cur == NULL) {
    2848       67201 :                         rval = xfs_ilog_fext(whichfork);
    2849             :                 } else {
    2850      143293 :                         rval = 0;
    2851      143293 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2852      143293 :                         if (error)
    2853           0 :                                 goto done;
    2854      143293 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2855           0 :                                 xfs_btree_mark_sick(cur);
    2856           0 :                                 error = -EFSCORRUPTED;
    2857           0 :                                 goto done;
    2858             :                         }
    2859      143293 :                         error = xfs_bmbt_update(cur, &right);
    2860      143293 :                         if (error)
    2861           0 :                                 goto done;
    2862             :                 }
    2863             :                 break;
    2864             : 
    2865    96763845 :         case 0:
    2866             :                 /*
    2867             :                  * New allocation is not contiguous with another
    2868             :                  * real allocation.
    2869             :                  * Insert a new entry.
    2870             :                  */
    2871    96763845 :                 xfs_iext_insert(ip, icur, new, state);
    2872    96761327 :                 ifp->if_nextents++;
    2873             : 
    2874    96761327 :                 if (cur == NULL) {
    2875    45532413 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2876             :                 } else {
    2877    55867746 :                         rval = XFS_ILOG_CORE;
    2878    55867746 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2879    55867805 :                         if (error)
    2880         994 :                                 goto done;
    2881    55866811 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2882           0 :                                 xfs_btree_mark_sick(cur);
    2883           0 :                                 error = -EFSCORRUPTED;
    2884           0 :                                 goto done;
    2885             :                         }
    2886    55866811 :                         error = xfs_btree_insert(cur, &i);
    2887    55866818 :                         if (error)
    2888         118 :                                 goto done;
    2889    55866700 :                         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   101827107 :         if (!(flags & XFS_BMAPI_NORMAP))
    2900   101825925 :                 xfs_rmap_map_extent(tp, ip, whichfork, new);
    2901             : 
    2902             :         /* convert to a btree if necessary */
    2903   101829137 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2904      379283 :                 int     tmp_logflags;   /* partial log flag return val */
    2905             : 
    2906      379283 :                 ASSERT(cur == NULL);
    2907      379283 :                 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
    2908             :                                 &tmp_logflags, whichfork);
    2909      379283 :                 *logflagsp |= tmp_logflags;
    2910      379283 :                 cur = *curp;
    2911      379283 :                 if (error)
    2912           7 :                         goto done;
    2913             :         }
    2914             : 
    2915             :         /* clear out the allocated field, done with it now in any case. */
    2916   101827507 :         if (cur)
    2917    57509063 :                 cur->bc_ino.allocated = 0;
    2918             : 
    2919   101827507 :         xfs_bmap_check_leaf_extents(cur, ip, whichfork);
    2920   101826942 : done:
    2921   101826942 :         *logflagsp |= rval;
    2922   101826942 :         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    11738938 : 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    11738938 :         xfs_fileoff_t   orig_off;       /* original offset */
    2946    11738938 :         xfs_extlen_t    orig_alen;      /* original length */
    2947    11738938 :         xfs_fileoff_t   orig_end;       /* original off+len */
    2948    11738938 :         xfs_fileoff_t   nexto;          /* next file offset */
    2949    11738938 :         xfs_fileoff_t   prevo;          /* previous file offset */
    2950    11738938 :         xfs_fileoff_t   align_off;      /* temp for offset */
    2951    11738938 :         xfs_extlen_t    align_alen;     /* temp for length */
    2952    11738938 :         xfs_extlen_t    temp;           /* temp for calculations */
    2953             : 
    2954    11738938 :         if (convert)
    2955             :                 return 0;
    2956             : 
    2957    11738938 :         orig_off = align_off = *offp;
    2958    11738938 :         orig_alen = align_alen = *lenp;
    2959    11738938 :         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    11738938 :         if (!delay && !eof &&
    2966     4693790 :             (orig_off >= gotp->br_startoff) &&
    2967      117376 :             (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    11621562 :         div_u64_rem(orig_off, extsz, &temp);
    2979    11621562 :         if (temp) {
    2980      300065 :                 align_alen += temp;
    2981      300065 :                 align_off -= temp;
    2982             :         }
    2983             : 
    2984             :         /* Same adjustment for the end of the requested area. */
    2985    11621562 :         temp = (align_alen % extsz);
    2986    11621562 :         if (temp)
    2987      337908 :                 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    11621565 :         while (align_alen > XFS_MAX_BMBT_EXTLEN)
    2998           3 :                 align_alen -= extsz;
    2999    11621562 :         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    11621562 :         if (prevp->br_startoff != NULLFILEOFF) {
    3006     9990106 :                 if (prevp->br_startblock == HOLESTARTBLOCK)
    3007             :                         prevo = prevp->br_startoff;
    3008             :                 else
    3009     9990106 :                         prevo = prevp->br_startoff + prevp->br_blockcount;
    3010             :         } else
    3011             :                 prevo = 0;
    3012    11621562 :         if (align_off != orig_off && align_off < prevo)
    3013        5110 :                 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    11621562 :         if (!eof && gotp->br_startoff != NULLFILEOFF) {
    3023     4631094 :                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
    3024     4576472 :                     (!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    11621562 :         if (!eof &&
    3031     4631065 :             align_off + align_alen != orig_end &&
    3032             :             align_off + align_alen > nexto)
    3033        8331 :                 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    11621562 :         if (align_off != orig_off && align_off < prevo)
    3041        2718 :                 align_off = prevo;
    3042    11621562 :         if (align_off + align_alen != orig_end &&
    3043             :             align_off + align_alen > nexto &&
    3044             :             nexto != NULLFILEOFF) {
    3045        3524 :                 ASSERT(nexto > prevo);
    3046        3524 :                 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    11621562 :         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    11621562 :                 ASSERT(orig_off >= align_off);
    3089             :                 /* see XFS_BMBT_MAX_EXTLEN handling above */
    3090    11621562 :                 ASSERT(orig_end <= align_off + align_alen ||
    3091             :                        align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
    3092             :         }
    3093             : 
    3094             : #ifdef DEBUG
    3095    11621562 :         if (!eof && gotp->br_startoff != NULLFILEOFF)
    3096     4631152 :                 ASSERT(align_off + align_alen <= gotp->br_startoff);
    3097    11621562 :         if (prevp->br_startoff != NULLFILEOFF)
    3098     9990509 :                 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
    3099             : #endif
    3100             : 
    3101    11621562 :         *lenp = align_alen;
    3102    11621562 :         *offp = align_off;
    3103    11621562 :         return 0;
    3104             : }
    3105             : 
    3106             : #define XFS_ALLOC_GAP_UNITS     4
    3107             : 
    3108             : void
    3109    63895157 : xfs_bmap_adjacent(
    3110             :         struct xfs_bmalloca     *ap)    /* bmap alloc argument struct */
    3111             : {
    3112    63895157 :         xfs_fsblock_t   adjust;         /* adjustment to block numbers */
    3113    63895157 :         xfs_mount_t     *mp;            /* mount point structure */
    3114    63895157 :         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    63895157 :         mp = ap->ip->i_mount;
    3124    63895157 :         rt = XFS_IS_REALTIME_INODE(ap->ip) &&
    3125     9457760 :                 (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    63895157 :         if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
    3131    11968636 :             !isnullstartblock(ap->prev.br_startblock) &&
    3132     7381243 :             ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
    3133             :                     ap->prev.br_startblock)) {
    3134     7381052 :                 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
    3135             :                 /*
    3136             :                  * Adjust for the gap between prevp and us.
    3137             :                  */
    3138     7381052 :                 adjust = ap->offset -
    3139     7381052 :                         (ap->prev.br_startoff + ap->prev.br_blockcount);
    3140    10132785 :                 if (adjust &&
    3141     2751733 :                     ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
    3142     2742208 :                         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    56514105 :         else if (!ap->eof) {
    3150    24853765 :                 xfs_fsblock_t   gotbno;         /* right side block number */
    3151    24853765 :                 xfs_fsblock_t   gotdiff=0;      /* right side difference */
    3152    24853765 :                 xfs_fsblock_t   prevbno;        /* left side block number */
    3153    24853765 :                 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    43403685 :                 if (ap->prev.br_startoff != NULLFILEOFF &&
    3160    18658346 :                     !isnullstartblock(ap->prev.br_startblock) &&
    3161    18549983 :                     (prevbno = ap->prev.br_startblock +
    3162    33324106 :                                ap->prev.br_blockcount) &&
    3163    18549920 :                     ISVALID(prevbno, ap->prev.br_startblock)) {
    3164             :                         /*
    3165             :                          * Calculate gap to end of previous block.
    3166             :                          */
    3167    18549813 :                         adjust = prevdiff = ap->offset -
    3168    18549813 :                                 (ap->prev.br_startoff +
    3169             :                                  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    21882668 :                         if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3179     6016578 :                             ISVALID(prevbno + prevdiff,
    3180             :                                     ap->prev.br_startblock))
    3181     6016529 :                                 prevbno += adjust;
    3182             :                         else
    3183    12533284 :                                 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    24853765 :                 if (!isnullstartblock(ap->got.br_startblock)) {
    3195             :                         /*
    3196             :                          * Calculate gap to start of next block.
    3197             :                          */
    3198     9191715 :                         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     9191715 :                         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    10662533 :                         if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3212     3856996 :                             ISVALID(gotbno - gotdiff, gotbno))
    3213     3831752 :                                 gotbno -= adjust;
    3214     5359963 :                         else if (ISVALID(gotbno - ap->length, gotbno)) {
    3215     5330891 :                                 gotbno -= ap->length;
    3216     5330891 :                                 gotdiff += adjust - ap->length;
    3217             :                         } else
    3218       29072 :                                 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    24853765 :                 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
    3230    10812691 :                         ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
    3231    16696549 :                 else if (prevbno != NULLFSBLOCK)
    3232    10392744 :                         ap->blkno = prevbno;
    3233     6303805 :                 else if (gotbno != NULLFSBLOCK)
    3234     1034461 :                         ap->blkno = gotbno;
    3235             :         }
    3236             : #undef ISVALID
    3237    63895157 : }
    3238             : 
    3239             : int
    3240    56087809 : xfs_bmap_longest_free_extent(
    3241             :         struct xfs_perag        *pag,
    3242             :         struct xfs_trans        *tp,
    3243             :         xfs_extlen_t            *blen)
    3244             : {
    3245    56087809 :         xfs_extlen_t            longest;
    3246    56087809 :         int                     error = 0;
    3247             : 
    3248   112175618 :         if (!xfs_perag_initialised_agf(pag)) {
    3249        1621 :                 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
    3250             :                                 NULL);
    3251        1621 :                 if (error)
    3252             :                         return error;
    3253             :         }
    3254             : 
    3255    56086204 :         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    56086770 :         if (*blen < longest)
    3259    55944750 :                 *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    55751365 :         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    55751327 :         if (blen < args->maxlen)
    3284             :                 return blen;
    3285             :         return args->maxlen;
    3286             : }
    3287             : 
    3288             : static int
    3289    55740402 : xfs_bmap_btalloc_select_lengths(
    3290             :         struct xfs_bmalloca     *ap,
    3291             :         struct xfs_alloc_arg    *args,
    3292             :         xfs_extlen_t            *blen)
    3293             : {
    3294    55740402 :         struct xfs_mount        *mp = args->mp;
    3295    55740402 :         struct xfs_perag        *pag;
    3296    55740402 :         xfs_agnumber_t          agno, startag;
    3297    55740402 :         int                     error = 0;
    3298             : 
    3299    55740402 :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3300         122 :                 args->total = ap->minlen;
    3301         122 :                 args->minlen = ap->minlen;
    3302         122 :                 return 0;
    3303             :         }
    3304             : 
    3305    55740280 :         args->total = ap->total;
    3306    55740280 :         startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
    3307    55740280 :         if (startag == NULLAGNUMBER)
    3308           0 :                 startag = 0;
    3309             : 
    3310    55740280 :         *blen = 0;
    3311    56070841 :         for_each_perag_wrap(mp, startag, agno, pag) {
    3312    56001284 :                 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
    3313    56000348 :                 if (error && error != -EAGAIN)
    3314             :                         break;
    3315    56000348 :                 error = 0;
    3316    56000348 :                 if (*blen >= args->maxlen)
    3317             :                         break;
    3318             :         }
    3319    55738813 :         if (pag)
    3320    55671608 :                 xfs_perag_rele(pag);
    3321             : 
    3322    55740140 :         args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
    3323    55740140 :         return error;
    3324             : }
    3325             : 
    3326             : /* Update all inode and quota accounting for the allocation we just did. */
    3327             : static void
    3328    56120309 : xfs_bmap_btalloc_accounting(
    3329             :         struct xfs_bmalloca     *ap,
    3330             :         struct xfs_alloc_arg    *args)
    3331             : {
    3332    56120309 :         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      336674 :                 if (ap->wasdel) {
    3343      117343 :                         xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3344      117343 :                         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      219331 :                 ap->ip->i_delayed_blks += args->len;
    3356      219331 :                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
    3357      219331 :                                 -(long)args->len);
    3358      219331 :                 return;
    3359             :         }
    3360             : 
    3361             :         /* data/attr fork only */
    3362    55783635 :         ap->ip->i_nblocks += args->len;
    3363    55783635 :         xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
    3364    55785616 :         if (ap->wasdel) {
    3365    15612300 :                 ap->ip->i_delayed_blks -= args->len;
    3366    15612300 :                 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3367             :         }
    3368   111571226 :         xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
    3369    55785613 :                 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
    3370    55785613 :                 args->len);
    3371             : }
    3372             : 
    3373             : static int
    3374    56119309 : xfs_bmap_compute_alignments(
    3375             :         struct xfs_bmalloca     *ap,
    3376             :         struct xfs_alloc_arg    *args)
    3377             : {
    3378    56119309 :         struct xfs_mount        *mp = args->mp;
    3379    56119309 :         xfs_extlen_t            align = 0; /* minimum allocation alignment */
    3380    56119309 :         int                     stripe_align = 0;
    3381             : 
    3382             :         /* stripe alignment for allocation is determined by mount parameters */
    3383    56119309 :         if (mp->m_swidth && xfs_has_swalloc(mp))
    3384             :                 stripe_align = mp->m_swidth;
    3385    56119311 :         else if (mp->m_dalign)
    3386       40541 :                 stripe_align = mp->m_dalign;
    3387             : 
    3388    56119309 :         if (ap->flags & XFS_BMAPI_COWFORK)
    3389      336690 :                 align = xfs_get_cowextsz_hint(ap->ip);
    3390    55782619 :         else if (ap->datatype & XFS_ALLOC_USERDATA)
    3391    23837319 :                 align = xfs_get_extsz_hint(ap->ip);
    3392    24174203 :         if (align) {
    3393     3433906 :                 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
    3394     3433903 :                                         ap->eof, 0, ap->conv, &ap->offset,
    3395             :                                         &ap->length))
    3396           0 :                         ASSERT(0);
    3397     3433906 :                 ASSERT(ap->length);
    3398             :         }
    3399             : 
    3400             :         /* apply extent size hints if obtained earlier */
    3401    56119506 :         if (align) {
    3402     3433911 :                 args->prod = align;
    3403     3433911 :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3404     3433911 :                 if (args->mod)
    3405        6385 :                         args->mod = args->prod - args->mod;
    3406    52685595 :         } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
    3407         358 :                 args->prod = 1;
    3408         358 :                 args->mod = 0;
    3409             :         } else {
    3410    52685237 :                 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
    3411    52685237 :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3412    52685237 :                 if (args->mod)
    3413    18052161 :                         args->mod = args->prod - args->mod;
    3414             :         }
    3415             : 
    3416    56119506 :         return stripe_align;
    3417             : }
    3418             : 
    3419             : static void
    3420    56120373 : 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    56120373 :         ap->blkno = args->fsbno;
    3427    56120373 :         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    56120373 :         if (ap->length <= orig_length)
    3441    55885056 :                 ap->offset = orig_offset;
    3442      235317 :         else if (ap->offset + ap->length < orig_offset + orig_length)
    3443           0 :                 ap->offset = orig_offset + orig_length - ap->length;
    3444    56120373 :         xfs_bmap_btalloc_accounting(ap, args);
    3445    56121704 : }
    3446             : 
    3447             : #ifdef DEBUG
    3448             : static int
    3449      370107 : xfs_bmap_exact_minlen_extent_alloc(
    3450             :         struct xfs_bmalloca     *ap)
    3451             : {
    3452      370107 :         struct xfs_mount        *mp = ap->ip->i_mount;
    3453      370107 :         struct xfs_alloc_arg    args = { .tp = ap->tp, .mp = mp };
    3454      370107 :         xfs_fileoff_t           orig_offset;
    3455      370107 :         xfs_extlen_t            orig_length;
    3456      370107 :         int                     error;
    3457             : 
    3458      370107 :         ASSERT(ap->length);
    3459             : 
    3460      370107 :         if (ap->minlen != 1) {
    3461           0 :                 ap->blkno = NULLFSBLOCK;
    3462           0 :                 ap->length = 0;
    3463           0 :                 return 0;
    3464             :         }
    3465             : 
    3466      370107 :         orig_offset = ap->offset;
    3467      370107 :         orig_length = ap->length;
    3468             : 
    3469      370107 :         args.alloc_minlen_only = 1;
    3470             : 
    3471      370107 :         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      370109 :         ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
    3482             : 
    3483      370109 :         args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
    3484      370109 :         args.minlen = args.maxlen = ap->minlen;
    3485      370109 :         args.total = ap->total;
    3486             : 
    3487      370109 :         args.alignment = 1;
    3488      370109 :         args.minalignslop = 0;
    3489             : 
    3490      370109 :         args.minleft = ap->minleft;
    3491      370109 :         args.wasdel = ap->wasdel;
    3492      370109 :         args.resv = XFS_AG_RESV_NONE;
    3493      370109 :         args.datatype = ap->datatype;
    3494             : 
    3495      370109 :         error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
    3496      370112 :         if (error)
    3497             :                 return error;
    3498             : 
    3499      370112 :         if (args.fsbno != NULLFSBLOCK) {
    3500      370112 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3501             :                         orig_length);
    3502             :         } else {
    3503           0 :                 ap->blkno = NULLFSBLOCK;
    3504           0 :                 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        3829 : 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        3829 :         struct xfs_mount        *mp = args->mp;
    3532        3829 :         struct xfs_perag        *caller_pag = args->pag;
    3533        3829 :         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        3829 :         if (ap->offset) {
    3542        3378 :                 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        3378 :                 args->alignment = 1;
    3550        3378 :                 if (blen > stripe_align && blen <= args->maxlen)
    3551           0 :                         nextminlen = blen - stripe_align;
    3552             :                 else
    3553        3378 :                         nextminlen = args->minlen;
    3554        3378 :                 if (nextminlen + stripe_align > args->minlen + 1)
    3555        3378 :                         args->minalignslop = nextminlen + stripe_align -
    3556        3378 :                                         args->minlen - 1;
    3557             :                 else
    3558           0 :                         args->minalignslop = 0;
    3559             : 
    3560        3378 :                 if (!caller_pag)
    3561        3379 :                         args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
    3562        3377 :                 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
    3563        3379 :                 if (!caller_pag) {
    3564        3379 :                         xfs_perag_put(args->pag);
    3565        3379 :                         args->pag = NULL;
    3566             :                 }
    3567        3379 :                 if (error)
    3568             :                         return error;
    3569             : 
    3570        3379 :                 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        2925 :                 args->alignment = stripe_align;
    3577        2925 :                 args->minlen = nextminlen;
    3578        2925 :                 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         451 :                 args->alignment = stripe_align;
    3585         451 :                 if (blen > args->alignment &&
    3586         451 :                     blen <= args->maxlen + args->alignment)
    3587           0 :                         args->minlen = blen - args->alignment;
    3588         451 :                 args->minalignslop = 0;
    3589             :         }
    3590             : 
    3591        3376 :         if (ag_only) {
    3592           0 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3593             :         } else {
    3594        3376 :                 args->pag = NULL;
    3595        3376 :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3596        3376 :                 ASSERT(args->pag == NULL);
    3597        3376 :                 args->pag = caller_pag;
    3598             :         }
    3599        3376 :         if (error)
    3600             :                 return error;
    3601             : 
    3602        3376 :         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           0 :         args->alignment = 1;
    3611           0 :         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       31224 : xfs_bmap_btalloc_low_space(
    3628             :         struct xfs_bmalloca     *ap,
    3629             :         struct xfs_alloc_arg    *args)
    3630             : {
    3631       31224 :         int                     error;
    3632             : 
    3633       31224 :         if (args->minlen > ap->minlen) {
    3634       31184 :                 args->minlen = ap->minlen;
    3635       31184 :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3636       31184 :                 if (error || args->fsbno != NULLFSBLOCK)
    3637             :                         return error;
    3638             :         }
    3639             : 
    3640             :         /* Last ditch attempt before failure is declared. */
    3641          40 :         args->total = ap->minlen;
    3642          40 :         error = xfs_alloc_vextent_first_ag(args, 0);
    3643          40 :         if (error)
    3644             :                 return error;
    3645          40 :         ap->tp->t_flags |= XFS_TRANS_LOWMODE;
    3646          40 :         return 0;
    3647             : }
    3648             : 
    3649             : static int
    3650       11222 : xfs_bmap_btalloc_filestreams(
    3651             :         struct xfs_bmalloca     *ap,
    3652             :         struct xfs_alloc_arg    *args,
    3653             :         int                     stripe_align)
    3654             : {
    3655       11222 :         xfs_extlen_t            blen = 0;
    3656       11222 :         int                     error = 0;
    3657             : 
    3658             : 
    3659       11222 :         error = xfs_filestream_select_ag(ap, args, &blen);
    3660       11225 :         if (error)
    3661             :                 return error;
    3662       11225 :         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       11225 :         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       11225 :         args->minlen = xfs_bmap_select_minlen(ap, args, blen);
    3676       11225 :         if (ap->aeof)
    3677           0 :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3678             :                                 true);
    3679             : 
    3680       11225 :         if (!error && args->fsbno == NULLFSBLOCK)
    3681       11225 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3682             : 
    3683           0 : 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       11222 :         xfs_perag_rele(args->pag);
    3692       11223 :         args->pag = NULL;
    3693       11223 :         if (error || args->fsbno != NULLFSBLOCK)
    3694             :                 return error;
    3695             : 
    3696           1 :         return xfs_bmap_btalloc_low_space(ap, args);
    3697             : }
    3698             : 
    3699             : static int
    3700    55738303 : xfs_bmap_btalloc_best_length(
    3701             :         struct xfs_bmalloca     *ap,
    3702             :         struct xfs_alloc_arg    *args,
    3703             :         int                     stripe_align)
    3704             : {
    3705    55738303 :         xfs_extlen_t            blen = 0;
    3706    55738303 :         int                     error;
    3707             : 
    3708    55738303 :         ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
    3709    55738303 :         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    55738116 :         error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
    3717    55740014 :         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    55740014 :         if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
    3727        3829 :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3728             :                                 false);
    3729        3830 :                 if (error || args->fsbno != NULLFSBLOCK)
    3730             :                         return error;
    3731             :         }
    3732             : 
    3733    55736185 :         error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3734    55736778 :         if (error || args->fsbno != NULLFSBLOCK)
    3735             :                 return error;
    3736             : 
    3737       31223 :         return xfs_bmap_btalloc_low_space(ap, args);
    3738             : }
    3739             : 
    3740             : static int
    3741    55751722 : xfs_bmap_btalloc(
    3742             :         struct xfs_bmalloca     *ap)
    3743             : {
    3744    55751722 :         struct xfs_mount        *mp = ap->ip->i_mount;
    3745    55751722 :         struct xfs_alloc_arg    args = {
    3746    55751722 :                 .tp             = ap->tp,
    3747             :                 .mp             = mp,
    3748             :                 .fsbno          = NULLFSBLOCK,
    3749             :                 .oinfo          = XFS_RMAP_OINFO_SKIP_UPDATE,
    3750    55751722 :                 .minleft        = ap->minleft,
    3751    55751722 :                 .wasdel         = ap->wasdel,
    3752             :                 .resv           = XFS_AG_RESV_NONE,
    3753    55751722 :                 .datatype       = ap->datatype,
    3754             :                 .alignment      = 1,
    3755             :                 .minalignslop   = 0,
    3756             :         };
    3757    55751722 :         xfs_fileoff_t           orig_offset;
    3758    55751722 :         xfs_extlen_t            orig_length;
    3759    55751722 :         int                     error;
    3760    55751722 :         int                     stripe_align;
    3761             : 
    3762    55751722 :         ASSERT(ap->length);
    3763    55751722 :         orig_offset = ap->offset;
    3764    55751722 :         orig_length = ap->length;
    3765             : 
    3766    55751722 :         stripe_align = xfs_bmap_compute_alignments(ap, &args);
    3767             : 
    3768             :         /* Trim the allocation back to the maximum an AG can fit. */
    3769    55751004 :         args.maxlen = min(ap->length, mp->m_ag_max_usable);
    3770             : 
    3771    55751004 :         if ((ap->datatype & XFS_ALLOC_USERDATA) &&
    3772    23809289 :             xfs_inode_is_filestream(ap->ip))
    3773       11318 :                 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
    3774             :         else
    3775    55739694 :                 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
    3776    55752274 :         if (error)
    3777             :                 return error;
    3778             : 
    3779    55750783 :         if (args.fsbno != NULLFSBLOCK) {
    3780    55750743 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3781             :                         orig_length);
    3782             :         } else {
    3783          40 :                 ap->blkno = NULLFSBLOCK;
    3784          40 :                 ap->length = 0;
    3785             :         }
    3786             :         return 0;
    3787             : }
    3788             : 
    3789             : /* Trim extent to fit a logical block range. */
    3790             : void
    3791   278259771 : xfs_trim_extent(
    3792             :         struct xfs_bmbt_irec    *irec,
    3793             :         xfs_fileoff_t           bno,
    3794             :         xfs_filblks_t           len)
    3795             : {
    3796   278259771 :         xfs_fileoff_t           distance;
    3797   278259771 :         xfs_fileoff_t           end = bno + len;
    3798             : 
    3799   278259771 :         if (irec->br_startoff + irec->br_blockcount <= bno ||
    3800             :             irec->br_startoff >= end) {
    3801    93866037 :                 irec->br_blockcount = 0;
    3802    93866037 :                 return;
    3803             :         }
    3804             : 
    3805   184393734 :         if (irec->br_startoff < bno) {
    3806    56018655 :                 distance = bno - irec->br_startoff;
    3807    56018655 :                 if (isnullstartblock(irec->br_startblock))
    3808      274418 :                         irec->br_startblock = DELAYSTARTBLOCK;
    3809    56018655 :                 if (irec->br_startblock != DELAYSTARTBLOCK &&
    3810             :                     irec->br_startblock != HOLESTARTBLOCK)
    3811    55745135 :                         irec->br_startblock += distance;
    3812    56018655 :                 irec->br_startoff += distance;
    3813    56018655 :                 irec->br_blockcount -= distance;
    3814             :         }
    3815             : 
    3816   184393734 :         if (end < irec->br_startoff + irec->br_blockcount) {
    3817     3248367 :                 distance = irec->br_startoff + irec->br_blockcount - end;
    3818     3248367 :                 irec->br_blockcount -= distance;
    3819             :         }
    3820             : }
    3821             : 
    3822             : /*
    3823             :  * Trim the returned map to the required bounds
    3824             :  */
    3825             : STATIC void
    3826  2912203957 : 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  2912203957 :         if ((flags & XFS_BMAPI_ENTIRE) ||
    3837  2912203957 :             got->br_startoff + got->br_blockcount <= obno) {
    3838           8 :                 *mval = *got;
    3839           8 :                 if (isnullstartblock(got->br_startblock))
    3840           0 :                         mval->br_startblock = DELAYSTARTBLOCK;
    3841           8 :                 return;
    3842             :         }
    3843             : 
    3844  2912203949 :         if (obno > *bno)
    3845           0 :                 *bno = obno;
    3846  2912203949 :         ASSERT((*bno >= obno) || (n == 0));
    3847  2912203949 :         ASSERT(*bno < end);
    3848  2912203949 :         mval->br_startoff = *bno;
    3849  2912203949 :         if (isnullstartblock(got->br_startblock))
    3850     3598080 :                 mval->br_startblock = DELAYSTARTBLOCK;
    3851             :         else
    3852  2908605869 :                 mval->br_startblock = got->br_startblock +
    3853  2908605869 :                                         (*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  2912203949 :         mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
    3862             :                         got->br_blockcount - (*bno - got->br_startoff));
    3863  2912203949 :         mval->br_state = got->br_state;
    3864  2912203949 :         ASSERT(mval->br_blockcount <= len);
    3865             :         return;
    3866             : }
    3867             : 
    3868             : /*
    3869             :  * Update and validate the extent map to return
    3870             :  */
    3871             : STATIC void
    3872  2912493873 : 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  2912493873 :         xfs_bmbt_irec_t *mval = *map;
    3882             : 
    3883  2912493873 :         ASSERT((flags & XFS_BMAPI_ENTIRE) ||
    3884             :                ((mval->br_startoff + mval->br_blockcount) <= end));
    3885  2912493873 :         ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
    3886             :                (mval->br_startoff < obno));
    3887             : 
    3888  2912493873 :         *bno = mval->br_startoff + mval->br_blockcount;
    3889  2912493873 :         *len = end - *bno;
    3890  2912493873 :         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  2912493873 :         } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
    3898      138052 :                    mval[-1].br_startblock != DELAYSTARTBLOCK &&
    3899      137586 :                    mval[-1].br_startblock != HOLESTARTBLOCK &&
    3900      137586 :                    mval->br_startblock == mval[-1].br_startblock +
    3901      137586 :                                           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  2912493873 :         } else if (*n > 0 &&
    3907      138052 :                    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  2912493873 :         } else if (!((*n == 0) &&
    3914  2912291794 :                      ((mval->br_startoff + mval->br_blockcount) <=
    3915             :                       obno))) {
    3916  2912512938 :                 mval++;
    3917  2912512938 :                 (*n)++;
    3918             :         }
    3919  2912493873 :         *map = mval;
    3920  2912493873 : }
    3921             : 
    3922             : /*
    3923             :  * Map file blocks to filesystem blocks without allocation.
    3924             :  */
    3925             : int
    3926  3390642069 : 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  3390642069 :         struct xfs_mount        *mp = ip->i_mount;
    3935  3390642069 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    3936  3390642069 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    3937  3390932569 :         struct xfs_bmbt_irec    got;
    3938  3390932569 :         xfs_fileoff_t           obno;
    3939  3390932569 :         xfs_fileoff_t           end;
    3940  3390932569 :         struct xfs_iext_cursor  icur;
    3941  3390932569 :         int                     error;
    3942  3390932569 :         bool                    eof = false;
    3943  3390932569 :         int                     n = 0;
    3944             : 
    3945  3390932569 :         ASSERT(*nmap >= 1);
    3946  3390932569 :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
    3947  3390932569 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
    3948             : 
    3949  3390836089 :         if (WARN_ON_ONCE(!ifp)) {
    3950           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    3951           0 :                 return -EFSCORRUPTED;
    3952             :         }
    3953             : 
    3954  6782092232 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    3955  3390836089 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    3956           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    3957           0 :                 return -EFSCORRUPTED;
    3958             :         }
    3959             : 
    3960  6782512286 :         if (xfs_is_shutdown(mp))
    3961             :                 return -EIO;
    3962             : 
    3963  3391251372 :         XFS_STATS_INC(mp, xs_blk_mapr);
    3964             : 
    3965  3391251372 :         error = xfs_iread_extents(NULL, ip, whichfork);
    3966  3390953796 :         if (error)
    3967             :                 return error;
    3968             : 
    3969  3391435131 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
    3970   266907710 :                 eof = true;
    3971  3391409664 :         end = bno + len;
    3972  3391409664 :         obno = bno;
    3973             : 
    3974  3995814842 :         while (bno < end && n < *nmap) {
    3975             :                 /* Reading past eof, act as though there's a hole up to end. */
    3976  3415735857 :                 if (eof)
    3977   291091626 :                         got.br_startoff = end;
    3978  3415735857 :                 if (got.br_startoff > bno) {
    3979             :                         /* Reading in a hole.  */
    3980   580084362 :                         mval->br_startoff = bno;
    3981   580084362 :                         mval->br_startblock = HOLESTARTBLOCK;
    3982   580084362 :                         mval->br_blockcount =
    3983   580084362 :                                 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
    3984   580084362 :                         mval->br_state = XFS_EXT_NORM;
    3985   580084362 :                         bno += mval->br_blockcount;
    3986   580084362 :                         len -= mval->br_blockcount;
    3987   580084362 :                         mval++;
    3988   580084362 :                         n++;
    3989   580084362 :                         continue;
    3990             :                 }
    3991             : 
    3992             :                 /* set up the extent map to return. */
    3993  2835651495 :                 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
    3994  2836266908 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    3995             : 
    3996             :                 /* If we're done, stop now. */
    3997  2836479794 :                 if (bno >= end || n >= *nmap)
    3998             :                         break;
    3999             : 
    4000             :                 /* Else go on to the next record. */
    4001    24320801 :                 if (!xfs_iext_next_extent(ifp, &icur, &got))
    4002    24182752 :                         eof = true;
    4003             :         }
    4004  3392237978 :         *nmap = n;
    4005  3392237978 :         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    18808087 : 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    18808087 :         struct xfs_mount        *mp = ip->i_mount;
    4033    18808087 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4034    18808157 :         xfs_extlen_t            alen;
    4035    18808157 :         xfs_extlen_t            indlen;
    4036    18808157 :         int                     error;
    4037    18808157 :         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    18808157 :         alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
    4044    18808157 :         if (!eof)
    4045     8798718 :                 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
    4046    18808157 :         if (prealloc && alen >= len)
    4047     3150396 :                 prealloc = alen - len;
    4048             : 
    4049             :         /* Figure out the extent size, adjust alen */
    4050    18808157 :         if (whichfork == XFS_COW_FORK) {
    4051      160892 :                 struct xfs_bmbt_irec    prev;
    4052      160892 :                 xfs_extlen_t            extsz = xfs_get_cowextsz_hint(ip);
    4053             : 
    4054      160892 :                 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
    4055      100800 :                         prev.br_startoff = NULLFILEOFF;
    4056             : 
    4057      160892 :                 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
    4058             :                                                1, 0, &aoff, &alen);
    4059      160892 :                 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    18808157 :         error = xfs_quota_reserve_blkres(ip, alen);
    4068    18811971 :         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    18811249 :         indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
    4076    18810709 :         ASSERT(indlen > 0);
    4077             : 
    4078    18810709 :         error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
    4079    18810030 :         if (error)
    4080      567211 :                 goto out_unreserve_quota;
    4081             : 
    4082    18242819 :         error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
    4083    18243488 :         if (error)
    4084      166643 :                 goto out_unreserve_blocks;
    4085             : 
    4086             : 
    4087    18076845 :         ip->i_delayed_blks += alen;
    4088    18076845 :         xfs_mod_delalloc(ip->i_mount, alen + indlen);
    4089             : 
    4090    18076268 :         got->br_startoff = aoff;
    4091    18076268 :         got->br_startblock = nullstartblock(indlen);
    4092    18076207 :         got->br_blockcount = alen;
    4093    18076207 :         got->br_state = XFS_EXT_NORM;
    4094             : 
    4095    18076207 :         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    18071680 :         if (whichfork == XFS_DATA_FORK && prealloc)
    4103     3016123 :                 xfs_inode_set_eofblocks_tag(ip);
    4104    18071715 :         if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
    4105      151463 :                 xfs_inode_set_cowblocks_tag(ip);
    4106             : 
    4107             :         return 0;
    4108             : 
    4109             : out_unreserve_blocks:
    4110      166643 :         xfs_mod_fdblocks(mp, alen, false);
    4111      733855 : out_unreserve_quota:
    4112      733855 :         if (XFS_IS_QUOTA_ON(mp))
    4113      733828 :                 xfs_quota_unreserve_blkres(ip, alen);
    4114             :         return error;
    4115             : }
    4116             : 
    4117             : static int
    4118    32318501 : xfs_bmap_alloc_userdata(
    4119             :         struct xfs_bmalloca     *bma)
    4120             : {
    4121    32318501 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4122    32318501 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4123    32318501 :         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    32318501 :         bma->datatype = XFS_ALLOC_NOBUSY;
    4132    32318501 :         if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
    4133    32317974 :                 bma->datatype |= XFS_ALLOC_USERDATA;
    4134    32317974 :                 if (bma->offset == 0)
    4135     2528199 :                         bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
    4136             : 
    4137    32317974 :                 if (mp->m_dalign && bma->length >= mp->m_dalign) {
    4138        6443 :                         error = xfs_bmap_isaeof(bma, whichfork);
    4139        6443 :                         if (error)
    4140             :                                 return error;
    4141             :                 }
    4142             : 
    4143    32317974 :                 if (XFS_IS_REALTIME_INODE(bma->ip))
    4144     8143832 :                         return xfs_bmap_rtalloc(bma);
    4145             :         }
    4146             : 
    4147    24174669 :         if (unlikely(XFS_TEST_ERROR(false, mp,
    4148             :                         XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4149      364807 :                 return xfs_bmap_exact_minlen_extent_alloc(bma);
    4150             : 
    4151    23809789 :         return xfs_bmap_btalloc(bma);
    4152             : }
    4153             : 
    4154             : static int
    4155    64264378 : xfs_bmapi_allocate(
    4156             :         struct xfs_bmalloca     *bma)
    4157             : {
    4158    64264378 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4159    64264378 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4160    64264378 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4161    64266181 :         int                     tmp_logflags = 0;
    4162    64266181 :         int                     error;
    4163             : 
    4164    64266181 :         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    64266181 :         if (bma->wasdel) {
    4171    15730356 :                 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
    4172    15730356 :                 bma->offset = bma->got.br_startoff;
    4173    15730356 :                 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
    4174     5172692 :                         bma->prev.br_startoff = NULLFILEOFF;
    4175             :         } else {
    4176    48535825 :                 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
    4177    48535825 :                 if (!bma->eof)
    4178     9416511 :                         bma->length = XFS_FILBLKS_MIN(bma->length,
    4179             :                                         bma->got.br_startoff - bma->offset);
    4180             :         }
    4181             : 
    4182    64266178 :         if (bma->flags & XFS_BMAPI_CONTIG)
    4183     4750469 :                 bma->minlen = bma->length;
    4184             :         else
    4185    59515709 :                 bma->minlen = 1;
    4186             : 
    4187    64266178 :         if (bma->flags & XFS_BMAPI_METADATA) {
    4188    31946643 :                 if (unlikely(XFS_TEST_ERROR(false, mp,
    4189             :                                 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4190        5307 :                         error = xfs_bmap_exact_minlen_extent_alloc(bma);
    4191             :                 else
    4192    31940362 :                         error = xfs_bmap_btalloc(bma);
    4193             :         } else {
    4194    32319535 :                 error = xfs_bmap_alloc_userdata(bma);
    4195             :         }
    4196    64265136 :         if (error || bma->blkno == NULLFSBLOCK)
    4197             :                 return error;
    4198             : 
    4199    64263605 :         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    64263605 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
    4206     8118254 :                 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    64263532 :         bma->nallocs++;
    4212             : 
    4213    64263532 :         if (bma->cur)
    4214     8116167 :                 bma->cur->bc_ino.flags =
    4215     8116167 :                         bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
    4216             : 
    4217    64263532 :         bma->got.br_startoff = bma->offset;
    4218    64263532 :         bma->got.br_startblock = bma->blkno;
    4219    64263532 :         bma->got.br_blockcount = bma->length;
    4220    64263532 :         bma->got.br_state = XFS_EXT_NORM;
    4221             : 
    4222    64263532 :         if (bma->flags & XFS_BMAPI_PREALLOC)
    4223    32317478 :                 bma->got.br_state = XFS_EXT_UNWRITTEN;
    4224             : 
    4225    64263532 :         if (bma->wasdel)
    4226    15729665 :                 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
    4227             :         else
    4228    48533867 :                 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    64263502 :         bma->logflags |= tmp_logflags;
    4233    64263502 :         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    64263202 :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4242             : 
    4243    64257967 :         ASSERT(bma->got.br_startoff <= bma->offset);
    4244    64257967 :         ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
    4245             :                bma->offset + bma->length);
    4246    64257967 :         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    76589841 : 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    76589841 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4259    76589841 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4260    76586223 :         int                     tmp_logflags = 0;
    4261    76586223 :         int                     error;
    4262             : 
    4263             :         /* check if we need to do unwritten->real conversion */
    4264    76586223 :         if (mval->br_state == XFS_EXT_UNWRITTEN &&
    4265    42221176 :             (flags & XFS_BMAPI_PREALLOC))
    4266             :                 return 0;
    4267             : 
    4268             :         /* check if we need to do real->unwritten conversion */
    4269    59240682 :         if (mval->br_state == XFS_EXT_NORM &&
    4270    34364237 :             (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    24876445 :         ASSERT(mval->br_blockcount <= len);
    4278    24876445 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
    4279     5949565 :                 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
    4280             :                                         bma->ip, whichfork);
    4281             :         }
    4282    24876409 :         mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
    4283    24876409 :                                 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
    4284             : 
    4285             :         /*
    4286             :          * Before insertion into the bmbt, zero the range being converted
    4287             :          * if required.
    4288             :          */
    4289    24876409 :         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    24876409 :         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    24877612 :         if (whichfork != XFS_COW_FORK)
    4310    24877612 :                 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
    4311    24877612 :         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    24877629 :         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    24876725 :         if (mval->br_blockcount < len)
    4326      389748 :                 return -EAGAIN;
    4327             :         return 0;
    4328             : }
    4329             : 
    4330             : xfs_extlen_t
    4331    92090940 : xfs_bmapi_minleft(
    4332             :         struct xfs_trans        *tp,
    4333             :         struct xfs_inode        *ip,
    4334             :         int                     fork)
    4335             : {
    4336    92090940 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, fork);
    4337             : 
    4338    92090335 :         if (tp && tp->t_highest_agno != NULLAGNUMBER)
    4339             :                 return 0;
    4340    92052565 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
    4341             :                 return 1;
    4342    14881724 :         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    91879198 : xfs_bmapi_finish(
    4353             :         struct xfs_bmalloca     *bma,
    4354             :         int                     whichfork,
    4355             :         int                     error)
    4356             : {
    4357    91879198 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4358             : 
    4359    96578631 :         if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
    4360    74984114 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    4361      725448 :                 bma->logflags &= ~xfs_ilog_fext(whichfork);
    4362    95853214 :         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    91880004 :         if (bma->logflags)
    4367    88847591 :                 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
    4368    91880457 :         if (bma->cur)
    4369    14793079 :                 xfs_btree_del_cursor(bma->cur, error);
    4370    91880387 : }
    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    76205124 : 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    76205124 :         struct xfs_bmalloca     bma = {
    4390             :                 .tp             = tp,
    4391             :                 .ip             = ip,
    4392             :                 .total          = total,
    4393             :         };
    4394    76205124 :         struct xfs_mount        *mp = ip->i_mount;
    4395    76205124 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4396    76205124 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4397    76205746 :         xfs_fileoff_t           end;            /* end of mapped file region */
    4398    76205746 :         bool                    eof = false;    /* after the end of extents */
    4399    76205746 :         int                     error;          /* error return */
    4400    76205746 :         int                     n;              /* current extent index */
    4401    76205746 :         xfs_fileoff_t           obno;           /* old block number (offset) */
    4402             : 
    4403             : #ifdef DEBUG
    4404    76205746 :         xfs_fileoff_t           orig_bno;       /* original block number value */
    4405    76205746 :         int                     orig_flags;     /* original flags arg value */
    4406    76205746 :         xfs_filblks_t           orig_len;       /* original value of len arg */
    4407    76205746 :         struct xfs_bmbt_irec    *orig_mval;     /* original value of mval */
    4408    76205746 :         int                     orig_nmap;      /* original value of *nmap */
    4409             : 
    4410    76205746 :         orig_bno = bno;
    4411    76205746 :         orig_len = len;
    4412    76205746 :         orig_flags = flags;
    4413    76205746 :         orig_mval = mval;
    4414    76205746 :         orig_nmap = *nmap;
    4415             : #endif
    4416             : 
    4417    76205746 :         ASSERT(*nmap >= 1);
    4418    76205746 :         ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
    4419    76205746 :         ASSERT(tp != NULL);
    4420    76205746 :         ASSERT(len > 0);
    4421    76205746 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
    4422    76205746 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4423    76203841 :         ASSERT(!(flags & XFS_BMAPI_REMAP));
    4424             : 
    4425             :         /* zeroing is for currently only for data extents, not metadata */
    4426    76203841 :         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    76203841 :         ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
    4435             :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
    4436             : 
    4437   152407322 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4438    76203841 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4439           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    4440           0 :                 return -EFSCORRUPTED;
    4441             :         }
    4442             : 
    4443   152406962 :         if (xfs_is_shutdown(mp))
    4444             :                 return -EIO;
    4445             : 
    4446    76203423 :         XFS_STATS_INC(mp, xs_blk_mapw);
    4447             : 
    4448    76203423 :         error = xfs_iread_extents(tp, ip, whichfork);
    4449    76202474 :         if (error)
    4450           0 :                 goto error0;
    4451             : 
    4452    76202474 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
    4453    39120366 :                 eof = true;
    4454    76202792 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4455    40145091 :                 bma.prev.br_startoff = NULLFILEOFF;
    4456    76201496 :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4457             : 
    4458    76199999 :         n = 0;
    4459    76199999 :         end = bno + len;
    4460    76199999 :         obno = bno;
    4461    76591187 :         while (bno < end && n < *nmap) {
    4462    76591187 :                 bool                    need_alloc = false, wasdelay = false;
    4463             : 
    4464             :                 /* in hole or beyond EOF? */
    4465    76591187 :                 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    48535539 :                         ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
    4472             :                                  (flags & XFS_BMAPI_COWFORK)));
    4473             : 
    4474             :                         need_alloc = true;
    4475    28055648 :                 } else if (isnullstartblock(bma.got.br_startblock)) {
    4476       45355 :                         wasdelay = true;
    4477             :                 }
    4478             : 
    4479             :                 /*
    4480             :                  * First, deal with the hole before the allocated space
    4481             :                  * that we found, if any.
    4482             :                  */
    4483    76591187 :                 if (need_alloc || wasdelay) {
    4484    48580797 :                         bma.eof = eof;
    4485    48580797 :                         bma.conv = !!(flags & XFS_BMAPI_CONVERT);
    4486    48580797 :                         bma.wasdel = wasdelay;
    4487    48580797 :                         bma.offset = bno;
    4488    48580797 :                         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    48580797 :                         if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
    4498        6772 :                                 bma.length = XFS_MAX_BMBT_EXTLEN;
    4499             :                         else
    4500    48574025 :                                 bma.length = len;
    4501             : 
    4502    48580797 :                         ASSERT(len > 0);
    4503    48580797 :                         ASSERT(bma.length > 0);
    4504    48580797 :                         error = xfs_bmapi_allocate(&bma);
    4505    48579182 :                         if (error)
    4506        1906 :                                 goto error0;
    4507    48577276 :                         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    48577236 :                         if (whichfork == XFS_COW_FORK)
    4515      219820 :                                 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
    4516             :                                                 bma.length);
    4517             :                 }
    4518             : 
    4519             :                 /* Deal with the allocated space we found.  */
    4520    76587626 :                 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
    4521             :                                                         end, n, flags);
    4522             : 
    4523             :                 /* Execute unwritten extent conversion if necessary */
    4524    76586850 :                 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
    4525    76587214 :                 if (error == -EAGAIN)
    4526      389739 :                         continue;
    4527    76197475 :                 if (error)
    4528           7 :                         goto error0;
    4529             : 
    4530             :                 /* update the extent map to return */
    4531    76197468 :                 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    76198124 :                 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
    4539             :                         break;
    4540             : 
    4541             :                 /* Else go on to the next record. */
    4542          80 :                 bma.prev = bma.got;
    4543          80 :                 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
    4544          24 :                         eof = true;
    4545             :         }
    4546    76198084 :         *nmap = n;
    4547             : 
    4548    76198084 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4549             :                         whichfork);
    4550    76193993 :         if (error)
    4551           0 :                 goto error0;
    4552             : 
    4553    76193993 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
    4554             :                ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
    4555    76193993 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4556    76200119 :         xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
    4557             :                 orig_nmap, *nmap);
    4558    76200119 :         return 0;
    4559        1913 : error0:
    4560        1913 :         xfs_bmapi_finish(&bma, whichfork, error);
    4561        1913 :         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    15818941 : 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    15818941 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4579    15818960 :         struct xfs_mount        *mp = ip->i_mount;
    4580    15818960 :         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
    4581    15818960 :         struct xfs_bmalloca     bma = { NULL };
    4582    15818960 :         uint16_t                flags = 0;
    4583    15818960 :         struct xfs_trans        *tp;
    4584    15818960 :         int                     error;
    4585             : 
    4586    15818960 :         if (whichfork == XFS_COW_FORK)
    4587      250431 :                 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    15818960 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
    4594             :                                 XFS_TRANS_RESERVE, &tp);
    4595    15819051 :         if (error)
    4596             :                 return error;
    4597             : 
    4598    15819040 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    4599    15819049 :         xfs_trans_ijoin(tp, ip, 0);
    4600             : 
    4601    15818637 :         error = xfs_iext_count_may_overflow(ip, whichfork,
    4602             :                         XFS_IEXT_ADD_NOSPLIT_CNT);
    4603    15819108 :         if (error == -EFBIG)
    4604           1 :                 error = xfs_iext_count_upgrade(tp, ip,
    4605             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
    4606    15819108 :         if (error)
    4607           1 :                 goto out_trans_cancel;
    4608             : 
    4609    15819107 :         if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
    4610    15818957 :             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    15818957 :         if (!isnullstartblock(bma.got.br_startblock)) {
    4626      133934 :                 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4627             :                                 xfs_iomap_inode_sequence(ip, flags));
    4628      133595 :                 *seq = READ_ONCE(ifp->if_seq);
    4629      133595 :                 goto out_trans_cancel;
    4630             :         }
    4631             : 
    4632    15685023 :         bma.tp = tp;
    4633    15685023 :         bma.ip = ip;
    4634    15685023 :         bma.wasdel = true;
    4635    15685023 :         bma.offset = bma.got.br_startoff;
    4636    15685023 :         bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
    4637             :                         XFS_MAX_BMBT_EXTLEN);
    4638    15685023 :         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    15684984 :         bma.flags = XFS_BMAPI_PREALLOC;
    4654    15684984 :         if (whichfork == XFS_COW_FORK)
    4655      116869 :                 bma.flags |= XFS_BMAPI_COWFORK;
    4656             : 
    4657    15684984 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4658     5170861 :                 bma.prev.br_startoff = NULLFILEOFF;
    4659             : 
    4660    15684984 :         error = xfs_bmapi_allocate(&bma);
    4661    15684945 :         if (error)
    4662         743 :                 goto out_finish;
    4663             : 
    4664    15684202 :         error = -ENOSPC;
    4665    15684202 :         if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
    4666           0 :                 goto out_finish;
    4667    31368404 :         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    15684202 :         XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
    4674    15684202 :         XFS_STATS_INC(mp, xs_xstrat_quick);
    4675             : 
    4676    15684202 :         ASSERT(!isnullstartblock(bma.got.br_startblock));
    4677    15684202 :         xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4678             :                                 xfs_iomap_inode_sequence(ip, flags));
    4679    15684198 :         *seq = READ_ONCE(ifp->if_seq);
    4680             : 
    4681    15684198 :         if (whichfork == XFS_COW_FORK)
    4682      116849 :                 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
    4683             : 
    4684    15684217 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4685             :                         whichfork);
    4686    15684210 :         if (error)
    4687           0 :                 goto out_finish;
    4688             : 
    4689    15684210 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4690    15684232 :         error = xfs_trans_commit(tp);
    4691    15684283 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4692    15684283 :         return error;
    4693             : 
    4694         743 : out_finish:
    4695         743 :         xfs_bmapi_finish(&bma, whichfork, error);
    4696      134339 : out_trans_cancel:
    4697      134339 :         xfs_trans_cancel(tp);
    4698      134854 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4699      134854 :         return error;
    4700             : }
    4701             : 
    4702             : int
    4703    53297250 : 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    53297250 :         struct xfs_mount        *mp = ip->i_mount;
    4712    53297250 :         struct xfs_ifork        *ifp;
    4713    53297250 :         struct xfs_btree_cur    *cur = NULL;
    4714    53297250 :         struct xfs_bmbt_irec    got;
    4715    53297250 :         struct xfs_iext_cursor  icur;
    4716    53297250 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4717    53297250 :         int                     logflags = 0, error;
    4718             : 
    4719    53297250 :         ifp = xfs_ifork_ptr(ip, whichfork);
    4720    53297250 :         ASSERT(len > 0);
    4721    53297250 :         ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
    4722    53297250 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4723    53297250 :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
    4724             :                            XFS_BMAPI_NORMAP)));
    4725    53297250 :         ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
    4726             :                         (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
    4727             : 
    4728   106594500 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4729    53297250 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4730           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    4731           0 :                 return -EFSCORRUPTED;
    4732             :         }
    4733             : 
    4734   106594500 :         if (xfs_is_shutdown(mp))
    4735             :                 return -EIO;
    4736             : 
    4737    53297247 :         error = xfs_iread_extents(tp, ip, whichfork);
    4738    53297247 :         if (error)
    4739             :                 return error;
    4740             : 
    4741    53297247 :         if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
    4742             :                 /* make sure we only reflink into a hole. */
    4743     3580328 :                 ASSERT(got.br_startoff > bno);
    4744     3580328 :                 ASSERT(got.br_startoff - bno >= len);
    4745             :         }
    4746             : 
    4747    53297247 :         ip->i_nblocks += len;
    4748    53297247 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    4749             : 
    4750    53297247 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    4751    51498850 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    4752    51498850 :                 cur->bc_ino.flags = 0;
    4753             :         }
    4754             : 
    4755    53297247 :         got.br_startoff = bno;
    4756    53297247 :         got.br_startblock = startblock;
    4757    53297247 :         got.br_blockcount = len;
    4758    53297247 :         if (flags & XFS_BMAPI_PREALLOC)
    4759      264542 :                 got.br_state = XFS_EXT_UNWRITTEN;
    4760             :         else
    4761    53032705 :                 got.br_state = XFS_EXT_NORM;
    4762             : 
    4763    53297247 :         error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
    4764             :                         &cur, &got, &logflags, flags);
    4765    53297246 :         if (error)
    4766           1 :                 goto error0;
    4767             : 
    4768    53297245 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
    4769             : 
    4770    53297247 : error0:
    4771    53297247 :         if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
    4772    51568925 :                 logflags &= ~XFS_ILOG_DEXT;
    4773     1728322 :         else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
    4774     1728322 :                 logflags &= ~XFS_ILOG_DBROOT;
    4775             : 
    4776    53297247 :         if (logflags)
    4777    52470813 :                 xfs_trans_log_inode(tp, ip, logflags);
    4778    53297246 :         if (cur)
    4779    51572484 :                 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         585 : 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         585 :         xfs_filblks_t                   len1 = *indlen1;
    4803         585 :         xfs_filblks_t                   len2 = *indlen2;
    4804         585 :         xfs_filblks_t                   nres = len1 + len2; /* new total res. */
    4805         585 :         xfs_filblks_t                   stolen = 0;
    4806         585 :         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         585 :         if (ores < nres && avail)
    4813         585 :                 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
    4814         585 :         ores += stolen;
    4815             : 
    4816             :          /* nothing else to do if we've satisfied the new reservation */
    4817         585 :         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          96 :         resfactor = (ores * 100);
    4829          96 :         do_div(resfactor, nres);
    4830          96 :         len1 *= resfactor;
    4831          96 :         do_div(len1, 100);
    4832          96 :         len2 *= resfactor;
    4833          96 :         do_div(len2, 100);
    4834          96 :         ASSERT(len1 + len2 <= ores);
    4835          96 :         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          96 :         ores -= (len1 + len2);
    4844          96 :         ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
    4845          96 :         if (ores && !len2 && *indlen2) {
    4846           0 :                 len2++;
    4847           0 :                 ores--;
    4848             :         }
    4849         158 :         while (ores) {
    4850          96 :                 if (len1 < *indlen1) {
    4851          96 :                         len1++;
    4852          96 :                         ores--;
    4853             :                 }
    4854          96 :                 if (!ores)
    4855             :                         break;
    4856          62 :                 if (len2 < *indlen2) {
    4857          62 :                         len2++;
    4858          62 :                         ores--;
    4859             :                 }
    4860             :         }
    4861             : 
    4862          96 :         *indlen1 = len1;
    4863          96 :         *indlen2 = len2;
    4864             : 
    4865          96 :         return stolen;
    4866             : }
    4867             : 
    4868             : int
    4869     1187498 : 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     1187498 :         struct xfs_mount        *mp = ip->i_mount;
    4877     1187498 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4878     1187970 :         struct xfs_bmbt_irec    new;
    4879     1187970 :         int64_t                 da_old, da_new, da_diff = 0;
    4880     1187970 :         xfs_fileoff_t           del_endoff, got_endoff;
    4881     1187970 :         xfs_filblks_t           got_indlen, new_indlen, stolen;
    4882     1187970 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    4883     1187970 :         int                     error = 0;
    4884     1187970 :         bool                    isrt;
    4885             : 
    4886     1187970 :         XFS_STATS_INC(mp, xs_del_exlist);
    4887             : 
    4888     1187970 :         isrt = xfs_ifork_is_realtime(ip, whichfork);
    4889     1187943 :         del_endoff = del->br_startoff + del->br_blockcount;
    4890     1187943 :         got_endoff = got->br_startoff + got->br_blockcount;
    4891     1187943 :         da_old = startblockval(got->br_startblock);
    4892     1187943 :         da_new = 0;
    4893             : 
    4894     1187943 :         ASSERT(del->br_blockcount > 0);
    4895     1187943 :         ASSERT(got->br_startoff <= del->br_startoff);
    4896     1187943 :         ASSERT(got_endoff >= del_endoff);
    4897             : 
    4898     1187943 :         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     1187943 :         ASSERT(!isrt);
    4911     1187943 :         error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
    4912     1187914 :         if (error)
    4913             :                 return error;
    4914     1187914 :         ip->i_delayed_blks -= del->br_blockcount;
    4915             : 
    4916     1187914 :         if (got->br_startoff == del->br_startoff)
    4917      563109 :                 state |= BMAP_LEFT_FILLING;
    4918     1187914 :         if (got_endoff == del_endoff)
    4919     1186159 :                 state |= BMAP_RIGHT_FILLING;
    4920             : 
    4921     1187914 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    4922      561709 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    4923             :                 /*
    4924             :                  * Matches the whole extent.  Delete the entry.
    4925             :                  */
    4926      561709 :                 xfs_iext_remove(ip, icur, state);
    4927      561630 :                 xfs_iext_prev(ifp, icur);
    4928      561630 :                 break;
    4929        1287 :         case BMAP_LEFT_FILLING:
    4930             :                 /*
    4931             :                  * Deleting the first part of the extent.
    4932             :                  */
    4933        1287 :                 got->br_startoff = del_endoff;
    4934        1287 :                 got->br_blockcount -= del->br_blockcount;
    4935        1287 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4936             :                                 got->br_blockcount), da_old);
    4937        1287 :                 got->br_startblock = nullstartblock((int)da_new);
    4938        1287 :                 xfs_iext_update_extent(ip, state, icur, got);
    4939        1287 :                 break;
    4940      624333 :         case BMAP_RIGHT_FILLING:
    4941             :                 /*
    4942             :                  * Deleting the last part of the extent.
    4943             :                  */
    4944      624333 :                 got->br_blockcount = got->br_blockcount - del->br_blockcount;
    4945      624333 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4946             :                                 got->br_blockcount), da_old);
    4947      624341 :                 got->br_startblock = nullstartblock((int)da_new);
    4948      624340 :                 xfs_iext_update_extent(ip, state, icur, got);
    4949      624340 :                 break;
    4950         585 :         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         585 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    4961         585 :                 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
    4962             : 
    4963         585 :                 new.br_blockcount = got_endoff - del_endoff;
    4964         585 :                 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
    4965             : 
    4966         585 :                 WARN_ON_ONCE(!got_indlen || !new_indlen);
    4967         585 :                 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
    4968             :                                                        del->br_blockcount);
    4969             : 
    4970         585 :                 got->br_startblock = nullstartblock((int)got_indlen);
    4971             : 
    4972         585 :                 new.br_startoff = del_endoff;
    4973         585 :                 new.br_state = got->br_state;
    4974         585 :                 new.br_startblock = nullstartblock((int)new_indlen);
    4975             : 
    4976         585 :                 xfs_iext_update_extent(ip, state, icur, got);
    4977         585 :                 xfs_iext_next(ifp, icur);
    4978         585 :                 xfs_iext_insert(ip, icur, &new, state);
    4979             : 
    4980         585 :                 da_new = got_indlen + new_indlen - stolen;
    4981         585 :                 del->br_blockcount -= stolen;
    4982         585 :                 break;
    4983             :         }
    4984             : 
    4985     1187760 :         ASSERT(da_old >= da_new);
    4986     1187760 :         da_diff = da_old - da_new;
    4987     1187760 :         if (!isrt)
    4988     1187911 :                 da_diff += del->br_blockcount;
    4989     1187760 :         if (da_diff) {
    4990     1187670 :                 xfs_mod_fdblocks(mp, da_diff, false);
    4991     1187850 :                 xfs_mod_delalloc(mp, -da_diff);
    4992             :         }
    4993             :         return error;
    4994             : }
    4995             : 
    4996             : void
    4997     1960109 : 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     1960109 :         struct xfs_mount        *mp = ip->i_mount;
    5004     1960109 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
    5005     1960109 :         struct xfs_bmbt_irec    new;
    5006     1960109 :         xfs_fileoff_t           del_endoff, got_endoff;
    5007     1960109 :         uint32_t                state = BMAP_COWFORK;
    5008             : 
    5009     1960109 :         XFS_STATS_INC(mp, xs_del_exlist);
    5010             : 
    5011     1960109 :         del_endoff = del->br_startoff + del->br_blockcount;
    5012     1960109 :         got_endoff = got->br_startoff + got->br_blockcount;
    5013             : 
    5014     1960109 :         ASSERT(del->br_blockcount > 0);
    5015     1960109 :         ASSERT(got->br_startoff <= del->br_startoff);
    5016     1960109 :         ASSERT(got_endoff >= del_endoff);
    5017     1960109 :         ASSERT(!isnullstartblock(got->br_startblock));
    5018             : 
    5019     1960109 :         if (got->br_startoff == del->br_startoff)
    5020     1946977 :                 state |= BMAP_LEFT_FILLING;
    5021     1960109 :         if (got_endoff == del_endoff)
    5022     1667137 :                 state |= BMAP_RIGHT_FILLING;
    5023             : 
    5024     1960109 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5025     1654469 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5026             :                 /*
    5027             :                  * Matches the whole extent.  Delete the entry.
    5028             :                  */
    5029     1654469 :                 xfs_iext_remove(ip, icur, state);
    5030     1654469 :                 xfs_iext_prev(ifp, icur);
    5031     1654469 :                 break;
    5032      292508 :         case BMAP_LEFT_FILLING:
    5033             :                 /*
    5034             :                  * Deleting the first part of the extent.
    5035             :                  */
    5036      292508 :                 got->br_startoff = del_endoff;
    5037      292508 :                 got->br_blockcount -= del->br_blockcount;
    5038      292508 :                 got->br_startblock = del->br_startblock + del->br_blockcount;
    5039      292508 :                 xfs_iext_update_extent(ip, state, icur, got);
    5040      292508 :                 break;
    5041       12668 :         case BMAP_RIGHT_FILLING:
    5042             :                 /*
    5043             :                  * Deleting the last part of the extent.
    5044             :                  */
    5045       12668 :                 got->br_blockcount -= del->br_blockcount;
    5046       12668 :                 xfs_iext_update_extent(ip, state, icur, got);
    5047       12668 :                 break;
    5048         464 :         case 0:
    5049             :                 /*
    5050             :                  * Deleting the middle of the extent.
    5051             :                  */
    5052         464 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    5053             : 
    5054         464 :                 new.br_startoff = del_endoff;
    5055         464 :                 new.br_blockcount = got_endoff - del_endoff;
    5056         464 :                 new.br_state = got->br_state;
    5057         464 :                 new.br_startblock = del->br_startblock + del->br_blockcount;
    5058             : 
    5059         464 :                 xfs_iext_update_extent(ip, state, icur, got);
    5060         464 :                 xfs_iext_next(ifp, icur);
    5061         464 :                 xfs_iext_insert(ip, icur, &new, state);
    5062         464 :                 break;
    5063             :         }
    5064     1960109 :         ip->i_delayed_blks -= del->br_blockcount;
    5065     1960109 : }
    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    93811630 : 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    93811630 :         xfs_fsblock_t           del_endblock=0; /* first block past del */
    5083    93811630 :         xfs_fileoff_t           del_endoff;     /* first offset past del */
    5084    93811630 :         int                     do_fx;  /* free extent at end of routine */
    5085    93811630 :         int                     error;  /* error return value */
    5086    93811630 :         int                     flags = 0;/* inode logging flags */
    5087    93811630 :         struct xfs_bmbt_irec    got;    /* current extent entry */
    5088    93811630 :         xfs_fileoff_t           got_endoff;     /* first offset past got */
    5089    93811630 :         int                     i;      /* temp state */
    5090    93811630 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    5091    93811630 :         xfs_mount_t             *mp;    /* mount structure */
    5092    93811630 :         xfs_filblks_t           nblks;  /* quota/sb block count */
    5093    93811630 :         xfs_bmbt_irec_t         new;    /* new record to be inserted */
    5094             :         /* REFERENCED */
    5095    93811630 :         uint                    qfield; /* quota field to update */
    5096    93811630 :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    5097    93811630 :         struct xfs_bmbt_irec    old;
    5098             : 
    5099    93811630 :         mp = ip->i_mount;
    5100    93811630 :         XFS_STATS_INC(mp, xs_del_exlist);
    5101             : 
    5102    93811630 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5103    93806513 :         ASSERT(del->br_blockcount > 0);
    5104    93806513 :         xfs_iext_get_extent(ifp, icur, &got);
    5105    93807576 :         ASSERT(got.br_startoff <= del->br_startoff);
    5106    93807576 :         del_endoff = del->br_startoff + del->br_blockcount;
    5107    93807576 :         got_endoff = got.br_startoff + got.br_blockcount;
    5108    93807576 :         ASSERT(got_endoff >= del_endoff);
    5109    93807576 :         ASSERT(!isnullstartblock(got.br_startblock));
    5110    93807576 :         qfield = 0;
    5111    93807576 :         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    93807576 :         if (tp->t_blk_res == 0 &&
    5122    77084535 :             ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
    5123    35894741 :             ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
    5124      208614 :             del->br_startoff > got.br_startoff && del_endoff < got_endoff)
    5125             :                 return -ENOSPC;
    5126             : 
    5127    93807576 :         flags = XFS_ILOG_CORE;
    5128    93807576 :         if (xfs_ifork_is_realtime(ip, whichfork)) {
    5129     3204583 :                 if (!(bflags & XFS_BMAPI_REMAP)) {
    5130     3204583 :                         error = xfs_rtfree_blocks(tp, del->br_startblock,
    5131             :                                         del->br_blockcount);
    5132     3204583 :                         if (error)
    5133           0 :                                 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    93806115 :         nblks = del->br_blockcount;
    5142             : 
    5143    93806115 :         del_endblock = del->br_startblock + del->br_blockcount;
    5144    93806115 :         if (cur) {
    5145    50116788 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5146    50116803 :                 if (error)
    5147           0 :                         goto done;
    5148    50116803 :                 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    93806130 :         if (got.br_startoff == del->br_startoff)
    5156    84703596 :                 state |= BMAP_LEFT_FILLING;
    5157    93806130 :         if (got_endoff == del_endoff)
    5158    84139304 :                 state |= BMAP_RIGHT_FILLING;
    5159             : 
    5160    93806130 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5161    82617888 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5162             :                 /*
    5163             :                  * Matches the whole extent.  Delete the entry.
    5164             :                  */
    5165    82617888 :                 xfs_iext_remove(ip, icur, state);
    5166    82620942 :                 xfs_iext_prev(ifp, icur);
    5167    82622195 :                 ifp->if_nextents--;
    5168             : 
    5169    82622195 :                 flags |= XFS_ILOG_CORE;
    5170    82622195 :                 if (!cur) {
    5171    39885954 :                         flags |= xfs_ilog_fext(whichfork);
    5172    39885954 :                         break;
    5173             :                 }
    5174    42736241 :                 if ((error = xfs_btree_delete(cur, &i)))
    5175           0 :                         goto done;
    5176    42736241 :                 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     2081027 :         case BMAP_LEFT_FILLING:
    5183             :                 /*
    5184             :                  * Deleting the first part of the extent.
    5185             :                  */
    5186     2081027 :                 got.br_startoff = del_endoff;
    5187     2081027 :                 got.br_startblock = del_endblock;
    5188     2081027 :                 got.br_blockcount -= del->br_blockcount;
    5189     2081027 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5190     2081032 :                 if (!cur) {
    5191     1238614 :                         flags |= xfs_ilog_fext(whichfork);
    5192     1238614 :                         break;
    5193             :                 }
    5194      842418 :                 error = xfs_bmbt_update(cur, &got);
    5195      842386 :                 if (error)
    5196           0 :                         goto done;
    5197             :                 break;
    5198     1517479 :         case BMAP_RIGHT_FILLING:
    5199             :                 /*
    5200             :                  * Deleting the last part of the extent.
    5201             :                  */
    5202     1517479 :                 got.br_blockcount -= del->br_blockcount;
    5203     1517479 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5204     1517481 :                 if (!cur) {
    5205     1141263 :                         flags |= xfs_ilog_fext(whichfork);
    5206     1141263 :                         break;
    5207             :                 }
    5208      376218 :                 error = xfs_bmbt_update(cur, &got);
    5209      376218 :                 if (error)
    5210           0 :                         goto done;
    5211             :                 break;
    5212     7589736 :         case 0:
    5213             :                 /*
    5214             :                  * Deleting the middle of the extent.
    5215             :                  */
    5216             : 
    5217     7589736 :                 old = got;
    5218             : 
    5219     7589736 :                 got.br_blockcount = del->br_startoff - got.br_startoff;
    5220     7589736 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5221             : 
    5222     7589738 :                 new.br_startoff = del_endoff;
    5223     7589738 :                 new.br_blockcount = got_endoff - del_endoff;
    5224     7589738 :                 new.br_state = got.br_state;
    5225     7589738 :                 new.br_startblock = del_endblock;
    5226             : 
    5227     7589738 :                 flags |= XFS_ILOG_CORE;
    5228     7589738 :                 if (cur) {
    5229     6161926 :                         error = xfs_bmbt_update(cur, &got);
    5230     6161926 :                         if (error)
    5231           0 :                                 goto done;
    5232     6161926 :                         error = xfs_btree_increment(cur, 0, &i);
    5233     6161926 :                         if (error)
    5234           0 :                                 goto done;
    5235     6161926 :                         cur->bc_rec.b = new;
    5236     6161926 :                         error = xfs_btree_insert(cur, &i);
    5237     6161925 :                         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     6161925 :                         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     6161925 :                         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     1427829 :                         flags |= xfs_ilog_fext(whichfork);
    5280             : 
    5281     7589737 :                 ifp->if_nextents++;
    5282     7589737 :                 xfs_iext_next(ifp, icur);
    5283     7589737 :                 xfs_iext_insert(ip, icur, &new, state);
    5284     7589737 :                 break;
    5285             :         }
    5286             : 
    5287             :         /* remove reverse mapping */
    5288    93810414 :         xfs_rmap_unmap_extent(tp, ip, whichfork, del);
    5289             : 
    5290             :         /*
    5291             :          * If we need to, add to list of extents to delete.
    5292             :          */
    5293    93812383 :         if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
    5294    84650032 :                 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
    5295    50195050 :                         xfs_refcount_decrease_extent(tp, del);
    5296             :                 } else {
    5297    34454982 :                         error = __xfs_free_extent_later(tp, del->br_startblock,
    5298             :                                         del->br_blockcount, NULL,
    5299             :                                         XFS_AG_RESV_NONE,
    5300    34454982 :                                         ((bflags & XFS_BMAPI_NODISCARD) ||
    5301    34439378 :                                         del->br_state == XFS_EXT_UNWRITTEN));
    5302    34451902 :                         if (error)
    5303           0 :                                 goto done;
    5304             :                 }
    5305             :         }
    5306             : 
    5307             :         /*
    5308             :          * Adjust inode # blocks in the file.
    5309             :          */
    5310    93809415 :         if (nblks)
    5311    93809026 :                 ip->i_nblocks -= nblks;
    5312             :         /*
    5313             :          * Adjust quota data.
    5314             :          */
    5315    93809415 :         if (qfield && !(bflags & XFS_BMAPI_REMAP))
    5316    87852664 :                 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
    5317             : 
    5318     5956751 : done:
    5319    93808035 :         *logflagsp = flags;
    5320    93808035 :         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    78608057 : __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    78608057 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
    5339    78608057 :         struct xfs_bmbt_irec    del;            /* extent being deleted */
    5340    78608057 :         int                     error;          /* error return value */
    5341    78608057 :         xfs_extnum_t            extno;          /* extent number in list */
    5342    78608057 :         struct xfs_bmbt_irec    got;            /* current extent record */
    5343    78608057 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
    5344    78608057 :         int                     isrt;           /* freeing in rt area */
    5345    78608057 :         int                     logflags;       /* transaction logging flags */
    5346    78608057 :         xfs_extlen_t            mod;            /* rt extent offset */
    5347    78608057 :         struct xfs_mount        *mp = ip->i_mount;
    5348    78608057 :         int                     tmp_logflags;   /* partial logging flags */
    5349    78608057 :         int                     wasdel;         /* was a delayed alloc extent */
    5350    78608057 :         int                     whichfork;      /* data or attribute fork */
    5351    78608057 :         xfs_fsblock_t           sum;
    5352    78608057 :         xfs_filblks_t           len = *rlen;    /* length to unmap in file */
    5353    78608057 :         xfs_fileoff_t           end;
    5354    78608057 :         struct xfs_iext_cursor  icur;
    5355    78608057 :         bool                    done = false;
    5356             : 
    5357    78608057 :         trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
    5358             : 
    5359    78606833 :         whichfork = xfs_bmapi_whichfork(flags);
    5360    78606833 :         ASSERT(whichfork != XFS_COW_FORK);
    5361    78606833 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5362    78607235 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
    5363           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5364           0 :                 return -EFSCORRUPTED;
    5365             :         }
    5366   157214470 :         if (xfs_is_shutdown(mp))
    5367             :                 return -EIO;
    5368             : 
    5369    78606906 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5370    78606391 :         ASSERT(len > 0);
    5371    78606391 :         ASSERT(nexts >= 0);
    5372             : 
    5373    78606391 :         error = xfs_iread_extents(tp, ip, whichfork);
    5374    78609425 :         if (error)
    5375             :                 return error;
    5376             : 
    5377    78609559 :         if (xfs_iext_count(ifp) == 0) {
    5378      727011 :                 *rlen = 0;
    5379      727011 :                 return 0;
    5380             :         }
    5381    77878413 :         XFS_STATS_INC(mp, xs_blk_unmap);
    5382    77878413 :         isrt = xfs_ifork_is_realtime(ip, whichfork);
    5383    77875174 :         end = start + len;
    5384             : 
    5385    77875174 :         if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
    5386      219002 :                 *rlen = 0;
    5387      219002 :                 return 0;
    5388             :         }
    5389    77656033 :         end--;
    5390             : 
    5391    77656033 :         logflags = 0;
    5392    77656033 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5393    29939284 :                 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
    5394    29939284 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5395    29939287 :                 cur->bc_ino.flags = 0;
    5396             :         } else
    5397    47716749 :                 cur = NULL;
    5398             : 
    5399    77656036 :         if (isrt) {
    5400             :                 /*
    5401             :                  * Synchronize by locking the bitmap inode.
    5402             :                  */
    5403     2506874 :                 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
    5404     2506884 :                 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
    5405     2506884 :                 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
    5406     2506884 :                 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
    5407             :         }
    5408             : 
    5409             :         extno = 0;
    5410   171510510 :         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    95832380 :                 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    95832380 :                 end = XFS_FILEOFF_MIN(end,
    5426             :                         got.br_startoff + got.br_blockcount - 1);
    5427    95832380 :                 if (end < start)
    5428             :                         break;
    5429             :                 /*
    5430             :                  * Then deal with the (possibly delayed) allocated space
    5431             :                  * we found.
    5432             :                  */
    5433    94972396 :                 del = got;
    5434    94972396 :                 wasdel = isnullstartblock(del.br_startblock);
    5435             : 
    5436    94972396 :                 if (got.br_startoff < start) {
    5437     9720162 :                         del.br_startoff = start;
    5438     9720162 :                         del.br_blockcount -= start - got.br_startoff;
    5439     9720162 :                         if (!wasdel)
    5440     9107214 :                                 del.br_startblock += start - got.br_startoff;
    5441             :                 }
    5442    94972396 :                 if (del.br_startoff + del.br_blockcount > end + 1)
    5443     9671365 :                         del.br_blockcount = end + 1 - del.br_startoff;
    5444             : 
    5445    94972396 :                 if (!isrt || (flags & XFS_BMAPI_REMAP))
    5446    91767811 :                         goto delete;
    5447             : 
    5448     3204585 :                 sum = del.br_startblock + del.br_blockcount;
    5449     3204585 :                 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
    5450     3204585 :                 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     3204585 :                 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
    5497     3204585 :                 if (mod) {
    5498           2 :                         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           2 :                         if (del.br_blockcount > off) {
    5506           0 :                                 del.br_blockcount -= off;
    5507           0 :                                 del.br_startoff += off;
    5508           0 :                                 del.br_startblock += off;
    5509           2 :                         } else if (del.br_startoff == start &&
    5510           0 :                                    (del.br_state == XFS_EXT_UNWRITTEN ||
    5511           0 :                                     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           0 :                                 ASSERT(end >= del.br_blockcount);
    5517           0 :                                 end -= del.br_blockcount;
    5518           0 :                                 if (got.br_startoff > end &&
    5519           0 :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5520             :                                         done = true;
    5521             :                                         break;
    5522             :                                 }
    5523           0 :                                 continue;
    5524           2 :                         } else if (del.br_state == XFS_EXT_UNWRITTEN) {
    5525           2 :                                 struct xfs_bmbt_irec    prev;
    5526           2 :                                 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           2 :                                 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
    5535           0 :                                         ASSERT(0);
    5536           2 :                                 ASSERT(prev.br_state == XFS_EXT_NORM);
    5537           2 :                                 ASSERT(!isnullstartblock(prev.br_startblock));
    5538           2 :                                 ASSERT(del.br_startblock ==
    5539             :                                        prev.br_startblock + prev.br_blockcount);
    5540           2 :                                 unwrite_start = max3(start,
    5541             :                                                      del.br_startoff - mod,
    5542             :                                                      prev.br_startoff);
    5543           2 :                                 mod = unwrite_start - prev.br_startoff;
    5544           2 :                                 prev.br_startoff = unwrite_start;
    5545           2 :                                 prev.br_startblock += mod;
    5546           2 :                                 prev.br_blockcount -= mod;
    5547           2 :                                 prev.br_state = XFS_EXT_UNWRITTEN;
    5548           2 :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5549             :                                                 ip, whichfork, &icur, &cur,
    5550             :                                                 &prev, &logflags);
    5551           2 :                                 if (error)
    5552           0 :                                         goto error0;
    5553           2 :                                 goto nodelete;
    5554             :                         } else {
    5555           0 :                                 ASSERT(del.br_state == XFS_EXT_NORM);
    5556           0 :                                 del.br_state = XFS_EXT_UNWRITTEN;
    5557           0 :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5558             :                                                 ip, whichfork, &icur, &cur,
    5559             :                                                 &del, &logflags);
    5560           0 :                                 if (error)
    5561           0 :                                         goto error0;
    5562           0 :                                 goto nodelete;
    5563             :                         }
    5564             :                 }
    5565             : 
    5566     3204583 : delete:
    5567    94972394 :                 if (wasdel) {
    5568     1163997 :                         error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
    5569             :                                         &got, &del);
    5570             :                 } else {
    5571    93808397 :                         error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
    5572             :                                         &del, &tmp_logflags, whichfork,
    5573             :                                         flags);
    5574    93809405 :                         logflags |= tmp_logflags;
    5575             :                 }
    5576             : 
    5577    94976967 :                 if (error)
    5578           0 :                         goto error0;
    5579             : 
    5580    94976967 :                 end = del.br_startoff - 1;
    5581    94976969 : nodelete:
    5582             :                 /*
    5583             :                  * If not done go on to the next (previous) record.
    5584             :                  */
    5585    94976969 :                 if (end != (xfs_fileoff_t)-1 && end >= start) {
    5586    51219274 :                         if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5587    50616315 :                             (got.br_startoff > end &&
    5588      486989 :                              !xfs_iext_prev_extent(ifp, &icur, &got))) {
    5589             :                                 done = true;
    5590             :                                 break;
    5591             :                         }
    5592    50096769 :                         extno++;
    5593             :                 }
    5594             :         }
    5595    76538114 :         if (done || end == (xfs_fileoff_t)-1 || end < start)
    5596    53513692 :                 *rlen = 0;
    5597             :         else
    5598    24146920 :                 *rlen = end - start + 1;
    5599             : 
    5600             :         /*
    5601             :          * Convert to a btree if necessary.
    5602             :          */
    5603    77660612 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    5604       56127 :                 ASSERT(cur == NULL);
    5605       56127 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    5606             :                                 &tmp_logflags, whichfork);
    5607       56127 :                 logflags |= tmp_logflags;
    5608             :         } else {
    5609    77600898 :                 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
    5610             :                         whichfork);
    5611             :         }
    5612             : 
    5613    77652438 : 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    81021969 :         if ((logflags & xfs_ilog_fext(whichfork)) &&
    5619    39972447 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    5620       56127 :                 logflags &= ~xfs_ilog_fext(whichfork);
    5621    80968290 :         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    77652438 :         if (logflags)
    5629    68925875 :                 xfs_trans_log_inode(tp, ip, logflags);
    5630    77653254 :         if (cur) {
    5631    29991202 :                 if (!error)
    5632    29995413 :                         cur->bc_ino.allocated = 0;
    5633    29991202 :                 xfs_btree_del_cursor(cur, error);
    5634             :         }
    5635             :         return error;
    5636             : }
    5637             : 
    5638             : /* Unmap a range of a file. */
    5639             : int
    5640    36996823 : 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    36996823 :         int                     error;
    5650             : 
    5651    36996823 :         error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
    5652    36995581 :         *done = (len == 0);
    5653    36995581 :         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    20047464 : 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    20047464 :         xfs_fileoff_t           startoff;
    5667             : 
    5668    20047464 :         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    20047464 :         if ((left->br_startoff + left->br_blockcount != startoff) ||
    5675    18846154 :             (left->br_startblock + left->br_blockcount != got->br_startblock) ||
    5676      226700 :             (left->br_state != got->br_state) ||
    5677        5190 :             (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
    5678    20042274 :                 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        2595 : 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        2595 :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    5705        2595 :         struct xfs_bmbt_irec            new;
    5706        2595 :         xfs_filblks_t                   blockcount;
    5707        2595 :         int                             error, i;
    5708        2595 :         struct xfs_mount                *mp = ip->i_mount;
    5709             : 
    5710        2595 :         blockcount = left->br_blockcount + got->br_blockcount;
    5711             : 
    5712        2595 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5713        2595 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5714        2595 :         ASSERT(xfs_bmse_can_merge(left, got, shift));
    5715             : 
    5716        2595 :         new = *left;
    5717        2595 :         new.br_blockcount = blockcount;
    5718             : 
    5719             :         /*
    5720             :          * Update the on-disk extent count, the btree if necessary and log the
    5721             :          * inode.
    5722             :          */
    5723        2595 :         ifp->if_nextents--;
    5724        2595 :         *logflags |= XFS_ILOG_CORE;
    5725        2595 :         if (!cur) {
    5726        2239 :                 *logflags |= XFS_ILOG_DEXT;
    5727        2239 :                 goto done;
    5728             :         }
    5729             : 
    5730             :         /* lookup and remove the extent to merge */
    5731         356 :         error = xfs_bmbt_lookup_eq(cur, got, &i);
    5732         356 :         if (error)
    5733             :                 return error;
    5734         356 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5735           0 :                 xfs_btree_mark_sick(cur);
    5736           0 :                 return -EFSCORRUPTED;
    5737             :         }
    5738             : 
    5739         356 :         error = xfs_btree_delete(cur, &i);
    5740         356 :         if (error)
    5741             :                 return error;
    5742         356 :         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         356 :         error = xfs_bmbt_lookup_eq(cur, left, &i);
    5749         356 :         if (error)
    5750             :                 return error;
    5751         356 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5752           0 :                 xfs_btree_mark_sick(cur);
    5753           0 :                 return -EFSCORRUPTED;
    5754             :         }
    5755             : 
    5756         356 :         error = xfs_bmbt_update(cur, &new);
    5757         356 :         if (error)
    5758             :                 return error;
    5759             : 
    5760             :         /* change to extent format if required after extent removal */
    5761         356 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
    5762         356 :         if (error)
    5763             :                 return error;
    5764             : 
    5765         356 : done:
    5766        2595 :         xfs_iext_remove(ip, icur, 0);
    5767        2595 :         xfs_iext_prev(ifp, icur);
    5768        5190 :         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        2595 :         xfs_rmap_unmap_extent(tp, ip, whichfork, got);
    5773        2595 :         memcpy(&new, got, sizeof(new));
    5774        2595 :         new.br_startoff = left->br_startoff + left->br_blockcount;
    5775        2595 :         xfs_rmap_map_extent(tp, ip, whichfork, &new);
    5776        2595 :         return 0;
    5777             : }
    5778             : 
    5779             : static int
    5780    20444338 : 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    20444338 :         struct xfs_mount        *mp = ip->i_mount;
    5791    20444338 :         struct xfs_bmbt_irec    prev = *got;
    5792    20444338 :         int                     error, i;
    5793             : 
    5794    20444338 :         *logflags |= XFS_ILOG_CORE;
    5795             : 
    5796    20444338 :         got->br_startoff = startoff;
    5797             : 
    5798    20444338 :         if (cur) {
    5799    18546973 :                 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
    5800    18546973 :                 if (error)
    5801             :                         return error;
    5802    18546973 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5803           0 :                         xfs_btree_mark_sick(cur);
    5804           0 :                         return -EFSCORRUPTED;
    5805             :                 }
    5806             : 
    5807    18546973 :                 error = xfs_bmbt_update(cur, got);
    5808    18546973 :                 if (error)
    5809             :                         return error;
    5810             :         } else {
    5811     1897365 :                 *logflags |= XFS_ILOG_DEXT;
    5812             :         }
    5813             : 
    5814    40888676 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5815             :                         got);
    5816             : 
    5817             :         /* update reverse mapping */
    5818    20444337 :         xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
    5819    20444338 :         xfs_rmap_map_extent(tp, ip, whichfork, got);
    5820    20444338 :         return 0;
    5821             : }
    5822             : 
    5823             : int
    5824    18963645 : 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    18963645 :         int                     whichfork = XFS_DATA_FORK;
    5832    18963645 :         struct xfs_mount        *mp = ip->i_mount;
    5833    18963645 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5834    18963645 :         struct xfs_btree_cur    *cur = NULL;
    5835    18963645 :         struct xfs_bmbt_irec    got, prev;
    5836    18963645 :         struct xfs_iext_cursor  icur;
    5837    18963645 :         xfs_fileoff_t           new_startoff;
    5838    18963645 :         int                     error = 0;
    5839    18963645 :         int                     logflags = 0;
    5840             : 
    5841    37927290 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5842    18963645 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5843           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5844           0 :                 return -EFSCORRUPTED;
    5845             :         }
    5846             : 
    5847    37927290 :         if (xfs_is_shutdown(mp))
    5848             :                 return -EIO;
    5849             : 
    5850    18963643 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5851             : 
    5852    18963643 :         error = xfs_iread_extents(tp, ip, whichfork);
    5853    18963643 :         if (error)
    5854             :                 return error;
    5855             : 
    5856    18963643 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5857    17896167 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5858    17896167 :                 cur->bc_ino.flags = 0;
    5859             :         }
    5860             : 
    5861    18963643 :         if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5862       45676 :                 *done = true;
    5863       45676 :                 goto del_cursor;
    5864             :         }
    5865    18917967 :         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    18917967 :         new_startoff = got.br_startoff - offset_shift_fsb;
    5872    18917967 :         if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
    5873    18854442 :                 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
    5874           0 :                         error = -EINVAL;
    5875           0 :                         goto del_cursor;
    5876             :                 }
    5877             : 
    5878    18854442 :                 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
    5879        2595 :                         error = xfs_bmse_merge(tp, ip, whichfork,
    5880             :                                         offset_shift_fsb, &icur, &got, &prev,
    5881             :                                         cur, &logflags);
    5882        2595 :                         if (error)
    5883           0 :                                 goto del_cursor;
    5884        2595 :                         goto done;
    5885             :                 }
    5886             :         } else {
    5887       63525 :                 if (got.br_startoff < offset_shift_fsb) {
    5888           0 :                         error = -EINVAL;
    5889           0 :                         goto del_cursor;
    5890             :                 }
    5891             :         }
    5892             : 
    5893    18915372 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    5894             :                         cur, &logflags, new_startoff);
    5895    18915372 :         if (error)
    5896           0 :                 goto del_cursor;
    5897             : 
    5898    18915372 : done:
    5899    18917967 :         if (!xfs_iext_next_extent(ifp, &icur, &got)) {
    5900      411771 :                 *done = true;
    5901      411771 :                 goto del_cursor;
    5902             :         }
    5903             : 
    5904    18506196 :         *next_fsb = got.br_startoff;
    5905    18963643 : del_cursor:
    5906    18963643 :         if (cur)
    5907    17896167 :                 xfs_btree_del_cursor(cur, error);
    5908    18963643 :         if (logflags)
    5909    18917967 :                 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      379186 : xfs_bmap_can_insert_extents(
    5916             :         struct xfs_inode        *ip,
    5917             :         xfs_fileoff_t           off,
    5918             :         xfs_fileoff_t           shift)
    5919             : {
    5920      379186 :         struct xfs_bmbt_irec    got;
    5921      379186 :         int                     is_empty;
    5922      379186 :         int                     error = 0;
    5923             : 
    5924      379186 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5925             : 
    5926      758372 :         if (xfs_is_shutdown(ip->i_mount))
    5927             :                 return -EIO;
    5928             : 
    5929      379184 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    5930      379184 :         error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
    5931      379184 :         if (!error && !is_empty && got.br_startoff >= off &&
    5932      321839 :             ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
    5933           0 :                 error = -EINVAL;
    5934      379184 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    5935             : 
    5936      379184 :         return error;
    5937             : }
    5938             : 
    5939             : int
    5940     1568460 : 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     1568460 :         int                     whichfork = XFS_DATA_FORK;
    5949     1568460 :         struct xfs_mount        *mp = ip->i_mount;
    5950     1568460 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5951     1568461 :         struct xfs_btree_cur    *cur = NULL;
    5952     1568461 :         struct xfs_bmbt_irec    got, next;
    5953     1568461 :         struct xfs_iext_cursor  icur;
    5954     1568461 :         xfs_fileoff_t           new_startoff;
    5955     1568461 :         int                     error = 0;
    5956     1568461 :         int                     logflags = 0;
    5957             : 
    5958     3136922 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5959     1568461 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5960           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    5961           0 :                 return -EFSCORRUPTED;
    5962             :         }
    5963             : 
    5964     3136922 :         if (xfs_is_shutdown(mp))
    5965             :                 return -EIO;
    5966             : 
    5967     1568461 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5968             : 
    5969     1568461 :         error = xfs_iread_extents(tp, ip, whichfork);
    5970     1568460 :         if (error)
    5971             :                 return error;
    5972             : 
    5973     1568460 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5974      656267 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5975      656267 :                 cur->bc_ino.flags = 0;
    5976             :         }
    5977             : 
    5978     1568460 :         if (*next_fsb == NULLFSBLOCK) {
    5979      378033 :                 xfs_iext_last(ifp, &icur);
    5980      378034 :                 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5981      369874 :                     stop_fsb > got.br_startoff) {
    5982       39495 :                         *done = true;
    5983       39495 :                         goto del_cursor;
    5984             :                 }
    5985             :         } else {
    5986     1190427 :                 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5987           0 :                         *done = true;
    5988           0 :                         goto del_cursor;
    5989             :                 }
    5990             :         }
    5991     1528966 :         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     1528966 :         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     1528966 :         new_startoff = got.br_startoff + offset_shift_fsb;
    6004     1528966 :         if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
    6005     1190427 :                 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     1190427 :                 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
    6017           0 :                         WARN_ON_ONCE(1);
    6018             :         }
    6019             : 
    6020     1528966 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    6021             :                         cur, &logflags, new_startoff);
    6022     1528966 :         if (error)
    6023           0 :                 goto del_cursor;
    6024             : 
    6025     1528966 :         if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
    6026     1465829 :             stop_fsb >= got.br_startoff + got.br_blockcount) {
    6027      338539 :                 *done = true;
    6028      338539 :                 goto del_cursor;
    6029             :         }
    6030             : 
    6031     1190427 :         *next_fsb = got.br_startoff;
    6032     1568461 : del_cursor:
    6033     1568461 :         if (cur)
    6034      656268 :                 xfs_btree_del_cursor(cur, error);
    6035     1568461 :         if (logflags)
    6036     1528965 :                 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      378034 : xfs_bmap_split_extent(
    6048             :         struct xfs_trans        *tp,
    6049             :         struct xfs_inode        *ip,
    6050             :         xfs_fileoff_t           split_fsb)
    6051             : {
    6052      378034 :         int                             whichfork = XFS_DATA_FORK;
    6053      378034 :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    6054      378034 :         struct xfs_btree_cur            *cur = NULL;
    6055      378034 :         struct xfs_bmbt_irec            got;
    6056      378034 :         struct xfs_bmbt_irec            new; /* split extent */
    6057      378034 :         struct xfs_mount                *mp = ip->i_mount;
    6058      378034 :         xfs_fsblock_t                   gotblkcnt; /* new block count for got */
    6059      378034 :         struct xfs_iext_cursor          icur;
    6060      378034 :         int                             error = 0;
    6061      378034 :         int                             logflags = 0;
    6062      378034 :         int                             i = 0;
    6063             : 
    6064      756067 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    6065      378034 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    6066           0 :                 xfs_bmap_mark_sick(ip, whichfork);
    6067           0 :                 return -EFSCORRUPTED;
    6068             :         }
    6069             : 
    6070      756066 :         if (xfs_is_shutdown(mp))
    6071             :                 return -EIO;
    6072             : 
    6073             :         /* Read in all the extents */
    6074      378033 :         error = xfs_iread_extents(tp, ip, whichfork);
    6075      378033 :         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      378033 :         if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
    6082      338538 :             got.br_startoff >= split_fsb)
    6083             :                 return 0;
    6084             : 
    6085       80670 :         gotblkcnt = split_fsb - got.br_startoff;
    6086       80670 :         new.br_startoff = split_fsb;
    6087       80670 :         new.br_startblock = got.br_startblock + gotblkcnt;
    6088       80670 :         new.br_blockcount = got.br_blockcount - gotblkcnt;
    6089       80670 :         new.br_state = got.br_state;
    6090             : 
    6091       80670 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    6092        9594 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    6093        9594 :                 cur->bc_ino.flags = 0;
    6094        9594 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    6095        9594 :                 if (error)
    6096           0 :                         goto del_cursor;
    6097        9594 :                 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       80670 :         got.br_blockcount = gotblkcnt;
    6105       80670 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
    6106             :                         &got);
    6107             : 
    6108       80670 :         logflags = XFS_ILOG_CORE;
    6109       80670 :         if (cur) {
    6110        9594 :                 error = xfs_bmbt_update(cur, &got);
    6111        9594 :                 if (error)
    6112           0 :                         goto del_cursor;
    6113             :         } else
    6114             :                 logflags |= XFS_ILOG_DEXT;
    6115             : 
    6116             :         /* Add new extent */
    6117       80670 :         xfs_iext_next(ifp, &icur);
    6118       80670 :         xfs_iext_insert(ip, &icur, &new, 0);
    6119       80670 :         ifp->if_nextents++;
    6120             : 
    6121       80670 :         if (cur) {
    6122        9594 :                 error = xfs_bmbt_lookup_eq(cur, &new, &i);
    6123        9594 :                 if (error)
    6124           0 :                         goto del_cursor;
    6125        9594 :                 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        9594 :                 error = xfs_btree_insert(cur, &i);
    6131        9594 :                 if (error)
    6132           0 :                         goto del_cursor;
    6133        9594 :                 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       80670 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    6144        3028 :                 int tmp_logflags; /* partial log flag return val */
    6145             : 
    6146        3028 :                 ASSERT(cur == NULL);
    6147        3028 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    6148             :                                 &tmp_logflags, whichfork);
    6149        3028 :                 logflags |= tmp_logflags;
    6150             :         }
    6151             : 
    6152       77642 : del_cursor:
    6153       80670 :         if (cur) {
    6154       12622 :                 cur->bc_ino.allocated = 0;
    6155       12622 :                 xfs_btree_del_cursor(cur, error);
    6156             :         }
    6157             : 
    6158       80670 :         if (logflags)
    6159       80670 :                 xfs_trans_log_inode(tp, ip, logflags);
    6160             :         return error;
    6161             : }
    6162             : 
    6163             : /* Record a bmap intent. */
    6164             : static void
    6165    60004635 : __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    60004635 :         struct xfs_bmap_intent          *bi;
    6173             : 
    6174    60004635 :         if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
    6175    60004635 :             bmap->br_startblock == HOLESTARTBLOCK ||
    6176             :             bmap->br_startblock == DELAYSTARTBLOCK)
    6177             :                 return;
    6178             : 
    6179    59253869 :         bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
    6180    59253894 :         INIT_LIST_HEAD(&bi->bi_list);
    6181    59253894 :         bi->bi_type = type;
    6182    59253894 :         bi->bi_owner = ip;
    6183    59253894 :         bi->bi_whichfork = whichfork;
    6184    59253894 :         bi->bi_bmap = *bmap;
    6185             : 
    6186    59253894 :         trace_xfs_bmap_defer(bi);
    6187             : 
    6188    59253898 :         xfs_bmap_update_get_group(tp->t_mountp, bi);
    6189    59253870 :         xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
    6190             : }
    6191             : 
    6192             : /* Map an extent into a file. */
    6193             : void
    6194    53672559 : 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    53672559 :         __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
    6201    53672565 : }
    6202             : 
    6203             : /* Unmap an extent out of a file. */
    6204             : void
    6205     6332095 : 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     6332095 :         __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
    6212     6332099 : }
    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    59254016 : xfs_bmap_finish_one(
    6220             :         struct xfs_trans                *tp,
    6221             :         struct xfs_bmap_intent          *bi)
    6222             : {
    6223    59254016 :         struct xfs_bmbt_irec            *bmap = &bi->bi_bmap;
    6224    59254016 :         int                             error = 0;
    6225    59254016 :         int                             flags = 0;
    6226             : 
    6227    59254016 :         if (bi->bi_whichfork == XFS_ATTR_FORK)
    6228      218466 :                 flags |= XFS_BMAPI_ATTRFORK;
    6229             : 
    6230    59254016 :         ASSERT(tp->t_highest_agno == NULLAGNUMBER);
    6231             : 
    6232    59254016 :         trace_xfs_bmap_deferred(bi);
    6233             : 
    6234    59254015 :         if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
    6235             :                 return -EIO;
    6236             : 
    6237    59253999 :         switch (bi->bi_type) {
    6238    53297250 :         case XFS_BMAP_MAP:
    6239    53297250 :                 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
    6240      264542 :                         flags |= XFS_BMAPI_PREALLOC;
    6241    53297250 :                 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
    6242             :                                 bmap->br_blockcount, bmap->br_startblock,
    6243             :                                 flags);
    6244    53297250 :                 bmap->br_blockcount = 0;
    6245    53297250 :                 break;
    6246     5956749 :         case XFS_BMAP_UNMAP:
    6247     5956749 :                 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
    6248             :                                 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
    6249             :                                 1);
    6250     5956749 :                 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  2460853677 : xfs_bmap_validate_extent_raw(
    6263             :         struct xfs_mount        *mp,
    6264             :         bool                    rtfile,
    6265             :         int                     whichfork,
    6266             :         struct xfs_bmbt_irec    *irec)
    6267             : {
    6268  2460853677 :         if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
    6269           0 :                 return __this_address;
    6270             : 
    6271  2460884022 :         if (rtfile && whichfork == XFS_DATA_FORK) {
    6272   144989847 :                 if (!xfs_verify_rtext(mp, irec->br_startblock,
    6273             :                                           irec->br_blockcount))
    6274           0 :                         return __this_address;
    6275             :         } else {
    6276  2315894175 :                 if (!xfs_verify_fsbext(mp, irec->br_startblock,
    6277             :                                            irec->br_blockcount))
    6278           0 :                         return __this_address;
    6279             :         }
    6280  2460930736 :         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          12 : xfs_bmap_intent_init_cache(void)
    6287             : {
    6288          12 :         xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
    6289             :                         sizeof(struct xfs_bmap_intent),
    6290             :                         0, 0, NULL);
    6291             : 
    6292          12 :         return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
    6293             : }
    6294             : 
    6295             : void
    6296          12 : xfs_bmap_intent_destroy_cache(void)
    6297             : {
    6298          12 :         kmem_cache_destroy(xfs_bmap_intent_cache);
    6299          12 :         xfs_bmap_intent_cache = NULL;
    6300          12 : }
    6301             : 
    6302             : /* Check that an inode's extent does not have invalid flags or bad ranges. */
    6303             : xfs_failaddr_t
    6304  2460832587 : xfs_bmap_validate_extent(
    6305             :         struct xfs_inode        *ip,
    6306             :         int                     whichfork,
    6307             :         struct xfs_bmbt_irec    *irec)
    6308             : {
    6309  2460832587 :         return xfs_bmap_validate_extent_raw(ip->i_mount,
    6310  2460832587 :                         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    11946806 : 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    11946806 :         xfs_filblks_t           unmap_len = endoff - startoff + 1;
    6332    11946806 :         int                     error = 0;
    6333             : 
    6334    11946806 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    6335             : 
    6336    47595452 :         while (unmap_len > 0) {
    6337    35653158 :                 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
    6338    35653158 :                 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
    6339             :                                 XFS_ITRUNC_MAX_EXTENTS);
    6340    35652492 :                 if (error)
    6341         523 :                         goto out;
    6342             : 
    6343             :                 /* free the just unmapped extents */
    6344    35651969 :                 error = xfs_defer_finish(tpp);
    6345    35653173 :                 if (error)
    6346        1284 :                         goto out;
    6347             :         }
    6348    11942294 : out:
    6349    11944101 :         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      687274 : xfs_bmap_query_range_helper(
    6360             :         struct xfs_btree_cur            *cur,
    6361             :         const union xfs_btree_rec       *rec,
    6362             :         void                            *priv)
    6363             : {
    6364      687274 :         struct xfs_bmap_query_range     *query = priv;
    6365      687274 :         struct xfs_bmbt_irec            irec;
    6366      687274 :         xfs_failaddr_t                  fa;
    6367             : 
    6368      687274 :         xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
    6369      687274 :         fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
    6370             :                         &irec);
    6371      687274 :         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      687274 :         return query->fn(cur, &irec, query->priv);
    6378             : }
    6379             : 
    6380             : /* Find all bmaps. */
    6381             : int
    6382       37458 : xfs_bmap_query_all(
    6383             :         struct xfs_btree_cur            *cur,
    6384             :         xfs_bmap_query_range_fn         fn,
    6385             :         void                            *priv)
    6386             : {
    6387       37458 :         struct xfs_bmap_query_range     query = {
    6388             :                 .priv                   = priv,
    6389             :                 .fn                     = fn,
    6390             :         };
    6391             : 
    6392       37458 :         return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
    6393             : }

Generated by: LCOV version 1.14