parent.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017-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_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/readdir.h"
  23. #include "scrub/tempfile.h"
  24. #include "scrub/repair.h"
  25. #include "scrub/listxattr.h"
  26. #include "scrub/xfile.h"
  27. #include "scrub/xfarray.h"
  28. #include "scrub/xfblob.h"
  29. #include "scrub/trace.h"
  30. /* Set us up to scrub parents. */
  31. int
  32. xchk_setup_parent(
  33. struct xfs_scrub *sc)
  34. {
  35. int error;
  36. if (xchk_could_repair(sc)) {
  37. error = xrep_setup_parent(sc);
  38. if (error)
  39. return error;
  40. }
  41. return xchk_setup_inode_contents(sc, 0);
  42. }
  43. /* Parent pointers */
  44. /* Look for an entry in a parent pointing to this inode. */
  45. struct xchk_parent_ctx {
  46. struct xfs_scrub *sc;
  47. xfs_nlink_t nlink;
  48. };
  49. /* Look for a single entry in a directory pointing to an inode. */
  50. STATIC int
  51. xchk_parent_actor(
  52. struct xfs_scrub *sc,
  53. struct xfs_inode *dp,
  54. xfs_dir2_dataptr_t dapos,
  55. const struct xfs_name *name,
  56. xfs_ino_t ino,
  57. void *priv)
  58. {
  59. struct xchk_parent_ctx *spc = priv;
  60. int error = 0;
  61. /* Does this name make sense? */
  62. if (!xfs_dir2_namecheck(name->name, name->len))
  63. error = -EFSCORRUPTED;
  64. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
  65. return error;
  66. if (sc->ip->i_ino == ino)
  67. spc->nlink++;
  68. if (xchk_should_terminate(spc->sc, &error))
  69. return error;
  70. return 0;
  71. }
  72. /*
  73. * Try to lock a parent directory for checking dirents. Returns the inode
  74. * flags for the locks we now hold, or zero if we failed.
  75. */
  76. STATIC unsigned int
  77. xchk_parent_ilock_dir(
  78. struct xfs_inode *dp)
  79. {
  80. if (!xfs_ilock_nowait(dp, XFS_ILOCK_SHARED))
  81. return 0;
  82. if (!xfs_need_iread_extents(&dp->i_df))
  83. return XFS_ILOCK_SHARED;
  84. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  85. if (!xfs_ilock_nowait(dp, XFS_ILOCK_EXCL))
  86. return 0;
  87. return XFS_ILOCK_EXCL;
  88. }
  89. /*
  90. * Given the inode number of the alleged parent of the inode being scrubbed,
  91. * try to validate that the parent has exactly one directory entry pointing
  92. * back to the inode being scrubbed. Returns -EAGAIN if we need to revalidate
  93. * the dotdot entry.
  94. */
  95. STATIC int
  96. xchk_parent_validate(
  97. struct xfs_scrub *sc,
  98. xfs_ino_t parent_ino)
  99. {
  100. struct xchk_parent_ctx spc = {
  101. .sc = sc,
  102. .nlink = 0,
  103. };
  104. struct xfs_mount *mp = sc->mp;
  105. struct xfs_inode *dp = NULL;
  106. xfs_nlink_t expected_nlink;
  107. unsigned int lock_mode;
  108. int error = 0;
  109. /* Is this the root dir? Then '..' must point to itself. */
  110. if (sc->ip == mp->m_rootip) {
  111. if (sc->ip->i_ino != mp->m_sb.sb_rootino ||
  112. sc->ip->i_ino != parent_ino)
  113. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  114. return 0;
  115. }
  116. /* '..' must not point to ourselves. */
  117. if (sc->ip->i_ino == parent_ino) {
  118. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  119. return 0;
  120. }
  121. /*
  122. * If we're an unlinked directory, the parent /won't/ have a link
  123. * to us. Otherwise, it should have one link.
  124. */
  125. expected_nlink = VFS_I(sc->ip)->i_nlink == 0 ? 0 : 1;
  126. /*
  127. * Grab the parent directory inode. This must be released before we
  128. * cancel the scrub transaction.
  129. *
  130. * If _iget returns -EINVAL or -ENOENT then the parent inode number is
  131. * garbage and the directory is corrupt. If the _iget returns
  132. * -EFSCORRUPTED or -EFSBADCRC then the parent is corrupt which is a
  133. * cross referencing error. Any other error is an operational error.
  134. */
  135. error = xchk_iget(sc, parent_ino, &dp);
  136. if (error == -EINVAL || error == -ENOENT) {
  137. error = -EFSCORRUPTED;
  138. xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
  139. return error;
  140. }
  141. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
  142. return error;
  143. if (dp == sc->ip || xrep_is_tempfile(dp) ||
  144. !S_ISDIR(VFS_I(dp)->i_mode)) {
  145. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  146. goto out_rele;
  147. }
  148. lock_mode = xchk_parent_ilock_dir(dp);
  149. if (!lock_mode) {
  150. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  151. xchk_ilock(sc, XFS_ILOCK_EXCL);
  152. error = -EAGAIN;
  153. goto out_rele;
  154. }
  155. /*
  156. * We cannot yet validate this parent pointer if the directory looks as
  157. * though it has been zapped by the inode record repair code.
  158. */
  159. if (xchk_dir_looks_zapped(dp)) {
  160. error = -EBUSY;
  161. xchk_set_incomplete(sc);
  162. goto out_unlock;
  163. }
  164. /* Look for a directory entry in the parent pointing to the child. */
  165. error = xchk_dir_walk(sc, dp, xchk_parent_actor, &spc);
  166. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
  167. goto out_unlock;
  168. /*
  169. * Ensure that the parent has as many links to the child as the child
  170. * thinks it has to the parent.
  171. */
  172. if (spc.nlink != expected_nlink)
  173. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  174. out_unlock:
  175. xfs_iunlock(dp, lock_mode);
  176. out_rele:
  177. xchk_irele(sc, dp);
  178. return error;
  179. }
  180. /*
  181. * Checking of Parent Pointers
  182. * ===========================
  183. *
  184. * On filesystems with directory parent pointers, we check the referential
  185. * integrity by visiting each parent pointer of a child file and checking that
  186. * the directory referenced by the pointer actually has a dirent pointing
  187. * forward to the child file.
  188. */
  189. /* Deferred parent pointer entry that we saved for later. */
  190. struct xchk_pptr {
  191. /* Cookie for retrieval of the pptr name. */
  192. xfblob_cookie name_cookie;
  193. /* Parent pointer record. */
  194. struct xfs_parent_rec pptr_rec;
  195. /* Length of the pptr name. */
  196. uint8_t namelen;
  197. };
  198. struct xchk_pptrs {
  199. struct xfs_scrub *sc;
  200. /* How many parent pointers did we find at the end? */
  201. unsigned long long pptrs_found;
  202. /* Parent of this directory. */
  203. xfs_ino_t parent_ino;
  204. /* Fixed-size array of xchk_pptr structures. */
  205. struct xfarray *pptr_entries;
  206. /* Blobs containing parent pointer names. */
  207. struct xfblob *pptr_names;
  208. /* Scratch buffer for scanning pptr xattrs */
  209. struct xfs_da_args pptr_args;
  210. /* If we've cycled the ILOCK, we must revalidate all deferred pptrs. */
  211. bool need_revalidate;
  212. /* Name buffer */
  213. struct xfs_name xname;
  214. char namebuf[MAXNAMELEN];
  215. };
  216. /* Does this parent pointer match the dotdot entry? */
  217. STATIC int
  218. xchk_parent_scan_dotdot(
  219. struct xfs_scrub *sc,
  220. struct xfs_inode *ip,
  221. unsigned int attr_flags,
  222. const unsigned char *name,
  223. unsigned int namelen,
  224. const void *value,
  225. unsigned int valuelen,
  226. void *priv)
  227. {
  228. struct xchk_pptrs *pp = priv;
  229. xfs_ino_t parent_ino;
  230. int error;
  231. if (!(attr_flags & XFS_ATTR_PARENT))
  232. return 0;
  233. error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
  234. valuelen, &parent_ino, NULL);
  235. if (error)
  236. return error;
  237. if (pp->parent_ino == parent_ino)
  238. return -ECANCELED;
  239. return 0;
  240. }
  241. /* Look up the dotdot entry so that we can check it as we walk the pptrs. */
  242. STATIC int
  243. xchk_parent_pptr_and_dotdot(
  244. struct xchk_pptrs *pp)
  245. {
  246. struct xfs_scrub *sc = pp->sc;
  247. int error;
  248. /* Look up '..' */
  249. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot, &pp->parent_ino);
  250. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
  251. return error;
  252. if (!xfs_verify_dir_ino(sc->mp, pp->parent_ino)) {
  253. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  254. return 0;
  255. }
  256. /* Is this the root dir? Then '..' must point to itself. */
  257. if (sc->ip == sc->mp->m_rootip) {
  258. if (sc->ip->i_ino != pp->parent_ino)
  259. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  260. return 0;
  261. }
  262. /*
  263. * If this is now an unlinked directory, the dotdot value is
  264. * meaningless as long as it points to a valid inode.
  265. */
  266. if (VFS_I(sc->ip)->i_nlink == 0)
  267. return 0;
  268. if (pp->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  269. return 0;
  270. /* Otherwise, walk the pptrs again, and check. */
  271. error = xchk_xattr_walk(sc, sc->ip, xchk_parent_scan_dotdot, NULL, pp);
  272. if (error == -ECANCELED) {
  273. /* Found a parent pointer that matches dotdot. */
  274. return 0;
  275. }
  276. if (!error || error == -EFSCORRUPTED) {
  277. /* Found a broken parent pointer or no match. */
  278. xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
  279. return 0;
  280. }
  281. return error;
  282. }
  283. /*
  284. * Try to lock a parent directory for checking dirents. Returns the inode
  285. * flags for the locks we now hold, or zero if we failed.
  286. */
  287. STATIC unsigned int
  288. xchk_parent_lock_dir(
  289. struct xfs_scrub *sc,
  290. struct xfs_inode *dp)
  291. {
  292. if (!xfs_ilock_nowait(dp, XFS_IOLOCK_SHARED))
  293. return 0;
  294. if (!xfs_ilock_nowait(dp, XFS_ILOCK_SHARED)) {
  295. xfs_iunlock(dp, XFS_IOLOCK_SHARED);
  296. return 0;
  297. }
  298. if (!xfs_need_iread_extents(&dp->i_df))
  299. return XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED;
  300. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  301. if (!xfs_ilock_nowait(dp, XFS_ILOCK_EXCL)) {
  302. xfs_iunlock(dp, XFS_IOLOCK_SHARED);
  303. return 0;
  304. }
  305. return XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL;
  306. }
  307. /* Check the forward link (dirent) associated with this parent pointer. */
  308. STATIC int
  309. xchk_parent_dirent(
  310. struct xchk_pptrs *pp,
  311. const struct xfs_name *xname,
  312. struct xfs_inode *dp)
  313. {
  314. struct xfs_scrub *sc = pp->sc;
  315. xfs_ino_t child_ino;
  316. int error;
  317. /*
  318. * Use the name attached to this parent pointer to look up the
  319. * directory entry in the alleged parent.
  320. */
  321. error = xchk_dir_lookup(sc, dp, xname, &child_ino);
  322. if (error == -ENOENT) {
  323. xchk_fblock_xref_set_corrupt(sc, XFS_ATTR_FORK, 0);
  324. return 0;
  325. }
  326. if (!xchk_fblock_xref_process_error(sc, XFS_ATTR_FORK, 0, &error))
  327. return error;
  328. /* Does the inode number match? */
  329. if (child_ino != sc->ip->i_ino) {
  330. xchk_fblock_xref_set_corrupt(sc, XFS_ATTR_FORK, 0);
  331. return 0;
  332. }
  333. return 0;
  334. }
  335. /* Try to grab a parent directory. */
  336. STATIC int
  337. xchk_parent_iget(
  338. struct xchk_pptrs *pp,
  339. const struct xfs_parent_rec *pptr,
  340. struct xfs_inode **dpp)
  341. {
  342. struct xfs_scrub *sc = pp->sc;
  343. struct xfs_inode *ip;
  344. xfs_ino_t parent_ino = be64_to_cpu(pptr->p_ino);
  345. int error;
  346. /* Validate inode number. */
  347. error = xfs_dir_ino_validate(sc->mp, parent_ino);
  348. if (error) {
  349. xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
  350. return -ECANCELED;
  351. }
  352. error = xchk_iget(sc, parent_ino, &ip);
  353. if (error == -EINVAL || error == -ENOENT) {
  354. xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
  355. return -ECANCELED;
  356. }
  357. if (!xchk_fblock_xref_process_error(sc, XFS_ATTR_FORK, 0, &error))
  358. return error;
  359. /* The parent must be a directory. */
  360. if (!S_ISDIR(VFS_I(ip)->i_mode)) {
  361. xchk_fblock_xref_set_corrupt(sc, XFS_ATTR_FORK, 0);
  362. goto out_rele;
  363. }
  364. /* Validate generation number. */
  365. if (VFS_I(ip)->i_generation != be32_to_cpu(pptr->p_gen)) {
  366. xchk_fblock_xref_set_corrupt(sc, XFS_ATTR_FORK, 0);
  367. goto out_rele;
  368. }
  369. *dpp = ip;
  370. return 0;
  371. out_rele:
  372. xchk_irele(sc, ip);
  373. return 0;
  374. }
  375. /*
  376. * Walk an xattr of a file. If this xattr is a parent pointer, follow it up
  377. * to a parent directory and check that the parent has a dirent pointing back
  378. * to us.
  379. */
  380. STATIC int
  381. xchk_parent_scan_attr(
  382. struct xfs_scrub *sc,
  383. struct xfs_inode *ip,
  384. unsigned int attr_flags,
  385. const unsigned char *name,
  386. unsigned int namelen,
  387. const void *value,
  388. unsigned int valuelen,
  389. void *priv)
  390. {
  391. struct xfs_name xname = {
  392. .name = name,
  393. .len = namelen,
  394. };
  395. struct xchk_pptrs *pp = priv;
  396. struct xfs_inode *dp = NULL;
  397. const struct xfs_parent_rec *pptr_rec = value;
  398. xfs_ino_t parent_ino;
  399. unsigned int lockmode;
  400. int error;
  401. if (!(attr_flags & XFS_ATTR_PARENT))
  402. return 0;
  403. error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
  404. valuelen, &parent_ino, NULL);
  405. if (error) {
  406. xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
  407. return error;
  408. }
  409. /* No self-referential parent pointers. */
  410. if (parent_ino == sc->ip->i_ino) {
  411. xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
  412. return -ECANCELED;
  413. }
  414. pp->pptrs_found++;
  415. error = xchk_parent_iget(pp, pptr_rec, &dp);
  416. if (error)
  417. return error;
  418. if (!dp)
  419. return 0;
  420. /* Try to lock the inode. */
  421. lockmode = xchk_parent_lock_dir(sc, dp);
  422. if (!lockmode) {
  423. struct xchk_pptr save_pp = {
  424. .pptr_rec = *pptr_rec, /* struct copy */
  425. .namelen = namelen,
  426. };
  427. /* Couldn't lock the inode, so save the pptr for later. */
  428. trace_xchk_parent_defer(sc->ip, &xname, dp->i_ino);
  429. error = xfblob_storename(pp->pptr_names, &save_pp.name_cookie,
  430. &xname);
  431. if (!xchk_fblock_xref_process_error(sc, XFS_ATTR_FORK, 0,
  432. &error))
  433. goto out_rele;
  434. error = xfarray_append(pp->pptr_entries, &save_pp);
  435. if (!xchk_fblock_xref_process_error(sc, XFS_ATTR_FORK, 0,
  436. &error))
  437. goto out_rele;
  438. goto out_rele;
  439. }
  440. error = xchk_parent_dirent(pp, &xname, dp);
  441. if (error)
  442. goto out_unlock;
  443. out_unlock:
  444. xfs_iunlock(dp, lockmode);
  445. out_rele:
  446. xchk_irele(sc, dp);
  447. return error;
  448. }
  449. /*
  450. * Revalidate a parent pointer that we collected in the past but couldn't check
  451. * because of lock contention. Returns 0 if the parent pointer is still valid,
  452. * -ENOENT if it has gone away on us, or a negative errno.
  453. */
  454. STATIC int
  455. xchk_parent_revalidate_pptr(
  456. struct xchk_pptrs *pp,
  457. const struct xfs_name *xname,
  458. struct xfs_parent_rec *pptr)
  459. {
  460. struct xfs_scrub *sc = pp->sc;
  461. int error;
  462. error = xfs_parent_lookup(sc->tp, sc->ip, xname, pptr, &pp->pptr_args);
  463. if (error == -ENOATTR) {
  464. /* Parent pointer went away, nothing to revalidate. */
  465. return -ENOENT;
  466. }
  467. return error;
  468. }
  469. /*
  470. * Check a parent pointer the slow way, which means we cycle locks a bunch
  471. * and put up with revalidation until we get it done.
  472. */
  473. STATIC int
  474. xchk_parent_slow_pptr(
  475. struct xchk_pptrs *pp,
  476. const struct xfs_name *xname,
  477. struct xfs_parent_rec *pptr)
  478. {
  479. struct xfs_scrub *sc = pp->sc;
  480. struct xfs_inode *dp = NULL;
  481. unsigned int lockmode;
  482. int error;
  483. /* Check that the deferred parent pointer still exists. */
  484. if (pp->need_revalidate) {
  485. error = xchk_parent_revalidate_pptr(pp, xname, pptr);
  486. if (error == -ENOENT)
  487. return 0;
  488. if (!xchk_fblock_xref_process_error(sc, XFS_ATTR_FORK, 0,
  489. &error))
  490. return error;
  491. }
  492. error = xchk_parent_iget(pp, pptr, &dp);
  493. if (error)
  494. return error;
  495. if (!dp)
  496. return 0;
  497. /*
  498. * If we can grab both IOLOCK and ILOCK of the alleged parent, we
  499. * can proceed with the validation.
  500. */
  501. lockmode = xchk_parent_lock_dir(sc, dp);
  502. if (lockmode) {
  503. trace_xchk_parent_slowpath(sc->ip, xname, dp->i_ino);
  504. goto check_dirent;
  505. }
  506. /*
  507. * We couldn't lock the parent dir. Drop all the locks and try to
  508. * get them again, one at a time.
  509. */
  510. xchk_iunlock(sc, sc->ilock_flags);
  511. pp->need_revalidate = true;
  512. trace_xchk_parent_ultraslowpath(sc->ip, xname, dp->i_ino);
  513. error = xchk_dir_trylock_for_pptrs(sc, dp, &lockmode);
  514. if (error)
  515. goto out_rele;
  516. /* Revalidate the parent pointer now that we cycled locks. */
  517. error = xchk_parent_revalidate_pptr(pp, xname, pptr);
  518. if (error == -ENOENT) {
  519. error = 0;
  520. goto out_unlock;
  521. }
  522. if (!xchk_fblock_xref_process_error(sc, XFS_ATTR_FORK, 0, &error))
  523. goto out_unlock;
  524. check_dirent:
  525. error = xchk_parent_dirent(pp, xname, dp);
  526. out_unlock:
  527. xfs_iunlock(dp, lockmode);
  528. out_rele:
  529. xchk_irele(sc, dp);
  530. return error;
  531. }
  532. /* Check all the parent pointers that we deferred the first time around. */
  533. STATIC int
  534. xchk_parent_finish_slow_pptrs(
  535. struct xchk_pptrs *pp)
  536. {
  537. xfarray_idx_t array_cur;
  538. int error;
  539. foreach_xfarray_idx(pp->pptr_entries, array_cur) {
  540. struct xchk_pptr pptr;
  541. if (pp->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  542. return 0;
  543. error = xfarray_load(pp->pptr_entries, array_cur, &pptr);
  544. if (error)
  545. return error;
  546. error = xfblob_loadname(pp->pptr_names, pptr.name_cookie,
  547. &pp->xname, pptr.namelen);
  548. if (error)
  549. return error;
  550. error = xchk_parent_slow_pptr(pp, &pp->xname, &pptr.pptr_rec);
  551. if (error)
  552. return error;
  553. }
  554. /* Empty out both xfiles now that we've checked everything. */
  555. xfarray_truncate(pp->pptr_entries);
  556. xfblob_truncate(pp->pptr_names);
  557. return 0;
  558. }
  559. /* Count the number of parent pointers. */
  560. STATIC int
  561. xchk_parent_count_pptr(
  562. struct xfs_scrub *sc,
  563. struct xfs_inode *ip,
  564. unsigned int attr_flags,
  565. const unsigned char *name,
  566. unsigned int namelen,
  567. const void *value,
  568. unsigned int valuelen,
  569. void *priv)
  570. {
  571. struct xchk_pptrs *pp = priv;
  572. int error;
  573. if (!(attr_flags & XFS_ATTR_PARENT))
  574. return 0;
  575. error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
  576. valuelen, NULL, NULL);
  577. if (error)
  578. return error;
  579. pp->pptrs_found++;
  580. return 0;
  581. }
  582. /*
  583. * Compare the number of parent pointers to the link count. For
  584. * non-directories these should be the same. For unlinked directories the
  585. * count should be zero; for linked directories, it should be nonzero.
  586. */
  587. STATIC int
  588. xchk_parent_count_pptrs(
  589. struct xchk_pptrs *pp)
  590. {
  591. struct xfs_scrub *sc = pp->sc;
  592. int error;
  593. /*
  594. * If we cycled the ILOCK while cross-checking parent pointers with
  595. * dirents, then we need to recalculate the number of parent pointers.
  596. */
  597. if (pp->need_revalidate) {
  598. pp->pptrs_found = 0;
  599. error = xchk_xattr_walk(sc, sc->ip, xchk_parent_count_pptr,
  600. NULL, pp);
  601. if (error == -EFSCORRUPTED) {
  602. /* Found a bad parent pointer */
  603. xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
  604. return 0;
  605. }
  606. if (error)
  607. return error;
  608. }
  609. if (S_ISDIR(VFS_I(sc->ip)->i_mode)) {
  610. if (sc->ip == sc->mp->m_rootip)
  611. pp->pptrs_found++;
  612. if (VFS_I(sc->ip)->i_nlink == 0 && pp->pptrs_found > 0)
  613. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  614. else if (VFS_I(sc->ip)->i_nlink > 0 &&
  615. pp->pptrs_found == 0)
  616. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  617. } else {
  618. if (VFS_I(sc->ip)->i_nlink != pp->pptrs_found)
  619. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  620. }
  621. return 0;
  622. }
  623. /* Check parent pointers of a file. */
  624. STATIC int
  625. xchk_parent_pptr(
  626. struct xfs_scrub *sc)
  627. {
  628. struct xchk_pptrs *pp;
  629. char *descr;
  630. int error;
  631. pp = kvzalloc(sizeof(struct xchk_pptrs), XCHK_GFP_FLAGS);
  632. if (!pp)
  633. return -ENOMEM;
  634. pp->sc = sc;
  635. pp->xname.name = pp->namebuf;
  636. /*
  637. * Set up some staging memory for parent pointers that we can't check
  638. * due to locking contention.
  639. */
  640. descr = xchk_xfile_ino_descr(sc, "slow parent pointer entries");
  641. error = xfarray_create(descr, 0, sizeof(struct xchk_pptr),
  642. &pp->pptr_entries);
  643. kfree(descr);
  644. if (error)
  645. goto out_pp;
  646. descr = xchk_xfile_ino_descr(sc, "slow parent pointer names");
  647. error = xfblob_create(descr, &pp->pptr_names);
  648. kfree(descr);
  649. if (error)
  650. goto out_entries;
  651. error = xchk_xattr_walk(sc, sc->ip, xchk_parent_scan_attr, NULL, pp);
  652. if (error == -ECANCELED) {
  653. error = 0;
  654. goto out_names;
  655. }
  656. if (error)
  657. goto out_names;
  658. error = xchk_parent_finish_slow_pptrs(pp);
  659. if (error == -ETIMEDOUT) {
  660. /* Couldn't grab a lock, scrub was marked incomplete */
  661. error = 0;
  662. goto out_names;
  663. }
  664. if (error)
  665. goto out_names;
  666. if (pp->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  667. goto out_names;
  668. /*
  669. * For subdirectories, make sure the dotdot entry references the same
  670. * inode as the parent pointers.
  671. *
  672. * If we're scanning a /consistent/ directory, there should only be
  673. * one parent pointer, and it should point to the same directory as
  674. * the dotdot entry.
  675. *
  676. * However, a corrupt directory tree might feature a subdirectory with
  677. * multiple parents. The directory loop scanner is responsible for
  678. * correcting that kind of problem, so for now we only validate that
  679. * the dotdot entry matches /one/ of the parents.
  680. */
  681. if (S_ISDIR(VFS_I(sc->ip)->i_mode)) {
  682. error = xchk_parent_pptr_and_dotdot(pp);
  683. if (error)
  684. goto out_names;
  685. }
  686. if (pp->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  687. goto out_names;
  688. /*
  689. * Complain if the number of parent pointers doesn't match the link
  690. * count. This could be a sign of missing parent pointers (or an
  691. * incorrect link count).
  692. */
  693. error = xchk_parent_count_pptrs(pp);
  694. if (error)
  695. goto out_names;
  696. out_names:
  697. xfblob_destroy(pp->pptr_names);
  698. out_entries:
  699. xfarray_destroy(pp->pptr_entries);
  700. out_pp:
  701. kvfree(pp);
  702. return error;
  703. }
  704. /* Scrub a parent pointer. */
  705. int
  706. xchk_parent(
  707. struct xfs_scrub *sc)
  708. {
  709. struct xfs_mount *mp = sc->mp;
  710. xfs_ino_t parent_ino;
  711. int error = 0;
  712. if (xfs_has_parent(mp))
  713. return xchk_parent_pptr(sc);
  714. /*
  715. * If we're a directory, check that the '..' link points up to
  716. * a directory that has one entry pointing to us.
  717. */
  718. if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
  719. return -ENOENT;
  720. /* We're not a special inode, are we? */
  721. if (!xfs_verify_dir_ino(mp, sc->ip->i_ino)) {
  722. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  723. return 0;
  724. }
  725. do {
  726. if (xchk_should_terminate(sc, &error))
  727. break;
  728. /* Look up '..' */
  729. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
  730. &parent_ino);
  731. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
  732. return error;
  733. if (!xfs_verify_dir_ino(mp, parent_ino)) {
  734. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  735. return 0;
  736. }
  737. /*
  738. * Check that the dotdot entry points to a parent directory
  739. * containing a dirent pointing to this subdirectory.
  740. */
  741. error = xchk_parent_validate(sc, parent_ino);
  742. } while (error == -EAGAIN);
  743. if (error == -EBUSY) {
  744. /*
  745. * We could not scan a directory, so we marked the check
  746. * incomplete. No further error return is necessary.
  747. */
  748. return 0;
  749. }
  750. return error;
  751. }
  752. /*
  753. * Decide if this file's extended attributes (and therefore its parent
  754. * pointers) have been zapped to satisfy the inode and ifork verifiers.
  755. * Checking and repairing should be postponed until the extended attribute
  756. * structure is fixed.
  757. */
  758. bool
  759. xchk_pptr_looks_zapped(
  760. struct xfs_inode *ip)
  761. {
  762. struct xfs_mount *mp = ip->i_mount;
  763. struct inode *inode = VFS_I(ip);
  764. ASSERT(xfs_has_parent(mp));
  765. /*
  766. * Temporary files that cannot be linked into the directory tree do not
  767. * have attr forks because they cannot ever have parents.
  768. */
  769. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  770. return false;
  771. /*
  772. * Directory tree roots do not have parents, so the expected outcome
  773. * of a parent pointer scan is always the empty set. It's safe to scan
  774. * them even if the attr fork was zapped.
  775. */
  776. if (ip == mp->m_rootip)
  777. return false;
  778. /*
  779. * Metadata inodes are all rooted in the superblock and do not have
  780. * any parents. Hence the attr fork will not be initialized, but
  781. * there are no parent pointers that might have been zapped.
  782. */
  783. if (xfs_is_metadata_inode(ip))
  784. return false;
  785. /*
  786. * Linked and linkable non-rootdir files should always have an
  787. * attribute fork because that is where parent pointers are
  788. * stored. If the fork is absent, something is amiss.
  789. */
  790. if (!xfs_inode_has_attr_fork(ip))
  791. return true;
  792. /* Repair zapped this file's attr fork a short time ago */
  793. if (xfs_ifork_zapped(ip, XFS_ATTR_FORK))
  794. return true;
  795. /*
  796. * If the dinode repair found a bad attr fork, it will reset the fork
  797. * to extents format with zero records and wait for the bmapbta
  798. * scrubber to reconstruct the block mappings. The extended attribute
  799. * structure always contain some content when parent pointers are
  800. * enabled, so this is a clear sign of a zapped attr fork.
  801. */
  802. return ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
  803. ip->i_af.if_nextents == 0;
  804. }