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 #ifndef __XFS_SCRUB_SCRUB_H__
7*4882a593Smuzhiyun #define __XFS_SCRUB_SCRUB_H__
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun struct xfs_scrub;
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /* Type info and names for the scrub types. */
12*4882a593Smuzhiyun enum xchk_type {
13*4882a593Smuzhiyun ST_NONE = 1, /* disabled */
14*4882a593Smuzhiyun ST_PERAG, /* per-AG metadata */
15*4882a593Smuzhiyun ST_FS, /* per-FS metadata */
16*4882a593Smuzhiyun ST_INODE, /* per-inode metadata */
17*4882a593Smuzhiyun };
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun struct xchk_meta_ops {
20*4882a593Smuzhiyun /* Acquire whatever resources are needed for the operation. */
21*4882a593Smuzhiyun int (*setup)(struct xfs_scrub *,
22*4882a593Smuzhiyun struct xfs_inode *);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* Examine metadata for errors. */
25*4882a593Smuzhiyun int (*scrub)(struct xfs_scrub *);
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* Repair or optimize the metadata. */
28*4882a593Smuzhiyun int (*repair)(struct xfs_scrub *);
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* Decide if we even have this piece of metadata. */
31*4882a593Smuzhiyun bool (*has)(struct xfs_sb *);
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* type describing required/allowed inputs */
34*4882a593Smuzhiyun enum xchk_type type;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Buffer pointers and btree cursors for an entire AG. */
38*4882a593Smuzhiyun struct xchk_ag {
39*4882a593Smuzhiyun xfs_agnumber_t agno;
40*4882a593Smuzhiyun struct xfs_perag *pag;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* AG btree roots */
43*4882a593Smuzhiyun struct xfs_buf *agf_bp;
44*4882a593Smuzhiyun struct xfs_buf *agfl_bp;
45*4882a593Smuzhiyun struct xfs_buf *agi_bp;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* AG btrees */
48*4882a593Smuzhiyun struct xfs_btree_cur *bno_cur;
49*4882a593Smuzhiyun struct xfs_btree_cur *cnt_cur;
50*4882a593Smuzhiyun struct xfs_btree_cur *ino_cur;
51*4882a593Smuzhiyun struct xfs_btree_cur *fino_cur;
52*4882a593Smuzhiyun struct xfs_btree_cur *rmap_cur;
53*4882a593Smuzhiyun struct xfs_btree_cur *refc_cur;
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun struct xfs_scrub {
57*4882a593Smuzhiyun /* General scrub state. */
58*4882a593Smuzhiyun struct xfs_mount *mp;
59*4882a593Smuzhiyun struct xfs_scrub_metadata *sm;
60*4882a593Smuzhiyun const struct xchk_meta_ops *ops;
61*4882a593Smuzhiyun struct xfs_trans *tp;
62*4882a593Smuzhiyun struct xfs_inode *ip;
63*4882a593Smuzhiyun void *buf;
64*4882a593Smuzhiyun uint ilock_flags;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* See the XCHK/XREP state flags below. */
67*4882a593Smuzhiyun unsigned int flags;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * The XFS_SICK_* flags that correspond to the metadata being scrubbed
71*4882a593Smuzhiyun * or repaired. We will use this mask to update the in-core fs health
72*4882a593Smuzhiyun * status with whatever we find.
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun unsigned int sick_mask;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* State tracking for single-AG operations. */
77*4882a593Smuzhiyun struct xchk_ag sa;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* XCHK state flags grow up from zero, XREP state flags grown down from 2^31 */
81*4882a593Smuzhiyun #define XCHK_TRY_HARDER (1 << 0) /* can't get resources, try again */
82*4882a593Smuzhiyun #define XCHK_HAS_QUOTAOFFLOCK (1 << 1) /* we hold the quotaoff lock */
83*4882a593Smuzhiyun #define XCHK_REAPING_DISABLED (1 << 2) /* background block reaping paused */
84*4882a593Smuzhiyun #define XREP_ALREADY_FIXED (1 << 31) /* checking our repair work */
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* Metadata scrubbers */
87*4882a593Smuzhiyun int xchk_tester(struct xfs_scrub *sc);
88*4882a593Smuzhiyun int xchk_superblock(struct xfs_scrub *sc);
89*4882a593Smuzhiyun int xchk_agf(struct xfs_scrub *sc);
90*4882a593Smuzhiyun int xchk_agfl(struct xfs_scrub *sc);
91*4882a593Smuzhiyun int xchk_agi(struct xfs_scrub *sc);
92*4882a593Smuzhiyun int xchk_bnobt(struct xfs_scrub *sc);
93*4882a593Smuzhiyun int xchk_cntbt(struct xfs_scrub *sc);
94*4882a593Smuzhiyun int xchk_inobt(struct xfs_scrub *sc);
95*4882a593Smuzhiyun int xchk_finobt(struct xfs_scrub *sc);
96*4882a593Smuzhiyun int xchk_rmapbt(struct xfs_scrub *sc);
97*4882a593Smuzhiyun int xchk_refcountbt(struct xfs_scrub *sc);
98*4882a593Smuzhiyun int xchk_inode(struct xfs_scrub *sc);
99*4882a593Smuzhiyun int xchk_bmap_data(struct xfs_scrub *sc);
100*4882a593Smuzhiyun int xchk_bmap_attr(struct xfs_scrub *sc);
101*4882a593Smuzhiyun int xchk_bmap_cow(struct xfs_scrub *sc);
102*4882a593Smuzhiyun int xchk_directory(struct xfs_scrub *sc);
103*4882a593Smuzhiyun int xchk_xattr(struct xfs_scrub *sc);
104*4882a593Smuzhiyun int xchk_symlink(struct xfs_scrub *sc);
105*4882a593Smuzhiyun int xchk_parent(struct xfs_scrub *sc);
106*4882a593Smuzhiyun #ifdef CONFIG_XFS_RT
107*4882a593Smuzhiyun int xchk_rtbitmap(struct xfs_scrub *sc);
108*4882a593Smuzhiyun int xchk_rtsummary(struct xfs_scrub *sc);
109*4882a593Smuzhiyun #else
110*4882a593Smuzhiyun static inline int
xchk_rtbitmap(struct xfs_scrub * sc)111*4882a593Smuzhiyun xchk_rtbitmap(struct xfs_scrub *sc)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun return -ENOENT;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun static inline int
xchk_rtsummary(struct xfs_scrub * sc)116*4882a593Smuzhiyun xchk_rtsummary(struct xfs_scrub *sc)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun return -ENOENT;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun #endif
121*4882a593Smuzhiyun #ifdef CONFIG_XFS_QUOTA
122*4882a593Smuzhiyun int xchk_quota(struct xfs_scrub *sc);
123*4882a593Smuzhiyun #else
124*4882a593Smuzhiyun static inline int
xchk_quota(struct xfs_scrub * sc)125*4882a593Smuzhiyun xchk_quota(struct xfs_scrub *sc)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun return -ENOENT;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun int xchk_fscounters(struct xfs_scrub *sc);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* cross-referencing helpers */
133*4882a593Smuzhiyun void xchk_xref_is_used_space(struct xfs_scrub *sc, xfs_agblock_t agbno,
134*4882a593Smuzhiyun xfs_extlen_t len);
135*4882a593Smuzhiyun void xchk_xref_is_not_inode_chunk(struct xfs_scrub *sc, xfs_agblock_t agbno,
136*4882a593Smuzhiyun xfs_extlen_t len);
137*4882a593Smuzhiyun void xchk_xref_is_inode_chunk(struct xfs_scrub *sc, xfs_agblock_t agbno,
138*4882a593Smuzhiyun xfs_extlen_t len);
139*4882a593Smuzhiyun void xchk_xref_is_owned_by(struct xfs_scrub *sc, xfs_agblock_t agbno,
140*4882a593Smuzhiyun xfs_extlen_t len, const struct xfs_owner_info *oinfo);
141*4882a593Smuzhiyun void xchk_xref_is_not_owned_by(struct xfs_scrub *sc, xfs_agblock_t agbno,
142*4882a593Smuzhiyun xfs_extlen_t len, const struct xfs_owner_info *oinfo);
143*4882a593Smuzhiyun void xchk_xref_has_no_owner(struct xfs_scrub *sc, xfs_agblock_t agbno,
144*4882a593Smuzhiyun xfs_extlen_t len);
145*4882a593Smuzhiyun void xchk_xref_is_cow_staging(struct xfs_scrub *sc, xfs_agblock_t bno,
146*4882a593Smuzhiyun xfs_extlen_t len);
147*4882a593Smuzhiyun void xchk_xref_is_not_shared(struct xfs_scrub *sc, xfs_agblock_t bno,
148*4882a593Smuzhiyun xfs_extlen_t len);
149*4882a593Smuzhiyun #ifdef CONFIG_XFS_RT
150*4882a593Smuzhiyun void xchk_xref_is_used_rt_space(struct xfs_scrub *sc, xfs_rtblock_t rtbno,
151*4882a593Smuzhiyun xfs_extlen_t len);
152*4882a593Smuzhiyun #else
153*4882a593Smuzhiyun # define xchk_xref_is_used_rt_space(sc, rtbno, len) do { } while (0)
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun struct xchk_fscounters {
157*4882a593Smuzhiyun uint64_t icount;
158*4882a593Smuzhiyun uint64_t ifree;
159*4882a593Smuzhiyun uint64_t fdblocks;
160*4882a593Smuzhiyun unsigned long long icount_min;
161*4882a593Smuzhiyun unsigned long long icount_max;
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun #endif /* __XFS_SCRUB_SCRUB_H__ */
165