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