dir.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_log_format.h"
  13. #include "xfs_trans.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_icache.h"
  16. #include "xfs_dir2.h"
  17. #include "xfs_dir2_priv.h"
  18. #include "xfs_health.h"
  19. #include "xfs_attr.h"
  20. #include "xfs_parent.h"
  21. #include "scrub/scrub.h"
  22. #include "scrub/common.h"
  23. #include "scrub/dabtree.h"
  24. #include "scrub/readdir.h"
  25. #include "scrub/health.h"
  26. #include "scrub/repair.h"
  27. #include "scrub/trace.h"
  28. #include "scrub/xfile.h"
  29. #include "scrub/xfarray.h"
  30. #include "scrub/xfblob.h"
  31. /* Set us up to scrub directories. */
  32. int
  33. xchk_setup_directory(
  34. struct xfs_scrub *sc)
  35. {
  36. int error;
  37. if (xchk_could_repair(sc)) {
  38. error = xrep_setup_directory(sc);
  39. if (error)
  40. return error;
  41. }
  42. return xchk_setup_inode_contents(sc, 0);
  43. }
  44. /* Directories */
  45. /* Deferred directory entry that we saved for later. */
  46. struct xchk_dirent {
  47. /* Cookie for retrieval of the dirent name. */
  48. xfblob_cookie name_cookie;
  49. /* Child inode number. */
  50. xfs_ino_t ino;
  51. /* Length of the pptr name. */
  52. uint8_t namelen;
  53. };
  54. struct xchk_dir {
  55. struct xfs_scrub *sc;
  56. /* information for parent pointer validation. */
  57. struct xfs_parent_rec pptr_rec;
  58. struct xfs_da_args pptr_args;
  59. /* Fixed-size array of xchk_dirent structures. */
  60. struct xfarray *dir_entries;
  61. /* Blobs containing dirent names. */
  62. struct xfblob *dir_names;
  63. /* If we've cycled the ILOCK, we must revalidate deferred dirents. */
  64. bool need_revalidate;
  65. /* Name buffer for dirent revalidation. */
  66. struct xfs_name xname;
  67. uint8_t namebuf[MAXNAMELEN];
  68. };
  69. /* Scrub a directory entry. */
  70. /* Check that an inode's mode matches a given XFS_DIR3_FT_* type. */
  71. STATIC void
  72. xchk_dir_check_ftype(
  73. struct xfs_scrub *sc,
  74. xfs_fileoff_t offset,
  75. struct xfs_inode *ip,
  76. int ftype)
  77. {
  78. struct xfs_mount *mp = sc->mp;
  79. if (!xfs_has_ftype(mp)) {
  80. if (ftype != XFS_DIR3_FT_UNKNOWN && ftype != XFS_DIR3_FT_DIR)
  81. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  82. return;
  83. }
  84. if (xfs_mode_to_ftype(VFS_I(ip)->i_mode) != ftype)
  85. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  86. }
  87. /*
  88. * Try to lock a child file for checking parent pointers. Returns the inode
  89. * flags for the locks we now hold, or zero if we failed.
  90. */
  91. STATIC unsigned int
  92. xchk_dir_lock_child(
  93. struct xfs_scrub *sc,
  94. struct xfs_inode *ip)
  95. {
  96. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
  97. return 0;
  98. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  99. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  100. return 0;
  101. }
  102. if (!xfs_inode_has_attr_fork(ip) || !xfs_need_iread_extents(&ip->i_af))
  103. return XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED;
  104. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  105. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  106. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  107. return 0;
  108. }
  109. return XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL;
  110. }
  111. /* Check the backwards link (parent pointer) associated with this dirent. */
  112. STATIC int
  113. xchk_dir_parent_pointer(
  114. struct xchk_dir *sd,
  115. const struct xfs_name *name,
  116. struct xfs_inode *ip)
  117. {
  118. struct xfs_scrub *sc = sd->sc;
  119. int error;
  120. xfs_inode_to_parent_rec(&sd->pptr_rec, sc->ip);
  121. error = xfs_parent_lookup(sc->tp, ip, name, &sd->pptr_rec,
  122. &sd->pptr_args);
  123. if (error == -ENOATTR)
  124. xchk_fblock_xref_set_corrupt(sc, XFS_DATA_FORK, 0);
  125. return 0;
  126. }
  127. /* Look for a parent pointer matching this dirent, if the child isn't busy. */
  128. STATIC int
  129. xchk_dir_check_pptr_fast(
  130. struct xchk_dir *sd,
  131. xfs_dir2_dataptr_t dapos,
  132. const struct xfs_name *name,
  133. struct xfs_inode *ip)
  134. {
  135. struct xfs_scrub *sc = sd->sc;
  136. unsigned int lockmode;
  137. int error;
  138. /* dot and dotdot entries do not have parent pointers */
  139. if (xfs_dir2_samename(name, &xfs_name_dot) ||
  140. xfs_dir2_samename(name, &xfs_name_dotdot))
  141. return 0;
  142. /* No self-referential non-dot or dotdot dirents. */
  143. if (ip == sc->ip) {
  144. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  145. return -ECANCELED;
  146. }
  147. /* Try to lock the inode. */
  148. lockmode = xchk_dir_lock_child(sc, ip);
  149. if (!lockmode) {
  150. struct xchk_dirent save_de = {
  151. .namelen = name->len,
  152. .ino = ip->i_ino,
  153. };
  154. /* Couldn't lock the inode, so save the dirent for later. */
  155. trace_xchk_dir_defer(sc->ip, name, ip->i_ino);
  156. error = xfblob_storename(sd->dir_names, &save_de.name_cookie,
  157. name);
  158. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
  159. &error))
  160. return error;
  161. error = xfarray_append(sd->dir_entries, &save_de);
  162. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
  163. &error))
  164. return error;
  165. return 0;
  166. }
  167. error = xchk_dir_parent_pointer(sd, name, ip);
  168. xfs_iunlock(ip, lockmode);
  169. return error;
  170. }
  171. /*
  172. * Scrub a single directory entry.
  173. *
  174. * Check the inode number to make sure it's sane, then we check that we can
  175. * look up this filename. Finally, we check the ftype.
  176. */
  177. STATIC int
  178. xchk_dir_actor(
  179. struct xfs_scrub *sc,
  180. struct xfs_inode *dp,
  181. xfs_dir2_dataptr_t dapos,
  182. const struct xfs_name *name,
  183. xfs_ino_t ino,
  184. void *priv)
  185. {
  186. struct xfs_mount *mp = dp->i_mount;
  187. struct xfs_inode *ip;
  188. struct xchk_dir *sd = priv;
  189. xfs_ino_t lookup_ino;
  190. xfs_dablk_t offset;
  191. int error = 0;
  192. offset = xfs_dir2_db_to_da(mp->m_dir_geo,
  193. xfs_dir2_dataptr_to_db(mp->m_dir_geo, dapos));
  194. if (xchk_should_terminate(sc, &error))
  195. return error;
  196. /* Does this inode number make sense? */
  197. if (!xfs_verify_dir_ino(mp, ino)) {
  198. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  199. return -ECANCELED;
  200. }
  201. /* Does this name make sense? */
  202. if (!xfs_dir2_namecheck(name->name, name->len)) {
  203. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  204. return -ECANCELED;
  205. }
  206. if (xfs_dir2_samename(name, &xfs_name_dot)) {
  207. /* If this is "." then check that the inum matches the dir. */
  208. if (ino != dp->i_ino)
  209. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  210. } else if (xfs_dir2_samename(name, &xfs_name_dotdot)) {
  211. /*
  212. * If this is ".." in the root inode, check that the inum
  213. * matches this dir.
  214. */
  215. if (dp->i_ino == mp->m_sb.sb_rootino && ino != dp->i_ino)
  216. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  217. }
  218. /* Verify that we can look up this name by hash. */
  219. error = xchk_dir_lookup(sc, dp, name, &lookup_ino);
  220. /* ENOENT means the hash lookup failed and the dir is corrupt */
  221. if (error == -ENOENT)
  222. error = -EFSCORRUPTED;
  223. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error))
  224. goto out;
  225. if (lookup_ino != ino) {
  226. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  227. return -ECANCELED;
  228. }
  229. /*
  230. * Grab the inode pointed to by the dirent. We release the inode
  231. * before we cancel the scrub transaction.
  232. *
  233. * If _iget returns -EINVAL or -ENOENT then the child inode number is
  234. * garbage and the directory is corrupt. If the _iget returns
  235. * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
  236. * cross referencing error. Any other error is an operational error.
  237. */
  238. error = xchk_iget(sc, ino, &ip);
  239. if (error == -EINVAL || error == -ENOENT) {
  240. error = -EFSCORRUPTED;
  241. xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
  242. goto out;
  243. }
  244. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset, &error))
  245. goto out;
  246. xchk_dir_check_ftype(sc, offset, ip, name->type);
  247. if (xfs_has_parent(mp)) {
  248. error = xchk_dir_check_pptr_fast(sd, dapos, name, ip);
  249. if (error)
  250. goto out_rele;
  251. }
  252. out_rele:
  253. xchk_irele(sc, ip);
  254. out:
  255. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  256. return -ECANCELED;
  257. return error;
  258. }
  259. /* Scrub a directory btree record. */
  260. STATIC int
  261. xchk_dir_rec(
  262. struct xchk_da_btree *ds,
  263. int level)
  264. {
  265. struct xfs_name dname = { };
  266. struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
  267. struct xfs_mount *mp = ds->state->mp;
  268. struct xfs_inode *dp = ds->dargs.dp;
  269. struct xfs_da_geometry *geo = mp->m_dir_geo;
  270. struct xfs_dir2_data_entry *dent;
  271. struct xfs_buf *bp;
  272. struct xfs_dir2_leaf_entry *ent;
  273. unsigned int end;
  274. unsigned int iter_off;
  275. xfs_ino_t ino;
  276. xfs_dablk_t rec_bno;
  277. xfs_dir2_db_t db;
  278. xfs_dir2_data_aoff_t off;
  279. xfs_dir2_dataptr_t ptr;
  280. xfs_dahash_t calc_hash;
  281. xfs_dahash_t hash;
  282. struct xfs_dir3_icleaf_hdr hdr;
  283. unsigned int tag;
  284. int error;
  285. ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
  286. blk->magic == XFS_DIR2_LEAFN_MAGIC);
  287. xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
  288. ent = hdr.ents + blk->index;
  289. /* Check the hash of the entry. */
  290. error = xchk_da_btree_hash(ds, level, &ent->hashval);
  291. if (error)
  292. goto out;
  293. /* Valid hash pointer? */
  294. ptr = be32_to_cpu(ent->address);
  295. if (ptr == 0)
  296. return 0;
  297. /* Find the directory entry's location. */
  298. db = xfs_dir2_dataptr_to_db(geo, ptr);
  299. off = xfs_dir2_dataptr_to_off(geo, ptr);
  300. rec_bno = xfs_dir2_db_to_da(geo, db);
  301. if (rec_bno >= geo->leafblk) {
  302. xchk_da_set_corrupt(ds, level);
  303. goto out;
  304. }
  305. error = xfs_dir3_data_read(ds->dargs.trans, dp, ds->dargs.owner,
  306. rec_bno, XFS_DABUF_MAP_HOLE_OK, &bp);
  307. if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
  308. &error))
  309. goto out;
  310. if (!bp) {
  311. xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
  312. goto out;
  313. }
  314. xchk_buffer_recheck(ds->sc, bp);
  315. if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  316. goto out_relse;
  317. dent = bp->b_addr + off;
  318. /* Make sure we got a real directory entry. */
  319. iter_off = geo->data_entry_offset;
  320. end = xfs_dir3_data_end_offset(geo, bp->b_addr);
  321. if (!end) {
  322. xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
  323. goto out_relse;
  324. }
  325. for (;;) {
  326. struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
  327. struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
  328. if (iter_off >= end) {
  329. xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
  330. goto out_relse;
  331. }
  332. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  333. iter_off += be16_to_cpu(dup->length);
  334. continue;
  335. }
  336. if (dep == dent)
  337. break;
  338. iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
  339. }
  340. /* Retrieve the entry, sanity check it, and compare hashes. */
  341. ino = be64_to_cpu(dent->inumber);
  342. hash = be32_to_cpu(ent->hashval);
  343. tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
  344. if (!xfs_verify_dir_ino(mp, ino) || tag != off)
  345. xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
  346. if (dent->namelen == 0) {
  347. xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
  348. goto out_relse;
  349. }
  350. /* Does the directory hash match? */
  351. dname.name = dent->name;
  352. dname.len = dent->namelen;
  353. calc_hash = xfs_dir2_hashname(mp, &dname);
  354. if (calc_hash != hash)
  355. xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
  356. out_relse:
  357. xfs_trans_brelse(ds->dargs.trans, bp);
  358. out:
  359. return error;
  360. }
  361. /*
  362. * Is this unused entry either in the bestfree or smaller than all of
  363. * them? We've already checked that the bestfrees are sorted longest to
  364. * shortest, and that there aren't any bogus entries.
  365. */
  366. STATIC void
  367. xchk_directory_check_free_entry(
  368. struct xfs_scrub *sc,
  369. xfs_dablk_t lblk,
  370. struct xfs_dir2_data_free *bf,
  371. struct xfs_dir2_data_unused *dup)
  372. {
  373. struct xfs_dir2_data_free *dfp;
  374. unsigned int dup_length;
  375. dup_length = be16_to_cpu(dup->length);
  376. /* Unused entry is shorter than any of the bestfrees */
  377. if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
  378. return;
  379. for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
  380. if (dup_length == be16_to_cpu(dfp->length))
  381. return;
  382. /* Unused entry should be in the bestfrees but wasn't found. */
  383. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  384. }
  385. /* Check free space info in a directory data block. */
  386. STATIC int
  387. xchk_directory_data_bestfree(
  388. struct xfs_scrub *sc,
  389. xfs_dablk_t lblk,
  390. bool is_block)
  391. {
  392. struct xfs_dir2_data_unused *dup;
  393. struct xfs_dir2_data_free *dfp;
  394. struct xfs_buf *bp;
  395. struct xfs_dir2_data_free *bf;
  396. struct xfs_mount *mp = sc->mp;
  397. u16 tag;
  398. unsigned int nr_bestfrees = 0;
  399. unsigned int nr_frees = 0;
  400. unsigned int smallest_bestfree;
  401. int newlen;
  402. unsigned int offset;
  403. unsigned int end;
  404. int error;
  405. if (is_block) {
  406. /* dir block format */
  407. if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
  408. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  409. error = xfs_dir3_block_read(sc->tp, sc->ip, sc->ip->i_ino, &bp);
  410. } else {
  411. /* dir data format */
  412. error = xfs_dir3_data_read(sc->tp, sc->ip, sc->ip->i_ino, lblk,
  413. 0, &bp);
  414. }
  415. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
  416. goto out;
  417. xchk_buffer_recheck(sc, bp);
  418. /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
  419. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  420. goto out_buf;
  421. /* Do the bestfrees correspond to actual free space? */
  422. bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
  423. smallest_bestfree = UINT_MAX;
  424. for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
  425. offset = be16_to_cpu(dfp->offset);
  426. if (offset == 0)
  427. continue;
  428. if (offset >= mp->m_dir_geo->blksize) {
  429. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  430. goto out_buf;
  431. }
  432. dup = bp->b_addr + offset;
  433. tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
  434. /* bestfree doesn't match the entry it points at? */
  435. if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
  436. be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
  437. tag != offset) {
  438. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  439. goto out_buf;
  440. }
  441. /* bestfree records should be ordered largest to smallest */
  442. if (smallest_bestfree < be16_to_cpu(dfp->length)) {
  443. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  444. goto out_buf;
  445. }
  446. smallest_bestfree = be16_to_cpu(dfp->length);
  447. nr_bestfrees++;
  448. }
  449. /* Make sure the bestfrees are actually the best free spaces. */
  450. offset = mp->m_dir_geo->data_entry_offset;
  451. end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
  452. /* Iterate the entries, stopping when we hit or go past the end. */
  453. while (offset < end) {
  454. dup = bp->b_addr + offset;
  455. /* Skip real entries */
  456. if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
  457. struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
  458. newlen = xfs_dir2_data_entsize(mp, dep->namelen);
  459. if (newlen <= 0) {
  460. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
  461. lblk);
  462. goto out_buf;
  463. }
  464. offset += newlen;
  465. continue;
  466. }
  467. /* Spot check this free entry */
  468. tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
  469. if (tag != offset) {
  470. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  471. goto out_buf;
  472. }
  473. /*
  474. * Either this entry is a bestfree or it's smaller than
  475. * any of the bestfrees.
  476. */
  477. xchk_directory_check_free_entry(sc, lblk, bf, dup);
  478. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  479. goto out_buf;
  480. /* Move on. */
  481. newlen = be16_to_cpu(dup->length);
  482. if (newlen <= 0) {
  483. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  484. goto out_buf;
  485. }
  486. offset += newlen;
  487. if (offset <= end)
  488. nr_frees++;
  489. }
  490. /* We're required to fill all the space. */
  491. if (offset != end)
  492. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  493. /* Did we see at least as many free slots as there are bestfrees? */
  494. if (nr_frees < nr_bestfrees)
  495. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  496. out_buf:
  497. xfs_trans_brelse(sc->tp, bp);
  498. out:
  499. return error;
  500. }
  501. /*
  502. * Does the free space length in the free space index block ($len) match
  503. * the longest length in the directory data block's bestfree array?
  504. * Assume that we've already checked that the data block's bestfree
  505. * array is in order.
  506. */
  507. STATIC void
  508. xchk_directory_check_freesp(
  509. struct xfs_scrub *sc,
  510. xfs_dablk_t lblk,
  511. struct xfs_buf *dbp,
  512. unsigned int len)
  513. {
  514. struct xfs_dir2_data_free *dfp;
  515. dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
  516. if (len != be16_to_cpu(dfp->length))
  517. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  518. if (len > 0 && be16_to_cpu(dfp->offset) == 0)
  519. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  520. }
  521. /* Check free space info in a directory leaf1 block. */
  522. STATIC int
  523. xchk_directory_leaf1_bestfree(
  524. struct xfs_scrub *sc,
  525. struct xfs_da_args *args,
  526. xfs_dir2_db_t last_data_db,
  527. xfs_dablk_t lblk)
  528. {
  529. struct xfs_dir3_icleaf_hdr leafhdr;
  530. struct xfs_dir2_leaf_tail *ltp;
  531. struct xfs_dir2_leaf *leaf;
  532. struct xfs_buf *dbp;
  533. struct xfs_buf *bp;
  534. struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
  535. __be16 *bestp;
  536. __u16 best;
  537. __u32 hash;
  538. __u32 lasthash = 0;
  539. __u32 bestcount;
  540. unsigned int stale = 0;
  541. int i;
  542. int error;
  543. /* Read the free space block. */
  544. error = xfs_dir3_leaf_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, &bp);
  545. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
  546. return error;
  547. xchk_buffer_recheck(sc, bp);
  548. leaf = bp->b_addr;
  549. xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
  550. ltp = xfs_dir2_leaf_tail_p(geo, leaf);
  551. bestcount = be32_to_cpu(ltp->bestcount);
  552. bestp = xfs_dir2_leaf_bests_p(ltp);
  553. if (xfs_has_crc(sc->mp)) {
  554. struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
  555. if (hdr3->pad != cpu_to_be32(0))
  556. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  557. }
  558. /*
  559. * There must be enough bestfree slots to cover all the directory data
  560. * blocks that we scanned. It is possible for there to be a hole
  561. * between the last data block and i_disk_size. This seems like an
  562. * oversight to the scrub author, but as we have been writing out
  563. * directories like this (and xfs_repair doesn't mind them) for years,
  564. * that's what we have to check.
  565. */
  566. if (bestcount != last_data_db + 1) {
  567. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  568. goto out;
  569. }
  570. /* Is the leaf count even remotely sane? */
  571. if (leafhdr.count > geo->leaf_max_ents) {
  572. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  573. goto out;
  574. }
  575. /* Leaves and bests don't overlap in leaf format. */
  576. if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
  577. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  578. goto out;
  579. }
  580. /* Check hash value order, count stale entries. */
  581. for (i = 0; i < leafhdr.count; i++) {
  582. hash = be32_to_cpu(leafhdr.ents[i].hashval);
  583. if (i > 0 && lasthash > hash)
  584. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  585. lasthash = hash;
  586. if (leafhdr.ents[i].address ==
  587. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  588. stale++;
  589. }
  590. if (leafhdr.stale != stale)
  591. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  592. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  593. goto out;
  594. /* Check all the bestfree entries. */
  595. for (i = 0; i < bestcount; i++, bestp++) {
  596. best = be16_to_cpu(*bestp);
  597. error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
  598. xfs_dir2_db_to_da(args->geo, i),
  599. XFS_DABUF_MAP_HOLE_OK, &dbp);
  600. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
  601. &error))
  602. break;
  603. if (!dbp) {
  604. if (best != NULLDATAOFF) {
  605. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
  606. lblk);
  607. break;
  608. }
  609. continue;
  610. }
  611. if (best == NULLDATAOFF)
  612. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  613. else
  614. xchk_directory_check_freesp(sc, lblk, dbp, best);
  615. xfs_trans_brelse(sc->tp, dbp);
  616. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  617. break;
  618. }
  619. out:
  620. xfs_trans_brelse(sc->tp, bp);
  621. return error;
  622. }
  623. /* Check free space info in a directory freespace block. */
  624. STATIC int
  625. xchk_directory_free_bestfree(
  626. struct xfs_scrub *sc,
  627. struct xfs_da_args *args,
  628. xfs_dablk_t lblk)
  629. {
  630. struct xfs_dir3_icfree_hdr freehdr;
  631. struct xfs_buf *dbp;
  632. struct xfs_buf *bp;
  633. __u16 best;
  634. unsigned int stale = 0;
  635. int i;
  636. int error;
  637. /* Read the free space block */
  638. error = xfs_dir2_free_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, &bp);
  639. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
  640. return error;
  641. xchk_buffer_recheck(sc, bp);
  642. if (xfs_has_crc(sc->mp)) {
  643. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  644. if (hdr3->pad != cpu_to_be32(0))
  645. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  646. }
  647. /* Check all the entries. */
  648. xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
  649. for (i = 0; i < freehdr.nvalid; i++) {
  650. best = be16_to_cpu(freehdr.bests[i]);
  651. if (best == NULLDATAOFF) {
  652. stale++;
  653. continue;
  654. }
  655. error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
  656. (freehdr.firstdb + i) * args->geo->fsbcount,
  657. 0, &dbp);
  658. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
  659. &error))
  660. goto out;
  661. xchk_directory_check_freesp(sc, lblk, dbp, best);
  662. xfs_trans_brelse(sc->tp, dbp);
  663. }
  664. if (freehdr.nused + stale != freehdr.nvalid)
  665. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  666. out:
  667. xfs_trans_brelse(sc->tp, bp);
  668. return error;
  669. }
  670. /* Check free space information in directories. */
  671. STATIC int
  672. xchk_directory_blocks(
  673. struct xfs_scrub *sc)
  674. {
  675. struct xfs_bmbt_irec got;
  676. struct xfs_da_args args = {
  677. .dp = sc->ip,
  678. .whichfork = XFS_DATA_FORK,
  679. .geo = sc->mp->m_dir_geo,
  680. .trans = sc->tp,
  681. .owner = sc->ip->i_ino,
  682. };
  683. struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
  684. struct xfs_mount *mp = sc->mp;
  685. xfs_fileoff_t leaf_lblk;
  686. xfs_fileoff_t free_lblk;
  687. xfs_fileoff_t lblk;
  688. struct xfs_iext_cursor icur;
  689. xfs_dablk_t dabno;
  690. xfs_dir2_db_t last_data_db = 0;
  691. bool found;
  692. bool is_block = false;
  693. int error;
  694. /* Ignore local format directories. */
  695. if (ifp->if_format != XFS_DINODE_FMT_EXTENTS &&
  696. ifp->if_format != XFS_DINODE_FMT_BTREE)
  697. return 0;
  698. lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
  699. leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
  700. free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
  701. /* Is this a block dir? */
  702. if (xfs_dir2_format(&args, &error) == XFS_DIR2_FMT_BLOCK)
  703. is_block = true;
  704. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
  705. goto out;
  706. /* Iterate all the data extents in the directory... */
  707. found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
  708. while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
  709. /* No more data blocks... */
  710. if (got.br_startoff >= leaf_lblk)
  711. break;
  712. /*
  713. * Check each data block's bestfree data.
  714. *
  715. * Iterate all the fsbcount-aligned block offsets in
  716. * this directory. The directory block reading code is
  717. * smart enough to do its own bmap lookups to handle
  718. * discontiguous directory blocks. When we're done
  719. * with the extent record, re-query the bmap at the
  720. * next fsbcount-aligned offset to avoid redundant
  721. * block checks.
  722. */
  723. for (lblk = roundup((xfs_dablk_t)got.br_startoff,
  724. args.geo->fsbcount);
  725. lblk < got.br_startoff + got.br_blockcount;
  726. lblk += args.geo->fsbcount) {
  727. last_data_db = xfs_dir2_da_to_db(args.geo, lblk);
  728. error = xchk_directory_data_bestfree(sc, lblk,
  729. is_block);
  730. if (error)
  731. goto out;
  732. }
  733. dabno = got.br_startoff + got.br_blockcount;
  734. lblk = roundup(dabno, args.geo->fsbcount);
  735. found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
  736. }
  737. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  738. goto out;
  739. /* Look for a leaf1 block, which has free info. */
  740. if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
  741. got.br_startoff == leaf_lblk &&
  742. got.br_blockcount == args.geo->fsbcount &&
  743. !xfs_iext_next_extent(ifp, &icur, &got)) {
  744. if (is_block) {
  745. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  746. goto out;
  747. }
  748. error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db,
  749. leaf_lblk);
  750. if (error)
  751. goto out;
  752. }
  753. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  754. goto out;
  755. /* Scan for free blocks */
  756. lblk = free_lblk;
  757. found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
  758. while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
  759. /*
  760. * Dirs can't have blocks mapped above 2^32.
  761. * Single-block dirs shouldn't even be here.
  762. */
  763. lblk = got.br_startoff;
  764. if (lblk & ~0xFFFFFFFFULL) {
  765. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  766. goto out;
  767. }
  768. if (is_block) {
  769. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
  770. goto out;
  771. }
  772. /*
  773. * Check each dir free block's bestfree data.
  774. *
  775. * Iterate all the fsbcount-aligned block offsets in
  776. * this directory. The directory block reading code is
  777. * smart enough to do its own bmap lookups to handle
  778. * discontiguous directory blocks. When we're done
  779. * with the extent record, re-query the bmap at the
  780. * next fsbcount-aligned offset to avoid redundant
  781. * block checks.
  782. */
  783. for (lblk = roundup((xfs_dablk_t)got.br_startoff,
  784. args.geo->fsbcount);
  785. lblk < got.br_startoff + got.br_blockcount;
  786. lblk += args.geo->fsbcount) {
  787. error = xchk_directory_free_bestfree(sc, &args,
  788. lblk);
  789. if (error)
  790. goto out;
  791. }
  792. dabno = got.br_startoff + got.br_blockcount;
  793. lblk = roundup(dabno, args.geo->fsbcount);
  794. found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
  795. }
  796. out:
  797. return error;
  798. }
  799. /*
  800. * Revalidate a dirent that we collected in the past but couldn't check because
  801. * of lock contention. Returns 0 if the dirent is still valid, -ENOENT if it
  802. * has gone away on us, or a negative errno.
  803. */
  804. STATIC int
  805. xchk_dir_revalidate_dirent(
  806. struct xchk_dir *sd,
  807. const struct xfs_name *xname,
  808. xfs_ino_t ino)
  809. {
  810. struct xfs_scrub *sc = sd->sc;
  811. xfs_ino_t child_ino;
  812. int error;
  813. /*
  814. * Look up the directory entry. If we get -ENOENT, the directory entry
  815. * went away and there's nothing to revalidate. Return any other
  816. * error.
  817. */
  818. error = xchk_dir_lookup(sc, sc->ip, xname, &child_ino);
  819. if (error)
  820. return error;
  821. /* The inode number changed, nothing to revalidate. */
  822. if (ino != child_ino)
  823. return -ENOENT;
  824. return 0;
  825. }
  826. /*
  827. * Check a directory entry's parent pointers the slow way, which means we cycle
  828. * locks a bunch and put up with revalidation until we get it done.
  829. */
  830. STATIC int
  831. xchk_dir_slow_dirent(
  832. struct xchk_dir *sd,
  833. struct xchk_dirent *dirent,
  834. const struct xfs_name *xname)
  835. {
  836. struct xfs_scrub *sc = sd->sc;
  837. struct xfs_inode *ip;
  838. unsigned int lockmode;
  839. int error;
  840. /* Check that the deferred dirent still exists. */
  841. if (sd->need_revalidate) {
  842. error = xchk_dir_revalidate_dirent(sd, xname, dirent->ino);
  843. if (error == -ENOENT)
  844. return 0;
  845. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
  846. &error))
  847. return error;
  848. }
  849. error = xchk_iget(sc, dirent->ino, &ip);
  850. if (error == -EINVAL || error == -ENOENT) {
  851. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  852. return 0;
  853. }
  854. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
  855. return error;
  856. /*
  857. * If we can grab both IOLOCK and ILOCK of the alleged child, we can
  858. * proceed with the validation.
  859. */
  860. lockmode = xchk_dir_lock_child(sc, ip);
  861. if (lockmode) {
  862. trace_xchk_dir_slowpath(sc->ip, xname, ip->i_ino);
  863. goto check_pptr;
  864. }
  865. /*
  866. * We couldn't lock the child file. Drop all the locks and try to
  867. * get them again, one at a time.
  868. */
  869. xchk_iunlock(sc, sc->ilock_flags);
  870. sd->need_revalidate = true;
  871. trace_xchk_dir_ultraslowpath(sc->ip, xname, ip->i_ino);
  872. error = xchk_dir_trylock_for_pptrs(sc, ip, &lockmode);
  873. if (error)
  874. goto out_rele;
  875. /* Revalidate, since we just cycled the locks. */
  876. error = xchk_dir_revalidate_dirent(sd, xname, dirent->ino);
  877. if (error == -ENOENT) {
  878. error = 0;
  879. goto out_unlock;
  880. }
  881. if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
  882. goto out_unlock;
  883. check_pptr:
  884. error = xchk_dir_parent_pointer(sd, xname, ip);
  885. out_unlock:
  886. xfs_iunlock(ip, lockmode);
  887. out_rele:
  888. xchk_irele(sc, ip);
  889. return error;
  890. }
  891. /* Check all the dirents that we deferred the first time around. */
  892. STATIC int
  893. xchk_dir_finish_slow_dirents(
  894. struct xchk_dir *sd)
  895. {
  896. xfarray_idx_t array_cur;
  897. int error;
  898. foreach_xfarray_idx(sd->dir_entries, array_cur) {
  899. struct xchk_dirent dirent;
  900. if (sd->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  901. return 0;
  902. error = xfarray_load(sd->dir_entries, array_cur, &dirent);
  903. if (error)
  904. return error;
  905. error = xfblob_loadname(sd->dir_names, dirent.name_cookie,
  906. &sd->xname, dirent.namelen);
  907. if (error)
  908. return error;
  909. error = xchk_dir_slow_dirent(sd, &dirent, &sd->xname);
  910. if (error)
  911. return error;
  912. }
  913. return 0;
  914. }
  915. /* Scrub a whole directory. */
  916. int
  917. xchk_directory(
  918. struct xfs_scrub *sc)
  919. {
  920. struct xchk_dir *sd;
  921. int error;
  922. if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
  923. return -ENOENT;
  924. if (xchk_file_looks_zapped(sc, XFS_SICK_INO_DIR_ZAPPED)) {
  925. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  926. return 0;
  927. }
  928. /* Plausible size? */
  929. if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) {
  930. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  931. return 0;
  932. }
  933. /* Check directory tree structure */
  934. error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
  935. if (error)
  936. return error;
  937. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  938. return 0;
  939. /* Check the freespace. */
  940. error = xchk_directory_blocks(sc);
  941. if (error)
  942. return error;
  943. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  944. return 0;
  945. sd = kvzalloc(sizeof(struct xchk_dir), XCHK_GFP_FLAGS);
  946. if (!sd)
  947. return -ENOMEM;
  948. sd->sc = sc;
  949. sd->xname.name = sd->namebuf;
  950. if (xfs_has_parent(sc->mp)) {
  951. char *descr;
  952. /*
  953. * Set up some staging memory for dirents that we can't check
  954. * due to locking contention.
  955. */
  956. descr = xchk_xfile_ino_descr(sc, "slow directory entries");
  957. error = xfarray_create(descr, 0, sizeof(struct xchk_dirent),
  958. &sd->dir_entries);
  959. kfree(descr);
  960. if (error)
  961. goto out_sd;
  962. descr = xchk_xfile_ino_descr(sc, "slow directory entry names");
  963. error = xfblob_create(descr, &sd->dir_names);
  964. kfree(descr);
  965. if (error)
  966. goto out_entries;
  967. }
  968. /* Look up every name in this directory by hash. */
  969. error = xchk_dir_walk(sc, sc->ip, xchk_dir_actor, sd);
  970. if (error == -ECANCELED)
  971. error = 0;
  972. if (error)
  973. goto out_names;
  974. if (xfs_has_parent(sc->mp)) {
  975. error = xchk_dir_finish_slow_dirents(sd);
  976. if (error == -ETIMEDOUT) {
  977. /* Couldn't grab a lock, scrub was marked incomplete */
  978. error = 0;
  979. goto out_names;
  980. }
  981. if (error)
  982. goto out_names;
  983. }
  984. out_names:
  985. if (sd->dir_names)
  986. xfblob_destroy(sd->dir_names);
  987. out_entries:
  988. if (sd->dir_entries)
  989. xfarray_destroy(sd->dir_entries);
  990. out_sd:
  991. kvfree(sd);
  992. if (error)
  993. return error;
  994. /* If the dir is clean, it is clearly not zapped. */
  995. xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_DIR_ZAPPED);
  996. return 0;
  997. }
  998. /*
  999. * Decide if this directory has been zapped to satisfy the inode and ifork
  1000. * verifiers. Checking and repairing should be postponed until the directory
  1001. * is fixed.
  1002. */
  1003. bool
  1004. xchk_dir_looks_zapped(
  1005. struct xfs_inode *dp)
  1006. {
  1007. /* Repair zapped this dir's data fork a short time ago */
  1008. if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
  1009. return true;
  1010. /*
  1011. * If the dinode repair found a bad data fork, it will reset the fork
  1012. * to extents format with zero records and wait for the bmapbtd
  1013. * scrubber to reconstruct the block mappings. Directories always
  1014. * contain some content, so this is a clear sign of a zapped directory.
  1015. * The state checked by xfs_ifork_zapped is not persisted, so this is
  1016. * the secondary strategy if repairs are interrupted by a crash or an
  1017. * unmount.
  1018. */
  1019. return dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS &&
  1020. dp->i_df.if_nextents == 0;
  1021. }