1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2023-2024 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_trans_space.h"
12 #include "xfs_mount.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode.h"
16 #include "xfs_icache.h"
18 #include "xfs_dir2_priv.h"
20 #include "xfs_parent.h"
21 #include "scrub/scrub.h"
22 #include "scrub/common.h"
23 #include "scrub/bitmap.h"
24 #include "scrub/ino_bitmap.h"
25 #include "scrub/xfile.h"
26 #include "scrub/xfarray.h"
27 #include "scrub/xfblob.h"
28 #include "scrub/listxattr.h"
29 #include "scrub/trace.h"
30 #include "scrub/repair.h"
31 #include "scrub/orphanage.h"
32 #include "scrub/dirtree.h"
33 #include "scrub/readdir.h"
36 * Directory Tree Structure Repairs
37 * ================================
39 * If we decide that the directory being scanned is participating in a
40 * directory loop, the only change we can make is to remove directory entries
41 * pointing down to @sc->ip. If that leaves it with no parents, the directory
42 * should be adopted by the orphanage.
45 /* Set up to repair directory loops. */
50 return xrep_orphanage_try_create(sc
);
53 /* Change the outcome of this path. */
55 xrep_dirpath_set_outcome(
56 struct xchk_dirtree
*dl
,
57 struct xchk_dirpath
*path
,
58 enum xchk_dirpath_outcome outcome
)
60 trace_xrep_dirpath_set_outcome(dl
->sc
, path
->path_nr
, path
->nr_steps
,
63 path
->outcome
= outcome
;
66 /* Delete all paths. */
68 xrep_dirtree_delete_all_paths(
69 struct xchk_dirtree
*dl
,
70 struct xchk_dirtree_outcomes
*oc
)
72 struct xchk_dirpath
*path
;
74 xchk_dirtree_for_each_path(dl
, path
) {
75 switch (path
->outcome
) {
76 case XCHK_DIRPATH_CORRUPT
:
77 case XCHK_DIRPATH_LOOP
:
80 xrep_dirpath_set_outcome(dl
, path
, XCHK_DIRPATH_DELETE
);
85 xrep_dirpath_set_outcome(dl
, path
, XCHK_DIRPATH_DELETE
);
92 ASSERT(oc
->suspect
== 0);
93 ASSERT(oc
->good
== 0);
96 /* Since this is the surviving path, set the dotdot entry to this value. */
98 xrep_dirpath_retain_parent(
99 struct xchk_dirtree
*dl
,
100 struct xchk_dirpath
*path
)
102 struct xchk_dirpath_step step
;
105 error
= xfarray_load(dl
->path_steps
, path
->first_step
, &step
);
109 dl
->parent_ino
= be64_to_cpu(step
.pptr_rec
.p_ino
);
112 /* Find the one surviving path so we know how to set dotdot. */
114 xrep_dirtree_find_surviving_path(
115 struct xchk_dirtree
*dl
,
116 struct xchk_dirtree_outcomes
*oc
)
118 struct xchk_dirpath
*path
;
119 bool foundit
= false;
121 xchk_dirtree_for_each_path(dl
, path
) {
122 switch (path
->outcome
) {
123 case XCHK_DIRPATH_CORRUPT
:
124 case XCHK_DIRPATH_LOOP
:
125 case XCHK_DIRPATH_OK
:
127 xrep_dirpath_retain_parent(dl
, path
);
131 ASSERT(foundit
== false);
138 ASSERT(oc
->suspect
+ oc
->good
== 1);
141 /* Delete all paths except for the one good one. */
143 xrep_dirtree_keep_one_good_path(
144 struct xchk_dirtree
*dl
,
145 struct xchk_dirtree_outcomes
*oc
)
147 struct xchk_dirpath
*path
;
148 bool foundit
= false;
150 xchk_dirtree_for_each_path(dl
, path
) {
151 switch (path
->outcome
) {
152 case XCHK_DIRPATH_CORRUPT
:
153 case XCHK_DIRPATH_LOOP
:
156 xrep_dirpath_set_outcome(dl
, path
, XCHK_DIRPATH_DELETE
);
158 case XCHK_DIRPATH_OK
:
160 xrep_dirpath_retain_parent(dl
, path
);
166 xrep_dirpath_set_outcome(dl
, path
, XCHK_DIRPATH_DELETE
);
173 ASSERT(oc
->suspect
== 0);
174 ASSERT(oc
->good
< 2);
177 /* Delete all paths except for one suspect one. */
179 xrep_dirtree_keep_one_suspect_path(
180 struct xchk_dirtree
*dl
,
181 struct xchk_dirtree_outcomes
*oc
)
183 struct xchk_dirpath
*path
;
184 bool foundit
= false;
186 xchk_dirtree_for_each_path(dl
, path
) {
187 switch (path
->outcome
) {
188 case XCHK_DIRPATH_CORRUPT
:
189 case XCHK_DIRPATH_LOOP
:
191 xrep_dirpath_retain_parent(dl
, path
);
197 xrep_dirpath_set_outcome(dl
, path
, XCHK_DIRPATH_DELETE
);
199 case XCHK_DIRPATH_OK
:
207 ASSERT(oc
->suspect
== 1);
208 ASSERT(oc
->good
== 0);
212 * Figure out what to do with the paths we tried to find. Returns -EDEADLOCK
213 * if the scan results have become stale.
216 xrep_dirtree_decide_fate(
217 struct xchk_dirtree
*dl
,
218 struct xchk_dirtree_outcomes
*oc
)
220 xchk_dirtree_evaluate(dl
, oc
);
222 /* Parentless directories should not have any paths at all. */
223 if (xchk_dirtree_parentless(dl
)) {
224 xrep_dirtree_delete_all_paths(dl
, oc
);
228 /* One path is exactly the number of paths we want. */
229 if (oc
->good
+ oc
->suspect
== 1) {
230 xrep_dirtree_find_surviving_path(dl
, oc
);
234 /* Zero paths means we should reattach the subdir to the orphanage. */
235 if (oc
->good
+ oc
->suspect
== 0) {
236 if (dl
->sc
->orphanage
)
237 oc
->needs_adoption
= true;
242 * Otherwise, this subdirectory has too many parents. If there's at
243 * least one good path, keep it and delete the others.
246 xrep_dirtree_keep_one_good_path(dl
, oc
);
251 * There are no good paths and there are too many suspect paths.
252 * Keep the first suspect path and delete the rest.
254 xrep_dirtree_keep_one_suspect_path(dl
, oc
);
258 * Load the first step of this path into @step and @dl->xname/pptr
259 * for later repair work.
262 xrep_dirtree_prep_path(
263 struct xchk_dirtree
*dl
,
264 struct xchk_dirpath
*path
,
265 struct xchk_dirpath_step
*step
)
269 error
= xfarray_load(dl
->path_steps
, path
->first_step
, step
);
273 error
= xfblob_loadname(dl
->path_names
, step
->name_cookie
, &dl
->xname
,
278 dl
->pptr_rec
= step
->pptr_rec
; /* struct copy */
282 /* Delete the VFS dentry for a removed child. */
284 xrep_dirtree_purge_dentry(
285 struct xchk_dirtree
*dl
,
286 struct xfs_inode
*dp
,
287 const struct xfs_name
*name
)
289 struct qstr qname
= QSTR_INIT(name
->name
, name
->len
);
290 struct dentry
*parent_dentry
, *child_dentry
;
294 * Find the dentry for the parent directory. If there isn't one, we're
295 * done. Caller already holds i_rwsem for parent and child.
297 parent_dentry
= d_find_alias(VFS_I(dp
));
301 /* The VFS thinks the parent is a directory, right? */
302 if (!d_is_dir(parent_dentry
)) {
303 ASSERT(d_is_dir(parent_dentry
));
304 error
= -EFSCORRUPTED
;
305 goto out_dput_parent
;
309 * Try to find the dirent pointing to the child. If there isn't one,
312 qname
.hash
= full_name_hash(parent_dentry
, name
->name
, name
->len
);
313 child_dentry
= d_lookup(parent_dentry
, &qname
);
316 goto out_dput_parent
;
319 trace_xrep_dirtree_delete_child(dp
->i_mount
, child_dentry
);
321 /* Child is not a directory? We're screwed. */
322 if (!d_is_dir(child_dentry
)) {
323 ASSERT(d_is_dir(child_dentry
));
324 error
= -EFSCORRUPTED
;
328 /* Replace the child dentry with a negative one. */
329 d_delete(child_dentry
);
339 * Prepare to delete a link by taking the IOLOCK of the parent and the child
340 * (scrub target). Caller must hold IOLOCK_EXCL on @sc->ip. Returns 0 if we
341 * took both locks, or a negative errno if we couldn't lock the parent in time.
344 xrep_dirtree_unlink_iolock(
345 struct xfs_scrub
*sc
,
346 struct xfs_inode
*dp
)
350 ASSERT(sc
->ilock_flags
& XFS_IOLOCK_EXCL
);
352 if (xfs_ilock_nowait(dp
, XFS_IOLOCK_EXCL
))
355 xchk_iunlock(sc
, XFS_IOLOCK_EXCL
);
357 xfs_ilock(dp
, XFS_IOLOCK_EXCL
);
358 if (xchk_ilock_nowait(sc
, XFS_IOLOCK_EXCL
))
360 xfs_iunlock(dp
, XFS_IOLOCK_EXCL
);
362 if (xchk_should_terminate(sc
, &error
)) {
363 xchk_ilock(sc
, XFS_IOLOCK_EXCL
);
374 * Remove a link from the directory tree and update the dcache. Returns
375 * -ESTALE if the scan data are now out of date.
379 struct xchk_dirtree
*dl
,
380 struct xfs_inode
*dp
,
381 struct xchk_dirpath
*path
,
382 struct xchk_dirpath_step
*step
)
384 struct xfs_scrub
*sc
= dl
->sc
;
385 struct xfs_mount
*mp
= sc
->mp
;
386 xfs_ino_t dotdot_ino
;
387 xfs_ino_t parent_ino
= dl
->parent_ino
;
388 unsigned int resblks
;
392 /* Take IOLOCK_EXCL of the parent and child. */
393 error
= xrep_dirtree_unlink_iolock(sc
, dp
);
398 * Create the transaction that we need to sever the path. Ignore
399 * EDQUOT and ENOSPC being returned via nospace_error because the
400 * directory code can handle a reservationless update.
402 resblks
= xfs_remove_space_res(mp
, step
->name_len
);
403 error
= xfs_trans_alloc_dir(dp
, &M_RES(mp
)->tr_remove
, sc
->ip
,
404 &resblks
, &sc
->tp
, &dontcare
);
409 * Cancel if someone invalidate the paths while we were trying to get
412 mutex_lock(&dl
->lock
);
414 mutex_unlock(&dl
->lock
);
416 goto out_trans_cancel
;
418 xrep_dirpath_set_outcome(dl
, path
, XREP_DIRPATH_DELETING
);
419 mutex_unlock(&dl
->lock
);
421 trace_xrep_dirtree_delete_path(dl
->sc
, sc
->ip
, path
->path_nr
,
422 &dl
->xname
, &dl
->pptr_rec
);
425 * Decide if we need to reset the dotdot entry. Rules:
427 * - If there's a surviving parent, we want dotdot to point there.
428 * - If we don't have any surviving parents, then point dotdot at the
430 * - If dotdot is already set to the value we want, pass in NULLFSINO
431 * for no change necessary.
433 * Do this /before/ we dirty anything, in case the dotdot lookup
436 error
= xchk_dir_lookup(sc
, sc
->ip
, &xfs_name_dotdot
, &dotdot_ino
);
438 goto out_trans_cancel
;
439 if (parent_ino
== NULLFSINO
)
440 parent_ino
= dl
->root_ino
;
441 if (dotdot_ino
== parent_ino
)
442 parent_ino
= NULLFSINO
;
444 /* Drop the link from sc->ip's dotdot entry. */
445 error
= xfs_droplink(sc
->tp
, dp
);
447 goto out_trans_cancel
;
449 /* Reset the dotdot entry to a surviving parent. */
450 if (parent_ino
!= NULLFSINO
) {
451 error
= xfs_dir_replace(sc
->tp
, sc
->ip
, &xfs_name_dotdot
,
454 goto out_trans_cancel
;
457 /* Drop the link from dp to sc->ip. */
458 error
= xfs_droplink(sc
->tp
, sc
->ip
);
460 goto out_trans_cancel
;
462 error
= xfs_dir_removename(sc
->tp
, dp
, &dl
->xname
, sc
->ip
->i_ino
,
465 ASSERT(error
!= -ENOENT
);
466 goto out_trans_cancel
;
469 if (xfs_has_parent(sc
->mp
)) {
470 error
= xfs_parent_removename(sc
->tp
, &dl
->ppargs
, dp
,
473 goto out_trans_cancel
;
477 * Notify dirent hooks that we removed the bad link, invalidate the
478 * dcache, and commit the repair.
480 xfs_dir_update_hook(dp
, sc
->ip
, -1, &dl
->xname
);
481 error
= xrep_dirtree_purge_dentry(dl
, dp
, &dl
->xname
);
483 goto out_trans_cancel
;
485 error
= xrep_trans_commit(sc
);
489 xchk_trans_cancel(sc
);
491 xfs_iunlock(sc
->ip
, XFS_ILOCK_EXCL
);
492 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
494 xfs_iunlock(dp
, XFS_IOLOCK_EXCL
);
499 * Delete a directory entry that points to this directory. Returns -ESTALE
500 * if the scan data are now out of date.
503 xrep_dirtree_delete_path(
504 struct xchk_dirtree
*dl
,
505 struct xchk_dirpath
*path
)
507 struct xchk_dirpath_step step
;
508 struct xfs_scrub
*sc
= dl
->sc
;
509 struct xfs_inode
*dp
;
513 * Load the parent pointer and directory inode for this path, then
514 * drop the scan lock, the ILOCK, and the transaction so that
515 * _delete_path can reserve the proper transaction. This sets up
516 * @dl->xname for the deletion.
518 error
= xrep_dirtree_prep_path(dl
, path
, &step
);
522 error
= xchk_iget(sc
, be64_to_cpu(step
.pptr_rec
.p_ino
), &dp
);
526 mutex_unlock(&dl
->lock
);
527 xchk_trans_cancel(sc
);
528 xchk_iunlock(sc
, XFS_ILOCK_EXCL
);
530 /* Delete the directory link and release the parent. */
531 error
= xrep_dirtree_unlink(dl
, dp
, path
, &step
);
535 * Retake all the resources we had at the beginning even if the repair
536 * failed or the scan data are now stale. This keeps things simple for
539 xchk_trans_alloc_empty(sc
);
540 xchk_ilock(sc
, XFS_ILOCK_EXCL
);
541 mutex_lock(&dl
->lock
);
543 if (!error
&& dl
->stale
)
548 /* Add a new path to represent our in-progress adoption. */
550 xrep_dirtree_create_adoption_path(
551 struct xchk_dirtree
*dl
)
553 struct xfs_scrub
*sc
= dl
->sc
;
554 struct xchk_dirpath
*path
;
558 * We should have capped the number of paths at XFS_MAXLINK-1 in the
561 if (dl
->nr_paths
> XFS_MAXLINK
) {
562 ASSERT(dl
->nr_paths
<= XFS_MAXLINK
);
563 return -EFSCORRUPTED
;
567 * Create a new xchk_path structure to remember this parent pointer
568 * and record the first name step.
570 path
= kmalloc(sizeof(struct xchk_dirpath
), XCHK_GFP_FLAGS
);
574 INIT_LIST_HEAD(&path
->list
);
575 xino_bitmap_init(&path
->seen_inodes
);
577 path
->outcome
= XREP_DIRPATH_ADOPTING
;
580 * Record the new link that we just created in the orphanage. Because
581 * adoption is the last repair that we perform, we don't bother filling
582 * in the path all the way back to the root.
584 xfs_inode_to_parent_rec(&dl
->pptr_rec
, sc
->orphanage
);
586 error
= xino_bitmap_set(&path
->seen_inodes
, sc
->orphanage
->i_ino
);
590 trace_xrep_dirtree_create_adoption(sc
, sc
->ip
, dl
->nr_paths
,
591 &dl
->xname
, &dl
->pptr_rec
);
593 error
= xchk_dirpath_append(dl
, sc
->ip
, path
, &dl
->xname
,
598 path
->first_step
= xfarray_length(dl
->path_steps
) - 1;
599 path
->second_step
= XFARRAY_NULLIDX
;
600 path
->path_nr
= dl
->nr_paths
;
602 list_add_tail(&path
->list
, &dl
->path_list
);
612 * Prepare to move a file to the orphanage by taking the IOLOCK of the
613 * orphanage and the child (scrub target). Caller must hold IOLOCK_EXCL on
614 * @sc->ip. Returns 0 if we took both locks, or a negative errno if we
615 * couldn't lock the orphanage in time.
618 xrep_dirtree_adopt_iolock(
619 struct xfs_scrub
*sc
)
623 ASSERT(sc
->ilock_flags
& XFS_IOLOCK_EXCL
);
625 if (xrep_orphanage_ilock_nowait(sc
, XFS_IOLOCK_EXCL
))
628 xchk_iunlock(sc
, XFS_IOLOCK_EXCL
);
630 xrep_orphanage_ilock(sc
, XFS_IOLOCK_EXCL
);
631 if (xchk_ilock_nowait(sc
, XFS_IOLOCK_EXCL
))
633 xrep_orphanage_iunlock(sc
, XFS_IOLOCK_EXCL
);
635 if (xchk_should_terminate(sc
, &error
)) {
636 xchk_ilock(sc
, XFS_IOLOCK_EXCL
);
647 * Reattach this orphaned directory to the orphanage. Do not call this with
648 * any resources held. Returns -ESTALE if the scan data have become out of
653 struct xchk_dirtree
*dl
)
655 struct xfs_scrub
*sc
= dl
->sc
;
658 /* Take the IOLOCK of the orphanage and the scrub target. */
659 error
= xrep_dirtree_adopt_iolock(sc
);
664 * Set up for an adoption. The directory tree fixer runs after the
665 * link counts have been corrected. Therefore, we must bump the
666 * child's link count since there will be no further opportunity to fix
669 error
= xrep_adoption_trans_alloc(sc
, &dl
->adoption
);
672 dl
->adoption
.bump_child_nlink
= true;
674 /* Figure out what name we're going to use here. */
675 error
= xrep_adoption_compute_name(&dl
->adoption
, &dl
->xname
);
680 * Now that we have a proposed name for the orphanage entry, create
681 * a faux path so that the live update hook will see it.
683 mutex_lock(&dl
->lock
);
685 mutex_unlock(&dl
->lock
);
689 error
= xrep_dirtree_create_adoption_path(dl
);
690 mutex_unlock(&dl
->lock
);
694 /* Reparent the directory. */
695 error
= xrep_adoption_move(&dl
->adoption
);
700 * Commit the name and release all inode locks except for the scrub
703 error
= xrep_trans_commit(sc
);
707 xchk_trans_cancel(sc
);
709 xchk_iunlock(sc
, XFS_ILOCK_EXCL
);
710 xrep_orphanage_iunlock(sc
, XFS_ILOCK_EXCL
);
712 xrep_orphanage_iunlock(sc
, XFS_IOLOCK_EXCL
);
717 * This newly orphaned directory needs to be adopted by the orphanage.
721 xrep_dirtree_move_to_orphanage(
722 struct xchk_dirtree
*dl
)
724 struct xfs_scrub
*sc
= dl
->sc
;
728 * Start by dropping all the resources that we hold so that we can grab
729 * all the resources that we need for the adoption.
731 mutex_unlock(&dl
->lock
);
732 xchk_trans_cancel(sc
);
733 xchk_iunlock(sc
, XFS_ILOCK_EXCL
);
735 /* Perform the adoption. */
736 error
= xrep_dirtree_adopt(dl
);
739 * Retake all the resources we had at the beginning even if the repair
740 * failed or the scan data are now stale. This keeps things simple for
743 xchk_trans_alloc_empty(sc
);
744 xchk_ilock(sc
, XFS_ILOCK_EXCL
);
745 mutex_lock(&dl
->lock
);
747 if (!error
&& dl
->stale
)
753 * Try to fix all the problems. Returns -ESTALE if the scan data have become
757 xrep_dirtree_fix_problems(
758 struct xchk_dirtree
*dl
,
759 struct xchk_dirtree_outcomes
*oc
)
761 struct xchk_dirpath
*path
;
764 /* Delete all the paths we don't want. */
765 xchk_dirtree_for_each_path(dl
, path
) {
766 if (path
->outcome
!= XCHK_DIRPATH_DELETE
)
769 error
= xrep_dirtree_delete_path(dl
, path
);
774 /* Reparent this directory to the orphanage. */
775 if (oc
->needs_adoption
) {
776 if (xrep_orphanage_can_adopt(dl
->sc
))
777 return xrep_dirtree_move_to_orphanage(dl
);
778 return -EFSCORRUPTED
;
784 /* Fix directory loops involving this directory. */
787 struct xfs_scrub
*sc
)
789 struct xchk_dirtree
*dl
= sc
->buf
;
790 struct xchk_dirtree_outcomes oc
;
794 * Prepare to fix the directory tree by retaking the scan lock. The
795 * order of resource acquisition is still IOLOCK -> transaction ->
796 * ILOCK -> scan lock.
798 mutex_lock(&dl
->lock
);
801 * Decide what we're going to do, then do it. An -ESTALE
802 * return here means the scan results are invalid and we have
806 xrep_dirtree_decide_fate(dl
, &oc
);
808 trace_xrep_dirtree_decided_fate(dl
, &oc
);
810 error
= xrep_dirtree_fix_problems(dl
, &oc
);
811 if (!error
|| error
!= -ESTALE
)
814 error
= xchk_dirtree_find_paths_to_root(dl
);
815 if (error
== -ELNRNG
|| error
== -ENOSR
)
816 error
= -EFSCORRUPTED
;
818 mutex_unlock(&dl
->lock
);