Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2020-2023 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <djwong@kernel.org>
5 : */
6 : #include "xfs.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_defer.h"
13 : #include "xfs_bit.h"
14 : #include "xfs_log_format.h"
15 : #include "xfs_trans.h"
16 : #include "xfs_sb.h"
17 : #include "xfs_inode.h"
18 : #include "xfs_icache.h"
19 : #include "xfs_da_format.h"
20 : #include "xfs_da_btree.h"
21 : #include "xfs_dir2.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_swapext.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"
38 :
39 : /*
40 : * Finding the Parent of a Directory
41 : * =================================
42 : *
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.
47 : *
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.
52 : *
53 : * Because we cannot hold @sc->ip's ILOCK during a scan of the whole fs, it is
54 : * necessary to use dirent hooks to update the parent scan results. Callers
55 : * must not read the scan results without re-taking @sc->ip's ILOCK.
56 : *
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.
60 : */
61 :
62 : struct xrep_findparent_info {
63 : /* The directory currently being scanned. */
64 : struct xfs_inode *dp;
65 :
66 : /*
67 : * Scrub context. We're looking for a @dp containing a directory
68 : * entry pointing to sc->ip->i_ino.
69 : */
70 : struct xfs_scrub *sc;
71 :
72 : /* Optional scan information for a xrep_findparent_scan call. */
73 : struct xrep_parent_scan_info *parent_scan;
74 :
75 : /*
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/
78 : * parent directory.
79 : */
80 : xfs_ino_t found_parent;
81 :
82 : /*
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.
86 : */
87 : bool parent_tentative;
88 : };
89 :
90 : /*
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.
93 : */
94 : STATIC int
95 3083825 : xrep_findparent_dirent(
96 : struct xfs_scrub *sc,
97 : struct xfs_inode *dp,
98 : xfs_dir2_dataptr_t dapos,
99 : const struct xfs_name *name,
100 : xfs_ino_t ino,
101 : void *priv)
102 : {
103 3083825 : struct xrep_findparent_info *fpi = priv;
104 3083825 : int error = 0;
105 :
106 3083825 : if (xchk_should_terminate(fpi->sc, &error))
107 0 : return error;
108 :
109 3083916 : if (ino != fpi->sc->ip->i_ino)
110 : return 0;
111 :
112 : /* Ignore garbage directory entry names. */
113 45727 : if (name->len == 0 || !xfs_dir2_namecheck(name->name, name->len))
114 0 : return -EFSCORRUPTED;
115 :
116 : /*
117 : * Ignore dotdot and dot entries -- we're looking for parent -> child
118 : * links only.
119 : */
120 45727 : if (name->name[0] == '.' && (name->len == 1 ||
121 696 : (name->len == 2 && name->name[1] == '.')))
122 : return 0;
123 :
124 : /* Uhoh, more than one parent for a dir? */
125 45031 : if (fpi->found_parent != NULLFSINO &&
126 0 : !(fpi->parent_tentative && fpi->found_parent == fpi->dp->i_ino)) {
127 0 : trace_xrep_findparent_dirent(fpi->sc->ip, 0);
128 0 : return -EFSCORRUPTED;
129 : }
130 :
131 : /* We found a potential parent; remember this. */
132 45031 : trace_xrep_findparent_dirent(fpi->sc->ip, fpi->dp->i_ino);
133 45028 : fpi->found_parent = fpi->dp->i_ino;
134 45028 : fpi->parent_tentative = false;
135 :
136 45028 : if (fpi->parent_scan)
137 688 : xrep_findparent_scan_found(fpi->parent_scan, fpi->dp->i_ino);
138 :
139 : return 0;
140 : }
141 :
142 : /*
143 : * If this is a directory, walk the dirents looking for any that point to the
144 : * scrub target inode.
145 : */
146 : STATIC int
147 401804 : xrep_findparent_walk_directory(
148 : struct xrep_findparent_info *fpi)
149 : {
150 401804 : struct xfs_scrub *sc = fpi->sc;
151 401804 : struct xfs_inode *dp = fpi->dp;
152 401804 : unsigned int lock_mode;
153 401804 : int error = 0;
154 :
155 : /*
156 : * The inode being scanned cannot be its own parent, nor can any
157 : * temporary directory we created to stage this repair.
158 : */
159 401804 : if (dp == sc->ip || dp == sc->tempip)
160 : return 0;
161 :
162 : /*
163 : * Similarly, temporary files created to stage a repair cannot be the
164 : * parent of this inode.
165 : */
166 401115 : if (xrep_is_tempfile(dp))
167 : return 0;
168 :
169 : /*
170 : * Scan the directory to see if there it contains an entry pointing to
171 : * the directory that we are repairing.
172 : */
173 401110 : lock_mode = xfs_ilock_data_map_shared(dp);
174 :
175 : /* Don't mix metadata and regular directory trees. */
176 401107 : if (xfs_is_metadir_inode(dp) != xfs_is_metadir_inode(sc->ip))
177 20 : goto out_unlock;
178 :
179 : /*
180 : * If this directory is known to be sick, we cannot scan it reliably
181 : * and must abort.
182 : */
183 401087 : if (xfs_inode_has_sickness(dp, XFS_SICK_INO_CORE |
184 : XFS_SICK_INO_BMBTD |
185 : XFS_SICK_INO_DIR)) {
186 0 : error = -EFSCORRUPTED;
187 0 : goto out_unlock;
188 : }
189 :
190 : /*
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.
193 : */
194 401098 : if (xchk_dir_looks_zapped(dp)) {
195 0 : error = -EFSCORRUPTED;
196 0 : goto out_unlock;
197 : }
198 :
199 401083 : error = xchk_dir_walk(sc, dp, xrep_findparent_dirent, fpi);
200 401100 : if (error)
201 0 : goto out_unlock;
202 :
203 401100 : out_unlock:
204 401120 : xfs_iunlock(dp, lock_mode);
205 401120 : return error;
206 : }
207 :
208 : /*
209 : * Update this directory's dotdot pointer based on ongoing dirent updates.
210 : */
211 : STATIC int
212 33156 : xrep_findparent_live_update(
213 : struct notifier_block *nb,
214 : unsigned long action,
215 : void *data)
216 : {
217 33156 : struct xfs_dir_update_params *p = data;
218 33156 : struct xrep_parent_scan_info *pscan;
219 33156 : struct xfs_scrub *sc;
220 :
221 33156 : pscan = container_of(nb, struct xrep_parent_scan_info,
222 : hooks.dirent_hook.nb);
223 33156 : sc = pscan->sc;
224 :
225 : /*
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
228 : * parent inode.
229 : */
230 33156 : if (p->ip->i_ino == sc->ip->i_ino &&
231 0 : xchk_iscan_want_live_update(&pscan->iscan, p->dp->i_ino)) {
232 0 : if (p->delta > 0) {
233 0 : xrep_findparent_scan_found(pscan, p->dp->i_ino);
234 : } else {
235 0 : xrep_findparent_scan_found(pscan, NULLFSINO);
236 : }
237 : }
238 :
239 33156 : return NOTIFY_DONE;
240 : }
241 :
242 : /*
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.
245 : */
246 : int
247 2506523 : __xrep_findparent_scan_start(
248 : struct xfs_scrub *sc,
249 : struct xrep_parent_scan_info *pscan,
250 : notifier_fn_t custom_fn)
251 : {
252 2506523 : int error;
253 :
254 2506523 : if (!(sc->flags & XCHK_FSGATES_DIRENTS)) {
255 0 : ASSERT(sc->flags & XCHK_FSGATES_DIRENTS);
256 0 : return -EINVAL;
257 : }
258 :
259 2506523 : pscan->sc = sc;
260 2506523 : pscan->parent_ino = NULLFSINO;
261 :
262 2506523 : mutex_init(&pscan->lock);
263 :
264 2506497 : xchk_iscan_start(sc, 30000, 100, &pscan->iscan);
265 :
266 : /*
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.
271 : */
272 2506481 : xfs_hook_setup(&pscan->hooks.dirent_hook,
273 : custom_fn ? custom_fn : xrep_findparent_live_update);
274 2506481 : error = xfs_dir_hook_add(sc->mp, &pscan->hooks);
275 2506540 : if (error)
276 0 : goto out_iscan;
277 :
278 : return 0;
279 : out_iscan:
280 0 : xchk_iscan_teardown(&pscan->iscan);
281 0 : mutex_destroy(&pscan->lock);
282 0 : return error;
283 : }
284 :
285 : /*
286 : * Scan the entire filesystem looking for a parent inode for the inode being
287 : * scrubbed. @sc->ip must not be the root of a directory tree. Callers must
288 : * not hold a dirty transaction or any lock that would interfere with taking
289 : * an ILOCK.
290 : *
291 : * Returns 0 with @pscan->parent_ino set to the parent that we found.
292 : * Returns 0 with @pscan->parent_ino set to NULLFSINO if we found no parents.
293 : * Returns the usual negative errno if something else happened.
294 : */
295 : int
296 692 : xrep_findparent_scan(
297 : struct xrep_parent_scan_info *pscan)
298 : {
299 692 : struct xrep_findparent_info fpi = {
300 692 : .sc = pscan->sc,
301 : .found_parent = NULLFSINO,
302 : .parent_scan = pscan,
303 : };
304 692 : struct xfs_scrub *sc = pscan->sc;
305 692 : int ret;
306 :
307 692 : ASSERT(S_ISDIR(VFS_IC(sc->ip)->i_mode));
308 :
309 1868144 : while ((ret = xchk_iscan_iter(&pscan->iscan, &fpi.dp)) == 1) {
310 1867534 : if (S_ISDIR(VFS_I(fpi.dp)->i_mode))
311 357457 : ret = xrep_findparent_walk_directory(&fpi);
312 : else
313 1510077 : ret = 0;
314 1867543 : xchk_iscan_mark_visited(&pscan->iscan, fpi.dp);
315 1867310 : xchk_irele(sc, fpi.dp);
316 1867682 : if (ret)
317 : break;
318 :
319 1867682 : if (xchk_should_terminate(sc, &ret))
320 : break;
321 : }
322 692 : xchk_iscan_iter_finish(&pscan->iscan);
323 :
324 692 : return ret;
325 : }
326 :
327 : /* Tear down a parent scan. */
328 : void
329 2497735 : xrep_findparent_scan_teardown(
330 : struct xrep_parent_scan_info *pscan)
331 : {
332 2497735 : xfs_dir_hook_del(pscan->sc->mp, &pscan->hooks);
333 2506530 : xchk_iscan_teardown(&pscan->iscan);
334 2503464 : mutex_destroy(&pscan->lock);
335 2503290 : }
336 :
337 : /* Finish a parent scan early. */
338 : void
339 44581 : xrep_findparent_scan_finish_early(
340 : struct xrep_parent_scan_info *pscan,
341 : xfs_ino_t ino)
342 : {
343 44581 : xrep_findparent_scan_found(pscan, ino);
344 44583 : xchk_iscan_finish_early(&pscan->iscan);
345 44582 : }
346 :
347 : /*
348 : * Confirm that the directory @parent_ino actually contains a directory entry
349 : * pointing to the child @sc->ip->ino. This function returns one of several
350 : * ways:
351 : *
352 : * Returns 0 with @parent_ino unchanged if the parent was confirmed.
353 : * Returns 0 with @parent_ino set to NULLFSINO if the parent was not valid.
354 : * Returns the usual negative errno if something else happened.
355 : */
356 : int
357 44343 : xrep_findparent_confirm(
358 : struct xfs_scrub *sc,
359 : xfs_ino_t *parent_ino)
360 : {
361 44343 : struct xrep_findparent_info fpi = {
362 : .sc = sc,
363 : .found_parent = NULLFSINO,
364 : };
365 44343 : int error;
366 :
367 : /* The root directory always points to itself. */
368 44343 : if (sc->ip == sc->mp->m_rootip) {
369 0 : *parent_ino = sc->mp->m_sb.sb_rootino;
370 0 : return 0;
371 : }
372 :
373 : /* The metadata root directory always points to itself. */
374 44343 : if (sc->ip == sc->mp->m_metadirip) {
375 0 : *parent_ino = sc->mp->m_sb.sb_metadirino;
376 0 : return 0;
377 : }
378 :
379 : /*
380 : * Unlinked dirs can point anywhere, so we point them at the root dir
381 : * of whichever tree is appropriate.
382 : */
383 44343 : if (VFS_I(sc->ip)->i_nlink == 0) {
384 0 : if (xfs_is_metadir_inode(sc->ip))
385 0 : *parent_ino = sc->mp->m_sb.sb_metadirino;
386 : else
387 0 : *parent_ino = sc->mp->m_sb.sb_rootino;
388 0 : return 0;
389 : }
390 :
391 : /* Reject garbage parent inode numbers and self-referential parents. */
392 44343 : if (*parent_ino == NULLFSINO)
393 : return 0;
394 44343 : if (!xfs_verify_dir_ino(sc->mp, *parent_ino) ||
395 44341 : *parent_ino == sc->ip->i_ino) {
396 0 : *parent_ino = NULLFSINO;
397 0 : return 0;
398 : }
399 :
400 44341 : error = xchk_iget(sc, *parent_ino, &fpi.dp);
401 44343 : if (error)
402 : return error;
403 :
404 44343 : if (!S_ISDIR(VFS_I(fpi.dp)->i_mode)) {
405 0 : *parent_ino = NULLFSINO;
406 0 : goto out_rele;
407 : }
408 :
409 44343 : error = xrep_findparent_walk_directory(&fpi);
410 44343 : if (error)
411 0 : goto out_rele;
412 :
413 44343 : *parent_ino = fpi.found_parent;
414 44343 : out_rele:
415 44343 : xchk_irele(sc, fpi.dp);
416 44343 : return error;
417 : }
418 :
419 : /*
420 : * If we're the root of a directory tree, we are our own parent. If we're an
421 : * unlinked directory, the parent /won't/ have a link to us. Set the parent
422 : * directory to the root for both cases. Returns NULLFSINO if we don't know
423 : * what to do.
424 : */
425 : xfs_ino_t
426 45274 : xrep_findparent_self_reference(
427 : struct xfs_scrub *sc)
428 : {
429 45274 : if (sc->ip->i_ino == sc->mp->m_sb.sb_rootino)
430 : return sc->mp->m_sb.sb_rootino;
431 :
432 45033 : if (sc->ip->i_ino == sc->mp->m_sb.sb_metadirino)
433 : return sc->mp->m_sb.sb_metadirino;
434 :
435 45033 : if (VFS_I(sc->ip)->i_nlink == 0) {
436 0 : if (xfs_is_metadir_inode(sc->ip))
437 : return sc->mp->m_sb.sb_metadirino;
438 0 : return sc->mp->m_sb.sb_rootino;
439 : }
440 :
441 : return NULLFSINO;
442 : }
443 :
444 : /* Check the dentry cache to see if knows of a parent for the scrub target. */
445 : xfs_ino_t
446 45032 : xrep_findparent_from_dcache(
447 : struct xfs_scrub *sc)
448 : {
449 45032 : struct inode *pip = NULL;
450 45032 : struct dentry *dentry, *parent;
451 45032 : xfs_ino_t ret = NULLFSINO;
452 :
453 45032 : dentry = d_find_alias(VFS_I(sc->ip));
454 45033 : if (!dentry)
455 696 : goto out;
456 :
457 44337 : parent = dget_parent(dentry);
458 44337 : if (!parent)
459 0 : goto out_dput;
460 :
461 44337 : if (parent->d_sb != sc->ip->i_mount->m_super) {
462 0 : dput(parent);
463 0 : goto out_dput;
464 : }
465 :
466 44337 : pip = igrab(d_inode(parent));
467 44336 : dput(parent);
468 :
469 44339 : if (S_ISDIR(pip->i_mode)) {
470 44339 : trace_xrep_findparent_from_dcache(sc->ip, XFS_I(pip)->i_ino);
471 44337 : ret = XFS_I(pip)->i_ino;
472 : }
473 :
474 44337 : xchk_irele(sc, XFS_I(pip));
475 :
476 44339 : out_dput:
477 44339 : dput(dentry);
478 45034 : out:
479 45034 : return ret;
480 : }
481 :
482 : /* Pass back the parent inumber if this a parent pointer */
483 : STATIC int
484 2250223 : xrep_findparent_from_pptr(
485 : struct xfs_scrub *sc,
486 : struct xfs_inode *ip,
487 : const struct xfs_parent_name_irec *pptr,
488 : void *priv)
489 : {
490 2250223 : xfs_ino_t *inop = priv;
491 :
492 2250223 : *inop = pptr->p_ino;
493 2250223 : return -ECANCELED;
494 : }
495 :
496 : /*
497 : * Find the first parent of the inode being scrubbed by walking parent
498 : * pointers. Caller must hold sc->ip's ILOCK.
499 : */
500 : int
501 2255040 : xrep_findparent_from_pptrs(
502 : struct xfs_scrub *sc,
503 : xfs_ino_t *inop)
504 : {
505 2255040 : struct xfs_parent_name_irec pptr;
506 2255040 : int error;
507 :
508 2255040 : *inop = NULLFSINO;
509 :
510 2255040 : error = xchk_pptr_walk(sc, sc->ip, xrep_findparent_from_pptr, &pptr,
511 : inop);
512 2243807 : if (error && error != -ECANCELED)
513 0 : return error;
514 : return 0;
515 : }
|