Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2018 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_log_format.h"
0014 #include "xfs_trans.h"
0015 #include "xfs_sb.h"
0016 #include "xfs_inode.h"
0017 #include "xfs_alloc.h"
0018 #include "xfs_alloc_btree.h"
0019 #include "xfs_ialloc.h"
0020 #include "xfs_ialloc_btree.h"
0021 #include "xfs_rmap.h"
0022 #include "xfs_rmap_btree.h"
0023 #include "xfs_refcount_btree.h"
0024 #include "xfs_extent_busy.h"
0025 #include "xfs_ag.h"
0026 #include "xfs_ag_resv.h"
0027 #include "xfs_quota.h"
0028 #include "xfs_qm.h"
0029 #include "scrub/scrub.h"
0030 #include "scrub/common.h"
0031 #include "scrub/trace.h"
0032 #include "scrub/repair.h"
0033 #include "scrub/bitmap.h"
0034 
0035 /*
0036  * Attempt to repair some metadata, if the metadata is corrupt and userspace
0037  * told us to fix it.  This function returns -EAGAIN to mean "re-run scrub",
0038  * and will set *fixed to true if it thinks it repaired anything.
0039  */
0040 int
0041 xrep_attempt(
0042     struct xfs_scrub    *sc)
0043 {
0044     int         error = 0;
0045 
0046     trace_xrep_attempt(XFS_I(file_inode(sc->file)), sc->sm, error);
0047 
0048     xchk_ag_btcur_free(&sc->sa);
0049 
0050     /* Repair whatever's broken. */
0051     ASSERT(sc->ops->repair);
0052     error = sc->ops->repair(sc);
0053     trace_xrep_done(XFS_I(file_inode(sc->file)), sc->sm, error);
0054     switch (error) {
0055     case 0:
0056         /*
0057          * Repair succeeded.  Commit the fixes and perform a second
0058          * scrub so that we can tell userspace if we fixed the problem.
0059          */
0060         sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
0061         sc->flags |= XREP_ALREADY_FIXED;
0062         return -EAGAIN;
0063     case -EDEADLOCK:
0064     case -EAGAIN:
0065         /* Tell the caller to try again having grabbed all the locks. */
0066         if (!(sc->flags & XCHK_TRY_HARDER)) {
0067             sc->flags |= XCHK_TRY_HARDER;
0068             return -EAGAIN;
0069         }
0070         /*
0071          * We tried harder but still couldn't grab all the resources
0072          * we needed to fix it.  The corruption has not been fixed,
0073          * so report back to userspace.
0074          */
0075         return -EFSCORRUPTED;
0076     default:
0077         return error;
0078     }
0079 }
0080 
0081 /*
0082  * Complain about unfixable problems in the filesystem.  We don't log
0083  * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
0084  * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
0085  * administrator isn't running xfs_scrub in no-repairs mode.
0086  *
0087  * Use this helper function because _ratelimited silently declares a static
0088  * structure to track rate limiting information.
0089  */
0090 void
0091 xrep_failure(
0092     struct xfs_mount    *mp)
0093 {
0094     xfs_alert_ratelimited(mp,
0095 "Corruption not fixed during online repair.  Unmount and run xfs_repair.");
0096 }
0097 
0098 /*
0099  * Repair probe -- userspace uses this to probe if we're willing to repair a
0100  * given mountpoint.
0101  */
0102 int
0103 xrep_probe(
0104     struct xfs_scrub    *sc)
0105 {
0106     int         error = 0;
0107 
0108     if (xchk_should_terminate(sc, &error))
0109         return error;
0110 
0111     return 0;
0112 }
0113 
0114 /*
0115  * Roll a transaction, keeping the AG headers locked and reinitializing
0116  * the btree cursors.
0117  */
0118 int
0119 xrep_roll_ag_trans(
0120     struct xfs_scrub    *sc)
0121 {
0122     int         error;
0123 
0124     /* Keep the AG header buffers locked so we can keep going. */
0125     if (sc->sa.agi_bp)
0126         xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
0127     if (sc->sa.agf_bp)
0128         xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
0129     if (sc->sa.agfl_bp)
0130         xfs_trans_bhold(sc->tp, sc->sa.agfl_bp);
0131 
0132     /*
0133      * Roll the transaction.  We still own the buffer and the buffer lock
0134      * regardless of whether or not the roll succeeds.  If the roll fails,
0135      * the buffers will be released during teardown on our way out of the
0136      * kernel.  If it succeeds, we join them to the new transaction and
0137      * move on.
0138      */
0139     error = xfs_trans_roll(&sc->tp);
0140     if (error)
0141         return error;
0142 
0143     /* Join AG headers to the new transaction. */
0144     if (sc->sa.agi_bp)
0145         xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
0146     if (sc->sa.agf_bp)
0147         xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
0148     if (sc->sa.agfl_bp)
0149         xfs_trans_bjoin(sc->tp, sc->sa.agfl_bp);
0150 
0151     return 0;
0152 }
0153 
0154 /*
0155  * Does the given AG have enough space to rebuild a btree?  Neither AG
0156  * reservation can be critical, and we must have enough space (factoring
0157  * in AG reservations) to construct a whole btree.
0158  */
0159 bool
0160 xrep_ag_has_space(
0161     struct xfs_perag    *pag,
0162     xfs_extlen_t        nr_blocks,
0163     enum xfs_ag_resv_type   type)
0164 {
0165     return  !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
0166         !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
0167         pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
0168 }
0169 
0170 /*
0171  * Figure out how many blocks to reserve for an AG repair.  We calculate the
0172  * worst case estimate for the number of blocks we'd need to rebuild one of
0173  * any type of per-AG btree.
0174  */
0175 xfs_extlen_t
0176 xrep_calc_ag_resblks(
0177     struct xfs_scrub        *sc)
0178 {
0179     struct xfs_mount        *mp = sc->mp;
0180     struct xfs_scrub_metadata   *sm = sc->sm;
0181     struct xfs_perag        *pag;
0182     struct xfs_buf          *bp;
0183     xfs_agino_t         icount = NULLAGINO;
0184     xfs_extlen_t            aglen = NULLAGBLOCK;
0185     xfs_extlen_t            usedlen;
0186     xfs_extlen_t            freelen;
0187     xfs_extlen_t            bnobt_sz;
0188     xfs_extlen_t            inobt_sz;
0189     xfs_extlen_t            rmapbt_sz;
0190     xfs_extlen_t            refcbt_sz;
0191     int             error;
0192 
0193     if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
0194         return 0;
0195 
0196     pag = xfs_perag_get(mp, sm->sm_agno);
0197     if (pag->pagi_init) {
0198         /* Use in-core icount if possible. */
0199         icount = pag->pagi_count;
0200     } else {
0201         /* Try to get the actual counters from disk. */
0202         error = xfs_ialloc_read_agi(pag, NULL, &bp);
0203         if (!error) {
0204             icount = pag->pagi_count;
0205             xfs_buf_relse(bp);
0206         }
0207     }
0208 
0209     /* Now grab the block counters from the AGF. */
0210     error = xfs_alloc_read_agf(pag, NULL, 0, &bp);
0211     if (error) {
0212         aglen = pag->block_count;
0213         freelen = aglen;
0214         usedlen = aglen;
0215     } else {
0216         struct xfs_agf  *agf = bp->b_addr;
0217 
0218         aglen = be32_to_cpu(agf->agf_length);
0219         freelen = be32_to_cpu(agf->agf_freeblks);
0220         usedlen = aglen - freelen;
0221         xfs_buf_relse(bp);
0222     }
0223 
0224     /* If the icount is impossible, make some worst-case assumptions. */
0225     if (icount == NULLAGINO ||
0226         !xfs_verify_agino(pag, icount)) {
0227         icount = pag->agino_max - pag->agino_min + 1;
0228     }
0229 
0230     /* If the block counts are impossible, make worst-case assumptions. */
0231     if (aglen == NULLAGBLOCK ||
0232         aglen != pag->block_count ||
0233         freelen >= aglen) {
0234         aglen = pag->block_count;
0235         freelen = aglen;
0236         usedlen = aglen;
0237     }
0238     xfs_perag_put(pag);
0239 
0240     trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
0241             freelen, usedlen);
0242 
0243     /*
0244      * Figure out how many blocks we'd need worst case to rebuild
0245      * each type of btree.  Note that we can only rebuild the
0246      * bnobt/cntbt or inobt/finobt as pairs.
0247      */
0248     bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
0249     if (xfs_has_sparseinodes(mp))
0250         inobt_sz = xfs_iallocbt_calc_size(mp, icount /
0251                 XFS_INODES_PER_HOLEMASK_BIT);
0252     else
0253         inobt_sz = xfs_iallocbt_calc_size(mp, icount /
0254                 XFS_INODES_PER_CHUNK);
0255     if (xfs_has_finobt(mp))
0256         inobt_sz *= 2;
0257     if (xfs_has_reflink(mp))
0258         refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
0259     else
0260         refcbt_sz = 0;
0261     if (xfs_has_rmapbt(mp)) {
0262         /*
0263          * Guess how many blocks we need to rebuild the rmapbt.
0264          * For non-reflink filesystems we can't have more records than
0265          * used blocks.  However, with reflink it's possible to have
0266          * more than one rmap record per AG block.  We don't know how
0267          * many rmaps there could be in the AG, so we start off with
0268          * what we hope is an generous over-estimation.
0269          */
0270         if (xfs_has_reflink(mp))
0271             rmapbt_sz = xfs_rmapbt_calc_size(mp,
0272                     (unsigned long long)aglen * 2);
0273         else
0274             rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
0275     } else {
0276         rmapbt_sz = 0;
0277     }
0278 
0279     trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
0280             inobt_sz, rmapbt_sz, refcbt_sz);
0281 
0282     return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
0283 }
0284 
0285 /* Allocate a block in an AG. */
0286 int
0287 xrep_alloc_ag_block(
0288     struct xfs_scrub        *sc,
0289     const struct xfs_owner_info *oinfo,
0290     xfs_fsblock_t           *fsbno,
0291     enum xfs_ag_resv_type       resv)
0292 {
0293     struct xfs_alloc_arg        args = {0};
0294     xfs_agblock_t           bno;
0295     int             error;
0296 
0297     switch (resv) {
0298     case XFS_AG_RESV_AGFL:
0299     case XFS_AG_RESV_RMAPBT:
0300         error = xfs_alloc_get_freelist(sc->sa.pag, sc->tp,
0301                 sc->sa.agf_bp, &bno, 1);
0302         if (error)
0303             return error;
0304         if (bno == NULLAGBLOCK)
0305             return -ENOSPC;
0306         xfs_extent_busy_reuse(sc->mp, sc->sa.pag, bno, 1, false);
0307         *fsbno = XFS_AGB_TO_FSB(sc->mp, sc->sa.pag->pag_agno, bno);
0308         if (resv == XFS_AG_RESV_RMAPBT)
0309             xfs_ag_resv_rmapbt_alloc(sc->mp, sc->sa.pag->pag_agno);
0310         return 0;
0311     default:
0312         break;
0313     }
0314 
0315     args.tp = sc->tp;
0316     args.mp = sc->mp;
0317     args.oinfo = *oinfo;
0318     args.fsbno = XFS_AGB_TO_FSB(args.mp, sc->sa.pag->pag_agno, 0);
0319     args.minlen = 1;
0320     args.maxlen = 1;
0321     args.prod = 1;
0322     args.type = XFS_ALLOCTYPE_THIS_AG;
0323     args.resv = resv;
0324 
0325     error = xfs_alloc_vextent(&args);
0326     if (error)
0327         return error;
0328     if (args.fsbno == NULLFSBLOCK)
0329         return -ENOSPC;
0330     ASSERT(args.len == 1);
0331     *fsbno = args.fsbno;
0332 
0333     return 0;
0334 }
0335 
0336 /* Initialize a new AG btree root block with zero entries. */
0337 int
0338 xrep_init_btblock(
0339     struct xfs_scrub        *sc,
0340     xfs_fsblock_t           fsb,
0341     struct xfs_buf          **bpp,
0342     xfs_btnum_t         btnum,
0343     const struct xfs_buf_ops    *ops)
0344 {
0345     struct xfs_trans        *tp = sc->tp;
0346     struct xfs_mount        *mp = sc->mp;
0347     struct xfs_buf          *bp;
0348     int             error;
0349 
0350     trace_xrep_init_btblock(mp, XFS_FSB_TO_AGNO(mp, fsb),
0351             XFS_FSB_TO_AGBNO(mp, fsb), btnum);
0352 
0353     ASSERT(XFS_FSB_TO_AGNO(mp, fsb) == sc->sa.pag->pag_agno);
0354     error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
0355             XFS_FSB_TO_DADDR(mp, fsb), XFS_FSB_TO_BB(mp, 1), 0,
0356             &bp);
0357     if (error)
0358         return error;
0359     xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
0360     xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.pag->pag_agno);
0361     xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
0362     xfs_trans_log_buf(tp, bp, 0, BBTOB(bp->b_length) - 1);
0363     bp->b_ops = ops;
0364     *bpp = bp;
0365 
0366     return 0;
0367 }
0368 
0369 /*
0370  * Reconstructing per-AG Btrees
0371  *
0372  * When a space btree is corrupt, we don't bother trying to fix it.  Instead,
0373  * we scan secondary space metadata to derive the records that should be in
0374  * the damaged btree, initialize a fresh btree root, and insert the records.
0375  * Note that for rebuilding the rmapbt we scan all the primary data to
0376  * generate the new records.
0377  *
0378  * However, that leaves the matter of removing all the metadata describing the
0379  * old broken structure.  For primary metadata we use the rmap data to collect
0380  * every extent with a matching rmap owner (bitmap); we then iterate all other
0381  * metadata structures with the same rmap owner to collect the extents that
0382  * cannot be removed (sublist).  We then subtract sublist from bitmap to
0383  * derive the blocks that were used by the old btree.  These blocks can be
0384  * reaped.
0385  *
0386  * For rmapbt reconstructions we must use different tactics for extent
0387  * collection.  First we iterate all primary metadata (this excludes the old
0388  * rmapbt, obviously) to generate new rmap records.  The gaps in the rmap
0389  * records are collected as bitmap.  The bnobt records are collected as
0390  * sublist.  As with the other btrees we subtract sublist from bitmap, and the
0391  * result (since the rmapbt lives in the free space) are the blocks from the
0392  * old rmapbt.
0393  *
0394  * Disposal of Blocks from Old per-AG Btrees
0395  *
0396  * Now that we've constructed a new btree to replace the damaged one, we want
0397  * to dispose of the blocks that (we think) the old btree was using.
0398  * Previously, we used the rmapbt to collect the extents (bitmap) with the
0399  * rmap owner corresponding to the tree we rebuilt, collected extents for any
0400  * blocks with the same rmap owner that are owned by another data structure
0401  * (sublist), and subtracted sublist from bitmap.  In theory the extents
0402  * remaining in bitmap are the old btree's blocks.
0403  *
0404  * Unfortunately, it's possible that the btree was crosslinked with other
0405  * blocks on disk.  The rmap data can tell us if there are multiple owners, so
0406  * if the rmapbt says there is an owner of this block other than @oinfo, then
0407  * the block is crosslinked.  Remove the reverse mapping and continue.
0408  *
0409  * If there is one rmap record, we can free the block, which removes the
0410  * reverse mapping but doesn't add the block to the free space.  Our repair
0411  * strategy is to hope the other metadata objects crosslinked on this block
0412  * will be rebuilt (atop different blocks), thereby removing all the cross
0413  * links.
0414  *
0415  * If there are no rmap records at all, we also free the block.  If the btree
0416  * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
0417  * supposed to be a rmap record and everything is ok.  For other btrees there
0418  * had to have been an rmap entry for the block to have ended up on @bitmap,
0419  * so if it's gone now there's something wrong and the fs will shut down.
0420  *
0421  * Note: If there are multiple rmap records with only the same rmap owner as
0422  * the btree we're trying to rebuild and the block is indeed owned by another
0423  * data structure with the same rmap owner, then the block will be in sublist
0424  * and therefore doesn't need disposal.  If there are multiple rmap records
0425  * with only the same rmap owner but the block is not owned by something with
0426  * the same rmap owner, the block will be freed.
0427  *
0428  * The caller is responsible for locking the AG headers for the entire rebuild
0429  * operation so that nothing else can sneak in and change the AG state while
0430  * we're not looking.  We also assume that the caller already invalidated any
0431  * buffers associated with @bitmap.
0432  */
0433 
0434 /*
0435  * Invalidate buffers for per-AG btree blocks we're dumping.  This function
0436  * is not intended for use with file data repairs; we have bunmapi for that.
0437  */
0438 int
0439 xrep_invalidate_blocks(
0440     struct xfs_scrub    *sc,
0441     struct xbitmap      *bitmap)
0442 {
0443     struct xbitmap_range    *bmr;
0444     struct xbitmap_range    *n;
0445     struct xfs_buf      *bp;
0446     xfs_fsblock_t       fsbno;
0447 
0448     /*
0449      * For each block in each extent, see if there's an incore buffer for
0450      * exactly that block; if so, invalidate it.  The buffer cache only
0451      * lets us look for one buffer at a time, so we have to look one block
0452      * at a time.  Avoid invalidating AG headers and post-EOFS blocks
0453      * because we never own those; and if we can't TRYLOCK the buffer we
0454      * assume it's owned by someone else.
0455      */
0456     for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
0457         int     error;
0458 
0459         /* Skip AG headers and post-EOFS blocks */
0460         if (!xfs_verify_fsbno(sc->mp, fsbno))
0461             continue;
0462         error = xfs_buf_incore(sc->mp->m_ddev_targp,
0463                 XFS_FSB_TO_DADDR(sc->mp, fsbno),
0464                 XFS_FSB_TO_BB(sc->mp, 1), XBF_TRYLOCK, &bp);
0465         if (error)
0466             continue;
0467 
0468         xfs_trans_bjoin(sc->tp, bp);
0469         xfs_trans_binval(sc->tp, bp);
0470     }
0471 
0472     return 0;
0473 }
0474 
0475 /* Ensure the freelist is the correct size. */
0476 int
0477 xrep_fix_freelist(
0478     struct xfs_scrub    *sc,
0479     bool            can_shrink)
0480 {
0481     struct xfs_alloc_arg    args = {0};
0482 
0483     args.mp = sc->mp;
0484     args.tp = sc->tp;
0485     args.agno = sc->sa.pag->pag_agno;
0486     args.alignment = 1;
0487     args.pag = sc->sa.pag;
0488 
0489     return xfs_alloc_fix_freelist(&args,
0490             can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
0491 }
0492 
0493 /*
0494  * Put a block back on the AGFL.
0495  */
0496 STATIC int
0497 xrep_put_freelist(
0498     struct xfs_scrub    *sc,
0499     xfs_agblock_t       agbno)
0500 {
0501     int         error;
0502 
0503     /* Make sure there's space on the freelist. */
0504     error = xrep_fix_freelist(sc, true);
0505     if (error)
0506         return error;
0507 
0508     /*
0509      * Since we're "freeing" a lost block onto the AGFL, we have to
0510      * create an rmap for the block prior to merging it or else other
0511      * parts will break.
0512      */
0513     error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.pag, agbno, 1,
0514             &XFS_RMAP_OINFO_AG);
0515     if (error)
0516         return error;
0517 
0518     /* Put the block on the AGFL. */
0519     error = xfs_alloc_put_freelist(sc->sa.pag, sc->tp, sc->sa.agf_bp,
0520             sc->sa.agfl_bp, agbno, 0);
0521     if (error)
0522         return error;
0523     xfs_extent_busy_insert(sc->tp, sc->sa.pag, agbno, 1,
0524             XFS_EXTENT_BUSY_SKIP_DISCARD);
0525 
0526     return 0;
0527 }
0528 
0529 /* Dispose of a single block. */
0530 STATIC int
0531 xrep_reap_block(
0532     struct xfs_scrub        *sc,
0533     xfs_fsblock_t           fsbno,
0534     const struct xfs_owner_info *oinfo,
0535     enum xfs_ag_resv_type       resv)
0536 {
0537     struct xfs_btree_cur        *cur;
0538     struct xfs_buf          *agf_bp = NULL;
0539     xfs_agblock_t           agbno;
0540     bool                has_other_rmap;
0541     int             error;
0542 
0543     agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
0544     ASSERT(XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.pag->pag_agno);
0545 
0546     /*
0547      * If we are repairing per-inode metadata, we need to read in the AGF
0548      * buffer.  Otherwise, we're repairing a per-AG structure, so reuse
0549      * the AGF buffer that the setup functions already grabbed.
0550      */
0551     if (sc->ip) {
0552         error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
0553         if (error)
0554             return error;
0555     } else {
0556         agf_bp = sc->sa.agf_bp;
0557     }
0558     cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf_bp, sc->sa.pag);
0559 
0560     /* Can we find any other rmappings? */
0561     error = xfs_rmap_has_other_keys(cur, agbno, 1, oinfo, &has_other_rmap);
0562     xfs_btree_del_cursor(cur, error);
0563     if (error)
0564         goto out_free;
0565 
0566     /*
0567      * If there are other rmappings, this block is cross linked and must
0568      * not be freed.  Remove the reverse mapping and move on.  Otherwise,
0569      * we were the only owner of the block, so free the extent, which will
0570      * also remove the rmap.
0571      *
0572      * XXX: XFS doesn't support detecting the case where a single block
0573      * metadata structure is crosslinked with a multi-block structure
0574      * because the buffer cache doesn't detect aliasing problems, so we
0575      * can't fix 100% of crosslinking problems (yet).  The verifiers will
0576      * blow on writeout, the filesystem will shut down, and the admin gets
0577      * to run xfs_repair.
0578      */
0579     if (has_other_rmap)
0580         error = xfs_rmap_free(sc->tp, agf_bp, sc->sa.pag, agbno,
0581                     1, oinfo);
0582     else if (resv == XFS_AG_RESV_AGFL)
0583         error = xrep_put_freelist(sc, agbno);
0584     else
0585         error = xfs_free_extent(sc->tp, fsbno, 1, oinfo, resv);
0586     if (agf_bp != sc->sa.agf_bp)
0587         xfs_trans_brelse(sc->tp, agf_bp);
0588     if (error)
0589         return error;
0590 
0591     if (sc->ip)
0592         return xfs_trans_roll_inode(&sc->tp, sc->ip);
0593     return xrep_roll_ag_trans(sc);
0594 
0595 out_free:
0596     if (agf_bp != sc->sa.agf_bp)
0597         xfs_trans_brelse(sc->tp, agf_bp);
0598     return error;
0599 }
0600 
0601 /* Dispose of every block of every extent in the bitmap. */
0602 int
0603 xrep_reap_extents(
0604     struct xfs_scrub        *sc,
0605     struct xbitmap          *bitmap,
0606     const struct xfs_owner_info *oinfo,
0607     enum xfs_ag_resv_type       type)
0608 {
0609     struct xbitmap_range        *bmr;
0610     struct xbitmap_range        *n;
0611     xfs_fsblock_t           fsbno;
0612     int             error = 0;
0613 
0614     ASSERT(xfs_has_rmapbt(sc->mp));
0615 
0616     for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
0617         ASSERT(sc->ip != NULL ||
0618                XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.pag->pag_agno);
0619         trace_xrep_dispose_btree_extent(sc->mp,
0620                 XFS_FSB_TO_AGNO(sc->mp, fsbno),
0621                 XFS_FSB_TO_AGBNO(sc->mp, fsbno), 1);
0622 
0623         error = xrep_reap_block(sc, fsbno, oinfo, type);
0624         if (error)
0625             break;
0626     }
0627 
0628     return error;
0629 }
0630 
0631 /*
0632  * Finding per-AG Btree Roots for AGF/AGI Reconstruction
0633  *
0634  * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
0635  * the AG headers by using the rmap data to rummage through the AG looking for
0636  * btree roots.  This is not guaranteed to work if the AG is heavily damaged
0637  * or the rmap data are corrupt.
0638  *
0639  * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
0640  * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
0641  * AGI is being rebuilt.  It must maintain these locks until it's safe for
0642  * other threads to change the btrees' shapes.  The caller provides
0643  * information about the btrees to look for by passing in an array of
0644  * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
0645  * The (root, height) fields will be set on return if anything is found.  The
0646  * last element of the array should have a NULL buf_ops to mark the end of the
0647  * array.
0648  *
0649  * For every rmapbt record matching any of the rmap owners in btree_info,
0650  * read each block referenced by the rmap record.  If the block is a btree
0651  * block from this filesystem matching any of the magic numbers and has a
0652  * level higher than what we've already seen, remember the block and the
0653  * height of the tree required to have such a block.  When the call completes,
0654  * we return the highest block we've found for each btree description; those
0655  * should be the roots.
0656  */
0657 
0658 struct xrep_findroot {
0659     struct xfs_scrub        *sc;
0660     struct xfs_buf          *agfl_bp;
0661     struct xfs_agf          *agf;
0662     struct xrep_find_ag_btree   *btree_info;
0663 };
0664 
0665 /* See if our block is in the AGFL. */
0666 STATIC int
0667 xrep_findroot_agfl_walk(
0668     struct xfs_mount    *mp,
0669     xfs_agblock_t       bno,
0670     void            *priv)
0671 {
0672     xfs_agblock_t       *agbno = priv;
0673 
0674     return (*agbno == bno) ? -ECANCELED : 0;
0675 }
0676 
0677 /* Does this block match the btree information passed in? */
0678 STATIC int
0679 xrep_findroot_block(
0680     struct xrep_findroot        *ri,
0681     struct xrep_find_ag_btree   *fab,
0682     uint64_t            owner,
0683     xfs_agblock_t           agbno,
0684     bool                *done_with_block)
0685 {
0686     struct xfs_mount        *mp = ri->sc->mp;
0687     struct xfs_buf          *bp;
0688     struct xfs_btree_block      *btblock;
0689     xfs_daddr_t         daddr;
0690     int             block_level;
0691     int             error = 0;
0692 
0693     daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.pag->pag_agno, agbno);
0694 
0695     /*
0696      * Blocks in the AGFL have stale contents that might just happen to
0697      * have a matching magic and uuid.  We don't want to pull these blocks
0698      * in as part of a tree root, so we have to filter out the AGFL stuff
0699      * here.  If the AGFL looks insane we'll just refuse to repair.
0700      */
0701     if (owner == XFS_RMAP_OWN_AG) {
0702         error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
0703                 xrep_findroot_agfl_walk, &agbno);
0704         if (error == -ECANCELED)
0705             return 0;
0706         if (error)
0707             return error;
0708     }
0709 
0710     /*
0711      * Read the buffer into memory so that we can see if it's a match for
0712      * our btree type.  We have no clue if it is beforehand, and we want to
0713      * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
0714      * will cause needless disk reads in subsequent calls to this function)
0715      * and logging metadata verifier failures.
0716      *
0717      * Therefore, pass in NULL buffer ops.  If the buffer was already in
0718      * memory from some other caller it will already have b_ops assigned.
0719      * If it was in memory from a previous unsuccessful findroot_block
0720      * call, the buffer won't have b_ops but it should be clean and ready
0721      * for us to try to verify if the read call succeeds.  The same applies
0722      * if the buffer wasn't in memory at all.
0723      *
0724      * Note: If we never match a btree type with this buffer, it will be
0725      * left in memory with NULL b_ops.  This shouldn't be a problem unless
0726      * the buffer gets written.
0727      */
0728     error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
0729             mp->m_bsize, 0, &bp, NULL);
0730     if (error)
0731         return error;
0732 
0733     /* Ensure the block magic matches the btree type we're looking for. */
0734     btblock = XFS_BUF_TO_BLOCK(bp);
0735     ASSERT(fab->buf_ops->magic[1] != 0);
0736     if (btblock->bb_magic != fab->buf_ops->magic[1])
0737         goto out;
0738 
0739     /*
0740      * If the buffer already has ops applied and they're not the ones for
0741      * this btree type, we know this block doesn't match the btree and we
0742      * can bail out.
0743      *
0744      * If the buffer ops match ours, someone else has already validated
0745      * the block for us, so we can move on to checking if this is a root
0746      * block candidate.
0747      *
0748      * If the buffer does not have ops, nobody has successfully validated
0749      * the contents and the buffer cannot be dirty.  If the magic, uuid,
0750      * and structure match this btree type then we'll move on to checking
0751      * if it's a root block candidate.  If there is no match, bail out.
0752      */
0753     if (bp->b_ops) {
0754         if (bp->b_ops != fab->buf_ops)
0755             goto out;
0756     } else {
0757         ASSERT(!xfs_trans_buf_is_dirty(bp));
0758         if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
0759                 &mp->m_sb.sb_meta_uuid))
0760             goto out;
0761         /*
0762          * Read verifiers can reference b_ops, so we set the pointer
0763          * here.  If the verifier fails we'll reset the buffer state
0764          * to what it was before we touched the buffer.
0765          */
0766         bp->b_ops = fab->buf_ops;
0767         fab->buf_ops->verify_read(bp);
0768         if (bp->b_error) {
0769             bp->b_ops = NULL;
0770             bp->b_error = 0;
0771             goto out;
0772         }
0773 
0774         /*
0775          * Some read verifiers will (re)set b_ops, so we must be
0776          * careful not to change b_ops after running the verifier.
0777          */
0778     }
0779 
0780     /*
0781      * This block passes the magic/uuid and verifier tests for this btree
0782      * type.  We don't need the caller to try the other tree types.
0783      */
0784     *done_with_block = true;
0785 
0786     /*
0787      * Compare this btree block's level to the height of the current
0788      * candidate root block.
0789      *
0790      * If the level matches the root we found previously, throw away both
0791      * blocks because there can't be two candidate roots.
0792      *
0793      * If level is lower in the tree than the root we found previously,
0794      * ignore this block.
0795      */
0796     block_level = xfs_btree_get_level(btblock);
0797     if (block_level + 1 == fab->height) {
0798         fab->root = NULLAGBLOCK;
0799         goto out;
0800     } else if (block_level < fab->height) {
0801         goto out;
0802     }
0803 
0804     /*
0805      * This is the highest block in the tree that we've found so far.
0806      * Update the btree height to reflect what we've learned from this
0807      * block.
0808      */
0809     fab->height = block_level + 1;
0810 
0811     /*
0812      * If this block doesn't have sibling pointers, then it's the new root
0813      * block candidate.  Otherwise, the root will be found farther up the
0814      * tree.
0815      */
0816     if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
0817         btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
0818         fab->root = agbno;
0819     else
0820         fab->root = NULLAGBLOCK;
0821 
0822     trace_xrep_findroot_block(mp, ri->sc->sa.pag->pag_agno, agbno,
0823             be32_to_cpu(btblock->bb_magic), fab->height - 1);
0824 out:
0825     xfs_trans_brelse(ri->sc->tp, bp);
0826     return error;
0827 }
0828 
0829 /*
0830  * Do any of the blocks in this rmap record match one of the btrees we're
0831  * looking for?
0832  */
0833 STATIC int
0834 xrep_findroot_rmap(
0835     struct xfs_btree_cur        *cur,
0836     const struct xfs_rmap_irec  *rec,
0837     void                *priv)
0838 {
0839     struct xrep_findroot        *ri = priv;
0840     struct xrep_find_ag_btree   *fab;
0841     xfs_agblock_t           b;
0842     bool                done;
0843     int             error = 0;
0844 
0845     /* Ignore anything that isn't AG metadata. */
0846     if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
0847         return 0;
0848 
0849     /* Otherwise scan each block + btree type. */
0850     for (b = 0; b < rec->rm_blockcount; b++) {
0851         done = false;
0852         for (fab = ri->btree_info; fab->buf_ops; fab++) {
0853             if (rec->rm_owner != fab->rmap_owner)
0854                 continue;
0855             error = xrep_findroot_block(ri, fab,
0856                     rec->rm_owner, rec->rm_startblock + b,
0857                     &done);
0858             if (error)
0859                 return error;
0860             if (done)
0861                 break;
0862         }
0863     }
0864 
0865     return 0;
0866 }
0867 
0868 /* Find the roots of the per-AG btrees described in btree_info. */
0869 int
0870 xrep_find_ag_btree_roots(
0871     struct xfs_scrub        *sc,
0872     struct xfs_buf          *agf_bp,
0873     struct xrep_find_ag_btree   *btree_info,
0874     struct xfs_buf          *agfl_bp)
0875 {
0876     struct xfs_mount        *mp = sc->mp;
0877     struct xrep_findroot        ri;
0878     struct xrep_find_ag_btree   *fab;
0879     struct xfs_btree_cur        *cur;
0880     int             error;
0881 
0882     ASSERT(xfs_buf_islocked(agf_bp));
0883     ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
0884 
0885     ri.sc = sc;
0886     ri.btree_info = btree_info;
0887     ri.agf = agf_bp->b_addr;
0888     ri.agfl_bp = agfl_bp;
0889     for (fab = btree_info; fab->buf_ops; fab++) {
0890         ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
0891         ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
0892         fab->root = NULLAGBLOCK;
0893         fab->height = 0;
0894     }
0895 
0896     cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
0897     error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
0898     xfs_btree_del_cursor(cur, error);
0899 
0900     return error;
0901 }
0902 
0903 /* Force a quotacheck the next time we mount. */
0904 void
0905 xrep_force_quotacheck(
0906     struct xfs_scrub    *sc,
0907     xfs_dqtype_t        type)
0908 {
0909     uint            flag;
0910 
0911     flag = xfs_quota_chkd_flag(type);
0912     if (!(flag & sc->mp->m_qflags))
0913         return;
0914 
0915     mutex_lock(&sc->mp->m_quotainfo->qi_quotaofflock);
0916     sc->mp->m_qflags &= ~flag;
0917     spin_lock(&sc->mp->m_sb_lock);
0918     sc->mp->m_sb.sb_qflags &= ~flag;
0919     spin_unlock(&sc->mp->m_sb_lock);
0920     xfs_log_sb(sc->tp);
0921     mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock);
0922 }
0923 
0924 /*
0925  * Attach dquots to this inode, or schedule quotacheck to fix them.
0926  *
0927  * This function ensures that the appropriate dquots are attached to an inode.
0928  * We cannot allow the dquot code to allocate an on-disk dquot block here
0929  * because we're already in transaction context with the inode locked.  The
0930  * on-disk dquot should already exist anyway.  If the quota code signals
0931  * corruption or missing quota information, schedule quotacheck, which will
0932  * repair corruptions in the quota metadata.
0933  */
0934 int
0935 xrep_ino_dqattach(
0936     struct xfs_scrub    *sc)
0937 {
0938     int         error;
0939 
0940     error = xfs_qm_dqattach_locked(sc->ip, false);
0941     switch (error) {
0942     case -EFSBADCRC:
0943     case -EFSCORRUPTED:
0944     case -ENOENT:
0945         xfs_err_ratelimited(sc->mp,
0946 "inode %llu repair encountered quota error %d, quotacheck forced.",
0947                 (unsigned long long)sc->ip->i_ino, error);
0948         if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
0949             xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
0950         if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
0951             xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
0952         if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
0953             xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
0954         fallthrough;
0955     case -ESRCH:
0956         error = 0;
0957         break;
0958     default:
0959         break;
0960     }
0961 
0962     return error;
0963 }