xref: /linux/fs/xfs/scrub/dir.c (revision bb29b957eaaa15be33243550d9ee576c2d601669)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #include "xfs_platform.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 "xfs_health.h"
19 #include "xfs_attr.h"
20 #include "xfs_parent.h"
21 #include "scrub/scrub.h"
22 #include "scrub/common.h"
23 #include "scrub/dabtree.h"
24 #include "scrub/readdir.h"
25 #include "scrub/health.h"
26 #include "scrub/repair.h"
27 #include "scrub/trace.h"
28 #include "scrub/xfile.h"
29 #include "scrub/xfarray.h"
30 #include "scrub/xfblob.h"
31 
32 /* Set us up to scrub directories. */
33 int
34 xchk_setup_directory(
35 	struct xfs_scrub	*sc)
36 {
37 	int			error;
38 
39 	if (xchk_could_repair(sc)) {
40 		error = xrep_setup_directory(sc);
41 		if (error)
42 			return error;
43 	}
44 
45 	return xchk_setup_inode_contents(sc, 0);
46 }
47 
48 /* Directories */
49 
50 /* Deferred directory entry that we saved for later. */
51 struct xchk_dirent {
52 	/* Cookie for retrieval of the dirent name. */
53 	xfblob_cookie		name_cookie;
54 
55 	/* Child inode number. */
56 	xfs_ino_t		ino;
57 
58 	/* Length of the pptr name. */
59 	uint8_t			namelen;
60 };
61 
62 struct xchk_dir {
63 	struct xfs_scrub	*sc;
64 
65 	/* information for parent pointer validation. */
66 	struct xfs_parent_rec	pptr_rec;
67 	struct xfs_da_args	pptr_args;
68 
69 	/* Fixed-size array of xchk_dirent structures. */
70 	struct xfarray		*dir_entries;
71 
72 	/* Blobs containing dirent names. */
73 	struct xfblob		*dir_names;
74 
75 	/* If we've cycled the ILOCK, we must revalidate deferred dirents. */
76 	bool			need_revalidate;
77 
78 	/* Name buffer for dirent revalidation. */
79 	struct xfs_name		xname;
80 	uint8_t			namebuf[MAXNAMELEN];
81 };
82 
83 /* Scrub a directory entry. */
84 
85 /* Check that an inode's mode matches a given XFS_DIR3_FT_* type. */
86 STATIC void
87 xchk_dir_check_ftype(
88 	struct xfs_scrub	*sc,
89 	xfs_fileoff_t		offset,
90 	struct xfs_inode	*ip,
91 	int			ftype)
92 {
93 	struct xfs_mount	*mp = sc->mp;
94 
95 	if (!xfs_has_ftype(mp)) {
96 		if (ftype != XFS_DIR3_FT_UNKNOWN && ftype != XFS_DIR3_FT_DIR)
97 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
98 		return;
99 	}
100 
101 	if (xfs_mode_to_ftype(VFS_I(ip)->i_mode) != ftype)
102 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
103 
104 	/*
105 	 * Metadata and regular inodes cannot cross trees.  This property
106 	 * cannot change without a full inode free and realloc cycle, so it's
107 	 * safe to check this without holding locks.
108 	 */
109 	if (xfs_is_metadir_inode(ip) != xfs_is_metadir_inode(sc->ip))
110 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
111 }
112 
113 /*
114  * Try to lock a child file for checking parent pointers.  Returns the inode
115  * flags for the locks we now hold, or zero if we failed.
116  */
117 STATIC unsigned int
118 xchk_dir_lock_child(
119 	struct xfs_scrub	*sc,
120 	struct xfs_inode	*ip)
121 {
122 	if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
123 		return 0;
124 
125 	if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
126 		xfs_iunlock(ip, XFS_IOLOCK_SHARED);
127 		return 0;
128 	}
129 
130 	if (!xfs_inode_has_attr_fork(ip) || !xfs_need_iread_extents(&ip->i_af))
131 		return XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED;
132 
133 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
134 
135 	if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
136 		xfs_iunlock(ip, XFS_IOLOCK_SHARED);
137 		return 0;
138 	}
139 
140 	return XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL;
141 }
142 
143 /* Check the backwards link (parent pointer) associated with this dirent. */
144 STATIC int
145 xchk_dir_parent_pointer(
146 	struct xchk_dir		*sd,
147 	xfs_dablk_t		offset,
148 	const struct xfs_name	*name,
149 	struct xfs_inode	*ip)
150 {
151 	struct xfs_scrub	*sc = sd->sc;
152 	int			error;
153 
154 	xfs_inode_to_parent_rec(&sd->pptr_rec, sc->ip);
155 	error = xfs_parent_lookup(sc->tp, ip, name, &sd->pptr_rec,
156 			&sd->pptr_args);
157 	if (error == -ENOATTR)
158 		xchk_fblock_xref_set_corrupt(sc, XFS_DATA_FORK, offset);
159 	else
160 		xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset,
161 				&error);
162 
163 	return 0;
164 }
165 
166 /* Look for a parent pointer matching this dirent, if the child isn't busy. */
167 STATIC int
168 xchk_dir_check_pptr_fast(
169 	struct xchk_dir		*sd,
170 	xfs_dablk_t		offset,
171 	const struct xfs_name	*name,
172 	struct xfs_inode	*ip)
173 {
174 	struct xfs_scrub	*sc = sd->sc;
175 	unsigned int		lockmode;
176 	int			error;
177 
178 	/* dot and dotdot entries do not have parent pointers */
179 	if (xfs_dir2_samename(name, &xfs_name_dot) ||
180 	    xfs_dir2_samename(name, &xfs_name_dotdot))
181 		return 0;
182 
183 	/* No self-referential non-dot or dotdot dirents. */
184 	if (ip == sc->ip) {
185 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
186 		return -ECANCELED;
187 	}
188 
189 	/* Try to lock the inode. */
190 	lockmode = xchk_dir_lock_child(sc, ip);
191 	if (!lockmode) {
192 		struct xchk_dirent	save_de = {
193 			.namelen	= name->len,
194 			.ino		= I_INO(ip),
195 		};
196 
197 		/* Couldn't lock the inode, so save the dirent for later. */
198 		trace_xchk_dir_defer(sc->ip, name, I_INO(ip));
199 
200 		error = xfblob_storename(sd->dir_names, &save_de.name_cookie,
201 				name);
202 		if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset,
203 					&error))
204 			return error;
205 
206 		error = xfarray_append(sd->dir_entries, &save_de);
207 		if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset,
208 					&error))
209 			return error;
210 
211 		return 0;
212 	}
213 
214 	error = xchk_dir_parent_pointer(sd, offset, name, ip);
215 	xfs_iunlock(ip, lockmode);
216 	return error;
217 }
218 
219 /*
220  * Scrub a single directory entry.
221  *
222  * Check the inode number to make sure it's sane, then we check that we can
223  * look up this filename.  Finally, we check the ftype.
224  */
225 STATIC int
226 xchk_dir_actor(
227 	struct xfs_scrub	*sc,
228 	struct xfs_inode	*dp,
229 	xfs_dir2_dataptr_t	dapos,
230 	const struct xfs_name	*name,
231 	xfs_ino_t		ino,
232 	void			*priv)
233 {
234 	struct xfs_mount	*mp = dp->i_mount;
235 	struct xfs_inode	*ip;
236 	struct xchk_dir		*sd = priv;
237 	xfs_ino_t		lookup_ino;
238 	xfs_dablk_t		offset;
239 	int			error = 0;
240 
241 	offset = xfs_dir2_db_to_da(mp->m_dir_geo,
242 			xfs_dir2_dataptr_to_db(mp->m_dir_geo, dapos));
243 
244 	if (xchk_should_terminate(sc, &error))
245 		return error;
246 
247 	/* Does this inode number make sense? */
248 	if (!xfs_verify_dir_ino(mp, ino)) {
249 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
250 		return -ECANCELED;
251 	}
252 
253 	/* Does this name make sense? */
254 	if (!xfs_dir2_namecheck(name->name, name->len)) {
255 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
256 		return -ECANCELED;
257 	}
258 
259 	if (xfs_dir2_samename(name, &xfs_name_dot)) {
260 		/* If this is "." then check that the inum matches the dir. */
261 		if (ino != I_INO(dp))
262 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
263 	} else if (xfs_dir2_samename(name, &xfs_name_dotdot)) {
264 		/*
265 		 * If this is ".." in the root inode, check that the inum
266 		 * matches this dir.
267 		 */
268 		if (xchk_inode_is_dirtree_root(dp) && ino != I_INO(dp))
269 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
270 	}
271 
272 	/* Verify that we can look up this name by hash. */
273 	error = xchk_dir_lookup(sc, dp, name, &lookup_ino);
274 	/* ENOENT means the hash lookup failed and the dir is corrupt */
275 	if (error == -ENOENT)
276 		error = -EFSCORRUPTED;
277 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error))
278 		goto out;
279 	if (lookup_ino != ino) {
280 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
281 		return -ECANCELED;
282 	}
283 
284 	/*
285 	 * Grab the inode pointed to by the dirent.  We release the inode
286 	 * before we cancel the scrub transaction.
287 	 *
288 	 * If _iget returns -EINVAL or -ENOENT then the child inode number is
289 	 * garbage and the directory is corrupt.  If the _iget returns
290 	 * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
291 	 *  cross referencing error.  Any other error is an operational error.
292 	 */
293 	error = xchk_iget(sc, ino, &ip);
294 	if (error == -EINVAL || error == -ENOENT) {
295 		error = -EFSCORRUPTED;
296 		xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
297 		goto out;
298 	}
299 	if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset, &error))
300 		goto out;
301 
302 	xchk_dir_check_ftype(sc, offset, ip, name->type);
303 
304 	if (xfs_has_parent(mp)) {
305 		error = xchk_dir_check_pptr_fast(sd, offset, name, ip);
306 		if (error)
307 			goto out_rele;
308 	}
309 
310 out_rele:
311 	xchk_irele(sc, ip);
312 out:
313 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
314 		return -ECANCELED;
315 	return error;
316 }
317 
318 /*
319  * Deal with an unused directory entry.  Returns a positive number of bytes to
320  * advance in the directory block, zero to stop processing, or -1 if corruption
321  * was found.
322  */
323 static int
324 xchk_dir_check_unused(
325 	struct xchk_da_btree		*ds,
326 	struct xfs_buf			*bp,
327 	unsigned int			iter_off,
328 	unsigned int			end,
329 	xfs_dablk_t			rec_bno)
330 {
331 	struct xfs_dir2_data_unused	*dup = bp->b_addr + iter_off;
332 	unsigned int			advance;
333 
334 	if (iter_off + sizeof(*dup) > end) {
335 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
336 		return -1;
337 	}
338 
339 	advance = xfs_dir2_data_unusedsize(be16_to_cpu(dup->length));
340 	if (!advance || iter_off + advance > end) {
341 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
342 		return -1;
343 	}
344 
345 	return advance;
346 }
347 
348 /*
349  * Check a directory entry.  Returns a positive number of bytes to advance
350  * in the directory block, zero to stop processing, or -1 if corruption was
351  * found.
352  */
353 static int
354 xchk_dir_check_entry(
355 	struct xchk_da_btree		*ds,
356 	struct xfs_buf			*bp,
357 	unsigned int			iter_off,
358 	unsigned int			end,
359 	struct xfs_dir2_data_entry	*dent,
360 	xfs_dablk_t			rec_bno)
361 {
362 	struct xfs_dir2_data_entry	*dep = bp->b_addr + iter_off;
363 	struct xfs_mount		*mp = ds->state->mp;
364 	unsigned int			advance;
365 
366 	/* must have namelen */
367 	advance = offsetof(typeof(*dep), name);
368 	if (iter_off + advance >= end) {
369 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
370 		return -1;
371 	}
372 
373 	/* Don't walk off the end of the block. */
374 	advance = xfs_dir2_data_entsize(mp, dep->namelen);
375 	if (!advance || iter_off + advance > end) {
376 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
377 		return -1;
378 	}
379 
380 	if (dep == dent)
381 		return 0;
382 
383 	return advance;
384 }
385 
386 /* Scrub a directory btree record. */
387 STATIC int
388 xchk_dir_rec(
389 	struct xchk_da_btree		*ds,
390 	int				level)
391 {
392 	struct xfs_name			dname = { };
393 	struct xfs_da_state_blk		*blk = &ds->state->path.blk[level];
394 	struct xfs_mount		*mp = ds->state->mp;
395 	struct xfs_inode		*dp = ds->dargs.dp;
396 	struct xfs_da_geometry		*geo = mp->m_dir_geo;
397 	struct xfs_dir2_data_entry	*dent;
398 	struct xfs_buf			*bp;
399 	struct xfs_dir2_leaf_entry	*ent;
400 	unsigned int			end;
401 	unsigned int			iter_off;
402 	xfs_ino_t			ino;
403 	xfs_dablk_t			rec_bno;
404 	xfs_dir2_db_t			db;
405 	xfs_dir2_data_aoff_t		off;
406 	xfs_dir2_dataptr_t		ptr;
407 	xfs_dahash_t			calc_hash;
408 	xfs_dahash_t			hash;
409 	struct xfs_dir3_icleaf_hdr	hdr;
410 	unsigned int			tag;
411 	bool				foundit = false;
412 	int				error;
413 
414 	ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
415 	       blk->magic == XFS_DIR2_LEAFN_MAGIC);
416 
417 	xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
418 	ent = hdr.ents + blk->index;
419 
420 	/* Check the hash of the entry. */
421 	error = xchk_da_btree_hash(ds, level, &ent->hashval);
422 	if (error)
423 		goto out;
424 
425 	/* Valid hash pointer? */
426 	ptr = be32_to_cpu(ent->address);
427 	if (ptr == 0)
428 		return 0;
429 
430 	/* Find the directory entry's location. */
431 	db = xfs_dir2_dataptr_to_db(geo, ptr);
432 	off = xfs_dir2_dataptr_to_off(geo, ptr);
433 	rec_bno = xfs_dir2_db_to_da(geo, db);
434 
435 	if (rec_bno >= geo->leafblk) {
436 		xchk_da_set_corrupt(ds, level);
437 		goto out;
438 	}
439 	error = xfs_dir3_data_read(ds->dargs.trans, dp, ds->dargs.owner,
440 			rec_bno, XFS_DABUF_MAP_HOLE_OK, &bp);
441 	if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
442 			&error))
443 		goto out;
444 	if (!bp) {
445 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
446 		goto out;
447 	}
448 	xchk_buffer_recheck(ds->sc, bp);
449 
450 	if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
451 		goto out_relse;
452 
453 	dent = bp->b_addr + off;
454 
455 	/* Make sure we got a real directory entry. */
456 	end = xfs_dir3_data_end_offset(geo, bp->b_addr);
457 	if (!end) {
458 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
459 		goto out_relse;
460 	}
461 	iter_off = geo->data_entry_offset;
462 	while (iter_off < end) {
463 		struct xfs_dir2_data_unused	*dup = bp->b_addr + iter_off;
464 		int				advance;
465 
466 		/* must have freetag */
467 		advance = offsetof(typeof(*dup), length);
468 		if (iter_off + advance >= end) {
469 			xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
470 			goto out_relse;
471 		}
472 
473 		if (dup->freetag == cpu_to_be16(XFS_DIR2_DATA_FREE_TAG))
474 			advance = xchk_dir_check_unused(ds, bp, iter_off, end,
475 					rec_bno);
476 		else
477 			advance = xchk_dir_check_entry(ds, bp, iter_off, end,
478 					dent, rec_bno);
479 		if (advance < 0)
480 			goto out_relse;
481 		if (!advance) {
482 			foundit = true;
483 			break;
484 		}
485 		iter_off += advance;
486 	}
487 
488 	/* Hash tree must point to the exact dirent */
489 	if (!foundit) {
490 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
491 		goto out_relse;
492 	}
493 
494 	/* Retrieve the entry, sanity check it, and compare hashes. */
495 	ino = be64_to_cpu(dent->inumber);
496 	hash = be32_to_cpu(ent->hashval);
497 	tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
498 	if (!xfs_verify_dir_ino(mp, ino) || tag != off)
499 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
500 	if (dent->namelen == 0) {
501 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
502 		goto out_relse;
503 	}
504 
505 	/* Name must not overflow end of block */
506 	if ((char *)dent->name + dent->namelen >
507 	    (char *)bp->b_addr + BBTOB(bp->b_length)) {
508 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
509 		goto out_relse;
510 	}
511 
512 	/* Does the directory hash match? */
513 	dname.name = dent->name;
514 	dname.len = dent->namelen;
515 	calc_hash = xfs_dir2_hashname(mp, &dname);
516 	if (calc_hash != hash)
517 		xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
518 
519 out_relse:
520 	xfs_trans_brelse(ds->dargs.trans, bp);
521 out:
522 	return error;
523 }
524 
525 /*
526  * Is this unused entry either in the bestfree or smaller than all of
527  * them?  We've already checked that the bestfrees are sorted longest to
528  * shortest, and that there aren't any bogus entries.
529  */
530 STATIC void
531 xchk_directory_check_free_entry(
532 	struct xfs_scrub		*sc,
533 	xfs_dablk_t			lblk,
534 	struct xfs_dir2_data_free	*bf,
535 	struct xfs_dir2_data_unused	*dup)
536 {
537 	struct xfs_dir2_data_free	*dfp;
538 	unsigned int			dup_length;
539 
540 	dup_length = be16_to_cpu(dup->length);
541 
542 	/* Unused entry is shorter than any of the bestfrees */
543 	if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
544 		return;
545 
546 	for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
547 		if (dup_length == be16_to_cpu(dfp->length))
548 			return;
549 
550 	/* Unused entry should be in the bestfrees but wasn't found. */
551 	xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
552 }
553 
554 /* Check free space info in a directory data block. */
555 STATIC int
556 xchk_directory_data_bestfree(
557 	struct xfs_scrub		*sc,
558 	xfs_dablk_t			lblk,
559 	bool				is_block)
560 {
561 	struct xfs_dir2_data_unused	*dup;
562 	struct xfs_dir2_data_free	*dfp;
563 	struct xfs_buf			*bp;
564 	struct xfs_dir2_data_free	*bf;
565 	struct xfs_mount		*mp = sc->mp;
566 	u16				tag;
567 	unsigned int			nr_bestfrees = 0;
568 	unsigned int			nr_frees = 0;
569 	unsigned int			smallest_bestfree;
570 	int				newlen;
571 	unsigned int			offset;
572 	unsigned int			end;
573 	int				error;
574 
575 	if (is_block) {
576 		/* dir block format */
577 		if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
578 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
579 		error = xfs_dir3_block_read(sc->tp, sc->ip, I_INO(sc->ip), &bp);
580 	} else {
581 		/* dir data format */
582 		error = xfs_dir3_data_read(sc->tp, sc->ip, I_INO(sc->ip), lblk,
583 				0, &bp);
584 	}
585 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
586 		goto out;
587 	xchk_buffer_recheck(sc, bp);
588 
589 	if (!is_block && xfs_has_crc(sc->mp)) {
590 		struct xfs_dir3_data_hdr    *hdr3 = bp->b_addr;
591 
592 		if (hdr3->pad)
593 			xchk_fblock_set_preen(sc, XFS_DATA_FORK, lblk);
594 	}
595 
596 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
597 		goto out_buf;
598 
599 	/* Do the bestfrees correspond to actual free space? */
600 	bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
601 	smallest_bestfree = UINT_MAX;
602 	for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
603 		offset = be16_to_cpu(dfp->offset);
604 		if (offset == 0)
605 			continue;
606 		if (offset >= mp->m_dir_geo->blksize) {
607 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
608 			goto out_buf;
609 		}
610 		dup = bp->b_addr + offset;
611 		tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
612 
613 		/* bestfree doesn't match the entry it points at? */
614 		if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
615 		    be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
616 		    tag != offset) {
617 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
618 			goto out_buf;
619 		}
620 
621 		/* bestfree records should be ordered largest to smallest */
622 		if (smallest_bestfree < be16_to_cpu(dfp->length)) {
623 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
624 			goto out_buf;
625 		}
626 
627 		smallest_bestfree = be16_to_cpu(dfp->length);
628 		nr_bestfrees++;
629 	}
630 
631 	/* Make sure the bestfrees are actually the best free spaces. */
632 	offset = mp->m_dir_geo->data_entry_offset;
633 	end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
634 
635 	/* Iterate the entries, stopping when we hit or go past the end. */
636 	while (offset < end) {
637 		dup = bp->b_addr + offset;
638 
639 		/* Skip real entries */
640 		if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
641 			struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
642 
643 			newlen = xfs_dir2_data_entsize(mp, dep->namelen);
644 			if (newlen <= 0) {
645 				xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
646 						lblk);
647 				goto out_buf;
648 			}
649 			offset += newlen;
650 			continue;
651 		}
652 
653 		/* Spot check this free entry */
654 		tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
655 		if (tag != offset) {
656 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
657 			goto out_buf;
658 		}
659 
660 		/*
661 		 * Either this entry is a bestfree or it's smaller than
662 		 * any of the bestfrees.
663 		 */
664 		xchk_directory_check_free_entry(sc, lblk, bf, dup);
665 		if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
666 			goto out_buf;
667 
668 		/* Move on. */
669 		newlen = be16_to_cpu(dup->length);
670 		if (newlen <= 0) {
671 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
672 			goto out_buf;
673 		}
674 		offset += newlen;
675 		if (offset <= end)
676 			nr_frees++;
677 	}
678 
679 	/* We're required to fill all the space. */
680 	if (offset != end)
681 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
682 
683 	/* Did we see at least as many free slots as there are bestfrees? */
684 	if (nr_frees < nr_bestfrees)
685 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
686 out_buf:
687 	xfs_trans_brelse(sc->tp, bp);
688 out:
689 	return error;
690 }
691 
692 /*
693  * Does the free space length in the free space index block ($len) match
694  * the longest length in the directory data block's bestfree array?
695  * Assume that we've already checked that the data block's bestfree
696  * array is in order.
697  */
698 STATIC void
699 xchk_directory_check_freesp(
700 	struct xfs_scrub		*sc,
701 	xfs_dablk_t			lblk,
702 	struct xfs_buf			*dbp,
703 	unsigned int			len)
704 {
705 	struct xfs_dir2_data_free	*dfp;
706 
707 	dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
708 
709 	if (len != be16_to_cpu(dfp->length))
710 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
711 
712 	if (len > 0 && be16_to_cpu(dfp->offset) == 0)
713 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
714 }
715 
716 /* Check free space info in a directory leaf1 block. */
717 STATIC int
718 xchk_directory_leaf1_bestfree(
719 	struct xfs_scrub		*sc,
720 	struct xfs_da_args		*args,
721 	xfs_dir2_db_t			last_data_db,
722 	xfs_dablk_t			lblk)
723 {
724 	struct xfs_dir3_icleaf_hdr	leafhdr;
725 	struct xfs_dir2_leaf_tail	*ltp;
726 	struct xfs_dir2_leaf		*leaf;
727 	struct xfs_buf			*dbp;
728 	struct xfs_buf			*bp;
729 	struct xfs_da_geometry		*geo = sc->mp->m_dir_geo;
730 	__be16				*bestp;
731 	__u16				best;
732 	__u32				hash;
733 	__u32				lasthash = 0;
734 	__u32				bestcount;
735 	unsigned int			stale = 0;
736 	int				i;
737 	int				error;
738 
739 	/* Read the free space block. */
740 	error = xfs_dir3_leaf_read(sc->tp, sc->ip, I_INO(sc->ip), lblk, &bp);
741 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
742 		return error;
743 	xchk_buffer_recheck(sc, bp);
744 
745 	leaf = bp->b_addr;
746 	xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
747 	ltp = xfs_dir2_leaf_tail_p(geo, leaf);
748 	bestcount = be32_to_cpu(ltp->bestcount);
749 	bestp = xfs_dir2_leaf_bests_p(ltp);
750 
751 	if (xfs_has_crc(sc->mp)) {
752 		struct xfs_dir3_leaf_hdr	*hdr3 = bp->b_addr;
753 
754 		if (hdr3->pad != cpu_to_be32(0))
755 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
756 	}
757 
758 	/*
759 	 * There must be enough bestfree slots to cover all the directory data
760 	 * blocks that we scanned.  It is possible for there to be a hole
761 	 * between the last data block and i_disk_size.  This seems like an
762 	 * oversight to the scrub author, but as we have been writing out
763 	 * directories like this (and xfs_repair doesn't mind them) for years,
764 	 * that's what we have to check.
765 	 */
766 	if (bestcount != last_data_db + 1) {
767 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
768 		goto out;
769 	}
770 
771 	/* Is the leaf count even remotely sane? */
772 	if (leafhdr.count > geo->leaf_max_ents) {
773 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
774 		goto out;
775 	}
776 
777 	/* Leaves and bests don't overlap in leaf format. */
778 	if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
779 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
780 		goto out;
781 	}
782 
783 	/* Check hash value order, count stale entries.  */
784 	for (i = 0; i < leafhdr.count; i++) {
785 		hash = be32_to_cpu(leafhdr.ents[i].hashval);
786 		if (i > 0 && lasthash > hash)
787 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
788 		lasthash = hash;
789 		if (leafhdr.ents[i].address ==
790 		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
791 			stale++;
792 	}
793 	if (leafhdr.stale != stale)
794 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
795 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
796 		goto out;
797 
798 	/* Check all the bestfree entries. */
799 	for (i = 0; i < bestcount; i++, bestp++) {
800 		best = be16_to_cpu(*bestp);
801 		error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
802 				xfs_dir2_db_to_da(args->geo, i),
803 				XFS_DABUF_MAP_HOLE_OK, &dbp);
804 		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
805 				&error))
806 			break;
807 
808 		if (!dbp) {
809 			if (best != NULLDATAOFF) {
810 				xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
811 						lblk);
812 				break;
813 			}
814 			continue;
815 		}
816 
817 		if (best == NULLDATAOFF)
818 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
819 		else
820 			xchk_directory_check_freesp(sc, lblk, dbp, best);
821 		xfs_trans_brelse(sc->tp, dbp);
822 		if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
823 			break;
824 	}
825 out:
826 	xfs_trans_brelse(sc->tp, bp);
827 	return error;
828 }
829 
830 /* Check free space info in a directory freespace block. */
831 STATIC int
832 xchk_directory_free_bestfree(
833 	struct xfs_scrub		*sc,
834 	struct xfs_da_args		*args,
835 	xfs_dablk_t			lblk)
836 {
837 	struct xfs_dir3_icfree_hdr	freehdr;
838 	struct xfs_buf			*dbp;
839 	struct xfs_buf			*bp;
840 	__u16				best;
841 	unsigned int			stale = 0;
842 	int				i;
843 	int				error;
844 
845 	/* Read the free space block */
846 	error = xfs_dir2_free_read(sc->tp, sc->ip, I_INO(sc->ip), lblk, &bp);
847 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
848 		return error;
849 	xchk_buffer_recheck(sc, bp);
850 
851 	if (xfs_has_crc(sc->mp)) {
852 		struct xfs_dir3_free_hdr	*hdr3 = bp->b_addr;
853 
854 		if (hdr3->pad != cpu_to_be32(0))
855 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
856 	}
857 
858 	/* Check all the entries. */
859 	xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
860 	for (i = 0; i < freehdr.nvalid; i++) {
861 		best = be16_to_cpu(freehdr.bests[i]);
862 		if (best == NULLDATAOFF) {
863 			stale++;
864 			continue;
865 		}
866 		error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
867 				(freehdr.firstdb + i) * args->geo->fsbcount,
868 				0, &dbp);
869 		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
870 				&error))
871 			goto out;
872 		xchk_directory_check_freesp(sc, lblk, dbp, best);
873 		xfs_trans_brelse(sc->tp, dbp);
874 	}
875 
876 	if (freehdr.nused + stale != freehdr.nvalid)
877 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
878 out:
879 	xfs_trans_brelse(sc->tp, bp);
880 	return error;
881 }
882 
883 /* Check free space information in directories. */
884 STATIC int
885 xchk_directory_blocks(
886 	struct xfs_scrub	*sc)
887 {
888 	struct xfs_bmbt_irec	got;
889 	struct xfs_da_args	args = {
890 		.dp		= sc->ip,
891 		.whichfork	= XFS_DATA_FORK,
892 		.geo		= sc->mp->m_dir_geo,
893 		.trans		= sc->tp,
894 		.owner		= I_INO(sc->ip),
895 	};
896 	struct xfs_ifork	*ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
897 	struct xfs_mount	*mp = sc->mp;
898 	xfs_fileoff_t		leaf_lblk;
899 	xfs_fileoff_t		free_lblk;
900 	xfs_fileoff_t		lblk;
901 	struct xfs_iext_cursor	icur;
902 	xfs_dablk_t		dabno;
903 	xfs_dir2_db_t		last_data_db = 0;
904 	bool			found;
905 	bool			is_block = false;
906 	int			error;
907 
908 	/* Ignore local format directories. */
909 	if (ifp->if_format != XFS_DINODE_FMT_EXTENTS &&
910 	    ifp->if_format != XFS_DINODE_FMT_BTREE)
911 		return 0;
912 
913 	lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
914 	leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
915 	free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
916 
917 	/* Is this a block dir? */
918 	if (xfs_dir2_format(&args, &error) == XFS_DIR2_FMT_BLOCK)
919 		is_block = true;
920 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
921 		goto out;
922 
923 	/* Iterate all the data extents in the directory... */
924 	found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
925 	while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
926 		/* No more data blocks... */
927 		if (got.br_startoff >= leaf_lblk)
928 			break;
929 
930 		/*
931 		 * Check each data block's bestfree data.
932 		 *
933 		 * Iterate all the fsbcount-aligned block offsets in
934 		 * this directory.  The directory block reading code is
935 		 * smart enough to do its own bmap lookups to handle
936 		 * discontiguous directory blocks.  When we're done
937 		 * with the extent record, re-query the bmap at the
938 		 * next fsbcount-aligned offset to avoid redundant
939 		 * block checks.
940 		 */
941 		for (lblk = roundup((xfs_dablk_t)got.br_startoff,
942 				args.geo->fsbcount);
943 		     lblk < got.br_startoff + got.br_blockcount;
944 		     lblk += args.geo->fsbcount) {
945 			last_data_db = xfs_dir2_da_to_db(args.geo, lblk);
946 			error = xchk_directory_data_bestfree(sc, lblk,
947 					is_block);
948 			if (error)
949 				goto out;
950 		}
951 		dabno = got.br_startoff + got.br_blockcount;
952 		lblk = roundup(dabno, args.geo->fsbcount);
953 		found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
954 	}
955 
956 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
957 		goto out;
958 
959 	/* Look for a leaf1 block, which has free info. */
960 	if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
961 	    got.br_startoff == leaf_lblk &&
962 	    got.br_blockcount == args.geo->fsbcount &&
963 	    !xfs_iext_next_extent(ifp, &icur, &got)) {
964 		if (is_block) {
965 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
966 			goto out;
967 		}
968 		error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db,
969 				leaf_lblk);
970 		if (error)
971 			goto out;
972 	}
973 
974 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
975 		goto out;
976 
977 	/* Scan for free blocks */
978 	lblk = free_lblk;
979 	found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
980 	while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
981 		/*
982 		 * Dirs can't have blocks mapped above 2^32.
983 		 * Single-block dirs shouldn't even be here.
984 		 */
985 		lblk = got.br_startoff;
986 		if (lblk & ~0xFFFFFFFFULL) {
987 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
988 			goto out;
989 		}
990 		if (is_block) {
991 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
992 			goto out;
993 		}
994 
995 		/*
996 		 * Check each dir free block's bestfree data.
997 		 *
998 		 * Iterate all the fsbcount-aligned block offsets in
999 		 * this directory.  The directory block reading code is
1000 		 * smart enough to do its own bmap lookups to handle
1001 		 * discontiguous directory blocks.  When we're done
1002 		 * with the extent record, re-query the bmap at the
1003 		 * next fsbcount-aligned offset to avoid redundant
1004 		 * block checks.
1005 		 */
1006 		for (lblk = roundup((xfs_dablk_t)got.br_startoff,
1007 				args.geo->fsbcount);
1008 		     lblk < got.br_startoff + got.br_blockcount;
1009 		     lblk += args.geo->fsbcount) {
1010 			error = xchk_directory_free_bestfree(sc, &args,
1011 					lblk);
1012 			if (error)
1013 				goto out;
1014 		}
1015 		dabno = got.br_startoff + got.br_blockcount;
1016 		lblk = roundup(dabno, args.geo->fsbcount);
1017 		found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
1018 	}
1019 out:
1020 	return error;
1021 }
1022 
1023 /*
1024  * Revalidate a dirent that we collected in the past but couldn't check because
1025  * of lock contention.  Returns 0 if the dirent is still valid, -ENOENT if it
1026  * has gone away on us, or a negative errno.
1027  */
1028 STATIC int
1029 xchk_dir_revalidate_dirent(
1030 	struct xchk_dir		*sd,
1031 	const struct xfs_name	*xname,
1032 	xfs_ino_t		ino)
1033 {
1034 	struct xfs_scrub	*sc = sd->sc;
1035 	xfs_ino_t		child_ino;
1036 	int			error;
1037 
1038 	/*
1039 	 * Look up the directory entry.  If we get -ENOENT, the directory entry
1040 	 * went away and there's nothing to revalidate.  Return any other
1041 	 * error.
1042 	 */
1043 	error = xchk_dir_lookup(sc, sc->ip, xname, &child_ino);
1044 	if (error)
1045 		return error;
1046 
1047 	/* The inode number changed, nothing to revalidate. */
1048 	if (ino != child_ino)
1049 		return -ENOENT;
1050 
1051 	return 0;
1052 }
1053 
1054 /*
1055  * Check a directory entry's parent pointers the slow way, which means we cycle
1056  * locks a bunch and put up with revalidation until we get it done.
1057  */
1058 STATIC int
1059 xchk_dir_slow_dirent(
1060 	struct xchk_dir		*sd,
1061 	struct xchk_dirent	*dirent,
1062 	const struct xfs_name	*xname)
1063 {
1064 	struct xfs_scrub	*sc = sd->sc;
1065 	struct xfs_inode	*ip;
1066 	unsigned int		lockmode;
1067 	int			error;
1068 
1069 	/* Check that the deferred dirent still exists. */
1070 	if (sd->need_revalidate) {
1071 		error = xchk_dir_revalidate_dirent(sd, xname, dirent->ino);
1072 		if (error == -ENOENT)
1073 			return 0;
1074 		if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
1075 					&error))
1076 			return error;
1077 	}
1078 
1079 	error = xchk_iget(sc, dirent->ino, &ip);
1080 	if (error == -EINVAL || error == -ENOENT) {
1081 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
1082 		return 0;
1083 	}
1084 	if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
1085 		return error;
1086 
1087 	/*
1088 	 * If we can grab both IOLOCK and ILOCK of the alleged child, we can
1089 	 * proceed with the validation.
1090 	 */
1091 	lockmode = xchk_dir_lock_child(sc, ip);
1092 	if (lockmode) {
1093 		trace_xchk_dir_slowpath(sc->ip, xname, I_INO(ip));
1094 		goto check_pptr;
1095 	}
1096 
1097 	/*
1098 	 * We couldn't lock the child file.  Drop all the locks and try to
1099 	 * get them again, one at a time.
1100 	 */
1101 	xchk_iunlock(sc, sc->ilock_flags);
1102 	sd->need_revalidate = true;
1103 
1104 	trace_xchk_dir_ultraslowpath(sc->ip, xname, I_INO(ip));
1105 
1106 	error = xchk_dir_trylock_for_pptrs(sc, ip, &lockmode);
1107 	if (error)
1108 		goto out_rele;
1109 
1110 	/* Revalidate, since we just cycled the locks. */
1111 	error = xchk_dir_revalidate_dirent(sd, xname, dirent->ino);
1112 	if (error == -ENOENT) {
1113 		error = 0;
1114 		goto out_unlock;
1115 	}
1116 	if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
1117 		goto out_unlock;
1118 
1119 check_pptr:
1120 	error = xchk_dir_parent_pointer(sd, 0, xname, ip);
1121 out_unlock:
1122 	xfs_iunlock(ip, lockmode);
1123 out_rele:
1124 	xchk_irele(sc, ip);
1125 	return error;
1126 }
1127 
1128 /* Check all the dirents that we deferred the first time around. */
1129 STATIC int
1130 xchk_dir_finish_slow_dirents(
1131 	struct xchk_dir		*sd)
1132 {
1133 	xfarray_idx_t		array_cur;
1134 	int			error;
1135 
1136 	foreach_xfarray_idx(sd->dir_entries, array_cur) {
1137 		struct xchk_dirent	dirent;
1138 
1139 		if (sd->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1140 			return 0;
1141 
1142 		error = xfarray_load(sd->dir_entries, array_cur, &dirent);
1143 		if (error)
1144 			return error;
1145 
1146 		error = xfblob_loadname(sd->dir_names, dirent.name_cookie,
1147 				&sd->xname, dirent.namelen);
1148 		if (error)
1149 			return error;
1150 
1151 		error = xchk_dir_slow_dirent(sd, &dirent, &sd->xname);
1152 		if (error)
1153 			return error;
1154 	}
1155 
1156 	return 0;
1157 }
1158 
1159 /* Scrub a whole directory. */
1160 int
1161 xchk_directory(
1162 	struct xfs_scrub	*sc)
1163 {
1164 	struct xchk_dir		*sd;
1165 	int			error;
1166 
1167 	if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
1168 		return -ENOENT;
1169 
1170 	if (xchk_file_looks_zapped(sc, XFS_SICK_INO_DIR_ZAPPED)) {
1171 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
1172 		return 0;
1173 	}
1174 
1175 	/* Plausible size? */
1176 	if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) {
1177 		xchk_ip_set_corrupt(sc, sc->ip);
1178 		return 0;
1179 	}
1180 
1181 	/* Check directory tree structure */
1182 	error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
1183 	if (error)
1184 		return error;
1185 
1186 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1187 		return 0;
1188 
1189 	/* Check the freespace. */
1190 	error = xchk_directory_blocks(sc);
1191 	if (error)
1192 		return error;
1193 
1194 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1195 		return 0;
1196 
1197 	sd = kvzalloc_obj(struct xchk_dir, XCHK_GFP_FLAGS);
1198 	if (!sd)
1199 		return -ENOMEM;
1200 	sd->sc = sc;
1201 	sd->xname.name = sd->namebuf;
1202 
1203 	if (xfs_has_parent(sc->mp)) {
1204 		/*
1205 		 * Set up some staging memory for dirents that we can't check
1206 		 * due to locking contention.
1207 		 */
1208 		error = xfarray_create("slow directory entries", 0,
1209 				sizeof(struct xchk_dirent), &sd->dir_entries);
1210 		if (error)
1211 			goto out_sd;
1212 
1213 		error = xfblob_create("slow directory entry names",
1214 				&sd->dir_names);
1215 		if (error)
1216 			goto out_entries;
1217 	}
1218 
1219 	/* Look up every name in this directory by hash. */
1220 	error = xchk_dir_walk(sc, sc->ip, xchk_dir_actor, sd);
1221 	if (error == -ECANCELED)
1222 		error = 0;
1223 	if (error)
1224 		goto out_names;
1225 
1226 	if (xfs_has_parent(sc->mp)) {
1227 		error = xchk_dir_finish_slow_dirents(sd);
1228 		if (error == -ETIMEDOUT) {
1229 			/* Couldn't grab a lock, scrub was marked incomplete */
1230 			error = 0;
1231 			goto out_names;
1232 		}
1233 		if (error)
1234 			goto out_names;
1235 	}
1236 
1237 out_names:
1238 	if (sd->dir_names)
1239 		xfblob_destroy(sd->dir_names);
1240 out_entries:
1241 	if (sd->dir_entries)
1242 		xfarray_destroy(sd->dir_entries);
1243 out_sd:
1244 	kvfree(sd);
1245 	if (error)
1246 		return error;
1247 
1248 	/* If the dir is clean, it is clearly not zapped. */
1249 	xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_DIR_ZAPPED);
1250 	return 0;
1251 }
1252 
1253 /*
1254  * Decide if this directory has been zapped to satisfy the inode and ifork
1255  * verifiers.  Checking and repairing should be postponed until the directory
1256  * is fixed.
1257  */
1258 bool
1259 xchk_dir_looks_zapped(
1260 	struct xfs_inode	*dp)
1261 {
1262 	/* Repair zapped this dir's data fork a short time ago */
1263 	if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
1264 		return true;
1265 
1266 	/*
1267 	 * If the dinode repair found a bad data fork, it will reset the fork
1268 	 * to extents format with zero records and wait for the bmapbtd
1269 	 * scrubber to reconstruct the block mappings.  Directories always
1270 	 * contain some content, so this is a clear sign of a zapped directory.
1271 	 * The state checked by xfs_ifork_zapped is not persisted, so this is
1272 	 * the secondary strategy if repairs are interrupted by a crash or an
1273 	 * unmount.
1274 	 */
1275 	return dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS &&
1276 	       dp->i_df.if_nextents == 0;
1277 }
1278