inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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_btree.h"
  13. #include "xfs_log_format.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_ag.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_ialloc.h"
  18. #include "xfs_icache.h"
  19. #include "xfs_da_format.h"
  20. #include "xfs_reflink.h"
  21. #include "xfs_rmap.h"
  22. #include "xfs_bmap_util.h"
  23. #include "xfs_rtbitmap.h"
  24. #include "scrub/scrub.h"
  25. #include "scrub/common.h"
  26. #include "scrub/btree.h"
  27. #include "scrub/trace.h"
  28. #include "scrub/repair.h"
  29. /* Prepare the attached inode for scrubbing. */
  30. static inline int
  31. xchk_prepare_iscrub(
  32. struct xfs_scrub *sc)
  33. {
  34. int error;
  35. xchk_ilock(sc, XFS_IOLOCK_EXCL);
  36. error = xchk_trans_alloc(sc, 0);
  37. if (error)
  38. return error;
  39. error = xchk_ino_dqattach(sc);
  40. if (error)
  41. return error;
  42. xchk_ilock(sc, XFS_ILOCK_EXCL);
  43. return 0;
  44. }
  45. /* Install this scrub-by-handle inode and prepare it for scrubbing. */
  46. static inline int
  47. xchk_install_handle_iscrub(
  48. struct xfs_scrub *sc,
  49. struct xfs_inode *ip)
  50. {
  51. int error;
  52. error = xchk_install_handle_inode(sc, ip);
  53. if (error)
  54. return error;
  55. return xchk_prepare_iscrub(sc);
  56. }
  57. /*
  58. * Grab total control of the inode metadata. In the best case, we grab the
  59. * incore inode and take all locks on it. If the incore inode cannot be
  60. * constructed due to corruption problems, lock the AGI so that we can single
  61. * step the loading process to fix everything that can go wrong.
  62. */
  63. int
  64. xchk_setup_inode(
  65. struct xfs_scrub *sc)
  66. {
  67. struct xfs_imap imap;
  68. struct xfs_inode *ip;
  69. struct xfs_mount *mp = sc->mp;
  70. struct xfs_inode *ip_in = XFS_I(file_inode(sc->file));
  71. struct xfs_buf *agi_bp;
  72. struct xfs_perag *pag;
  73. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, sc->sm->sm_ino);
  74. int error;
  75. if (xchk_need_intent_drain(sc))
  76. xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
  77. /* We want to scan the opened inode, so lock it and exit. */
  78. if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
  79. error = xchk_install_live_inode(sc, ip_in);
  80. if (error)
  81. return error;
  82. return xchk_prepare_iscrub(sc);
  83. }
  84. /* Reject internal metadata files and obviously bad inode numbers. */
  85. if (xfs_internal_inum(mp, sc->sm->sm_ino))
  86. return -ENOENT;
  87. if (!xfs_verify_ino(sc->mp, sc->sm->sm_ino))
  88. return -ENOENT;
  89. /* Try a safe untrusted iget. */
  90. error = xchk_iget_safe(sc, sc->sm->sm_ino, &ip);
  91. if (!error)
  92. return xchk_install_handle_iscrub(sc, ip);
  93. if (error == -ENOENT)
  94. return error;
  95. if (error != -EFSCORRUPTED && error != -EFSBADCRC && error != -EINVAL)
  96. goto out_error;
  97. /*
  98. * EINVAL with IGET_UNTRUSTED probably means one of several things:
  99. * userspace gave us an inode number that doesn't correspond to fs
  100. * space; the inode btree lacks a record for this inode; or there is
  101. * a record, and it says this inode is free.
  102. *
  103. * EFSCORRUPTED/EFSBADCRC could mean that the inode was mappable, but
  104. * some other metadata corruption (e.g. inode forks) prevented
  105. * instantiation of the incore inode. Or it could mean the inobt is
  106. * corrupt.
  107. *
  108. * We want to look up this inode in the inobt directly to distinguish
  109. * three different scenarios: (1) the inobt says the inode is free,
  110. * in which case there's nothing to do; (2) the inobt is corrupt so we
  111. * should flag the corruption and exit to userspace to let it fix the
  112. * inobt; and (3) the inobt says the inode is allocated, but loading it
  113. * failed due to corruption.
  114. *
  115. * Allocate a transaction and grab the AGI to prevent inobt activity in
  116. * this AG. Retry the iget in case someone allocated a new inode after
  117. * the first iget failed.
  118. */
  119. error = xchk_trans_alloc(sc, 0);
  120. if (error)
  121. goto out_error;
  122. error = xchk_iget_agi(sc, sc->sm->sm_ino, &agi_bp, &ip);
  123. if (error == 0) {
  124. /* Actually got the incore inode, so install it and proceed. */
  125. xchk_trans_cancel(sc);
  126. return xchk_install_handle_iscrub(sc, ip);
  127. }
  128. if (error == -ENOENT)
  129. goto out_gone;
  130. if (error != -EFSCORRUPTED && error != -EFSBADCRC && error != -EINVAL)
  131. goto out_cancel;
  132. /* Ensure that we have protected against inode allocation/freeing. */
  133. if (agi_bp == NULL) {
  134. ASSERT(agi_bp != NULL);
  135. error = -ECANCELED;
  136. goto out_cancel;
  137. }
  138. /*
  139. * Untrusted iget failed a second time. Let's try an inobt lookup.
  140. * If the inobt doesn't think this is an allocated inode then we'll
  141. * return ENOENT to signal that the check can be skipped.
  142. *
  143. * If the lookup signals corruption, we'll mark this inode corrupt and
  144. * exit to userspace. There's little chance of fixing anything until
  145. * the inobt is straightened out, but there's nothing we can do here.
  146. *
  147. * If the lookup encounters a runtime error, exit to userspace.
  148. */
  149. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sc->sm->sm_ino));
  150. if (!pag) {
  151. error = -EFSCORRUPTED;
  152. goto out_cancel;
  153. }
  154. error = xfs_imap(pag, sc->tp, sc->sm->sm_ino, &imap,
  155. XFS_IGET_UNTRUSTED);
  156. xfs_perag_put(pag);
  157. if (error == -EINVAL || error == -ENOENT)
  158. goto out_gone;
  159. if (error)
  160. goto out_cancel;
  161. /*
  162. * The lookup succeeded. Chances are the ondisk inode is corrupt and
  163. * preventing iget from reading it. Retain the scrub transaction and
  164. * the AGI buffer to prevent anyone from allocating or freeing inodes.
  165. * This ensures that we preserve the inconsistency between the inobt
  166. * saying the inode is allocated and the icache being unable to load
  167. * the inode until we can flag the corruption in xchk_inode. The
  168. * scrub function has to note the corruption, since we're not really
  169. * supposed to do that from the setup function. Save the mapping to
  170. * make repairs to the ondisk inode buffer.
  171. */
  172. if (xchk_could_repair(sc))
  173. xrep_setup_inode(sc, &imap);
  174. return 0;
  175. out_cancel:
  176. xchk_trans_cancel(sc);
  177. out_error:
  178. trace_xchk_op_error(sc, agno, XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
  179. error, __return_address);
  180. return error;
  181. out_gone:
  182. /* The file is gone, so there's nothing to check. */
  183. xchk_trans_cancel(sc);
  184. return -ENOENT;
  185. }
  186. /* Inode core */
  187. /* Validate di_extsize hint. */
  188. STATIC void
  189. xchk_inode_extsize(
  190. struct xfs_scrub *sc,
  191. struct xfs_dinode *dip,
  192. xfs_ino_t ino,
  193. uint16_t mode,
  194. uint16_t flags)
  195. {
  196. xfs_failaddr_t fa;
  197. uint32_t value = be32_to_cpu(dip->di_extsize);
  198. fa = xfs_inode_validate_extsize(sc->mp, value, mode, flags);
  199. if (fa)
  200. xchk_ino_set_corrupt(sc, ino);
  201. /*
  202. * XFS allows a sysadmin to change the rt extent size when adding a rt
  203. * section to a filesystem after formatting. If there are any
  204. * directories with extszinherit and rtinherit set, the hint could
  205. * become misaligned with the new rextsize. The verifier doesn't check
  206. * this, because we allow rtinherit directories even without an rt
  207. * device. Flag this as an administrative warning since we will clean
  208. * this up eventually.
  209. */
  210. if ((flags & XFS_DIFLAG_RTINHERIT) &&
  211. (flags & XFS_DIFLAG_EXTSZINHERIT) &&
  212. xfs_extlen_to_rtxmod(sc->mp, value) > 0)
  213. xchk_ino_set_warning(sc, ino);
  214. }
  215. /*
  216. * Validate di_cowextsize hint.
  217. *
  218. * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
  219. * These functions must be kept in sync with each other.
  220. */
  221. STATIC void
  222. xchk_inode_cowextsize(
  223. struct xfs_scrub *sc,
  224. struct xfs_dinode *dip,
  225. xfs_ino_t ino,
  226. uint16_t mode,
  227. uint16_t flags,
  228. uint64_t flags2)
  229. {
  230. xfs_failaddr_t fa;
  231. fa = xfs_inode_validate_cowextsize(sc->mp,
  232. be32_to_cpu(dip->di_cowextsize), mode, flags,
  233. flags2);
  234. if (fa)
  235. xchk_ino_set_corrupt(sc, ino);
  236. }
  237. /* Make sure the di_flags make sense for the inode. */
  238. STATIC void
  239. xchk_inode_flags(
  240. struct xfs_scrub *sc,
  241. struct xfs_dinode *dip,
  242. xfs_ino_t ino,
  243. uint16_t mode,
  244. uint16_t flags)
  245. {
  246. struct xfs_mount *mp = sc->mp;
  247. /* di_flags are all taken, last bit cannot be used */
  248. if (flags & ~XFS_DIFLAG_ANY)
  249. goto bad;
  250. /* rt flags require rt device */
  251. if ((flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
  252. goto bad;
  253. /* new rt bitmap flag only valid for rbmino */
  254. if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino)
  255. goto bad;
  256. /* directory-only flags */
  257. if ((flags & (XFS_DIFLAG_RTINHERIT |
  258. XFS_DIFLAG_EXTSZINHERIT |
  259. XFS_DIFLAG_PROJINHERIT |
  260. XFS_DIFLAG_NOSYMLINKS)) &&
  261. !S_ISDIR(mode))
  262. goto bad;
  263. /* file-only flags */
  264. if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) &&
  265. !S_ISREG(mode))
  266. goto bad;
  267. /* filestreams and rt make no sense */
  268. if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME))
  269. goto bad;
  270. return;
  271. bad:
  272. xchk_ino_set_corrupt(sc, ino);
  273. }
  274. /* Make sure the di_flags2 make sense for the inode. */
  275. STATIC void
  276. xchk_inode_flags2(
  277. struct xfs_scrub *sc,
  278. struct xfs_dinode *dip,
  279. xfs_ino_t ino,
  280. uint16_t mode,
  281. uint16_t flags,
  282. uint64_t flags2)
  283. {
  284. struct xfs_mount *mp = sc->mp;
  285. /* Unknown di_flags2 could be from a future kernel */
  286. if (flags2 & ~XFS_DIFLAG2_ANY)
  287. xchk_ino_set_warning(sc, ino);
  288. /* reflink flag requires reflink feature */
  289. if ((flags2 & XFS_DIFLAG2_REFLINK) &&
  290. !xfs_has_reflink(mp))
  291. goto bad;
  292. /* cowextsize flag is checked w.r.t. mode separately */
  293. /* file/dir-only flags */
  294. if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode)))
  295. goto bad;
  296. /* file-only flags */
  297. if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode))
  298. goto bad;
  299. /* realtime and reflink make no sense, currently */
  300. if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK))
  301. goto bad;
  302. /* no bigtime iflag without the bigtime feature */
  303. if (xfs_dinode_has_bigtime(dip) && !xfs_has_bigtime(mp))
  304. goto bad;
  305. /* no large extent counts without the filesystem feature */
  306. if ((flags2 & XFS_DIFLAG2_NREXT64) && !xfs_has_large_extent_counts(mp))
  307. goto bad;
  308. return;
  309. bad:
  310. xchk_ino_set_corrupt(sc, ino);
  311. }
  312. static inline void
  313. xchk_dinode_nsec(
  314. struct xfs_scrub *sc,
  315. xfs_ino_t ino,
  316. struct xfs_dinode *dip,
  317. const xfs_timestamp_t ts)
  318. {
  319. struct timespec64 tv;
  320. tv = xfs_inode_from_disk_ts(dip, ts);
  321. if (tv.tv_nsec < 0 || tv.tv_nsec >= NSEC_PER_SEC)
  322. xchk_ino_set_corrupt(sc, ino);
  323. }
  324. /* Scrub all the ondisk inode fields. */
  325. STATIC void
  326. xchk_dinode(
  327. struct xfs_scrub *sc,
  328. struct xfs_dinode *dip,
  329. xfs_ino_t ino)
  330. {
  331. struct xfs_mount *mp = sc->mp;
  332. size_t fork_recs;
  333. unsigned long long isize;
  334. uint64_t flags2;
  335. xfs_extnum_t nextents;
  336. xfs_extnum_t naextents;
  337. prid_t prid;
  338. uint16_t flags;
  339. uint16_t mode;
  340. flags = be16_to_cpu(dip->di_flags);
  341. if (dip->di_version >= 3)
  342. flags2 = be64_to_cpu(dip->di_flags2);
  343. else
  344. flags2 = 0;
  345. /* di_mode */
  346. mode = be16_to_cpu(dip->di_mode);
  347. switch (mode & S_IFMT) {
  348. case S_IFLNK:
  349. case S_IFREG:
  350. case S_IFDIR:
  351. case S_IFCHR:
  352. case S_IFBLK:
  353. case S_IFIFO:
  354. case S_IFSOCK:
  355. /* mode is recognized */
  356. break;
  357. default:
  358. xchk_ino_set_corrupt(sc, ino);
  359. break;
  360. }
  361. /* v1/v2 fields */
  362. switch (dip->di_version) {
  363. case 1:
  364. /*
  365. * We autoconvert v1 inodes into v2 inodes on writeout,
  366. * so just mark this inode for preening.
  367. */
  368. xchk_ino_set_preen(sc, ino);
  369. prid = 0;
  370. break;
  371. case 2:
  372. case 3:
  373. if (dip->di_onlink != 0)
  374. xchk_ino_set_corrupt(sc, ino);
  375. if (dip->di_mode == 0 && sc->ip)
  376. xchk_ino_set_corrupt(sc, ino);
  377. if (dip->di_projid_hi != 0 &&
  378. !xfs_has_projid32(mp))
  379. xchk_ino_set_corrupt(sc, ino);
  380. prid = be16_to_cpu(dip->di_projid_lo);
  381. break;
  382. default:
  383. xchk_ino_set_corrupt(sc, ino);
  384. return;
  385. }
  386. if (xfs_has_projid32(mp))
  387. prid |= (prid_t)be16_to_cpu(dip->di_projid_hi) << 16;
  388. /*
  389. * di_uid/di_gid -- -1 isn't invalid, but there's no way that
  390. * userspace could have created that.
  391. */
  392. if (dip->di_uid == cpu_to_be32(-1U) ||
  393. dip->di_gid == cpu_to_be32(-1U))
  394. xchk_ino_set_warning(sc, ino);
  395. /*
  396. * project id of -1 isn't supposed to be valid, but the kernel didn't
  397. * always validate that.
  398. */
  399. if (prid == -1U)
  400. xchk_ino_set_warning(sc, ino);
  401. /* di_format */
  402. switch (dip->di_format) {
  403. case XFS_DINODE_FMT_DEV:
  404. if (!S_ISCHR(mode) && !S_ISBLK(mode) &&
  405. !S_ISFIFO(mode) && !S_ISSOCK(mode))
  406. xchk_ino_set_corrupt(sc, ino);
  407. break;
  408. case XFS_DINODE_FMT_LOCAL:
  409. if (!S_ISDIR(mode) && !S_ISLNK(mode))
  410. xchk_ino_set_corrupt(sc, ino);
  411. break;
  412. case XFS_DINODE_FMT_EXTENTS:
  413. if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode))
  414. xchk_ino_set_corrupt(sc, ino);
  415. break;
  416. case XFS_DINODE_FMT_BTREE:
  417. if (!S_ISREG(mode) && !S_ISDIR(mode))
  418. xchk_ino_set_corrupt(sc, ino);
  419. break;
  420. case XFS_DINODE_FMT_UUID:
  421. default:
  422. xchk_ino_set_corrupt(sc, ino);
  423. break;
  424. }
  425. /* di_[amc]time.nsec */
  426. xchk_dinode_nsec(sc, ino, dip, dip->di_atime);
  427. xchk_dinode_nsec(sc, ino, dip, dip->di_mtime);
  428. xchk_dinode_nsec(sc, ino, dip, dip->di_ctime);
  429. /*
  430. * di_size. xfs_dinode_verify checks for things that screw up
  431. * the VFS such as the upper bit being set and zero-length
  432. * symlinks/directories, but we can do more here.
  433. */
  434. isize = be64_to_cpu(dip->di_size);
  435. if (isize & (1ULL << 63))
  436. xchk_ino_set_corrupt(sc, ino);
  437. /* Devices, fifos, and sockets must have zero size */
  438. if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0)
  439. xchk_ino_set_corrupt(sc, ino);
  440. /* Directories can't be larger than the data section size (32G) */
  441. if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE))
  442. xchk_ino_set_corrupt(sc, ino);
  443. /* Symlinks can't be larger than SYMLINK_MAXLEN */
  444. if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN))
  445. xchk_ino_set_corrupt(sc, ino);
  446. /*
  447. * Warn if the running kernel can't handle the kinds of offsets
  448. * needed to deal with the file size. In other words, if the
  449. * pagecache can't cache all the blocks in this file due to
  450. * overly large offsets, flag the inode for admin review.
  451. */
  452. if (isize > mp->m_super->s_maxbytes)
  453. xchk_ino_set_warning(sc, ino);
  454. /* di_nblocks */
  455. if (flags2 & XFS_DIFLAG2_REFLINK) {
  456. ; /* nblocks can exceed dblocks */
  457. } else if (flags & XFS_DIFLAG_REALTIME) {
  458. /*
  459. * nblocks is the sum of data extents (in the rtdev),
  460. * attr extents (in the datadev), and both forks' bmbt
  461. * blocks (in the datadev). This clumsy check is the
  462. * best we can do without cross-referencing with the
  463. * inode forks.
  464. */
  465. if (be64_to_cpu(dip->di_nblocks) >=
  466. mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks)
  467. xchk_ino_set_corrupt(sc, ino);
  468. } else {
  469. if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks)
  470. xchk_ino_set_corrupt(sc, ino);
  471. }
  472. xchk_inode_flags(sc, dip, ino, mode, flags);
  473. xchk_inode_extsize(sc, dip, ino, mode, flags);
  474. nextents = xfs_dfork_data_extents(dip);
  475. naextents = xfs_dfork_attr_extents(dip);
  476. /* di_nextents */
  477. fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
  478. switch (dip->di_format) {
  479. case XFS_DINODE_FMT_EXTENTS:
  480. if (nextents > fork_recs)
  481. xchk_ino_set_corrupt(sc, ino);
  482. break;
  483. case XFS_DINODE_FMT_BTREE:
  484. if (nextents <= fork_recs)
  485. xchk_ino_set_corrupt(sc, ino);
  486. break;
  487. default:
  488. if (nextents != 0)
  489. xchk_ino_set_corrupt(sc, ino);
  490. break;
  491. }
  492. /* di_forkoff */
  493. if (XFS_DFORK_BOFF(dip) >= mp->m_sb.sb_inodesize)
  494. xchk_ino_set_corrupt(sc, ino);
  495. if (naextents != 0 && dip->di_forkoff == 0)
  496. xchk_ino_set_corrupt(sc, ino);
  497. if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS)
  498. xchk_ino_set_corrupt(sc, ino);
  499. /* di_aformat */
  500. if (dip->di_aformat != XFS_DINODE_FMT_LOCAL &&
  501. dip->di_aformat != XFS_DINODE_FMT_EXTENTS &&
  502. dip->di_aformat != XFS_DINODE_FMT_BTREE)
  503. xchk_ino_set_corrupt(sc, ino);
  504. /* di_anextents */
  505. fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec);
  506. switch (dip->di_aformat) {
  507. case XFS_DINODE_FMT_EXTENTS:
  508. if (naextents > fork_recs)
  509. xchk_ino_set_corrupt(sc, ino);
  510. break;
  511. case XFS_DINODE_FMT_BTREE:
  512. if (naextents <= fork_recs)
  513. xchk_ino_set_corrupt(sc, ino);
  514. break;
  515. default:
  516. if (naextents != 0)
  517. xchk_ino_set_corrupt(sc, ino);
  518. }
  519. if (dip->di_version >= 3) {
  520. xchk_dinode_nsec(sc, ino, dip, dip->di_crtime);
  521. xchk_inode_flags2(sc, dip, ino, mode, flags, flags2);
  522. xchk_inode_cowextsize(sc, dip, ino, mode, flags,
  523. flags2);
  524. }
  525. }
  526. /*
  527. * Make sure the finobt doesn't think this inode is free.
  528. * We don't have to check the inobt ourselves because we got the inode via
  529. * IGET_UNTRUSTED, which checks the inobt for us.
  530. */
  531. static void
  532. xchk_inode_xref_finobt(
  533. struct xfs_scrub *sc,
  534. xfs_ino_t ino)
  535. {
  536. struct xfs_inobt_rec_incore rec;
  537. xfs_agino_t agino;
  538. int has_record;
  539. int error;
  540. if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
  541. return;
  542. agino = XFS_INO_TO_AGINO(sc->mp, ino);
  543. /*
  544. * Try to get the finobt record. If we can't get it, then we're
  545. * in good shape.
  546. */
  547. error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE,
  548. &has_record);
  549. if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
  550. !has_record)
  551. return;
  552. error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record);
  553. if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) ||
  554. !has_record)
  555. return;
  556. /*
  557. * Otherwise, make sure this record either doesn't cover this inode,
  558. * or that it does but it's marked present.
  559. */
  560. if (rec.ir_startino > agino ||
  561. rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
  562. return;
  563. if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))
  564. xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0);
  565. }
  566. /* Cross reference the inode fields with the forks. */
  567. STATIC void
  568. xchk_inode_xref_bmap(
  569. struct xfs_scrub *sc,
  570. struct xfs_dinode *dip)
  571. {
  572. xfs_extnum_t nextents;
  573. xfs_filblks_t count;
  574. xfs_filblks_t acount;
  575. int error;
  576. if (xchk_skip_xref(sc->sm))
  577. return;
  578. /* Walk all the extents to check nextents/naextents/nblocks. */
  579. error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK,
  580. &nextents, &count);
  581. if (!xchk_should_check_xref(sc, &error, NULL))
  582. return;
  583. if (nextents < xfs_dfork_data_extents(dip))
  584. xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
  585. error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK,
  586. &nextents, &acount);
  587. if (!xchk_should_check_xref(sc, &error, NULL))
  588. return;
  589. if (nextents != xfs_dfork_attr_extents(dip))
  590. xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
  591. /* Check nblocks against the inode. */
  592. if (count + acount != be64_to_cpu(dip->di_nblocks))
  593. xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino);
  594. }
  595. /* Cross-reference with the other btrees. */
  596. STATIC void
  597. xchk_inode_xref(
  598. struct xfs_scrub *sc,
  599. xfs_ino_t ino,
  600. struct xfs_dinode *dip)
  601. {
  602. xfs_agnumber_t agno;
  603. xfs_agblock_t agbno;
  604. int error;
  605. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  606. return;
  607. agno = XFS_INO_TO_AGNO(sc->mp, ino);
  608. agbno = XFS_INO_TO_AGBNO(sc->mp, ino);
  609. error = xchk_ag_init_existing(sc, agno, &sc->sa);
  610. if (!xchk_xref_process_error(sc, agno, agbno, &error))
  611. goto out_free;
  612. xchk_xref_is_used_space(sc, agbno, 1);
  613. xchk_inode_xref_finobt(sc, ino);
  614. xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES);
  615. xchk_xref_is_not_shared(sc, agbno, 1);
  616. xchk_xref_is_not_cow_staging(sc, agbno, 1);
  617. xchk_inode_xref_bmap(sc, dip);
  618. out_free:
  619. xchk_ag_free(sc, &sc->sa);
  620. }
  621. /*
  622. * If the reflink iflag disagrees with a scan for shared data fork extents,
  623. * either flag an error (shared extents w/ no flag) or a preen (flag set w/o
  624. * any shared extents). We already checked for reflink iflag set on a non
  625. * reflink filesystem.
  626. */
  627. static void
  628. xchk_inode_check_reflink_iflag(
  629. struct xfs_scrub *sc,
  630. xfs_ino_t ino)
  631. {
  632. struct xfs_mount *mp = sc->mp;
  633. bool has_shared;
  634. int error;
  635. if (!xfs_has_reflink(mp))
  636. return;
  637. error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
  638. &has_shared);
  639. if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino),
  640. XFS_INO_TO_AGBNO(mp, ino), &error))
  641. return;
  642. if (xfs_is_reflink_inode(sc->ip) && !has_shared)
  643. xchk_ino_set_preen(sc, ino);
  644. else if (!xfs_is_reflink_inode(sc->ip) && has_shared)
  645. xchk_ino_set_corrupt(sc, ino);
  646. }
  647. /*
  648. * If this inode has zero link count, it must be on the unlinked list. If
  649. * it has nonzero link count, it must not be on the unlinked list.
  650. */
  651. STATIC void
  652. xchk_inode_check_unlinked(
  653. struct xfs_scrub *sc)
  654. {
  655. if (VFS_I(sc->ip)->i_nlink == 0) {
  656. if (!xfs_inode_on_unlinked_list(sc->ip))
  657. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  658. } else {
  659. if (xfs_inode_on_unlinked_list(sc->ip))
  660. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  661. }
  662. }
  663. /* Scrub an inode. */
  664. int
  665. xchk_inode(
  666. struct xfs_scrub *sc)
  667. {
  668. struct xfs_dinode di;
  669. int error = 0;
  670. /*
  671. * If sc->ip is NULL, that means that the setup function called
  672. * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED
  673. * and a NULL inode, so flag the corruption error and return.
  674. */
  675. if (!sc->ip) {
  676. xchk_ino_set_corrupt(sc, sc->sm->sm_ino);
  677. return 0;
  678. }
  679. /* Scrub the inode core. */
  680. xfs_inode_to_disk(sc->ip, &di, 0);
  681. xchk_dinode(sc, &di, sc->ip->i_ino);
  682. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  683. goto out;
  684. /*
  685. * Look for discrepancies between file's data blocks and the reflink
  686. * iflag. We already checked the iflag against the file mode when
  687. * we scrubbed the dinode.
  688. */
  689. if (S_ISREG(VFS_I(sc->ip)->i_mode))
  690. xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino);
  691. xchk_inode_check_unlinked(sc);
  692. xchk_inode_xref(sc, sc->ip->i_ino, &di);
  693. out:
  694. return error;
  695. }