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-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 2657 3274 81.2 %
Date: 2023-07-31 20:08:27 Functions: 87 89 97.8 %

          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_rtbitmap.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             : #include "xfs_inode_util.h"
      42             : #include "xfs_rtalloc.h"
      43             : 
      44             : struct kmem_cache               *xfs_bmap_intent_cache;
      45             : 
      46             : /*
      47             :  * Miscellaneous helper functions
      48             :  */
      49             : 
      50             : /*
      51             :  * Compute and fill in the value of the maximum depth of a bmap btree
      52             :  * in this filesystem.  Done once, during mount.
      53             :  */
      54             : void
      55       48674 : xfs_bmap_compute_maxlevels(
      56             :         xfs_mount_t     *mp,            /* file system mount structure */
      57             :         int             whichfork)      /* data or attr fork */
      58             : {
      59       48674 :         uint64_t        maxblocks;      /* max blocks at this level */
      60       48674 :         xfs_extnum_t    maxleafents;    /* max leaf entries possible */
      61       48674 :         int             level;          /* btree level */
      62       48674 :         int             maxrootrecs;    /* max records in root block */
      63       48674 :         int             minleafrecs;    /* min records in leaf block */
      64       48674 :         int             minnoderecs;    /* min records in node block */
      65       48674 :         int             sz;             /* root block size */
      66             : 
      67             :         /*
      68             :          * The maximum number of extents in a fork, hence the maximum number of
      69             :          * leaf entries, is controlled by the size of the on-disk extent count.
      70             :          *
      71             :          * Note that we can no longer assume that if we are in ATTR1 that the
      72             :          * fork offset of all the inodes will be
      73             :          * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
      74             :          * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
      75             :          * but probably at various positions. Therefore, for both ATTR1 and
      76             :          * ATTR2 we have to assume the worst case scenario of a minimum size
      77             :          * available.
      78             :          */
      79       48674 :         maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
      80             :                                 whichfork);
      81       48674 :         if (whichfork == XFS_DATA_FORK)
      82             :                 sz = xfs_bmdr_space_calc(MINDBTPTRS);
      83             :         else
      84       24337 :                 sz = xfs_bmdr_space_calc(MINABTPTRS);
      85             : 
      86       48674 :         maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
      87       48674 :         minleafrecs = mp->m_bmap_dmnr[0];
      88       48674 :         minnoderecs = mp->m_bmap_dmnr[1];
      89       48674 :         maxblocks = howmany_64(maxleafents, minleafrecs);
      90      291606 :         for (level = 1; maxblocks > 1; level++) {
      91      242932 :                 if (maxblocks <= maxrootrecs)
      92             :                         maxblocks = 1;
      93             :                 else
      94      233392 :                         maxblocks = howmany_64(maxblocks, minnoderecs);
      95             :         }
      96       48674 :         mp->m_bm_maxlevels[whichfork] = level;
      97       48674 :         ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
      98       48674 : }
      99             : 
     100             : unsigned int
     101       24337 : xfs_bmap_compute_attr_offset(
     102             :         struct xfs_mount        *mp)
     103             : {
     104       24337 :         if (mp->m_sb.sb_inodesize == 256)
     105          80 :                 return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
     106             :         return xfs_bmdr_space_calc(6 * MINABTPTRS);
     107             : }
     108             : 
     109             : STATIC int                              /* error */
     110   182835927 : xfs_bmbt_lookup_eq(
     111             :         struct xfs_btree_cur    *cur,
     112             :         struct xfs_bmbt_irec    *irec,
     113             :         int                     *stat)  /* success/failure */
     114             : {
     115   182835927 :         cur->bc_rec.b = *irec;
     116   182835927 :         return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
     117             : }
     118             : 
     119             : STATIC int                              /* error */
     120           0 : xfs_bmbt_lookup_first(
     121             :         struct xfs_btree_cur    *cur,
     122             :         int                     *stat)  /* success/failure */
     123             : {
     124           0 :         cur->bc_rec.b.br_startoff = 0;
     125           0 :         cur->bc_rec.b.br_startblock = 0;
     126           0 :         cur->bc_rec.b.br_blockcount = 0;
     127           0 :         return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
     128             : }
     129             : 
     130             : /*
     131             :  * Check if the inode needs to be converted to btree format.
     132             :  */
     133   287917891 : static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
     134             : {
     135   287917891 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     136             : 
     137   573169288 :         return whichfork != XFS_COW_FORK &&
     138   287919878 :                 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
     139   145813288 :                 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
     140             : }
     141             : 
     142             : /*
     143             :  * Check if the inode should be converted to extent format.
     144             :  */
     145   288946114 : static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
     146             : {
     147   288946114 :         struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
     148             : 
     149   577221977 :         return whichfork != XFS_COW_FORK &&
     150   288944084 :                 ifp->if_format == XFS_DINODE_FMT_BTREE &&
     151   141951110 :                 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
     152             : }
     153             : 
     154             : /*
     155             :  * Update the record referred to by cur to the value given by irec
     156             :  * This either works (return 0) or gets an EFSCORRUPTED error.
     157             :  */
     158             : STATIC int
     159    48024918 : xfs_bmbt_update(
     160             :         struct xfs_btree_cur    *cur,
     161             :         struct xfs_bmbt_irec    *irec)
     162             : {
     163    48024918 :         union xfs_btree_rec     rec;
     164             : 
     165    48024918 :         xfs_bmbt_disk_set_all(&rec.bmbt, irec);
     166    48024939 :         return xfs_btree_update(cur, &rec);
     167             : }
     168             : 
     169             : /*
     170             :  * Compute the worst-case number of indirect blocks that will be used
     171             :  * for ip's delayed extent of length "len".
     172             :  */
     173             : STATIC xfs_filblks_t
     174    23737267 : xfs_bmap_worst_indlen(
     175             :         xfs_inode_t     *ip,            /* incore inode pointer */
     176             :         xfs_filblks_t   len)            /* delayed extent length */
     177             : {
     178    23737267 :         int             level;          /* btree level number */
     179    23737267 :         int             maxrecs;        /* maximum record count at this level */
     180    23737267 :         xfs_mount_t     *mp;            /* mount structure */
     181    23737267 :         xfs_filblks_t   rval;           /* return value */
     182             : 
     183    23737267 :         mp = ip->i_mount;
     184    23737267 :         maxrecs = mp->m_bmap_dmxr[0];
     185    23737267 :         for (level = 0, rval = 0;
     186    25242183 :              level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
     187     1504916 :              level++) {
     188    25242548 :                 len += maxrecs - 1;
     189    25242548 :                 do_div(len, maxrecs);
     190    25242548 :                 rval += len;
     191    25242548 :                 if (len == 1)
     192    23737632 :                         return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
     193    23737632 :                                 level - 1;
     194     1504916 :                 if (level == 0)
     195     1169321 :                         maxrecs = mp->m_bmap_dmxr[1];
     196             :         }
     197             :         return rval;
     198             : }
     199             : 
     200             : /*
     201             :  * Calculate the default attribute fork offset for newly created inodes.
     202             :  */
     203             : uint
     204    63964782 : xfs_default_attroffset(
     205             :         struct xfs_inode        *ip)
     206             : {
     207    63964782 :         if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
     208             :                 return roundup(sizeof(xfs_dev_t), 8);
     209    55272748 :         return M_IGEO(ip->i_mount)->attr_fork_offset;
     210             : }
     211             : 
     212             : /*
     213             :  * Helper routine to reset inode i_forkoff field when switching attribute fork
     214             :  * from local to extent format - we reset it where possible to make space
     215             :  * available for inline data fork extents.
     216             :  */
     217             : STATIC void
     218     8984671 : xfs_bmap_forkoff_reset(
     219             :         xfs_inode_t     *ip,
     220             :         int             whichfork)
     221             : {
     222     8984671 :         if (whichfork == XFS_ATTR_FORK &&
     223     8588036 :             ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
     224             :             ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
     225     8486272 :                 uint    dfl_forkoff = xfs_default_attroffset(ip) >> 3;
     226             : 
     227     8486272 :                 if (dfl_forkoff > ip->i_forkoff)
     228     8465934 :                         ip->i_forkoff = dfl_forkoff;
     229             :         }
     230     8984671 : }
     231             : 
     232             : #ifdef DEBUG
     233             : STATIC struct xfs_buf *
     234   497264533 : xfs_bmap_get_bp(
     235             :         struct xfs_btree_cur    *cur,
     236             :         xfs_fsblock_t           bno)
     237             : {
     238   497264533 :         struct xfs_log_item     *lip;
     239   497264533 :         int                     i;
     240             : 
     241   497264533 :         if (!cur)
     242             :                 return NULL;
     243             : 
     244  1317766793 :         for (i = 0; i < cur->bc_maxlevels; i++) {
     245  1317766600 :                 if (!cur->bc_levels[i].bp)
     246             :                         break;
     247   895575795 :                 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
     248    75073535 :                         return cur->bc_levels[i].bp;
     249             :         }
     250             : 
     251             :         /* Chase down all the log items to see if the bp is there */
     252  2404596370 :         list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
     253  1995038362 :                 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
     254             : 
     255  1995038362 :                 if (bip->bli_item.li_type == XFS_LI_BUF &&
     256  1200618406 :                     xfs_buf_daddr(bip->bli_buf) == bno)
     257    12632990 :                         return bip->bli_buf;
     258             :         }
     259             : 
     260             :         return NULL;
     261             : }
     262             : 
     263             : STATIC void
     264    76532114 : xfs_check_block(
     265             :         struct xfs_btree_block  *block,
     266             :         xfs_mount_t             *mp,
     267             :         int                     root,
     268             :         short                   sz)
     269             : {
     270    76532114 :         int                     i, j, dmxr;
     271    76532114 :         __be64                  *pp, *thispa;   /* pointer to block address */
     272    76532114 :         xfs_bmbt_key_t          *prevp, *keyp;
     273             : 
     274    76532114 :         ASSERT(be16_to_cpu(block->bb_level) > 0);
     275             : 
     276             :         prevp = NULL;
     277  1147586544 :         for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
     278   497261124 :                 dmxr = mp->m_bmap_dmxr[0];
     279   497261124 :                 keyp = xfs_bmbt_key_addr(mp, block, i);
     280             : 
     281   497261124 :                 if (prevp) {
     282   420730981 :                         ASSERT(be64_to_cpu(prevp->br_startoff) <
     283             :                                be64_to_cpu(keyp->br_startoff));
     284             :                 }
     285   497261124 :                 prevp = keyp;
     286             : 
     287             :                 /*
     288             :                  * Compare the block numbers to see if there are dups.
     289             :                  */
     290   497261124 :                 if (root)
     291    97467055 :                         pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz);
     292             :                 else
     293   399794069 :                         pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr);
     294             : 
     295  6155299230 :                 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
     296  5658038072 :                         if (root)
     297   142677175 :                                 thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz);
     298             :                         else
     299  5515360897 :                                 thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr);
     300  5658038105 :                         if (*thispa == *pp) {
     301           0 :                                 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
     302             :                                         __func__, j, i,
     303             :                                         (unsigned long long)be64_to_cpu(*thispa));
     304           0 :                                 xfs_err(mp, "%s: ptrs are equal in node\n",
     305             :                                         __func__);
     306           0 :                                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     307             :                         }
     308             :                 }
     309             :         }
     310    76532148 : }
     311             : 
     312             : /*
     313             :  * Check that the extents for the inode ip are in the right order in all
     314             :  * btree leaves. THis becomes prohibitively expensive for large extent count
     315             :  * files, so don't bother with inodes that have more than 10,000 extents in
     316             :  * them. The btree record ordering checks will still be done, so for such large
     317             :  * bmapbt constructs that is going to catch most corruptions.
     318             :  */
     319             : STATIC void
     320   189538718 : xfs_bmap_check_leaf_extents(
     321             :         struct xfs_btree_cur    *cur,   /* btree cursor or null */
     322             :         xfs_inode_t             *ip,            /* incore inode pointer */
     323             :         int                     whichfork)      /* data or attr fork */
     324             : {
     325   189538718 :         struct xfs_mount        *mp = ip->i_mount;
     326   189538718 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     327   189539646 :         struct xfs_btree_block  *block; /* current btree block */
     328   189539646 :         xfs_fsblock_t           bno;    /* block # of "block" */
     329   189539646 :         struct xfs_buf          *bp;    /* buffer for "block" */
     330   189539646 :         int                     error;  /* error return value */
     331   189539646 :         xfs_extnum_t            i=0, j; /* index into the extents list */
     332   189539646 :         int                     level;  /* btree level, for checking */
     333   189539646 :         __be64                  *pp;    /* pointer to block address */
     334   189539646 :         xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
     335   189539646 :         xfs_bmbt_rec_t          last = {0, 0}; /* last extent in prev block */
     336   189539646 :         xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
     337   189539646 :         int                     bp_release = 0;
     338             : 
     339   189539646 :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
     340             :                 return;
     341             : 
     342             :         /* skip large extent count inodes */
     343    96881149 :         if (ip->i_df.if_nextents > 10000)
     344             :                 return;
     345             : 
     346    60928218 :         bno = NULLFSBLOCK;
     347    60928218 :         block = ifp->if_broot;
     348             :         /*
     349             :          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
     350             :          */
     351    60928218 :         level = be16_to_cpu(block->bb_level);
     352    60928218 :         ASSERT(level > 0);
     353    60928218 :         xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
     354    60928315 :         pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes);
     355    60928203 :         bno = be64_to_cpu(*pp);
     356             : 
     357    60928203 :         ASSERT(bno != NULLFSBLOCK);
     358    60928203 :         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
     359    60928203 :         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
     360             : 
     361             :         /*
     362             :          * Go down the tree until leaf level is reached, following the first
     363             :          * pointer (leftmost) at each level.
     364             :          */
     365    76532126 :         while (level-- > 0) {
     366             :                 /* See if buf is in cur first */
     367    76532154 :                 bp_release = 0;
     368    76532154 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     369    76532280 :                 if (!bp) {
     370    22064768 :                         bp_release = 1;
     371    22064768 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     372             :                                                 XFS_BMAP_BTREE_REF,
     373             :                                                 &xfs_bmbt_buf_ops);
     374    22064777 :                         if (xfs_metadata_is_sick(error))
     375           0 :                                 xfs_btree_mark_sick(cur);
     376    22064777 :                         if (error)
     377           6 :                                 goto error_norelse;
     378             :                 }
     379    76532283 :                 block = XFS_BUF_TO_BLOCK(bp);
     380    76532283 :                 if (level == 0)
     381             :                         break;
     382             : 
     383             :                 /*
     384             :                  * Check this block for basic sanity (increasing keys and
     385             :                  * no duplicate blocks).
     386             :                  */
     387             : 
     388    15603931 :                 xfs_check_block(block, mp, 0, 0);
     389    15603928 :                 pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]);
     390    15603928 :                 bno = be64_to_cpu(*pp);
     391    15603928 :                 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
     392           0 :                         xfs_btree_mark_sick(cur);
     393           0 :                         error = -EFSCORRUPTED;
     394           0 :                         goto error0;
     395             :                 }
     396    15603923 :                 if (bp_release) {
     397           0 :                         bp_release = 0;
     398           0 :                         xfs_trans_brelse(NULL, bp);
     399             :                 }
     400             :         }
     401             : 
     402             :         /*
     403             :          * Here with bp and block set to the leftmost leaf node in the tree.
     404             :          */
     405             :         i = 0;
     406             : 
     407             :         /*
     408             :          * Loop over all leaf nodes checking that all extents are in the right order.
     409             :          */
     410   902383370 :         for (;;) {
     411   481655847 :                 xfs_fsblock_t   nextbno;
     412   481655847 :                 xfs_extnum_t    num_recs;
     413             : 
     414             : 
     415   481655847 :                 num_recs = xfs_btree_get_numrecs(block);
     416             : 
     417             :                 /*
     418             :                  * Read-ahead the next leaf block, if any.
     419             :                  */
     420             : 
     421   481655847 :                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
     422             : 
     423             :                 /*
     424             :                  * Check all the extents to make sure they are OK.
     425             :                  * If we had a previous block, the last entry should
     426             :                  * conform with the first entry in this one.
     427             :                  */
     428             : 
     429   481655847 :                 ep = xfs_bmbt_rec_addr(mp, block, 1);
     430   481655847 :                 if (i) {
     431   420729929 :                         ASSERT(xfs_bmbt_disk_get_startoff(&last) +
     432             :                                xfs_bmbt_disk_get_blockcount(&last) <=
     433             :                                xfs_bmbt_disk_get_startoff(ep));
     434             :                 }
     435 >10843*10^7 :                 for (j = 1; j < num_recs; j++) {
     436 >10795*10^7 :                         nextp = xfs_bmbt_rec_addr(mp, block, j + 1);
     437 >10795*10^7 :                         ASSERT(xfs_bmbt_disk_get_startoff(ep) +
     438             :                                xfs_bmbt_disk_get_blockcount(ep) <=
     439             :                                xfs_bmbt_disk_get_startoff(nextp));
     440 >10795*10^7 :                         ep = nextp;
     441             :                 }
     442             : 
     443   481659957 :                 last = *ep;
     444   481659957 :                 i += num_recs;
     445   481659957 :                 if (bp_release) {
     446   409558190 :                         bp_release = 0;
     447   409558190 :                         xfs_trans_brelse(NULL, bp);
     448             :                 }
     449   481660838 :                 bno = nextbno;
     450             :                 /*
     451             :                  * If we've reached the end, stop.
     452             :                  */
     453   481660838 :                 if (bno == NULLFSBLOCK)
     454             :                         break;
     455             : 
     456   420732501 :                 bp_release = 0;
     457   420732501 :                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
     458   420726146 :                 if (!bp) {
     459   387492807 :                         bp_release = 1;
     460   387492807 :                         error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
     461             :                                                 XFS_BMAP_BTREE_REF,
     462             :                                                 &xfs_bmbt_buf_ops);
     463   387494229 :                         if (xfs_metadata_is_sick(error))
     464           0 :                                 xfs_btree_mark_sick(cur);
     465   387494229 :                         if (error)
     466          45 :                                 goto error_norelse;
     467             :                 }
     468   420727523 :                 block = XFS_BUF_TO_BLOCK(bp);
     469             :         }
     470             : 
     471             :         return;
     472             : 
     473             : error0:
     474           0 :         xfs_warn(mp, "%s: at error0", __func__);
     475           0 :         if (bp_release)
     476           0 :                 xfs_trans_brelse(NULL, bp);
     477           0 : error_norelse:
     478          51 :         xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
     479             :                 __func__, i);
     480          51 :         xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
     481          51 :         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
     482          51 :         return;
     483             : }
     484             : 
     485             : /*
     486             :  * Validate that the bmbt_irecs being returned from bmapi are valid
     487             :  * given the caller's original parameters.  Specifically check the
     488             :  * ranges of the returned irecs to ensure that they only extend beyond
     489             :  * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
     490             :  */
     491             : STATIC void
     492   107882961 : xfs_bmap_validate_ret(
     493             :         xfs_fileoff_t           bno,
     494             :         xfs_filblks_t           len,
     495             :         uint32_t                flags,
     496             :         xfs_bmbt_irec_t         *mval,
     497             :         int                     nmap,
     498             :         int                     ret_nmap)
     499             : {
     500   107882961 :         int                     i;              /* index to map values */
     501             : 
     502   107882961 :         ASSERT(ret_nmap <= nmap);
     503             : 
     504   215768260 :         for (i = 0; i < ret_nmap; i++) {
     505   107884047 :                 ASSERT(mval[i].br_blockcount > 0);
     506   107884047 :                 if (!(flags & XFS_BMAPI_ENTIRE)) {
     507   107884047 :                         ASSERT(mval[i].br_startoff >= bno);
     508   107884047 :                         ASSERT(mval[i].br_blockcount <= len);
     509   107884047 :                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
     510             :                                bno + len);
     511             :                 } else {
     512           0 :                         ASSERT(mval[i].br_startoff < bno + len);
     513           0 :                         ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
     514             :                                bno);
     515             :                 }
     516   107884047 :                 ASSERT(i == 0 ||
     517             :                        mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
     518             :                        mval[i].br_startoff);
     519   107884047 :                 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
     520             :                        mval[i].br_startblock != HOLESTARTBLOCK);
     521   107884047 :                 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
     522             :                        mval[i].br_state == XFS_EXT_UNWRITTEN);
     523             :         }
     524   107884213 : }
     525             : 
     526             : #else
     527             : #define xfs_bmap_check_leaf_extents(cur, ip, whichfork)         do { } while (0)
     528             : #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)    do { } while (0)
     529             : #endif /* DEBUG */
     530             : 
     531             : /*
     532             :  * Inode fork format manipulation functions
     533             :  */
     534             : 
     535             : /*
     536             :  * Convert the inode format to extent format if it currently is in btree format,
     537             :  * but the extent list is small enough that it fits into the extent format.
     538             :  *
     539             :  * Since the extents are already in-core, all we have to do is give up the space
     540             :  * for the btree root and pitch the leaf block.
     541             :  */
     542             : STATIC int                              /* error */
     543   288939881 : xfs_bmap_btree_to_extents(
     544             :         struct xfs_trans        *tp,    /* transaction pointer */
     545             :         struct xfs_inode        *ip,    /* incore inode pointer */
     546             :         struct xfs_btree_cur    *cur,   /* btree cursor */
     547             :         int                     *logflagsp, /* inode logging flags */
     548             :         int                     whichfork)  /* data or attr fork */
     549             : {
     550   288939881 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     551   288942782 :         struct xfs_mount        *mp = ip->i_mount;
     552   288942782 :         struct xfs_btree_block  *rblock = ifp->if_broot;
     553   288942782 :         struct xfs_btree_block  *cblock;/* child btree block */
     554   288942782 :         xfs_fsblock_t           cbno;   /* child block number */
     555   288942782 :         struct xfs_buf          *cbp;   /* child block's buffer */
     556   288942782 :         int                     error;  /* error return value */
     557   288942782 :         __be64                  *pp;    /* ptr to block address */
     558   288942782 :         struct xfs_owner_info   oinfo;
     559             : 
     560             :         /* check if we actually need the extent format first: */
     561   288942782 :         if (!xfs_bmap_wants_extents(ip, whichfork))
     562             :                 return 0;
     563             : 
     564      763087 :         ASSERT(cur);
     565      763087 :         ASSERT(whichfork != XFS_COW_FORK);
     566      763087 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
     567      763087 :         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
     568      763087 :         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
     569      763087 :         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1);
     570             : 
     571      763087 :         pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes);
     572      763087 :         cbno = be64_to_cpu(*pp);
     573             : #ifdef DEBUG
     574      763087 :         if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1))) {
     575           0 :                 xfs_btree_mark_sick(cur);
     576           0 :                 return -EFSCORRUPTED;
     577             :         }
     578             : #endif
     579      763087 :         error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
     580             :                                 &xfs_bmbt_buf_ops);
     581      763087 :         if (xfs_metadata_is_sick(error))
     582           0 :                 xfs_btree_mark_sick(cur);
     583      763087 :         if (error)
     584             :                 return error;
     585      763087 :         cblock = XFS_BUF_TO_BLOCK(cbp);
     586      763087 :         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
     587             :                 return error;
     588             : 
     589      763087 :         xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
     590      763087 :         error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
     591             :                         XFS_AG_RESV_NONE, 0);
     592      763087 :         if (error)
     593             :                 return error;
     594             : 
     595      763087 :         ip->i_nblocks--;
     596      763087 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     597      763087 :         xfs_trans_binval(tp, cbp);
     598      763087 :         if (cur->bc_levels[0].bp == cbp)
     599      763087 :                 cur->bc_levels[0].bp = NULL;
     600      763087 :         xfs_iroot_free(ip, whichfork);
     601      763087 :         ASSERT(ifp->if_broot == NULL);
     602      763087 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     603      763087 :         *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
     604      763087 :         return 0;
     605             : }
     606             : 
     607             : /*
     608             :  * Convert an extents-format file into a btree-format file.
     609             :  * The new file will have a root block (in the inode) and a single child block.
     610             :  */
     611             : STATIC int                                      /* error */
     612     1566492 : xfs_bmap_extents_to_btree(
     613             :         struct xfs_trans        *tp,            /* transaction pointer */
     614             :         struct xfs_inode        *ip,            /* incore inode pointer */
     615             :         struct xfs_btree_cur    **curp,         /* cursor returned to caller */
     616             :         int                     wasdel,         /* converting a delayed alloc */
     617             :         int                     *logflagsp,     /* inode logging flags */
     618             :         int                     whichfork)      /* data or attr fork */
     619             : {
     620     1566492 :         struct xfs_btree_block  *ablock;        /* allocated (child) bt block */
     621     1566492 :         struct xfs_buf          *abp;           /* buffer for ablock */
     622     1566492 :         struct xfs_alloc_arg    args;           /* allocation arguments */
     623     1566492 :         struct xfs_bmbt_rec     *arp;           /* child record pointer */
     624     1566492 :         struct xfs_btree_block  *block;         /* btree root block */
     625     1566492 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     626     1566492 :         int                     error;          /* error return value */
     627     1566492 :         struct xfs_ifork        *ifp;           /* inode fork pointer */
     628     1566492 :         struct xfs_bmbt_key     *kp;            /* root block key pointer */
     629     1566492 :         struct xfs_mount        *mp;            /* mount structure */
     630     1566492 :         xfs_bmbt_ptr_t          *pp;            /* root block address pointer */
     631     1566492 :         struct xfs_iext_cursor  icur;
     632     1566492 :         struct xfs_bmbt_irec    rec;
     633     1566492 :         xfs_extnum_t            cnt = 0;
     634             : 
     635     1566492 :         mp = ip->i_mount;
     636     1566492 :         ASSERT(whichfork != XFS_COW_FORK);
     637     1566492 :         ifp = xfs_ifork_ptr(ip, whichfork);
     638     1566492 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
     639             : 
     640             :         /*
     641             :          * Need a cursor.  Can't allocate until bb_level is filled in.
     642             :          */
     643     1566492 :         xfs_bmbt_iroot_alloc(ip, whichfork);
     644     1566493 :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
     645     1566493 :         cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
     646             :         /*
     647             :          * Convert to a btree with two levels, one record in root.
     648             :          */
     649     1566493 :         ifp->if_format = XFS_DINODE_FMT_BTREE;
     650     1566493 :         memset(&args, 0, sizeof(args));
     651     1566493 :         args.tp = tp;
     652     1566493 :         args.mp = mp;
     653     1566493 :         xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
     654             : 
     655     1566493 :         args.minlen = args.maxlen = args.prod = 1;
     656     1566493 :         args.wasdel = wasdel;
     657     1566493 :         *logflagsp = 0;
     658     1566493 :         error = xfs_alloc_vextent_start_ag(&args,
     659     1566493 :                                 XFS_INO_TO_FSB(mp, ip->i_ino));
     660     1566493 :         if (error)
     661          49 :                 goto out_root_realloc;
     662             : 
     663             :         /*
     664             :          * Allocation can't fail, the space was reserved.
     665             :          */
     666     1566444 :         if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
     667           0 :                 error = -ENOSPC;
     668           0 :                 goto out_root_realloc;
     669             :         }
     670             : 
     671     1566444 :         cur->bc_ino.allocated++;
     672     1566444 :         ip->i_nblocks++;
     673     1566444 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     674     1566444 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
     675     1566444 :                         XFS_FSB_TO_DADDR(mp, args.fsbno),
     676             :                         mp->m_bsize, 0, &abp);
     677     1566444 :         if (error)
     678           0 :                 goto out_unreserve_dquot;
     679             : 
     680             :         /*
     681             :          * Fill in the child block.
     682             :          */
     683     1566444 :         ablock = XFS_BUF_TO_BLOCK(abp);
     684     1566444 :         xfs_btree_init_buf(mp, abp, &xfs_bmbt_ops, 0, 0, ip->i_ino);
     685             : 
     686    22012330 :         for_each_xfs_iext(ifp, &icur, &rec) {
     687    20445887 :                 if (isnullstartblock(rec.br_startblock))
     688      217801 :                         continue;
     689    20228086 :                 arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt);
     690    20228086 :                 xfs_bmbt_disk_set_all(arp, &rec);
     691    20228085 :                 cnt++;
     692             :         }
     693     1566444 :         ASSERT(cnt == ifp->if_nextents);
     694     1566444 :         xfs_btree_set_numrecs(ablock, cnt);
     695             : 
     696             :         /*
     697             :          * Fill in the root key and pointer.
     698             :          */
     699     1566444 :         block = ifp->if_broot;
     700     1566444 :         kp = xfs_bmbt_key_addr(mp, block, 1);
     701     1566444 :         arp = xfs_bmbt_rec_addr(mp, ablock, 1);
     702     1566444 :         kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
     703     1566444 :         pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
     704     1566444 :                                                 be16_to_cpu(block->bb_level)));
     705     1566444 :         *pp = cpu_to_be64(args.fsbno);
     706             : 
     707             :         /*
     708             :          * Do all this logging at the end so that
     709             :          * the root is at the right level.
     710             :          */
     711     1566444 :         xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
     712     1566444 :         xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
     713     1566443 :         ASSERT(*curp == NULL);
     714     1566443 :         *curp = cur;
     715     1566443 :         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
     716     1566443 :         return 0;
     717             : 
     718             : out_unreserve_dquot:
     719           0 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
     720          49 : out_root_realloc:
     721          49 :         xfs_iroot_free(ip, whichfork);
     722          49 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     723          49 :         ASSERT(ifp->if_broot == NULL);
     724          49 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     725             : 
     726          49 :         return error;
     727             : }
     728             : 
     729             : /*
     730             :  * Convert a local file to an extents file.
     731             :  * This code is out of bounds for data forks of regular files,
     732             :  * since the file data needs to get logged so things will stay consistent.
     733             :  * (The bmap-level manipulations are ok, though).
     734             :  */
     735             : void
     736     8984527 : xfs_bmap_local_to_extents_empty(
     737             :         struct xfs_trans        *tp,
     738             :         struct xfs_inode        *ip,
     739             :         int                     whichfork)
     740             : {
     741     8984527 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     742             : 
     743     8984742 :         ASSERT(whichfork != XFS_COW_FORK);
     744     8984742 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     745     8984742 :         ASSERT(ifp->if_bytes == 0);
     746     8984742 :         ASSERT(ifp->if_nextents == 0);
     747             : 
     748     8984742 :         xfs_bmap_forkoff_reset(ip, whichfork);
     749     8984602 :         ifp->if_u1.if_root = NULL;
     750     8984602 :         ifp->if_height = 0;
     751     8984602 :         ifp->if_format = XFS_DINODE_FMT_EXTENTS;
     752     8984602 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     753     8984745 : }
     754             : 
     755             : 
     756             : int                                     /* error */
     757        1043 : xfs_bmap_local_to_extents(
     758             :         xfs_trans_t     *tp,            /* transaction pointer */
     759             :         xfs_inode_t     *ip,            /* incore inode pointer */
     760             :         xfs_extlen_t    total,          /* total blocks needed by transaction */
     761             :         int             *logflagsp,     /* inode logging flags */
     762             :         int             whichfork,
     763             :         void            (*init_fn)(struct xfs_trans *tp,
     764             :                                    struct xfs_buf *bp,
     765             :                                    struct xfs_inode *ip,
     766             :                                    struct xfs_ifork *ifp, void *priv),
     767             :         void            *priv)
     768             : {
     769        1043 :         int             error = 0;
     770        1043 :         int             flags;          /* logging flags returned */
     771        1043 :         struct xfs_ifork *ifp;          /* inode fork pointer */
     772        1043 :         xfs_alloc_arg_t args;           /* allocation arguments */
     773        1043 :         struct xfs_buf  *bp;            /* buffer for extent block */
     774        1043 :         struct xfs_bmbt_irec rec;
     775        1043 :         struct xfs_iext_cursor icur;
     776             : 
     777             :         /*
     778             :          * We don't want to deal with the case of keeping inode data inline yet.
     779             :          * So sending the data fork of a regular inode is invalid.
     780             :          */
     781        1043 :         ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
     782        1043 :         ifp = xfs_ifork_ptr(ip, whichfork);
     783        1043 :         ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
     784             : 
     785        1043 :         if (!ifp->if_bytes) {
     786           0 :                 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     787           0 :                 flags = XFS_ILOG_CORE;
     788           0 :                 goto done;
     789             :         }
     790             : 
     791        1043 :         flags = 0;
     792        1043 :         error = 0;
     793        1043 :         memset(&args, 0, sizeof(args));
     794        1043 :         args.tp = tp;
     795        1043 :         args.mp = ip->i_mount;
     796        1043 :         args.total = total;
     797        1043 :         args.minlen = args.maxlen = args.prod = 1;
     798        1043 :         xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
     799             : 
     800             :         /*
     801             :          * Allocate a block.  We know we need only one, since the
     802             :          * file currently fits in an inode.
     803             :          */
     804        1043 :         args.total = total;
     805        1043 :         args.minlen = args.maxlen = args.prod = 1;
     806        1043 :         error = xfs_alloc_vextent_start_ag(&args,
     807        1043 :                         XFS_INO_TO_FSB(args.mp, ip->i_ino));
     808        1043 :         if (error)
     809           0 :                 goto done;
     810             : 
     811             :         /* Can't fail, the space was reserved. */
     812        1043 :         ASSERT(args.fsbno != NULLFSBLOCK);
     813        1043 :         ASSERT(args.len == 1);
     814        1043 :         error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
     815        1043 :                         XFS_FSB_TO_DADDR(args.mp, args.fsbno),
     816             :                         args.mp->m_bsize, 0, &bp);
     817        1043 :         if (error)
     818           0 :                 goto done;
     819             : 
     820             :         /*
     821             :          * Initialize the block, copy the data and log the remote buffer.
     822             :          *
     823             :          * The callout is responsible for logging because the remote format
     824             :          * might differ from the local format and thus we don't know how much to
     825             :          * log here. Note that init_fn must also set the buffer log item type
     826             :          * correctly.
     827             :          */
     828        1043 :         init_fn(tp, bp, ip, ifp, priv);
     829             : 
     830             :         /* account for the change in fork size */
     831        1043 :         xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
     832        1043 :         xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
     833        1043 :         flags |= XFS_ILOG_CORE;
     834             : 
     835        1043 :         ifp->if_u1.if_root = NULL;
     836        1043 :         ifp->if_height = 0;
     837             : 
     838        1043 :         rec.br_startoff = 0;
     839        1043 :         rec.br_startblock = args.fsbno;
     840        1043 :         rec.br_blockcount = 1;
     841        1043 :         rec.br_state = XFS_EXT_NORM;
     842        1043 :         xfs_iext_first(ifp, &icur);
     843        1043 :         xfs_iext_insert(ip, &icur, &rec, 0);
     844             : 
     845        1043 :         ifp->if_nextents = 1;
     846        1043 :         ip->i_nblocks = 1;
     847        1043 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
     848        1043 :         flags |= xfs_ilog_fext(whichfork);
     849             : 
     850        1043 : done:
     851        1043 :         *logflagsp = flags;
     852        1043 :         return error;
     853             : }
     854             : 
     855             : /*
     856             :  * Called from xfs_bmap_add_attrfork to handle btree format files.
     857             :  */
     858             : STATIC int                                      /* error */
     859          18 : xfs_bmap_add_attrfork_btree(
     860             :         xfs_trans_t             *tp,            /* transaction pointer */
     861             :         xfs_inode_t             *ip,            /* incore inode pointer */
     862             :         int                     *flags)         /* inode logging flags */
     863             : {
     864          18 :         struct xfs_btree_block  *block = ip->i_df.if_broot;
     865          18 :         struct xfs_btree_cur    *cur;           /* btree cursor */
     866          18 :         int                     error;          /* error return value */
     867          18 :         xfs_mount_t             *mp;            /* file system mount struct */
     868          18 :         int                     stat;           /* newroot status */
     869             : 
     870          18 :         mp = ip->i_mount;
     871             : 
     872          18 :         if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip))
     873          18 :                 *flags |= XFS_ILOG_DBROOT;
     874             :         else {
     875           0 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
     876           0 :                 error = xfs_bmbt_lookup_first(cur, &stat);
     877           0 :                 if (error)
     878           0 :                         goto error0;
     879             :                 /* must be at least one entry */
     880           0 :                 if (XFS_IS_CORRUPT(mp, stat != 1)) {
     881           0 :                         xfs_btree_mark_sick(cur);
     882           0 :                         error = -EFSCORRUPTED;
     883           0 :                         goto error0;
     884             :                 }
     885           0 :                 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
     886           0 :                         goto error0;
     887           0 :                 if (stat == 0) {
     888           0 :                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     889           0 :                         return -ENOSPC;
     890             :                 }
     891           0 :                 cur->bc_ino.allocated = 0;
     892           0 :                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     893             :         }
     894             :         return 0;
     895           0 : error0:
     896           0 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     897           0 :         return error;
     898             : }
     899             : 
     900             : /*
     901             :  * Called from xfs_bmap_add_attrfork to handle extents format files.
     902             :  */
     903             : STATIC int                                      /* error */
     904      318497 : xfs_bmap_add_attrfork_extents(
     905             :         struct xfs_trans        *tp,            /* transaction pointer */
     906             :         struct xfs_inode        *ip,            /* incore inode pointer */
     907             :         int                     *flags)         /* inode logging flags */
     908             : {
     909      318497 :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
     910      318497 :         int                     error;          /* error return value */
     911             : 
     912      318497 :         if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
     913      318497 :             xfs_inode_data_fork_size(ip))
     914             :                 return 0;
     915          21 :         cur = NULL;
     916          21 :         error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
     917             :                                           XFS_DATA_FORK);
     918          21 :         if (cur) {
     919          21 :                 cur->bc_ino.allocated = 0;
     920          21 :                 xfs_btree_del_cursor(cur, error);
     921             :         }
     922             :         return error;
     923             : }
     924             : 
     925             : /*
     926             :  * Called from xfs_bmap_add_attrfork to handle local format files. Each
     927             :  * different data fork content type needs a different callout to do the
     928             :  * conversion. Some are basic and only require special block initialisation
     929             :  * callouts for the data formating, others (directories) are so specialised they
     930             :  * handle everything themselves.
     931             :  *
     932             :  * XXX (dgc): investigate whether directory conversion can use the generic
     933             :  * formatting callout. It should be possible - it's just a very complex
     934             :  * formatter.
     935             :  */
     936             : STATIC int                                      /* error */
     937         666 : xfs_bmap_add_attrfork_local(
     938             :         struct xfs_trans        *tp,            /* transaction pointer */
     939             :         struct xfs_inode        *ip,            /* incore inode pointer */
     940             :         int                     *flags)         /* inode logging flags */
     941             : {
     942         666 :         struct xfs_da_args      dargs;          /* args for dir/attr code */
     943             : 
     944         666 :         if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
     945             :                 return 0;
     946             : 
     947           1 :         if (S_ISDIR(VFS_I(ip)->i_mode)) {
     948           1 :                 memset(&dargs, 0, sizeof(dargs));
     949           1 :                 dargs.geo = ip->i_mount->m_dir_geo;
     950           1 :                 dargs.dp = ip;
     951           1 :                 dargs.total = dargs.geo->fsbcount;
     952           1 :                 dargs.whichfork = XFS_DATA_FORK;
     953           1 :                 dargs.trans = tp;
     954           1 :                 dargs.owner = ip->i_ino;
     955           1 :                 return xfs_dir2_sf_to_block(&dargs);
     956             :         }
     957             : 
     958           0 :         if (S_ISLNK(VFS_I(ip)->i_mode))
     959           0 :                 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
     960             :                                 XFS_DATA_FORK, xfs_symlink_local_to_remote,
     961             :                                 NULL);
     962             : 
     963             :         /* should only be called for types that support local format data */
     964           0 :         ASSERT(0);
     965           0 :         xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
     966           0 :         return -EFSCORRUPTED;
     967             : }
     968             : 
     969             : /*
     970             :  * Set an inode attr fork offset based on the format of the data fork.
     971             :  */
     972             : static int
     973      319192 : xfs_bmap_set_attrforkoff(
     974             :         struct xfs_inode        *ip,
     975             :         int                     size,
     976             :         int                     *version)
     977             : {
     978      319192 :         int                     default_size = xfs_default_attroffset(ip) >> 3;
     979             : 
     980      319192 :         switch (ip->i_df.if_format) {
     981           0 :         case XFS_DINODE_FMT_DEV:
     982           0 :                 ip->i_forkoff = default_size;
     983           0 :                 break;
     984      319192 :         case XFS_DINODE_FMT_LOCAL:
     985             :         case XFS_DINODE_FMT_EXTENTS:
     986             :         case XFS_DINODE_FMT_BTREE:
     987      319192 :                 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
     988      319178 :                 if (!ip->i_forkoff)
     989           1 :                         ip->i_forkoff = default_size;
     990      319177 :                 else if (xfs_has_attr2(ip->i_mount) && version)
     991      319177 :                         *version = 2;
     992             :                 break;
     993           0 :         default:
     994           0 :                 ASSERT(0);
     995           0 :                 return -EINVAL;
     996             :         }
     997             : 
     998             :         return 0;
     999             : }
    1000             : 
    1001             : /*
    1002             :  * Convert inode from non-attributed to attributed.  Caller must hold the
    1003             :  * ILOCK_EXCL and the file cannot have an attr fork.
    1004             :  */
    1005             : int                                             /* error code */
    1006      595145 : xfs_bmap_add_attrfork(
    1007             :         struct xfs_trans        *tp,
    1008             :         struct xfs_inode        *ip,            /* incore inode pointer */
    1009             :         int                     size,           /* space new attribute needs */
    1010             :         int                     rsvd)           /* xact may use reserved blks */
    1011      595145 : {
    1012      595145 :         struct xfs_mount        *mp = tp->t_mountp;
    1013      595145 :         int                     version = 1;    /* superblock attr version */
    1014      595145 :         int                     logflags;       /* logging flags */
    1015      595145 :         int                     error;          /* error return value */
    1016      595145 : 
    1017             :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1018      595145 :         if (xfs_is_metadir_inode(ip))
    1019      595145 :                 ASSERT(XFS_IS_DQDETACHED(ip->i_mount, ip));
    1020        1377 :         else
    1021             :                 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
    1022      593768 :         ASSERT(!xfs_inode_has_attr_fork(ip));
    1023             : 
    1024      595145 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    1025             :         error = xfs_bmap_set_attrforkoff(ip, size, &version);
    1026      595145 :         if (error)
    1027             :                 return error;
    1028      595155 : 
    1029             :         xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
    1030      595155 :         logflags = 0;
    1031      275964 :         switch (ip->i_df.if_format) {
    1032             :         case XFS_DINODE_FMT_LOCAL:
    1033      319191 :                 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
    1034      319192 :                 break;
    1035      319176 :         case XFS_DINODE_FMT_EXTENTS:
    1036           0 :                 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
    1037             :                 break;
    1038      319176 :         case XFS_DINODE_FMT_BTREE:
    1039      319161 :                 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
    1040      319161 :                 break;
    1041         666 :         default:
    1042         666 :                 error = 0;
    1043         666 :                 break;
    1044      318477 :         }
    1045      318477 :         if (logflags)
    1046      318477 :                 xfs_trans_log_inode(tp, ip, logflags);
    1047          18 :         if (error)
    1048          18 :                 return error;
    1049          18 :         if (!xfs_has_attr(mp) ||
    1050             :            (!xfs_has_attr2(mp) && version == 2)) {
    1051             :                 bool log_sb = false;
    1052             : 
    1053             :                 spin_lock(&mp->m_sb_lock);
    1054      319169 :                 if (!xfs_has_attr(mp)) {
    1055          39 :                         xfs_add_attr(mp);
    1056      319163 :                         log_sb = true;
    1057           0 :                 }
    1058      319163 :                 if (!xfs_has_attr2(mp) && version == 2) {
    1059           0 :                         xfs_add_attr2(mp);
    1060           0 :                         log_sb = true;
    1061             :                 }
    1062           0 :                 spin_unlock(&mp->m_sb_lock);
    1063           0 :                 if (log_sb)
    1064           0 :                         xfs_log_sb(tp);
    1065           0 :         }
    1066             : 
    1067           0 :         return 0;
    1068           0 : }
    1069           0 : 
    1070             : /*
    1071           0 :  * Internal and external extent tree search functions.
    1072           0 :  */
    1073           0 : 
    1074             : struct xfs_iread_state {
    1075             :         struct xfs_iext_cursor  icur;
    1076      319163 :         xfs_extnum_t            loaded;
    1077      319193 : };
    1078      319193 : 
    1079             : int
    1080      275964 : xfs_bmap_complain_bad_rec(
    1081      275964 :         struct xfs_inode                *ip,
    1082      275961 :         int                             whichfork,
    1083      275961 :         xfs_failaddr_t                  fa,
    1084             :         const struct xfs_bmbt_irec      *irec)
    1085             : {
    1086             :         struct xfs_mount                *mp = ip->i_mount;
    1087             :         const char                      *forkname;
    1088             : 
    1089             :         switch (whichfork) {
    1090             :         case XFS_DATA_FORK:     forkname = "data"; break;
    1091             :         case XFS_ATTR_FORK:     forkname = "attr"; break;
    1092             :         case XFS_COW_FORK:      forkname = "CoW"; break;
    1093             :         default:                forkname = "???"; break;
    1094             :         }
    1095             : 
    1096           0 :         xfs_warn(mp,
    1097             :  "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
    1098             :                                 ip->i_ino, forkname, fa);
    1099             :         xfs_warn(mp,
    1100             :                 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
    1101             :                 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
    1102           0 :                 irec->br_state);
    1103           0 : 
    1104             :         return -EFSCORRUPTED;
    1105           0 : }
    1106             : 
    1107             : /* Stuff every bmbt record from this block into the incore extent map. */
    1108             : static int
    1109             : xfs_iread_bmbt_block(
    1110             :         struct xfs_btree_cur    *cur,
    1111             :         int                     level,
    1112           0 :         void                    *priv)
    1113             : {
    1114             :         struct xfs_iread_state  *ir = priv;
    1115           0 :         struct xfs_mount        *mp = cur->bc_mp;
    1116             :         struct xfs_inode        *ip = cur->bc_ino.ip;
    1117             :         struct xfs_btree_block  *block;
    1118             :         struct xfs_buf          *bp;
    1119             :         struct xfs_bmbt_rec     *frp;
    1120           0 :         xfs_extnum_t            num_recs;
    1121             :         xfs_extnum_t            j;
    1122             :         int                     whichfork = cur->bc_ino.whichfork;
    1123             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1124             : 
    1125     6188314 :         block = xfs_btree_get_block(cur, level, &bp);
    1126             : 
    1127             :         /* Abort if we find more records than nextents. */
    1128             :         num_recs = xfs_btree_get_numrecs(block);
    1129             :         if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
    1130     6188314 :                 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
    1131     6188314 :                                 (unsigned long long)ip->i_ino);
    1132     6188314 :                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
    1133     6188314 :                                 sizeof(*block), __this_address);
    1134     6188314 :                 xfs_bmap_mark_sick(ip, whichfork);
    1135     6188314 :                 return -EFSCORRUPTED;
    1136     6188314 :         }
    1137     6188314 : 
    1138     6188314 :         /* Copy records into the incore cache. */
    1139     6188314 :         frp = xfs_bmbt_rec_addr(mp, block, 1);
    1140             :         for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
    1141     6188324 :                 struct xfs_bmbt_irec    new;
    1142             :                 xfs_failaddr_t          fa;
    1143             : 
    1144     6188324 :                 xfs_bmbt_disk_get_all(frp, &new);
    1145     6188324 :                 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
    1146           0 :                 if (fa) {
    1147             :                         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
    1148           0 :                                         "xfs_iread_extents(2)", frp,
    1149           0 :                                         sizeof(*frp), fa);
    1150           0 :                         xfs_bmap_mark_sick(ip, whichfork);
    1151           0 :                         return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
    1152             :                                         &new);
    1153             :                 }
    1154             :                 xfs_iext_insert(ip, &ir->icur, &new,
    1155     6188324 :                                 xfs_bmap_fork_to_state(whichfork));
    1156  1011696990 :                 trace_xfs_read_extent(ip, &ir->icur,
    1157  1005508664 :                                 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
    1158  1005508664 :                 xfs_iext_next(ifp, &ir->icur);
    1159             :         }
    1160  1005508664 : 
    1161  1005499365 :         return 0;
    1162  1005508007 : }
    1163           0 : 
    1164             : /*
    1165             :  * Read in extents from a btree-format inode.
    1166           0 :  */
    1167           0 : int
    1168             : xfs_iread_extents(
    1169             :         struct xfs_trans        *tp,
    1170  2011010968 :         struct xfs_inode        *ip,
    1171             :         int                     whichfork)
    1172  2011009016 : {
    1173  1005507031 :         struct xfs_iread_state  ir;
    1174  1005507519 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1175             :         struct xfs_mount        *mp = ip->i_mount;
    1176             :         struct xfs_btree_cur    *cur;
    1177             :         int                     error;
    1178             : 
    1179             :         if (!xfs_need_iread_extents(ifp))
    1180             :                 return 0;
    1181             : 
    1182             :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1183             : 
    1184  6722844179 :         ir.loaded = 0;
    1185             :         xfs_iext_first(ifp, &ir.icur);
    1186             :         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    1187             :         error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
    1188             :                         XFS_BTREE_VISIT_RECORDS, &ir);
    1189  6722844179 :         xfs_btree_del_cursor(cur, error);
    1190  6722844179 :         if (error)
    1191  6723831713 :                 goto out;
    1192  6723831713 : 
    1193  6723831713 :         if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
    1194             :                 xfs_bmap_mark_sick(ip, whichfork);
    1195  6723831713 :                 error = -EFSCORRUPTED;
    1196             :                 goto out;
    1197             :         }
    1198     1518794 :         ASSERT(ir.loaded == xfs_iext_count(ifp));
    1199             :         /*
    1200     1518794 :          * Use release semantics so that we can use acquire semantics in
    1201     1518794 :          * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
    1202     1518791 :          * after that load.
    1203     1518788 :          */
    1204             :         smp_store_release(&ifp->if_needextents, 0);
    1205     1518790 :         return 0;
    1206     1518793 : out:
    1207         186 :         if (xfs_metadata_is_sick(error))
    1208             :                 xfs_bmap_mark_sick(ip, whichfork);
    1209     1518607 :         xfs_iext_destroy(ifp);
    1210           0 :         return error;
    1211           0 : }
    1212           0 : 
    1213             : /*
    1214     1518607 :  * Returns the relative block number of the first unused block(s) in the given
    1215             :  * fork with at least "len" logically contiguous blocks free.  This is the
    1216             :  * lowest-address hole if the fork has holes, else the first block past the end
    1217             :  * of fork.  Return 0 if the fork is currently local (in-inode).
    1218             :  */
    1219             : int                                             /* error */
    1220     1518607 : xfs_bmap_first_unused(
    1221     1518607 :         struct xfs_trans        *tp,            /* transaction pointer */
    1222         186 :         struct xfs_inode        *ip,            /* incore inode */
    1223         186 :         xfs_extlen_t            len,            /* size of hole to find */
    1224           2 :         xfs_fileoff_t           *first_unused,  /* unused block */
    1225         186 :         int                     whichfork)      /* data or attr fork */
    1226         186 : {
    1227             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1228             :         struct xfs_bmbt_irec    got;
    1229             :         struct xfs_iext_cursor  icur;
    1230             :         xfs_fileoff_t           lastaddr = 0;
    1231             :         xfs_fileoff_t           lowest, max;
    1232             :         int                     error;
    1233             : 
    1234             :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
    1235             :                 *first_unused = 0;
    1236     9265495 :                 return 0;
    1237             :         }
    1238             : 
    1239             :         ASSERT(xfs_ifork_has_extents(ifp));
    1240             : 
    1241             :         error = xfs_iread_extents(tp, ip, whichfork);
    1242             :         if (error)
    1243     9265495 :                 return error;
    1244     9265591 : 
    1245     9265591 :         lowest = max = *first_unused;
    1246     9265591 :         for_each_xfs_iext(ifp, &icur, &got) {
    1247     9265591 :                 /*
    1248     9265591 :                  * See if the hole before this extent will work.
    1249             :                  */
    1250     9265591 :                 if (got.br_startoff >= lowest + len &&
    1251           0 :                     got.br_startoff - max >= len)
    1252           0 :                         break;
    1253             :                 lastaddr = got.br_startoff + got.br_blockcount;
    1254             :                 max = XFS_FILEOFF_MAX(lastaddr, lowest);
    1255     9265591 :         }
    1256             : 
    1257     9265591 :         *first_unused = max;
    1258     9265561 :         return 0;
    1259             : }
    1260             : 
    1261     9265548 : /*
    1262    28139908 :  * Returns the file-relative block number of the last block - 1 before
    1263             :  * last_block (input value) in the file.
    1264             :  * This is not based on i_size, it is based on the extent records.
    1265             :  * Returns 0 for local files, as they do not have extent records.
    1266    18961191 :  */
    1267    13988101 : int                                             /* error */
    1268             : xfs_bmap_last_before(
    1269    18875301 :         struct xfs_trans        *tp,            /* transaction pointer */
    1270    18875301 :         struct xfs_inode        *ip,            /* incore inode */
    1271             :         xfs_fileoff_t           *last_block,    /* last block */
    1272             :         int                     whichfork)      /* data or attr fork */
    1273     9265397 : {
    1274     9265397 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1275             :         struct xfs_bmbt_irec    got;
    1276             :         struct xfs_iext_cursor  icur;
    1277             :         int                     error;
    1278             : 
    1279             :         switch (ifp->if_format) {
    1280             :         case XFS_DINODE_FMT_LOCAL:
    1281             :                 *last_block = 0;
    1282             :                 return 0;
    1283             :         case XFS_DINODE_FMT_BTREE:
    1284      295286 :         case XFS_DINODE_FMT_EXTENTS:
    1285             :                 break;
    1286             :         default:
    1287             :                 ASSERT(0);
    1288             :                 xfs_bmap_mark_sick(ip, whichfork);
    1289             :                 return -EFSCORRUPTED;
    1290      295286 :         }
    1291      295286 : 
    1292      295286 :         error = xfs_iread_extents(tp, ip, whichfork);
    1293      295286 :         if (error)
    1294             :                 return error;
    1295      295286 : 
    1296           0 :         if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
    1297           0 :                 *last_block = 0;
    1298           0 :         return 0;
    1299             : }
    1300             : 
    1301      295286 : int
    1302           0 : xfs_bmap_last_extent(
    1303           0 :         struct xfs_trans        *tp,
    1304           0 :         struct xfs_inode        *ip,
    1305           0 :         int                     whichfork,
    1306             :         struct xfs_bmbt_irec    *rec,
    1307             :         int                     *is_empty)
    1308      295286 : {
    1309      295286 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1310             :         struct xfs_iext_cursor  icur;
    1311             :         int                     error;
    1312      295286 : 
    1313      272763 :         error = xfs_iread_extents(tp, ip, whichfork);
    1314             :         if (error)
    1315             :                 return error;
    1316             : 
    1317             :         xfs_iext_last(ifp, &icur);
    1318   448816440 :         if (!xfs_iext_get_extent(ifp, &icur, rec))
    1319             :                 *is_empty = 1;
    1320             :         else
    1321             :                 *is_empty = 0;
    1322             :         return 0;
    1323             : }
    1324             : 
    1325   448816440 : /*
    1326   448858446 :  * Check the last inode extent to determine whether this allocation will result
    1327   448858446 :  * in blocks being allocated at the end of the file. When we allocate new data
    1328             :  * blocks at the end of the file which do not start at the previous data block,
    1329   448858446 :  * we will try to align the new blocks at stripe unit boundaries.
    1330   448910252 :  *
    1331             :  * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
    1332             :  * at, or past the EOF.
    1333   448859213 :  */
    1334   448934679 : STATIC int
    1335    19803419 : xfs_bmap_isaeof(
    1336             :         struct xfs_bmalloca     *bma,
    1337   429154194 :         int                     whichfork)
    1338             : {
    1339             :         struct xfs_bmbt_irec    rec;
    1340             :         int                     is_empty;
    1341             :         int                     error;
    1342             : 
    1343             :         bma->aeof = false;
    1344             :         error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
    1345             :                                      &is_empty);
    1346             :         if (error)
    1347             :                 return error;
    1348             : 
    1349             :         if (is_empty) {
    1350             :                 bma->aeof = true;
    1351        6496 :                 return 0;
    1352             :         }
    1353             : 
    1354             :         /*
    1355        6496 :          * Check if we are allocation or past the last extent, or at least into
    1356        6496 :          * the last delayed allocated extent.
    1357        6496 :          */
    1358             :         bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
    1359        6496 :                 (bma->offset >= rec.br_startoff &&
    1360        6496 :                  isnullstartblock(rec.br_startblock));
    1361             :         return 0;
    1362        6496 : }
    1363             : 
    1364             : /*
    1365        6496 :  * Returns the file-relative block number of the first block past eof in
    1366        1156 :  * the file.  This is not based on i_size, it is based on the extent records.
    1367        1156 :  * Returns 0 for local files, as they do not have extent records.
    1368             :  */
    1369             : int
    1370             : xfs_bmap_last_offset(
    1371             :         struct xfs_inode        *ip,
    1372             :         xfs_fileoff_t           *last_block,
    1373             :         int                     whichfork)
    1374        5340 : {
    1375        1241 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    1376        1241 :         struct xfs_bmbt_irec    rec;
    1377        5340 :         int                     is_empty;
    1378             :         int                     error;
    1379             : 
    1380             :         *last_block = 0;
    1381             : 
    1382             :         if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
    1383             :                 return 0;
    1384             : 
    1385             :         if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
    1386   448392863 :                 xfs_bmap_mark_sick(ip, whichfork);
    1387             :                 return -EFSCORRUPTED;
    1388             :         }
    1389             : 
    1390             :         error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
    1391   448392863 :         if (error || is_empty)
    1392   448426967 :                 return error;
    1393   448426967 : 
    1394   448426967 :         *last_block = rec.br_startoff + rec.br_blockcount;
    1395             :         return 0;
    1396   448426967 : }
    1397             : 
    1398   448426967 : /*
    1399             :  * Extent tree manipulation functions used during allocation.
    1400             :  */
    1401   448426967 : 
    1402           0 : /*
    1403           0 :  * Convert a delayed allocation to a real allocation.
    1404             :  */
    1405             : STATIC int                              /* error */
    1406   448426967 : xfs_bmap_add_extent_delay_real(
    1407   448502276 :         struct xfs_bmalloca     *bma,
    1408             :         int                     whichfork)
    1409             : {
    1410   428709689 :         struct xfs_mount        *mp = bma->ip->i_mount;
    1411   428709689 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    1412             :         struct xfs_bmbt_irec    *new = &bma->got;
    1413             :         int                     error;  /* error return value */
    1414             :         int                     i;      /* temp state */
    1415             :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    1416             :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    1417             :                                         /* left is 0, right is 1, prev is 2 */
    1418             :         int                     rval=0; /* return value (logging flags) */
    1419             :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    1420             :         xfs_filblks_t           da_new; /* new count del alloc blocks used */
    1421             :         xfs_filblks_t           da_old; /* old count del alloc blocks used */
    1422    16663601 :         xfs_filblks_t           temp=0; /* value for da_new calculations */
    1423             :         int                     tmp_rval;       /* partial logging flags */
    1424             :         struct xfs_bmbt_irec    old;
    1425             : 
    1426    16663601 :         ASSERT(whichfork != XFS_ATTR_FORK);
    1427    16663601 :         ASSERT(!isnullstartblock(new->br_startblock));
    1428    16663589 :         ASSERT(!bma->cur ||
    1429    16663589 :                (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    1430    16663589 : 
    1431    16663589 :         XFS_STATS_INC(mp, xs_add_exlist);
    1432    16663589 : 
    1433             : #define LEFT            r[0]
    1434    16663589 : #define RIGHT           r[1]
    1435    16663589 : #define PREV            r[2]
    1436    16663589 : 
    1437    16663589 :         /*
    1438    16663589 :          * Set up a bunch of variables to make the tests simpler.
    1439    16663589 :          */
    1440    16663589 :         xfs_iext_get_extent(ifp, &bma->icur, &PREV);
    1441             :         new_endoff = new->br_startoff + new->br_blockcount;
    1442    16663589 :         ASSERT(isnullstartblock(PREV.br_startblock));
    1443    16663589 :         ASSERT(PREV.br_startoff <= new->br_startoff);
    1444    16663589 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    1445             : 
    1446             :         da_old = startblockval(PREV.br_startblock);
    1447    16663589 :         da_new = 0;
    1448             : 
    1449             :         /*
    1450             :          * Set flags determining what part of the previous delayed allocation
    1451             :          * extent is being replaced by a real allocation.
    1452             :          */
    1453             :         if (PREV.br_startoff == new->br_startoff)
    1454             :                 state |= BMAP_LEFT_FILLING;
    1455             :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    1456    16663589 :                 state |= BMAP_RIGHT_FILLING;
    1457    16663591 : 
    1458    16663591 :         /*
    1459    16663591 :          * Check and set flags if this segment has a left neighbor.
    1460    16663591 :          * Don't set contiguous if the combined extent would be too large.
    1461             :          */
    1462    16663591 :         if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
    1463    16663591 :                 state |= BMAP_LEFT_VALID;
    1464             :                 if (isnullstartblock(LEFT.br_startblock))
    1465             :                         state |= BMAP_LEFT_DELAY;
    1466             :         }
    1467             : 
    1468             :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    1469    16663591 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    1470    16663611 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    1471    16663591 :             LEFT.br_state == new->br_state &&
    1472    16427459 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    1473             :                 state |= BMAP_LEFT_CONTIG;
    1474             : 
    1475             :         /*
    1476             :          * Check and set flags if this segment has a right neighbor.
    1477             :          * Don't set contiguous if the combined extent would be too large.
    1478    16663591 :          * Also check for all-three-contiguous being too large.
    1479    11842765 :          */
    1480    11842765 :         if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
    1481       47433 :                 state |= BMAP_RIGHT_VALID;
    1482             :                 if (isnullstartblock(RIGHT.br_startblock))
    1483             :                         state |= BMAP_RIGHT_DELAY;
    1484    16663590 :         }
    1485    11795345 : 
    1486      946806 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    1487      211916 :             new_endoff == RIGHT.br_startoff &&
    1488       97932 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    1489       93690 :             new->br_state == RIGHT.br_state &&
    1490             :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    1491             :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1492             :                        BMAP_RIGHT_FILLING)) !=
    1493             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    1494             :                        BMAP_RIGHT_FILLING) ||
    1495             :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    1496    16663590 :                         <= XFS_MAX_BMBT_EXTLEN))
    1497     8493913 :                 state |= BMAP_RIGHT_CONTIG;
    1498     8493913 : 
    1499      792746 :         error = 0;
    1500             :         /*
    1501             :          * Switch out based on the FILLING and CONTIG state bits.
    1502    16663596 :          */
    1503     7701176 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1504      646275 :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    1505       59820 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    1506       28213 :              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1507       28213 :                 /*
    1508             :                  * Filling in all of a previously delayed allocation extent.
    1509             :                  * The left and right neighbors are both contiguous with new.
    1510       12787 :                  */
    1511       12787 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    1512             : 
    1513       28213 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1514             :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1515    16663596 :                 xfs_iext_prev(ifp, &bma->icur);
    1516             :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1517             :                 ifp->if_nextents--;
    1518             : 
    1519    16663596 :                 if (bma->cur == NULL)
    1520             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1521       12787 :                 else {
    1522             :                         rval = XFS_ILOG_CORE;
    1523             :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1524             :                         if (error)
    1525             :                                 goto done;
    1526             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1527       12787 :                                 xfs_btree_mark_sick(bma->cur);
    1528             :                                 error = -EFSCORRUPTED;
    1529       12787 :                                 goto done;
    1530       12787 :                         }
    1531       12787 :                         error = xfs_btree_delete(bma->cur, &i);
    1532       12787 :                         if (error)
    1533       12787 :                                 goto done;
    1534             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1535       12787 :                                 xfs_btree_mark_sick(bma->cur);
    1536             :                                 error = -EFSCORRUPTED;
    1537             :                                 goto done;
    1538        1994 :                         }
    1539        1994 :                         error = xfs_btree_decrement(bma->cur, 0, &i);
    1540        1994 :                         if (error)
    1541           0 :                                 goto done;
    1542        1994 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1543           0 :                                 xfs_btree_mark_sick(bma->cur);
    1544           0 :                                 error = -EFSCORRUPTED;
    1545           0 :                                 goto done;
    1546             :                         }
    1547        1994 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1548        1994 :                         if (error)
    1549           0 :                                 goto done;
    1550        1994 :                 }
    1551           0 :                 break;
    1552           0 : 
    1553           0 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    1554             :                 /*
    1555        1994 :                  * Filling in all of a previously delayed allocation extent.
    1556        1994 :                  * The left neighbor is contiguous, the right is not.
    1557           0 :                  */
    1558        1994 :                 old = LEFT;
    1559           0 :                 LEFT.br_blockcount += PREV.br_blockcount;
    1560           0 : 
    1561           0 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1562             :                 xfs_iext_prev(ifp, &bma->icur);
    1563        1994 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1564        1994 : 
    1565           0 :                 if (bma->cur == NULL)
    1566             :                         rval = XFS_ILOG_DEXT;
    1567             :                 else {
    1568             :                         rval = 0;
    1569       69048 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1570             :                         if (error)
    1571             :                                 goto done;
    1572             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1573             :                                 xfs_btree_mark_sick(bma->cur);
    1574       69048 :                                 error = -EFSCORRUPTED;
    1575       69048 :                                 goto done;
    1576             :                         }
    1577       69048 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1578       69048 :                         if (error)
    1579       69048 :                                 goto done;
    1580             :                 }
    1581       69048 :                 break;
    1582             : 
    1583             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1584        8374 :                 /*
    1585        8374 :                  * Filling in all of a previously delayed allocation extent.
    1586        8374 :                  * The right neighbor is contiguous, the left is not. Take care
    1587           0 :                  * with delay -> unwritten extent allocation here because the
    1588        8374 :                  * delalloc record we are overwriting is always written.
    1589           0 :                  */
    1590           0 :                 PREV.br_startblock = new->br_startblock;
    1591           0 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    1592             :                 PREV.br_state = new->br_state;
    1593        8374 : 
    1594        8374 :                 xfs_iext_next(ifp, &bma->icur);
    1595           0 :                 xfs_iext_remove(bma->ip, &bma->icur, state);
    1596             :                 xfs_iext_prev(ifp, &bma->icur);
    1597             :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1598             : 
    1599       15426 :                 if (bma->cur == NULL)
    1600             :                         rval = XFS_ILOG_DEXT;
    1601             :                 else {
    1602             :                         rval = 0;
    1603             :                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
    1604             :                         if (error)
    1605             :                                 goto done;
    1606       15426 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1607       15426 :                                 xfs_btree_mark_sick(bma->cur);
    1608       15426 :                                 error = -EFSCORRUPTED;
    1609             :                                 goto done;
    1610       15426 :                         }
    1611       15426 :                         error = xfs_bmbt_update(bma->cur, &PREV);
    1612       15426 :                         if (error)
    1613       15426 :                                 goto done;
    1614             :                 }
    1615       15426 :                 break;
    1616             : 
    1617             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    1618        1983 :                 /*
    1619        1983 :                  * Filling in all of a previously delayed allocation extent.
    1620        1983 :                  * Neither the left nor right neighbors are contiguous with
    1621           0 :                  * the new one.
    1622        1983 :                  */
    1623           0 :                 PREV.br_startblock = new->br_startblock;
    1624           0 :                 PREV.br_state = new->br_state;
    1625           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1626             :                 ifp->if_nextents++;
    1627        1983 : 
    1628        1983 :                 if (bma->cur == NULL)
    1629           0 :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1630             :                 else {
    1631             :                         rval = XFS_ILOG_CORE;
    1632             :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1633    16330183 :                         if (error)
    1634             :                                 goto done;
    1635             :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1636             :                                 xfs_btree_mark_sick(bma->cur);
    1637             :                                 error = -EFSCORRUPTED;
    1638             :                                 goto done;
    1639    16330183 :                         }
    1640    16330183 :                         error = xfs_btree_insert(bma->cur, &i);
    1641    16330183 :                         if (error)
    1642    16330151 :                                 goto done;
    1643             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1644    16330151 :                                 xfs_btree_mark_sick(bma->cur);
    1645             :                                 error = -EFSCORRUPTED;
    1646             :                                 goto done;
    1647     2531444 :                         }
    1648     2531444 :                 }
    1649     2531445 :                 break;
    1650           4 : 
    1651     2531441 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
    1652           0 :                 /*
    1653           0 :                  * Filling in the first part of a previous delayed allocation.
    1654           0 :                  * The left neighbor is contiguous.
    1655             :                  */
    1656     2531441 :                 old = LEFT;
    1657     2531441 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1658           0 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1659     2531441 :                                 startblockval(PREV.br_startblock));
    1660           0 : 
    1661           0 :                 LEFT.br_blockcount += new->br_blockcount;
    1662           0 : 
    1663             :                 PREV.br_blockcount = temp;
    1664             :                 PREV.br_startoff += new->br_blockcount;
    1665             :                 PREV.br_startblock = nullstartblock(da_new);
    1666             : 
    1667       11855 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1668             :                 xfs_iext_prev(ifp, &bma->icur);
    1669             :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
    1670             : 
    1671             :                 if (bma->cur == NULL)
    1672       11855 :                         rval = XFS_ILOG_DEXT;
    1673       11855 :                 else {
    1674       11855 :                         rval = 0;
    1675             :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1676             :                         if (error)
    1677       11855 :                                 goto done;
    1678             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1679       11855 :                                 xfs_btree_mark_sick(bma->cur);
    1680       11855 :                                 error = -EFSCORRUPTED;
    1681       11855 :                                 goto done;
    1682             :                         }
    1683       11855 :                         error = xfs_bmbt_update(bma->cur, &LEFT);
    1684       11855 :                         if (error)
    1685       11855 :                                 goto done;
    1686             :                 }
    1687       11855 :                 break;
    1688             : 
    1689             :         case BMAP_LEFT_FILLING:
    1690        9674 :                 /*
    1691        9674 :                  * Filling in the first part of a previous delayed allocation.
    1692        9674 :                  * The left neighbor is not contiguous.
    1693           0 :                  */
    1694        9674 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1695           0 :                 ifp->if_nextents++;
    1696           0 : 
    1697           0 :                 if (bma->cur == NULL)
    1698             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1699        9674 :                 else {
    1700        9674 :                         rval = XFS_ILOG_CORE;
    1701           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1702             :                         if (error)
    1703             :                                 goto done;
    1704             :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1705      224297 :                                 xfs_btree_mark_sick(bma->cur);
    1706             :                                 error = -EFSCORRUPTED;
    1707             :                                 goto done;
    1708             :                         }
    1709             :                         error = xfs_btree_insert(bma->cur, &i);
    1710      224297 :                         if (error)
    1711      224298 :                                 goto done;
    1712             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1713      224298 :                                 xfs_btree_mark_sick(bma->cur);
    1714             :                                 error = -EFSCORRUPTED;
    1715             :                                 goto done;
    1716      185094 :                         }
    1717      185094 :                 }
    1718      185094 : 
    1719           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1720      185094 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1721           0 :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1722           0 :                         rval |= tmp_rval;
    1723           0 :                         if (error)
    1724             :                                 goto done;
    1725      185094 :                 }
    1726      185093 : 
    1727           0 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1728      185093 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1729           0 :                         startblockval(PREV.br_startblock) -
    1730           0 :                         (bma->cur ? bma->cur->bc_ino.allocated : 0));
    1731           0 : 
    1732             :                 PREV.br_startoff = new_endoff;
    1733             :                 PREV.br_blockcount = temp;
    1734             :                 PREV.br_startblock = nullstartblock(da_new);
    1735      224297 :                 xfs_iext_next(ifp, &bma->icur);
    1736        2293 :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1737             :                 xfs_iext_prev(ifp, &bma->icur);
    1738        2293 :                 break;
    1739        2293 : 
    1740           0 :         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    1741             :                 /*
    1742             :                  * Filling in the last part of a previous delayed allocation.
    1743      224296 :                  * The right neighbor is contiguous with the new allocation.
    1744      224296 :                  */
    1745             :                 old = RIGHT;
    1746             :                 RIGHT.br_startoff = new->br_startoff;
    1747             :                 RIGHT.br_startblock = new->br_startblock;
    1748      224297 :                 RIGHT.br_blockcount += new->br_blockcount;
    1749      224297 : 
    1750      224297 :                 if (bma->cur == NULL)
    1751      224297 :                         rval = XFS_ILOG_DEXT;
    1752      224298 :                 else {
    1753      224296 :                         rval = 0;
    1754      224296 :                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
    1755             :                         if (error)
    1756           0 :                                 goto done;
    1757             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1758             :                                 xfs_btree_mark_sick(bma->cur);
    1759             :                                 error = -EFSCORRUPTED;
    1760             :                                 goto done;
    1761           0 :                         }
    1762           0 :                         error = xfs_bmbt_update(bma->cur, &RIGHT);
    1763           0 :                         if (error)
    1764           0 :                                 goto done;
    1765             :                 }
    1766           0 : 
    1767             :                 temp = PREV.br_blockcount - new->br_blockcount;
    1768             :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1769           0 :                         startblockval(PREV.br_startblock));
    1770           0 : 
    1771           0 :                 PREV.br_blockcount = temp;
    1772           0 :                 PREV.br_startblock = nullstartblock(da_new);
    1773           0 : 
    1774           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1775           0 :                 xfs_iext_next(ifp, &bma->icur);
    1776           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
    1777             :                 break;
    1778           0 : 
    1779           0 :         case BMAP_RIGHT_FILLING:
    1780           0 :                 /*
    1781             :                  * Filling in the last part of a previous delayed allocation.
    1782             :                  * The right neighbor is not contiguous.
    1783           0 :                  */
    1784           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
    1785             :                 ifp->if_nextents++;
    1786             : 
    1787           0 :                 if (bma->cur == NULL)
    1788           0 :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1789             :                 else {
    1790           0 :                         rval = XFS_ILOG_CORE;
    1791           0 :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1792           0 :                         if (error)
    1793           0 :                                 goto done;
    1794             :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1795           0 :                                 xfs_btree_mark_sick(bma->cur);
    1796             :                                 error = -EFSCORRUPTED;
    1797             :                                 goto done;
    1798             :                         }
    1799             :                         error = xfs_btree_insert(bma->cur, &i);
    1800           0 :                         if (error)
    1801           0 :                                 goto done;
    1802             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1803           0 :                                 xfs_btree_mark_sick(bma->cur);
    1804             :                                 error = -EFSCORRUPTED;
    1805             :                                 goto done;
    1806           0 :                         }
    1807           0 :                 }
    1808           0 : 
    1809           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1810           0 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1811           0 :                                 &bma->cur, 1, &tmp_rval, whichfork);
    1812           0 :                         rval |= tmp_rval;
    1813           0 :                         if (error)
    1814             :                                 goto done;
    1815           0 :                 }
    1816           0 : 
    1817           0 :                 temp = PREV.br_blockcount - new->br_blockcount;
    1818           0 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
    1819           0 :                         startblockval(PREV.br_startblock) -
    1820           0 :                         (bma->cur ? bma->cur->bc_ino.allocated : 0));
    1821           0 : 
    1822             :                 PREV.br_startblock = nullstartblock(da_new);
    1823             :                 PREV.br_blockcount = temp;
    1824             :                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
    1825           0 :                 xfs_iext_next(ifp, &bma->icur);
    1826           0 :                 break;
    1827             : 
    1828           0 :         case 0:
    1829           0 :                 /*
    1830           0 :                  * Filling in the middle part of a previous delayed allocation.
    1831             :                  * Contiguity is impossible here.
    1832             :                  * This case is avoided almost all the time.
    1833           0 :                  *
    1834           0 :                  * We start with a delayed allocation:
    1835             :                  *
    1836             :                  * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
    1837             :                  *  PREV @ idx
    1838           0 :                  *
    1839           0 :                  * and we are allocating:
    1840           0 :                  *                     +rrrrrrrrrrrrrrrrr+
    1841           0 :                  *                            new
    1842           0 :                  *
    1843             :                  * and we set it up for insertion as:
    1844           0 :                  * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
    1845             :                  *                            new
    1846             :                  *  PREV @ idx          LEFT              RIGHT
    1847             :                  *                      inserted at idx + 1
    1848             :                  */
    1849             :                 old = PREV;
    1850             : 
    1851             :                 /* LEFT is the new middle */
    1852             :                 LEFT = *new;
    1853             : 
    1854             :                 /* RIGHT is the new right */
    1855             :                 RIGHT.br_state = PREV.br_state;
    1856             :                 RIGHT.br_startoff = new_endoff;
    1857             :                 RIGHT.br_blockcount =
    1858             :                         PREV.br_startoff + PREV.br_blockcount - new_endoff;
    1859             :                 RIGHT.br_startblock =
    1860             :                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
    1861             :                                         RIGHT.br_blockcount));
    1862             : 
    1863             :                 /* truncate PREV */
    1864             :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    1865           0 :                 PREV.br_startblock =
    1866             :                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
    1867             :                                         PREV.br_blockcount));
    1868           0 :                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
    1869             : 
    1870             :                 xfs_iext_next(ifp, &bma->icur);
    1871           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
    1872           0 :                 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
    1873           0 :                 ifp->if_nextents++;
    1874           0 : 
    1875           0 :                 if (bma->cur == NULL)
    1876           0 :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    1877             :                 else {
    1878             :                         rval = XFS_ILOG_CORE;
    1879             :                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
    1880           0 :                         if (error)
    1881           0 :                                 goto done;
    1882           0 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    1883             :                                 xfs_btree_mark_sick(bma->cur);
    1884           0 :                                 error = -EFSCORRUPTED;
    1885             :                                 goto done;
    1886           0 :                         }
    1887           0 :                         error = xfs_btree_insert(bma->cur, &i);
    1888           0 :                         if (error)
    1889           0 :                                 goto done;
    1890             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1891           0 :                                 xfs_btree_mark_sick(bma->cur);
    1892             :                                 error = -EFSCORRUPTED;
    1893             :                                 goto done;
    1894           0 :                         }
    1895           0 :                 }
    1896           0 : 
    1897           0 :                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1898           0 :                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1899           0 :                                         &bma->cur, 1, &tmp_rval, whichfork);
    1900           0 :                         rval |= tmp_rval;
    1901           0 :                         if (error)
    1902             :                                 goto done;
    1903           0 :                 }
    1904           0 : 
    1905           0 :                 da_new = startblockval(PREV.br_startblock) +
    1906           0 :                          startblockval(RIGHT.br_startblock);
    1907           0 :                 break;
    1908           0 : 
    1909           0 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1910             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1911             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
    1912             :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    1913           0 :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    1914           0 :         case BMAP_LEFT_CONTIG:
    1915             :         case BMAP_RIGHT_CONTIG:
    1916           0 :                 /*
    1917           0 :                  * These cases are all impossible.
    1918           0 :                  */
    1919             :                 ASSERT(0);
    1920             :         }
    1921           0 : 
    1922           0 :         /* add reverse mapping unless caller opted out */
    1923           0 :         if (!(bma->flags & XFS_BMAPI_NORMAP))
    1924             :                 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
    1925           0 : 
    1926             :         /* convert to a btree if necessary */
    1927             :         if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
    1928             :                 int     tmp_logflags;   /* partial log flag return val */
    1929             : 
    1930             :                 ASSERT(bma->cur == NULL);
    1931             :                 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
    1932             :                                 &bma->cur, da_old > 0, &tmp_logflags,
    1933             :                                 whichfork);
    1934             :                 bma->logflags |= tmp_logflags;
    1935           0 :                 if (error)
    1936             :                         goto done;
    1937             :         }
    1938             : 
    1939    16663561 :         if (da_new != da_old)
    1940    16663557 :                 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
    1941             : 
    1942             :         if (bma->cur) {
    1943    16663581 :                 da_new += bma->cur->bc_ino.allocated;
    1944      359748 :                 bma->cur->bc_ino.allocated = 0;
    1945             :         }
    1946      359748 : 
    1947      359748 :         /* adjust for changes in reserved delayed indirect blocks */
    1948             :         if (da_new != da_old) {
    1949             :                 ASSERT(state == 0 || da_new < da_old);
    1950      359748 :                 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
    1951      359748 :                                 false);
    1952          40 :         }
    1953             : 
    1954             :         xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
    1955    16663562 : done:
    1956    16430296 :         if (whichfork != XFS_COW_FORK)
    1957             :                 bma->logflags |= rval;
    1958    16663561 :         return error;
    1959     3100562 : #undef  LEFT
    1960     3100562 : #undef  RIGHT
    1961             : #undef  PREV
    1962             : }
    1963             : 
    1964    16663561 : /*
    1965    16427771 :  * Convert an unwritten allocation to a real allocation or vice versa.
    1966    16427771 :  */
    1967             : int                                     /* error */
    1968             : xfs_bmap_add_extent_unwritten_real(
    1969             :         struct xfs_trans        *tp,
    1970    16663531 :         xfs_inode_t             *ip,    /* incore inode pointer */
    1971    16663566 :         int                     whichfork,
    1972    16663566 :         struct xfs_iext_cursor  *icur,
    1973    16536635 :         struct xfs_btree_cur    **curp, /* if *curp is null, not a btree */
    1974    16663566 :         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
    1975             :         int                     *logflagsp) /* inode logging flags */
    1976             : {
    1977             :         struct xfs_btree_cur    *cur;   /* btree cursor */
    1978             :         int                     error;  /* error return value */
    1979             :         int                     i;      /* temp state */
    1980             :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    1981             :         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
    1982             :         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
    1983             :                                         /* left is 0, right is 1, prev is 2 */
    1984    38474908 :         int                     rval=0; /* return value (logging flags) */
    1985             :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    1986             :         struct xfs_mount        *mp = ip->i_mount;
    1987             :         struct xfs_bmbt_irec    old;
    1988             : 
    1989             :         *logflagsp = 0;
    1990             : 
    1991             :         cur = *curp;
    1992             :         ifp = xfs_ifork_ptr(ip, whichfork);
    1993    38474908 : 
    1994    38474908 :         ASSERT(!isnullstartblock(new->br_startblock));
    1995    38474908 : 
    1996    38474908 :         XFS_STATS_INC(mp, xs_add_exlist);
    1997    38474908 : 
    1998    38474908 : #define LEFT            r[0]
    1999             : #define RIGHT           r[1]
    2000    38474908 : #define PREV            r[2]
    2001    38474908 : 
    2002    38474908 :         /*
    2003    38474908 :          * Set up a bunch of variables to make the tests simpler.
    2004             :          */
    2005    38474908 :         error = 0;
    2006             :         xfs_iext_get_extent(ifp, icur, &PREV);
    2007    38474908 :         ASSERT(new->br_state != PREV.br_state);
    2008    38474908 :         new_endoff = new->br_startoff + new->br_blockcount;
    2009             :         ASSERT(PREV.br_startoff <= new->br_startoff);
    2010    38474837 :         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
    2011             : 
    2012    38474837 :         /*
    2013             :          * Set flags determining what part of the previous oldext allocation
    2014             :          * extent is being replaced by a newext allocation.
    2015             :          */
    2016             :         if (PREV.br_startoff == new->br_startoff)
    2017             :                 state |= BMAP_LEFT_FILLING;
    2018             :         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
    2019             :                 state |= BMAP_RIGHT_FILLING;
    2020             : 
    2021    38474837 :         /*
    2022    38474837 :          * Check and set flags if this segment has a left neighbor.
    2023    38474526 :          * Don't set contiguous if the combined extent would be too large.
    2024    38474526 :          */
    2025    38474526 :         if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
    2026    38474526 :                 state |= BMAP_LEFT_VALID;
    2027             :                 if (isnullstartblock(LEFT.br_startblock))
    2028             :                         state |= BMAP_LEFT_DELAY;
    2029             :         }
    2030             : 
    2031             :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2032    38474526 :             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
    2033    36568960 :             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
    2034    38474526 :             LEFT.br_state == new->br_state &&
    2035    33692322 :             LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2036             :                 state |= BMAP_LEFT_CONTIG;
    2037             : 
    2038             :         /*
    2039             :          * Check and set flags if this segment has a right neighbor.
    2040             :          * Don't set contiguous if the combined extent would be too large.
    2041    38474526 :          * Also check for all-three-contiguous being too large.
    2042    30362421 :          */
    2043    30362421 :         if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
    2044      119245 :                 state |= BMAP_RIGHT_VALID;
    2045             :                 if (isnullstartblock(RIGHT.br_startblock))
    2046             :                         state |= BMAP_RIGHT_DELAY;
    2047    38474578 :         }
    2048    30243271 : 
    2049    10503529 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2050      904149 :             new_endoff == RIGHT.br_startoff &&
    2051      899907 :             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
    2052      899907 :             new->br_state == RIGHT.br_state &&
    2053             :             new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2054             :             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2055             :                        BMAP_RIGHT_FILLING)) !=
    2056             :                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
    2057             :                        BMAP_RIGHT_FILLING) ||
    2058             :              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
    2059    38474578 :                         <= XFS_MAX_BMBT_EXTLEN))
    2060    24406527 :                 state |= BMAP_RIGHT_CONTIG;
    2061    24406527 : 
    2062      142989 :         /*
    2063             :          * Switch out based on the FILLING and CONTIG state bits.
    2064             :          */
    2065    38474795 :         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2066    24263552 :                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
    2067     9914849 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
    2068      309856 :              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2069      309856 :                 /*
    2070      309856 :                  * Setting all of a previous oldext extent to newext.
    2071             :                  * The left and right neighbors are both contiguous with new.
    2072             :                  */
    2073       76354 :                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
    2074       76354 : 
    2075             :                 xfs_iext_remove(ip, icur, state);
    2076      309855 :                 xfs_iext_remove(ip, icur, state);
    2077             :                 xfs_iext_prev(ifp, icur);
    2078             :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2079             :                 ifp->if_nextents -= 2;
    2080             :                 if (cur == NULL)
    2081    38474795 :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2082             :                 else {
    2083       76353 :                         rval = XFS_ILOG_CORE;
    2084             :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2085             :                         if (error)
    2086             :                                 goto done;
    2087             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2088             :                                 xfs_btree_mark_sick(cur);
    2089       76353 :                                 error = -EFSCORRUPTED;
    2090             :                                 goto done;
    2091       76353 :                         }
    2092       76353 :                         if ((error = xfs_btree_delete(cur, &i)))
    2093       76353 :                                 goto done;
    2094       76353 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2095       76353 :                                 xfs_btree_mark_sick(cur);
    2096       76353 :                                 error = -EFSCORRUPTED;
    2097             :                                 goto done;
    2098             :                         }
    2099       46318 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2100       46318 :                                 goto done;
    2101       46318 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2102           0 :                                 xfs_btree_mark_sick(cur);
    2103       46318 :                                 error = -EFSCORRUPTED;
    2104           0 :                                 goto done;
    2105           0 :                         }
    2106           0 :                         if ((error = xfs_btree_delete(cur, &i)))
    2107             :                                 goto done;
    2108       46318 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2109           0 :                                 xfs_btree_mark_sick(cur);
    2110       46318 :                                 error = -EFSCORRUPTED;
    2111           0 :                                 goto done;
    2112           0 :                         }
    2113           0 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2114             :                                 goto done;
    2115       46318 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2116           0 :                                 xfs_btree_mark_sick(cur);
    2117       46318 :                                 error = -EFSCORRUPTED;
    2118           0 :                                 goto done;
    2119           0 :                         }
    2120           0 :                         error = xfs_bmbt_update(cur, &LEFT);
    2121             :                         if (error)
    2122       46318 :                                 goto done;
    2123           0 :                 }
    2124       46318 :                 break;
    2125           0 : 
    2126           0 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    2127           0 :                 /*
    2128             :                  * Setting all of a previous oldext extent to newext.
    2129       46318 :                  * The left neighbor is contiguous, the right is not.
    2130           0 :                  */
    2131       46318 :                 LEFT.br_blockcount += PREV.br_blockcount;
    2132           0 : 
    2133           0 :                 xfs_iext_remove(ip, icur, state);
    2134           0 :                 xfs_iext_prev(ifp, icur);
    2135             :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2136       46318 :                 ifp->if_nextents--;
    2137       46318 :                 if (cur == NULL)
    2138           0 :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2139             :                 else {
    2140             :                         rval = XFS_ILOG_CORE;
    2141             :                         error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
    2142      542951 :                         if (error)
    2143             :                                 goto done;
    2144             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2145             :                                 xfs_btree_mark_sick(cur);
    2146             :                                 error = -EFSCORRUPTED;
    2147      542951 :                                 goto done;
    2148             :                         }
    2149      542951 :                         if ((error = xfs_btree_delete(cur, &i)))
    2150      542951 :                                 goto done;
    2151      542951 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2152      542951 :                                 xfs_btree_mark_sick(cur);
    2153      542951 :                                 error = -EFSCORRUPTED;
    2154             :                                 goto done;
    2155             :                         }
    2156      175526 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2157      175526 :                                 goto done;
    2158      175526 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2159           0 :                                 xfs_btree_mark_sick(cur);
    2160      175526 :                                 error = -EFSCORRUPTED;
    2161           0 :                                 goto done;
    2162           0 :                         }
    2163           0 :                         error = xfs_bmbt_update(cur, &LEFT);
    2164             :                         if (error)
    2165      175526 :                                 goto done;
    2166           0 :                 }
    2167      175526 :                 break;
    2168           0 : 
    2169           0 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2170           0 :                 /*
    2171             :                  * Setting all of a previous oldext extent to newext.
    2172      175526 :                  * The right neighbor is contiguous, the left is not.
    2173           0 :                  */
    2174      175526 :                 PREV.br_blockcount += RIGHT.br_blockcount;
    2175           0 :                 PREV.br_state = new->br_state;
    2176           0 : 
    2177           0 :                 xfs_iext_next(ifp, icur);
    2178             :                 xfs_iext_remove(ip, icur, state);
    2179      175526 :                 xfs_iext_prev(ifp, icur);
    2180      175526 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2181           0 :                 ifp->if_nextents--;
    2182             : 
    2183             :                 if (cur == NULL)
    2184             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2185      166902 :                 else {
    2186             :                         rval = XFS_ILOG_CORE;
    2187             :                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
    2188             :                         if (error)
    2189             :                                 goto done;
    2190      166902 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2191      166902 :                                 xfs_btree_mark_sick(cur);
    2192             :                                 error = -EFSCORRUPTED;
    2193      166902 :                                 goto done;
    2194      166902 :                         }
    2195      166902 :                         if ((error = xfs_btree_delete(cur, &i)))
    2196      166902 :                                 goto done;
    2197      166902 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2198             :                                 xfs_btree_mark_sick(cur);
    2199      166902 :                                 error = -EFSCORRUPTED;
    2200             :                                 goto done;
    2201             :                         }
    2202       42524 :                         if ((error = xfs_btree_decrement(cur, 0, &i)))
    2203       42524 :                                 goto done;
    2204       42524 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2205           0 :                                 xfs_btree_mark_sick(cur);
    2206       42524 :                                 error = -EFSCORRUPTED;
    2207           0 :                                 goto done;
    2208           0 :                         }
    2209           0 :                         error = xfs_bmbt_update(cur, &PREV);
    2210             :                         if (error)
    2211       42524 :                                 goto done;
    2212           0 :                 }
    2213       42524 :                 break;
    2214           0 : 
    2215           0 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    2216           0 :                 /*
    2217             :                  * Setting all of a previous oldext extent to newext.
    2218       42524 :                  * Neither the left nor right neighbors are contiguous with
    2219           0 :                  * the new one.
    2220       42524 :                  */
    2221           0 :                 PREV.br_state = new->br_state;
    2222           0 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2223           0 : 
    2224             :                 if (cur == NULL)
    2225       42524 :                         rval = XFS_ILOG_DEXT;
    2226       42524 :                 else {
    2227           0 :                         rval = 0;
    2228             :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2229             :                         if (error)
    2230             :                                 goto done;
    2231    32153046 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2232             :                                 xfs_btree_mark_sick(cur);
    2233             :                                 error = -EFSCORRUPTED;
    2234             :                                 goto done;
    2235             :                         }
    2236             :                         error = xfs_bmbt_update(cur, &PREV);
    2237    32153046 :                         if (error)
    2238    32153046 :                                 goto done;
    2239             :                 }
    2240    32152847 :                 break;
    2241             : 
    2242             :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
    2243    12902402 :                 /*
    2244    12902402 :                  * Setting the first part of a previous oldext extent to newext.
    2245    12902405 :                  * The left neighbor is contiguous.
    2246           0 :                  */
    2247    12902405 :                 LEFT.br_blockcount += new->br_blockcount;
    2248           0 : 
    2249           0 :                 old = PREV;
    2250           0 :                 PREV.br_startoff += new->br_blockcount;
    2251             :                 PREV.br_startblock += new->br_blockcount;
    2252    12902405 :                 PREV.br_blockcount -= new->br_blockcount;
    2253    12902388 : 
    2254           0 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2255             :                 xfs_iext_prev(ifp, icur);
    2256             :                 xfs_iext_update_extent(ip, state, icur, &LEFT);
    2257             : 
    2258      280603 :                 if (cur == NULL)
    2259             :                         rval = XFS_ILOG_DEXT;
    2260             :                 else {
    2261             :                         rval = 0;
    2262             :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2263      280603 :                         if (error)
    2264             :                                 goto done;
    2265      280603 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2266      280603 :                                 xfs_btree_mark_sick(cur);
    2267      280603 :                                 error = -EFSCORRUPTED;
    2268      280603 :                                 goto done;
    2269             :                         }
    2270      280603 :                         error = xfs_bmbt_update(cur, &PREV);
    2271      280603 :                         if (error)
    2272      280603 :                                 goto done;
    2273             :                         error = xfs_btree_decrement(cur, 0, &i);
    2274      280603 :                         if (error)
    2275             :                                 goto done;
    2276             :                         error = xfs_bmbt_update(cur, &LEFT);
    2277      100491 :                         if (error)
    2278      100491 :                                 goto done;
    2279      100491 :                 }
    2280           0 :                 break;
    2281      100491 : 
    2282           0 :         case BMAP_LEFT_FILLING:
    2283           0 :                 /*
    2284           0 :                  * Setting the first part of a previous oldext extent to newext.
    2285             :                  * The left neighbor is not contiguous.
    2286      100491 :                  */
    2287      100491 :                 old = PREV;
    2288           0 :                 PREV.br_startoff += new->br_blockcount;
    2289      100491 :                 PREV.br_startblock += new->br_blockcount;
    2290      100491 :                 PREV.br_blockcount -= new->br_blockcount;
    2291           0 : 
    2292      100491 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2293      100491 :                 xfs_iext_insert(ip, icur, new, state);
    2294           0 :                 ifp->if_nextents++;
    2295             : 
    2296             :                 if (cur == NULL)
    2297             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2298     3348901 :                 else {
    2299             :                         rval = XFS_ILOG_CORE;
    2300             :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2301             :                         if (error)
    2302             :                                 goto done;
    2303     3348901 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2304     3348901 :                                 xfs_btree_mark_sick(cur);
    2305     3348901 :                                 error = -EFSCORRUPTED;
    2306     3348901 :                                 goto done;
    2307             :                         }
    2308     3348901 :                         error = xfs_bmbt_update(cur, &PREV);
    2309     3348900 :                         if (error)
    2310     3348897 :                                 goto done;
    2311             :                         cur->bc_rec.b = *new;
    2312     3348897 :                         if ((error = xfs_btree_insert(cur, &i)))
    2313             :                                 goto done;
    2314             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2315      493616 :                                 xfs_btree_mark_sick(cur);
    2316      493616 :                                 error = -EFSCORRUPTED;
    2317      493616 :                                 goto done;
    2318           0 :                         }
    2319      493616 :                 }
    2320           0 :                 break;
    2321           0 : 
    2322           0 :         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
    2323             :                 /*
    2324      493616 :                  * Setting the last part of a previous oldext extent to newext.
    2325      493616 :                  * The right neighbor is contiguous with the new allocation.
    2326           0 :                  */
    2327      493616 :                 old = PREV;
    2328      493616 :                 PREV.br_blockcount -= new->br_blockcount;
    2329           0 : 
    2330      493616 :                 RIGHT.br_startoff = new->br_startoff;
    2331           0 :                 RIGHT.br_startblock = new->br_startblock;
    2332           0 :                 RIGHT.br_blockcount += new->br_blockcount;
    2333           0 : 
    2334             :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2335             :                 xfs_iext_next(ifp, icur);
    2336             :                 xfs_iext_update_extent(ip, state, icur, &RIGHT);
    2337             : 
    2338       66600 :                 if (cur == NULL)
    2339             :                         rval = XFS_ILOG_DEXT;
    2340             :                 else {
    2341             :                         rval = 0;
    2342             :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2343       66600 :                         if (error)
    2344       66600 :                                 goto done;
    2345             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2346       66600 :                                 xfs_btree_mark_sick(cur);
    2347       66600 :                                 error = -EFSCORRUPTED;
    2348       66600 :                                 goto done;
    2349             :                         }
    2350       66600 :                         error = xfs_bmbt_update(cur, &PREV);
    2351       66600 :                         if (error)
    2352       66600 :                                 goto done;
    2353             :                         error = xfs_btree_increment(cur, 0, &i);
    2354       66600 :                         if (error)
    2355             :                                 goto done;
    2356             :                         error = xfs_bmbt_update(cur, &RIGHT);
    2357       45437 :                         if (error)
    2358       45437 :                                 goto done;
    2359       45437 :                 }
    2360           0 :                 break;
    2361       45437 : 
    2362           0 :         case BMAP_RIGHT_FILLING:
    2363           0 :                 /*
    2364           0 :                  * Setting the last part of a previous oldext extent to newext.
    2365             :                  * The right neighbor is not contiguous.
    2366       45437 :                  */
    2367       45437 :                 old = PREV;
    2368           0 :                 PREV.br_blockcount -= new->br_blockcount;
    2369       45437 : 
    2370       45437 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2371           0 :                 xfs_iext_next(ifp, icur);
    2372       45437 :                 xfs_iext_insert(ip, icur, new, state);
    2373       45437 :                 ifp->if_nextents++;
    2374           0 : 
    2375             :                 if (cur == NULL)
    2376             :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2377             :                 else {
    2378      686257 :                         rval = XFS_ILOG_CORE;
    2379             :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2380             :                         if (error)
    2381             :                                 goto done;
    2382             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2383      686257 :                                 xfs_btree_mark_sick(cur);
    2384      686257 :                                 error = -EFSCORRUPTED;
    2385             :                                 goto done;
    2386      686257 :                         }
    2387      686257 :                         error = xfs_bmbt_update(cur, &PREV);
    2388      686257 :                         if (error)
    2389      686257 :                                 goto done;
    2390             :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2391      686257 :                         if (error)
    2392             :                                 goto done;
    2393             :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2394      148697 :                                 xfs_btree_mark_sick(cur);
    2395      148697 :                                 error = -EFSCORRUPTED;
    2396      148697 :                                 goto done;
    2397           0 :                         }
    2398      148697 :                         if ((error = xfs_btree_insert(cur, &i)))
    2399           0 :                                 goto done;
    2400           0 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2401           0 :                                 xfs_btree_mark_sick(cur);
    2402             :                                 error = -EFSCORRUPTED;
    2403      148697 :                                 goto done;
    2404      148697 :                         }
    2405           0 :                 }
    2406      148697 :                 break;
    2407      148697 : 
    2408           0 :         case 0:
    2409      148697 :                 /*
    2410           0 :                  * Setting the middle part of a previous oldext extent to
    2411           0 :                  * newext.  Contiguity is impossible here.
    2412           0 :                  * One extent becomes three extents.
    2413             :                  */
    2414      148697 :                 old = PREV;
    2415           0 :                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
    2416      148697 : 
    2417           0 :                 r[0] = *new;
    2418           0 :                 r[1].br_startoff = new_endoff;
    2419           0 :                 r[1].br_blockcount =
    2420             :                         old.br_startoff + old.br_blockcount - new_endoff;
    2421             :                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
    2422             :                 r[1].br_state = PREV.br_state;
    2423             : 
    2424     1153182 :                 xfs_iext_update_extent(ip, state, icur, &PREV);
    2425             :                 xfs_iext_next(ifp, icur);
    2426             :                 xfs_iext_insert(ip, icur, &r[1], state);
    2427             :                 xfs_iext_insert(ip, icur, &r[0], state);
    2428             :                 ifp->if_nextents += 2;
    2429             : 
    2430     1153182 :                 if (cur == NULL)
    2431     1153182 :                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
    2432             :                 else {
    2433     1153182 :                         rval = XFS_ILOG_CORE;
    2434     1153182 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2435     1153182 :                         if (error)
    2436     1153182 :                                 goto done;
    2437     1153182 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2438     1153182 :                                 xfs_btree_mark_sick(cur);
    2439             :                                 error = -EFSCORRUPTED;
    2440     1153182 :                                 goto done;
    2441     1153129 :                         }
    2442     1153013 :                         /* new right extent - oldext */
    2443     1152977 :                         error = xfs_bmbt_update(cur, &r[1]);
    2444     1152964 :                         if (error)
    2445             :                                 goto done;
    2446     1152964 :                         /* new left extent - oldext */
    2447             :                         cur->bc_rec.b = PREV;
    2448             :                         if ((error = xfs_btree_insert(cur, &i)))
    2449      199522 :                                 goto done;
    2450      199522 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2451      199522 :                                 xfs_btree_mark_sick(cur);
    2452           0 :                                 error = -EFSCORRUPTED;
    2453      199522 :                                 goto done;
    2454           0 :                         }
    2455           0 :                         /*
    2456           0 :                          * Reset the cursor to the position of the new extent
    2457             :                          * we are about to insert as we can't trust it after
    2458             :                          * the previous insert.
    2459      199522 :                          */
    2460      199521 :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2461           0 :                         if (error)
    2462             :                                 goto done;
    2463      199521 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2464      199521 :                                 xfs_btree_mark_sick(cur);
    2465           0 :                                 error = -EFSCORRUPTED;
    2466      199522 :                                 goto done;
    2467           0 :                         }
    2468           0 :                         /* new middle extent - newext */
    2469           0 :                         if ((error = xfs_btree_insert(cur, &i)))
    2470             :                                 goto done;
    2471             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2472             :                                 xfs_btree_mark_sick(cur);
    2473             :                                 error = -EFSCORRUPTED;
    2474             :                                 goto done;
    2475             :                         }
    2476      199522 :                 }
    2477      199522 :                 break;
    2478           0 : 
    2479      199522 :         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2480           0 :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2481           0 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
    2482           0 :         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
    2483             :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2484             :         case BMAP_LEFT_CONTIG:
    2485      199522 :         case BMAP_RIGHT_CONTIG:
    2486           0 :                 /*
    2487      199522 :                  * These cases are all impossible.
    2488           0 :                  */
    2489           0 :                 ASSERT(0);
    2490           0 :         }
    2491             : 
    2492             :         /* update reverse mappings */
    2493             :         xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
    2494             : 
    2495           0 :         /* convert to a btree if necessary */
    2496             :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2497             :                 int     tmp_logflags;   /* partial log flag return val */
    2498             : 
    2499             :                 ASSERT(cur == NULL);
    2500             :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    2501             :                                 &tmp_logflags, whichfork);
    2502             :                 *logflagsp |= tmp_logflags;
    2503             :                 if (error)
    2504             :                         goto done;
    2505           0 :         }
    2506             : 
    2507             :         /* clear out the allocated field, done with it now in any case. */
    2508             :         if (cur) {
    2509    38474360 :                 cur->bc_ino.allocated = 0;
    2510             :                 *curp = cur;
    2511             :         }
    2512    38474531 : 
    2513      111303 :         xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
    2514             : done:
    2515      111303 :         *logflagsp |= rval;
    2516      111303 :         return error;
    2517             : #undef  LEFT
    2518      111303 : #undef  RIGHT
    2519      111303 : #undef  PREV
    2520           3 : }
    2521             : 
    2522             : /*
    2523             :  * Convert a hole to a delayed allocation.
    2524    38474584 :  */
    2525    14265829 : STATIC void
    2526    14265829 : xfs_bmap_add_extent_hole_delay(
    2527             :         xfs_inode_t             *ip,    /* incore inode pointer */
    2528             :         int                     whichfork,
    2529    38474584 :         struct xfs_iext_cursor  *icur,
    2530    38474567 :         xfs_bmbt_irec_t         *new)   /* new data to add to file extents */
    2531    38474567 : {
    2532    38474567 :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    2533             :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2534             :         xfs_filblks_t           newlen=0;       /* new indirect size */
    2535             :         xfs_filblks_t           oldlen=0;       /* old indirect size */
    2536             :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2537             :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2538             :         xfs_filblks_t           temp;    /* temp for indirect calculations */
    2539             : 
    2540             :         ifp = xfs_ifork_ptr(ip, whichfork);
    2541             :         ASSERT(isnullstartblock(new->br_startblock));
    2542    19820384 : 
    2543             :         /*
    2544             :          * Check and set flags if this segment has a left neighbor
    2545             :          */
    2546             :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2547             :                 state |= BMAP_LEFT_VALID;
    2548    19820384 :                 if (isnullstartblock(left.br_startblock))
    2549    19820384 :                         state |= BMAP_LEFT_DELAY;
    2550    19820384 :         }
    2551    19820384 : 
    2552    19820384 :         /*
    2553    19820384 :          * Check and set flags if the current (right) segment exists.
    2554    19820384 :          * If it doesn't exist, we're converting the hole at end-of-file.
    2555             :          */
    2556    19820384 :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2557    19819866 :                 state |= BMAP_RIGHT_VALID;
    2558             :                 if (isnullstartblock(right.br_startblock))
    2559             :                         state |= BMAP_RIGHT_DELAY;
    2560             :         }
    2561             : 
    2562    19819866 :         /*
    2563    13851236 :          * Set contiguity flags on the left and right neighbors.
    2564    13851236 :          * Don't let extents get too large, even if the pieces are contiguous.
    2565     2512335 :          */
    2566             :         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
    2567             :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2568             :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2569             :                 state |= BMAP_LEFT_CONTIG;
    2570             : 
    2571             :         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
    2572    19819141 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2573     9397757 :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2574     9397757 :             (!(state & BMAP_LEFT_CONTIG) ||
    2575     1389550 :              (left.br_blockcount + new->br_blockcount +
    2576             :               right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
    2577             :                 state |= BMAP_RIGHT_CONTIG;
    2578             : 
    2579             :         /*
    2580             :          * Switch out based on the contiguity flags.
    2581             :          */
    2582    19819360 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2583     2511668 :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2584     1894642 :                 /*
    2585     1894641 :                  * New allocation is contiguous with delayed allocations
    2586             :                  * on the left and on the right.
    2587    19819360 :                  * Merge all three into a single extent record.
    2588     1389550 :                  */
    2589       94054 :                 temp = left.br_blockcount + new->br_blockcount +
    2590       89758 :                         right.br_blockcount;
    2591       20966 : 
    2592             :                 oldlen = startblockval(left.br_startblock) +
    2593       89758 :                         startblockval(new->br_startblock) +
    2594             :                         startblockval(right.br_startblock);
    2595             :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2596             :                                          oldlen);
    2597             :                 left.br_startblock = nullstartblock(newlen);
    2598    19819360 :                 left.br_blockcount = temp;
    2599       20966 : 
    2600             :                 xfs_iext_remove(ip, icur, state);
    2601             :                 xfs_iext_prev(ifp, icur);
    2602             :                 xfs_iext_update_extent(ip, state, icur, &left);
    2603             :                 break;
    2604             : 
    2605       20966 :         case BMAP_LEFT_CONTIG:
    2606       20966 :                 /*
    2607             :                  * New allocation is contiguous with a delayed allocation
    2608       20966 :                  * on the left.
    2609       20966 :                  * Merge the new allocation with the left neighbor.
    2610       20966 :                  */
    2611       20966 :                 temp = left.br_blockcount + new->br_blockcount;
    2612             : 
    2613       20966 :                 oldlen = startblockval(left.br_startblock) +
    2614       20966 :                         startblockval(new->br_startblock);
    2615             :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2616       20966 :                                          oldlen);
    2617       20966 :                 left.br_blockcount = temp;
    2618       20966 :                 left.br_startblock = nullstartblock(newlen);
    2619       20966 : 
    2620             :                 xfs_iext_prev(ifp, icur);
    2621     1873675 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2622             :                 break;
    2623             : 
    2624             :         case BMAP_RIGHT_CONTIG:
    2625             :                 /*
    2626             :                  * New allocation is contiguous with a delayed allocation
    2627     1873675 :                  * on the right.
    2628             :                  * Merge the new allocation with the right neighbor.
    2629     1873675 :                  */
    2630     1873675 :                 temp = new->br_blockcount + right.br_blockcount;
    2631     1873675 :                 oldlen = startblockval(new->br_startblock) +
    2632             :                         startblockval(right.br_startblock);
    2633     1873673 :                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
    2634     1873673 :                                          oldlen);
    2635             :                 right.br_startoff = new->br_startoff;
    2636     1873673 :                 right.br_startblock = nullstartblock(newlen);
    2637     1873672 :                 right.br_blockcount = temp;
    2638     1873672 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2639             :                 break;
    2640       68792 : 
    2641             :         case 0:
    2642             :                 /*
    2643             :                  * New allocation is not contiguous with another
    2644             :                  * delayed allocation.
    2645             :                  * Insert a new entry.
    2646       68792 :                  */
    2647       68792 :                 oldlen = newlen = 0;
    2648       68792 :                 xfs_iext_insert(ip, icur, new, state);
    2649       68792 :                 break;
    2650             :         }
    2651       68792 :         if (oldlen != newlen) {
    2652       68792 :                 ASSERT(oldlen > newlen);
    2653       68792 :                 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
    2654       68792 :                                  false);
    2655       68792 :                 /*
    2656             :                  * Nothing to do for disk quota accounting here.
    2657    17855927 :                  */
    2658             :                 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
    2659             :         }
    2660             : }
    2661             : 
    2662             : /*
    2663    17855927 :  * Convert a hole to a real allocation.
    2664    17855927 :  */
    2665    17855927 : STATIC int                              /* error */
    2666             : xfs_bmap_add_extent_hole_real(
    2667    19818698 :         struct xfs_trans        *tp,
    2668     1963427 :         struct xfs_inode        *ip,
    2669     1963427 :         int                     whichfork,
    2670             :         struct xfs_iext_cursor  *icur,
    2671             :         struct xfs_btree_cur    **curp,
    2672             :         struct xfs_bmbt_irec    *new,
    2673             :         int                     *logflagsp,
    2674     1963427 :         uint32_t                flags)
    2675             : {
    2676    19818690 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    2677             :         struct xfs_mount        *mp = ip->i_mount;
    2678             :         struct xfs_btree_cur    *cur = *curp;
    2679             :         int                     error;  /* error return value */
    2680             :         int                     i;      /* temp state */
    2681             :         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
    2682   134404588 :         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
    2683             :         int                     rval=0; /* return value (logging flags) */
    2684             :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    2685             :         struct xfs_bmbt_irec    old;
    2686             : 
    2687             :         ASSERT(!isnullstartblock(new->br_startblock));
    2688             :         ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
    2689             : 
    2690             :         XFS_STATS_INC(mp, xs_add_exlist);
    2691             : 
    2692   134404588 :         /*
    2693   134405868 :          * Check and set flags if this segment has a left neighbor.
    2694   134405868 :          */
    2695   134405868 :         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
    2696   134405868 :                 state |= BMAP_LEFT_VALID;
    2697   134405868 :                 if (isnullstartblock(left.br_startblock))
    2698   134405868 :                         state |= BMAP_LEFT_DELAY;
    2699   134405868 :         }
    2700   134405868 : 
    2701   134405868 :         /*
    2702             :          * Check and set flags if this segment has a current value.
    2703   134405868 :          * Not true if we're inserting into the "hole" at eof.
    2704   134405868 :          */
    2705             :         if (xfs_iext_get_extent(ifp, icur, &right)) {
    2706   134405868 :                 state |= BMAP_RIGHT_VALID;
    2707             :                 if (isnullstartblock(right.br_startblock))
    2708             :                         state |= BMAP_RIGHT_DELAY;
    2709             :         }
    2710             : 
    2711   134405868 :         /*
    2712    97583072 :          * We're inserting a real allocation between "left" and "right".
    2713    97583072 :          * Set the contiguity flags.  Don't let extents get too large.
    2714      145054 :          */
    2715             :         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
    2716             :             left.br_startoff + left.br_blockcount == new->br_startoff &&
    2717             :             left.br_startblock + left.br_blockcount == new->br_startblock &&
    2718             :             left.br_state == new->br_state &&
    2719             :             left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
    2720             :                 state |= BMAP_LEFT_CONTIG;
    2721   134406290 : 
    2722    32911285 :         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
    2723    32911285 :             new->br_startoff + new->br_blockcount == right.br_startoff &&
    2724      216486 :             new->br_startblock + new->br_blockcount == right.br_startblock &&
    2725             :             new->br_state == right.br_state &&
    2726             :             new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
    2727             :             (!(state & BMAP_LEFT_CONTIG) ||
    2728             :              left.br_blockcount + new->br_blockcount +
    2729             :              right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
    2730             :                 state |= BMAP_RIGHT_CONTIG;
    2731   134405703 : 
    2732    97438013 :         error = 0;
    2733    67691948 :         /*
    2734     6572655 :          * Select which case we're in here, and implement it.
    2735     6150324 :          */
    2736     6150308 :         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
    2737             :         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
    2738   134405703 :                 /*
    2739    32694793 :                  * New allocation is contiguous with real allocations on the
    2740     6985617 :                  * left and on the right.
    2741      612241 :                  * Merge all three into a single extent record.
    2742      543691 :                  */
    2743      543691 :                 left.br_blockcount += new->br_blockcount + right.br_blockcount;
    2744      349534 : 
    2745             :                 xfs_iext_remove(ip, icur, state);
    2746      543691 :                 xfs_iext_prev(ifp, icur);
    2747             :                 xfs_iext_update_extent(ip, state, icur, &left);
    2748   134405703 :                 ifp->if_nextents--;
    2749             : 
    2750             :                 if (cur == NULL) {
    2751             :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2752   134405703 :                 } else {
    2753      349534 :                         rval = XFS_ILOG_CORE;
    2754             :                         error = xfs_bmbt_lookup_eq(cur, &right, &i);
    2755             :                         if (error)
    2756             :                                 goto done;
    2757             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2758             :                                 xfs_btree_mark_sick(cur);
    2759      349534 :                                 error = -EFSCORRUPTED;
    2760             :                                 goto done;
    2761      349534 :                         }
    2762      349534 :                         error = xfs_btree_delete(cur, &i);
    2763      349534 :                         if (error)
    2764      349534 :                                 goto done;
    2765             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2766      349534 :                                 xfs_btree_mark_sick(cur);
    2767       60403 :                                 error = -EFSCORRUPTED;
    2768             :                                 goto done;
    2769      289280 :                         }
    2770      289280 :                         error = xfs_btree_decrement(cur, 0, &i);
    2771      289280 :                         if (error)
    2772           0 :                                 goto done;
    2773      289280 :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2774           0 :                                 xfs_btree_mark_sick(cur);
    2775           0 :                                 error = -EFSCORRUPTED;
    2776           0 :                                 goto done;
    2777             :                         }
    2778      289280 :                         error = xfs_bmbt_update(cur, &left);
    2779      289280 :                         if (error)
    2780           0 :                                 goto done;
    2781      289280 :                 }
    2782           0 :                 break;
    2783           0 : 
    2784           0 :         case BMAP_LEFT_CONTIG:
    2785             :                 /*
    2786      289280 :                  * New allocation is contiguous with a real allocation
    2787      289280 :                  * on the left.
    2788           0 :                  * Merge the new allocation with the left neighbor.
    2789      289280 :                  */
    2790           0 :                 old = left;
    2791           0 :                 left.br_blockcount += new->br_blockcount;
    2792           0 : 
    2793             :                 xfs_iext_prev(ifp, icur);
    2794      289280 :                 xfs_iext_update_extent(ip, state, icur, &left);
    2795      289280 : 
    2796           0 :                 if (cur == NULL) {
    2797             :                         rval = xfs_ilog_fext(whichfork);
    2798             :                 } else {
    2799             :                         rval = 0;
    2800     5800774 :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2801             :                         if (error)
    2802             :                                 goto done;
    2803             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2804             :                                 xfs_btree_mark_sick(cur);
    2805             :                                 error = -EFSCORRUPTED;
    2806     5800774 :                                 goto done;
    2807     5800774 :                         }
    2808             :                         error = xfs_bmbt_update(cur, &left);
    2809     5800774 :                         if (error)
    2810     5800774 :                                 goto done;
    2811             :                 }
    2812     5800774 :                 break;
    2813     4076989 : 
    2814             :         case BMAP_RIGHT_CONTIG:
    2815     1723785 :                 /*
    2816     1723785 :                  * New allocation is contiguous with a real allocation
    2817     1723785 :                  * on the right.
    2818           0 :                  * Merge the new allocation with the right neighbor.
    2819     1723785 :                  */
    2820           0 :                 old = right;
    2821           0 : 
    2822           0 :                 right.br_startoff = new->br_startoff;
    2823             :                 right.br_startblock = new->br_startblock;
    2824     1723785 :                 right.br_blockcount += new->br_blockcount;
    2825     1723785 :                 xfs_iext_update_extent(ip, state, icur, &right);
    2826           0 : 
    2827             :                 if (cur == NULL) {
    2828             :                         rval = xfs_ilog_fext(whichfork);
    2829             :                 } else {
    2830      194157 :                         rval = 0;
    2831             :                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
    2832             :                         if (error)
    2833             :                                 goto done;
    2834             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2835             :                                 xfs_btree_mark_sick(cur);
    2836      194157 :                                 error = -EFSCORRUPTED;
    2837             :                                 goto done;
    2838      194157 :                         }
    2839      194157 :                         error = xfs_bmbt_update(cur, &right);
    2840      194157 :                         if (error)
    2841      194157 :                                 goto done;
    2842             :                 }
    2843      194157 :                 break;
    2844       47951 : 
    2845             :         case 0:
    2846      146206 :                 /*
    2847      146206 :                  * New allocation is not contiguous with another
    2848      146206 :                  * real allocation.
    2849           0 :                  * Insert a new entry.
    2850      146206 :                  */
    2851           0 :                 xfs_iext_insert(ip, icur, new, state);
    2852           0 :                 ifp->if_nextents++;
    2853           0 : 
    2854             :                 if (cur == NULL) {
    2855      146206 :                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
    2856      146206 :                 } else {
    2857           0 :                         rval = XFS_ILOG_CORE;
    2858             :                         error = xfs_bmbt_lookup_eq(cur, new, &i);
    2859             :                         if (error)
    2860             :                                 goto done;
    2861   128061238 :                         if (XFS_IS_CORRUPT(mp, i != 0)) {
    2862             :                                 xfs_btree_mark_sick(cur);
    2863             :                                 error = -EFSCORRUPTED;
    2864             :                                 goto done;
    2865             :                         }
    2866             :                         error = xfs_btree_insert(cur, &i);
    2867   128061238 :                         if (error)
    2868   128061069 :                                 goto done;
    2869             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2870   128061069 :                                 xfs_btree_mark_sick(cur);
    2871    61041623 :                                 error = -EFSCORRUPTED;
    2872             :                                 goto done;
    2873    76374308 :                         }
    2874    76374308 :                 }
    2875    76374360 :                 break;
    2876        1107 :         }
    2877    76373253 : 
    2878           0 :         /* add reverse mapping unless caller opted out */
    2879           0 :         if (!(flags & XFS_BMAPI_NORMAP))
    2880           0 :                 xfs_rmap_map_extent(tp, ip, whichfork, new);
    2881             : 
    2882    76373253 :         /* convert to a btree if necessary */
    2883    76373211 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    2884         138 :                 int     tmp_logflags;   /* partial log flag return val */
    2885    76373073 : 
    2886           0 :                 ASSERT(cur == NULL);
    2887           0 :                 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
    2888           0 :                                 &tmp_logflags, whichfork);
    2889             :                 *logflagsp |= tmp_logflags;
    2890             :                 cur = *curp;
    2891             :                 if (error)
    2892             :                         goto done;
    2893             :         }
    2894             : 
    2895   134404299 :         /* clear out the allocated field, done with it now in any case. */
    2896   134403407 :         if (cur)
    2897             :                 cur->bc_ino.allocated = 0;
    2898             : 
    2899   134404781 :         xfs_bmap_check_leaf_extents(cur, ip, whichfork);
    2900      982852 : done:
    2901             :         *logflagsp |= rval;
    2902      982852 :         return error;
    2903      982852 : }
    2904             : 
    2905      982852 : /*
    2906      982852 :  * Functions used in the extent read, allocate and remove paths
    2907      982852 :  */
    2908           5 : 
    2909             : /*
    2910             :  * Adjust the size of the new extent based on i_extsize and rt extsize.
    2911             :  */
    2912   134403456 : int
    2913    79515174 : xfs_bmap_extsize_align(
    2914             :         xfs_mount_t     *mp,
    2915   134403456 :         xfs_bmbt_irec_t *gotp,          /* next extent pointer */
    2916   134404610 :         xfs_bmbt_irec_t *prevp,         /* previous extent pointer */
    2917   134404610 :         xfs_extlen_t    extsz,          /* align to this extent size */
    2918   134404610 :         int             rt,             /* is this a realtime inode? */
    2919             :         int             eof,            /* is extent at end-of-file? */
    2920             :         int             delay,          /* creating delalloc extent? */
    2921             :         int             convert,        /* overwriting unwritten extent? */
    2922             :         xfs_fileoff_t   *offp,          /* in/out: aligned offset */
    2923             :         xfs_extlen_t    *lenp)          /* in/out: aligned length */
    2924             : {
    2925             :         xfs_fileoff_t   orig_off;       /* original offset */
    2926             :         xfs_extlen_t    orig_alen;      /* original length */
    2927             :         xfs_fileoff_t   orig_end;       /* original off+len */
    2928             :         xfs_fileoff_t   nexto;          /* next file offset */
    2929    27388502 :         xfs_fileoff_t   prevo;          /* previous file offset */
    2930             :         xfs_fileoff_t   align_off;      /* temp for offset */
    2931             :         xfs_extlen_t    align_alen;     /* temp for length */
    2932             :         xfs_extlen_t    temp;           /* temp for calculations */
    2933             : 
    2934             :         if (convert)
    2935             :                 return 0;
    2936             : 
    2937             :         orig_off = align_off = *offp;
    2938             :         orig_alen = align_alen = *lenp;
    2939             :         orig_end = orig_off + orig_alen;
    2940             : 
    2941    27388502 :         /*
    2942    27388502 :          * If this request overlaps an existing extent, then don't
    2943    27388502 :          * attempt to perform any additional alignment.
    2944    27388502 :          */
    2945    27388502 :         if (!delay && !eof &&
    2946    27388502 :             (orig_off >= gotp->br_startoff) &&
    2947    27388502 :             (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
    2948    27388502 :                 return 0;
    2949             :         }
    2950    27388502 : 
    2951             :         /*
    2952             :          * If the file offset is unaligned vs. the extent size
    2953    27388502 :          * we need to align it.  This will be possible unless
    2954    27388502 :          * the file was previously written with a kernel that didn't
    2955    27388502 :          * perform this alignment, or if a truncate shot us in the
    2956             :          * foot.
    2957             :          */
    2958             :         div_u64_rem(orig_off, extsz, &temp);
    2959             :         if (temp) {
    2960             :                 align_alen += temp;
    2961    27388502 :                 align_off -= temp;
    2962    14222341 :         }
    2963      126975 : 
    2964             :         /* Same adjustment for the end of the requested area. */
    2965             :         temp = (align_alen % extsz);
    2966             :         if (temp)
    2967             :                 align_alen += extsz - temp;
    2968             : 
    2969             :         /*
    2970             :          * For large extent hint sizes, the aligned extent might be larger than
    2971             :          * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
    2972             :          * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
    2973             :          * allocation loops handle short allocation just fine, so it is safe to
    2974    27261527 :          * do this. We only want to do it when we are forced to, though, because
    2975    27261527 :          * it means more allocation operations are required.
    2976      608115 :          */
    2977      608115 :         while (align_alen > XFS_MAX_BMBT_EXTLEN)
    2978             :                 align_alen -= extsz;
    2979             :         ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
    2980             : 
    2981    27261527 :         /*
    2982    27261527 :          * If the previous block overlaps with this proposed allocation
    2983      651708 :          * then move the start forward without adjusting the length.
    2984             :          */
    2985             :         if (prevp->br_startoff != NULLFILEOFF) {
    2986             :                 if (prevp->br_startblock == HOLESTARTBLOCK)
    2987             :                         prevo = prevp->br_startoff;
    2988             :                 else
    2989             :                         prevo = prevp->br_startoff + prevp->br_blockcount;
    2990             :         } else
    2991             :                 prevo = 0;
    2992             :         if (align_off != orig_off && align_off < prevo)
    2993    27261530 :                 align_off = prevo;
    2994           3 :         /*
    2995    27261527 :          * If the next block overlaps with this proposed allocation
    2996             :          * then move the start back without adjusting the length,
    2997             :          * but not before offset 0.
    2998             :          * This may of course make the start overlap previous block,
    2999             :          * and if we hit the offset 0 limit then the next block
    3000             :          * can still overlap too.
    3001    27261527 :          */
    3002    24432905 :         if (!eof && gotp->br_startoff != NULLFILEOFF) {
    3003             :                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
    3004             :                     (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
    3005    24432905 :                         nexto = gotp->br_startoff + gotp->br_blockcount;
    3006             :                 else
    3007             :                         nexto = gotp->br_startoff;
    3008    27261527 :         } else
    3009        6742 :                 nexto = NULLFILEOFF;
    3010             :         if (!eof &&
    3011             :             align_off + align_alen != orig_end &&
    3012             :             align_off + align_alen > nexto)
    3013             :                 align_off = nexto > align_alen ? nexto - align_alen : 0;
    3014             :         /*
    3015             :          * If we're now overlapping the next or previous extent that
    3016             :          * means we can't fit an extsz piece in this hole.  Just move
    3017             :          * the start forward to the first valid spot and set
    3018    27261527 :          * the length so we hit the end.
    3019    14151067 :          */
    3020    14095447 :         if (align_off != orig_off && align_off < prevo)
    3021           0 :                 align_off = prevo;
    3022             :         if (align_off + align_alen != orig_end &&
    3023             :             align_off + align_alen > nexto &&
    3024             :             nexto != NULLFILEOFF) {
    3025             :                 ASSERT(nexto > prevo);
    3026    27261527 :                 align_alen = nexto - align_off;
    3027    14151036 :         }
    3028             : 
    3029        8545 :         /*
    3030             :          * If realtime, and the result isn't a multiple of the realtime
    3031             :          * extent size we need to remove blocks until it is.
    3032             :          */
    3033             :         if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
    3034             :                 /*
    3035             :                  * We're not covering the original request, or
    3036    27261527 :                  * we won't be able to once we fix the length.
    3037        3065 :                  */
    3038    27261527 :                 if (orig_off < align_off ||
    3039             :                     orig_end > align_off + align_alen ||
    3040             :                     align_alen - temp < orig_alen)
    3041        3848 :                         return -EINVAL;
    3042        3848 :                 /*
    3043             :                  * Try to fix it by moving the start up.
    3044             :                  */
    3045             :                 if (align_off + temp <= orig_off) {
    3046             :                         align_alen -= temp;
    3047             :                         align_off += temp;
    3048             :                 }
    3049    51027866 :                 /*
    3050             :                  * Try to fix it by moving the end in.
    3051             :                  */
    3052             :                 else if (align_off + align_alen - temp >= orig_end)
    3053             :                         align_alen -= temp;
    3054           0 :                 /*
    3055           0 :                  * Set the start to the minimum then trim the length.
    3056           0 :                  */
    3057             :                 else {
    3058             :                         align_alen -= orig_off - align_off;
    3059             :                         align_off = orig_off;
    3060             :                         align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
    3061           0 :                 }
    3062             :                 /*
    3063             :                  * Result doesn't cover the request, fail it.
    3064             :                  */
    3065             :                 if (orig_off < align_off || orig_end > align_off + align_alen)
    3066             :                         return -EINVAL;
    3067             :         } else {
    3068           0 :                 ASSERT(orig_off >= align_off);
    3069             :                 /* see XFS_BMBT_MAX_EXTLEN handling above */
    3070             :                 ASSERT(orig_end <= align_off + align_alen ||
    3071             :                        align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
    3072             :         }
    3073             : 
    3074           0 : #ifdef DEBUG
    3075           0 :         if (!eof && gotp->br_startoff != NULLFILEOFF)
    3076           0 :                 ASSERT(align_off + align_alen <= gotp->br_startoff);
    3077             :         if (prevp->br_startoff != NULLFILEOFF)
    3078             :                 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
    3079             : #endif
    3080             : 
    3081           0 :         *lenp = align_alen;
    3082             :         *offp = align_off;
    3083             :         return 0;
    3084    27261527 : }
    3085             : 
    3086    27261527 : #define XFS_ALLOC_GAP_UNITS     4
    3087             : 
    3088             : void
    3089             : xfs_bmap_adjacent(
    3090             :         struct xfs_bmalloca     *ap)    /* bmap alloc argument struct */
    3091    27261527 : {
    3092    14151090 :         xfs_fsblock_t   adjust;         /* adjustment to block numbers */
    3093    27261527 :         xfs_mount_t     *mp;            /* mount point structure */
    3094    24433148 :         int             rt;             /* true if inode is realtime */
    3095             : 
    3096             : #define ISVALID(x,y)    \
    3097    27261527 :         (rt ? \
    3098    27261527 :                 (x) < mp->m_sb.sb_rblocks : \
    3099    27261527 :                 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
    3100             :                 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
    3101             :                 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
    3102             : 
    3103             :         mp = ap->ip->i_mount;
    3104             :         rt = XFS_IS_REALTIME_INODE(ap->ip) &&
    3105    84215419 :                 (ap->datatype & XFS_ALLOC_USERDATA);
    3106             :         /*
    3107             :          * If allocating at eof, and there's a previous real block,
    3108    84215419 :          * try to use its last block as our starting point.
    3109    84215419 :          */
    3110    84215419 :         if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
    3111             :             !isnullstartblock(ap->prev.br_startblock) &&
    3112             :             ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
    3113             :                     ap->prev.br_startblock)) {
    3114             :                 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
    3115             :                 /*
    3116             :                  * Adjust for the gap between prevp and us.
    3117             :                  */
    3118             :                 adjust = ap->offset -
    3119    84215419 :                         (ap->prev.br_startoff + ap->prev.br_blockcount);
    3120    84215419 :                 if (adjust &&
    3121    27445283 :                     ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
    3122             :                         ap->blkno += adjust;
    3123             :         }
    3124             :         /*
    3125             :          * If not at eof, then compare the two neighbor blocks.
    3126    84215419 :          * Figure out whether either one gives us a good starting point,
    3127    17597699 :          * and pick the better one.
    3128    12822819 :          */
    3129             :         else if (!ap->eof) {
    3130    12822578 :                 xfs_fsblock_t   gotbno;         /* right side block number */
    3131             :                 xfs_fsblock_t   gotdiff=0;      /* right side difference */
    3132             :                 xfs_fsblock_t   prevbno;        /* left side block number */
    3133             :                 xfs_fsblock_t   prevdiff=0;     /* left side difference */
    3134    12822578 : 
    3135    12822578 :                 /*
    3136    16942618 :                  * If there's a previous (left) block, select a requested
    3137     4120040 :                  * start block based on it.
    3138     4108878 :                  */
    3139             :                 if (ap->prev.br_startoff != NULLFILEOFF &&
    3140             :                     !isnullstartblock(ap->prev.br_startblock) &&
    3141             :                     (prevbno = ap->prev.br_startblock +
    3142             :                                ap->prev.br_blockcount) &&
    3143             :                     ISVALID(prevbno, ap->prev.br_startblock)) {
    3144             :                         /*
    3145    71392841 :                          * Calculate gap to end of previous block.
    3146    37375092 :                          */
    3147    37375092 :                         adjust = prevdiff = ap->offset -
    3148    37375092 :                                 (ap->prev.br_startoff +
    3149    37375092 :                                  ap->prev.br_blockcount);
    3150             :                         /*
    3151             :                          * Figure the startblock based on the previous block's
    3152             :                          * end and the gap size.
    3153             :                          * Heuristic!
    3154             :                          * If the gap is large relative to the piece we're
    3155    68423069 :                          * allocating, or using it gives us an invalid block
    3156    31161417 :                          * number, then just use the end of the previous block.
    3157    31047966 :                          */
    3158    49141809 :                         if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3159    31047977 :                             ISVALID(prevbno + prevdiff,
    3160             :                                     ap->prev.br_startblock))
    3161             :                                 prevbno += adjust;
    3162             :                         else
    3163    31047821 :                                 prevdiff += adjust;
    3164    31047821 :                 }
    3165             :                 /*
    3166             :                  * No previous block or can't follow it, just default.
    3167             :                  */
    3168             :                 else
    3169             :                         prevbno = NULLFSBLOCK;
    3170             :                 /*
    3171             :                  * If there's a following (right) block, select a requested
    3172             :                  * start block based on it.
    3173             :                  */
    3174    35060561 :                 if (!isnullstartblock(ap->got.br_startblock)) {
    3175    14738078 :                         /*
    3176             :                          * Calculate gap to start of next block.
    3177    14737967 :                          */
    3178             :                         adjust = gotdiff = ap->got.br_startoff - ap->offset;
    3179    16309854 :                         /*
    3180             :                          * Figure the startblock based on the next block's
    3181             :                          * start and the gap size.
    3182             :                          */
    3183             :                         gotbno = ap->got.br_startblock;
    3184             :                         /*
    3185             :                          * Heuristic!
    3186             :                          * If the gap is large relative to the piece we're
    3187             :                          * allocating, or using it gives us an invalid block
    3188             :                          * number, then just use the start of the next block
    3189             :                          * offset by our length.
    3190    37375092 :                          */
    3191             :                         if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
    3192             :                             ISVALID(gotbno - gotdiff, gotbno))
    3193             :                                 gotbno -= adjust;
    3194    20772338 :                         else if (ISVALID(gotbno - ap->length, gotbno)) {
    3195             :                                 gotbno -= ap->length;
    3196             :                                 gotdiff += adjust - ap->length;
    3197             :                         } else
    3198             :                                 gotdiff += adjust;
    3199    20772338 :                 }
    3200             :                 /*
    3201             :                  * No next block, just default.
    3202             :                  */
    3203             :                 else
    3204             :                         gotbno = NULLFSBLOCK;
    3205             :                 /*
    3206             :                  * If both valid, pick the better one, else the only good
    3207    22674891 :                  * one, else ap->blkno is already set (to 0 or the inode block).
    3208     9272051 :                  */
    3209     9266757 :                 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
    3210    11505581 :                         ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
    3211    11502183 :                 else if (prevbno != NULLFSBLOCK)
    3212    11502183 :                         ap->blkno = prevbno;
    3213             :                 else if (gotbno != NULLFSBLOCK)
    3214        3398 :                         ap->blkno = gotbno;
    3215             :         }
    3216             : #undef ISVALID
    3217             : }
    3218             : 
    3219             : int
    3220             : xfs_bmap_longest_free_extent(
    3221             :         struct xfs_perag        *pag,
    3222             :         struct xfs_trans        *tp,
    3223             :         xfs_extlen_t            *blen)
    3224             : {
    3225    37375092 :         xfs_extlen_t            longest;
    3226    23900945 :         int                     error = 0;
    3227    18007006 : 
    3228    11679816 :         if (!xfs_perag_initialised_agf(pag)) {
    3229     6327190 :                 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
    3230     1404209 :                                 NULL);
    3231             :                 if (error)
    3232             :                         return error;
    3233    84215419 :         }
    3234             : 
    3235             :         longest = xfs_alloc_longest_free_extent(pag,
    3236    60815497 :                                 xfs_alloc_min_freelist(pag->pag_mount, pag),
    3237             :                                 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
    3238             :         if (*blen < longest)
    3239             :                 *blen = longest;
    3240             : 
    3241    60815497 :         return 0;
    3242    60815497 : }
    3243             : 
    3244   121630994 : static xfs_extlen_t
    3245        1527 : xfs_bmap_select_minlen(
    3246             :         struct xfs_bmalloca     *ap,
    3247        1527 :         struct xfs_alloc_arg    *args,
    3248             :         xfs_extlen_t            blen)
    3249             : {
    3250             : 
    3251    60813987 :         /*
    3252             :          * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
    3253             :          * possible that there is enough contiguous free space for this request.
    3254    60814415 :          */
    3255    60652076 :         if (blen < ap->minlen)
    3256             :                 return ap->minlen;
    3257             : 
    3258             :         /*
    3259             :          * If the best seen length is less than the request length,
    3260             :          * use the best as the minimum, otherwise we've got the maxlen we
    3261             :          * were asked for.
    3262             :          */
    3263             :         if (blen < args->maxlen)
    3264             :                 return blen;
    3265             :         return args->maxlen;
    3266             : }
    3267             : 
    3268             : static int
    3269             : xfs_bmap_btalloc_select_lengths(
    3270             :         struct xfs_bmalloca     *ap,
    3271    60448503 :         struct xfs_alloc_arg    *args,
    3272             :         xfs_extlen_t            *blen)
    3273             : {
    3274             :         struct xfs_mount        *mp = args->mp;
    3275             :         struct xfs_perag        *pag;
    3276             :         xfs_agnumber_t          agno, startag;
    3277             :         int                     error = 0;
    3278             : 
    3279    60448464 :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3280             :                 args->total = ap->minlen;
    3281             :                 args->minlen = ap->minlen;
    3282             :                 return 0;
    3283             :         }
    3284             : 
    3285    60436942 :         args->total = ap->total;
    3286             :         startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
    3287             :         if (startag == NULLAGNUMBER)
    3288             :                 startag = 0;
    3289             : 
    3290    60436942 :         *blen = 0;
    3291    60436942 :         for_each_perag_wrap(mp, startag, agno, pag) {
    3292    60436942 :                 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
    3293    60436942 :                 if (error && error != -EAGAIN)
    3294             :                         break;
    3295    60436942 :                 error = 0;
    3296         122 :                 if (*blen >= args->maxlen)
    3297         122 :                         break;
    3298         122 :         }
    3299             :         if (pag)
    3300             :                 xfs_perag_rele(pag);
    3301    60436820 : 
    3302    60436820 :         args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
    3303    60436820 :         return error;
    3304           0 : }
    3305             : 
    3306    60436820 : /* Update all inode and quota accounting for the allocation we just did. */
    3307    60803466 : static void
    3308    60729076 : xfs_bmap_btalloc_accounting(
    3309    60728814 :         struct xfs_bmalloca     *ap,
    3310             :         struct xfs_alloc_arg    *args)
    3311    60728814 : {
    3312    60728814 :         if (ap->flags & XFS_BMAPI_COWFORK) {
    3313             :                 /*
    3314             :                  * COW fork blocks are in-core only and thus are treated as
    3315    60436293 :                  * in-core quota reservation (like delalloc blocks) even when
    3316    60362930 :                  * converted to real blocks. The quota reservation is not
    3317             :                  * accounted to disk until blocks are remapped to the data
    3318    60437279 :                  * fork. So if these blocks were previously delalloc, we
    3319    60437279 :                  * already have quota reservation and there's nothing to do
    3320             :                  * yet.
    3321             :                  */
    3322             :                 if (ap->wasdel) {
    3323             :                         xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3324    60810397 :                         return;
    3325             :                 }
    3326             : 
    3327             :                 /*
    3328    60810397 :                  * Otherwise, we've allocated blocks in a hole. The transaction
    3329             :                  * has acquired in-core quota reservation for this extent.
    3330             :                  * Rather than account these as real blocks, however, we reduce
    3331             :                  * the transaction quota reservation based on the allocation.
    3332             :                  * This essentially transfers the transaction quota reservation
    3333             :                  * to that of a delalloc extent.
    3334             :                  */
    3335             :                 ap->ip->i_delayed_blks += args->len;
    3336             :                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
    3337             :                                 -(long)args->len);
    3338      352816 :                 return;
    3339      126932 :         }
    3340      126932 : 
    3341             :         /* data/attr fork only */
    3342             :         ap->ip->i_nblocks += args->len;
    3343             :         xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
    3344             :         if (ap->wasdel) {
    3345             :                 ap->ip->i_delayed_blks -= args->len;
    3346             :                 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
    3347             :         }
    3348             :         xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
    3349             :                 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
    3350             :                 args->len);
    3351      225884 : }
    3352      225884 : 
    3353      225884 : static int
    3354      225884 : xfs_bmap_compute_alignments(
    3355             :         struct xfs_bmalloca     *ap,
    3356             :         struct xfs_alloc_arg    *args)
    3357             : {
    3358    60457581 :         struct xfs_mount        *mp = args->mp;
    3359    60457581 :         xfs_extlen_t            align = 0; /* minimum allocation alignment */
    3360    60458225 :         int                     stripe_align = 0;
    3361    16536661 : 
    3362    16536661 :         /* stripe alignment for allocation is determined by mount parameters */
    3363             :         if (mp->m_swidth && xfs_has_swalloc(mp))
    3364   120916456 :                 stripe_align = mp->m_swidth;
    3365    60458228 :         else if (mp->m_dalign)
    3366    60458228 :                 stripe_align = mp->m_dalign;
    3367             : 
    3368             :         if (ap->flags & XFS_BMAPI_COWFORK)
    3369             :                 align = xfs_get_cowextsz_hint(ap->ip);
    3370    60812239 :         else if (ap->datatype & XFS_ALLOC_USERDATA)
    3371             :                 align = xfs_get_extsz_hint(ap->ip);
    3372             :         if (align) {
    3373             :                 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
    3374    60812239 :                                         ap->eof, 0, ap->conv, &ap->offset,
    3375    60812239 :                                         &ap->length))
    3376    60812239 :                         ASSERT(0);
    3377             :                 ASSERT(ap->length);
    3378             :         }
    3379    60812239 : 
    3380             :         /* apply extent size hints if obtained earlier */
    3381    60812238 :         if (align) {
    3382       40471 :                 args->prod = align;
    3383             :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3384    60812239 :                 if (args->mod)
    3385      352861 :                         args->mod = args->prod - args->mod;
    3386    60459378 :         } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
    3387    25416933 :                 args->prod = 1;
    3388    25769876 :                 args->mod = 0;
    3389     3450067 :         } else {
    3390     3450068 :                 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
    3391             :                 div_u64_rem(ap->offset, args->prod, &args->mod);
    3392           0 :                 if (args->mod)
    3393     3450067 :                         args->mod = args->prod - args->mod;
    3394             :         }
    3395             : 
    3396             :         return stripe_align;
    3397    60812320 : }
    3398     3450073 : 
    3399     3450073 : static void
    3400     3450073 : xfs_bmap_process_allocated_extent(
    3401        6299 :         struct xfs_bmalloca     *ap,
    3402    57362247 :         struct xfs_alloc_arg    *args,
    3403         322 :         xfs_fileoff_t           orig_offset,
    3404         322 :         xfs_extlen_t            orig_length)
    3405             : {
    3406    57361925 :         ap->blkno = args->fsbno;
    3407    57361925 :         ap->length = args->len;
    3408    57361925 :         /*
    3409    21558189 :          * If the extent size hint is active, we tried to round the
    3410             :          * caller's allocation request offset down to extsz and the
    3411             :          * length up to another extsz boundary.  If we found a free
    3412    60812320 :          * extent we mapped it in starting at this new offset.  If the
    3413             :          * newly mapped space isn't long enough to cover any of the
    3414             :          * range of offsets that was originally requested, move the
    3415             :          * mapping up so that we can fill as much of the caller's
    3416    60810054 :          * original request as possible.  Free space is apparently
    3417             :          * very fragmented so we're unlikely to be able to satisfy the
    3418             :          * hints anyway.
    3419             :          */
    3420             :         if (ap->length <= orig_length)
    3421             :                 ap->offset = orig_offset;
    3422    60810054 :         else if (ap->offset + ap->length < orig_offset + orig_length)
    3423    60810054 :                 ap->offset = orig_offset + orig_length - ap->length;
    3424             :         xfs_bmap_btalloc_accounting(ap, args);
    3425             : }
    3426             : 
    3427             : #ifdef DEBUG
    3428             : static int
    3429             : xfs_bmap_exact_minlen_extent_alloc(
    3430             :         struct xfs_bmalloca     *ap)
    3431             : {
    3432             :         struct xfs_mount        *mp = ap->ip->i_mount;
    3433             :         struct xfs_alloc_arg    args = { .tp = ap->tp, .mp = mp };
    3434             :         xfs_fileoff_t           orig_offset;
    3435             :         xfs_extlen_t            orig_length;
    3436    60810054 :         int                     error;
    3437    60568569 : 
    3438      241485 :         ASSERT(ap->length);
    3439           0 : 
    3440    60810054 :         if (ap->minlen != 1) {
    3441    60811375 :                 ap->blkno = NULLFSBLOCK;
    3442             :                 ap->length = 0;
    3443             :                 return 0;
    3444             :         }
    3445      364215 : 
    3446             :         orig_offset = ap->offset;
    3447             :         orig_length = ap->length;
    3448      364215 : 
    3449      364215 :         args.alloc_minlen_only = 1;
    3450      364215 : 
    3451      364215 :         xfs_bmap_compute_alignments(ap, &args);
    3452      364215 : 
    3453             :         /*
    3454      364215 :          * Unlike the longest extent available in an AG, we don't track
    3455             :          * the length of an AG's shortest extent.
    3456      364215 :          * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
    3457           0 :          * hence we can afford to start traversing from the 0th AG since
    3458           0 :          * we need not be concerned about a drop in performance in
    3459           0 :          * "debug only" code paths.
    3460             :          */
    3461             :         ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
    3462      364215 : 
    3463      364215 :         args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
    3464             :         args.minlen = args.maxlen = ap->minlen;
    3465      364215 :         args.total = ap->total;
    3466             : 
    3467      364215 :         args.alignment = 1;
    3468             :         args.minalignslop = 0;
    3469             : 
    3470             :         args.minleft = ap->minleft;
    3471             :         args.wasdel = ap->wasdel;
    3472             :         args.resv = XFS_AG_RESV_NONE;
    3473             :         args.datatype = ap->datatype;
    3474             : 
    3475             :         error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
    3476             :         if (error)
    3477      364215 :                 return error;
    3478             : 
    3479      364215 :         if (args.fsbno != NULLFSBLOCK) {
    3480      364215 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3481      364215 :                         orig_length);
    3482             :         } else {
    3483      364215 :                 ap->blkno = NULLFSBLOCK;
    3484      364215 :                 ap->length = 0;
    3485             :         }
    3486      364215 : 
    3487      364215 :         return 0;
    3488      364215 : }
    3489      364215 : #else
    3490             : 
    3491      364215 : #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
    3492      364224 : 
    3493             : #endif
    3494             : 
    3495      364224 : /*
    3496      364224 :  * If we are not low on available data blocks and we are allocating at
    3497             :  * EOF, optimise allocation for contiguous file extension and/or stripe
    3498             :  * alignment of the new extent.
    3499           0 :  *
    3500           0 :  * NOTE: ap->aeof is only set if the allocation length is >= the
    3501             :  * stripe unit and the allocation offset is at the end of file.
    3502             :  */
    3503             : static int
    3504             : xfs_bmap_btalloc_at_eof(
    3505             :         struct xfs_bmalloca     *ap,
    3506             :         struct xfs_alloc_arg    *args,
    3507             :         xfs_extlen_t            blen,
    3508             :         int                     stripe_align,
    3509             :         bool                    ag_only)
    3510             : {
    3511             :         struct xfs_mount        *mp = args->mp;
    3512             :         struct xfs_perag        *caller_pag = args->pag;
    3513             :         int                     error;
    3514             : 
    3515             :         /*
    3516             :          * If there are already extents in the file, try an exact EOF block
    3517             :          * allocation to extend the file as a contiguous extent. If that fails,
    3518             :          * or it's the first allocation in a file, just try for a stripe aligned
    3519             :          * allocation.
    3520        3782 :          */
    3521             :         if (ap->offset) {
    3522             :                 xfs_extlen_t    nextminlen = 0;
    3523             : 
    3524             :                 /*
    3525             :                  * Compute the minlen+alignment for the next case.  Set slop so
    3526             :                  * that the value of minlen+alignment+slop doesn't go up between
    3527        3782 :                  * the calls.
    3528        3782 :                  */
    3529        3782 :                 args->alignment = 1;
    3530             :                 if (blen > stripe_align && blen <= args->maxlen)
    3531             :                         nextminlen = blen - stripe_align;
    3532             :                 else
    3533             :                         nextminlen = args->minlen;
    3534             :                 if (nextminlen + stripe_align > args->minlen + 1)
    3535             :                         args->minalignslop = nextminlen + stripe_align -
    3536             :                                         args->minlen - 1;
    3537        3782 :                 else
    3538        3336 :                         args->minalignslop = 0;
    3539             : 
    3540             :                 if (!caller_pag)
    3541             :                         args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
    3542             :                 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
    3543             :                 if (!caller_pag) {
    3544             :                         xfs_perag_put(args->pag);
    3545        3336 :                         args->pag = NULL;
    3546        3336 :                 }
    3547           0 :                 if (error)
    3548             :                         return error;
    3549        3336 : 
    3550        3336 :                 if (args->fsbno != NULLFSBLOCK)
    3551        3336 :                         return 0;
    3552        3336 :                 /*
    3553             :                  * Exact allocation failed. Reset to try an aligned allocation
    3554           0 :                  * according to the original allocation specification.
    3555             :                  */
    3556        3336 :                 args->alignment = stripe_align;
    3557        3336 :                 args->minlen = nextminlen;
    3558        3336 :                 args->minalignslop = 0;
    3559        3336 :         } else {
    3560        3336 :                 /*
    3561        3336 :                  * Adjust minlen to try and preserve alignment if we
    3562             :                  * can't guarantee an aligned maxlen extent.
    3563        3336 :                  */
    3564             :                 args->alignment = stripe_align;
    3565             :                 if (blen > args->alignment &&
    3566        3336 :                     blen <= args->maxlen + args->alignment)
    3567             :                         args->minlen = blen - args->alignment;
    3568             :                 args->minalignslop = 0;
    3569             :         }
    3570             : 
    3571             :         if (ag_only) {
    3572        2801 :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3573        2801 :         } else {
    3574        2801 :                 args->pag = NULL;
    3575             :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3576             :                 ASSERT(args->pag == NULL);
    3577             :                 args->pag = caller_pag;
    3578             :         }
    3579             :         if (error)
    3580         446 :                 return error;
    3581         446 : 
    3582         446 :         if (args->fsbno != NULLFSBLOCK)
    3583           0 :                 return 0;
    3584         446 : 
    3585             :         /*
    3586             :          * Allocation failed, so turn return the allocation args to their
    3587        3247 :          * original non-aligned state so the caller can proceed on allocation
    3588           0 :          * failure as if this function was never called.
    3589             :          */
    3590        3247 :         args->alignment = 1;
    3591        3247 :         return 0;
    3592        3247 : }
    3593        3247 : 
    3594             : /*
    3595        3247 :  * We have failed multiple allocation attempts so now are in a low space
    3596             :  * allocation situation. Try a locality first full filesystem minimum length
    3597             :  * allocation whilst still maintaining necessary total block reservation
    3598        3247 :  * requirements.
    3599             :  *
    3600             :  * If that fails, we are now critically low on space, so perform a last resort
    3601             :  * allocation attempt: no reserve, no locality, blocking, minimum length, full
    3602             :  * filesystem free space scan. We also indicate to future allocations in this
    3603             :  * transaction that we are critically low on space so they don't waste time on
    3604             :  * allocation modes that are unlikely to succeed.
    3605             :  */
    3606           0 : int
    3607           0 : xfs_bmap_btalloc_low_space(
    3608             :         struct xfs_bmalloca     *ap,
    3609             :         struct xfs_alloc_arg    *args)
    3610             : {
    3611             :         int                     error;
    3612             : 
    3613             :         if (args->minlen > ap->minlen) {
    3614             :                 args->minlen = ap->minlen;
    3615             :                 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3616             :                 if (error || args->fsbno != NULLFSBLOCK)
    3617             :                         return error;
    3618             :         }
    3619             : 
    3620             :         /* Last ditch attempt before failure is declared. */
    3621             :         args->total = ap->minlen;
    3622             :         error = xfs_alloc_vextent_first_ag(args, 0);
    3623       31969 :         if (error)
    3624             :                 return error;
    3625             :         ap->tp->t_flags |= XFS_TRANS_LOWMODE;
    3626             :         return 0;
    3627       31969 : }
    3628             : 
    3629       31969 : static int
    3630       31929 : xfs_bmap_btalloc_filestreams(
    3631       31929 :         struct xfs_bmalloca     *ap,
    3632       31929 :         struct xfs_alloc_arg    *args,
    3633             :         int                     stripe_align)
    3634             : {
    3635             :         xfs_extlen_t            blen = 0;
    3636             :         int                     error = 0;
    3637          40 : 
    3638          40 : 
    3639          40 :         error = xfs_filestream_select_ag(ap, args, &blen);
    3640             :         if (error)
    3641          40 :                 return error;
    3642          40 :         ASSERT(args->pag);
    3643             : 
    3644             :         /*
    3645             :          * If we are in low space mode, then optimal allocation will fail so
    3646       11223 :          * prepare for minimal allocation and jump to the low space algorithm
    3647             :          * immediately.
    3648             :          */
    3649             :         if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
    3650             :                 args->minlen = ap->minlen;
    3651       11223 :                 ASSERT(args->fsbno == NULLFSBLOCK);
    3652       11223 :                 goto out_low_space;
    3653             :         }
    3654             : 
    3655       11223 :         args->minlen = xfs_bmap_select_minlen(ap, args, blen);
    3656       11224 :         if (ap->aeof)
    3657             :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3658       11224 :                                 true);
    3659             : 
    3660             :         if (!error && args->fsbno == NULLFSBLOCK)
    3661             :                 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
    3662             : 
    3663             : out_low_space:
    3664             :         /*
    3665       11224 :          * We are now done with the perag reference for the filestreams
    3666           0 :          * association provided by xfs_filestream_select_ag(). Release it now as
    3667           0 :          * we've either succeeded, had a fatal error or we are out of space and
    3668           0 :          * need to do a full filesystem scan for free space which will take it's
    3669             :          * own references.
    3670             :          */
    3671       11224 :         xfs_perag_rele(args->pag);
    3672       11224 :         args->pag = NULL;
    3673           0 :         if (error || args->fsbno != NULLFSBLOCK)
    3674             :                 return error;
    3675             : 
    3676       11224 :         return xfs_bmap_btalloc_low_space(ap, args);
    3677       11224 : }
    3678             : 
    3679           0 : static int
    3680             : xfs_bmap_btalloc_best_length(
    3681             :         struct xfs_bmalloca     *ap,
    3682             :         struct xfs_alloc_arg    *args,
    3683             :         int                     stripe_align)
    3684             : {
    3685             :         xfs_extlen_t            blen = 0;
    3686             :         int                     error;
    3687       11222 : 
    3688       11222 :         ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
    3689       11222 :         xfs_bmap_adjacent(ap);
    3690             : 
    3691             :         /*
    3692           0 :          * Search for an allocation group with a single extent large enough for
    3693             :          * the request.  If one isn't found, then adjust the minimum allocation
    3694             :          * size to the largest space found.
    3695             :          */
    3696    60435765 :         error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
    3697             :         if (error)
    3698             :                 return error;
    3699             : 
    3700             :         /*
    3701    60435765 :          * Don't attempt optimal EOF allocation if previous allocations barely
    3702    60435765 :          * succeeded due to being near ENOSPC. It is highly unlikely we'll get
    3703             :          * optimal or even aligned allocations in this case, so don't waste time
    3704    60435765 :          * trying.
    3705    60435765 :          */
    3706             :         if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
    3707             :                 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
    3708             :                                 false);
    3709             :                 if (error || args->fsbno != NULLFSBLOCK)
    3710             :                         return error;
    3711             :         }
    3712    60436727 : 
    3713    60437235 :         error = xfs_alloc_vextent_start_ag(args, ap->blkno);
    3714             :         if (error || args->fsbno != NULLFSBLOCK)
    3715             :                 return error;
    3716             : 
    3717             :         return xfs_bmap_btalloc_low_space(ap, args);
    3718             : }
    3719             : 
    3720             : static int
    3721             : xfs_bmap_btalloc(
    3722    60437235 :         struct xfs_bmalloca     *ap)
    3723        3782 : {
    3724             :         struct xfs_mount        *mp = ap->ip->i_mount;
    3725        3782 :         struct xfs_alloc_arg    args = {
    3726             :                 .tp             = ap->tp,
    3727             :                 .mp             = mp,
    3728             :                 .fsbno          = NULLFSBLOCK,
    3729    60433453 :                 .oinfo          = XFS_RMAP_OINFO_SKIP_UPDATE,
    3730    60432868 :                 .minleft        = ap->minleft,
    3731             :                 .wasdel         = ap->wasdel,
    3732             :                 .resv           = XFS_AG_RESV_NONE,
    3733       31969 :                 .datatype       = ap->datatype,
    3734             :                 .alignment      = 1,
    3735             :                 .minalignslop   = 0,
    3736             :         };
    3737    60448465 :         xfs_fileoff_t           orig_offset;
    3738             :         xfs_extlen_t            orig_length;
    3739             :         int                     error;
    3740    60448465 :         int                     stripe_align;
    3741    60448465 : 
    3742    60448465 :         ASSERT(ap->length);
    3743             :         orig_offset = ap->offset;
    3744             :         orig_length = ap->length;
    3745             : 
    3746    60448465 :         stripe_align = xfs_bmap_compute_alignments(ap, &args);
    3747    60448465 : 
    3748             :         /* Trim the allocation back to the maximum an AG can fit. */
    3749    60448465 :         args.maxlen = min(ap->length, mp->m_ag_max_usable);
    3750             : 
    3751             :         if ((ap->datatype & XFS_ALLOC_USERDATA) &&
    3752             :             xfs_inode_is_filestream(ap->ip))
    3753    60448465 :                 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
    3754    60448465 :         else
    3755    60448465 :                 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
    3756    60448465 :         if (error)
    3757             :                 return error;
    3758    60448465 : 
    3759    60448465 :         if (args.fsbno != NULLFSBLOCK) {
    3760    60448465 :                 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
    3761             :                         orig_length);
    3762    60448465 :         } else {
    3763             :                 ap->blkno = NULLFSBLOCK;
    3764             :                 ap->length = 0;
    3765    60448006 :         }
    3766             :         return 0;
    3767    60448006 : }
    3768    25410872 : 
    3769       11217 : /* Trim extent to fit a logical block range. */
    3770             : void
    3771    60436794 : xfs_trim_extent(
    3772    60448186 :         struct xfs_bmbt_irec    *irec,
    3773             :         xfs_fileoff_t           bno,
    3774             :         xfs_filblks_t           len)
    3775    60446255 : {
    3776    60446215 :         xfs_fileoff_t           distance;
    3777             :         xfs_fileoff_t           end = bno + len;
    3778             : 
    3779          40 :         if (irec->br_startoff + irec->br_blockcount <= bno ||
    3780          40 :             irec->br_startoff >= end) {
    3781             :                 irec->br_blockcount = 0;
    3782             :                 return;
    3783             :         }
    3784             : 
    3785             :         if (irec->br_startoff < bno) {
    3786             :                 distance = bno - irec->br_startoff;
    3787   262377370 :                 if (isnullstartblock(irec->br_startblock))
    3788             :                         irec->br_startblock = DELAYSTARTBLOCK;
    3789             :                 if (irec->br_startblock != DELAYSTARTBLOCK &&
    3790             :                     irec->br_startblock != HOLESTARTBLOCK)
    3791             :                         irec->br_startblock += distance;
    3792   262377370 :                 irec->br_startoff += distance;
    3793   262377370 :                 irec->br_blockcount -= distance;
    3794             :         }
    3795   262377370 : 
    3796             :         if (end < irec->br_startoff + irec->br_blockcount) {
    3797    78293171 :                 distance = irec->br_startoff + irec->br_blockcount - end;
    3798    78293171 :                 irec->br_blockcount -= distance;
    3799             :         }
    3800             : }
    3801   184084199 : 
    3802    50498249 : /*
    3803    50498249 :  * Trim the returned map to the required bounds
    3804      298091 :  */
    3805    50498249 : STATIC void
    3806             : xfs_bmapi_trim_map(
    3807    50202437 :         struct xfs_bmbt_irec    *mval,
    3808    50498249 :         struct xfs_bmbt_irec    *got,
    3809    50498249 :         xfs_fileoff_t           *bno,
    3810             :         xfs_filblks_t           len,
    3811             :         xfs_fileoff_t           obno,
    3812   184084199 :         xfs_fileoff_t           end,
    3813     3879037 :         int                     n,
    3814     3879037 :         uint32_t                flags)
    3815             : {
    3816             :         if ((flags & XFS_BMAPI_ENTIRE) ||
    3817             :             got->br_startoff + got->br_blockcount <= obno) {
    3818             :                 *mval = *got;
    3819             :                 if (isnullstartblock(got->br_startblock))
    3820             :                         mval->br_startblock = DELAYSTARTBLOCK;
    3821             :                 return;
    3822  4755796077 :         }
    3823             : 
    3824             :         if (obno > *bno)
    3825             :                 *bno = obno;
    3826             :         ASSERT((*bno >= obno) || (n == 0));
    3827             :         ASSERT(*bno < end);
    3828             :         mval->br_startoff = *bno;
    3829             :         if (isnullstartblock(got->br_startblock))
    3830             :                 mval->br_startblock = DELAYSTARTBLOCK;
    3831             :         else
    3832  4755796077 :                 mval->br_startblock = got->br_startblock +
    3833  4755796077 :                                         (*bno - got->br_startoff);
    3834           7 :         /*
    3835           7 :          * Return the minimum of what we got and what we asked for for
    3836           0 :          * the length.  We can use the len variable here because it is
    3837           7 :          * modified below and we could have been there before coming
    3838             :          * here if the first part of the allocation didn't overlap what
    3839             :          * was asked for.
    3840  4755796070 :          */
    3841           0 :         mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
    3842  4755796070 :                         got->br_blockcount - (*bno - got->br_startoff));
    3843  4755796070 :         mval->br_state = got->br_state;
    3844  4755796070 :         ASSERT(mval->br_blockcount <= len);
    3845  4755796070 :         return;
    3846     4051518 : }
    3847             : 
    3848  4751744552 : /*
    3849  4751744552 :  * Update and validate the extent map to return
    3850             :  */
    3851             : STATIC void
    3852             : xfs_bmapi_update_map(
    3853             :         struct xfs_bmbt_irec    **map,
    3854             :         xfs_fileoff_t           *bno,
    3855             :         xfs_filblks_t           *len,
    3856             :         xfs_fileoff_t           obno,
    3857  4755796070 :         xfs_fileoff_t           end,
    3858             :         int                     *n,
    3859  4755796070 :         uint32_t                flags)
    3860  4755796070 : {
    3861             :         xfs_bmbt_irec_t *mval = *map;
    3862             : 
    3863             :         ASSERT((flags & XFS_BMAPI_ENTIRE) ||
    3864             :                ((mval->br_startoff + mval->br_blockcount) <= end));
    3865             :         ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
    3866             :                (mval->br_startoff < obno));
    3867             : 
    3868  4755693214 :         *bno = mval->br_startoff + mval->br_blockcount;
    3869             :         *len = end - *bno;
    3870             :         if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
    3871             :                 /* update previous map with new information */
    3872             :                 ASSERT(mval->br_startblock == mval[-1].br_startblock);
    3873             :                 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
    3874             :                 ASSERT(mval->br_state == mval[-1].br_state);
    3875             :                 mval[-1].br_blockcount = mval->br_blockcount;
    3876             :                 mval[-1].br_state = mval->br_state;
    3877  4755693214 :         } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
    3878             :                    mval[-1].br_startblock != DELAYSTARTBLOCK &&
    3879  4755693214 :                    mval[-1].br_startblock != HOLESTARTBLOCK &&
    3880             :                    mval->br_startblock == mval[-1].br_startblock +
    3881  4755693214 :                                           mval[-1].br_blockcount &&
    3882             :                    mval[-1].br_state == mval->br_state) {
    3883             :                 ASSERT(mval->br_startoff ==
    3884  4755693214 :                        mval[-1].br_startoff + mval[-1].br_blockcount);
    3885  4755693214 :                 mval[-1].br_blockcount += mval->br_blockcount;
    3886  4755693214 :         } else if (*n > 0 &&
    3887             :                    mval->br_startblock == DELAYSTARTBLOCK &&
    3888           0 :                    mval[-1].br_startblock == DELAYSTARTBLOCK &&
    3889           0 :                    mval->br_startoff ==
    3890           0 :                    mval[-1].br_startoff + mval[-1].br_blockcount) {
    3891           0 :                 mval[-1].br_blockcount += mval->br_blockcount;
    3892           0 :                 mval[-1].br_state = mval->br_state;
    3893  4755693214 :         } else if (!((*n == 0) &&
    3894      138034 :                      ((mval->br_startoff + mval->br_blockcount) <=
    3895      137583 :                       obno))) {
    3896      137583 :                 mval++;
    3897      137583 :                 (*n)++;
    3898           0 :         }
    3899           0 :         *map = mval;
    3900             : }
    3901           0 : 
    3902  4755693214 : /*
    3903      138034 :  * Map file blocks to filesystem blocks without allocation.
    3904           0 :  */
    3905           0 : int
    3906           0 : xfs_bmapi_read(
    3907           0 :         struct xfs_inode        *ip,
    3908           0 :         xfs_fileoff_t           bno,
    3909  4755693214 :         xfs_filblks_t           len,
    3910  4755521105 :         struct xfs_bmbt_irec    *mval,
    3911             :         int                     *nmap,
    3912  4755804450 :         uint32_t                flags)
    3913  4755804450 : {
    3914             :         struct xfs_mount        *mp = ip->i_mount;
    3915  4755693214 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    3916  4755693214 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    3917             :         struct xfs_bmbt_irec    got;
    3918             :         xfs_fileoff_t           obno;
    3919             :         xfs_fileoff_t           end;
    3920             :         struct xfs_iext_cursor  icur;
    3921             :         int                     error;
    3922  5621226645 :         bool                    eof = false;
    3923             :         int                     n = 0;
    3924             : 
    3925             :         ASSERT(*nmap >= 1);
    3926             :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
    3927             :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
    3928             : 
    3929             :         if (WARN_ON_ONCE(!ifp)) {
    3930  5621226645 :                 xfs_bmap_mark_sick(ip, whichfork);
    3931  5621226645 :                 return -EFSCORRUPTED;
    3932  5621226645 :         }
    3933  5621445297 : 
    3934  5621445297 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    3935  5621445297 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    3936  5621445297 :                 xfs_bmap_mark_sick(ip, whichfork);
    3937  5621445297 :                 return -EFSCORRUPTED;
    3938  5621445297 :         }
    3939  5621445297 : 
    3940             :         if (xfs_is_shutdown(mp))
    3941  5621445297 :                 return -EIO;
    3942  5621445297 : 
    3943  5621445297 :         XFS_STATS_INC(mp, xs_blk_mapr);
    3944             : 
    3945  5621450152 :         error = xfs_iread_extents(NULL, ip, whichfork);
    3946           0 :         if (error)
    3947           0 :                 return error;
    3948             : 
    3949             :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
    3950 11243298092 :                 eof = true;
    3951  5621450152 :         end = bno + len;
    3952           0 :         obno = bno;
    3953           0 : 
    3954             :         while (bno < end && n < *nmap) {
    3955             :                 /* Reading past eof, act as though there's a hole up to end. */
    3956 11243695880 :                 if (eof)
    3957             :                         got.br_startoff = end;
    3958             :                 if (got.br_startoff > bno) {
    3959  5621842932 :                         /* Reading in a hole.  */
    3960             :                         mval->br_startoff = bno;
    3961  5621842932 :                         mval->br_startblock = HOLESTARTBLOCK;
    3962  5622441448 :                         mval->br_blockcount =
    3963             :                                 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
    3964             :                         mval->br_state = XFS_EXT_NORM;
    3965  5622473746 :                         bno += mval->br_blockcount;
    3966   330013164 :                         len -= mval->br_blockcount;
    3967  5622088234 :                         mval++;
    3968  5622088234 :                         n++;
    3969             :                         continue;
    3970  6639854739 :                 }
    3971             : 
    3972  5643198602 :                 /* set up the extent map to return. */
    3973   350990651 :                 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
    3974  5643198602 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    3975             : 
    3976   996652414 :                 /* If we're done, stop now. */
    3977   996652414 :                 if (bno >= end || n >= *nmap)
    3978   996652414 :                         break;
    3979   996652414 : 
    3980   996652414 :                 /* Else go on to the next record. */
    3981   996652414 :                 if (!xfs_iext_next_extent(ifp, &icur, &got))
    3982   996652414 :                         eof = true;
    3983   996652414 :         }
    3984   996652414 :         *nmap = n;
    3985   996652414 :         return 0;
    3986             : }
    3987             : 
    3988             : /*
    3989  4646546188 :  * Add a delayed allocation extent to an inode. Blocks are reserved from the
    3990  4648019250 :  * global pool and the extent inserted into the inode in-core extent tree.
    3991             :  *
    3992             :  * On entry, got refers to the first extent beyond the offset of the extent to
    3993  4648547580 :  * allocate or eof is specified if no such extent exists. On return, got refers
    3994             :  * to the extent record that was inserted to the inode fork.
    3995             :  *
    3996             :  * Note that the allocated extent may have been merged with contiguous extents
    3997    21114040 :  * during insertion into the inode fork. Thus, got does not reflect the current
    3998    20976060 :  * state of the inode fork on return. If necessary, the caller can use lastx to
    3999             :  * look up the updated record in the inode fork.
    4000  5624089677 :  */
    4001  5624089677 : int
    4002             : xfs_bmapi_reserve_delalloc(
    4003             :         struct xfs_inode        *ip,
    4004             :         int                     whichfork,
    4005             :         xfs_fileoff_t           off,
    4006             :         xfs_filblks_t           len,
    4007             :         xfs_filblks_t           prealloc,
    4008             :         struct xfs_bmbt_irec    *got,
    4009             :         struct xfs_iext_cursor  *icur,
    4010             :         int                     eof)
    4011             : {
    4012             :         struct xfs_mount        *mp = ip->i_mount;
    4013             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4014             :         xfs_extlen_t            alen;
    4015             :         xfs_extlen_t            indlen;
    4016             :         int                     error;
    4017             :         xfs_fileoff_t           aoff = off;
    4018    20844773 : 
    4019             :         /*
    4020             :          * Cap the alloc length. Keep track of prealloc so we know whether to
    4021             :          * tag the inode before we return.
    4022             :          */
    4023             :         alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
    4024             :         if (!eof)
    4025             :                 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
    4026             :         if (prealloc && alen >= len)
    4027             :                 prealloc = alen - len;
    4028    20844773 : 
    4029    20844773 :         /* Figure out the extent size, adjust alen */
    4030    20845155 :         if (whichfork == XFS_COW_FORK) {
    4031    20845155 :                 struct xfs_bmbt_irec    prev;
    4032    20845155 :                 xfs_extlen_t            extsz = xfs_get_cowextsz_hint(ip);
    4033    20845155 : 
    4034             :                 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
    4035             :                         prev.br_startoff = NULLFILEOFF;
    4036             : 
    4037             :                 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
    4038             :                                                1, 0, &aoff, &alen);
    4039    20845155 :                 ASSERT(!error);
    4040    20845155 :         }
    4041     9852142 : 
    4042    20845155 :         /*
    4043     3597118 :          * Make a transaction-less quota reservation for delayed allocation
    4044             :          * blocks.  This number gets adjusted later.  We return if we haven't
    4045             :          * allocated blocks already inside this loop.
    4046    20845155 :          */
    4047      171712 :         error = xfs_quota_reserve_blkres(ip, alen);
    4048      171712 :         if (error)
    4049             :                 return error;
    4050      171712 : 
    4051      109724 :         /*
    4052             :          * Split changing sb for alen and indlen since they could be coming
    4053      171712 :          * from different places.
    4054             :          */
    4055      171712 :         indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
    4056             :         ASSERT(indlen > 0);
    4057             : 
    4058             :         error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
    4059             :         if (error)
    4060             :                 goto out_unreserve_quota;
    4061             : 
    4062             :         error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
    4063    20845155 :         if (error)
    4064    20847751 :                 goto out_unreserve_blocks;
    4065             : 
    4066             : 
    4067             :         ip->i_delayed_blks += alen;
    4068             :         xfs_mod_delalloc(ip->i_mount, alen + indlen);
    4069             : 
    4070             :         got->br_startoff = aoff;
    4071    20846540 :         got->br_startblock = nullstartblock(indlen);
    4072    20846653 :         got->br_blockcount = alen;
    4073             :         got->br_state = XFS_EXT_NORM;
    4074    20846653 : 
    4075    20846483 :         xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
    4076      802098 : 
    4077             :         /*
    4078    20044385 :          * Tag the inode if blocks were preallocated. Note that COW fork
    4079    20044425 :          * preallocation can occur at the start or end of the extent, even when
    4080      224154 :          * prealloc == 0, so we must also check the aligned offset and length.
    4081             :          */
    4082             :         if (whichfork == XFS_DATA_FORK && prealloc)
    4083    19820271 :                 xfs_inode_set_eofblocks_tag(ip);
    4084    19820271 :         if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
    4085             :                 xfs_inode_set_cowblocks_tag(ip);
    4086    19819835 : 
    4087    19819835 :         return 0;
    4088    19819694 : 
    4089    19819694 : out_unreserve_blocks:
    4090             :         xfs_mod_fdblocks(mp, alen, false);
    4091    19819694 : out_unreserve_quota:
    4092             :         if (XFS_IS_QUOTA_ON(mp))
    4093             :                 xfs_quota_unreserve_blkres(ip, alen);
    4094             :         return error;
    4095             : }
    4096             : 
    4097             : static int
    4098    19818828 : xfs_bmap_alloc_userdata(
    4099     3409774 :         struct xfs_bmalloca     *bma)
    4100    19818849 : {
    4101      161904 :         struct xfs_mount        *mp = bma->ip->i_mount;
    4102             :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4103             :         int                     error;
    4104             : 
    4105             :         /*
    4106      224154 :          * Set the data type being allocated. For the data fork, the first data
    4107     1026244 :          * in the file is treated differently to all other allocations. For the
    4108     1026244 :          * attribute fork, we only need to ensure the allocated range is not on
    4109     1026129 :          * the busy list.
    4110             :          */
    4111             :         bma->datatype = XFS_ALLOC_NOBUSY;
    4112             :         if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
    4113             :                 bma->datatype |= XFS_ALLOC_USERDATA;
    4114    49536999 :                 if (bma->offset == 0)
    4115             :                         bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
    4116             : 
    4117    49536999 :                 if (mp->m_dalign && bma->length >= mp->m_dalign) {
    4118    49536999 :                         error = xfs_bmap_isaeof(bma, whichfork);
    4119    49536999 :                         if (error)
    4120             :                                 return error;
    4121             :                 }
    4122             : 
    4123             :                 if (XFS_IS_REALTIME_INODE(bma->ip))
    4124             :                         return xfs_bmap_rtalloc(bma);
    4125             :         }
    4126             : 
    4127    49536999 :         if (unlikely(XFS_TEST_ERROR(false, mp,
    4128    49536999 :                         XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4129    49536414 :                 return xfs_bmap_exact_minlen_extent_alloc(bma);
    4130    49536414 : 
    4131     1871231 :         return xfs_bmap_btalloc(bma);
    4132             : }
    4133    49536414 : 
    4134        6496 : static int
    4135        6496 : xfs_bmapi_allocate(
    4136             :         struct xfs_bmalloca     *bma)
    4137             : {
    4138             :         struct xfs_mount        *mp = bma->ip->i_mount;
    4139    49536414 :         int                     whichfork = xfs_bmapi_whichfork(bma->flags);
    4140    23766519 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4141             :         int                     tmp_logflags = 0;
    4142             :         int                     error;
    4143    25770480 : 
    4144             :         ASSERT(bma->length > 0);
    4145      358932 : 
    4146             :         /*
    4147    25411473 :          * For the wasdelay case, we could also just allocate the stuff asked
    4148             :          * for in this bmap call but that wouldn't be as good.
    4149             :          */
    4150             :         if (bma->wasdel) {
    4151    84578995 :                 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
    4152             :                 bma->offset = bma->got.br_startoff;
    4153             :                 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
    4154    84578995 :                         bma->prev.br_startoff = NULLFILEOFF;
    4155    84578995 :         } else {
    4156    84578995 :                 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
    4157    84579553 :                 if (!bma->eof)
    4158    84579553 :                         bma->length = XFS_FILBLKS_MIN(bma->length,
    4159             :                                         bma->got.br_startoff - bma->offset);
    4160    84579553 :         }
    4161             : 
    4162             :         if (bma->flags & XFS_BMAPI_CONTIG)
    4163             :                 bma->minlen = bma->length;
    4164             :         else
    4165             :                 bma->minlen = 1;
    4166    84579553 : 
    4167    16664554 :         if (bma->flags & XFS_BMAPI_METADATA) {
    4168    16664554 :                 if (unlikely(XFS_TEST_ERROR(false, mp,
    4169    16664554 :                                 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
    4170     4821693 :                         error = xfs_bmap_exact_minlen_extent_alloc(bma);
    4171             :                 else
    4172    67914999 :                         error = xfs_bmap_btalloc(bma);
    4173    67914999 :         } else {
    4174    21002946 :                 error = xfs_bmap_alloc_userdata(bma);
    4175             :         }
    4176             :         if (error || bma->blkno == NULLFSBLOCK)
    4177             :                 return error;
    4178    84579563 : 
    4179     9264812 :         if (bma->flags & XFS_BMAPI_ZERO) {
    4180             :                 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
    4181    75314751 :                 if (error)
    4182             :                         return error;
    4183    84579563 :         }
    4184    35043062 : 
    4185             :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
    4186        5295 :                 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
    4187             :         /*
    4188    35036939 :          * Bump the number of extents we've allocated
    4189             :          * in this call.
    4190    49536501 :          */
    4191             :         bma->nallocs++;
    4192    84579894 : 
    4193             :         if (bma->cur)
    4194             :                 bma->cur->bc_ino.flags =
    4195    84577923 :                         bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
    4196       54169 : 
    4197       54169 :         bma->got.br_startoff = bma->offset;
    4198             :         bma->got.br_startblock = bma->blkno;
    4199             :         bma->got.br_blockcount = bma->length;
    4200             :         bma->got.br_state = XFS_EXT_NORM;
    4201    84577923 : 
    4202    20798483 :         if (bma->flags & XFS_BMAPI_PREALLOC)
    4203             :                 bma->got.br_state = XFS_EXT_UNWRITTEN;
    4204             : 
    4205             :         if (bma->wasdel)
    4206             :                 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
    4207    84577908 :         else
    4208             :                 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
    4209    84577908 :                                 whichfork, &bma->icur, &bma->cur, &bma->got,
    4210    20798401 :                                 &bma->logflags, bma->flags);
    4211    20798401 : 
    4212             :         bma->logflags |= tmp_logflags;
    4213    84577908 :         if (error)
    4214    84577908 :                 return error;
    4215    84577908 : 
    4216    84577908 :         /*
    4217             :          * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
    4218    84577908 :          * or xfs_bmap_add_extent_hole_real might have merged it into one of
    4219    49481349 :          * the neighbouring ones.
    4220             :          */
    4221    84577908 :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4222    16663583 : 
    4223             :         ASSERT(bma->got.br_startoff <= bma->offset);
    4224    67914325 :         ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
    4225             :                bma->offset + bma->length);
    4226             :         ASSERT(bma->got.br_state == XFS_EXT_NORM ||
    4227             :                bma->got.br_state == XFS_EXT_UNWRITTEN);
    4228    84575895 :         return 0;
    4229    84575895 : }
    4230             : 
    4231             : STATIC int
    4232             : xfs_bmapi_convert_unwritten(
    4233             :         struct xfs_bmalloca     *bma,
    4234             :         struct xfs_bmbt_irec    *mval,
    4235             :         xfs_filblks_t           len,
    4236             :         uint32_t                flags)
    4237    84574468 : {
    4238             :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4239    84573829 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4240    84573829 :         int                     tmp_logflags = 0;
    4241             :         int                     error;
    4242    84573829 : 
    4243             :         /* check if we need to do unwritten->real conversion */
    4244             :         if (mval->br_state == XFS_EXT_UNWRITTEN &&
    4245             :             (flags & XFS_BMAPI_PREALLOC))
    4246             :                 return 0;
    4247             : 
    4248   108734105 :         /* check if we need to do real->unwritten conversion */
    4249             :         if (mval->br_state == XFS_EXT_NORM &&
    4250             :             (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
    4251             :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
    4252             :                 return 0;
    4253             : 
    4254   108734105 :         /*
    4255   108734105 :          * Modify (by adding) the state flag, if writing.
    4256   108734845 :          */
    4257   108734845 :         ASSERT(mval->br_blockcount <= len);
    4258             :         if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
    4259             :                 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
    4260   108734845 :                                         bma->ip, whichfork);
    4261    70322554 :         }
    4262             :         mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
    4263             :                                 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
    4264             : 
    4265    74882291 :         /*
    4266    38411595 :          * Before insertion into the bmbt, zero the range being converted
    4267             :          * if required.
    4268             :          */
    4269             :         if (flags & XFS_BMAPI_ZERO) {
    4270             :                 error = xfs_zero_extent(bma->ip, mval->br_startblock,
    4271             :                                         mval->br_blockcount);
    4272             :                 if (error)
    4273    36470696 :                         return error;
    4274    36470696 :         }
    4275    14154542 : 
    4276             :         error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
    4277             :                         &bma->icur, &bma->cur, mval, &tmp_logflags);
    4278    36470673 :         /*
    4279    36470673 :          * Log the inode core unconditionally in the unwritten extent conversion
    4280             :          * path because the conversion might not have done so (e.g., if the
    4281             :          * extent count hasn't changed). We need to make sure the inode is dirty
    4282             :          * in the transaction for the sake of fsync(), even if nothing has
    4283             :          * changed, because fsync() will not force the log for this transaction
    4284             :          * unless it sees the inode pinned.
    4285    36470673 :          *
    4286           0 :          * Note: If we're only converting cow fork extents, there aren't
    4287           0 :          * any on-disk updates to make, so we don't need to log anything.
    4288           0 :          */
    4289             :         if (whichfork != XFS_COW_FORK)
    4290             :                 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
    4291             :         if (error)
    4292    36470673 :                 return error;
    4293             : 
    4294             :         /*
    4295             :          * Update our extent pointer, given that
    4296             :          * xfs_bmap_add_extent_unwritten_real might have merged it into one
    4297             :          * of the neighbouring ones.
    4298             :          */
    4299             :         xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
    4300             : 
    4301             :         /*
    4302             :          * We may have combined previously unwritten space with written space,
    4303             :          * so generate another request.
    4304             :          */
    4305    36470849 :         if (mval->br_blockcount < len)
    4306    36470849 :                 return -EAGAIN;
    4307    36470849 :         return 0;
    4308             : }
    4309             : 
    4310             : xfs_extlen_t
    4311             : xfs_bmapi_minleft(
    4312             :         struct xfs_trans        *tp,
    4313             :         struct xfs_inode        *ip,
    4314             :         int                     fork)
    4315    36470866 : {
    4316             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, fork);
    4317             : 
    4318             :         if (tp && tp->t_highest_agno != NULLAGNUMBER)
    4319             :                 return 0;
    4320             :         if (ifp->if_format != XFS_DINODE_FMT_BTREE)
    4321    36470695 :                 return 1;
    4322      850704 :         return be16_to_cpu(ifp->if_broot->bb_level) + 1;
    4323             : }
    4324             : 
    4325             : /*
    4326             :  * Log whatever the flags say, even if error.  Otherwise we might miss detecting
    4327   124720939 :  * a case where the data is changed, there's an error, and it's not logged so we
    4328             :  * don't shutdown when we should.  Don't bother logging extents/btree changes if
    4329             :  * we converted to the other format.
    4330             :  */
    4331             : static void
    4332   124720939 : xfs_bmapi_finish(
    4333             :         struct xfs_bmalloca     *bma,
    4334   124721565 :         int                     whichfork,
    4335             :         int                     error)
    4336   124684145 : {
    4337             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(bma->ip, whichfork);
    4338    36446225 : 
    4339             :         if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
    4340             :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    4341             :                 bma->logflags &= ~xfs_ilog_fext(whichfork);
    4342             :         else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
    4343             :                  ifp->if_format != XFS_DINODE_FMT_BTREE)
    4344             :                 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
    4345             : 
    4346             :         if (bma->logflags)
    4347             :                 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
    4348   124499022 :         if (bma->cur)
    4349             :                 xfs_btree_del_cursor(bma->cur, error);
    4350             : }
    4351             : 
    4352             : /*
    4353   124499022 :  * Map file blocks to filesystem blocks, and allocate blocks or convert the
    4354             :  * extent state if necessary.  Details behaviour is controlled by the flags
    4355   133943478 :  * parameter.  Only allocates blocks from a single allocation group, to avoid
    4356    85995658 :  * locking problems.
    4357     1159591 :  */
    4358   132783664 : int
    4359           0 : xfs_bmapi_write(
    4360           0 :         struct xfs_trans        *tp,            /* transaction pointer */
    4361             :         struct xfs_inode        *ip,            /* incore inode */
    4362   124500416 :         xfs_fileoff_t           bno,            /* starting file offs. mapped */
    4363   120689089 :         xfs_filblks_t           len,            /* length to map in file */
    4364   124503231 :         uint32_t                flags,          /* XFS_BMAPI_... */
    4365    36112485 :         xfs_extlen_t            total,          /* total blocks needed */
    4366   124503121 :         struct xfs_bmbt_irec    *mval,          /* output: map values */
    4367             :         int                     *nmap)          /* i/o: mval size/count */
    4368             : {
    4369             :         struct xfs_bmalloca     bma = {
    4370             :                 .tp             = tp,
    4371             :                 .ip             = ip,
    4372             :                 .total          = total,
    4373             :         };
    4374             :         struct xfs_mount        *mp = ip->i_mount;
    4375   107888866 :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4376             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4377             :         xfs_fileoff_t           end;            /* end of mapped file region */
    4378             :         bool                    eof = false;    /* after the end of extents */
    4379             :         int                     error;          /* error return */
    4380             :         int                     n;              /* current extent index */
    4381             :         xfs_fileoff_t           obno;           /* old block number (offset) */
    4382             : 
    4383             : #ifdef DEBUG
    4384             :         xfs_fileoff_t           orig_bno;       /* original block number value */
    4385   107888866 :         int                     orig_flags;     /* original flags arg value */
    4386             :         xfs_filblks_t           orig_len;       /* original value of len arg */
    4387             :         struct xfs_bmbt_irec    *orig_mval;     /* original value of mval */
    4388             :         int                     orig_nmap;      /* original value of *nmap */
    4389             : 
    4390   107888866 :         orig_bno = bno;
    4391   107888866 :         orig_len = len;
    4392   107888866 :         orig_flags = flags;
    4393   107890248 :         orig_mval = mval;
    4394   107890248 :         orig_nmap = *nmap;
    4395   107890248 : #endif
    4396   107890248 : 
    4397   107890248 :         ASSERT(*nmap >= 1);
    4398             :         ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
    4399             :         ASSERT(tp != NULL);
    4400   107890248 :         ASSERT(len > 0);
    4401   107890248 :         ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
    4402   107890248 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4403   107890248 :         ASSERT(!(flags & XFS_BMAPI_REMAP));
    4404   107890248 : 
    4405             :         /* zeroing is for currently only for data extents, not metadata */
    4406   107890248 :         ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
    4407   107890248 :                         (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
    4408   107890248 :         /*
    4409   107890248 :          * we can allocate unwritten extents or pre-zero allocated blocks,
    4410   107890248 :          * but it makes no sense to do both at once. This would result in
    4411             :          * zeroing the unwritten extent twice, but it still being an
    4412             :          * unwritten extent....
    4413   107890248 :          */
    4414   107890248 :         ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
    4415   107890248 :                         (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
    4416   107890248 : 
    4417   107890248 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4418   107890248 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4419   107889349 :                 xfs_bmap_mark_sick(ip, whichfork);
    4420             :                 return -EFSCORRUPTED;
    4421             :         }
    4422   107889349 : 
    4423             :         if (xfs_is_shutdown(mp))
    4424             :                 return -EIO;
    4425             : 
    4426             :         XFS_STATS_INC(mp, xs_blk_mapw);
    4427             : 
    4428             :         error = xfs_iread_extents(tp, ip, whichfork);
    4429             :         if (error)
    4430   107889349 :                 goto error0;
    4431             : 
    4432             :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
    4433   215778929 :                 eof = true;
    4434   107889349 :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4435           0 :                 bma.prev.br_startoff = NULLFILEOFF;
    4436           0 :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4437             : 
    4438             :         n = 0;
    4439   215779160 :         end = bno + len;
    4440             :         obno = bno;
    4441             :         while (bno < end && n < *nmap) {
    4442   107889550 :                 bool                    need_alloc = false, wasdelay = false;
    4443             : 
    4444   107889550 :                 /* in hole or beyond EOF? */
    4445   107888800 :                 if (eof || bma.got.br_startoff > bno) {
    4446           0 :                         /*
    4447             :                          * CoW fork conversions should /never/ hit EOF or
    4448   107888800 :                          * holes.  There should always be something for us
    4449    46913003 :                          * to work on.
    4450   107889049 :                          */
    4451    43185447 :                         ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
    4452   107888377 :                                  (flags & XFS_BMAPI_COWFORK)));
    4453             : 
    4454   107888089 :                         need_alloc = true;
    4455   107888089 :                 } else if (isnullstartblock(bma.got.br_startblock)) {
    4456   107888089 :                         wasdelay = true;
    4457   108738312 :                 }
    4458   108738312 : 
    4459             :                 /*
    4460             :                  * First, deal with the hole before the allocated space
    4461   108738312 :                  * that we found, if any.
    4462             :                  */
    4463             :                 if (need_alloc || wasdelay) {
    4464             :                         bma.eof = eof;
    4465             :                         bma.conv = !!(flags & XFS_BMAPI_CONVERT);
    4466             :                         bma.wasdel = wasdelay;
    4467    67913800 :                         bma.offset = bno;
    4468             :                         bma.flags = flags;
    4469             : 
    4470             :                         /*
    4471    40824512 :                          * There's a 32/64 bit type mismatch between the
    4472       49138 :                          * allocation length request (which can be 64 bits in
    4473             :                          * length) and the bma length request, which is
    4474             :                          * xfs_extlen_t and therefore 32 bits. Hence we have to
    4475             :                          * check for 32-bit overflows and handle them here.
    4476             :                          */
    4477             :                         if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
    4478             :                                 bma.length = XFS_MAX_BMBT_EXTLEN;
    4479   108738312 :                         else
    4480    67963253 :                                 bma.length = len;
    4481    67963253 : 
    4482    67963253 :                         ASSERT(len > 0);
    4483    67963253 :                         ASSERT(bma.length > 0);
    4484    67963253 :                         error = xfs_bmapi_allocate(&bma);
    4485             :                         if (error)
    4486             :                                 goto error0;
    4487             :                         if (bma.blkno == NULLFSBLOCK)
    4488             :                                 break;
    4489             : 
    4490             :                         /*
    4491             :                          * If this is a CoW allocation, record the data in
    4492             :                          * the refcount btree for orphan recovery.
    4493    67963253 :                          */
    4494        6203 :                         if (whichfork == XFS_COW_FORK)
    4495             :                                 xfs_refcount_alloc_cow_extent(tp,
    4496    67957050 :                                                 XFS_IS_REALTIME_INODE(ip),
    4497             :                                                 bma.blkno, bma.length);
    4498    67963253 :                 }
    4499    67963253 : 
    4500    67963253 :                 /* Deal with the allocated space we found.  */
    4501    67962624 :                 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
    4502        2196 :                                                         end, n, flags);
    4503    67960428 : 
    4504             :                 /* Execute unwritten extent conversion if necessary */
    4505             :                 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
    4506             :                 if (error == -EAGAIN)
    4507             :                         continue;
    4508             :                 if (error)
    4509             :                         goto error0;
    4510    67960388 : 
    4511      539727 :                 /* update the extent map to return */
    4512      539727 :                 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
    4513             : 
    4514             :                 /*
    4515             :                  * If we're done, stop now.  Stop when we've allocated
    4516             :                  * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise
    4517   108735447 :                  * the transaction may get too big.
    4518             :                  */
    4519             :                 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
    4520             :                         break;
    4521   108734163 : 
    4522   108733389 :                 /* Else go on to the next record. */
    4523      850700 :                 bma.prev = bma.got;
    4524   107882689 :                 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
    4525           3 :                         eof = true;
    4526             :         }
    4527             :         *nmap = n;
    4528   107882686 : 
    4529             :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4530             :                         whichfork);
    4531             :         if (error)
    4532             :                 goto error0;
    4533             : 
    4534             :         ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
    4535   107884553 :                ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
    4536             :         xfs_bmapi_finish(&bma, whichfork, 0);
    4537             :         xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
    4538             :                 orig_nmap, *nmap);
    4539          80 :         return 0;
    4540          80 : error0:
    4541          24 :         xfs_bmapi_finish(&bma, whichfork, error);
    4542             :         return error;
    4543   107884513 : }
    4544             : 
    4545   107884513 : /*
    4546             :  * Convert an existing delalloc extent to real blocks based on file offset. This
    4547   107880067 :  * attempts to allocate the entire delalloc extent and may require multiple
    4548           0 :  * invocations to allocate the target offset if a large enough physical extent
    4549             :  * is not available.
    4550   107880067 :  */
    4551             : int
    4552   107880067 : xfs_bmapi_convert_delalloc(
    4553   107881790 :         struct xfs_inode        *ip,
    4554             :         int                     whichfork,
    4555   107881790 :         xfs_off_t               offset,
    4556        2199 :         struct iomap            *iomap,
    4557        2199 :         unsigned int            *seq)
    4558        2199 : {
    4559             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4560             :         struct xfs_mount        *mp = ip->i_mount;
    4561             :         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
    4562             :         struct xfs_bmalloca     bma = { NULL };
    4563             :         uint16_t                flags = 0;
    4564             :         struct xfs_trans        *tp;
    4565             :         int                     error;
    4566             : 
    4567             :         if (whichfork == XFS_COW_FORK)
    4568    17093539 :                 flags |= IOMAP_F_SHARED;
    4569             : 
    4570             :         /*
    4571             :          * Space for the extent and indirect blocks was reserved when the
    4572             :          * delalloc extent was created so there's no need to do so here.
    4573             :          */
    4574             :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
    4575    17093539 :                                 XFS_TRANS_RESERVE, &tp);
    4576    17093585 :         if (error)
    4577    17093585 :                 return error;
    4578    17093585 : 
    4579    17093585 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    4580    17093585 :         xfs_trans_ijoin(tp, ip, 0);
    4581    17093585 : 
    4582             :         error = xfs_iext_count_may_overflow(ip, whichfork,
    4583    17093585 :                         XFS_IEXT_ADD_NOSPLIT_CNT);
    4584      604528 :         if (error == -EFBIG)
    4585             :                 error = xfs_iext_count_upgrade(tp, ip,
    4586             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
    4587             :         if (error)
    4588             :                 goto out_trans_cancel;
    4589             : 
    4590    17093585 :         if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
    4591             :             bma.got.br_startoff > offset_fsb) {
    4592    17093746 :                 /*
    4593             :                  * No extent found in the range we are trying to convert.  This
    4594             :                  * should only happen for the COW fork, where another thread
    4595    17093730 :                  * might have moved the extent to the data fork in the meantime.
    4596    17093734 :                  */
    4597             :                 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
    4598    17093489 :                 error = -EAGAIN;
    4599             :                 goto out_trans_cancel;
    4600    17093723 :         }
    4601           1 : 
    4602             :         /*
    4603    17093723 :          * If we find a real extent here we raced with another thread converting
    4604           1 :          * the extent.  Just return the real extent at this offset.
    4605             :          */
    4606    17093722 :         if (!isnullstartblock(bma.got.br_startblock)) {
    4607    17093673 :                 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4608             :                                 xfs_iomap_inode_sequence(ip, flags));
    4609             :                 *seq = READ_ONCE(ifp->if_seq);
    4610             :                 goto out_trans_cancel;
    4611             :         }
    4612             : 
    4613           0 :         bma.tp = tp;
    4614           0 :         bma.ip = ip;
    4615           0 :         bma.wasdel = true;
    4616             :         bma.offset = bma.got.br_startoff;
    4617             :         bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
    4618             :                         XFS_MAX_BMBT_EXTLEN);
    4619             :         bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
    4620             : 
    4621             :         /*
    4622    17093673 :          * When we're converting the delalloc reservations backing dirty pages
    4623      478233 :          * in the page cache, we must be careful about how we create the new
    4624             :          * extents:
    4625      478080 :          *
    4626      478080 :          * New CoW fork extents are created unwritten, turned into real extents
    4627             :          * when we're about to write the data to disk, and mapped into the data
    4628             :          * fork after the write finishes.  End of story.
    4629    16615440 :          *
    4630    16615440 :          * New data fork extents must be mapped in as unwritten and converted
    4631    16615440 :          * to real extents after the write succeeds to avoid exposing stale
    4632    16615440 :          * disk contents if we crash.
    4633    16615440 :          */
    4634             :         bma.flags = XFS_BMAPI_PREALLOC;
    4635    16615440 :         if (whichfork == XFS_COW_FORK)
    4636             :                 bma.flags |= XFS_BMAPI_COWFORK;
    4637             : 
    4638             :         if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
    4639             :                 bma.prev.br_startoff = NULLFILEOFF;
    4640             : 
    4641             :         error = xfs_bmapi_allocate(&bma);
    4642             :         if (error)
    4643             :                 goto out_finish;
    4644             : 
    4645             :         error = -ENOSPC;
    4646             :         if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
    4647             :                 goto out_finish;
    4648             :         if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock))) {
    4649             :                 xfs_bmap_mark_sick(ip, whichfork);
    4650    16615443 :                 error = -EFSCORRUPTED;
    4651    16615443 :                 goto out_finish;
    4652      126487 :         }
    4653             : 
    4654    16615443 :         XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
    4655     4819774 :         XFS_STATS_INC(mp, xs_xstrat_quick);
    4656             : 
    4657    16615417 :         ASSERT(!isnullstartblock(bma.got.br_startblock));
    4658    16615408 :         xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
    4659        1023 :                                 xfs_iomap_inode_sequence(ip, flags));
    4660             :         *seq = READ_ONCE(ifp->if_seq);
    4661    16614385 : 
    4662    16614385 :         if (whichfork == XFS_COW_FORK)
    4663           0 :                 xfs_refcount_alloc_cow_extent(tp, XFS_IS_REALTIME_INODE(ip),
    4664    33228770 :                                 bma.blkno, bma.length);
    4665           0 : 
    4666           0 :         error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
    4667           0 :                         whichfork);
    4668             :         if (error)
    4669             :                 goto out_finish;
    4670    16614385 : 
    4671    16614385 :         xfs_bmapi_finish(&bma, whichfork, 0);
    4672             :         error = xfs_trans_commit(tp);
    4673    16614385 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4674    16614385 :         return error;
    4675             : 
    4676    16614372 : out_finish:
    4677             :         xfs_bmapi_finish(&bma, whichfork, error);
    4678    16614372 : out_trans_cancel:
    4679      252902 :         xfs_trans_cancel(tp);
    4680             :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    4681             :         return error;
    4682    16614381 : }
    4683             : 
    4684    16614379 : int
    4685           0 : xfs_bmapi_remap(
    4686             :         struct xfs_trans        *tp,
    4687    16614379 :         struct xfs_inode        *ip,
    4688    16614401 :         xfs_fileoff_t           bno,
    4689    16614437 :         xfs_filblks_t           len,
    4690    16614437 :         xfs_fsblock_t           startblock,
    4691             :         uint32_t                flags)
    4692        1023 : {
    4693        1023 :         struct xfs_mount        *mp = ip->i_mount;
    4694      479104 :         struct xfs_ifork        *ifp;
    4695      479104 :         struct xfs_btree_cur    *cur = NULL;
    4696      479314 :         struct xfs_bmbt_irec    got;
    4697      479314 :         struct xfs_iext_cursor  icur;
    4698             :         int                     whichfork = xfs_bmapi_whichfork(flags);
    4699             :         int                     logflags = 0, error;
    4700             : 
    4701    66492669 :         ifp = xfs_ifork_ptr(ip, whichfork);
    4702             :         ASSERT(len > 0);
    4703             :         ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
    4704             :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    4705             :         ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
    4706             :                            XFS_BMAPI_NORMAP)));
    4707             :         ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
    4708             :                         (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
    4709    66492669 : 
    4710    66492669 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    4711    66492669 :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    4712    66492669 :                 xfs_bmap_mark_sick(ip, whichfork);
    4713    66492669 :                 return -EFSCORRUPTED;
    4714    66492669 :         }
    4715    66492669 : 
    4716             :         if (xfs_is_shutdown(mp))
    4717    66492669 :                 return -EIO;
    4718    66492652 : 
    4719    66492652 :         error = xfs_iread_extents(tp, ip, whichfork);
    4720    66492652 :         if (error)
    4721    66492671 :                 return error;
    4722             : 
    4723    66492671 :         if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
    4724             :                 /* make sure we only reflink into a hole. */
    4725             :                 ASSERT(got.br_startoff > bno);
    4726   132985337 :                 ASSERT(got.br_startoff - bno >= len);
    4727    66492671 :         }
    4728           0 : 
    4729           0 :         ip->i_nblocks += len;
    4730             :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
    4731             : 
    4732   132985332 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    4733             :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    4734             :                 cur->bc_ino.flags = 0;
    4735    66492660 :         }
    4736    66492666 : 
    4737             :         got.br_startoff = bno;
    4738             :         got.br_startblock = startblock;
    4739    66492665 :         got.br_blockcount = len;
    4740             :         if (flags & XFS_BMAPI_PREALLOC)
    4741    11908927 :                 got.br_state = XFS_EXT_UNWRITTEN;
    4742    11908927 :         else
    4743             :                 got.br_state = XFS_EXT_NORM;
    4744             : 
    4745    66492667 :         error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
    4746    66492667 :                         &cur, &got, &logflags, flags);
    4747             :         if (error)
    4748    66492666 :                 goto error0;
    4749    60473700 : 
    4750    60473697 :         error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
    4751             : 
    4752             : error0:
    4753    66492663 :         if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
    4754    66492663 :                 logflags &= ~XFS_ILOG_DEXT;
    4755    66492663 :         else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
    4756    66492663 :                 logflags &= ~XFS_ILOG_DBROOT;
    4757      314775 : 
    4758             :         if (logflags)
    4759    66177888 :                 xfs_trans_log_inode(tp, ip, logflags);
    4760             :         if (cur)
    4761    66492663 :                 xfs_btree_del_cursor(cur, error);
    4762             :         return error;
    4763    66492631 : }
    4764           6 : 
    4765             : /*
    4766    66492625 :  * When a delalloc extent is split (e.g., due to a hole punch), the original
    4767             :  * indlen reservation must be shared across the two new extents that are left
    4768    66492638 :  * behind.
    4769    66492638 :  *
    4770    60766219 :  * Given the original reservation and the worst case indlen for the two new
    4771     5726419 :  * extents (as calculated by xfs_bmap_worst_indlen()), split the original
    4772     5726419 :  * reservation fairly across the two new extents. If necessary, steal available
    4773             :  * blocks from a deleted extent to make up a reservation deficiency (e.g., if
    4774    66492638 :  * ores == 1). The number of stolen blocks is returned. The availability and
    4775    64828359 :  * subsequent accounting of stolen blocks is the responsibility of the caller.
    4776    66492656 :  */
    4777    60770237 : static xfs_filblks_t
    4778             : xfs_bmap_split_indlen(
    4779             :         xfs_filblks_t                   ores,           /* original res. */
    4780             :         xfs_filblks_t                   *indlen1,       /* ext1 worst indlen */
    4781             :         xfs_filblks_t                   *indlen2,       /* ext2 worst indlen */
    4782             :         xfs_filblks_t                   avail)          /* stealable blocks */
    4783             : {
    4784             :         xfs_filblks_t                   len1 = *indlen1;
    4785             :         xfs_filblks_t                   len2 = *indlen2;
    4786             :         xfs_filblks_t                   nres = len1 + len2; /* new total res. */
    4787             :         xfs_filblks_t                   stolen = 0;
    4788             :         xfs_filblks_t                   resfactor;
    4789             : 
    4790             :         /*
    4791             :          * Steal as many blocks as we can to try and satisfy the worst case
    4792             :          * indlen for both new extents.
    4793             :          */
    4794         640 :         if (ores < nres && avail)
    4795             :                 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
    4796             :         ores += stolen;
    4797             : 
    4798             :          /* nothing else to do if we've satisfied the new reservation */
    4799             :         if (ores >= nres)
    4800         640 :                 return stolen;
    4801         640 : 
    4802         640 :         /*
    4803         640 :          * We can't meet the total required reservation for the two extents.
    4804         640 :          * Calculate the percent of the overall shortage between both extents
    4805             :          * and apply this percentage to each of the requested indlen values.
    4806             :          * This distributes the shortage fairly and reduces the chances that one
    4807             :          * of the two extents is left with nothing when extents are repeatedly
    4808             :          * split.
    4809             :          */
    4810         640 :         resfactor = (ores * 100);
    4811         640 :         do_div(resfactor, nres);
    4812         640 :         len1 *= resfactor;
    4813             :         do_div(len1, 100);
    4814             :         len2 *= resfactor;
    4815         640 :         do_div(len2, 100);
    4816             :         ASSERT(len1 + len2 <= ores);
    4817             :         ASSERT(len1 < *indlen1 && len2 < *indlen2);
    4818             : 
    4819             :         /*
    4820             :          * Hand out the remainder to each extent. If one of the two reservations
    4821             :          * is zero, we want to make sure that one gets a block first. The loop
    4822             :          * below starts with len1, so hand len2 a block right off the bat if it
    4823             :          * is zero.
    4824             :          */
    4825             :         ores -= (len1 + len2);
    4826         116 :         ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
    4827         116 :         if (ores && !len2 && *indlen2) {
    4828         116 :                 len2++;
    4829         116 :                 ores--;
    4830         116 :         }
    4831         116 :         while (ores) {
    4832         116 :                 if (len1 < *indlen1) {
    4833         116 :                         len1++;
    4834             :                         ores--;
    4835             :                 }
    4836             :                 if (!ores)
    4837             :                         break;
    4838             :                 if (len2 < *indlen2) {
    4839             :                         len2++;
    4840             :                         ores--;
    4841         116 :                 }
    4842         116 :         }
    4843         116 : 
    4844           0 :         *indlen1 = len1;
    4845           0 :         *indlen2 = len2;
    4846             : 
    4847         186 :         return stolen;
    4848         116 : }
    4849         116 : 
    4850         116 : int
    4851             : xfs_bmap_del_extent_delay(
    4852         116 :         struct xfs_inode        *ip,
    4853             :         int                     whichfork,
    4854          70 :         struct xfs_iext_cursor  *icur,
    4855          70 :         struct xfs_bmbt_irec    *got,
    4856          70 :         struct xfs_bmbt_irec    *del)
    4857             : {
    4858             :         struct xfs_mount        *mp = ip->i_mount;
    4859             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    4860         116 :         struct xfs_bmbt_irec    new;
    4861         116 :         int64_t                 da_old, da_new, da_diff = 0;
    4862             :         xfs_fileoff_t           del_endoff, got_endoff;
    4863         116 :         xfs_filblks_t           got_indlen, new_indlen, stolen;
    4864             :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    4865             :         int                     error = 0;
    4866             :         bool                    isrt;
    4867     2011234 : 
    4868             :         XFS_STATS_INC(mp, xs_del_exlist);
    4869             : 
    4870             :         isrt = xfs_ifork_is_realtime(ip, whichfork);
    4871             :         del_endoff = del->br_startoff + del->br_blockcount;
    4872             :         got_endoff = got->br_startoff + got->br_blockcount;
    4873             :         da_old = startblockval(got->br_startblock);
    4874     2011234 :         da_new = 0;
    4875     2011234 : 
    4876     2011198 :         ASSERT(del->br_blockcount > 0);
    4877     2011198 :         ASSERT(got->br_startoff <= del->br_startoff);
    4878     2011198 :         ASSERT(got_endoff >= del_endoff);
    4879     2011198 : 
    4880     2011198 :         if (isrt)
    4881     2011198 :                 xfs_mod_frextents(mp, xfs_rtb_to_rtxt(mp, del->br_blockcount));
    4882     2011198 : 
    4883             :         /*
    4884     2011198 :          * Update the inode delalloc counter now and wait to update the
    4885             :          * sb counters as we might have to borrow some blocks for the
    4886     2011198 :          * indirect block accounting.
    4887     2011198 :          */
    4888     2011198 :         ASSERT(!isrt);
    4889     2011198 :         error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
    4890     2011198 :         if (error)
    4891             :                 return error;
    4892     2011198 :         ip->i_delayed_blks -= del->br_blockcount;
    4893     2011198 : 
    4894     2011198 :         if (got->br_startoff == del->br_startoff)
    4895             :                 state |= BMAP_LEFT_FILLING;
    4896     2011198 :         if (got_endoff == del_endoff)
    4897           0 :                 state |= BMAP_RIGHT_FILLING;
    4898             : 
    4899             :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    4900             :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    4901             :                 /*
    4902             :                  * Matches the whole extent.  Delete the entry.
    4903             :                  */
    4904     2011198 :                 xfs_iext_remove(ip, icur, state);
    4905     2011198 :                 xfs_iext_prev(ifp, icur);
    4906     2011361 :                 break;
    4907             :         case BMAP_LEFT_FILLING:
    4908     2011361 :                 /*
    4909             :                  * Deleting the first part of the extent.
    4910     2011361 :                  */
    4911     1321485 :                 got->br_startoff = del_endoff;
    4912     2011361 :                 got->br_blockcount -= del->br_blockcount;
    4913     2009303 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4914             :                                 got->br_blockcount), da_old);
    4915     2011361 :                 got->br_startblock = nullstartblock((int)da_new);
    4916     1320051 :                 xfs_iext_update_extent(ip, state, icur, got);
    4917             :                 break;
    4918             :         case BMAP_RIGHT_FILLING:
    4919             :                 /*
    4920     1320051 :                  * Deleting the last part of the extent.
    4921     1319968 :                  */
    4922     1319968 :                 got->br_blockcount = got->br_blockcount - del->br_blockcount;
    4923        1425 :                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
    4924             :                                 got->br_blockcount), da_old);
    4925             :                 got->br_startblock = nullstartblock((int)da_new);
    4926             :                 xfs_iext_update_extent(ip, state, icur, got);
    4927        1425 :                 break;
    4928        1425 :         case 0:
    4929        1425 :                 /*
    4930             :                  * Deleting the middle of the extent.
    4931        1425 :                  *
    4932        1425 :                  * Distribute the original indlen reservation across the two new
    4933        1425 :                  * extents.  Steal blocks from the deleted extent if necessary.
    4934      689245 :                  * Stealing blocks simply fudges the fdblocks accounting below.
    4935             :                  * Warn if either of the new indlen reservations is zero as this
    4936             :                  * can lead to delalloc problems.
    4937             :                  */
    4938      689245 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    4939      689245 :                 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
    4940             : 
    4941      689245 :                 new.br_blockcount = got_endoff - del_endoff;
    4942      689245 :                 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
    4943      689245 : 
    4944         640 :                 WARN_ON_ONCE(!got_indlen || !new_indlen);
    4945             :                 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
    4946             :                                                        del->br_blockcount);
    4947             : 
    4948             :                 got->br_startblock = nullstartblock((int)got_indlen);
    4949             : 
    4950             :                 new.br_startoff = del_endoff;
    4951             :                 new.br_state = got->br_state;
    4952             :                 new.br_startblock = nullstartblock((int)new_indlen);
    4953             : 
    4954         640 :                 xfs_iext_update_extent(ip, state, icur, got);
    4955         640 :                 xfs_iext_next(ifp, icur);
    4956             :                 xfs_iext_insert(ip, icur, &new, state);
    4957         640 : 
    4958         640 :                 da_new = got_indlen + new_indlen - stolen;
    4959             :                 del->br_blockcount -= stolen;
    4960         640 :                 break;
    4961         640 :         }
    4962             : 
    4963             :         ASSERT(da_old >= da_new);
    4964         640 :         da_diff = da_old - da_new;
    4965             :         if (!isrt)
    4966         640 :                 da_diff += del->br_blockcount;
    4967         640 :         if (da_diff) {
    4968         640 :                 xfs_mod_fdblocks(mp, da_diff, false);
    4969             :                 xfs_mod_delalloc(mp, -da_diff);
    4970         640 :         }
    4971         640 :         return error;
    4972         640 : }
    4973             : 
    4974         640 : void
    4975         640 : xfs_bmap_del_extent_cow(
    4976         640 :         struct xfs_inode        *ip,
    4977             :         struct xfs_iext_cursor  *icur,
    4978             :         struct xfs_bmbt_irec    *got,
    4979     2011260 :         struct xfs_bmbt_irec    *del)
    4980     2011260 : {
    4981     2011260 :         struct xfs_mount        *mp = ip->i_mount;
    4982     2011282 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
    4983     2011260 :         struct xfs_bmbt_irec    new;
    4984     2011163 :         xfs_fileoff_t           del_endoff, got_endoff;
    4985     2011139 :         uint32_t                state = BMAP_COWFORK;
    4986             : 
    4987             :         XFS_STATS_INC(mp, xs_del_exlist);
    4988             : 
    4989             :         del_endoff = del->br_startoff + del->br_blockcount;
    4990             :         got_endoff = got->br_startoff + got->br_blockcount;
    4991     3461463 : 
    4992             :         ASSERT(del->br_blockcount > 0);
    4993             :         ASSERT(got->br_startoff <= del->br_startoff);
    4994             :         ASSERT(got_endoff >= del_endoff);
    4995             :         ASSERT(!isnullstartblock(got->br_startblock));
    4996             : 
    4997     3461463 :         if (got->br_startoff == del->br_startoff)
    4998     3461463 :                 state |= BMAP_LEFT_FILLING;
    4999     3461463 :         if (got_endoff == del_endoff)
    5000     3461463 :                 state |= BMAP_RIGHT_FILLING;
    5001     3461463 : 
    5002             :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5003     3461463 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5004             :                 /*
    5005     3461463 :                  * Matches the whole extent.  Delete the entry.
    5006     3461463 :                  */
    5007             :                 xfs_iext_remove(ip, icur, state);
    5008     3461463 :                 xfs_iext_prev(ifp, icur);
    5009     3461463 :                 break;
    5010     3461463 :         case BMAP_LEFT_FILLING:
    5011     3461463 :                 /*
    5012             :                  * Deleting the first part of the extent.
    5013     3461463 :                  */
    5014     3432896 :                 got->br_startoff = del_endoff;
    5015     3461463 :                 got->br_blockcount -= del->br_blockcount;
    5016     2372807 :                 got->br_startblock = del->br_startblock + del->br_blockcount;
    5017             :                 xfs_iext_update_extent(ip, state, icur, got);
    5018     3461463 :                 break;
    5019     2345398 :         case BMAP_RIGHT_FILLING:
    5020             :                 /*
    5021             :                  * Deleting the last part of the extent.
    5022             :                  */
    5023     2345398 :                 got->br_blockcount -= del->br_blockcount;
    5024     2345398 :                 xfs_iext_update_extent(ip, state, icur, got);
    5025     2345398 :                 break;
    5026     1087503 :         case 0:
    5027             :                 /*
    5028             :                  * Deleting the middle of the extent.
    5029             :                  */
    5030     1087503 :                 got->br_blockcount = del->br_startoff - got->br_startoff;
    5031     1087503 : 
    5032     1087503 :                 new.br_startoff = del_endoff;
    5033     1087503 :                 new.br_blockcount = got_endoff - del_endoff;
    5034     1087503 :                 new.br_state = got->br_state;
    5035       27409 :                 new.br_startblock = del->br_startblock + del->br_blockcount;
    5036             : 
    5037             :                 xfs_iext_update_extent(ip, state, icur, got);
    5038             :                 xfs_iext_next(ifp, icur);
    5039       27409 :                 xfs_iext_insert(ip, icur, &new, state);
    5040       27409 :                 break;
    5041       27409 :         }
    5042        1153 :         ip->i_delayed_blks -= del->br_blockcount;
    5043             : }
    5044             : 
    5045             : /*
    5046        1153 :  * Called by xfs_bmapi to update file extent records and the btree
    5047             :  * after removing space.
    5048        1153 :  */
    5049        1153 : STATIC int                              /* error */
    5050        1153 : xfs_bmap_del_extent_real(
    5051        1153 :         xfs_inode_t             *ip,    /* incore inode pointer */
    5052             :         xfs_trans_t             *tp,    /* current transaction pointer */
    5053        1153 :         struct xfs_iext_cursor  *icur,
    5054        1153 :         struct xfs_btree_cur    *cur,   /* if null, not a btree */
    5055        1153 :         xfs_bmbt_irec_t         *del,   /* data to remove from extents */
    5056        1153 :         int                     *logflagsp, /* inode logging flags */
    5057             :         int                     whichfork, /* data or attr fork */
    5058     3461463 :         uint32_t                bflags) /* bmapi flags */
    5059     3461463 : {
    5060             :         xfs_fsblock_t           del_endblock=0; /* first block past del */
    5061             :         xfs_fileoff_t           del_endoff;     /* first offset past del */
    5062             :         int                     error;  /* error return value */
    5063             :         int                     flags = 0;/* inode logging flags */
    5064             :         struct xfs_bmbt_irec    got;    /* current extent entry */
    5065             :         xfs_fileoff_t           got_endoff;     /* first offset past got */
    5066   117371053 :         int                     i;      /* temp state */
    5067             :         struct xfs_ifork        *ifp;   /* inode fork pointer */
    5068             :         xfs_mount_t             *mp;    /* mount structure */
    5069             :         xfs_filblks_t           nblks;  /* quota/sb block count */
    5070             :         xfs_bmbt_irec_t         new;    /* new record to be inserted */
    5071             :         /* REFERENCED */
    5072             :         uint                    qfield; /* quota field to update */
    5073             :         uint32_t                state = xfs_bmap_fork_to_state(whichfork);
    5074             :         struct xfs_bmbt_irec    old;
    5075             :         bool                    isrt = xfs_ifork_is_realtime(ip, whichfork);
    5076   117371053 :         bool                    want_free = !(bflags & XFS_BMAPI_REMAP);
    5077   117371053 : 
    5078   117371053 :         mp = ip->i_mount;
    5079   117371053 :         XFS_STATS_INC(mp, xs_del_exlist);
    5080   117371053 : 
    5081   117371053 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5082   117371053 :         ASSERT(del->br_blockcount > 0);
    5083   117371053 :         xfs_iext_get_extent(ifp, icur, &got);
    5084   117371053 :         ASSERT(got.br_startoff <= del->br_startoff);
    5085   117371053 :         del_endoff = del->br_startoff + del->br_blockcount;
    5086   117371053 :         got_endoff = got.br_startoff + got.br_blockcount;
    5087             :         ASSERT(got_endoff >= del_endoff);
    5088   117371053 :         ASSERT(!isnullstartblock(got.br_startblock));
    5089   117371053 :         qfield = 0;
    5090   117371053 :         error = 0;
    5091   117371053 : 
    5092   117372983 :         /*
    5093             :          * If it's the case where the directory code is running with no block
    5094   117372983 :          * reservation, and the deleted block is in the middle of its extent,
    5095   117372983 :          * and the resulting insert of an extent would cause transformation to
    5096             :          * btree format, then reject it.  The calling code will then swap blocks
    5097   117372983 :          * around instead.  We have to do this now, rather than waiting for the
    5098   117370869 :          * conversion to btree format, since the transaction will be dirty then.
    5099   117370869 :          */
    5100   117371398 :         if (tp->t_blk_res == 0 &&
    5101   117371398 :             ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
    5102   117371398 :             ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
    5103   117371398 :             del->br_startoff > got.br_startoff && del_endoff < got_endoff)
    5104   117371398 :                 return -ENOSPC;
    5105   117371398 : 
    5106   117371398 :         flags = XFS_ILOG_CORE;
    5107             :         if (isrt) {
    5108             :                 /*
    5109             :                  * Historically, we did not use EFIs to free realtime extents.
    5110             :                  * However, when reverse mapping is enabled, we must maintain
    5111             :                  * the same order of operations as the data device, which is:
    5112             :                  * Remove the file mapping, remove the reverse mapping, and
    5113             :                  * then free the blocks.  This means that we must delay the
    5114             :                  * freeing until after we've scheduled the rmap update.  If
    5115             :                  * realtime reflink is enabled, use deferred refcount intent
    5116   117371398 :                  * items to decide what to do with the extent, just like we do
    5117    83368828 :                  * for the data device.
    5118    34811639 :                  */
    5119      278800 :                 if (want_free && !xfs_has_rtrmapbt(mp) &&
    5120             :                                  !xfs_has_rtreflink(mp)) {
    5121             :                         error = xfs_rtfree_blocks(tp, del->br_startblock,
    5122   117371398 :                                         del->br_blockcount);
    5123   117371398 :                         if (error)
    5124             :                                 goto done;
    5125             :                         want_free = false;
    5126             :                 }
    5127             :                 qfield = XFS_TRANS_DQ_RTBCOUNT;
    5128             :         } else {
    5129             :                 qfield = XFS_TRANS_DQ_BCOUNT;
    5130             :         }
    5131             :         nblks = del->br_blockcount;
    5132             : 
    5133             :         del_endblock = del->br_startblock + del->br_blockcount;
    5134             :         if (cur) {
    5135    21317830 :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5136           0 :                 if (error)
    5137           0 :                         goto done;
    5138             :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5139           0 :                         xfs_btree_mark_sick(cur);
    5140           0 :                         error = -EFSCORRUPTED;
    5141             :                         goto done;
    5142             :                 }
    5143             :         }
    5144             : 
    5145             :         if (got.br_startoff == del->br_startoff)
    5146             :                 state |= BMAP_LEFT_FILLING;
    5147   117371398 :         if (got_endoff == del_endoff)
    5148             :                 state |= BMAP_RIGHT_FILLING;
    5149   117371398 : 
    5150   117371398 :         switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
    5151    67658089 :         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
    5152    67658095 :                 /*
    5153           0 :                  * Matches the whole extent.  Delete the entry.
    5154    67658095 :                  */
    5155           0 :                 xfs_iext_remove(ip, icur, state);
    5156           0 :                 xfs_iext_prev(ifp, icur);
    5157           0 :                 ifp->if_nextents--;
    5158             : 
    5159             :                 flags |= XFS_ILOG_CORE;
    5160             :                 if (!cur) {
    5161   117371404 :                         flags |= xfs_ilog_fext(whichfork);
    5162   104184508 :                         break;
    5163   117371404 :                 }
    5164   102694405 :                 if ((error = xfs_btree_delete(cur, &i)))
    5165             :                         goto done;
    5166   117371404 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5167    98845639 :                         xfs_btree_mark_sick(cur);
    5168             :                         error = -EFSCORRUPTED;
    5169             :                         goto done;
    5170             :                 }
    5171    98845639 :                 break;
    5172    98845755 :         case BMAP_LEFT_FILLING:
    5173    98845480 :                 /*
    5174             :                  * Deleting the first part of the extent.
    5175    98845480 :                  */
    5176    98845480 :                 got.br_startoff = del_endoff;
    5177    43337128 :                 got.br_startblock = del_endblock;
    5178    43337128 :                 got.br_blockcount -= del->br_blockcount;
    5179             :                 xfs_iext_update_extent(ip, state, icur, &got);
    5180    55508352 :                 if (!cur) {
    5181           0 :                         flags |= xfs_ilog_fext(whichfork);
    5182    55508348 :                         break;
    5183           0 :                 }
    5184           0 :                 error = xfs_bmbt_update(cur, &got);
    5185           0 :                 if (error)
    5186             :                         goto done;
    5187             :                 break;
    5188     5337483 :         case BMAP_RIGHT_FILLING:
    5189             :                 /*
    5190             :                  * Deleting the last part of the extent.
    5191             :                  */
    5192     5337483 :                 got.br_blockcount -= del->br_blockcount;
    5193     5337483 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5194     5337483 :                 if (!cur) {
    5195     5337483 :                         flags |= xfs_ilog_fext(whichfork);
    5196     5337482 :                         break;
    5197     2375838 :                 }
    5198     2375838 :                 error = xfs_bmbt_update(cur, &got);
    5199             :                 if (error)
    5200     2961644 :                         goto done;
    5201     2961636 :                 break;
    5202           0 :         case 0:
    5203             :                 /*
    5204     3847291 :                  * Deleting the middle of the extent.
    5205             :                  */
    5206             : 
    5207             :                 old = got;
    5208     3847291 : 
    5209     3847291 :                 got.br_blockcount = del->br_startoff - got.br_startoff;
    5210     3847291 :                 xfs_iext_update_extent(ip, state, icur, &got);
    5211     1823223 : 
    5212     1823223 :                 new.br_startoff = del_endoff;
    5213             :                 new.br_blockcount = got_endoff - del_endoff;
    5214     2024068 :                 new.br_state = got.br_state;
    5215     2024068 :                 new.br_startblock = del_endblock;
    5216           0 : 
    5217             :                 flags |= XFS_ILOG_CORE;
    5218     9340991 :                 if (cur) {
    5219             :                         error = xfs_bmbt_update(cur, &got);
    5220             :                         if (error)
    5221             :                                 goto done;
    5222             :                         error = xfs_btree_increment(cur, 0, &i);
    5223     9340991 :                         if (error)
    5224             :                                 goto done;
    5225     9340991 :                         cur->bc_rec.b = new;
    5226     9340991 :                         error = xfs_btree_insert(cur, &i);
    5227             :                         if (error && error != -ENOSPC)
    5228     9340992 :                                 goto done;
    5229     9340992 :                         /*
    5230     9340992 :                          * If get no-space back from btree insert, it tried a
    5231     9340992 :                          * split, and we have a zero block reservation.  Fix up
    5232             :                          * our state and return the error.
    5233     9340992 :                          */
    5234     9340992 :                         if (error == -ENOSPC) {
    5235     7164028 :                                 /*
    5236     7164028 :                                  * Reset the cursor, don't trust it after any
    5237           0 :                                  * insert operation.
    5238     7164028 :                                  */
    5239     7164028 :                                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    5240           0 :                                 if (error)
    5241     7164028 :                                         goto done;
    5242     7164028 :                                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5243     7164028 :                                         xfs_btree_mark_sick(cur);
    5244           0 :                                         error = -EFSCORRUPTED;
    5245             :                                         goto done;
    5246             :                                 }
    5247             :                                 /*
    5248             :                                  * Update the btree record back
    5249             :                                  * to the original value.
    5250     7164028 :                                  */
    5251             :                                 error = xfs_bmbt_update(cur, &old);
    5252             :                                 if (error)
    5253             :                                         goto done;
    5254             :                                 /*
    5255           0 :                                  * Reset the extent record back
    5256           0 :                                  * to the original value.
    5257           0 :                                  */
    5258           0 :                                 xfs_iext_update_extent(ip, state, icur, &old);
    5259           0 :                                 flags = 0;
    5260           0 :                                 error = -ENOSPC;
    5261           0 :                                 goto done;
    5262             :                         }
    5263             :                         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5264             :                                 xfs_btree_mark_sick(cur);
    5265             :                                 error = -EFSCORRUPTED;
    5266             :                                 goto done;
    5267           0 :                         }
    5268           0 :                 } else
    5269           0 :                         flags |= xfs_ilog_fext(whichfork);
    5270             : 
    5271             :                 ifp->if_nextents++;
    5272             :                 xfs_iext_next(ifp, icur);
    5273             :                 xfs_iext_insert(ip, icur, &new, state);
    5274           0 :                 break;
    5275           0 :         }
    5276           0 : 
    5277           0 :         /* remove reverse mapping */
    5278             :         xfs_rmap_unmap_extent(tp, ip, whichfork, del);
    5279     7164028 : 
    5280           0 :         /*
    5281           0 :          * If we need to, add to list of extents to delete.
    5282           0 :          */
    5283             :         if (want_free) {
    5284             :                 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
    5285     2176981 :                         xfs_refcount_decrease_extent(tp, isrt, del);
    5286             :                 } else {
    5287     9340992 :                         unsigned int    efi_flags = 0;
    5288     9340992 : 
    5289     9340991 :                         if ((bflags & XFS_BMAPI_NODISCARD) ||
    5290     9340991 :                             del->br_state == XFS_EXT_UNWRITTEN)
    5291             :                                 efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
    5292             :                         if (isrt)
    5293             :                                 efi_flags |= XFS_FREE_EXTENT_REALTIME;
    5294   117371231 : 
    5295             :                         error = xfs_free_extent_later(tp, del->br_startblock,
    5296             :                                         del->br_blockcount, NULL,
    5297             :                                         XFS_AG_RESV_NONE, efi_flags);
    5298             :                         if (error)
    5299   117372902 :                                 goto done;
    5300    97323534 :                 }
    5301    61548240 :         }
    5302             : 
    5303    35775294 :         /*
    5304             :          * Adjust inode # blocks in the file.
    5305    35775294 :          */
    5306    35753802 :         if (nblks)
    5307     2416006 :                 ip->i_nblocks -= nblks;
    5308    35775294 :         /*
    5309     1763015 :          * Adjust quota data.
    5310             :          */
    5311    35775294 :         if (qfield && !(bflags & XFS_BMAPI_REMAP))
    5312             :                 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
    5313             : 
    5314    35775763 : done:
    5315           0 :         *logflagsp = flags;
    5316             :         return error;
    5317             : }
    5318             : 
    5319             : /*
    5320             :  * Unmap (remove) blocks from a file.
    5321             :  * If nexts is nonzero then the number of extents to remove is limited to
    5322   117373378 :  * that value.  If not all extents in the block range can be removed then
    5323   117373246 :  * *done is set.
    5324             :  */
    5325             : int                                             /* error */
    5326             : __xfs_bunmapi(
    5327   117373378 :         struct xfs_trans        *tp,            /* transaction pointer */
    5328    97324922 :         struct xfs_inode        *ip,            /* incore inode */
    5329             :         xfs_fileoff_t           start,          /* first file offset deleted */
    5330    20048456 :         xfs_filblks_t           *rlen,          /* i/o: amount remaining */
    5331   117373142 :         uint32_t                flags,          /* misc flags */
    5332   117373142 :         xfs_extnum_t            nexts)          /* number of extents max */
    5333             : {
    5334             :         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
    5335             :         struct xfs_bmbt_irec    del;            /* extent being deleted */
    5336             :         int                     error;          /* error return value */
    5337             :         xfs_extnum_t            extno;          /* extent number in list */
    5338             :         struct xfs_bmbt_irec    got;            /* current extent record */
    5339             :         struct xfs_ifork        *ifp;           /* inode fork pointer */
    5340             :         int                     isrt;           /* freeing in rt area */
    5341             :         int                     logflags;       /* transaction logging flags */
    5342    99306503 :         xfs_extlen_t            mod;            /* rt extent offset */
    5343             :         struct xfs_mount        *mp = ip->i_mount;
    5344             :         int                     tmp_logflags;   /* partial logging flags */
    5345             :         int                     wasdel;         /* was a delayed alloc extent */
    5346             :         int                     whichfork;      /* data or attribute fork */
    5347             :         xfs_filblks_t           len = *rlen;    /* length to unmap in file */
    5348             :         xfs_fileoff_t           end;
    5349             :         struct xfs_iext_cursor  icur;
    5350    99306503 :         bool                    done = false;
    5351    99306503 : 
    5352    99306503 :         trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
    5353    99306503 : 
    5354    99306503 :         whichfork = xfs_bmapi_whichfork(flags);
    5355    99306503 :         ASSERT(whichfork != XFS_COW_FORK);
    5356    99306503 :         ifp = xfs_ifork_ptr(ip, whichfork);
    5357    99306503 :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
    5358    99306503 :                 xfs_bmap_mark_sick(ip, whichfork);
    5359    99306503 :                 return -EFSCORRUPTED;
    5360    99306503 :         }
    5361    99306503 :         if (xfs_is_shutdown(mp))
    5362    99306503 :                 return -EIO;
    5363    99306503 : 
    5364    99306503 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5365    99306503 :         ASSERT(len > 0);
    5366    99306503 :         ASSERT(nexts >= 0);
    5367             : 
    5368    99306503 :         error = xfs_iread_extents(tp, ip, whichfork);
    5369             :         if (error)
    5370    99307407 :                 return error;
    5371    99307407 : 
    5372    99307407 :         if (xfs_iext_count(ifp) == 0) {
    5373    99307399 :                 *rlen = 0;
    5374           0 :                 return 0;
    5375           0 :         }
    5376             :         XFS_STATS_INC(mp, xs_blk_unmap);
    5377   198614798 :         isrt = xfs_ifork_is_realtime(ip, whichfork);
    5378             :         end = start + len;
    5379             : 
    5380    99307019 :         if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
    5381    99307345 :                 *rlen = 0;
    5382    99307345 :                 return 0;
    5383             :         }
    5384    99307345 :         end--;
    5385    99308540 : 
    5386             :         logflags = 0;
    5387             :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5388    99307325 :                 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
    5389      954811 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5390      954811 :                 cur->bc_ino.flags = 0;
    5391             :         } else
    5392    98351903 :                 cur = NULL;
    5393    98351903 : 
    5394    98350789 :         if (isrt) {
    5395             :                 /*
    5396    98350789 :                  * Synchronize by locking the realtime bitmap.
    5397      258712 :                  */
    5398      258712 :                 xfs_rtbitmap_lock(tp, mp);
    5399             :         }
    5400    98091066 : 
    5401             :         extno = 0;
    5402    98091066 :         while (end != (xfs_fileoff_t)-1 && end >= start &&
    5403    98091066 :                (nexts == 0 || extno < nexts)) {
    5404    43806667 :                 /*
    5405    43806667 :                  * Is the found extent after a hole in which end lives?
    5406    43806442 :                  * Just back up to the previous extent, if so.
    5407             :                  */
    5408    54284399 :                 if (got.br_startoff > end &&
    5409             :                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5410    98090841 :                         done = true;
    5411             :                         break;
    5412             :                 }
    5413             :                 /*
    5414    18634873 :                  * Is the last block of this extent before the range
    5415             :                  * we're supposed to delete?  If so, we're done.
    5416             :                  */
    5417             :                 end = XFS_FILEOFF_MIN(end,
    5418   216130533 :                         got.br_startoff + got.br_blockcount - 1);
    5419             :                 if (end < start)
    5420             :                         break;
    5421             :                 /*
    5422             :                  * Then deal with the (possibly delayed) allocated space
    5423             :                  * we found.
    5424   120409559 :                  */
    5425           0 :                 del = got;
    5426             :                 wasdel = isnullstartblock(del.br_startblock);
    5427             : 
    5428             :                 if (got.br_startoff < start) {
    5429             :                         del.br_startoff = start;
    5430             :                         del.br_blockcount -= start - got.br_startoff;
    5431             :                         if (!wasdel)
    5432             :                                 del.br_startblock += start - got.br_startoff;
    5433   120409559 :                 }
    5434             :                 if (del.br_startoff + del.br_blockcount > end + 1)
    5435   120409559 :                         del.br_blockcount = end + 1 - del.br_startoff;
    5436             : 
    5437             :                 if (!isrt || (flags & XFS_BMAPI_REMAP))
    5438             :                         goto delete;
    5439             : 
    5440             :                 xfs_rtb_to_rtx(mp, del.br_startblock + del.br_blockcount, &mod);
    5441   119357740 :                 if (mod) {
    5442   119357740 :                         /*
    5443             :                          * Realtime extent not lined up at the end.
    5444   119357740 :                          * The extent could have been split into written
    5445    13865041 :                          * and unwritten pieces, or we could just be
    5446    13865041 :                          * unmapping part of it.  But we can't really
    5447    13865041 :                          * get rid of part of a realtime extent.
    5448    13188281 :                          */
    5449             :                         if (del.br_state == XFS_EXT_UNWRITTEN) {
    5450   119357740 :                                 /*
    5451    14679101 :                                  * This piece is unwritten, or we're not
    5452             :                                  * using unwritten extents.  Skip over it.
    5453   119357740 :                                  */
    5454   111327723 :                                 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
    5455             :                                 end -= mod > del.br_blockcount ?
    5456     8030017 :                                         del.br_blockcount : mod;
    5457     8030017 :                                 if (end < got.br_startoff &&
    5458             :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5459             :                                         done = true;
    5460             :                                         break;
    5461             :                                 }
    5462             :                                 continue;
    5463             :                         }
    5464             :                         /*
    5465           0 :                          * It's written, turn it unwritten.
    5466             :                          * This is better than zeroing it.
    5467             :                          */
    5468             :                         ASSERT(del.br_state == XFS_EXT_NORM);
    5469             :                         ASSERT(tp->t_blk_res > 0);
    5470           0 :                         /*
    5471           0 :                          * If this spans a realtime extent boundary,
    5472           0 :                          * chop it back to the start of the one we end at.
    5473           0 :                          */
    5474           0 :                         if (del.br_blockcount > mod) {
    5475             :                                 del.br_startoff += del.br_blockcount - mod;
    5476             :                                 del.br_startblock += del.br_blockcount - mod;
    5477             :                                 del.br_blockcount = mod;
    5478           0 :                         }
    5479             :                         del.br_state = XFS_EXT_UNWRITTEN;
    5480             :                         error = xfs_bmap_add_extent_unwritten_real(tp, ip,
    5481             :                                         whichfork, &icur, &cur, &del,
    5482             :                                         &logflags);
    5483             :                         if (error)
    5484           0 :                                 goto error0;
    5485           0 :                         goto nodelete;
    5486             :                 }
    5487             : 
    5488             :                 xfs_rtb_to_rtx(mp, del.br_startblock, &mod);
    5489             :                 if (mod) {
    5490           0 :                         xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
    5491           0 : 
    5492           0 :                         /*
    5493           0 :                          * Realtime extent is lined up at the end but not
    5494             :                          * at the front.  We'll get rid of full extents if
    5495           0 :                          * we can.
    5496           0 :                          */
    5497             :                         if (del.br_blockcount > off) {
    5498             :                                 del.br_blockcount -= off;
    5499           0 :                                 del.br_startoff += off;
    5500           0 :                                 del.br_startblock += off;
    5501           0 :                         } else if (del.br_startoff == start &&
    5502             :                                    (del.br_state == XFS_EXT_UNWRITTEN ||
    5503             :                                     tp->t_blk_res == 0)) {
    5504     8030017 :                                 /*
    5505     8030017 :                                  * Can't make it unwritten.  There isn't
    5506           2 :                                  * a full extent here so just skip it.
    5507             :                                  */
    5508             :                                 ASSERT(end >= del.br_blockcount);
    5509             :                                 end -= del.br_blockcount;
    5510             :                                 if (got.br_startoff > end &&
    5511             :                                     !xfs_iext_prev_extent(ifp, &icur, &got)) {
    5512             :                                         done = true;
    5513           2 :                                         break;
    5514           0 :                                 }
    5515           0 :                                 continue;
    5516           0 :                         } else if (del.br_state == XFS_EXT_UNWRITTEN) {
    5517           2 :                                 struct xfs_bmbt_irec    prev;
    5518           0 :                                 xfs_fileoff_t           unwrite_start;
    5519           0 : 
    5520             :                                 /*
    5521             :                                  * This one is already unwritten.
    5522             :                                  * It must have a written left neighbor.
    5523             :                                  * Unwrite the killed part of that one and
    5524           0 :                                  * try again.
    5525           0 :                                  */
    5526           0 :                                 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
    5527           0 :                                         ASSERT(0);
    5528             :                                 ASSERT(prev.br_state == XFS_EXT_NORM);
    5529             :                                 ASSERT(!isnullstartblock(prev.br_startblock));
    5530             :                                 ASSERT(del.br_startblock ==
    5531           0 :                                        prev.br_startblock + prev.br_blockcount);
    5532           2 :                                 unwrite_start = max3(start,
    5533           2 :                                                      del.br_startoff - mod,
    5534           2 :                                                      prev.br_startoff);
    5535             :                                 mod = unwrite_start - prev.br_startoff;
    5536             :                                 prev.br_startoff = unwrite_start;
    5537             :                                 prev.br_startblock += mod;
    5538             :                                 prev.br_blockcount -= mod;
    5539             :                                 prev.br_state = XFS_EXT_UNWRITTEN;
    5540             :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5541             :                                                 ip, whichfork, &icur, &cur,
    5542           2 :                                                 &prev, &logflags);
    5543           0 :                                 if (error)
    5544           2 :                                         goto error0;
    5545           2 :                                 goto nodelete;
    5546           2 :                         } else {
    5547             :                                 ASSERT(del.br_state == XFS_EXT_NORM);
    5548           2 :                                 del.br_state = XFS_EXT_UNWRITTEN;
    5549             :                                 error = xfs_bmap_add_extent_unwritten_real(tp,
    5550             :                                                 ip, whichfork, &icur, &cur,
    5551           2 :                                                 &del, &logflags);
    5552           2 :                                 if (error)
    5553           2 :                                         goto error0;
    5554           2 :                                 goto nodelete;
    5555           2 :                         }
    5556           2 :                 }
    5557             : 
    5558             : delete:
    5559           2 :                 if (wasdel) {
    5560           0 :                         error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
    5561           2 :                                         &got, &del);
    5562             :                 } else {
    5563           0 :                         error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
    5564           0 :                                         &del, &tmp_logflags, whichfork,
    5565           0 :                                         flags);
    5566             :                         logflags |= tmp_logflags;
    5567             :                 }
    5568           0 : 
    5569           0 :                 if (error)
    5570           0 :                         goto error0;
    5571             : 
    5572             :                 end = del.br_startoff - 1;
    5573             : nodelete:
    5574     8030015 :                 /*
    5575   119357738 :                  * If not done go on to the next (previous) record.
    5576     1985273 :                  */
    5577             :                 if (end != (xfs_fileoff_t)-1 && end >= start) {
    5578             :                         if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5579   117372465 :                             (got.br_startoff > end &&
    5580             :                              !xfs_iext_prev_extent(ifp, &icur, &got))) {
    5581             :                                 done = true;
    5582   117373366 :                                 break;
    5583             :                         }
    5584             :                         extno++;
    5585   119360926 :                 }
    5586           0 :         }
    5587             :         if (done || end == (xfs_fileoff_t)-1 || end < start)
    5588   119360926 :                 *rlen = 0;
    5589   119360928 :         else
    5590             :                 *rlen = end - start + 1;
    5591             : 
    5592             :         /*
    5593   119360928 :          * Convert to a btree if necessary.
    5594    61165453 :          */
    5595    60408640 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    5596      525221 :                 ASSERT(cur == NULL);
    5597             :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    5598             :                                 &tmp_logflags, whichfork);
    5599             :                 logflags |= tmp_logflags;
    5600    59843844 :         } else {
    5601             :                 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
    5602             :                         whichfork);
    5603    96772793 :         }
    5604    69229823 : 
    5605             : error0:
    5606    28864614 :         /*
    5607             :          * Log everything.  Do this after conversion, there's no point in
    5608             :          * logging the extent records if we've converted to btree format.
    5609             :          */
    5610             :         if ((logflags & xfs_ilog_fext(whichfork)) &&
    5611    98094437 :             ifp->if_format != XFS_DINODE_FMT_EXTENTS)
    5612      107467 :                 logflags &= ~xfs_ilog_fext(whichfork);
    5613      107467 :         else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
    5614             :                  ifp->if_format != XFS_DINODE_FMT_BTREE)
    5615      107467 :                 logflags &= ~xfs_ilog_fbroot(whichfork);
    5616             :         /*
    5617    97985335 :          * Log inode even in the error case, if the transaction
    5618             :          * is dirty we'll need to shut down the filesystem.
    5619             :          */
    5620             :         if (logflags)
    5621    98091451 :                 xfs_trans_log_inode(tp, ip, logflags);
    5622             :         if (cur) {
    5623             :                 if (!error)
    5624             :                         cur->bc_ino.allocated = 0;
    5625             :                 xfs_btree_del_cursor(cur, error);
    5626   104815683 :         }
    5627    45464936 :         return error;
    5628      107466 : }
    5629   104708767 : 
    5630           0 : /* Unmap a range of a file. */
    5631           0 : int
    5632             : xfs_bunmapi(
    5633             :         xfs_trans_t             *tp,
    5634             :         struct xfs_inode        *ip,
    5635             :         xfs_fileoff_t           bno,
    5636    98091451 :         xfs_filblks_t           len,
    5637    87662632 :         uint32_t                flags,
    5638    98093430 :         xfs_extnum_t            nexts,
    5639    43914534 :         int                     *done)
    5640    43914197 : {
    5641    43914534 :         int                     error;
    5642             : 
    5643             :         error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
    5644             :         *done = (len == 0);
    5645             :         return error;
    5646             : }
    5647             : 
    5648    37200295 : /*
    5649             :  * Determine whether an extent shift can be accomplished by a merge with the
    5650             :  * extent that precedes the target hole of the shift.
    5651             :  */
    5652             : STATIC bool
    5653             : xfs_bmse_can_merge(
    5654             :         struct xfs_bmbt_irec    *left,  /* preceding extent */
    5655             :         struct xfs_bmbt_irec    *got,   /* current extent to shift */
    5656             :         xfs_fileoff_t           shift)  /* shift fsb */
    5657    37200295 : {
    5658             :         xfs_fileoff_t           startoff;
    5659    37200295 : 
    5660    37200082 :         startoff = got->br_startoff - shift;
    5661    37200082 : 
    5662             :         /*
    5663             :          * The extent, once shifted, must be adjacent in-file and on-disk with
    5664             :          * the preceding extent.
    5665             :          */
    5666             :         if ((left->br_startoff + left->br_blockcount != startoff) ||
    5667             :             (left->br_startblock + left->br_blockcount != got->br_startblock) ||
    5668             :             (left->br_state != got->br_state) ||
    5669    20828242 :             (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
    5670             :                 return false;
    5671             : 
    5672             :         return true;
    5673             : }
    5674    20828242 : 
    5675             : /*
    5676    20828242 :  * A bmap extent shift adjusts the file offset of an extent to fill a preceding
    5677             :  * hole in the file. If an extent shift would result in the extent being fully
    5678             :  * adjacent to the extent that currently precedes the hole, we can merge with
    5679             :  * the preceding extent rather than do the shift.
    5680             :  *
    5681             :  * This function assumes the caller has verified a shift-by-merge is possible
    5682    20828242 :  * with the provided extents via xfs_bmse_can_merge().
    5683    19315457 :  */
    5684      226719 : STATIC int
    5685        5690 : xfs_bmse_merge(
    5686    20822552 :         struct xfs_trans                *tp,
    5687             :         struct xfs_inode                *ip,
    5688             :         int                             whichfork,
    5689             :         xfs_fileoff_t                   shift,          /* shift fsb */
    5690             :         struct xfs_iext_cursor          *icur,
    5691             :         struct xfs_bmbt_irec            *got,           /* extent to shift */
    5692             :         struct xfs_bmbt_irec            *left,          /* preceding extent */
    5693             :         struct xfs_btree_cur            *cur,
    5694             :         int                             *logflags)      /* output */
    5695             : {
    5696             :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    5697             :         struct xfs_bmbt_irec            new;
    5698             :         xfs_filblks_t                   blockcount;
    5699             :         int                             error, i;
    5700             :         struct xfs_mount                *mp = ip->i_mount;
    5701        2845 : 
    5702             :         blockcount = left->br_blockcount + got->br_blockcount;
    5703             : 
    5704             :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5705             :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    5706             :         ASSERT(xfs_bmse_can_merge(left, got, shift));
    5707             : 
    5708             :         new = *left;
    5709             :         new.br_blockcount = blockcount;
    5710             : 
    5711             :         /*
    5712        2845 :          * Update the on-disk extent count, the btree if necessary and log the
    5713        2845 :          * inode.
    5714        2845 :          */
    5715        2845 :         ifp->if_nextents--;
    5716        2845 :         *logflags |= XFS_ILOG_CORE;
    5717             :         if (!cur) {
    5718        2845 :                 *logflags |= XFS_ILOG_DEXT;
    5719             :                 goto done;
    5720        2845 :         }
    5721        2845 : 
    5722        2845 :         /* lookup and remove the extent to merge */
    5723             :         error = xfs_bmbt_lookup_eq(cur, got, &i);
    5724        2845 :         if (error)
    5725        2845 :                 return error;
    5726             :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5727             :                 xfs_btree_mark_sick(cur);
    5728             :                 return -EFSCORRUPTED;
    5729             :         }
    5730             : 
    5731        2845 :         error = xfs_btree_delete(cur, &i);
    5732        2845 :         if (error)
    5733        2845 :                 return error;
    5734        2435 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5735        2435 :                 xfs_btree_mark_sick(cur);
    5736             :                 return -EFSCORRUPTED;
    5737             :         }
    5738             : 
    5739         410 :         /* lookup and update size of the previous extent */
    5740         410 :         error = xfs_bmbt_lookup_eq(cur, left, &i);
    5741             :         if (error)
    5742         410 :                 return error;
    5743           0 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    5744           0 :                 xfs_btree_mark_sick(cur);
    5745             :                 return -EFSCORRUPTED;
    5746             :         }
    5747         410 : 
    5748         410 :         error = xfs_bmbt_update(cur, &new);
    5749             :         if (error)
    5750         410 :                 return error;
    5751           0 : 
    5752           0 :         /* change to extent format if required after extent removal */
    5753             :         error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
    5754             :         if (error)
    5755             :                 return error;
    5756         410 : 
    5757         410 : done:
    5758             :         xfs_iext_remove(ip, icur, 0);
    5759         410 :         xfs_iext_prev(ifp, icur);
    5760           0 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5761           0 :                         &new);
    5762             : 
    5763             :         /* update reverse mapping. rmap functions merge the rmaps for us */
    5764         410 :         xfs_rmap_unmap_extent(tp, ip, whichfork, got);
    5765         410 :         memcpy(&new, got, sizeof(new));
    5766             :         new.br_startoff = left->br_startoff + left->br_blockcount;
    5767             :         xfs_rmap_map_extent(tp, ip, whichfork, &new);
    5768             :         return 0;
    5769         410 : }
    5770         410 : 
    5771             : static int
    5772             : xfs_bmap_shift_update_extent(
    5773         410 :         struct xfs_trans        *tp,
    5774        2845 :         struct xfs_inode        *ip,
    5775        2845 :         int                     whichfork,
    5776        5690 :         struct xfs_iext_cursor  *icur,
    5777             :         struct xfs_bmbt_irec    *got,
    5778             :         struct xfs_btree_cur    *cur,
    5779             :         int                     *logflags,
    5780        2845 :         xfs_fileoff_t           startoff)
    5781        2845 : {
    5782        2845 :         struct xfs_mount        *mp = ip->i_mount;
    5783        2845 :         struct xfs_bmbt_irec    prev = *got;
    5784        2845 :         int                     error, i;
    5785             : 
    5786             :         *logflags |= XFS_ILOG_CORE;
    5787             : 
    5788    21250935 :         got->br_startoff = startoff;
    5789             : 
    5790             :         if (cur) {
    5791             :                 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
    5792             :                 if (error)
    5793             :                         return error;
    5794             :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    5795             :                         xfs_btree_mark_sick(cur);
    5796             :                         return -EFSCORRUPTED;
    5797             :                 }
    5798    21250935 : 
    5799    21250935 :                 error = xfs_bmbt_update(cur, got);
    5800    21250935 :                 if (error)
    5801             :                         return error;
    5802    21250935 :         } else {
    5803             :                 *logflags |= XFS_ILOG_DEXT;
    5804    21250935 :         }
    5805             : 
    5806    21250935 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
    5807    19375944 :                         got);
    5808    19375944 : 
    5809             :         /* update reverse mapping */
    5810    19375944 :         xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
    5811           0 :         xfs_rmap_map_extent(tp, ip, whichfork, got);
    5812           0 :         return 0;
    5813             : }
    5814             : 
    5815    19375944 : int
    5816    19375944 : xfs_bmap_collapse_extents(
    5817             :         struct xfs_trans        *tp,
    5818             :         struct xfs_inode        *ip,
    5819     1874991 :         xfs_fileoff_t           *next_fsb,
    5820             :         xfs_fileoff_t           offset_shift_fsb,
    5821             :         bool                    *done)
    5822    42501870 : {
    5823             :         int                     whichfork = XFS_DATA_FORK;
    5824             :         struct xfs_mount        *mp = ip->i_mount;
    5825             :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5826    21250935 :         struct xfs_btree_cur    *cur = NULL;
    5827    21250936 :         struct xfs_bmbt_irec    got, prev;
    5828    21250936 :         struct xfs_iext_cursor  icur;
    5829             :         xfs_fileoff_t           new_startoff;
    5830             :         int                     error = 0;
    5831             :         int                     logflags = 0;
    5832    19351633 : 
    5833             :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5834             :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5835             :                 xfs_bmap_mark_sick(ip, whichfork);
    5836             :                 return -EFSCORRUPTED;
    5837             :         }
    5838             : 
    5839    19351633 :         if (xfs_is_shutdown(mp))
    5840    19351633 :                 return -EIO;
    5841    19351633 : 
    5842    19351633 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5843    19351633 : 
    5844    19351633 :         error = xfs_iread_extents(tp, ip, whichfork);
    5845    19351633 :         if (error)
    5846    19351633 :                 return error;
    5847    19351633 : 
    5848             :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5849    38703266 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5850    19351633 :                 cur->bc_ino.flags = 0;
    5851           0 :         }
    5852           0 : 
    5853             :         if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5854             :                 *done = true;
    5855    38703266 :                 goto del_cursor;
    5856             :         }
    5857             :         if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
    5858    19351632 :                 xfs_bmap_mark_sick(ip, whichfork);
    5859             :                 error = -EFSCORRUPTED;
    5860    19351632 :                 goto del_cursor;
    5861    19351632 :         }
    5862             : 
    5863             :         new_startoff = got.br_startoff - offset_shift_fsb;
    5864    19351632 :         if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
    5865    18291037 :                 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
    5866    18291037 :                         error = -EINVAL;
    5867             :                         goto del_cursor;
    5868             :                 }
    5869    19351632 : 
    5870       49930 :                 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
    5871       49930 :                         error = xfs_bmse_merge(tp, ip, whichfork,
    5872             :                                         offset_shift_fsb, &icur, &got, &prev,
    5873    19301702 :                                         cur, &logflags);
    5874           0 :                         if (error)
    5875           0 :                                 goto del_cursor;
    5876           0 :                         goto done;
    5877             :                 }
    5878             :         } else {
    5879    19301702 :                 if (got.br_startoff < offset_shift_fsb) {
    5880    19301702 :                         error = -EINVAL;
    5881    19230479 :                         goto del_cursor;
    5882           0 :                 }
    5883           0 :         }
    5884             : 
    5885             :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    5886    19230479 :                         cur, &logflags, new_startoff);
    5887        2845 :         if (error)
    5888             :                 goto del_cursor;
    5889             : 
    5890        2845 : done:
    5891           0 :         if (!xfs_iext_next_extent(ifp, &icur, &got)) {
    5892        2845 :                 *done = true;
    5893             :                 goto del_cursor;
    5894             :         }
    5895       71223 : 
    5896           0 :         *next_fsb = got.br_startoff;
    5897           0 : del_cursor:
    5898             :         if (cur)
    5899             :                 xfs_btree_del_cursor(cur, error);
    5900             :         if (logflags)
    5901    19298857 :                 xfs_trans_log_inode(tp, ip, logflags);
    5902             :         return error;
    5903    19298857 : }
    5904           0 : 
    5905             : /* Make sure we won't be right-shifting an extent past the maximum bound. */
    5906    19298857 : int
    5907    19301702 : xfs_bmap_can_insert_extents(
    5908      422799 :         struct xfs_inode        *ip,
    5909      422799 :         xfs_fileoff_t           off,
    5910             :         xfs_fileoff_t           shift)
    5911             : {
    5912    18878903 :         struct xfs_bmbt_irec    got;
    5913    19351632 :         int                     is_empty;
    5914    19351632 :         int                     error = 0;
    5915    18291037 : 
    5916    19351632 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    5917    19301702 : 
    5918             :         if (xfs_is_shutdown(ip->i_mount))
    5919             :                 return -EIO;
    5920             : 
    5921             :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    5922             :         error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
    5923      404342 :         if (!error && !is_empty && got.br_startoff >= off &&
    5924             :             ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
    5925             :                 error = -EINVAL;
    5926             :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    5927             : 
    5928      404342 :         return error;
    5929      404342 : }
    5930      404342 : 
    5931             : int
    5932      404342 : xfs_bmap_insert_extents(
    5933             :         struct xfs_trans        *tp,
    5934      808682 :         struct xfs_inode        *ip,
    5935             :         xfs_fileoff_t           *next_fsb,
    5936             :         xfs_fileoff_t           offset_shift_fsb,
    5937      404341 :         bool                    *done,
    5938      404342 :         xfs_fileoff_t           stop_fsb)
    5939      404342 : {
    5940      345097 :         int                     whichfork = XFS_DATA_FORK;
    5941           0 :         struct xfs_mount        *mp = ip->i_mount;
    5942      404342 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
    5943             :         struct xfs_btree_cur    *cur = NULL;
    5944      404342 :         struct xfs_bmbt_irec    got, next;
    5945             :         struct xfs_iext_cursor  icur;
    5946             :         xfs_fileoff_t           new_startoff;
    5947             :         int                     error = 0;
    5948     1997484 :         int                     logflags = 0;
    5949             : 
    5950             :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    5951             :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    5952             :                 xfs_bmap_mark_sick(ip, whichfork);
    5953             :                 return -EFSCORRUPTED;
    5954             :         }
    5955             : 
    5956     1997484 :         if (xfs_is_shutdown(mp))
    5957     1997484 :                 return -EIO;
    5958     1997484 : 
    5959     1997484 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
    5960     1997484 : 
    5961     1997484 :         error = xfs_iread_extents(tp, ip, whichfork);
    5962     1997484 :         if (error)
    5963     1997484 :                 return error;
    5964     1997484 : 
    5965             :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    5966     3994968 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    5967     1997484 :                 cur->bc_ino.flags = 0;
    5968           0 :         }
    5969           0 : 
    5970             :         if (*next_fsb == NULLFSBLOCK) {
    5971             :                 xfs_iext_last(ifp, &icur);
    5972     3994968 :                 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
    5973             :                     stop_fsb > got.br_startoff) {
    5974             :                         *done = true;
    5975     1997483 :                         goto del_cursor;
    5976             :                 }
    5977     1997483 :         } else {
    5978     1997482 :                 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
    5979             :                         *done = true;
    5980             :                         goto del_cursor;
    5981     1997482 :                 }
    5982     1093932 :         }
    5983     1093932 :         if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
    5984             :                 xfs_bmap_mark_sick(ip, whichfork);
    5985             :                 error = -EFSCORRUPTED;
    5986     1997482 :                 goto del_cursor;
    5987      402564 :         }
    5988      402564 : 
    5989      392913 :         if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
    5990       45404 :                 xfs_bmap_mark_sick(ip, whichfork);
    5991       45404 :                 error = -EFSCORRUPTED;
    5992             :                 goto del_cursor;
    5993             :         }
    5994     1594918 : 
    5995           0 :         new_startoff = got.br_startoff + offset_shift_fsb;
    5996           0 :         if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
    5997             :                 if (new_startoff + got.br_blockcount > next.br_startoff) {
    5998             :                         error = -EINVAL;
    5999     1952079 :                         goto del_cursor;
    6000           0 :                 }
    6001           0 : 
    6002           0 :                 /*
    6003             :                  * Unlike a left shift (which involves a hole punch), a right
    6004             :                  * shift does not modify extent neighbors in any way.  We should
    6005     1952079 :                  * never find mergeable extents in this scenario.  Check anyways
    6006           0 :                  * and warn if we encounter two extents that could be one.
    6007           0 :                  */
    6008           0 :                 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
    6009             :                         WARN_ON_ONCE(1);
    6010             :         }
    6011     1952079 : 
    6012     1952079 :         error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
    6013     1594918 :                         cur, &logflags, new_startoff);
    6014           0 :         if (error)
    6015           0 :                 goto del_cursor;
    6016             : 
    6017             :         if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
    6018             :             stop_fsb >= got.br_startoff + got.br_blockcount) {
    6019             :                 *done = true;
    6020             :                 goto del_cursor;
    6021             :         }
    6022             : 
    6023             :         *next_fsb = got.br_startoff;
    6024     1594918 : del_cursor:
    6025           0 :         if (cur)
    6026             :                 xfs_btree_del_cursor(cur, error);
    6027             :         if (logflags)
    6028     1952078 :                 xfs_trans_log_inode(tp, ip, logflags);
    6029             :         return error;
    6030     1952079 : }
    6031           0 : 
    6032             : /*
    6033     1952079 :  * Splits an extent into two extents at split_fsb block such that it is the
    6034     1880470 :  * first block of the current_ext. @ext is a target extent to be split.
    6035      357159 :  * @split_fsb is a block where the extents is split.  If split_fsb lies in a
    6036      357159 :  * hole or the first block of extents, just return 0.
    6037             :  */
    6038             : int
    6039     1594920 : xfs_bmap_split_extent(
    6040     1997483 :         struct xfs_trans        *tp,
    6041     1997483 :         struct xfs_inode        *ip,
    6042     1093932 :         xfs_fileoff_t           split_fsb)
    6043     1997483 : {
    6044     1952079 :         int                             whichfork = XFS_DATA_FORK;
    6045             :         struct xfs_ifork                *ifp = xfs_ifork_ptr(ip, whichfork);
    6046             :         struct xfs_btree_cur            *cur = NULL;
    6047             :         struct xfs_bmbt_irec            got;
    6048             :         struct xfs_bmbt_irec            new; /* split extent */
    6049             :         struct xfs_mount                *mp = ip->i_mount;
    6050             :         xfs_fsblock_t                   gotblkcnt; /* new block count for got */
    6051             :         struct xfs_iext_cursor          icur;
    6052             :         int                             error = 0;
    6053             :         int                             logflags = 0;
    6054             :         int                             i = 0;
    6055      402565 : 
    6056             :         if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
    6057             :             XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
    6058             :                 xfs_bmap_mark_sick(ip, whichfork);
    6059             :                 return -EFSCORRUPTED;
    6060      402565 :         }
    6061      402565 : 
    6062      402565 :         if (xfs_is_shutdown(mp))
    6063      402565 :                 return -EIO;
    6064      402565 : 
    6065      402565 :         /* Read in all the extents */
    6066      402565 :         error = xfs_iread_extents(tp, ip, whichfork);
    6067      402565 :         if (error)
    6068      402565 :                 return error;
    6069      402565 : 
    6070      402565 :         /*
    6071             :          * If there are not extents, or split_fsb lies in a hole we are done.
    6072      805130 :          */
    6073      402565 :         if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
    6074           0 :             got.br_startoff >= split_fsb)
    6075           0 :                 return 0;
    6076             : 
    6077             :         gotblkcnt = split_fsb - got.br_startoff;
    6078      805130 :         new.br_startoff = split_fsb;
    6079             :         new.br_startblock = got.br_startblock + gotblkcnt;
    6080             :         new.br_blockcount = got.br_blockcount - gotblkcnt;
    6081             :         new.br_state = got.br_state;
    6082      402565 : 
    6083      402565 :         if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
    6084             :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
    6085             :                 cur->bc_ino.flags = 0;
    6086             :                 error = xfs_bmbt_lookup_eq(cur, &got, &i);
    6087             :                 if (error)
    6088             :                         goto del_cursor;
    6089      402565 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    6090      357161 :                         xfs_btree_mark_sick(cur);
    6091             :                         error = -EFSCORRUPTED;
    6092             :                         goto del_cursor;
    6093       68021 :                 }
    6094       68021 :         }
    6095       68021 : 
    6096       68021 :         got.br_blockcount = gotblkcnt;
    6097       68021 :         xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
    6098             :                         &got);
    6099       68021 : 
    6100       17134 :         logflags = XFS_ILOG_CORE;
    6101       17134 :         if (cur) {
    6102       17134 :                 error = xfs_bmbt_update(cur, &got);
    6103       17134 :                 if (error)
    6104           0 :                         goto del_cursor;
    6105       17134 :         } else
    6106           0 :                 logflags |= XFS_ILOG_DEXT;
    6107           0 : 
    6108           0 :         /* Add new extent */
    6109             :         xfs_iext_next(ifp, &icur);
    6110             :         xfs_iext_insert(ip, &icur, &new, 0);
    6111             :         ifp->if_nextents++;
    6112       68021 : 
    6113       68021 :         if (cur) {
    6114             :                 error = xfs_bmbt_lookup_eq(cur, &new, &i);
    6115             :                 if (error)
    6116       68021 :                         goto del_cursor;
    6117       68021 :                 if (XFS_IS_CORRUPT(mp, i != 0)) {
    6118       17134 :                         xfs_btree_mark_sick(cur);
    6119       17134 :                         error = -EFSCORRUPTED;
    6120           0 :                         goto del_cursor;
    6121             :                 }
    6122             :                 error = xfs_btree_insert(cur, &i);
    6123             :                 if (error)
    6124             :                         goto del_cursor;
    6125       68021 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    6126       68021 :                         xfs_btree_mark_sick(cur);
    6127       68021 :                         error = -EFSCORRUPTED;
    6128             :                         goto del_cursor;
    6129       68021 :                 }
    6130       17134 :         }
    6131       17134 : 
    6132           0 :         /*
    6133       17134 :          * Convert to a btree if necessary.
    6134           0 :          */
    6135           0 :         if (xfs_bmap_needs_btree(ip, whichfork)) {
    6136           0 :                 int tmp_logflags; /* partial log flag return val */
    6137             : 
    6138       17134 :                 ASSERT(cur == NULL);
    6139       17134 :                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
    6140           0 :                                 &tmp_logflags, whichfork);
    6141       17134 :                 logflags |= tmp_logflags;
    6142           0 :         }
    6143           0 : 
    6144           0 : del_cursor:
    6145             :         if (cur) {
    6146             :                 cur->bc_ino.allocated = 0;
    6147             :                 xfs_btree_del_cursor(cur, error);
    6148             :         }
    6149             : 
    6150             :         if (logflags)
    6151       68021 :                 xfs_trans_log_inode(tp, ip, logflags);
    6152        2809 :         return error;
    6153             : }
    6154        2809 : 
    6155        2809 : /* Record a bmap intent. */
    6156             : static void
    6157        2809 : __xfs_bmap_add(
    6158             :         struct xfs_trans                *tp,
    6159             :         enum xfs_bmap_intent_type       type,
    6160       65212 :         struct xfs_inode                *ip,
    6161       68021 :         int                             whichfork,
    6162       19943 :         struct xfs_bmbt_irec            *bmap)
    6163       19943 : {
    6164             :         struct xfs_bmap_intent          *bi;
    6165             : 
    6166       68021 :         if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
    6167       68021 :             bmap->br_startblock == HOLESTARTBLOCK ||
    6168             :             bmap->br_startblock == DELAYSTARTBLOCK)
    6169             :                 return;
    6170             : 
    6171             :         bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
    6172             :         INIT_LIST_HEAD(&bi->bi_list);
    6173    87536062 :         bi->bi_type = type;
    6174             :         bi->bi_owner = ip;
    6175             :         bi->bi_whichfork = whichfork;
    6176             :         bi->bi_bmap = *bmap;
    6177             : 
    6178             :         trace_xfs_bmap_defer(bi);
    6179             : 
    6180    87536062 :         xfs_bmap_update_get_group(tp->t_mountp, bi);
    6181             :         xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
    6182    87536062 : }
    6183    87536062 : 
    6184             : /* Map an extent into a file. */
    6185             : void
    6186             : xfs_bmap_map_extent(
    6187    86539968 :         struct xfs_trans        *tp,
    6188    86539756 :         struct xfs_inode        *ip,
    6189    86539756 :         int                     whichfork,
    6190    86539756 :         struct xfs_bmbt_irec    *PREV)
    6191    86539756 : {
    6192    86539756 :         __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
    6193             : }
    6194    86539756 : 
    6195             : /* Unmap an extent out of a file. */
    6196    86539699 : void
    6197    86539739 : xfs_bmap_unmap_extent(
    6198             :         struct xfs_trans        *tp,
    6199             :         struct xfs_inode        *ip,
    6200             :         int                     whichfork,
    6201             :         struct xfs_bmbt_irec    *PREV)
    6202    66990579 : {
    6203             :         __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
    6204             : }
    6205             : 
    6206             : /*
    6207             :  * Process one of the deferred bmap operations.  We pass back the
    6208    66990579 :  * btree cursor to maintain our lock on the bmapbt between calls.
    6209    66990585 :  */
    6210             : int
    6211             : xfs_bmap_finish_one(
    6212             :         struct xfs_trans                *tp,
    6213    20545770 :         struct xfs_bmap_intent          *bi)
    6214             : {
    6215             :         struct xfs_bmbt_irec            *bmap = &bi->bi_bmap;
    6216             :         int                             error = 0;
    6217             :         int                             flags = 0;
    6218             : 
    6219    20545770 :         if (bi->bi_whichfork == XFS_ATTR_FORK)
    6220    20545661 :                 flags |= XFS_BMAPI_ATTRFORK;
    6221             : 
    6222             :         ASSERT(tp->t_highest_agno == NULLAGNUMBER);
    6223             : 
    6224             :         trace_xfs_bmap_deferred(bi);
    6225             : 
    6226             :         if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
    6227    86540976 :                 return -EIO;
    6228             : 
    6229             :         switch (bi->bi_type) {
    6230             :         case XFS_BMAP_MAP:
    6231    86540976 :                 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
    6232    86540976 :                         flags |= XFS_BMAPI_PREALLOC;
    6233    86540976 :                 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
    6234             :                                 bmap->br_blockcount, bmap->br_startblock,
    6235    86540976 :                                 flags);
    6236      214323 :                 bmap->br_blockcount = 0;
    6237             :                 break;
    6238    86540976 :         case XFS_BMAP_UNMAP:
    6239             :                 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
    6240    86540976 :                                 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
    6241             :                                 1);
    6242    86541082 :                 break;
    6243             :         default:
    6244             :                 ASSERT(0);
    6245    86540883 :                 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
    6246    66492669 :                 error = -EFSCORRUPTED;
    6247    66492669 :         }
    6248      314775 : 
    6249    66492669 :         return error;
    6250             : }
    6251             : 
    6252    66492640 : /* Check that an extent does not have invalid flags or bad ranges. */
    6253    66492640 : xfs_failaddr_t
    6254    20048214 : xfs_bmap_validate_extent_raw(
    6255    20048214 :         struct xfs_mount        *mp,
    6256             :         bool                    rtfile,
    6257             :         int                     whichfork,
    6258    20048214 :         struct xfs_bmbt_irec    *irec)
    6259           0 : {
    6260           0 :         if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
    6261           0 :                 return __this_address;
    6262           0 : 
    6263             :         if (rtfile && whichfork == XFS_DATA_FORK) {
    6264             :                 if (!xfs_verify_rtbext(mp, irec->br_startblock,
    6265             :                                           irec->br_blockcount))
    6266             :                         return __this_address;
    6267             :         } else {
    6268             :                 if (!xfs_verify_fsbext(mp, irec->br_startblock,
    6269             :                                            irec->br_blockcount))
    6270  3300325424 :                         return __this_address;
    6271             :         }
    6272             :         if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
    6273             :                 return __this_address;
    6274             :         return NULL;
    6275             : }
    6276  3300325424 : 
    6277           0 : int __init
    6278             : xfs_bmap_intent_init_cache(void)
    6279  3300400752 : {
    6280   434078940 :         xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
    6281             :                         sizeof(struct xfs_bmap_intent),
    6282           0 :                         0, 0, NULL);
    6283             : 
    6284  2866321812 :         return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
    6285             : }
    6286           0 : 
    6287             : void
    6288  3300497888 : xfs_bmap_intent_destroy_cache(void)
    6289           0 : {
    6290             :         kmem_cache_destroy(xfs_bmap_intent_cache);
    6291             :         xfs_bmap_intent_cache = NULL;
    6292             : }
    6293             : 
    6294          12 : /* Check that an inode's extent does not have invalid flags or bad ranges. */
    6295             : xfs_failaddr_t
    6296          12 : xfs_bmap_validate_extent(
    6297             :         struct xfs_inode        *ip,
    6298             :         int                     whichfork,
    6299             :         struct xfs_bmbt_irec    *irec)
    6300          12 : {
    6301             :         return xfs_bmap_validate_extent_raw(ip->i_mount,
    6302             :                         XFS_IS_REALTIME_INODE(ip), whichfork, irec);
    6303             : }
    6304          12 : 
    6305             : /*
    6306          12 :  * Used in xfs_itruncate_extents().  This is the maximum number of extents
    6307          12 :  * freed from a file in a single transaction.
    6308          12 :  */
    6309             : #define XFS_ITRUNC_MAX_EXTENTS  2
    6310             : 
    6311             : /*
    6312  3300351187 :  * Unmap every extent in part of an inode's fork.  We don't do any higher level
    6313             :  * invalidation work at all.
    6314             :  */
    6315             : int
    6316             : xfs_bunmapi_range(
    6317  3300351187 :         struct xfs_trans        **tpp,
    6318  3300351187 :         struct xfs_inode        *ip,
    6319             :         uint32_t                flags,
    6320             :         xfs_fileoff_t           startoff,
    6321             :         xfs_fileoff_t           endoff)
    6322             : {
    6323             :         xfs_filblks_t           unmap_len = endoff - startoff + 1;
    6324             :         int                     error = 0;
    6325             : 
    6326             :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    6327             : 
    6328             :         while (unmap_len > 0) {
    6329             :                 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
    6330             :                 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
    6331             :                                 XFS_ITRUNC_MAX_EXTENTS);
    6332    13999518 :                 if (error)
    6333             :                         goto out;
    6334             : 
    6335             :                 /* free the just unmapped extents */
    6336             :                 error = xfs_defer_finish(tpp);
    6337             :                 if (error)
    6338             :                         goto out;
    6339    13999518 :         }
    6340    13999518 : out:
    6341             :         return error;
    6342    13999518 : }
    6343             : 
    6344    56055570 : struct xfs_bmap_query_range {
    6345    42059538 :         xfs_bmap_query_range_fn fn;
    6346    42059538 :         void                    *priv;
    6347             : };
    6348    42058452 : 
    6349         558 : /* Format btree record and pass to our callback. */
    6350             : STATIC int
    6351             : xfs_bmap_query_range_helper(
    6352    42057894 :         struct xfs_btree_cur            *cur,
    6353    42058735 :         const union xfs_btree_rec       *rec,
    6354        1364 :         void                            *priv)
    6355             : {
    6356    13996032 :         struct xfs_bmap_query_range     *query = priv;
    6357    13997954 :         struct xfs_bmbt_irec            irec;
    6358             :         xfs_failaddr_t                  fa;
    6359             : 
    6360             :         xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
    6361             :         fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
    6362             :                         &irec);
    6363             :         if (fa) {
    6364             :                 xfs_btree_mark_sick(cur);
    6365             :                 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
    6366             :                                 cur->bc_ino.whichfork, fa, &irec);
    6367     2245167 :         }
    6368             : 
    6369             :         return query->fn(cur, &irec, query->priv);
    6370             : }
    6371             : 
    6372     2245167 : /* Find all bmaps. */
    6373     2245167 : int
    6374     2245167 : xfs_bmap_query_all(
    6375             :         struct xfs_btree_cur            *cur,
    6376     2245167 :         xfs_bmap_query_range_fn         fn,
    6377     2245167 :         void                            *priv)
    6378             : {
    6379     2245167 :         struct xfs_bmap_query_range     query = {
    6380           0 :                 .priv                   = priv,
    6381           0 :                 .fn                     = fn,
    6382           0 :         };
    6383             : 
    6384             :         return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
    6385     2245167 : }
    6386             : 
    6387             : /* Helper function to extract extent size hint from inode */
    6388             : xfs_extlen_t
    6389             : xfs_get_extsz_hint(
    6390      104421 :         struct xfs_inode        *ip)
    6391             : {
    6392             :         /*
    6393             :          * No point in aligning allocations if we need to COW to actually
    6394             :          * write to them.
    6395      104421 :          */
    6396             :         if (!xfs_is_always_cow_inode(ip) &&
    6397             :             (ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
    6398             :                 return ip->i_extsize;
    6399             :         if (XFS_IS_REALTIME_INODE(ip))
    6400      104421 :                 return ip->i_mount->m_sb.sb_rextsize;
    6401             :         return 0;
    6402             : }
    6403             : 
    6404             : /*
    6405   817744295 :  * Helper function to extract CoW extent size hint from inode.
    6406             :  * Between the extent size hint and the CoW extent size hint, we
    6407             :  * return the greater of the two.  If the value is zero (automatic),
    6408             :  * use the default size.
    6409             :  */
    6410             : xfs_extlen_t
    6411             : xfs_get_cowextsz_hint(
    6412   817744295 :         struct xfs_inode        *ip)
    6413   817765810 : {
    6414             :         xfs_extlen_t            a, b;
    6415   805113448 : 
    6416   379920491 :         a = 0;
    6417             :         if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
    6418             :                 a = ip->i_cowextsize;
    6419             :         if (XFS_IS_REALTIME_INODE(ip)) {
    6420             :                 /*
    6421             :                  * For realtime files, the realtime extent is the fundamental
    6422             :                  * unit of allocation.  This means that data sharing and CoW
    6423             :                  * remapping can only be done in those units.  For filesystems
    6424             :                  * where the extent size is larger than one block, write
    6425             :                  * requests that are not aligned to an extent boundary employ
    6426             :                  * an unshare-around strategy to ensure that all pages for a
    6427     1377248 :                  * shared extent are fully dirtied.
    6428             :                  *
    6429             :                  * Because the remapping alignment requirement applies equally
    6430     1377248 :                  * to all CoW writes, any regular overwrites that could be
    6431             :                  * turned (by a speculative CoW preallocation) into a CoW write
    6432     1377248 :                  * must either employ this dirty-around strategy, or be smart
    6433     1377248 :                  * enough to ignore the CoW fork mapping unless the entire
    6434       27888 :                  * extent is dirty or becomes shared by writeback time.  Doing
    6435     1377248 :                  * the first would dramatically increase write amplification,
    6436             :                  * and the second would require deeper insight into the state
    6437             :                  * of the page cache during a writeback request.  For now, we
    6438             :                  * ignore the hint.
    6439             :                  */
    6440             :                 if (ip->i_mount->m_sb.sb_rextsize > 1)
    6441             :                         return ip->i_mount->m_sb.sb_rextsize;
    6442             :                 b = 0;
    6443             :                 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
    6444             :                         b = ip->i_extsize;
    6445             :         } else {
    6446             :                 b = xfs_get_extsz_hint(ip);
    6447             :         }
    6448             : 
    6449             :         a = max(a, b);
    6450             :         if (a == 0)
    6451             :                 return XFS_DEFAULT_COWEXTSZ_HINT;
    6452             :         return a;
    6453             : }
    6454             : 
    6455             : static inline xfs_fileoff_t
    6456      626722 : xfs_fsblock_to_fileoff(
    6457             :         struct xfs_mount        *mp,
    6458      626722 :         xfs_fsblock_t           fsbno)
    6459      626722 : {
    6460           0 :         xfs_daddr_t             daddr = XFS_FSB_TO_DADDR(mp, fsbno);
    6461             : 
    6462      750526 :         return XFS_B_TO_FSB(mp, BBTOB(daddr));
    6463             : }
    6464             : 
    6465     1377244 : /*
    6466     1377244 :  * Given a file and a free physical extent, map it into the file at the same
    6467     1349352 :  * offset if the file were a sparse image of the physical device.  Set @mval to
    6468             :  * whatever mapping we added to the file.
    6469             :  */
    6470             : int
    6471             : xfs_bmapi_freesp(
    6472         210 :         struct xfs_trans        *tp,
    6473             :         struct xfs_inode        *ip,
    6474             :         xfs_fsblock_t           fsbno,
    6475             :         xfs_extlen_t            len,
    6476         210 :         struct xfs_bmbt_irec    *mval)
    6477             : {
    6478         210 :         struct xfs_bmbt_irec    irec;
    6479             :         struct xfs_mount        *mp = ip->i_mount;
    6480             :         xfs_fileoff_t           startoff;
    6481             :         bool                    isrt = XFS_IS_REALTIME_INODE(ip);
    6482             :         int                     nimaps;
    6483             :         int                     error;
    6484             : 
    6485             :         trace_xfs_bmapi_freesp(ip, fsbno, len);
    6486             : 
    6487         210 :         error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
    6488             :                         XFS_IEXT_ADD_NOSPLIT_CNT);
    6489             :         if (error)
    6490             :                 return error;
    6491             : 
    6492             :         if (isrt)
    6493             :                 startoff = fsbno;
    6494         210 :         else
    6495         210 :                 startoff = xfs_fsblock_to_fileoff(mp, fsbno);
    6496         210 : 
    6497         210 :         /* Make sure the entire range is a hole. */
    6498         210 :         nimaps = 1;
    6499         210 :         error = xfs_bmapi_read(ip, startoff, len, &irec, &nimaps, 0);
    6500             :         if (error)
    6501         210 :                 return error;
    6502             : 
    6503         210 :         if (irec.br_startoff != startoff ||
    6504             :             irec.br_startblock != HOLESTARTBLOCK ||
    6505         210 :             irec.br_blockcount < len)
    6506             :                 return -EINVAL;
    6507             : 
    6508         210 :         /*
    6509             :          * Allocate the physical extent.  We should not have dropped the lock
    6510             :          * since the scan of the free space metadata, so this should work,
    6511         210 :          * though the length may be adjusted to play nicely with metadata space
    6512             :          * reservations.
    6513             :          */
    6514         210 :         if (isrt) {
    6515         210 :                 xfs_rtxnum_t    rtx_in, rtx_out;
    6516         210 :                 xfs_extlen_t    rtxlen_in, rtxlen_out;
    6517             :                 uint32_t        mod;
    6518             : 
    6519         210 :                 rtx_in = xfs_rtb_to_rtx(mp, fsbno, &mod);
    6520         210 :                 if (mod) {
    6521         210 :                         ASSERT(mod == 0);
    6522             :                         return -EFSCORRUPTED;
    6523             :                 }
    6524             : 
    6525             :                 rtxlen_in = xfs_rtb_to_rtx(mp, len, &mod);
    6526             :                 if (mod) {
    6527             :                         ASSERT(mod == 0);
    6528             :                         return -EFSCORRUPTED;
    6529             :                 }
    6530         210 : 
    6531           0 :                 error = xfs_rtallocate_extent(tp, rtx_in, 1, rtxlen_in,
    6532           0 :                                 &rtxlen_out, 0, 1, &rtx_out);
    6533           0 :                 if (error)
    6534             :                         return error;
    6535           0 :                 if (rtx_out == NULLRTEXTNO) {
    6536           0 :                         /*
    6537           0 :                          * We were promised the space!  In theory there aren't
    6538           0 :                          * any reserve lists that would prevent us from getting
    6539             :                          * the space.
    6540             :                          */
    6541           0 :                         return -ENOSPC;
    6542           0 :                 }
    6543           0 :                 if (rtx_out != rtx_in) {
    6544           0 :                         ASSERT(0);
    6545             :                         xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
    6546             :                         return -EFSCORRUPTED;
    6547           0 :                 }
    6548             :                 mval->br_blockcount = rtxlen_out * mp->m_sb.sb_rextsize;
    6549           0 :         } else {
    6550             :                 struct xfs_alloc_arg    args = {
    6551           0 :                         .mp             = mp,
    6552             :                         .tp             = tp,
    6553             :                         .oinfo          = XFS_RMAP_OINFO_SKIP_UPDATE,
    6554             :                         .resv           = XFS_AG_RESV_NONE,
    6555             :                         .prod           = 1,
    6556             :                         .datatype       = XFS_ALLOC_USERDATA,
    6557             :                         .maxlen         = len,
    6558             :                         .minlen         = 1,
    6559           0 :                 };
    6560           0 :                 args.pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, fsbno));
    6561           0 :                 error = xfs_alloc_vextent_exact_bno(&args, fsbno);
    6562           0 :                 xfs_perag_put(args.pag);
    6563             :                 if (error)
    6564           0 :                         return error;
    6565             :                 if (args.fsbno == NULLFSBLOCK) {
    6566         210 :                         /*
    6567             :                          * We were promised the space, but failed to get it.
    6568             :                          * This could be because the space is reserved for
    6569             :                          * metadata expansion, or it could be because the AGFL
    6570             :                          * fixup grabbed the first block we wanted.  Either
    6571             :                          * way, if the transaction is dirty we must commit it
    6572             :                          * and tell the caller to try again.
    6573             :                          */
    6574             :                         if (tp->t_flags & XFS_TRANS_DIRTY)
    6575             :                                 return -EAGAIN;
    6576         210 :                         return -ENOSPC;
    6577         210 :                 }
    6578         210 :                 if (args.fsbno != fsbno) {
    6579         210 :                         ASSERT(0);
    6580         180 :                         xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
    6581         210 :                         return -EFSCORRUPTED;
    6582             :                 }
    6583             :                 mval->br_blockcount = args.len;
    6584             :         }
    6585             : 
    6586             :         /* Map extent into file, update quota. */
    6587             :         mval->br_startblock = fsbno;
    6588             :         mval->br_startoff = startoff;
    6589             :         mval->br_state = XFS_EXT_UNWRITTEN;
    6590         180 : 
    6591             :         trace_xfs_bmapi_freesp_done(ip, mval);
    6592         180 : 
    6593             :         xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, mval);
    6594          30 :         if (isrt)
    6595           0 :                 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_RTBCOUNT,
    6596           0 :                                 mval->br_blockcount);
    6597           0 :         else
    6598             :                 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
    6599          30 :                                 mval->br_blockcount);
    6600             : 
    6601             :         return 0;
    6602             : }

Generated by: LCOV version 1.14