1739a2fe0SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later
284d42ea6SDarrick J. Wong /*
3ecc73f8aSDarrick J. Wong * Copyright (C) 2018-2023 Oracle. All Rights Reserved.
4739a2fe0SDarrick J. Wong * Author: Darrick J. Wong <djwong@kernel.org>
584d42ea6SDarrick J. Wong */
684d42ea6SDarrick J. Wong #include "xfs.h"
784d42ea6SDarrick J. Wong #include "xfs_fs.h"
884d42ea6SDarrick J. Wong #include "xfs_shared.h"
984d42ea6SDarrick J. Wong #include "xfs_format.h"
1084d42ea6SDarrick J. Wong #include "xfs_trans_resv.h"
1184d42ea6SDarrick J. Wong #include "xfs_mount.h"
1284d42ea6SDarrick J. Wong #include "xfs_btree.h"
1384d42ea6SDarrick J. Wong #include "xfs_log_format.h"
1484d42ea6SDarrick J. Wong #include "xfs_trans.h"
1584d42ea6SDarrick J. Wong #include "xfs_sb.h"
1684d42ea6SDarrick J. Wong #include "xfs_inode.h"
1784d42ea6SDarrick J. Wong #include "xfs_alloc.h"
1884d42ea6SDarrick J. Wong #include "xfs_alloc_btree.h"
1984d42ea6SDarrick J. Wong #include "xfs_ialloc.h"
2084d42ea6SDarrick J. Wong #include "xfs_ialloc_btree.h"
2184d42ea6SDarrick J. Wong #include "xfs_rmap.h"
2284d42ea6SDarrick J. Wong #include "xfs_rmap_btree.h"
2384d42ea6SDarrick J. Wong #include "xfs_refcount_btree.h"
2484d42ea6SDarrick J. Wong #include "xfs_extent_busy.h"
259bbafc71SDave Chinner #include "xfs_ag.h"
2684d42ea6SDarrick J. Wong #include "xfs_ag_resv.h"
277e85bc6cSDarrick J. Wong #include "xfs_quota.h"
2859d7fab2SDarrick J. Wong #include "xfs_qm.h"
291c7ce115SDarrick J. Wong #include "xfs_defer.h"
3084d42ea6SDarrick J. Wong #include "scrub/scrub.h"
3184d42ea6SDarrick J. Wong #include "scrub/common.h"
3284d42ea6SDarrick J. Wong #include "scrub/trace.h"
3384d42ea6SDarrick J. Wong #include "scrub/repair.h"
34bc270b53SDarrick J. Wong #include "scrub/bitmap.h"
35*d7a74cadSDarrick J. Wong #include "scrub/stats.h"
3684d42ea6SDarrick J. Wong
3784d42ea6SDarrick J. Wong /*
3884d42ea6SDarrick J. Wong * Attempt to repair some metadata, if the metadata is corrupt and userspace
3984d42ea6SDarrick J. Wong * told us to fix it. This function returns -EAGAIN to mean "re-run scrub",
4084d42ea6SDarrick J. Wong * and will set *fixed to true if it thinks it repaired anything.
4184d42ea6SDarrick J. Wong */
4284d42ea6SDarrick J. Wong int
xrep_attempt(struct xfs_scrub * sc,struct xchk_stats_run * run)43b5e2196eSDarrick J. Wong xrep_attempt(
44*d7a74cadSDarrick J. Wong struct xfs_scrub *sc,
45*d7a74cadSDarrick J. Wong struct xchk_stats_run *run)
4684d42ea6SDarrick J. Wong {
47*d7a74cadSDarrick J. Wong u64 repair_start;
4884d42ea6SDarrick J. Wong int error = 0;
4984d42ea6SDarrick J. Wong
50026f57ebSDarrick J. Wong trace_xrep_attempt(XFS_I(file_inode(sc->file)), sc->sm, error);
5184d42ea6SDarrick J. Wong
52c517b3aaSDarrick J. Wong xchk_ag_btcur_free(&sc->sa);
5384d42ea6SDarrick J. Wong
5484d42ea6SDarrick J. Wong /* Repair whatever's broken. */
5584d42ea6SDarrick J. Wong ASSERT(sc->ops->repair);
56*d7a74cadSDarrick J. Wong run->repair_attempted = true;
57*d7a74cadSDarrick J. Wong repair_start = xchk_stats_now();
5884d42ea6SDarrick J. Wong error = sc->ops->repair(sc);
59026f57ebSDarrick J. Wong trace_xrep_done(XFS_I(file_inode(sc->file)), sc->sm, error);
60*d7a74cadSDarrick J. Wong run->repair_ns += xchk_stats_elapsed_ns(repair_start);
6184d42ea6SDarrick J. Wong switch (error) {
6284d42ea6SDarrick J. Wong case 0:
6384d42ea6SDarrick J. Wong /*
6484d42ea6SDarrick J. Wong * Repair succeeded. Commit the fixes and perform a second
6584d42ea6SDarrick J. Wong * scrub so that we can tell userspace if we fixed the problem.
6684d42ea6SDarrick J. Wong */
6784d42ea6SDarrick J. Wong sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
68160b5a78SDarrick J. Wong sc->flags |= XREP_ALREADY_FIXED;
69*d7a74cadSDarrick J. Wong run->repair_succeeded = true;
7084d42ea6SDarrick J. Wong return -EAGAIN;
7188accf17SDarrick J. Wong case -ECHRNG:
7288accf17SDarrick J. Wong sc->flags |= XCHK_NEED_DRAIN;
73*d7a74cadSDarrick J. Wong run->retries++;
7488accf17SDarrick J. Wong return -EAGAIN;
7584d42ea6SDarrick J. Wong case -EDEADLOCK:
7684d42ea6SDarrick J. Wong /* Tell the caller to try again having grabbed all the locks. */
77f8c2a225SDarrick J. Wong if (!(sc->flags & XCHK_TRY_HARDER)) {
78f8c2a225SDarrick J. Wong sc->flags |= XCHK_TRY_HARDER;
79*d7a74cadSDarrick J. Wong run->retries++;
8084d42ea6SDarrick J. Wong return -EAGAIN;
8184d42ea6SDarrick J. Wong }
8284d42ea6SDarrick J. Wong /*
8384d42ea6SDarrick J. Wong * We tried harder but still couldn't grab all the resources
8484d42ea6SDarrick J. Wong * we needed to fix it. The corruption has not been fixed,
8593b0c58eSDarrick J. Wong * so exit to userspace with the scan's output flags unchanged.
8684d42ea6SDarrick J. Wong */
8793b0c58eSDarrick J. Wong return 0;
8884d42ea6SDarrick J. Wong default:
896bf2f879SDarrick J. Wong /*
906bf2f879SDarrick J. Wong * EAGAIN tells the caller to re-scrub, so we cannot return
916bf2f879SDarrick J. Wong * that here.
926bf2f879SDarrick J. Wong */
936bf2f879SDarrick J. Wong ASSERT(error != -EAGAIN);
9484d42ea6SDarrick J. Wong return error;
9584d42ea6SDarrick J. Wong }
9684d42ea6SDarrick J. Wong }
9784d42ea6SDarrick J. Wong
9884d42ea6SDarrick J. Wong /*
9984d42ea6SDarrick J. Wong * Complain about unfixable problems in the filesystem. We don't log
10084d42ea6SDarrick J. Wong * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
10184d42ea6SDarrick J. Wong * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
10284d42ea6SDarrick J. Wong * administrator isn't running xfs_scrub in no-repairs mode.
10384d42ea6SDarrick J. Wong *
10484d42ea6SDarrick J. Wong * Use this helper function because _ratelimited silently declares a static
10584d42ea6SDarrick J. Wong * structure to track rate limiting information.
10684d42ea6SDarrick J. Wong */
10784d42ea6SDarrick J. Wong void
xrep_failure(struct xfs_mount * mp)108b5e2196eSDarrick J. Wong xrep_failure(
10984d42ea6SDarrick J. Wong struct xfs_mount *mp)
11084d42ea6SDarrick J. Wong {
11184d42ea6SDarrick J. Wong xfs_alert_ratelimited(mp,
11284d42ea6SDarrick J. Wong "Corruption not fixed during online repair. Unmount and run xfs_repair.");
11384d42ea6SDarrick J. Wong }
11484d42ea6SDarrick J. Wong
11584d42ea6SDarrick J. Wong /*
11684d42ea6SDarrick J. Wong * Repair probe -- userspace uses this to probe if we're willing to repair a
11784d42ea6SDarrick J. Wong * given mountpoint.
11884d42ea6SDarrick J. Wong */
11984d42ea6SDarrick J. Wong int
xrep_probe(struct xfs_scrub * sc)120b5e2196eSDarrick J. Wong xrep_probe(
1211d8a748aSDarrick J. Wong struct xfs_scrub *sc)
12284d42ea6SDarrick J. Wong {
12384d42ea6SDarrick J. Wong int error = 0;
12484d42ea6SDarrick J. Wong
125c517b3aaSDarrick J. Wong if (xchk_should_terminate(sc, &error))
12684d42ea6SDarrick J. Wong return error;
12784d42ea6SDarrick J. Wong
12884d42ea6SDarrick J. Wong return 0;
12984d42ea6SDarrick J. Wong }
1300a9633faSDarrick J. Wong
1310a9633faSDarrick J. Wong /*
1320a9633faSDarrick J. Wong * Roll a transaction, keeping the AG headers locked and reinitializing
1330a9633faSDarrick J. Wong * the btree cursors.
1340a9633faSDarrick J. Wong */
1350a9633faSDarrick J. Wong int
xrep_roll_ag_trans(struct xfs_scrub * sc)136b5e2196eSDarrick J. Wong xrep_roll_ag_trans(
1371d8a748aSDarrick J. Wong struct xfs_scrub *sc)
1380a9633faSDarrick J. Wong {
1390a9633faSDarrick J. Wong int error;
1400a9633faSDarrick J. Wong
1413e59c010SDarrick J. Wong /*
1423e59c010SDarrick J. Wong * Keep the AG header buffers locked while we roll the transaction.
1433e59c010SDarrick J. Wong * Ensure that both AG buffers are dirty and held when we roll the
1443e59c010SDarrick J. Wong * transaction so that they move forward in the log without losing the
1453e59c010SDarrick J. Wong * bli (and hence the bli type) when the transaction commits.
1463e59c010SDarrick J. Wong *
1473e59c010SDarrick J. Wong * Normal code would never hold clean buffers across a roll, but repair
1483e59c010SDarrick J. Wong * needs both buffers to maintain a total lock on the AG.
1493e59c010SDarrick J. Wong */
1503e59c010SDarrick J. Wong if (sc->sa.agi_bp) {
1513e59c010SDarrick J. Wong xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM);
1520a9633faSDarrick J. Wong xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
1533e59c010SDarrick J. Wong }
1543e59c010SDarrick J. Wong
1553e59c010SDarrick J. Wong if (sc->sa.agf_bp) {
1563e59c010SDarrick J. Wong xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM);
1570a9633faSDarrick J. Wong xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
1583e59c010SDarrick J. Wong }
1590a9633faSDarrick J. Wong
160f60be90fSDarrick J. Wong /*
1613e59c010SDarrick J. Wong * Roll the transaction. We still hold the AG header buffers locked
1623e59c010SDarrick J. Wong * regardless of whether or not that succeeds. On failure, the buffers
1633e59c010SDarrick J. Wong * will be released during teardown on our way out of the kernel. If
1643e59c010SDarrick J. Wong * successful, join the buffers to the new transaction and move on.
165f60be90fSDarrick J. Wong */
1660a9633faSDarrick J. Wong error = xfs_trans_roll(&sc->tp);
1670a9633faSDarrick J. Wong if (error)
168f60be90fSDarrick J. Wong return error;
1690a9633faSDarrick J. Wong
1703e59c010SDarrick J. Wong /* Join the AG headers to the new transaction. */
171f9ed6debSDarrick J. Wong if (sc->sa.agi_bp)
1720a9633faSDarrick J. Wong xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
173f9ed6debSDarrick J. Wong if (sc->sa.agf_bp)
1740a9633faSDarrick J. Wong xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
1750a9633faSDarrick J. Wong
1760a9633faSDarrick J. Wong return 0;
1770a9633faSDarrick J. Wong }
1780a9633faSDarrick J. Wong
1791c7ce115SDarrick J. Wong /* Finish all deferred work attached to the repair transaction. */
1801c7ce115SDarrick J. Wong int
xrep_defer_finish(struct xfs_scrub * sc)1811c7ce115SDarrick J. Wong xrep_defer_finish(
1821c7ce115SDarrick J. Wong struct xfs_scrub *sc)
1831c7ce115SDarrick J. Wong {
1841c7ce115SDarrick J. Wong int error;
1851c7ce115SDarrick J. Wong
1861c7ce115SDarrick J. Wong /*
1871c7ce115SDarrick J. Wong * Keep the AG header buffers locked while we complete deferred work
1881c7ce115SDarrick J. Wong * items. Ensure that both AG buffers are dirty and held when we roll
1891c7ce115SDarrick J. Wong * the transaction so that they move forward in the log without losing
1901c7ce115SDarrick J. Wong * the bli (and hence the bli type) when the transaction commits.
1911c7ce115SDarrick J. Wong *
1921c7ce115SDarrick J. Wong * Normal code would never hold clean buffers across a roll, but repair
1931c7ce115SDarrick J. Wong * needs both buffers to maintain a total lock on the AG.
1941c7ce115SDarrick J. Wong */
1951c7ce115SDarrick J. Wong if (sc->sa.agi_bp) {
1961c7ce115SDarrick J. Wong xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM);
1971c7ce115SDarrick J. Wong xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
1981c7ce115SDarrick J. Wong }
1991c7ce115SDarrick J. Wong
2001c7ce115SDarrick J. Wong if (sc->sa.agf_bp) {
2011c7ce115SDarrick J. Wong xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM);
2021c7ce115SDarrick J. Wong xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
2031c7ce115SDarrick J. Wong }
2041c7ce115SDarrick J. Wong
2051c7ce115SDarrick J. Wong /*
2061c7ce115SDarrick J. Wong * Finish all deferred work items. We still hold the AG header buffers
2071c7ce115SDarrick J. Wong * locked regardless of whether or not that succeeds. On failure, the
2081c7ce115SDarrick J. Wong * buffers will be released during teardown on our way out of the
2091c7ce115SDarrick J. Wong * kernel. If successful, join the buffers to the new transaction
2101c7ce115SDarrick J. Wong * and move on.
2111c7ce115SDarrick J. Wong */
2121c7ce115SDarrick J. Wong error = xfs_defer_finish(&sc->tp);
2131c7ce115SDarrick J. Wong if (error)
2141c7ce115SDarrick J. Wong return error;
2151c7ce115SDarrick J. Wong
2161c7ce115SDarrick J. Wong /*
2171c7ce115SDarrick J. Wong * Release the hold that we set above because defer_finish won't do
2181c7ce115SDarrick J. Wong * that for us. The defer roll code redirties held buffers after each
2191c7ce115SDarrick J. Wong * roll, so the AG header buffers should be ready for logging.
2201c7ce115SDarrick J. Wong */
2211c7ce115SDarrick J. Wong if (sc->sa.agi_bp)
2221c7ce115SDarrick J. Wong xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp);
2231c7ce115SDarrick J. Wong if (sc->sa.agf_bp)
2241c7ce115SDarrick J. Wong xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp);
2251c7ce115SDarrick J. Wong
2261c7ce115SDarrick J. Wong return 0;
2271c7ce115SDarrick J. Wong }
2281c7ce115SDarrick J. Wong
2290a9633faSDarrick J. Wong /*
2300a9633faSDarrick J. Wong * Does the given AG have enough space to rebuild a btree? Neither AG
2310a9633faSDarrick J. Wong * reservation can be critical, and we must have enough space (factoring
2320a9633faSDarrick J. Wong * in AG reservations) to construct a whole btree.
2330a9633faSDarrick J. Wong */
2340a9633faSDarrick J. Wong bool
xrep_ag_has_space(struct xfs_perag * pag,xfs_extlen_t nr_blocks,enum xfs_ag_resv_type type)235b5e2196eSDarrick J. Wong xrep_ag_has_space(
2360a9633faSDarrick J. Wong struct xfs_perag *pag,
2370a9633faSDarrick J. Wong xfs_extlen_t nr_blocks,
2380a9633faSDarrick J. Wong enum xfs_ag_resv_type type)
2390a9633faSDarrick J. Wong {
2400a9633faSDarrick J. Wong return !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
2410a9633faSDarrick J. Wong !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
2420a9633faSDarrick J. Wong pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
2430a9633faSDarrick J. Wong }
2440a9633faSDarrick J. Wong
2450a9633faSDarrick J. Wong /*
2460a9633faSDarrick J. Wong * Figure out how many blocks to reserve for an AG repair. We calculate the
2470a9633faSDarrick J. Wong * worst case estimate for the number of blocks we'd need to rebuild one of
2480a9633faSDarrick J. Wong * any type of per-AG btree.
2490a9633faSDarrick J. Wong */
2500a9633faSDarrick J. Wong xfs_extlen_t
xrep_calc_ag_resblks(struct xfs_scrub * sc)251b5e2196eSDarrick J. Wong xrep_calc_ag_resblks(
2521d8a748aSDarrick J. Wong struct xfs_scrub *sc)
2530a9633faSDarrick J. Wong {
2540a9633faSDarrick J. Wong struct xfs_mount *mp = sc->mp;
2550a9633faSDarrick J. Wong struct xfs_scrub_metadata *sm = sc->sm;
2560a9633faSDarrick J. Wong struct xfs_perag *pag;
2570a9633faSDarrick J. Wong struct xfs_buf *bp;
2581fc25f51SDarrick J. Wong xfs_agino_t icount = NULLAGINO;
2591fc25f51SDarrick J. Wong xfs_extlen_t aglen = NULLAGBLOCK;
2600a9633faSDarrick J. Wong xfs_extlen_t usedlen;
2610a9633faSDarrick J. Wong xfs_extlen_t freelen;
2620a9633faSDarrick J. Wong xfs_extlen_t bnobt_sz;
2630a9633faSDarrick J. Wong xfs_extlen_t inobt_sz;
2640a9633faSDarrick J. Wong xfs_extlen_t rmapbt_sz;
2650a9633faSDarrick J. Wong xfs_extlen_t refcbt_sz;
2660a9633faSDarrick J. Wong int error;
2670a9633faSDarrick J. Wong
2680a9633faSDarrick J. Wong if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
2690a9633faSDarrick J. Wong return 0;
2700a9633faSDarrick J. Wong
2710a9633faSDarrick J. Wong pag = xfs_perag_get(mp, sm->sm_agno);
2727ac2ff8bSDave Chinner if (xfs_perag_initialised_agi(pag)) {
2731fc25f51SDarrick J. Wong /* Use in-core icount if possible. */
2740a9633faSDarrick J. Wong icount = pag->pagi_count;
2750a9633faSDarrick J. Wong } else {
2761fc25f51SDarrick J. Wong /* Try to get the actual counters from disk. */
27799b13c7fSDave Chinner error = xfs_ialloc_read_agi(pag, NULL, &bp);
2781fc25f51SDarrick J. Wong if (!error) {
2790a9633faSDarrick J. Wong icount = pag->pagi_count;
2800a9633faSDarrick J. Wong xfs_buf_relse(bp);
2810a9633faSDarrick J. Wong }
2820a9633faSDarrick J. Wong }
2830a9633faSDarrick J. Wong
2840a9633faSDarrick J. Wong /* Now grab the block counters from the AGF. */
28508d3e84fSDave Chinner error = xfs_alloc_read_agf(pag, NULL, 0, &bp);
2861aa26707SDarrick J. Wong if (error) {
2873829c9a1SDave Chinner aglen = pag->block_count;
2881aa26707SDarrick J. Wong freelen = aglen;
2891aa26707SDarrick J. Wong usedlen = aglen;
2901aa26707SDarrick J. Wong } else {
2919798f615SChristoph Hellwig struct xfs_agf *agf = bp->b_addr;
2929798f615SChristoph Hellwig
2939798f615SChristoph Hellwig aglen = be32_to_cpu(agf->agf_length);
2949798f615SChristoph Hellwig freelen = be32_to_cpu(agf->agf_freeblks);
2950a9633faSDarrick J. Wong usedlen = aglen - freelen;
2960a9633faSDarrick J. Wong xfs_buf_relse(bp);
2970a9633faSDarrick J. Wong }
2980a9633faSDarrick J. Wong
2991fc25f51SDarrick J. Wong /* If the icount is impossible, make some worst-case assumptions. */
3001fc25f51SDarrick J. Wong if (icount == NULLAGINO ||
3012d6ca832SDave Chinner !xfs_verify_agino(pag, icount)) {
3022d6ca832SDave Chinner icount = pag->agino_max - pag->agino_min + 1;
3031fc25f51SDarrick J. Wong }
3041fc25f51SDarrick J. Wong
3051fc25f51SDarrick J. Wong /* If the block counts are impossible, make worst-case assumptions. */
3061fc25f51SDarrick J. Wong if (aglen == NULLAGBLOCK ||
3073829c9a1SDave Chinner aglen != pag->block_count ||
3081fc25f51SDarrick J. Wong freelen >= aglen) {
3093829c9a1SDave Chinner aglen = pag->block_count;
3101fc25f51SDarrick J. Wong freelen = aglen;
3111fc25f51SDarrick J. Wong usedlen = aglen;
3121fc25f51SDarrick J. Wong }
3133829c9a1SDave Chinner xfs_perag_put(pag);
3141fc25f51SDarrick J. Wong
315b5e2196eSDarrick J. Wong trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
3160a9633faSDarrick J. Wong freelen, usedlen);
3170a9633faSDarrick J. Wong
3180a9633faSDarrick J. Wong /*
3190a9633faSDarrick J. Wong * Figure out how many blocks we'd need worst case to rebuild
3200a9633faSDarrick J. Wong * each type of btree. Note that we can only rebuild the
3210a9633faSDarrick J. Wong * bnobt/cntbt or inobt/finobt as pairs.
3220a9633faSDarrick J. Wong */
3230a9633faSDarrick J. Wong bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
32438c26bfdSDave Chinner if (xfs_has_sparseinodes(mp))
3250a9633faSDarrick J. Wong inobt_sz = xfs_iallocbt_calc_size(mp, icount /
3260a9633faSDarrick J. Wong XFS_INODES_PER_HOLEMASK_BIT);
3270a9633faSDarrick J. Wong else
3280a9633faSDarrick J. Wong inobt_sz = xfs_iallocbt_calc_size(mp, icount /
3290a9633faSDarrick J. Wong XFS_INODES_PER_CHUNK);
33038c26bfdSDave Chinner if (xfs_has_finobt(mp))
3310a9633faSDarrick J. Wong inobt_sz *= 2;
33238c26bfdSDave Chinner if (xfs_has_reflink(mp))
3330a9633faSDarrick J. Wong refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
3340a9633faSDarrick J. Wong else
3350a9633faSDarrick J. Wong refcbt_sz = 0;
33638c26bfdSDave Chinner if (xfs_has_rmapbt(mp)) {
3370a9633faSDarrick J. Wong /*
3380a9633faSDarrick J. Wong * Guess how many blocks we need to rebuild the rmapbt.
3390a9633faSDarrick J. Wong * For non-reflink filesystems we can't have more records than
3400a9633faSDarrick J. Wong * used blocks. However, with reflink it's possible to have
3410a9633faSDarrick J. Wong * more than one rmap record per AG block. We don't know how
3420a9633faSDarrick J. Wong * many rmaps there could be in the AG, so we start off with
3430a9633faSDarrick J. Wong * what we hope is an generous over-estimation.
3440a9633faSDarrick J. Wong */
34538c26bfdSDave Chinner if (xfs_has_reflink(mp))
3460a9633faSDarrick J. Wong rmapbt_sz = xfs_rmapbt_calc_size(mp,
3470a9633faSDarrick J. Wong (unsigned long long)aglen * 2);
3480a9633faSDarrick J. Wong else
3490a9633faSDarrick J. Wong rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
3500a9633faSDarrick J. Wong } else {
3510a9633faSDarrick J. Wong rmapbt_sz = 0;
3520a9633faSDarrick J. Wong }
3530a9633faSDarrick J. Wong
354b5e2196eSDarrick J. Wong trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
3550a9633faSDarrick J. Wong inobt_sz, rmapbt_sz, refcbt_sz);
3560a9633faSDarrick J. Wong
3570a9633faSDarrick J. Wong return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
3580a9633faSDarrick J. Wong }
35973d6b42aSDarrick J. Wong
36064a39d87SDarrick J. Wong /*
36164a39d87SDarrick J. Wong * Reconstructing per-AG Btrees
36264a39d87SDarrick J. Wong *
36364a39d87SDarrick J. Wong * When a space btree is corrupt, we don't bother trying to fix it. Instead,
36464a39d87SDarrick J. Wong * we scan secondary space metadata to derive the records that should be in
36564a39d87SDarrick J. Wong * the damaged btree, initialize a fresh btree root, and insert the records.
36664a39d87SDarrick J. Wong * Note that for rebuilding the rmapbt we scan all the primary data to
36764a39d87SDarrick J. Wong * generate the new records.
36864a39d87SDarrick J. Wong *
36964a39d87SDarrick J. Wong * However, that leaves the matter of removing all the metadata describing the
37064a39d87SDarrick J. Wong * old broken structure. For primary metadata we use the rmap data to collect
37186d969b4SDarrick J. Wong * every extent with a matching rmap owner (bitmap); we then iterate all other
37264a39d87SDarrick J. Wong * metadata structures with the same rmap owner to collect the extents that
37386d969b4SDarrick J. Wong * cannot be removed (sublist). We then subtract sublist from bitmap to
37464a39d87SDarrick J. Wong * derive the blocks that were used by the old btree. These blocks can be
37564a39d87SDarrick J. Wong * reaped.
37664a39d87SDarrick J. Wong *
37764a39d87SDarrick J. Wong * For rmapbt reconstructions we must use different tactics for extent
37864a39d87SDarrick J. Wong * collection. First we iterate all primary metadata (this excludes the old
37964a39d87SDarrick J. Wong * rmapbt, obviously) to generate new rmap records. The gaps in the rmap
38086d969b4SDarrick J. Wong * records are collected as bitmap. The bnobt records are collected as
38186d969b4SDarrick J. Wong * sublist. As with the other btrees we subtract sublist from bitmap, and the
38264a39d87SDarrick J. Wong * result (since the rmapbt lives in the free space) are the blocks from the
38364a39d87SDarrick J. Wong * old rmapbt.
38412c6510eSDarrick J. Wong */
38512c6510eSDarrick J. Wong
38612c6510eSDarrick J. Wong /* Ensure the freelist is the correct size. */
38712c6510eSDarrick J. Wong int
xrep_fix_freelist(struct xfs_scrub * sc,bool can_shrink)388b5e2196eSDarrick J. Wong xrep_fix_freelist(
3891d8a748aSDarrick J. Wong struct xfs_scrub *sc,
39012c6510eSDarrick J. Wong bool can_shrink)
39112c6510eSDarrick J. Wong {
39212c6510eSDarrick J. Wong struct xfs_alloc_arg args = {0};
39312c6510eSDarrick J. Wong
39412c6510eSDarrick J. Wong args.mp = sc->mp;
39512c6510eSDarrick J. Wong args.tp = sc->tp;
39654406764SDarrick J. Wong args.agno = sc->sa.pag->pag_agno;
39712c6510eSDarrick J. Wong args.alignment = 1;
39812c6510eSDarrick J. Wong args.pag = sc->sa.pag;
39912c6510eSDarrick J. Wong
40012c6510eSDarrick J. Wong return xfs_alloc_fix_freelist(&args,
40112c6510eSDarrick J. Wong can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
40212c6510eSDarrick J. Wong }
40312c6510eSDarrick J. Wong
40404a2b7b2SDarrick J. Wong /*
40504a2b7b2SDarrick J. Wong * Finding per-AG Btree Roots for AGF/AGI Reconstruction
40604a2b7b2SDarrick J. Wong *
40704a2b7b2SDarrick J. Wong * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
40804a2b7b2SDarrick J. Wong * the AG headers by using the rmap data to rummage through the AG looking for
40904a2b7b2SDarrick J. Wong * btree roots. This is not guaranteed to work if the AG is heavily damaged
41004a2b7b2SDarrick J. Wong * or the rmap data are corrupt.
41104a2b7b2SDarrick J. Wong *
412b5e2196eSDarrick J. Wong * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
41304a2b7b2SDarrick J. Wong * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
41404a2b7b2SDarrick J. Wong * AGI is being rebuilt. It must maintain these locks until it's safe for
41504a2b7b2SDarrick J. Wong * other threads to change the btrees' shapes. The caller provides
41604a2b7b2SDarrick J. Wong * information about the btrees to look for by passing in an array of
417b5e2196eSDarrick J. Wong * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
41804a2b7b2SDarrick J. Wong * The (root, height) fields will be set on return if anything is found. The
41904a2b7b2SDarrick J. Wong * last element of the array should have a NULL buf_ops to mark the end of the
42004a2b7b2SDarrick J. Wong * array.
42104a2b7b2SDarrick J. Wong *
42204a2b7b2SDarrick J. Wong * For every rmapbt record matching any of the rmap owners in btree_info,
42304a2b7b2SDarrick J. Wong * read each block referenced by the rmap record. If the block is a btree
42404a2b7b2SDarrick J. Wong * block from this filesystem matching any of the magic numbers and has a
42504a2b7b2SDarrick J. Wong * level higher than what we've already seen, remember the block and the
42604a2b7b2SDarrick J. Wong * height of the tree required to have such a block. When the call completes,
42704a2b7b2SDarrick J. Wong * we return the highest block we've found for each btree description; those
42804a2b7b2SDarrick J. Wong * should be the roots.
42904a2b7b2SDarrick J. Wong */
43004a2b7b2SDarrick J. Wong
431b5e2196eSDarrick J. Wong struct xrep_findroot {
4321d8a748aSDarrick J. Wong struct xfs_scrub *sc;
43304a2b7b2SDarrick J. Wong struct xfs_buf *agfl_bp;
43404a2b7b2SDarrick J. Wong struct xfs_agf *agf;
435b5e2196eSDarrick J. Wong struct xrep_find_ag_btree *btree_info;
43604a2b7b2SDarrick J. Wong };
43704a2b7b2SDarrick J. Wong
43804a2b7b2SDarrick J. Wong /* See if our block is in the AGFL. */
43904a2b7b2SDarrick J. Wong STATIC int
xrep_findroot_agfl_walk(struct xfs_mount * mp,xfs_agblock_t bno,void * priv)440b5e2196eSDarrick J. Wong xrep_findroot_agfl_walk(
44104a2b7b2SDarrick J. Wong struct xfs_mount *mp,
44204a2b7b2SDarrick J. Wong xfs_agblock_t bno,
44304a2b7b2SDarrick J. Wong void *priv)
44404a2b7b2SDarrick J. Wong {
44504a2b7b2SDarrick J. Wong xfs_agblock_t *agbno = priv;
44604a2b7b2SDarrick J. Wong
447e7ee96dfSDarrick J. Wong return (*agbno == bno) ? -ECANCELED : 0;
44804a2b7b2SDarrick J. Wong }
44904a2b7b2SDarrick J. Wong
45004a2b7b2SDarrick J. Wong /* Does this block match the btree information passed in? */
45104a2b7b2SDarrick J. Wong STATIC int
xrep_findroot_block(struct xrep_findroot * ri,struct xrep_find_ag_btree * fab,uint64_t owner,xfs_agblock_t agbno,bool * done_with_block)452b5e2196eSDarrick J. Wong xrep_findroot_block(
453b5e2196eSDarrick J. Wong struct xrep_findroot *ri,
454b5e2196eSDarrick J. Wong struct xrep_find_ag_btree *fab,
45504a2b7b2SDarrick J. Wong uint64_t owner,
45604a2b7b2SDarrick J. Wong xfs_agblock_t agbno,
4571002ff45SDarrick J. Wong bool *done_with_block)
45804a2b7b2SDarrick J. Wong {
45904a2b7b2SDarrick J. Wong struct xfs_mount *mp = ri->sc->mp;
46004a2b7b2SDarrick J. Wong struct xfs_buf *bp;
46104a2b7b2SDarrick J. Wong struct xfs_btree_block *btblock;
46204a2b7b2SDarrick J. Wong xfs_daddr_t daddr;
4631002ff45SDarrick J. Wong int block_level;
46438b6238eSDarrick J. Wong int error = 0;
46504a2b7b2SDarrick J. Wong
46654406764SDarrick J. Wong daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.pag->pag_agno, agbno);
46704a2b7b2SDarrick J. Wong
46804a2b7b2SDarrick J. Wong /*
46904a2b7b2SDarrick J. Wong * Blocks in the AGFL have stale contents that might just happen to
47004a2b7b2SDarrick J. Wong * have a matching magic and uuid. We don't want to pull these blocks
47104a2b7b2SDarrick J. Wong * in as part of a tree root, so we have to filter out the AGFL stuff
47204a2b7b2SDarrick J. Wong * here. If the AGFL looks insane we'll just refuse to repair.
47304a2b7b2SDarrick J. Wong */
47404a2b7b2SDarrick J. Wong if (owner == XFS_RMAP_OWN_AG) {
47504a2b7b2SDarrick J. Wong error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
476b5e2196eSDarrick J. Wong xrep_findroot_agfl_walk, &agbno);
477e7ee96dfSDarrick J. Wong if (error == -ECANCELED)
47804a2b7b2SDarrick J. Wong return 0;
47904a2b7b2SDarrick J. Wong if (error)
48004a2b7b2SDarrick J. Wong return error;
48104a2b7b2SDarrick J. Wong }
48204a2b7b2SDarrick J. Wong
48338b6238eSDarrick J. Wong /*
48438b6238eSDarrick J. Wong * Read the buffer into memory so that we can see if it's a match for
48538b6238eSDarrick J. Wong * our btree type. We have no clue if it is beforehand, and we want to
48638b6238eSDarrick J. Wong * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
48738b6238eSDarrick J. Wong * will cause needless disk reads in subsequent calls to this function)
48838b6238eSDarrick J. Wong * and logging metadata verifier failures.
48938b6238eSDarrick J. Wong *
49038b6238eSDarrick J. Wong * Therefore, pass in NULL buffer ops. If the buffer was already in
49138b6238eSDarrick J. Wong * memory from some other caller it will already have b_ops assigned.
49238b6238eSDarrick J. Wong * If it was in memory from a previous unsuccessful findroot_block
49338b6238eSDarrick J. Wong * call, the buffer won't have b_ops but it should be clean and ready
49438b6238eSDarrick J. Wong * for us to try to verify if the read call succeeds. The same applies
49538b6238eSDarrick J. Wong * if the buffer wasn't in memory at all.
49638b6238eSDarrick J. Wong *
49738b6238eSDarrick J. Wong * Note: If we never match a btree type with this buffer, it will be
49838b6238eSDarrick J. Wong * left in memory with NULL b_ops. This shouldn't be a problem unless
49938b6238eSDarrick J. Wong * the buffer gets written.
50038b6238eSDarrick J. Wong */
50104a2b7b2SDarrick J. Wong error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
50204a2b7b2SDarrick J. Wong mp->m_bsize, 0, &bp, NULL);
50304a2b7b2SDarrick J. Wong if (error)
50404a2b7b2SDarrick J. Wong return error;
50504a2b7b2SDarrick J. Wong
50638b6238eSDarrick J. Wong /* Ensure the block magic matches the btree type we're looking for. */
50704a2b7b2SDarrick J. Wong btblock = XFS_BUF_TO_BLOCK(bp);
5089228d751SDarrick J. Wong ASSERT(fab->buf_ops->magic[1] != 0);
5099228d751SDarrick J. Wong if (btblock->bb_magic != fab->buf_ops->magic[1])
51004a2b7b2SDarrick J. Wong goto out;
51104a2b7b2SDarrick J. Wong
51238b6238eSDarrick J. Wong /*
51338b6238eSDarrick J. Wong * If the buffer already has ops applied and they're not the ones for
51438b6238eSDarrick J. Wong * this btree type, we know this block doesn't match the btree and we
51538b6238eSDarrick J. Wong * can bail out.
51638b6238eSDarrick J. Wong *
51738b6238eSDarrick J. Wong * If the buffer ops match ours, someone else has already validated
51838b6238eSDarrick J. Wong * the block for us, so we can move on to checking if this is a root
51938b6238eSDarrick J. Wong * block candidate.
52038b6238eSDarrick J. Wong *
52138b6238eSDarrick J. Wong * If the buffer does not have ops, nobody has successfully validated
52238b6238eSDarrick J. Wong * the contents and the buffer cannot be dirty. If the magic, uuid,
52338b6238eSDarrick J. Wong * and structure match this btree type then we'll move on to checking
52438b6238eSDarrick J. Wong * if it's a root block candidate. If there is no match, bail out.
52538b6238eSDarrick J. Wong */
52638b6238eSDarrick J. Wong if (bp->b_ops) {
52738b6238eSDarrick J. Wong if (bp->b_ops != fab->buf_ops)
52804a2b7b2SDarrick J. Wong goto out;
52938b6238eSDarrick J. Wong } else {
53038b6238eSDarrick J. Wong ASSERT(!xfs_trans_buf_is_dirty(bp));
53138b6238eSDarrick J. Wong if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
53238b6238eSDarrick J. Wong &mp->m_sb.sb_meta_uuid))
53338b6238eSDarrick J. Wong goto out;
534add46b3bSDarrick J. Wong /*
535add46b3bSDarrick J. Wong * Read verifiers can reference b_ops, so we set the pointer
536add46b3bSDarrick J. Wong * here. If the verifier fails we'll reset the buffer state
537add46b3bSDarrick J. Wong * to what it was before we touched the buffer.
538add46b3bSDarrick J. Wong */
539add46b3bSDarrick J. Wong bp->b_ops = fab->buf_ops;
54038b6238eSDarrick J. Wong fab->buf_ops->verify_read(bp);
54138b6238eSDarrick J. Wong if (bp->b_error) {
542add46b3bSDarrick J. Wong bp->b_ops = NULL;
54338b6238eSDarrick J. Wong bp->b_error = 0;
54438b6238eSDarrick J. Wong goto out;
54538b6238eSDarrick J. Wong }
54638b6238eSDarrick J. Wong
54738b6238eSDarrick J. Wong /*
54838b6238eSDarrick J. Wong * Some read verifiers will (re)set b_ops, so we must be
549add46b3bSDarrick J. Wong * careful not to change b_ops after running the verifier.
55038b6238eSDarrick J. Wong */
55138b6238eSDarrick J. Wong }
5521002ff45SDarrick J. Wong
5531002ff45SDarrick J. Wong /*
5541002ff45SDarrick J. Wong * This block passes the magic/uuid and verifier tests for this btree
5551002ff45SDarrick J. Wong * type. We don't need the caller to try the other tree types.
5561002ff45SDarrick J. Wong */
5571002ff45SDarrick J. Wong *done_with_block = true;
5581002ff45SDarrick J. Wong
5591002ff45SDarrick J. Wong /*
5601002ff45SDarrick J. Wong * Compare this btree block's level to the height of the current
5611002ff45SDarrick J. Wong * candidate root block.
5621002ff45SDarrick J. Wong *
5631002ff45SDarrick J. Wong * If the level matches the root we found previously, throw away both
5641002ff45SDarrick J. Wong * blocks because there can't be two candidate roots.
5651002ff45SDarrick J. Wong *
5661002ff45SDarrick J. Wong * If level is lower in the tree than the root we found previously,
5671002ff45SDarrick J. Wong * ignore this block.
5681002ff45SDarrick J. Wong */
5691002ff45SDarrick J. Wong block_level = xfs_btree_get_level(btblock);
5701002ff45SDarrick J. Wong if (block_level + 1 == fab->height) {
5711002ff45SDarrick J. Wong fab->root = NULLAGBLOCK;
5721002ff45SDarrick J. Wong goto out;
5731002ff45SDarrick J. Wong } else if (block_level < fab->height) {
5741002ff45SDarrick J. Wong goto out;
5751002ff45SDarrick J. Wong }
5761002ff45SDarrick J. Wong
5771002ff45SDarrick J. Wong /*
5781002ff45SDarrick J. Wong * This is the highest block in the tree that we've found so far.
5791002ff45SDarrick J. Wong * Update the btree height to reflect what we've learned from this
5801002ff45SDarrick J. Wong * block.
5811002ff45SDarrick J. Wong */
5821002ff45SDarrick J. Wong fab->height = block_level + 1;
5831002ff45SDarrick J. Wong
5841002ff45SDarrick J. Wong /*
5851002ff45SDarrick J. Wong * If this block doesn't have sibling pointers, then it's the new root
5861002ff45SDarrick J. Wong * block candidate. Otherwise, the root will be found farther up the
5871002ff45SDarrick J. Wong * tree.
5881002ff45SDarrick J. Wong */
5891002ff45SDarrick J. Wong if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
5901002ff45SDarrick J. Wong btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
59104a2b7b2SDarrick J. Wong fab->root = agbno;
5921002ff45SDarrick J. Wong else
5931002ff45SDarrick J. Wong fab->root = NULLAGBLOCK;
59404a2b7b2SDarrick J. Wong
59554406764SDarrick J. Wong trace_xrep_findroot_block(mp, ri->sc->sa.pag->pag_agno, agbno,
59604a2b7b2SDarrick J. Wong be32_to_cpu(btblock->bb_magic), fab->height - 1);
59704a2b7b2SDarrick J. Wong out:
59804a2b7b2SDarrick J. Wong xfs_trans_brelse(ri->sc->tp, bp);
59904a2b7b2SDarrick J. Wong return error;
60004a2b7b2SDarrick J. Wong }
60104a2b7b2SDarrick J. Wong
60204a2b7b2SDarrick J. Wong /*
60304a2b7b2SDarrick J. Wong * Do any of the blocks in this rmap record match one of the btrees we're
60404a2b7b2SDarrick J. Wong * looking for?
60504a2b7b2SDarrick J. Wong */
60604a2b7b2SDarrick J. Wong STATIC int
xrep_findroot_rmap(struct xfs_btree_cur * cur,const struct xfs_rmap_irec * rec,void * priv)607b5e2196eSDarrick J. Wong xrep_findroot_rmap(
60804a2b7b2SDarrick J. Wong struct xfs_btree_cur *cur,
609159eb69dSDarrick J. Wong const struct xfs_rmap_irec *rec,
61004a2b7b2SDarrick J. Wong void *priv)
61104a2b7b2SDarrick J. Wong {
612b5e2196eSDarrick J. Wong struct xrep_findroot *ri = priv;
613b5e2196eSDarrick J. Wong struct xrep_find_ag_btree *fab;
61404a2b7b2SDarrick J. Wong xfs_agblock_t b;
6151002ff45SDarrick J. Wong bool done;
61604a2b7b2SDarrick J. Wong int error = 0;
61704a2b7b2SDarrick J. Wong
61804a2b7b2SDarrick J. Wong /* Ignore anything that isn't AG metadata. */
61904a2b7b2SDarrick J. Wong if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
62004a2b7b2SDarrick J. Wong return 0;
62104a2b7b2SDarrick J. Wong
62204a2b7b2SDarrick J. Wong /* Otherwise scan each block + btree type. */
62304a2b7b2SDarrick J. Wong for (b = 0; b < rec->rm_blockcount; b++) {
6241002ff45SDarrick J. Wong done = false;
62504a2b7b2SDarrick J. Wong for (fab = ri->btree_info; fab->buf_ops; fab++) {
62604a2b7b2SDarrick J. Wong if (rec->rm_owner != fab->rmap_owner)
62704a2b7b2SDarrick J. Wong continue;
628b5e2196eSDarrick J. Wong error = xrep_findroot_block(ri, fab,
62904a2b7b2SDarrick J. Wong rec->rm_owner, rec->rm_startblock + b,
6301002ff45SDarrick J. Wong &done);
63104a2b7b2SDarrick J. Wong if (error)
63204a2b7b2SDarrick J. Wong return error;
6331002ff45SDarrick J. Wong if (done)
63404a2b7b2SDarrick J. Wong break;
63504a2b7b2SDarrick J. Wong }
63604a2b7b2SDarrick J. Wong }
63704a2b7b2SDarrick J. Wong
63804a2b7b2SDarrick J. Wong return 0;
63904a2b7b2SDarrick J. Wong }
64004a2b7b2SDarrick J. Wong
64104a2b7b2SDarrick J. Wong /* Find the roots of the per-AG btrees described in btree_info. */
64204a2b7b2SDarrick J. Wong int
xrep_find_ag_btree_roots(struct xfs_scrub * sc,struct xfs_buf * agf_bp,struct xrep_find_ag_btree * btree_info,struct xfs_buf * agfl_bp)643b5e2196eSDarrick J. Wong xrep_find_ag_btree_roots(
6441d8a748aSDarrick J. Wong struct xfs_scrub *sc,
64504a2b7b2SDarrick J. Wong struct xfs_buf *agf_bp,
646b5e2196eSDarrick J. Wong struct xrep_find_ag_btree *btree_info,
64704a2b7b2SDarrick J. Wong struct xfs_buf *agfl_bp)
64804a2b7b2SDarrick J. Wong {
64904a2b7b2SDarrick J. Wong struct xfs_mount *mp = sc->mp;
650b5e2196eSDarrick J. Wong struct xrep_findroot ri;
651b5e2196eSDarrick J. Wong struct xrep_find_ag_btree *fab;
65204a2b7b2SDarrick J. Wong struct xfs_btree_cur *cur;
65304a2b7b2SDarrick J. Wong int error;
65404a2b7b2SDarrick J. Wong
65504a2b7b2SDarrick J. Wong ASSERT(xfs_buf_islocked(agf_bp));
65604a2b7b2SDarrick J. Wong ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
65704a2b7b2SDarrick J. Wong
65804a2b7b2SDarrick J. Wong ri.sc = sc;
65904a2b7b2SDarrick J. Wong ri.btree_info = btree_info;
6609798f615SChristoph Hellwig ri.agf = agf_bp->b_addr;
66104a2b7b2SDarrick J. Wong ri.agfl_bp = agfl_bp;
66204a2b7b2SDarrick J. Wong for (fab = btree_info; fab->buf_ops; fab++) {
66304a2b7b2SDarrick J. Wong ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
66404a2b7b2SDarrick J. Wong ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
66504a2b7b2SDarrick J. Wong fab->root = NULLAGBLOCK;
66604a2b7b2SDarrick J. Wong fab->height = 0;
66704a2b7b2SDarrick J. Wong }
66804a2b7b2SDarrick J. Wong
669fa9c3c19SDave Chinner cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
670b5e2196eSDarrick J. Wong error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
6710b04b6b8SDarrick J. Wong xfs_btree_del_cursor(cur, error);
67204a2b7b2SDarrick J. Wong
67304a2b7b2SDarrick J. Wong return error;
67404a2b7b2SDarrick J. Wong }
6757e85bc6cSDarrick J. Wong
6767e85bc6cSDarrick J. Wong /* Force a quotacheck the next time we mount. */
6777e85bc6cSDarrick J. Wong void
xrep_force_quotacheck(struct xfs_scrub * sc,xfs_dqtype_t type)678b5e2196eSDarrick J. Wong xrep_force_quotacheck(
6791d8a748aSDarrick J. Wong struct xfs_scrub *sc,
6801a7ed271SDarrick J. Wong xfs_dqtype_t type)
6817e85bc6cSDarrick J. Wong {
6827e85bc6cSDarrick J. Wong uint flag;
6837e85bc6cSDarrick J. Wong
6841a7ed271SDarrick J. Wong flag = xfs_quota_chkd_flag(type);
6857e85bc6cSDarrick J. Wong if (!(flag & sc->mp->m_qflags))
6867e85bc6cSDarrick J. Wong return;
6877e85bc6cSDarrick J. Wong
68859d7fab2SDarrick J. Wong mutex_lock(&sc->mp->m_quotainfo->qi_quotaofflock);
6897e85bc6cSDarrick J. Wong sc->mp->m_qflags &= ~flag;
6907e85bc6cSDarrick J. Wong spin_lock(&sc->mp->m_sb_lock);
6917e85bc6cSDarrick J. Wong sc->mp->m_sb.sb_qflags &= ~flag;
6927e85bc6cSDarrick J. Wong spin_unlock(&sc->mp->m_sb_lock);
6937e85bc6cSDarrick J. Wong xfs_log_sb(sc->tp);
69459d7fab2SDarrick J. Wong mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock);
6957e85bc6cSDarrick J. Wong }
6967e85bc6cSDarrick J. Wong
6977e85bc6cSDarrick J. Wong /*
6987e85bc6cSDarrick J. Wong * Attach dquots to this inode, or schedule quotacheck to fix them.
6997e85bc6cSDarrick J. Wong *
7007e85bc6cSDarrick J. Wong * This function ensures that the appropriate dquots are attached to an inode.
7017e85bc6cSDarrick J. Wong * We cannot allow the dquot code to allocate an on-disk dquot block here
7027e85bc6cSDarrick J. Wong * because we're already in transaction context with the inode locked. The
7037e85bc6cSDarrick J. Wong * on-disk dquot should already exist anyway. If the quota code signals
7047e85bc6cSDarrick J. Wong * corruption or missing quota information, schedule quotacheck, which will
7057e85bc6cSDarrick J. Wong * repair corruptions in the quota metadata.
7067e85bc6cSDarrick J. Wong */
7077e85bc6cSDarrick J. Wong int
xrep_ino_dqattach(struct xfs_scrub * sc)708b5e2196eSDarrick J. Wong xrep_ino_dqattach(
7091d8a748aSDarrick J. Wong struct xfs_scrub *sc)
7107e85bc6cSDarrick J. Wong {
7117e85bc6cSDarrick J. Wong int error;
7127e85bc6cSDarrick J. Wong
7137e85bc6cSDarrick J. Wong error = xfs_qm_dqattach_locked(sc->ip, false);
7147e85bc6cSDarrick J. Wong switch (error) {
7157e85bc6cSDarrick J. Wong case -EFSBADCRC:
7167e85bc6cSDarrick J. Wong case -EFSCORRUPTED:
7177e85bc6cSDarrick J. Wong case -ENOENT:
7187e85bc6cSDarrick J. Wong xfs_err_ratelimited(sc->mp,
7197e85bc6cSDarrick J. Wong "inode %llu repair encountered quota error %d, quotacheck forced.",
7207e85bc6cSDarrick J. Wong (unsigned long long)sc->ip->i_ino, error);
7217e85bc6cSDarrick J. Wong if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
7228cd4901dSDarrick J. Wong xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
7237e85bc6cSDarrick J. Wong if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
7248cd4901dSDarrick J. Wong xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
7257e85bc6cSDarrick J. Wong if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
7268cd4901dSDarrick J. Wong xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
72753004ee7SGustavo A. R. Silva fallthrough;
7287e85bc6cSDarrick J. Wong case -ESRCH:
7297e85bc6cSDarrick J. Wong error = 0;
7307e85bc6cSDarrick J. Wong break;
7317e85bc6cSDarrick J. Wong default:
7327e85bc6cSDarrick J. Wong break;
7337e85bc6cSDarrick J. Wong }
7347e85bc6cSDarrick J. Wong
7357e85bc6cSDarrick J. Wong return error;
7367e85bc6cSDarrick J. Wong }
737