xref: /OK3568_Linux_fs/kernel/fs/xfs/scrub/alloc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4*4882a593Smuzhiyun  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_trans_resv.h"
11*4882a593Smuzhiyun #include "xfs_mount.h"
12*4882a593Smuzhiyun #include "xfs_btree.h"
13*4882a593Smuzhiyun #include "xfs_alloc.h"
14*4882a593Smuzhiyun #include "xfs_rmap.h"
15*4882a593Smuzhiyun #include "scrub/scrub.h"
16*4882a593Smuzhiyun #include "scrub/common.h"
17*4882a593Smuzhiyun #include "scrub/btree.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Set us up to scrub free space btrees.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun int
xchk_setup_ag_allocbt(struct xfs_scrub * sc,struct xfs_inode * ip)23*4882a593Smuzhiyun xchk_setup_ag_allocbt(
24*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
25*4882a593Smuzhiyun 	struct xfs_inode	*ip)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	return xchk_setup_ag_btree(sc, ip, false);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* Free space btree scrubber. */
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun  * Ensure there's a corresponding cntbt/bnobt record matching this
33*4882a593Smuzhiyun  * bnobt/cntbt record, respectively.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun STATIC void
xchk_allocbt_xref_other(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)36*4882a593Smuzhiyun xchk_allocbt_xref_other(
37*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
38*4882a593Smuzhiyun 	xfs_agblock_t		agbno,
39*4882a593Smuzhiyun 	xfs_extlen_t		len)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	struct xfs_btree_cur	**pcur;
42*4882a593Smuzhiyun 	xfs_agblock_t		fbno;
43*4882a593Smuzhiyun 	xfs_extlen_t		flen;
44*4882a593Smuzhiyun 	int			has_otherrec;
45*4882a593Smuzhiyun 	int			error;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
48*4882a593Smuzhiyun 		pcur = &sc->sa.cnt_cur;
49*4882a593Smuzhiyun 	else
50*4882a593Smuzhiyun 		pcur = &sc->sa.bno_cur;
51*4882a593Smuzhiyun 	if (!*pcur || xchk_skip_xref(sc->sm))
52*4882a593Smuzhiyun 		return;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
55*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, pcur))
56*4882a593Smuzhiyun 		return;
57*4882a593Smuzhiyun 	if (!has_otherrec) {
58*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
59*4882a593Smuzhiyun 		return;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &has_otherrec);
63*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, pcur))
64*4882a593Smuzhiyun 		return;
65*4882a593Smuzhiyun 	if (!has_otherrec) {
66*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
67*4882a593Smuzhiyun 		return;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (fbno != agbno || flen != len)
71*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /* Cross-reference with the other btrees. */
75*4882a593Smuzhiyun STATIC void
xchk_allocbt_xref(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)76*4882a593Smuzhiyun xchk_allocbt_xref(
77*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
78*4882a593Smuzhiyun 	xfs_agblock_t		agbno,
79*4882a593Smuzhiyun 	xfs_extlen_t		len)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
82*4882a593Smuzhiyun 		return;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	xchk_allocbt_xref_other(sc, agbno, len);
85*4882a593Smuzhiyun 	xchk_xref_is_not_inode_chunk(sc, agbno, len);
86*4882a593Smuzhiyun 	xchk_xref_has_no_owner(sc, agbno, len);
87*4882a593Smuzhiyun 	xchk_xref_is_not_shared(sc, agbno, len);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* Scrub a bnobt/cntbt record. */
91*4882a593Smuzhiyun STATIC int
xchk_allocbt_rec(struct xchk_btree * bs,union xfs_btree_rec * rec)92*4882a593Smuzhiyun xchk_allocbt_rec(
93*4882a593Smuzhiyun 	struct xchk_btree	*bs,
94*4882a593Smuzhiyun 	union xfs_btree_rec	*rec)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct xfs_mount	*mp = bs->cur->bc_mp;
97*4882a593Smuzhiyun 	xfs_agnumber_t		agno = bs->cur->bc_ag.agno;
98*4882a593Smuzhiyun 	xfs_agblock_t		bno;
99*4882a593Smuzhiyun 	xfs_extlen_t		len;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	bno = be32_to_cpu(rec->alloc.ar_startblock);
102*4882a593Smuzhiyun 	len = be32_to_cpu(rec->alloc.ar_blockcount);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (bno + len <= bno ||
105*4882a593Smuzhiyun 	    !xfs_verify_agbno(mp, agno, bno) ||
106*4882a593Smuzhiyun 	    !xfs_verify_agbno(mp, agno, bno + len - 1))
107*4882a593Smuzhiyun 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	xchk_allocbt_xref(bs->sc, bno, len);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	return 0;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /* Scrub the freespace btrees for some AG. */
115*4882a593Smuzhiyun STATIC int
xchk_allocbt(struct xfs_scrub * sc,xfs_btnum_t which)116*4882a593Smuzhiyun xchk_allocbt(
117*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
118*4882a593Smuzhiyun 	xfs_btnum_t		which)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	struct xfs_btree_cur	*cur;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
123*4882a593Smuzhiyun 	return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, NULL);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun int
xchk_bnobt(struct xfs_scrub * sc)127*4882a593Smuzhiyun xchk_bnobt(
128*4882a593Smuzhiyun 	struct xfs_scrub	*sc)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	return xchk_allocbt(sc, XFS_BTNUM_BNO);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun int
xchk_cntbt(struct xfs_scrub * sc)134*4882a593Smuzhiyun xchk_cntbt(
135*4882a593Smuzhiyun 	struct xfs_scrub	*sc)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	return xchk_allocbt(sc, XFS_BTNUM_CNT);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /* xref check that the extent is not free */
141*4882a593Smuzhiyun void
xchk_xref_is_used_space(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)142*4882a593Smuzhiyun xchk_xref_is_used_space(
143*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
144*4882a593Smuzhiyun 	xfs_agblock_t		agbno,
145*4882a593Smuzhiyun 	xfs_extlen_t		len)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	bool			is_freesp;
148*4882a593Smuzhiyun 	int			error;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
151*4882a593Smuzhiyun 		return;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	error = xfs_alloc_has_record(sc->sa.bno_cur, agbno, len, &is_freesp);
154*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
155*4882a593Smuzhiyun 		return;
156*4882a593Smuzhiyun 	if (is_freesp)
157*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);
158*4882a593Smuzhiyun }
159