dirtree.c 25 KB

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