1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
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"
18 #include "xfs_rmap.h"
19 #include "xfs_bmap_util.h"
20 #include "scrub/scrub.h"
21 #include "scrub/common.h"
22 #include "scrub/btree.h"
23
24 /*
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
27 * the goal.
28 */
29 int
xchk_setup_inode(struct xfs_scrub * sc,struct xfs_inode * ip)30 xchk_setup_inode(
31 struct xfs_scrub *sc,
32 struct xfs_inode *ip)
33 {
34 int error;
35
36 /*
37 * Try to get the inode. If the verifiers fail, we try again
38 * in raw mode.
39 */
40 error = xchk_get_inode(sc, ip);
41 switch (error) {
42 case 0:
43 break;
44 case -EFSCORRUPTED:
45 case -EFSBADCRC:
46 return xchk_trans_alloc(sc, 0);
47 default:
48 return error;
49 }
50
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);
55 if (error)
56 goto out;
57 sc->ilock_flags |= XFS_ILOCK_EXCL;
58 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
59
60 out:
61 /* scrub teardown will unlock and release the inode for us */
62 return error;
63 }
64
65 /* Inode core */
66
67 /* Validate di_extsize hint. */
68 STATIC void
xchk_inode_extsize(struct xfs_scrub * sc,struct xfs_dinode * dip,xfs_ino_t ino,uint16_t mode,uint16_t flags)69 xchk_inode_extsize(
70 struct xfs_scrub *sc,
71 struct xfs_dinode *dip,
72 xfs_ino_t ino,
73 uint16_t mode,
74 uint16_t flags)
75 {
76 xfs_failaddr_t fa;
77
78 fa = xfs_inode_validate_extsize(sc->mp, be32_to_cpu(dip->di_extsize),
79 mode, flags);
80 if (fa)
81 xchk_ino_set_corrupt(sc, ino);
82 }
83
84 /*
85 * Validate di_cowextsize hint.
86 *
87 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
88 * These functions must be kept in sync with each other.
89 */
90 STATIC void
xchk_inode_cowextsize(struct xfs_scrub * sc,struct xfs_dinode * dip,xfs_ino_t ino,uint16_t mode,uint16_t flags,uint64_t flags2)91 xchk_inode_cowextsize(
92 struct xfs_scrub *sc,
93 struct xfs_dinode *dip,
94 xfs_ino_t ino,
95 uint16_t mode,
96 uint16_t flags,
97 uint64_t flags2)
98 {
99 xfs_failaddr_t fa;
100
101 fa = xfs_inode_validate_cowextsize(sc->mp,
102 be32_to_cpu(dip->di_cowextsize), mode, flags,
103 flags2);
104 if (fa)
105 xchk_ino_set_corrupt(sc, ino);
106 }
107
108 /* Make sure the di_flags make sense for the inode. */
109 STATIC void
xchk_inode_flags(struct xfs_scrub * sc,struct xfs_dinode * dip,xfs_ino_t ino,uint16_t mode,uint16_t flags)110 xchk_inode_flags(
111 struct xfs_scrub *sc,
112 struct xfs_dinode *dip,
113 xfs_ino_t ino,
114 uint16_t mode,
115 uint16_t flags)
116 {
117 struct xfs_mount *mp = sc->mp;
118
119 /* di_flags are all taken, last bit cannot be used */
120 if (flags & ~XFS_DIFLAG_ANY)
121 goto bad;
122
123 /* rt flags require rt device */
124 if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
125 goto bad;
126
127 /* new rt bitmap flag only valid for rbmino */
128 if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
129 goto bad;
130
131 /* directory-only flags */
132 if ((flags & (XFS_DIFLAG_RTINHERIT |
133 XFS_DIFLAG_EXTSZINHERIT |
134 XFS_DIFLAG_PROJINHERIT |
135 XFS_DIFLAG_NOSYMLINKS)) &&
136 !S_ISDIR(mode))
137 goto bad;
138
139 /* file-only flags */
140 if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
141 !S_ISREG(mode))
142 goto bad;
143
144 /* filestreams and rt make no sense */
145 if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
146 goto bad;
147
148 return;
149 bad:
150 xchk_ino_set_corrupt(sc, ino);
151 }
152
153 /* Make sure the di_flags2 make sense for the inode. */
154 STATIC void
xchk_inode_flags2(struct xfs_scrub * sc,struct xfs_dinode * dip,xfs_ino_t ino,uint16_t mode,uint16_t flags,uint64_t flags2)155 xchk_inode_flags2(
156 struct xfs_scrub *sc,
157 struct xfs_dinode *dip,
158 xfs_ino_t ino,
159 uint16_t mode,
160 uint16_t flags,
161 uint64_t flags2)
162 {
163 struct xfs_mount *mp = sc->mp;
164
165 /* Unknown di_flags2 could be from a future kernel */
166 if (flags2 & ~XFS_DIFLAG2_ANY)
167 xchk_ino_set_warning(sc, ino);
168
169 /* reflink flag requires reflink feature */
170 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
171 !xfs_sb_version_hasreflink(&mp->m_sb))
172 goto bad;
173
174 /* cowextsize flag is checked w.r.t. mode separately */
175
176 /* file/dir-only flags */
177 if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
178 goto bad;
179
180 /* file-only flags */
181 if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
182 goto bad;
183
184 /* realtime and reflink make no sense, currently */
185 if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
186 goto bad;
187
188 /* dax and reflink make no sense, currently */
189 if ((flags2 & XFS_DIFLAG2_DAX) && (flags2 & XFS_DIFLAG2_REFLINK))
190 goto bad;
191
192 return;
193 bad:
194 xchk_ino_set_corrupt(sc, ino);
195 }
196
197 /* Scrub all the ondisk inode fields. */
198 STATIC void
xchk_dinode(struct xfs_scrub * sc,struct xfs_dinode * dip,xfs_ino_t ino)199 xchk_dinode(
200 struct xfs_scrub *sc,
201 struct xfs_dinode *dip,
202 xfs_ino_t ino)
203 {
204 struct xfs_mount *mp = sc->mp;
205 size_t fork_recs;
206 unsigned long long isize;
207 uint64_t flags2;
208 uint32_t nextents;
209 uint16_t flags;
210 uint16_t mode;
211
212 flags = be16_to_cpu(dip->di_flags);
213 if (dip->di_version >= 3)
214 flags2 = be64_to_cpu(dip->di_flags2);
215 else
216 flags2 = 0;
217
218 /* di_mode */
219 mode = be16_to_cpu(dip->di_mode);
220 switch (mode & S_IFMT) {
221 case S_IFLNK:
222 case S_IFREG:
223 case S_IFDIR:
224 case S_IFCHR:
225 case S_IFBLK:
226 case S_IFIFO:
227 case S_IFSOCK:
228 /* mode is recognized */
229 break;
230 default:
231 xchk_ino_set_corrupt(sc, ino);
232 break;
233 }
234
235 /* v1/v2 fields */
236 switch (dip->di_version) {
237 case 1:
238 /*
239 * We autoconvert v1 inodes into v2 inodes on writeout,
240 * so just mark this inode for preening.
241 */
242 xchk_ino_set_preen(sc, ino);
243 break;
244 case 2:
245 case 3:
246 if (dip->di_onlink != 0)
247 xchk_ino_set_corrupt(sc, ino);
248
249 if (dip->di_mode == 0 && sc->ip)
250 xchk_ino_set_corrupt(sc, ino);
251
252 if (dip->di_projid_hi != 0 &&
253 !xfs_sb_version_hasprojid32bit(&mp->m_sb))
254 xchk_ino_set_corrupt(sc, ino);
255 break;
256 default:
257 xchk_ino_set_corrupt(sc, ino);
258 return;
259 }
260
261 /*
262 * di_uid/di_gid -- -1 isn't invalid, but there's no way that
263 * userspace could have created that.
264 */
265 if (dip->di_uid == cpu_to_be32(-1U) ||
266 dip->di_gid == cpu_to_be32(-1U))
267 xchk_ino_set_warning(sc, ino);
268
269 /* di_format */
270 switch (dip->di_format) {
271 case XFS_DINODE_FMT_DEV:
272 if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
273 !S_ISFIFO(mode) && !S_ISSOCK(mode))
274 xchk_ino_set_corrupt(sc, ino);
275 break;
276 case XFS_DINODE_FMT_LOCAL:
277 if (!S_ISDIR(mode) && !S_ISLNK(mode))
278 xchk_ino_set_corrupt(sc, ino);
279 break;
280 case XFS_DINODE_FMT_EXTENTS:
281 if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
282 xchk_ino_set_corrupt(sc, ino);
283 break;
284 case XFS_DINODE_FMT_BTREE:
285 if (!S_ISREG(mode) && !S_ISDIR(mode))
286 xchk_ino_set_corrupt(sc, ino);
287 break;
288 case XFS_DINODE_FMT_UUID:
289 default:
290 xchk_ino_set_corrupt(sc, ino);
291 break;
292 }
293
294 /* di_[amc]time.nsec */
295 if (be32_to_cpu(dip->di_atime.t_nsec) >= NSEC_PER_SEC)
296 xchk_ino_set_corrupt(sc, ino);
297 if (be32_to_cpu(dip->di_mtime.t_nsec) >= NSEC_PER_SEC)
298 xchk_ino_set_corrupt(sc, ino);
299 if (be32_to_cpu(dip->di_ctime.t_nsec) >= NSEC_PER_SEC)
300 xchk_ino_set_corrupt(sc, ino);
301
302 /*
303 * di_size. xfs_dinode_verify checks for things that screw up
304 * the VFS such as the upper bit being set and zero-length
305 * symlinks/directories, but we can do more here.
306 */
307 isize = be64_to_cpu(dip->di_size);
308 if (isize & (1ULL << 63))
309 xchk_ino_set_corrupt(sc, ino);
310
311 /* Devices, fifos, and sockets must have zero size */
312 if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
313 xchk_ino_set_corrupt(sc, ino);
314
315 /* Directories can't be larger than the data section size (32G) */
316 if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
317 xchk_ino_set_corrupt(sc, ino);
318
319 /* Symlinks can't be larger than SYMLINK_MAXLEN */
320 if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
321 xchk_ino_set_corrupt(sc, ino);
322
323 /*
324 * Warn if the running kernel can't handle the kinds of offsets
325 * needed to deal with the file size. In other words, if the
326 * pagecache can't cache all the blocks in this file due to
327 * overly large offsets, flag the inode for admin review.
328 */
329 if (isize >= mp->m_super->s_maxbytes)
330 xchk_ino_set_warning(sc, ino);
331
332 /* di_nblocks */
333 if (flags2 & XFS_DIFLAG2_REFLINK) {
334 ; /* nblocks can exceed dblocks */
335 } else if (flags & XFS_DIFLAG_REALTIME) {
336 /*
337 * nblocks is the sum of data extents (in the rtdev),
338 * attr extents (in the datadev), and both forks' bmbt
339 * blocks (in the datadev). This clumsy check is the
340 * best we can do without cross-referencing with the
341 * inode forks.
342 */
343 if (be64_to_cpu(dip->di_nblocks) >=
344 mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
345 xchk_ino_set_corrupt(sc, ino);
346 } else {
347 if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
348 xchk_ino_set_corrupt(sc, ino);
349 }
350
351 xchk_inode_flags(sc, dip, ino, mode, flags);
352
353 xchk_inode_extsize(sc, dip, ino, mode, flags);
354
355 /* di_nextents */
356 nextents = be32_to_cpu(dip->di_nextents);
357 fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
358 switch (dip->di_format) {
359 case XFS_DINODE_FMT_EXTENTS:
360 if (nextents > fork_recs)
361 xchk_ino_set_corrupt(sc, ino);
362 break;
363 case XFS_DINODE_FMT_BTREE:
364 if (nextents <= fork_recs)
365 xchk_ino_set_corrupt(sc, ino);
366 break;
367 default:
368 if (nextents != 0)
369 xchk_ino_set_corrupt(sc, ino);
370 break;
371 }
372
373 /* di_forkoff */
374 if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize)
375 xchk_ino_set_corrupt(sc, ino);
376 if (dip->di_anextents != 0 && dip->di_forkoff == 0)
377 xchk_ino_set_corrupt(sc, ino);
378 if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
379 xchk_ino_set_corrupt(sc, ino);
380
381 /* di_aformat */
382 if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
383 dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
384 dip->di_aformat != XFS_DINODE_FMT_BTREE)
385 xchk_ino_set_corrupt(sc, ino);
386
387 /* di_anextents */
388 nextents = be16_to_cpu(dip->di_anextents);
389 fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
390 switch (dip->di_aformat) {
391 case XFS_DINODE_FMT_EXTENTS:
392 if (nextents > fork_recs)
393 xchk_ino_set_corrupt(sc, ino);
394 break;
395 case XFS_DINODE_FMT_BTREE:
396 if (nextents <= fork_recs)
397 xchk_ino_set_corrupt(sc, ino);
398 break;
399 default:
400 if (nextents != 0)
401 xchk_ino_set_corrupt(sc, ino);
402 }
403
404 if (dip->di_version >= 3) {
405 if (be32_to_cpu(dip->di_crtime.t_nsec) >= NSEC_PER_SEC)
406 xchk_ino_set_corrupt(sc, ino);
407 xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
408 xchk_inode_cowextsize(sc, dip, ino, mode, flags,
409 flags2);
410 }
411 }
412
413 /*
414 * Make sure the finobt doesn't think this inode is free.
415 * We don't have to check the inobt ourselves because we got the inode via
416 * IGET_UNTRUSTED, which checks the inobt for us.
417 */
418 static void
xchk_inode_xref_finobt(struct xfs_scrub * sc,xfs_ino_t ino)419 xchk_inode_xref_finobt(
420 struct xfs_scrub *sc,
421 xfs_ino_t ino)
422 {
423 struct xfs_inobt_rec_incore rec;
424 xfs_agino_t agino;
425 int has_record;
426 int error;
427
428 if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
429 return;
430
431 agino = XFS_INO_TO_AGINO(sc->mp, ino);
432
433 /*
434 * Try to get the finobt record. If we can't get it, then we're
435 * in good shape.
436 */
437 error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
438 &has_record);
439 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
440 !has_record)
441 return;
442
443 error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
444 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
445 !has_record)
446 return;
447
448 /*
449 * Otherwise, make sure this record either doesn't cover this inode,
450 * or that it does but it's marked present.
451 */
452 if (rec.ir_startino > agino ||
453 rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
454 return;
455
456 if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
457 xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
458 }
459
460 /* Cross reference the inode fields with the forks. */
461 STATIC void
xchk_inode_xref_bmap(struct xfs_scrub * sc,struct xfs_dinode * dip)462 xchk_inode_xref_bmap(
463 struct xfs_scrub *sc,
464 struct xfs_dinode *dip)
465 {
466 xfs_extnum_t nextents;
467 xfs_filblks_t count;
468 xfs_filblks_t acount;
469 int error;
470
471 if (xchk_skip_xref(sc->sm))
472 return;
473
474 /* Walk all the extents to check nextents/naextents/nblocks. */
475 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
476 &nextents, &count);
477 if (!xchk_should_check_xref(sc, &error, NULL))
478 return;
479 if (nextents < be32_to_cpu(dip->di_nextents))
480 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
481
482 error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
483 &nextents, &acount);
484 if (!xchk_should_check_xref(sc, &error, NULL))
485 return;
486 if (nextents != be16_to_cpu(dip->di_anextents))
487 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
488
489 /* Check nblocks against the inode. */
490 if (count + acount != be64_to_cpu(dip->di_nblocks))
491 xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
492 }
493
494 /* Cross-reference with the other btrees. */
495 STATIC void
xchk_inode_xref(struct xfs_scrub * sc,xfs_ino_t ino,struct xfs_dinode * dip)496 xchk_inode_xref(
497 struct xfs_scrub *sc,
498 xfs_ino_t ino,
499 struct xfs_dinode *dip)
500 {
501 xfs_agnumber_t agno;
502 xfs_agblock_t agbno;
503 int error;
504
505 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
506 return;
507
508 agno = XFS_INO_TO_AGNO(sc->mp, ino);
509 agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
510
511 error = xchk_ag_init(sc, agno, &sc->sa);
512 if (!xchk_xref_process_error(sc, agno, agbno, &error))
513 return;
514
515 xchk_xref_is_used_space(sc, agbno, 1);
516 xchk_inode_xref_finobt(sc, ino);
517 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
518 xchk_xref_is_not_shared(sc, agbno, 1);
519 xchk_inode_xref_bmap(sc, dip);
520
521 xchk_ag_free(sc, &sc->sa);
522 }
523
524 /*
525 * If the reflink iflag disagrees with a scan for shared data fork extents,
526 * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
527 * any shared extents). We already checked for reflink iflag set on a non
528 * reflink filesystem.
529 */
530 static void
xchk_inode_check_reflink_iflag(struct xfs_scrub * sc,xfs_ino_t ino)531 xchk_inode_check_reflink_iflag(
532 struct xfs_scrub *sc,
533 xfs_ino_t ino)
534 {
535 struct xfs_mount *mp = sc->mp;
536 bool has_shared;
537 int error;
538
539 if (!xfs_sb_version_hasreflink(&mp->m_sb))
540 return;
541
542 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
543 &has_shared);
544 if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
545 XFS_INO_TO_AGBNO(mp, ino), &error))
546 return;
547 if (xfs_is_reflink_inode(sc->ip) && !has_shared)
548 xchk_ino_set_preen(sc, ino);
549 else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
550 xchk_ino_set_corrupt(sc, ino);
551 }
552
553 /* Scrub an inode. */
554 int
xchk_inode(struct xfs_scrub * sc)555 xchk_inode(
556 struct xfs_scrub *sc)
557 {
558 struct xfs_dinode di;
559 int error = 0;
560
561 /*
562 * If sc->ip is NULL, that means that the setup function called
563 * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
564 * and a NULL inode, so flag the corruption error and return.
565 */
566 if (!sc->ip) {
567 xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
568 return 0;
569 }
570
571 /* Scrub the inode core. */
572 xfs_inode_to_disk(sc->ip, &di, 0);
573 xchk_dinode(sc, &di, sc->ip->i_ino);
574 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
575 goto out;
576
577 /*
578 * Look for discrepancies between file's data blocks and the reflink
579 * iflag. We already checked the iflag against the file mode when
580 * we scrubbed the dinode.
581 */
582 if (S_ISREG(VFS_I(sc->ip)->i_mode))
583 xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
584
585 xchk_inode_xref(sc, sc->ip->i_ino, &di);
586 out:
587 return error;
588 }
589