1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2020-2024 Oracle. All Rights Reserved.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_defer.h"
14 #include "xfs_log_format.h"
15 #include "xfs_trans.h"
17 #include "xfs_inode.h"
18 #include "xfs_icache.h"
19 #include "xfs_da_format.h"
20 #include "xfs_da_btree.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_dir2_priv.h"
24 #include "xfs_trans_space.h"
25 #include "xfs_health.h"
26 #include "xfs_exchmaps.h"
27 #include "xfs_parent.h"
28 #include "scrub/xfs_scrub.h"
29 #include "scrub/scrub.h"
30 #include "scrub/common.h"
31 #include "scrub/trace.h"
32 #include "scrub/repair.h"
33 #include "scrub/iscan.h"
34 #include "scrub/findparent.h"
35 #include "scrub/readdir.h"
36 #include "scrub/tempfile.h"
37 #include "scrub/listxattr.h"
40 * Finding the Parent of a Directory
41 * =================================
43 * Directories have parent pointers, in the sense that each directory contains
44 * a dotdot entry that points to the single allowed parent. The brute force
45 * way to find the parent of a given directory is to scan every directory in
46 * the filesystem looking for a child dirent that references this directory.
48 * This module wraps the process of scanning the directory tree. It requires
49 * that @sc->ip is the directory whose parent we want to find, and that the
50 * caller hold only the IOLOCK on that directory. The scan itself needs to
51 * take the ILOCK of each directory visited.
53 * Because we cannot hold @sc->ip's ILOCK during a scan of the whole fs, it is
54 * necessary to use dirent hook to update the parent scan results. Callers
55 * must not read the scan results without re-taking @sc->ip's ILOCK.
57 * There are a few shortcuts that we can take to avoid scanning the entire
58 * filesystem, such as noticing directory tree roots and querying the dentry
59 * cache for parent information.
62 struct xrep_findparent_info {
63 /* The directory currently being scanned. */
67 * Scrub context. We're looking for a @dp containing a directory
68 * entry pointing to sc->ip->i_ino.
72 /* Optional scan information for a xrep_findparent_scan call. */
73 struct xrep_parent_scan_info *parent_scan;
76 * Parent that we've found for sc->ip. If we're scanning the entire
77 * directory tree, we need this to ensure that we only find /one/
80 xfs_ino_t found_parent;
83 * This is set to true if @found_parent was not observed directly from
84 * the directory scan but by noticing a change in dotdot entries after
85 * cycling the sc->ip IOLOCK.
87 bool parent_tentative;
91 * If this directory entry points to the scrub target inode, then the directory
92 * we're scanning is the parent of the scrub target inode.
95 xrep_findparent_dirent(
98 xfs_dir2_dataptr_t dapos,
99 const struct xfs_name *name,
103 struct xrep_findparent_info *fpi = priv;
106 if (xchk_should_terminate(fpi->sc, &error))
109 if (ino != fpi->sc->ip->i_ino)
112 /* Ignore garbage directory entry names. */
113 if (name->len == 0 || !xfs_dir2_namecheck(name->name, name->len))
114 return -EFSCORRUPTED;
117 * Ignore dotdot and dot entries -- we're looking for parent -> child
120 if (name->name[0] == '.' && (name->len == 1 ||
121 (name->len == 2 && name->name[1] == '.')))
124 /* Uhoh, more than one parent for a dir? */
125 if (fpi->found_parent != NULLFSINO &&
126 !(fpi->parent_tentative && fpi->found_parent == fpi->dp->i_ino)) {
127 trace_xrep_findparent_dirent(fpi->sc->ip, 0);
128 return -EFSCORRUPTED;
131 /* We found a potential parent; remember this. */
132 trace_xrep_findparent_dirent(fpi->sc->ip, fpi->dp->i_ino);
133 fpi->found_parent = fpi->dp->i_ino;
134 fpi->parent_tentative = false;
136 if (fpi->parent_scan)
137 xrep_findparent_scan_found(fpi->parent_scan, fpi->dp->i_ino);
143 * If this is a directory, walk the dirents looking for any that point to the
144 * scrub target inode.
147 xrep_findparent_walk_directory(
148 struct xrep_findparent_info *fpi)
150 struct xfs_scrub *sc = fpi->sc;
151 struct xfs_inode *dp = fpi->dp;
152 unsigned int lock_mode;
156 * The inode being scanned cannot be its own parent, nor can any
157 * temporary directory we created to stage this repair.
159 if (dp == sc->ip || dp == sc->tempip)
163 * Similarly, temporary files created to stage a repair cannot be the
164 * parent of this inode.
166 if (xrep_is_tempfile(dp))
170 * Scan the directory to see if there it contains an entry pointing to
171 * the directory that we are repairing.
173 lock_mode = xfs_ilock_data_map_shared(dp);
175 /* Don't mix metadata and regular directory trees. */
176 if (xfs_is_metadir_inode(dp) != xfs_is_metadir_inode(sc->ip))
180 * If this directory is known to be sick, we cannot scan it reliably
183 if (xfs_inode_has_sickness(dp, XFS_SICK_INO_CORE |
186 error = -EFSCORRUPTED;
191 * We cannot complete our parent pointer scan if a directory looks as
192 * though it has been zapped by the inode record repair code.
194 if (xchk_dir_looks_zapped(dp)) {
199 error = xchk_dir_walk(sc, dp, xrep_findparent_dirent, fpi);
204 xfs_iunlock(dp, lock_mode);
209 * Update this directory's dotdot pointer based on ongoing dirent updates.
212 xrep_findparent_live_update(
213 struct notifier_block *nb,
214 unsigned long action,
217 struct xfs_dir_update_params *p = data;
218 struct xrep_parent_scan_info *pscan;
219 struct xfs_scrub *sc;
221 pscan = container_of(nb, struct xrep_parent_scan_info,
222 dhook.dirent_hook.nb);
226 * If @p->ip is the subdirectory that we're interested in and we've
227 * already scanned @p->dp, update the dotdot target inumber to the
230 if (p->ip->i_ino == sc->ip->i_ino &&
231 xchk_iscan_want_live_update(&pscan->iscan, p->dp->i_ino)) {
233 xrep_findparent_scan_found(pscan, p->dp->i_ino);
235 xrep_findparent_scan_found(pscan, NULLFSINO);
243 * Set up a scan to find the parent of a directory. The provided dirent hook
244 * will be called when there is a dotdot update for the inode being repaired.
247 __xrep_findparent_scan_start(
248 struct xfs_scrub *sc,
249 struct xrep_parent_scan_info *pscan,
250 notifier_fn_t custom_fn)
254 if (!(sc->flags & XCHK_FSGATES_DIRENTS)) {
255 ASSERT(sc->flags & XCHK_FSGATES_DIRENTS);
260 pscan->parent_ino = NULLFSINO;
262 mutex_init(&pscan->lock);
264 xchk_iscan_start(sc, 30000, 100, &pscan->iscan);
267 * Hook into the dirent update code. The hook only operates on inodes
268 * that were already scanned, and the scanner thread takes each inode's
269 * ILOCK, which means that any in-progress inode updates will finish
270 * before we can scan the inode.
273 xfs_dir_hook_setup(&pscan->dhook, custom_fn);
275 xfs_dir_hook_setup(&pscan->dhook, xrep_findparent_live_update);
276 error = xfs_dir_hook_add(sc->mp, &pscan->dhook);
282 xchk_iscan_teardown(&pscan->iscan);
283 mutex_destroy(&pscan->lock);
288 * Scan the entire filesystem looking for a parent inode for the inode being
289 * scrubbed. @sc->ip must not be the root of a directory tree. Callers must
290 * not hold a dirty transaction or any lock that would interfere with taking
293 * Returns 0 with @pscan->parent_ino set to the parent that we found.
294 * Returns 0 with @pscan->parent_ino set to NULLFSINO if we found no parents.
295 * Returns the usual negative errno if something else happened.
298 xrep_findparent_scan(
299 struct xrep_parent_scan_info *pscan)
301 struct xrep_findparent_info fpi = {
303 .found_parent = NULLFSINO,
304 .parent_scan = pscan,
306 struct xfs_scrub *sc = pscan->sc;
309 ASSERT(S_ISDIR(VFS_IC(sc->ip)->i_mode));
311 while ((ret = xchk_iscan_iter(&pscan->iscan, &fpi.dp)) == 1) {
312 if (S_ISDIR(VFS_I(fpi.dp)->i_mode))
313 ret = xrep_findparent_walk_directory(&fpi);
316 xchk_iscan_mark_visited(&pscan->iscan, fpi.dp);
317 xchk_irele(sc, fpi.dp);
321 if (xchk_should_terminate(sc, &ret))
324 xchk_iscan_iter_finish(&pscan->iscan);
329 /* Tear down a parent scan. */
331 xrep_findparent_scan_teardown(
332 struct xrep_parent_scan_info *pscan)
334 xfs_dir_hook_del(pscan->sc->mp, &pscan->dhook);
335 xchk_iscan_teardown(&pscan->iscan);
336 mutex_destroy(&pscan->lock);
339 /* Finish a parent scan early. */
341 xrep_findparent_scan_finish_early(
342 struct xrep_parent_scan_info *pscan,
345 xrep_findparent_scan_found(pscan, ino);
346 xchk_iscan_finish_early(&pscan->iscan);
350 * Confirm that the directory @parent_ino actually contains a directory entry
351 * pointing to the child @sc->ip->ino. This function returns one of several
354 * Returns 0 with @parent_ino unchanged if the parent was confirmed.
355 * Returns 0 with @parent_ino set to NULLFSINO if the parent was not valid.
356 * Returns the usual negative errno if something else happened.
359 xrep_findparent_confirm(
360 struct xfs_scrub *sc,
361 xfs_ino_t *parent_ino)
363 struct xrep_findparent_info fpi = {
365 .found_parent = NULLFSINO,
369 /* The root directory always points to itself. */
370 if (sc->ip == sc->mp->m_rootip) {
371 *parent_ino = sc->mp->m_sb.sb_rootino;
375 /* The metadata root directory always points to itself. */
376 if (sc->ip == sc->mp->m_metadirip) {
377 *parent_ino = sc->mp->m_sb.sb_metadirino;
381 /* Unlinked dirs can point anywhere; point them up to the root dir. */
382 if (VFS_I(sc->ip)->i_nlink == 0) {
383 *parent_ino = xchk_inode_rootdir_inum(sc->ip);
387 /* Reject garbage parent inode numbers and self-referential parents. */
388 if (*parent_ino == NULLFSINO)
390 if (!xfs_verify_dir_ino(sc->mp, *parent_ino) ||
391 *parent_ino == sc->ip->i_ino) {
392 *parent_ino = NULLFSINO;
396 error = xchk_iget(sc, *parent_ino, &fpi.dp);
400 if (!S_ISDIR(VFS_I(fpi.dp)->i_mode)) {
401 *parent_ino = NULLFSINO;
405 error = xrep_findparent_walk_directory(&fpi);
409 *parent_ino = fpi.found_parent;
411 xchk_irele(sc, fpi.dp);
416 * If we're the root of a directory tree, we are our own parent. If we're an
417 * unlinked directory, the parent /won't/ have a link to us. Set the parent
418 * directory to the root for both cases. Returns NULLFSINO if we don't know
422 xrep_findparent_self_reference(
423 struct xfs_scrub *sc)
425 if (sc->ip->i_ino == sc->mp->m_sb.sb_rootino)
426 return sc->mp->m_sb.sb_rootino;
428 if (sc->ip->i_ino == sc->mp->m_sb.sb_metadirino)
429 return sc->mp->m_sb.sb_metadirino;
431 if (VFS_I(sc->ip)->i_nlink == 0)
432 return xchk_inode_rootdir_inum(sc->ip);
437 /* Check the dentry cache to see if knows of a parent for the scrub target. */
439 xrep_findparent_from_dcache(
440 struct xfs_scrub *sc)
442 struct inode *pip = NULL;
443 struct dentry *dentry, *parent;
444 xfs_ino_t ret = NULLFSINO;
446 dentry = d_find_alias(VFS_I(sc->ip));
450 parent = dget_parent(dentry);
454 ASSERT(parent->d_sb == sc->ip->i_mount->m_super);
456 pip = igrab(d_inode(parent));
459 if (S_ISDIR(pip->i_mode)) {
460 trace_xrep_findparent_from_dcache(sc->ip, XFS_I(pip)->i_ino);
461 ret = XFS_I(pip)->i_ino;
464 xchk_irele(sc, XFS_I(pip));