1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #ifndef __XFS_SCRUB_COMMON_H__
7 #define __XFS_SCRUB_COMMON_H__
8 
9 /*
10  * We /could/ terminate a scrub/repair operation early.  If we're not
11  * in a good place to continue (fatal signal, etc.) then bail out.
12  * Note that we're careful not to make any judgements about *error.
13  */
14 static inline bool
xchk_should_terminate(struct xfs_scrub * sc,int * error)15 xchk_should_terminate(
16 	struct xfs_scrub	*sc,
17 	int			*error)
18 {
19 	/*
20 	 * If preemption is disabled, we need to yield to the scheduler every
21 	 * few seconds so that we don't run afoul of the soft lockup watchdog
22 	 * or RCU stall detector.
23 	 */
24 	cond_resched();
25 
26 	if (fatal_signal_pending(current)) {
27 		if (*error == 0)
28 			*error = -EAGAIN;
29 		return true;
30 	}
31 	return false;
32 }
33 
34 int xchk_trans_alloc(struct xfs_scrub *sc, uint resblks);
35 bool xchk_process_error(struct xfs_scrub *sc, xfs_agnumber_t agno,
36 		xfs_agblock_t bno, int *error);
37 bool xchk_fblock_process_error(struct xfs_scrub *sc, int whichfork,
38 		xfs_fileoff_t offset, int *error);
39 
40 bool xchk_xref_process_error(struct xfs_scrub *sc,
41 		xfs_agnumber_t agno, xfs_agblock_t bno, int *error);
42 bool xchk_fblock_xref_process_error(struct xfs_scrub *sc,
43 		int whichfork, xfs_fileoff_t offset, int *error);
44 
45 void xchk_block_set_preen(struct xfs_scrub *sc,
46 		struct xfs_buf *bp);
47 void xchk_ino_set_preen(struct xfs_scrub *sc, xfs_ino_t ino);
48 
49 void xchk_block_set_corrupt(struct xfs_scrub *sc,
50 		struct xfs_buf *bp);
51 void xchk_ino_set_corrupt(struct xfs_scrub *sc, xfs_ino_t ino);
52 void xchk_fblock_set_corrupt(struct xfs_scrub *sc, int whichfork,
53 		xfs_fileoff_t offset);
54 
55 void xchk_block_xref_set_corrupt(struct xfs_scrub *sc,
56 		struct xfs_buf *bp);
57 void xchk_ino_xref_set_corrupt(struct xfs_scrub *sc,
58 		xfs_ino_t ino);
59 void xchk_fblock_xref_set_corrupt(struct xfs_scrub *sc,
60 		int whichfork, xfs_fileoff_t offset);
61 
62 void xchk_ino_set_warning(struct xfs_scrub *sc, xfs_ino_t ino);
63 void xchk_fblock_set_warning(struct xfs_scrub *sc, int whichfork,
64 		xfs_fileoff_t offset);
65 
66 void xchk_set_incomplete(struct xfs_scrub *sc);
67 int xchk_checkpoint_log(struct xfs_mount *mp);
68 
69 /* Are we set up for a cross-referencing check? */
70 bool xchk_should_check_xref(struct xfs_scrub *sc, int *error,
71 			   struct xfs_btree_cur **curpp);
72 
73 /* Setup functions */
74 int xchk_setup_fs(struct xfs_scrub *sc, struct xfs_inode *ip);
75 int xchk_setup_ag_allocbt(struct xfs_scrub *sc,
76 			       struct xfs_inode *ip);
77 int xchk_setup_ag_iallocbt(struct xfs_scrub *sc,
78 				struct xfs_inode *ip);
79 int xchk_setup_ag_rmapbt(struct xfs_scrub *sc,
80 			      struct xfs_inode *ip);
81 int xchk_setup_ag_refcountbt(struct xfs_scrub *sc,
82 				  struct xfs_inode *ip);
83 int xchk_setup_inode(struct xfs_scrub *sc,
84 			  struct xfs_inode *ip);
85 int xchk_setup_inode_bmap(struct xfs_scrub *sc,
86 			       struct xfs_inode *ip);
87 int xchk_setup_inode_bmap_data(struct xfs_scrub *sc,
88 				    struct xfs_inode *ip);
89 int xchk_setup_directory(struct xfs_scrub *sc,
90 			      struct xfs_inode *ip);
91 int xchk_setup_xattr(struct xfs_scrub *sc,
92 			  struct xfs_inode *ip);
93 int xchk_setup_symlink(struct xfs_scrub *sc,
94 			    struct xfs_inode *ip);
95 int xchk_setup_parent(struct xfs_scrub *sc,
96 			   struct xfs_inode *ip);
97 #ifdef CONFIG_XFS_RT
98 int xchk_setup_rt(struct xfs_scrub *sc, struct xfs_inode *ip);
99 #else
100 static inline int
xchk_setup_rt(struct xfs_scrub * sc,struct xfs_inode * ip)101 xchk_setup_rt(struct xfs_scrub *sc, struct xfs_inode *ip)
102 {
103 	return -ENOENT;
104 }
105 #endif
106 #ifdef CONFIG_XFS_QUOTA
107 int xchk_setup_quota(struct xfs_scrub *sc, struct xfs_inode *ip);
108 #else
109 static inline int
xchk_setup_quota(struct xfs_scrub * sc,struct xfs_inode * ip)110 xchk_setup_quota(struct xfs_scrub *sc, struct xfs_inode *ip)
111 {
112 	return -ENOENT;
113 }
114 #endif
115 
116 void xchk_ag_free(struct xfs_scrub *sc, struct xchk_ag *sa);
117 int xchk_ag_init(struct xfs_scrub *sc, xfs_agnumber_t agno,
118 		struct xchk_ag *sa);
119 void xchk_perag_get(struct xfs_mount *mp, struct xchk_ag *sa);
120 int xchk_ag_read_headers(struct xfs_scrub *sc, xfs_agnumber_t agno,
121 		struct xfs_buf **agi, struct xfs_buf **agf,
122 		struct xfs_buf **agfl);
123 void xchk_ag_btcur_free(struct xchk_ag *sa);
124 int xchk_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
125 int xchk_count_rmap_ownedby_ag(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
126 		struct xfs_owner_info *oinfo, xfs_filblks_t *blocks);
127 
128 int xchk_setup_ag_btree(struct xfs_scrub *sc, struct xfs_inode *ip,
129 		bool force_log);
130 int xchk_get_inode(struct xfs_scrub *sc, struct xfs_inode *ip_in);
131 int xchk_setup_inode_contents(struct xfs_scrub *sc, struct xfs_inode *ip,
132 		unsigned int resblks);
133 void xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
134 
135 /*
136  * Don't bother cross-referencing if we already found corruption or cross
137  * referencing discrepancies.
138  */
xchk_skip_xref(struct xfs_scrub_metadata * sm)139 static inline bool xchk_skip_xref(struct xfs_scrub_metadata *sm)
140 {
141 	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
142 			       XFS_SCRUB_OFLAG_XCORRUPT);
143 }
144 
145 int xchk_metadata_inode_forks(struct xfs_scrub *sc);
146 int xchk_ilock_inverted(struct xfs_inode *ip, uint lock_mode);
147 
148 #endif	/* __XFS_SCRUB_COMMON_H__ */
149