xref: /linux/fs/xfs/scrub/common.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
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(mp, sc->tp, sa->agi_bp,
482 				sa->pag, 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(mp, sc->tp, sa->agi_bp,
489 				sa->pag, 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_inode	*ip_in = XFS_I(file_inode(sc->file));
640 	struct xfs_inode	*ip = NULL;
641 	int			error;
642 
643 	/* We want to scan the inode we already had opened. */
644 	if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
645 		sc->ip = ip_in;
646 		return 0;
647 	}
648 
649 	/* Look up the inode, see if the generation number matches. */
650 	if (xfs_internal_inum(mp, sc->sm->sm_ino))
651 		return -ENOENT;
652 	error = xfs_iget(mp, NULL, sc->sm->sm_ino,
653 			XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
654 	switch (error) {
655 	case -ENOENT:
656 		/* Inode doesn't exist, just bail out. */
657 		return error;
658 	case 0:
659 		/* Got an inode, continue. */
660 		break;
661 	case -EINVAL:
662 		/*
663 		 * -EINVAL with IGET_UNTRUSTED could mean one of several
664 		 * things: userspace gave us an inode number that doesn't
665 		 * correspond to fs space, or doesn't have an inobt entry;
666 		 * or it could simply mean that the inode buffer failed the
667 		 * read verifiers.
668 		 *
669 		 * Try just the inode mapping lookup -- if it succeeds, then
670 		 * the inode buffer verifier failed and something needs fixing.
671 		 * Otherwise, we really couldn't find it so tell userspace
672 		 * that it no longer exists.
673 		 */
674 		error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
675 				XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
676 		if (error)
677 			return -ENOENT;
678 		error = -EFSCORRUPTED;
679 		fallthrough;
680 	default:
681 		trace_xchk_op_error(sc,
682 				XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
683 				XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
684 				error, __return_address);
685 		return error;
686 	}
687 	if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
688 		xfs_irele(ip);
689 		return -ENOENT;
690 	}
691 
692 	sc->ip = ip;
693 	return 0;
694 }
695 
696 /* Set us up to scrub a file's contents. */
697 int
698 xchk_setup_inode_contents(
699 	struct xfs_scrub	*sc,
700 	unsigned int		resblks)
701 {
702 	int			error;
703 
704 	error = xchk_get_inode(sc);
705 	if (error)
706 		return error;
707 
708 	/* Got the inode, lock it and we're ready to go. */
709 	sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
710 	xfs_ilock(sc->ip, sc->ilock_flags);
711 	error = xchk_trans_alloc(sc, resblks);
712 	if (error)
713 		goto out;
714 	sc->ilock_flags |= XFS_ILOCK_EXCL;
715 	xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
716 
717 out:
718 	/* scrub teardown will unlock and release the inode for us */
719 	return error;
720 }
721 
722 /*
723  * Predicate that decides if we need to evaluate the cross-reference check.
724  * If there was an error accessing the cross-reference btree, just delete
725  * the cursor and skip the check.
726  */
727 bool
728 xchk_should_check_xref(
729 	struct xfs_scrub	*sc,
730 	int			*error,
731 	struct xfs_btree_cur	**curpp)
732 {
733 	/* No point in xref if we already know we're corrupt. */
734 	if (xchk_skip_xref(sc->sm))
735 		return false;
736 
737 	if (*error == 0)
738 		return true;
739 
740 	if (curpp) {
741 		/* If we've already given up on xref, just bail out. */
742 		if (!*curpp)
743 			return false;
744 
745 		/* xref error, delete cursor and bail out. */
746 		xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
747 		*curpp = NULL;
748 	}
749 
750 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
751 	trace_xchk_xref_error(sc, *error, __return_address);
752 
753 	/*
754 	 * Errors encountered during cross-referencing with another
755 	 * data structure should not cause this scrubber to abort.
756 	 */
757 	*error = 0;
758 	return false;
759 }
760 
761 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
762 void
763 xchk_buffer_recheck(
764 	struct xfs_scrub	*sc,
765 	struct xfs_buf		*bp)
766 {
767 	xfs_failaddr_t		fa;
768 
769 	if (bp->b_ops == NULL) {
770 		xchk_block_set_corrupt(sc, bp);
771 		return;
772 	}
773 	if (bp->b_ops->verify_struct == NULL) {
774 		xchk_set_incomplete(sc);
775 		return;
776 	}
777 	fa = bp->b_ops->verify_struct(bp);
778 	if (!fa)
779 		return;
780 	sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
781 	trace_xchk_block_error(sc, xfs_buf_daddr(bp), fa);
782 }
783 
784 static inline int
785 xchk_metadata_inode_subtype(
786 	struct xfs_scrub	*sc,
787 	unsigned int		scrub_type)
788 {
789 	__u32			smtype = sc->sm->sm_type;
790 	int			error;
791 
792 	sc->sm->sm_type = scrub_type;
793 
794 	switch (scrub_type) {
795 	case XFS_SCRUB_TYPE_INODE:
796 		error = xchk_inode(sc);
797 		break;
798 	case XFS_SCRUB_TYPE_BMBTD:
799 		error = xchk_bmap_data(sc);
800 		break;
801 	default:
802 		ASSERT(0);
803 		error = -EFSCORRUPTED;
804 		break;
805 	}
806 
807 	sc->sm->sm_type = smtype;
808 	return error;
809 }
810 
811 /*
812  * Scrub the attr/data forks of a metadata inode.  The metadata inode must be
813  * pointed to by sc->ip and the ILOCK must be held.
814  */
815 int
816 xchk_metadata_inode_forks(
817 	struct xfs_scrub	*sc)
818 {
819 	bool			shared;
820 	int			error;
821 
822 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
823 		return 0;
824 
825 	/* Check the inode record. */
826 	error = xchk_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_INODE);
827 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
828 		return error;
829 
830 	/* Metadata inodes don't live on the rt device. */
831 	if (sc->ip->i_diflags & XFS_DIFLAG_REALTIME) {
832 		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
833 		return 0;
834 	}
835 
836 	/* They should never participate in reflink. */
837 	if (xfs_is_reflink_inode(sc->ip)) {
838 		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
839 		return 0;
840 	}
841 
842 	/* They also should never have extended attributes. */
843 	if (xfs_inode_hasattr(sc->ip)) {
844 		xchk_ino_set_corrupt(sc, sc->ip->i_ino);
845 		return 0;
846 	}
847 
848 	/* Invoke the data fork scrubber. */
849 	error = xchk_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTD);
850 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
851 		return error;
852 
853 	/* Look for incorrect shared blocks. */
854 	if (xfs_has_reflink(sc->mp)) {
855 		error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
856 				&shared);
857 		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
858 				&error))
859 			return error;
860 		if (shared)
861 			xchk_ino_set_corrupt(sc, sc->ip->i_ino);
862 	}
863 
864 	return 0;
865 }
866 
867 /*
868  * Try to lock an inode in violation of the usual locking order rules.  For
869  * example, trying to get the IOLOCK while in transaction context, or just
870  * plain breaking AG-order or inode-order inode locking rules.  Either way,
871  * the only way to avoid an ABBA deadlock is to use trylock and back off if
872  * we can't.
873  */
874 int
875 xchk_ilock_inverted(
876 	struct xfs_inode	*ip,
877 	uint			lock_mode)
878 {
879 	int			i;
880 
881 	for (i = 0; i < 20; i++) {
882 		if (xfs_ilock_nowait(ip, lock_mode))
883 			return 0;
884 		delay(1);
885 	}
886 	return -EDEADLOCK;
887 }
888 
889 /* Pause background reaping of resources. */
890 void
891 xchk_stop_reaping(
892 	struct xfs_scrub	*sc)
893 {
894 	sc->flags |= XCHK_REAPING_DISABLED;
895 	xfs_blockgc_stop(sc->mp);
896 	xfs_inodegc_stop(sc->mp);
897 }
898 
899 /* Restart background reaping of resources. */
900 void
901 xchk_start_reaping(
902 	struct xfs_scrub	*sc)
903 {
904 	/*
905 	 * Readonly filesystems do not perform inactivation or speculative
906 	 * preallocation, so there's no need to restart the workers.
907 	 */
908 	if (!xfs_is_readonly(sc->mp)) {
909 		xfs_inodegc_start(sc->mp);
910 		xfs_blockgc_start(sc->mp);
911 	}
912 	sc->flags &= ~XCHK_REAPING_DISABLED;
913 }
914