Line data Source code
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_btree.h"
13 : #include "xfs_inode.h"
14 : #include "xfs_log_format.h"
15 : #include "xfs_trans.h"
16 : #include "xfs_rtbitmap.h"
17 : #include "xfs_bit.h"
18 : #include "xfs_bmap.h"
19 : #include "scrub/scrub.h"
20 : #include "scrub/common.h"
21 : #include "scrub/trace.h"
22 : #include "scrub/xfile.h"
23 : #include "scrub/repair.h"
24 : #include "scrub/rtsummary.h"
25 :
26 : /*
27 : * Realtime Summary
28 : * ================
29 : *
30 : * We check the realtime summary by scanning the realtime bitmap file to create
31 : * a new summary file incore, and then we compare the computed version against
32 : * the ondisk version. We use the 'xfile' functionality to store this
33 : * (potentially large) amount of data in pageable memory.
34 : */
35 :
36 : /* Set us up to check the rtsummary file. */
37 : int
38 122814 : xchk_setup_rtsummary(
39 : struct xfs_scrub *sc)
40 : {
41 122814 : struct xfs_mount *mp = sc->mp;
42 122814 : char *descr;
43 122814 : size_t bufsize = mp->m_sb.sb_blocksize;
44 122814 : unsigned int wordcnt;
45 122814 : unsigned int resblks = 0;
46 122814 : int error;
47 :
48 245628 : if (xchk_could_repair(sc)) {
49 33633 : error = xrep_setup_rtsummary(sc, &resblks, &bufsize);
50 33633 : if (error)
51 : return error;
52 : }
53 :
54 : /*
55 : * Create an xfile to construct a new rtsummary file. The xfile allows
56 : * us to avoid pinning kernel memory for this purpose.
57 : */
58 122717 : wordcnt = xfs_rtsummary_wordcount(mp, mp->m_rsumlevels,
59 : mp->m_sb.sb_rbmblocks);
60 122718 : descr = xchk_xfile_descr(sc, "realtime summary file");
61 122718 : error = xfile_create(descr, wordcnt << XFS_WORDLOG, &sc->xfile);
62 122716 : kfree(descr);
63 122715 : if (error)
64 : return error;
65 :
66 122715 : error = xchk_trans_alloc(sc, resblks);
67 122716 : if (error)
68 : return error;
69 :
70 : /* Allocate a memory buffer for the summary comparison. */
71 122716 : sc->buf = kvmalloc(bufsize, XCHK_GFP_FLAGS);
72 122718 : if (!sc->buf)
73 : return -ENOMEM;
74 :
75 122718 : error = xchk_install_live_inode(sc, mp->m_rsumip);
76 122718 : if (error)
77 : return error;
78 :
79 122718 : error = xchk_ino_dqattach(sc);
80 122718 : if (error)
81 : return error;
82 :
83 122718 : xchk_rt_init(sc, &sc->sr,
84 : XCHK_RTLOCK_SUMMARY | XCHK_RTLOCK_BITMAP_SHARED);
85 122718 : return 0;
86 : }
87 :
88 : /* Helper functions to record suminfo words in an xfile. */
89 :
90 : static inline int
91 : xfsum_load(
92 : struct xfs_scrub *sc,
93 : xfs_rtsumoff_t sumoff,
94 : union xfs_suminfo_ondisk *rawinfo)
95 : {
96 78057718 : return xfile_obj_load(sc->xfile, rawinfo,
97 : sizeof(union xfs_suminfo_ondisk),
98 78057718 : sumoff << XFS_WORDLOG);
99 : }
100 :
101 : static inline int
102 : xfsum_store(
103 : struct xfs_scrub *sc,
104 : xfs_rtsumoff_t sumoff,
105 : const union xfs_suminfo_ondisk rawinfo)
106 : {
107 78057718 : return xfile_obj_store(sc->xfile, &rawinfo,
108 : sizeof(union xfs_suminfo_ondisk),
109 : sumoff << XFS_WORDLOG);
110 : }
111 :
112 : inline int
113 218649 : xfsum_copyout(
114 : struct xfs_scrub *sc,
115 : xfs_rtsumoff_t sumoff,
116 : union xfs_suminfo_ondisk *rawinfo,
117 : unsigned int nr_words)
118 : {
119 1234383 : return xfile_obj_load(sc->xfile, rawinfo, nr_words << XFS_WORDLOG,
120 218649 : sumoff << XFS_WORDLOG);
121 : }
122 :
123 : /* Update the summary file to reflect the free extent that we've accumulated. */
124 : STATIC int
125 78057718 : xchk_rtsum_record_free(
126 : struct xfs_mount *mp,
127 : struct xfs_trans *tp,
128 : const struct xfs_rtalloc_rec *rec,
129 : void *priv)
130 : {
131 78057718 : struct xfs_scrub *sc = priv;
132 78057718 : xfs_fileoff_t rbmoff;
133 78057718 : xfs_rtxnum_t rtbno;
134 78057718 : xfs_filblks_t rtlen;
135 78057718 : xfs_rtsumoff_t offs;
136 78057718 : unsigned int lenlog;
137 78057718 : union xfs_suminfo_ondisk v;
138 78057718 : int error = 0;
139 :
140 78057718 : if (xchk_should_terminate(sc, &error))
141 0 : return error;
142 :
143 : /* Compute the relevant location in the rtsum file. */
144 78057718 : rbmoff = xfs_rtx_to_rbmblock(mp, rec->ar_startext);
145 78057718 : lenlog = XFS_RTBLOCKLOG(rec->ar_extcount);
146 78057718 : offs = xfs_rtsumoffs(mp, lenlog, rbmoff);
147 :
148 78057718 : rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
149 78057718 : rtlen = xfs_rtx_to_rtb(mp, rec->ar_extcount);
150 :
151 78057718 : if (!xfs_verify_rtbext(mp, rtbno, rtlen)) {
152 0 : xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
153 0 : return -EFSCORRUPTED;
154 : }
155 :
156 : /* Bump the summary count. */
157 78057718 : error = xfsum_load(sc, offs, &v);
158 78057718 : if (error)
159 : return error;
160 :
161 78057718 : xfs_suminfo_add(mp, &v, 1);
162 78057718 : trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
163 : lenlog, offs, &v);
164 :
165 78057718 : return xfsum_store(sc, offs, v);
166 : }
167 :
168 : /* Compute the realtime summary from the realtime bitmap. */
169 : STATIC int
170 122718 : xchk_rtsum_compute(
171 : struct xfs_scrub *sc)
172 : {
173 122718 : struct xfs_mount *mp = sc->mp;
174 122718 : unsigned long long rtbmp_blocks;
175 :
176 : /* If the bitmap size doesn't match the computed size, bail. */
177 122718 : rtbmp_blocks = xfs_rtbitmap_blockcount(mp, mp->m_sb.sb_rextents);
178 122718 : if (XFS_FSB_TO_B(mp, rtbmp_blocks) != mp->m_rbmip->i_disk_size)
179 : return -EFSCORRUPTED;
180 :
181 122718 : return xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtsum_record_free,
182 : sc);
183 : }
184 :
185 : /* Compare the rtsummary file against the one we computed. */
186 : STATIC int
187 122718 : xchk_rtsum_compare(
188 : struct xfs_scrub *sc)
189 : {
190 122718 : struct xfs_mount *mp = sc->mp;
191 122718 : struct xfs_buf *bp;
192 122718 : struct xfs_bmbt_irec map;
193 122718 : xfs_fileoff_t off;
194 122718 : xfs_rtsumoff_t sumoff = 0;
195 122718 : int nmap;
196 :
197 919803 : for (off = 0; off < XFS_B_TO_FSB(mp, mp->m_rsumsize); off++) {
198 797085 : union xfs_suminfo_ondisk *ondisk_info;
199 797085 : int error = 0;
200 :
201 797085 : if (xchk_should_terminate(sc, &error))
202 0 : return error;
203 797085 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
204 : return 0;
205 :
206 : /* Make sure we have a written extent. */
207 797085 : nmap = 1;
208 797085 : error = xfs_bmapi_read(mp->m_rsumip, off, 1, &map, &nmap,
209 : XFS_DATA_FORK);
210 797085 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
211 0 : return error;
212 :
213 797085 : if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
214 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
215 0 : return 0;
216 : }
217 :
218 : /* Read a block's worth of ondisk rtsummary file. */
219 797085 : error = xfs_rtbuf_get(mp, sc->tp, off, 1, &bp);
220 797085 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
221 0 : return error;
222 :
223 : /* Read a block's worth of computed rtsummary file. */
224 797085 : error = xfsum_copyout(sc, sumoff, sc->buf, mp->m_blockwsize);
225 797085 : if (error) {
226 0 : xfs_trans_brelse(sc->tp, bp);
227 0 : return error;
228 : }
229 :
230 797085 : ondisk_info = xfs_rsumblock_infoptr(bp, 0);
231 797085 : if (memcmp(ondisk_info, sc->buf,
232 797085 : mp->m_blockwsize << XFS_WORDLOG) != 0)
233 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
234 :
235 797085 : xfs_trans_brelse(sc->tp, bp);
236 797085 : sumoff += mp->m_blockwsize;
237 : }
238 :
239 : return 0;
240 : }
241 :
242 : /* Scrub the realtime summary. */
243 : int
244 122718 : xchk_rtsummary(
245 : struct xfs_scrub *sc)
246 : {
247 122718 : struct xfs_mount *mp = sc->mp;
248 122718 : int error = 0;
249 :
250 : /* Invoke the fork scrubber. */
251 122718 : error = xchk_metadata_inode_forks(sc);
252 122718 : if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
253 : return error;
254 :
255 : /* Construct the new summary file from the rtbitmap. */
256 122718 : error = xchk_rtsum_compute(sc);
257 122718 : if (error == -EFSCORRUPTED) {
258 : /*
259 : * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
260 : * error since we're checking the summary file.
261 : */
262 0 : xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
263 0 : return 0;
264 : }
265 122718 : if (error)
266 : return error;
267 :
268 : /* Does the computed summary file match the actual rtsummary file? */
269 122718 : return xchk_rtsum_compare(sc);
270 : }
|