xref: /linux/fs/xfs/scrub/common.c (revision d6fd48ef)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_btree.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode.h"
16 #include "xfs_icache.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_refcount_btree.h"
22 #include "xfs_rmap.h"
23 #include "xfs_rmap_btree.h"
24 #include "xfs_log.h"
25 #include "xfs_trans_priv.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_attr.h"
29 #include "xfs_reflink.h"
30 #include "xfs_ag.h"
31 #include "scrub/scrub.h"
32 #include "scrub/common.h"
33 #include "scrub/trace.h"
34 #include "scrub/repair.h"
35 #include "scrub/health.h"
36 
37 /* Common code for the metadata scrubbers. */
38 
39 /*
40  * Handling operational errors.
41  *
42  * The *_process_error() family of functions are used to process error return
43  * codes from functions called as part of a scrub operation.
44  *
45  * If there's no error, we return true to tell the caller that it's ok
46  * to move on to the next check in its list.
47  *
48  * For non-verifier errors (e.g. ENOMEM) we return false to tell the
49  * caller that something bad happened, and we preserve *error so that
50  * the caller can return the *error up the stack to userspace.
51  *
52  * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
53  * OFLAG_CORRUPT in sm_flags and the *error is cleared.  In other words,
54  * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
55  * not via return codes.  We return false to tell the caller that
56  * something bad happened.  Since the error has been cleared, the caller
57  * will (presumably) return that zero and scrubbing will move on to
58  * whatever's next.
59  *
60  * ftrace can be used to record the precise metadata location and the
61  * approximate code location of the failed operation.
62  */
63 
64 /* Check for operational errors. */
65 static bool
66 __xchk_process_error(
67 	struct xfs_scrub	*sc,
68 	xfs_agnumber_t		agno,
69 	xfs_agblock_t		bno,
70 	int			*error,
71 	__u32			errflag,
72 	void			*ret_ip)
73 {
74 	switch (*error) {
75 	case 0:
76 		return true;
77 	case -EDEADLOCK:
78 		/* Used to restart an op with deadlock avoidance. */
79 		trace_xchk_deadlock_retry(
80 				sc->ip ? sc->ip : XFS_I(file_inode(sc->file)),
81 				sc->sm, *error);
82 		break;
83 	case -EFSBADCRC:
84 	case -EFSCORRUPTED:
85 		/* Note the badness but don't abort. */
86 		sc->sm->sm_flags |= errflag;
87 		*error = 0;
88 		fallthrough;
89 	default:
90 		trace_xchk_op_error(sc, agno, bno, *error,
91 				ret_ip);
92 		break;
93 	}
94 	return false;
95 }
96 
97 bool
98 xchk_process_error(
99 	struct xfs_scrub	*sc,
100 	xfs_agnumber_t		agno,
101 	xfs_agblock_t		bno,
102 	int			*error)
103 {
104 	return __xchk_process_error(sc, agno, bno, error,
105 			XFS_SCRUB_OFLAG_CORRUPT, __return_address);
106 }
107 
108 bool
109 xchk_xref_process_error(
110 	struct xfs_scrub	*sc,
111 	xfs_agnumber_t		agno,
112 	xfs_agblock_t		bno,
113 	int			*error)
114 {
115 	return __xchk_process_error(sc, agno, bno, error,
116 			XFS_SCRUB_OFLAG_XFAIL, __return_address);
117 }
118 
119 /* Check for operational errors for a file offset. */
120 static bool
121 __xchk_fblock_process_error(
122 	struct xfs_scrub	*sc,
123 	int			whichfork,
124 	xfs_fileoff_t		offset,
125 	int			*error,
126 	__u32			errflag,
127 	void			*ret_ip)
128 {
129 	switch (*error) {
130 	case 0:
131 		return true;
132 	case -EDEADLOCK:
133 		/* Used to restart an op with deadlock avoidance. */
134 		trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
135 		break;
136 	case -EFSBADCRC:
137 	case -EFSCORRUPTED:
138 		/* Note the badness but don't abort. */
139 		sc->sm->sm_flags |= errflag;
140 		*error = 0;
141 		fallthrough;
142 	default:
143 		trace_xchk_file_op_error(sc, whichfork, offset, *error,
144 				ret_ip);
145 		break;
146 	}
147 	return false;
148 }
149 
150 bool
151 xchk_fblock_process_error(
152 	struct xfs_scrub	*sc,
153 	int			whichfork,
154 	xfs_fileoff_t		offset,
155 	int			*error)
156 {
157 	return __xchk_fblock_process_error(sc, whichfork, offset, error,
158 			XFS_SCRUB_OFLAG_CORRUPT, __return_address);
159 }
160 
161 bool
162 xchk_fblock_xref_process_error(
163 	struct xfs_scrub	*sc,
164 	int			whichfork,
165 	xfs_fileoff_t		offset,
166 	int			*error)
167 {
168 	return __xchk_fblock_process_error(sc, whichfork, offset, error,
169 			XFS_SCRUB_OFLAG_XFAIL, __return_address);
170 }
171 
172 /*
173  * Handling scrub corruption/optimization/warning checks.
174  *
175  * The *_set_{corrupt,preen,warning}() family of functions are used to
176  * record the presence of metadata that is incorrect (corrupt), could be
177  * optimized somehow (preen), or should be flagged for administrative
178  * review but is not incorrect (warn).
179  *
180  * ftrace can be used to record the precise metadata location and
181  * approximate code location of the failed check.
182  */
183 
184 /* Record a block which could be optimized. */
185 void
186 xchk_block_set_preen(
187 	struct xfs_scrub	*sc,
188 	struct xfs_buf		*bp)
189 {
190 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
191 	trace_xchk_block_preen(sc, xfs_buf_daddr(bp), __return_address);
192 }
193 
194 /*
195  * Record an inode which could be optimized.  The trace data will
196  * include the block given by bp if bp is given; otherwise it will use
197  * the block location of the inode record itself.
198  */
199 void
200 xchk_ino_set_preen(
201 	struct xfs_scrub	*sc,
202 	xfs_ino_t		ino)
203 {
204 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
205 	trace_xchk_ino_preen(sc, ino, __return_address);
206 }
207 
208 /* Record something being wrong with the filesystem primary superblock. */
209 void
210 xchk_set_corrupt(
211 	struct xfs_scrub	*sc)
212 {
213 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
214 	trace_xchk_fs_error(sc, 0, __return_address);
215 }
216 
217 /* Record a corrupt block. */
218 void
219 xchk_block_set_corrupt(
220 	struct xfs_scrub	*sc,
221 	struct xfs_buf		*bp)
222 {
223 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
224 	trace_xchk_block_error(sc, xfs_buf_daddr(bp), __return_address);
225 }
226 
227 /* Record a corruption while cross-referencing. */
228 void
229 xchk_block_xref_set_corrupt(
230 	struct xfs_scrub	*sc,
231 	struct xfs_buf		*bp)
232 {
233 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
234 	trace_xchk_block_error(sc, xfs_buf_daddr(bp), __return_address);
235 }
236 
237 /*
238  * Record a corrupt inode.  The trace data will include the block given
239  * by bp if bp is given; otherwise it will use the block location of the
240  * inode record itself.
241  */
242 void
243 xchk_ino_set_corrupt(
244 	struct xfs_scrub	*sc,
245 	xfs_ino_t		ino)
246 {
247 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
248 	trace_xchk_ino_error(sc, ino, __return_address);
249 }
250 
251 /* Record a corruption while cross-referencing with an inode. */
252 void
253 xchk_ino_xref_set_corrupt(
254 	struct xfs_scrub	*sc,
255 	xfs_ino_t		ino)
256 {
257 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
258 	trace_xchk_ino_error(sc, ino, __return_address);
259 }
260 
261 /* Record corruption in a block indexed by a file fork. */
262 void
263 xchk_fblock_set_corrupt(
264 	struct xfs_scrub	*sc,
265 	int			whichfork,
266 	xfs_fileoff_t		offset)
267 {
268 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
269 	trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
270 }
271 
272 /* Record a corruption while cross-referencing a fork block. */
273 void
274 xchk_fblock_xref_set_corrupt(
275 	struct xfs_scrub	*sc,
276 	int			whichfork,
277 	xfs_fileoff_t		offset)
278 {
279 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
280 	trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
281 }
282 
283 /*
284  * Warn about inodes that need administrative review but is not
285  * incorrect.
286  */
287 void
288 xchk_ino_set_warning(
289 	struct xfs_scrub	*sc,
290 	xfs_ino_t		ino)
291 {
292 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
293 	trace_xchk_ino_warning(sc, ino, __return_address);
294 }
295 
296 /* Warn about a block indexed by a file fork that needs review. */
297 void
298 xchk_fblock_set_warning(
299 	struct xfs_scrub	*sc,
300 	int			whichfork,
301 	xfs_fileoff_t		offset)
302 {
303 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
304 	trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
305 }
306 
307 /* Signal an incomplete scrub. */
308 void
309 xchk_set_incomplete(
310 	struct xfs_scrub	*sc)
311 {
312 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
313 	trace_xchk_incomplete(sc, __return_address);
314 }
315 
316 /*
317  * rmap scrubbing -- compute the number of blocks with a given owner,
318  * at least according to the reverse mapping data.
319  */
320 
321 struct xchk_rmap_ownedby_info {
322 	const struct xfs_owner_info	*oinfo;
323 	xfs_filblks_t			*blocks;
324 };
325 
326 STATIC int
327 xchk_count_rmap_ownedby_irec(
328 	struct xfs_btree_cur		*cur,
329 	const struct xfs_rmap_irec	*rec,
330 	void				*priv)
331 {
332 	struct xchk_rmap_ownedby_info	*sroi = priv;
333 	bool				irec_attr;
334 	bool				oinfo_attr;
335 
336 	irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
337 	oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
338 
339 	if (rec->rm_owner != sroi->oinfo->oi_owner)
340 		return 0;
341 
342 	if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
343 		(*sroi->blocks) += rec->rm_blockcount;
344 
345 	return 0;
346 }
347 
348 /*
349  * Calculate the number of blocks the rmap thinks are owned by something.
350  * The caller should pass us an rmapbt cursor.
351  */
352 int
353 xchk_count_rmap_ownedby_ag(
354 	struct xfs_scrub		*sc,
355 	struct xfs_btree_cur		*cur,
356 	const struct xfs_owner_info	*oinfo,
357 	xfs_filblks_t			*blocks)
358 {
359 	struct xchk_rmap_ownedby_info	sroi = {
360 		.oinfo			= oinfo,
361 		.blocks			= blocks,
362 	};
363 
364 	*blocks = 0;
365 	return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
366 			&sroi);
367 }
368 
369 /*
370  * AG scrubbing
371  *
372  * These helpers facilitate locking an allocation group's header
373  * buffers, setting up cursors for all btrees that are present, and
374  * cleaning everything up once we're through.
375  */
376 
377 /* Decide if we want to return an AG header read failure. */
378 static inline bool
379 want_ag_read_header_failure(
380 	struct xfs_scrub	*sc,
381 	unsigned int		type)
382 {
383 	/* Return all AG header read failures when scanning btrees. */
384 	if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
385 	    sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
386 	    sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
387 		return true;
388 	/*
389 	 * If we're scanning a given type of AG header, we only want to
390 	 * see read failures from that specific header.  We'd like the
391 	 * other headers to cross-check them, but this isn't required.
392 	 */
393 	if (sc->sm->sm_type == type)
394 		return true;
395 	return false;
396 }
397 
398 /*
399  * Grab the perag structure and all the headers for an AG.
400  *
401  * The headers should be released by xchk_ag_free, but as a fail safe we attach
402  * all the buffers we grab to the scrub transaction so they'll all be freed
403  * when we cancel it.  Returns ENOENT if we can't grab the perag structure.
404  */
405 int
406 xchk_ag_read_headers(
407 	struct xfs_scrub	*sc,
408 	xfs_agnumber_t		agno,
409 	struct xchk_ag		*sa)
410 {
411 	struct xfs_mount	*mp = sc->mp;
412 	int			error;
413 
414 	ASSERT(!sa->pag);
415 	sa->pag = xfs_perag_get(mp, agno);
416 	if (!sa->pag)
417 		return -ENOENT;
418 
419 	error = xfs_ialloc_read_agi(sa->pag, sc->tp, &sa->agi_bp);
420 	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
421 		return error;
422 
423 	error = xfs_alloc_read_agf(sa->pag, sc->tp, 0, &sa->agf_bp);
424 	if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
425 		return error;
426 
427 	return 0;
428 }
429 
430 /* Release all the AG btree cursors. */
431 void
432 xchk_ag_btcur_free(
433 	struct xchk_ag		*sa)
434 {
435 	if (sa->refc_cur)
436 		xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
437 	if (sa->rmap_cur)
438 		xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
439 	if (sa->fino_cur)
440 		xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
441 	if (sa->ino_cur)
442 		xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
443 	if (sa->cnt_cur)
444 		xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
445 	if (sa->bno_cur)
446 		xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
447 
448 	sa->refc_cur = NULL;
449 	sa->rmap_cur = NULL;
450 	sa->fino_cur = NULL;
451 	sa->ino_cur = NULL;
452 	sa->bno_cur = NULL;
453 	sa->cnt_cur = NULL;
454 }
455 
456 /* Initialize all the btree cursors for an AG. */
457 void
458 xchk_ag_btcur_init(
459 	struct xfs_scrub	*sc,
460 	struct xchk_ag		*sa)
461 {
462 	struct xfs_mount	*mp = sc->mp;
463 
464 	if (sa->agf_bp &&
465 	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_BNO)) {
466 		/* Set up a bnobt cursor for cross-referencing. */
467 		sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
468 				sa->pag, XFS_BTNUM_BNO);
469 	}
470 
471 	if (sa->agf_bp &&
472 	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_CNT)) {
473 		/* Set up a cntbt cursor for cross-referencing. */
474 		sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
475 				sa->pag, XFS_BTNUM_CNT);
476 	}
477 
478 	/* Set up a inobt cursor for cross-referencing. */
479 	if (sa->agi_bp &&
480 	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_INO)) {
481 		sa->ino_cur = xfs_inobt_init_cursor(sa->pag, sc->tp, sa->agi_bp,
482 				XFS_BTNUM_INO);
483 	}
484 
485 	/* Set up a finobt cursor for cross-referencing. */
486 	if (sa->agi_bp && xfs_has_finobt(mp) &&
487 	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_FINO)) {
488 		sa->fino_cur = xfs_inobt_init_cursor(sa->pag, sc->tp, sa->agi_bp,
489 				XFS_BTNUM_FINO);
490 	}
491 
492 	/* Set up a rmapbt cursor for cross-referencing. */
493 	if (sa->agf_bp && xfs_has_rmapbt(mp) &&
494 	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_RMAP)) {
495 		sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
496 				sa->pag);
497 	}
498 
499 	/* Set up a refcountbt cursor for cross-referencing. */
500 	if (sa->agf_bp && xfs_has_reflink(mp) &&
501 	    xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_REFC)) {
502 		sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
503 				sa->agf_bp, sa->pag);
504 	}
505 }
506 
507 /* Release the AG header context and btree cursors. */
508 void
509 xchk_ag_free(
510 	struct xfs_scrub	*sc,
511 	struct xchk_ag		*sa)
512 {
513 	xchk_ag_btcur_free(sa);
514 	if (sa->agf_bp) {
515 		xfs_trans_brelse(sc->tp, sa->agf_bp);
516 		sa->agf_bp = NULL;
517 	}
518 	if (sa->agi_bp) {
519 		xfs_trans_brelse(sc->tp, sa->agi_bp);
520 		sa->agi_bp = NULL;
521 	}
522 	if (sa->pag) {
523 		xfs_perag_put(sa->pag);
524 		sa->pag = NULL;
525 	}
526 }
527 
528 /*
529  * For scrub, grab the perag structure, the AGI, and the AGF headers, in that
530  * order.  Locking order requires us to get the AGI before the AGF.  We use the
531  * transaction to avoid deadlocking on crosslinked metadata buffers; either the
532  * caller passes one in (bmap scrub) or we have to create a transaction
533  * ourselves.  Returns ENOENT if the perag struct cannot be grabbed.
534  */
535 int
536 xchk_ag_init(
537 	struct xfs_scrub	*sc,
538 	xfs_agnumber_t		agno,
539 	struct xchk_ag		*sa)
540 {
541 	int			error;
542 
543 	error = xchk_ag_read_headers(sc, agno, sa);
544 	if (error)
545 		return error;
546 
547 	xchk_ag_btcur_init(sc, sa);
548 	return 0;
549 }
550 
551 /* Per-scrubber setup functions */
552 
553 /*
554  * Grab an empty transaction so that we can re-grab locked buffers if
555  * one of our btrees turns out to be cyclic.
556  *
557  * If we're going to repair something, we need to ask for the largest possible
558  * log reservation so that we can handle the worst case scenario for metadata
559  * updates while rebuilding a metadata item.  We also need to reserve as many
560  * blocks in the head transaction as we think we're going to need to rebuild
561  * the metadata object.
562  */
563 int
564 xchk_trans_alloc(
565 	struct xfs_scrub	*sc,
566 	uint			resblks)
567 {
568 	if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
569 		return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
570 				resblks, 0, 0, &sc->tp);
571 
572 	return xfs_trans_alloc_empty(sc->mp, &sc->tp);
573 }
574 
575 /* Set us up with a transaction and an empty context. */
576 int
577 xchk_setup_fs(
578 	struct xfs_scrub	*sc)
579 {
580 	uint			resblks;
581 
582 	resblks = xrep_calc_ag_resblks(sc);
583 	return xchk_trans_alloc(sc, resblks);
584 }
585 
586 /* Set us up with AG headers and btree cursors. */
587 int
588 xchk_setup_ag_btree(
589 	struct xfs_scrub	*sc,
590 	bool			force_log)
591 {
592 	struct xfs_mount	*mp = sc->mp;
593 	int			error;
594 
595 	/*
596 	 * If the caller asks us to checkpont the log, do so.  This
597 	 * expensive operation should be performed infrequently and only
598 	 * as a last resort.  Any caller that sets force_log should
599 	 * document why they need to do so.
600 	 */
601 	if (force_log) {
602 		error = xchk_checkpoint_log(mp);
603 		if (error)
604 			return error;
605 	}
606 
607 	error = xchk_setup_fs(sc);
608 	if (error)
609 		return error;
610 
611 	return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
612 }
613 
614 /* Push everything out of the log onto disk. */
615 int
616 xchk_checkpoint_log(
617 	struct xfs_mount	*mp)
618 {
619 	int			error;
620 
621 	error = xfs_log_force(mp, XFS_LOG_SYNC);
622 	if (error)
623 		return error;
624 	xfs_ail_push_all_sync(mp->m_ail);
625 	return 0;
626 }
627 
628 /*
629  * Given an inode and the scrub control structure, grab either the
630  * inode referenced in the control structure or the inode passed in.
631  * The inode is not locked.
632  */
633 int
634 xchk_get_inode(
635 	struct xfs_scrub	*sc)
636 {
637 	struct xfs_imap		imap;
638 	struct xfs_mount	*mp = sc->mp;
639 	struct xfs_perag	*pag;
640 	struct xfs_inode	*ip_in = XFS_I(file_inode(sc->file));
641 	struct xfs_inode	*ip = NULL;
642 	int			error;
643 
644 	/* We want to scan the inode we already had opened. */
645 	if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
646 		sc->ip = ip_in;
647 		return 0;
648 	}
649 
650 	/* Look up the inode, see if the generation number matches. */
651 	if (xfs_internal_inum(mp, sc->sm->sm_ino))
652 		return -ENOENT;
653 	error = xfs_iget(mp, NULL, sc->sm->sm_ino,
654 			XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
655 	switch (error) {
656 	case -ENOENT:
657 		/* Inode doesn't exist, just bail out. */
658 		return error;
659 	case 0:
660 		/* Got an inode, continue. */
661 		break;
662 	case -EINVAL:
663 		/*
664 		 * -EINVAL with IGET_UNTRUSTED could mean one of several
665 		 * things: userspace gave us an inode number that doesn't
666 		 * correspond to fs space, or doesn't have an inobt entry;
667 		 * or it could simply mean that the inode buffer failed the
668 		 * read verifiers.
669 		 *
670 		 * Try just the inode mapping lookup -- if it succeeds, then
671 		 * the inode buffer verifier failed and something needs fixing.
672 		 * Otherwise, we really couldn't find it so tell userspace
673 		 * that it no longer exists.
674 		 */
675 		pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sc->sm->sm_ino));
676 		if (pag) {
677 			error = xfs_imap(pag, sc->tp, sc->sm->sm_ino, &imap,
678 					XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
679 			xfs_perag_put(pag);
680 			if (error)
681 				return -ENOENT;
682 		}
683 		error = -EFSCORRUPTED;
684 		fallthrough;
685 	default:
686 		trace_xchk_op_error(sc,
687 				XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
688 				XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
689 				error, __return_address);
690 		return error;
691 	}
692 	if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
693 		xfs_irele(ip);
694 		return -ENOENT;
695 	}
696 
697 	sc->ip = ip;
698 	return 0;
699 }
700 
701 /* Set us up to scrub a file's contents. */
702 int
703 xchk_setup_inode_contents(
704 	struct xfs_scrub	*sc,
705 	unsigned int		resblks)
706 {
707 	int			error;
708 
709 	error = xchk_get_inode(sc);
710 	if (error)
711 		return error;
712 
713 	/* Got the inode, lock it and we're ready to go. */
714 	sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
715 	xfs_ilock(sc->ip, sc->ilock_flags);
716 	error = xchk_trans_alloc(sc, resblks);
717 	if (error)
718 		goto out;
719 	sc->ilock_flags |= XFS_ILOCK_EXCL;
720 	xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
721 
722 out:
723 	/* scrub teardown will unlock and release the inode for us */
724 	return error;
725 }
726 
727 /*
728  * Predicate that decides if we need to evaluate the cross-reference check.
729  * If there was an error accessing the cross-reference btree, just delete
730  * the cursor and skip the check.
731  */
732 bool
733 xchk_should_check_xref(
734 	struct xfs_scrub	*sc,
735 	int			*error,
736 	struct xfs_btree_cur	**curpp)
737 {
738 	/* No point in xref if we already know we're corrupt. */
739 	if (xchk_skip_xref(sc->sm))
740 		return false;
741 
742 	if (*error == 0)
743 		return true;
744 
745 	if (curpp) {
746 		/* If we've already given up on xref, just bail out. */
747 		if (!*curpp)
748 			return false;
749 
750 		/* xref error, delete cursor and bail out. */
751 		xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
752 		*curpp = NULL;
753 	}
754 
755 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
756 	trace_xchk_xref_error(sc, *error, __return_address);
757 
758 	/*
759 	 * Errors encountered during cross-referencing with another
760 	 * data structure should not cause this scrubber to abort.
761 	 */
762 	*error = 0;
763 	return false;
764 }
765 
766 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
767 void
768 xchk_buffer_recheck(
769 	struct xfs_scrub	*sc,
770 	struct xfs_buf		*bp)
771 {
772 	xfs_failaddr_t		fa;
773 
774 	if (bp->b_ops == NULL) {
775 		xchk_block_set_corrupt(sc, bp);
776 		return;
777 	}
778 	if (bp->b_ops->verify_struct == NULL) {
779 		xchk_set_incomplete(sc);
780 		return;
781 	}
782 	fa = bp->b_ops->verify_struct(bp);
783 	if (!fa)
784 		return;
785 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
786 	trace_xchk_block_error(sc, xfs_buf_daddr(bp), fa);
787 }
788 
789 static inline int
790 xchk_metadata_inode_subtype(
791 	struct xfs_scrub	*sc,
792 	unsigned int		scrub_type)
793 {
794 	__u32			smtype = sc->sm->sm_type;
795 	int			error;
796 
797 	sc->sm->sm_type = scrub_type;
798 
799 	switch (scrub_type) {
800 	case XFS_SCRUB_TYPE_INODE:
801 		error = xchk_inode(sc);
802 		break;
803 	case XFS_SCRUB_TYPE_BMBTD:
804 		error = xchk_bmap_data(sc);
805 		break;
806 	default:
807 		ASSERT(0);
808 		error = -EFSCORRUPTED;
809 		break;
810 	}
811 
812 	sc->sm->sm_type = smtype;
813 	return error;
814 }
815 
816 /*
817  * Scrub the attr/data forks of a metadata inode.  The metadata inode must be
818  * pointed to by sc->ip and the ILOCK must be held.
819  */
820 int
821 xchk_metadata_inode_forks(
822 	struct xfs_scrub	*sc)
823 {
824 	bool			shared;
825 	int			error;
826 
827 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
828 		return 0;
829 
830 	/* Check the inode record. */
831 	error = xchk_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_INODE);
832 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
833 		return error;
834 
835 	/* Metadata inodes don't live on the rt device. */
836 	if (sc->ip->i_diflags & XFS_DIFLAG_REALTIME) {
837 		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
838 		return 0;
839 	}
840 
841 	/* They should never participate in reflink. */
842 	if (xfs_is_reflink_inode(sc->ip)) {
843 		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
844 		return 0;
845 	}
846 
847 	/* They also should never have extended attributes. */
848 	if (xfs_inode_hasattr(sc->ip)) {
849 		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
850 		return 0;
851 	}
852 
853 	/* Invoke the data fork scrubber. */
854 	error = xchk_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTD);
855 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
856 		return error;
857 
858 	/* Look for incorrect shared blocks. */
859 	if (xfs_has_reflink(sc->mp)) {
860 		error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
861 				&shared);
862 		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
863 				&error))
864 			return error;
865 		if (shared)
866 			xchk_ino_set_corrupt(sc, sc->ip->i_ino);
867 	}
868 
869 	return 0;
870 }
871 
872 /*
873  * Try to lock an inode in violation of the usual locking order rules.  For
874  * example, trying to get the IOLOCK while in transaction context, or just
875  * plain breaking AG-order or inode-order inode locking rules.  Either way,
876  * the only way to avoid an ABBA deadlock is to use trylock and back off if
877  * we can't.
878  */
879 int
880 xchk_ilock_inverted(
881 	struct xfs_inode	*ip,
882 	uint			lock_mode)
883 {
884 	int			i;
885 
886 	for (i = 0; i < 20; i++) {
887 		if (xfs_ilock_nowait(ip, lock_mode))
888 			return 0;
889 		delay(1);
890 	}
891 	return -EDEADLOCK;
892 }
893 
894 /* Pause background reaping of resources. */
895 void
896 xchk_stop_reaping(
897 	struct xfs_scrub	*sc)
898 {
899 	sc->flags |= XCHK_REAPING_DISABLED;
900 	xfs_blockgc_stop(sc->mp);
901 	xfs_inodegc_stop(sc->mp);
902 }
903 
904 /* Restart background reaping of resources. */
905 void
906 xchk_start_reaping(
907 	struct xfs_scrub	*sc)
908 {
909 	/*
910 	 * Readonly filesystems do not perform inactivation or speculative
911 	 * preallocation, so there's no need to restart the workers.
912 	 */
913 	if (!xfs_is_readonly(sc->mp)) {
914 		xfs_inodegc_start(sc->mp);
915 		xfs_blockgc_start(sc->mp);
916 	}
917 	sc->flags &= ~XCHK_REAPING_DISABLED;
918 }
919