Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2017 Oracle.  All Rights Reserved.
0004  * Author: Darrick J. Wong <darrick.wong@oracle.com>
0005  */
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_trans_resv.h"
0011 #include "xfs_mount.h"
0012 #include "xfs_log_format.h"
0013 #include "xfs_trans.h"
0014 #include "xfs_rtalloc.h"
0015 #include "xfs_inode.h"
0016 #include "xfs_bmap.h"
0017 #include "scrub/scrub.h"
0018 #include "scrub/common.h"
0019 
0020 /* Set us up with the realtime metadata locked. */
0021 int
0022 xchk_setup_rt(
0023     struct xfs_scrub    *sc)
0024 {
0025     int         error;
0026 
0027     error = xchk_setup_fs(sc);
0028     if (error)
0029         return error;
0030 
0031     sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
0032     sc->ip = sc->mp->m_rbmip;
0033     xfs_ilock(sc->ip, sc->ilock_flags);
0034 
0035     return 0;
0036 }
0037 
0038 /* Realtime bitmap. */
0039 
0040 /* Scrub a free extent record from the realtime bitmap. */
0041 STATIC int
0042 xchk_rtbitmap_rec(
0043     struct xfs_mount    *mp,
0044     struct xfs_trans    *tp,
0045     const struct xfs_rtalloc_rec *rec,
0046     void            *priv)
0047 {
0048     struct xfs_scrub    *sc = priv;
0049     xfs_rtblock_t       startblock;
0050     xfs_rtblock_t       blockcount;
0051 
0052     startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
0053     blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
0054 
0055     if (!xfs_verify_rtext(mp, startblock, blockcount))
0056         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
0057     return 0;
0058 }
0059 
0060 /* Make sure the entire rtbitmap file is mapped with written extents. */
0061 STATIC int
0062 xchk_rtbitmap_check_extents(
0063     struct xfs_scrub    *sc)
0064 {
0065     struct xfs_mount    *mp = sc->mp;
0066     struct xfs_bmbt_irec    map;
0067     xfs_rtblock_t       off;
0068     int         nmap;
0069     int         error = 0;
0070 
0071     for (off = 0; off < mp->m_sb.sb_rbmblocks;) {
0072         if (xchk_should_terminate(sc, &error) ||
0073             (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
0074             break;
0075 
0076         /* Make sure we have a written extent. */
0077         nmap = 1;
0078         error = xfs_bmapi_read(mp->m_rbmip, off,
0079                 mp->m_sb.sb_rbmblocks - off, &map, &nmap,
0080                 XFS_DATA_FORK);
0081         if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
0082             break;
0083 
0084         if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
0085             xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
0086             break;
0087         }
0088 
0089         off += map.br_blockcount;
0090     }
0091 
0092     return error;
0093 }
0094 
0095 /* Scrub the realtime bitmap. */
0096 int
0097 xchk_rtbitmap(
0098     struct xfs_scrub    *sc)
0099 {
0100     int         error;
0101 
0102     /* Is the size of the rtbitmap correct? */
0103     if (sc->mp->m_rbmip->i_disk_size !=
0104         XFS_FSB_TO_B(sc->mp, sc->mp->m_sb.sb_rbmblocks)) {
0105         xchk_ino_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
0106         return 0;
0107     }
0108 
0109     /* Invoke the fork scrubber. */
0110     error = xchk_metadata_inode_forks(sc);
0111     if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
0112         return error;
0113 
0114     error = xchk_rtbitmap_check_extents(sc);
0115     if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
0116         return error;
0117 
0118     error = xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtbitmap_rec, sc);
0119     if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
0120         goto out;
0121 
0122 out:
0123     return error;
0124 }
0125 
0126 /* Scrub the realtime summary. */
0127 int
0128 xchk_rtsummary(
0129     struct xfs_scrub    *sc)
0130 {
0131     struct xfs_inode    *rsumip = sc->mp->m_rsumip;
0132     struct xfs_inode    *old_ip = sc->ip;
0133     uint            old_ilock_flags = sc->ilock_flags;
0134     int         error = 0;
0135 
0136     /*
0137      * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
0138      * rt summary ip in compliance with the rt inode locking rules.
0139      *
0140      * Since we switch sc->ip to rsumip we have to save the old ilock
0141      * flags so that we don't mix up the inode state that @sc tracks.
0142      */
0143     sc->ip = rsumip;
0144     sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
0145     xfs_ilock(sc->ip, sc->ilock_flags);
0146 
0147     /* Invoke the fork scrubber. */
0148     error = xchk_metadata_inode_forks(sc);
0149     if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
0150         goto out;
0151 
0152     /* XXX: implement this some day */
0153     xchk_set_incomplete(sc);
0154 out:
0155     /* Switch back to the rtbitmap inode and lock flags. */
0156     xfs_iunlock(sc->ip, sc->ilock_flags);
0157     sc->ilock_flags = old_ilock_flags;
0158     sc->ip = old_ip;
0159     return error;
0160 }
0161 
0162 
0163 /* xref check that the extent is not free in the rtbitmap */
0164 void
0165 xchk_xref_is_used_rt_space(
0166     struct xfs_scrub    *sc,
0167     xfs_rtblock_t       fsbno,
0168     xfs_extlen_t        len)
0169 {
0170     xfs_rtblock_t       startext;
0171     xfs_rtblock_t       endext;
0172     xfs_rtblock_t       extcount;
0173     bool            is_free;
0174     int         error;
0175 
0176     if (xchk_skip_xref(sc->sm))
0177         return;
0178 
0179     startext = fsbno;
0180     endext = fsbno + len - 1;
0181     do_div(startext, sc->mp->m_sb.sb_rextsize);
0182     do_div(endext, sc->mp->m_sb.sb_rextsize);
0183     extcount = endext - startext + 1;
0184     xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
0185     error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
0186             &is_free);
0187     if (!xchk_should_check_xref(sc, &error, NULL))
0188         goto out_unlock;
0189     if (is_free)
0190         xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
0191 out_unlock:
0192     xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
0193 }