1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2021-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_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_bmap_util.h"
17 #include "xfs_iwalk.h"
18 #include "xfs_ialloc.h"
22 #include "xfs_parent.h"
23 #include "scrub/scrub.h"
24 #include "scrub/common.h"
25 #include "scrub/repair.h"
26 #include "scrub/xfile.h"
27 #include "scrub/xfarray.h"
28 #include "scrub/iscan.h"
29 #include "scrub/orphanage.h"
30 #include "scrub/nlinks.h"
31 #include "scrub/trace.h"
32 #include "scrub/tempfile.h"
35 * Live Inode Link Count Repair
36 * ============================
38 * Use the live inode link count information that we collected to replace the
39 * nlink values of the incore inodes. A scrub->repair cycle should have left
40 * the live data and hooks active, so this is safe so long as we make sure the
44 /* Set up to repair inode link counts. */
49 return xrep_orphanage_try_create(sc
);
53 * Inodes that aren't the root directory or the orphanage, have a nonzero link
54 * count, and no observed parents should be moved to the orphanage.
57 xrep_nlinks_is_orphaned(
60 unsigned int actual_nlink
,
61 const struct xchk_nlink
*obs
)
63 if (obs
->parents
!= 0)
65 if (xchk_inode_is_dirtree_root(ip
) || ip
== sc
->orphanage
)
67 return actual_nlink
!= 0;
70 /* Remove an inode from the unlinked list. */
72 xrep_nlinks_iunlink_remove(
75 struct xfs_perag
*pag
;
78 pag
= xfs_perag_get(sc
->mp
, XFS_INO_TO_AGNO(sc
->mp
, sc
->ip
->i_ino
));
79 error
= xfs_iunlink_remove(sc
->tp
, pag
, sc
->ip
);
85 * Correct the link count of the given inode. Because we have to grab locks
86 * and resources in a certain order, it's possible that this will be a no-op.
89 xrep_nlinks_repair_inode(
90 struct xchk_nlink_ctrs
*xnc
)
92 struct xchk_nlink obs
;
93 struct xfs_scrub
*sc
= xnc
->sc
;
94 struct xfs_mount
*mp
= sc
->mp
;
95 struct xfs_inode
*ip
= sc
->ip
;
97 uint64_t actual_nlink
;
98 bool orphanage_available
= false;
103 * Ignore temporary files being used to stage repairs, since we assume
104 * they're correct for non-directories, and the directory repair code
105 * doesn't bump the link counts for the children.
107 if (xrep_is_tempfile(ip
))
111 * If the filesystem has an orphanage attached to the scrub context,
112 * prepare for a link count repair that could involve @ip being adopted
115 if (xrep_orphanage_can_adopt(sc
)) {
116 error
= xrep_orphanage_iolock_two(sc
);
120 error
= xrep_adoption_trans_alloc(sc
, &xnc
->adoption
);
122 xchk_iunlock(sc
, XFS_IOLOCK_EXCL
);
123 xrep_orphanage_iunlock(sc
, XFS_IOLOCK_EXCL
);
125 orphanage_available
= true;
130 * Either there is no orphanage or we couldn't allocate resources for
131 * that kind of update. Let's try again with only the resources we
132 * need for a simple link count update, since that's much more common.
134 if (!orphanage_available
) {
135 xchk_ilock(sc
, XFS_IOLOCK_EXCL
);
137 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_link
, 0, 0, 0,
140 xchk_iunlock(sc
, XFS_IOLOCK_EXCL
);
144 xchk_ilock(sc
, XFS_ILOCK_EXCL
);
145 xfs_trans_ijoin(sc
->tp
, ip
, 0);
148 mutex_lock(&xnc
->lock
);
150 if (xchk_iscan_aborted(&xnc
->collect_iscan
)) {
155 error
= xfarray_load_sparse(xnc
->nlinks
, ip
->i_ino
, &obs
);
160 * We're done accessing the shared scan data, so we can drop the lock.
161 * We still hold @ip's ILOCK, so its link count cannot change.
163 mutex_unlock(&xnc
->lock
);
165 total_links
= xchk_nlink_total(ip
, &obs
);
166 actual_nlink
= VFS_I(ip
)->i_nlink
;
169 * Non-directories cannot have directories pointing up to them.
171 * We previously set error to zero, but set it again because one static
172 * checker author fears that programmers will fail to maintain this
173 * invariant and built their tool to flag this as a security risk. A
174 * different tool author made their bot complain about the redundant
175 * store. This is a never-ending and stupid battle; both tools missed
176 * *actual bugs* elsewhere; and I no longer care.
178 if (!S_ISDIR(VFS_I(ip
)->i_mode
) && obs
.children
!= 0) {
179 trace_xrep_nlinks_unfixable_inode(mp
, ip
, &obs
);
185 * Decide if we're going to move this file to the orphanage, and fix
186 * up the incore link counts if we are.
188 if (orphanage_available
&&
189 xrep_nlinks_is_orphaned(sc
, ip
, actual_nlink
, &obs
)) {
190 /* Figure out what name we're going to use here. */
191 error
= xrep_adoption_compute_name(&xnc
->adoption
, &xnc
->xname
);
196 * Reattach this file to the directory tree by moving it to
197 * the orphanage per the adoption parameters that we already
200 error
= xrep_adoption_move(&xnc
->adoption
);
205 * Re-read the link counts since the reparenting will have
206 * updated our scan info.
208 mutex_lock(&xnc
->lock
);
209 error
= xfarray_load_sparse(xnc
->nlinks
, ip
->i_ino
, &obs
);
210 mutex_unlock(&xnc
->lock
);
214 total_links
= xchk_nlink_total(ip
, &obs
);
215 actual_nlink
= VFS_I(ip
)->i_nlink
;
220 * If this inode is linked from the directory tree and on the unlinked
221 * list, remove it from the unlinked list.
223 if (total_links
> 0 && xfs_inode_on_unlinked_list(ip
)) {
224 error
= xrep_nlinks_iunlink_remove(sc
);
231 * If this inode is not linked from the directory tree yet not on the
232 * unlinked list, put it on the unlinked list.
234 if (total_links
== 0 && !xfs_inode_on_unlinked_list(ip
)) {
235 error
= xfs_iunlink(sc
->tp
, ip
);
241 /* Commit the new link count if it changed. */
242 if (total_links
!= actual_nlink
) {
243 trace_xrep_nlinks_update_inode(mp
, ip
, &obs
);
245 set_nlink(VFS_I(ip
), min_t(unsigned long long, total_links
,
255 xfs_trans_log_inode(sc
->tp
, ip
, XFS_ILOG_CORE
);
257 error
= xrep_trans_commit(sc
);
261 mutex_unlock(&xnc
->lock
);
263 xchk_trans_cancel(sc
);
265 xchk_iunlock(sc
, XFS_ILOCK_EXCL
);
266 if (orphanage_available
) {
267 xrep_orphanage_iunlock(sc
, XFS_ILOCK_EXCL
);
268 xrep_orphanage_iunlock(sc
, XFS_IOLOCK_EXCL
);
270 xchk_iunlock(sc
, XFS_IOLOCK_EXCL
);
275 * Try to visit every inode in the filesystem for repairs. Move on if we can't
276 * grab an inode, since we're still making forward progress.
280 struct xchk_nlink_ctrs
*xnc
,
281 struct xfs_inode
**ipp
)
286 error
= xchk_iscan_iter(&xnc
->compare_iscan
, ipp
);
287 } while (error
== -EBUSY
);
292 /* Commit the new inode link counters. */
295 struct xfs_scrub
*sc
)
297 struct xchk_nlink_ctrs
*xnc
= sc
->buf
;
301 * We need ftype for an accurate count of the number of child
302 * subdirectory links. Child subdirectories with a back link (dotdot
303 * entry) but no forward link are moved to the orphanage, so we cannot
304 * repair the link count of the parent directory based on the back link
305 * count alone. Filesystems without ftype support are rare (old V4) so
306 * we just skip out here.
308 if (!xfs_has_ftype(sc
->mp
))
312 * Use the inobt to walk all allocated inodes to compare and fix the
313 * link counts. Retry iget every tenth of a second for up to 30
314 * seconds -- even if repair misses a few inodes, we still try to fix
315 * as many of them as we can.
317 xchk_iscan_start(sc
, 30000, 100, &xnc
->compare_iscan
);
318 ASSERT(sc
->ip
== NULL
);
320 while ((error
= xrep_nlinks_iter(xnc
, &sc
->ip
)) == 1) {
322 * Commit the scrub transaction so that we can create repair
323 * transactions with the correct reservations.
325 xchk_trans_cancel(sc
);
327 error
= xrep_nlinks_repair_inode(xnc
);
328 xchk_iscan_mark_visited(&xnc
->compare_iscan
, sc
->ip
);
329 xchk_irele(sc
, sc
->ip
);
334 if (xchk_should_terminate(sc
, &error
))
338 * Create a new empty transaction so that we can advance the
339 * iscan cursor without deadlocking if the inobt has a cycle.
340 * We can only push the inactivation workqueues with an empty
343 error
= xchk_trans_alloc_empty(sc
);
347 xchk_iscan_iter_finish(&xnc
->compare_iscan
);
348 xchk_iscan_teardown(&xnc
->compare_iscan
);