1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
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"
16 #include "xfs_inode.h"
17 #include "xfs_ialloc.h"
18 #include "xfs_icache.h"
19 #include "xfs_da_format.h"
20 #include "xfs_reflink.h"
22 #include "xfs_bmap_util.h"
23 #include "xfs_rtbitmap.h"
24 #include "scrub/scrub.h"
25 #include "scrub/common.h"
26 #include "scrub/btree.h"
27 #include "scrub/trace.h"
28 #include "scrub/repair.h"
30 /* Prepare the attached inode for scrubbing. */
37 xchk_ilock(sc, XFS_IOLOCK_EXCL);
39 error = xchk_trans_alloc(sc, 0);
43 error = xchk_ino_dqattach(sc);
47 xchk_ilock(sc, XFS_ILOCK_EXCL);
51 /* Install this scrub-by-handle inode and prepare it for scrubbing. */
53 xchk_install_handle_iscrub(
59 error = xchk_install_handle_inode(sc, ip);
63 return xchk_prepare_iscrub(sc);
67 * Grab total control of the inode metadata. In the best case, we grab the
68 * incore inode and take all locks on it. If the incore inode cannot be
69 * constructed due to corruption problems, lock the AGI so that we can single
70 * step the loading process to fix everything that can go wrong.
78 struct xfs_mount *mp = sc->mp;
79 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file));
80 struct xfs_buf *agi_bp;
81 struct xfs_perag *pag;
82 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, sc->sm->sm_ino);
85 if (xchk_need_intent_drain(sc))
86 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
88 /* We want to scan the opened inode, so lock it and exit. */
89 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
90 error = xchk_install_live_inode(sc, ip_in);
94 return xchk_prepare_iscrub(sc);
97 /* Reject internal metadata files and obviously bad inode numbers. */
98 if (xfs_internal_inum(mp, sc->sm->sm_ino))
100 if (!xfs_verify_ino(sc->mp, sc->sm->sm_ino))
103 /* Try a safe untrusted iget. */
104 error = xchk_iget_safe(sc, sc->sm->sm_ino, &ip);
106 return xchk_install_handle_iscrub(sc, ip);
107 if (error == -ENOENT)
109 if (error != -EFSCORRUPTED && error != -EFSBADCRC && error != -EINVAL)
113 * EINVAL with IGET_UNTRUSTED probably means one of several things:
114 * userspace gave us an inode number that doesn't correspond to fs
115 * space; the inode btree lacks a record for this inode; or there is
116 * a record, and it says this inode is free.
118 * EFSCORRUPTED/EFSBADCRC could mean that the inode was mappable, but
119 * some other metadata corruption (e.g. inode forks) prevented
120 * instantiation of the incore inode. Or it could mean the inobt is
123 * We want to look up this inode in the inobt directly to distinguish
124 * three different scenarios: (1) the inobt says the inode is free,
125 * in which case there's nothing to do; (2) the inobt is corrupt so we
126 * should flag the corruption and exit to userspace to let it fix the
127 * inobt; and (3) the inobt says the inode is allocated, but loading it
128 * failed due to corruption.
130 * Allocate a transaction and grab the AGI to prevent inobt activity in
131 * this AG. Retry the iget in case someone allocated a new inode after
132 * the first iget failed.
134 error = xchk_trans_alloc(sc, 0);
138 error = xchk_iget_agi(sc, sc->sm->sm_ino, &agi_bp, &ip);
140 /* Actually got the incore inode, so install it and proceed. */
141 xchk_trans_cancel(sc);
142 return xchk_install_handle_iscrub(sc, ip);
144 if (error == -ENOENT)
146 if (error != -EFSCORRUPTED && error != -EFSBADCRC && error != -EINVAL)
149 /* Ensure that we have protected against inode allocation/freeing. */
150 if (agi_bp == NULL) {
151 ASSERT(agi_bp != NULL);
157 * Untrusted iget failed a second time. Let's try an inobt lookup.
158 * If the inobt doesn't think this is an allocated inode then we'll
159 * return ENOENT to signal that the check can be skipped.
161 * If the lookup signals corruption, we'll mark this inode corrupt and
162 * exit to userspace. There's little chance of fixing anything until
163 * the inobt is straightened out, but there's nothing we can do here.
165 * If the lookup encounters a runtime error, exit to userspace.
167 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sc->sm->sm_ino));
169 error = -EFSCORRUPTED;
173 error = xfs_imap(pag, sc->tp, sc->sm->sm_ino, &imap,
176 if (error == -EINVAL || error == -ENOENT)
182 * The lookup succeeded. Chances are the ondisk inode is corrupt and
183 * preventing iget from reading it. Retain the scrub transaction and
184 * the AGI buffer to prevent anyone from allocating or freeing inodes.
185 * This ensures that we preserve the inconsistency between the inobt
186 * saying the inode is allocated and the icache being unable to load
187 * the inode until we can flag the corruption in xchk_inode. The
188 * scrub function has to note the corruption, since we're not really
189 * supposed to do that from the setup function. Save the mapping to
190 * make repairs to the ondisk inode buffer.
192 if (xchk_could_repair(sc))
193 xrep_setup_inode(sc, &imap);
197 xchk_trans_cancel(sc);
199 trace_xchk_op_error(sc, agno, XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
200 error, __return_address);
203 /* The file is gone, so there's nothing to check. */
204 xchk_trans_cancel(sc);
210 /* Validate di_extsize hint. */
213 struct xfs_scrub *sc,
214 struct xfs_dinode *dip,
220 uint32_t value = be32_to_cpu(dip->di_extsize);
222 fa = xfs_inode_validate_extsize(sc->mp, value, mode, flags);
224 xchk_ino_set_corrupt(sc, ino);
227 * XFS allows a sysadmin to change the rt extent size when adding a rt
228 * section to a filesystem after formatting. If there are any
229 * directories with extszinherit and rtinherit set, the hint could
230 * become misaligned with the new rextsize. The verifier doesn't check
231 * this, because we allow rtinherit directories even without an rt
232 * device. Flag this as an administrative warning since we will clean
233 * this up eventually.
235 if ((flags & XFS_DIFLAG_RTINHERIT) &&
236 (flags & XFS_DIFLAG_EXTSZINHERIT) &&
237 xfs_extlen_to_rtxmod(sc->mp, value) > 0)
238 xchk_ino_set_warning(sc, ino);
242 * Validate di_cowextsize hint.
244 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
245 * These functions must be kept in sync with each other.
248 xchk_inode_cowextsize(
249 struct xfs_scrub *sc,
250 struct xfs_dinode *dip,
258 fa = xfs_inode_validate_cowextsize(sc->mp,
259 be32_to_cpu(dip->di_cowextsize), mode, flags,
262 xchk_ino_set_corrupt(sc, ino);
265 /* Make sure the di_flags make sense for the inode. */
268 struct xfs_scrub *sc,
269 struct xfs_dinode *dip,
274 struct xfs_mount *mp = sc->mp;
276 /* di_flags are all taken, last bit cannot be used */
277 if (flags & ~XFS_DIFLAG_ANY)
280 /* rt flags require rt device */
281 if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
284 /* new rt bitmap flag only valid for rbmino */
285 if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
288 /* directory-only flags */
289 if ((flags & (XFS_DIFLAG_RTINHERIT |
290 XFS_DIFLAG_EXTSZINHERIT |
291 XFS_DIFLAG_PROJINHERIT |
292 XFS_DIFLAG_NOSYMLINKS)) &&
296 /* file-only flags */
297 if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
301 /* filestreams and rt make no sense */
302 if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
307 xchk_ino_set_corrupt(sc, ino);
310 /* Make sure the di_flags2 make sense for the inode. */
313 struct xfs_scrub *sc,
314 struct xfs_dinode *dip,
320 struct xfs_mount *mp = sc->mp;
322 /* Unknown di_flags2 could be from a future kernel */
323 if (flags2 & ~XFS_DIFLAG2_ANY)
324 xchk_ino_set_warning(sc, ino);
326 /* reflink flag requires reflink feature */
327 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
328 !xfs_has_reflink(mp))
331 /* cowextsize flag is checked w.r.t. mode separately */
333 /* file/dir-only flags */
334 if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
337 /* file-only flags */
338 if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
341 /* realtime and reflink make no sense, currently */
342 if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
345 /* no bigtime iflag without the bigtime feature */
346 if (xfs_dinode_has_bigtime(dip) && !xfs_has_bigtime(mp))
349 /* no large extent counts without the filesystem feature */
350 if ((flags2 & XFS_DIFLAG2_NREXT64) && !xfs_has_large_extent_counts(mp))
355 xchk_ino_set_corrupt(sc, ino);
360 struct xfs_scrub *sc,
362 struct xfs_dinode *dip,
363 const xfs_timestamp_t ts)
365 struct timespec64 tv;
367 tv = xfs_inode_from_disk_ts(dip, ts);
368 if (tv.tv_nsec < 0 || tv.tv_nsec >= NSEC_PER_SEC)
369 xchk_ino_set_corrupt(sc, ino);
372 /* Scrub all the ondisk inode fields. */
375 struct xfs_scrub *sc,
376 struct xfs_dinode *dip,
379 struct xfs_mount *mp = sc->mp;
381 unsigned long long isize;
383 xfs_extnum_t nextents;
384 xfs_extnum_t naextents;
389 flags = be16_to_cpu(dip->di_flags);
390 if (dip->di_version >= 3)
391 flags2 = be64_to_cpu(dip->di_flags2);
396 mode = be16_to_cpu(dip->di_mode);
397 switch (mode & S_IFMT) {
405 /* mode is recognized */
408 xchk_ino_set_corrupt(sc, ino);
413 switch (dip->di_version) {
416 * We autoconvert v1 inodes into v2 inodes on writeout,
417 * so just mark this inode for preening.
419 xchk_ino_set_preen(sc, ino);
424 if (dip->di_onlink != 0)
425 xchk_ino_set_corrupt(sc, ino);
427 if (dip->di_mode == 0 && sc->ip)
428 xchk_ino_set_corrupt(sc, ino);
430 if (dip->di_projid_hi != 0 &&
431 !xfs_has_projid32(mp))
432 xchk_ino_set_corrupt(sc, ino);
434 prid = be16_to_cpu(dip->di_projid_lo);
437 xchk_ino_set_corrupt(sc, ino);
441 if (xfs_has_projid32(mp))
442 prid |= (prid_t)be16_to_cpu(dip->di_projid_hi) << 16;
445 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
446 * userspace could have created that.
448 if (dip->di_uid == cpu_to_be32(-1U) ||
449 dip->di_gid == cpu_to_be32(-1U))
450 xchk_ino_set_warning(sc, ino);
453 * project id of -1 isn't supposed to be valid, but the kernel didn't
454 * always validate that.
457 xchk_ino_set_warning(sc, ino);
460 switch (dip->di_format) {
461 case XFS_DINODE_FMT_DEV:
462 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
463 !S_ISFIFO(mode) && !S_ISSOCK(mode))
464 xchk_ino_set_corrupt(sc, ino);
466 case XFS_DINODE_FMT_LOCAL:
467 if (!S_ISDIR(mode) && !S_ISLNK(mode))
468 xchk_ino_set_corrupt(sc, ino);
470 case XFS_DINODE_FMT_EXTENTS:
471 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
472 xchk_ino_set_corrupt(sc, ino);
474 case XFS_DINODE_FMT_BTREE:
475 if (!S_ISREG(mode) && !S_ISDIR(mode))
476 xchk_ino_set_corrupt(sc, ino);
478 case XFS_DINODE_FMT_UUID:
480 xchk_ino_set_corrupt(sc, ino);
484 /* di_[amc]time.nsec */
485 xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
486 xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
487 xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
490 * di_size. xfs_dinode_verify checks for things that screw up
491 * the VFS such as the upper bit being set and zero-length
492 * symlinks/directories, but we can do more here.
494 isize = be64_to_cpu(dip->di_size);
495 if (isize & (1ULL << 63))
496 xchk_ino_set_corrupt(sc, ino);
498 /* Devices, fifos, and sockets must have zero size */
499 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
500 xchk_ino_set_corrupt(sc, ino);
502 /* Directories can't be larger than the data section size (32G) */
503 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
504 xchk_ino_set_corrupt(sc, ino);
506 /* Symlinks can't be larger than SYMLINK_MAXLEN */
507 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
508 xchk_ino_set_corrupt(sc, ino);
511 * Warn if the running kernel can't handle the kinds of offsets
512 * needed to deal with the file size. In other words, if the
513 * pagecache can't cache all the blocks in this file due to
514 * overly large offsets, flag the inode for admin review.
516 if (isize > mp->m_super->s_maxbytes)
517 xchk_ino_set_warning(sc, ino);
520 if (flags2 & XFS_DIFLAG2_REFLINK) {
521 ; /* nblocks can exceed dblocks */
522 } else if (flags & XFS_DIFLAG_REALTIME) {
524 * nblocks is the sum of data extents (in the rtdev),
525 * attr extents (in the datadev), and both forks' bmbt
526 * blocks (in the datadev). This clumsy check is the
527 * best we can do without cross-referencing with the
530 if (be64_to_cpu(dip->di_nblocks) >=
531 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
532 xchk_ino_set_corrupt(sc, ino);
534 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
535 xchk_ino_set_corrupt(sc, ino);
538 xchk_inode_flags(sc, dip, ino, mode, flags);
540 xchk_inode_extsize(sc, dip, ino, mode, flags);
542 nextents = xfs_dfork_data_extents(dip);
543 naextents = xfs_dfork_attr_extents(dip);
546 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
547 switch (dip->di_format) {
548 case XFS_DINODE_FMT_EXTENTS:
549 if (nextents > fork_recs)
550 xchk_ino_set_corrupt(sc, ino);
552 case XFS_DINODE_FMT_BTREE:
553 if (nextents <= fork_recs)
554 xchk_ino_set_corrupt(sc, ino);
558 xchk_ino_set_corrupt(sc, ino);
563 if (XFS_DFORK_BOFF(dip) >= mp->m_sb.sb_inodesize)
564 xchk_ino_set_corrupt(sc, ino);
565 if (naextents != 0 && dip->di_forkoff == 0)
566 xchk_ino_set_corrupt(sc, ino);
567 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
568 xchk_ino_set_corrupt(sc, ino);
571 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
572 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
573 dip->di_aformat != XFS_DINODE_FMT_BTREE)
574 xchk_ino_set_corrupt(sc, ino);
577 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
578 switch (dip->di_aformat) {
579 case XFS_DINODE_FMT_EXTENTS:
580 if (naextents > fork_recs)
581 xchk_ino_set_corrupt(sc, ino);
583 case XFS_DINODE_FMT_BTREE:
584 if (naextents <= fork_recs)
585 xchk_ino_set_corrupt(sc, ino);
589 xchk_ino_set_corrupt(sc, ino);
592 if (dip->di_version >= 3) {
593 xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
594 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
595 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
601 * Make sure the finobt doesn't think this inode is free.
602 * We don't have to check the inobt ourselves because we got the inode via
603 * IGET_UNTRUSTED, which checks the inobt for us.
606 xchk_inode_xref_finobt(
607 struct xfs_scrub *sc,
610 struct xfs_inobt_rec_incore rec;
615 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
618 agino = XFS_INO_TO_AGINO(sc->mp, ino);
621 * Try to get the finobt record. If we can't get it, then we're
624 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
626 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
630 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
631 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
636 * Otherwise, make sure this record either doesn't cover this inode,
637 * or that it does but it's marked present.
639 if (rec.ir_startino > agino ||
640 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
643 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
644 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
647 /* Cross reference the inode fields with the forks. */
649 xchk_inode_xref_bmap(
650 struct xfs_scrub *sc,
651 struct xfs_dinode *dip)
653 xfs_extnum_t nextents;
655 xfs_filblks_t acount;
658 if (xchk_skip_xref(sc->sm))
661 /* Walk all the extents to check nextents/naextents/nblocks. */
662 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
664 if (!xchk_should_check_xref(sc, &error, NULL))
666 if (nextents < xfs_dfork_data_extents(dip))
667 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
669 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
671 if (!xchk_should_check_xref(sc, &error, NULL))
673 if (nextents != xfs_dfork_attr_extents(dip))
674 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
676 /* Check nblocks against the inode. */
677 if (count + acount != be64_to_cpu(dip->di_nblocks))
678 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
681 /* Cross-reference with the other btrees. */
684 struct xfs_scrub *sc,
686 struct xfs_dinode *dip)
692 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
695 agno = XFS_INO_TO_AGNO(sc->mp, ino);
696 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
698 error = xchk_ag_init_existing(sc, agno, &sc->sa);
699 if (!xchk_xref_process_error(sc, agno, agbno, &error))
702 xchk_xref_is_used_space(sc, agbno, 1);
703 xchk_inode_xref_finobt(sc, ino);
704 xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
705 xchk_xref_is_not_shared(sc, agbno, 1);
706 xchk_xref_is_not_cow_staging(sc, agbno, 1);
707 xchk_inode_xref_bmap(sc, dip);
710 xchk_ag_free(sc, &sc->sa);
714 * If the reflink iflag disagrees with a scan for shared data fork extents,
715 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
716 * any shared extents). We already checked for reflink iflag set on a non
717 * reflink filesystem.
720 xchk_inode_check_reflink_iflag(
721 struct xfs_scrub *sc,
724 struct xfs_mount *mp = sc->mp;
728 if (!xfs_has_reflink(mp))
731 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
733 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
734 XFS_INO_TO_AGBNO(mp, ino), &error))
736 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
737 xchk_ino_set_preen(sc, ino);
738 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
739 xchk_ino_set_corrupt(sc, ino);
743 * If this inode has zero link count, it must be on the unlinked list. If
744 * it has nonzero link count, it must not be on the unlinked list.
747 xchk_inode_check_unlinked(
748 struct xfs_scrub *sc)
750 if (VFS_I(sc->ip)->i_nlink == 0) {
751 if (!xfs_inode_on_unlinked_list(sc->ip))
752 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
754 if (xfs_inode_on_unlinked_list(sc->ip))
755 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
759 /* Scrub an inode. */
762 struct xfs_scrub *sc)
764 struct xfs_dinode di;
768 * If sc->ip is NULL, that means that the setup function called
769 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
770 * and a NULL inode, so flag the corruption error and return.
773 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
777 /* Scrub the inode core. */
778 xfs_inode_to_disk(sc->ip, &di, 0);
779 xchk_dinode(sc, &di, sc->ip->i_ino);
780 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
784 * Look for discrepancies between file's data blocks and the reflink
785 * iflag. We already checked the iflag against the file mode when
786 * we scrubbed the dinode.
788 if (S_ISREG(VFS_I(sc->ip)->i_mode))
789 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
791 xchk_inode_check_unlinked(sc);
793 xchk_inode_xref(sc, sc->ip->i_ino, &di);