LCOV - code coverage report
Current view: top level - fs/xfs/scrub - rtsummary.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 91 109 83.5 %
Date: 2023-07-31 20:08:12 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2017-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_btree.h"
      13             : #include "xfs_inode.h"
      14             : #include "xfs_log_format.h"
      15             : #include "xfs_trans.h"
      16             : #include "xfs_rtalloc.h"
      17             : #include "xfs_bit.h"
      18             : #include "xfs_bmap.h"
      19             : #include "scrub/scrub.h"
      20             : #include "scrub/common.h"
      21             : #include "scrub/trace.h"
      22             : #include "scrub/xfile.h"
      23             : #include "scrub/repair.h"
      24             : #include "scrub/rtsummary.h"
      25             : 
      26             : /*
      27             :  * Realtime Summary
      28             :  * ================
      29             :  *
      30             :  * We check the realtime summary by scanning the realtime bitmap file to create
      31             :  * a new summary file incore, and then we compare the computed version against
      32             :  * the ondisk version.  We use the 'xfile' functionality to store this
      33             :  * (potentially large) amount of data in pageable memory.
      34             :  */
      35             : 
      36             : /* Set us up to check the rtsummary file. */
      37             : int
      38       39792 : xchk_setup_rtsummary(
      39             :         struct xfs_scrub        *sc)
      40             : {
      41       39792 :         struct xfs_mount        *mp = sc->mp;
      42       39792 :         char                    *descr;
      43       39792 :         size_t                  bufsize = mp->m_sb.sb_blocksize;
      44       39792 :         unsigned int            resblks = 0;
      45       39792 :         int                     error;
      46             : 
      47       79584 :         if (xchk_could_repair(sc)) {
      48        1764 :                 error = xrep_setup_rtsummary(sc, &resblks, &bufsize);
      49        1764 :                 if (error)
      50             :                         return error;
      51             :         }
      52             : 
      53             :         /*
      54             :          * Create an xfile to construct a new rtsummary file.  The xfile allows
      55             :          * us to avoid pinning kernel memory for this purpose.
      56             :          */
      57       38028 :         descr = xchk_xfile_descr(sc, "realtime summary file");
      58       38028 :         error = xfile_create(descr, mp->m_rsumsize, &sc->xfile);
      59       38028 :         kfree(descr);
      60       38028 :         if (error)
      61             :                 return error;
      62             : 
      63       38028 :         error = xchk_trans_alloc(sc, resblks);
      64       38028 :         if (error)
      65             :                 return error;
      66             : 
      67             :         /* Allocate a memory buffer for the summary comparison. */
      68       38028 :         sc->buf = kvmalloc(bufsize, XCHK_GFP_FLAGS);
      69       38028 :         if (!sc->buf)
      70             :                 return -ENOMEM;
      71             : 
      72       38028 :         error = xchk_install_live_inode(sc, mp->m_rsumip);
      73       38028 :         if (error)
      74             :                 return error;
      75             : 
      76       38028 :         error = xchk_ino_dqattach(sc);
      77       38028 :         if (error)
      78             :                 return error;
      79             : 
      80             :         /*
      81             :          * Locking order requires us to take the rtbitmap first.  We must be
      82             :          * careful to unlock it ourselves when we are done with the rtbitmap
      83             :          * file since the scrub infrastructure won't do that for us.  Only
      84             :          * then we can lock the rtsummary inode.
      85             :          */
      86       38028 :         xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
      87       38028 :         xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
      88       38028 :         return 0;
      89             : }
      90             : 
      91             : /* Helper functions to record suminfo words in an xfile. */
      92             : 
      93             : static inline int
      94             : xfsum_load(
      95             :         struct xfs_scrub        *sc,
      96             :         xchk_rtsumoff_t         sumoff,
      97             :         xfs_suminfo_t           *info)
      98             : {
      99    16643782 :         return xfile_obj_load(sc->xfile, info, sizeof(xfs_suminfo_t),
     100    16643782 :                         sumoff << XFS_WORDLOG);
     101             : }
     102             : 
     103             : static inline int
     104             : xfsum_store(
     105             :         struct xfs_scrub        *sc,
     106             :         xchk_rtsumoff_t         sumoff,
     107             :         const xfs_suminfo_t     info)
     108             : {
     109    16643782 :         return xfile_obj_store(sc->xfile, &info, sizeof(xfs_suminfo_t),
     110             :                         sumoff << XFS_WORDLOG);
     111             : }
     112             : 
     113             : inline int
     114           0 : xfsum_copyout(
     115             :         struct xfs_scrub        *sc,
     116             :         xchk_rtsumoff_t         sumoff,
     117             :         xfs_suminfo_t           *info,
     118             :         unsigned int            nr_words)
     119             : {
     120      242007 :         return xfile_obj_load(sc->xfile, info, nr_words << XFS_WORDLOG,
     121           0 :                         sumoff << XFS_WORDLOG);
     122             : }
     123             : 
     124             : /* Update the summary file to reflect the free extent that we've accumulated. */
     125             : STATIC int
     126    16643782 : xchk_rtsum_record_free(
     127             :         struct xfs_mount                *mp,
     128             :         struct xfs_trans                *tp,
     129             :         const struct xfs_rtalloc_rec    *rec,
     130             :         void                            *priv)
     131             : {
     132    16643782 :         struct xfs_scrub                *sc = priv;
     133    16643782 :         xfs_fileoff_t                   rbmoff;
     134    16643782 :         xfs_rtblock_t                   rtbno;
     135    16643782 :         xfs_filblks_t                   rtlen;
     136    16643782 :         xchk_rtsumoff_t                 offs;
     137    16643782 :         unsigned int                    lenlog;
     138    16643782 :         xfs_suminfo_t                   v = 0;
     139    16643782 :         int                             error = 0;
     140             : 
     141    16643782 :         if (xchk_should_terminate(sc, &error))
     142           0 :                 return error;
     143             : 
     144             :         /* Compute the relevant location in the rtsum file. */
     145    16643782 :         rbmoff = XFS_BITTOBLOCK(mp, rec->ar_startext);
     146    16643782 :         lenlog = XFS_RTBLOCKLOG(rec->ar_extcount);
     147    16643782 :         offs = XFS_SUMOFFS(mp, lenlog, rbmoff);
     148             : 
     149    16643782 :         rtbno = rec->ar_startext * mp->m_sb.sb_rextsize;
     150    16643782 :         rtlen = rec->ar_extcount * mp->m_sb.sb_rextsize;
     151             : 
     152    16643782 :         if (!xfs_verify_rtext(mp, rtbno, rtlen)) {
     153           0 :                 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
     154           0 :                 return -EFSCORRUPTED;
     155             :         }
     156             : 
     157             :         /* Bump the summary count. */
     158    16643782 :         error = xfsum_load(sc, offs, &v);
     159    16643782 :         if (error)
     160             :                 return error;
     161             : 
     162    16643782 :         v++;
     163    16643782 :         trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
     164             :                         lenlog, offs, v);
     165             : 
     166    16643782 :         return xfsum_store(sc, offs, v);
     167             : }
     168             : 
     169             : /* Compute the realtime summary from the realtime bitmap. */
     170             : STATIC int
     171       38028 : xchk_rtsum_compute(
     172             :         struct xfs_scrub        *sc)
     173             : {
     174       38028 :         struct xfs_mount        *mp = sc->mp;
     175       38028 :         unsigned long long      rtbmp_bytes;
     176             : 
     177             :         /* If the bitmap size doesn't match the computed size, bail. */
     178       38028 :         rtbmp_bytes = howmany_64(mp->m_sb.sb_rextents, NBBY);
     179       38028 :         if (roundup_64(rtbmp_bytes, mp->m_sb.sb_blocksize) !=
     180       38028 :                         mp->m_rbmip->i_disk_size)
     181             :                 return -EFSCORRUPTED;
     182             : 
     183       38028 :         return xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtsum_record_free,
     184             :                         sc);
     185             : }
     186             : 
     187             : /* Compare the rtsummary file against the one we computed. */
     188             : STATIC int
     189       38028 : xchk_rtsum_compare(
     190             :         struct xfs_scrub        *sc)
     191             : {
     192       38028 :         struct xfs_mount        *mp = sc->mp;
     193       38028 :         struct xfs_buf          *bp;
     194       38028 :         struct xfs_bmbt_irec    map;
     195       38028 :         xfs_fileoff_t           off;
     196       38028 :         xchk_rtsumoff_t         sumoff = 0;
     197       38028 :         int                     nmap;
     198             : 
     199      280035 :         for (off = 0; off < XFS_B_TO_FSB(mp, mp->m_rsumsize); off++) {
     200      242007 :                 int             error = 0;
     201             : 
     202      242007 :                 if (xchk_should_terminate(sc, &error))
     203           0 :                         return error;
     204      242007 :                 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
     205             :                         return 0;
     206             : 
     207             :                 /* Make sure we have a written extent. */
     208      242007 :                 nmap = 1;
     209      242007 :                 error = xfs_bmapi_read(mp->m_rsumip, off, 1, &map, &nmap,
     210             :                                 XFS_DATA_FORK);
     211      242007 :                 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
     212           0 :                         return error;
     213             : 
     214      242007 :                 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
     215           0 :                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
     216           0 :                         return 0;
     217             :                 }
     218             : 
     219             :                 /* Read a block's worth of ondisk rtsummary file. */
     220      242007 :                 error = xfs_rtbuf_get(mp, sc->tp, off, 1, &bp);
     221      242007 :                 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
     222           0 :                         return error;
     223             : 
     224             :                 /* Read a block's worth of computed rtsummary file. */
     225      242007 :                 error = xfsum_copyout(sc, sumoff, sc->buf, mp->m_blockwsize);
     226      242007 :                 if (error) {
     227           0 :                         xfs_trans_brelse(sc->tp, bp);
     228           0 :                         return error;
     229             :                 }
     230             : 
     231      242007 :                 if (memcmp(bp->b_addr, sc->buf,
     232      242007 :                                         mp->m_blockwsize << XFS_WORDLOG) != 0)
     233           0 :                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
     234             : 
     235      242007 :                 xfs_trans_brelse(sc->tp, bp);
     236      242007 :                 sumoff += mp->m_blockwsize;
     237             :         }
     238             : 
     239             :         return 0;
     240             : }
     241             : 
     242             : /* Scrub the realtime summary. */
     243             : int
     244       38028 : xchk_rtsummary(
     245             :         struct xfs_scrub        *sc)
     246             : {
     247       38028 :         struct xfs_mount        *mp = sc->mp;
     248       38028 :         int                     error = 0;
     249             : 
     250             :         /* Invoke the fork scrubber. */
     251       38028 :         error = xchk_metadata_inode_forks(sc);
     252       38028 :         if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
     253           0 :                 goto out_rbm;
     254             : 
     255             :         /* Construct the new summary file from the rtbitmap. */
     256       38028 :         error = xchk_rtsum_compute(sc);
     257       38028 :         if (error == -EFSCORRUPTED) {
     258             :                 /*
     259             :                  * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
     260             :                  * error since we're checking the summary file.
     261             :                  */
     262           0 :                 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
     263           0 :                 error = 0;
     264           0 :                 goto out_rbm;
     265             :         }
     266       38028 :         if (error)
     267           0 :                 goto out_rbm;
     268             : 
     269             :         /* Does the computed summary file match the actual rtsummary file? */
     270       38028 :         error = xchk_rtsum_compare(sc);
     271             : 
     272       38028 : out_rbm:
     273             :         /* Unlock the rtbitmap since we're done with it. */
     274       38028 :         xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
     275       38028 :         return error;
     276             : }

Generated by: LCOV version 1.14