1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2023-2024 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_attr.h"
19 #include "xfs_parent.h"
20 #include "scrub/scrub.h"
21 #include "scrub/common.h"
22 #include "scrub/bitmap.h"
23 #include "scrub/ino_bitmap.h"
24 #include "scrub/xfile.h"
25 #include "scrub/xfarray.h"
26 #include "scrub/xfblob.h"
27 #include "scrub/listxattr.h"
28 #include "scrub/trace.h"
29 #include "scrub/repair.h"
30 #include "scrub/orphanage.h"
31 #include "scrub/dirtree.h"
32
33 /*
34 * Directory Tree Structure Validation
35 * ===================================
36 *
37 * Validating the tree qualities of the directory tree structure can be
38 * difficult. If the tree is frozen, running a depth (or breadth) first search
39 * and marking a bitmap suffices to determine if there is a cycle. XORing the
40 * mark bitmap with the inode bitmap afterwards tells us if there are
41 * disconnected cycles. If the tree is not frozen, directory updates can move
42 * subtrees across the scanner wavefront, which complicates the design greatly.
43 *
44 * Directory parent pointers change that by enabling an incremental approach to
45 * validation of the tree structure. Instead of using one thread to scan the
46 * entire filesystem, we instead can have multiple threads walking individual
47 * subdirectories upwards to the root. In a perfect world, the IOLOCK would
48 * suffice to stabilize two directories in a parent -> child relationship.
49 * Unfortunately, the VFS does not take the IOLOCK when moving a child
50 * subdirectory, so we instead synchronize on ILOCK and use dirent update hooks
51 * to detect a race. If a race occurs in a path, we restart the scan.
52 *
53 * If the walk terminates without reaching the root, we know the path is
54 * disconnected and ought to be attached to the lost and found. If on the walk
55 * we find the same subdir that we're scanning, we know this is a cycle and
56 * should delete an incoming edge. If we find multiple paths to the root, we
57 * know to delete an incoming edge.
58 *
59 * There are two big hitches with this approach: first, all file link counts
60 * must be correct to prevent other writers from doing the wrong thing with the
61 * directory tree structure. Second, because we're walking upwards in a tree
62 * of arbitrary depth, we cannot hold all the ILOCKs. Instead, we will use a
63 * directory update hook to invalidate the scan results if one of the paths
64 * we've scanned has changed.
65 */
66
67 /* Clean up the dirtree checking resources. */
68 STATIC void
xchk_dirtree_buf_cleanup(void * buf)69 xchk_dirtree_buf_cleanup(
70 void *buf)
71 {
72 struct xchk_dirtree *dl = buf;
73 struct xchk_dirpath *path, *n;
74
75 if (dl->scan_ino != NULLFSINO)
76 xfs_dir_hook_del(dl->sc->mp, &dl->dhook);
77
78 xchk_dirtree_for_each_path_safe(dl, path, n) {
79 list_del_init(&path->list);
80 xino_bitmap_destroy(&path->seen_inodes);
81 kfree(path);
82 }
83
84 if (dl->path_names)
85 xfblob_destroy(dl->path_names);
86 dl->path_names = NULL;
87 if (dl->path_steps)
88 xfarray_destroy(dl->path_steps);
89 dl->path_steps = NULL;
90 mutex_destroy(&dl->lock);
91 }
92
93 /* Set us up to look for directory loops. */
94 int
xchk_setup_dirtree(struct xfs_scrub * sc)95 xchk_setup_dirtree(
96 struct xfs_scrub *sc)
97 {
98 struct xchk_dirtree *dl;
99 int error;
100
101 xchk_fsgates_enable(sc, XCHK_FSGATES_DIRENTS);
102
103 if (xchk_could_repair(sc)) {
104 error = xrep_setup_dirtree(sc);
105 if (error)
106 return error;
107 }
108
109 dl = kvzalloc_obj(struct xchk_dirtree, XCHK_GFP_FLAGS);
110 if (!dl)
111 return -ENOMEM;
112 dl->sc = sc;
113 dl->xname.name = dl->namebuf;
114 dl->hook_xname.name = dl->hook_namebuf;
115 INIT_LIST_HEAD(&dl->path_list);
116 dl->root_ino = NULLFSINO;
117 dl->scan_ino = NULLFSINO;
118 dl->parent_ino = NULLFSINO;
119
120 mutex_init(&dl->lock);
121
122 error = xfarray_create("dirtree path steps", 0,
123 sizeof(struct xchk_dirpath_step), &dl->path_steps);
124 if (error)
125 goto out_dl;
126
127 error = xfblob_create("dirtree path names", &dl->path_names);
128 if (error)
129 goto out_steps;
130
131 error = xchk_setup_inode_contents(sc, 0);
132 if (error)
133 goto out_names;
134
135 sc->buf = dl;
136 sc->buf_cleanup = xchk_dirtree_buf_cleanup;
137 return 0;
138
139 out_names:
140 xfblob_destroy(dl->path_names);
141 out_steps:
142 xfarray_destroy(dl->path_steps);
143 out_dl:
144 mutex_destroy(&dl->lock);
145 kvfree(dl);
146 return error;
147 }
148
149 /*
150 * Add the parent pointer described by @dl->pptr to the given path as a new
151 * step. Returns -ELNRNG if the path is too deep.
152 */
153 int
xchk_dirpath_append(struct xchk_dirtree * dl,struct xfs_inode * ip,struct xchk_dirpath * path,const struct xfs_name * name,const struct xfs_parent_rec * pptr)154 xchk_dirpath_append(
155 struct xchk_dirtree *dl,
156 struct xfs_inode *ip,
157 struct xchk_dirpath *path,
158 const struct xfs_name *name,
159 const struct xfs_parent_rec *pptr)
160 {
161 struct xchk_dirpath_step step = {
162 .pptr_rec = *pptr, /* struct copy */
163 .name_len = name->len,
164 };
165 int error;
166
167 /*
168 * If this path is more than 2 billion steps long, this directory tree
169 * is too far gone to fix.
170 */
171 if (path->nr_steps >= XFS_MAXLINK)
172 return -ELNRNG;
173
174 error = xfblob_storename(dl->path_names, &step.name_cookie, name);
175 if (error)
176 return error;
177
178 error = xino_bitmap_set(&path->seen_inodes, I_INO(ip));
179 if (error)
180 return error;
181
182 error = xfarray_append(dl->path_steps, &step);
183 if (error)
184 return error;
185
186 path->nr_steps++;
187 return 0;
188 }
189
190 /*
191 * Create an xchk_path for each parent pointer of the directory that we're
192 * scanning. For each path created, we will eventually try to walk towards the
193 * root with the goal of deleting all parents except for one that leads to the
194 * root.
195 *
196 * Returns -EFSCORRUPTED to signal that the inode being scanned has a corrupt
197 * parent pointer and hence there's no point in continuing; or -ENOSR if there
198 * are too many parent pointers for this directory.
199 */
200 STATIC int
xchk_dirtree_create_path(struct xfs_scrub * sc,struct xfs_inode * ip,unsigned int attr_flags,const unsigned char * name,unsigned int namelen,const void * value,unsigned int valuelen,void * priv)201 xchk_dirtree_create_path(
202 struct xfs_scrub *sc,
203 struct xfs_inode *ip,
204 unsigned int attr_flags,
205 const unsigned char *name,
206 unsigned int namelen,
207 const void *value,
208 unsigned int valuelen,
209 void *priv)
210 {
211 struct xfs_name xname = {
212 .name = name,
213 .len = namelen,
214 };
215 struct xchk_dirtree *dl = priv;
216 struct xchk_dirpath *path;
217 const struct xfs_parent_rec *rec = value;
218 int error;
219
220 if (!(attr_flags & XFS_ATTR_PARENT))
221 return 0;
222
223 error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
224 valuelen, NULL, NULL);
225 if (error)
226 return error;
227
228 /*
229 * If there are more than 2 billion actual parent pointers for this
230 * subdirectory, this fs is too far gone to fix.
231 */
232 if (dl->nr_paths >= XFS_MAXLINK)
233 return -ENOSR;
234
235 trace_xchk_dirtree_create_path(sc, ip, dl->nr_paths, &xname, rec);
236
237 /*
238 * Create a new xchk_path structure to remember this parent pointer
239 * and record the first name step.
240 */
241 path = kmalloc_obj(struct xchk_dirpath, XCHK_GFP_FLAGS);
242 if (!path)
243 return -ENOMEM;
244
245 INIT_LIST_HEAD(&path->list);
246 xino_bitmap_init(&path->seen_inodes);
247 path->nr_steps = 0;
248 path->outcome = XCHK_DIRPATH_SCANNING;
249
250 error = xchk_dirpath_append(dl, sc->ip, path, &xname, rec);
251 if (error)
252 goto out_path;
253
254 path->first_step = xfarray_length(dl->path_steps) - 1;
255 path->second_step = XFARRAY_NULLIDX;
256 path->path_nr = dl->nr_paths;
257
258 list_add_tail(&path->list, &dl->path_list);
259 dl->nr_paths++;
260 return 0;
261 out_path:
262 xino_bitmap_destroy(&path->seen_inodes);
263 kfree(path);
264 return error;
265 }
266
267 /*
268 * Validate that the first step of this path still has a corresponding
269 * parent pointer in @sc->ip. We probably dropped @sc->ip's ILOCK while
270 * walking towards the roots, which is why this is necessary.
271 *
272 * This function has a side effect of loading the first parent pointer of this
273 * path into the parent pointer scratch pad. This prepares us to walk up the
274 * directory tree towards the root. Returns -ESTALE if the scan data is now
275 * out of date.
276 */
277 STATIC int
xchk_dirpath_revalidate(struct xchk_dirtree * dl,struct xchk_dirpath * path)278 xchk_dirpath_revalidate(
279 struct xchk_dirtree *dl,
280 struct xchk_dirpath *path)
281 {
282 struct xfs_scrub *sc = dl->sc;
283 int error;
284
285 /*
286 * Look up the parent pointer that corresponds to the start of this
287 * path. If the parent pointer has disappeared on us, dump all the
288 * scan results and try again.
289 */
290 error = xfs_parent_lookup(sc->tp, sc->ip, &dl->xname, &dl->pptr_rec,
291 &dl->pptr_args);
292 if (error == -ENOATTR) {
293 trace_xchk_dirpath_disappeared(dl->sc, sc->ip, path->path_nr,
294 path->first_step, &dl->xname, &dl->pptr_rec);
295 dl->stale = true;
296 return -ESTALE;
297 }
298
299 return error;
300 }
301
302 /*
303 * Walk the parent pointers of a directory at the end of a path and record
304 * the parent that we find in @dl->xname/pptr_rec.
305 */
306 STATIC int
xchk_dirpath_find_next_step(struct xfs_scrub * sc,struct xfs_inode * ip,unsigned int attr_flags,const unsigned char * name,unsigned int namelen,const void * value,unsigned int valuelen,void * priv)307 xchk_dirpath_find_next_step(
308 struct xfs_scrub *sc,
309 struct xfs_inode *ip,
310 unsigned int attr_flags,
311 const unsigned char *name,
312 unsigned int namelen,
313 const void *value,
314 unsigned int valuelen,
315 void *priv)
316 {
317 struct xchk_dirtree *dl = priv;
318 const struct xfs_parent_rec *rec = value;
319 int error;
320
321 if (!(attr_flags & XFS_ATTR_PARENT))
322 return 0;
323
324 error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
325 valuelen, NULL, NULL);
326 if (error)
327 return error;
328
329 /*
330 * If we've already set @dl->pptr_rec, then this directory has multiple
331 * parents. Signal this back to the caller via -EMLINK.
332 */
333 if (dl->parents_found > 0)
334 return -EMLINK;
335
336 dl->parents_found++;
337 memcpy(dl->namebuf, name, namelen);
338 dl->xname.len = namelen;
339 dl->pptr_rec = *rec; /* struct copy */
340 return 0;
341 }
342
343 /* Set and log the outcome of a path walk. */
344 static inline void
xchk_dirpath_set_outcome(struct xchk_dirtree * dl,struct xchk_dirpath * path,enum xchk_dirpath_outcome outcome)345 xchk_dirpath_set_outcome(
346 struct xchk_dirtree *dl,
347 struct xchk_dirpath *path,
348 enum xchk_dirpath_outcome outcome)
349 {
350 trace_xchk_dirpath_set_outcome(dl->sc, path->path_nr, path->nr_steps,
351 outcome);
352
353 path->outcome = outcome;
354 }
355
356 /*
357 * Scan the directory at the end of this path for its parent directory link.
358 * If we find one, extend the path. Returns -ESTALE if the scan data out of
359 * date. Returns -EFSCORRUPTED if the parent pointer is bad; or -ELNRNG if
360 * the path got too deep.
361 */
362 STATIC int
xchk_dirpath_step_up(struct xchk_dirtree * dl,struct xchk_dirpath * path,bool is_metadir)363 xchk_dirpath_step_up(
364 struct xchk_dirtree *dl,
365 struct xchk_dirpath *path,
366 bool is_metadir)
367 {
368 struct xfs_scrub *sc = dl->sc;
369 struct xfs_inode *dp;
370 xfs_ino_t parent_ino = be64_to_cpu(dl->pptr_rec.p_ino);
371 unsigned int lock_mode;
372 int error = 0;
373
374 if (xchk_should_terminate(sc, &error))
375 return error;
376
377 /* Grab and lock the parent directory. */
378 error = xchk_iget(sc, parent_ino, &dp);
379 switch (error) {
380 case -EINVAL:
381 case -ENOENT:
382 mutex_lock(&dl->lock);
383
384 if (dl->stale) {
385 /* live update detected a change in this path */
386 error = -ESTALE;
387 } else {
388 /* inode doesn't exist, path invalid */
389 error = -EFSCORRUPTED;
390
391 trace_xchk_dirpath_badino(dl->sc, path->path_nr,
392 path->nr_steps, &dl->xname,
393 &dl->pptr_rec);
394 }
395
396 mutex_unlock(&dl->lock);
397 return error;
398 case 0:
399 /* keep going */
400 break;
401 default:
402 return error;
403 }
404
405 lock_mode = xfs_ilock_attr_map_shared(dp);
406 mutex_lock(&dl->lock);
407
408 if (dl->stale) {
409 error = -ESTALE;
410 goto out_scanlock;
411 }
412
413 /* The handle encoded in the parent pointer must match. */
414 if (VFS_I(dp)->i_generation != be32_to_cpu(dl->pptr_rec.p_gen)) {
415 trace_xchk_dirpath_badgen(dl->sc, dp, path->path_nr,
416 path->nr_steps, &dl->xname, &dl->pptr_rec);
417 error = -EFSCORRUPTED;
418 goto out_scanlock;
419 }
420
421 /* We've reached the root directory; the path is ok. */
422 if (parent_ino == dl->root_ino) {
423 xchk_dirpath_set_outcome(dl, path, XCHK_DIRPATH_OK);
424 error = 0;
425 goto out_scanlock;
426 }
427
428 /*
429 * The inode being scanned is its own distant ancestor! Get rid of
430 * this path.
431 */
432 if (parent_ino == I_INO(sc->ip)) {
433 xchk_dirpath_set_outcome(dl, path, XCHK_DIRPATH_DELETE);
434 error = 0;
435 goto out_scanlock;
436 }
437
438 /*
439 * We've seen this inode before during the path walk. There's a loop
440 * above us in the directory tree. This probably means that we cannot
441 * continue, but let's keep walking paths to get a full picture.
442 */
443 if (xino_bitmap_test(&path->seen_inodes, parent_ino)) {
444 xchk_dirpath_set_outcome(dl, path, XCHK_DIRPATH_LOOP);
445 error = 0;
446 goto out_scanlock;
447 }
448
449 /* Parent pointer must point up to a directory. */
450 if (!S_ISDIR(VFS_I(dp)->i_mode)) {
451 trace_xchk_dirpath_nondir_parent(dl->sc, dp, path->path_nr,
452 path->nr_steps, &dl->xname, &dl->pptr_rec);
453 error = -EFSCORRUPTED;
454 goto out_scanlock;
455 }
456
457 /* Parent cannot be an unlinked directory. */
458 if (VFS_I(dp)->i_nlink == 0) {
459 trace_xchk_dirpath_unlinked_parent(dl->sc, dp, path->path_nr,
460 path->nr_steps, &dl->xname, &dl->pptr_rec);
461 error = -EFSCORRUPTED;
462 goto out_scanlock;
463 }
464
465 /* Parent must be in the same directory tree. */
466 if (is_metadir != xfs_is_metadir_inode(dp)) {
467 trace_xchk_dirpath_crosses_tree(dl->sc, dp, path->path_nr,
468 path->nr_steps, &dl->xname, &dl->pptr_rec);
469 error = -EFSCORRUPTED;
470 goto out_scanlock;
471 }
472
473 /*
474 * If the extended attributes look as though they has been zapped by
475 * the inode record repair code, we cannot scan for parent pointers.
476 */
477 if (xchk_pptr_looks_zapped(dp)) {
478 error = -EBUSY;
479 xchk_set_incomplete(sc);
480 goto out_scanlock;
481 }
482
483 /*
484 * Walk the parent pointers of @dp to find the parent of this directory
485 * to find the next step in our walk. If we find that @dp has exactly
486 * one parent, the parent pointer information will be stored in
487 * @dl->pptr_rec. This prepares us for the next step of the walk.
488 */
489 mutex_unlock(&dl->lock);
490 dl->parents_found = 0;
491 error = xchk_xattr_walk(sc, dp, xchk_dirpath_find_next_step, NULL, dl);
492 mutex_lock(&dl->lock);
493 if (error == -EFSCORRUPTED || error == -EMLINK ||
494 (!error && dl->parents_found == 0)) {
495 /*
496 * Further up the directory tree from @sc->ip, we found a
497 * corrupt parent pointer, multiple parent pointers while
498 * finding this directory's parent, or zero parents despite
499 * having a nonzero link count. Keep looking for other paths.
500 */
501 xchk_dirpath_set_outcome(dl, path, XCHK_DIRPATH_CORRUPT);
502 error = 0;
503 goto out_scanlock;
504 }
505 if (error)
506 goto out_scanlock;
507
508 if (dl->stale) {
509 error = -ESTALE;
510 goto out_scanlock;
511 }
512
513 trace_xchk_dirpath_found_next_step(sc, dp, path->path_nr,
514 path->nr_steps, &dl->xname, &dl->pptr_rec);
515
516 /* Append to the path steps */
517 error = xchk_dirpath_append(dl, dp, path, &dl->xname, &dl->pptr_rec);
518 if (error)
519 goto out_scanlock;
520
521 if (path->second_step == XFARRAY_NULLIDX)
522 path->second_step = xfarray_length(dl->path_steps) - 1;
523
524 out_scanlock:
525 mutex_unlock(&dl->lock);
526 xfs_iunlock(dp, lock_mode);
527 xchk_irele(sc, dp);
528 return error;
529 }
530
531 /*
532 * Walk the directory tree upwards towards what is hopefully the root
533 * directory, recording path steps as we go. The current path components are
534 * stored in dl->pptr_rec and dl->xname.
535 *
536 * Returns -ESTALE if the scan data are out of date. Returns -EFSCORRUPTED
537 * only if the direct parent pointer of @sc->ip associated with this path is
538 * corrupt.
539 */
540 STATIC int
xchk_dirpath_walk_upwards(struct xchk_dirtree * dl,struct xchk_dirpath * path)541 xchk_dirpath_walk_upwards(
542 struct xchk_dirtree *dl,
543 struct xchk_dirpath *path)
544 {
545 struct xfs_scrub *sc = dl->sc;
546 bool is_metadir;
547 int error;
548
549 ASSERT(sc->ilock_flags & XFS_ILOCK_EXCL);
550
551 /* Reload the start of this path and make sure it's still there. */
552 error = xchk_dirpath_revalidate(dl, path);
553 if (error)
554 return error;
555
556 trace_xchk_dirpath_walk_upwards(sc, sc->ip, path->path_nr, &dl->xname,
557 &dl->pptr_rec);
558
559 /*
560 * The inode being scanned is its own direct ancestor!
561 * Get rid of this path.
562 */
563 if (be64_to_cpu(dl->pptr_rec.p_ino) == I_INO(sc->ip)) {
564 xchk_dirpath_set_outcome(dl, path, XCHK_DIRPATH_DELETE);
565 return 0;
566 }
567
568 /*
569 * Drop ILOCK_EXCL on the inode being scanned. We still hold
570 * IOLOCK_EXCL on it, so it cannot move around or be renamed.
571 *
572 * Beyond this point we're walking up the directory tree, which means
573 * that we can acquire and drop the ILOCK on an alias of sc->ip. The
574 * ILOCK state is no longer tracked in the scrub context. Hence we
575 * must drop @sc->ip's ILOCK during the walk.
576 */
577 is_metadir = xfs_is_metadir_inode(sc->ip);
578 mutex_unlock(&dl->lock);
579 xchk_iunlock(sc, XFS_ILOCK_EXCL);
580
581 /*
582 * Take the first step in the walk towards the root by checking the
583 * start of this path, which is a direct parent pointer of @sc->ip.
584 * If we see any kind of error here (including corruptions), the parent
585 * pointer of @sc->ip is corrupt. Stop the whole scan.
586 */
587 error = xchk_dirpath_step_up(dl, path, is_metadir);
588 if (error) {
589 xchk_ilock(sc, XFS_ILOCK_EXCL);
590 mutex_lock(&dl->lock);
591 return error;
592 }
593
594 /*
595 * Take steps upward from the second step in this path towards the
596 * root. If we hit corruption errors here, there's a problem
597 * *somewhere* in the path, but we don't need to stop scanning.
598 */
599 while (!error && path->outcome == XCHK_DIRPATH_SCANNING)
600 error = xchk_dirpath_step_up(dl, path, is_metadir);
601
602 /* Retake the locks we had, mark paths, etc. */
603 xchk_ilock(sc, XFS_ILOCK_EXCL);
604 mutex_lock(&dl->lock);
605 if (error == -EFSCORRUPTED) {
606 xchk_dirpath_set_outcome(dl, path, XCHK_DIRPATH_CORRUPT);
607 error = 0;
608 }
609 if (!error && dl->stale)
610 return -ESTALE;
611 return error;
612 }
613
614 /*
615 * Decide if this path step has been touched by this live update. Returns
616 * 1 for yes, 0 for no, or a negative errno.
617 */
618 STATIC int
xchk_dirpath_step_is_stale(struct xchk_dirtree * dl,struct xchk_dirpath * path,unsigned int step_nr,xfarray_idx_t step_idx,struct xfs_dir_update_params * p,xfs_ino_t * cursor)619 xchk_dirpath_step_is_stale(
620 struct xchk_dirtree *dl,
621 struct xchk_dirpath *path,
622 unsigned int step_nr,
623 xfarray_idx_t step_idx,
624 struct xfs_dir_update_params *p,
625 xfs_ino_t *cursor)
626 {
627 struct xchk_dirpath_step step;
628 xfs_ino_t child_ino = *cursor;
629 int error;
630
631 error = xfarray_load(dl->path_steps, step_idx, &step);
632 if (error)
633 return error;
634 *cursor = be64_to_cpu(step.pptr_rec.p_ino);
635
636 /*
637 * If the parent and child being updated are not the ones mentioned in
638 * this path step, the scan data is still ok.
639 */
640 if (I_INO(p->ip) != child_ino || I_INO(p->dp) != *cursor)
641 return 0;
642
643 /*
644 * If the dirent name lengths or byte sequences are different, the scan
645 * data is still ok.
646 */
647 if (p->name->len != step.name_len)
648 return 0;
649
650 error = xfblob_loadname(dl->path_names, step.name_cookie,
651 &dl->hook_xname, step.name_len);
652 if (error)
653 return error;
654
655 if (memcmp(dl->hook_xname.name, p->name->name, p->name->len) != 0)
656 return 0;
657
658 /*
659 * If the update comes from the repair code itself, walk the state
660 * machine forward.
661 */
662 if (I_INO(p->ip) == dl->scan_ino &&
663 path->outcome == XREP_DIRPATH_ADOPTING) {
664 xchk_dirpath_set_outcome(dl, path, XREP_DIRPATH_ADOPTED);
665 return 0;
666 }
667
668 if (I_INO(p->ip) == dl->scan_ino &&
669 path->outcome == XREP_DIRPATH_DELETING) {
670 xchk_dirpath_set_outcome(dl, path, XREP_DIRPATH_DELETED);
671 return 0;
672 }
673
674 /* Exact match, scan data is out of date. */
675 trace_xchk_dirpath_changed(dl->sc, path->path_nr, step_nr, p->dp,
676 p->ip, p->name);
677 return 1;
678 }
679
680 /*
681 * Decide if this path has been touched by this live update. Returns 1 for
682 * yes, 0 for no, or a negative errno.
683 */
684 STATIC int
xchk_dirpath_is_stale(struct xchk_dirtree * dl,struct xchk_dirpath * path,struct xfs_dir_update_params * p)685 xchk_dirpath_is_stale(
686 struct xchk_dirtree *dl,
687 struct xchk_dirpath *path,
688 struct xfs_dir_update_params *p)
689 {
690 xfs_ino_t cursor = dl->scan_ino;
691 xfarray_idx_t idx = path->first_step;
692 unsigned int i;
693 int ret;
694
695 /*
696 * The child being updated has not been seen by this path at all; this
697 * path cannot be stale.
698 */
699 if (!xino_bitmap_test(&path->seen_inodes, I_INO(p->ip)))
700 return 0;
701
702 ret = xchk_dirpath_step_is_stale(dl, path, 0, idx, p, &cursor);
703 if (ret != 0)
704 return ret;
705
706 for (i = 1, idx = path->second_step; i < path->nr_steps; i++, idx++) {
707 ret = xchk_dirpath_step_is_stale(dl, path, i, idx, p, &cursor);
708 if (ret != 0)
709 return ret;
710 }
711
712 return 0;
713 }
714
715 /*
716 * Decide if a directory update from the regular filesystem touches any of the
717 * paths we've scanned, and invalidate the scan data if true.
718 */
719 STATIC int
xchk_dirtree_live_update(struct notifier_block * nb,unsigned long action,void * data)720 xchk_dirtree_live_update(
721 struct notifier_block *nb,
722 unsigned long action,
723 void *data)
724 {
725 struct xfs_dir_update_params *p = data;
726 struct xchk_dirtree *dl;
727 struct xchk_dirpath *path;
728 int ret;
729
730 dl = container_of(nb, struct xchk_dirtree, dhook.dirent_hook.nb);
731
732 trace_xchk_dirtree_live_update(dl->sc, p->dp, action, p->ip, p->delta,
733 p->name);
734
735 mutex_lock(&dl->lock);
736
737 if (dl->stale || dl->aborted)
738 goto out_unlock;
739
740 xchk_dirtree_for_each_path(dl, path) {
741 ret = xchk_dirpath_is_stale(dl, path, p);
742 if (ret < 0) {
743 dl->aborted = true;
744 break;
745 }
746 if (ret == 1) {
747 dl->stale = true;
748 break;
749 }
750 }
751
752 out_unlock:
753 mutex_unlock(&dl->lock);
754 return NOTIFY_DONE;
755 }
756
757 /* Delete all the collected path information. */
758 STATIC void
xchk_dirtree_reset(void * buf)759 xchk_dirtree_reset(
760 void *buf)
761 {
762 struct xchk_dirtree *dl = buf;
763 struct xchk_dirpath *path, *n;
764
765 ASSERT(dl->sc->ilock_flags & XFS_ILOCK_EXCL);
766
767 xchk_dirtree_for_each_path_safe(dl, path, n) {
768 list_del_init(&path->list);
769 xino_bitmap_destroy(&path->seen_inodes);
770 kfree(path);
771 }
772 dl->nr_paths = 0;
773
774 xfarray_truncate(dl->path_steps);
775 xfblob_truncate(dl->path_names);
776
777 dl->stale = false;
778 }
779
780 /*
781 * Load the name/pptr from the first step in this path into @dl->pptr_rec and
782 * @dl->xname.
783 */
784 STATIC int
xchk_dirtree_load_path(struct xchk_dirtree * dl,struct xchk_dirpath * path)785 xchk_dirtree_load_path(
786 struct xchk_dirtree *dl,
787 struct xchk_dirpath *path)
788 {
789 struct xchk_dirpath_step step;
790 int error;
791
792 error = xfarray_load(dl->path_steps, path->first_step, &step);
793 if (error)
794 return error;
795
796 error = xfblob_loadname(dl->path_names, step.name_cookie, &dl->xname,
797 step.name_len);
798 if (error)
799 return error;
800
801 dl->pptr_rec = step.pptr_rec; /* struct copy */
802 return 0;
803 }
804
805 /*
806 * For each parent pointer of this subdir, trace a path upwards towards the
807 * root directory and record what we find. Returns 0 for success;
808 * -EFSCORRUPTED if walking the parent pointers of @sc->ip failed, -ELNRNG if a
809 * path was too deep; -ENOSR if there were too many parent pointers; or
810 * a negative errno.
811 */
812 int
xchk_dirtree_find_paths_to_root(struct xchk_dirtree * dl)813 xchk_dirtree_find_paths_to_root(
814 struct xchk_dirtree *dl)
815 {
816 struct xfs_scrub *sc = dl->sc;
817 struct xchk_dirpath *path;
818 int error = 0;
819
820 do {
821 if (xchk_should_terminate(sc, &error))
822 return error;
823
824 xchk_dirtree_reset(dl);
825
826 /*
827 * If the extended attributes look as though they has been
828 * zapped by the inode record repair code, we cannot scan for
829 * parent pointers.
830 */
831 if (xchk_pptr_looks_zapped(sc->ip)) {
832 xchk_set_incomplete(sc);
833 return -EBUSY;
834 }
835
836 /*
837 * Create path walk contexts for each parent of the directory
838 * that is being scanned. Directories are supposed to have
839 * only one parent, but this is how we detect multiple parents.
840 */
841 error = xchk_xattr_walk(sc, sc->ip, xchk_dirtree_create_path,
842 NULL, dl);
843 if (error)
844 return error;
845
846 xchk_dirtree_for_each_path(dl, path) {
847 /* Load path components into dl->pptr/xname */
848 error = xchk_dirtree_load_path(dl, path);
849 if (error)
850 return error;
851
852 /*
853 * Try to walk up each path to the root. This enables
854 * us to find directory loops in ancestors, and the
855 * like.
856 */
857 error = xchk_dirpath_walk_upwards(dl, path);
858 if (error == -EFSCORRUPTED) {
859 /*
860 * A parent pointer of @sc->ip is bad, don't
861 * bother continuing.
862 */
863 break;
864 }
865 if (error == -ESTALE) {
866 /* This had better be an invalidation. */
867 ASSERT(dl->stale);
868 break;
869 }
870 if (error)
871 return error;
872 if (dl->aborted)
873 return 0;
874 }
875 } while (dl->stale);
876
877 return error;
878 }
879
880 /*
881 * Figure out what to do with the paths we tried to find. Do not call this
882 * if the scan results are stale.
883 */
884 void
xchk_dirtree_evaluate(struct xchk_dirtree * dl,struct xchk_dirtree_outcomes * oc)885 xchk_dirtree_evaluate(
886 struct xchk_dirtree *dl,
887 struct xchk_dirtree_outcomes *oc)
888 {
889 struct xchk_dirpath *path;
890
891 ASSERT(!dl->stale);
892
893 /* Scan the paths we have to decide what to do. */
894 memset(oc, 0, sizeof(struct xchk_dirtree_outcomes));
895 xchk_dirtree_for_each_path(dl, path) {
896 trace_xchk_dirpath_evaluate_path(dl->sc, path->path_nr,
897 path->nr_steps, path->outcome);
898
899 switch (path->outcome) {
900 case XCHK_DIRPATH_SCANNING:
901 /* shouldn't get here */
902 ASSERT(0);
903 break;
904 case XCHK_DIRPATH_DELETE:
905 /* This one is already going away. */
906 oc->bad++;
907 break;
908 case XCHK_DIRPATH_CORRUPT:
909 case XCHK_DIRPATH_LOOP:
910 /* Couldn't find the end of this path. */
911 oc->suspect++;
912 break;
913 case XCHK_DIRPATH_STALE:
914 /* shouldn't get here either */
915 ASSERT(0);
916 break;
917 case XCHK_DIRPATH_OK:
918 /* This path got all the way to the root. */
919 oc->good++;
920 break;
921 case XREP_DIRPATH_DELETING:
922 case XREP_DIRPATH_DELETED:
923 case XREP_DIRPATH_ADOPTING:
924 case XREP_DIRPATH_ADOPTED:
925 /* These should not be in progress! */
926 ASSERT(0);
927 break;
928 }
929 }
930
931 trace_xchk_dirtree_evaluate(dl, oc);
932 }
933
934 /* Look for directory loops. */
935 int
xchk_dirtree(struct xfs_scrub * sc)936 xchk_dirtree(
937 struct xfs_scrub *sc)
938 {
939 struct xchk_dirtree_outcomes oc;
940 struct xchk_dirtree *dl = sc->buf;
941 int error;
942
943 /*
944 * Nondirectories do not point downwards to other files, so they cannot
945 * cause a cycle in the directory tree.
946 */
947 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
948 return -ENOENT;
949
950 ASSERT(xfs_has_parent(sc->mp));
951
952 /*
953 * Find the root of the directory tree. Remember which directory to
954 * scan, because the hook doesn't detach until after sc->ip gets
955 * released during teardown.
956 */
957 dl->root_ino = xchk_inode_rootdir_inum(sc->ip);
958 dl->scan_ino = I_INO(sc->ip);
959
960 trace_xchk_dirtree_start(sc->ip, sc->sm, 0);
961
962 /*
963 * Hook into the directory entry code so that we can capture updates to
964 * paths that we have already scanned. The scanner thread takes each
965 * directory's ILOCK, which means that any in-progress directory update
966 * will finish before we can scan the directory.
967 */
968 ASSERT(sc->flags & XCHK_FSGATES_DIRENTS);
969 xfs_dir_hook_setup(&dl->dhook, xchk_dirtree_live_update);
970 error = xfs_dir_hook_add(sc->mp, &dl->dhook);
971 if (error)
972 goto out;
973
974 mutex_lock(&dl->lock);
975
976 /* Trace each parent pointer's path to the root. */
977 error = xchk_dirtree_find_paths_to_root(dl);
978 if (error == -EFSCORRUPTED || error == -ELNRNG || error == -ENOSR) {
979 /*
980 * Don't bother walking the paths if the xattr structure or the
981 * parent pointers are corrupt; this scan cannot be completed
982 * without full information.
983 */
984 xchk_ip_xref_set_corrupt(sc, sc->ip);
985 error = 0;
986 goto out_scanlock;
987 }
988 if (error == -EBUSY) {
989 /*
990 * We couldn't scan some directory's parent pointers because
991 * the attr fork looked like it had been zapped. The
992 * scan was marked incomplete, so no further error code
993 * is necessary.
994 */
995 error = 0;
996 goto out_scanlock;
997 }
998 if (error)
999 goto out_scanlock;
1000 if (dl->aborted) {
1001 xchk_set_incomplete(sc);
1002 goto out_scanlock;
1003 }
1004
1005 /* Assess what we found in our path evaluation. */
1006 xchk_dirtree_evaluate(dl, &oc);
1007 if (xchk_dirtree_parentless(dl)) {
1008 if (oc.good || oc.bad || oc.suspect)
1009 xchk_ip_set_corrupt(sc, sc->ip);
1010 } else {
1011 if (oc.bad || oc.good + oc.suspect != 1)
1012 xchk_ip_set_corrupt(sc, sc->ip);
1013 if (oc.suspect)
1014 xchk_ip_xref_set_corrupt(sc, sc->ip);
1015 }
1016
1017 out_scanlock:
1018 mutex_unlock(&dl->lock);
1019 out:
1020 trace_xchk_dirtree_done(sc->ip, sc->sm, error);
1021 return error;
1022 }
1023
1024 /* Does the directory targeted by this scrub have no parents? */
1025 bool
xchk_dirtree_parentless(const struct xchk_dirtree * dl)1026 xchk_dirtree_parentless(const struct xchk_dirtree *dl)
1027 {
1028 struct xfs_scrub *sc = dl->sc;
1029
1030 if (xchk_inode_is_dirtree_root(sc->ip))
1031 return true;
1032 if (VFS_I(sc->ip)->i_nlink == 0)
1033 return true;
1034 return false;
1035 }
1036