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;
239 flags = be16_to_cpu(dip->di_flags);
240 if (dip->di_version >= 3)
241 flags2 = be64_to_cpu(dip->di_flags2);
246 mode = be16_to_cpu(dip->di_mode);
247 switch (mode & S_IFMT) {
255 /* mode is recognized */
258 xchk_ino_set_corrupt(sc, ino);
263 switch (dip->di_version) {
266 * We autoconvert v1 inodes into v2 inodes on writeout,
267 * so just mark this inode for preening.
269 xchk_ino_set_preen(sc, ino);
273 if (dip->di_onlink != 0)
274 xchk_ino_set_corrupt(sc, ino);
276 if (dip->di_mode == 0 && sc->ip)
277 xchk_ino_set_corrupt(sc, ino);
279 if (dip->di_projid_hi != 0 &&
280 !xfs_has_projid32(mp))
281 xchk_ino_set_corrupt(sc, ino);
284 xchk_ino_set_corrupt(sc, ino);
289 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
290 * userspace could have created that.
292 if (dip->di_uid == cpu_to_be32(-1U) ||
293 dip->di_gid == cpu_to_be32(-1U))
294 xchk_ino_set_warning(sc, ino);
297 switch (dip->di_format) {
298 case XFS_DINODE_FMT_DEV:
299 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
300 !S_ISFIFO(mode) && !S_ISSOCK(mode))
301 xchk_ino_set_corrupt(sc, ino);
303 case XFS_DINODE_FMT_LOCAL:
304 if (!S_ISDIR(mode) && !S_ISLNK(mode))
305 xchk_ino_set_corrupt(sc, ino);
307 case XFS_DINODE_FMT_EXTENTS:
308 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
309 xchk_ino_set_corrupt(sc, ino);
311 case XFS_DINODE_FMT_BTREE:
312 if (!S_ISREG(mode) && !S_ISDIR(mode))
313 xchk_ino_set_corrupt(sc, ino);
315 case XFS_DINODE_FMT_UUID:
317 xchk_ino_set_corrupt(sc, ino);
321 /* di_[amc]time.nsec */
322 xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
323 xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
324 xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
327 * di_size. xfs_dinode_verify checks for things that screw up
328 * the VFS such as the upper bit being set and zero-length
329 * symlinks/directories, but we can do more here.
331 isize = be64_to_cpu(dip->di_size);
332 if (isize & (1ULL << 63))
333 xchk_ino_set_corrupt(sc, ino);
335 /* Devices, fifos, and sockets must have zero size */
336 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
337 xchk_ino_set_corrupt(sc, ino);
339 /* Directories can't be larger than the data section size (32G) */
340 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
341 xchk_ino_set_corrupt(sc, ino);
343 /* Symlinks can't be larger than SYMLINK_MAXLEN */
344 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
345 xchk_ino_set_corrupt(sc, ino);
348 * Warn if the running kernel can't handle the kinds of offsets
349 * needed to deal with the file size. In other words, if the
350 * pagecache can't cache all the blocks in this file due to
351 * overly large offsets, flag the inode for admin review.
353 if (isize >= mp->m_super->s_maxbytes)
354 xchk_ino_set_warning(sc, ino);
357 if (flags2 & XFS_DIFLAG2_REFLINK) {
358 ; /* nblocks can exceed dblocks */
359 } else if (flags & XFS_DIFLAG_REALTIME) {
361 * nblocks is the sum of data extents (in the rtdev),
362 * attr extents (in the datadev), and both forks' bmbt
363 * blocks (in the datadev). This clumsy check is the
364 * best we can do without cross-referencing with the
367 if (be64_to_cpu(dip->di_nblocks) >=
368 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
369 xchk_ino_set_corrupt(sc, ino);
371 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
372 xchk_ino_set_corrupt(sc, ino);
375 xchk_inode_flags(sc, dip, ino, mode, flags);
377 xchk_inode_extsize(sc, dip, ino, mode, flags);
380 nextents = be32_to_cpu(dip->di_nextents);
381 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
382 switch (dip->di_format) {
383 case XFS_DINODE_FMT_EXTENTS:
384 if (nextents > fork_recs)
385 xchk_ino_set_corrupt(sc, ino);
387 case XFS_DINODE_FMT_BTREE:
388 if (nextents <= fork_recs)
389 xchk_ino_set_corrupt(sc, ino);
393 xchk_ino_set_corrupt(sc, ino);
398 if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize)
399 xchk_ino_set_corrupt(sc, ino);
400 if (dip->di_anextents != 0 && dip->di_forkoff == 0)
401 xchk_ino_set_corrupt(sc, ino);
402 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
403 xchk_ino_set_corrupt(sc, ino);
406 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
407 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
408 dip->di_aformat != XFS_DINODE_FMT_BTREE)
409 xchk_ino_set_corrupt(sc, ino);
412 nextents = be16_to_cpu(dip->di_anextents);
413 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
414 switch (dip->di_aformat) {
415 case XFS_DINODE_FMT_EXTENTS:
416 if (nextents > fork_recs)
417 xchk_ino_set_corrupt(sc, ino);
419 case XFS_DINODE_FMT_BTREE:
420 if (nextents <= fork_recs)
421 xchk_ino_set_corrupt(sc, ino);
425 xchk_ino_set_corrupt(sc, ino);
428 if (dip->di_version >= 3) {
429 xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
430 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
431 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
437 * Make sure the finobt doesn't think this inode is free.
438 * We don't have to check the inobt ourselves because we got the inode via
439 * IGET_UNTRUSTED, which checks the inobt for us.
442 xchk_inode_xref_finobt(
443 struct xfs_scrub *sc,
446 struct xfs_inobt_rec_incore rec;
451 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
454 agino = XFS_INO_TO_AGINO(sc->mp, ino);
457 * Try to get the finobt record. If we can't get it, then we're
460 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
462 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
466 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
467 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
472 * Otherwise, make sure this record either doesn't cover this inode,
473 * or that it does but it's marked present.
475 if (rec.ir_startino > agino ||
476 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
479 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
480 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
483 /* Cross reference the inode fields with the forks. */
485 xchk_inode_xref_bmap(
486 struct xfs_scrub *sc,
487 struct xfs_dinode *dip)
489 xfs_extnum_t nextents;
491 xfs_filblks_t acount;
494 if (xchk_skip_xref(sc->sm))
497 /* Walk all the extents to check nextents/naextents/nblocks. */
498 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
500 if (!xchk_should_check_xref(sc, &error, NULL))
502 if (nextents < be32_to_cpu(dip->di_nextents))
503 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
505 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
507 if (!xchk_should_check_xref(sc, &error, NULL))
509 if (nextents != be16_to_cpu(dip->di_anextents))
510 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
512 /* Check nblocks against the inode. */
513 if (count + acount != be64_to_cpu(dip->di_nblocks))
514 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
517 /* Cross-reference with the other btrees. */
520 struct xfs_scrub *sc,
522 struct xfs_dinode *dip)
528 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
531 agno = XFS_INO_TO_AGNO(sc->mp, ino);
532 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
534 error = xchk_ag_init_existing(sc, agno, &sc->sa);
535 if (!xchk_xref_process_error(sc, agno, agbno, &error))
538 xchk_xref_is_used_space(sc, agbno, 1);
539 xchk_inode_xref_finobt(sc, ino);
540 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
541 xchk_xref_is_not_shared(sc, agbno, 1);
542 xchk_inode_xref_bmap(sc, dip);
545 xchk_ag_free(sc, &sc->sa);
549 * If the reflink iflag disagrees with a scan for shared data fork extents,
550 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
551 * any shared extents). We already checked for reflink iflag set on a non
552 * reflink filesystem.
555 xchk_inode_check_reflink_iflag(
556 struct xfs_scrub *sc,
559 struct xfs_mount *mp = sc->mp;
563 if (!xfs_has_reflink(mp))
566 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
568 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
569 XFS_INO_TO_AGBNO(mp, ino), &error))
571 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
572 xchk_ino_set_preen(sc, ino);
573 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
574 xchk_ino_set_corrupt(sc, ino);
577 /* Scrub an inode. */
580 struct xfs_scrub *sc)
582 struct xfs_dinode di;
586 * If sc->ip is NULL, that means that the setup function called
587 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
588 * and a NULL inode, so flag the corruption error and return.
591 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
595 /* Scrub the inode core. */
596 xfs_inode_to_disk(sc->ip, &di, 0);
597 xchk_dinode(sc, &di, sc->ip->i_ino);
598 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
602 * Look for discrepancies between file's data blocks and the reflink
603 * iflag. We already checked the iflag against the file mode when
604 * we scrubbed the dinode.
606 if (S_ISREG(VFS_I(sc->ip)->i_mode))
607 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
609 xchk_inode_xref(sc, sc->ip->i_ino, &di);