1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018-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_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_sb.h"
16 #include "xfs_inode.h"
17 #include "xfs_alloc.h"
18 #include "xfs_alloc_btree.h"
19 #include "xfs_ialloc.h"
20 #include "xfs_ialloc_btree.h"
21 #include "xfs_rmap.h"
22 #include "xfs_rmap_btree.h"
23 #include "xfs_refcount_btree.h"
24 #include "xfs_rtbitmap.h"
25 #include "xfs_extent_busy.h"
26 #include "xfs_ag.h"
27 #include "xfs_ag_resv.h"
28 #include "xfs_quota.h"
29 #include "xfs_qm.h"
30 #include "xfs_defer.h"
31 #include "xfs_errortag.h"
32 #include "xfs_error.h"
33 #include "xfs_reflink.h"
34 #include "xfs_health.h"
35 #include "xfs_buf_mem.h"
36 #include "xfs_da_format.h"
37 #include "xfs_da_btree.h"
38 #include "xfs_attr.h"
39 #include "xfs_dir2.h"
40 #include "xfs_rtrmap_btree.h"
41 #include "xfs_rtbitmap.h"
42 #include "xfs_rtgroup.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_metafile.h"
45 #include "xfs_rtrefcount_btree.h"
46 #include "scrub/scrub.h"
47 #include "scrub/common.h"
48 #include "scrub/trace.h"
49 #include "scrub/repair.h"
50 #include "scrub/bitmap.h"
51 #include "scrub/stats.h"
52 #include "scrub/xfile.h"
53 #include "scrub/attr_repair.h"
54
55 /*
56 * Attempt to repair some metadata, if the metadata is corrupt and userspace
57 * told us to fix it. This function returns -EAGAIN to mean "re-run scrub",
58 * and will set *fixed to true if it thinks it repaired anything.
59 */
60 int
xrep_attempt(struct xfs_scrub * sc,struct xchk_stats_run * run)61 xrep_attempt(
62 struct xfs_scrub *sc,
63 struct xchk_stats_run *run)
64 {
65 u64 repair_start;
66 int error = 0;
67
68 trace_xrep_attempt(XFS_I(file_inode(sc->file)), sc->sm, error);
69
70 xchk_ag_btcur_free(&sc->sa);
71 xchk_rtgroup_btcur_free(&sc->sr);
72
73 /* Repair whatever's broken. */
74 ASSERT(sc->ops->repair);
75 run->repair_attempted = true;
76 repair_start = xchk_stats_now();
77 error = sc->ops->repair(sc);
78 trace_xrep_done(XFS_I(file_inode(sc->file)), sc->sm, error);
79 run->repair_ns += xchk_stats_elapsed_ns(repair_start);
80 switch (error) {
81 case 0:
82 /*
83 * Repair succeeded. Commit the fixes and perform a second
84 * scrub so that we can tell userspace if we fixed the problem.
85 */
86 sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
87 sc->flags |= XREP_ALREADY_FIXED;
88 run->repair_succeeded = true;
89 return -EAGAIN;
90 case -ECHRNG:
91 sc->flags |= XCHK_NEED_DRAIN;
92 run->retries++;
93 return -EAGAIN;
94 case -EDEADLOCK:
95 /* Tell the caller to try again having grabbed all the locks. */
96 if (!(sc->flags & XCHK_TRY_HARDER)) {
97 sc->flags |= XCHK_TRY_HARDER;
98 run->retries++;
99 return -EAGAIN;
100 }
101 /*
102 * We tried harder but still couldn't grab all the resources
103 * we needed to fix it. The corruption has not been fixed,
104 * so exit to userspace with the scan's output flags unchanged.
105 */
106 return 0;
107 default:
108 /*
109 * EAGAIN tells the caller to re-scrub, so we cannot return
110 * that here.
111 */
112 ASSERT(error != -EAGAIN);
113 return error;
114 }
115 }
116
117 /*
118 * Complain about unfixable problems in the filesystem. We don't log
119 * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
120 * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
121 * administrator isn't running xfs_scrub in no-repairs mode.
122 *
123 * Use this helper function because _ratelimited silently declares a static
124 * structure to track rate limiting information.
125 */
126 void
xrep_failure(struct xfs_mount * mp)127 xrep_failure(
128 struct xfs_mount *mp)
129 {
130 xfs_alert_ratelimited(mp,
131 "Corruption not fixed during online repair. Unmount and run xfs_repair.");
132 }
133
134 /*
135 * Repair probe -- userspace uses this to probe if we're willing to repair a
136 * given mountpoint.
137 */
138 int
xrep_probe(struct xfs_scrub * sc)139 xrep_probe(
140 struct xfs_scrub *sc)
141 {
142 int error = 0;
143
144 if (xchk_should_terminate(sc, &error))
145 return error;
146
147 return 0;
148 }
149
150 /*
151 * Roll a transaction, keeping the AG headers locked and reinitializing
152 * the btree cursors.
153 */
154 int
xrep_roll_ag_trans(struct xfs_scrub * sc)155 xrep_roll_ag_trans(
156 struct xfs_scrub *sc)
157 {
158 int error;
159
160 /*
161 * Keep the AG header buffers locked while we roll the transaction.
162 * Ensure that both AG buffers are dirty and held when we roll the
163 * transaction so that they move forward in the log without losing the
164 * bli (and hence the bli type) when the transaction commits.
165 *
166 * Normal code would never hold clean buffers across a roll, but repair
167 * needs both buffers to maintain a total lock on the AG.
168 */
169 if (sc->sa.agi_bp) {
170 xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM);
171 xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
172 }
173
174 if (sc->sa.agf_bp) {
175 xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM);
176 xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
177 }
178
179 /*
180 * Roll the transaction. We still hold the AG header buffers locked
181 * regardless of whether or not that succeeds. On failure, the buffers
182 * will be released during teardown on our way out of the kernel. If
183 * successful, join the buffers to the new transaction and move on.
184 */
185 error = xfs_trans_roll(&sc->tp);
186 if (error)
187 return error;
188
189 /* Join the AG headers to the new transaction. */
190 if (sc->sa.agi_bp)
191 xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
192 if (sc->sa.agf_bp)
193 xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
194
195 return 0;
196 }
197
198 /* Roll the scrub transaction, holding the primary metadata locked. */
199 int
xrep_roll_trans(struct xfs_scrub * sc)200 xrep_roll_trans(
201 struct xfs_scrub *sc)
202 {
203 if (!sc->ip)
204 return xrep_roll_ag_trans(sc);
205 return xfs_trans_roll_inode(&sc->tp, sc->ip);
206 }
207
208 /* Finish all deferred work attached to the repair transaction. */
209 int
xrep_defer_finish(struct xfs_scrub * sc)210 xrep_defer_finish(
211 struct xfs_scrub *sc)
212 {
213 int error;
214
215 /*
216 * Keep the AG header buffers locked while we complete deferred work
217 * items. Ensure that both AG buffers are dirty and held when we roll
218 * the transaction so that they move forward in the log without losing
219 * the bli (and hence the bli type) when the transaction commits.
220 *
221 * Normal code would never hold clean buffers across a roll, but repair
222 * needs both buffers to maintain a total lock on the AG.
223 */
224 if (sc->sa.agi_bp) {
225 xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM);
226 xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
227 }
228
229 if (sc->sa.agf_bp) {
230 xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM);
231 xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
232 }
233
234 /*
235 * Finish all deferred work items. We still hold the AG header buffers
236 * locked regardless of whether or not that succeeds. On failure, the
237 * buffers will be released during teardown on our way out of the
238 * kernel. If successful, join the buffers to the new transaction
239 * and move on.
240 */
241 error = xfs_defer_finish(&sc->tp);
242 if (error)
243 return error;
244
245 /*
246 * Release the hold that we set above because defer_finish won't do
247 * that for us. The defer roll code redirties held buffers after each
248 * roll, so the AG header buffers should be ready for logging.
249 */
250 if (sc->sa.agi_bp)
251 xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp);
252 if (sc->sa.agf_bp)
253 xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp);
254
255 return 0;
256 }
257
258 /*
259 * Does the given AG have enough space to rebuild a btree? Neither AG
260 * reservation can be critical, and we must have enough space (factoring
261 * in AG reservations) to construct a whole btree.
262 */
263 bool
xrep_ag_has_space(struct xfs_perag * pag,xfs_extlen_t nr_blocks,enum xfs_ag_resv_type type)264 xrep_ag_has_space(
265 struct xfs_perag *pag,
266 xfs_extlen_t nr_blocks,
267 enum xfs_ag_resv_type type)
268 {
269 return !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
270 !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
271 pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
272 }
273
274 /*
275 * Figure out how many blocks to reserve for an AG repair. We calculate the
276 * worst case estimate for the number of blocks we'd need to rebuild one of
277 * any type of per-AG btree.
278 */
279 xfs_extlen_t
xrep_calc_ag_resblks(struct xfs_scrub * sc)280 xrep_calc_ag_resblks(
281 struct xfs_scrub *sc)
282 {
283 struct xfs_mount *mp = sc->mp;
284 struct xfs_scrub_metadata *sm = sc->sm;
285 struct xfs_perag *pag;
286 struct xfs_buf *bp;
287 xfs_agino_t icount = NULLAGINO;
288 xfs_extlen_t aglen = NULLAGBLOCK;
289 xfs_extlen_t usedlen;
290 xfs_extlen_t freelen;
291 xfs_extlen_t bnobt_sz;
292 xfs_extlen_t inobt_sz;
293 xfs_extlen_t rmapbt_sz;
294 xfs_extlen_t refcbt_sz;
295 int error;
296
297 if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
298 return 0;
299
300 pag = xfs_perag_get(mp, sm->sm_agno);
301 if (xfs_perag_initialised_agi(pag)) {
302 /* Use in-core icount if possible. */
303 icount = pag->pagi_count;
304 } else {
305 /* Try to get the actual counters from disk. */
306 error = xfs_ialloc_read_agi(pag, NULL, 0, &bp);
307 if (!error) {
308 icount = pag->pagi_count;
309 xfs_buf_relse(bp);
310 }
311 }
312
313 /* Now grab the block counters from the AGF. */
314 error = xfs_alloc_read_agf(pag, NULL, 0, &bp);
315 if (error) {
316 aglen = pag_group(pag)->xg_block_count;
317 freelen = aglen;
318 usedlen = aglen;
319 } else {
320 struct xfs_agf *agf = bp->b_addr;
321
322 aglen = be32_to_cpu(agf->agf_length);
323 freelen = be32_to_cpu(agf->agf_freeblks);
324 usedlen = aglen - freelen;
325 xfs_buf_relse(bp);
326 }
327
328 /* If the icount is impossible, make some worst-case assumptions. */
329 if (icount == NULLAGINO ||
330 !xfs_verify_agino(pag, icount)) {
331 icount = pag->agino_max - pag->agino_min + 1;
332 }
333
334 /* If the block counts are impossible, make worst-case assumptions. */
335 if (aglen == NULLAGBLOCK ||
336 aglen != pag_group(pag)->xg_block_count ||
337 freelen >= aglen) {
338 aglen = pag_group(pag)->xg_block_count;
339 freelen = aglen;
340 usedlen = aglen;
341 }
342
343 trace_xrep_calc_ag_resblks(pag, icount, aglen, freelen, usedlen);
344
345 /*
346 * Figure out how many blocks we'd need worst case to rebuild
347 * each type of btree. Note that we can only rebuild the
348 * bnobt/cntbt or inobt/finobt as pairs.
349 */
350 bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
351 if (xfs_has_sparseinodes(mp))
352 inobt_sz = xfs_iallocbt_calc_size(mp, icount /
353 XFS_INODES_PER_HOLEMASK_BIT);
354 else
355 inobt_sz = xfs_iallocbt_calc_size(mp, icount /
356 XFS_INODES_PER_CHUNK);
357 if (xfs_has_finobt(mp))
358 inobt_sz *= 2;
359 if (xfs_has_reflink(mp))
360 refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
361 else
362 refcbt_sz = 0;
363 if (xfs_has_rmapbt(mp)) {
364 /*
365 * Guess how many blocks we need to rebuild the rmapbt.
366 * For non-reflink filesystems we can't have more records than
367 * used blocks. However, with reflink it's possible to have
368 * more than one rmap record per AG block. We don't know how
369 * many rmaps there could be in the AG, so we start off with
370 * what we hope is an generous over-estimation.
371 */
372 if (xfs_has_reflink(mp))
373 rmapbt_sz = xfs_rmapbt_calc_size(mp,
374 (unsigned long long)aglen * 2);
375 else
376 rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
377 } else {
378 rmapbt_sz = 0;
379 }
380
381 trace_xrep_calc_ag_resblks_btsize(pag, bnobt_sz, inobt_sz, rmapbt_sz,
382 refcbt_sz);
383 xfs_perag_put(pag);
384
385 return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
386 }
387
388 #ifdef CONFIG_XFS_RT
389 /*
390 * Figure out how many blocks to reserve for a rtgroup repair. We calculate
391 * the worst case estimate for the number of blocks we'd need to rebuild one of
392 * any type of per-rtgroup btree.
393 */
394 xfs_extlen_t
xrep_calc_rtgroup_resblks(struct xfs_scrub * sc)395 xrep_calc_rtgroup_resblks(
396 struct xfs_scrub *sc)
397 {
398 struct xfs_mount *mp = sc->mp;
399 struct xfs_scrub_metadata *sm = sc->sm;
400 uint64_t usedlen;
401 xfs_extlen_t rmapbt_sz = 0;
402
403 if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
404 return 0;
405 if (!xfs_has_rtgroups(mp)) {
406 ASSERT(0);
407 return -EFSCORRUPTED;
408 }
409
410 usedlen = xfs_rtbxlen_to_blen(mp, xfs_rtgroup_extents(mp, sm->sm_agno));
411 ASSERT(usedlen <= XFS_MAX_RGBLOCKS);
412
413 if (xfs_has_rmapbt(mp))
414 rmapbt_sz = xfs_rtrmapbt_calc_size(mp, usedlen);
415
416 trace_xrep_calc_rtgroup_resblks_btsize(mp, sm->sm_agno, usedlen,
417 rmapbt_sz);
418
419 return rmapbt_sz;
420 }
421 #endif /* CONFIG_XFS_RT */
422
423 /*
424 * Reconstructing per-AG Btrees
425 *
426 * When a space btree is corrupt, we don't bother trying to fix it. Instead,
427 * we scan secondary space metadata to derive the records that should be in
428 * the damaged btree, initialize a fresh btree root, and insert the records.
429 * Note that for rebuilding the rmapbt we scan all the primary data to
430 * generate the new records.
431 *
432 * However, that leaves the matter of removing all the metadata describing the
433 * old broken structure. For primary metadata we use the rmap data to collect
434 * every extent with a matching rmap owner (bitmap); we then iterate all other
435 * metadata structures with the same rmap owner to collect the extents that
436 * cannot be removed (sublist). We then subtract sublist from bitmap to
437 * derive the blocks that were used by the old btree. These blocks can be
438 * reaped.
439 *
440 * For rmapbt reconstructions we must use different tactics for extent
441 * collection. First we iterate all primary metadata (this excludes the old
442 * rmapbt, obviously) to generate new rmap records. The gaps in the rmap
443 * records are collected as bitmap. The bnobt records are collected as
444 * sublist. As with the other btrees we subtract sublist from bitmap, and the
445 * result (since the rmapbt lives in the free space) are the blocks from the
446 * old rmapbt.
447 */
448
449 /* Ensure the freelist is the correct size. */
450 int
xrep_fix_freelist(struct xfs_scrub * sc,int alloc_flags)451 xrep_fix_freelist(
452 struct xfs_scrub *sc,
453 int alloc_flags)
454 {
455 struct xfs_alloc_arg args = {0};
456
457 args.mp = sc->mp;
458 args.tp = sc->tp;
459 args.agno = pag_agno(sc->sa.pag);
460 args.alignment = 1;
461 args.pag = sc->sa.pag;
462
463 return xfs_alloc_fix_freelist(&args, alloc_flags);
464 }
465
466 /*
467 * Finding per-AG Btree Roots for AGF/AGI Reconstruction
468 *
469 * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
470 * the AG headers by using the rmap data to rummage through the AG looking for
471 * btree roots. This is not guaranteed to work if the AG is heavily damaged
472 * or the rmap data are corrupt.
473 *
474 * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
475 * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
476 * AGI is being rebuilt. It must maintain these locks until it's safe for
477 * other threads to change the btrees' shapes. The caller provides
478 * information about the btrees to look for by passing in an array of
479 * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
480 * The (root, height) fields will be set on return if anything is found. The
481 * last element of the array should have a NULL buf_ops to mark the end of the
482 * array.
483 *
484 * For every rmapbt record matching any of the rmap owners in btree_info,
485 * read each block referenced by the rmap record. If the block is a btree
486 * block from this filesystem matching any of the magic numbers and has a
487 * level higher than what we've already seen, remember the block and the
488 * height of the tree required to have such a block. When the call completes,
489 * we return the highest block we've found for each btree description; those
490 * should be the roots.
491 */
492
493 struct xrep_findroot {
494 struct xfs_scrub *sc;
495 struct xfs_buf *agfl_bp;
496 struct xfs_agf *agf;
497 struct xrep_find_ag_btree *btree_info;
498 };
499
500 /* See if our block is in the AGFL. */
501 STATIC int
xrep_findroot_agfl_walk(struct xfs_mount * mp,xfs_agblock_t bno,void * priv)502 xrep_findroot_agfl_walk(
503 struct xfs_mount *mp,
504 xfs_agblock_t bno,
505 void *priv)
506 {
507 xfs_agblock_t *agbno = priv;
508
509 return (*agbno == bno) ? -ECANCELED : 0;
510 }
511
512 /* Does this block match the btree information passed in? */
513 STATIC int
xrep_findroot_block(struct xrep_findroot * ri,struct xrep_find_ag_btree * fab,uint64_t owner,xfs_agblock_t agbno,bool * done_with_block)514 xrep_findroot_block(
515 struct xrep_findroot *ri,
516 struct xrep_find_ag_btree *fab,
517 uint64_t owner,
518 xfs_agblock_t agbno,
519 bool *done_with_block)
520 {
521 struct xfs_mount *mp = ri->sc->mp;
522 struct xfs_buf *bp;
523 struct xfs_btree_block *btblock;
524 xfs_daddr_t daddr;
525 int block_level;
526 int error = 0;
527
528 daddr = xfs_agbno_to_daddr(ri->sc->sa.pag, agbno);
529
530 /*
531 * Blocks in the AGFL have stale contents that might just happen to
532 * have a matching magic and uuid. We don't want to pull these blocks
533 * in as part of a tree root, so we have to filter out the AGFL stuff
534 * here. If the AGFL looks insane we'll just refuse to repair.
535 */
536 if (owner == XFS_RMAP_OWN_AG) {
537 error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
538 xrep_findroot_agfl_walk, &agbno);
539 if (error == -ECANCELED)
540 return 0;
541 if (error)
542 return error;
543 }
544
545 /*
546 * Read the buffer into memory so that we can see if it's a match for
547 * our btree type. We have no clue if it is beforehand, and we want to
548 * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
549 * will cause needless disk reads in subsequent calls to this function)
550 * and logging metadata verifier failures.
551 *
552 * Therefore, pass in NULL buffer ops. If the buffer was already in
553 * memory from some other caller it will already have b_ops assigned.
554 * If it was in memory from a previous unsuccessful findroot_block
555 * call, the buffer won't have b_ops but it should be clean and ready
556 * for us to try to verify if the read call succeeds. The same applies
557 * if the buffer wasn't in memory at all.
558 *
559 * Note: If we never match a btree type with this buffer, it will be
560 * left in memory with NULL b_ops. This shouldn't be a problem unless
561 * the buffer gets written.
562 */
563 error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
564 mp->m_bsize, 0, &bp, NULL);
565 if (error)
566 return error;
567
568 /* Ensure the block magic matches the btree type we're looking for. */
569 btblock = XFS_BUF_TO_BLOCK(bp);
570 ASSERT(fab->buf_ops->magic[1] != 0);
571 if (btblock->bb_magic != fab->buf_ops->magic[1])
572 goto out;
573
574 /*
575 * If the buffer already has ops applied and they're not the ones for
576 * this btree type, we know this block doesn't match the btree and we
577 * can bail out.
578 *
579 * If the buffer ops match ours, someone else has already validated
580 * the block for us, so we can move on to checking if this is a root
581 * block candidate.
582 *
583 * If the buffer does not have ops, nobody has successfully validated
584 * the contents and the buffer cannot be dirty. If the magic, uuid,
585 * and structure match this btree type then we'll move on to checking
586 * if it's a root block candidate. If there is no match, bail out.
587 */
588 if (bp->b_ops) {
589 if (bp->b_ops != fab->buf_ops)
590 goto out;
591 } else {
592 ASSERT(!xfs_trans_buf_is_dirty(bp));
593 if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
594 &mp->m_sb.sb_meta_uuid))
595 goto out;
596 /*
597 * Read verifiers can reference b_ops, so we set the pointer
598 * here. If the verifier fails we'll reset the buffer state
599 * to what it was before we touched the buffer.
600 */
601 bp->b_ops = fab->buf_ops;
602 fab->buf_ops->verify_read(bp);
603 if (bp->b_error) {
604 bp->b_ops = NULL;
605 bp->b_error = 0;
606 goto out;
607 }
608
609 /*
610 * Some read verifiers will (re)set b_ops, so we must be
611 * careful not to change b_ops after running the verifier.
612 */
613 }
614
615 /*
616 * This block passes the magic/uuid and verifier tests for this btree
617 * type. We don't need the caller to try the other tree types.
618 */
619 *done_with_block = true;
620
621 /*
622 * Compare this btree block's level to the height of the current
623 * candidate root block.
624 *
625 * If the level matches the root we found previously, throw away both
626 * blocks because there can't be two candidate roots.
627 *
628 * If level is lower in the tree than the root we found previously,
629 * ignore this block.
630 */
631 block_level = xfs_btree_get_level(btblock);
632 if (block_level + 1 == fab->height) {
633 fab->root = NULLAGBLOCK;
634 goto out;
635 } else if (block_level < fab->height) {
636 goto out;
637 }
638
639 /*
640 * This is the highest block in the tree that we've found so far.
641 * Update the btree height to reflect what we've learned from this
642 * block.
643 */
644 fab->height = block_level + 1;
645
646 /*
647 * If this block doesn't have sibling pointers, then it's the new root
648 * block candidate. Otherwise, the root will be found farther up the
649 * tree.
650 */
651 if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
652 btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
653 fab->root = agbno;
654 else
655 fab->root = NULLAGBLOCK;
656
657 trace_xrep_findroot_block(ri->sc->sa.pag, agbno,
658 be32_to_cpu(btblock->bb_magic), fab->height - 1);
659 out:
660 xfs_trans_brelse(ri->sc->tp, bp);
661 return error;
662 }
663
664 /*
665 * Do any of the blocks in this rmap record match one of the btrees we're
666 * looking for?
667 */
668 STATIC int
xrep_findroot_rmap(struct xfs_btree_cur * cur,const struct xfs_rmap_irec * rec,void * priv)669 xrep_findroot_rmap(
670 struct xfs_btree_cur *cur,
671 const struct xfs_rmap_irec *rec,
672 void *priv)
673 {
674 struct xrep_findroot *ri = priv;
675 struct xrep_find_ag_btree *fab;
676 xfs_agblock_t b;
677 bool done;
678 int error = 0;
679
680 /* Ignore anything that isn't AG metadata. */
681 if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
682 return 0;
683
684 /* Otherwise scan each block + btree type. */
685 for (b = 0; b < rec->rm_blockcount; b++) {
686 done = false;
687 for (fab = ri->btree_info; fab->buf_ops; fab++) {
688 if (rec->rm_owner != fab->rmap_owner)
689 continue;
690 error = xrep_findroot_block(ri, fab,
691 rec->rm_owner, rec->rm_startblock + b,
692 &done);
693 if (error)
694 return error;
695 if (done)
696 break;
697 }
698 }
699
700 return 0;
701 }
702
703 /* Find the roots of the per-AG btrees described in btree_info. */
704 int
xrep_find_ag_btree_roots(struct xfs_scrub * sc,struct xfs_buf * agf_bp,struct xrep_find_ag_btree * btree_info,struct xfs_buf * agfl_bp)705 xrep_find_ag_btree_roots(
706 struct xfs_scrub *sc,
707 struct xfs_buf *agf_bp,
708 struct xrep_find_ag_btree *btree_info,
709 struct xfs_buf *agfl_bp)
710 {
711 struct xfs_mount *mp = sc->mp;
712 struct xrep_findroot ri;
713 struct xrep_find_ag_btree *fab;
714 struct xfs_btree_cur *cur;
715 int error;
716
717 ASSERT(xfs_buf_islocked(agf_bp));
718 ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
719
720 ri.sc = sc;
721 ri.btree_info = btree_info;
722 ri.agf = agf_bp->b_addr;
723 ri.agfl_bp = agfl_bp;
724 for (fab = btree_info; fab->buf_ops; fab++) {
725 ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
726 ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
727 fab->root = NULLAGBLOCK;
728 fab->height = 0;
729 }
730
731 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
732 error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
733 xfs_btree_del_cursor(cur, error);
734
735 return error;
736 }
737
738 #ifdef CONFIG_XFS_QUOTA
739 /* Update some quota flags in the superblock. */
740 void
xrep_update_qflags(struct xfs_scrub * sc,unsigned int clear_flags,unsigned int set_flags)741 xrep_update_qflags(
742 struct xfs_scrub *sc,
743 unsigned int clear_flags,
744 unsigned int set_flags)
745 {
746 struct xfs_mount *mp = sc->mp;
747 struct xfs_buf *bp;
748
749 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
750 if ((mp->m_qflags & clear_flags) == 0 &&
751 (mp->m_qflags & set_flags) == set_flags)
752 goto no_update;
753
754 mp->m_qflags &= ~clear_flags;
755 mp->m_qflags |= set_flags;
756
757 spin_lock(&mp->m_sb_lock);
758 mp->m_sb.sb_qflags &= ~clear_flags;
759 mp->m_sb.sb_qflags |= set_flags;
760 spin_unlock(&mp->m_sb_lock);
761
762 /*
763 * Update the quota flags in the ondisk superblock without touching
764 * the summary counters. We have not quiesced inode chunk allocation,
765 * so we cannot coordinate with updates to the icount and ifree percpu
766 * counters.
767 */
768 bp = xfs_trans_getsb(sc->tp);
769 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
770 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
771 xfs_trans_log_buf(sc->tp, bp, 0, sizeof(struct xfs_dsb) - 1);
772
773 no_update:
774 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
775 }
776
777 /* Force a quotacheck the next time we mount. */
778 void
xrep_force_quotacheck(struct xfs_scrub * sc,xfs_dqtype_t type)779 xrep_force_quotacheck(
780 struct xfs_scrub *sc,
781 xfs_dqtype_t type)
782 {
783 uint flag;
784
785 flag = xfs_quota_chkd_flag(type);
786 if (!(flag & sc->mp->m_qflags))
787 return;
788
789 xrep_update_qflags(sc, flag, 0);
790 }
791
792 /*
793 * Attach dquots to this inode, or schedule quotacheck to fix them.
794 *
795 * This function ensures that the appropriate dquots are attached to an inode.
796 * We cannot allow the dquot code to allocate an on-disk dquot block here
797 * because we're already in transaction context. The on-disk dquot should
798 * already exist anyway. If the quota code signals corruption or missing quota
799 * information, schedule quotacheck, which will repair corruptions in the quota
800 * metadata.
801 */
802 int
xrep_ino_dqattach(struct xfs_scrub * sc)803 xrep_ino_dqattach(
804 struct xfs_scrub *sc)
805 {
806 int error;
807
808 ASSERT(sc->tp != NULL);
809 ASSERT(sc->ip != NULL);
810
811 error = xfs_qm_dqattach(sc->ip);
812 switch (error) {
813 case -EFSBADCRC:
814 case -EFSCORRUPTED:
815 case -ENOENT:
816 xfs_err_ratelimited(sc->mp,
817 "inode %llu repair encountered quota error %d, quotacheck forced.",
818 (unsigned long long)sc->ip->i_ino, error);
819 if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
820 xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
821 if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
822 xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
823 if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
824 xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
825 fallthrough;
826 case -ESRCH:
827 error = 0;
828 break;
829 default:
830 break;
831 }
832
833 return error;
834 }
835 #endif /* CONFIG_XFS_QUOTA */
836
837 /*
838 * Ensure that the inode being repaired is ready to handle a certain number of
839 * extents, or return EFSCORRUPTED. Caller must hold the ILOCK of the inode
840 * being repaired and have joined it to the scrub transaction.
841 */
842 int
xrep_ino_ensure_extent_count(struct xfs_scrub * sc,int whichfork,xfs_extnum_t nextents)843 xrep_ino_ensure_extent_count(
844 struct xfs_scrub *sc,
845 int whichfork,
846 xfs_extnum_t nextents)
847 {
848 xfs_extnum_t max_extents;
849 bool inode_has_nrext64;
850
851 inode_has_nrext64 = xfs_inode_has_large_extent_counts(sc->ip);
852 max_extents = xfs_iext_max_nextents(inode_has_nrext64, whichfork);
853 if (nextents <= max_extents)
854 return 0;
855 if (inode_has_nrext64)
856 return -EFSCORRUPTED;
857 if (!xfs_has_large_extent_counts(sc->mp))
858 return -EFSCORRUPTED;
859
860 max_extents = xfs_iext_max_nextents(true, whichfork);
861 if (nextents > max_extents)
862 return -EFSCORRUPTED;
863
864 sc->ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
865 xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
866 return 0;
867 }
868
869 /*
870 * Initialize all the btree cursors for an AG repair except for the btree that
871 * we're rebuilding.
872 */
873 void
xrep_ag_btcur_init(struct xfs_scrub * sc,struct xchk_ag * sa)874 xrep_ag_btcur_init(
875 struct xfs_scrub *sc,
876 struct xchk_ag *sa)
877 {
878 struct xfs_mount *mp = sc->mp;
879
880 /* Set up a bnobt cursor for cross-referencing. */
881 if (sc->sm->sm_type != XFS_SCRUB_TYPE_BNOBT &&
882 sc->sm->sm_type != XFS_SCRUB_TYPE_CNTBT) {
883 sa->bno_cur = xfs_bnobt_init_cursor(mp, sc->tp, sa->agf_bp,
884 sc->sa.pag);
885 sa->cnt_cur = xfs_cntbt_init_cursor(mp, sc->tp, sa->agf_bp,
886 sc->sa.pag);
887 }
888
889 /* Set up a inobt cursor for cross-referencing. */
890 if (sc->sm->sm_type != XFS_SCRUB_TYPE_INOBT &&
891 sc->sm->sm_type != XFS_SCRUB_TYPE_FINOBT) {
892 sa->ino_cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp,
893 sa->agi_bp);
894 if (xfs_has_finobt(mp))
895 sa->fino_cur = xfs_finobt_init_cursor(sc->sa.pag,
896 sc->tp, sa->agi_bp);
897 }
898
899 /* Set up a rmapbt cursor for cross-referencing. */
900 if (sc->sm->sm_type != XFS_SCRUB_TYPE_RMAPBT &&
901 xfs_has_rmapbt(mp))
902 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
903 sc->sa.pag);
904
905 /* Set up a refcountbt cursor for cross-referencing. */
906 if (sc->sm->sm_type != XFS_SCRUB_TYPE_REFCNTBT &&
907 xfs_has_reflink(mp))
908 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
909 sa->agf_bp, sc->sa.pag);
910 }
911
912 /*
913 * Reinitialize the in-core AG state after a repair by rereading the AGF
914 * buffer. We had better get the same AGF buffer as the one that's attached
915 * to the scrub context.
916 */
917 int
xrep_reinit_pagf(struct xfs_scrub * sc)918 xrep_reinit_pagf(
919 struct xfs_scrub *sc)
920 {
921 struct xfs_perag *pag = sc->sa.pag;
922 struct xfs_buf *bp;
923 int error;
924
925 ASSERT(pag);
926 ASSERT(xfs_perag_initialised_agf(pag));
927
928 clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
929 error = xfs_alloc_read_agf(pag, sc->tp, 0, &bp);
930 if (error)
931 return error;
932
933 if (bp != sc->sa.agf_bp) {
934 ASSERT(bp == sc->sa.agf_bp);
935 return -EFSCORRUPTED;
936 }
937
938 return 0;
939 }
940
941 /*
942 * Reinitialize the in-core AG state after a repair by rereading the AGI
943 * buffer. We had better get the same AGI buffer as the one that's attached
944 * to the scrub context.
945 */
946 int
xrep_reinit_pagi(struct xfs_scrub * sc)947 xrep_reinit_pagi(
948 struct xfs_scrub *sc)
949 {
950 struct xfs_perag *pag = sc->sa.pag;
951 struct xfs_buf *bp;
952 int error;
953
954 ASSERT(pag);
955 ASSERT(xfs_perag_initialised_agi(pag));
956
957 clear_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
958 error = xfs_ialloc_read_agi(pag, sc->tp, 0, &bp);
959 if (error)
960 return error;
961
962 if (bp != sc->sa.agi_bp) {
963 ASSERT(bp == sc->sa.agi_bp);
964 return -EFSCORRUPTED;
965 }
966
967 return 0;
968 }
969
970 /*
971 * Given an active reference to a perag structure, load AG headers and cursors.
972 * This should only be called to scan an AG while repairing file-based metadata.
973 */
974 int
xrep_ag_init(struct xfs_scrub * sc,struct xfs_perag * pag,struct xchk_ag * sa)975 xrep_ag_init(
976 struct xfs_scrub *sc,
977 struct xfs_perag *pag,
978 struct xchk_ag *sa)
979 {
980 int error;
981
982 ASSERT(!sa->pag);
983
984 error = xfs_ialloc_read_agi(pag, sc->tp, 0, &sa->agi_bp);
985 if (error)
986 return error;
987
988 error = xfs_alloc_read_agf(pag, sc->tp, 0, &sa->agf_bp);
989 if (error)
990 return error;
991
992 /* Grab our own passive reference from the caller's ref. */
993 sa->pag = xfs_perag_hold(pag);
994 xrep_ag_btcur_init(sc, sa);
995 return 0;
996 }
997
998 #ifdef CONFIG_XFS_RT
999 /* Initialize all the btree cursors for a RT repair. */
1000 void
xrep_rtgroup_btcur_init(struct xfs_scrub * sc,struct xchk_rt * sr)1001 xrep_rtgroup_btcur_init(
1002 struct xfs_scrub *sc,
1003 struct xchk_rt *sr)
1004 {
1005 struct xfs_mount *mp = sc->mp;
1006
1007 ASSERT(sr->rtg != NULL);
1008
1009 if (sc->sm->sm_type != XFS_SCRUB_TYPE_RTRMAPBT &&
1010 (sr->rtlock_flags & XFS_RTGLOCK_RMAP) &&
1011 xfs_has_rtrmapbt(mp))
1012 sr->rmap_cur = xfs_rtrmapbt_init_cursor(sc->tp, sr->rtg);
1013
1014 if (sc->sm->sm_type != XFS_SCRUB_TYPE_RTREFCBT &&
1015 (sr->rtlock_flags & XFS_RTGLOCK_REFCOUNT) &&
1016 xfs_has_rtreflink(mp))
1017 sr->refc_cur = xfs_rtrefcountbt_init_cursor(sc->tp, sr->rtg);
1018 }
1019
1020 /*
1021 * Given a reference to a rtgroup structure, lock rtgroup btree inodes and
1022 * create btree cursors. Must only be called to repair a regular rt file.
1023 */
1024 int
xrep_rtgroup_init(struct xfs_scrub * sc,struct xfs_rtgroup * rtg,struct xchk_rt * sr,unsigned int rtglock_flags)1025 xrep_rtgroup_init(
1026 struct xfs_scrub *sc,
1027 struct xfs_rtgroup *rtg,
1028 struct xchk_rt *sr,
1029 unsigned int rtglock_flags)
1030 {
1031 ASSERT(sr->rtg == NULL);
1032
1033 xfs_rtgroup_lock(rtg, rtglock_flags);
1034 sr->rtlock_flags = rtglock_flags;
1035
1036 /* Grab our own passive reference from the caller's ref. */
1037 sr->rtg = xfs_rtgroup_hold(rtg);
1038 xrep_rtgroup_btcur_init(sc, sr);
1039 return 0;
1040 }
1041
1042 /* Ensure that all rt blocks in the given range are not marked free. */
1043 int
xrep_require_rtext_inuse(struct xfs_scrub * sc,xfs_rgblock_t rgbno,xfs_filblks_t len)1044 xrep_require_rtext_inuse(
1045 struct xfs_scrub *sc,
1046 xfs_rgblock_t rgbno,
1047 xfs_filblks_t len)
1048 {
1049 struct xfs_mount *mp = sc->mp;
1050 xfs_rtxnum_t startrtx;
1051 xfs_rtxnum_t endrtx;
1052 bool is_free = false;
1053 int error;
1054
1055 startrtx = xfs_rgbno_to_rtx(mp, rgbno);
1056 endrtx = xfs_rgbno_to_rtx(mp, rgbno + len - 1);
1057
1058 error = xfs_rtalloc_extent_is_free(sc->sr.rtg, sc->tp, startrtx,
1059 endrtx - startrtx + 1, &is_free);
1060 if (error)
1061 return error;
1062 if (is_free)
1063 return -EFSCORRUPTED;
1064
1065 return 0;
1066 }
1067 #endif /* CONFIG_XFS_RT */
1068
1069 /* Reinitialize the per-AG block reservation for the AG we just fixed. */
1070 int
xrep_reset_perag_resv(struct xfs_scrub * sc)1071 xrep_reset_perag_resv(
1072 struct xfs_scrub *sc)
1073 {
1074 int error;
1075
1076 if (!(sc->flags & XREP_RESET_PERAG_RESV))
1077 return 0;
1078
1079 ASSERT(sc->sa.pag != NULL);
1080 ASSERT(sc->ops->type == ST_PERAG);
1081 ASSERT(sc->tp);
1082
1083 sc->flags &= ~XREP_RESET_PERAG_RESV;
1084 xfs_ag_resv_free(sc->sa.pag);
1085 error = xfs_ag_resv_init(sc->sa.pag, sc->tp);
1086 if (error == -ENOSPC) {
1087 xfs_err(sc->mp,
1088 "Insufficient free space to reset per-AG reservation for AG %u after repair.",
1089 pag_agno(sc->sa.pag));
1090 error = 0;
1091 }
1092
1093 return error;
1094 }
1095
1096 /* Decide if we are going to call the repair function for a scrub type. */
1097 bool
xrep_will_attempt(struct xfs_scrub * sc)1098 xrep_will_attempt(
1099 struct xfs_scrub *sc)
1100 {
1101 /* Userspace asked us to rebuild the structure regardless. */
1102 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_FORCE_REBUILD)
1103 return true;
1104
1105 /* Let debug users force us into the repair routines. */
1106 if (XFS_TEST_ERROR(false, sc->mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR))
1107 return true;
1108
1109 /* Metadata is corrupt or failed cross-referencing. */
1110 if (xchk_needs_repair(sc->sm))
1111 return true;
1112
1113 return false;
1114 }
1115
1116 /* Try to fix some part of a metadata inode by calling another scrubber. */
1117 STATIC int
xrep_metadata_inode_subtype(struct xfs_scrub * sc,unsigned int scrub_type)1118 xrep_metadata_inode_subtype(
1119 struct xfs_scrub *sc,
1120 unsigned int scrub_type)
1121 {
1122 struct xfs_scrub_subord *sub;
1123 int error;
1124
1125 /*
1126 * Let's see if the inode needs repair. Use a subordinate scrub context
1127 * to call the scrub and repair functions so that we can hang on to the
1128 * resources that we already acquired instead of using the standard
1129 * setup/teardown routines.
1130 */
1131 sub = xchk_scrub_create_subord(sc, scrub_type);
1132 error = sub->sc.ops->scrub(&sub->sc);
1133 if (error)
1134 goto out;
1135 if (!xrep_will_attempt(&sub->sc))
1136 goto out;
1137
1138 /*
1139 * Repair some part of the inode. This will potentially join the inode
1140 * to the transaction.
1141 */
1142 error = sub->sc.ops->repair(&sub->sc);
1143 if (error)
1144 goto out;
1145
1146 /*
1147 * Finish all deferred intent items and then roll the transaction so
1148 * that the inode will not be joined to the transaction when we exit
1149 * the function.
1150 */
1151 error = xfs_defer_finish(&sub->sc.tp);
1152 if (error)
1153 goto out;
1154 error = xfs_trans_roll(&sub->sc.tp);
1155 if (error)
1156 goto out;
1157
1158 /*
1159 * Clear the corruption flags and re-check the metadata that we just
1160 * repaired.
1161 */
1162 sub->sc.sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
1163 error = sub->sc.ops->scrub(&sub->sc);
1164 if (error)
1165 goto out;
1166
1167 /* If corruption persists, the repair has failed. */
1168 if (xchk_needs_repair(sub->sc.sm)) {
1169 error = -EFSCORRUPTED;
1170 goto out;
1171 }
1172 out:
1173 xchk_scrub_free_subord(sub);
1174 return error;
1175 }
1176
1177 /*
1178 * Repair the ondisk forks of a metadata inode. The caller must ensure that
1179 * sc->ip points to the metadata inode and the ILOCK is held on that inode.
1180 * The inode must not be joined to the transaction before the call, and will
1181 * not be afterwards.
1182 */
1183 int
xrep_metadata_inode_forks(struct xfs_scrub * sc)1184 xrep_metadata_inode_forks(
1185 struct xfs_scrub *sc)
1186 {
1187 bool dirty = false;
1188 int error;
1189
1190 /* Repair the inode record and the data fork. */
1191 error = xrep_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_INODE);
1192 if (error)
1193 return error;
1194
1195 error = xrep_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTD);
1196 if (error)
1197 return error;
1198
1199 /*
1200 * Metadata files can only have extended attributes on metadir
1201 * filesystems, either for parent pointers or for actual xattr data.
1202 * For a non-metadir filesystem, make sure the attr fork looks ok
1203 * before we delete it.
1204 */
1205 if (xfs_inode_hasattr(sc->ip)) {
1206 error = xrep_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTA);
1207 if (error)
1208 return error;
1209 }
1210
1211 /* Clear the reflink flag since metadata never shares. */
1212 if (xfs_is_reflink_inode(sc->ip)) {
1213 dirty = true;
1214 xfs_trans_ijoin(sc->tp, sc->ip, 0);
1215 error = xfs_reflink_clear_inode_flag(sc->ip, &sc->tp);
1216 if (error)
1217 return error;
1218 }
1219
1220 /*
1221 * Metadata files on non-metadir filesystems cannot have attr forks,
1222 * so clear them now.
1223 */
1224 if (xfs_inode_hasattr(sc->ip) && !xfs_has_metadir(sc->mp)) {
1225 if (!dirty) {
1226 dirty = true;
1227 xfs_trans_ijoin(sc->tp, sc->ip, 0);
1228 }
1229 error = xrep_xattr_reset_fork(sc);
1230 if (error)
1231 return error;
1232 }
1233
1234 /*
1235 * If we modified the inode, roll the transaction but don't rejoin the
1236 * inode to the new transaction because xrep_bmap_data can do that.
1237 */
1238 if (dirty) {
1239 error = xfs_trans_roll(&sc->tp);
1240 if (error)
1241 return error;
1242 dirty = false;
1243 }
1244
1245 return 0;
1246 }
1247
1248 /*
1249 * Set up an in-memory buffer cache so that we can use the xfbtree. Allocating
1250 * a shmem file might take loks, so we cannot be in transaction context. Park
1251 * our resources in the scrub context and let the teardown function take care
1252 * of them at the right time.
1253 */
1254 int
xrep_setup_xfbtree(struct xfs_scrub * sc,const char * descr)1255 xrep_setup_xfbtree(
1256 struct xfs_scrub *sc,
1257 const char *descr)
1258 {
1259 ASSERT(sc->tp == NULL);
1260
1261 return xmbuf_alloc(sc->mp, descr, &sc->xmbtp);
1262 }
1263
1264 /*
1265 * Create a dummy transaction for use in a live update hook function. This
1266 * function MUST NOT be called from regular repair code because the current
1267 * process' transaction is saved via the cookie.
1268 */
1269 int
xrep_trans_alloc_hook_dummy(struct xfs_mount * mp,void ** cookiep,struct xfs_trans ** tpp)1270 xrep_trans_alloc_hook_dummy(
1271 struct xfs_mount *mp,
1272 void **cookiep,
1273 struct xfs_trans **tpp)
1274 {
1275 int error;
1276
1277 *cookiep = current->journal_info;
1278 current->journal_info = NULL;
1279
1280 error = xfs_trans_alloc_empty(mp, tpp);
1281 if (!error)
1282 return 0;
1283
1284 current->journal_info = *cookiep;
1285 *cookiep = NULL;
1286 return error;
1287 }
1288
1289 /* Cancel a dummy transaction used by a live update hook function. */
1290 void
xrep_trans_cancel_hook_dummy(void ** cookiep,struct xfs_trans * tp)1291 xrep_trans_cancel_hook_dummy(
1292 void **cookiep,
1293 struct xfs_trans *tp)
1294 {
1295 xfs_trans_cancel(tp);
1296 current->journal_info = *cookiep;
1297 *cookiep = NULL;
1298 }
1299
1300 /*
1301 * See if this buffer can pass the given ->verify_struct() function.
1302 *
1303 * If the buffer already has ops attached and they're not the ones that were
1304 * passed in, we reject the buffer. Otherwise, we perform the structure test
1305 * (note that we do not check CRCs) and return the outcome of the test. The
1306 * buffer ops and error state are left unchanged.
1307 */
1308 bool
xrep_buf_verify_struct(struct xfs_buf * bp,const struct xfs_buf_ops * ops)1309 xrep_buf_verify_struct(
1310 struct xfs_buf *bp,
1311 const struct xfs_buf_ops *ops)
1312 {
1313 const struct xfs_buf_ops *old_ops = bp->b_ops;
1314 xfs_failaddr_t fa;
1315 int old_error;
1316
1317 if (old_ops) {
1318 if (old_ops != ops)
1319 return false;
1320 }
1321
1322 old_error = bp->b_error;
1323 bp->b_ops = ops;
1324 fa = bp->b_ops->verify_struct(bp);
1325 bp->b_ops = old_ops;
1326 bp->b_error = old_error;
1327
1328 return fa == NULL;
1329 }
1330
1331 /* Check the sanity of a rmap record for a metadata btree inode. */
1332 int
xrep_check_ino_btree_mapping(struct xfs_scrub * sc,const struct xfs_rmap_irec * rec)1333 xrep_check_ino_btree_mapping(
1334 struct xfs_scrub *sc,
1335 const struct xfs_rmap_irec *rec)
1336 {
1337 enum xbtree_recpacking outcome;
1338 int error;
1339
1340 /*
1341 * Metadata btree inodes never have extended attributes, and all blocks
1342 * should have the bmbt block flag set.
1343 */
1344 if ((rec->rm_flags & XFS_RMAP_ATTR_FORK) ||
1345 !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK))
1346 return -EFSCORRUPTED;
1347
1348 /* Make sure the block is within the AG. */
1349 if (!xfs_verify_agbext(sc->sa.pag, rec->rm_startblock,
1350 rec->rm_blockcount))
1351 return -EFSCORRUPTED;
1352
1353 /* Make sure this isn't free space. */
1354 error = xfs_alloc_has_records(sc->sa.bno_cur, rec->rm_startblock,
1355 rec->rm_blockcount, &outcome);
1356 if (error)
1357 return error;
1358 if (outcome != XBTREE_RECPACKING_EMPTY)
1359 return -EFSCORRUPTED;
1360
1361 return 0;
1362 }
1363
1364 /*
1365 * Reset the block count of the inode being repaired, and adjust the dquot
1366 * block usage to match. The inode must not have an xattr fork.
1367 */
1368 void
xrep_inode_set_nblocks(struct xfs_scrub * sc,int64_t new_blocks)1369 xrep_inode_set_nblocks(
1370 struct xfs_scrub *sc,
1371 int64_t new_blocks)
1372 {
1373 int64_t delta =
1374 new_blocks - sc->ip->i_nblocks;
1375
1376 sc->ip->i_nblocks = new_blocks;
1377
1378 xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
1379 if (delta != 0)
1380 xfs_trans_mod_dquot_byino(sc->tp, sc->ip, XFS_TRANS_DQ_BCOUNT,
1381 delta);
1382 }
1383
1384 /* Reset the block reservation for a metadata inode. */
1385 int
xrep_reset_metafile_resv(struct xfs_scrub * sc)1386 xrep_reset_metafile_resv(
1387 struct xfs_scrub *sc)
1388 {
1389 struct xfs_inode *ip = sc->ip;
1390 int64_t delta;
1391 int error;
1392
1393 delta = ip->i_nblocks + ip->i_delayed_blks - ip->i_meta_resv_asked;
1394 if (delta == 0)
1395 return 0;
1396
1397 /*
1398 * Too many blocks have been reserved, transfer some from the incore
1399 * reservation back to the filesystem.
1400 */
1401 if (delta > 0) {
1402 int64_t give_back;
1403
1404 give_back = min_t(uint64_t, delta, ip->i_delayed_blks);
1405 if (give_back > 0) {
1406 xfs_mod_delalloc(ip, 0, -give_back);
1407 xfs_add_fdblocks(ip->i_mount, give_back);
1408 ip->i_delayed_blks -= give_back;
1409 }
1410
1411 return 0;
1412 }
1413
1414 /*
1415 * Not enough reservation; try to take some blocks from the filesystem
1416 * to the metadata inode. @delta is negative here, so invert the sign.
1417 */
1418 delta = -delta;
1419 error = xfs_dec_fdblocks(sc->mp, delta, true);
1420 while (error == -ENOSPC) {
1421 delta--;
1422 if (delta == 0) {
1423 xfs_warn(sc->mp,
1424 "Insufficient free space to reset space reservation for inode 0x%llx after repair.",
1425 ip->i_ino);
1426 return 0;
1427 }
1428 error = xfs_dec_fdblocks(sc->mp, delta, true);
1429 }
1430 if (error)
1431 return error;
1432
1433 xfs_mod_delalloc(ip, 0, delta);
1434 ip->i_delayed_blks += delta;
1435 return 0;
1436 }
1437