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