blob: 266da4e4bde6d0ac6dfc62245ee8534543dda8ce [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wonga5c46e52017-10-17 21:37:44 -07002/*
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
Darrick J. Wonga5c46e52017-10-17 21:37:44 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wonga5c46e52017-10-17 21:37:44 -07005 */
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. Wonga5c46e52017-10-17 21:37:44 -070012#include "xfs_log_format.h"
13#include "xfs_trans.h"
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070014#include "xfs_inode.h"
15#include "xfs_icache.h"
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070016#include "xfs_dir2.h"
17#include "xfs_dir2_priv.h"
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070018#include "scrub/scrub.h"
19#include "scrub/common.h"
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070020#include "scrub/dabtree.h"
21
22/* Set us up to scrub directories. */
23int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070024xchk_setup_directory(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070025 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -070026 struct xfs_inode *ip)
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070027{
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070028 return xchk_setup_inode_contents(sc, ip, 0);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070029}
30
31/* Directories */
32
33/* Scrub a directory entry. */
34
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070035struct xchk_dir_ctx {
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070036 /* VFS fill-directory iterator */
Darrick J. Wong032d91f2018-07-19 12:29:12 -070037 struct dir_context dir_iter;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070038
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070039 struct xfs_scrub *sc;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070040};
41
42/* Check that an inode's mode matches a given DT_ type. */
43STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070044xchk_dir_check_ftype(
Darrick J. Wong032d91f2018-07-19 12:29:12 -070045 struct xchk_dir_ctx *sdc,
46 xfs_fileoff_t offset,
47 xfs_ino_t inum,
48 int dtype)
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070049{
Darrick J. Wong032d91f2018-07-19 12:29:12 -070050 struct xfs_mount *mp = sdc->sc->mp;
51 struct xfs_inode *ip;
52 int ino_dtype;
53 int error = 0;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070054
55 if (!xfs_sb_version_hasftype(&mp->m_sb)) {
56 if (dtype != DT_UNKNOWN && dtype != DT_DIR)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070057 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070058 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. Wongc517b3a2018-07-19 12:29:11 -070071 if (!xchk_fblock_xref_process_error(sdc->sc, XFS_DATA_FORK, offset,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070072 &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. Wongc517b3a2018-07-19 12:29:11 -070079 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
Darrick J. Wong44a87362018-07-25 12:52:32 -070080 xfs_irele(ip);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -070081out:
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 */
93STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070094xchk_dir_actor(
Darrick J. Wong032d91f2018-07-19 12:29:12 -070095 struct dir_context *dir_iter,
96 const char *name,
97 int namelen,
98 loff_t pos,
99 u64 ino,
100 unsigned type)
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700101{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700102 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. Wonga5c46e52017-10-17 21:37:44 -0700109
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700110 sdc = container_of(dir_iter, struct xchk_dir_ctx, dir_iter);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700111 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. Wong8ef347232019-11-05 15:33:56 -0800116 if (xchk_should_terminate(sdc->sc, &error))
117 return error;
118
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700119 /* Does this inode number make sense? */
120 if (!xfs_verify_dir_ino(mp, ino)) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700121 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700122 goto out;
123 }
124
Darrick J. Wonge5d7d512019-02-01 09:08:54 -0800125 /* 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. Wonga5c46e52017-10-17 21:37:44 -0700131 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. Wongc517b3a2018-07-19 12:29:11 -0700134 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700135 offset);
136 if (ino != ip->i_ino)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700137 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700138 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. Wongc517b3a2018-07-19 12:29:11 -0700145 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700146 offset);
147 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700148 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700149 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. Wongc517b3a2018-07-19 12:29:11 -0700158 if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700159 &error))
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700160 goto out;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700161 if (lookup_ino != ino) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700162 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700163 goto out;
164 }
165
166 /* Verify the file type. This function absorbs error codes. */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700167 error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700168 if (error)
169 goto out;
170out:
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700171 /*
172 * A negative error code returned here is supposed to cause the
173 * dir_emit caller (xfs_readdir) to abort the directory iteration
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700174 * and return zero to xchk_directory.
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700175 */
176 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
177 return -EFSCORRUPTED;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700178 return error;
179}
180
181/* Scrub a directory btree record. */
182STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700183xchk_dir_rec(
184 struct xchk_da_btree *ds,
Christoph Hellwig649d9d92019-11-08 14:52:07 -0800185 int level)
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700186{
Christoph Hellwig649d9d92019-11-08 14:52:07 -0800187 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700188 struct xfs_mount *mp = ds->state->mp;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700189 struct xfs_inode *dp = ds->dargs.dp;
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800190 struct xfs_da_geometry *geo = mp->m_dir_geo;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700191 struct xfs_dir2_data_entry *dent;
192 struct xfs_buf *bp;
Christoph Hellwig649d9d92019-11-08 14:52:07 -0800193 struct xfs_dir2_leaf_entry *ent;
Christoph Hellwig5c072122019-11-08 15:05:36 -0800194 unsigned int end;
Christoph Hellwig4c037dd2019-11-08 15:05:33 -0800195 unsigned int iter_off;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700196 xfs_ino_t ino;
197 xfs_dablk_t rec_bno;
198 xfs_dir2_db_t db;
199 xfs_dir2_data_aoff_t off;
200 xfs_dir2_dataptr_t ptr;
201 xfs_dahash_t calc_hash;
202 xfs_dahash_t hash;
Christoph Hellwig787b0892019-11-08 14:57:50 -0800203 struct xfs_dir3_icleaf_hdr hdr;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700204 unsigned int tag;
205 int error;
206
Christoph Hellwig649d9d92019-11-08 14:52:07 -0800207 ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
208 blk->magic == XFS_DIR2_LEAFN_MAGIC);
209
Christoph Hellwig787b0892019-11-08 14:57:50 -0800210 xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
211 ent = hdr.ents + blk->index;
Christoph Hellwig649d9d92019-11-08 14:52:07 -0800212
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700213 /* Check the hash of the entry. */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700214 error = xchk_da_btree_hash(ds, level, &ent->hashval);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700215 if (error)
216 goto out;
217
218 /* Valid hash pointer? */
219 ptr = be32_to_cpu(ent->address);
220 if (ptr == 0)
221 return 0;
222
223 /* Find the directory entry's location. */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800224 db = xfs_dir2_dataptr_to_db(geo, ptr);
225 off = xfs_dir2_dataptr_to_off(geo, ptr);
226 rec_bno = xfs_dir2_db_to_da(geo, db);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700227
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800228 if (rec_bno >= geo->leafblk) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700229 xchk_da_set_corrupt(ds, level);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700230 goto out;
231 }
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800232 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno,
233 XFS_DABUF_MAP_HOLE_OK, &bp);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700234 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700235 &error))
236 goto out;
237 if (!bp) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700238 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700239 goto out;
240 }
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700241 xchk_buffer_recheck(ds->sc, bp);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700242
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700243 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
244 goto out_relse;
245
Christoph Hellwig4c037dd2019-11-08 15:05:33 -0800246 dent = bp->b_addr + off;
Darrick J. Wongce92d292018-01-16 18:54:12 -0800247
248 /* Make sure we got a real directory entry. */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800249 iter_off = geo->data_entry_offset;
250 end = xfs_dir3_data_end_offset(geo, bp->b_addr);
Christoph Hellwig5c072122019-11-08 15:05:36 -0800251 if (!end) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700252 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
Darrick J. Wongce92d292018-01-16 18:54:12 -0800253 goto out_relse;
254 }
Christoph Hellwig4c037dd2019-11-08 15:05:33 -0800255 for (;;) {
256 struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
257 struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
Darrick J. Wongce92d292018-01-16 18:54:12 -0800258
Christoph Hellwig5c072122019-11-08 15:05:36 -0800259 if (iter_off >= end) {
Christoph Hellwig4c037dd2019-11-08 15:05:33 -0800260 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
261 goto out_relse;
262 }
263
Darrick J. Wongce92d292018-01-16 18:54:12 -0800264 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwig4c037dd2019-11-08 15:05:33 -0800265 iter_off += be16_to_cpu(dup->length);
Darrick J. Wongce92d292018-01-16 18:54:12 -0800266 continue;
267 }
Darrick J. Wongce92d292018-01-16 18:54:12 -0800268 if (dep == dent)
269 break;
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800270 iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
Darrick J. Wongce92d292018-01-16 18:54:12 -0800271 }
272
273 /* Retrieve the entry, sanity check it, and compare hashes. */
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700274 ino = be64_to_cpu(dent->inumber);
275 hash = be32_to_cpu(ent->hashval);
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -0800276 tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700277 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700278 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700279 if (dent->namelen == 0) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700280 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700281 goto out_relse;
282 }
283 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
284 if (calc_hash != hash)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700285 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700286
287out_relse:
288 xfs_trans_brelse(ds->dargs.trans, bp);
289out:
290 return error;
291}
292
Darrick J. Wongdf481962017-10-17 21:37:44 -0700293/*
294 * Is this unused entry either in the bestfree or smaller than all of
295 * them? We've already checked that the bestfrees are sorted longest to
296 * shortest, and that there aren't any bogus entries.
297 */
298STATIC void
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700299xchk_directory_check_free_entry(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700300 struct xfs_scrub *sc,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700301 xfs_dablk_t lblk,
302 struct xfs_dir2_data_free *bf,
303 struct xfs_dir2_data_unused *dup)
304{
305 struct xfs_dir2_data_free *dfp;
306 unsigned int dup_length;
307
308 dup_length = be16_to_cpu(dup->length);
309
310 /* Unused entry is shorter than any of the bestfrees */
311 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
312 return;
313
314 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
315 if (dup_length == be16_to_cpu(dfp->length))
316 return;
317
318 /* Unused entry should be in the bestfrees but wasn't found. */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700319 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700320}
321
322/* Check free space info in a directory data block. */
323STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700324xchk_directory_data_bestfree(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700325 struct xfs_scrub *sc,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700326 xfs_dablk_t lblk,
327 bool is_block)
328{
329 struct xfs_dir2_data_unused *dup;
330 struct xfs_dir2_data_free *dfp;
331 struct xfs_buf *bp;
332 struct xfs_dir2_data_free *bf;
333 struct xfs_mount *mp = sc->mp;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700334 u16 tag;
335 unsigned int nr_bestfrees = 0;
336 unsigned int nr_frees = 0;
337 unsigned int smallest_bestfree;
338 int newlen;
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800339 unsigned int offset;
340 unsigned int end;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700341 int error;
342
Darrick J. Wongdf481962017-10-17 21:37:44 -0700343 if (is_block) {
344 /* dir block format */
345 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700346 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700347 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
348 } else {
349 /* dir data format */
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800350 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, 0, &bp);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700351 }
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700352 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
Darrick J. Wongdf481962017-10-17 21:37:44 -0700353 goto out;
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700354 xchk_buffer_recheck(sc, bp);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700355
356 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
357
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700358 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
359 goto out_buf;
360
Darrick J. Wongdf481962017-10-17 21:37:44 -0700361 /* Do the bestfrees correspond to actual free space? */
Christoph Hellwig1848b602019-11-08 15:05:39 -0800362 bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700363 smallest_bestfree = UINT_MAX;
364 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
365 offset = be16_to_cpu(dfp->offset);
366 if (offset == 0)
367 continue;
368 if (offset >= mp->m_dir_geo->blksize) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700369 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700370 goto out_buf;
371 }
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800372 dup = bp->b_addr + offset;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700373 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
374
375 /* bestfree doesn't match the entry it points at? */
376 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
377 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800378 tag != offset) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700379 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700380 goto out_buf;
381 }
382
383 /* bestfree records should be ordered largest to smallest */
384 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700385 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700386 goto out_buf;
387 }
388
389 smallest_bestfree = be16_to_cpu(dfp->length);
390 nr_bestfrees++;
391 }
392
393 /* Make sure the bestfrees are actually the best free spaces. */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800394 offset = mp->m_dir_geo->data_entry_offset;
Christoph Hellwig5c072122019-11-08 15:05:36 -0800395 end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700396
397 /* Iterate the entries, stopping when we hit or go past the end. */
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800398 while (offset < end) {
399 dup = bp->b_addr + offset;
400
Darrick J. Wongdf481962017-10-17 21:37:44 -0700401 /* Skip real entries */
402 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800403 struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700404
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800405 newlen = xfs_dir2_data_entsize(mp, dep->namelen);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700406 if (newlen <= 0) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700407 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700408 lblk);
409 goto out_buf;
410 }
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800411 offset += newlen;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700412 continue;
413 }
414
415 /* Spot check this free entry */
416 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800417 if (tag != offset) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700418 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700419 goto out_buf;
420 }
Darrick J. Wongdf481962017-10-17 21:37:44 -0700421
422 /*
423 * Either this entry is a bestfree or it's smaller than
424 * any of the bestfrees.
425 */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700426 xchk_directory_check_free_entry(sc, lblk, bf, dup);
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700427 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
428 goto out_buf;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700429
430 /* Move on. */
431 newlen = be16_to_cpu(dup->length);
432 if (newlen <= 0) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700433 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700434 goto out_buf;
435 }
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800436 offset += newlen;
437 if (offset <= end)
Darrick J. Wongdf481962017-10-17 21:37:44 -0700438 nr_frees++;
439 }
440
441 /* We're required to fill all the space. */
Christoph Hellwig4a1a8b22019-11-08 15:05:33 -0800442 if (offset != end)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700443 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700444
445 /* Did we see at least as many free slots as there are bestfrees? */
446 if (nr_frees < nr_bestfrees)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700447 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700448out_buf:
449 xfs_trans_brelse(sc->tp, bp);
450out:
451 return error;
452}
453
454/*
455 * Does the free space length in the free space index block ($len) match
456 * the longest length in the directory data block's bestfree array?
457 * Assume that we've already checked that the data block's bestfree
458 * array is in order.
459 */
460STATIC void
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700461xchk_directory_check_freesp(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700462 struct xfs_scrub *sc,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700463 xfs_dablk_t lblk,
464 struct xfs_buf *dbp,
465 unsigned int len)
466{
Darrick J. Wongdf481962017-10-17 21:37:44 -0700467 struct xfs_dir2_data_free *dfp;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700468
Christoph Hellwig1848b602019-11-08 15:05:39 -0800469 dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700470
Darrick J. Wong35ce8522017-11-06 11:37:46 -0800471 if (len != be16_to_cpu(dfp->length))
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700472 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700473
Darrick J. Wong35ce8522017-11-06 11:37:46 -0800474 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700475 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700476}
477
478/* Check free space info in a directory leaf1 block. */
479STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700480xchk_directory_leaf1_bestfree(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700481 struct xfs_scrub *sc,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700482 struct xfs_da_args *args,
483 xfs_dablk_t lblk)
484{
485 struct xfs_dir3_icleaf_hdr leafhdr;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700486 struct xfs_dir2_leaf_tail *ltp;
487 struct xfs_dir2_leaf *leaf;
488 struct xfs_buf *dbp;
489 struct xfs_buf *bp;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700490 struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
491 __be16 *bestp;
492 __u16 best;
493 __u32 hash;
494 __u32 lasthash = 0;
495 __u32 bestcount;
496 unsigned int stale = 0;
497 int i;
498 int error;
499
500 /* Read the free space block. */
Christoph Hellwigc943c0b2019-11-20 09:46:03 -0800501 error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, &bp);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700502 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
Darrick J. Wongdf481962017-10-17 21:37:44 -0700503 goto out;
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700504 xchk_buffer_recheck(sc, bp);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700505
506 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800507 xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700508 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
509 bestcount = be32_to_cpu(ltp->bestcount);
510 bestp = xfs_dir2_leaf_bests_p(ltp);
511
512 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
513 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
514
515 if (hdr3->pad != cpu_to_be32(0))
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700516 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700517 }
518
519 /*
520 * There should be as many bestfree slots as there are dir data
521 * blocks that can fit under i_size.
522 */
523 if (bestcount != xfs_dir2_byte_to_db(geo, sc->ip->i_d.di_size)) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700524 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700525 goto out;
526 }
527
528 /* Is the leaf count even remotely sane? */
Christoph Hellwig478c7832019-11-08 14:57:51 -0800529 if (leafhdr.count > geo->leaf_max_ents) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700530 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700531 goto out;
532 }
533
534 /* Leaves and bests don't overlap in leaf format. */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800535 if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700536 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700537 goto out;
538 }
539
540 /* Check hash value order, count stale entries. */
541 for (i = 0; i < leafhdr.count; i++) {
Christoph Hellwig787b0892019-11-08 14:57:50 -0800542 hash = be32_to_cpu(leafhdr.ents[i].hashval);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700543 if (i > 0 && lasthash > hash)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700544 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700545 lasthash = hash;
Christoph Hellwig787b0892019-11-08 14:57:50 -0800546 if (leafhdr.ents[i].address ==
547 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Darrick J. Wongdf481962017-10-17 21:37:44 -0700548 stale++;
549 }
550 if (leafhdr.stale != stale)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700551 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700552 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
553 goto out;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700554
555 /* Check all the bestfree entries. */
556 for (i = 0; i < bestcount; i++, bestp++) {
557 best = be16_to_cpu(*bestp);
558 if (best == NULLDATAOFF)
559 continue;
560 error = xfs_dir3_data_read(sc->tp, sc->ip,
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800561 i * args->geo->fsbcount, 0, &dbp);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700562 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700563 &error))
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700564 break;
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700565 xchk_directory_check_freesp(sc, lblk, dbp, best);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700566 xfs_trans_brelse(sc->tp, dbp);
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700567 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
568 goto out;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700569 }
570out:
571 return error;
572}
573
574/* Check free space info in a directory freespace block. */
575STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700576xchk_directory_free_bestfree(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700577 struct xfs_scrub *sc,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700578 struct xfs_da_args *args,
579 xfs_dablk_t lblk)
580{
581 struct xfs_dir3_icfree_hdr freehdr;
582 struct xfs_buf *dbp;
583 struct xfs_buf *bp;
Christoph Hellwig88aa5de2017-11-06 11:53:58 -0800584 __u16 best;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700585 unsigned int stale = 0;
586 int i;
587 int error;
588
589 /* Read the free space block */
590 error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700591 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
Darrick J. Wongdf481962017-10-17 21:37:44 -0700592 goto out;
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700593 xchk_buffer_recheck(sc, bp);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700594
595 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
596 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
597
598 if (hdr3->pad != cpu_to_be32(0))
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700599 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700600 }
601
602 /* Check all the entries. */
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800603 xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800604 for (i = 0; i < freehdr.nvalid; i++) {
605 best = be16_to_cpu(freehdr.bests[i]);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700606 if (best == NULLDATAOFF) {
607 stale++;
608 continue;
609 }
610 error = xfs_dir3_data_read(sc->tp, sc->ip,
611 (freehdr.firstdb + i) * args->geo->fsbcount,
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800612 0, &dbp);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700613 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700614 &error))
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700615 break;
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700616 xchk_directory_check_freesp(sc, lblk, dbp, best);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700617 xfs_trans_brelse(sc->tp, dbp);
618 }
619
620 if (freehdr.nused + stale != freehdr.nvalid)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700621 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700622out:
623 return error;
624}
625
626/* Check free space information in directories. */
627STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700628xchk_directory_blocks(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700629 struct xfs_scrub *sc)
Darrick J. Wongdf481962017-10-17 21:37:44 -0700630{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700631 struct xfs_bmbt_irec got;
632 struct xfs_da_args args;
633 struct xfs_ifork *ifp;
634 struct xfs_mount *mp = sc->mp;
635 xfs_fileoff_t leaf_lblk;
636 xfs_fileoff_t free_lblk;
637 xfs_fileoff_t lblk;
638 struct xfs_iext_cursor icur;
639 xfs_dablk_t dabno;
640 bool found;
641 int is_block = 0;
642 int error;
Darrick J. Wongdf481962017-10-17 21:37:44 -0700643
644 /* Ignore local format directories. */
645 if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
646 sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
647 return 0;
648
649 ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
650 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
651 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
652 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
653
654 /* Is this a block dir? */
655 args.dp = sc->ip;
656 args.geo = mp->m_dir_geo;
657 args.trans = sc->tp;
658 error = xfs_dir2_isblock(&args, &is_block);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700659 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
Darrick J. Wongdf481962017-10-17 21:37:44 -0700660 goto out;
661
662 /* Iterate all the data extents in the directory... */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700663 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700664 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
Darrick J. Wongdf481962017-10-17 21:37:44 -0700665 /* Block directories only have a single block at offset 0. */
666 if (is_block &&
667 (got.br_startoff > 0 ||
668 got.br_blockcount != args.geo->fsbcount)) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700669 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700670 got.br_startoff);
671 break;
672 }
673
674 /* No more data blocks... */
675 if (got.br_startoff >= leaf_lblk)
676 break;
677
678 /*
679 * Check each data block's bestfree data.
680 *
681 * Iterate all the fsbcount-aligned block offsets in
682 * this directory. The directory block reading code is
683 * smart enough to do its own bmap lookups to handle
684 * discontiguous directory blocks. When we're done
685 * with the extent record, re-query the bmap at the
686 * next fsbcount-aligned offset to avoid redundant
687 * block checks.
688 */
689 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
690 args.geo->fsbcount);
691 lblk < got.br_startoff + got.br_blockcount;
692 lblk += args.geo->fsbcount) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700693 error = xchk_directory_data_bestfree(sc, lblk,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700694 is_block);
695 if (error)
696 goto out;
697 }
698 dabno = got.br_startoff + got.br_blockcount;
699 lblk = roundup(dabno, args.geo->fsbcount);
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700700 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700701 }
702
703 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
704 goto out;
705
706 /* Look for a leaf1 block, which has free info. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700707 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
Darrick J. Wongdf481962017-10-17 21:37:44 -0700708 got.br_startoff == leaf_lblk &&
709 got.br_blockcount == args.geo->fsbcount &&
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700710 !xfs_iext_next_extent(ifp, &icur, &got)) {
Darrick J. Wongdf481962017-10-17 21:37:44 -0700711 if (is_block) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700712 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700713 goto out;
714 }
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700715 error = xchk_directory_leaf1_bestfree(sc, &args,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700716 leaf_lblk);
717 if (error)
718 goto out;
719 }
720
721 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
722 goto out;
723
724 /* Scan for free blocks */
725 lblk = free_lblk;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700726 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
Darrick J. Wong8bc763c2018-05-14 06:34:32 -0700727 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
Darrick J. Wongdf481962017-10-17 21:37:44 -0700728 /*
729 * Dirs can't have blocks mapped above 2^32.
730 * Single-block dirs shouldn't even be here.
731 */
732 lblk = got.br_startoff;
733 if (lblk & ~0xFFFFFFFFULL) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700734 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700735 goto out;
736 }
737 if (is_block) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700738 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700739 goto out;
740 }
741
742 /*
743 * Check each dir free block's bestfree data.
744 *
745 * Iterate all the fsbcount-aligned block offsets in
746 * this directory. The directory block reading code is
747 * smart enough to do its own bmap lookups to handle
748 * discontiguous directory blocks. When we're done
749 * with the extent record, re-query the bmap at the
750 * next fsbcount-aligned offset to avoid redundant
751 * block checks.
752 */
753 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
754 args.geo->fsbcount);
755 lblk < got.br_startoff + got.br_blockcount;
756 lblk += args.geo->fsbcount) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700757 error = xchk_directory_free_bestfree(sc, &args,
Darrick J. Wongdf481962017-10-17 21:37:44 -0700758 lblk);
759 if (error)
760 goto out;
761 }
762 dabno = got.br_startoff + got.br_blockcount;
763 lblk = roundup(dabno, args.geo->fsbcount);
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700764 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700765 }
766out:
767 return error;
768}
769
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700770/* Scrub a whole directory. */
771int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700772xchk_directory(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700773 struct xfs_scrub *sc)
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700774{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700775 struct xchk_dir_ctx sdc = {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700776 .dir_iter.actor = xchk_dir_actor,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700777 .dir_iter.pos = 0,
778 .sc = sc,
779 };
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700780 size_t bufsize;
781 loff_t oldpos;
782 int error = 0;
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700783
784 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
785 return -ENOENT;
786
787 /* Plausible size? */
788 if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700789 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700790 goto out;
791 }
792
793 /* Check directory tree structure */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700794 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700795 if (error)
796 return error;
797
798 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
799 return error;
800
Darrick J. Wongdf481962017-10-17 21:37:44 -0700801 /* Check the freespace. */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700802 error = xchk_directory_blocks(sc);
Darrick J. Wongdf481962017-10-17 21:37:44 -0700803 if (error)
804 return error;
805
806 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
807 return error;
808
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700809 /*
810 * Check that every dirent we see can also be looked up by hash.
811 * Userspace usually asks for a 32k buffer, so we will too.
812 */
813 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
814 sc->ip->i_d.di_size);
815
816 /*
817 * Look up every name in this directory by hash.
818 *
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700819 * Use the xfs_readdir function to call xchk_dir_actor on
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700820 * every directory entry in this directory. In _actor, we check
821 * the name, inode number, and ftype (if applicable) of the
822 * entry. xfs_readdir uses the VFS filldir functions to provide
823 * iteration context.
824 *
825 * The VFS grabs a read or write lock via i_rwsem before it reads
826 * or writes to a directory. If we've gotten this far we've
827 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
828 * getting a write lock on i_rwsem. Therefore, it is safe for us
829 * to drop the ILOCK here in order to reuse the _readdir and
830 * _dir_lookup routines, which do their own ILOCK locking.
831 */
832 oldpos = 0;
833 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
834 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
835 while (true) {
836 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700837 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700838 &error))
839 goto out;
840 if (oldpos == sdc.dir_iter.pos)
841 break;
842 oldpos = sdc.dir_iter.pos;
843 }
844
845out:
846 return error;
847}