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 "scrub/scrub.h"
24 #include "scrub/common.h"
25 #include "scrub/btree.h"
26 #include "scrub/trace.h"
28 /* Prepare the attached inode for scrubbing. */
35 sc->ilock_flags = XFS_IOLOCK_EXCL;
36 xfs_ilock(sc->ip, sc->ilock_flags);
38 error = xchk_trans_alloc(sc, 0);
42 sc->ilock_flags |= XFS_ILOCK_EXCL;
43 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
47 /* Install this scrub-by-handle inode and prepare it for scrubbing. */
49 xchk_install_handle_iscrub(
55 error = xchk_install_handle_inode(sc, ip);
59 return xchk_prepare_iscrub(sc);
63 * Grab total control of the inode metadata. In the best case, we grab the
64 * incore inode and take all locks on it. If the incore inode cannot be
65 * constructed due to corruption problems, lock the AGI so that we can single
66 * step the loading process to fix everything that can go wrong.
74 struct xfs_mount *mp = sc->mp;
75 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file));
76 struct xfs_buf *agi_bp;
77 struct xfs_perag *pag;
78 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, sc->sm->sm_ino);
81 if (xchk_need_intent_drain(sc))
82 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
84 /* We want to scan the opened inode, so lock it and exit. */
85 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
87 return xchk_prepare_iscrub(sc);
90 /* Reject internal metadata files and obviously bad inode numbers. */
91 if (xfs_internal_inum(mp, sc->sm->sm_ino))
93 if (!xfs_verify_ino(sc->mp, sc->sm->sm_ino))
96 /* Try a regular untrusted iget. */
97 error = xchk_iget(sc, sc->sm->sm_ino, &ip);
99 return xchk_install_handle_iscrub(sc, ip);
100 if (error == -ENOENT)
102 if (error != -EFSCORRUPTED && error != -EFSBADCRC && error != -EINVAL)
106 * EINVAL with IGET_UNTRUSTED probably means one of several things:
107 * userspace gave us an inode number that doesn't correspond to fs
108 * space; the inode btree lacks a record for this inode; or there is
109 * a record, and it says this inode is free.
111 * EFSCORRUPTED/EFSBADCRC could mean that the inode was mappable, but
112 * some other metadata corruption (e.g. inode forks) prevented
113 * instantiation of the incore inode. Or it could mean the inobt is
116 * We want to look up this inode in the inobt directly to distinguish
117 * three different scenarios: (1) the inobt says the inode is free,
118 * in which case there's nothing to do; (2) the inobt is corrupt so we
119 * should flag the corruption and exit to userspace to let it fix the
120 * inobt; and (3) the inobt says the inode is allocated, but loading it
121 * failed due to corruption.
123 * Allocate a transaction and grab the AGI to prevent inobt activity in
124 * this AG. Retry the iget in case someone allocated a new inode after
125 * the first iget failed.
127 error = xchk_trans_alloc(sc, 0);
131 error = xchk_iget_agi(sc, sc->sm->sm_ino, &agi_bp, &ip);
133 /* Actually got the incore inode, so install it and proceed. */
134 xchk_trans_cancel(sc);
135 return xchk_install_handle_iscrub(sc, ip);
137 if (error == -ENOENT)
139 if (error != -EFSCORRUPTED && error != -EFSBADCRC && error != -EINVAL)
142 /* Ensure that we have protected against inode allocation/freeing. */
143 if (agi_bp == NULL) {
144 ASSERT(agi_bp != NULL);
150 * Untrusted iget failed a second time. Let's try an inobt lookup.
151 * If the inobt doesn't think this is an allocated inode then we'll
152 * return ENOENT to signal that the check can be skipped.
154 * If the lookup signals corruption, we'll mark this inode corrupt and
155 * exit to userspace. There's little chance of fixing anything until
156 * the inobt is straightened out, but there's nothing we can do here.
158 * If the lookup encounters a runtime error, exit to userspace.
160 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sc->sm->sm_ino));
162 error = -EFSCORRUPTED;
166 error = xfs_imap(pag, sc->tp, sc->sm->sm_ino, &imap,
169 if (error == -EINVAL || error == -ENOENT)
175 * The lookup succeeded. Chances are the ondisk inode is corrupt and
176 * preventing iget from reading it. Retain the scrub transaction and
177 * the AGI buffer to prevent anyone from allocating or freeing inodes.
178 * This ensures that we preserve the inconsistency between the inobt
179 * saying the inode is allocated and the icache being unable to load
180 * the inode until we can flag the corruption in xchk_inode. The
181 * scrub function has to note the corruption, since we're not really
182 * supposed to do that from the setup function.
187 xchk_trans_cancel(sc);
189 trace_xchk_op_error(sc, agno, XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
190 error, __return_address);
193 /* The file is gone, so there's nothing to check. */
194 xchk_trans_cancel(sc);
200 /* Validate di_extsize hint. */
203 struct xfs_scrub *sc,
204 struct xfs_dinode *dip,
210 uint32_t value = be32_to_cpu(dip->di_extsize);
212 fa = xfs_inode_validate_extsize(sc->mp, value, mode, flags);
214 xchk_ino_set_corrupt(sc, ino);
217 * XFS allows a sysadmin to change the rt extent size when adding a rt
218 * section to a filesystem after formatting. If there are any
219 * directories with extszinherit and rtinherit set, the hint could
220 * become misaligned with the new rextsize. The verifier doesn't check
221 * this, because we allow rtinherit directories even without an rt
222 * device. Flag this as an administrative warning since we will clean
223 * this up eventually.
225 if ((flags & XFS_DIFLAG_RTINHERIT) &&
226 (flags & XFS_DIFLAG_EXTSZINHERIT) &&
227 value % sc->mp->m_sb.sb_rextsize > 0)
228 xchk_ino_set_warning(sc, ino);
232 * Validate di_cowextsize hint.
234 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
235 * These functions must be kept in sync with each other.
238 xchk_inode_cowextsize(
239 struct xfs_scrub *sc,
240 struct xfs_dinode *dip,
248 fa = xfs_inode_validate_cowextsize(sc->mp,
249 be32_to_cpu(dip->di_cowextsize), mode, flags,
252 xchk_ino_set_corrupt(sc, ino);
255 /* Make sure the di_flags make sense for the inode. */
258 struct xfs_scrub *sc,
259 struct xfs_dinode *dip,
264 struct xfs_mount *mp = sc->mp;
266 /* di_flags are all taken, last bit cannot be used */
267 if (flags & ~XFS_DIFLAG_ANY)
270 /* rt flags require rt device */
271 if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
274 /* new rt bitmap flag only valid for rbmino */
275 if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
278 /* directory-only flags */
279 if ((flags & (XFS_DIFLAG_RTINHERIT |
280 XFS_DIFLAG_EXTSZINHERIT |
281 XFS_DIFLAG_PROJINHERIT |
282 XFS_DIFLAG_NOSYMLINKS)) &&
286 /* file-only flags */
287 if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
291 /* filestreams and rt make no sense */
292 if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
297 xchk_ino_set_corrupt(sc, ino);
300 /* Make sure the di_flags2 make sense for the inode. */
303 struct xfs_scrub *sc,
304 struct xfs_dinode *dip,
310 struct xfs_mount *mp = sc->mp;
312 /* Unknown di_flags2 could be from a future kernel */
313 if (flags2 & ~XFS_DIFLAG2_ANY)
314 xchk_ino_set_warning(sc, ino);
316 /* reflink flag requires reflink feature */
317 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
318 !xfs_has_reflink(mp))
321 /* cowextsize flag is checked w.r.t. mode separately */
323 /* file/dir-only flags */
324 if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
327 /* file-only flags */
328 if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
331 /* realtime and reflink make no sense, currently */
332 if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
335 /* no bigtime iflag without the bigtime feature */
336 if (xfs_dinode_has_bigtime(dip) && !xfs_has_bigtime(mp))
341 xchk_ino_set_corrupt(sc, ino);
346 struct xfs_scrub *sc,
348 struct xfs_dinode *dip,
349 const xfs_timestamp_t ts)
351 struct timespec64 tv;
353 tv = xfs_inode_from_disk_ts(dip, ts);
354 if (tv.tv_nsec < 0 || tv.tv_nsec >= NSEC_PER_SEC)
355 xchk_ino_set_corrupt(sc, ino);
358 /* Scrub all the ondisk inode fields. */
361 struct xfs_scrub *sc,
362 struct xfs_dinode *dip,
365 struct xfs_mount *mp = sc->mp;
367 unsigned long long isize;
369 xfs_extnum_t nextents;
370 xfs_extnum_t naextents;
375 flags = be16_to_cpu(dip->di_flags);
376 if (dip->di_version >= 3)
377 flags2 = be64_to_cpu(dip->di_flags2);
382 mode = be16_to_cpu(dip->di_mode);
383 switch (mode & S_IFMT) {
391 /* mode is recognized */
394 xchk_ino_set_corrupt(sc, ino);
399 switch (dip->di_version) {
402 * We autoconvert v1 inodes into v2 inodes on writeout,
403 * so just mark this inode for preening.
405 xchk_ino_set_preen(sc, ino);
410 if (dip->di_onlink != 0)
411 xchk_ino_set_corrupt(sc, ino);
413 if (dip->di_mode == 0 && sc->ip)
414 xchk_ino_set_corrupt(sc, ino);
416 if (dip->di_projid_hi != 0 &&
417 !xfs_has_projid32(mp))
418 xchk_ino_set_corrupt(sc, ino);
420 prid = be16_to_cpu(dip->di_projid_lo);
423 xchk_ino_set_corrupt(sc, ino);
427 if (xfs_has_projid32(mp))
428 prid |= (prid_t)be16_to_cpu(dip->di_projid_hi) << 16;
431 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
432 * userspace could have created that.
434 if (dip->di_uid == cpu_to_be32(-1U) ||
435 dip->di_gid == cpu_to_be32(-1U))
436 xchk_ino_set_warning(sc, ino);
439 * project id of -1 isn't supposed to be valid, but the kernel didn't
440 * always validate that.
443 xchk_ino_set_warning(sc, ino);
446 switch (dip->di_format) {
447 case XFS_DINODE_FMT_DEV:
448 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
449 !S_ISFIFO(mode) && !S_ISSOCK(mode))
450 xchk_ino_set_corrupt(sc, ino);
452 case XFS_DINODE_FMT_LOCAL:
453 if (!S_ISDIR(mode) && !S_ISLNK(mode))
454 xchk_ino_set_corrupt(sc, ino);
456 case XFS_DINODE_FMT_EXTENTS:
457 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
458 xchk_ino_set_corrupt(sc, ino);
460 case XFS_DINODE_FMT_BTREE:
461 if (!S_ISREG(mode) && !S_ISDIR(mode))
462 xchk_ino_set_corrupt(sc, ino);
464 case XFS_DINODE_FMT_UUID:
466 xchk_ino_set_corrupt(sc, ino);
470 /* di_[amc]time.nsec */
471 xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
472 xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
473 xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
476 * di_size. xfs_dinode_verify checks for things that screw up
477 * the VFS such as the upper bit being set and zero-length
478 * symlinks/directories, but we can do more here.
480 isize = be64_to_cpu(dip->di_size);
481 if (isize & (1ULL << 63))
482 xchk_ino_set_corrupt(sc, ino);
484 /* Devices, fifos, and sockets must have zero size */
485 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
486 xchk_ino_set_corrupt(sc, ino);
488 /* Directories can't be larger than the data section size (32G) */
489 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
490 xchk_ino_set_corrupt(sc, ino);
492 /* Symlinks can't be larger than SYMLINK_MAXLEN */
493 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
494 xchk_ino_set_corrupt(sc, ino);
497 * Warn if the running kernel can't handle the kinds of offsets
498 * needed to deal with the file size. In other words, if the
499 * pagecache can't cache all the blocks in this file due to
500 * overly large offsets, flag the inode for admin review.
502 if (isize > mp->m_super->s_maxbytes)
503 xchk_ino_set_warning(sc, ino);
506 if (flags2 & XFS_DIFLAG2_REFLINK) {
507 ; /* nblocks can exceed dblocks */
508 } else if (flags & XFS_DIFLAG_REALTIME) {
510 * nblocks is the sum of data extents (in the rtdev),
511 * attr extents (in the datadev), and both forks' bmbt
512 * blocks (in the datadev). This clumsy check is the
513 * best we can do without cross-referencing with the
516 if (be64_to_cpu(dip->di_nblocks) >=
517 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
518 xchk_ino_set_corrupt(sc, ino);
520 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
521 xchk_ino_set_corrupt(sc, ino);
524 xchk_inode_flags(sc, dip, ino, mode, flags);
526 xchk_inode_extsize(sc, dip, ino, mode, flags);
528 nextents = xfs_dfork_data_extents(dip);
529 naextents = xfs_dfork_attr_extents(dip);
532 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
533 switch (dip->di_format) {
534 case XFS_DINODE_FMT_EXTENTS:
535 if (nextents > fork_recs)
536 xchk_ino_set_corrupt(sc, ino);
538 case XFS_DINODE_FMT_BTREE:
539 if (nextents <= fork_recs)
540 xchk_ino_set_corrupt(sc, ino);
544 xchk_ino_set_corrupt(sc, ino);
549 if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize)
550 xchk_ino_set_corrupt(sc, ino);
551 if (naextents != 0 && dip->di_forkoff == 0)
552 xchk_ino_set_corrupt(sc, ino);
553 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
554 xchk_ino_set_corrupt(sc, ino);
557 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
558 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
559 dip->di_aformat != XFS_DINODE_FMT_BTREE)
560 xchk_ino_set_corrupt(sc, ino);
563 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
564 switch (dip->di_aformat) {
565 case XFS_DINODE_FMT_EXTENTS:
566 if (naextents > fork_recs)
567 xchk_ino_set_corrupt(sc, ino);
569 case XFS_DINODE_FMT_BTREE:
570 if (naextents <= fork_recs)
571 xchk_ino_set_corrupt(sc, ino);
575 xchk_ino_set_corrupt(sc, ino);
578 if (dip->di_version >= 3) {
579 xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
580 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
581 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
587 * Make sure the finobt doesn't think this inode is free.
588 * We don't have to check the inobt ourselves because we got the inode via
589 * IGET_UNTRUSTED, which checks the inobt for us.
592 xchk_inode_xref_finobt(
593 struct xfs_scrub *sc,
596 struct xfs_inobt_rec_incore rec;
601 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
604 agino = XFS_INO_TO_AGINO(sc->mp, ino);
607 * Try to get the finobt record. If we can't get it, then we're
610 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
612 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
616 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
617 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
622 * Otherwise, make sure this record either doesn't cover this inode,
623 * or that it does but it's marked present.
625 if (rec.ir_startino > agino ||
626 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
629 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
630 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
633 /* Cross reference the inode fields with the forks. */
635 xchk_inode_xref_bmap(
636 struct xfs_scrub *sc,
637 struct xfs_dinode *dip)
639 xfs_extnum_t nextents;
641 xfs_filblks_t acount;
644 if (xchk_skip_xref(sc->sm))
647 /* Walk all the extents to check nextents/naextents/nblocks. */
648 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
650 if (!xchk_should_check_xref(sc, &error, NULL))
652 if (nextents < xfs_dfork_data_extents(dip))
653 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
655 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
657 if (!xchk_should_check_xref(sc, &error, NULL))
659 if (nextents != xfs_dfork_attr_extents(dip))
660 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
662 /* Check nblocks against the inode. */
663 if (count + acount != be64_to_cpu(dip->di_nblocks))
664 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
667 /* Cross-reference with the other btrees. */
670 struct xfs_scrub *sc,
672 struct xfs_dinode *dip)
678 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
681 agno = XFS_INO_TO_AGNO(sc->mp, ino);
682 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
684 error = xchk_ag_init_existing(sc, agno, &sc->sa);
685 if (!xchk_xref_process_error(sc, agno, agbno, &error))
688 xchk_xref_is_used_space(sc, agbno, 1);
689 xchk_inode_xref_finobt(sc, ino);
690 xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
691 xchk_xref_is_not_shared(sc, agbno, 1);
692 xchk_xref_is_not_cow_staging(sc, agbno, 1);
693 xchk_inode_xref_bmap(sc, dip);
696 xchk_ag_free(sc, &sc->sa);
700 * If the reflink iflag disagrees with a scan for shared data fork extents,
701 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
702 * any shared extents). We already checked for reflink iflag set on a non
703 * reflink filesystem.
706 xchk_inode_check_reflink_iflag(
707 struct xfs_scrub *sc,
710 struct xfs_mount *mp = sc->mp;
714 if (!xfs_has_reflink(mp))
717 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
719 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
720 XFS_INO_TO_AGBNO(mp, ino), &error))
722 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
723 xchk_ino_set_preen(sc, ino);
724 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
725 xchk_ino_set_corrupt(sc, ino);
728 /* Scrub an inode. */
731 struct xfs_scrub *sc)
733 struct xfs_dinode di;
737 * If sc->ip is NULL, that means that the setup function called
738 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
739 * and a NULL inode, so flag the corruption error and return.
742 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
746 /* Scrub the inode core. */
747 xfs_inode_to_disk(sc->ip, &di, 0);
748 xchk_dinode(sc, &di, sc->ip->i_ino);
749 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
753 * Look for discrepancies between file's data blocks and the reflink
754 * iflag. We already checked the iflag against the file mode when
755 * we scrubbed the dinode.
757 if (S_ISREG(VFS_I(sc->ip)->i_mode))
758 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
760 xchk_inode_xref(sc, sc->ip->i_ino, &di);