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