inode.c 22 KB

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