LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_ialloc.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 1118 1344 83.2 %
Date: 2023-07-31 20:08:27 Functions: 47 50 94.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2002,2005 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_mount.h"
      14             : #include "xfs_inode.h"
      15             : #include "xfs_btree.h"
      16             : #include "xfs_ialloc.h"
      17             : #include "xfs_ialloc_btree.h"
      18             : #include "xfs_alloc.h"
      19             : #include "xfs_errortag.h"
      20             : #include "xfs_error.h"
      21             : #include "xfs_bmap.h"
      22             : #include "xfs_trans.h"
      23             : #include "xfs_buf_item.h"
      24             : #include "xfs_icreate_item.h"
      25             : #include "xfs_icache.h"
      26             : #include "xfs_trace.h"
      27             : #include "xfs_log.h"
      28             : #include "xfs_rmap.h"
      29             : #include "xfs_ag.h"
      30             : #include "xfs_health.h"
      31             : 
      32             : /*
      33             :  * Lookup a record by ino in the btree given by cur.
      34             :  */
      35             : int                                     /* error */
      36  3676132890 : xfs_inobt_lookup(
      37             :         struct xfs_btree_cur    *cur,   /* btree cursor */
      38             :         xfs_agino_t             ino,    /* starting inode of chunk */
      39             :         xfs_lookup_t            dir,    /* <=, >=, == */
      40             :         int                     *stat)  /* success/failure */
      41             : {
      42  8422581513 :         cur->bc_rec.i.ir_startino = ino;
      43  8422581513 :         cur->bc_rec.i.ir_holemask = 0;
      44  8422581513 :         cur->bc_rec.i.ir_count = 0;
      45  8422581513 :         cur->bc_rec.i.ir_freecount = 0;
      46  8422581513 :         cur->bc_rec.i.ir_free = 0;
      47  3681489988 :         return xfs_btree_lookup(cur, dir, stat);
      48             : }
      49             : 
      50             : /*
      51             :  * Update the record referred to by cur to the value given.
      52             :  * This either works (return 0) or gets an EFSCORRUPTED error.
      53             :  */
      54             : STATIC int                              /* error */
      55   177113245 : xfs_inobt_update(
      56             :         struct xfs_btree_cur    *cur,   /* btree cursor */
      57             :         xfs_inobt_rec_incore_t  *irec)  /* btree record */
      58             : {
      59   177113245 :         union xfs_btree_rec     rec;
      60             : 
      61   177113245 :         rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
      62   177113245 :         if (xfs_has_sparseinodes(cur->bc_mp)) {
      63   177112983 :                 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
      64   177112983 :                 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
      65   177112983 :                 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
      66             :         } else {
      67             :                 /* ir_holemask/ir_count not supported on-disk */
      68         262 :                 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
      69             :         }
      70   177113245 :         rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
      71   177113245 :         return xfs_btree_update(cur, &rec);
      72             : }
      73             : 
      74             : /* Convert on-disk btree record to incore inobt record. */
      75             : void
      76 19378237658 : xfs_inobt_btrec_to_irec(
      77             :         struct xfs_mount                *mp,
      78             :         const union xfs_btree_rec       *rec,
      79             :         struct xfs_inobt_rec_incore     *irec)
      80             : {
      81 19378237658 :         irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
      82 19378237658 :         if (xfs_has_sparseinodes(mp)) {
      83 19378236422 :                 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
      84 19378236422 :                 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
      85 19378236422 :                 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
      86             :         } else {
      87             :                 /*
      88             :                  * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
      89             :                  * values for full inode chunks.
      90             :                  */
      91        1236 :                 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
      92        1236 :                 irec->ir_count = XFS_INODES_PER_CHUNK;
      93        1236 :                 irec->ir_freecount =
      94        1236 :                                 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
      95             :         }
      96 19378237658 :         irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
      97 19378237658 : }
      98             : 
      99             : /* Compute the freecount of an incore inode record. */
     100             : uint8_t
     101 19477928819 : xfs_inobt_rec_freecount(
     102             :         const struct xfs_inobt_rec_incore       *irec)
     103             : {
     104 19477928819 :         uint64_t                                realfree;
     105             : 
     106 19477928819 :         if (!xfs_inobt_issparse(irec->ir_holemask))
     107 11360599655 :                 realfree = irec->ir_free;
     108             :         else
     109  8117329164 :                 realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
     110 38974045346 :         return hweight64(realfree);
     111             : }
     112             : 
     113             : inline xfs_failaddr_t
     114 19403689855 : xfs_inobt_check_perag_irec(
     115             :         struct xfs_perag                        *pag,
     116             :         const struct xfs_inobt_rec_incore       *irec)
     117             : {
     118             :         /* Record has to be properly aligned within the AG. */
     119 19403689855 :         if (!xfs_verify_agino(pag, irec->ir_startino))
     120           0 :                 return __this_address;
     121 19403689855 :         if (!xfs_verify_agino(pag,
     122             :                                 irec->ir_startino + XFS_INODES_PER_CHUNK - 1))
     123           0 :                 return __this_address;
     124 19403689855 :         if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
     125             :             irec->ir_count > XFS_INODES_PER_CHUNK)
     126           0 :                 return __this_address;
     127 19403689855 :         if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
     128           0 :                 return __this_address;
     129             : 
     130 19403689855 :         if (xfs_inobt_rec_freecount(irec) != irec->ir_freecount)
     131           0 :                 return __this_address;
     132             : 
     133             :         return NULL;
     134             : }
     135             : 
     136             : /* Simple checks for inode records. */
     137             : xfs_failaddr_t
     138    59440095 : xfs_inobt_check_irec(
     139             :         struct xfs_btree_cur                    *cur,
     140             :         const struct xfs_inobt_rec_incore       *irec)
     141             : {
     142    59440095 :         return xfs_inobt_check_perag_irec(cur->bc_ag.pag, irec);
     143             : }
     144             : 
     145             : static inline int
     146           0 : xfs_inobt_complain_bad_rec(
     147             :         struct xfs_btree_cur            *cur,
     148             :         xfs_failaddr_t                  fa,
     149             :         const struct xfs_inobt_rec_incore *irec)
     150             : {
     151           0 :         struct xfs_mount                *mp = cur->bc_mp;
     152             : 
     153           0 :         xfs_warn(mp,
     154             :                 "%s Inode BTree record corruption in AG %d detected at %pS!",
     155             :                 cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free",
     156             :                 cur->bc_ag.pag->pag_agno, fa);
     157           0 :         xfs_warn(mp,
     158             : "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
     159             :                 irec->ir_startino, irec->ir_count, irec->ir_freecount,
     160             :                 irec->ir_free, irec->ir_holemask);
     161           0 :         xfs_btree_mark_sick(cur);
     162           0 :         return -EFSCORRUPTED;
     163             : }
     164             : 
     165             : /*
     166             :  * Get the data from the pointed-to record.
     167             :  */
     168             : int
     169 19152152321 : xfs_inobt_get_rec(
     170             :         struct xfs_btree_cur            *cur,
     171             :         struct xfs_inobt_rec_incore     *irec,
     172             :         int                             *stat)
     173             : {
     174 19152152321 :         struct xfs_mount                *mp = cur->bc_mp;
     175 19152152321 :         union xfs_btree_rec             *rec;
     176 19152152321 :         xfs_failaddr_t                  fa;
     177 19152152321 :         int                             error;
     178             : 
     179 19152152321 :         error = xfs_btree_get_rec(cur, &rec, stat);
     180 19077422623 :         if (error || *stat == 0)
     181             :                 return error;
     182             : 
     183 19086030087 :         xfs_inobt_btrec_to_irec(mp, rec, irec);
     184 19113374521 :         fa = xfs_inobt_check_irec(cur, irec);
     185 19151801905 :         if (fa)
     186           0 :                 return xfs_inobt_complain_bad_rec(cur, fa, irec);
     187             : 
     188             :         return 0;
     189             : }
     190             : 
     191             : /*
     192             :  * Insert a single inobt record. Cursor must already point to desired location.
     193             :  */
     194             : int
     195           0 : xfs_inobt_insert_rec(
     196             :         struct xfs_btree_cur    *cur,
     197             :         uint16_t                holemask,
     198             :         uint8_t                 count,
     199             :         int32_t                 freecount,
     200             :         xfs_inofree_t           free,
     201             :         int                     *stat)
     202             : {
     203    14726187 :         cur->bc_rec.i.ir_holemask = holemask;
     204    14726187 :         cur->bc_rec.i.ir_count = count;
     205    14726187 :         cur->bc_rec.i.ir_freecount = freecount;
     206    14726187 :         cur->bc_rec.i.ir_free = free;
     207           0 :         return xfs_btree_insert(cur, stat);
     208             : }
     209             : 
     210             : /*
     211             :  * Insert records describing a newly allocated inode chunk into the inobt.
     212             :  */
     213             : STATIC int
     214      635171 : xfs_inobt_insert(
     215             :         struct xfs_perag        *pag,
     216             :         struct xfs_trans        *tp,
     217             :         struct xfs_buf          *agbp,
     218             :         xfs_agino_t             newino,
     219             :         xfs_agino_t             newlen,
     220             :         xfs_btnum_t             btnum)
     221             : {
     222      635171 :         struct xfs_btree_cur    *cur;
     223      635171 :         xfs_agino_t             thisino;
     224      635171 :         int                     i;
     225      635171 :         int                     error;
     226             : 
     227      635171 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, btnum);
     228             : 
     229      635171 :         for (thisino = newino;
     230     1270399 :              thisino < newino + newlen;
     231      635227 :              thisino += XFS_INODES_PER_CHUNK) {
     232      635224 :                 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
     233      635227 :                 if (error) {
     234           0 :                         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     235           0 :                         return error;
     236             :                 }
     237      635227 :                 ASSERT(i == 0);
     238             : 
     239      635227 :                 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
     240             :                                              XFS_INODES_PER_CHUNK,
     241             :                                              XFS_INODES_PER_CHUNK,
     242             :                                              XFS_INOBT_ALL_FREE, &i);
     243      635227 :                 if (error) {
     244           0 :                         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     245           0 :                         return error;
     246             :                 }
     247      635227 :                 ASSERT(i == 1);
     248             :         }
     249             : 
     250      635175 :         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     251             : 
     252      635175 :         return 0;
     253             : }
     254             : 
     255             : /*
     256             :  * Verify that the number of free inodes in the AGI is correct.
     257             :  */
     258             : #ifdef DEBUG
     259             : static int
     260   371367895 : xfs_check_agi_freecount(
     261             :         struct xfs_btree_cur    *cur)
     262             : {
     263   371367895 :         if (cur->bc_nlevels == 1) {
     264   307656471 :                 xfs_inobt_rec_incore_t rec;
     265   307656471 :                 int             freecount = 0;
     266   307656471 :                 int             error;
     267   307656471 :                 int             i;
     268             : 
     269   307656471 :                 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
     270   307692005 :                 if (error)
     271         327 :                         return error;
     272             : 
     273 10294781034 :                 do {
     274 10294781034 :                         error = xfs_inobt_get_rec(cur, &rec, &i);
     275 10283831312 :                         if (error)
     276           0 :                                 return error;
     277             : 
     278 10283831312 :                         if (i) {
     279 10268505705 :                                 freecount += rec.ir_freecount;
     280 10268505705 :                                 error = xfs_btree_increment(cur, 0, &i);
     281 10279475144 :                                 if (error)
     282           0 :                                         return error;
     283             :                         }
     284 10294800751 :                 } while (i == 1);
     285             : 
     286   615422790 :                 if (!xfs_is_shutdown(cur->bc_mp))
     287   307723524 :                         ASSERT(freecount == cur->bc_ag.pag->pagi_freecount);
     288             :         }
     289             :         return 0;
     290             : }
     291             : #else
     292             : #define xfs_check_agi_freecount(cur)    0
     293             : #endif
     294             : 
     295             : /*
     296             :  * Initialise a new set of inodes. When called without a transaction context
     297             :  * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
     298             :  * than logging them (which in a transaction context puts them into the AIL
     299             :  * for writeback rather than the xfsbufd queue).
     300             :  */
     301             : int
     302      644205 : xfs_ialloc_inode_init(
     303             :         struct xfs_mount        *mp,
     304             :         struct xfs_trans        *tp,
     305             :         struct list_head        *buffer_list,
     306             :         int                     icount,
     307             :         xfs_agnumber_t          agno,
     308             :         xfs_agblock_t           agbno,
     309             :         xfs_agblock_t           length,
     310             :         unsigned int            gen)
     311             : {
     312      644205 :         struct xfs_buf          *fbuf;
     313      644205 :         struct xfs_dinode       *free;
     314      644205 :         int                     nbufs;
     315      644205 :         int                     version;
     316      644205 :         int                     i, j;
     317      644205 :         xfs_daddr_t             d;
     318      644205 :         xfs_ino_t               ino = 0;
     319      644205 :         int                     error;
     320             : 
     321             :         /*
     322             :          * Loop over the new block(s), filling in the inodes.  For small block
     323             :          * sizes, manipulate the inodes in buffers  which are multiples of the
     324             :          * blocks size.
     325             :          */
     326      644205 :         nbufs = length / M_IGEO(mp)->blocks_per_cluster;
     327             : 
     328             :         /*
     329             :          * Figure out what version number to use in the inodes we create.  If
     330             :          * the superblock version has caught up to the one that supports the new
     331             :          * inode format, then use the new inode version.  Otherwise use the old
     332             :          * version so that old kernels will continue to be able to use the file
     333             :          * system.
     334             :          *
     335             :          * For v3 inodes, we also need to write the inode number into the inode,
     336             :          * so calculate the first inode number of the chunk here as
     337             :          * XFS_AGB_TO_AGINO() only works within a filesystem block, not
     338             :          * across multiple filesystem blocks (such as a cluster) and so cannot
     339             :          * be used in the cluster buffer loop below.
     340             :          *
     341             :          * Further, because we are writing the inode directly into the buffer
     342             :          * and calculating a CRC on the entire inode, we have ot log the entire
     343             :          * inode so that the entire range the CRC covers is present in the log.
     344             :          * That means for v3 inode we log the entire buffer rather than just the
     345             :          * inode cores.
     346             :          */
     347      644205 :         if (xfs_has_v3inodes(mp)) {
     348      644202 :                 version = 3;
     349      644202 :                 ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
     350             : 
     351             :                 /*
     352             :                  * log the initialisation that is about to take place as an
     353             :                  * logical operation. This means the transaction does not
     354             :                  * need to log the physical changes to the inode buffers as log
     355             :                  * recovery will know what initialisation is actually needed.
     356             :                  * Hence we only need to log the buffers as "ordered" buffers so
     357             :                  * they track in the AIL as if they were physically logged.
     358             :                  */
     359      644202 :                 if (tp)
     360      636680 :                         xfs_icreate_log(tp, agno, agbno, icount,
     361      636680 :                                         mp->m_sb.sb_inodesize, length, gen);
     362             :         } else
     363             :                 version = 2;
     364             : 
     365     1609706 :         for (j = 0; j < nbufs; j++) {
     366             :                 /*
     367             :                  * Get the block.
     368             :                  */
     369      965499 :                 d = XFS_AGB_TO_DADDR(mp, agno, agbno +
     370             :                                 (j * M_IGEO(mp)->blocks_per_cluster));
     371      965499 :                 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
     372      965499 :                                 mp->m_bsize * M_IGEO(mp)->blocks_per_cluster,
     373             :                                 XBF_UNMAPPED, &fbuf);
     374      965519 :                 if (error)
     375           0 :                         return error;
     376             : 
     377             :                 /* Initialize the inode buffers and log them appropriately. */
     378      965519 :                 fbuf->b_ops = &xfs_inode_buf_ops;
     379      965519 :                 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
     380    32828661 :                 for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
     381    30897621 :                         int     ioffset = i << mp->m_sb.sb_inodelog;
     382             : 
     383    30897621 :                         free = xfs_make_iptr(mp, fbuf, i);
     384    30897109 :                         free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
     385    30897109 :                         free->di_version = version;
     386    30897109 :                         free->di_gen = cpu_to_be32(gen);
     387    30897109 :                         free->di_next_unlinked = cpu_to_be32(NULLAGINO);
     388             : 
     389    30897109 :                         if (version == 3) {
     390    30896981 :                                 free->di_ino = cpu_to_be64(ino);
     391    30896981 :                                 ino++;
     392    30896981 :                                 uuid_copy(&free->di_uuid,
     393    30896981 :                                           &mp->m_sb.sb_meta_uuid);
     394    30896962 :                                 xfs_dinode_calc_crc(mp, free);
     395         128 :                         } else if (tp) {
     396             :                                 /* just log the inode core */
     397         128 :                                 xfs_trans_log_buf(tp, fbuf, ioffset,
     398         256 :                                           ioffset + XFS_DINODE_SIZE(mp) - 1);
     399             :                         }
     400             :                 }
     401             : 
     402      965521 :                 if (tp) {
     403             :                         /*
     404             :                          * Mark the buffer as an inode allocation buffer so it
     405             :                          * sticks in AIL at the point of this allocation
     406             :                          * transaction. This ensures the they are on disk before
     407             :                          * the tail of the log can be moved past this
     408             :                          * transaction (i.e. by preventing relogging from moving
     409             :                          * it forward in the log).
     410             :                          */
     411      954231 :                         xfs_trans_inode_alloc_buf(tp, fbuf);
     412      954228 :                         if (version == 3) {
     413             :                                 /*
     414             :                                  * Mark the buffer as ordered so that they are
     415             :                                  * not physically logged in the transaction but
     416             :                                  * still tracked in the AIL as part of the
     417             :                                  * transaction and pin the log appropriately.
     418             :                                  */
     419      954223 :                                 xfs_trans_ordered_buf(tp, fbuf);
     420             :                         }
     421             :                 } else {
     422       11290 :                         fbuf->b_flags |= XBF_DONE;
     423       11290 :                         xfs_buf_delwri_queue(fbuf, buffer_list);
     424       11290 :                         xfs_buf_relse(fbuf);
     425             :                 }
     426             :         }
     427             :         return 0;
     428             : }
     429             : 
     430             : /*
     431             :  * Align startino and allocmask for a recently allocated sparse chunk such that
     432             :  * they are fit for insertion (or merge) into the on-disk inode btrees.
     433             :  *
     434             :  * Background:
     435             :  *
     436             :  * When enabled, sparse inode support increases the inode alignment from cluster
     437             :  * size to inode chunk size. This means that the minimum range between two
     438             :  * non-adjacent inode records in the inobt is large enough for a full inode
     439             :  * record. This allows for cluster sized, cluster aligned block allocation
     440             :  * without need to worry about whether the resulting inode record overlaps with
     441             :  * another record in the tree. Without this basic rule, we would have to deal
     442             :  * with the consequences of overlap by potentially undoing recent allocations in
     443             :  * the inode allocation codepath.
     444             :  *
     445             :  * Because of this alignment rule (which is enforced on mount), there are two
     446             :  * inobt possibilities for newly allocated sparse chunks. One is that the
     447             :  * aligned inode record for the chunk covers a range of inodes not already
     448             :  * covered in the inobt (i.e., it is safe to insert a new sparse record). The
     449             :  * other is that a record already exists at the aligned startino that considers
     450             :  * the newly allocated range as sparse. In the latter case, record content is
     451             :  * merged in hope that sparse inode chunks fill to full chunks over time.
     452             :  */
     453             : STATIC void
     454      319097 : xfs_align_sparse_ino(
     455             :         struct xfs_mount                *mp,
     456             :         xfs_agino_t                     *startino,
     457             :         uint16_t                        *allocmask)
     458             : {
     459      319097 :         xfs_agblock_t                   agbno;
     460      319097 :         xfs_agblock_t                   mod;
     461      319097 :         int                             offset;
     462             : 
     463      319097 :         agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
     464      319097 :         mod = agbno % mp->m_sb.sb_inoalignmt;
     465      319097 :         if (!mod)
     466             :                 return;
     467             : 
     468             :         /* calculate the inode offset and align startino */
     469      181836 :         offset = XFS_AGB_TO_AGINO(mp, mod);
     470      181836 :         *startino -= offset;
     471             : 
     472             :         /*
     473             :          * Since startino has been aligned down, left shift allocmask such that
     474             :          * it continues to represent the same physical inodes relative to the
     475             :          * new startino.
     476             :          */
     477      181836 :         *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
     478             : }
     479             : 
     480             : /*
     481             :  * Determine whether the source inode record can merge into the target. Both
     482             :  * records must be sparse, the inode ranges must match and there must be no
     483             :  * allocation overlap between the records.
     484             :  */
     485             : STATIC bool
     486       27421 : __xfs_inobt_can_merge(
     487             :         struct xfs_inobt_rec_incore     *trec,  /* tgt record */
     488             :         struct xfs_inobt_rec_incore     *srec)  /* src record */
     489             : {
     490       27421 :         uint64_t                        talloc;
     491       27421 :         uint64_t                        salloc;
     492             : 
     493             :         /* records must cover the same inode range */
     494       27421 :         if (trec->ir_startino != srec->ir_startino)
     495             :                 return false;
     496             : 
     497             :         /* both records must be sparse */
     498       27421 :         if (!xfs_inobt_issparse(trec->ir_holemask) ||
     499       27421 :             !xfs_inobt_issparse(srec->ir_holemask))
     500             :                 return false;
     501             : 
     502             :         /* both records must track some inodes */
     503       27421 :         if (!trec->ir_count || !srec->ir_count)
     504             :                 return false;
     505             : 
     506             :         /* can't exceed capacity of a full record */
     507       27421 :         if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
     508             :                 return false;
     509             : 
     510             :         /* verify there is no allocation overlap */
     511       27421 :         talloc = xfs_inobt_irec_to_allocmask(trec);
     512       27421 :         salloc = xfs_inobt_irec_to_allocmask(srec);
     513       27421 :         if (talloc & salloc)
     514           0 :                 return false;
     515             : 
     516             :         return true;
     517             : }
     518             : 
     519             : /*
     520             :  * Merge the source inode record into the target. The caller must call
     521             :  * __xfs_inobt_can_merge() to ensure the merge is valid.
     522             :  */
     523             : STATIC void
     524       27421 : __xfs_inobt_rec_merge(
     525             :         struct xfs_inobt_rec_incore     *trec,  /* target */
     526             :         struct xfs_inobt_rec_incore     *srec)  /* src */
     527             : {
     528       27421 :         ASSERT(trec->ir_startino == srec->ir_startino);
     529             : 
     530             :         /* combine the counts */
     531       27421 :         trec->ir_count += srec->ir_count;
     532       27421 :         trec->ir_freecount += srec->ir_freecount;
     533             : 
     534             :         /*
     535             :          * Merge the holemask and free mask. For both fields, 0 bits refer to
     536             :          * allocated inodes. We combine the allocated ranges with bitwise AND.
     537             :          */
     538       27421 :         trec->ir_holemask &= srec->ir_holemask;
     539       27421 :         trec->ir_free &= srec->ir_free;
     540       27421 : }
     541             : 
     542             : /*
     543             :  * Insert a new sparse inode chunk into the associated inode btree. The inode
     544             :  * record for the sparse chunk is pre-aligned to a startino that should match
     545             :  * any pre-existing sparse inode record in the tree. This allows sparse chunks
     546             :  * to fill over time.
     547             :  *
     548             :  * This function supports two modes of handling preexisting records depending on
     549             :  * the merge flag. If merge is true, the provided record is merged with the
     550             :  * existing record and updated in place. The merged record is returned in nrec.
     551             :  * If merge is false, an existing record is replaced with the provided record.
     552             :  * If no preexisting record exists, the provided record is always inserted.
     553             :  *
     554             :  * It is considered corruption if a merge is requested and not possible. Given
     555             :  * the sparse inode alignment constraints, this should never happen.
     556             :  */
     557             : STATIC int
     558      638171 : xfs_inobt_insert_sprec(
     559             :         struct xfs_perag                *pag,
     560             :         struct xfs_trans                *tp,
     561             :         struct xfs_buf                  *agbp,
     562             :         int                             btnum,
     563             :         struct xfs_inobt_rec_incore     *nrec,  /* in/out: new/merged rec. */
     564             :         bool                            merge)  /* merge or replace */
     565             : {
     566      638171 :         struct xfs_mount                *mp = pag->pag_mount;
     567      638171 :         struct xfs_btree_cur            *cur;
     568      638171 :         int                             error;
     569      638171 :         int                             i;
     570      638171 :         struct xfs_inobt_rec_incore     rec;
     571             : 
     572      638171 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, btnum);
     573             : 
     574             :         /* the new record is pre-aligned so we know where to look */
     575      638176 :         error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
     576      638183 :         if (error)
     577           2 :                 goto error;
     578             :         /* if nothing there, insert a new record and return */
     579      638181 :         if (i == 0) {
     580      610760 :                 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
     581      610760 :                                              nrec->ir_count, nrec->ir_freecount,
     582             :                                              nrec->ir_free, &i);
     583      610756 :                 if (error)
     584           0 :                         goto error;
     585      610756 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
     586           0 :                         xfs_btree_mark_sick(cur);
     587           0 :                         error = -EFSCORRUPTED;
     588           0 :                         goto error;
     589             :                 }
     590             : 
     591      610756 :                 goto out;
     592             :         }
     593             : 
     594             :         /*
     595             :          * A record exists at this startino. Merge or replace the record
     596             :          * depending on what we've been asked to do.
     597             :          */
     598       27421 :         if (merge) {
     599       27421 :                 error = xfs_inobt_get_rec(cur, &rec, &i);
     600       27421 :                 if (error)
     601           0 :                         goto error;
     602       27421 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
     603           0 :                         xfs_btree_mark_sick(cur);
     604           0 :                         error = -EFSCORRUPTED;
     605           0 :                         goto error;
     606             :                 }
     607       27421 :                 if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) {
     608           0 :                         xfs_btree_mark_sick(cur);
     609           0 :                         error = -EFSCORRUPTED;
     610           0 :                         goto error;
     611             :                 }
     612             : 
     613             :                 /*
     614             :                  * This should never fail. If we have coexisting records that
     615             :                  * cannot merge, something is seriously wrong.
     616             :                  */
     617       27421 :                 if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) {
     618           0 :                         xfs_btree_mark_sick(cur);
     619           0 :                         error = -EFSCORRUPTED;
     620           0 :                         goto error;
     621             :                 }
     622             : 
     623       27421 :                 trace_xfs_irec_merge_pre(mp, pag->pag_agno, rec.ir_startino,
     624             :                                          rec.ir_holemask, nrec->ir_startino,
     625             :                                          nrec->ir_holemask);
     626             : 
     627             :                 /* merge to nrec to output the updated record */
     628       27421 :                 __xfs_inobt_rec_merge(nrec, &rec);
     629             : 
     630       27421 :                 trace_xfs_irec_merge_post(mp, pag->pag_agno, nrec->ir_startino,
     631             :                                           nrec->ir_holemask);
     632             : 
     633       27421 :                 error = xfs_inobt_rec_check_count(mp, nrec);
     634       27421 :                 if (error)
     635           0 :                         goto error;
     636             :         }
     637             : 
     638       27421 :         error = xfs_inobt_update(cur, nrec);
     639       27421 :         if (error)
     640           0 :                 goto error;
     641             : 
     642       27421 : out:
     643      638177 :         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
     644      638177 :         return 0;
     645           2 : error:
     646           2 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
     647           2 :         return error;
     648             : }
     649             : 
     650             : /*
     651             :  * Allocate new inodes in the allocation group specified by agbp.  Returns 0 if
     652             :  * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so
     653             :  * the caller knows it can try another AG, a hard -ENOSPC when over the maximum
     654             :  * inode count threshold, or the usual negative error code for other errors.
     655             :  */
     656             : STATIC int
     657      921539 : xfs_ialloc_ag_alloc(
     658             :         struct xfs_perag        *pag,
     659             :         struct xfs_trans        *tp,
     660             :         struct xfs_buf          *agbp)
     661             : {
     662      921539 :         struct xfs_agi          *agi;
     663      921539 :         struct xfs_alloc_arg    args;
     664      921539 :         int                     error;
     665      921539 :         xfs_agino_t             newino;         /* new first inode's number */
     666      921539 :         xfs_agino_t             newlen;         /* new number of inodes */
     667      921539 :         int                     isaligned = 0;  /* inode allocation at stripe */
     668             :                                                 /* unit boundary */
     669             :         /* init. to full chunk */
     670      921539 :         struct xfs_inobt_rec_incore rec;
     671      921539 :         struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp);
     672      921539 :         uint16_t                allocmask = (uint16_t) -1;
     673      921539 :         int                     do_sparse = 0;
     674             : 
     675      921539 :         memset(&args, 0, sizeof(args));
     676      921539 :         args.tp = tp;
     677      921539 :         args.mp = tp->t_mountp;
     678      921539 :         args.fsbno = NULLFSBLOCK;
     679      921539 :         args.oinfo = XFS_RMAP_OINFO_INODES;
     680      921539 :         args.pag = pag;
     681             : 
     682             : #ifdef DEBUG
     683             :         /* randomly do sparse inode allocations */
     684      921539 :         if (xfs_has_sparseinodes(tp->t_mountp) &&
     685      921535 :             igeo->ialloc_min_blks < igeo->ialloc_blks)
     686      921485 :                 do_sparse = get_random_u32_below(2);
     687             : #endif
     688             : 
     689             :         /*
     690             :          * Locking will ensure that we don't have two callers in here
     691             :          * at one time.
     692             :          */
     693      921538 :         newlen = igeo->ialloc_inos;
     694      921538 :         if (igeo->maxicount &&
     695      921536 :             percpu_counter_read_positive(&args.mp->m_icount) + newlen >
     696             :                                                         igeo->maxicount)
     697             :                 return -ENOSPC;
     698      921513 :         args.minlen = args.maxlen = igeo->ialloc_blks;
     699             :         /*
     700             :          * First try to allocate inodes contiguous with the last-allocated
     701             :          * chunk of inodes.  If the filesystem is striped, this will fill
     702             :          * an entire stripe unit with inodes.
     703             :          */
     704      921513 :         agi = agbp->b_addr;
     705      921513 :         newino = be32_to_cpu(agi->agi_newino);
     706      921513 :         args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
     707             :                      igeo->ialloc_blks;
     708      921513 :         if (do_sparse)
     709      460473 :                 goto sparse_alloc;
     710      902137 :         if (likely(newino != NULLAGINO &&
     711             :                   (args.agbno < be32_to_cpu(agi->agi_length)))) {
     712      441093 :                 args.prod = 1;
     713             : 
     714             :                 /*
     715             :                  * We need to take into account alignment here to ensure that
     716             :                  * we don't modify the free list if we fail to have an exact
     717             :                  * block. If we don't have an exact match, and every oher
     718             :                  * attempt allocation attempt fails, we'll end up cancelling
     719             :                  * a dirty transaction and shutting down.
     720             :                  *
     721             :                  * For an exact allocation, alignment must be 1,
     722             :                  * however we need to take cluster alignment into account when
     723             :                  * fixing up the freelist. Use the minalignslop field to
     724             :                  * indicate that extra blocks might be required for alignment,
     725             :                  * but not to use them in the actual exact allocation.
     726             :                  */
     727      441093 :                 args.alignment = 1;
     728      441093 :                 args.minalignslop = igeo->cluster_align - 1;
     729             : 
     730             :                 /* Allow space for the inode btree to split. */
     731      441093 :                 args.minleft = igeo->inobt_maxlevels;
     732      441093 :                 error = xfs_alloc_vextent_exact_bno(&args,
     733      441093 :                                 XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
     734             :                                                 args.agbno));
     735      441092 :                 if (error)
     736             :                         return error;
     737             : 
     738             :                 /*
     739             :                  * This request might have dirtied the transaction if the AG can
     740             :                  * satisfy the request, but the exact block was not available.
     741             :                  * If the allocation did fail, subsequent requests will relax
     742             :                  * the exact agbno requirement and increase the alignment
     743             :                  * instead. It is critical that the total size of the request
     744             :                  * (len + alignment + slop) does not increase from this point
     745             :                  * on, so reset minalignslop to ensure it is not included in
     746             :                  * subsequent requests.
     747             :                  */
     748      441090 :                 args.minalignslop = 0;
     749             :         }
     750             : 
     751      461037 :         if (unlikely(args.fsbno == NULLFSBLOCK)) {
     752             :                 /*
     753             :                  * Set the alignment for the allocation.
     754             :                  * If stripe alignment is turned on then align at stripe unit
     755             :                  * boundary.
     756             :                  * If the cluster size is smaller than a filesystem block
     757             :                  * then we're doing I/O for inodes in filesystem block size
     758             :                  * pieces, so don't need alignment anyway.
     759             :                  */
     760      389458 :                 isaligned = 0;
     761      389458 :                 if (igeo->ialloc_align) {
     762           0 :                         ASSERT(!xfs_has_noalign(args.mp));
     763           0 :                         args.alignment = args.mp->m_dalign;
     764           0 :                         isaligned = 1;
     765             :                 } else
     766      389458 :                         args.alignment = igeo->cluster_align;
     767             :                 /*
     768             :                  * Allocate a fixed-size extent of inodes.
     769             :                  */
     770      389458 :                 args.prod = 1;
     771             :                 /*
     772             :                  * Allow space for the inode btree to split.
     773             :                  */
     774      389458 :                 args.minleft = igeo->inobt_maxlevels;
     775      389458 :                 error = xfs_alloc_vextent_near_bno(&args,
     776      389458 :                                 XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
     777             :                                                 be32_to_cpu(agi->agi_root)));
     778      389457 :                 if (error)
     779             :                         return error;
     780             :         }
     781             : 
     782             :         /*
     783             :          * If stripe alignment is turned on, then try again with cluster
     784             :          * alignment.
     785             :          */
     786      389456 :         if (isaligned && args.fsbno == NULLFSBLOCK) {
     787           0 :                 args.alignment = igeo->cluster_align;
     788           0 :                 error = xfs_alloc_vextent_near_bno(&args,
     789           0 :                                 XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
     790             :                                                 be32_to_cpu(agi->agi_root)));
     791           0 :                 if (error)
     792             :                         return error;
     793             :         }
     794             : 
     795             :         /*
     796             :          * Finally, try a sparse allocation if the filesystem supports it and
     797             :          * the sparse allocation length is smaller than a full chunk.
     798             :          */
     799      461035 :         if (xfs_has_sparseinodes(args.mp) &&
     800      461027 :             igeo->ialloc_min_blks < igeo->ialloc_blks &&
     801      460977 :             args.fsbno == NULLFSBLOCK) {
     802      143445 : sparse_alloc:
     803      603918 :                 args.alignment = args.mp->m_sb.sb_spino_align;
     804      603918 :                 args.prod = 1;
     805             : 
     806      603918 :                 args.minlen = igeo->ialloc_min_blks;
     807      603918 :                 args.maxlen = args.minlen;
     808             : 
     809             :                 /*
     810             :                  * The inode record will be aligned to full chunk size. We must
     811             :                  * prevent sparse allocation from AG boundaries that result in
     812             :                  * invalid inode records, such as records that start at agbno 0
     813             :                  * or extend beyond the AG.
     814             :                  *
     815             :                  * Set min agbno to the first aligned, non-zero agbno and max to
     816             :                  * the last aligned agbno that is at least one full chunk from
     817             :                  * the end of the AG.
     818             :                  */
     819      603918 :                 args.min_agbno = args.mp->m_sb.sb_inoalignmt;
     820      603918 :                 args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
     821      603918 :                                             args.mp->m_sb.sb_inoalignmt) -
     822      603918 :                                  igeo->ialloc_blks;
     823             : 
     824      603918 :                 error = xfs_alloc_vextent_near_bno(&args,
     825      603918 :                                 XFS_AGB_TO_FSB(args.mp, pag->pag_agno,
     826             :                                                 be32_to_cpu(agi->agi_root)));
     827      603914 :                 if (error)
     828             :                         return error;
     829             : 
     830      603909 :                 newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
     831      603909 :                 ASSERT(newlen <= XFS_INODES_PER_CHUNK);
     832      603909 :                 allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
     833             :         }
     834             : 
     835      921499 :         if (args.fsbno == NULLFSBLOCK)
     836             :                 return -EAGAIN;
     837             : 
     838      636685 :         ASSERT(args.len == args.minlen);
     839             : 
     840             :         /*
     841             :          * Stamp and write the inode buffers.
     842             :          *
     843             :          * Seed the new inode cluster with a random generation number. This
     844             :          * prevents short-term reuse of generation numbers if a chunk is
     845             :          * freed and then immediately reallocated. We use random numbers
     846             :          * rather than a linear progression to prevent the next generation
     847             :          * number from being easily guessable.
     848             :          */
     849      636685 :         error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag->pag_agno,
     850             :                         args.agbno, args.len, get_random_u32());
     851             : 
     852      636686 :         if (error)
     853             :                 return error;
     854             :         /*
     855             :          * Convert the results.
     856             :          */
     857      636686 :         newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
     858             : 
     859      636686 :         if (xfs_inobt_issparse(~allocmask)) {
     860             :                 /*
     861             :                  * We've allocated a sparse chunk. Align the startino and mask.
     862             :                  */
     863      319095 :                 xfs_align_sparse_ino(args.mp, &newino, &allocmask);
     864             : 
     865      319093 :                 rec.ir_startino = newino;
     866      319093 :                 rec.ir_holemask = ~allocmask;
     867      319093 :                 rec.ir_count = newlen;
     868      319093 :                 rec.ir_freecount = newlen;
     869      319093 :                 rec.ir_free = XFS_INOBT_ALL_FREE;
     870             : 
     871             :                 /*
     872             :                  * Insert the sparse record into the inobt and allow for a merge
     873             :                  * if necessary. If a merge does occur, rec is updated to the
     874             :                  * merged record.
     875             :                  */
     876      319093 :                 error = xfs_inobt_insert_sprec(pag, tp, agbp,
     877             :                                 XFS_BTNUM_INO, &rec, true);
     878      319095 :                 if (error == -EFSCORRUPTED) {
     879           0 :                         xfs_alert(args.mp,
     880             :         "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
     881             :                                   XFS_AGINO_TO_INO(args.mp, pag->pag_agno,
     882             :                                                    rec.ir_startino),
     883             :                                   rec.ir_holemask, rec.ir_count);
     884           0 :                         xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
     885             :                 }
     886      319095 :                 if (error)
     887             :                         return error;
     888             : 
     889             :                 /*
     890             :                  * We can't merge the part we've just allocated as for the inobt
     891             :                  * due to finobt semantics. The original record may or may not
     892             :                  * exist independent of whether physical inodes exist in this
     893             :                  * sparse chunk.
     894             :                  *
     895             :                  * We must update the finobt record based on the inobt record.
     896             :                  * rec contains the fully merged and up to date inobt record
     897             :                  * from the previous call. Set merge false to replace any
     898             :                  * existing record with this one.
     899             :                  */
     900      319095 :                 if (xfs_has_finobt(args.mp)) {
     901      319082 :                         error = xfs_inobt_insert_sprec(pag, tp, agbp,
     902             :                                        XFS_BTNUM_FINO, &rec, false);
     903      319084 :                         if (error)
     904             :                                 return error;
     905             :                 }
     906             :         } else {
     907             :                 /* full chunk - insert new records to both btrees */
     908      317591 :                 error = xfs_inobt_insert(pag, tp, agbp, newino, newlen,
     909             :                                          XFS_BTNUM_INO);
     910      317593 :                 if (error)
     911             :                         return error;
     912             : 
     913      317593 :                 if (xfs_has_finobt(args.mp)) {
     914      317583 :                         error = xfs_inobt_insert(pag, tp, agbp, newino,
     915             :                                                  newlen, XFS_BTNUM_FINO);
     916      317583 :                         if (error)
     917             :                                 return error;
     918             :                 }
     919             :         }
     920             : 
     921             :         /*
     922             :          * Update AGI counts and newino.
     923             :          */
     924      636688 :         be32_add_cpu(&agi->agi_count, newlen);
     925      636684 :         be32_add_cpu(&agi->agi_freecount, newlen);
     926      636690 :         pag->pagi_freecount += newlen;
     927      636690 :         pag->pagi_count += newlen;
     928      636690 :         agi->agi_newino = cpu_to_be32(newino);
     929             : 
     930             :         /*
     931             :          * Log allocation group header fields
     932             :          */
     933      636690 :         xfs_ialloc_log_agi(tp, agbp,
     934             :                 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
     935             :         /*
     936             :          * Modify/log superblock values for inode count and inode free count.
     937             :          */
     938      636689 :         xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
     939      636688 :         xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
     940      636688 :         return 0;
     941             : }
     942             : 
     943             : /*
     944             :  * Try to retrieve the next record to the left/right from the current one.
     945             :  */
     946             : STATIC int
     947         533 : xfs_ialloc_next_rec(
     948             :         struct xfs_btree_cur    *cur,
     949             :         xfs_inobt_rec_incore_t  *rec,
     950             :         int                     *done,
     951             :         int                     left)
     952             : {
     953         533 :         int                     error;
     954         533 :         int                     i;
     955             : 
     956         533 :         if (left)
     957         260 :                 error = xfs_btree_decrement(cur, 0, &i);
     958             :         else
     959         273 :                 error = xfs_btree_increment(cur, 0, &i);
     960             : 
     961         533 :         if (error)
     962             :                 return error;
     963         533 :         *done = !i;
     964         533 :         if (i) {
     965         287 :                 error = xfs_inobt_get_rec(cur, rec, &i);
     966         287 :                 if (error)
     967             :                         return error;
     968         287 :                 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
     969           0 :                         xfs_btree_mark_sick(cur);
     970           0 :                         return -EFSCORRUPTED;
     971             :                 }
     972             :         }
     973             : 
     974             :         return 0;
     975             : }
     976             : 
     977             : STATIC int
     978         248 : xfs_ialloc_get_rec(
     979             :         struct xfs_btree_cur    *cur,
     980             :         xfs_agino_t             agino,
     981             :         xfs_inobt_rec_incore_t  *rec,
     982             :         int                     *done)
     983             : {
     984         248 :         int                     error;
     985         248 :         int                     i;
     986             : 
     987         248 :         error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
     988         248 :         if (error)
     989             :                 return error;
     990         248 :         *done = !i;
     991         248 :         if (i) {
     992         127 :                 error = xfs_inobt_get_rec(cur, rec, &i);
     993         127 :                 if (error)
     994             :                         return error;
     995         127 :                 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
     996           0 :                         xfs_btree_mark_sick(cur);
     997           0 :                         return -EFSCORRUPTED;
     998             :                 }
     999             :         }
    1000             : 
    1001             :         return 0;
    1002             : }
    1003             : 
    1004             : /*
    1005             :  * Return the offset of the first free inode in the record. If the inode chunk
    1006             :  * is sparsely allocated, we convert the record holemask to inode granularity
    1007             :  * and mask off the unallocated regions from the inode free mask.
    1008             :  */
    1009             : STATIC int
    1010    64362721 : xfs_inobt_first_free_inode(
    1011             :         struct xfs_inobt_rec_incore     *rec)
    1012             : {
    1013    64362721 :         xfs_inofree_t                   realfree;
    1014             : 
    1015             :         /* if there are no holes, return the first available offset */
    1016    64362721 :         if (!xfs_inobt_issparse(rec->ir_holemask))
    1017    40717234 :                 return xfs_lowbit64(rec->ir_free);
    1018             : 
    1019    23645487 :         realfree = xfs_inobt_irec_to_allocmask(rec);
    1020    23645639 :         realfree &= rec->ir_free;
    1021             : 
    1022    47291278 :         return xfs_lowbit64(realfree);
    1023             : }
    1024             : 
    1025             : /*
    1026             :  * If this AG has corrupt inodes, check if allocating this inode would fail
    1027             :  * with corruption errors.  Returns 0 if we're clear, or EAGAIN to try again
    1028             :  * somewhere else.
    1029             :  */
    1030             : static int
    1031           0 : xfs_dialloc_check_ino(
    1032             :         struct xfs_perag        *pag,
    1033             :         struct xfs_trans        *tp,
    1034             :         xfs_ino_t               ino)
    1035             : {
    1036           0 :         struct xfs_imap         imap;
    1037           0 :         struct xfs_buf          *bp;
    1038           0 :         int                     error;
    1039             : 
    1040           0 :         error = xfs_imap(pag, tp, ino, &imap, 0);
    1041           0 :         if (error)
    1042             :                 return -EAGAIN;
    1043             : 
    1044           0 :         error = xfs_imap_to_bp(pag->pag_mount, tp, &imap, &bp);
    1045           0 :         if (error)
    1046             :                 return -EAGAIN;
    1047             : 
    1048           0 :         xfs_trans_brelse(tp, bp);
    1049           0 :         return 0;
    1050             : }
    1051             : 
    1052             : /*
    1053             :  * Allocate an inode using the inobt-only algorithm.
    1054             :  */
    1055             : STATIC int
    1056        1341 : xfs_dialloc_ag_inobt(
    1057             :         struct xfs_perag        *pag,
    1058             :         struct xfs_trans        *tp,
    1059             :         struct xfs_buf          *agbp,
    1060             :         xfs_ino_t               parent,
    1061             :         xfs_ino_t               *inop)
    1062             : {
    1063        1341 :         struct xfs_mount        *mp = tp->t_mountp;
    1064        1341 :         struct xfs_agi          *agi = agbp->b_addr;
    1065        1341 :         xfs_agnumber_t          pagno = XFS_INO_TO_AGNO(mp, parent);
    1066        1341 :         xfs_agino_t             pagino = XFS_INO_TO_AGINO(mp, parent);
    1067        1341 :         struct xfs_btree_cur    *cur, *tcur;
    1068        1341 :         struct xfs_inobt_rec_incore rec, trec;
    1069        1341 :         xfs_ino_t               ino;
    1070        1341 :         int                     error;
    1071        1341 :         int                     offset;
    1072        1341 :         int                     i, j;
    1073        1341 :         int                     searchdistance = 10;
    1074             : 
    1075        2682 :         ASSERT(xfs_perag_initialised_agi(pag));
    1076        2682 :         ASSERT(xfs_perag_allows_inodes(pag));
    1077        2682 :         ASSERT(!xfs_perag_prohibits_alloc(pag));
    1078        1341 :         ASSERT(pag->pagi_freecount > 0);
    1079             : 
    1080        1341 :  restart_pagno:
    1081        1341 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
    1082             :         /*
    1083             :          * If pagino is 0 (this is the root inode allocation) use newino.
    1084             :          * This must work because we've just allocated some.
    1085             :          */
    1086        1341 :         if (!pagino)
    1087          22 :                 pagino = be32_to_cpu(agi->agi_newino);
    1088             : 
    1089        1341 :         error = xfs_check_agi_freecount(cur);
    1090        1341 :         if (error)
    1091           0 :                 goto error0;
    1092             : 
    1093             :         /*
    1094             :          * If in the same AG as the parent, try to get near the parent.
    1095             :          */
    1096        1341 :         if (pagno == pag->pag_agno) {
    1097        1194 :                 int             doneleft;       /* done, to the left */
    1098        1194 :                 int             doneright;      /* done, to the right */
    1099             : 
    1100        1194 :                 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
    1101        1194 :                 if (error)
    1102           0 :                         goto error0;
    1103        1194 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    1104           0 :                         xfs_btree_mark_sick(cur);
    1105           0 :                         error = -EFSCORRUPTED;
    1106           0 :                         goto error0;
    1107             :                 }
    1108             : 
    1109        1194 :                 error = xfs_inobt_get_rec(cur, &rec, &j);
    1110        1194 :                 if (error)
    1111           0 :                         goto error0;
    1112        1194 :                 if (XFS_IS_CORRUPT(mp, j != 1)) {
    1113           0 :                         xfs_btree_mark_sick(cur);
    1114           0 :                         error = -EFSCORRUPTED;
    1115           0 :                         goto error0;
    1116             :                 }
    1117             : 
    1118        1194 :                 if (rec.ir_freecount > 0) {
    1119             :                         /*
    1120             :                          * Found a free inode in the same chunk
    1121             :                          * as the parent, done.
    1122             :                          */
    1123        1194 :                         goto alloc_inode;
    1124             :                 }
    1125             : 
    1126             : 
    1127             :                 /*
    1128             :                  * In the same AG as parent, but parent's chunk is full.
    1129             :                  */
    1130             : 
    1131             :                 /* duplicate the cursor, search left & right simultaneously */
    1132         382 :                 error = xfs_btree_dup_cursor(cur, &tcur);
    1133         382 :                 if (error)
    1134           0 :                         goto error0;
    1135             : 
    1136             :                 /*
    1137             :                  * Skip to last blocks looked up if same parent inode.
    1138             :                  */
    1139         382 :                 if (pagino != NULLAGINO &&
    1140         382 :                     pag->pagl_pagino == pagino &&
    1141         124 :                     pag->pagl_leftrec != NULLAGINO &&
    1142         124 :                     pag->pagl_rightrec != NULLAGINO) {
    1143         124 :                         error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
    1144             :                                                    &trec, &doneleft);
    1145         124 :                         if (error)
    1146           0 :                                 goto error1;
    1147             : 
    1148         124 :                         error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
    1149             :                                                    &rec, &doneright);
    1150         124 :                         if (error)
    1151           0 :                                 goto error1;
    1152             :                 } else {
    1153             :                         /* search left with tcur, back up 1 record */
    1154         258 :                         error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
    1155         258 :                         if (error)
    1156           0 :                                 goto error1;
    1157             : 
    1158             :                         /* search right with cur, go forward 1 record. */
    1159         258 :                         error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
    1160         258 :                         if (error)
    1161           0 :                                 goto error1;
    1162             :                 }
    1163             : 
    1164             :                 /*
    1165             :                  * Loop until we find an inode chunk with a free inode.
    1166             :                  */
    1167         399 :                 while (--searchdistance > 0 && (!doneleft || !doneright)) {
    1168         399 :                         int     useleft;  /* using left inode chunk this time */
    1169             : 
    1170             :                         /* figure out the closer block if both are valid. */
    1171         399 :                         if (!doneleft && !doneright) {
    1172          22 :                                 useleft = pagino -
    1173          22 :                                  (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
    1174          22 :                                   rec.ir_startino - pagino;
    1175             :                         } else {
    1176         377 :                                 useleft = !doneleft;
    1177             :                         }
    1178             : 
    1179             :                         /* free inodes to the left? */
    1180         399 :                         if (useleft && trec.ir_freecount) {
    1181          33 :                                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
    1182          33 :                                 cur = tcur;
    1183             : 
    1184          33 :                                 pag->pagl_leftrec = trec.ir_startino;
    1185          33 :                                 pag->pagl_rightrec = rec.ir_startino;
    1186          33 :                                 pag->pagl_pagino = pagino;
    1187          33 :                                 rec = trec;
    1188          33 :                                 goto alloc_inode;
    1189             :                         }
    1190             : 
    1191             :                         /* free inodes to the right? */
    1192         366 :                         if (!useleft && rec.ir_freecount) {
    1193         349 :                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
    1194             : 
    1195         349 :                                 pag->pagl_leftrec = trec.ir_startino;
    1196         349 :                                 pag->pagl_rightrec = rec.ir_startino;
    1197         349 :                                 pag->pagl_pagino = pagino;
    1198         349 :                                 goto alloc_inode;
    1199             :                         }
    1200             : 
    1201             :                         /* get next record to check */
    1202          17 :                         if (useleft) {
    1203           2 :                                 error = xfs_ialloc_next_rec(tcur, &trec,
    1204             :                                                                  &doneleft, 1);
    1205             :                         } else {
    1206          15 :                                 error = xfs_ialloc_next_rec(cur, &rec,
    1207             :                                                                  &doneright, 0);
    1208             :                         }
    1209          17 :                         if (error)
    1210           0 :                                 goto error1;
    1211             :                 }
    1212             : 
    1213           0 :                 if (searchdistance <= 0) {
    1214             :                         /*
    1215             :                          * Not in range - save last search
    1216             :                          * location and allocate a new inode
    1217             :                          */
    1218           0 :                         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
    1219           0 :                         pag->pagl_leftrec = trec.ir_startino;
    1220           0 :                         pag->pagl_rightrec = rec.ir_startino;
    1221           0 :                         pag->pagl_pagino = pagino;
    1222             : 
    1223             :                 } else {
    1224             :                         /*
    1225             :                          * We've reached the end of the btree. because
    1226             :                          * we are only searching a small chunk of the
    1227             :                          * btree each search, there is obviously free
    1228             :                          * inodes closer to the parent inode than we
    1229             :                          * are now. restart the search again.
    1230             :                          */
    1231           0 :                         pag->pagl_pagino = NULLAGINO;
    1232           0 :                         pag->pagl_leftrec = NULLAGINO;
    1233           0 :                         pag->pagl_rightrec = NULLAGINO;
    1234           0 :                         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
    1235           0 :                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
    1236           0 :                         goto restart_pagno;
    1237             :                 }
    1238             :         }
    1239             : 
    1240             :         /*
    1241             :          * In a different AG from the parent.
    1242             :          * See if the most recently allocated block has any free.
    1243             :          */
    1244         147 :         if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
    1245         147 :                 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
    1246             :                                          XFS_LOOKUP_EQ, &i);
    1247         147 :                 if (error)
    1248           0 :                         goto error0;
    1249             : 
    1250         147 :                 if (i == 1) {
    1251         147 :                         error = xfs_inobt_get_rec(cur, &rec, &j);
    1252         147 :                         if (error)
    1253           0 :                                 goto error0;
    1254             : 
    1255         147 :                         if (j == 1 && rec.ir_freecount > 0) {
    1256             :                                 /*
    1257             :                                  * The last chunk allocated in the group
    1258             :                                  * still has a free inode.
    1259             :                                  */
    1260         147 :                                 goto alloc_inode;
    1261             :                         }
    1262             :                 }
    1263             :         }
    1264             : 
    1265             :         /*
    1266             :          * None left in the last group, search the whole AG
    1267             :          */
    1268           0 :         error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
    1269           0 :         if (error)
    1270           0 :                 goto error0;
    1271           0 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    1272           0 :                 xfs_btree_mark_sick(cur);
    1273           0 :                 error = -EFSCORRUPTED;
    1274           0 :                 goto error0;
    1275             :         }
    1276             : 
    1277           0 :         for (;;) {
    1278           0 :                 error = xfs_inobt_get_rec(cur, &rec, &i);
    1279           0 :                 if (error)
    1280           0 :                         goto error0;
    1281           0 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    1282           0 :                         xfs_btree_mark_sick(cur);
    1283           0 :                         error = -EFSCORRUPTED;
    1284           0 :                         goto error0;
    1285             :                 }
    1286           0 :                 if (rec.ir_freecount > 0)
    1287             :                         break;
    1288           0 :                 error = xfs_btree_increment(cur, 0, &i);
    1289           0 :                 if (error)
    1290           0 :                         goto error0;
    1291           0 :                 if (XFS_IS_CORRUPT(mp, i != 1)) {
    1292           0 :                         xfs_btree_mark_sick(cur);
    1293           0 :                         error = -EFSCORRUPTED;
    1294           0 :                         goto error0;
    1295             :                 }
    1296             :         }
    1297             : 
    1298           0 : alloc_inode:
    1299        1341 :         offset = xfs_inobt_first_free_inode(&rec);
    1300        1341 :         ASSERT(offset >= 0);
    1301        1341 :         ASSERT(offset < XFS_INODES_PER_CHUNK);
    1302        1341 :         ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
    1303             :                                    XFS_INODES_PER_CHUNK) == 0);
    1304        1341 :         ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset);
    1305             : 
    1306        1341 :         if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) {
    1307           0 :                 error = xfs_dialloc_check_ino(pag, tp, ino);
    1308           0 :                 if (error)
    1309           0 :                         goto error0;
    1310             :         }
    1311             : 
    1312        1341 :         rec.ir_free &= ~XFS_INOBT_MASK(offset);
    1313        1341 :         rec.ir_freecount--;
    1314        1341 :         error = xfs_inobt_update(cur, &rec);
    1315        1341 :         if (error)
    1316           0 :                 goto error0;
    1317        1341 :         be32_add_cpu(&agi->agi_freecount, -1);
    1318        1341 :         xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
    1319        1341 :         pag->pagi_freecount--;
    1320             : 
    1321        1341 :         error = xfs_check_agi_freecount(cur);
    1322        1341 :         if (error)
    1323           0 :                 goto error0;
    1324             : 
    1325        1341 :         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
    1326        1341 :         xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
    1327        1341 :         *inop = ino;
    1328        1341 :         return 0;
    1329             : error1:
    1330           0 :         xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
    1331           0 : error0:
    1332           0 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
    1333           0 :         return error;
    1334             : }
    1335             : 
    1336             : /*
    1337             :  * Use the free inode btree to allocate an inode based on distance from the
    1338             :  * parent. Note that the provided cursor may be deleted and replaced.
    1339             :  */
    1340             : STATIC int
    1341    58957526 : xfs_dialloc_ag_finobt_near(
    1342             :         xfs_agino_t                     pagino,
    1343             :         struct xfs_btree_cur            **ocur,
    1344             :         struct xfs_inobt_rec_incore     *rec)
    1345             : {
    1346    58957526 :         struct xfs_btree_cur            *lcur = *ocur;  /* left search cursor */
    1347    58957526 :         struct xfs_btree_cur            *rcur;  /* right search cursor */
    1348    58957526 :         struct xfs_inobt_rec_incore     rrec;
    1349    58957526 :         int                             error;
    1350    58957526 :         int                             i, j;
    1351             : 
    1352    58957526 :         error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
    1353    58958878 :         if (error)
    1354             :                 return error;
    1355             : 
    1356    58958878 :         if (i == 1) {
    1357     6870028 :                 error = xfs_inobt_get_rec(lcur, rec, &i);
    1358     6870059 :                 if (error)
    1359             :                         return error;
    1360     6870059 :                 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1)) {
    1361           0 :                         xfs_btree_mark_sick(lcur);
    1362           0 :                         return -EFSCORRUPTED;
    1363             :                 }
    1364             : 
    1365             :                 /*
    1366             :                  * See if we've landed in the parent inode record. The finobt
    1367             :                  * only tracks chunks with at least one free inode, so record
    1368             :                  * existence is enough.
    1369             :                  */
    1370     6870059 :                 if (pagino >= rec->ir_startino &&
    1371     6870073 :                     pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
    1372             :                         return 0;
    1373             :         }
    1374             : 
    1375    56718570 :         error = xfs_btree_dup_cursor(lcur, &rcur);
    1376    56718129 :         if (error)
    1377             :                 return error;
    1378             : 
    1379    56719500 :         error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
    1380    56718028 :         if (error)
    1381           0 :                 goto error_rcur;
    1382    56718028 :         if (j == 1) {
    1383    54722713 :                 error = xfs_inobt_get_rec(rcur, &rrec, &j);
    1384    54723438 :                 if (error)
    1385           0 :                         goto error_rcur;
    1386    54723438 :                 if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) {
    1387           0 :                         xfs_btree_mark_sick(lcur);
    1388           0 :                         error = -EFSCORRUPTED;
    1389           0 :                         goto error_rcur;
    1390             :                 }
    1391             :         }
    1392             : 
    1393    56718753 :         if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) {
    1394           0 :                 xfs_btree_mark_sick(lcur);
    1395           0 :                 error = -EFSCORRUPTED;
    1396           0 :                 goto error_rcur;
    1397             :         }
    1398    56718753 :         if (i == 1 && j == 1) {
    1399             :                 /*
    1400             :                  * Both the left and right records are valid. Choose the closer
    1401             :                  * inode chunk to the target.
    1402             :                  */
    1403     2633683 :                 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
    1404     2633683 :                     (rrec.ir_startino - pagino)) {
    1405     1199867 :                         *rec = rrec;
    1406     1199867 :                         xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
    1407     1199866 :                         *ocur = rcur;
    1408             :                 } else {
    1409     1433816 :                         xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
    1410             :                 }
    1411    54085070 :         } else if (j == 1) {
    1412             :                 /* only the right record is valid */
    1413    52089015 :                 *rec = rrec;
    1414    52089015 :                 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
    1415    52089688 :                 *ocur = rcur;
    1416     1996055 :         } else if (i == 1) {
    1417             :                 /* only the left record is valid */
    1418     1996055 :                 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
    1419             :         }
    1420             : 
    1421             :         return 0;
    1422             : 
    1423           0 : error_rcur:
    1424           0 :         xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
    1425           0 :         return error;
    1426             : }
    1427             : 
    1428             : /*
    1429             :  * Use the free inode btree to find a free inode based on a newino hint. If
    1430             :  * the hint is NULL, find the first free inode in the AG.
    1431             :  */
    1432             : STATIC int
    1433     5401776 : xfs_dialloc_ag_finobt_newino(
    1434             :         struct xfs_agi                  *agi,
    1435             :         struct xfs_btree_cur            *cur,
    1436             :         struct xfs_inobt_rec_incore     *rec)
    1437             : {
    1438     5401776 :         int error;
    1439     5401776 :         int i;
    1440             : 
    1441     5401776 :         if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
    1442     5356951 :                 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
    1443             :                                          XFS_LOOKUP_EQ, &i);
    1444     5356932 :                 if (error)
    1445             :                         return error;
    1446     5356932 :                 if (i == 1) {
    1447     5279224 :                         error = xfs_inobt_get_rec(cur, rec, &i);
    1448     5279233 :                         if (error)
    1449             :                                 return error;
    1450     5279233 :                         if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
    1451           0 :                                 xfs_btree_mark_sick(cur);
    1452           0 :                                 return -EFSCORRUPTED;
    1453             :                         }
    1454             :                         return 0;
    1455             :                 }
    1456             :         }
    1457             : 
    1458             :         /*
    1459             :          * Find the first inode available in the AG.
    1460             :          */
    1461      122533 :         error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
    1462      122546 :         if (error)
    1463             :                 return error;
    1464      122546 :         if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
    1465           0 :                 xfs_btree_mark_sick(cur);
    1466           0 :                 return -EFSCORRUPTED;
    1467             :         }
    1468             : 
    1469      122546 :         error = xfs_inobt_get_rec(cur, rec, &i);
    1470      122547 :         if (error)
    1471             :                 return error;
    1472      122547 :         if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
    1473           0 :                 xfs_btree_mark_sick(cur);
    1474           0 :                 return -EFSCORRUPTED;
    1475             :         }
    1476             : 
    1477             :         return 0;
    1478             : }
    1479             : 
    1480             : /*
    1481             :  * Update the inobt based on a modification made to the finobt. Also ensure that
    1482             :  * the records from both trees are equivalent post-modification.
    1483             :  */
    1484             : STATIC int
    1485    64358707 : xfs_dialloc_ag_update_inobt(
    1486             :         struct xfs_btree_cur            *cur,   /* inobt cursor */
    1487             :         struct xfs_inobt_rec_incore     *frec,  /* finobt record */
    1488             :         int                             offset) /* inode offset */
    1489             : {
    1490    64358707 :         struct xfs_inobt_rec_incore     rec;
    1491    64358707 :         int                             error;
    1492    64358707 :         int                             i;
    1493             : 
    1494    64358707 :         error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
    1495    64358844 :         if (error)
    1496             :                 return error;
    1497    64358679 :         if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
    1498           0 :                 xfs_btree_mark_sick(cur);
    1499           0 :                 return -EFSCORRUPTED;
    1500             :         }
    1501             : 
    1502    64358679 :         error = xfs_inobt_get_rec(cur, &rec, &i);
    1503    64358870 :         if (error)
    1504             :                 return error;
    1505    64358870 :         if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
    1506           0 :                 xfs_btree_mark_sick(cur);
    1507           0 :                 return -EFSCORRUPTED;
    1508             :         }
    1509    64358870 :         ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
    1510             :                                    XFS_INODES_PER_CHUNK) == 0);
    1511             : 
    1512    64358870 :         rec.ir_free &= ~XFS_INOBT_MASK(offset);
    1513    64358870 :         rec.ir_freecount--;
    1514             : 
    1515    64358870 :         if (XFS_IS_CORRUPT(cur->bc_mp,
    1516             :                            rec.ir_free != frec->ir_free ||
    1517             :                            rec.ir_freecount != frec->ir_freecount)) {
    1518           0 :                 xfs_btree_mark_sick(cur);
    1519           0 :                 return -EFSCORRUPTED;
    1520             :         }
    1521             : 
    1522    64358870 :         return xfs_inobt_update(cur, &rec);
    1523             : }
    1524             : 
    1525             : /*
    1526             :  * Allocate an inode using the free inode btree, if available. Otherwise, fall
    1527             :  * back to the inobt search algorithm.
    1528             :  *
    1529             :  * The caller selected an AG for us, and made sure that free inodes are
    1530             :  * available.
    1531             :  */
    1532             : static int
    1533    64363018 : xfs_dialloc_ag(
    1534             :         struct xfs_perag        *pag,
    1535             :         struct xfs_trans        *tp,
    1536             :         struct xfs_buf          *agbp,
    1537             :         xfs_ino_t               parent,
    1538             :         xfs_ino_t               *inop)
    1539             : {
    1540    64363018 :         struct xfs_mount                *mp = tp->t_mountp;
    1541    64363018 :         struct xfs_agi                  *agi = agbp->b_addr;
    1542    64363018 :         xfs_agnumber_t                  pagno = XFS_INO_TO_AGNO(mp, parent);
    1543    64363018 :         xfs_agino_t                     pagino = XFS_INO_TO_AGINO(mp, parent);
    1544    64363018 :         struct xfs_btree_cur            *cur;   /* finobt cursor */
    1545    64363018 :         struct xfs_btree_cur            *icur;  /* inobt cursor */
    1546    64363018 :         struct xfs_inobt_rec_incore     rec;
    1547    64363018 :         xfs_ino_t                       ino;
    1548    64363018 :         int                             error;
    1549    64363018 :         int                             offset;
    1550    64363018 :         int                             i;
    1551             : 
    1552    64363018 :         if (!xfs_has_finobt(mp))
    1553        1341 :                 return xfs_dialloc_ag_inobt(pag, tp, agbp, parent, inop);
    1554             : 
    1555             :         /*
    1556             :          * If pagino is 0 (this is the root inode allocation) use newino.
    1557             :          * This must work because we've just allocated some.
    1558             :          */
    1559    64361677 :         if (!pagino)
    1560         300 :                 pagino = be32_to_cpu(agi->agi_newino);
    1561             : 
    1562    64361677 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_FINO);
    1563             : 
    1564    64361466 :         error = xfs_check_agi_freecount(cur);
    1565    64361990 :         if (error)
    1566         301 :                 goto error_cur;
    1567             : 
    1568             :         /*
    1569             :          * The search algorithm depends on whether we're in the same AG as the
    1570             :          * parent. If so, find the closest available inode to the parent. If
    1571             :          * not, consider the agi hint or find the first free inode in the AG.
    1572             :          */
    1573    64361689 :         if (pag->pag_agno == pagno)
    1574    58959904 :                 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
    1575             :         else
    1576     5401785 :                 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
    1577    64360244 :         if (error)
    1578           1 :                 goto error_cur;
    1579             : 
    1580    64360243 :         offset = xfs_inobt_first_free_inode(&rec);
    1581    64360647 :         ASSERT(offset >= 0);
    1582    64360647 :         ASSERT(offset < XFS_INODES_PER_CHUNK);
    1583    64360647 :         ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
    1584             :                                    XFS_INODES_PER_CHUNK) == 0);
    1585    64360647 :         ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset);
    1586             : 
    1587    64360647 :         if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) {
    1588           0 :                 error = xfs_dialloc_check_ino(pag, tp, ino);
    1589           0 :                 if (error)
    1590           0 :                         goto error_cur;
    1591             :         }
    1592             : 
    1593             :         /*
    1594             :          * Modify or remove the finobt record.
    1595             :          */
    1596    64360643 :         rec.ir_free &= ~XFS_INOBT_MASK(offset);
    1597    64360643 :         rec.ir_freecount--;
    1598    64360643 :         if (rec.ir_freecount)
    1599    50323232 :                 error = xfs_inobt_update(cur, &rec);
    1600             :         else
    1601    14037411 :                 error = xfs_btree_delete(cur, &i);
    1602    64357432 :         if (error)
    1603           0 :                 goto error_cur;
    1604             : 
    1605             :         /*
    1606             :          * The finobt has now been updated appropriately. We haven't updated the
    1607             :          * agi and superblock yet, so we can create an inobt cursor and validate
    1608             :          * the original freecount. If all is well, make the equivalent update to
    1609             :          * the inobt using the finobt record and offset information.
    1610             :          */
    1611    64357432 :         icur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
    1612             : 
    1613    64357093 :         error = xfs_check_agi_freecount(icur);
    1614    64361203 :         if (error)
    1615          21 :                 goto error_icur;
    1616             : 
    1617    64361182 :         error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
    1618    64359005 :         if (error)
    1619         165 :                 goto error_icur;
    1620             : 
    1621             :         /*
    1622             :          * Both trees have now been updated. We must update the perag and
    1623             :          * superblock before we can check the freecount for each btree.
    1624             :          */
    1625    64358840 :         be32_add_cpu(&agi->agi_freecount, -1);
    1626    64362194 :         xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
    1627    64361036 :         pag->pagi_freecount--;
    1628             : 
    1629    64361036 :         xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
    1630             : 
    1631    64360558 :         error = xfs_check_agi_freecount(icur);
    1632    64361017 :         if (error)
    1633           0 :                 goto error_icur;
    1634    64361017 :         error = xfs_check_agi_freecount(cur);
    1635    64359684 :         if (error)
    1636           0 :                 goto error_icur;
    1637             : 
    1638    64359684 :         xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
    1639    64362271 :         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
    1640    64362229 :         *inop = ino;
    1641    64362229 :         return 0;
    1642             : 
    1643         186 : error_icur:
    1644         186 :         xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
    1645         488 : error_cur:
    1646         488 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
    1647         488 :         return error;
    1648             : }
    1649             : 
    1650             : static int
    1651      636686 : xfs_dialloc_roll(
    1652             :         struct xfs_trans        **tpp,
    1653             :         struct xfs_buf          *agibp)
    1654             : {
    1655      636686 :         struct xfs_trans        *tp = *tpp;
    1656      636686 :         struct xfs_dquot_acct   *dqinfo;
    1657      636686 :         int                     error;
    1658             : 
    1659             :         /*
    1660             :          * Hold to on to the agibp across the commit so no other allocation can
    1661             :          * come in and take the free inodes we just allocated for our caller.
    1662             :          */
    1663      636686 :         xfs_trans_bhold(tp, agibp);
    1664             : 
    1665             :         /*
    1666             :          * We want the quota changes to be associated with the next transaction,
    1667             :          * NOT this one. So, detach the dqinfo from this and attach it to the
    1668             :          * next transaction.
    1669             :          */
    1670      636686 :         dqinfo = tp->t_dqinfo;
    1671      636686 :         tp->t_dqinfo = NULL;
    1672             : 
    1673      636686 :         error = xfs_trans_roll(&tp);
    1674             : 
    1675             :         /* Re-attach the quota info that we detached from prev trx. */
    1676      636688 :         tp->t_dqinfo = dqinfo;
    1677             : 
    1678             :         /*
    1679             :          * Join the buffer even on commit error so that the buffer is released
    1680             :          * when the caller cancels the transaction and doesn't have to handle
    1681             :          * this error case specially.
    1682             :          */
    1683      636688 :         xfs_trans_bjoin(tp, agibp);
    1684      636685 :         *tpp = tp;
    1685      636685 :         return error;
    1686             : }
    1687             : 
    1688             : static bool
    1689    65943981 : xfs_dialloc_good_ag(
    1690             :         struct xfs_perag        *pag,
    1691             :         struct xfs_trans        *tp,
    1692             :         umode_t                 mode,
    1693             :         int                     flags,
    1694             :         bool                    ok_alloc)
    1695             : {
    1696    65943981 :         struct xfs_mount        *mp = tp->t_mountp;
    1697    65943981 :         xfs_extlen_t            ineed;
    1698    65943981 :         xfs_extlen_t            longest = 0;
    1699    65943981 :         int                     needspace;
    1700    65943981 :         int                     error;
    1701             : 
    1702    65943981 :         if (!pag)
    1703             :                 return false;
    1704   131887962 :         if (!xfs_perag_allows_inodes(pag))
    1705             :                 return false;
    1706   131887962 :         if (xfs_perag_prohibits_alloc(pag))
    1707             :                 return false;
    1708             : 
    1709   131887962 :         if (!xfs_perag_initialised_agi(pag)) {
    1710         426 :                 error = xfs_ialloc_read_agi(pag, tp, NULL);
    1711         426 :                 if (error)
    1712             :                         return false;
    1713             :         }
    1714             : 
    1715    65943981 :         if (pag->pagi_freecount)
    1716             :                 return true;
    1717     2224313 :         if (!ok_alloc)
    1718             :                 return false;
    1719             : 
    1720     1928786 :         if (!xfs_perag_initialised_agf(pag)) {
    1721          28 :                 error = xfs_alloc_read_agf(pag, tp, flags, NULL);
    1722          28 :                 if (error)
    1723             :                         return false;
    1724             :         }
    1725             : 
    1726             :         /*
    1727             :          * Check that there is enough free space for the file plus a chunk of
    1728             :          * inodes if we need to allocate some. If this is the first pass across
    1729             :          * the AGs, take into account the potential space needed for alignment
    1730             :          * of inode chunks when checking the longest contiguous free space in
    1731             :          * the AG - this prevents us from getting ENOSPC because we have free
    1732             :          * space larger than ialloc_blks but alignment constraints prevent us
    1733             :          * from using it.
    1734             :          *
    1735             :          * If we can't find an AG with space for full alignment slack to be
    1736             :          * taken into account, we must be near ENOSPC in all AGs.  Hence we
    1737             :          * don't include alignment for the second pass and so if we fail
    1738             :          * allocation due to alignment issues then it is most likely a real
    1739             :          * ENOSPC condition.
    1740             :          *
    1741             :          * XXX(dgc): this calculation is now bogus thanks to the per-ag
    1742             :          * reservations that xfs_alloc_fix_freelist() now does via
    1743             :          * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will
    1744             :          * be more than large enough for the check below to succeed, but
    1745             :          * xfs_alloc_space_available() will fail because of the non-zero
    1746             :          * metadata reservation and hence we won't actually be able to allocate
    1747             :          * more inodes in this AG. We do soooo much unnecessary work near ENOSPC
    1748             :          * because of this.
    1749             :          */
    1750      964371 :         ineed = M_IGEO(mp)->ialloc_min_blks;
    1751      964371 :         if (flags && ineed > 1)
    1752      956164 :                 ineed += M_IGEO(mp)->cluster_align;
    1753      964371 :         longest = pag->pagf_longest;
    1754      964371 :         if (!longest)
    1755          10 :                 longest = pag->pagf_flcount > 0;
    1756      964371 :         needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
    1757             : 
    1758      964371 :         if (pag->pagf_freeblks < needspace + ineed || longest < ineed)
    1759       34618 :                 return false;
    1760             :         return true;
    1761             : }
    1762             : 
    1763             : static int
    1764    64649314 : xfs_dialloc_try_ag(
    1765             :         struct xfs_perag        *pag,
    1766             :         struct xfs_trans        **tpp,
    1767             :         xfs_ino_t               parent,
    1768             :         xfs_ino_t               *new_ino,
    1769             :         bool                    ok_alloc)
    1770             : {
    1771    64649314 :         struct xfs_buf          *agbp;
    1772    64649314 :         xfs_ino_t               ino;
    1773    64649314 :         int                     error;
    1774             : 
    1775             :         /*
    1776             :          * Then read in the AGI buffer and recheck with the AGI buffer
    1777             :          * lock held.
    1778             :          */
    1779    64649314 :         error = xfs_ialloc_read_agi(pag, *tpp, &agbp);
    1780    64648572 :         if (error)
    1781             :                 return error;
    1782             : 
    1783    64648513 :         if (!pag->pagi_freecount) {
    1784      921853 :                 if (!ok_alloc) {
    1785         316 :                         error = -EAGAIN;
    1786         316 :                         goto out_release;
    1787             :                 }
    1788             : 
    1789      921537 :                 error = xfs_ialloc_ag_alloc(pag, *tpp, agbp);
    1790      921538 :                 if (error < 0)
    1791      284849 :                         goto out_release;
    1792             : 
    1793             :                 /*
    1794             :                  * We successfully allocated space for an inode cluster in this
    1795             :                  * AG.  Roll the transaction so that we can allocate one of the
    1796             :                  * new inodes.
    1797             :                  */
    1798      636689 :                 ASSERT(pag->pagi_freecount > 0);
    1799      636689 :                 error = xfs_dialloc_roll(tpp, agbp);
    1800      636687 :                 if (error)
    1801           0 :                         goto out_release;
    1802             :         }
    1803             : 
    1804             :         /* Allocate an inode in the found AG */
    1805    64363347 :         error = xfs_dialloc_ag(pag, *tpp, agbp, parent, &ino);
    1806    64362828 :         if (!error)
    1807    64364032 :                 *new_ino = ino;
    1808             :         return error;
    1809             : 
    1810      285165 : out_release:
    1811      285165 :         xfs_trans_brelse(*tpp, agbp);
    1812      285165 :         return error;
    1813             : }
    1814             : 
    1815             : /*
    1816             :  * Pick an AG for the new inode.
    1817             :  *
    1818             :  * Directories, symlinks, and regular files frequently allocate at least one
    1819             :  * block, so factor that potential expansion when we examine whether an AG has
    1820             :  * enough space for file creation.  Try to keep metadata files all in the same
    1821             :  * AG.
    1822             :  */
    1823             : static inline xfs_agnumber_t
    1824    64510866 : xfs_dialloc_pick_ag(
    1825             :         struct xfs_mount        *mp,
    1826             :         struct xfs_inode        *dp,
    1827             :         umode_t                 mode)
    1828             : {
    1829    64510866 :         xfs_agnumber_t          start_agno;
    1830             : 
    1831    64510866 :         if (!dp)
    1832             :                 return 0;
    1833    64510544 :         if (xfs_is_metadir_inode(dp))
    1834             :                 return 0;
    1835             : 
    1836    64499474 :         if (S_ISDIR(mode))
    1837    13706861 :                 return (atomic_inc_return(&mp->m_agirotor) - 1) % mp->m_maxagi;
    1838             : 
    1839    57646048 :         start_agno = XFS_INO_TO_AGNO(mp, dp->i_ino);
    1840    57646048 :         if (start_agno >= mp->m_maxagi)
    1841           0 :                 start_agno = 0;
    1842             : 
    1843             :         return start_agno;
    1844             : }
    1845             : 
    1846             : /*
    1847             :  * Allocate an on-disk inode.
    1848             :  *
    1849             :  * Mode is used to tell whether the new inode is a directory and hence where to
    1850             :  * locate it. The on-disk inode that is allocated will be returned in @new_ino
    1851             :  * on success, otherwise an error will be set to indicate the failure (e.g.
    1852             :  * -ENOSPC).
    1853             :  */
    1854             : int
    1855    64509581 : xfs_dialloc(
    1856             :         struct xfs_trans        **tpp,
    1857             :         struct xfs_inode        *dp,
    1858             :         umode_t                 mode,
    1859             :         xfs_ino_t               *new_ino)
    1860             : {
    1861    64509581 :         struct xfs_mount        *mp = (*tpp)->t_mountp;
    1862    64509581 :         struct xfs_perag        *pag;
    1863    64509581 :         struct xfs_ino_geometry *igeo = M_IGEO(mp);
    1864    64509581 :         xfs_ino_t               ino = NULLFSINO;
    1865    64509581 :         xfs_ino_t               parent = dp ? dp->i_ino : 0;
    1866    64509581 :         xfs_agnumber_t          agno;
    1867    64509581 :         xfs_agnumber_t          start_agno;
    1868    64509581 :         bool                    ok_alloc = true;
    1869    64509581 :         bool                    low_space = false;
    1870    64509581 :         int                     flags;
    1871    64509581 :         int                     error = 0;
    1872             : 
    1873    64509581 :         start_agno = xfs_dialloc_pick_ag(mp, dp, mode);
    1874             : 
    1875             :         /*
    1876             :          * If we have already hit the ceiling of inode blocks then clear
    1877             :          * ok_alloc so we scan all available agi structures for a free
    1878             :          * inode.
    1879             :          *
    1880             :          * Read rough value of mp->m_icount by percpu_counter_read_positive,
    1881             :          * which will sacrifice the preciseness but improve the performance.
    1882             :          */
    1883    64511076 :         if (igeo->maxicount &&
    1884    64511481 :             percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos
    1885             :                                                         > igeo->maxicount) {
    1886      146598 :                 ok_alloc = false;
    1887             :         }
    1888             : 
    1889             :         /*
    1890             :          * If we are near to ENOSPC, we want to prefer allocation from AGs that
    1891             :          * have free inodes in them rather than use up free space allocating new
    1892             :          * inode chunks. Hence we turn off allocation for the first non-blocking
    1893             :          * pass through the AGs if we are near ENOSPC to consume free inodes
    1894             :          * that we can immediately allocate, but then we allow allocation on the
    1895             :          * second pass if we fail to find an AG with free inodes in it.
    1896             :          */
    1897    64511076 :         if (percpu_counter_read_positive(&mp->m_fdblocks) <
    1898    64511076 :                         mp->m_low_space[XFS_LOWSP_1_PCNT]) {
    1899      245337 :                 ok_alloc = false;
    1900      245337 :                 low_space = true;
    1901             :         }
    1902             : 
    1903             :         /*
    1904             :          * Loop until we find an allocation group that either has free inodes
    1905             :          * or in which we can allocate some inodes.  Iterate through the
    1906             :          * allocation groups upward, wrapping at the end.
    1907             :          */
    1908    64511076 :         flags = XFS_ALLOC_FLAG_TRYLOCK;
    1909    64662088 : retry:
    1910    66241771 :         for_each_perag_wrap_at(mp, start_agno, mp->m_maxagi, agno, pag) {
    1911    65944484 :                 if (xfs_dialloc_good_ag(pag, *tpp, mode, flags, ok_alloc)) {
    1912    64649308 :                         error = xfs_dialloc_try_ag(pag, tpp, parent,
    1913             :                                         &ino, ok_alloc);
    1914    64649294 :                         if (error != -EAGAIN)
    1915             :                                 break;
    1916             :                         error = 0;
    1917             :                 }
    1918             : 
    1919     3159366 :                 if (xfs_is_shutdown(mp)) {
    1920             :                         error = -EFSCORRUPTED;
    1921             :                         break;
    1922             :                 }
    1923             :         }
    1924    64661331 :         if (pag)
    1925    64364317 :                 xfs_perag_rele(pag);
    1926    64660075 :         if (error)
    1927         582 :                 return error;
    1928    64659493 :         if (ino == NULLFSINO) {
    1929      295432 :                 if (flags) {
    1930      151012 :                         flags = 0;
    1931      151012 :                         if (low_space)
    1932        4222 :                                 ok_alloc = true;
    1933      151012 :                         goto retry;
    1934             :                 }
    1935             :                 return -ENOSPC;
    1936             :         }
    1937             : 
    1938             :         /*
    1939             :          * Protect against obviously corrupt allocation btree records. Later
    1940             :          * xfs_iget checks will catch re-allocation of other active in-memory
    1941             :          * and on-disk inodes. If we don't catch reallocating the parent inode
    1942             :          * here we will deadlock in xfs_iget() so we have to do these checks
    1943             :          * first.
    1944             :          */
    1945    64364061 :         if (ino == parent || !xfs_verify_dir_ino(mp, ino)) {
    1946           0 :                 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
    1947           0 :                 xfs_agno_mark_sick(mp, XFS_INO_TO_AGNO(mp, ino),
    1948             :                                 XFS_SICK_AG_INOBT);
    1949           0 :                 return -EFSCORRUPTED;
    1950             :         }
    1951             : 
    1952    64362218 :         *new_ino = ino;
    1953    64362218 :         return 0;
    1954             : }
    1955             : 
    1956             : /*
    1957             :  * Free the blocks of an inode chunk. We must consider that the inode chunk
    1958             :  * might be sparse and only free the regions that are allocated as part of the
    1959             :  * chunk.
    1960             :  */
    1961             : static int
    1962       72601 : xfs_difree_inode_chunk(
    1963             :         struct xfs_trans                *tp,
    1964             :         xfs_agnumber_t                  agno,
    1965             :         struct xfs_inobt_rec_incore     *rec)
    1966             : {
    1967       72601 :         struct xfs_mount                *mp = tp->t_mountp;
    1968       72601 :         xfs_agblock_t                   sagbno = XFS_AGINO_TO_AGBNO(mp,
    1969             :                                                         rec->ir_startino);
    1970       72601 :         int                             startidx, endidx;
    1971       72601 :         int                             nextbit;
    1972       72601 :         xfs_agblock_t                   agbno;
    1973       72601 :         int                             contigblk;
    1974       72601 :         DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
    1975             : 
    1976       72601 :         if (!xfs_inobt_issparse(rec->ir_holemask)) {
    1977             :                 /* not sparse, calculate extent info directly */
    1978       56001 :                 return xfs_free_extent_later(tp,
    1979       56001 :                                 XFS_AGB_TO_FSB(mp, agno, sagbno),
    1980       56001 :                                 M_IGEO(mp)->ialloc_blks, &XFS_RMAP_OINFO_INODES,
    1981             :                                 XFS_AG_RESV_NONE, 0);
    1982             :         }
    1983             : 
    1984             :         /* holemask is only 16-bits (fits in an unsigned long) */
    1985       16600 :         ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
    1986       16600 :         holemask[0] = rec->ir_holemask;
    1987             : 
    1988             :         /*
    1989             :          * Find contiguous ranges of zeroes (i.e., allocated regions) in the
    1990             :          * holemask and convert the start/end index of each range to an extent.
    1991             :          * We start with the start and end index both pointing at the first 0 in
    1992             :          * the mask.
    1993             :          */
    1994       16600 :         startidx = endidx = find_first_zero_bit(holemask,
    1995             :                                                 XFS_INOBT_HOLEMASK_BITS);
    1996       16600 :         nextbit = startidx + 1;
    1997      149383 :         while (startidx < XFS_INOBT_HOLEMASK_BITS) {
    1998      132781 :                 int error;
    1999             : 
    2000      132781 :                 nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
    2001             :                                              nextbit);
    2002             :                 /*
    2003             :                  * If the next zero bit is contiguous, update the end index of
    2004             :                  * the current range and continue.
    2005             :                  */
    2006      132783 :                 if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
    2007      116185 :                     nextbit == endidx + 1) {
    2008      116181 :                         endidx = nextbit;
    2009      116181 :                         goto next;
    2010             :                 }
    2011             : 
    2012             :                 /*
    2013             :                  * nextbit is not contiguous with the current end index. Convert
    2014             :                  * the current start/end to an extent and add it to the free
    2015             :                  * list.
    2016             :                  */
    2017       16602 :                 agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
    2018       16602 :                                   mp->m_sb.sb_inopblock;
    2019       16602 :                 contigblk = ((endidx - startidx + 1) *
    2020       16602 :                              XFS_INODES_PER_HOLEMASK_BIT) /
    2021             :                             mp->m_sb.sb_inopblock;
    2022             : 
    2023       16602 :                 ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
    2024       16602 :                 ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
    2025       16602 :                 error = xfs_free_extent_later(tp,
    2026       16602 :                                 XFS_AGB_TO_FSB(mp, agno, agbno), contigblk,
    2027             :                                 &XFS_RMAP_OINFO_INODES, XFS_AG_RESV_NONE, 0);
    2028       16602 :                 if (error)
    2029           0 :                         return error;
    2030             : 
    2031             :                 /* reset range to current bit and carry on... */
    2032             :                 startidx = endidx = nextbit;
    2033             : 
    2034      132783 : next:
    2035      132783 :                 nextbit++;
    2036             :         }
    2037             :         return 0;
    2038             : }
    2039             : 
    2040             : STATIC int
    2041    38023495 : xfs_difree_inobt(
    2042             :         struct xfs_perag                *pag,
    2043             :         struct xfs_trans                *tp,
    2044             :         struct xfs_buf                  *agbp,
    2045             :         xfs_agino_t                     agino,
    2046             :         struct xfs_icluster             *xic,
    2047             :         struct xfs_inobt_rec_incore     *orec)
    2048             : {
    2049    38023495 :         struct xfs_mount                *mp = pag->pag_mount;
    2050    38023495 :         struct xfs_agi                  *agi = agbp->b_addr;
    2051    38023495 :         struct xfs_btree_cur            *cur;
    2052    38023495 :         struct xfs_inobt_rec_incore     rec;
    2053    38023495 :         int                             ilen;
    2054    38023495 :         int                             error;
    2055    38023495 :         int                             i;
    2056    38023495 :         int                             off;
    2057             : 
    2058    38023495 :         ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
    2059    76046990 :         ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
    2060             : 
    2061             :         /*
    2062             :          * Initialize the cursor.
    2063             :          */
    2064    38023495 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
    2065             : 
    2066    38022934 :         error = xfs_check_agi_freecount(cur);
    2067    38021927 :         if (error)
    2068           5 :                 goto error0;
    2069             : 
    2070             :         /*
    2071             :          * Look for the entry describing this inode.
    2072             :          */
    2073    38021922 :         if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
    2074           1 :                 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
    2075             :                         __func__, error);
    2076           1 :                 goto error0;
    2077             :         }
    2078    38025239 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2079           0 :                 xfs_btree_mark_sick(cur);
    2080           0 :                 error = -EFSCORRUPTED;
    2081           0 :                 goto error0;
    2082             :         }
    2083    38025239 :         error = xfs_inobt_get_rec(cur, &rec, &i);
    2084    38025292 :         if (error) {
    2085           0 :                 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
    2086             :                         __func__, error);
    2087           0 :                 goto error0;
    2088             :         }
    2089    38025292 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2090           0 :                 xfs_btree_mark_sick(cur);
    2091           0 :                 error = -EFSCORRUPTED;
    2092           0 :                 goto error0;
    2093             :         }
    2094             :         /*
    2095             :          * Get the offset in the inode chunk.
    2096             :          */
    2097    38025292 :         off = agino - rec.ir_startino;
    2098    38025292 :         ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
    2099    38025292 :         ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
    2100             :         /*
    2101             :          * Mark the inode free & increment the count.
    2102             :          */
    2103    38025292 :         rec.ir_free |= XFS_INOBT_MASK(off);
    2104    38025292 :         rec.ir_freecount++;
    2105             : 
    2106             :         /*
    2107             :          * When an inode chunk is free, it becomes eligible for removal. Don't
    2108             :          * remove the chunk if the block size is large enough for multiple inode
    2109             :          * chunks (that might not be free).
    2110             :          */
    2111    38025292 :         if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
    2112       72609 :             mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
    2113       72604 :                 xic->deleted = true;
    2114       72604 :                 xic->first_ino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
    2115             :                                 rec.ir_startino);
    2116       72604 :                 xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
    2117             : 
    2118             :                 /*
    2119             :                  * Remove the inode cluster from the AGI B+Tree, adjust the
    2120             :                  * AGI and Superblock inode counts, and mark the disk space
    2121             :                  * to be freed when the transaction is committed.
    2122             :                  */
    2123       72603 :                 ilen = rec.ir_freecount;
    2124       72603 :                 be32_add_cpu(&agi->agi_count, -ilen);
    2125       72601 :                 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
    2126       72604 :                 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
    2127       72603 :                 pag->pagi_freecount -= ilen - 1;
    2128       72603 :                 pag->pagi_count -= ilen;
    2129       72603 :                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
    2130       72600 :                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
    2131             : 
    2132       72601 :                 if ((error = xfs_btree_delete(cur, &i))) {
    2133           0 :                         xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
    2134             :                                 __func__, error);
    2135           0 :                         goto error0;
    2136             :                 }
    2137             : 
    2138       72596 :                 error = xfs_difree_inode_chunk(tp, pag->pag_agno, &rec);
    2139       72600 :                 if (error)
    2140           0 :                         goto error0;
    2141             :         } else {
    2142    37952688 :                 xic->deleted = false;
    2143             : 
    2144    37952688 :                 error = xfs_inobt_update(cur, &rec);
    2145    37953285 :                 if (error) {
    2146           0 :                         xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
    2147             :                                 __func__, error);
    2148           0 :                         goto error0;
    2149             :                 }
    2150             : 
    2151             :                 /*
    2152             :                  * Change the inode free counts and log the ag/sb changes.
    2153             :                  */
    2154    37953285 :                 be32_add_cpu(&agi->agi_freecount, 1);
    2155    37952865 :                 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
    2156    37947997 :                 pag->pagi_freecount++;
    2157    37947997 :                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
    2158             :         }
    2159             : 
    2160    38020458 :         error = xfs_check_agi_freecount(cur);
    2161    38020613 :         if (error)
    2162           0 :                 goto error0;
    2163             : 
    2164    38020613 :         *orec = rec;
    2165    38020613 :         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
    2166    38020613 :         return 0;
    2167             : 
    2168           6 : error0:
    2169           6 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
    2170           6 :         return error;
    2171             : }
    2172             : 
    2173             : /*
    2174             :  * Free an inode in the free inode btree.
    2175             :  */
    2176             : STATIC int
    2177    38024497 : xfs_difree_finobt(
    2178             :         struct xfs_perag                *pag,
    2179             :         struct xfs_trans                *tp,
    2180             :         struct xfs_buf                  *agbp,
    2181             :         xfs_agino_t                     agino,
    2182             :         struct xfs_inobt_rec_incore     *ibtrec) /* inobt record */
    2183             : {
    2184    38024497 :         struct xfs_mount                *mp = pag->pag_mount;
    2185    38024497 :         struct xfs_btree_cur            *cur;
    2186    38024497 :         struct xfs_inobt_rec_incore     rec;
    2187    38024497 :         int                             offset = agino - ibtrec->ir_startino;
    2188    38024497 :         int                             error;
    2189    38024497 :         int                             i;
    2190             : 
    2191    38024497 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_FINO);
    2192             : 
    2193    38022956 :         error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
    2194    38025898 :         if (error)
    2195           6 :                 goto error;
    2196    38025892 :         if (i == 0) {
    2197             :                 /*
    2198             :                  * If the record does not exist in the finobt, we must have just
    2199             :                  * freed an inode in a previously fully allocated chunk. If not,
    2200             :                  * something is out of sync.
    2201             :                  */
    2202    13480200 :                 if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) {
    2203           0 :                         xfs_btree_mark_sick(cur);
    2204           0 :                         error = -EFSCORRUPTED;
    2205           0 :                         goto error;
    2206             :                 }
    2207             : 
    2208    13480200 :                 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
    2209             :                                              ibtrec->ir_count,
    2210             :                                              ibtrec->ir_freecount,
    2211             :                                              ibtrec->ir_free, &i);
    2212    13477710 :                 if (error)
    2213           0 :                         goto error;
    2214    13477710 :                 ASSERT(i == 1);
    2215             : 
    2216    13477710 :                 goto out;
    2217             :         }
    2218             : 
    2219             :         /*
    2220             :          * Read and update the existing record. We could just copy the ibtrec
    2221             :          * across here, but that would defeat the purpose of having redundant
    2222             :          * metadata. By making the modifications independently, we can catch
    2223             :          * corruptions that we wouldn't see if we just copied from one record
    2224             :          * to another.
    2225             :          */
    2226    24545692 :         error = xfs_inobt_get_rec(cur, &rec, &i);
    2227    24545730 :         if (error)
    2228           0 :                 goto error;
    2229    24545730 :         if (XFS_IS_CORRUPT(mp, i != 1)) {
    2230           0 :                 xfs_btree_mark_sick(cur);
    2231           0 :                 error = -EFSCORRUPTED;
    2232           0 :                 goto error;
    2233             :         }
    2234             : 
    2235    24545730 :         rec.ir_free |= XFS_INOBT_MASK(offset);
    2236    24545730 :         rec.ir_freecount++;
    2237             : 
    2238    24545730 :         if (XFS_IS_CORRUPT(mp,
    2239             :                            rec.ir_free != ibtrec->ir_free ||
    2240             :                            rec.ir_freecount != ibtrec->ir_freecount)) {
    2241           0 :                 xfs_btree_mark_sick(cur);
    2242           0 :                 error = -EFSCORRUPTED;
    2243           0 :                 goto error;
    2244             :         }
    2245             : 
    2246             :         /*
    2247             :          * The content of inobt records should always match between the inobt
    2248             :          * and finobt. The lifecycle of records in the finobt is different from
    2249             :          * the inobt in that the finobt only tracks records with at least one
    2250             :          * free inode. Hence, if all of the inodes are free and we aren't
    2251             :          * keeping inode chunks permanently on disk, remove the record.
    2252             :          * Otherwise, update the record with the new information.
    2253             :          *
    2254             :          * Note that we currently can't free chunks when the block size is large
    2255             :          * enough for multiple chunks. Leave the finobt record to remain in sync
    2256             :          * with the inobt.
    2257             :          */
    2258    24545730 :         if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
    2259       72609 :             mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
    2260       72605 :                 error = xfs_btree_delete(cur, &i);
    2261       72605 :                 if (error)
    2262           0 :                         goto error;
    2263       72605 :                 ASSERT(i == 1);
    2264             :         } else {
    2265    24473125 :                 error = xfs_inobt_update(cur, &rec);
    2266    24473254 :                 if (error)
    2267           0 :                         goto error;
    2268             :         }
    2269             : 
    2270    24473254 : out:
    2271    38023569 :         error = xfs_check_agi_freecount(cur);
    2272    38024314 :         if (error)
    2273           0 :                 goto error;
    2274             : 
    2275    38024314 :         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
    2276    38024314 :         return 0;
    2277             : 
    2278           6 : error:
    2279           6 :         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
    2280           6 :         return error;
    2281             : }
    2282             : 
    2283             : /*
    2284             :  * Free disk inode.  Carefully avoids touching the incore inode, all
    2285             :  * manipulations incore are the caller's responsibility.
    2286             :  * The on-disk inode is not changed by this operation, only the
    2287             :  * btree (free inode mask) is changed.
    2288             :  */
    2289             : int
    2290    38023772 : xfs_difree(
    2291             :         struct xfs_trans        *tp,
    2292             :         struct xfs_perag        *pag,
    2293             :         xfs_ino_t               inode,
    2294             :         struct xfs_icluster     *xic)
    2295             : {
    2296             :         /* REFERENCED */
    2297    38023772 :         xfs_agblock_t           agbno;  /* block number containing inode */
    2298    38023772 :         struct xfs_buf          *agbp;  /* buffer for allocation group header */
    2299    38023772 :         xfs_agino_t             agino;  /* allocation group inode number */
    2300    38023772 :         int                     error;  /* error return value */
    2301    38023772 :         struct xfs_mount        *mp = tp->t_mountp;
    2302    38023772 :         struct xfs_inobt_rec_incore rec;/* btree record */
    2303             : 
    2304             :         /*
    2305             :          * Break up inode number into its components.
    2306             :          */
    2307    38023772 :         if (pag->pag_agno != XFS_INO_TO_AGNO(mp, inode)) {
    2308           0 :                 xfs_warn(mp, "%s: agno != pag->pag_agno (%d != %d).",
    2309             :                         __func__, XFS_INO_TO_AGNO(mp, inode), pag->pag_agno);
    2310           0 :                 ASSERT(0);
    2311           0 :                 return -EINVAL;
    2312             :         }
    2313    38023772 :         agino = XFS_INO_TO_AGINO(mp, inode);
    2314    38023772 :         if (inode != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino))  {
    2315           0 :                 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
    2316             :                         __func__, (unsigned long long)inode,
    2317             :                         (unsigned long long)XFS_AGINO_TO_INO(mp, pag->pag_agno, agino));
    2318           0 :                 ASSERT(0);
    2319           0 :                 return -EINVAL;
    2320             :         }
    2321    38023772 :         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
    2322    38023772 :         if (agbno >= mp->m_sb.sb_agblocks)  {
    2323           0 :                 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
    2324             :                         __func__, agbno, mp->m_sb.sb_agblocks);
    2325           0 :                 ASSERT(0);
    2326           0 :                 return -EINVAL;
    2327             :         }
    2328             :         /*
    2329             :          * Get the allocation group header.
    2330             :          */
    2331    38023772 :         error = xfs_ialloc_read_agi(pag, tp, &agbp);
    2332    38022001 :         if (error) {
    2333         141 :                 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
    2334             :                         __func__, error);
    2335         141 :                 return error;
    2336             :         }
    2337             : 
    2338             :         /*
    2339             :          * Fix up the inode allocation btree.
    2340             :          */
    2341    38021860 :         error = xfs_difree_inobt(pag, tp, agbp, agino, xic, &rec);
    2342    38025281 :         if (error)
    2343           6 :                 goto error0;
    2344             : 
    2345             :         /*
    2346             :          * Fix up the free inode btree.
    2347             :          */
    2348    38025275 :         if (xfs_has_finobt(mp)) {
    2349    38024066 :                 error = xfs_difree_finobt(pag, tp, agbp, agino, &rec);
    2350    38024386 :                 if (error)
    2351           6 :                         goto error0;
    2352             :         }
    2353             : 
    2354             :         return 0;
    2355             : 
    2356             : error0:
    2357             :         return error;
    2358             : }
    2359             : 
    2360             : STATIC int
    2361   470852942 : xfs_imap_lookup(
    2362             :         struct xfs_perag        *pag,
    2363             :         struct xfs_trans        *tp,
    2364             :         xfs_agino_t             agino,
    2365             :         xfs_agblock_t           agbno,
    2366             :         xfs_agblock_t           *chunk_agbno,
    2367             :         xfs_agblock_t           *offset_agbno,
    2368             :         int                     flags)
    2369             : {
    2370   470852942 :         struct xfs_mount        *mp = pag->pag_mount;
    2371   470852942 :         struct xfs_inobt_rec_incore rec;
    2372   470852942 :         struct xfs_btree_cur    *cur;
    2373   470852942 :         struct xfs_buf          *agbp;
    2374   470852942 :         int                     error;
    2375   470852942 :         int                     i;
    2376             : 
    2377   470852942 :         error = xfs_ialloc_read_agi(pag, tp, &agbp);
    2378   470910438 :         if (error) {
    2379         248 :                 xfs_alert(mp,
    2380             :                         "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
    2381             :                         __func__, error, pag->pag_agno);
    2382         248 :                 return error;
    2383             :         }
    2384             : 
    2385             :         /*
    2386             :          * Lookup the inode record for the given agino. If the record cannot be
    2387             :          * found, then it's an invalid inode number and we should abort. Once
    2388             :          * we have a record, we need to ensure it contains the inode number
    2389             :          * we are looking up.
    2390             :          */
    2391   470910190 :         cur = xfs_inobt_init_cursor(pag, tp, agbp, XFS_BTNUM_INO);
    2392   470906301 :         error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
    2393   470910341 :         if (!error) {
    2394   470908138 :                 if (i)
    2395   470904369 :                         error = xfs_inobt_get_rec(cur, &rec, &i);
    2396   470910415 :                 if (!error && i == 0)
    2397        1193 :                         error = -EINVAL;
    2398             :         }
    2399             : 
    2400   470912618 :         xfs_trans_brelse(tp, agbp);
    2401   470908430 :         xfs_btree_del_cursor(cur, error);
    2402   470909113 :         if (error)
    2403             :                 return error;
    2404             : 
    2405             :         /* check that the returned record contains the required inode */
    2406   470907905 :         if (rec.ir_startino > agino ||
    2407   470907905 :             rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino)
    2408             :                 return -EINVAL;
    2409             : 
    2410             :         /* for untrusted inodes check it is allocated first */
    2411   470855795 :         if ((flags & XFS_IGET_UNTRUSTED) &&
    2412   470857007 :             (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
    2413             :                 return -EINVAL;
    2414             : 
    2415   470853845 :         *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
    2416   470853845 :         *offset_agbno = agbno - *chunk_agbno;
    2417   470853845 :         return 0;
    2418             : }
    2419             : 
    2420             : /*
    2421             :  * Return the location of the inode in imap, for mapping it into a buffer.
    2422             :  */
    2423             : int
    2424   509870549 : xfs_imap(
    2425             :         struct xfs_perag        *pag,
    2426             :         struct xfs_trans        *tp,
    2427             :         xfs_ino_t               ino,    /* inode to locate */
    2428             :         struct xfs_imap         *imap,  /* location map structure */
    2429             :         uint                    flags)  /* flags for inode btree lookup */
    2430             : {
    2431   509870549 :         struct xfs_mount        *mp = pag->pag_mount;
    2432   509870549 :         xfs_agblock_t           agbno;  /* block number of inode in the alloc group */
    2433   509870549 :         xfs_agino_t             agino;  /* inode number within alloc group */
    2434   509870549 :         xfs_agblock_t           chunk_agbno;    /* first block in inode chunk */
    2435   509870549 :         xfs_agblock_t           cluster_agbno;  /* first block in inode cluster */
    2436   509870549 :         int                     error;  /* error code */
    2437   509870549 :         int                     offset; /* index of inode in its buffer */
    2438   509870549 :         xfs_agblock_t           offset_agbno;   /* blks from chunk start to inode */
    2439             : 
    2440   509870549 :         ASSERT(ino != NULLFSINO);
    2441             : 
    2442             :         /*
    2443             :          * Split up the inode number into its parts.
    2444             :          */
    2445   509870549 :         agino = XFS_INO_TO_AGINO(mp, ino);
    2446   509870549 :         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
    2447   509870549 :         if (agbno >= mp->m_sb.sb_agblocks ||
    2448   509846356 :             ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
    2449       21225 :                 error = -EINVAL;
    2450             : #ifdef DEBUG
    2451             :                 /*
    2452             :                  * Don't output diagnostic information for untrusted inodes
    2453             :                  * as they can be invalid without implying corruption.
    2454             :                  */
    2455       21225 :                 if (flags & XFS_IGET_UNTRUSTED)
    2456             :                         return error;
    2457           0 :                 if (agbno >= mp->m_sb.sb_agblocks) {
    2458           0 :                         xfs_alert(mp,
    2459             :                 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
    2460             :                                 __func__, (unsigned long long)agbno,
    2461             :                                 (unsigned long)mp->m_sb.sb_agblocks);
    2462             :                 }
    2463           0 :                 if (ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) {
    2464           0 :                         xfs_alert(mp,
    2465             :                 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
    2466             :                                 __func__, ino,
    2467             :                                 XFS_AGINO_TO_INO(mp, pag->pag_agno, agino));
    2468             :                 }
    2469           0 :                 xfs_stack_trace();
    2470             : #endif /* DEBUG */
    2471           0 :                 return error;
    2472             :         }
    2473             : 
    2474             :         /*
    2475             :          * For bulkstat and handle lookups, we have an untrusted inode number
    2476             :          * that we have to verify is valid. We cannot do this just by reading
    2477             :          * the inode buffer as it may have been unlinked and removed leaving
    2478             :          * inodes in stale state on disk. Hence we have to do a btree lookup
    2479             :          * in all cases where an untrusted inode number is passed.
    2480             :          */
    2481   509849324 :         if (flags & XFS_IGET_UNTRUSTED) {
    2482   470891893 :                 error = xfs_imap_lookup(pag, tp, agino, agbno,
    2483             :                                         &chunk_agbno, &offset_agbno, flags);
    2484   470897133 :                 if (error)
    2485             :                         return error;
    2486   470854882 :                 goto out_map;
    2487             :         }
    2488             : 
    2489             :         /*
    2490             :          * If the inode cluster size is the same as the blocksize or
    2491             :          * smaller we get to the buffer by simple arithmetics.
    2492             :          */
    2493    38957431 :         if (M_IGEO(mp)->blocks_per_cluster == 1) {
    2494        1650 :                 offset = XFS_INO_TO_OFFSET(mp, ino);
    2495        1650 :                 ASSERT(offset < mp->m_sb.sb_inopblock);
    2496             : 
    2497        1650 :                 imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, agbno);
    2498        1650 :                 imap->im_len = XFS_FSB_TO_BB(mp, 1);
    2499        1650 :                 imap->im_boffset = (unsigned short)(offset <<
    2500        1650 :                                                         mp->m_sb.sb_inodelog);
    2501        1650 :                 return 0;
    2502             :         }
    2503             : 
    2504             :         /*
    2505             :          * If the inode chunks are aligned then use simple maths to
    2506             :          * find the location. Otherwise we have to do a btree
    2507             :          * lookup to find the location.
    2508             :          */
    2509    38955781 :         if (M_IGEO(mp)->inoalign_mask) {
    2510    38955781 :                 offset_agbno = agbno & M_IGEO(mp)->inoalign_mask;
    2511    38955781 :                 chunk_agbno = agbno - offset_agbno;
    2512             :         } else {
    2513           0 :                 error = xfs_imap_lookup(pag, tp, agino, agbno,
    2514             :                                         &chunk_agbno, &offset_agbno, flags);
    2515           0 :                 if (error)
    2516             :                         return error;
    2517             :         }
    2518             : 
    2519           0 : out_map:
    2520   509810663 :         ASSERT(agbno >= chunk_agbno);
    2521   509810663 :         cluster_agbno = chunk_agbno +
    2522   509810663 :                 ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) *
    2523             :                  M_IGEO(mp)->blocks_per_cluster);
    2524   509810663 :         offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
    2525   509810663 :                 XFS_INO_TO_OFFSET(mp, ino);
    2526             : 
    2527   509810663 :         imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, cluster_agbno);
    2528   509810663 :         imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
    2529   509810663 :         imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
    2530             : 
    2531             :         /*
    2532             :          * If the inode number maps to a block outside the bounds
    2533             :          * of the file system then return NULL rather than calling
    2534             :          * read_buf and panicing when we get an error from the
    2535             :          * driver.
    2536             :          */
    2537   509810663 :         if ((imap->im_blkno + imap->im_len) >
    2538   509810663 :             XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
    2539           0 :                 xfs_alert(mp,
    2540             :         "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
    2541             :                         __func__, (unsigned long long) imap->im_blkno,
    2542             :                         (unsigned long long) imap->im_len,
    2543             :                         XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
    2544           0 :                 return -EINVAL;
    2545             :         }
    2546             :         return 0;
    2547             : }
    2548             : 
    2549             : /*
    2550             :  * Log specified fields for the ag hdr (inode section). The growth of the agi
    2551             :  * structure over time requires that we interpret the buffer as two logical
    2552             :  * regions delineated by the end of the unlinked list. This is due to the size
    2553             :  * of the hash table and its location in the middle of the agi.
    2554             :  *
    2555             :  * For example, a request to log a field before agi_unlinked and a field after
    2556             :  * agi_unlinked could cause us to log the entire hash table and use an excessive
    2557             :  * amount of log space. To avoid this behavior, log the region up through
    2558             :  * agi_unlinked in one call and the region after agi_unlinked through the end of
    2559             :  * the structure in another.
    2560             :  */
    2561             : void
    2562   103916348 : xfs_ialloc_log_agi(
    2563             :         struct xfs_trans        *tp,
    2564             :         struct xfs_buf          *bp,
    2565             :         uint32_t                fields)
    2566             : {
    2567   103916348 :         int                     first;          /* first byte number */
    2568   103916348 :         int                     last;           /* last byte number */
    2569   103916348 :         static const short      offsets[] = {   /* field starting offsets */
    2570             :                                         /* keep in sync with bit definitions */
    2571             :                 offsetof(xfs_agi_t, agi_magicnum),
    2572             :                 offsetof(xfs_agi_t, agi_versionnum),
    2573             :                 offsetof(xfs_agi_t, agi_seqno),
    2574             :                 offsetof(xfs_agi_t, agi_length),
    2575             :                 offsetof(xfs_agi_t, agi_count),
    2576             :                 offsetof(xfs_agi_t, agi_root),
    2577             :                 offsetof(xfs_agi_t, agi_level),
    2578             :                 offsetof(xfs_agi_t, agi_freecount),
    2579             :                 offsetof(xfs_agi_t, agi_newino),
    2580             :                 offsetof(xfs_agi_t, agi_dirino),
    2581             :                 offsetof(xfs_agi_t, agi_unlinked),
    2582             :                 offsetof(xfs_agi_t, agi_free_root),
    2583             :                 offsetof(xfs_agi_t, agi_free_level),
    2584             :                 offsetof(xfs_agi_t, agi_iblocks),
    2585             :                 sizeof(xfs_agi_t)
    2586             :         };
    2587             : #ifdef DEBUG
    2588   103916348 :         struct xfs_agi          *agi = bp->b_addr;
    2589             : 
    2590   103916348 :         ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
    2591             : #endif
    2592             : 
    2593             :         /*
    2594             :          * Compute byte offsets for the first and last fields in the first
    2595             :          * region and log the agi buffer. This only logs up through
    2596             :          * agi_unlinked.
    2597             :          */
    2598   103916348 :         if (fields & XFS_AGI_ALL_BITS_R1) {
    2599   103857392 :                 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
    2600             :                                   &first, &last);
    2601   103855329 :                 xfs_trans_log_buf(tp, bp, first, last);
    2602             :         }
    2603             : 
    2604             :         /*
    2605             :          * Mask off the bits in the first region and calculate the first and
    2606             :          * last field offsets for any bits in the second region.
    2607             :          */
    2608   103913808 :         fields &= ~XFS_AGI_ALL_BITS_R1;
    2609   103913808 :         if (fields) {
    2610      116268 :                 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
    2611             :                                   &first, &last);
    2612      116271 :                 xfs_trans_log_buf(tp, bp, first, last);
    2613             :         }
    2614   103913810 : }
    2615             : 
    2616             : static xfs_failaddr_t
    2617     1233845 : xfs_agi_verify(
    2618             :         struct xfs_buf          *bp)
    2619             : {
    2620     1233845 :         struct xfs_mount        *mp = bp->b_mount;
    2621     1233845 :         struct xfs_agi          *agi = bp->b_addr;
    2622     1233845 :         xfs_failaddr_t          fa;
    2623     1233845 :         uint32_t                agi_seqno = be32_to_cpu(agi->agi_seqno);
    2624     1233845 :         uint32_t                agi_length = be32_to_cpu(agi->agi_length);
    2625     1233845 :         int                     i;
    2626             : 
    2627     1233845 :         if (xfs_has_crc(mp)) {
    2628     1231429 :                 if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
    2629           0 :                         return __this_address;
    2630     1231430 :                 if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn)))
    2631           0 :                         return __this_address;
    2632             :         }
    2633             : 
    2634             :         /*
    2635             :          * Validate the magic number of the agi block.
    2636             :          */
    2637     1233846 :         if (!xfs_verify_magic(bp, agi->agi_magicnum))
    2638           0 :                 return __this_address;
    2639     1233846 :         if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
    2640           0 :                 return __this_address;
    2641             : 
    2642     1233846 :         fa = xfs_validate_ag_length(bp, agi_seqno, agi_length);
    2643     1233844 :         if (fa)
    2644             :                 return fa;
    2645             : 
    2646     1233844 :         if (be32_to_cpu(agi->agi_level) < 1 ||
    2647     2467688 :             be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels)
    2648           2 :                 return __this_address;
    2649             : 
    2650     1233842 :         if (xfs_has_finobt(mp) &&
    2651     2462784 :             (be32_to_cpu(agi->agi_free_level) < 1 ||
    2652     1231392 :              be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels))
    2653           0 :                 return __this_address;
    2654             : 
    2655    80199721 :         for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
    2656    78965876 :                 if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
    2657    78677122 :                         continue;
    2658      288754 :                 if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
    2659           0 :                         return __this_address;
    2660             :         }
    2661             : 
    2662             :         return NULL;
    2663             : }
    2664             : 
    2665             : static void
    2666      226404 : xfs_agi_read_verify(
    2667             :         struct xfs_buf  *bp)
    2668             : {
    2669      226404 :         struct xfs_mount *mp = bp->b_mount;
    2670      226404 :         xfs_failaddr_t  fa;
    2671             : 
    2672      452692 :         if (xfs_has_crc(mp) &&
    2673             :             !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
    2674           2 :                 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
    2675             :         else {
    2676      226402 :                 fa = xfs_agi_verify(bp);
    2677      226402 :                 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
    2678           0 :                         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
    2679             :         }
    2680      226404 : }
    2681             : 
    2682             : static void
    2683      562377 : xfs_agi_write_verify(
    2684             :         struct xfs_buf  *bp)
    2685             : {
    2686      562377 :         struct xfs_mount        *mp = bp->b_mount;
    2687      562377 :         struct xfs_buf_log_item *bip = bp->b_log_item;
    2688      562377 :         struct xfs_agi          *agi = bp->b_addr;
    2689      562377 :         xfs_failaddr_t          fa;
    2690             : 
    2691      562377 :         fa = xfs_agi_verify(bp);
    2692      562377 :         if (fa) {
    2693           0 :                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
    2694           0 :                 return;
    2695             :         }
    2696             : 
    2697      562377 :         if (!xfs_has_crc(mp))
    2698             :                 return;
    2699             : 
    2700      560081 :         if (bip)
    2701      554673 :                 agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
    2702      560081 :         xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
    2703             : }
    2704             : 
    2705             : const struct xfs_buf_ops xfs_agi_buf_ops = {
    2706             :         .name = "xfs_agi",
    2707             :         .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) },
    2708             :         .verify_read = xfs_agi_read_verify,
    2709             :         .verify_write = xfs_agi_write_verify,
    2710             :         .verify_struct = xfs_agi_verify,
    2711             : };
    2712             : 
    2713             : /*
    2714             :  * Read in the allocation group header (inode allocation section)
    2715             :  */
    2716             : int
    2717  1946436824 : xfs_read_agi(
    2718             :         struct xfs_perag        *pag,
    2719             :         struct xfs_trans        *tp,
    2720             :         struct xfs_buf          **agibpp)
    2721             : {
    2722  1946436824 :         struct xfs_mount        *mp = pag->pag_mount;
    2723  1946436824 :         int                     error;
    2724             : 
    2725  1946436824 :         trace_xfs_read_agi(pag->pag_mount, pag->pag_agno);
    2726             : 
    2727  1946485979 :         error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
    2728  1946485979 :                         XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGI_DADDR(mp)),
    2729             :                         XFS_FSS_TO_BB(mp, 1), 0, agibpp, &xfs_agi_buf_ops);
    2730  1946743390 :         if (xfs_metadata_is_sick(error))
    2731           2 :                 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGI);
    2732  1946743390 :         if (error)
    2733             :                 return error;
    2734  1946734718 :         if (tp)
    2735  1946158274 :                 xfs_trans_buf_set_type(tp, *agibpp, XFS_BLFT_AGI_BUF);
    2736             : 
    2737  1946720172 :         xfs_buf_set_ref(*agibpp, XFS_AGI_REF);
    2738  1946720172 :         return 0;
    2739             : }
    2740             : 
    2741             : /*
    2742             :  * Read in the agi and initialise the per-ag data. If the caller supplies a
    2743             :  * @agibpp, return the locked AGI buffer to them, otherwise release it.
    2744             :  */
    2745             : int
    2746  1862978330 : xfs_ialloc_read_agi(
    2747             :         struct xfs_perag        *pag,
    2748             :         struct xfs_trans        *tp,
    2749             :         struct xfs_buf          **agibpp)
    2750             : {
    2751  1862978330 :         struct xfs_buf          *agibp;
    2752  1862978330 :         struct xfs_agi          *agi;
    2753  1862978330 :         int                     error;
    2754             : 
    2755  1862978330 :         trace_xfs_ialloc_read_agi(pag->pag_mount, pag->pag_agno);
    2756             : 
    2757  1863077595 :         error = xfs_read_agi(pag, tp, &agibp);
    2758  1863213538 :         if (error)
    2759             :                 return error;
    2760             : 
    2761  1863204900 :         agi = agibp->b_addr;
    2762  3726409800 :         if (!xfs_perag_initialised_agi(pag)) {
    2763      188489 :                 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
    2764      188489 :                 pag->pagi_count = be32_to_cpu(agi->agi_count);
    2765      188489 :                 set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
    2766             :         }
    2767             : 
    2768             :         /*
    2769             :          * It's possible for these to be out of sync if
    2770             :          * we are in the middle of a forced shutdown.
    2771             :          */
    2772  3726409800 :         ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
    2773             :                 xfs_is_shutdown(pag->pag_mount));
    2774  1863204900 :         if (agibpp)
    2775  1863158952 :                 *agibpp = agibp;
    2776             :         else
    2777       45948 :                 xfs_trans_brelse(tp, agibp);
    2778             :         return 0;
    2779             : }
    2780             : 
    2781             : /* How many inodes are backed by inode clusters ondisk? */
    2782             : STATIC int
    2783  3705050576 : xfs_ialloc_count_ondisk(
    2784             :         struct xfs_btree_cur            *cur,
    2785             :         xfs_agino_t                     low,
    2786             :         xfs_agino_t                     high,
    2787             :         unsigned int                    *allocated)
    2788             : {
    2789  3705050576 :         struct xfs_inobt_rec_incore     irec;
    2790  3705050576 :         unsigned int                    ret = 0;
    2791  3705050576 :         int                             has_record;
    2792  3705050576 :         int                             error;
    2793             : 
    2794  3705050576 :         error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
    2795  3704942619 :         if (error)
    2796             :                 return error;
    2797             : 
    2798  6534189935 :         while (has_record) {
    2799  4582256237 :                 unsigned int            i, hole_idx;
    2800             : 
    2801  4582256237 :                 error = xfs_inobt_get_rec(cur, &irec, &has_record);
    2802  4581449017 :                 if (error)
    2803           0 :                         return error;
    2804  4581449017 :                 if (irec.ir_startino > high)
    2805             :                         break;
    2806             : 
    2807 >18235*10^7 :                 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
    2808 >17956*10^7 :                         if (irec.ir_startino + i < low)
    2809 >17830*10^7 :                                 continue;
    2810  1266703973 :                         if (irec.ir_startino + i > high)
    2811             :                                 break;
    2812             : 
    2813  1221072118 :                         hole_idx = i / XFS_INODES_PER_HOLEMASK_BIT;
    2814  1221072118 :                         if (!(irec.ir_holemask & (1U << hole_idx)))
    2815   794987916 :                                 ret++;
    2816             :                 }
    2817             : 
    2818  2828649235 :                 error = xfs_btree_increment(cur, 0, &has_record);
    2819  2829247316 :                 if (error)
    2820           0 :                         return error;
    2821             :         }
    2822             : 
    2823  3704733480 :         *allocated = ret;
    2824  3704733480 :         return 0;
    2825             : }
    2826             : 
    2827             : /* Is there an inode record covering a given extent? */
    2828             : int
    2829  3705033327 : xfs_ialloc_has_inodes_at_extent(
    2830             :         struct xfs_btree_cur    *cur,
    2831             :         xfs_agblock_t           bno,
    2832             :         xfs_extlen_t            len,
    2833             :         enum xbtree_recpacking  *outcome)
    2834             : {
    2835  3705033327 :         xfs_agino_t             agino;
    2836  3705033327 :         xfs_agino_t             last_agino;
    2837  3705033327 :         unsigned int            allocated;
    2838  3705033327 :         int                     error;
    2839             : 
    2840  3705033327 :         agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
    2841  3705033327 :         last_agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
    2842             : 
    2843  3705033327 :         error = xfs_ialloc_count_ondisk(cur, agino, last_agino, &allocated);
    2844  3704837882 :         if (error)
    2845             :                 return error;
    2846             : 
    2847  3704837882 :         if (allocated == 0)
    2848  3688858233 :                 *outcome = XBTREE_RECPACKING_EMPTY;
    2849    15979649 :         else if (allocated == last_agino - agino + 1)
    2850    15979649 :                 *outcome = XBTREE_RECPACKING_FULL;
    2851             :         else
    2852           0 :                 *outcome = XBTREE_RECPACKING_SPARSE;
    2853             :         return 0;
    2854             : }
    2855             : 
    2856             : struct xfs_ialloc_count_inodes {
    2857             :         xfs_agino_t                     count;
    2858             :         xfs_agino_t                     freecount;
    2859             : };
    2860             : 
    2861             : /* Record inode counts across all inobt records. */
    2862             : STATIC int
    2863   172835915 : xfs_ialloc_count_inodes_rec(
    2864             :         struct xfs_btree_cur            *cur,
    2865             :         const union xfs_btree_rec       *rec,
    2866             :         void                            *priv)
    2867             : {
    2868   172835915 :         struct xfs_inobt_rec_incore     irec;
    2869   172835915 :         struct xfs_ialloc_count_inodes  *ci = priv;
    2870   172835915 :         xfs_failaddr_t                  fa;
    2871             : 
    2872   172835915 :         xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
    2873   172835817 :         fa = xfs_inobt_check_irec(cur, &irec);
    2874   172836471 :         if (fa)
    2875           0 :                 return xfs_inobt_complain_bad_rec(cur, fa, &irec);
    2876             : 
    2877   172836471 :         ci->count += irec.ir_count;
    2878   172836471 :         ci->freecount += irec.ir_freecount;
    2879             : 
    2880   172836471 :         return 0;
    2881             : }
    2882             : 
    2883             : /* Count allocated and free inodes under an inobt. */
    2884             : int
    2885      567243 : xfs_ialloc_count_inodes(
    2886             :         struct xfs_btree_cur            *cur,
    2887             :         xfs_agino_t                     *count,
    2888             :         xfs_agino_t                     *freecount)
    2889             : {
    2890      567243 :         struct xfs_ialloc_count_inodes  ci = {0};
    2891      567243 :         int                             error;
    2892             : 
    2893      567243 :         ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
    2894      567243 :         error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
    2895      567242 :         if (error)
    2896             :                 return error;
    2897             : 
    2898      567242 :         *count = ci.count;
    2899      567242 :         *freecount = ci.freecount;
    2900      567242 :         return 0;
    2901             : }
    2902             : 
    2903             : /*
    2904             :  * Initialize inode-related geometry information.
    2905             :  *
    2906             :  * Compute the inode btree min and max levels and set maxicount.
    2907             :  *
    2908             :  * Set the inode cluster size.  This may still be overridden by the file
    2909             :  * system block size if it is larger than the chosen cluster size.
    2910             :  *
    2911             :  * For v5 filesystems, scale the cluster size with the inode size to keep a
    2912             :  * constant ratio of inode per cluster buffer, but only if mkfs has set the
    2913             :  * inode alignment value appropriately for larger cluster sizes.
    2914             :  *
    2915             :  * Then compute the inode cluster alignment information.
    2916             :  */
    2917             : void
    2918       24337 : xfs_ialloc_setup_geometry(
    2919             :         struct xfs_mount        *mp)
    2920             : {
    2921       24337 :         struct xfs_sb           *sbp = &mp->m_sb;
    2922       24337 :         struct xfs_ino_geometry *igeo = M_IGEO(mp);
    2923       24337 :         uint64_t                icount;
    2924       24337 :         uint                    inodes;
    2925             : 
    2926       24337 :         igeo->new_diflags2 = 0;
    2927       24337 :         if (xfs_has_bigtime(mp))
    2928       24258 :                 igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME;
    2929       24337 :         if (xfs_has_large_extent_counts(mp))
    2930       24291 :                 igeo->new_diflags2 |= XFS_DIFLAG2_NREXT64;
    2931             : 
    2932             :         /* Compute inode btree geometry. */
    2933       24337 :         igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
    2934       24337 :         igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, true);
    2935       24337 :         igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, false);
    2936       24337 :         igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2;
    2937       24337 :         igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2;
    2938             : 
    2939       24337 :         igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
    2940             :                         sbp->sb_inopblock);
    2941       24337 :         igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog;
    2942             : 
    2943       24337 :         if (sbp->sb_spino_align)
    2944       24253 :                 igeo->ialloc_min_blks = sbp->sb_spino_align;
    2945             :         else
    2946          84 :                 igeo->ialloc_min_blks = igeo->ialloc_blks;
    2947             : 
    2948             :         /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */
    2949       24337 :         inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
    2950       24337 :         igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
    2951             :                         inodes);
    2952       24337 :         ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk());
    2953             : 
    2954             :         /*
    2955             :          * Set the maximum inode count for this filesystem, being careful not
    2956             :          * to use obviously garbage sb_inopblog/sb_inopblock values.  Regular
    2957             :          * users should never get here due to failing sb verification, but
    2958             :          * certain users (xfs_db) need to be usable even with corrupt metadata.
    2959             :          */
    2960       24337 :         if (sbp->sb_imax_pct && igeo->ialloc_blks) {
    2961             :                 /*
    2962             :                  * Make sure the maximum inode count is a multiple
    2963             :                  * of the units we allocate inodes in.
    2964             :                  */
    2965       24337 :                 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
    2966       24337 :                 do_div(icount, 100);
    2967       24337 :                 do_div(icount, igeo->ialloc_blks);
    2968       24337 :                 igeo->maxicount = XFS_FSB_TO_INO(mp,
    2969             :                                 icount * igeo->ialloc_blks);
    2970             :         } else {
    2971           0 :                 igeo->maxicount = 0;
    2972             :         }
    2973             : 
    2974             :         /*
    2975             :          * Compute the desired size of an inode cluster buffer size, which
    2976             :          * starts at 8K and (on v5 filesystems) scales up with larger inode
    2977             :          * sizes.
    2978             :          *
    2979             :          * Preserve the desired inode cluster size because the sparse inodes
    2980             :          * feature uses that desired size (not the actual size) to compute the
    2981             :          * sparse inode alignment.  The mount code validates this value, so we
    2982             :          * cannot change the behavior.
    2983             :          */
    2984       24337 :         igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE;
    2985       24337 :         if (xfs_has_v3inodes(mp)) {
    2986       24293 :                 int     new_size = igeo->inode_cluster_size_raw;
    2987             : 
    2988       24293 :                 new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
    2989       24293 :                 if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
    2990       24293 :                         igeo->inode_cluster_size_raw = new_size;
    2991             :         }
    2992             : 
    2993             :         /* Calculate inode cluster ratios. */
    2994       24337 :         if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize)
    2995       24126 :                 igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp,
    2996             :                                 igeo->inode_cluster_size_raw);
    2997             :         else
    2998         211 :                 igeo->blocks_per_cluster = 1;
    2999       24337 :         igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster);
    3000       24337 :         igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster);
    3001             : 
    3002             :         /* Calculate inode cluster alignment. */
    3003       24337 :         if (xfs_has_align(mp) &&
    3004       24337 :             mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster)
    3005       24317 :                 igeo->cluster_align = mp->m_sb.sb_inoalignmt;
    3006             :         else
    3007          20 :                 igeo->cluster_align = 1;
    3008       24337 :         igeo->inoalign_mask = igeo->cluster_align - 1;
    3009       24337 :         igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align);
    3010             : 
    3011             :         /*
    3012             :          * If we are using stripe alignment, check whether
    3013             :          * the stripe unit is a multiple of the inode alignment
    3014             :          */
    3015       24337 :         if (mp->m_dalign && igeo->inoalign_mask &&
    3016          16 :             !(mp->m_dalign & igeo->inoalign_mask))
    3017          10 :                 igeo->ialloc_align = mp->m_dalign;
    3018             :         else
    3019       24327 :                 igeo->ialloc_align = 0;
    3020       24337 : }
    3021             : 
    3022             : /* Compute the location of the root directory inode that is laid out by mkfs. */
    3023             : xfs_ino_t
    3024          14 : xfs_ialloc_calc_rootino(
    3025             :         struct xfs_mount        *mp,
    3026             :         int                     sunit)
    3027             : {
    3028          14 :         struct xfs_ino_geometry *igeo = M_IGEO(mp);
    3029          14 :         xfs_agblock_t           first_bno;
    3030             : 
    3031             :         /*
    3032             :          * Pre-calculate the geometry of AG 0.  We know what it looks like
    3033             :          * because libxfs knows how to create allocation groups now.
    3034             :          *
    3035             :          * first_bno is the first block in which mkfs could possibly have
    3036             :          * allocated the root directory inode, once we factor in the metadata
    3037             :          * that mkfs formats before it.  Namely, the four AG headers...
    3038             :          */
    3039          14 :         first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
    3040             : 
    3041             :         /* ...the two free space btree roots... */
    3042          14 :         first_bno += 2;
    3043             : 
    3044             :         /* ...the inode btree root... */
    3045          14 :         first_bno += 1;
    3046             : 
    3047             :         /* ...the initial AGFL... */
    3048          14 :         first_bno += xfs_alloc_min_freelist(mp, NULL);
    3049             : 
    3050             :         /* ...the free inode btree root... */
    3051          14 :         if (xfs_has_finobt(mp))
    3052          14 :                 first_bno++;
    3053             : 
    3054             :         /* ...the reverse mapping btree root... */
    3055          14 :         if (xfs_has_rmapbt(mp))
    3056          14 :                 first_bno++;
    3057             : 
    3058             :         /* ...the reference count btree... */
    3059          14 :         if (xfs_has_reflink(mp))
    3060          14 :                 first_bno++;
    3061             : 
    3062             :         /*
    3063             :          * ...and the log, if it is allocated in the first allocation group.
    3064             :          *
    3065             :          * This can happen with filesystems that only have a single
    3066             :          * allocation group, or very odd geometries created by old mkfs
    3067             :          * versions on very small filesystems.
    3068             :          */
    3069          28 :         if (xfs_ag_contains_log(mp, 0))
    3070           0 :                  first_bno += mp->m_sb.sb_logblocks;
    3071             : 
    3072             :         /*
    3073             :          * Now round first_bno up to whatever allocation alignment is given
    3074             :          * by the filesystem or was passed in.
    3075             :          */
    3076          14 :         if (xfs_has_dalign(mp) && igeo->ialloc_align > 0)
    3077           8 :                 first_bno = roundup(first_bno, sunit);
    3078           6 :         else if (xfs_has_align(mp) &&
    3079           6 :                         mp->m_sb.sb_inoalignmt > 1)
    3080           6 :                 first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt);
    3081             : 
    3082          14 :         return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno));
    3083             : }
    3084             : 
    3085             : /*
    3086             :  * Ensure there are not sparse inode clusters that cross the new EOAG.
    3087             :  *
    3088             :  * This is a no-op for non-spinode filesystems since clusters are always fully
    3089             :  * allocated and checking the bnobt suffices.  However, a spinode filesystem
    3090             :  * could have a record where the upper inodes are free blocks.  If those blocks
    3091             :  * were removed from the filesystem, the inode record would extend beyond EOAG,
    3092             :  * which will be flagged as corruption.
    3093             :  */
    3094             : int
    3095         191 : xfs_ialloc_check_shrink(
    3096             :         struct xfs_perag        *pag,
    3097             :         struct xfs_trans        *tp,
    3098             :         struct xfs_buf          *agibp,
    3099             :         xfs_agblock_t           new_length)
    3100             : {
    3101         191 :         struct xfs_inobt_rec_incore rec;
    3102         191 :         struct xfs_btree_cur    *cur;
    3103         191 :         xfs_agino_t             agino;
    3104         191 :         int                     has;
    3105         191 :         int                     error;
    3106             : 
    3107         191 :         if (!xfs_has_sparseinodes(pag->pag_mount))
    3108             :                 return 0;
    3109             : 
    3110         191 :         cur = xfs_inobt_init_cursor(pag, tp, agibp, XFS_BTNUM_INO);
    3111             : 
    3112             :         /* Look up the inobt record that would correspond to the new EOFS. */
    3113         191 :         agino = XFS_AGB_TO_AGINO(pag->pag_mount, new_length);
    3114         191 :         error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has);
    3115         191 :         if (error || !has)
    3116          10 :                 goto out;
    3117             : 
    3118         181 :         error = xfs_inobt_get_rec(cur, &rec, &has);
    3119         181 :         if (error)
    3120           0 :                 goto out;
    3121             : 
    3122         181 :         if (!has) {
    3123           0 :                 xfs_ag_mark_sick(pag, XFS_SICK_AG_INOBT);
    3124           0 :                 error = -EFSCORRUPTED;
    3125           0 :                 goto out;
    3126             :         }
    3127             : 
    3128             :         /* If the record covers inodes that would be beyond EOFS, bail out. */
    3129         181 :         if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) {
    3130          95 :                 error = -ENOSPC;
    3131          95 :                 goto out;
    3132             :         }
    3133          86 : out:
    3134         191 :         xfs_btree_del_cursor(cur, error);
    3135         191 :         return error;
    3136             : }

Generated by: LCOV version 1.14