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,
76 uint32_t value = be32_to_cpu(dip->di_extsize);
78 fa = xfs_inode_validate_extsize(sc->mp, value, mode, flags);
80 xchk_ino_set_corrupt(sc, ino);
83 * XFS allows a sysadmin to change the rt extent size when adding a rt
84 * section to a filesystem after formatting. If there are any
85 * directories with extszinherit and rtinherit set, the hint could
86 * become misaligned with the new rextsize. The verifier doesn't check
87 * this, because we allow rtinherit directories even without an rt
88 * device. Flag this as an administrative warning since we will clean
91 if ((flags & XFS_DIFLAG_RTINHERIT) &&
92 (flags & XFS_DIFLAG_EXTSZINHERIT) &&
93 value % sc->mp->m_sb.sb_rextsize > 0)
94 xchk_ino_set_warning(sc, ino);
98 * Validate di_cowextsize hint.
100 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
101 * These functions must be kept in sync with each other.
104 xchk_inode_cowextsize(
105 struct xfs_scrub *sc,
106 struct xfs_dinode *dip,
114 fa = xfs_inode_validate_cowextsize(sc->mp,
115 be32_to_cpu(dip->di_cowextsize), mode, flags,
118 xchk_ino_set_corrupt(sc, ino);
121 /* Make sure the di_flags make sense for the inode. */
124 struct xfs_scrub *sc,
125 struct xfs_dinode *dip,
130 struct xfs_mount *mp = sc->mp;
132 /* di_flags are all taken, last bit cannot be used */
133 if (flags & ~XFS_DIFLAG_ANY)
136 /* rt flags require rt device */
137 if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
140 /* new rt bitmap flag only valid for rbmino */
141 if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
144 /* directory-only flags */
145 if ((flags & (XFS_DIFLAG_RTINHERIT |
146 XFS_DIFLAG_EXTSZINHERIT |
147 XFS_DIFLAG_PROJINHERIT |
148 XFS_DIFLAG_NOSYMLINKS)) &&
152 /* file-only flags */
153 if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
157 /* filestreams and rt make no sense */
158 if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
163 xchk_ino_set_corrupt(sc, ino);
166 /* Make sure the di_flags2 make sense for the inode. */
169 struct xfs_scrub *sc,
170 struct xfs_dinode *dip,
176 struct xfs_mount *mp = sc->mp;
178 /* Unknown di_flags2 could be from a future kernel */
179 if (flags2 & ~XFS_DIFLAG2_ANY)
180 xchk_ino_set_warning(sc, ino);
182 /* reflink flag requires reflink feature */
183 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
184 !xfs_has_reflink(mp))
187 /* cowextsize flag is checked w.r.t. mode separately */
189 /* file/dir-only flags */
190 if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
193 /* file-only flags */
194 if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
197 /* realtime and reflink make no sense, currently */
198 if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
201 /* no bigtime iflag without the bigtime feature */
202 if (xfs_dinode_has_bigtime(dip) && !xfs_has_bigtime(mp))
207 xchk_ino_set_corrupt(sc, ino);
212 struct xfs_scrub *sc,
214 struct xfs_dinode *dip,
215 const xfs_timestamp_t ts)
217 struct timespec64 tv;
219 tv = xfs_inode_from_disk_ts(dip, ts);
220 if (tv.tv_nsec < 0 || tv.tv_nsec >= NSEC_PER_SEC)
221 xchk_ino_set_corrupt(sc, ino);
224 /* Scrub all the ondisk inode fields. */
227 struct xfs_scrub *sc,
228 struct xfs_dinode *dip,
231 struct xfs_mount *mp = sc->mp;
233 unsigned long long isize;
235 xfs_extnum_t nextents;
236 xfs_extnum_t naextents;
241 flags = be16_to_cpu(dip->di_flags);
242 if (dip->di_version >= 3)
243 flags2 = be64_to_cpu(dip->di_flags2);
248 mode = be16_to_cpu(dip->di_mode);
249 switch (mode & S_IFMT) {
257 /* mode is recognized */
260 xchk_ino_set_corrupt(sc, ino);
265 switch (dip->di_version) {
268 * We autoconvert v1 inodes into v2 inodes on writeout,
269 * so just mark this inode for preening.
271 xchk_ino_set_preen(sc, ino);
276 if (dip->di_onlink != 0)
277 xchk_ino_set_corrupt(sc, ino);
279 if (dip->di_mode == 0 && sc->ip)
280 xchk_ino_set_corrupt(sc, ino);
282 if (dip->di_projid_hi != 0 &&
283 !xfs_has_projid32(mp))
284 xchk_ino_set_corrupt(sc, ino);
286 prid = be16_to_cpu(dip->di_projid_lo);
289 xchk_ino_set_corrupt(sc, ino);
293 if (xfs_has_projid32(mp))
294 prid |= (prid_t)be16_to_cpu(dip->di_projid_hi) << 16;
297 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
298 * userspace could have created that.
300 if (dip->di_uid == cpu_to_be32(-1U) ||
301 dip->di_gid == cpu_to_be32(-1U))
302 xchk_ino_set_warning(sc, ino);
305 * project id of -1 isn't supposed to be valid, but the kernel didn't
306 * always validate that.
309 xchk_ino_set_warning(sc, ino);
312 switch (dip->di_format) {
313 case XFS_DINODE_FMT_DEV:
314 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
315 !S_ISFIFO(mode) && !S_ISSOCK(mode))
316 xchk_ino_set_corrupt(sc, ino);
318 case XFS_DINODE_FMT_LOCAL:
319 if (!S_ISDIR(mode) && !S_ISLNK(mode))
320 xchk_ino_set_corrupt(sc, ino);
322 case XFS_DINODE_FMT_EXTENTS:
323 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
324 xchk_ino_set_corrupt(sc, ino);
326 case XFS_DINODE_FMT_BTREE:
327 if (!S_ISREG(mode) && !S_ISDIR(mode))
328 xchk_ino_set_corrupt(sc, ino);
330 case XFS_DINODE_FMT_UUID:
332 xchk_ino_set_corrupt(sc, ino);
336 /* di_[amc]time.nsec */
337 xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
338 xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
339 xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
342 * di_size. xfs_dinode_verify checks for things that screw up
343 * the VFS such as the upper bit being set and zero-length
344 * symlinks/directories, but we can do more here.
346 isize = be64_to_cpu(dip->di_size);
347 if (isize & (1ULL << 63))
348 xchk_ino_set_corrupt(sc, ino);
350 /* Devices, fifos, and sockets must have zero size */
351 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
352 xchk_ino_set_corrupt(sc, ino);
354 /* Directories can't be larger than the data section size (32G) */
355 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
356 xchk_ino_set_corrupt(sc, ino);
358 /* Symlinks can't be larger than SYMLINK_MAXLEN */
359 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
360 xchk_ino_set_corrupt(sc, ino);
363 * Warn if the running kernel can't handle the kinds of offsets
364 * needed to deal with the file size. In other words, if the
365 * pagecache can't cache all the blocks in this file due to
366 * overly large offsets, flag the inode for admin review.
368 if (isize >= mp->m_super->s_maxbytes)
369 xchk_ino_set_warning(sc, ino);
372 if (flags2 & XFS_DIFLAG2_REFLINK) {
373 ; /* nblocks can exceed dblocks */
374 } else if (flags & XFS_DIFLAG_REALTIME) {
376 * nblocks is the sum of data extents (in the rtdev),
377 * attr extents (in the datadev), and both forks' bmbt
378 * blocks (in the datadev). This clumsy check is the
379 * best we can do without cross-referencing with the
382 if (be64_to_cpu(dip->di_nblocks) >=
383 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
384 xchk_ino_set_corrupt(sc, ino);
386 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
387 xchk_ino_set_corrupt(sc, ino);
390 xchk_inode_flags(sc, dip, ino, mode, flags);
392 xchk_inode_extsize(sc, dip, ino, mode, flags);
394 nextents = xfs_dfork_data_extents(dip);
395 naextents = xfs_dfork_attr_extents(dip);
398 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
399 switch (dip->di_format) {
400 case XFS_DINODE_FMT_EXTENTS:
401 if (nextents > fork_recs)
402 xchk_ino_set_corrupt(sc, ino);
404 case XFS_DINODE_FMT_BTREE:
405 if (nextents <= fork_recs)
406 xchk_ino_set_corrupt(sc, ino);
410 xchk_ino_set_corrupt(sc, ino);
415 if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize)
416 xchk_ino_set_corrupt(sc, ino);
417 if (naextents != 0 && dip->di_forkoff == 0)
418 xchk_ino_set_corrupt(sc, ino);
419 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
420 xchk_ino_set_corrupt(sc, ino);
423 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
424 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
425 dip->di_aformat != XFS_DINODE_FMT_BTREE)
426 xchk_ino_set_corrupt(sc, ino);
429 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
430 switch (dip->di_aformat) {
431 case XFS_DINODE_FMT_EXTENTS:
432 if (naextents > fork_recs)
433 xchk_ino_set_corrupt(sc, ino);
435 case XFS_DINODE_FMT_BTREE:
436 if (naextents <= fork_recs)
437 xchk_ino_set_corrupt(sc, ino);
441 xchk_ino_set_corrupt(sc, ino);
444 if (dip->di_version >= 3) {
445 xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
446 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
447 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
453 * Make sure the finobt doesn't think this inode is free.
454 * We don't have to check the inobt ourselves because we got the inode via
455 * IGET_UNTRUSTED, which checks the inobt for us.
458 xchk_inode_xref_finobt(
459 struct xfs_scrub *sc,
462 struct xfs_inobt_rec_incore rec;
467 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
470 agino = XFS_INO_TO_AGINO(sc->mp, ino);
473 * Try to get the finobt record. If we can't get it, then we're
476 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
478 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
482 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
483 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
488 * Otherwise, make sure this record either doesn't cover this inode,
489 * or that it does but it's marked present.
491 if (rec.ir_startino > agino ||
492 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
495 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
496 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
499 /* Cross reference the inode fields with the forks. */
501 xchk_inode_xref_bmap(
502 struct xfs_scrub *sc,
503 struct xfs_dinode *dip)
505 xfs_extnum_t nextents;
507 xfs_filblks_t acount;
510 if (xchk_skip_xref(sc->sm))
513 /* Walk all the extents to check nextents/naextents/nblocks. */
514 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
516 if (!xchk_should_check_xref(sc, &error, NULL))
518 if (nextents < xfs_dfork_data_extents(dip))
519 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
521 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
523 if (!xchk_should_check_xref(sc, &error, NULL))
525 if (nextents != xfs_dfork_attr_extents(dip))
526 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
528 /* Check nblocks against the inode. */
529 if (count + acount != be64_to_cpu(dip->di_nblocks))
530 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
533 /* Cross-reference with the other btrees. */
536 struct xfs_scrub *sc,
538 struct xfs_dinode *dip)
544 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
547 agno = XFS_INO_TO_AGNO(sc->mp, ino);
548 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
550 error = xchk_ag_init_existing(sc, agno, &sc->sa);
551 if (!xchk_xref_process_error(sc, agno, agbno, &error))
554 xchk_xref_is_used_space(sc, agbno, 1);
555 xchk_inode_xref_finobt(sc, ino);
556 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
557 xchk_xref_is_not_shared(sc, agbno, 1);
558 xchk_inode_xref_bmap(sc, dip);
561 xchk_ag_free(sc, &sc->sa);
565 * If the reflink iflag disagrees with a scan for shared data fork extents,
566 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
567 * any shared extents). We already checked for reflink iflag set on a non
568 * reflink filesystem.
571 xchk_inode_check_reflink_iflag(
572 struct xfs_scrub *sc,
575 struct xfs_mount *mp = sc->mp;
579 if (!xfs_has_reflink(mp))
582 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
584 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
585 XFS_INO_TO_AGBNO(mp, ino), &error))
587 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
588 xchk_ino_set_preen(sc, ino);
589 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
590 xchk_ino_set_corrupt(sc, ino);
593 /* Scrub an inode. */
596 struct xfs_scrub *sc)
598 struct xfs_dinode di;
602 * If sc->ip is NULL, that means that the setup function called
603 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
604 * and a NULL inode, so flag the corruption error and return.
607 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
611 /* Scrub the inode core. */
612 xfs_inode_to_disk(sc->ip, &di, 0);
613 xchk_dinode(sc, &di, sc->ip->i_ino);
614 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
618 * Look for discrepancies between file's data blocks and the reflink
619 * iflag. We already checked the iflag against the file mode when
620 * we scrubbed the dinode.
622 if (S_ISREG(VFS_I(sc->ip)->i_mode))
623 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
625 xchk_inode_xref(sc, sc->ip->i_ino, &di);