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