LCOV - code coverage report
Current view: top level - fs/xfs - xfs_iwalk.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 249 264 94.3 %
Date: 2023-07-31 20:08:34 Functions: 13 13 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2019 Oracle.  All Rights Reserved.
       4             :  * Author: Darrick J. Wong <darrick.wong@oracle.com>
       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_mount.h"
      13             : #include "xfs_inode.h"
      14             : #include "xfs_btree.h"
      15             : #include "xfs_ialloc.h"
      16             : #include "xfs_ialloc_btree.h"
      17             : #include "xfs_iwalk.h"
      18             : #include "xfs_error.h"
      19             : #include "xfs_trace.h"
      20             : #include "xfs_icache.h"
      21             : #include "xfs_health.h"
      22             : #include "xfs_trans.h"
      23             : #include "xfs_pwork.h"
      24             : #include "xfs_ag.h"
      25             : #include "xfs_bit.h"
      26             : 
      27             : /*
      28             :  * Walking Inodes in the Filesystem
      29             :  * ================================
      30             :  *
      31             :  * This iterator function walks a subset of filesystem inodes in increasing
      32             :  * order from @startino until there are no more inodes.  For each allocated
      33             :  * inode it finds, it calls a walk function with the relevant inode number and
      34             :  * a pointer to caller-provided data.  The walk function can return the usual
      35             :  * negative error code to stop the iteration; 0 to continue the iteration; or
      36             :  * -ECANCELED to stop the iteration.  This return value is returned to the
      37             :  * caller.
      38             :  *
      39             :  * Internally, we allow the walk function to do anything, which means that we
      40             :  * cannot maintain the inobt cursor or our lock on the AGI buffer.  We
      41             :  * therefore cache the inobt records in kernel memory and only call the walk
      42             :  * function when our memory buffer is full.  @nr_recs is the number of records
      43             :  * that we've cached, and @sz_recs is the size of our cache.
      44             :  *
      45             :  * It is the responsibility of the walk function to ensure it accesses
      46             :  * allocated inodes, as the inobt records may be stale by the time they are
      47             :  * acted upon.
      48             :  */
      49             : 
      50             : struct xfs_iwalk_ag {
      51             :         /* parallel work control data; will be null if single threaded */
      52             :         struct xfs_pwork                pwork;
      53             : 
      54             :         struct xfs_mount                *mp;
      55             :         struct xfs_trans                *tp;
      56             :         struct xfs_perag                *pag;
      57             : 
      58             :         /* Where do we start the traversal? */
      59             :         xfs_ino_t                       startino;
      60             : 
      61             :         /* What was the last inode number we saw when iterating the inobt? */
      62             :         xfs_ino_t                       lastino;
      63             : 
      64             :         /* Array of inobt records we cache. */
      65             :         struct xfs_inobt_rec_incore     *recs;
      66             : 
      67             :         /* Number of entries allocated for the @recs array. */
      68             :         unsigned int                    sz_recs;
      69             : 
      70             :         /* Number of entries in the @recs array that are in use. */
      71             :         unsigned int                    nr_recs;
      72             : 
      73             :         /* Inode walk function and data pointer. */
      74             :         xfs_iwalk_fn                    iwalk_fn;
      75             :         xfs_inobt_walk_fn               inobt_walk_fn;
      76             :         void                            *data;
      77             : 
      78             :         /*
      79             :          * Make it look like the inodes up to startino are free so that
      80             :          * bulkstat can start its inode iteration at the correct place without
      81             :          * needing to special case everywhere.
      82             :          */
      83             :         unsigned int                    trim_start:1;
      84             : 
      85             :         /* Skip empty inobt records? */
      86             :         unsigned int                    skip_empty:1;
      87             : 
      88             :         /* Drop the (hopefully empty) transaction when calling iwalk_fn. */
      89             :         unsigned int                    drop_trans:1;
      90             : };
      91             : 
      92             : /*
      93             :  * Loop over all clusters in a chunk for a given incore inode allocation btree
      94             :  * record.  Do a readahead if there are any allocated inodes in that cluster.
      95             :  */
      96             : STATIC void
      97  2773746951 : xfs_iwalk_ichunk_ra(
      98             :         struct xfs_mount                *mp,
      99             :         struct xfs_perag                *pag,
     100             :         struct xfs_inobt_rec_incore     *irec)
     101             : {
     102  2773746951 :         struct xfs_ino_geometry         *igeo = M_IGEO(mp);
     103  2773746951 :         xfs_agblock_t                   agbno;
     104  2773746951 :         struct blk_plug                 plug;
     105  2773746951 :         int                             i;      /* inode chunk index */
     106             : 
     107  2773746951 :         agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
     108             : 
     109  2773746951 :         blk_start_plug(&plug);
     110 11094302389 :         for (i = 0; i < XFS_INODES_PER_CHUNK; i += igeo->inodes_per_cluster) {
     111  5546692810 :                 xfs_inofree_t   imask;
     112             : 
     113  5546692810 :                 imask = xfs_inobt_maskn(i, igeo->inodes_per_cluster);
     114  5546585314 :                 if (imask & ~irec->ir_free) {
     115  4172160537 :                         xfs_btree_reada_bufs(mp, pag->pag_agno, agbno,
     116             :                                         igeo->blocks_per_cluster,
     117             :                                         &xfs_inode_buf_ops);
     118             :                 }
     119  5546776862 :                 agbno += igeo->blocks_per_cluster;
     120             :         }
     121  2773862628 :         blk_finish_plug(&plug);
     122  2773795957 : }
     123             : 
     124             : /*
     125             :  * Set the bits in @irec's free mask that correspond to the inodes before
     126             :  * @agino so that we skip them.  This is how we restart an inode walk that was
     127             :  * interrupted in the middle of an inode record.
     128             :  */
     129             : STATIC void
     130   540745681 : xfs_iwalk_adjust_start(
     131             :         xfs_agino_t                     agino,  /* starting inode of chunk */
     132             :         struct xfs_inobt_rec_incore     *irec)  /* btree record */
     133             : {
     134   540745681 :         int                             idx;    /* index into inode chunk */
     135             : 
     136   540745681 :         idx = agino - irec->ir_startino;
     137             : 
     138   540745681 :         irec->ir_free |= xfs_inobt_maskn(0, idx);
     139   540745681 :         irec->ir_freecount = hweight64(irec->ir_free);
     140   540745681 : }
     141             : 
     142             : /* Allocate memory for a walk. */
     143             : STATIC int
     144   557324971 : xfs_iwalk_alloc(
     145             :         struct xfs_iwalk_ag     *iwag)
     146             : {
     147   557324971 :         size_t                  size;
     148             : 
     149   557324971 :         ASSERT(iwag->recs == NULL);
     150   557324971 :         iwag->nr_recs = 0;
     151             : 
     152             :         /* Allocate a prefetch buffer for inobt records. */
     153   557324971 :         size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
     154   557324971 :         iwag->recs = kmem_alloc(size, KM_MAYFAIL);
     155   557312990 :         if (iwag->recs == NULL)
     156           0 :                 return -ENOMEM;
     157             : 
     158             :         return 0;
     159             : }
     160             : 
     161             : /* Free memory we allocated for a walk. */
     162             : STATIC void
     163   557345513 : xfs_iwalk_free(
     164             :         struct xfs_iwalk_ag     *iwag)
     165             : {
     166   557345513 :         kmem_free(iwag->recs);
     167   557343528 :         iwag->recs = NULL;
     168   557343528 : }
     169             : 
     170             : /* For each inuse inode in each cached inobt record, call our function. */
     171             : STATIC int
     172   728539972 : xfs_iwalk_ag_recs(
     173             :         struct xfs_iwalk_ag     *iwag)
     174             : {
     175   728539972 :         struct xfs_mount        *mp = iwag->mp;
     176   728539972 :         struct xfs_trans        *tp = iwag->tp;
     177   728539972 :         struct xfs_perag        *pag = iwag->pag;
     178   728539972 :         xfs_ino_t               ino;
     179   728539972 :         unsigned int            i, j;
     180   728539972 :         int                     error;
     181             : 
     182  2448577025 :         for (i = 0; i < iwag->nr_recs; i++) {
     183  2272371249 :                 struct xfs_inobt_rec_incore     *irec = &iwag->recs[i];
     184             : 
     185  2272371249 :                 trace_xfs_iwalk_ag_rec(mp, pag->pag_agno, irec);
     186             : 
     187  4544017124 :                 if (xfs_pwork_want_abort(&iwag->pwork))
     188             :                         return 0;
     189             : 
     190  2272008562 :                 if (iwag->inobt_walk_fn) {
     191     6883327 :                         error = iwag->inobt_walk_fn(mp, tp, pag->pag_agno, irec,
     192             :                                         iwag->data);
     193     6884924 :                         if (error)
     194     4366026 :                                 return error;
     195             :                 }
     196             : 
     197  2267644133 :                 if (!iwag->iwalk_fn)
     198     2520425 :                         continue;
     199             : 
     200 >12754*10^7 :                 for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {
     201 >25164*10^7 :                         if (xfs_pwork_want_abort(&iwag->pwork))
     202             :                                 return 0;
     203             : 
     204             :                         /* Skip if this inode is free */
     205 >12582*10^7 :                         if (XFS_INOBT_MASK(j) & irec->ir_free)
     206 44430363134 :                                 continue;
     207             : 
     208             :                         /* Otherwise call our function. */
     209 81392755720 :                         ino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
     210             :                                                 irec->ir_startino + j);
     211 81392755720 :                         error = iwag->iwalk_fn(mp, tp, ino, iwag->data);
     212 81393076121 :                         if (error)
     213   547927481 :                                 return error;
     214             :                 }
     215             :         }
     216             : 
     217             :         return 0;
     218             : }
     219             : 
     220             : /* Delete cursor and let go of AGI. */
     221             : static inline void
     222  1294418956 : xfs_iwalk_del_inobt(
     223             :         struct xfs_trans        *tp,
     224             :         struct xfs_btree_cur    **curpp,
     225             :         struct xfs_buf          **agi_bpp,
     226             :         int                     error)
     227             : {
     228  1294418956 :         if (*curpp) {
     229   742208543 :                 xfs_btree_del_cursor(*curpp, error);
     230   742268066 :                 *curpp = NULL;
     231             :         }
     232  1294478479 :         if (*agi_bpp) {
     233   742266310 :                 xfs_trans_brelse(tp, *agi_bpp);
     234   742281741 :                 *agi_bpp = NULL;
     235             :         }
     236  1294493910 : }
     237             : 
     238             : /*
     239             :  * Set ourselves up for walking inobt records starting from a given point in
     240             :  * the filesystem.
     241             :  *
     242             :  * If caller passed in a nonzero start inode number, load the record from the
     243             :  * inobt and make the record look like all the inodes before agino are free so
     244             :  * that we skip them, and then move the cursor to the next inobt record.  This
     245             :  * is how we support starting an iwalk in the middle of an inode chunk.
     246             :  *
     247             :  * If the caller passed in a start number of zero, move the cursor to the first
     248             :  * inobt record.
     249             :  *
     250             :  * The caller is responsible for cleaning up the cursor and buffer pointer
     251             :  * regardless of the error status.
     252             :  */
     253             : STATIC int
     254   565884408 : xfs_iwalk_ag_start(
     255             :         struct xfs_iwalk_ag     *iwag,
     256             :         xfs_agino_t             agino,
     257             :         struct xfs_btree_cur    **curpp,
     258             :         struct xfs_buf          **agi_bpp,
     259             :         int                     *has_more)
     260             : {
     261   565884408 :         struct xfs_mount        *mp = iwag->mp;
     262   565884408 :         struct xfs_trans        *tp = iwag->tp;
     263   565884408 :         struct xfs_perag        *pag = iwag->pag;
     264   565884408 :         struct xfs_inobt_rec_incore *irec;
     265   565884408 :         int                     error;
     266             : 
     267             :         /* Set up a fresh cursor and empty the inobt cache. */
     268   565884408 :         iwag->nr_recs = 0;
     269   565884408 :         error = xfs_inobt_cur(pag, tp, XFS_BTNUM_INO, curpp, agi_bpp);
     270   566023832 :         if (error)
     271             :                 return error;
     272             : 
     273             :         /* Starting at the beginning of the AG?  That's easy! */
     274   566023265 :         if (agino == 0)
     275    11405610 :                 return xfs_inobt_lookup(*curpp, 0, XFS_LOOKUP_GE, has_more);
     276             : 
     277             :         /*
     278             :          * Otherwise, we have to grab the inobt record where we left off, stuff
     279             :          * the record into our cache, and then see if there are more records.
     280             :          * We require a lookup cache of at least two elements so that the
     281             :          * caller doesn't have to deal with tearing down the cursor to walk the
     282             :          * records.
     283             :          */
     284   554617655 :         error = xfs_inobt_lookup(*curpp, agino, XFS_LOOKUP_LE, has_more);
     285   554621147 :         if (error)
     286             :                 return error;
     287             : 
     288             :         /*
     289             :          * If the LE lookup at @agino yields no records, jump ahead to the
     290             :          * inobt cursor increment to see if there are more records to process.
     291             :          */
     292   554621082 :         if (!*has_more)
     293          54 :                 goto out_advance;
     294             : 
     295             :         /* Get the record, should always work */
     296   554621028 :         irec = &iwag->recs[iwag->nr_recs];
     297   554621028 :         error = xfs_inobt_get_rec(*curpp, irec, has_more);
     298   554577403 :         if (error)
     299             :                 return error;
     300   554577403 :         if (XFS_IS_CORRUPT(mp, *has_more != 1)) {
     301           0 :                 xfs_btree_mark_sick(*curpp);
     302           0 :                 return -EFSCORRUPTED;
     303             :         }
     304             : 
     305   554577403 :         iwag->lastino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
     306             :                                 irec->ir_startino + XFS_INODES_PER_CHUNK - 1);
     307             : 
     308             :         /*
     309             :          * If the LE lookup yielded an inobt record before the cursor position,
     310             :          * skip it and see if there's another one after it.
     311             :          */
     312   554577403 :         if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino)
     313    11381276 :                 goto out_advance;
     314             : 
     315             :         /*
     316             :          * If agino fell in the middle of the inode record, make it look like
     317             :          * the inodes up to agino are free so that we don't return them again.
     318             :          */
     319   543196127 :         if (iwag->trim_start)
     320   540749429 :                 xfs_iwalk_adjust_start(agino, irec);
     321             : 
     322             :         /*
     323             :          * The prefetch calculation is supposed to give us a large enough inobt
     324             :          * record cache that grab_ichunk can stage a partial first record and
     325             :          * the loop body can cache a record without having to check for cache
     326             :          * space until after it reads an inobt record.
     327             :          */
     328   543187939 :         iwag->nr_recs++;
     329   543187939 :         ASSERT(iwag->nr_recs < iwag->sz_recs);
     330             : 
     331   543187939 : out_advance:
     332   554569269 :         return xfs_btree_increment(*curpp, 0, has_more);
     333             : }
     334             : 
     335             : /*
     336             :  * The inobt record cache is full, so preserve the inobt cursor state and
     337             :  * run callbacks on the cached inobt records.  When we're done, restore the
     338             :  * cursor state to wherever the cursor would have been had the cache not been
     339             :  * full (and therefore we could've just incremented the cursor) if *@has_more
     340             :  * is true.  On exit, *@has_more will indicate whether or not the caller should
     341             :  * try for more inode records.
     342             :  */
     343             : STATIC int
     344   728531740 : xfs_iwalk_run_callbacks(
     345             :         struct xfs_iwalk_ag             *iwag,
     346             :         struct xfs_btree_cur            **curpp,
     347             :         struct xfs_buf                  **agi_bpp,
     348             :         int                             *has_more)
     349             : {
     350   728531740 :         struct xfs_mount                *mp = iwag->mp;
     351   728531740 :         struct xfs_inobt_rec_incore     *irec;
     352   728531740 :         xfs_agino_t                     next_agino;
     353   728531740 :         int                             error;
     354             : 
     355   728531740 :         next_agino = XFS_INO_TO_AGINO(mp, iwag->lastino) + 1;
     356             : 
     357   728531740 :         ASSERT(iwag->nr_recs > 0);
     358             : 
     359             :         /* Delete cursor but remember the last record we cached... */
     360   728531740 :         xfs_iwalk_del_inobt(iwag->tp, curpp, agi_bpp, 0);
     361   728557643 :         irec = &iwag->recs[iwag->nr_recs - 1];
     362   728557643 :         ASSERT(next_agino >= irec->ir_startino + XFS_INODES_PER_CHUNK);
     363             : 
     364   728557643 :         if (iwag->drop_trans) {
     365       14633 :                 xfs_trans_cancel(iwag->tp);
     366       14633 :                 iwag->tp = NULL;
     367             :         }
     368             : 
     369   728557643 :         error = xfs_iwalk_ag_recs(iwag);
     370   728450516 :         if (error)
     371             :                 return error;
     372             : 
     373             :         /* ...empty the cache... */
     374   176200133 :         iwag->nr_recs = 0;
     375             : 
     376   176200133 :         if (!has_more)
     377             :                 return 0;
     378             : 
     379   176200133 :         if (iwag->drop_trans) {
     380       14633 :                 error = xfs_trans_alloc_empty(mp, &iwag->tp);
     381       14632 :                 if (error)
     382             :                         return error;
     383             :         }
     384             : 
     385             :         /* ...and recreate the cursor just past where we left off. */
     386   176200132 :         error = xfs_inobt_cur(iwag->pag, iwag->tp, XFS_BTNUM_INO, curpp,
     387             :                         agi_bpp);
     388   176219246 :         if (error)
     389             :                 return error;
     390             : 
     391   176213413 :         return xfs_inobt_lookup(*curpp, next_agino, XFS_LOOKUP_GE, has_more);
     392             : }
     393             : 
     394             : /* Walk all inodes in a single AG, from @iwag->startino to the end of the AG. */
     395             : STATIC int
     396   565975606 : xfs_iwalk_ag(
     397             :         struct xfs_iwalk_ag             *iwag)
     398             : {
     399   565975606 :         struct xfs_mount                *mp = iwag->mp;
     400   565975606 :         struct xfs_perag                *pag = iwag->pag;
     401   565975606 :         struct xfs_buf                  *agi_bp = NULL;
     402   565975606 :         struct xfs_btree_cur            *cur = NULL;
     403   565975606 :         xfs_agino_t                     agino;
     404   565975606 :         int                             has_more;
     405   565975606 :         int                             error = 0;
     406             : 
     407             :         /* Set up our cursor at the right place in the inode btree. */
     408   565975606 :         ASSERT(pag->pag_agno == XFS_INO_TO_AGNO(mp, iwag->startino));
     409   565975606 :         agino = XFS_INO_TO_AGINO(mp, iwag->startino);
     410   565975606 :         error = xfs_iwalk_ag_start(iwag, agino, &cur, &agi_bp, &has_more);
     411             : 
     412  3337805099 :         while (!error && has_more) {
     413  2782410862 :                 struct xfs_inobt_rec_incore     *irec;
     414  2782410862 :                 xfs_ino_t                       rec_fsino;
     415             : 
     416  2782410862 :                 cond_resched();
     417  5564885190 :                 if (xfs_pwork_want_abort(&iwag->pwork))
     418           0 :                         goto out;
     419             : 
     420             :                 /* Fetch the inobt record. */
     421  2782442595 :                 irec = &iwag->recs[iwag->nr_recs];
     422  2782442595 :                 error = xfs_inobt_get_rec(cur, irec, &has_more);
     423  2782450308 :                 if (error || !has_more)
     424             :                         break;
     425             : 
     426             :                 /* Make sure that we always move forward. */
     427  2782450308 :                 rec_fsino = XFS_AGINO_TO_INO(mp, pag->pag_agno, irec->ir_startino);
     428  2782450308 :                 if (iwag->lastino != NULLFSINO &&
     429  2780058648 :                     XFS_IS_CORRUPT(mp, iwag->lastino >= rec_fsino)) {
     430           0 :                         xfs_btree_mark_sick(cur);
     431           0 :                         error = -EFSCORRUPTED;
     432           0 :                         goto out;
     433             :                 }
     434  2782450308 :                 iwag->lastino = rec_fsino + XFS_INODES_PER_CHUNK - 1;
     435             : 
     436             :                 /* No allocated inodes in this chunk; skip it. */
     437  2782450308 :                 if (iwag->skip_empty && irec->ir_freecount == irec->ir_count) {
     438           0 :                         error = xfs_btree_increment(cur, 0, &has_more);
     439           0 :                         if (error)
     440             :                                 break;
     441           0 :                         continue;
     442             :                 }
     443             : 
     444             :                 /*
     445             :                  * Start readahead for this inode chunk in anticipation of
     446             :                  * walking the inodes.
     447             :                  */
     448  2782450308 :                 if (iwag->iwalk_fn)
     449  2773789829 :                         xfs_iwalk_ichunk_ra(mp, pag, irec);
     450             : 
     451             :                 /*
     452             :                  * If there's space in the buffer for more records, increment
     453             :                  * the btree cursor and grab more.
     454             :                  */
     455  2782720236 :                 if (++iwag->nr_recs < iwag->sz_recs) {
     456  2069255763 :                         error = xfs_btree_increment(cur, 0, &has_more);
     457  2069182013 :                         if (error || !has_more)
     458             :                                 break;
     459  2058647027 :                         continue;
     460             :                 }
     461             : 
     462             :                 /*
     463             :                  * Otherwise, we need to save cursor state and run the callback
     464             :                  * function on the cached records.  The run_callbacks function
     465             :                  * is supposed to return a cursor pointing to the record where
     466             :                  * we would be if we had been able to increment like above.
     467             :                  */
     468   713464473 :                 ASSERT(has_more);
     469   713464473 :                 error = xfs_iwalk_run_callbacks(iwag, &cur, &agi_bp, &has_more);
     470             :         }
     471             : 
     472   565970107 :         if (iwag->nr_recs == 0 || error)
     473   550883881 :                 goto out;
     474             : 
     475             :         /* Walk the unprocessed records in the cache. */
     476    15086226 :         error = xfs_iwalk_run_callbacks(iwag, &cur, &agi_bp, &has_more);
     477             : 
     478   565968897 : out:
     479   565968897 :         xfs_iwalk_del_inobt(iwag->tp, &cur, &agi_bp, error);
     480   565927071 :         return error;
     481             : }
     482             : 
     483             : /*
     484             :  * We experimentally determined that the reduction in ioctl call overhead
     485             :  * diminishes when userspace asks for more than 2048 inodes, so we'll cap
     486             :  * prefetch at this point.
     487             :  */
     488             : #define IWALK_MAX_INODE_PREFETCH        (2048U)
     489             : 
     490             : /*
     491             :  * Given the number of inodes to prefetch, set the number of inobt records that
     492             :  * we cache in memory, which controls the number of inodes we try to read
     493             :  * ahead.  Set the maximum if @inodes == 0.
     494             :  */
     495             : static inline unsigned int
     496             : xfs_iwalk_prefetch(
     497             :         unsigned int            inodes)
     498             : {
     499   552388820 :         unsigned int            inobt_records;
     500             : 
     501             :         /*
     502             :          * If the caller didn't tell us the number of inodes they wanted,
     503             :          * assume the maximum prefetch possible for best performance.
     504             :          * Otherwise, cap prefetch at that maximum so that we don't start an
     505             :          * absurd amount of prefetch.
     506             :          */
     507   552388820 :         if (inodes == 0)
     508       95570 :                 inodes = IWALK_MAX_INODE_PREFETCH;
     509   552388820 :         inodes = min(inodes, IWALK_MAX_INODE_PREFETCH);
     510             : 
     511             :         /* Round the inode count up to a full chunk. */
     512   552388820 :         inodes = round_up(inodes, XFS_INODES_PER_CHUNK);
     513             : 
     514             :         /*
     515             :          * In order to convert the number of inodes to prefetch into an
     516             :          * estimate of the number of inobt records to cache, we require a
     517             :          * conversion factor that reflects our expectations of the average
     518             :          * loading factor of an inode chunk.  Based on data gathered, most
     519             :          * (but not all) filesystems manage to keep the inode chunks totally
     520             :          * full, so we'll underestimate slightly so that our readahead will
     521             :          * still deliver the performance we want on aging filesystems:
     522             :          *
     523             :          * inobt = inodes / (INODES_PER_CHUNK * (4 / 5));
     524             :          *
     525             :          * The funny math is to avoid integer division.
     526             :          */
     527   552388820 :         inobt_records = (inodes * 5) / (4 * XFS_INODES_PER_CHUNK);
     528             : 
     529             :         /*
     530             :          * Allocate enough space to prefetch at least two inobt records so that
     531             :          * we can cache both the record where the iwalk started and the next
     532             :          * record.  This simplifies the AG inode walk loop setup code.
     533             :          */
     534   552388820 :         return max(inobt_records, 2U);
     535             : }
     536             : 
     537             : /*
     538             :  * Walk all inodes in the filesystem starting from @startino.  The @iwalk_fn
     539             :  * will be called for each allocated inode, being passed the inode's number and
     540             :  * @data.  @max_prefetch controls how many inobt records' worth of inodes we
     541             :  * try to readahead.
     542             :  */
     543             : int
     544   552293250 : xfs_iwalk(
     545             :         struct xfs_mount        *mp,
     546             :         struct xfs_trans        *tp,
     547             :         xfs_ino_t               startino,
     548             :         unsigned int            flags,
     549             :         xfs_iwalk_fn            iwalk_fn,
     550             :         unsigned int            inode_records,
     551             :         void                    *data)
     552             : {
     553   552293250 :         struct xfs_iwalk_ag     iwag = {
     554             :                 .mp             = mp,
     555             :                 .tp             = tp,
     556             :                 .iwalk_fn       = iwalk_fn,
     557             :                 .data           = data,
     558             :                 .startino       = startino,
     559             :                 .sz_recs        = xfs_iwalk_prefetch(inode_records),
     560             :                 .trim_start     = 1,
     561             :                 .skip_empty     = 1,
     562             :                 .pwork          = XFS_PWORK_SINGLE_THREADED,
     563             :                 .lastino        = NULLFSINO,
     564             :         };
     565   552293250 :         struct xfs_perag        *pag;
     566   552293250 :         xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
     567   552293250 :         int                     error;
     568             : 
     569   552293250 :         ASSERT(agno < mp->m_sb.sb_agcount);
     570   552293250 :         ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
     571             : 
     572   552293250 :         error = xfs_iwalk_alloc(&iwag);
     573   552269562 :         if (error)
     574             :                 return error;
     575             : 
     576   565259480 :         for_each_perag_from(mp, agno, pag) {
     577   560898921 :                 iwag.pag = pag;
     578   560898921 :                 error = xfs_iwalk_ag(&iwag);
     579   560863882 :                 if (error)
     580             :                         break;
     581    12998014 :                 iwag.startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
     582    12998014 :                 if (flags & XFS_INOBT_WALK_SAME_AG)
     583             :                         break;
     584    12993086 :                 iwag.pag = NULL;
     585             :         }
     586             : 
     587   552264544 :         if (iwag.pag)
     588   547868826 :                 xfs_perag_rele(pag);
     589   552300036 :         xfs_iwalk_free(&iwag);
     590   552300036 :         return error;
     591             : }
     592             : 
     593             : /* Run per-thread iwalk work. */
     594             : static int
     595       95569 : xfs_iwalk_ag_work(
     596             :         struct xfs_mount        *mp,
     597             :         struct xfs_pwork        *pwork)
     598             : {
     599       95569 :         struct xfs_iwalk_ag     *iwag;
     600       95569 :         int                     error = 0;
     601             : 
     602       95569 :         iwag = container_of(pwork, struct xfs_iwalk_ag, pwork);
     603      191138 :         if (xfs_pwork_want_abort(pwork))
     604           0 :                 goto out;
     605             : 
     606       95569 :         error = xfs_iwalk_alloc(iwag);
     607       95562 :         if (error)
     608           0 :                 goto out;
     609             :         /*
     610             :          * Grab an empty transaction so that we can use its recursive buffer
     611             :          * locking abilities to detect cycles in the inobt without deadlocking.
     612             :          */
     613       95562 :         error = xfs_trans_alloc_empty(mp, &iwag->tp);
     614       95557 :         if (error)
     615           0 :                 goto out;
     616       95557 :         iwag->drop_trans = 1;
     617             : 
     618       95557 :         error = xfs_iwalk_ag(iwag);
     619       95519 :         if (iwag->tp)
     620       95519 :                 xfs_trans_cancel(iwag->tp);
     621       95392 :         xfs_iwalk_free(iwag);
     622       95371 : out:
     623       95371 :         xfs_perag_put(iwag->pag);
     624       95518 :         kmem_free(iwag);
     625       95474 :         return error;
     626             : }
     627             : 
     628             : /*
     629             :  * Walk all the inodes in the filesystem using multiple threads to process each
     630             :  * AG.
     631             :  */
     632             : int
     633       14200 : xfs_iwalk_threaded(
     634             :         struct xfs_mount        *mp,
     635             :         xfs_ino_t               startino,
     636             :         unsigned int            flags,
     637             :         xfs_iwalk_fn            iwalk_fn,
     638             :         unsigned int            inode_records,
     639             :         bool                    polled,
     640             :         void                    *data)
     641             : {
     642       14200 :         struct xfs_pwork_ctl    pctl;
     643       14200 :         struct xfs_perag        *pag;
     644       14200 :         xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
     645       14200 :         int                     error;
     646             : 
     647       14200 :         ASSERT(agno < mp->m_sb.sb_agcount);
     648       14200 :         ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
     649             : 
     650       14200 :         error = xfs_pwork_init(mp, &pctl, xfs_iwalk_ag_work, "xfs_iwalk");
     651       14200 :         if (error)
     652             :                 return error;
     653             : 
     654      109770 :         for_each_perag_from(mp, agno, pag) {
     655       95570 :                 struct xfs_iwalk_ag     *iwag;
     656             : 
     657      191140 :                 if (xfs_pwork_ctl_want_abort(&pctl))
     658             :                         break;
     659             : 
     660       95570 :                 iwag = kmem_zalloc(sizeof(struct xfs_iwalk_ag), 0);
     661       95570 :                 iwag->mp = mp;
     662             : 
     663             :                 /*
     664             :                  * perag is being handed off to async work, so take a passive
     665             :                  * reference for the async work to release.
     666             :                  */
     667       95570 :                 iwag->pag = xfs_perag_hold(pag);
     668       95570 :                 iwag->iwalk_fn = iwalk_fn;
     669       95570 :                 iwag->data = data;
     670       95570 :                 iwag->startino = startino;
     671       95570 :                 iwag->sz_recs = xfs_iwalk_prefetch(inode_records);
     672       95570 :                 iwag->lastino = NULLFSINO;
     673       95570 :                 xfs_pwork_queue(&pctl, &iwag->pwork);
     674       95570 :                 startino = XFS_AGINO_TO_INO(mp, pag->pag_agno + 1, 0);
     675       95570 :                 if (flags & XFS_INOBT_WALK_SAME_AG)
     676             :                         break;
     677             :         }
     678       14200 :         if (pag)
     679           0 :                 xfs_perag_rele(pag);
     680       14200 :         if (polled)
     681       14200 :                 xfs_pwork_poll(&pctl);
     682       14200 :         return xfs_pwork_destroy(&pctl);
     683             : }
     684             : 
     685             : /*
     686             :  * Allow callers to cache up to a page's worth of inobt records.  This reflects
     687             :  * the existing inumbers prefetching behavior.  Since the inobt walk does not
     688             :  * itself do anything with the inobt records, we can set a fairly high limit
     689             :  * here.
     690             :  */
     691             : #define MAX_INOBT_WALK_PREFETCH \
     692             :         (PAGE_SIZE / sizeof(struct xfs_inobt_rec_incore))
     693             : 
     694             : /*
     695             :  * Given the number of records that the user wanted, set the number of inobt
     696             :  * records that we buffer in memory.  Set the maximum if @inobt_records == 0.
     697             :  */
     698             : static inline unsigned int
     699             : xfs_inobt_walk_prefetch(
     700             :         unsigned int            inobt_records)
     701             : {
     702             :         /*
     703             :          * If the caller didn't tell us the number of inobt records they
     704             :          * wanted, assume the maximum prefetch possible for best performance.
     705             :          */
     706     4961253 :         if (inobt_records == 0)
     707           0 :                 inobt_records = MAX_INOBT_WALK_PREFETCH;
     708             : 
     709             :         /*
     710             :          * Allocate enough space to prefetch at least two inobt records so that
     711             :          * we can cache both the record where the iwalk started and the next
     712             :          * record.  This simplifies the AG inode walk loop setup code.
     713             :          */
     714     4961253 :         inobt_records = max(inobt_records, 2U);
     715             : 
     716             :         /*
     717             :          * Cap prefetch at that maximum so that we don't use an absurd amount
     718             :          * of memory.
     719             :          */
     720     4961253 :         return min_t(unsigned int, inobt_records, MAX_INOBT_WALK_PREFETCH);
     721             : }
     722             : 
     723             : /*
     724             :  * Walk all inode btree records in the filesystem starting from @startino.  The
     725             :  * @inobt_walk_fn will be called for each btree record, being passed the incore
     726             :  * record and @data.  @max_prefetch controls how many inobt records we try to
     727             :  * cache ahead of time.
     728             :  */
     729             : int
     730     4961253 : xfs_inobt_walk(
     731             :         struct xfs_mount        *mp,
     732             :         struct xfs_trans        *tp,
     733             :         xfs_ino_t               startino,
     734             :         unsigned int            flags,
     735             :         xfs_inobt_walk_fn       inobt_walk_fn,
     736             :         unsigned int            inobt_records,
     737             :         void                    *data)
     738             : {
     739     4961253 :         struct xfs_iwalk_ag     iwag = {
     740             :                 .mp             = mp,
     741             :                 .tp             = tp,
     742             :                 .inobt_walk_fn  = inobt_walk_fn,
     743             :                 .data           = data,
     744             :                 .startino       = startino,
     745             :                 .sz_recs        = xfs_inobt_walk_prefetch(inobt_records),
     746             :                 .pwork          = XFS_PWORK_SINGLE_THREADED,
     747             :                 .lastino        = NULLFSINO,
     748             :         };
     749     4961253 :         struct xfs_perag        *pag;
     750     4961253 :         xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
     751     4961253 :         int                     error;
     752             : 
     753     4961253 :         ASSERT(agno < mp->m_sb.sb_agcount);
     754     4961253 :         ASSERT(!(flags & ~XFS_INOBT_WALK_FLAGS_ALL));
     755             : 
     756     4961253 :         error = xfs_iwalk_alloc(&iwag);
     757     4950831 :         if (error)
     758             :                 return error;
     759             : 
     760     4998897 :         for_each_perag_from(mp, agno, pag) {
     761     5008547 :                 iwag.pag = pag;
     762     5008547 :                 error = xfs_iwalk_ag(&iwag);
     763     4997703 :                 if (error)
     764             :                         break;
     765      629684 :                 iwag.startino = XFS_AGINO_TO_INO(mp, pag->pag_agno + 1, 0);
     766      629684 :                 if (flags & XFS_INOBT_WALK_SAME_AG)
     767             :                         break;
     768       44526 :                 iwag.pag = NULL;
     769             :         }
     770             : 
     771     4960677 :         if (iwag.pag)
     772     4949366 :                 xfs_perag_rele(pag);
     773     4968014 :         xfs_iwalk_free(&iwag);
     774     4968014 :         return error;
     775             : }

Generated by: LCOV version 1.14