xref: /linux/fs/xfs/scrub/rtbitmap.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_rtbitmap.h"
15 #include "xfs_inode.h"
16 #include "xfs_bmap.h"
17 #include "xfs_bit.h"
18 #include "xfs_sb.h"
19 #include "scrub/scrub.h"
20 #include "scrub/common.h"
21 #include "scrub/repair.h"
22 #include "scrub/rtbitmap.h"
23 
24 /* Set us up with the realtime metadata locked. */
25 int
26 xchk_setup_rtbitmap(
27 	struct xfs_scrub	*sc)
28 {
29 	struct xfs_mount	*mp = sc->mp;
30 	struct xchk_rtbitmap	*rtb;
31 	int			error;
32 
33 	rtb = kzalloc(sizeof(struct xchk_rtbitmap), XCHK_GFP_FLAGS);
34 	if (!rtb)
35 		return -ENOMEM;
36 	sc->buf = rtb;
37 
38 	error = xchk_rtgroup_init(sc, sc->sm->sm_agno, &sc->sr);
39 	if (error)
40 		return error;
41 
42 	if (xchk_could_repair(sc)) {
43 		error = xrep_setup_rtbitmap(sc, rtb);
44 		if (error)
45 			return error;
46 	}
47 
48 	error = xchk_trans_alloc(sc, rtb->resblks);
49 	if (error)
50 		return error;
51 
52 	error = xchk_install_live_inode(sc,
53 			sc->sr.rtg->rtg_inodes[XFS_RTGI_BITMAP]);
54 	if (error)
55 		return error;
56 
57 	error = xchk_ino_dqattach(sc);
58 	if (error)
59 		return error;
60 
61 	/*
62 	 * Now that we've locked the rtbitmap, we can't race with growfsrt
63 	 * trying to expand the bitmap or change the size of the rt volume.
64 	 * Hence it is safe to compute and check the geometry values.
65 	 */
66 	xchk_rtgroup_lock(&sc->sr, XFS_RTGLOCK_BITMAP);
67 	if (mp->m_sb.sb_rblocks) {
68 		rtb->rextents = xfs_blen_to_rtbxlen(mp, mp->m_sb.sb_rblocks);
69 		rtb->rextslog = xfs_compute_rextslog(rtb->rextents);
70 		rtb->rbmblocks = xfs_rtbitmap_blockcount(mp);
71 	}
72 
73 	return 0;
74 }
75 
76 /* Realtime bitmap. */
77 
78 /* Scrub a free extent record from the realtime bitmap. */
79 STATIC int
80 xchk_rtbitmap_rec(
81 	struct xfs_rtgroup	*rtg,
82 	struct xfs_trans	*tp,
83 	const struct xfs_rtalloc_rec *rec,
84 	void			*priv)
85 {
86 	struct xfs_scrub	*sc = priv;
87 	xfs_rtblock_t		startblock;
88 	xfs_filblks_t		blockcount;
89 
90 	startblock = xfs_rtx_to_rtb(rtg, rec->ar_startext);
91 	blockcount = xfs_rtxlen_to_extlen(rtg_mount(rtg), rec->ar_extcount);
92 
93 	if (!xfs_verify_rtbext(rtg_mount(rtg), startblock, blockcount))
94 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
95 	return 0;
96 }
97 
98 /* Make sure the entire rtbitmap file is mapped with written extents. */
99 STATIC int
100 xchk_rtbitmap_check_extents(
101 	struct xfs_scrub	*sc)
102 {
103 	struct xfs_bmbt_irec	map;
104 	struct xfs_iext_cursor	icur;
105 	struct xfs_mount	*mp = sc->mp;
106 	struct xfs_inode	*ip = sc->ip;
107 	xfs_fileoff_t		off = 0;
108 	xfs_fileoff_t		endoff;
109 	int			error = 0;
110 
111 	/* Mappings may not cross or lie beyond EOF. */
112 	endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
113 	if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
114 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
115 		return 0;
116 	}
117 
118 	while (off < endoff) {
119 		int		nmap = 1;
120 
121 		if (xchk_should_terminate(sc, &error) ||
122 		    (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
123 			break;
124 
125 		/* Make sure we have a written extent. */
126 		error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
127 				XFS_DATA_FORK);
128 		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
129 			break;
130 
131 		if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
132 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
133 			break;
134 		}
135 
136 		off += map.br_blockcount;
137 	}
138 
139 	return error;
140 }
141 
142 /* Scrub the realtime bitmap. */
143 int
144 xchk_rtbitmap(
145 	struct xfs_scrub	*sc)
146 {
147 	struct xfs_mount	*mp = sc->mp;
148 	struct xfs_rtgroup	*rtg = sc->sr.rtg;
149 	struct xfs_inode	*rbmip = rtg->rtg_inodes[XFS_RTGI_BITMAP];
150 	struct xchk_rtbitmap	*rtb = sc->buf;
151 	int			error;
152 
153 	/* Is sb_rextents correct? */
154 	if (mp->m_sb.sb_rextents != rtb->rextents) {
155 		xchk_ino_set_corrupt(sc, rbmip->i_ino);
156 		return 0;
157 	}
158 
159 	/* Is sb_rextslog correct? */
160 	if (mp->m_sb.sb_rextslog != rtb->rextslog) {
161 		xchk_ino_set_corrupt(sc, rbmip->i_ino);
162 		return 0;
163 	}
164 
165 	/*
166 	 * Is sb_rbmblocks large enough to handle the current rt volume?  In no
167 	 * case can we exceed 4bn bitmap blocks since the super field is a u32.
168 	 */
169 	if (rtb->rbmblocks > U32_MAX) {
170 		xchk_ino_set_corrupt(sc, rbmip->i_ino);
171 		return 0;
172 	}
173 	if (mp->m_sb.sb_rbmblocks != rtb->rbmblocks) {
174 		xchk_ino_set_corrupt(sc, rbmip->i_ino);
175 		return 0;
176 	}
177 
178 	/* The bitmap file length must be aligned to an fsblock. */
179 	if (rbmip->i_disk_size & mp->m_blockmask) {
180 		xchk_ino_set_corrupt(sc, rbmip->i_ino);
181 		return 0;
182 	}
183 
184 	/*
185 	 * Is the bitmap file itself large enough to handle the rt volume?
186 	 * growfsrt expands the bitmap file before updating sb_rextents, so the
187 	 * file can be larger than sb_rbmblocks.
188 	 */
189 	if (rbmip->i_disk_size < XFS_FSB_TO_B(mp, rtb->rbmblocks)) {
190 		xchk_ino_set_corrupt(sc, rbmip->i_ino);
191 		return 0;
192 	}
193 
194 	/* Invoke the fork scrubber. */
195 	error = xchk_metadata_inode_forks(sc);
196 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
197 		return error;
198 
199 	error = xchk_rtbitmap_check_extents(sc);
200 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
201 		return error;
202 
203 	error = xfs_rtalloc_query_all(rtg, sc->tp, xchk_rtbitmap_rec, sc);
204 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
205 		return error;
206 
207 	return 0;
208 }
209 
210 /* xref check that the extent is not free in the rtbitmap */
211 void
212 xchk_xref_is_used_rt_space(
213 	struct xfs_scrub	*sc,
214 	xfs_rtblock_t		rtbno,
215 	xfs_extlen_t		len)
216 {
217 	struct xfs_rtgroup	*rtg = sc->sr.rtg;
218 	struct xfs_inode	*rbmip = rtg->rtg_inodes[XFS_RTGI_BITMAP];
219 	xfs_rtxnum_t		startext;
220 	xfs_rtxnum_t		endext;
221 	bool			is_free;
222 	int			error;
223 
224 	if (xchk_skip_xref(sc->sm))
225 		return;
226 
227 	startext = xfs_rtb_to_rtx(sc->mp, rtbno);
228 	endext = xfs_rtb_to_rtx(sc->mp, rtbno + len - 1);
229 	error = xfs_rtalloc_extent_is_free(rtg, sc->tp, startext,
230 			endext - startext + 1, &is_free);
231 	if (!xchk_should_check_xref(sc, &error, NULL))
232 		return;
233 	if (is_free)
234 		xchk_ino_xref_set_corrupt(sc, rbmip->i_ino);
235 }
236