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_btree.h"
11#include "xfs_rmap.h"
12#include "xfs_refcount.h"
13#include "scrub/scrub.h"
14#include "scrub/common.h"
15#include "scrub/btree.h"
16
17/*
18 * Set us up to scrub reference count btrees.
19 */
20int
21xchk_setup_ag_refcountbt(
22 struct xfs_scrub *sc,
23 struct xfs_inode *ip)
24{
25 return xchk_setup_ag_btree(sc, ip, false);
26}
27
28/* Reference count btree scrubber. */
29
30/*
31 * Confirming Reference Counts via Reverse Mappings
32 *
33 * We want to count the reverse mappings overlapping a refcount record
34 * (bno, len, refcount), allowing for the possibility that some of the
35 * overlap may come from smaller adjoining reverse mappings, while some
36 * comes from single extents which overlap the range entirely. The
37 * outer loop is as follows:
38 *
39 * 1. For all reverse mappings overlapping the refcount extent,
40 * a. If a given rmap completely overlaps, mark it as seen.
41 * b. Otherwise, record the fragment (in agbno order) for later
42 * processing.
43 *
44 * Once we've seen all the rmaps, we know that for all blocks in the
45 * refcount record we want to find $refcount owners and we've already
46 * visited $seen extents that overlap all the blocks. Therefore, we
47 * need to find ($refcount - $seen) owners for every block in the
48 * extent; call that quantity $target_nr. Proceed as follows:
49 *
50 * 2. Pull the first $target_nr fragments from the list; all of them
51 * should start at or before the start of the extent.
52 * Call this subset of fragments the working set.
53 * 3. Until there are no more unprocessed fragments,
54 * a. Find the shortest fragments in the set and remove them.
55 * b. Note the block number of the end of these fragments.
56 * c. Pull the same number of fragments from the list. All of these
57 * fragments should start at the block number recorded in the
58 * previous step.
59 * d. Put those fragments in the set.
60 * 4. Check that there are $target_nr fragments remaining in the list,
61 * and that they all end at or beyond the end of the refcount extent.
62 *
63 * If the refcount is correct, all the check conditions in the algorithm
64 * should always hold true. If not, the refcount is incorrect.
65 */
66struct xchk_refcnt_frag {
67 struct list_head list;
68 struct xfs_rmap_irec rm;
69};
70
71struct xchk_refcnt_check {
72 struct xfs_scrub *sc;
73 struct list_head fragments;
74
75 /* refcount extent we're examining */
76 xfs_agblock_t bno;
77 xfs_extlen_t len;
78 xfs_nlink_t refcount;
79
80 /* number of owners seen */
81 xfs_nlink_t seen;
82};
83
84/*
85 * Decide if the given rmap is large enough that we can redeem it
86 * towards refcount verification now, or if it's a fragment, in
87 * which case we'll hang onto it in the hopes that we'll later
88 * discover that we've collected exactly the correct number of
89 * fragments as the refcountbt says we should have.
90 */
91STATIC int
92xchk_refcountbt_rmap_check(
93 struct xfs_btree_cur *cur,
94 struct xfs_rmap_irec *rec,
95 void *priv)
96{
97 struct xchk_refcnt_check *refchk = priv;
98 struct xchk_refcnt_frag *frag;
99 xfs_agblock_t rm_last;
100 xfs_agblock_t rc_last;
101 int error = 0;
102
103 if (xchk_should_terminate(refchk->sc, &error))
104 return error;
105
106 rm_last = rec->rm_startblock + rec->rm_blockcount - 1;
107 rc_last = refchk->bno + refchk->len - 1;
108
109 /* Confirm that a single-owner refc extent is a CoW stage. */
110 if (refchk->refcount == 1 && rec->rm_owner != XFS_RMAP_OWN_COW) {
111 xchk_btree_xref_set_corrupt(refchk->sc, cur, 0);
112 return 0;
113 }
114
115 if (rec->rm_startblock <= refchk->bno && rm_last >= rc_last) {
116 /*
117 * The rmap overlaps the refcount record, so we can confirm
118 * one refcount owner seen.
119 */
120 refchk->seen++;
121 } else {
122 /*
123 * This rmap covers only part of the refcount record, so
124 * save the fragment for later processing. If the rmapbt
125 * is healthy each rmap_irec we see will be in agbno order
126 * so we don't need insertion sort here.
127 */
128 frag = kmem_alloc(sizeof(struct xchk_refcnt_frag),
129 KM_MAYFAIL);
130 if (!frag)
131 return -ENOMEM;
132 memcpy(&frag->rm, rec, sizeof(frag->rm));
133 list_add_tail(&frag->list, &refchk->fragments);
134 }
135
136 return 0;
137}
138
139/*
140 * Given a bunch of rmap fragments, iterate through them, keeping
141 * a running tally of the refcount. If this ever deviates from
142 * what we expect (which is the refcountbt's refcount minus the
143 * number of extents that totally covered the refcountbt extent),
144 * we have a refcountbt error.
145 */
146STATIC void
147xchk_refcountbt_process_rmap_fragments(
148 struct xchk_refcnt_check *refchk)
149{
150 struct list_head worklist;
151 struct xchk_refcnt_frag *frag;
152 struct xchk_refcnt_frag *n;
153 xfs_agblock_t bno;
154 xfs_agblock_t rbno;
155 xfs_agblock_t next_rbno;
156 xfs_nlink_t nr;
157 xfs_nlink_t target_nr;
158
159 target_nr = refchk->refcount - refchk->seen;
160 if (target_nr == 0)
161 return;
162
163 /*
164 * There are (refchk->rc.rc_refcount - refchk->nr refcount)
165 * references we haven't found yet. Pull that many off the
166 * fragment list and figure out where the smallest rmap ends
167 * (and therefore the next rmap should start). All the rmaps
168 * we pull off should start at or before the beginning of the
169 * refcount record's range.
170 */
171 INIT_LIST_HEAD(&worklist);
172 rbno = NULLAGBLOCK;
173 nr = 1;
174
175 /* Make sure the fragments actually /are/ in agbno order. */
176 bno = 0;
177 list_for_each_entry(frag, &refchk->fragments, list) {
178 if (frag->rm.rm_startblock < bno)
179 goto done;
180 bno = frag->rm.rm_startblock;
181 }
182
183 /*
184 * Find all the rmaps that start at or before the refc extent,
185 * and put them on the worklist.
186 */
187 list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
188 if (frag->rm.rm_startblock > refchk->bno)
189 goto done;
190 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
191 if (bno < rbno)
192 rbno = bno;
193 list_move_tail(&frag->list, &worklist);
194 if (nr == target_nr)
195 break;
196 nr++;
197 }
198
199 /*
200 * We should have found exactly $target_nr rmap fragments starting
201 * at or before the refcount extent.
202 */
203 if (nr != target_nr)
204 goto done;
205
206 while (!list_empty(&refchk->fragments)) {
207 /* Discard any fragments ending at rbno from the worklist. */
208 nr = 0;
209 next_rbno = NULLAGBLOCK;
210 list_for_each_entry_safe(frag, n, &worklist, list) {
211 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
212 if (bno != rbno) {
213 if (bno < next_rbno)
214 next_rbno = bno;
215 continue;
216 }
217 list_del(&frag->list);
218 kmem_free(frag);
219 nr++;
220 }
221
222 /* Try to add nr rmaps starting at rbno to the worklist. */
223 list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
224 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
225 if (frag->rm.rm_startblock != rbno)
226 goto done;
227 list_move_tail(&frag->list, &worklist);
228 if (next_rbno > bno)
229 next_rbno = bno;
230 nr--;
231 if (nr == 0)
232 break;
233 }
234
235 /*
236 * If we get here and nr > 0, this means that we added fewer
237 * items to the worklist than we discarded because the fragment
238 * list ran out of items. Therefore, we cannot maintain the
239 * required refcount. Something is wrong, so we're done.
240 */
241 if (nr)
242 goto done;
243
244 rbno = next_rbno;
245 }
246
247 /*
248 * Make sure the last extent we processed ends at or beyond
249 * the end of the refcount extent.
250 */
251 if (rbno < refchk->bno + refchk->len)
252 goto done;
253
254 /* Actually record us having seen the remaining refcount. */
255 refchk->seen = refchk->refcount;
256done:
257 /* Delete fragments and work list. */
258 list_for_each_entry_safe(frag, n, &worklist, list) {
259 list_del(&frag->list);
260 kmem_free(frag);
261 }
262 list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
263 list_del(&frag->list);
264 kmem_free(frag);
265 }
266}
267
268/* Use the rmap entries covering this extent to verify the refcount. */
269STATIC void
270xchk_refcountbt_xref_rmap(
271 struct xfs_scrub *sc,
272 xfs_agblock_t bno,
273 xfs_extlen_t len,
274 xfs_nlink_t refcount)
275{
276 struct xchk_refcnt_check refchk = {
277 .sc = sc,
278 .bno = bno,
279 .len = len,
280 .refcount = refcount,
281 .seen = 0,
282 };
283 struct xfs_rmap_irec low;
284 struct xfs_rmap_irec high;
285 struct xchk_refcnt_frag *frag;
286 struct xchk_refcnt_frag *n;
287 int error;
288
289 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
290 return;
291
292 /* Cross-reference with the rmapbt to confirm the refcount. */
293 memset(&low, 0, sizeof(low));
294 low.rm_startblock = bno;
295 memset(&high, 0xFF, sizeof(high));
296 high.rm_startblock = bno + len - 1;
297
298 INIT_LIST_HEAD(&refchk.fragments);
299 error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high,
300 &xchk_refcountbt_rmap_check, &refchk);
301 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
302 goto out_free;
303
304 xchk_refcountbt_process_rmap_fragments(&refchk);
305 if (refcount != refchk.seen)
306 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
307
308out_free:
309 list_for_each_entry_safe(frag, n, &refchk.fragments, list) {
310 list_del(&frag->list);
311 kmem_free(frag);
312 }
313}
314
315/* Cross-reference with the other btrees. */
316STATIC void
317xchk_refcountbt_xref(
318 struct xfs_scrub *sc,
319 xfs_agblock_t agbno,
320 xfs_extlen_t len,
321 xfs_nlink_t refcount)
322{
323 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
324 return;
325
326 xchk_xref_is_used_space(sc, agbno, len);
327 xchk_xref_is_not_inode_chunk(sc, agbno, len);
328 xchk_refcountbt_xref_rmap(sc, agbno, len, refcount);
329}
330
331/* Scrub a refcountbt record. */
332STATIC int
333xchk_refcountbt_rec(
334 struct xchk_btree *bs,
335 union xfs_btree_rec *rec)
336{
337 struct xfs_mount *mp = bs->cur->bc_mp;
338 xfs_agblock_t *cow_blocks = bs->private;
339 xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
340 xfs_agblock_t bno;
341 xfs_extlen_t len;
342 xfs_nlink_t refcount;
343 bool has_cowflag;
344
345 bno = be32_to_cpu(rec->refc.rc_startblock);
346 len = be32_to_cpu(rec->refc.rc_blockcount);
347 refcount = be32_to_cpu(rec->refc.rc_refcount);
348
349 /* Only CoW records can have refcount == 1. */
350 has_cowflag = (bno & XFS_REFC_COW_START);
351 if ((refcount == 1 && !has_cowflag) || (refcount != 1 && has_cowflag))
352 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
353 if (has_cowflag)
354 (*cow_blocks) += len;
355
356 /* Check the extent. */
357 bno &= ~XFS_REFC_COW_START;
358 if (bno + len <= bno ||
359 !xfs_verify_agbno(mp, agno, bno) ||
360 !xfs_verify_agbno(mp, agno, bno + len - 1))
361 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
362
363 if (refcount == 0)
364 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
365
366 xchk_refcountbt_xref(bs->sc, bno, len, refcount);
367
368 return 0;
369}
370
371/* Make sure we have as many refc blocks as the rmap says. */
372STATIC void
373xchk_refcount_xref_rmap(
374 struct xfs_scrub *sc,
375 xfs_filblks_t cow_blocks)
376{
377 xfs_extlen_t refcbt_blocks = 0;
378 xfs_filblks_t blocks;
379 int error;
380
381 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
382 return;
383
384 /* Check that we saw as many refcbt blocks as the rmap knows about. */
385 error = xfs_btree_count_blocks(sc->sa.refc_cur, &refcbt_blocks);
386 if (!xchk_btree_process_error(sc, sc->sa.refc_cur, 0, &error))
387 return;
388 error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
389 &XFS_RMAP_OINFO_REFC, &blocks);
390 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
391 return;
392 if (blocks != refcbt_blocks)
393 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
394
395 /* Check that we saw as many cow blocks as the rmap knows about. */
396 error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
397 &XFS_RMAP_OINFO_COW, &blocks);
398 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
399 return;
400 if (blocks != cow_blocks)
401 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
402}
403
404/* Scrub the refcount btree for some AG. */
405int
406xchk_refcountbt(
407 struct xfs_scrub *sc)
408{
409 xfs_agblock_t cow_blocks = 0;
410 int error;
411
412 error = xchk_btree(sc, sc->sa.refc_cur, xchk_refcountbt_rec,
413 &XFS_RMAP_OINFO_REFC, &cow_blocks);
414 if (error)
415 return error;
416
417 xchk_refcount_xref_rmap(sc, cow_blocks);
418
419 return 0;
420}
421
422/* xref check that a cow staging extent is marked in the refcountbt. */
423void
424xchk_xref_is_cow_staging(
425 struct xfs_scrub *sc,
426 xfs_agblock_t agbno,
427 xfs_extlen_t len)
428{
429 struct xfs_refcount_irec rc;
430 bool has_cowflag;
431 int has_refcount;
432 int error;
433
434 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
435 return;
436
437 /* Find the CoW staging extent. */
438 error = xfs_refcount_lookup_le(sc->sa.refc_cur,
439 agbno + XFS_REFC_COW_START, &has_refcount);
440 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
441 return;
442 if (!has_refcount) {
443 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
444 return;
445 }
446
447 error = xfs_refcount_get_rec(sc->sa.refc_cur, &rc, &has_refcount);
448 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
449 return;
450 if (!has_refcount) {
451 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
452 return;
453 }
454
455 /* CoW flag must be set, refcount must be 1. */
456 has_cowflag = (rc.rc_startblock & XFS_REFC_COW_START);
457 if (!has_cowflag || rc.rc_refcount != 1)
458 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
459
460 /* Must be at least as long as what was passed in */
461 if (rc.rc_blockcount < len)
462 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
463}
464
465/*
466 * xref check that the extent is not shared. Only file data blocks
467 * can have multiple owners.
468 */
469void
470xchk_xref_is_not_shared(
471 struct xfs_scrub *sc,
472 xfs_agblock_t agbno,
473 xfs_extlen_t len)
474{
475 bool shared;
476 int error;
477
478 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
479 return;
480
481 error = xfs_refcount_has_record(sc->sa.refc_cur, agbno, len, &shared);
482 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
483 return;
484 if (shared)
485 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
486}
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_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
13#include "xfs_trans.h"
14#include "xfs_ag.h"
15#include "xfs_btree.h"
16#include "xfs_rmap.h"
17#include "xfs_refcount.h"
18#include "scrub/scrub.h"
19#include "scrub/common.h"
20#include "scrub/btree.h"
21#include "scrub/trace.h"
22#include "scrub/repair.h"
23
24/*
25 * Set us up to scrub reference count btrees.
26 */
27int
28xchk_setup_ag_refcountbt(
29 struct xfs_scrub *sc)
30{
31 if (xchk_need_intent_drain(sc))
32 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
33
34 if (xchk_could_repair(sc)) {
35 int error;
36
37 error = xrep_setup_ag_refcountbt(sc);
38 if (error)
39 return error;
40 }
41
42 return xchk_setup_ag_btree(sc, false);
43}
44
45/* Reference count btree scrubber. */
46
47/*
48 * Confirming Reference Counts via Reverse Mappings
49 *
50 * We want to count the reverse mappings overlapping a refcount record
51 * (bno, len, refcount), allowing for the possibility that some of the
52 * overlap may come from smaller adjoining reverse mappings, while some
53 * comes from single extents which overlap the range entirely. The
54 * outer loop is as follows:
55 *
56 * 1. For all reverse mappings overlapping the refcount extent,
57 * a. If a given rmap completely overlaps, mark it as seen.
58 * b. Otherwise, record the fragment (in agbno order) for later
59 * processing.
60 *
61 * Once we've seen all the rmaps, we know that for all blocks in the
62 * refcount record we want to find $refcount owners and we've already
63 * visited $seen extents that overlap all the blocks. Therefore, we
64 * need to find ($refcount - $seen) owners for every block in the
65 * extent; call that quantity $target_nr. Proceed as follows:
66 *
67 * 2. Pull the first $target_nr fragments from the list; all of them
68 * should start at or before the start of the extent.
69 * Call this subset of fragments the working set.
70 * 3. Until there are no more unprocessed fragments,
71 * a. Find the shortest fragments in the set and remove them.
72 * b. Note the block number of the end of these fragments.
73 * c. Pull the same number of fragments from the list. All of these
74 * fragments should start at the block number recorded in the
75 * previous step.
76 * d. Put those fragments in the set.
77 * 4. Check that there are $target_nr fragments remaining in the list,
78 * and that they all end at or beyond the end of the refcount extent.
79 *
80 * If the refcount is correct, all the check conditions in the algorithm
81 * should always hold true. If not, the refcount is incorrect.
82 */
83struct xchk_refcnt_frag {
84 struct list_head list;
85 struct xfs_rmap_irec rm;
86};
87
88struct xchk_refcnt_check {
89 struct xfs_scrub *sc;
90 struct list_head fragments;
91
92 /* refcount extent we're examining */
93 xfs_agblock_t bno;
94 xfs_extlen_t len;
95 xfs_nlink_t refcount;
96
97 /* number of owners seen */
98 xfs_nlink_t seen;
99};
100
101/*
102 * Decide if the given rmap is large enough that we can redeem it
103 * towards refcount verification now, or if it's a fragment, in
104 * which case we'll hang onto it in the hopes that we'll later
105 * discover that we've collected exactly the correct number of
106 * fragments as the refcountbt says we should have.
107 */
108STATIC int
109xchk_refcountbt_rmap_check(
110 struct xfs_btree_cur *cur,
111 const struct xfs_rmap_irec *rec,
112 void *priv)
113{
114 struct xchk_refcnt_check *refchk = priv;
115 struct xchk_refcnt_frag *frag;
116 xfs_agblock_t rm_last;
117 xfs_agblock_t rc_last;
118 int error = 0;
119
120 if (xchk_should_terminate(refchk->sc, &error))
121 return error;
122
123 rm_last = rec->rm_startblock + rec->rm_blockcount - 1;
124 rc_last = refchk->bno + refchk->len - 1;
125
126 /* Confirm that a single-owner refc extent is a CoW stage. */
127 if (refchk->refcount == 1 && rec->rm_owner != XFS_RMAP_OWN_COW) {
128 xchk_btree_xref_set_corrupt(refchk->sc, cur, 0);
129 return 0;
130 }
131
132 if (rec->rm_startblock <= refchk->bno && rm_last >= rc_last) {
133 /*
134 * The rmap overlaps the refcount record, so we can confirm
135 * one refcount owner seen.
136 */
137 refchk->seen++;
138 } else {
139 /*
140 * This rmap covers only part of the refcount record, so
141 * save the fragment for later processing. If the rmapbt
142 * is healthy each rmap_irec we see will be in agbno order
143 * so we don't need insertion sort here.
144 */
145 frag = kmalloc(sizeof(struct xchk_refcnt_frag),
146 XCHK_GFP_FLAGS);
147 if (!frag)
148 return -ENOMEM;
149 memcpy(&frag->rm, rec, sizeof(frag->rm));
150 list_add_tail(&frag->list, &refchk->fragments);
151 }
152
153 return 0;
154}
155
156/*
157 * Given a bunch of rmap fragments, iterate through them, keeping
158 * a running tally of the refcount. If this ever deviates from
159 * what we expect (which is the refcountbt's refcount minus the
160 * number of extents that totally covered the refcountbt extent),
161 * we have a refcountbt error.
162 */
163STATIC void
164xchk_refcountbt_process_rmap_fragments(
165 struct xchk_refcnt_check *refchk)
166{
167 struct list_head worklist;
168 struct xchk_refcnt_frag *frag;
169 struct xchk_refcnt_frag *n;
170 xfs_agblock_t bno;
171 xfs_agblock_t rbno;
172 xfs_agblock_t next_rbno;
173 xfs_nlink_t nr;
174 xfs_nlink_t target_nr;
175
176 target_nr = refchk->refcount - refchk->seen;
177 if (target_nr == 0)
178 return;
179
180 /*
181 * There are (refchk->rc.rc_refcount - refchk->nr refcount)
182 * references we haven't found yet. Pull that many off the
183 * fragment list and figure out where the smallest rmap ends
184 * (and therefore the next rmap should start). All the rmaps
185 * we pull off should start at or before the beginning of the
186 * refcount record's range.
187 */
188 INIT_LIST_HEAD(&worklist);
189 rbno = NULLAGBLOCK;
190
191 /* Make sure the fragments actually /are/ in agbno order. */
192 bno = 0;
193 list_for_each_entry(frag, &refchk->fragments, list) {
194 if (frag->rm.rm_startblock < bno)
195 goto done;
196 bno = frag->rm.rm_startblock;
197 }
198
199 /*
200 * Find all the rmaps that start at or before the refc extent,
201 * and put them on the worklist.
202 */
203 nr = 0;
204 list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
205 if (frag->rm.rm_startblock > refchk->bno || nr > target_nr)
206 break;
207 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
208 if (bno < rbno)
209 rbno = bno;
210 list_move_tail(&frag->list, &worklist);
211 nr++;
212 }
213
214 /*
215 * We should have found exactly $target_nr rmap fragments starting
216 * at or before the refcount extent.
217 */
218 if (nr != target_nr)
219 goto done;
220
221 while (!list_empty(&refchk->fragments)) {
222 /* Discard any fragments ending at rbno from the worklist. */
223 nr = 0;
224 next_rbno = NULLAGBLOCK;
225 list_for_each_entry_safe(frag, n, &worklist, list) {
226 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
227 if (bno != rbno) {
228 if (bno < next_rbno)
229 next_rbno = bno;
230 continue;
231 }
232 list_del(&frag->list);
233 kfree(frag);
234 nr++;
235 }
236
237 /* Try to add nr rmaps starting at rbno to the worklist. */
238 list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
239 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
240 if (frag->rm.rm_startblock != rbno)
241 goto done;
242 list_move_tail(&frag->list, &worklist);
243 if (next_rbno > bno)
244 next_rbno = bno;
245 nr--;
246 if (nr == 0)
247 break;
248 }
249
250 /*
251 * If we get here and nr > 0, this means that we added fewer
252 * items to the worklist than we discarded because the fragment
253 * list ran out of items. Therefore, we cannot maintain the
254 * required refcount. Something is wrong, so we're done.
255 */
256 if (nr)
257 goto done;
258
259 rbno = next_rbno;
260 }
261
262 /*
263 * Make sure the last extent we processed ends at or beyond
264 * the end of the refcount extent.
265 */
266 if (rbno < refchk->bno + refchk->len)
267 goto done;
268
269 /* Actually record us having seen the remaining refcount. */
270 refchk->seen = refchk->refcount;
271done:
272 /* Delete fragments and work list. */
273 list_for_each_entry_safe(frag, n, &worklist, list) {
274 list_del(&frag->list);
275 kfree(frag);
276 }
277 list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
278 list_del(&frag->list);
279 kfree(frag);
280 }
281}
282
283/* Use the rmap entries covering this extent to verify the refcount. */
284STATIC void
285xchk_refcountbt_xref_rmap(
286 struct xfs_scrub *sc,
287 const struct xfs_refcount_irec *irec)
288{
289 struct xchk_refcnt_check refchk = {
290 .sc = sc,
291 .bno = irec->rc_startblock,
292 .len = irec->rc_blockcount,
293 .refcount = irec->rc_refcount,
294 .seen = 0,
295 };
296 struct xfs_rmap_irec low;
297 struct xfs_rmap_irec high;
298 struct xchk_refcnt_frag *frag;
299 struct xchk_refcnt_frag *n;
300 int error;
301
302 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
303 return;
304
305 /* Cross-reference with the rmapbt to confirm the refcount. */
306 memset(&low, 0, sizeof(low));
307 low.rm_startblock = irec->rc_startblock;
308 memset(&high, 0xFF, sizeof(high));
309 high.rm_startblock = irec->rc_startblock + irec->rc_blockcount - 1;
310
311 INIT_LIST_HEAD(&refchk.fragments);
312 error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high,
313 &xchk_refcountbt_rmap_check, &refchk);
314 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
315 goto out_free;
316
317 xchk_refcountbt_process_rmap_fragments(&refchk);
318 if (irec->rc_refcount != refchk.seen) {
319 trace_xchk_refcount_incorrect(sc->sa.pag, irec, refchk.seen);
320 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
321 }
322
323out_free:
324 list_for_each_entry_safe(frag, n, &refchk.fragments, list) {
325 list_del(&frag->list);
326 kfree(frag);
327 }
328}
329
330/* Cross-reference with the other btrees. */
331STATIC void
332xchk_refcountbt_xref(
333 struct xfs_scrub *sc,
334 const struct xfs_refcount_irec *irec)
335{
336 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
337 return;
338
339 xchk_xref_is_used_space(sc, irec->rc_startblock, irec->rc_blockcount);
340 xchk_xref_is_not_inode_chunk(sc, irec->rc_startblock,
341 irec->rc_blockcount);
342 xchk_refcountbt_xref_rmap(sc, irec);
343}
344
345struct xchk_refcbt_records {
346 /* Previous refcount record. */
347 struct xfs_refcount_irec prev_rec;
348
349 /* The next AG block where we aren't expecting shared extents. */
350 xfs_agblock_t next_unshared_agbno;
351
352 /* Number of CoW blocks we expect. */
353 xfs_agblock_t cow_blocks;
354
355 /* Was the last record a shared or CoW staging extent? */
356 enum xfs_refc_domain prev_domain;
357};
358
359STATIC int
360xchk_refcountbt_rmap_check_gap(
361 struct xfs_btree_cur *cur,
362 const struct xfs_rmap_irec *rec,
363 void *priv)
364{
365 xfs_agblock_t *next_bno = priv;
366
367 if (*next_bno != NULLAGBLOCK && rec->rm_startblock < *next_bno)
368 return -ECANCELED;
369
370 *next_bno = rec->rm_startblock + rec->rm_blockcount;
371 return 0;
372}
373
374/*
375 * Make sure that a gap in the reference count records does not correspond to
376 * overlapping records (i.e. shared extents) in the reverse mappings.
377 */
378static inline void
379xchk_refcountbt_xref_gaps(
380 struct xfs_scrub *sc,
381 struct xchk_refcbt_records *rrc,
382 xfs_agblock_t bno)
383{
384 struct xfs_rmap_irec low;
385 struct xfs_rmap_irec high;
386 xfs_agblock_t next_bno = NULLAGBLOCK;
387 int error;
388
389 if (bno <= rrc->next_unshared_agbno || !sc->sa.rmap_cur ||
390 xchk_skip_xref(sc->sm))
391 return;
392
393 memset(&low, 0, sizeof(low));
394 low.rm_startblock = rrc->next_unshared_agbno;
395 memset(&high, 0xFF, sizeof(high));
396 high.rm_startblock = bno - 1;
397
398 error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high,
399 xchk_refcountbt_rmap_check_gap, &next_bno);
400 if (error == -ECANCELED)
401 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
402 else
403 xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur);
404}
405
406static inline bool
407xchk_refcount_mergeable(
408 struct xchk_refcbt_records *rrc,
409 const struct xfs_refcount_irec *r2)
410{
411 const struct xfs_refcount_irec *r1 = &rrc->prev_rec;
412
413 /* Ignore if prev_rec is not yet initialized. */
414 if (r1->rc_blockcount > 0)
415 return false;
416
417 if (r1->rc_domain != r2->rc_domain)
418 return false;
419 if (r1->rc_startblock + r1->rc_blockcount != r2->rc_startblock)
420 return false;
421 if (r1->rc_refcount != r2->rc_refcount)
422 return false;
423 if ((unsigned long long)r1->rc_blockcount + r2->rc_blockcount >
424 MAXREFCEXTLEN)
425 return false;
426
427 return true;
428}
429
430/* Flag failures for records that could be merged. */
431STATIC void
432xchk_refcountbt_check_mergeable(
433 struct xchk_btree *bs,
434 struct xchk_refcbt_records *rrc,
435 const struct xfs_refcount_irec *irec)
436{
437 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
438 return;
439
440 if (xchk_refcount_mergeable(rrc, irec))
441 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
442
443 memcpy(&rrc->prev_rec, irec, sizeof(struct xfs_refcount_irec));
444}
445
446/* Scrub a refcountbt record. */
447STATIC int
448xchk_refcountbt_rec(
449 struct xchk_btree *bs,
450 const union xfs_btree_rec *rec)
451{
452 struct xfs_refcount_irec irec;
453 struct xchk_refcbt_records *rrc = bs->private;
454
455 xfs_refcount_btrec_to_irec(rec, &irec);
456 if (xfs_refcount_check_irec(to_perag(bs->cur->bc_group), &irec) !=
457 NULL) {
458 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
459 return 0;
460 }
461
462 if (irec.rc_domain == XFS_REFC_DOMAIN_COW)
463 rrc->cow_blocks += irec.rc_blockcount;
464
465 /* Shared records always come before CoW records. */
466 if (irec.rc_domain == XFS_REFC_DOMAIN_SHARED &&
467 rrc->prev_domain == XFS_REFC_DOMAIN_COW)
468 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
469 rrc->prev_domain = irec.rc_domain;
470
471 xchk_refcountbt_check_mergeable(bs, rrc, &irec);
472 xchk_refcountbt_xref(bs->sc, &irec);
473
474 /*
475 * If this is a record for a shared extent, check that all blocks
476 * between the previous record and this one have at most one reverse
477 * mapping.
478 */
479 if (irec.rc_domain == XFS_REFC_DOMAIN_SHARED) {
480 xchk_refcountbt_xref_gaps(bs->sc, rrc, irec.rc_startblock);
481 rrc->next_unshared_agbno = irec.rc_startblock +
482 irec.rc_blockcount;
483 }
484
485 return 0;
486}
487
488/* Make sure we have as many refc blocks as the rmap says. */
489STATIC void
490xchk_refcount_xref_rmap(
491 struct xfs_scrub *sc,
492 xfs_filblks_t cow_blocks)
493{
494 xfs_filblks_t refcbt_blocks = 0;
495 xfs_filblks_t blocks;
496 int error;
497
498 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
499 return;
500
501 /* Check that we saw as many refcbt blocks as the rmap knows about. */
502 error = xfs_btree_count_blocks(sc->sa.refc_cur, &refcbt_blocks);
503 if (!xchk_btree_process_error(sc, sc->sa.refc_cur, 0, &error))
504 return;
505 error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
506 &XFS_RMAP_OINFO_REFC, &blocks);
507 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
508 return;
509 if (blocks != refcbt_blocks)
510 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
511
512 /* Check that we saw as many cow blocks as the rmap knows about. */
513 error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
514 &XFS_RMAP_OINFO_COW, &blocks);
515 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
516 return;
517 if (blocks != cow_blocks)
518 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
519}
520
521/* Scrub the refcount btree for some AG. */
522int
523xchk_refcountbt(
524 struct xfs_scrub *sc)
525{
526 struct xchk_refcbt_records rrc = {
527 .cow_blocks = 0,
528 .next_unshared_agbno = 0,
529 .prev_domain = XFS_REFC_DOMAIN_SHARED,
530 };
531 int error;
532
533 error = xchk_btree(sc, sc->sa.refc_cur, xchk_refcountbt_rec,
534 &XFS_RMAP_OINFO_REFC, &rrc);
535 if (error)
536 return error;
537
538 /*
539 * Check that all blocks between the last refcount > 1 record and the
540 * end of the AG have at most one reverse mapping.
541 */
542 xchk_refcountbt_xref_gaps(sc, &rrc, sc->mp->m_sb.sb_agblocks);
543
544 xchk_refcount_xref_rmap(sc, rrc.cow_blocks);
545
546 return 0;
547}
548
549/* xref check that a cow staging extent is marked in the refcountbt. */
550void
551xchk_xref_is_cow_staging(
552 struct xfs_scrub *sc,
553 xfs_agblock_t agbno,
554 xfs_extlen_t len)
555{
556 struct xfs_refcount_irec rc;
557 int has_refcount;
558 int error;
559
560 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
561 return;
562
563 /* Find the CoW staging extent. */
564 error = xfs_refcount_lookup_le(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW,
565 agbno, &has_refcount);
566 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
567 return;
568 if (!has_refcount) {
569 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
570 return;
571 }
572
573 error = xfs_refcount_get_rec(sc->sa.refc_cur, &rc, &has_refcount);
574 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
575 return;
576 if (!has_refcount) {
577 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
578 return;
579 }
580
581 /* CoW lookup returned a shared extent record? */
582 if (rc.rc_domain != XFS_REFC_DOMAIN_COW)
583 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
584
585 /* Must be at least as long as what was passed in */
586 if (rc.rc_blockcount < len)
587 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
588}
589
590/*
591 * xref check that the extent is not shared. Only file data blocks
592 * can have multiple owners.
593 */
594void
595xchk_xref_is_not_shared(
596 struct xfs_scrub *sc,
597 xfs_agblock_t agbno,
598 xfs_extlen_t len)
599{
600 enum xbtree_recpacking outcome;
601 int error;
602
603 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
604 return;
605
606 error = xfs_refcount_has_records(sc->sa.refc_cur,
607 XFS_REFC_DOMAIN_SHARED, agbno, len, &outcome);
608 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
609 return;
610 if (outcome != XBTREE_RECPACKING_EMPTY)
611 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
612}
613
614/* xref check that the extent is not being used for CoW staging. */
615void
616xchk_xref_is_not_cow_staging(
617 struct xfs_scrub *sc,
618 xfs_agblock_t agbno,
619 xfs_extlen_t len)
620{
621 enum xbtree_recpacking outcome;
622 int error;
623
624 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
625 return;
626
627 error = xfs_refcount_has_records(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW,
628 agbno, len, &outcome);
629 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
630 return;
631 if (outcome != XBTREE_RECPACKING_EMPTY)
632 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
633}