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