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