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-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 91 109 83.5 %
Date: 2023-07-31 20:08:07 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       24889 : xchk_setup_rtsummary(
      39             :         struct xfs_scrub        *sc)
      40             : {
      41       24889 :         struct xfs_mount        *mp = sc->mp;
      42       24889 :         char                    *descr;
      43       24889 :         size_t                  bufsize = mp->m_sb.sb_blocksize;
      44       24889 :         unsigned int            resblks = 0;
      45       24889 :         int                     error;
      46             : 
      47       49778 :         if (xchk_could_repair(sc)) {
      48         949 :                 error = xrep_setup_rtsummary(sc, &resblks, &bufsize);
      49         949 :                 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       23940 :         descr = xchk_xfile_descr(sc, "realtime summary file");
      58       23940 :         error = xfile_create(descr, mp->m_rsumsize, &sc->xfile);
      59       23940 :         kfree(descr);
      60       23940 :         if (error)
      61             :                 return error;
      62             : 
      63       23940 :         error = xchk_trans_alloc(sc, resblks);
      64       23940 :         if (error)
      65             :                 return error;
      66             : 
      67             :         /* Allocate a memory buffer for the summary comparison. */
      68       23940 :         sc->buf = kvmalloc(bufsize, XCHK_GFP_FLAGS);
      69       23940 :         if (!sc->buf)
      70             :                 return -ENOMEM;
      71             : 
      72       23940 :         error = xchk_install_live_inode(sc, mp->m_rsumip);
      73       23940 :         if (error)
      74             :                 return error;
      75             : 
      76       23940 :         error = xchk_ino_dqattach(sc);
      77       23940 :         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       23940 :         xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
      87       23940 :         xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
      88       23940 :         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    12760805 :         return xfile_obj_load(sc->xfile, info, sizeof(xfs_suminfo_t),
     100    12760805 :                         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    12760805 :         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      167544 :         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    12760805 : 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    12760805 :         struct xfs_scrub                *sc = priv;
     133    12760805 :         xfs_fileoff_t                   rbmoff;
     134    12760805 :         xfs_rtblock_t                   rtbno;
     135    12760805 :         xfs_filblks_t                   rtlen;
     136    12760805 :         xchk_rtsumoff_t                 offs;
     137    12760805 :         unsigned int                    lenlog;
     138    12760805 :         xfs_suminfo_t                   v = 0;
     139    12760805 :         int                             error = 0;
     140             : 
     141    12760805 :         if (xchk_should_terminate(sc, &error))
     142           0 :                 return error;
     143             : 
     144             :         /* Compute the relevant location in the rtsum file. */
     145    12760805 :         rbmoff = XFS_BITTOBLOCK(mp, rec->ar_startext);
     146    12760805 :         lenlog = XFS_RTBLOCKLOG(rec->ar_extcount);
     147    12760805 :         offs = XFS_SUMOFFS(mp, lenlog, rbmoff);
     148             : 
     149    12760805 :         rtbno = rec->ar_startext * mp->m_sb.sb_rextsize;
     150    12760805 :         rtlen = rec->ar_extcount * mp->m_sb.sb_rextsize;
     151             : 
     152    12760805 :         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    12760805 :         error = xfsum_load(sc, offs, &v);
     159    12760805 :         if (error)
     160             :                 return error;
     161             : 
     162    12760805 :         v++;
     163    12760805 :         trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
     164             :                         lenlog, offs, v);
     165             : 
     166    12760805 :         return xfsum_store(sc, offs, v);
     167             : }
     168             : 
     169             : /* Compute the realtime summary from the realtime bitmap. */
     170             : STATIC int
     171       23940 : xchk_rtsum_compute(
     172             :         struct xfs_scrub        *sc)
     173             : {
     174       23940 :         struct xfs_mount        *mp = sc->mp;
     175       23940 :         unsigned long long      rtbmp_bytes;
     176             : 
     177             :         /* If the bitmap size doesn't match the computed size, bail. */
     178       23940 :         rtbmp_bytes = howmany_64(mp->m_sb.sb_rextents, NBBY);
     179       23940 :         if (roundup_64(rtbmp_bytes, mp->m_sb.sb_blocksize) !=
     180       23940 :                         mp->m_rbmip->i_disk_size)
     181             :                 return -EFSCORRUPTED;
     182             : 
     183       23940 :         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       23940 : xchk_rtsum_compare(
     190             :         struct xfs_scrub        *sc)
     191             : {
     192       23940 :         struct xfs_mount        *mp = sc->mp;
     193       23940 :         struct xfs_buf          *bp;
     194       23940 :         struct xfs_bmbt_irec    map;
     195       23940 :         xfs_fileoff_t           off;
     196       23940 :         xchk_rtsumoff_t         sumoff = 0;
     197       23940 :         int                     nmap;
     198             : 
     199      191484 :         for (off = 0; off < XFS_B_TO_FSB(mp, mp->m_rsumsize); off++) {
     200      167544 :                 int             error = 0;
     201             : 
     202      167544 :                 if (xchk_should_terminate(sc, &error))
     203           0 :                         return error;
     204      167544 :                 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
     205             :                         return 0;
     206             : 
     207             :                 /* Make sure we have a written extent. */
     208      167544 :                 nmap = 1;
     209      167544 :                 error = xfs_bmapi_read(mp->m_rsumip, off, 1, &map, &nmap,
     210             :                                 XFS_DATA_FORK);
     211      167544 :                 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
     212           0 :                         return error;
     213             : 
     214      167544 :                 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      167544 :                 error = xfs_rtbuf_get(mp, sc->tp, off, 1, &bp);
     221      167544 :                 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      167544 :                 error = xfsum_copyout(sc, sumoff, sc->buf, mp->m_blockwsize);
     226      167544 :                 if (error) {
     227           0 :                         xfs_trans_brelse(sc->tp, bp);
     228           0 :                         return error;
     229             :                 }
     230             : 
     231      167544 :                 if (memcmp(bp->b_addr, sc->buf,
     232      167544 :                                         mp->m_blockwsize << XFS_WORDLOG) != 0)
     233           0 :                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
     234             : 
     235      167544 :                 xfs_trans_brelse(sc->tp, bp);
     236      167544 :                 sumoff += mp->m_blockwsize;
     237             :         }
     238             : 
     239             :         return 0;
     240             : }
     241             : 
     242             : /* Scrub the realtime summary. */
     243             : int
     244       23940 : xchk_rtsummary(
     245             :         struct xfs_scrub        *sc)
     246             : {
     247       23940 :         struct xfs_mount        *mp = sc->mp;
     248       23940 :         int                     error = 0;
     249             : 
     250             :         /* Invoke the fork scrubber. */
     251       23940 :         error = xchk_metadata_inode_forks(sc);
     252       23940 :         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       23940 :         error = xchk_rtsum_compute(sc);
     257       23940 :         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       23940 :         if (error)
     267           0 :                 goto out_rbm;
     268             : 
     269             :         /* Does the computed summary file match the actual rtsummary file? */
     270       23940 :         error = xchk_rtsum_compare(sc);
     271             : 
     272       23940 : out_rbm:
     273             :         /* Unlock the rtbitmap since we're done with it. */
     274       23940 :         xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
     275       23940 :         return error;
     276             : }

Generated by: LCOV version 1.14