1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved. 4 * Author: Darrick J. Wong <djwong@kernel.org> 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_log_format.h" 13 #include "xfs_trans.h" 14 #include "xfs_inode.h" 15 #include "xfs_icache.h" 16 #include "xfs_dir2.h" 17 #include "xfs_dir2_priv.h" 18 #include "xfs_health.h" 19 #include "scrub/scrub.h" 20 #include "scrub/common.h" 21 #include "scrub/dabtree.h" 22 #include "scrub/readdir.h" 23 #include "scrub/health.h" 24 #include "scrub/repair.h" 25 26 /* Set us up to scrub directories. */ 27 int 28 xchk_setup_directory( 29 struct xfs_scrub *sc) 30 { 31 int error; 32 33 if (xchk_could_repair(sc)) { 34 error = xrep_setup_directory(sc); 35 if (error) 36 return error; 37 } 38 39 return xchk_setup_inode_contents(sc, 0); 40 } 41 42 /* Directories */ 43 44 /* Scrub a directory entry. */ 45 46 /* Check that an inode's mode matches a given XFS_DIR3_FT_* type. */ 47 STATIC void 48 xchk_dir_check_ftype( 49 struct xfs_scrub *sc, 50 xfs_fileoff_t offset, 51 struct xfs_inode *ip, 52 int ftype) 53 { 54 struct xfs_mount *mp = sc->mp; 55 56 if (!xfs_has_ftype(mp)) { 57 if (ftype != XFS_DIR3_FT_UNKNOWN && ftype != XFS_DIR3_FT_DIR) 58 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 59 return; 60 } 61 62 if (xfs_mode_to_ftype(VFS_I(ip)->i_mode) != ftype) 63 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 64 } 65 66 /* 67 * Scrub a single directory entry. 68 * 69 * Check the inode number to make sure it's sane, then we check that we can 70 * look up this filename. Finally, we check the ftype. 71 */ 72 STATIC int 73 xchk_dir_actor( 74 struct xfs_scrub *sc, 75 struct xfs_inode *dp, 76 xfs_dir2_dataptr_t dapos, 77 const struct xfs_name *name, 78 xfs_ino_t ino, 79 void *priv) 80 { 81 struct xfs_mount *mp = dp->i_mount; 82 struct xfs_inode *ip; 83 xfs_ino_t lookup_ino; 84 xfs_dablk_t offset; 85 int error = 0; 86 87 offset = xfs_dir2_db_to_da(mp->m_dir_geo, 88 xfs_dir2_dataptr_to_db(mp->m_dir_geo, dapos)); 89 90 if (xchk_should_terminate(sc, &error)) 91 return error; 92 93 /* Does this inode number make sense? */ 94 if (!xfs_verify_dir_ino(mp, ino)) { 95 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 96 return -ECANCELED; 97 } 98 99 /* Does this name make sense? */ 100 if (!xfs_dir2_namecheck(name->name, name->len)) { 101 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 102 return -ECANCELED; 103 } 104 105 if (xfs_dir2_samename(name, &xfs_name_dot)) { 106 /* If this is "." then check that the inum matches the dir. */ 107 if (ino != dp->i_ino) 108 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 109 } else if (xfs_dir2_samename(name, &xfs_name_dotdot)) { 110 /* 111 * If this is ".." in the root inode, check that the inum 112 * matches this dir. 113 */ 114 if (dp->i_ino == mp->m_sb.sb_rootino && ino != dp->i_ino) 115 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 116 } 117 118 /* Verify that we can look up this name by hash. */ 119 error = xchk_dir_lookup(sc, dp, name, &lookup_ino); 120 /* ENOENT means the hash lookup failed and the dir is corrupt */ 121 if (error == -ENOENT) 122 error = -EFSCORRUPTED; 123 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error)) 124 goto out; 125 if (lookup_ino != ino) { 126 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset); 127 return -ECANCELED; 128 } 129 130 /* 131 * Grab the inode pointed to by the dirent. We release the inode 132 * before we cancel the scrub transaction. 133 * 134 * If _iget returns -EINVAL or -ENOENT then the child inode number is 135 * garbage and the directory is corrupt. If the _iget returns 136 * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a 137 * cross referencing error. Any other error is an operational error. 138 */ 139 error = xchk_iget(sc, ino, &ip); 140 if (error == -EINVAL || error == -ENOENT) { 141 error = -EFSCORRUPTED; 142 xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error); 143 goto out; 144 } 145 if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset, &error)) 146 goto out; 147 148 xchk_dir_check_ftype(sc, offset, ip, name->type); 149 xchk_irele(sc, ip); 150 out: 151 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 152 return -ECANCELED; 153 return error; 154 } 155 156 /* Scrub a directory btree record. */ 157 STATIC int 158 xchk_dir_rec( 159 struct xchk_da_btree *ds, 160 int level) 161 { 162 struct xfs_name dname = { }; 163 struct xfs_da_state_blk *blk = &ds->state->path.blk[level]; 164 struct xfs_mount *mp = ds->state->mp; 165 struct xfs_inode *dp = ds->dargs.dp; 166 struct xfs_da_geometry *geo = mp->m_dir_geo; 167 struct xfs_dir2_data_entry *dent; 168 struct xfs_buf *bp; 169 struct xfs_dir2_leaf_entry *ent; 170 unsigned int end; 171 unsigned int iter_off; 172 xfs_ino_t ino; 173 xfs_dablk_t rec_bno; 174 xfs_dir2_db_t db; 175 xfs_dir2_data_aoff_t off; 176 xfs_dir2_dataptr_t ptr; 177 xfs_dahash_t calc_hash; 178 xfs_dahash_t hash; 179 struct xfs_dir3_icleaf_hdr hdr; 180 unsigned int tag; 181 int error; 182 183 ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC || 184 blk->magic == XFS_DIR2_LEAFN_MAGIC); 185 186 xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr); 187 ent = hdr.ents + blk->index; 188 189 /* Check the hash of the entry. */ 190 error = xchk_da_btree_hash(ds, level, &ent->hashval); 191 if (error) 192 goto out; 193 194 /* Valid hash pointer? */ 195 ptr = be32_to_cpu(ent->address); 196 if (ptr == 0) 197 return 0; 198 199 /* Find the directory entry's location. */ 200 db = xfs_dir2_dataptr_to_db(geo, ptr); 201 off = xfs_dir2_dataptr_to_off(geo, ptr); 202 rec_bno = xfs_dir2_db_to_da(geo, db); 203 204 if (rec_bno >= geo->leafblk) { 205 xchk_da_set_corrupt(ds, level); 206 goto out; 207 } 208 error = xfs_dir3_data_read(ds->dargs.trans, dp, ds->dargs.owner, 209 rec_bno, XFS_DABUF_MAP_HOLE_OK, &bp); 210 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno, 211 &error)) 212 goto out; 213 if (!bp) { 214 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); 215 goto out; 216 } 217 xchk_buffer_recheck(ds->sc, bp); 218 219 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 220 goto out_relse; 221 222 dent = bp->b_addr + off; 223 224 /* Make sure we got a real directory entry. */ 225 iter_off = geo->data_entry_offset; 226 end = xfs_dir3_data_end_offset(geo, bp->b_addr); 227 if (!end) { 228 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); 229 goto out_relse; 230 } 231 for (;;) { 232 struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off; 233 struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off; 234 235 if (iter_off >= end) { 236 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); 237 goto out_relse; 238 } 239 240 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { 241 iter_off += be16_to_cpu(dup->length); 242 continue; 243 } 244 if (dep == dent) 245 break; 246 iter_off += xfs_dir2_data_entsize(mp, dep->namelen); 247 } 248 249 /* Retrieve the entry, sanity check it, and compare hashes. */ 250 ino = be64_to_cpu(dent->inumber); 251 hash = be32_to_cpu(ent->hashval); 252 tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent)); 253 if (!xfs_verify_dir_ino(mp, ino) || tag != off) 254 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); 255 if (dent->namelen == 0) { 256 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); 257 goto out_relse; 258 } 259 260 /* Does the directory hash match? */ 261 dname.name = dent->name; 262 dname.len = dent->namelen; 263 calc_hash = xfs_dir2_hashname(mp, &dname); 264 if (calc_hash != hash) 265 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); 266 267 out_relse: 268 xfs_trans_brelse(ds->dargs.trans, bp); 269 out: 270 return error; 271 } 272 273 /* 274 * Is this unused entry either in the bestfree or smaller than all of 275 * them? We've already checked that the bestfrees are sorted longest to 276 * shortest, and that there aren't any bogus entries. 277 */ 278 STATIC void 279 xchk_directory_check_free_entry( 280 struct xfs_scrub *sc, 281 xfs_dablk_t lblk, 282 struct xfs_dir2_data_free *bf, 283 struct xfs_dir2_data_unused *dup) 284 { 285 struct xfs_dir2_data_free *dfp; 286 unsigned int dup_length; 287 288 dup_length = be16_to_cpu(dup->length); 289 290 /* Unused entry is shorter than any of the bestfrees */ 291 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length)) 292 return; 293 294 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--) 295 if (dup_length == be16_to_cpu(dfp->length)) 296 return; 297 298 /* Unused entry should be in the bestfrees but wasn't found. */ 299 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 300 } 301 302 /* Check free space info in a directory data block. */ 303 STATIC int 304 xchk_directory_data_bestfree( 305 struct xfs_scrub *sc, 306 xfs_dablk_t lblk, 307 bool is_block) 308 { 309 struct xfs_dir2_data_unused *dup; 310 struct xfs_dir2_data_free *dfp; 311 struct xfs_buf *bp; 312 struct xfs_dir2_data_free *bf; 313 struct xfs_mount *mp = sc->mp; 314 u16 tag; 315 unsigned int nr_bestfrees = 0; 316 unsigned int nr_frees = 0; 317 unsigned int smallest_bestfree; 318 int newlen; 319 unsigned int offset; 320 unsigned int end; 321 int error; 322 323 if (is_block) { 324 /* dir block format */ 325 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET)) 326 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 327 error = xfs_dir3_block_read(sc->tp, sc->ip, sc->ip->i_ino, &bp); 328 } else { 329 /* dir data format */ 330 error = xfs_dir3_data_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, 331 0, &bp); 332 } 333 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) 334 goto out; 335 xchk_buffer_recheck(sc, bp); 336 337 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */ 338 339 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 340 goto out_buf; 341 342 /* Do the bestfrees correspond to actual free space? */ 343 bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr); 344 smallest_bestfree = UINT_MAX; 345 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) { 346 offset = be16_to_cpu(dfp->offset); 347 if (offset == 0) 348 continue; 349 if (offset >= mp->m_dir_geo->blksize) { 350 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 351 goto out_buf; 352 } 353 dup = bp->b_addr + offset; 354 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)); 355 356 /* bestfree doesn't match the entry it points at? */ 357 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) || 358 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) || 359 tag != offset) { 360 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 361 goto out_buf; 362 } 363 364 /* bestfree records should be ordered largest to smallest */ 365 if (smallest_bestfree < be16_to_cpu(dfp->length)) { 366 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 367 goto out_buf; 368 } 369 370 smallest_bestfree = be16_to_cpu(dfp->length); 371 nr_bestfrees++; 372 } 373 374 /* Make sure the bestfrees are actually the best free spaces. */ 375 offset = mp->m_dir_geo->data_entry_offset; 376 end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr); 377 378 /* Iterate the entries, stopping when we hit or go past the end. */ 379 while (offset < end) { 380 dup = bp->b_addr + offset; 381 382 /* Skip real entries */ 383 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) { 384 struct xfs_dir2_data_entry *dep = bp->b_addr + offset; 385 386 newlen = xfs_dir2_data_entsize(mp, dep->namelen); 387 if (newlen <= 0) { 388 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 389 lblk); 390 goto out_buf; 391 } 392 offset += newlen; 393 continue; 394 } 395 396 /* Spot check this free entry */ 397 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)); 398 if (tag != offset) { 399 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 400 goto out_buf; 401 } 402 403 /* 404 * Either this entry is a bestfree or it's smaller than 405 * any of the bestfrees. 406 */ 407 xchk_directory_check_free_entry(sc, lblk, bf, dup); 408 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 409 goto out_buf; 410 411 /* Move on. */ 412 newlen = be16_to_cpu(dup->length); 413 if (newlen <= 0) { 414 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 415 goto out_buf; 416 } 417 offset += newlen; 418 if (offset <= end) 419 nr_frees++; 420 } 421 422 /* We're required to fill all the space. */ 423 if (offset != end) 424 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 425 426 /* Did we see at least as many free slots as there are bestfrees? */ 427 if (nr_frees < nr_bestfrees) 428 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 429 out_buf: 430 xfs_trans_brelse(sc->tp, bp); 431 out: 432 return error; 433 } 434 435 /* 436 * Does the free space length in the free space index block ($len) match 437 * the longest length in the directory data block's bestfree array? 438 * Assume that we've already checked that the data block's bestfree 439 * array is in order. 440 */ 441 STATIC void 442 xchk_directory_check_freesp( 443 struct xfs_scrub *sc, 444 xfs_dablk_t lblk, 445 struct xfs_buf *dbp, 446 unsigned int len) 447 { 448 struct xfs_dir2_data_free *dfp; 449 450 dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr); 451 452 if (len != be16_to_cpu(dfp->length)) 453 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 454 455 if (len > 0 && be16_to_cpu(dfp->offset) == 0) 456 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 457 } 458 459 /* Check free space info in a directory leaf1 block. */ 460 STATIC int 461 xchk_directory_leaf1_bestfree( 462 struct xfs_scrub *sc, 463 struct xfs_da_args *args, 464 xfs_dir2_db_t last_data_db, 465 xfs_dablk_t lblk) 466 { 467 struct xfs_dir3_icleaf_hdr leafhdr; 468 struct xfs_dir2_leaf_tail *ltp; 469 struct xfs_dir2_leaf *leaf; 470 struct xfs_buf *dbp; 471 struct xfs_buf *bp; 472 struct xfs_da_geometry *geo = sc->mp->m_dir_geo; 473 __be16 *bestp; 474 __u16 best; 475 __u32 hash; 476 __u32 lasthash = 0; 477 __u32 bestcount; 478 unsigned int stale = 0; 479 int i; 480 int error; 481 482 /* Read the free space block. */ 483 error = xfs_dir3_leaf_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, &bp); 484 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) 485 return error; 486 xchk_buffer_recheck(sc, bp); 487 488 leaf = bp->b_addr; 489 xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf); 490 ltp = xfs_dir2_leaf_tail_p(geo, leaf); 491 bestcount = be32_to_cpu(ltp->bestcount); 492 bestp = xfs_dir2_leaf_bests_p(ltp); 493 494 if (xfs_has_crc(sc->mp)) { 495 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr; 496 497 if (hdr3->pad != cpu_to_be32(0)) 498 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 499 } 500 501 /* 502 * There must be enough bestfree slots to cover all the directory data 503 * blocks that we scanned. It is possible for there to be a hole 504 * between the last data block and i_disk_size. This seems like an 505 * oversight to the scrub author, but as we have been writing out 506 * directories like this (and xfs_repair doesn't mind them) for years, 507 * that's what we have to check. 508 */ 509 if (bestcount != last_data_db + 1) { 510 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 511 goto out; 512 } 513 514 /* Is the leaf count even remotely sane? */ 515 if (leafhdr.count > geo->leaf_max_ents) { 516 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 517 goto out; 518 } 519 520 /* Leaves and bests don't overlap in leaf format. */ 521 if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) { 522 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 523 goto out; 524 } 525 526 /* Check hash value order, count stale entries. */ 527 for (i = 0; i < leafhdr.count; i++) { 528 hash = be32_to_cpu(leafhdr.ents[i].hashval); 529 if (i > 0 && lasthash > hash) 530 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 531 lasthash = hash; 532 if (leafhdr.ents[i].address == 533 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 534 stale++; 535 } 536 if (leafhdr.stale != stale) 537 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 538 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 539 goto out; 540 541 /* Check all the bestfree entries. */ 542 for (i = 0; i < bestcount; i++, bestp++) { 543 best = be16_to_cpu(*bestp); 544 error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner, 545 xfs_dir2_db_to_da(args->geo, i), 546 XFS_DABUF_MAP_HOLE_OK, &dbp); 547 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, 548 &error)) 549 break; 550 551 if (!dbp) { 552 if (best != NULLDATAOFF) { 553 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 554 lblk); 555 break; 556 } 557 continue; 558 } 559 560 if (best == NULLDATAOFF) 561 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 562 else 563 xchk_directory_check_freesp(sc, lblk, dbp, best); 564 xfs_trans_brelse(sc->tp, dbp); 565 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 566 break; 567 } 568 out: 569 xfs_trans_brelse(sc->tp, bp); 570 return error; 571 } 572 573 /* Check free space info in a directory freespace block. */ 574 STATIC int 575 xchk_directory_free_bestfree( 576 struct xfs_scrub *sc, 577 struct xfs_da_args *args, 578 xfs_dablk_t lblk) 579 { 580 struct xfs_dir3_icfree_hdr freehdr; 581 struct xfs_buf *dbp; 582 struct xfs_buf *bp; 583 __u16 best; 584 unsigned int stale = 0; 585 int i; 586 int error; 587 588 /* Read the free space block */ 589 error = xfs_dir2_free_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, &bp); 590 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) 591 return error; 592 xchk_buffer_recheck(sc, bp); 593 594 if (xfs_has_crc(sc->mp)) { 595 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; 596 597 if (hdr3->pad != cpu_to_be32(0)) 598 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 599 } 600 601 /* Check all the entries. */ 602 xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr); 603 for (i = 0; i < freehdr.nvalid; i++) { 604 best = be16_to_cpu(freehdr.bests[i]); 605 if (best == NULLDATAOFF) { 606 stale++; 607 continue; 608 } 609 error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner, 610 (freehdr.firstdb + i) * args->geo->fsbcount, 611 0, &dbp); 612 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, 613 &error)) 614 goto out; 615 xchk_directory_check_freesp(sc, lblk, dbp, best); 616 xfs_trans_brelse(sc->tp, dbp); 617 } 618 619 if (freehdr.nused + stale != freehdr.nvalid) 620 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 621 out: 622 xfs_trans_brelse(sc->tp, bp); 623 return error; 624 } 625 626 /* Check free space information in directories. */ 627 STATIC int 628 xchk_directory_blocks( 629 struct xfs_scrub *sc) 630 { 631 struct xfs_bmbt_irec got; 632 struct xfs_da_args args = { 633 .dp = sc->ip, 634 .whichfork = XFS_DATA_FORK, 635 .geo = sc->mp->m_dir_geo, 636 .trans = sc->tp, 637 .owner = sc->ip->i_ino, 638 }; 639 struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK); 640 struct xfs_mount *mp = sc->mp; 641 xfs_fileoff_t leaf_lblk; 642 xfs_fileoff_t free_lblk; 643 xfs_fileoff_t lblk; 644 struct xfs_iext_cursor icur; 645 xfs_dablk_t dabno; 646 xfs_dir2_db_t last_data_db = 0; 647 bool found; 648 bool is_block = false; 649 int error; 650 651 /* Ignore local format directories. */ 652 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS && 653 ifp->if_format != XFS_DINODE_FMT_BTREE) 654 return 0; 655 656 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET); 657 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET); 658 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET); 659 660 /* Is this a block dir? */ 661 error = xfs_dir2_isblock(&args, &is_block); 662 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) 663 goto out; 664 665 /* Iterate all the data extents in the directory... */ 666 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); 667 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) { 668 /* No more data blocks... */ 669 if (got.br_startoff >= leaf_lblk) 670 break; 671 672 /* 673 * Check each data block's bestfree data. 674 * 675 * Iterate all the fsbcount-aligned block offsets in 676 * this directory. The directory block reading code is 677 * smart enough to do its own bmap lookups to handle 678 * discontiguous directory blocks. When we're done 679 * with the extent record, re-query the bmap at the 680 * next fsbcount-aligned offset to avoid redundant 681 * block checks. 682 */ 683 for (lblk = roundup((xfs_dablk_t)got.br_startoff, 684 args.geo->fsbcount); 685 lblk < got.br_startoff + got.br_blockcount; 686 lblk += args.geo->fsbcount) { 687 last_data_db = xfs_dir2_da_to_db(args.geo, lblk); 688 error = xchk_directory_data_bestfree(sc, lblk, 689 is_block); 690 if (error) 691 goto out; 692 } 693 dabno = got.br_startoff + got.br_blockcount; 694 lblk = roundup(dabno, args.geo->fsbcount); 695 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); 696 } 697 698 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 699 goto out; 700 701 /* Look for a leaf1 block, which has free info. */ 702 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) && 703 got.br_startoff == leaf_lblk && 704 got.br_blockcount == args.geo->fsbcount && 705 !xfs_iext_next_extent(ifp, &icur, &got)) { 706 if (is_block) { 707 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 708 goto out; 709 } 710 error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db, 711 leaf_lblk); 712 if (error) 713 goto out; 714 } 715 716 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 717 goto out; 718 719 /* Scan for free blocks */ 720 lblk = free_lblk; 721 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); 722 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) { 723 /* 724 * Dirs can't have blocks mapped above 2^32. 725 * Single-block dirs shouldn't even be here. 726 */ 727 lblk = got.br_startoff; 728 if (lblk & ~0xFFFFFFFFULL) { 729 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 730 goto out; 731 } 732 if (is_block) { 733 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); 734 goto out; 735 } 736 737 /* 738 * Check each dir free block's bestfree data. 739 * 740 * Iterate all the fsbcount-aligned block offsets in 741 * this directory. The directory block reading code is 742 * smart enough to do its own bmap lookups to handle 743 * discontiguous directory blocks. When we're done 744 * with the extent record, re-query the bmap at the 745 * next fsbcount-aligned offset to avoid redundant 746 * block checks. 747 */ 748 for (lblk = roundup((xfs_dablk_t)got.br_startoff, 749 args.geo->fsbcount); 750 lblk < got.br_startoff + got.br_blockcount; 751 lblk += args.geo->fsbcount) { 752 error = xchk_directory_free_bestfree(sc, &args, 753 lblk); 754 if (error) 755 goto out; 756 } 757 dabno = got.br_startoff + got.br_blockcount; 758 lblk = roundup(dabno, args.geo->fsbcount); 759 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); 760 } 761 out: 762 return error; 763 } 764 765 /* Scrub a whole directory. */ 766 int 767 xchk_directory( 768 struct xfs_scrub *sc) 769 { 770 int error; 771 772 if (!S_ISDIR(VFS_I(sc->ip)->i_mode)) 773 return -ENOENT; 774 775 if (xchk_file_looks_zapped(sc, XFS_SICK_INO_DIR_ZAPPED)) { 776 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0); 777 return 0; 778 } 779 780 /* Plausible size? */ 781 if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) { 782 xchk_ino_set_corrupt(sc, sc->ip->i_ino); 783 return 0; 784 } 785 786 /* Check directory tree structure */ 787 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL); 788 if (error) 789 return error; 790 791 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 792 return 0; 793 794 /* Check the freespace. */ 795 error = xchk_directory_blocks(sc); 796 if (error) 797 return error; 798 799 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 800 return 0; 801 802 /* Look up every name in this directory by hash. */ 803 error = xchk_dir_walk(sc, sc->ip, xchk_dir_actor, NULL); 804 if (error && error != -ECANCELED) 805 return error; 806 807 /* If the dir is clean, it is clearly not zapped. */ 808 xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_DIR_ZAPPED); 809 return 0; 810 } 811 812 /* 813 * Decide if this directory has been zapped to satisfy the inode and ifork 814 * verifiers. Checking and repairing should be postponed until the directory 815 * is fixed. 816 */ 817 bool 818 xchk_dir_looks_zapped( 819 struct xfs_inode *dp) 820 { 821 /* Repair zapped this dir's data fork a short time ago */ 822 if (xfs_ifork_zapped(dp, XFS_DATA_FORK)) 823 return true; 824 825 /* 826 * If the dinode repair found a bad data fork, it will reset the fork 827 * to extents format with zero records and wait for the bmapbtd 828 * scrubber to reconstruct the block mappings. Directories always 829 * contain some content, so this is a clear sign of a zapped directory. 830 * The state checked by xfs_ifork_zapped is not persisted, so this is 831 * the secondary strategy if repairs are interrupted by a crash or an 832 * unmount. 833 */ 834 return dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS && 835 dp->i_df.if_nextents == 0; 836 } 837