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