18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Oracle.  All Rights Reserved.
48c2ecf20Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include "xfs.h"
78c2ecf20Sopenharmony_ci#include "xfs_fs.h"
88c2ecf20Sopenharmony_ci#include "xfs_shared.h"
98c2ecf20Sopenharmony_ci#include "xfs_format.h"
108c2ecf20Sopenharmony_ci#include "xfs_trans_resv.h"
118c2ecf20Sopenharmony_ci#include "xfs_mount.h"
128c2ecf20Sopenharmony_ci#include "xfs_btree.h"
138c2ecf20Sopenharmony_ci#include "xfs_alloc.h"
148c2ecf20Sopenharmony_ci#include "xfs_rmap.h"
158c2ecf20Sopenharmony_ci#include "scrub/scrub.h"
168c2ecf20Sopenharmony_ci#include "scrub/common.h"
178c2ecf20Sopenharmony_ci#include "scrub/btree.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Set us up to scrub free space btrees.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ciint
238c2ecf20Sopenharmony_cixchk_setup_ag_allocbt(
248c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
258c2ecf20Sopenharmony_ci	struct xfs_inode	*ip)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	return xchk_setup_ag_btree(sc, ip, false);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Free space btree scrubber. */
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * Ensure there's a corresponding cntbt/bnobt record matching this
338c2ecf20Sopenharmony_ci * bnobt/cntbt record, respectively.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ciSTATIC void
368c2ecf20Sopenharmony_cixchk_allocbt_xref_other(
378c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
388c2ecf20Sopenharmony_ci	xfs_agblock_t		agbno,
398c2ecf20Sopenharmony_ci	xfs_extlen_t		len)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct xfs_btree_cur	**pcur;
428c2ecf20Sopenharmony_ci	xfs_agblock_t		fbno;
438c2ecf20Sopenharmony_ci	xfs_extlen_t		flen;
448c2ecf20Sopenharmony_ci	int			has_otherrec;
458c2ecf20Sopenharmony_ci	int			error;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
488c2ecf20Sopenharmony_ci		pcur = &sc->sa.cnt_cur;
498c2ecf20Sopenharmony_ci	else
508c2ecf20Sopenharmony_ci		pcur = &sc->sa.bno_cur;
518c2ecf20Sopenharmony_ci	if (!*pcur || xchk_skip_xref(sc->sm))
528c2ecf20Sopenharmony_ci		return;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
558c2ecf20Sopenharmony_ci	if (!xchk_should_check_xref(sc, &error, pcur))
568c2ecf20Sopenharmony_ci		return;
578c2ecf20Sopenharmony_ci	if (!has_otherrec) {
588c2ecf20Sopenharmony_ci		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
598c2ecf20Sopenharmony_ci		return;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &has_otherrec);
638c2ecf20Sopenharmony_ci	if (!xchk_should_check_xref(sc, &error, pcur))
648c2ecf20Sopenharmony_ci		return;
658c2ecf20Sopenharmony_ci	if (!has_otherrec) {
668c2ecf20Sopenharmony_ci		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
678c2ecf20Sopenharmony_ci		return;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (fbno != agbno || flen != len)
718c2ecf20Sopenharmony_ci		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* Cross-reference with the other btrees. */
758c2ecf20Sopenharmony_ciSTATIC void
768c2ecf20Sopenharmony_cixchk_allocbt_xref(
778c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
788c2ecf20Sopenharmony_ci	xfs_agblock_t		agbno,
798c2ecf20Sopenharmony_ci	xfs_extlen_t		len)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
828c2ecf20Sopenharmony_ci		return;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	xchk_allocbt_xref_other(sc, agbno, len);
858c2ecf20Sopenharmony_ci	xchk_xref_is_not_inode_chunk(sc, agbno, len);
868c2ecf20Sopenharmony_ci	xchk_xref_has_no_owner(sc, agbno, len);
878c2ecf20Sopenharmony_ci	xchk_xref_is_not_shared(sc, agbno, len);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* Scrub a bnobt/cntbt record. */
918c2ecf20Sopenharmony_ciSTATIC int
928c2ecf20Sopenharmony_cixchk_allocbt_rec(
938c2ecf20Sopenharmony_ci	struct xchk_btree	*bs,
948c2ecf20Sopenharmony_ci	union xfs_btree_rec	*rec)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = bs->cur->bc_mp;
978c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno = bs->cur->bc_ag.agno;
988c2ecf20Sopenharmony_ci	xfs_agblock_t		bno;
998c2ecf20Sopenharmony_ci	xfs_extlen_t		len;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	bno = be32_to_cpu(rec->alloc.ar_startblock);
1028c2ecf20Sopenharmony_ci	len = be32_to_cpu(rec->alloc.ar_blockcount);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (bno + len <= bno ||
1058c2ecf20Sopenharmony_ci	    !xfs_verify_agbno(mp, agno, bno) ||
1068c2ecf20Sopenharmony_ci	    !xfs_verify_agbno(mp, agno, bno + len - 1))
1078c2ecf20Sopenharmony_ci		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	xchk_allocbt_xref(bs->sc, bno, len);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* Scrub the freespace btrees for some AG. */
1158c2ecf20Sopenharmony_ciSTATIC int
1168c2ecf20Sopenharmony_cixchk_allocbt(
1178c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1188c2ecf20Sopenharmony_ci	xfs_btnum_t		which)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct xfs_btree_cur	*cur;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
1238c2ecf20Sopenharmony_ci	return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, NULL);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciint
1278c2ecf20Sopenharmony_cixchk_bnobt(
1288c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	return xchk_allocbt(sc, XFS_BTNUM_BNO);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciint
1348c2ecf20Sopenharmony_cixchk_cntbt(
1358c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return xchk_allocbt(sc, XFS_BTNUM_CNT);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* xref check that the extent is not free */
1418c2ecf20Sopenharmony_civoid
1428c2ecf20Sopenharmony_cixchk_xref_is_used_space(
1438c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1448c2ecf20Sopenharmony_ci	xfs_agblock_t		agbno,
1458c2ecf20Sopenharmony_ci	xfs_extlen_t		len)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	bool			is_freesp;
1488c2ecf20Sopenharmony_ci	int			error;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
1518c2ecf20Sopenharmony_ci		return;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	error = xfs_alloc_has_record(sc->sa.bno_cur, agbno, len, &is_freesp);
1548c2ecf20Sopenharmony_ci	if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
1558c2ecf20Sopenharmony_ci		return;
1568c2ecf20Sopenharmony_ci	if (is_freesp)
1578c2ecf20Sopenharmony_ci		xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);
1588c2ecf20Sopenharmony_ci}
159