parent_repair.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2020-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_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_exchmaps.h"
  27. #include "xfs_parent.h"
  28. #include "xfs_attr.h"
  29. #include "xfs_bmap.h"
  30. #include "xfs_ag.h"
  31. #include "scrub/xfs_scrub.h"
  32. #include "scrub/scrub.h"
  33. #include "scrub/common.h"
  34. #include "scrub/trace.h"
  35. #include "scrub/repair.h"
  36. #include "scrub/iscan.h"
  37. #include "scrub/findparent.h"
  38. #include "scrub/readdir.h"
  39. #include "scrub/tempfile.h"
  40. #include "scrub/tempexch.h"
  41. #include "scrub/orphanage.h"
  42. #include "scrub/xfile.h"
  43. #include "scrub/xfarray.h"
  44. #include "scrub/xfblob.h"
  45. #include "scrub/attr_repair.h"
  46. #include "scrub/listxattr.h"
  47. /*
  48. * Repairing The Directory Parent Pointer
  49. * ======================================
  50. *
  51. * Currently, only directories support parent pointers (in the form of '..'
  52. * entries), so we simply scan the filesystem and update the '..' entry.
  53. *
  54. * Note that because the only parent pointer is the dotdot entry, we won't
  55. * touch an unhealthy directory, since the directory repair code is perfectly
  56. * capable of rebuilding a directory with the proper parent inode.
  57. *
  58. * See the section on locking issues in dir_repair.c for more information about
  59. * conflicts with the VFS. The findparent code wll keep our incore parent
  60. * inode up to date.
  61. *
  62. * If parent pointers are enabled, we instead reconstruct the parent pointer
  63. * information by visiting every directory entry of every directory in the
  64. * system and translating the relevant dirents into parent pointers. In this
  65. * case, it is advantageous to stash all parent pointers created from dirents
  66. * from a single parent file before replaying them into the temporary file. To
  67. * save memory, the live filesystem scan reuses the findparent object. Parent
  68. * pointer repair chooses either directory scanning or findparent, but not
  69. * both.
  70. *
  71. * When salvaging completes, the remaining stashed entries are replayed to the
  72. * temporary file. All non-parent pointer extended attributes are copied to
  73. * the temporary file's extended attributes. An atomic file mapping exchange
  74. * is used to commit the new xattr blocks to the file being repaired. This
  75. * will disrupt attrmulti cursors.
  76. */
  77. /* Create a parent pointer in the tempfile. */
  78. #define XREP_PPTR_ADD (1)
  79. /* Remove a parent pointer from the tempfile. */
  80. #define XREP_PPTR_REMOVE (2)
  81. /* A stashed parent pointer update. */
  82. struct xrep_pptr {
  83. /* Cookie for retrieval of the pptr name. */
  84. xfblob_cookie name_cookie;
  85. /* Parent pointer record. */
  86. struct xfs_parent_rec pptr_rec;
  87. /* Length of the pptr name. */
  88. uint8_t namelen;
  89. /* XREP_PPTR_{ADD,REMOVE} */
  90. uint8_t action;
  91. };
  92. /*
  93. * Stash up to 8 pages of recovered parent pointers in pptr_recs and
  94. * pptr_names before we write them to the temp file.
  95. */
  96. #define XREP_PARENT_MAX_STASH_BYTES (PAGE_SIZE * 8)
  97. struct xrep_parent {
  98. struct xfs_scrub *sc;
  99. /* Fixed-size array of xrep_pptr structures. */
  100. struct xfarray *pptr_recs;
  101. /* Blobs containing parent pointer names. */
  102. struct xfblob *pptr_names;
  103. /* xattr keys */
  104. struct xfarray *xattr_records;
  105. /* xattr values */
  106. struct xfblob *xattr_blobs;
  107. /* Scratch buffers for saving extended attributes */
  108. unsigned char *xattr_name;
  109. void *xattr_value;
  110. unsigned int xattr_value_sz;
  111. /*
  112. * Information used to exchange the attr fork mappings, if the fs
  113. * supports parent pointers.
  114. */
  115. struct xrep_tempexch tx;
  116. /*
  117. * Information used to scan the filesystem to find the inumber of the
  118. * dotdot entry for this directory. On filesystems without parent
  119. * pointers, we use the findparent_* functions on this object and
  120. * access only the parent_ino field directly.
  121. *
  122. * When parent pointers are enabled, the directory entry scanner uses
  123. * the iscan, hooks, and lock fields of this object directly.
  124. * @pscan.lock coordinates access to pptr_recs, pptr_names, pptr, and
  125. * pptr_scratch. This reduces the memory requirements of this
  126. * structure.
  127. *
  128. * The lock also controls access to xattr_records and xattr_blobs(?)
  129. */
  130. struct xrep_parent_scan_info pscan;
  131. /* Orphanage reparenting request. */
  132. struct xrep_adoption adoption;
  133. /* Directory entry name, plus the trailing null. */
  134. struct xfs_name xname;
  135. unsigned char namebuf[MAXNAMELEN];
  136. /* Scratch buffer for scanning pptr xattrs */
  137. struct xfs_da_args pptr_args;
  138. /* Have we seen any live updates of parent pointers recently? */
  139. bool saw_pptr_updates;
  140. /* Number of parents we found after all other repairs */
  141. unsigned long long parents;
  142. };
  143. struct xrep_parent_xattr {
  144. /* Cookie for retrieval of the xattr name. */
  145. xfblob_cookie name_cookie;
  146. /* Cookie for retrieval of the xattr value. */
  147. xfblob_cookie value_cookie;
  148. /* XFS_ATTR_* flags */
  149. int flags;
  150. /* Length of the value and name. */
  151. uint32_t valuelen;
  152. uint16_t namelen;
  153. };
  154. /*
  155. * Stash up to 8 pages of attrs in xattr_records/xattr_blobs before we write
  156. * them to the temp file.
  157. */
  158. #define XREP_PARENT_XATTR_MAX_STASH_BYTES (PAGE_SIZE * 8)
  159. /* Tear down all the incore stuff we created. */
  160. static void
  161. xrep_parent_teardown(
  162. struct xrep_parent *rp)
  163. {
  164. xrep_findparent_scan_teardown(&rp->pscan);
  165. kvfree(rp->xattr_name);
  166. rp->xattr_name = NULL;
  167. kvfree(rp->xattr_value);
  168. rp->xattr_value = NULL;
  169. if (rp->xattr_blobs)
  170. xfblob_destroy(rp->xattr_blobs);
  171. rp->xattr_blobs = NULL;
  172. if (rp->xattr_records)
  173. xfarray_destroy(rp->xattr_records);
  174. rp->xattr_records = NULL;
  175. if (rp->pptr_names)
  176. xfblob_destroy(rp->pptr_names);
  177. rp->pptr_names = NULL;
  178. if (rp->pptr_recs)
  179. xfarray_destroy(rp->pptr_recs);
  180. rp->pptr_recs = NULL;
  181. }
  182. /* Set up for a parent repair. */
  183. int
  184. xrep_setup_parent(
  185. struct xfs_scrub *sc)
  186. {
  187. struct xrep_parent *rp;
  188. int error;
  189. xchk_fsgates_enable(sc, XCHK_FSGATES_DIRENTS);
  190. rp = kvzalloc(sizeof(struct xrep_parent), XCHK_GFP_FLAGS);
  191. if (!rp)
  192. return -ENOMEM;
  193. rp->sc = sc;
  194. rp->xname.name = rp->namebuf;
  195. sc->buf = rp;
  196. error = xrep_tempfile_create(sc, S_IFREG);
  197. if (error)
  198. return error;
  199. return xrep_orphanage_try_create(sc);
  200. }
  201. /*
  202. * Scan all files in the filesystem for a child dirent that we can turn into
  203. * the dotdot entry for this directory.
  204. */
  205. STATIC int
  206. xrep_parent_find_dotdot(
  207. struct xrep_parent *rp)
  208. {
  209. struct xfs_scrub *sc = rp->sc;
  210. xfs_ino_t ino;
  211. unsigned int sick, checked;
  212. int error;
  213. /*
  214. * Avoid sick directories. There shouldn't be anyone else clearing the
  215. * directory's sick status.
  216. */
  217. xfs_inode_measure_sickness(sc->ip, &sick, &checked);
  218. if (sick & XFS_SICK_INO_DIR)
  219. return -EFSCORRUPTED;
  220. ino = xrep_findparent_self_reference(sc);
  221. if (ino != NULLFSINO) {
  222. xrep_findparent_scan_finish_early(&rp->pscan, ino);
  223. return 0;
  224. }
  225. /*
  226. * Drop the ILOCK on this directory so that we can scan for the dotdot
  227. * entry. Figure out who is going to be the parent of this directory,
  228. * then retake the ILOCK so that we can salvage directory entries.
  229. */
  230. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  231. /* Does the VFS dcache have an answer for us? */
  232. ino = xrep_findparent_from_dcache(sc);
  233. if (ino != NULLFSINO) {
  234. error = xrep_findparent_confirm(sc, &ino);
  235. if (!error && ino != NULLFSINO) {
  236. xrep_findparent_scan_finish_early(&rp->pscan, ino);
  237. goto out_relock;
  238. }
  239. }
  240. /* Scan the entire filesystem for a parent. */
  241. error = xrep_findparent_scan(&rp->pscan);
  242. out_relock:
  243. xchk_ilock(sc, XFS_ILOCK_EXCL);
  244. return error;
  245. }
  246. /*
  247. * Add this stashed incore parent pointer to the temporary file.
  248. * The caller must hold the tempdir's IOLOCK, must not hold any ILOCKs, and
  249. * must not be in transaction context.
  250. */
  251. STATIC int
  252. xrep_parent_replay_update(
  253. struct xrep_parent *rp,
  254. const struct xfs_name *xname,
  255. struct xrep_pptr *pptr)
  256. {
  257. struct xfs_scrub *sc = rp->sc;
  258. switch (pptr->action) {
  259. case XREP_PPTR_ADD:
  260. /* Create parent pointer. */
  261. trace_xrep_parent_replay_parentadd(sc->tempip, xname,
  262. &pptr->pptr_rec);
  263. return xfs_parent_set(sc->tempip, sc->ip->i_ino, xname,
  264. &pptr->pptr_rec, &rp->pptr_args);
  265. case XREP_PPTR_REMOVE:
  266. /* Remove parent pointer. */
  267. trace_xrep_parent_replay_parentremove(sc->tempip, xname,
  268. &pptr->pptr_rec);
  269. return xfs_parent_unset(sc->tempip, sc->ip->i_ino, xname,
  270. &pptr->pptr_rec, &rp->pptr_args);
  271. }
  272. ASSERT(0);
  273. return -EIO;
  274. }
  275. /*
  276. * Flush stashed parent pointer updates that have been recorded by the scanner.
  277. * This is done to reduce the memory requirements of the parent pointer
  278. * rebuild, since files can have a lot of hardlinks and the fs can be busy.
  279. *
  280. * Caller must not hold transactions or ILOCKs. Caller must hold the tempfile
  281. * IOLOCK.
  282. */
  283. STATIC int
  284. xrep_parent_replay_updates(
  285. struct xrep_parent *rp)
  286. {
  287. xfarray_idx_t array_cur;
  288. int error;
  289. mutex_lock(&rp->pscan.lock);
  290. foreach_xfarray_idx(rp->pptr_recs, array_cur) {
  291. struct xrep_pptr pptr;
  292. error = xfarray_load(rp->pptr_recs, array_cur, &pptr);
  293. if (error)
  294. goto out_unlock;
  295. error = xfblob_loadname(rp->pptr_names, pptr.name_cookie,
  296. &rp->xname, pptr.namelen);
  297. if (error)
  298. goto out_unlock;
  299. rp->xname.len = pptr.namelen;
  300. mutex_unlock(&rp->pscan.lock);
  301. error = xrep_parent_replay_update(rp, &rp->xname, &pptr);
  302. if (error)
  303. return error;
  304. mutex_lock(&rp->pscan.lock);
  305. }
  306. /* Empty out both arrays now that we've added the entries. */
  307. xfarray_truncate(rp->pptr_recs);
  308. xfblob_truncate(rp->pptr_names);
  309. mutex_unlock(&rp->pscan.lock);
  310. return 0;
  311. out_unlock:
  312. mutex_unlock(&rp->pscan.lock);
  313. return error;
  314. }
  315. /*
  316. * Remember that we want to create a parent pointer in the tempfile. These
  317. * stashed actions will be replayed later.
  318. */
  319. STATIC int
  320. xrep_parent_stash_parentadd(
  321. struct xrep_parent *rp,
  322. const struct xfs_name *name,
  323. const struct xfs_inode *dp)
  324. {
  325. struct xrep_pptr pptr = {
  326. .action = XREP_PPTR_ADD,
  327. .namelen = name->len,
  328. };
  329. int error;
  330. trace_xrep_parent_stash_parentadd(rp->sc->tempip, dp, name);
  331. xfs_inode_to_parent_rec(&pptr.pptr_rec, dp);
  332. error = xfblob_storename(rp->pptr_names, &pptr.name_cookie, name);
  333. if (error)
  334. return error;
  335. return xfarray_append(rp->pptr_recs, &pptr);
  336. }
  337. /*
  338. * Remember that we want to remove a parent pointer from the tempfile. These
  339. * stashed actions will be replayed later.
  340. */
  341. STATIC int
  342. xrep_parent_stash_parentremove(
  343. struct xrep_parent *rp,
  344. const struct xfs_name *name,
  345. const struct xfs_inode *dp)
  346. {
  347. struct xrep_pptr pptr = {
  348. .action = XREP_PPTR_REMOVE,
  349. .namelen = name->len,
  350. };
  351. int error;
  352. trace_xrep_parent_stash_parentremove(rp->sc->tempip, dp, name);
  353. xfs_inode_to_parent_rec(&pptr.pptr_rec, dp);
  354. error = xfblob_storename(rp->pptr_names, &pptr.name_cookie, name);
  355. if (error)
  356. return error;
  357. return xfarray_append(rp->pptr_recs, &pptr);
  358. }
  359. /*
  360. * Examine an entry of a directory. If this dirent leads us back to the file
  361. * whose parent pointers we're rebuilding, add a pptr to the temporary
  362. * directory.
  363. */
  364. STATIC int
  365. xrep_parent_scan_dirent(
  366. struct xfs_scrub *sc,
  367. struct xfs_inode *dp,
  368. xfs_dir2_dataptr_t dapos,
  369. const struct xfs_name *name,
  370. xfs_ino_t ino,
  371. void *priv)
  372. {
  373. struct xrep_parent *rp = priv;
  374. int error;
  375. /* Dirent doesn't point to this directory. */
  376. if (ino != rp->sc->ip->i_ino)
  377. return 0;
  378. /* No weird looking names. */
  379. if (name->len == 0 || !xfs_dir2_namecheck(name->name, name->len))
  380. return -EFSCORRUPTED;
  381. /* No mismatching ftypes. */
  382. if (name->type != xfs_mode_to_ftype(VFS_I(sc->ip)->i_mode))
  383. return -EFSCORRUPTED;
  384. /* Don't pick up dot or dotdot entries; we only want child dirents. */
  385. if (xfs_dir2_samename(name, &xfs_name_dotdot) ||
  386. xfs_dir2_samename(name, &xfs_name_dot))
  387. return 0;
  388. /*
  389. * Transform this dirent into a parent pointer and queue it for later
  390. * addition to the temporary file.
  391. */
  392. mutex_lock(&rp->pscan.lock);
  393. error = xrep_parent_stash_parentadd(rp, name, dp);
  394. mutex_unlock(&rp->pscan.lock);
  395. return error;
  396. }
  397. /*
  398. * Decide if we want to look for dirents in this directory. Skip the file
  399. * being repaired and any files being used to stage repairs.
  400. */
  401. static inline bool
  402. xrep_parent_want_scan(
  403. struct xrep_parent *rp,
  404. const struct xfs_inode *ip)
  405. {
  406. return ip != rp->sc->ip && !xrep_is_tempfile(ip);
  407. }
  408. /*
  409. * Take ILOCK on a file that we want to scan.
  410. *
  411. * Select ILOCK_EXCL if the file is a directory with an unloaded data bmbt.
  412. * Otherwise, take ILOCK_SHARED.
  413. */
  414. static inline unsigned int
  415. xrep_parent_scan_ilock(
  416. struct xrep_parent *rp,
  417. struct xfs_inode *ip)
  418. {
  419. uint lock_mode = XFS_ILOCK_SHARED;
  420. /* Still need to take the shared ILOCK to advance the iscan cursor. */
  421. if (!xrep_parent_want_scan(rp, ip))
  422. goto lock;
  423. if (S_ISDIR(VFS_I(ip)->i_mode) && xfs_need_iread_extents(&ip->i_df)) {
  424. lock_mode = XFS_ILOCK_EXCL;
  425. goto lock;
  426. }
  427. lock:
  428. xfs_ilock(ip, lock_mode);
  429. return lock_mode;
  430. }
  431. /*
  432. * Scan this file for relevant child dirents that point to the file whose
  433. * parent pointers we're rebuilding.
  434. */
  435. STATIC int
  436. xrep_parent_scan_file(
  437. struct xrep_parent *rp,
  438. struct xfs_inode *ip)
  439. {
  440. unsigned int lock_mode;
  441. int error = 0;
  442. lock_mode = xrep_parent_scan_ilock(rp, ip);
  443. if (!xrep_parent_want_scan(rp, ip))
  444. goto scan_done;
  445. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  446. /*
  447. * If the directory looks as though it has been zapped by the
  448. * inode record repair code, we cannot scan for child dirents.
  449. */
  450. if (xchk_dir_looks_zapped(ip)) {
  451. error = -EBUSY;
  452. goto scan_done;
  453. }
  454. error = xchk_dir_walk(rp->sc, ip, xrep_parent_scan_dirent, rp);
  455. if (error)
  456. goto scan_done;
  457. }
  458. scan_done:
  459. xchk_iscan_mark_visited(&rp->pscan.iscan, ip);
  460. xfs_iunlock(ip, lock_mode);
  461. return error;
  462. }
  463. /* Decide if we've stashed too much pptr data in memory. */
  464. static inline bool
  465. xrep_parent_want_flush_stashed(
  466. struct xrep_parent *rp)
  467. {
  468. unsigned long long bytes;
  469. bytes = xfarray_bytes(rp->pptr_recs) + xfblob_bytes(rp->pptr_names);
  470. return bytes > XREP_PARENT_MAX_STASH_BYTES;
  471. }
  472. /*
  473. * Scan all directories in the filesystem to look for dirents that we can turn
  474. * into parent pointers.
  475. */
  476. STATIC int
  477. xrep_parent_scan_dirtree(
  478. struct xrep_parent *rp)
  479. {
  480. struct xfs_scrub *sc = rp->sc;
  481. struct xfs_inode *ip;
  482. int error;
  483. /*
  484. * Filesystem scans are time consuming. Drop the file ILOCK and all
  485. * other resources for the duration of the scan and hope for the best.
  486. * The live update hooks will keep our scan information up to date.
  487. */
  488. xchk_trans_cancel(sc);
  489. if (sc->ilock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL))
  490. xchk_iunlock(sc, sc->ilock_flags & (XFS_ILOCK_SHARED |
  491. XFS_ILOCK_EXCL));
  492. error = xchk_trans_alloc_empty(sc);
  493. if (error)
  494. return error;
  495. while ((error = xchk_iscan_iter(&rp->pscan.iscan, &ip)) == 1) {
  496. bool flush;
  497. error = xrep_parent_scan_file(rp, ip);
  498. xchk_irele(sc, ip);
  499. if (error)
  500. break;
  501. /* Flush stashed pptr updates to constrain memory usage. */
  502. mutex_lock(&rp->pscan.lock);
  503. flush = xrep_parent_want_flush_stashed(rp);
  504. mutex_unlock(&rp->pscan.lock);
  505. if (flush) {
  506. xchk_trans_cancel(sc);
  507. error = xrep_tempfile_iolock_polled(sc);
  508. if (error)
  509. break;
  510. error = xrep_parent_replay_updates(rp);
  511. xrep_tempfile_iounlock(sc);
  512. if (error)
  513. break;
  514. error = xchk_trans_alloc_empty(sc);
  515. if (error)
  516. break;
  517. }
  518. if (xchk_should_terminate(sc, &error))
  519. break;
  520. }
  521. xchk_iscan_iter_finish(&rp->pscan.iscan);
  522. if (error) {
  523. /*
  524. * If we couldn't grab an inode that was busy with a state
  525. * change, change the error code so that we exit to userspace
  526. * as quickly as possible.
  527. */
  528. if (error == -EBUSY)
  529. return -ECANCELED;
  530. return error;
  531. }
  532. /*
  533. * Retake sc->ip's ILOCK now that we're done flushing stashed parent
  534. * pointers. We end this function with an empty transaction and the
  535. * ILOCK.
  536. */
  537. xchk_ilock(rp->sc, XFS_ILOCK_EXCL);
  538. return 0;
  539. }
  540. /*
  541. * Capture dirent updates being made by other threads which are relevant to the
  542. * file being repaired.
  543. */
  544. STATIC int
  545. xrep_parent_live_update(
  546. struct notifier_block *nb,
  547. unsigned long action,
  548. void *data)
  549. {
  550. struct xfs_dir_update_params *p = data;
  551. struct xrep_parent *rp;
  552. struct xfs_scrub *sc;
  553. int error;
  554. rp = container_of(nb, struct xrep_parent, pscan.dhook.dirent_hook.nb);
  555. sc = rp->sc;
  556. /*
  557. * This thread updated a dirent that points to the file that we're
  558. * repairing, so stash the update for replay against the temporary
  559. * file.
  560. */
  561. if (p->ip->i_ino == sc->ip->i_ino &&
  562. xchk_iscan_want_live_update(&rp->pscan.iscan, p->dp->i_ino)) {
  563. mutex_lock(&rp->pscan.lock);
  564. if (p->delta > 0)
  565. error = xrep_parent_stash_parentadd(rp, p->name, p->dp);
  566. else
  567. error = xrep_parent_stash_parentremove(rp, p->name,
  568. p->dp);
  569. if (!error)
  570. rp->saw_pptr_updates = true;
  571. mutex_unlock(&rp->pscan.lock);
  572. if (error)
  573. goto out_abort;
  574. }
  575. return NOTIFY_DONE;
  576. out_abort:
  577. xchk_iscan_abort(&rp->pscan.iscan);
  578. return NOTIFY_DONE;
  579. }
  580. /* Reset a directory's dotdot entry, if needed. */
  581. STATIC int
  582. xrep_parent_reset_dotdot(
  583. struct xrep_parent *rp)
  584. {
  585. struct xfs_scrub *sc = rp->sc;
  586. xfs_ino_t ino;
  587. unsigned int spaceres;
  588. int error = 0;
  589. ASSERT(sc->ilock_flags & XFS_ILOCK_EXCL);
  590. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot, &ino);
  591. if (error || ino == rp->pscan.parent_ino)
  592. return error;
  593. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  594. trace_xrep_parent_reset_dotdot(sc->ip, rp->pscan.parent_ino);
  595. /*
  596. * Reserve more space just in case we have to expand the dir. We're
  597. * allowed to exceed quota to repair inconsistent metadata.
  598. */
  599. spaceres = xfs_rename_space_res(sc->mp, 0, false, xfs_name_dotdot.len,
  600. false);
  601. error = xfs_trans_reserve_more_inode(sc->tp, sc->ip, spaceres, 0,
  602. true);
  603. if (error)
  604. return error;
  605. error = xfs_dir_replace(sc->tp, sc->ip, &xfs_name_dotdot,
  606. rp->pscan.parent_ino, spaceres);
  607. if (error)
  608. return error;
  609. /*
  610. * Roll transaction to detach the inode from the transaction but retain
  611. * ILOCK_EXCL.
  612. */
  613. return xfs_trans_roll(&sc->tp);
  614. }
  615. /* Pass back the parent inumber if this a parent pointer */
  616. STATIC int
  617. xrep_parent_lookup_pptr(
  618. struct xfs_scrub *sc,
  619. struct xfs_inode *ip,
  620. unsigned int attr_flags,
  621. const unsigned char *name,
  622. unsigned int namelen,
  623. const void *value,
  624. unsigned int valuelen,
  625. void *priv)
  626. {
  627. xfs_ino_t *inop = priv;
  628. xfs_ino_t parent_ino;
  629. int error;
  630. if (!(attr_flags & XFS_ATTR_PARENT))
  631. return 0;
  632. error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
  633. valuelen, &parent_ino, NULL);
  634. if (error)
  635. return error;
  636. *inop = parent_ino;
  637. return -ECANCELED;
  638. }
  639. /*
  640. * Find the first parent of the scrub target by walking parent pointers for
  641. * the purpose of deciding if we're going to move it to the orphanage.
  642. * We don't care if the attr fork is zapped.
  643. */
  644. STATIC int
  645. xrep_parent_lookup_pptrs(
  646. struct xfs_scrub *sc,
  647. xfs_ino_t *inop)
  648. {
  649. int error;
  650. *inop = NULLFSINO;
  651. error = xchk_xattr_walk(sc, sc->ip, xrep_parent_lookup_pptr, NULL,
  652. inop);
  653. if (error && error != -ECANCELED)
  654. return error;
  655. return 0;
  656. }
  657. /*
  658. * Move the current file to the orphanage.
  659. *
  660. * Caller must hold IOLOCK_EXCL on @sc->ip, and no other inode locks. Upon
  661. * successful return, the scrub transaction will have enough extra reservation
  662. * to make the move; it will hold IOLOCK_EXCL and ILOCK_EXCL of @sc->ip and the
  663. * orphanage; and both inodes will be ijoined.
  664. */
  665. STATIC int
  666. xrep_parent_move_to_orphanage(
  667. struct xrep_parent *rp)
  668. {
  669. struct xfs_scrub *sc = rp->sc;
  670. xfs_ino_t orig_parent, new_parent;
  671. int error;
  672. if (S_ISDIR(VFS_I(sc->ip)->i_mode)) {
  673. /*
  674. * We are about to drop the ILOCK on sc->ip to lock the
  675. * orphanage and prepare for the adoption. Therefore, look up
  676. * the old dotdot entry for sc->ip so that we can compare it
  677. * after we re-lock sc->ip.
  678. */
  679. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
  680. &orig_parent);
  681. if (error)
  682. return error;
  683. } else {
  684. /*
  685. * We haven't dropped the ILOCK since we committed the new
  686. * xattr structure (and hence the new parent pointer records),
  687. * which means that the file cannot have been moved in the
  688. * directory tree, and there are no parents.
  689. */
  690. orig_parent = NULLFSINO;
  691. }
  692. /*
  693. * Drop the ILOCK on the scrub target and commit the transaction.
  694. * Adoption computes its own resource requirements and gathers the
  695. * necessary components.
  696. */
  697. error = xrep_trans_commit(sc);
  698. if (error)
  699. return error;
  700. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  701. /* If we can take the orphanage's iolock then we're ready to move. */
  702. if (!xrep_orphanage_ilock_nowait(sc, XFS_IOLOCK_EXCL)) {
  703. xchk_iunlock(sc, sc->ilock_flags);
  704. error = xrep_orphanage_iolock_two(sc);
  705. if (error)
  706. return error;
  707. }
  708. /* Grab transaction and ILOCK the two files. */
  709. error = xrep_adoption_trans_alloc(sc, &rp->adoption);
  710. if (error)
  711. return error;
  712. error = xrep_adoption_compute_name(&rp->adoption, &rp->xname);
  713. if (error)
  714. return error;
  715. /*
  716. * Now that we've reacquired the ILOCK on sc->ip, look up the dotdot
  717. * entry again. If the parent changed or the child was unlinked while
  718. * the child directory was unlocked, we don't need to move the child to
  719. * the orphanage after all. For a non-directory, we have to scan for
  720. * the first parent pointer to see if one has been added.
  721. */
  722. if (S_ISDIR(VFS_I(sc->ip)->i_mode))
  723. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
  724. &new_parent);
  725. else
  726. error = xrep_parent_lookup_pptrs(sc, &new_parent);
  727. if (error)
  728. return error;
  729. /*
  730. * Attach to the orphanage if we still have a linked directory and it
  731. * hasn't been moved.
  732. */
  733. if (orig_parent == new_parent && VFS_I(sc->ip)->i_nlink > 0) {
  734. error = xrep_adoption_move(&rp->adoption);
  735. if (error)
  736. return error;
  737. }
  738. /*
  739. * Launder the scrub transaction so we can drop the orphanage ILOCK
  740. * and IOLOCK. Return holding the scrub target's ILOCK and IOLOCK.
  741. */
  742. error = xrep_adoption_trans_roll(&rp->adoption);
  743. if (error)
  744. return error;
  745. xrep_orphanage_iunlock(sc, XFS_ILOCK_EXCL);
  746. xrep_orphanage_iunlock(sc, XFS_IOLOCK_EXCL);
  747. return 0;
  748. }
  749. /* Ensure that the xattr value buffer is large enough. */
  750. STATIC int
  751. xrep_parent_alloc_xattr_value(
  752. struct xrep_parent *rp,
  753. size_t bufsize)
  754. {
  755. void *new_val;
  756. if (rp->xattr_value_sz >= bufsize)
  757. return 0;
  758. if (rp->xattr_value) {
  759. kvfree(rp->xattr_value);
  760. rp->xattr_value = NULL;
  761. rp->xattr_value_sz = 0;
  762. }
  763. new_val = kvmalloc(bufsize, XCHK_GFP_FLAGS);
  764. if (!new_val)
  765. return -ENOMEM;
  766. rp->xattr_value = new_val;
  767. rp->xattr_value_sz = bufsize;
  768. return 0;
  769. }
  770. /* Retrieve the (remote) value of a non-pptr xattr. */
  771. STATIC int
  772. xrep_parent_fetch_xattr_remote(
  773. struct xrep_parent *rp,
  774. struct xfs_inode *ip,
  775. unsigned int attr_flags,
  776. const unsigned char *name,
  777. unsigned int namelen,
  778. unsigned int valuelen)
  779. {
  780. struct xfs_scrub *sc = rp->sc;
  781. struct xfs_da_args args = {
  782. .attr_filter = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
  783. .geo = sc->mp->m_attr_geo,
  784. .whichfork = XFS_ATTR_FORK,
  785. .dp = ip,
  786. .name = name,
  787. .namelen = namelen,
  788. .trans = sc->tp,
  789. .valuelen = valuelen,
  790. .owner = ip->i_ino,
  791. };
  792. int error;
  793. /*
  794. * If we need a larger value buffer, try to allocate one. If that
  795. * fails, return with -EDEADLOCK to try harder.
  796. */
  797. error = xrep_parent_alloc_xattr_value(rp, valuelen);
  798. if (error == -ENOMEM)
  799. return -EDEADLOCK;
  800. if (error)
  801. return error;
  802. args.value = rp->xattr_value;
  803. xfs_attr_sethash(&args);
  804. return xfs_attr_get_ilocked(&args);
  805. }
  806. /* Stash non-pptr attributes for later replay into the temporary file. */
  807. STATIC int
  808. xrep_parent_stash_xattr(
  809. struct xfs_scrub *sc,
  810. struct xfs_inode *ip,
  811. unsigned int attr_flags,
  812. const unsigned char *name,
  813. unsigned int namelen,
  814. const void *value,
  815. unsigned int valuelen,
  816. void *priv)
  817. {
  818. struct xrep_parent_xattr key = {
  819. .valuelen = valuelen,
  820. .namelen = namelen,
  821. .flags = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
  822. };
  823. struct xrep_parent *rp = priv;
  824. int error;
  825. if (attr_flags & (XFS_ATTR_INCOMPLETE | XFS_ATTR_PARENT))
  826. return 0;
  827. if (!value) {
  828. error = xrep_parent_fetch_xattr_remote(rp, ip, attr_flags,
  829. name, namelen, valuelen);
  830. if (error)
  831. return error;
  832. value = rp->xattr_value;
  833. }
  834. trace_xrep_parent_stash_xattr(rp->sc->tempip, key.flags, (void *)name,
  835. key.namelen, key.valuelen);
  836. error = xfblob_store(rp->xattr_blobs, &key.name_cookie, name,
  837. key.namelen);
  838. if (error)
  839. return error;
  840. error = xfblob_store(rp->xattr_blobs, &key.value_cookie, value,
  841. key.valuelen);
  842. if (error)
  843. return error;
  844. return xfarray_append(rp->xattr_records, &key);
  845. }
  846. /* Insert one xattr key/value. */
  847. STATIC int
  848. xrep_parent_insert_xattr(
  849. struct xrep_parent *rp,
  850. const struct xrep_parent_xattr *key)
  851. {
  852. struct xfs_da_args args = {
  853. .dp = rp->sc->tempip,
  854. .attr_filter = key->flags,
  855. .namelen = key->namelen,
  856. .valuelen = key->valuelen,
  857. .owner = rp->sc->ip->i_ino,
  858. .geo = rp->sc->mp->m_attr_geo,
  859. .whichfork = XFS_ATTR_FORK,
  860. .op_flags = XFS_DA_OP_OKNOENT,
  861. };
  862. int error;
  863. ASSERT(!(key->flags & XFS_ATTR_PARENT));
  864. /*
  865. * Grab pointers to the scrub buffer so that we can use them to insert
  866. * attrs into the temp file.
  867. */
  868. args.name = rp->xattr_name;
  869. args.value = rp->xattr_value;
  870. /*
  871. * The attribute name is stored near the end of the in-core buffer,
  872. * though we reserve one more byte to ensure null termination.
  873. */
  874. rp->xattr_name[XATTR_NAME_MAX] = 0;
  875. error = xfblob_load(rp->xattr_blobs, key->name_cookie, rp->xattr_name,
  876. key->namelen);
  877. if (error)
  878. return error;
  879. error = xfblob_free(rp->xattr_blobs, key->name_cookie);
  880. if (error)
  881. return error;
  882. error = xfblob_load(rp->xattr_blobs, key->value_cookie, args.value,
  883. key->valuelen);
  884. if (error)
  885. return error;
  886. error = xfblob_free(rp->xattr_blobs, key->value_cookie);
  887. if (error)
  888. return error;
  889. rp->xattr_name[key->namelen] = 0;
  890. trace_xrep_parent_insert_xattr(rp->sc->tempip, key->flags,
  891. rp->xattr_name, key->namelen, key->valuelen);
  892. xfs_attr_sethash(&args);
  893. return xfs_attr_set(&args, XFS_ATTRUPDATE_UPSERT, false);
  894. }
  895. /*
  896. * Periodically flush salvaged attributes to the temporary file. This is done
  897. * to reduce the memory requirements of the xattr rebuild because files can
  898. * contain millions of attributes.
  899. */
  900. STATIC int
  901. xrep_parent_flush_xattrs(
  902. struct xrep_parent *rp)
  903. {
  904. xfarray_idx_t array_cur;
  905. int error;
  906. /*
  907. * Entering this function, the scrub context has a reference to the
  908. * inode being repaired, the temporary file, and the empty scrub
  909. * transaction that we created for the xattr scan. We hold ILOCK_EXCL
  910. * on the inode being repaired.
  911. *
  912. * To constrain kernel memory use, we occasionally flush salvaged
  913. * xattrs from the xfarray and xfblob structures into the temporary
  914. * file in preparation for exchanging the xattr structures at the end.
  915. * Updating the temporary file requires a transaction, so we commit the
  916. * scrub transaction and drop the ILOCK so that xfs_attr_set can
  917. * allocate whatever transaction it wants.
  918. *
  919. * We still hold IOLOCK_EXCL on the inode being repaired, which
  920. * prevents anyone from adding xattrs (or parent pointers) while we're
  921. * flushing.
  922. */
  923. xchk_trans_cancel(rp->sc);
  924. xchk_iunlock(rp->sc, XFS_ILOCK_EXCL);
  925. /*
  926. * Take the IOLOCK of the temporary file while we modify xattrs. This
  927. * isn't strictly required because the temporary file is never revealed
  928. * to userspace, but we follow the same locking rules. We still hold
  929. * sc->ip's IOLOCK.
  930. */
  931. error = xrep_tempfile_iolock_polled(rp->sc);
  932. if (error)
  933. return error;
  934. /* Add all the salvaged attrs to the temporary file. */
  935. foreach_xfarray_idx(rp->xattr_records, array_cur) {
  936. struct xrep_parent_xattr key;
  937. error = xfarray_load(rp->xattr_records, array_cur, &key);
  938. if (error)
  939. return error;
  940. error = xrep_parent_insert_xattr(rp, &key);
  941. if (error)
  942. return error;
  943. }
  944. /* Empty out both arrays now that we've added the entries. */
  945. xfarray_truncate(rp->xattr_records);
  946. xfblob_truncate(rp->xattr_blobs);
  947. xrep_tempfile_iounlock(rp->sc);
  948. /* Recreate the empty transaction and relock the inode. */
  949. error = xchk_trans_alloc_empty(rp->sc);
  950. if (error)
  951. return error;
  952. xchk_ilock(rp->sc, XFS_ILOCK_EXCL);
  953. return 0;
  954. }
  955. /* Decide if we've stashed too much xattr data in memory. */
  956. static inline bool
  957. xrep_parent_want_flush_xattrs(
  958. struct xrep_parent *rp)
  959. {
  960. unsigned long long bytes;
  961. bytes = xfarray_bytes(rp->xattr_records) +
  962. xfblob_bytes(rp->xattr_blobs);
  963. return bytes > XREP_PARENT_XATTR_MAX_STASH_BYTES;
  964. }
  965. /* Flush staged attributes to the temporary file if we're over the limit. */
  966. STATIC int
  967. xrep_parent_try_flush_xattrs(
  968. struct xfs_scrub *sc,
  969. void *priv)
  970. {
  971. struct xrep_parent *rp = priv;
  972. int error;
  973. if (!xrep_parent_want_flush_xattrs(rp))
  974. return 0;
  975. error = xrep_parent_flush_xattrs(rp);
  976. if (error)
  977. return error;
  978. /*
  979. * If there were any parent pointer updates to the xattr structure
  980. * while we dropped the ILOCK, the xattr structure is now stale.
  981. * Signal to the attr copy process that we need to start over, but
  982. * this time without opportunistic attr flushing.
  983. *
  984. * This is unlikely to happen, so we're ok with restarting the copy.
  985. */
  986. mutex_lock(&rp->pscan.lock);
  987. if (rp->saw_pptr_updates)
  988. error = -ESTALE;
  989. mutex_unlock(&rp->pscan.lock);
  990. return error;
  991. }
  992. /* Copy all the non-pptr extended attributes into the temporary file. */
  993. STATIC int
  994. xrep_parent_copy_xattrs(
  995. struct xrep_parent *rp)
  996. {
  997. struct xfs_scrub *sc = rp->sc;
  998. int error;
  999. /*
  1000. * Clear the pptr updates flag. We hold sc->ip ILOCKed, so there
  1001. * can't be any parent pointer updates in progress.
  1002. */
  1003. mutex_lock(&rp->pscan.lock);
  1004. rp->saw_pptr_updates = false;
  1005. mutex_unlock(&rp->pscan.lock);
  1006. /* Copy xattrs, stopping periodically to flush the incore buffers. */
  1007. error = xchk_xattr_walk(sc, sc->ip, xrep_parent_stash_xattr,
  1008. xrep_parent_try_flush_xattrs, rp);
  1009. if (error && error != -ESTALE)
  1010. return error;
  1011. if (error == -ESTALE) {
  1012. /*
  1013. * The xattr copy collided with a parent pointer update.
  1014. * Restart the copy, but this time hold the ILOCK all the way
  1015. * to the end to lock out any directory parent pointer updates.
  1016. */
  1017. error = xchk_xattr_walk(sc, sc->ip, xrep_parent_stash_xattr,
  1018. NULL, rp);
  1019. if (error)
  1020. return error;
  1021. }
  1022. /* Flush any remaining stashed xattrs to the temporary file. */
  1023. if (xfarray_bytes(rp->xattr_records) == 0)
  1024. return 0;
  1025. return xrep_parent_flush_xattrs(rp);
  1026. }
  1027. /*
  1028. * Ensure that @sc->ip and @sc->tempip both have attribute forks before we head
  1029. * into the attr fork exchange transaction. All files on a filesystem with
  1030. * parent pointers must have an attr fork because the parent pointer code does
  1031. * not itself add attribute forks.
  1032. *
  1033. * Note: Unlinkable unlinked files don't need one, but the overhead of having
  1034. * an unnecessary attr fork is not justified by the additional code complexity
  1035. * that would be needed to track that state correctly.
  1036. */
  1037. STATIC int
  1038. xrep_parent_ensure_attr_fork(
  1039. struct xrep_parent *rp)
  1040. {
  1041. struct xfs_scrub *sc = rp->sc;
  1042. int error;
  1043. error = xfs_attr_add_fork(sc->tempip,
  1044. sizeof(struct xfs_attr_sf_hdr), 1);
  1045. if (error)
  1046. return error;
  1047. return xfs_attr_add_fork(sc->ip, sizeof(struct xfs_attr_sf_hdr), 1);
  1048. }
  1049. /*
  1050. * Finish replaying stashed parent pointer updates, allocate a transaction for
  1051. * exchanging extent mappings, and take the ILOCKs of both files before we
  1052. * commit the new attribute structure.
  1053. */
  1054. STATIC int
  1055. xrep_parent_finalize_tempfile(
  1056. struct xrep_parent *rp)
  1057. {
  1058. struct xfs_scrub *sc = rp->sc;
  1059. int error;
  1060. /*
  1061. * Repair relies on the ILOCK to quiesce all possible xattr updates.
  1062. * Replay all queued parent pointer updates into the tempfile before
  1063. * exchanging the contents, even if that means dropping the ILOCKs and
  1064. * the transaction.
  1065. */
  1066. do {
  1067. error = xrep_parent_replay_updates(rp);
  1068. if (error)
  1069. return error;
  1070. error = xrep_parent_ensure_attr_fork(rp);
  1071. if (error)
  1072. return error;
  1073. error = xrep_tempexch_trans_alloc(sc, XFS_ATTR_FORK, &rp->tx);
  1074. if (error)
  1075. return error;
  1076. if (xfarray_length(rp->pptr_recs) == 0)
  1077. break;
  1078. xchk_trans_cancel(sc);
  1079. xrep_tempfile_iunlock_both(sc);
  1080. } while (!xchk_should_terminate(sc, &error));
  1081. return error;
  1082. }
  1083. /*
  1084. * Replay all the stashed parent pointers into the temporary file, copy all
  1085. * the non-pptr xattrs from the file being repaired into the temporary file,
  1086. * and exchange the attr fork contents atomically.
  1087. */
  1088. STATIC int
  1089. xrep_parent_rebuild_pptrs(
  1090. struct xrep_parent *rp)
  1091. {
  1092. struct xfs_scrub *sc = rp->sc;
  1093. xfs_ino_t parent_ino = NULLFSINO;
  1094. int error;
  1095. /*
  1096. * Copy non-ppttr xattrs from the file being repaired into the
  1097. * temporary file's xattr structure. We hold sc->ip's IOLOCK, which
  1098. * prevents setxattr/removexattr calls from occurring, but renames
  1099. * update the parent pointers without holding IOLOCK. If we detect
  1100. * stale attr structures, we restart the scan but only flush at the
  1101. * end.
  1102. */
  1103. error = xrep_parent_copy_xattrs(rp);
  1104. if (error)
  1105. return error;
  1106. /*
  1107. * Cancel the empty transaction that we used to walk and copy attrs,
  1108. * and drop the ILOCK so that we can take the IOLOCK on the temporary
  1109. * file. We still hold sc->ip's IOLOCK.
  1110. */
  1111. xchk_trans_cancel(sc);
  1112. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  1113. error = xrep_tempfile_iolock_polled(sc);
  1114. if (error)
  1115. return error;
  1116. /*
  1117. * Allocate transaction, lock inodes, and make sure that we've replayed
  1118. * all the stashed pptr updates to the tempdir. After this point,
  1119. * we're ready to exchange the attr fork mappings.
  1120. */
  1121. error = xrep_parent_finalize_tempfile(rp);
  1122. if (error)
  1123. return error;
  1124. /* Last chance to abort before we start committing pptr fixes. */
  1125. if (xchk_should_terminate(sc, &error))
  1126. return error;
  1127. if (xchk_iscan_aborted(&rp->pscan.iscan))
  1128. return -ECANCELED;
  1129. /*
  1130. * Exchange the attr fork contents and junk the old attr fork contents,
  1131. * which are now in the tempfile.
  1132. */
  1133. error = xrep_xattr_swap(sc, &rp->tx);
  1134. if (error)
  1135. return error;
  1136. error = xrep_xattr_reset_tempfile_fork(sc);
  1137. if (error)
  1138. return error;
  1139. /*
  1140. * Roll to get a transaction without any inodes joined to it. Then we
  1141. * can drop the tempfile's ILOCK and IOLOCK before doing more work on
  1142. * the scrub target file.
  1143. */
  1144. error = xfs_trans_roll(&sc->tp);
  1145. if (error)
  1146. return error;
  1147. xrep_tempfile_iunlock(sc);
  1148. xrep_tempfile_iounlock(sc);
  1149. /*
  1150. * We've committed the new parent pointers. Find at least one parent
  1151. * so that we can decide if we're moving this file to the orphanage.
  1152. * For this purpose, root directories are their own parents.
  1153. */
  1154. if (sc->ip == sc->mp->m_rootip) {
  1155. xrep_findparent_scan_found(&rp->pscan, sc->ip->i_ino);
  1156. } else {
  1157. error = xrep_parent_lookup_pptrs(sc, &parent_ino);
  1158. if (error)
  1159. return error;
  1160. if (parent_ino != NULLFSINO)
  1161. xrep_findparent_scan_found(&rp->pscan, parent_ino);
  1162. }
  1163. return 0;
  1164. }
  1165. /*
  1166. * Commit the new parent pointer structure (currently only the dotdot entry) to
  1167. * the file that we're repairing.
  1168. */
  1169. STATIC int
  1170. xrep_parent_rebuild_tree(
  1171. struct xrep_parent *rp)
  1172. {
  1173. int error;
  1174. if (xfs_has_parent(rp->sc->mp)) {
  1175. error = xrep_parent_rebuild_pptrs(rp);
  1176. if (error)
  1177. return error;
  1178. }
  1179. if (rp->pscan.parent_ino == NULLFSINO) {
  1180. if (xrep_orphanage_can_adopt(rp->sc))
  1181. return xrep_parent_move_to_orphanage(rp);
  1182. return -EFSCORRUPTED;
  1183. }
  1184. if (S_ISDIR(VFS_I(rp->sc->ip)->i_mode))
  1185. return xrep_parent_reset_dotdot(rp);
  1186. return 0;
  1187. }
  1188. /* Count the number of parent pointers. */
  1189. STATIC int
  1190. xrep_parent_count_pptr(
  1191. struct xfs_scrub *sc,
  1192. struct xfs_inode *ip,
  1193. unsigned int attr_flags,
  1194. const unsigned char *name,
  1195. unsigned int namelen,
  1196. const void *value,
  1197. unsigned int valuelen,
  1198. void *priv)
  1199. {
  1200. struct xrep_parent *rp = priv;
  1201. int error;
  1202. if (!(attr_flags & XFS_ATTR_PARENT))
  1203. return 0;
  1204. error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
  1205. valuelen, NULL, NULL);
  1206. if (error)
  1207. return error;
  1208. rp->parents++;
  1209. return 0;
  1210. }
  1211. /*
  1212. * After all parent pointer rebuilding and adoption activity completes, reset
  1213. * the link count of this nondirectory, having scanned the fs to rebuild all
  1214. * parent pointers.
  1215. */
  1216. STATIC int
  1217. xrep_parent_set_nondir_nlink(
  1218. struct xrep_parent *rp)
  1219. {
  1220. struct xfs_scrub *sc = rp->sc;
  1221. struct xfs_inode *ip = sc->ip;
  1222. struct xfs_perag *pag;
  1223. bool joined = false;
  1224. int error;
  1225. /* Count parent pointers so we can reset the file link count. */
  1226. rp->parents = 0;
  1227. error = xchk_xattr_walk(sc, ip, xrep_parent_count_pptr, NULL, rp);
  1228. if (error)
  1229. return error;
  1230. if (rp->parents > 0 && xfs_inode_on_unlinked_list(ip)) {
  1231. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  1232. joined = true;
  1233. /*
  1234. * The file is on the unlinked list but we found parents.
  1235. * Remove the file from the unlinked list.
  1236. */
  1237. pag = xfs_perag_get(sc->mp, XFS_INO_TO_AGNO(sc->mp, ip->i_ino));
  1238. if (!pag) {
  1239. ASSERT(0);
  1240. return -EFSCORRUPTED;
  1241. }
  1242. error = xfs_iunlink_remove(sc->tp, pag, ip);
  1243. xfs_perag_put(pag);
  1244. if (error)
  1245. return error;
  1246. } else if (rp->parents == 0 && !xfs_inode_on_unlinked_list(ip)) {
  1247. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  1248. joined = true;
  1249. /*
  1250. * The file is not on the unlinked list but we found no
  1251. * parents. Add the file to the unlinked list.
  1252. */
  1253. error = xfs_iunlink(sc->tp, ip);
  1254. if (error)
  1255. return error;
  1256. }
  1257. /* Set the correct link count. */
  1258. if (VFS_I(ip)->i_nlink != rp->parents) {
  1259. if (!joined) {
  1260. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  1261. joined = true;
  1262. }
  1263. set_nlink(VFS_I(ip), min_t(unsigned long long, rp->parents,
  1264. XFS_NLINK_PINNED));
  1265. }
  1266. /* Log the inode to keep it moving forward if we dirtied anything. */
  1267. if (joined)
  1268. xfs_trans_log_inode(sc->tp, ip, XFS_ILOG_CORE);
  1269. return 0;
  1270. }
  1271. /* Set up the filesystem scan so we can look for parents. */
  1272. STATIC int
  1273. xrep_parent_setup_scan(
  1274. struct xrep_parent *rp)
  1275. {
  1276. struct xfs_scrub *sc = rp->sc;
  1277. char *descr;
  1278. struct xfs_da_geometry *geo = sc->mp->m_attr_geo;
  1279. int max_len;
  1280. int error;
  1281. if (!xfs_has_parent(sc->mp))
  1282. return xrep_findparent_scan_start(sc, &rp->pscan);
  1283. /* Buffers for copying non-pptr attrs to the tempfile */
  1284. rp->xattr_name = kvmalloc(XATTR_NAME_MAX + 1, XCHK_GFP_FLAGS);
  1285. if (!rp->xattr_name)
  1286. return -ENOMEM;
  1287. /*
  1288. * Allocate enough memory to handle loading local attr values from the
  1289. * xfblob data while flushing stashed attrs to the temporary file.
  1290. * We only realloc the buffer when salvaging remote attr values, so
  1291. * TRY_HARDER means we allocate the maximal attr value size.
  1292. */
  1293. if (sc->flags & XCHK_TRY_HARDER)
  1294. max_len = XATTR_SIZE_MAX;
  1295. else
  1296. max_len = xfs_attr_leaf_entsize_local_max(geo->blksize);
  1297. error = xrep_parent_alloc_xattr_value(rp, max_len);
  1298. if (error)
  1299. goto out_xattr_name;
  1300. /* Set up some staging memory for logging parent pointer updates. */
  1301. descr = xchk_xfile_ino_descr(sc, "parent pointer entries");
  1302. error = xfarray_create(descr, 0, sizeof(struct xrep_pptr),
  1303. &rp->pptr_recs);
  1304. kfree(descr);
  1305. if (error)
  1306. goto out_xattr_value;
  1307. descr = xchk_xfile_ino_descr(sc, "parent pointer names");
  1308. error = xfblob_create(descr, &rp->pptr_names);
  1309. kfree(descr);
  1310. if (error)
  1311. goto out_recs;
  1312. /* Set up some storage for copying attrs before the mapping exchange */
  1313. descr = xchk_xfile_ino_descr(sc,
  1314. "parent pointer retained xattr entries");
  1315. error = xfarray_create(descr, 0, sizeof(struct xrep_parent_xattr),
  1316. &rp->xattr_records);
  1317. kfree(descr);
  1318. if (error)
  1319. goto out_names;
  1320. descr = xchk_xfile_ino_descr(sc,
  1321. "parent pointer retained xattr values");
  1322. error = xfblob_create(descr, &rp->xattr_blobs);
  1323. kfree(descr);
  1324. if (error)
  1325. goto out_attr_keys;
  1326. error = __xrep_findparent_scan_start(sc, &rp->pscan,
  1327. xrep_parent_live_update);
  1328. if (error)
  1329. goto out_attr_values;
  1330. return 0;
  1331. out_attr_values:
  1332. xfblob_destroy(rp->xattr_blobs);
  1333. rp->xattr_blobs = NULL;
  1334. out_attr_keys:
  1335. xfarray_destroy(rp->xattr_records);
  1336. rp->xattr_records = NULL;
  1337. out_names:
  1338. xfblob_destroy(rp->pptr_names);
  1339. rp->pptr_names = NULL;
  1340. out_recs:
  1341. xfarray_destroy(rp->pptr_recs);
  1342. rp->pptr_recs = NULL;
  1343. out_xattr_value:
  1344. kvfree(rp->xattr_value);
  1345. rp->xattr_value = NULL;
  1346. out_xattr_name:
  1347. kvfree(rp->xattr_name);
  1348. rp->xattr_name = NULL;
  1349. return error;
  1350. }
  1351. int
  1352. xrep_parent(
  1353. struct xfs_scrub *sc)
  1354. {
  1355. struct xrep_parent *rp = sc->buf;
  1356. int error;
  1357. /*
  1358. * When the parent pointers feature is enabled, repairs are committed
  1359. * by atomically committing a new xattr structure and reaping the old
  1360. * attr fork. Reaping requires rmap and exchange-range to be enabled.
  1361. */
  1362. if (xfs_has_parent(sc->mp)) {
  1363. if (!xfs_has_rmapbt(sc->mp))
  1364. return -EOPNOTSUPP;
  1365. if (!xfs_has_exchange_range(sc->mp))
  1366. return -EOPNOTSUPP;
  1367. }
  1368. error = xrep_parent_setup_scan(rp);
  1369. if (error)
  1370. return error;
  1371. if (xfs_has_parent(sc->mp))
  1372. error = xrep_parent_scan_dirtree(rp);
  1373. else
  1374. error = xrep_parent_find_dotdot(rp);
  1375. if (error)
  1376. goto out_teardown;
  1377. /* Last chance to abort before we start committing dotdot fixes. */
  1378. if (xchk_should_terminate(sc, &error))
  1379. goto out_teardown;
  1380. error = xrep_parent_rebuild_tree(rp);
  1381. if (error)
  1382. goto out_teardown;
  1383. if (xfs_has_parent(sc->mp) && !S_ISDIR(VFS_I(sc->ip)->i_mode)) {
  1384. error = xrep_parent_set_nondir_nlink(rp);
  1385. if (error)
  1386. goto out_teardown;
  1387. }
  1388. error = xrep_defer_finish(sc);
  1389. out_teardown:
  1390. xrep_parent_teardown(rp);
  1391. return error;
  1392. }