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. |
| 69 | */ |
| 70 | error = xfs_iget(mp, sdc->sc->tp, inum, 0, 0, &ip); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 71 | 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] | 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) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 79 | xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset); |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 80 | xfs_irele(ip); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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 |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 94 | xchk_dir_actor( |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 95 | struct dir_context *dir_iter, |
| 96 | const char *name, |
| 97 | int namelen, |
| 98 | loff_t pos, |
| 99 | u64 ino, |
| 100 | unsigned type) |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 101 | { |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 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; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 109 | |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 110 | sdc = container_of(dir_iter, struct xchk_dir_ctx, dir_iter); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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 | |
Darrick J. Wong | 8ef34723 | 2019-11-05 15:33:56 -0800 | [diff] [blame] | 116 | if (xchk_should_terminate(sdc->sc, &error)) |
| 117 | return error; |
| 118 | |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 119 | /* Does this inode number make sense? */ |
| 120 | if (!xfs_verify_dir_ino(mp, ino)) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 121 | xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 122 | goto out; |
| 123 | } |
| 124 | |
Darrick J. Wong | e5d7d51 | 2019-02-01 09:08:54 -0800 | [diff] [blame] | 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 | |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 134 | xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 135 | offset); |
| 136 | if (ino != ip->i_ino) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 137 | xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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) |
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); |
| 147 | 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] | 148 | xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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); |
Darrick J. Wong | 2e107cf | 2020-03-11 10:37:57 -0700 | [diff] [blame^] | 158 | /* ENOENT means the hash lookup failed and the dir is corrupt */ |
| 159 | if (error == -ENOENT) |
| 160 | error = -EFSCORRUPTED; |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 161 | if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset, |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 162 | &error)) |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 163 | goto out; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 164 | if (lookup_ino != ino) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 165 | xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | /* Verify the file type. This function absorbs error codes. */ |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 170 | error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 171 | if (error) |
| 172 | goto out; |
| 173 | out: |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 174 | /* |
| 175 | * A negative error code returned here is supposed to cause the |
| 176 | * dir_emit caller (xfs_readdir) to abort the directory iteration |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 177 | * and return zero to xchk_directory. |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 178 | */ |
| 179 | if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 180 | return -EFSCORRUPTED; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 181 | return error; |
| 182 | } |
| 183 | |
| 184 | /* Scrub a directory btree record. */ |
| 185 | STATIC int |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 186 | xchk_dir_rec( |
| 187 | struct xchk_da_btree *ds, |
Christoph Hellwig | 649d9d9 | 2019-11-08 14:52:07 -0800 | [diff] [blame] | 188 | int level) |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 189 | { |
Christoph Hellwig | 649d9d9 | 2019-11-08 14:52:07 -0800 | [diff] [blame] | 190 | struct xfs_da_state_blk *blk = &ds->state->path.blk[level]; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 191 | struct xfs_mount *mp = ds->state->mp; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 192 | struct xfs_inode *dp = ds->dargs.dp; |
Christoph Hellwig | d73e1ce | 2019-11-08 15:05:38 -0800 | [diff] [blame] | 193 | struct xfs_da_geometry *geo = mp->m_dir_geo; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 194 | struct xfs_dir2_data_entry *dent; |
| 195 | struct xfs_buf *bp; |
Christoph Hellwig | 649d9d9 | 2019-11-08 14:52:07 -0800 | [diff] [blame] | 196 | struct xfs_dir2_leaf_entry *ent; |
Christoph Hellwig | 5c07212 | 2019-11-08 15:05:36 -0800 | [diff] [blame] | 197 | unsigned int end; |
Christoph Hellwig | 4c037dd | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 198 | unsigned int iter_off; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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; |
Christoph Hellwig | 787b089 | 2019-11-08 14:57:50 -0800 | [diff] [blame] | 206 | struct xfs_dir3_icleaf_hdr hdr; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 207 | unsigned int tag; |
| 208 | int error; |
| 209 | |
Christoph Hellwig | 649d9d9 | 2019-11-08 14:52:07 -0800 | [diff] [blame] | 210 | ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC || |
| 211 | blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 212 | |
Christoph Hellwig | 787b089 | 2019-11-08 14:57:50 -0800 | [diff] [blame] | 213 | xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr); |
| 214 | ent = hdr.ents + blk->index; |
Christoph Hellwig | 649d9d9 | 2019-11-08 14:52:07 -0800 | [diff] [blame] | 215 | |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 216 | /* Check the hash of the entry. */ |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 217 | error = xchk_da_btree_hash(ds, level, &ent->hashval); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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. */ |
Christoph Hellwig | d73e1ce | 2019-11-08 15:05:38 -0800 | [diff] [blame] | 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); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 230 | |
Christoph Hellwig | d73e1ce | 2019-11-08 15:05:38 -0800 | [diff] [blame] | 231 | if (rec_bno >= geo->leafblk) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 232 | xchk_da_set_corrupt(ds, level); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 233 | goto out; |
| 234 | } |
Christoph Hellwig | cd2c9f1 | 2019-11-20 09:46:04 -0800 | [diff] [blame] | 235 | error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno, |
| 236 | XFS_DABUF_MAP_HOLE_OK, &bp); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 237 | 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] | 238 | &error)) |
| 239 | goto out; |
| 240 | if (!bp) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 241 | xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 242 | goto out; |
| 243 | } |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 244 | xchk_buffer_recheck(ds->sc, bp); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 245 | |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 246 | if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 247 | goto out_relse; |
| 248 | |
Christoph Hellwig | 4c037dd | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 249 | dent = bp->b_addr + off; |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 250 | |
| 251 | /* Make sure we got a real directory entry. */ |
Christoph Hellwig | d73e1ce | 2019-11-08 15:05:38 -0800 | [diff] [blame] | 252 | iter_off = geo->data_entry_offset; |
| 253 | end = xfs_dir3_data_end_offset(geo, bp->b_addr); |
Christoph Hellwig | 5c07212 | 2019-11-08 15:05:36 -0800 | [diff] [blame] | 254 | if (!end) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 255 | xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 256 | goto out_relse; |
| 257 | } |
Christoph Hellwig | 4c037dd | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 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; |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 261 | |
Christoph Hellwig | 5c07212 | 2019-11-08 15:05:36 -0800 | [diff] [blame] | 262 | if (iter_off >= end) { |
Christoph Hellwig | 4c037dd | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 263 | xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); |
| 264 | goto out_relse; |
| 265 | } |
| 266 | |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 267 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
Christoph Hellwig | 4c037dd | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 268 | iter_off += be16_to_cpu(dup->length); |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 269 | continue; |
| 270 | } |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 271 | if (dep == dent) |
| 272 | break; |
Christoph Hellwig | fdbb8c5 | 2019-11-08 15:05:37 -0800 | [diff] [blame] | 273 | iter_off += xfs_dir2_data_entsize(mp, dep->namelen); |
Darrick J. Wong | ce92d29 | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* Retrieve the entry, sanity check it, and compare hashes. */ |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 277 | ino = be64_to_cpu(dent->inumber); |
| 278 | hash = be32_to_cpu(ent->hashval); |
Christoph Hellwig | 7e8ae7bd | 2019-11-08 15:05:37 -0800 | [diff] [blame] | 279 | 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] | 280 | if (!xfs_verify_dir_ino(mp, ino) || tag != off) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 281 | xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 282 | if (dent->namelen == 0) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 283 | xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 284 | goto out_relse; |
| 285 | } |
| 286 | calc_hash = xfs_da_hashname(dent->name, dent->namelen); |
| 287 | if (calc_hash != hash) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 288 | xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 289 | |
| 290 | out_relse: |
| 291 | xfs_trans_brelse(ds->dargs.trans, bp); |
| 292 | out: |
| 293 | return error; |
| 294 | } |
| 295 | |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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 |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 302 | xchk_directory_check_free_entry( |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 303 | struct xfs_scrub *sc, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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. */ |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 322 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* Check free space info in a directory data block. */ |
| 326 | STATIC int |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 327 | xchk_directory_data_bestfree( |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 328 | struct xfs_scrub *sc, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 337 | u16 tag; |
| 338 | unsigned int nr_bestfrees = 0; |
| 339 | unsigned int nr_frees = 0; |
| 340 | unsigned int smallest_bestfree; |
| 341 | int newlen; |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 342 | unsigned int offset; |
| 343 | unsigned int end; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 344 | int error; |
| 345 | |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 346 | if (is_block) { |
| 347 | /* dir block format */ |
| 348 | if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET)) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 349 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 350 | error = xfs_dir3_block_read(sc->tp, sc->ip, &bp); |
| 351 | } else { |
| 352 | /* dir data format */ |
Christoph Hellwig | cd2c9f1 | 2019-11-20 09:46:04 -0800 | [diff] [blame] | 353 | 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] | 354 | } |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 355 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 356 | goto out; |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 357 | xchk_buffer_recheck(sc, bp); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 358 | |
| 359 | /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */ |
| 360 | |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 361 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 362 | goto out_buf; |
| 363 | |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 364 | /* Do the bestfrees correspond to actual free space? */ |
Christoph Hellwig | 1848b60 | 2019-11-08 15:05:39 -0800 | [diff] [blame] | 365 | bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 372 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 373 | goto out_buf; |
| 374 | } |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 375 | dup = bp->b_addr + offset; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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) || |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 381 | tag != offset) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 382 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 383 | goto out_buf; |
| 384 | } |
| 385 | |
| 386 | /* bestfree records should be ordered largest to smallest */ |
| 387 | if (smallest_bestfree < be16_to_cpu(dfp->length)) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 388 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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. */ |
Christoph Hellwig | d73e1ce | 2019-11-08 15:05:38 -0800 | [diff] [blame] | 397 | offset = mp->m_dir_geo->data_entry_offset; |
Christoph Hellwig | 5c07212 | 2019-11-08 15:05:36 -0800 | [diff] [blame] | 398 | 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] | 399 | |
| 400 | /* Iterate the entries, stopping when we hit or go past the end. */ |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 401 | while (offset < end) { |
| 402 | dup = bp->b_addr + offset; |
| 403 | |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 404 | /* Skip real entries */ |
| 405 | if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) { |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 406 | struct xfs_dir2_data_entry *dep = bp->b_addr + offset; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 407 | |
Christoph Hellwig | fdbb8c5 | 2019-11-08 15:05:37 -0800 | [diff] [blame] | 408 | newlen = xfs_dir2_data_entsize(mp, dep->namelen); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 409 | if (newlen <= 0) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 410 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 411 | lblk); |
| 412 | goto out_buf; |
| 413 | } |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 414 | offset += newlen; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 415 | continue; |
| 416 | } |
| 417 | |
| 418 | /* Spot check this free entry */ |
| 419 | tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)); |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 420 | if (tag != offset) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 421 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 422 | goto out_buf; |
| 423 | } |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * Either this entry is a bestfree or it's smaller than |
| 427 | * any of the bestfrees. |
| 428 | */ |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 429 | xchk_directory_check_free_entry(sc, lblk, bf, dup); |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 430 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 431 | goto out_buf; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 432 | |
| 433 | /* Move on. */ |
| 434 | newlen = be16_to_cpu(dup->length); |
| 435 | if (newlen <= 0) { |
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 | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 437 | goto out_buf; |
| 438 | } |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 439 | offset += newlen; |
| 440 | if (offset <= end) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 441 | nr_frees++; |
| 442 | } |
| 443 | |
| 444 | /* We're required to fill all the space. */ |
Christoph Hellwig | 4a1a8b2 | 2019-11-08 15:05:33 -0800 | [diff] [blame] | 445 | if (offset != end) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 446 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 447 | |
| 448 | /* Did we see at least as many free slots as there are bestfrees? */ |
| 449 | if (nr_frees < nr_bestfrees) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 450 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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 |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 464 | xchk_directory_check_freesp( |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 465 | struct xfs_scrub *sc, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 466 | xfs_dablk_t lblk, |
| 467 | struct xfs_buf *dbp, |
| 468 | unsigned int len) |
| 469 | { |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 470 | struct xfs_dir2_data_free *dfp; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 471 | |
Christoph Hellwig | 1848b60 | 2019-11-08 15:05:39 -0800 | [diff] [blame] | 472 | dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 473 | |
Darrick J. Wong | 35ce852 | 2017-11-06 11:37:46 -0800 | [diff] [blame] | 474 | if (len != be16_to_cpu(dfp->length)) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 475 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 476 | |
Darrick J. Wong | 35ce852 | 2017-11-06 11:37:46 -0800 | [diff] [blame] | 477 | if (len > 0 && be16_to_cpu(dfp->offset) == 0) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 478 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* Check free space info in a directory leaf1 block. */ |
| 482 | STATIC int |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 483 | xchk_directory_leaf1_bestfree( |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 484 | struct xfs_scrub *sc, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 485 | struct xfs_da_args *args, |
| 486 | xfs_dablk_t lblk) |
| 487 | { |
| 488 | struct xfs_dir3_icleaf_hdr leafhdr; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 489 | struct xfs_dir2_leaf_tail *ltp; |
| 490 | struct xfs_dir2_leaf *leaf; |
| 491 | struct xfs_buf *dbp; |
| 492 | struct xfs_buf *bp; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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. */ |
Christoph Hellwig | c943c0b | 2019-11-20 09:46:03 -0800 | [diff] [blame] | 504 | error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, &bp); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 505 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 506 | goto out; |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 507 | xchk_buffer_recheck(sc, bp); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 508 | |
| 509 | leaf = bp->b_addr; |
Christoph Hellwig | 5184255 | 2019-11-08 14:57:49 -0800 | [diff] [blame] | 510 | 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] | 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)) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 519 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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)) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 527 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 528 | goto out; |
| 529 | } |
| 530 | |
| 531 | /* Is the leaf count even remotely sane? */ |
Christoph Hellwig | 478c783 | 2019-11-08 14:57:51 -0800 | [diff] [blame] | 532 | if (leafhdr.count > geo->leaf_max_ents) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 533 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | /* Leaves and bests don't overlap in leaf format. */ |
Christoph Hellwig | 787b089 | 2019-11-08 14:57:50 -0800 | [diff] [blame] | 538 | if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 539 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 540 | goto out; |
| 541 | } |
| 542 | |
| 543 | /* Check hash value order, count stale entries. */ |
| 544 | for (i = 0; i < leafhdr.count; i++) { |
Christoph Hellwig | 787b089 | 2019-11-08 14:57:50 -0800 | [diff] [blame] | 545 | hash = be32_to_cpu(leafhdr.ents[i].hashval); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 546 | if (i > 0 && lasthash > hash) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 547 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 548 | lasthash = hash; |
Christoph Hellwig | 787b089 | 2019-11-08 14:57:50 -0800 | [diff] [blame] | 549 | if (leafhdr.ents[i].address == |
| 550 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 551 | stale++; |
| 552 | } |
| 553 | if (leafhdr.stale != stale) |
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 | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 555 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 556 | goto out; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 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, |
Christoph Hellwig | cd2c9f1 | 2019-11-20 09:46:04 -0800 | [diff] [blame] | 564 | i * args->geo->fsbcount, 0, &dbp); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 565 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 566 | &error)) |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 567 | break; |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 568 | xchk_directory_check_freesp(sc, lblk, dbp, best); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 569 | xfs_trans_brelse(sc->tp, dbp); |
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 | out: |
| 574 | return error; |
| 575 | } |
| 576 | |
| 577 | /* Check free space info in a directory freespace block. */ |
| 578 | STATIC int |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 579 | xchk_directory_free_bestfree( |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 580 | struct xfs_scrub *sc, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 581 | struct xfs_da_args *args, |
| 582 | xfs_dablk_t lblk) |
| 583 | { |
| 584 | struct xfs_dir3_icfree_hdr freehdr; |
| 585 | struct xfs_buf *dbp; |
| 586 | struct xfs_buf *bp; |
Christoph Hellwig | 88aa5de | 2017-11-06 11:53:58 -0800 | [diff] [blame] | 587 | __u16 best; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 588 | unsigned int stale = 0; |
| 589 | int i; |
| 590 | int error; |
| 591 | |
| 592 | /* Read the free space block */ |
| 593 | error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 594 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 595 | goto out; |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 596 | xchk_buffer_recheck(sc, bp); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 597 | |
| 598 | if (xfs_sb_version_hascrc(&sc->mp->m_sb)) { |
| 599 | struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; |
| 600 | |
| 601 | if (hdr3->pad != cpu_to_be32(0)) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 602 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* Check all the entries. */ |
Christoph Hellwig | 5ba3091 | 2019-11-08 14:57:52 -0800 | [diff] [blame] | 606 | 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] | 607 | for (i = 0; i < freehdr.nvalid; i++) { |
| 608 | best = be16_to_cpu(freehdr.bests[i]); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 609 | if (best == NULLDATAOFF) { |
| 610 | stale++; |
| 611 | continue; |
| 612 | } |
| 613 | error = xfs_dir3_data_read(sc->tp, sc->ip, |
| 614 | (freehdr.firstdb + i) * args->geo->fsbcount, |
Christoph Hellwig | cd2c9f1 | 2019-11-20 09:46:04 -0800 | [diff] [blame] | 615 | 0, &dbp); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 616 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 617 | &error)) |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 618 | break; |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 619 | xchk_directory_check_freesp(sc, lblk, dbp, best); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 620 | xfs_trans_brelse(sc->tp, dbp); |
| 621 | } |
| 622 | |
| 623 | if (freehdr.nused + stale != freehdr.nvalid) |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 624 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 625 | out: |
| 626 | return error; |
| 627 | } |
| 628 | |
| 629 | /* Check free space information in directories. */ |
| 630 | STATIC int |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 631 | xchk_directory_blocks( |
Darrick J. Wong | 1d8a748 | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 632 | struct xfs_scrub *sc) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 633 | { |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 634 | struct xfs_bmbt_irec got; |
| 635 | struct xfs_da_args args; |
| 636 | struct xfs_ifork *ifp; |
| 637 | struct xfs_mount *mp = sc->mp; |
| 638 | xfs_fileoff_t leaf_lblk; |
| 639 | xfs_fileoff_t free_lblk; |
| 640 | xfs_fileoff_t lblk; |
| 641 | struct xfs_iext_cursor icur; |
| 642 | xfs_dablk_t dabno; |
| 643 | bool found; |
| 644 | int is_block = 0; |
| 645 | int error; |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 646 | |
| 647 | /* Ignore local format directories. */ |
| 648 | if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 649 | sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 650 | return 0; |
| 651 | |
| 652 | ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK); |
| 653 | lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET); |
| 654 | leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET); |
| 655 | free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET); |
| 656 | |
| 657 | /* Is this a block dir? */ |
| 658 | args.dp = sc->ip; |
| 659 | args.geo = mp->m_dir_geo; |
| 660 | args.trans = sc->tp; |
| 661 | error = xfs_dir2_isblock(&args, &is_block); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 662 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error)) |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 663 | goto out; |
| 664 | |
| 665 | /* Iterate all the data extents in the directory... */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 666 | found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 667 | while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) { |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 668 | /* Block directories only have a single block at offset 0. */ |
| 669 | if (is_block && |
| 670 | (got.br_startoff > 0 || |
| 671 | got.br_blockcount != args.geo->fsbcount)) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 672 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 673 | got.br_startoff); |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | /* No more data blocks... */ |
| 678 | if (got.br_startoff >= leaf_lblk) |
| 679 | break; |
| 680 | |
| 681 | /* |
| 682 | * Check each data block's bestfree data. |
| 683 | * |
| 684 | * Iterate all the fsbcount-aligned block offsets in |
| 685 | * this directory. The directory block reading code is |
| 686 | * smart enough to do its own bmap lookups to handle |
| 687 | * discontiguous directory blocks. When we're done |
| 688 | * with the extent record, re-query the bmap at the |
| 689 | * next fsbcount-aligned offset to avoid redundant |
| 690 | * block checks. |
| 691 | */ |
| 692 | for (lblk = roundup((xfs_dablk_t)got.br_startoff, |
| 693 | args.geo->fsbcount); |
| 694 | lblk < got.br_startoff + got.br_blockcount; |
| 695 | lblk += args.geo->fsbcount) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 696 | error = xchk_directory_data_bestfree(sc, lblk, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 697 | is_block); |
| 698 | if (error) |
| 699 | goto out; |
| 700 | } |
| 701 | dabno = got.br_startoff + got.br_blockcount; |
| 702 | lblk = roundup(dabno, args.geo->fsbcount); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 703 | found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 707 | goto out; |
| 708 | |
| 709 | /* Look for a leaf1 block, which has free info. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 710 | 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] | 711 | got.br_startoff == leaf_lblk && |
| 712 | got.br_blockcount == args.geo->fsbcount && |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 713 | !xfs_iext_next_extent(ifp, &icur, &got)) { |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 714 | if (is_block) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 715 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 716 | goto out; |
| 717 | } |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 718 | error = xchk_directory_leaf1_bestfree(sc, &args, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 719 | leaf_lblk); |
| 720 | if (error) |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 725 | goto out; |
| 726 | |
| 727 | /* Scan for free blocks */ |
| 728 | lblk = free_lblk; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 729 | found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); |
Darrick J. Wong | 8bc763c | 2018-05-14 06:34:32 -0700 | [diff] [blame] | 730 | while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) { |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Dirs can't have blocks mapped above 2^32. |
| 733 | * Single-block dirs shouldn't even be here. |
| 734 | */ |
| 735 | lblk = got.br_startoff; |
| 736 | if (lblk & ~0xFFFFFFFFULL) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 737 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 738 | goto out; |
| 739 | } |
| 740 | if (is_block) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 741 | xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 742 | goto out; |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Check each dir free block's bestfree data. |
| 747 | * |
| 748 | * Iterate all the fsbcount-aligned block offsets in |
| 749 | * this directory. The directory block reading code is |
| 750 | * smart enough to do its own bmap lookups to handle |
| 751 | * discontiguous directory blocks. When we're done |
| 752 | * with the extent record, re-query the bmap at the |
| 753 | * next fsbcount-aligned offset to avoid redundant |
| 754 | * block checks. |
| 755 | */ |
| 756 | for (lblk = roundup((xfs_dablk_t)got.br_startoff, |
| 757 | args.geo->fsbcount); |
| 758 | lblk < got.br_startoff + got.br_blockcount; |
| 759 | lblk += args.geo->fsbcount) { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 760 | error = xchk_directory_free_bestfree(sc, &args, |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 761 | lblk); |
| 762 | if (error) |
| 763 | goto out; |
| 764 | } |
| 765 | dabno = got.br_startoff + got.br_blockcount; |
| 766 | lblk = roundup(dabno, args.geo->fsbcount); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 767 | found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 768 | } |
| 769 | out: |
| 770 | return error; |
| 771 | } |
| 772 | |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 773 | /* Scrub a whole directory. */ |
| 774 | int |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 775 | xchk_directory( |
Darrick J. Wong | 1d8a748 | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 776 | struct xfs_scrub *sc) |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 777 | { |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 778 | struct xchk_dir_ctx sdc = { |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 779 | .dir_iter.actor = xchk_dir_actor, |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 780 | .dir_iter.pos = 0, |
| 781 | .sc = sc, |
| 782 | }; |
Darrick J. Wong | 032d91f | 2018-07-19 12:29:12 -0700 | [diff] [blame] | 783 | size_t bufsize; |
| 784 | loff_t oldpos; |
| 785 | int error = 0; |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 786 | |
| 787 | if (!S_ISDIR(VFS_I(sc->ip)->i_mode)) |
| 788 | return -ENOENT; |
| 789 | |
| 790 | /* Plausible size? */ |
| 791 | 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] | 792 | xchk_ino_set_corrupt(sc, sc->ip->i_ino); |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | /* Check directory tree structure */ |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 797 | 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] | 798 | if (error) |
| 799 | return error; |
| 800 | |
| 801 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 802 | return error; |
| 803 | |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 804 | /* Check the freespace. */ |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 805 | error = xchk_directory_blocks(sc); |
Darrick J. Wong | df48196 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 806 | if (error) |
| 807 | return error; |
| 808 | |
| 809 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 810 | return error; |
| 811 | |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 812 | /* |
| 813 | * Check that every dirent we see can also be looked up by hash. |
| 814 | * Userspace usually asks for a 32k buffer, so we will too. |
| 815 | */ |
| 816 | bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, |
| 817 | sc->ip->i_d.di_size); |
| 818 | |
| 819 | /* |
| 820 | * Look up every name in this directory by hash. |
| 821 | * |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 822 | * Use the xfs_readdir function to call xchk_dir_actor on |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 823 | * every directory entry in this directory. In _actor, we check |
| 824 | * the name, inode number, and ftype (if applicable) of the |
| 825 | * entry. xfs_readdir uses the VFS filldir functions to provide |
| 826 | * iteration context. |
| 827 | * |
| 828 | * The VFS grabs a read or write lock via i_rwsem before it reads |
| 829 | * or writes to a directory. If we've gotten this far we've |
| 830 | * already obtained IOLOCK_EXCL, which (since 4.10) is the same as |
| 831 | * getting a write lock on i_rwsem. Therefore, it is safe for us |
| 832 | * to drop the ILOCK here in order to reuse the _readdir and |
| 833 | * _dir_lookup routines, which do their own ILOCK locking. |
| 834 | */ |
| 835 | oldpos = 0; |
| 836 | sc->ilock_flags &= ~XFS_ILOCK_EXCL; |
| 837 | xfs_iunlock(sc->ip, XFS_ILOCK_EXCL); |
| 838 | while (true) { |
| 839 | error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize); |
Darrick J. Wong | c517b3a | 2018-07-19 12:29:11 -0700 | [diff] [blame] | 840 | if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, |
Darrick J. Wong | a5c46e5 | 2017-10-17 21:37:44 -0700 | [diff] [blame] | 841 | &error)) |
| 842 | goto out; |
| 843 | if (oldpos == sdc.dir_iter.pos) |
| 844 | break; |
| 845 | oldpos = sdc.dir_iter.pos; |
| 846 | } |
| 847 | |
| 848 | out: |
| 849 | return error; |
| 850 | } |