dir_repair.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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_dir2_priv.h"
  23. #include "xfs_bmap.h"
  24. #include "xfs_quota.h"
  25. #include "xfs_bmap_btree.h"
  26. #include "xfs_trans_space.h"
  27. #include "xfs_bmap_util.h"
  28. #include "xfs_exchmaps.h"
  29. #include "xfs_exchrange.h"
  30. #include "xfs_ag.h"
  31. #include "xfs_parent.h"
  32. #include "scrub/xfs_scrub.h"
  33. #include "scrub/scrub.h"
  34. #include "scrub/common.h"
  35. #include "scrub/trace.h"
  36. #include "scrub/repair.h"
  37. #include "scrub/tempfile.h"
  38. #include "scrub/tempexch.h"
  39. #include "scrub/xfile.h"
  40. #include "scrub/xfarray.h"
  41. #include "scrub/xfblob.h"
  42. #include "scrub/iscan.h"
  43. #include "scrub/readdir.h"
  44. #include "scrub/reap.h"
  45. #include "scrub/findparent.h"
  46. #include "scrub/orphanage.h"
  47. #include "scrub/listxattr.h"
  48. /*
  49. * Directory Repair
  50. * ================
  51. *
  52. * We repair directories by reading the directory data blocks looking for
  53. * directory entries that look salvageable (name passes verifiers, entry points
  54. * to a valid allocated inode, etc). Each entry worth salvaging is stashed in
  55. * memory, and the stashed entries are periodically replayed into a temporary
  56. * directory to constrain memory use. Batching the construction of the
  57. * temporary directory in this fashion reduces lock cycling of the directory
  58. * being repaired and the temporary directory, and will later become important
  59. * for parent pointer scanning.
  60. *
  61. * If parent pointers are enabled on this filesystem, we instead reconstruct
  62. * the directory by visiting each parent pointer of each file in the filesystem
  63. * and translating the relevant parent pointer records into dirents. In this
  64. * case, it is advantageous to stash all directory entries created from parent
  65. * pointers for a single child file before replaying them into the temporary
  66. * directory. To save memory, the live filesystem scan reuses the findparent
  67. * fields. Directory repair chooses either parent pointer scanning or
  68. * directory entry salvaging, but not both.
  69. *
  70. * Directory entries added to the temporary directory do not elevate the link
  71. * counts of the inodes found. When salvaging completes, the remaining stashed
  72. * entries are replayed to the temporary directory. An atomic mapping exchange
  73. * is used to commit the new directory blocks to the directory being repaired.
  74. * This will disrupt readdir cursors.
  75. *
  76. * Locking Issues
  77. * --------------
  78. *
  79. * If /a, /a/b, and /c are all directories, the VFS does not take i_rwsem on
  80. * /a/b for a "mv /a/b /c/" operation. This means that only b's ILOCK protects
  81. * b's dotdot update. This is in contrast to every other dotdot update (link,
  82. * remove, mkdir). If the repair code drops the ILOCK, it must either
  83. * revalidate the dotdot entry or use dirent hooks to capture updates from
  84. * other threads.
  85. */
  86. /* Create a dirent in the tempdir. */
  87. #define XREP_DIRENT_ADD (1)
  88. /* Remove a dirent from the tempdir. */
  89. #define XREP_DIRENT_REMOVE (2)
  90. /* Directory entry to be restored in the new directory. */
  91. struct xrep_dirent {
  92. /* Cookie for retrieval of the dirent name. */
  93. xfblob_cookie name_cookie;
  94. /* Target inode number. */
  95. xfs_ino_t ino;
  96. /* Length of the dirent name. */
  97. uint8_t namelen;
  98. /* File type of the dirent. */
  99. uint8_t ftype;
  100. /* XREP_DIRENT_{ADD,REMOVE} */
  101. uint8_t action;
  102. };
  103. /*
  104. * Stash up to 8 pages of recovered dirent data in dir_entries and dir_names
  105. * before we write them to the temp dir.
  106. */
  107. #define XREP_DIR_MAX_STASH_BYTES (PAGE_SIZE * 8)
  108. struct xrep_dir {
  109. struct xfs_scrub *sc;
  110. /* Fixed-size array of xrep_dirent structures. */
  111. struct xfarray *dir_entries;
  112. /* Blobs containing directory entry names. */
  113. struct xfblob *dir_names;
  114. /* Information for exchanging data forks at the end. */
  115. struct xrep_tempexch tx;
  116. /* Preallocated args struct for performing dir operations */
  117. struct xfs_da_args args;
  118. /*
  119. * Information used to scan the filesystem to find the inumber of the
  120. * dotdot entry for this directory. For directory salvaging when
  121. * parent pointers are not enabled, we use the findparent_* functions
  122. * on this object and access only the parent_ino field directly.
  123. *
  124. * When parent pointers are enabled, however, the pptr scanner uses the
  125. * iscan, hooks, lock, and parent_ino fields of this object directly.
  126. * @pscan.lock coordinates access to dir_entries, dir_names,
  127. * parent_ino, subdirs, dirents, and args. This reduces the memory
  128. * requirements of this structure.
  129. */
  130. struct xrep_parent_scan_info pscan;
  131. /*
  132. * Context information for attaching this directory to the lost+found
  133. * if this directory does not have a parent.
  134. */
  135. struct xrep_adoption adoption;
  136. /* How many subdirectories did we find? */
  137. uint64_t subdirs;
  138. /* How many dirents did we find? */
  139. unsigned int dirents;
  140. /* Should we move this directory to the orphanage? */
  141. bool needs_adoption;
  142. /* Directory entry name, plus the trailing null. */
  143. struct xfs_name xname;
  144. unsigned char namebuf[MAXNAMELEN];
  145. };
  146. /* Tear down all the incore stuff we created. */
  147. static void
  148. xrep_dir_teardown(
  149. struct xfs_scrub *sc)
  150. {
  151. struct xrep_dir *rd = sc->buf;
  152. xrep_findparent_scan_teardown(&rd->pscan);
  153. xfblob_destroy(rd->dir_names);
  154. xfarray_destroy(rd->dir_entries);
  155. }
  156. /* Set up for a directory repair. */
  157. int
  158. xrep_setup_directory(
  159. struct xfs_scrub *sc)
  160. {
  161. struct xrep_dir *rd;
  162. int error;
  163. xchk_fsgates_enable(sc, XCHK_FSGATES_DIRENTS);
  164. error = xrep_orphanage_try_create(sc);
  165. if (error)
  166. return error;
  167. error = xrep_tempfile_create(sc, S_IFDIR);
  168. if (error)
  169. return error;
  170. rd = kvzalloc(sizeof(struct xrep_dir), XCHK_GFP_FLAGS);
  171. if (!rd)
  172. return -ENOMEM;
  173. rd->sc = sc;
  174. rd->xname.name = rd->namebuf;
  175. sc->buf = rd;
  176. return 0;
  177. }
  178. /*
  179. * Look up the dotdot entry and confirm that it's really the parent.
  180. * Returns NULLFSINO if we don't know what to do.
  181. */
  182. static inline xfs_ino_t
  183. xrep_dir_lookup_parent(
  184. struct xrep_dir *rd)
  185. {
  186. struct xfs_scrub *sc = rd->sc;
  187. xfs_ino_t ino;
  188. int error;
  189. error = xfs_dir_lookup(sc->tp, sc->ip, &xfs_name_dotdot, &ino, NULL);
  190. if (error)
  191. return NULLFSINO;
  192. if (!xfs_verify_dir_ino(sc->mp, ino))
  193. return NULLFSINO;
  194. error = xrep_findparent_confirm(sc, &ino);
  195. if (error)
  196. return NULLFSINO;
  197. return ino;
  198. }
  199. /*
  200. * Look up '..' in the dentry cache and confirm that it's really the parent.
  201. * Returns NULLFSINO if the dcache misses or if the hit is implausible.
  202. */
  203. static inline xfs_ino_t
  204. xrep_dir_dcache_parent(
  205. struct xrep_dir *rd)
  206. {
  207. struct xfs_scrub *sc = rd->sc;
  208. xfs_ino_t parent_ino;
  209. int error;
  210. parent_ino = xrep_findparent_from_dcache(sc);
  211. if (parent_ino == NULLFSINO)
  212. return parent_ino;
  213. error = xrep_findparent_confirm(sc, &parent_ino);
  214. if (error)
  215. return NULLFSINO;
  216. return parent_ino;
  217. }
  218. /* Try to find the parent of the directory being repaired. */
  219. STATIC int
  220. xrep_dir_find_parent(
  221. struct xrep_dir *rd)
  222. {
  223. xfs_ino_t ino;
  224. ino = xrep_findparent_self_reference(rd->sc);
  225. if (ino != NULLFSINO) {
  226. xrep_findparent_scan_finish_early(&rd->pscan, ino);
  227. return 0;
  228. }
  229. ino = xrep_dir_dcache_parent(rd);
  230. if (ino != NULLFSINO) {
  231. xrep_findparent_scan_finish_early(&rd->pscan, ino);
  232. return 0;
  233. }
  234. ino = xrep_dir_lookup_parent(rd);
  235. if (ino != NULLFSINO) {
  236. xrep_findparent_scan_finish_early(&rd->pscan, ino);
  237. return 0;
  238. }
  239. /*
  240. * A full filesystem scan is the last resort. On a busy filesystem,
  241. * the scan can fail with -EBUSY if we cannot grab IOLOCKs. That means
  242. * that we don't know what who the parent is, so we should return to
  243. * userspace.
  244. */
  245. return xrep_findparent_scan(&rd->pscan);
  246. }
  247. /*
  248. * Decide if we want to salvage this entry. We don't bother with oversized
  249. * names or the dot entry.
  250. */
  251. STATIC int
  252. xrep_dir_want_salvage(
  253. struct xrep_dir *rd,
  254. const char *name,
  255. int namelen,
  256. xfs_ino_t ino)
  257. {
  258. struct xfs_mount *mp = rd->sc->mp;
  259. /* No pointers to ourselves or to garbage. */
  260. if (ino == rd->sc->ip->i_ino)
  261. return false;
  262. if (!xfs_verify_dir_ino(mp, ino))
  263. return false;
  264. /* No weird looking names or dot entries. */
  265. if (namelen >= MAXNAMELEN || namelen <= 0)
  266. return false;
  267. if (namelen == 1 && name[0] == '.')
  268. return false;
  269. if (!xfs_dir2_namecheck(name, namelen))
  270. return false;
  271. return true;
  272. }
  273. /*
  274. * Remember that we want to create a dirent in the tempdir. These stashed
  275. * actions will be replayed later.
  276. */
  277. STATIC int
  278. xrep_dir_stash_createname(
  279. struct xrep_dir *rd,
  280. const struct xfs_name *name,
  281. xfs_ino_t ino)
  282. {
  283. struct xrep_dirent dirent = {
  284. .action = XREP_DIRENT_ADD,
  285. .ino = ino,
  286. .namelen = name->len,
  287. .ftype = name->type,
  288. };
  289. int error;
  290. trace_xrep_dir_stash_createname(rd->sc->tempip, name, ino);
  291. error = xfblob_storename(rd->dir_names, &dirent.name_cookie, name);
  292. if (error)
  293. return error;
  294. return xfarray_append(rd->dir_entries, &dirent);
  295. }
  296. /*
  297. * Remember that we want to remove a dirent from the tempdir. These stashed
  298. * actions will be replayed later.
  299. */
  300. STATIC int
  301. xrep_dir_stash_removename(
  302. struct xrep_dir *rd,
  303. const struct xfs_name *name,
  304. xfs_ino_t ino)
  305. {
  306. struct xrep_dirent dirent = {
  307. .action = XREP_DIRENT_REMOVE,
  308. .ino = ino,
  309. .namelen = name->len,
  310. .ftype = name->type,
  311. };
  312. int error;
  313. trace_xrep_dir_stash_removename(rd->sc->tempip, name, ino);
  314. error = xfblob_storename(rd->dir_names, &dirent.name_cookie, name);
  315. if (error)
  316. return error;
  317. return xfarray_append(rd->dir_entries, &dirent);
  318. }
  319. /* Allocate an in-core record to hold entries while we rebuild the dir data. */
  320. STATIC int
  321. xrep_dir_salvage_entry(
  322. struct xrep_dir *rd,
  323. unsigned char *name,
  324. unsigned int namelen,
  325. xfs_ino_t ino)
  326. {
  327. struct xfs_name xname = {
  328. .name = name,
  329. };
  330. struct xfs_scrub *sc = rd->sc;
  331. struct xfs_inode *ip;
  332. unsigned int i = 0;
  333. int error = 0;
  334. if (xchk_should_terminate(sc, &error))
  335. return error;
  336. /*
  337. * Truncate the name to the first character that would trip namecheck.
  338. * If we no longer have a name after that, ignore this entry.
  339. */
  340. while (i < namelen && name[i] != 0 && name[i] != '/')
  341. i++;
  342. if (i == 0)
  343. return 0;
  344. xname.len = i;
  345. /* Ignore '..' entries; we already picked the new parent. */
  346. if (xname.len == 2 && name[0] == '.' && name[1] == '.') {
  347. trace_xrep_dir_salvaged_parent(sc->ip, ino);
  348. return 0;
  349. }
  350. trace_xrep_dir_salvage_entry(sc->ip, &xname, ino);
  351. /*
  352. * Compute the ftype or dump the entry if we can't. We don't lock the
  353. * inode because inodes can't change type while we have a reference.
  354. */
  355. error = xchk_iget(sc, ino, &ip);
  356. if (error)
  357. return 0;
  358. xname.type = xfs_mode_to_ftype(VFS_I(ip)->i_mode);
  359. xchk_irele(sc, ip);
  360. return xrep_dir_stash_createname(rd, &xname, ino);
  361. }
  362. /* Record a shortform directory entry for later reinsertion. */
  363. STATIC int
  364. xrep_dir_salvage_sf_entry(
  365. struct xrep_dir *rd,
  366. struct xfs_dir2_sf_hdr *sfp,
  367. struct xfs_dir2_sf_entry *sfep)
  368. {
  369. xfs_ino_t ino;
  370. ino = xfs_dir2_sf_get_ino(rd->sc->mp, sfp, sfep);
  371. if (!xrep_dir_want_salvage(rd, sfep->name, sfep->namelen, ino))
  372. return 0;
  373. return xrep_dir_salvage_entry(rd, sfep->name, sfep->namelen, ino);
  374. }
  375. /* Record a regular directory entry for later reinsertion. */
  376. STATIC int
  377. xrep_dir_salvage_data_entry(
  378. struct xrep_dir *rd,
  379. struct xfs_dir2_data_entry *dep)
  380. {
  381. xfs_ino_t ino;
  382. ino = be64_to_cpu(dep->inumber);
  383. if (!xrep_dir_want_salvage(rd, dep->name, dep->namelen, ino))
  384. return 0;
  385. return xrep_dir_salvage_entry(rd, dep->name, dep->namelen, ino);
  386. }
  387. /* Try to recover block/data format directory entries. */
  388. STATIC int
  389. xrep_dir_recover_data(
  390. struct xrep_dir *rd,
  391. struct xfs_buf *bp)
  392. {
  393. struct xfs_da_geometry *geo = rd->sc->mp->m_dir_geo;
  394. unsigned int offset;
  395. unsigned int end;
  396. int error = 0;
  397. /*
  398. * Loop over the data portion of the block.
  399. * Each object is a real entry (dep) or an unused one (dup).
  400. */
  401. offset = geo->data_entry_offset;
  402. end = min_t(unsigned int, BBTOB(bp->b_length),
  403. xfs_dir3_data_end_offset(geo, bp->b_addr));
  404. while (offset < end) {
  405. struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
  406. struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
  407. if (xchk_should_terminate(rd->sc, &error))
  408. return error;
  409. /* Skip unused entries. */
  410. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  411. offset += be16_to_cpu(dup->length);
  412. continue;
  413. }
  414. /* Don't walk off the end of the block. */
  415. offset += xfs_dir2_data_entsize(rd->sc->mp, dep->namelen);
  416. if (offset > end)
  417. break;
  418. /* Ok, let's save this entry. */
  419. error = xrep_dir_salvage_data_entry(rd, dep);
  420. if (error)
  421. return error;
  422. }
  423. return 0;
  424. }
  425. /* Try to recover shortform directory entries. */
  426. STATIC int
  427. xrep_dir_recover_sf(
  428. struct xrep_dir *rd)
  429. {
  430. struct xfs_dir2_sf_hdr *hdr;
  431. struct xfs_dir2_sf_entry *sfep;
  432. struct xfs_dir2_sf_entry *next;
  433. struct xfs_ifork *ifp;
  434. xfs_ino_t ino;
  435. unsigned char *end;
  436. int error = 0;
  437. ifp = xfs_ifork_ptr(rd->sc->ip, XFS_DATA_FORK);
  438. hdr = ifp->if_data;
  439. end = (unsigned char *)ifp->if_data + ifp->if_bytes;
  440. ino = xfs_dir2_sf_get_parent_ino(hdr);
  441. trace_xrep_dir_salvaged_parent(rd->sc->ip, ino);
  442. sfep = xfs_dir2_sf_firstentry(hdr);
  443. while ((unsigned char *)sfep < end) {
  444. if (xchk_should_terminate(rd->sc, &error))
  445. return error;
  446. next = xfs_dir2_sf_nextentry(rd->sc->mp, hdr, sfep);
  447. if ((unsigned char *)next > end)
  448. break;
  449. /* Ok, let's save this entry. */
  450. error = xrep_dir_salvage_sf_entry(rd, hdr, sfep);
  451. if (error)
  452. return error;
  453. sfep = next;
  454. }
  455. return 0;
  456. }
  457. /*
  458. * Try to figure out the format of this directory from the data fork mappings
  459. * and the directory size. If we can be reasonably sure of format, we can be
  460. * more aggressive in salvaging directory entries. On return, @magic_guess
  461. * will be set to DIR3_BLOCK_MAGIC if we think this is a "block format"
  462. * directory; DIR3_DATA_MAGIC if we think this is a "data format" directory,
  463. * and 0 if we can't tell.
  464. */
  465. STATIC void
  466. xrep_dir_guess_format(
  467. struct xrep_dir *rd,
  468. __be32 *magic_guess)
  469. {
  470. struct xfs_inode *dp = rd->sc->ip;
  471. struct xfs_mount *mp = rd->sc->mp;
  472. struct xfs_da_geometry *geo = mp->m_dir_geo;
  473. xfs_fileoff_t last;
  474. int error;
  475. ASSERT(xfs_has_crc(mp));
  476. *magic_guess = 0;
  477. /*
  478. * If there's a single directory block and the directory size is
  479. * exactly one block, this has to be a single block format directory.
  480. */
  481. error = xfs_bmap_last_offset(dp, &last, XFS_DATA_FORK);
  482. if (!error && XFS_FSB_TO_B(mp, last) == geo->blksize &&
  483. dp->i_disk_size == geo->blksize) {
  484. *magic_guess = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
  485. return;
  486. }
  487. /*
  488. * If the last extent before the leaf offset matches the directory
  489. * size and the directory size is larger than 1 block, this is a
  490. * data format directory.
  491. */
  492. last = geo->leafblk;
  493. error = xfs_bmap_last_before(rd->sc->tp, dp, &last, XFS_DATA_FORK);
  494. if (!error &&
  495. XFS_FSB_TO_B(mp, last) > geo->blksize &&
  496. XFS_FSB_TO_B(mp, last) == dp->i_disk_size) {
  497. *magic_guess = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
  498. return;
  499. }
  500. }
  501. /* Recover directory entries from a specific directory block. */
  502. STATIC int
  503. xrep_dir_recover_dirblock(
  504. struct xrep_dir *rd,
  505. __be32 magic_guess,
  506. xfs_dablk_t dabno)
  507. {
  508. struct xfs_dir2_data_hdr *hdr;
  509. struct xfs_buf *bp;
  510. __be32 oldmagic;
  511. int error;
  512. /*
  513. * Try to read buffer. We invalidate them in the next step so we don't
  514. * bother to set a buffer type or ops.
  515. */
  516. error = xfs_da_read_buf(rd->sc->tp, rd->sc->ip, dabno,
  517. XFS_DABUF_MAP_HOLE_OK, &bp, XFS_DATA_FORK, NULL);
  518. if (error || !bp)
  519. return error;
  520. hdr = bp->b_addr;
  521. oldmagic = hdr->magic;
  522. trace_xrep_dir_recover_dirblock(rd->sc->ip, dabno,
  523. be32_to_cpu(hdr->magic), be32_to_cpu(magic_guess));
  524. /*
  525. * If we're sure of the block's format, proceed with the salvage
  526. * operation using the specified magic number.
  527. */
  528. if (magic_guess) {
  529. hdr->magic = magic_guess;
  530. goto recover;
  531. }
  532. /*
  533. * If we couldn't guess what type of directory this is, then we will
  534. * only salvage entries from directory blocks that match the magic
  535. * number and pass verifiers.
  536. */
  537. switch (hdr->magic) {
  538. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  539. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  540. if (!xrep_buf_verify_struct(bp, &xfs_dir3_block_buf_ops))
  541. goto out;
  542. if (xfs_dir3_block_header_check(bp, rd->sc->ip->i_ino) != NULL)
  543. goto out;
  544. break;
  545. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  546. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  547. if (!xrep_buf_verify_struct(bp, &xfs_dir3_data_buf_ops))
  548. goto out;
  549. if (xfs_dir3_data_header_check(bp, rd->sc->ip->i_ino) != NULL)
  550. goto out;
  551. break;
  552. default:
  553. goto out;
  554. }
  555. recover:
  556. error = xrep_dir_recover_data(rd, bp);
  557. out:
  558. hdr->magic = oldmagic;
  559. xfs_trans_brelse(rd->sc->tp, bp);
  560. return error;
  561. }
  562. static inline void
  563. xrep_dir_init_args(
  564. struct xrep_dir *rd,
  565. struct xfs_inode *dp,
  566. const struct xfs_name *name)
  567. {
  568. memset(&rd->args, 0, sizeof(struct xfs_da_args));
  569. rd->args.geo = rd->sc->mp->m_dir_geo;
  570. rd->args.whichfork = XFS_DATA_FORK;
  571. rd->args.owner = rd->sc->ip->i_ino;
  572. rd->args.trans = rd->sc->tp;
  573. rd->args.dp = dp;
  574. if (!name)
  575. return;
  576. rd->args.name = name->name;
  577. rd->args.namelen = name->len;
  578. rd->args.filetype = name->type;
  579. rd->args.hashval = xfs_dir2_hashname(rd->sc->mp, name);
  580. }
  581. /* Replay a stashed createname into the temporary directory. */
  582. STATIC int
  583. xrep_dir_replay_createname(
  584. struct xrep_dir *rd,
  585. const struct xfs_name *name,
  586. xfs_ino_t inum,
  587. xfs_extlen_t total)
  588. {
  589. struct xfs_scrub *sc = rd->sc;
  590. struct xfs_inode *dp = rd->sc->tempip;
  591. int error;
  592. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  593. error = xfs_dir_ino_validate(sc->mp, inum);
  594. if (error)
  595. return error;
  596. trace_xrep_dir_replay_createname(dp, name, inum);
  597. xrep_dir_init_args(rd, dp, name);
  598. rd->args.inumber = inum;
  599. rd->args.total = total;
  600. rd->args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  601. return xfs_dir_createname_args(&rd->args);
  602. }
  603. /* Replay a stashed removename onto the temporary directory. */
  604. STATIC int
  605. xrep_dir_replay_removename(
  606. struct xrep_dir *rd,
  607. const struct xfs_name *name,
  608. xfs_extlen_t total)
  609. {
  610. struct xfs_inode *dp = rd->args.dp;
  611. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  612. xrep_dir_init_args(rd, dp, name);
  613. rd->args.op_flags = 0;
  614. rd->args.total = total;
  615. trace_xrep_dir_replay_removename(dp, name, 0);
  616. return xfs_dir_removename_args(&rd->args);
  617. }
  618. /*
  619. * Add this stashed incore directory entry to the temporary directory.
  620. * The caller must hold the tempdir's IOLOCK, must not hold any ILOCKs, and
  621. * must not be in transaction context.
  622. */
  623. STATIC int
  624. xrep_dir_replay_update(
  625. struct xrep_dir *rd,
  626. const struct xfs_name *xname,
  627. const struct xrep_dirent *dirent)
  628. {
  629. struct xfs_mount *mp = rd->sc->mp;
  630. #ifdef DEBUG
  631. xfs_ino_t ino;
  632. #endif
  633. uint resblks;
  634. int error;
  635. resblks = xfs_link_space_res(mp, xname->len);
  636. error = xchk_trans_alloc(rd->sc, resblks);
  637. if (error)
  638. return error;
  639. /* Lock the temporary directory and join it to the transaction */
  640. xrep_tempfile_ilock(rd->sc);
  641. xfs_trans_ijoin(rd->sc->tp, rd->sc->tempip, 0);
  642. switch (dirent->action) {
  643. case XREP_DIRENT_ADD:
  644. /*
  645. * Create a replacement dirent in the temporary directory.
  646. * Note that _createname doesn't check for existing entries.
  647. * There shouldn't be any in the temporary dir, but we'll
  648. * verify this in debug mode.
  649. */
  650. #ifdef DEBUG
  651. error = xchk_dir_lookup(rd->sc, rd->sc->tempip, xname, &ino);
  652. if (error != -ENOENT) {
  653. ASSERT(error != -ENOENT);
  654. goto out_cancel;
  655. }
  656. #endif
  657. error = xrep_dir_replay_createname(rd, xname, dirent->ino,
  658. resblks);
  659. if (error)
  660. goto out_cancel;
  661. if (xname->type == XFS_DIR3_FT_DIR)
  662. rd->subdirs++;
  663. rd->dirents++;
  664. break;
  665. case XREP_DIRENT_REMOVE:
  666. /*
  667. * Remove a dirent from the temporary directory. Note that
  668. * _removename doesn't check the inode target of the exist
  669. * entry. There should be a perfect match in the temporary
  670. * dir, but we'll verify this in debug mode.
  671. */
  672. #ifdef DEBUG
  673. error = xchk_dir_lookup(rd->sc, rd->sc->tempip, xname, &ino);
  674. if (error) {
  675. ASSERT(error != 0);
  676. goto out_cancel;
  677. }
  678. if (ino != dirent->ino) {
  679. ASSERT(ino == dirent->ino);
  680. error = -EIO;
  681. goto out_cancel;
  682. }
  683. #endif
  684. error = xrep_dir_replay_removename(rd, xname, resblks);
  685. if (error)
  686. goto out_cancel;
  687. if (xname->type == XFS_DIR3_FT_DIR)
  688. rd->subdirs--;
  689. rd->dirents--;
  690. break;
  691. default:
  692. ASSERT(0);
  693. error = -EIO;
  694. goto out_cancel;
  695. }
  696. /* Commit and unlock. */
  697. error = xrep_trans_commit(rd->sc);
  698. if (error)
  699. return error;
  700. xrep_tempfile_iunlock(rd->sc);
  701. return 0;
  702. out_cancel:
  703. xchk_trans_cancel(rd->sc);
  704. xrep_tempfile_iunlock(rd->sc);
  705. return error;
  706. }
  707. /*
  708. * Flush stashed incore dirent updates that have been recorded by the scanner.
  709. * This is done to reduce the memory requirements of the directory rebuild,
  710. * since directories can contain up to 32GB of directory data.
  711. *
  712. * Caller must not hold transactions or ILOCKs. Caller must hold the tempdir
  713. * IOLOCK.
  714. */
  715. STATIC int
  716. xrep_dir_replay_updates(
  717. struct xrep_dir *rd)
  718. {
  719. xfarray_idx_t array_cur;
  720. int error;
  721. /* Add all the salvaged dirents to the temporary directory. */
  722. mutex_lock(&rd->pscan.lock);
  723. foreach_xfarray_idx(rd->dir_entries, array_cur) {
  724. struct xrep_dirent dirent;
  725. error = xfarray_load(rd->dir_entries, array_cur, &dirent);
  726. if (error)
  727. goto out_unlock;
  728. error = xfblob_loadname(rd->dir_names, dirent.name_cookie,
  729. &rd->xname, dirent.namelen);
  730. if (error)
  731. goto out_unlock;
  732. rd->xname.type = dirent.ftype;
  733. mutex_unlock(&rd->pscan.lock);
  734. error = xrep_dir_replay_update(rd, &rd->xname, &dirent);
  735. if (error)
  736. return error;
  737. mutex_lock(&rd->pscan.lock);
  738. }
  739. /* Empty out both arrays now that we've added the entries. */
  740. xfarray_truncate(rd->dir_entries);
  741. xfblob_truncate(rd->dir_names);
  742. mutex_unlock(&rd->pscan.lock);
  743. return 0;
  744. out_unlock:
  745. mutex_unlock(&rd->pscan.lock);
  746. return error;
  747. }
  748. /*
  749. * Periodically flush stashed directory entries to the temporary dir. This
  750. * is done to reduce the memory requirements of the directory rebuild, since
  751. * directories can contain up to 32GB of directory data.
  752. */
  753. STATIC int
  754. xrep_dir_flush_stashed(
  755. struct xrep_dir *rd)
  756. {
  757. int error;
  758. /*
  759. * Entering this function, the scrub context has a reference to the
  760. * inode being repaired, the temporary file, and a scrub transaction
  761. * that we use during dirent salvaging to avoid livelocking if there
  762. * are cycles in the directory structures. We hold ILOCK_EXCL on both
  763. * the inode being repaired and the temporary file, though they are
  764. * not ijoined to the scrub transaction.
  765. *
  766. * To constrain kernel memory use, we occasionally write salvaged
  767. * dirents from the xfarray and xfblob structures into the temporary
  768. * directory in preparation for exchanging the directory structures at
  769. * the end. Updating the temporary file requires a transaction, so we
  770. * commit the scrub transaction and drop the two ILOCKs so that
  771. * we can allocate whatever transaction we want.
  772. *
  773. * We still hold IOLOCK_EXCL on the inode being repaired, which
  774. * prevents anyone from accessing the damaged directory data while we
  775. * repair it.
  776. */
  777. error = xrep_trans_commit(rd->sc);
  778. if (error)
  779. return error;
  780. xchk_iunlock(rd->sc, XFS_ILOCK_EXCL);
  781. /*
  782. * Take the IOLOCK of the temporary file while we modify dirents. This
  783. * isn't strictly required because the temporary file is never revealed
  784. * to userspace, but we follow the same locking rules. We still hold
  785. * sc->ip's IOLOCK.
  786. */
  787. error = xrep_tempfile_iolock_polled(rd->sc);
  788. if (error)
  789. return error;
  790. /* Write to the tempdir all the updates that we've stashed. */
  791. error = xrep_dir_replay_updates(rd);
  792. xrep_tempfile_iounlock(rd->sc);
  793. if (error)
  794. return error;
  795. /*
  796. * Recreate the salvage transaction and relock the dir we're salvaging.
  797. */
  798. error = xchk_trans_alloc(rd->sc, 0);
  799. if (error)
  800. return error;
  801. xchk_ilock(rd->sc, XFS_ILOCK_EXCL);
  802. return 0;
  803. }
  804. /* Decide if we've stashed too much dirent data in memory. */
  805. static inline bool
  806. xrep_dir_want_flush_stashed(
  807. struct xrep_dir *rd)
  808. {
  809. unsigned long long bytes;
  810. bytes = xfarray_bytes(rd->dir_entries) + xfblob_bytes(rd->dir_names);
  811. return bytes > XREP_DIR_MAX_STASH_BYTES;
  812. }
  813. /* Extract as many directory entries as we can. */
  814. STATIC int
  815. xrep_dir_recover(
  816. struct xrep_dir *rd)
  817. {
  818. struct xfs_bmbt_irec got;
  819. struct xfs_scrub *sc = rd->sc;
  820. struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
  821. xfs_fileoff_t offset;
  822. xfs_dablk_t dabno;
  823. __be32 magic_guess;
  824. int nmap;
  825. int error;
  826. xrep_dir_guess_format(rd, &magic_guess);
  827. /* Iterate each directory data block in the data fork. */
  828. for (offset = 0;
  829. offset < geo->leafblk;
  830. offset = got.br_startoff + got.br_blockcount) {
  831. nmap = 1;
  832. error = xfs_bmapi_read(sc->ip, offset, geo->leafblk - offset,
  833. &got, &nmap, 0);
  834. if (error)
  835. return error;
  836. if (nmap != 1)
  837. return -EFSCORRUPTED;
  838. if (!xfs_bmap_is_written_extent(&got))
  839. continue;
  840. for (dabno = round_up(got.br_startoff, geo->fsbcount);
  841. dabno < got.br_startoff + got.br_blockcount;
  842. dabno += geo->fsbcount) {
  843. if (xchk_should_terminate(rd->sc, &error))
  844. return error;
  845. error = xrep_dir_recover_dirblock(rd,
  846. magic_guess, dabno);
  847. if (error)
  848. return error;
  849. /* Flush dirents to constrain memory usage. */
  850. if (xrep_dir_want_flush_stashed(rd)) {
  851. error = xrep_dir_flush_stashed(rd);
  852. if (error)
  853. return error;
  854. }
  855. }
  856. }
  857. return 0;
  858. }
  859. /*
  860. * Find all the directory entries for this inode by scraping them out of the
  861. * directory leaf blocks by hand, and flushing them into the temp dir.
  862. */
  863. STATIC int
  864. xrep_dir_find_entries(
  865. struct xrep_dir *rd)
  866. {
  867. struct xfs_inode *dp = rd->sc->ip;
  868. int error;
  869. /*
  870. * Salvage directory entries from the old directory, and write them to
  871. * the temporary directory.
  872. */
  873. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  874. error = xrep_dir_recover_sf(rd);
  875. } else {
  876. error = xfs_iread_extents(rd->sc->tp, dp, XFS_DATA_FORK);
  877. if (error)
  878. return error;
  879. error = xrep_dir_recover(rd);
  880. }
  881. if (error)
  882. return error;
  883. return xrep_dir_flush_stashed(rd);
  884. }
  885. /* Scan all files in the filesystem for dirents. */
  886. STATIC int
  887. xrep_dir_salvage_entries(
  888. struct xrep_dir *rd)
  889. {
  890. struct xfs_scrub *sc = rd->sc;
  891. int error;
  892. /*
  893. * Drop the ILOCK on this directory so that we can scan for this
  894. * directory's parent. Figure out who is going to be the parent of
  895. * this directory, then retake the ILOCK so that we can salvage
  896. * directory entries.
  897. */
  898. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  899. error = xrep_dir_find_parent(rd);
  900. xchk_ilock(sc, XFS_ILOCK_EXCL);
  901. if (error)
  902. return error;
  903. /*
  904. * Collect directory entries by parsing raw leaf blocks to salvage
  905. * whatever we can. When we're done, free the staging memory before
  906. * exchanging the directories to reduce memory usage.
  907. */
  908. error = xrep_dir_find_entries(rd);
  909. if (error)
  910. return error;
  911. /*
  912. * Cancel the repair transaction and drop the ILOCK so that we can
  913. * (later) use the atomic mapping exchange functions to compute the
  914. * correct block reservations and re-lock the inodes.
  915. *
  916. * We still hold IOLOCK_EXCL (aka i_rwsem) which will prevent directory
  917. * modifications, but there's nothing to prevent userspace from reading
  918. * the directory until we're ready for the exchange operation. Reads
  919. * will return -EIO without shutting down the fs, so we're ok with
  920. * that.
  921. *
  922. * The VFS can change dotdot on us, but the findparent scan will keep
  923. * our incore parent inode up to date. See the note on locking issues
  924. * for more details.
  925. */
  926. error = xrep_trans_commit(sc);
  927. if (error)
  928. return error;
  929. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  930. return 0;
  931. }
  932. /*
  933. * Examine a parent pointer of a file. If it leads us back to the directory
  934. * that we're rebuilding, create an incore dirent from the parent pointer and
  935. * stash it.
  936. */
  937. STATIC int
  938. xrep_dir_scan_pptr(
  939. struct xfs_scrub *sc,
  940. struct xfs_inode *ip,
  941. unsigned int attr_flags,
  942. const unsigned char *name,
  943. unsigned int namelen,
  944. const void *value,
  945. unsigned int valuelen,
  946. void *priv)
  947. {
  948. struct xfs_name xname = {
  949. .name = name,
  950. .len = namelen,
  951. .type = xfs_mode_to_ftype(VFS_I(ip)->i_mode),
  952. };
  953. xfs_ino_t parent_ino;
  954. uint32_t parent_gen;
  955. struct xrep_dir *rd = priv;
  956. int error;
  957. if (!(attr_flags & XFS_ATTR_PARENT))
  958. return 0;
  959. /*
  960. * Ignore parent pointers that point back to a different dir, list the
  961. * wrong generation number, or are invalid.
  962. */
  963. error = xfs_parent_from_attr(sc->mp, attr_flags, name, namelen, value,
  964. valuelen, &parent_ino, &parent_gen);
  965. if (error)
  966. return error;
  967. if (parent_ino != sc->ip->i_ino ||
  968. parent_gen != VFS_I(sc->ip)->i_generation)
  969. return 0;
  970. mutex_lock(&rd->pscan.lock);
  971. error = xrep_dir_stash_createname(rd, &xname, ip->i_ino);
  972. mutex_unlock(&rd->pscan.lock);
  973. return error;
  974. }
  975. /*
  976. * If this child dirent points to the directory being repaired, remember that
  977. * fact so that we can reset the dotdot entry if necessary.
  978. */
  979. STATIC int
  980. xrep_dir_scan_dirent(
  981. struct xfs_scrub *sc,
  982. struct xfs_inode *dp,
  983. xfs_dir2_dataptr_t dapos,
  984. const struct xfs_name *name,
  985. xfs_ino_t ino,
  986. void *priv)
  987. {
  988. struct xrep_dir *rd = priv;
  989. /* Dirent doesn't point to this directory. */
  990. if (ino != rd->sc->ip->i_ino)
  991. return 0;
  992. /* Ignore garbage inum. */
  993. if (!xfs_verify_dir_ino(rd->sc->mp, ino))
  994. return 0;
  995. /* No weird looking names. */
  996. if (name->len >= MAXNAMELEN || name->len <= 0)
  997. return 0;
  998. /* Don't pick up dot or dotdot entries; we only want child dirents. */
  999. if (xfs_dir2_samename(name, &xfs_name_dotdot) ||
  1000. xfs_dir2_samename(name, &xfs_name_dot))
  1001. return 0;
  1002. trace_xrep_dir_stash_createname(sc->tempip, &xfs_name_dotdot,
  1003. dp->i_ino);
  1004. xrep_findparent_scan_found(&rd->pscan, dp->i_ino);
  1005. return 0;
  1006. }
  1007. /*
  1008. * Decide if we want to look for child dirents or parent pointers in this file.
  1009. * Skip the dir being repaired and any files being used to stage repairs.
  1010. */
  1011. static inline bool
  1012. xrep_dir_want_scan(
  1013. struct xrep_dir *rd,
  1014. const struct xfs_inode *ip)
  1015. {
  1016. return ip != rd->sc->ip && !xrep_is_tempfile(ip);
  1017. }
  1018. /*
  1019. * Take ILOCK on a file that we want to scan.
  1020. *
  1021. * Select ILOCK_EXCL if the file is a directory with an unloaded data bmbt or
  1022. * has an unloaded attr bmbt. Otherwise, take ILOCK_SHARED.
  1023. */
  1024. static inline unsigned int
  1025. xrep_dir_scan_ilock(
  1026. struct xrep_dir *rd,
  1027. struct xfs_inode *ip)
  1028. {
  1029. uint lock_mode = XFS_ILOCK_SHARED;
  1030. /* Need to take the shared ILOCK to advance the iscan cursor. */
  1031. if (!xrep_dir_want_scan(rd, ip))
  1032. goto lock;
  1033. if (S_ISDIR(VFS_I(ip)->i_mode) && xfs_need_iread_extents(&ip->i_df)) {
  1034. lock_mode = XFS_ILOCK_EXCL;
  1035. goto lock;
  1036. }
  1037. if (xfs_inode_has_attr_fork(ip) && xfs_need_iread_extents(&ip->i_af))
  1038. lock_mode = XFS_ILOCK_EXCL;
  1039. lock:
  1040. xfs_ilock(ip, lock_mode);
  1041. return lock_mode;
  1042. }
  1043. /*
  1044. * Scan this file for relevant child dirents or parent pointers that point to
  1045. * the directory we're rebuilding.
  1046. */
  1047. STATIC int
  1048. xrep_dir_scan_file(
  1049. struct xrep_dir *rd,
  1050. struct xfs_inode *ip)
  1051. {
  1052. unsigned int lock_mode;
  1053. int error = 0;
  1054. lock_mode = xrep_dir_scan_ilock(rd, ip);
  1055. if (!xrep_dir_want_scan(rd, ip))
  1056. goto scan_done;
  1057. /*
  1058. * If the extended attributes look as though they has been zapped by
  1059. * the inode record repair code, we cannot scan for parent pointers.
  1060. */
  1061. if (xchk_pptr_looks_zapped(ip)) {
  1062. error = -EBUSY;
  1063. goto scan_done;
  1064. }
  1065. error = xchk_xattr_walk(rd->sc, ip, xrep_dir_scan_pptr, NULL, rd);
  1066. if (error)
  1067. goto scan_done;
  1068. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  1069. /*
  1070. * If the directory looks as though it has been zapped by the
  1071. * inode record repair code, we cannot scan for child dirents.
  1072. */
  1073. if (xchk_dir_looks_zapped(ip)) {
  1074. error = -EBUSY;
  1075. goto scan_done;
  1076. }
  1077. error = xchk_dir_walk(rd->sc, ip, xrep_dir_scan_dirent, rd);
  1078. if (error)
  1079. goto scan_done;
  1080. }
  1081. scan_done:
  1082. xchk_iscan_mark_visited(&rd->pscan.iscan, ip);
  1083. xfs_iunlock(ip, lock_mode);
  1084. return error;
  1085. }
  1086. /*
  1087. * Scan all files in the filesystem for parent pointers that we can turn into
  1088. * replacement dirents, and a dirent that we can use to set the dotdot pointer.
  1089. */
  1090. STATIC int
  1091. xrep_dir_scan_dirtree(
  1092. struct xrep_dir *rd)
  1093. {
  1094. struct xfs_scrub *sc = rd->sc;
  1095. struct xfs_inode *ip;
  1096. int error;
  1097. /* Roots of directory trees are their own parents. */
  1098. if (sc->ip == sc->mp->m_rootip)
  1099. xrep_findparent_scan_found(&rd->pscan, sc->ip->i_ino);
  1100. /*
  1101. * Filesystem scans are time consuming. Drop the directory ILOCK and
  1102. * all other resources for the duration of the scan and hope for the
  1103. * best. The live update hooks will keep our scan information up to
  1104. * date even though we've dropped the locks.
  1105. */
  1106. xchk_trans_cancel(sc);
  1107. if (sc->ilock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL))
  1108. xchk_iunlock(sc, sc->ilock_flags & (XFS_ILOCK_SHARED |
  1109. XFS_ILOCK_EXCL));
  1110. error = xchk_trans_alloc_empty(sc);
  1111. if (error)
  1112. return error;
  1113. while ((error = xchk_iscan_iter(&rd->pscan.iscan, &ip)) == 1) {
  1114. bool flush;
  1115. error = xrep_dir_scan_file(rd, ip);
  1116. xchk_irele(sc, ip);
  1117. if (error)
  1118. break;
  1119. /* Flush stashed dirent updates to constrain memory usage. */
  1120. mutex_lock(&rd->pscan.lock);
  1121. flush = xrep_dir_want_flush_stashed(rd);
  1122. mutex_unlock(&rd->pscan.lock);
  1123. if (flush) {
  1124. xchk_trans_cancel(sc);
  1125. error = xrep_tempfile_iolock_polled(sc);
  1126. if (error)
  1127. break;
  1128. error = xrep_dir_replay_updates(rd);
  1129. xrep_tempfile_iounlock(sc);
  1130. if (error)
  1131. break;
  1132. error = xchk_trans_alloc_empty(sc);
  1133. if (error)
  1134. break;
  1135. }
  1136. if (xchk_should_terminate(sc, &error))
  1137. break;
  1138. }
  1139. xchk_iscan_iter_finish(&rd->pscan.iscan);
  1140. if (error) {
  1141. /*
  1142. * If we couldn't grab an inode that was busy with a state
  1143. * change, change the error code so that we exit to userspace
  1144. * as quickly as possible.
  1145. */
  1146. if (error == -EBUSY)
  1147. return -ECANCELED;
  1148. return error;
  1149. }
  1150. /*
  1151. * Cancel the empty transaction so that we can (later) use the atomic
  1152. * file mapping exchange functions to lock files and commit the new
  1153. * directory.
  1154. */
  1155. xchk_trans_cancel(rd->sc);
  1156. return 0;
  1157. }
  1158. /*
  1159. * Capture dirent updates being made by other threads which are relevant to the
  1160. * directory being repaired.
  1161. */
  1162. STATIC int
  1163. xrep_dir_live_update(
  1164. struct notifier_block *nb,
  1165. unsigned long action,
  1166. void *data)
  1167. {
  1168. struct xfs_dir_update_params *p = data;
  1169. struct xrep_dir *rd;
  1170. struct xfs_scrub *sc;
  1171. int error = 0;
  1172. rd = container_of(nb, struct xrep_dir, pscan.dhook.dirent_hook.nb);
  1173. sc = rd->sc;
  1174. /*
  1175. * This thread updated a child dirent in the directory that we're
  1176. * rebuilding. Stash the update for replay against the temporary
  1177. * directory.
  1178. */
  1179. if (p->dp->i_ino == sc->ip->i_ino &&
  1180. xchk_iscan_want_live_update(&rd->pscan.iscan, p->ip->i_ino)) {
  1181. mutex_lock(&rd->pscan.lock);
  1182. if (p->delta > 0)
  1183. error = xrep_dir_stash_createname(rd, p->name,
  1184. p->ip->i_ino);
  1185. else
  1186. error = xrep_dir_stash_removename(rd, p->name,
  1187. p->ip->i_ino);
  1188. mutex_unlock(&rd->pscan.lock);
  1189. if (error)
  1190. goto out_abort;
  1191. }
  1192. /*
  1193. * This thread updated another directory's child dirent that points to
  1194. * the directory that we're rebuilding, so remember the new dotdot
  1195. * target.
  1196. */
  1197. if (p->ip->i_ino == sc->ip->i_ino &&
  1198. xchk_iscan_want_live_update(&rd->pscan.iscan, p->dp->i_ino)) {
  1199. if (p->delta > 0) {
  1200. trace_xrep_dir_stash_createname(sc->tempip,
  1201. &xfs_name_dotdot,
  1202. p->dp->i_ino);
  1203. xrep_findparent_scan_found(&rd->pscan, p->dp->i_ino);
  1204. } else {
  1205. trace_xrep_dir_stash_removename(sc->tempip,
  1206. &xfs_name_dotdot,
  1207. rd->pscan.parent_ino);
  1208. xrep_findparent_scan_found(&rd->pscan, NULLFSINO);
  1209. }
  1210. }
  1211. return NOTIFY_DONE;
  1212. out_abort:
  1213. xchk_iscan_abort(&rd->pscan.iscan);
  1214. return NOTIFY_DONE;
  1215. }
  1216. /*
  1217. * Free all the directory blocks and reset the data fork. The caller must
  1218. * join the inode to the transaction. This function returns with the inode
  1219. * joined to a clean scrub transaction.
  1220. */
  1221. STATIC int
  1222. xrep_dir_reset_fork(
  1223. struct xrep_dir *rd,
  1224. xfs_ino_t parent_ino)
  1225. {
  1226. struct xfs_scrub *sc = rd->sc;
  1227. struct xfs_ifork *ifp = xfs_ifork_ptr(sc->tempip, XFS_DATA_FORK);
  1228. int error;
  1229. /* Unmap all the directory buffers. */
  1230. if (xfs_ifork_has_extents(ifp)) {
  1231. error = xrep_reap_ifork(sc, sc->tempip, XFS_DATA_FORK);
  1232. if (error)
  1233. return error;
  1234. }
  1235. trace_xrep_dir_reset_fork(sc->tempip, parent_ino);
  1236. /* Reset the data fork to an empty data fork. */
  1237. xfs_idestroy_fork(ifp);
  1238. ifp->if_bytes = 0;
  1239. sc->tempip->i_disk_size = 0;
  1240. /* Reinitialize the short form directory. */
  1241. xrep_dir_init_args(rd, sc->tempip, NULL);
  1242. return xfs_dir2_sf_create(&rd->args, parent_ino);
  1243. }
  1244. /*
  1245. * Prepare both inodes' directory forks for exchanging mappings. Promote the
  1246. * tempfile from short format to leaf format, and if the file being repaired
  1247. * has a short format data fork, turn it into an empty extent list.
  1248. */
  1249. STATIC int
  1250. xrep_dir_swap_prep(
  1251. struct xfs_scrub *sc,
  1252. bool temp_local,
  1253. bool ip_local)
  1254. {
  1255. int error;
  1256. /*
  1257. * If the tempfile's directory is in shortform format, convert that to
  1258. * a single leaf extent so that we can use the atomic mapping exchange.
  1259. */
  1260. if (temp_local) {
  1261. struct xfs_da_args args = {
  1262. .dp = sc->tempip,
  1263. .geo = sc->mp->m_dir_geo,
  1264. .whichfork = XFS_DATA_FORK,
  1265. .trans = sc->tp,
  1266. .total = 1,
  1267. .owner = sc->ip->i_ino,
  1268. };
  1269. error = xfs_dir2_sf_to_block(&args);
  1270. if (error)
  1271. return error;
  1272. /*
  1273. * Roll the deferred log items to get us back to a clean
  1274. * transaction.
  1275. */
  1276. error = xfs_defer_finish(&sc->tp);
  1277. if (error)
  1278. return error;
  1279. }
  1280. /*
  1281. * If the file being repaired had a shortform data fork, convert that
  1282. * to an empty extent list in preparation for the atomic mapping
  1283. * exchange.
  1284. */
  1285. if (ip_local) {
  1286. struct xfs_ifork *ifp;
  1287. ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
  1288. xfs_idestroy_fork(ifp);
  1289. ifp->if_format = XFS_DINODE_FMT_EXTENTS;
  1290. ifp->if_nextents = 0;
  1291. ifp->if_bytes = 0;
  1292. ifp->if_data = NULL;
  1293. ifp->if_height = 0;
  1294. xfs_trans_log_inode(sc->tp, sc->ip,
  1295. XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1296. }
  1297. return 0;
  1298. }
  1299. /*
  1300. * Replace the inode number of a directory entry.
  1301. */
  1302. static int
  1303. xrep_dir_replace(
  1304. struct xrep_dir *rd,
  1305. struct xfs_inode *dp,
  1306. const struct xfs_name *name,
  1307. xfs_ino_t inum,
  1308. xfs_extlen_t total)
  1309. {
  1310. struct xfs_scrub *sc = rd->sc;
  1311. int error;
  1312. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  1313. error = xfs_dir_ino_validate(sc->mp, inum);
  1314. if (error)
  1315. return error;
  1316. xrep_dir_init_args(rd, dp, name);
  1317. rd->args.inumber = inum;
  1318. rd->args.total = total;
  1319. return xfs_dir_replace_args(&rd->args);
  1320. }
  1321. /*
  1322. * Reset the link count of this directory and adjust the unlinked list pointers
  1323. * as needed.
  1324. */
  1325. STATIC int
  1326. xrep_dir_set_nlink(
  1327. struct xrep_dir *rd)
  1328. {
  1329. struct xfs_scrub *sc = rd->sc;
  1330. struct xfs_inode *dp = sc->ip;
  1331. struct xfs_perag *pag;
  1332. unsigned int new_nlink = min_t(unsigned long long,
  1333. rd->subdirs + 2,
  1334. XFS_NLINK_PINNED);
  1335. int error;
  1336. /*
  1337. * The directory is not on the incore unlinked list, which means that
  1338. * it needs to be reachable via the directory tree. Update the nlink
  1339. * with our observed link count. If the directory has no parent, it
  1340. * will be moved to the orphanage.
  1341. */
  1342. if (!xfs_inode_on_unlinked_list(dp))
  1343. goto reset_nlink;
  1344. /*
  1345. * The directory is on the unlinked list and we did not find any
  1346. * dirents. Set the link count to zero and let the directory
  1347. * inactivate when the last reference drops.
  1348. */
  1349. if (rd->dirents == 0) {
  1350. rd->needs_adoption = false;
  1351. new_nlink = 0;
  1352. goto reset_nlink;
  1353. }
  1354. /*
  1355. * The directory is on the unlinked list and we found dirents. This
  1356. * directory needs to be reachable via the directory tree. Remove the
  1357. * dir from the unlinked list and update nlink with the observed link
  1358. * count. If the directory has no parent, it will be moved to the
  1359. * orphanage.
  1360. */
  1361. pag = xfs_perag_get(sc->mp, XFS_INO_TO_AGNO(sc->mp, dp->i_ino));
  1362. if (!pag) {
  1363. ASSERT(0);
  1364. return -EFSCORRUPTED;
  1365. }
  1366. error = xfs_iunlink_remove(sc->tp, pag, dp);
  1367. xfs_perag_put(pag);
  1368. if (error)
  1369. return error;
  1370. reset_nlink:
  1371. if (VFS_I(dp)->i_nlink != new_nlink)
  1372. set_nlink(VFS_I(dp), new_nlink);
  1373. return 0;
  1374. }
  1375. /*
  1376. * Finish replaying stashed dirent updates, allocate a transaction for
  1377. * exchanging data fork mappings, and take the ILOCKs of both directories
  1378. * before we commit the new directory structure.
  1379. */
  1380. STATIC int
  1381. xrep_dir_finalize_tempdir(
  1382. struct xrep_dir *rd)
  1383. {
  1384. struct xfs_scrub *sc = rd->sc;
  1385. int error;
  1386. if (!xfs_has_parent(sc->mp))
  1387. return xrep_tempexch_trans_alloc(sc, XFS_DATA_FORK, &rd->tx);
  1388. /*
  1389. * Repair relies on the ILOCK to quiesce all possible dirent updates.
  1390. * Replay all queued dirent updates into the tempdir before exchanging
  1391. * the contents, even if that means dropping the ILOCKs and the
  1392. * transaction.
  1393. */
  1394. do {
  1395. error = xrep_dir_replay_updates(rd);
  1396. if (error)
  1397. return error;
  1398. error = xrep_tempexch_trans_alloc(sc, XFS_DATA_FORK, &rd->tx);
  1399. if (error)
  1400. return error;
  1401. if (xfarray_length(rd->dir_entries) == 0)
  1402. break;
  1403. xchk_trans_cancel(sc);
  1404. xrep_tempfile_iunlock_both(sc);
  1405. } while (!xchk_should_terminate(sc, &error));
  1406. return error;
  1407. }
  1408. /* Exchange the temporary directory's data fork with the one being repaired. */
  1409. STATIC int
  1410. xrep_dir_swap(
  1411. struct xrep_dir *rd)
  1412. {
  1413. struct xfs_scrub *sc = rd->sc;
  1414. bool ip_local, temp_local;
  1415. int error = 0;
  1416. /*
  1417. * If we never found the parent for this directory, temporarily assign
  1418. * the root dir as the parent; we'll move this to the orphanage after
  1419. * exchanging the dir contents. We hold the ILOCK of the dir being
  1420. * repaired, so we're not worried about racy updates of dotdot.
  1421. */
  1422. ASSERT(sc->ilock_flags & XFS_ILOCK_EXCL);
  1423. if (rd->pscan.parent_ino == NULLFSINO) {
  1424. rd->needs_adoption = true;
  1425. rd->pscan.parent_ino = rd->sc->mp->m_sb.sb_rootino;
  1426. }
  1427. /*
  1428. * Reset the temporary directory's '..' entry to point to the parent
  1429. * that we found. The temporary directory was created with the root
  1430. * directory as the parent, so we can skip this if repairing a
  1431. * subdirectory of the root.
  1432. *
  1433. * It's also possible that this replacement could also expand a sf
  1434. * tempdir into block format.
  1435. */
  1436. if (rd->pscan.parent_ino != sc->mp->m_rootip->i_ino) {
  1437. error = xrep_dir_replace(rd, rd->sc->tempip, &xfs_name_dotdot,
  1438. rd->pscan.parent_ino, rd->tx.req.resblks);
  1439. if (error)
  1440. return error;
  1441. }
  1442. /*
  1443. * Changing the dot and dotdot entries could have changed the shape of
  1444. * the directory, so we recompute these.
  1445. */
  1446. ip_local = sc->ip->i_df.if_format == XFS_DINODE_FMT_LOCAL;
  1447. temp_local = sc->tempip->i_df.if_format == XFS_DINODE_FMT_LOCAL;
  1448. /*
  1449. * If the both files have a local format data fork and the rebuilt
  1450. * directory data would fit in the repaired file's data fork, copy
  1451. * the contents from the tempfile and update the directory link count.
  1452. * We're done now.
  1453. */
  1454. if (ip_local && temp_local &&
  1455. sc->tempip->i_disk_size <= xfs_inode_data_fork_size(sc->ip)) {
  1456. xrep_tempfile_copyout_local(sc, XFS_DATA_FORK);
  1457. return xrep_dir_set_nlink(rd);
  1458. }
  1459. /*
  1460. * Clean the transaction before we start working on exchanging
  1461. * directory contents.
  1462. */
  1463. error = xrep_tempfile_roll_trans(rd->sc);
  1464. if (error)
  1465. return error;
  1466. /* Otherwise, make sure both data forks are in block-mapping mode. */
  1467. error = xrep_dir_swap_prep(sc, temp_local, ip_local);
  1468. if (error)
  1469. return error;
  1470. /*
  1471. * Set nlink of the directory in the same transaction sequence that
  1472. * (atomically) commits the new directory data.
  1473. */
  1474. error = xrep_dir_set_nlink(rd);
  1475. if (error)
  1476. return error;
  1477. return xrep_tempexch_contents(sc, &rd->tx);
  1478. }
  1479. /*
  1480. * Exchange the new directory contents (which we created in the tempfile) with
  1481. * the directory being repaired.
  1482. */
  1483. STATIC int
  1484. xrep_dir_rebuild_tree(
  1485. struct xrep_dir *rd)
  1486. {
  1487. struct xfs_scrub *sc = rd->sc;
  1488. int error;
  1489. trace_xrep_dir_rebuild_tree(sc->ip, rd->pscan.parent_ino);
  1490. /*
  1491. * Take the IOLOCK on the temporary file so that we can run dir
  1492. * operations with the same locks held as we would for a normal file.
  1493. * We still hold sc->ip's IOLOCK.
  1494. */
  1495. error = xrep_tempfile_iolock_polled(rd->sc);
  1496. if (error)
  1497. return error;
  1498. /*
  1499. * Allocate transaction, lock inodes, and make sure that we've replayed
  1500. * all the stashed dirent updates to the tempdir. After this point,
  1501. * we're ready to exchange data fork mappings.
  1502. */
  1503. error = xrep_dir_finalize_tempdir(rd);
  1504. if (error)
  1505. return error;
  1506. if (xchk_iscan_aborted(&rd->pscan.iscan))
  1507. return -ECANCELED;
  1508. /*
  1509. * Exchange the tempdir's data fork with the file being repaired. This
  1510. * recreates the transaction and re-takes the ILOCK in the scrub
  1511. * context.
  1512. */
  1513. error = xrep_dir_swap(rd);
  1514. if (error)
  1515. return error;
  1516. /*
  1517. * Release the old directory blocks and reset the data fork of the temp
  1518. * directory to an empty shortform directory because inactivation does
  1519. * nothing for directories.
  1520. */
  1521. error = xrep_dir_reset_fork(rd, sc->mp->m_rootip->i_ino);
  1522. if (error)
  1523. return error;
  1524. /*
  1525. * Roll to get a transaction without any inodes joined to it. Then we
  1526. * can drop the tempfile's ILOCK and IOLOCK before doing more work on
  1527. * the scrub target directory.
  1528. */
  1529. error = xfs_trans_roll(&sc->tp);
  1530. if (error)
  1531. return error;
  1532. xrep_tempfile_iunlock(sc);
  1533. xrep_tempfile_iounlock(sc);
  1534. return 0;
  1535. }
  1536. /* Set up the filesystem scan so we can regenerate directory entries. */
  1537. STATIC int
  1538. xrep_dir_setup_scan(
  1539. struct xrep_dir *rd)
  1540. {
  1541. struct xfs_scrub *sc = rd->sc;
  1542. char *descr;
  1543. int error;
  1544. /* Set up some staging memory for salvaging dirents. */
  1545. descr = xchk_xfile_ino_descr(sc, "directory entries");
  1546. error = xfarray_create(descr, 0, sizeof(struct xrep_dirent),
  1547. &rd->dir_entries);
  1548. kfree(descr);
  1549. if (error)
  1550. return error;
  1551. descr = xchk_xfile_ino_descr(sc, "directory entry names");
  1552. error = xfblob_create(descr, &rd->dir_names);
  1553. kfree(descr);
  1554. if (error)
  1555. goto out_xfarray;
  1556. if (xfs_has_parent(sc->mp))
  1557. error = __xrep_findparent_scan_start(sc, &rd->pscan,
  1558. xrep_dir_live_update);
  1559. else
  1560. error = xrep_findparent_scan_start(sc, &rd->pscan);
  1561. if (error)
  1562. goto out_xfblob;
  1563. return 0;
  1564. out_xfblob:
  1565. xfblob_destroy(rd->dir_names);
  1566. rd->dir_names = NULL;
  1567. out_xfarray:
  1568. xfarray_destroy(rd->dir_entries);
  1569. rd->dir_entries = NULL;
  1570. return error;
  1571. }
  1572. /*
  1573. * Move the current file to the orphanage.
  1574. *
  1575. * Caller must hold IOLOCK_EXCL on @sc->ip, and no other inode locks. Upon
  1576. * successful return, the scrub transaction will have enough extra reservation
  1577. * to make the move; it will hold IOLOCK_EXCL and ILOCK_EXCL of @sc->ip and the
  1578. * orphanage; and both inodes will be ijoined.
  1579. */
  1580. STATIC int
  1581. xrep_dir_move_to_orphanage(
  1582. struct xrep_dir *rd)
  1583. {
  1584. struct xfs_scrub *sc = rd->sc;
  1585. xfs_ino_t orig_parent, new_parent;
  1586. int error;
  1587. /*
  1588. * We are about to drop the ILOCK on sc->ip to lock the orphanage and
  1589. * prepare for the adoption. Therefore, look up the old dotdot entry
  1590. * for sc->ip so that we can compare it after we re-lock sc->ip.
  1591. */
  1592. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot, &orig_parent);
  1593. if (error)
  1594. return error;
  1595. /*
  1596. * Drop the ILOCK on the scrub target and commit the transaction.
  1597. * Adoption computes its own resource requirements and gathers the
  1598. * necessary components.
  1599. */
  1600. error = xrep_trans_commit(sc);
  1601. if (error)
  1602. return error;
  1603. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  1604. /* If we can take the orphanage's iolock then we're ready to move. */
  1605. if (!xrep_orphanage_ilock_nowait(sc, XFS_IOLOCK_EXCL)) {
  1606. xchk_iunlock(sc, sc->ilock_flags);
  1607. error = xrep_orphanage_iolock_two(sc);
  1608. if (error)
  1609. return error;
  1610. }
  1611. /* Grab transaction and ILOCK the two files. */
  1612. error = xrep_adoption_trans_alloc(sc, &rd->adoption);
  1613. if (error)
  1614. return error;
  1615. error = xrep_adoption_compute_name(&rd->adoption, &rd->xname);
  1616. if (error)
  1617. return error;
  1618. /*
  1619. * Now that we've reacquired the ILOCK on sc->ip, look up the dotdot
  1620. * entry again. If the parent changed or the child was unlinked while
  1621. * the child directory was unlocked, we don't need to move the child to
  1622. * the orphanage after all.
  1623. */
  1624. error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot, &new_parent);
  1625. if (error)
  1626. return error;
  1627. /*
  1628. * Attach to the orphanage if we still have a linked directory and it
  1629. * hasn't been moved.
  1630. */
  1631. if (orig_parent == new_parent && VFS_I(sc->ip)->i_nlink > 0) {
  1632. error = xrep_adoption_move(&rd->adoption);
  1633. if (error)
  1634. return error;
  1635. }
  1636. /*
  1637. * Launder the scrub transaction so we can drop the orphanage ILOCK
  1638. * and IOLOCK. Return holding the scrub target's ILOCK and IOLOCK.
  1639. */
  1640. error = xrep_adoption_trans_roll(&rd->adoption);
  1641. if (error)
  1642. return error;
  1643. xrep_orphanage_iunlock(sc, XFS_ILOCK_EXCL);
  1644. xrep_orphanage_iunlock(sc, XFS_IOLOCK_EXCL);
  1645. return 0;
  1646. }
  1647. /*
  1648. * Repair the directory metadata.
  1649. *
  1650. * XXX: Directory entry buffers can be multiple fsblocks in size. The buffer
  1651. * cache in XFS can't handle aliased multiblock buffers, so this might
  1652. * misbehave if the directory blocks are crosslinked with other filesystem
  1653. * metadata.
  1654. *
  1655. * XXX: Is it necessary to check the dcache for this directory to make sure
  1656. * that we always recreate every cached entry?
  1657. */
  1658. int
  1659. xrep_directory(
  1660. struct xfs_scrub *sc)
  1661. {
  1662. struct xrep_dir *rd = sc->buf;
  1663. int error;
  1664. /* The rmapbt is required to reap the old data fork. */
  1665. if (!xfs_has_rmapbt(sc->mp))
  1666. return -EOPNOTSUPP;
  1667. /* We require atomic file exchange range to rebuild anything. */
  1668. if (!xfs_has_exchange_range(sc->mp))
  1669. return -EOPNOTSUPP;
  1670. error = xrep_dir_setup_scan(rd);
  1671. if (error)
  1672. return error;
  1673. if (xfs_has_parent(sc->mp))
  1674. error = xrep_dir_scan_dirtree(rd);
  1675. else
  1676. error = xrep_dir_salvage_entries(rd);
  1677. if (error)
  1678. goto out_teardown;
  1679. /* Last chance to abort before we start committing fixes. */
  1680. if (xchk_should_terminate(sc, &error))
  1681. goto out_teardown;
  1682. error = xrep_dir_rebuild_tree(rd);
  1683. if (error)
  1684. goto out_teardown;
  1685. if (rd->needs_adoption) {
  1686. if (!xrep_orphanage_can_adopt(rd->sc))
  1687. error = -EFSCORRUPTED;
  1688. else
  1689. error = xrep_dir_move_to_orphanage(rd);
  1690. if (error)
  1691. goto out_teardown;
  1692. }
  1693. out_teardown:
  1694. xrep_dir_teardown(sc);
  1695. return error;
  1696. }