1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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_inode.h"
15 #include "xfs_ialloc.h"
16 #include "xfs_da_format.h"
17 #include "xfs_reflink.h"
19 #include "xfs_bmap_util.h"
20 #include "scrub/scrub.h"
21 #include "scrub/common.h"
22 #include "scrub/btree.h"
25 * Grab total control of the inode metadata. It doesn't matter here if
26 * the file data is still changing; exclusive access to the metadata is
36 * Try to get the inode. If the verifiers fail, we try again
39 error = xchk_get_inode(sc);
45 return xchk_trans_alloc(sc, 0);
50 /* Got the inode, lock it and we're ready to go. */
51 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
52 xfs_ilock(sc->ip, sc->ilock_flags);
53 error = xchk_trans_alloc(sc, 0);
56 sc->ilock_flags |= XFS_ILOCK_EXCL;
57 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
60 /* scrub teardown will unlock and release the inode for us */
66 /* Validate di_extsize hint. */
70 struct xfs_dinode *dip,
77 fa = xfs_inode_validate_extsize(sc->mp, be32_to_cpu(dip->di_extsize),
80 xchk_ino_set_corrupt(sc, ino);
84 * Validate di_cowextsize hint.
86 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
87 * These functions must be kept in sync with each other.
90 xchk_inode_cowextsize(
92 struct xfs_dinode *dip,
100 fa = xfs_inode_validate_cowextsize(sc->mp,
101 be32_to_cpu(dip->di_cowextsize), mode, flags,
104 xchk_ino_set_corrupt(sc, ino);
107 /* Make sure the di_flags make sense for the inode. */
110 struct xfs_scrub *sc,
111 struct xfs_dinode *dip,
116 struct xfs_mount *mp = sc->mp;
118 /* di_flags are all taken, last bit cannot be used */
119 if (flags & ~XFS_DIFLAG_ANY)
122 /* rt flags require rt device */
123 if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
126 /* new rt bitmap flag only valid for rbmino */
127 if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
130 /* directory-only flags */
131 if ((flags & (XFS_DIFLAG_RTINHERIT |
132 XFS_DIFLAG_EXTSZINHERIT |
133 XFS_DIFLAG_PROJINHERIT |
134 XFS_DIFLAG_NOSYMLINKS)) &&
138 /* file-only flags */
139 if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
143 /* filestreams and rt make no sense */
144 if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
149 xchk_ino_set_corrupt(sc, ino);
152 /* Make sure the di_flags2 make sense for the inode. */
155 struct xfs_scrub *sc,
156 struct xfs_dinode *dip,
162 struct xfs_mount *mp = sc->mp;
164 /* Unknown di_flags2 could be from a future kernel */
165 if (flags2 & ~XFS_DIFLAG2_ANY)
166 xchk_ino_set_warning(sc, ino);
168 /* reflink flag requires reflink feature */
169 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
170 !xfs_sb_version_hasreflink(&mp->m_sb))
173 /* cowextsize flag is checked w.r.t. mode separately */
175 /* file/dir-only flags */
176 if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
179 /* file-only flags */
180 if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
183 /* realtime and reflink make no sense, currently */
184 if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
187 /* no bigtime iflag without the bigtime feature */
188 if (xfs_dinode_has_bigtime(dip) &&
189 !xfs_sb_version_hasbigtime(&mp->m_sb))
194 xchk_ino_set_corrupt(sc, ino);
199 struct xfs_scrub *sc,
201 struct xfs_dinode *dip,
202 const xfs_timestamp_t ts)
204 struct timespec64 tv;
206 tv = xfs_inode_from_disk_ts(dip, ts);
207 if (tv.tv_nsec < 0 || tv.tv_nsec >= NSEC_PER_SEC)
208 xchk_ino_set_corrupt(sc, ino);
211 /* Scrub all the ondisk inode fields. */
214 struct xfs_scrub *sc,
215 struct xfs_dinode *dip,
218 struct xfs_mount *mp = sc->mp;
220 unsigned long long isize;
226 flags = be16_to_cpu(dip->di_flags);
227 if (dip->di_version >= 3)
228 flags2 = be64_to_cpu(dip->di_flags2);
233 mode = be16_to_cpu(dip->di_mode);
234 switch (mode & S_IFMT) {
242 /* mode is recognized */
245 xchk_ino_set_corrupt(sc, ino);
250 switch (dip->di_version) {
253 * We autoconvert v1 inodes into v2 inodes on writeout,
254 * so just mark this inode for preening.
256 xchk_ino_set_preen(sc, ino);
260 if (dip->di_onlink != 0)
261 xchk_ino_set_corrupt(sc, ino);
263 if (dip->di_mode == 0 && sc->ip)
264 xchk_ino_set_corrupt(sc, ino);
266 if (dip->di_projid_hi != 0 &&
267 !xfs_sb_version_hasprojid32bit(&mp->m_sb))
268 xchk_ino_set_corrupt(sc, ino);
271 xchk_ino_set_corrupt(sc, ino);
276 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
277 * userspace could have created that.
279 if (dip->di_uid == cpu_to_be32(-1U) ||
280 dip->di_gid == cpu_to_be32(-1U))
281 xchk_ino_set_warning(sc, ino);
284 switch (dip->di_format) {
285 case XFS_DINODE_FMT_DEV:
286 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
287 !S_ISFIFO(mode) && !S_ISSOCK(mode))
288 xchk_ino_set_corrupt(sc, ino);
290 case XFS_DINODE_FMT_LOCAL:
291 if (!S_ISDIR(mode) && !S_ISLNK(mode))
292 xchk_ino_set_corrupt(sc, ino);
294 case XFS_DINODE_FMT_EXTENTS:
295 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
296 xchk_ino_set_corrupt(sc, ino);
298 case XFS_DINODE_FMT_BTREE:
299 if (!S_ISREG(mode) && !S_ISDIR(mode))
300 xchk_ino_set_corrupt(sc, ino);
302 case XFS_DINODE_FMT_UUID:
304 xchk_ino_set_corrupt(sc, ino);
308 /* di_[amc]time.nsec */
309 xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
310 xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
311 xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
314 * di_size. xfs_dinode_verify checks for things that screw up
315 * the VFS such as the upper bit being set and zero-length
316 * symlinks/directories, but we can do more here.
318 isize = be64_to_cpu(dip->di_size);
319 if (isize & (1ULL << 63))
320 xchk_ino_set_corrupt(sc, ino);
322 /* Devices, fifos, and sockets must have zero size */
323 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
324 xchk_ino_set_corrupt(sc, ino);
326 /* Directories can't be larger than the data section size (32G) */
327 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
328 xchk_ino_set_corrupt(sc, ino);
330 /* Symlinks can't be larger than SYMLINK_MAXLEN */
331 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
332 xchk_ino_set_corrupt(sc, ino);
335 * Warn if the running kernel can't handle the kinds of offsets
336 * needed to deal with the file size. In other words, if the
337 * pagecache can't cache all the blocks in this file due to
338 * overly large offsets, flag the inode for admin review.
340 if (isize >= mp->m_super->s_maxbytes)
341 xchk_ino_set_warning(sc, ino);
344 if (flags2 & XFS_DIFLAG2_REFLINK) {
345 ; /* nblocks can exceed dblocks */
346 } else if (flags & XFS_DIFLAG_REALTIME) {
348 * nblocks is the sum of data extents (in the rtdev),
349 * attr extents (in the datadev), and both forks' bmbt
350 * blocks (in the datadev). This clumsy check is the
351 * best we can do without cross-referencing with the
354 if (be64_to_cpu(dip->di_nblocks) >=
355 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
356 xchk_ino_set_corrupt(sc, ino);
358 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
359 xchk_ino_set_corrupt(sc, ino);
362 xchk_inode_flags(sc, dip, ino, mode, flags);
364 xchk_inode_extsize(sc, dip, ino, mode, flags);
367 nextents = be32_to_cpu(dip->di_nextents);
368 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
369 switch (dip->di_format) {
370 case XFS_DINODE_FMT_EXTENTS:
371 if (nextents > fork_recs)
372 xchk_ino_set_corrupt(sc, ino);
374 case XFS_DINODE_FMT_BTREE:
375 if (nextents <= fork_recs)
376 xchk_ino_set_corrupt(sc, ino);
380 xchk_ino_set_corrupt(sc, ino);
385 if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize)
386 xchk_ino_set_corrupt(sc, ino);
387 if (dip->di_anextents != 0 && dip->di_forkoff == 0)
388 xchk_ino_set_corrupt(sc, ino);
389 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
390 xchk_ino_set_corrupt(sc, ino);
393 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
394 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
395 dip->di_aformat != XFS_DINODE_FMT_BTREE)
396 xchk_ino_set_corrupt(sc, ino);
399 nextents = be16_to_cpu(dip->di_anextents);
400 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
401 switch (dip->di_aformat) {
402 case XFS_DINODE_FMT_EXTENTS:
403 if (nextents > fork_recs)
404 xchk_ino_set_corrupt(sc, ino);
406 case XFS_DINODE_FMT_BTREE:
407 if (nextents <= fork_recs)
408 xchk_ino_set_corrupt(sc, ino);
412 xchk_ino_set_corrupt(sc, ino);
415 if (dip->di_version >= 3) {
416 xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
417 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
418 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
424 * Make sure the finobt doesn't think this inode is free.
425 * We don't have to check the inobt ourselves because we got the inode via
426 * IGET_UNTRUSTED, which checks the inobt for us.
429 xchk_inode_xref_finobt(
430 struct xfs_scrub *sc,
433 struct xfs_inobt_rec_incore rec;
438 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
441 agino = XFS_INO_TO_AGINO(sc->mp, ino);
444 * Try to get the finobt record. If we can't get it, then we're
447 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
449 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
453 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
454 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
459 * Otherwise, make sure this record either doesn't cover this inode,
460 * or that it does but it's marked present.
462 if (rec.ir_startino > agino ||
463 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
466 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
467 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
470 /* Cross reference the inode fields with the forks. */
472 xchk_inode_xref_bmap(
473 struct xfs_scrub *sc,
474 struct xfs_dinode *dip)
476 xfs_extnum_t nextents;
478 xfs_filblks_t acount;
481 if (xchk_skip_xref(sc->sm))
484 /* Walk all the extents to check nextents/naextents/nblocks. */
485 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
487 if (!xchk_should_check_xref(sc, &error, NULL))
489 if (nextents < be32_to_cpu(dip->di_nextents))
490 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
492 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
494 if (!xchk_should_check_xref(sc, &error, NULL))
496 if (nextents != be16_to_cpu(dip->di_anextents))
497 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
499 /* Check nblocks against the inode. */
500 if (count + acount != be64_to_cpu(dip->di_nblocks))
501 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
504 /* Cross-reference with the other btrees. */
507 struct xfs_scrub *sc,
509 struct xfs_dinode *dip)
515 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
518 agno = XFS_INO_TO_AGNO(sc->mp, ino);
519 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
521 error = xchk_ag_init(sc, agno, &sc->sa);
522 if (!xchk_xref_process_error(sc, agno, agbno, &error))
525 xchk_xref_is_used_space(sc, agbno, 1);
526 xchk_inode_xref_finobt(sc, ino);
527 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
528 xchk_xref_is_not_shared(sc, agbno, 1);
529 xchk_inode_xref_bmap(sc, dip);
531 xchk_ag_free(sc, &sc->sa);
535 * If the reflink iflag disagrees with a scan for shared data fork extents,
536 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
537 * any shared extents). We already checked for reflink iflag set on a non
538 * reflink filesystem.
541 xchk_inode_check_reflink_iflag(
542 struct xfs_scrub *sc,
545 struct xfs_mount *mp = sc->mp;
549 if (!xfs_sb_version_hasreflink(&mp->m_sb))
552 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
554 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
555 XFS_INO_TO_AGBNO(mp, ino), &error))
557 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
558 xchk_ino_set_preen(sc, ino);
559 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
560 xchk_ino_set_corrupt(sc, ino);
563 /* Scrub an inode. */
566 struct xfs_scrub *sc)
568 struct xfs_dinode di;
572 * If sc->ip is NULL, that means that the setup function called
573 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
574 * and a NULL inode, so flag the corruption error and return.
577 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
581 /* Scrub the inode core. */
582 xfs_inode_to_disk(sc->ip, &di, 0);
583 xchk_dinode(sc, &di, sc->ip->i_ino);
584 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
588 * Look for discrepancies between file's data blocks and the reflink
589 * iflag. We already checked the iflag against the file mode when
590 * we scrubbed the dinode.
592 if (S_ISREG(VFS_I(sc->ip)->i_mode))
593 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
595 xchk_inode_xref(sc, sc->ip->i_ino, &di);