1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 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_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
37 * Try to get the inode. If the verifiers fail, we try again
40 error = xchk_get_inode(sc, ip);
46 return xchk_trans_alloc(sc, 0);
51 /* Got the inode, lock it and we're ready to go. */
52 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
53 xfs_ilock(sc->ip, sc->ilock_flags);
54 error = xchk_trans_alloc(sc, 0);
57 sc->ilock_flags |= XFS_ILOCK_EXCL;
58 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
61 /* scrub teardown will unlock and release the inode for us */
67 /* Validate di_extsize hint. */
71 struct xfs_dinode *dip,
78 fa = xfs_inode_validate_extsize(sc->mp, be32_to_cpu(dip->di_extsize),
81 xchk_ino_set_corrupt(sc, ino);
85 * Validate di_cowextsize hint.
87 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
88 * These functions must be kept in sync with each other.
91 xchk_inode_cowextsize(
93 struct xfs_dinode *dip,
101 fa = xfs_inode_validate_cowextsize(sc->mp,
102 be32_to_cpu(dip->di_cowextsize), mode, flags,
105 xchk_ino_set_corrupt(sc, ino);
108 /* Make sure the di_flags make sense for the inode. */
111 struct xfs_scrub *sc,
112 struct xfs_dinode *dip,
117 struct xfs_mount *mp = sc->mp;
119 /* di_flags are all taken, last bit cannot be used */
120 if (flags & ~XFS_DIFLAG_ANY)
123 /* rt flags require rt device */
124 if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
127 /* new rt bitmap flag only valid for rbmino */
128 if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
131 /* directory-only flags */
132 if ((flags & (XFS_DIFLAG_RTINHERIT |
133 XFS_DIFLAG_EXTSZINHERIT |
134 XFS_DIFLAG_PROJINHERIT |
135 XFS_DIFLAG_NOSYMLINKS)) &&
139 /* file-only flags */
140 if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
144 /* filestreams and rt make no sense */
145 if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
150 xchk_ino_set_corrupt(sc, ino);
153 /* Make sure the di_flags2 make sense for the inode. */
156 struct xfs_scrub *sc,
157 struct xfs_dinode *dip,
163 struct xfs_mount *mp = sc->mp;
165 /* Unknown di_flags2 could be from a future kernel */
166 if (flags2 & ~XFS_DIFLAG2_ANY)
167 xchk_ino_set_warning(sc, ino);
169 /* reflink flag requires reflink feature */
170 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
171 !xfs_sb_version_hasreflink(&mp->m_sb))
174 /* cowextsize flag is checked w.r.t. mode separately */
176 /* file/dir-only flags */
177 if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
180 /* file-only flags */
181 if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
184 /* realtime and reflink make no sense, currently */
185 if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
188 /* no bigtime iflag without the bigtime feature */
189 if (xfs_dinode_has_bigtime(dip) &&
190 !xfs_sb_version_hasbigtime(&mp->m_sb))
195 xchk_ino_set_corrupt(sc, ino);
200 struct xfs_scrub *sc,
202 struct xfs_dinode *dip,
203 const xfs_timestamp_t ts)
205 struct timespec64 tv;
207 tv = xfs_inode_from_disk_ts(dip, ts);
208 if (tv.tv_nsec < 0 || tv.tv_nsec >= NSEC_PER_SEC)
209 xchk_ino_set_corrupt(sc, ino);
212 /* Scrub all the ondisk inode fields. */
215 struct xfs_scrub *sc,
216 struct xfs_dinode *dip,
219 struct xfs_mount *mp = sc->mp;
221 unsigned long long isize;
227 flags = be16_to_cpu(dip->di_flags);
228 if (dip->di_version >= 3)
229 flags2 = be64_to_cpu(dip->di_flags2);
234 mode = be16_to_cpu(dip->di_mode);
235 switch (mode & S_IFMT) {
243 /* mode is recognized */
246 xchk_ino_set_corrupt(sc, ino);
251 switch (dip->di_version) {
254 * We autoconvert v1 inodes into v2 inodes on writeout,
255 * so just mark this inode for preening.
257 xchk_ino_set_preen(sc, ino);
261 if (dip->di_onlink != 0)
262 xchk_ino_set_corrupt(sc, ino);
264 if (dip->di_mode == 0 && sc->ip)
265 xchk_ino_set_corrupt(sc, ino);
267 if (dip->di_projid_hi != 0 &&
268 !xfs_sb_version_hasprojid32bit(&mp->m_sb))
269 xchk_ino_set_corrupt(sc, ino);
272 xchk_ino_set_corrupt(sc, ino);
277 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
278 * userspace could have created that.
280 if (dip->di_uid == cpu_to_be32(-1U) ||
281 dip->di_gid == cpu_to_be32(-1U))
282 xchk_ino_set_warning(sc, ino);
285 switch (dip->di_format) {
286 case XFS_DINODE_FMT_DEV:
287 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
288 !S_ISFIFO(mode) && !S_ISSOCK(mode))
289 xchk_ino_set_corrupt(sc, ino);
291 case XFS_DINODE_FMT_LOCAL:
292 if (!S_ISDIR(mode) && !S_ISLNK(mode))
293 xchk_ino_set_corrupt(sc, ino);
295 case XFS_DINODE_FMT_EXTENTS:
296 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
297 xchk_ino_set_corrupt(sc, ino);
299 case XFS_DINODE_FMT_BTREE:
300 if (!S_ISREG(mode) && !S_ISDIR(mode))
301 xchk_ino_set_corrupt(sc, ino);
303 case XFS_DINODE_FMT_UUID:
305 xchk_ino_set_corrupt(sc, ino);
309 /* di_[amc]time.nsec */
310 xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
311 xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
312 xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
315 * di_size. xfs_dinode_verify checks for things that screw up
316 * the VFS such as the upper bit being set and zero-length
317 * symlinks/directories, but we can do more here.
319 isize = be64_to_cpu(dip->di_size);
320 if (isize & (1ULL << 63))
321 xchk_ino_set_corrupt(sc, ino);
323 /* Devices, fifos, and sockets must have zero size */
324 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
325 xchk_ino_set_corrupt(sc, ino);
327 /* Directories can't be larger than the data section size (32G) */
328 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
329 xchk_ino_set_corrupt(sc, ino);
331 /* Symlinks can't be larger than SYMLINK_MAXLEN */
332 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
333 xchk_ino_set_corrupt(sc, ino);
336 * Warn if the running kernel can't handle the kinds of offsets
337 * needed to deal with the file size. In other words, if the
338 * pagecache can't cache all the blocks in this file due to
339 * overly large offsets, flag the inode for admin review.
341 if (isize >= mp->m_super->s_maxbytes)
342 xchk_ino_set_warning(sc, ino);
345 if (flags2 & XFS_DIFLAG2_REFLINK) {
346 ; /* nblocks can exceed dblocks */
347 } else if (flags & XFS_DIFLAG_REALTIME) {
349 * nblocks is the sum of data extents (in the rtdev),
350 * attr extents (in the datadev), and both forks' bmbt
351 * blocks (in the datadev). This clumsy check is the
352 * best we can do without cross-referencing with the
355 if (be64_to_cpu(dip->di_nblocks) >=
356 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
357 xchk_ino_set_corrupt(sc, ino);
359 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
360 xchk_ino_set_corrupt(sc, ino);
363 xchk_inode_flags(sc, dip, ino, mode, flags);
365 xchk_inode_extsize(sc, dip, ino, mode, flags);
368 nextents = be32_to_cpu(dip->di_nextents);
369 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
370 switch (dip->di_format) {
371 case XFS_DINODE_FMT_EXTENTS:
372 if (nextents > fork_recs)
373 xchk_ino_set_corrupt(sc, ino);
375 case XFS_DINODE_FMT_BTREE:
376 if (nextents <= fork_recs)
377 xchk_ino_set_corrupt(sc, ino);
381 xchk_ino_set_corrupt(sc, ino);
386 if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize)
387 xchk_ino_set_corrupt(sc, ino);
388 if (dip->di_anextents != 0 && dip->di_forkoff == 0)
389 xchk_ino_set_corrupt(sc, ino);
390 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
391 xchk_ino_set_corrupt(sc, ino);
394 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
395 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
396 dip->di_aformat != XFS_DINODE_FMT_BTREE)
397 xchk_ino_set_corrupt(sc, ino);
400 nextents = be16_to_cpu(dip->di_anextents);
401 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
402 switch (dip->di_aformat) {
403 case XFS_DINODE_FMT_EXTENTS:
404 if (nextents > fork_recs)
405 xchk_ino_set_corrupt(sc, ino);
407 case XFS_DINODE_FMT_BTREE:
408 if (nextents <= fork_recs)
409 xchk_ino_set_corrupt(sc, ino);
413 xchk_ino_set_corrupt(sc, ino);
416 if (dip->di_version >= 3) {
417 xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
418 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
419 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
425 * Make sure the finobt doesn't think this inode is free.
426 * We don't have to check the inobt ourselves because we got the inode via
427 * IGET_UNTRUSTED, which checks the inobt for us.
430 xchk_inode_xref_finobt(
431 struct xfs_scrub *sc,
434 struct xfs_inobt_rec_incore rec;
439 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
442 agino = XFS_INO_TO_AGINO(sc->mp, ino);
445 * Try to get the finobt record. If we can't get it, then we're
448 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
450 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
454 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
455 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
460 * Otherwise, make sure this record either doesn't cover this inode,
461 * or that it does but it's marked present.
463 if (rec.ir_startino > agino ||
464 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
467 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
468 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
471 /* Cross reference the inode fields with the forks. */
473 xchk_inode_xref_bmap(
474 struct xfs_scrub *sc,
475 struct xfs_dinode *dip)
477 xfs_extnum_t nextents;
479 xfs_filblks_t acount;
482 if (xchk_skip_xref(sc->sm))
485 /* Walk all the extents to check nextents/naextents/nblocks. */
486 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
488 if (!xchk_should_check_xref(sc, &error, NULL))
490 if (nextents < be32_to_cpu(dip->di_nextents))
491 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
493 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
495 if (!xchk_should_check_xref(sc, &error, NULL))
497 if (nextents != be16_to_cpu(dip->di_anextents))
498 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
500 /* Check nblocks against the inode. */
501 if (count + acount != be64_to_cpu(dip->di_nblocks))
502 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
505 /* Cross-reference with the other btrees. */
508 struct xfs_scrub *sc,
510 struct xfs_dinode *dip)
516 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
519 agno = XFS_INO_TO_AGNO(sc->mp, ino);
520 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
522 error = xchk_ag_init(sc, agno, &sc->sa);
523 if (!xchk_xref_process_error(sc, agno, agbno, &error))
526 xchk_xref_is_used_space(sc, agbno, 1);
527 xchk_inode_xref_finobt(sc, ino);
528 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
529 xchk_xref_is_not_shared(sc, agbno, 1);
530 xchk_inode_xref_bmap(sc, dip);
532 xchk_ag_free(sc, &sc->sa);
536 * If the reflink iflag disagrees with a scan for shared data fork extents,
537 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
538 * any shared extents). We already checked for reflink iflag set on a non
539 * reflink filesystem.
542 xchk_inode_check_reflink_iflag(
543 struct xfs_scrub *sc,
546 struct xfs_mount *mp = sc->mp;
550 if (!xfs_sb_version_hasreflink(&mp->m_sb))
553 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
555 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
556 XFS_INO_TO_AGBNO(mp, ino), &error))
558 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
559 xchk_ino_set_preen(sc, ino);
560 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
561 xchk_ino_set_corrupt(sc, ino);
564 /* Scrub an inode. */
567 struct xfs_scrub *sc)
569 struct xfs_dinode di;
573 * If sc->ip is NULL, that means that the setup function called
574 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
575 * and a NULL inode, so flag the corruption error and return.
578 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
582 /* Scrub the inode core. */
583 xfs_inode_to_disk(sc->ip, &di, 0);
584 xchk_dinode(sc, &di, sc->ip->i_ino);
585 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
589 * Look for discrepancies between file's data blocks and the reflink
590 * iflag. We already checked the iflag against the file mode when
591 * we scrubbed the dinode.
593 if (S_ISREG(VFS_I(sc->ip)->i_mode))
594 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
596 xchk_inode_xref(sc, sc->ip->i_ino, &di);