Loading...
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#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_rtalloc.h"
15#include "xfs_inode.h"
16#include "scrub/scrub.h"
17#include "scrub/common.h"
18
19/* Set us up with the realtime metadata locked. */
20int
21xchk_setup_rt(
22 struct xfs_scrub *sc,
23 struct xfs_inode *ip)
24{
25 int error;
26
27 error = xchk_setup_fs(sc, ip);
28 if (error)
29 return error;
30
31 sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
32 sc->ip = sc->mp->m_rbmip;
33 xfs_ilock(sc->ip, sc->ilock_flags);
34
35 return 0;
36}
37
38/* Realtime bitmap. */
39
40/* Scrub a free extent record from the realtime bitmap. */
41STATIC int
42xchk_rtbitmap_rec(
43 struct xfs_trans *tp,
44 struct xfs_rtalloc_rec *rec,
45 void *priv)
46{
47 struct xfs_scrub *sc = priv;
48 xfs_rtblock_t startblock;
49 xfs_rtblock_t blockcount;
50
51 startblock = rec->ar_startext * tp->t_mountp->m_sb.sb_rextsize;
52 blockcount = rec->ar_extcount * tp->t_mountp->m_sb.sb_rextsize;
53
54 if (startblock + blockcount <= startblock ||
55 !xfs_verify_rtbno(sc->mp, startblock) ||
56 !xfs_verify_rtbno(sc->mp, startblock + blockcount - 1))
57 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
58 return 0;
59}
60
61/* Scrub the realtime bitmap. */
62int
63xchk_rtbitmap(
64 struct xfs_scrub *sc)
65{
66 int error;
67
68 /* Invoke the fork scrubber. */
69 error = xchk_metadata_inode_forks(sc);
70 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
71 return error;
72
73 error = xfs_rtalloc_query_all(sc->tp, xchk_rtbitmap_rec, sc);
74 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
75 goto out;
76
77out:
78 return error;
79}
80
81/* Scrub the realtime summary. */
82int
83xchk_rtsummary(
84 struct xfs_scrub *sc)
85{
86 struct xfs_inode *rsumip = sc->mp->m_rsumip;
87 struct xfs_inode *old_ip = sc->ip;
88 uint old_ilock_flags = sc->ilock_flags;
89 int error = 0;
90
91 /*
92 * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
93 * rt summary ip in compliance with the rt inode locking rules.
94 *
95 * Since we switch sc->ip to rsumip we have to save the old ilock
96 * flags so that we don't mix up the inode state that @sc tracks.
97 */
98 sc->ip = rsumip;
99 sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
100 xfs_ilock(sc->ip, sc->ilock_flags);
101
102 /* Invoke the fork scrubber. */
103 error = xchk_metadata_inode_forks(sc);
104 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
105 goto out;
106
107 /* XXX: implement this some day */
108 xchk_set_incomplete(sc);
109out:
110 /* Switch back to the rtbitmap inode and lock flags. */
111 xfs_iunlock(sc->ip, sc->ilock_flags);
112 sc->ilock_flags = old_ilock_flags;
113 sc->ip = old_ip;
114 return error;
115}
116
117
118/* xref check that the extent is not free in the rtbitmap */
119void
120xchk_xref_is_used_rt_space(
121 struct xfs_scrub *sc,
122 xfs_rtblock_t fsbno,
123 xfs_extlen_t len)
124{
125 xfs_rtblock_t startext;
126 xfs_rtblock_t endext;
127 xfs_rtblock_t extcount;
128 bool is_free;
129 int error;
130
131 if (xchk_skip_xref(sc->sm))
132 return;
133
134 startext = fsbno;
135 endext = fsbno + len - 1;
136 do_div(startext, sc->mp->m_sb.sb_rextsize);
137 do_div(endext, sc->mp->m_sb.sb_rextsize);
138 extcount = endext - startext + 1;
139 xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
140 error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
141 &is_free);
142 if (!xchk_should_check_xref(sc, &error, NULL))
143 goto out_unlock;
144 if (is_free)
145 xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
146out_unlock:
147 xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
148}
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. */
25int
26xchk_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. */
79STATIC int
80xchk_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. */
99STATIC int
100xchk_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. */
143int
144xchk_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 */
211void
212xchk_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}