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_btree.h"
0013 #include "xfs_rmap.h"
0014 #include "xfs_refcount.h"
0015 #include "scrub/scrub.h"
0016 #include "scrub/common.h"
0017 #include "scrub/btree.h"
0018 #include "xfs_ag.h"
0019 
0020 /*
0021  * Set us up to scrub reverse mapping btrees.
0022  */
0023 int
0024 xchk_setup_ag_rmapbt(
0025     struct xfs_scrub    *sc)
0026 {
0027     return xchk_setup_ag_btree(sc, false);
0028 }
0029 
0030 /* Reverse-mapping scrubber. */
0031 
0032 /* Cross-reference a rmap against the refcount btree. */
0033 STATIC void
0034 xchk_rmapbt_xref_refc(
0035     struct xfs_scrub    *sc,
0036     struct xfs_rmap_irec    *irec)
0037 {
0038     xfs_agblock_t       fbno;
0039     xfs_extlen_t        flen;
0040     bool            non_inode;
0041     bool            is_bmbt;
0042     bool            is_attr;
0043     bool            is_unwritten;
0044     int         error;
0045 
0046     if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
0047         return;
0048 
0049     non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
0050     is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
0051     is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
0052     is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
0053 
0054     /* If this is shared, must be a data fork extent. */
0055     error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
0056             irec->rm_blockcount, &fbno, &flen, false);
0057     if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
0058         return;
0059     if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
0060         xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
0061 }
0062 
0063 /* Cross-reference with the other btrees. */
0064 STATIC void
0065 xchk_rmapbt_xref(
0066     struct xfs_scrub    *sc,
0067     struct xfs_rmap_irec    *irec)
0068 {
0069     xfs_agblock_t       agbno = irec->rm_startblock;
0070     xfs_extlen_t        len = irec->rm_blockcount;
0071 
0072     if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
0073         return;
0074 
0075     xchk_xref_is_used_space(sc, agbno, len);
0076     if (irec->rm_owner == XFS_RMAP_OWN_INODES)
0077         xchk_xref_is_inode_chunk(sc, agbno, len);
0078     else
0079         xchk_xref_is_not_inode_chunk(sc, agbno, len);
0080     if (irec->rm_owner == XFS_RMAP_OWN_COW)
0081         xchk_xref_is_cow_staging(sc, irec->rm_startblock,
0082                 irec->rm_blockcount);
0083     else
0084         xchk_rmapbt_xref_refc(sc, irec);
0085 }
0086 
0087 /* Scrub an rmapbt record. */
0088 STATIC int
0089 xchk_rmapbt_rec(
0090     struct xchk_btree   *bs,
0091     const union xfs_btree_rec *rec)
0092 {
0093     struct xfs_mount    *mp = bs->cur->bc_mp;
0094     struct xfs_rmap_irec    irec;
0095     struct xfs_perag    *pag = bs->cur->bc_ag.pag;
0096     bool            non_inode;
0097     bool            is_unwritten;
0098     bool            is_bmbt;
0099     bool            is_attr;
0100     int         error;
0101 
0102     error = xfs_rmap_btrec_to_irec(rec, &irec);
0103     if (!xchk_btree_process_error(bs->sc, bs->cur, 0, &error))
0104         goto out;
0105 
0106     /* Check extent. */
0107     if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
0108         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0109 
0110     if (irec.rm_owner == XFS_RMAP_OWN_FS) {
0111         /*
0112          * xfs_verify_agbno returns false for static fs metadata.
0113          * Since that only exists at the start of the AG, validate
0114          * that by hand.
0115          */
0116         if (irec.rm_startblock != 0 ||
0117             irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
0118             xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0119     } else {
0120         /*
0121          * Otherwise we must point somewhere past the static metadata
0122          * but before the end of the FS.  Run the regular check.
0123          */
0124         if (!xfs_verify_agbno(pag, irec.rm_startblock) ||
0125             !xfs_verify_agbno(pag, irec.rm_startblock +
0126                 irec.rm_blockcount - 1))
0127             xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0128     }
0129 
0130     /* Check flags. */
0131     non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
0132     is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
0133     is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
0134     is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
0135 
0136     if (is_bmbt && irec.rm_offset != 0)
0137         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0138 
0139     if (non_inode && irec.rm_offset != 0)
0140         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0141 
0142     if (is_unwritten && (is_bmbt || non_inode || is_attr))
0143         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0144 
0145     if (non_inode && (is_bmbt || is_unwritten || is_attr))
0146         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0147 
0148     if (!non_inode) {
0149         if (!xfs_verify_ino(mp, irec.rm_owner))
0150             xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0151     } else {
0152         /* Non-inode owner within the magic values? */
0153         if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
0154             irec.rm_owner > XFS_RMAP_OWN_FS)
0155             xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
0156     }
0157 
0158     xchk_rmapbt_xref(bs->sc, &irec);
0159 out:
0160     return error;
0161 }
0162 
0163 /* Scrub the rmap btree for some AG. */
0164 int
0165 xchk_rmapbt(
0166     struct xfs_scrub    *sc)
0167 {
0168     return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
0169             &XFS_RMAP_OINFO_AG, NULL);
0170 }
0171 
0172 /* xref check that the extent is owned by a given owner */
0173 static inline void
0174 xchk_xref_check_owner(
0175     struct xfs_scrub        *sc,
0176     xfs_agblock_t           bno,
0177     xfs_extlen_t            len,
0178     const struct xfs_owner_info *oinfo,
0179     bool                should_have_rmap)
0180 {
0181     bool                has_rmap;
0182     int             error;
0183 
0184     if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
0185         return;
0186 
0187     error = xfs_rmap_record_exists(sc->sa.rmap_cur, bno, len, oinfo,
0188             &has_rmap);
0189     if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
0190         return;
0191     if (has_rmap != should_have_rmap)
0192         xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
0193 }
0194 
0195 /* xref check that the extent is owned by a given owner */
0196 void
0197 xchk_xref_is_owned_by(
0198     struct xfs_scrub        *sc,
0199     xfs_agblock_t           bno,
0200     xfs_extlen_t            len,
0201     const struct xfs_owner_info *oinfo)
0202 {
0203     xchk_xref_check_owner(sc, bno, len, oinfo, true);
0204 }
0205 
0206 /* xref check that the extent is not owned by a given owner */
0207 void
0208 xchk_xref_is_not_owned_by(
0209     struct xfs_scrub        *sc,
0210     xfs_agblock_t           bno,
0211     xfs_extlen_t            len,
0212     const struct xfs_owner_info *oinfo)
0213 {
0214     xchk_xref_check_owner(sc, bno, len, oinfo, false);
0215 }
0216 
0217 /* xref check that the extent has no reverse mapping at all */
0218 void
0219 xchk_xref_has_no_owner(
0220     struct xfs_scrub    *sc,
0221     xfs_agblock_t       bno,
0222     xfs_extlen_t        len)
0223 {
0224     bool            has_rmap;
0225     int         error;
0226 
0227     if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
0228         return;
0229 
0230     error = xfs_rmap_has_record(sc->sa.rmap_cur, bno, len, &has_rmap);
0231     if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
0232         return;
0233     if (has_rmap)
0234         xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
0235 }