LCOV - code coverage report
Current view: top level - fs/xfs/scrub - iscan.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 265 289 91.7 %
Date: 2023-07-31 20:08:07 Functions: 16 17 94.1 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2021-2023 Oracle.  All Rights Reserved.
       4             :  * Author: Darrick J. Wong <djwong@kernel.org>
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_trans_resv.h"
      11             : #include "xfs_mount.h"
      12             : #include "xfs_log_format.h"
      13             : #include "xfs_trans.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_ag.h"
      19             : #include "xfs_error.h"
      20             : #include "xfs_bit.h"
      21             : #include "xfs_icache.h"
      22             : #include "scrub/scrub.h"
      23             : #include "scrub/iscan.h"
      24             : #include "scrub/common.h"
      25             : #include "scrub/trace.h"
      26             : 
      27             : /*
      28             :  * Live File Scan
      29             :  * ==============
      30             :  *
      31             :  * Live file scans walk every inode in a live filesystem.  This is more or
      32             :  * less like a regular iwalk, except that when we're advancing the scan cursor,
      33             :  * we must ensure that inodes cannot be added or deleted anywhere between the
      34             :  * old cursor value and the new cursor value.  If we're advancing the cursor
      35             :  * by one inode, the caller must hold that inode; if we're finding the next
      36             :  * inode to scan, we must grab the AGI and hold it until we've updated the
      37             :  * scan cursor.
      38             :  *
      39             :  * Callers are expected to use this code to scan all files in the filesystem to
      40             :  * construct a new metadata index of some kind.  The scan races against other
      41             :  * live updates, which means there must be a provision to update the new index
      42             :  * when updates are made to inodes that already been scanned.  The iscan lock
      43             :  * can be used in live update hook code to stop the scan and protect this data
      44             :  * structure.
      45             :  *
      46             :  * To keep the new index up to date with other metadata updates being made to
      47             :  * the live filesystem, it is assumed that the caller will add hooks as needed
      48             :  * to be notified when a metadata update occurs.  The inode scanner must tell
      49             :  * the hook code when an inode has been visited with xchk_iscan_mark_visit.
      50             :  * Hook functions can use xchk_iscan_want_live_update to decide if the
      51             :  * scanner's observations must be updated.
      52             :  */
      53             : 
      54             : /*
      55             :  * Set *cursor to the next allocated inode after whatever it's set to now.
      56             :  * If there are no more inodes in this AG, cursor is set to NULLAGINO.
      57             :  */
      58             : STATIC int
      59    61987542 : xchk_iscan_find_next(
      60             :         struct xchk_iscan       *iscan,
      61             :         struct xfs_buf          *agi_bp,
      62             :         struct xfs_perag        *pag,
      63             :         xfs_inofree_t           *allocmaskp,
      64             :         xfs_agino_t             *cursor,
      65             :         uint8_t                 *nr_inodesp)
      66             : {
      67    61987542 :         struct xfs_scrub        *sc = iscan->sc;
      68    61987542 :         struct xfs_inobt_rec_incore     rec;
      69    61987542 :         struct xfs_btree_cur    *cur;
      70    61987542 :         struct xfs_mount        *mp = sc->mp;
      71    61987542 :         struct xfs_trans        *tp = sc->tp;
      72    61987542 :         xfs_agnumber_t          agno = pag->pag_agno;
      73    61987542 :         xfs_agino_t             lastino = NULLAGINO;
      74    61987542 :         xfs_agino_t             first, last;
      75    61987542 :         xfs_agino_t             agino = *cursor;
      76    61987542 :         int                     has_rec;
      77    61987542 :         int                     error;
      78             : 
      79             :         /* If the cursor is beyond the end of this AG, move to the next one. */
      80    61987542 :         xfs_agino_range(mp, agno, &first, &last);
      81    61987730 :         if (agino > last) {
      82           0 :                 *cursor = NULLAGINO;
      83           0 :                 return 0;
      84             :         }
      85             : 
      86             :         /*
      87             :          * Look up the inode chunk for the current cursor position.  If there
      88             :          * is no chunk here, we want the next one.
      89             :          */
      90    61987730 :         cur = xfs_inobt_init_cursor(pag, tp, agi_bp, XFS_BTNUM_INO);
      91    61987553 :         error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has_rec);
      92    61987882 :         if (!error && !has_rec)
      93      544066 :                 error = xfs_btree_increment(cur, 0, &has_rec);
      94   123293360 :         for (; !error; error = xfs_btree_increment(cur, 0, &has_rec)) {
      95   123292674 :                 xfs_inofree_t   allocmask;
      96             : 
      97             :                 /*
      98             :                  * If we've run out of inobt records in this AG, move the
      99             :                  * cursor on to the next AG and exit.  The caller can try
     100             :                  * again with the next AG.
     101             :                  */
     102   123292674 :                 if (!has_rec) {
     103      543385 :                         *cursor = NULLAGINO;
     104      543385 :                         break;
     105             :                 }
     106             : 
     107   122749289 :                 error = xfs_inobt_get_rec(cur, &rec, &has_rec);
     108   122749878 :                 if (error)
     109             :                         break;
     110   122749878 :                 if (!has_rec) {
     111             :                         error = -EFSCORRUPTED;
     112             :                         break;
     113             :                 }
     114             : 
     115             :                 /* Make sure that we always move forward. */
     116   122749878 :                 if (lastino != NULLAGINO &&
     117    60873418 :                     XFS_IS_CORRUPT(mp, lastino >= rec.ir_startino)) {
     118             :                         error = -EFSCORRUPTED;
     119             :                         break;
     120             :                 }
     121   122749878 :                 lastino = rec.ir_startino + XFS_INODES_PER_CHUNK - 1;
     122             : 
     123             :                 /*
     124             :                  * If this record only covers inodes that come before the
     125             :                  * cursor, advance to the next record.
     126             :                  */
     127   122749878 :                 if (rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
     128         564 :                         continue;
     129             : 
     130             :                 /*
     131             :                  * If the incoming lookup put us in the middle of an inobt
     132             :                  * record, mark it and the previous inodes "free" so that the
     133             :                  * search for allocated inodes will start at the cursor.
     134             :                  * We don't care about ir_freecount here.
     135             :                  */
     136   122749314 :                 if (agino >= rec.ir_startino)
     137    61442352 :                         rec.ir_free |= xfs_inobt_maskn(0,
     138    61442352 :                                                 agino + 1 - rec.ir_startino);
     139             : 
     140             :                 /*
     141             :                  * If there are allocated inodes in this chunk, find them
     142             :                  * and update the scan cursor.
     143             :                  */
     144   122749314 :                 allocmask = ~rec.ir_free;
     145   245498510 :                 if (hweight64(allocmask) > 0) {
     146    61444286 :                         int     next = xfs_lowbit64(allocmask);
     147             : 
     148    61444286 :                         ASSERT(next >= 0);
     149    61444286 :                         *cursor = rec.ir_startino + next;
     150    61444286 :                         *allocmaskp = allocmask >> next;
     151    61444286 :                         *nr_inodesp = XFS_INODES_PER_CHUNK - next;
     152    61444286 :                         break;
     153             :                 }
     154             :         }
     155             : 
     156    61987671 :         xfs_btree_del_cursor(cur, error);
     157    61987671 :         return error;
     158             : }
     159             : 
     160             : /*
     161             :  * Advance both the scan and the visited cursors.
     162             :  *
     163             :  * The inumber address space for a given filesystem is sparse, which means that
     164             :  * the scan cursor can jump a long ways in a single iter() call.  There are no
     165             :  * inodes in these sparse areas, so we must move the visited cursor forward at
     166             :  * the same time so that the scan user can receive live updates for inodes that
     167             :  * may get created once we release the AGI buffer.
     168             :  */
     169             : static inline void
     170    61987610 : xchk_iscan_move_cursor(
     171             :         struct xchk_iscan       *iscan,
     172             :         xfs_agnumber_t          agno,
     173             :         xfs_agino_t             agino)
     174             : {
     175    61987610 :         struct xfs_scrub        *sc = iscan->sc;
     176    61987610 :         struct xfs_mount        *mp = sc->mp;
     177    61987610 :         xfs_ino_t               cursor, visited;
     178             : 
     179    61987610 :         BUILD_BUG_ON(XFS_MAXINUMBER == NULLFSINO);
     180             : 
     181             :         /*
     182             :          * Special-case ino == 0 here so that we never set visited_ino to
     183             :          * NULLFSINO when wrapping around EOFS, for that will let through all
     184             :          * live updates.
     185             :          */
     186    61987610 :         cursor = XFS_AGINO_TO_INO(mp, agno, agino);
     187    61987610 :         if (cursor == 0)
     188             :                 visited = XFS_MAXINUMBER;
     189             :         else
     190    61857631 :                 visited = cursor - 1;
     191             : 
     192    61987610 :         mutex_lock(&iscan->lock);
     193    61987642 :         iscan->cursor_ino = cursor;
     194    61987642 :         iscan->__visited_ino = visited;
     195    61987642 :         trace_xchk_iscan_move_cursor(iscan);
     196    61987610 :         mutex_unlock(&iscan->lock);
     197    61986999 : }
     198             : 
     199             : /*
     200             :  * Prepare to return agno/agino to the iscan caller by moving the lastino
     201             :  * cursor to the previous inode.  Do this while we still hold the AGI so that
     202             :  * no other threads can create or delete inodes in this AG.
     203             :  */
     204             : static inline void
     205      259749 : xchk_iscan_finish(
     206             :         struct xchk_iscan       *iscan)
     207             : {
     208      259749 :         mutex_lock(&iscan->lock);
     209      259749 :         iscan->cursor_ino = NULLFSINO;
     210             : 
     211             :         /* All live updates will be applied from now on */
     212      259749 :         iscan->__visited_ino = NULLFSINO;
     213             : 
     214      259749 :         mutex_unlock(&iscan->lock);
     215      259747 : }
     216             : 
     217             : /* Mark an inode scan finished before we actually scan anything. */
     218             : void
     219           0 : xchk_iscan_finish_early(
     220             :         struct xchk_iscan       *iscan)
     221             : {
     222           0 :         ASSERT(iscan->cursor_ino == iscan->scan_start_ino);
     223           0 :         ASSERT(iscan->__visited_ino == iscan->scan_start_ino);
     224             : 
     225           0 :         xchk_iscan_finish(iscan);
     226           0 : }
     227             : 
     228             : /*
     229             :  * Advance ino to the next inode that the inobt thinks is allocated, being
     230             :  * careful to jump to the next AG if we've reached the right end of this AG's
     231             :  * inode btree.  Advancing ino effectively means that we've pushed the inode
     232             :  * scan forward, so set the iscan cursor to (ino - 1) so that our live update
     233             :  * predicates will track inode allocations in that part of the inode number
     234             :  * key space once we release the AGI buffer.
     235             :  *
     236             :  * Returns 1 if there's a new inode to examine, 0 if we've run out of inodes,
     237             :  * -ECANCELED if the live scan aborted, or the usual negative errno.
     238             :  */
     239             : STATIC int
     240    61574369 : xchk_iscan_advance(
     241             :         struct xchk_iscan       *iscan,
     242             :         struct xfs_perag        **pagp,
     243             :         struct xfs_buf          **agi_bpp,
     244             :         xfs_inofree_t           *allocmaskp,
     245             :         uint8_t                 *nr_inodesp)
     246             : {
     247    61574369 :         struct xfs_scrub        *sc = iscan->sc;
     248    61574369 :         struct xfs_mount        *mp = sc->mp;
     249    61574369 :         struct xfs_buf          *agi_bp;
     250    61574369 :         struct xfs_perag        *pag;
     251    61574369 :         xfs_agnumber_t          agno;
     252    61574369 :         xfs_agino_t             agino;
     253    61574369 :         int                     ret;
     254             : 
     255    61574369 :         ASSERT(iscan->cursor_ino >= iscan->__visited_ino);
     256             : 
     257    61987915 :         do {
     258    61987915 :                 if (xchk_iscan_aborted(iscan))
     259             :                         return -ECANCELED;
     260             : 
     261    61987654 :                 agno = XFS_INO_TO_AGNO(mp, iscan->cursor_ino);
     262    61987654 :                 pag = xfs_perag_get(mp, agno);
     263    61988037 :                 if (!pag)
     264             :                         return -ECANCELED;
     265             : 
     266    61988037 :                 ret = xfs_ialloc_read_agi(pag, sc->tp, &agi_bp);
     267    61987583 :                 if (ret)
     268           0 :                         goto out_pag;
     269             : 
     270    61987583 :                 agino = XFS_INO_TO_AGINO(mp, iscan->cursor_ino);
     271    61987583 :                 ret = xchk_iscan_find_next(iscan, agi_bp, pag, allocmaskp,
     272             :                                 &agino, nr_inodesp);
     273    61987530 :                 if (ret)
     274           0 :                         goto out_buf;
     275             : 
     276    61987530 :                 if (agino != NULLAGINO) {
     277             :                         /*
     278             :                          * Found the next inode in this AG, so return it along
     279             :                          * with the AGI buffer and the perag structure to
     280             :                          * ensure it cannot go away.
     281             :                          */
     282    61444141 :                         xchk_iscan_move_cursor(iscan, agno, agino);
     283    61443763 :                         *agi_bpp = agi_bp;
     284    61443763 :                         *pagp = pag;
     285    61443763 :                         return 1;
     286             :                 }
     287             : 
     288             :                 /*
     289             :                  * Did not find any more inodes in this AG, move on to the next
     290             :                  * AG.
     291             :                  */
     292      543389 :                 agno = (agno + 1) % mp->m_sb.sb_agcount;
     293      543389 :                 xchk_iscan_move_cursor(iscan, agno, 0);
     294      543383 :                 xfs_trans_brelse(sc->tp, agi_bp);
     295      543389 :                 xfs_perag_put(pag);
     296             : 
     297      543389 :                 trace_xchk_iscan_advance_ag(iscan);
     298      543389 :         } while (iscan->cursor_ino != iscan->scan_start_ino);
     299             : 
     300      129843 :         xchk_iscan_finish(iscan);
     301      129843 :         return 0;
     302             : 
     303             : out_buf:
     304           0 :         xfs_trans_brelse(sc->tp, agi_bp);
     305           0 : out_pag:
     306           0 :         xfs_perag_put(pag);
     307           0 :         return ret;
     308             : }
     309             : 
     310             : /*
     311             :  * Grabbing the inode failed, so we need to back up the scan and ask the caller
     312             :  * to try to _advance the scan again.  Returns -EBUSY if we've run out of retry
     313             :  * opportunities, -ECANCELED if the process has a fatal signal pending, or
     314             :  * -EAGAIN if we should try again.
     315             :  */
     316             : STATIC int
     317       72377 : xchk_iscan_iget_retry(
     318             :         struct xchk_iscan       *iscan,
     319             :         bool                    wait)
     320             : {
     321       72377 :         ASSERT(iscan->cursor_ino == iscan->__visited_ino + 1);
     322             : 
     323       72377 :         if (!iscan->iget_timeout ||
     324       69987 :             time_is_before_jiffies(iscan->__iget_deadline))
     325             :                 return -EBUSY;
     326             : 
     327       69987 :         if (wait) {
     328       70110 :                 unsigned long   relax;
     329             : 
     330             :                 /*
     331             :                  * Sleep for a period of time to let the rest of the system
     332             :                  * catch up.  If we return early, someone sent a kill signal to
     333             :                  * the calling process.
     334             :                  */
     335       70110 :                 relax = msecs_to_jiffies(iscan->iget_retry_delay);
     336       70110 :                 trace_xchk_iscan_iget_retry_wait(iscan);
     337             : 
     338      140090 :                 if (schedule_timeout_killable(relax) ||
     339       69971 :                     xchk_iscan_aborted(iscan))
     340          26 :                         return -ECANCELED;
     341             :         }
     342             : 
     343       69857 :         iscan->cursor_ino--;
     344       69857 :         return -EAGAIN;
     345             : }
     346             : 
     347             : /*
     348             :  * Grab an inode as part of an inode scan.  While scanning this inode, the
     349             :  * caller must ensure that no other threads can modify the inode until a call
     350             :  * to xchk_iscan_visit succeeds.
     351             :  *
     352             :  * Returns the number of incore inodes grabbed; -EAGAIN if the caller should
     353             :  * call again xchk_iscan_advance; -EBUSY if we couldn't grab an inode;
     354             :  * -ECANCELED if there's a fatal signal pending; or some other negative errno.
     355             :  */
     356             : STATIC int
     357    61443713 : xchk_iscan_iget(
     358             :         struct xchk_iscan       *iscan,
     359             :         struct xfs_perag        *pag,
     360             :         struct xfs_buf          *agi_bp,
     361             :         xfs_inofree_t           allocmask,
     362             :         uint8_t                 nr_inodes)
     363             : {
     364    61443713 :         struct xfs_scrub        *sc = iscan->sc;
     365    61443713 :         struct xfs_mount        *mp = sc->mp;
     366    61443713 :         xfs_ino_t               ino = iscan->cursor_ino;
     367    61443713 :         unsigned int            idx = 0;
     368    61443713 :         unsigned int            i;
     369    61443713 :         int                     error;
     370             : 
     371    61443713 :         ASSERT(iscan->__inodes[0] == NULL);
     372             : 
     373             :         /* Fill the first slot in the inode array. */
     374    61443713 :         error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
     375    61443713 :                         &iscan->__inodes[idx]);
     376             : 
     377    61443702 :         trace_xchk_iscan_iget(iscan, error);
     378             : 
     379    61444305 :         if (error == -ENOENT || error == -EAGAIN) {
     380       72500 :                 xfs_trans_brelse(sc->tp, agi_bp);
     381       72500 :                 xfs_perag_put(pag);
     382             : 
     383             :                 /*¬
     384             :                  * It's possible that this inode has lost all of its links but
     385             :                  * hasn't yet been inactivated.  If we don't have a transaction
     386             :                  * or it's not writable, flush the inodegc workers and wait.
     387             :                  * If we have a non-empty transaction, we must not block on
     388             :                  * inodegc, which allocates its own transactions.
     389             :                  */
     390       72500 :                 if (sc->tp && !(sc->tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
     391           0 :                         xfs_inodegc_push(mp);
     392             :                 else
     393       72500 :                         xfs_inodegc_flush(mp);
     394       72500 :                 return xchk_iscan_iget_retry(iscan, true);
     395             :         }
     396             : 
     397    61371805 :         if (error == -EINVAL) {
     398           0 :                 xfs_trans_brelse(sc->tp, agi_bp);
     399           0 :                 xfs_perag_put(pag);
     400             : 
     401             :                 /*
     402             :                  * We thought the inode was allocated, but the inode btree
     403             :                  * lookup failed, which means that it was freed since the last
     404             :                  * time we advanced the cursor.  Back up and try again.  This
     405             :                  * should never happen since still hold the AGI buffer from the
     406             :                  * inobt check, but we need to be careful about infinite loops.
     407             :                  */
     408           0 :                 return xchk_iscan_iget_retry(iscan, false);
     409             :         }
     410             : 
     411    61371805 :         if (error) {
     412           0 :                 xfs_trans_brelse(sc->tp, agi_bp);
     413           0 :                 xfs_perag_put(pag);
     414           0 :                 return error;
     415             :         }
     416    61371805 :         idx++;
     417    61371805 :         ino++;
     418    61371805 :         allocmask >>= 1;
     419             : 
     420             :         /*
     421             :          * Now that we've filled the first slot in __inodes, try to fill the
     422             :          * rest of the batch with consecutively ordered inodes.  to reduce the
     423             :          * number of _iter calls.  Make a bitmap of unallocated inodes from the
     424             :          * zeroes in the inuse bitmap; these inodes will not be scanned, but
     425             :          * the _want_live_update predicate will pass through all live updates.
     426             :          *
     427             :          * If we can't iget an allocated inode, stop and return what we have.
     428             :          */
     429    61371805 :         mutex_lock(&iscan->lock);
     430    61372060 :         iscan->__batch_ino = ino - 1;
     431    61372060 :         iscan->__skipped_inomask = 0;
     432    61372060 :         mutex_unlock(&iscan->lock);
     433             : 
     434  3576954877 :         for (i = 1; i < nr_inodes; i++, ino++, allocmask >>= 1) {
     435  3454279768 :                 if (!(allocmask & 1)) {
     436   283358667 :                         ASSERT(!(iscan->__skipped_inomask & (1ULL << i)));
     437             : 
     438   283358667 :                         mutex_lock(&iscan->lock);
     439   283362981 :                         iscan->cursor_ino = ino;
     440   283362981 :                         iscan->__skipped_inomask |= (1ULL << i);
     441   283362981 :                         mutex_unlock(&iscan->lock);
     442   283345281 :                         continue;
     443             :                 }
     444             : 
     445  3170921101 :                 ASSERT(iscan->__inodes[idx] == NULL);
     446             : 
     447  3170921101 :                 error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
     448  3170921101 :                                 &iscan->__inodes[idx]);
     449  3171189449 :                 if (error)
     450             :                         break;
     451             : 
     452  3171120844 :                 mutex_lock(&iscan->lock);
     453  3171488631 :                 iscan->cursor_ino = ino;
     454  3171488631 :                 mutex_unlock(&iscan->lock);
     455  3170865368 :                 idx++;
     456             :         }
     457             : 
     458    61371654 :         trace_xchk_iscan_iget_batch(sc->mp, iscan, nr_inodes, idx);
     459    61371739 :         xfs_trans_brelse(sc->tp, agi_bp);
     460    61372575 :         xfs_perag_put(pag);
     461    61372594 :         return idx;
     462             : }
     463             : 
     464             : /*
     465             :  * Advance the visit cursor to reflect skipped inodes beyond whatever we
     466             :  * scanned.
     467             :  */
     468             : STATIC void
     469    61504367 : xchk_iscan_finish_batch(
     470             :         struct xchk_iscan       *iscan)
     471             : {
     472    61504367 :         xfs_ino_t               highest_skipped;
     473             : 
     474    61504367 :         mutex_lock(&iscan->lock);
     475             : 
     476    61504893 :         if (iscan->__batch_ino != NULLFSINO) {
     477   122745192 :                 highest_skipped = iscan->__batch_ino +
     478    61372596 :                                         xfs_highbit64(iscan->__skipped_inomask);
     479    61372596 :                 iscan->__visited_ino = max(iscan->__visited_ino,
     480             :                                            highest_skipped);
     481             : 
     482    61372596 :                 trace_xchk_iscan_skip(iscan);
     483             :         }
     484             : 
     485    61504886 :         iscan->__batch_ino = NULLFSINO;
     486    61504886 :         iscan->__skipped_inomask = 0;
     487             : 
     488    61504886 :         mutex_unlock(&iscan->lock);
     489    61504792 : }
     490             : 
     491             : /*
     492             :  * Advance the inode scan cursor to the next allocated inode and return up to
     493             :  * 64 consecutive allocated inodes starting with the cursor position.
     494             :  */
     495             : STATIC int
     496    61504708 : xchk_iscan_iter_batch(
     497             :         struct xchk_iscan       *iscan)
     498             : {
     499    61504708 :         struct xfs_scrub        *sc = iscan->sc;
     500    61504708 :         int                     ret;
     501             : 
     502    61504708 :         xchk_iscan_finish_batch(iscan);
     503             : 
     504    61504811 :         if (iscan->iget_timeout)
     505   103604973 :                 iscan->__iget_deadline = jiffies +
     506             :                                          msecs_to_jiffies(iscan->iget_timeout);
     507             : 
     508    61574207 :         do {
     509    61574207 :                 struct xfs_buf  *agi_bp = NULL;
     510    61574207 :                 struct xfs_perag *pag = NULL;
     511    61574207 :                 xfs_inofree_t   allocmask = 0;
     512    61574207 :                 uint8_t         nr_inodes = 0;
     513             : 
     514    61574207 :                 ret = xchk_iscan_advance(iscan, &pag, &agi_bp, &allocmask,
     515             :                                 &nr_inodes);
     516    61573674 :                 if (ret != 1)
     517      129865 :                         return ret;
     518             : 
     519    61443809 :                 if (xchk_iscan_aborted(iscan)) {
     520           0 :                         xfs_trans_brelse(sc->tp, agi_bp);
     521           0 :                         xfs_perag_put(pag);
     522           0 :                         ret = -ECANCELED;
     523           0 :                         break;
     524             :                 }
     525             : 
     526    61443701 :                 ret = xchk_iscan_iget(iscan, pag, agi_bp, allocmask, nr_inodes);
     527    61444102 :         } while (ret == -EAGAIN);
     528             : 
     529             :         return ret;
     530             : }
     531             : 
     532             : /*
     533             :  * Advance the inode scan cursor to the next allocated inode and return the
     534             :  * incore inode structure associated with it.
     535             :  *
     536             :  * Returns 1 if there's a new inode to examine, 0 if we've run out of inodes,
     537             :  * -ECANCELED if the live scan aborted, -EBUSY if the incore inode could not be
     538             :  * grabbed, or the usual negative errno.
     539             :  *
     540             :  * If the function returns -EBUSY and the caller can handle skipping an inode,
     541             :  * it may call this function again to continue the scan with the next allocated
     542             :  * inode.
     543             :  */
     544             : int
     545  3233053237 : xchk_iscan_iter(
     546             :         struct xchk_iscan       *iscan,
     547             :         struct xfs_inode        **ipp)
     548             : {
     549  3233053237 :         unsigned int            i;
     550  3233053237 :         int                     error;
     551             : 
     552             :         /* Find a cached inode, or go get another batch. */
     553 97214666415 :         for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
     554 97153161590 :                 if (iscan->__inodes[i])
     555  3171548412 :                         goto foundit;
     556             :         }
     557             : 
     558    61504825 :         error = xchk_iscan_iter_batch(iscan);
     559    61504234 :         if (error <= 0)
     560             :                 return error;
     561             : 
     562    61371953 :         ASSERT(iscan->__inodes[0] != NULL);
     563             :         i = 0;
     564             : 
     565  3232920365 : foundit:
     566             :         /* Give the caller our reference. */
     567  3232920365 :         *ipp = iscan->__inodes[i];
     568  3232920365 :         iscan->__inodes[i] = NULL;
     569  3232920365 :         return 1;
     570             : }
     571             : 
     572             : /* Clean up an xfs_iscan_iter call by dropping any inodes that we still hold. */
     573             : void
     574      259803 : xchk_iscan_iter_finish(
     575             :         struct xchk_iscan       *iscan)
     576             : {
     577      259803 :         struct xfs_scrub        *sc = iscan->sc;
     578      259803 :         unsigned int            i;
     579             : 
     580    16886724 :         for (i = 0; i < XFS_INODES_PER_CHUNK; i++){
     581    16626910 :                 if (iscan->__inodes[i]) {
     582         587 :                         xchk_irele(sc, iscan->__inodes[i]);
     583         598 :                         iscan->__inodes[i] = NULL;
     584             :                 }
     585             :         }
     586      259814 : }
     587             : 
     588             : /* Mark this inode scan finished and release resources. */
     589             : void
     590      129902 : xchk_iscan_teardown(
     591             :         struct xchk_iscan       *iscan)
     592             : {
     593      129902 :         xchk_iscan_iter_finish(iscan);
     594      129907 :         xchk_iscan_finish(iscan);
     595      129903 :         mutex_destroy(&iscan->lock);
     596      129906 : }
     597             : 
     598             : /* Pick an AG from which to start a scan. */
     599             : static inline xfs_ino_t
     600      129906 : xchk_iscan_rotor(
     601             :         struct xfs_mount        *mp)
     602             : {
     603      129906 :         static atomic_t         agi_rotor;
     604      129906 :         unsigned int            r = atomic_inc_return(&agi_rotor) - 1;
     605             : 
     606             :         /*
     607             :          * Rotoring *backwards* through the AGs, so we add one here before
     608             :          * subtracting from the agcount to arrive at an AG number.
     609             :          */
     610      129906 :         r = (r % mp->m_sb.sb_agcount) + 1;
     611             : 
     612      129906 :         return XFS_AGINO_TO_INO(mp, mp->m_sb.sb_agcount - r, 0);
     613             : }
     614             : 
     615             : /*
     616             :  * Set ourselves up to start an inode scan.  If the @iget_timeout and
     617             :  * @iget_retry_delay parameters are set, the scan will try to iget each inode
     618             :  * for @iget_timeout milliseconds.  If an iget call indicates that the inode is
     619             :  * waiting to be inactivated, the CPU will relax for @iget_retry_delay
     620             :  * milliseconds after pushing the inactivation workers.
     621             :  */
     622             : void
     623      129907 : xchk_iscan_start(
     624             :         struct xfs_scrub        *sc,
     625             :         unsigned int            iget_timeout,
     626             :         unsigned int            iget_retry_delay,
     627             :         struct xchk_iscan       *iscan)
     628             : {
     629      129907 :         xfs_ino_t               start_ino;
     630             : 
     631      129907 :         start_ino = xchk_iscan_rotor(sc->mp);
     632             : 
     633      129906 :         iscan->__batch_ino = NULLFSINO;
     634      129906 :         iscan->__skipped_inomask = 0;
     635             : 
     636      129906 :         iscan->sc = sc;
     637      129906 :         clear_bit(XCHK_ISCAN_OPSTATE_ABORTED, &iscan->__opstate);
     638      129906 :         iscan->iget_timeout = iget_timeout;
     639      129906 :         iscan->iget_retry_delay = iget_retry_delay;
     640      129906 :         iscan->__visited_ino = start_ino;
     641      129906 :         iscan->cursor_ino = start_ino;
     642      129906 :         iscan->scan_start_ino = start_ino;
     643      129906 :         mutex_init(&iscan->lock);
     644      129905 :         memset(iscan->__inodes, 0, sizeof(iscan->__inodes));
     645             : 
     646      129905 :         trace_xchk_iscan_start(iscan, start_ino);
     647      129904 : }
     648             : 
     649             : /*
     650             :  * Mark this inode as having been visited.  Callers must hold a sufficiently
     651             :  * exclusive lock on the inode to prevent concurrent modifications.
     652             :  */
     653             : void
     654  3229491599 : xchk_iscan_mark_visited(
     655             :         struct xchk_iscan       *iscan,
     656             :         struct xfs_inode        *ip)
     657             : {
     658  3229491599 :         mutex_lock(&iscan->lock);
     659  3231082388 :         iscan->__visited_ino = ip->i_ino;
     660  3231082388 :         trace_xchk_iscan_visit(iscan);
     661  3231406839 :         mutex_unlock(&iscan->lock);
     662  3229860622 : }
     663             : 
     664             : /*
     665             :  * Did we skip this inode because it wasn't allocated when we loaded the batch?
     666             :  * If so, it is newly allocated and will not be scanned.  All live updates to
     667             :  * this inode must be passed to the caller to maintain scan correctness.
     668             :  */
     669             : static inline bool
     670     8568484 : xchk_iscan_skipped(
     671             :         const struct xchk_iscan *iscan,
     672             :         xfs_ino_t               ino)
     673             : {
     674     8568484 :         if (iscan->__batch_ino == NULLFSINO)
     675             :                 return false;
     676     3300443 :         if (ino < iscan->__batch_ino)
     677             :                 return false;
     678     1702444 :         if (ino >= iscan->__batch_ino + XFS_INODES_PER_CHUNK)
     679             :                 return false;
     680             : 
     681       12827 :         return iscan->__skipped_inomask & (1ULL << (ino - iscan->__batch_ino));
     682             : }
     683             : 
     684             : /*
     685             :  * Do we need a live update for this inode?  This is true if the scanner thread
     686             :  * has visited this inode and the scan hasn't been aborted due to errors.
     687             :  * Callers must hold a sufficiently exclusive lock on the inode to prevent
     688             :  * scanners from reading any inode metadata.
     689             :  */
     690             : bool
     691    17994261 : xchk_iscan_want_live_update(
     692             :         struct xchk_iscan       *iscan,
     693             :         xfs_ino_t               ino)
     694             : {
     695    17994261 :         bool                    ret = false;
     696             : 
     697    17994261 :         if (xchk_iscan_aborted(iscan))
     698             :                 return false;
     699             : 
     700    17979544 :         mutex_lock(&iscan->lock);
     701             : 
     702    17981240 :         trace_xchk_iscan_want_live_update(iscan, ino);
     703             : 
     704             :         /* Scan is finished, caller should receive all updates. */
     705    17981240 :         if (iscan->__visited_ino == NULLFSINO) {
     706     9408626 :                 ret = true;
     707     9408626 :                 goto unlock;
     708             :         }
     709             : 
     710             :         /*
     711             :          * No inodes have been visited yet, so the visited cursor points at the
     712             :          * start of the scan range.  The caller should not receive any updates.
     713             :          */
     714     8572614 :         if (iscan->scan_start_ino == iscan->__visited_ino) {
     715        4130 :                 ret = false;
     716        4130 :                 goto unlock;
     717             :         }
     718             : 
     719             :         /*
     720             :          * This inode was not allocated at the time of the iscan batch.
     721             :          * The caller should receive all updates.
     722             :          */
     723     8568484 :         if (xchk_iscan_skipped(iscan, ino)) {
     724         517 :                 ret = true;
     725         517 :                 goto unlock;
     726             :         }
     727             : 
     728             :         /*
     729             :          * The visited cursor hasn't yet wrapped around the end of the FS.  If
     730             :          * @ino is inside the starred range, the caller should receive updates:
     731             :          *
     732             :          * 0 ------------ S ************ V ------------ EOFS
     733             :          */
     734     8567967 :         if (iscan->scan_start_ino <= iscan->__visited_ino) {
     735     5326492 :                 if (ino >= iscan->scan_start_ino &&
     736             :                     ino <= iscan->__visited_ino)
     737     2013576 :                         ret = true;
     738             : 
     739     5326492 :                 goto unlock;
     740             :         }
     741             : 
     742             :         /*
     743             :          * The visited cursor wrapped around the end of the FS.  If @ino is
     744             :          * inside the starred range, the caller should receive updates:
     745             :          *
     746             :          * 0 ************ V ------------ S ************ EOFS
     747             :          */
     748     3241475 :         if (ino >= iscan->scan_start_ino || ino <= iscan->__visited_ino)
     749     2278541 :                 ret = true;
     750             : 
     751      962934 : unlock:
     752    17981240 :         mutex_unlock(&iscan->lock);
     753    17981240 :         return ret;
     754             : }

Generated by: LCOV version 1.14