xfs_inode_buf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_ag.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_errortag.h"
  16. #include "xfs_error.h"
  17. #include "xfs_icache.h"
  18. #include "xfs_trans.h"
  19. #include "xfs_ialloc.h"
  20. #include "xfs_dir2.h"
  21. #include "xfs_health.h"
  22. #include <linux/iversion.h>
  23. /*
  24. * If we are doing readahead on an inode buffer, we might be in log recovery
  25. * reading an inode allocation buffer that hasn't yet been replayed, and hence
  26. * has not had the inode cores stamped into it. Hence for readahead, the buffer
  27. * may be potentially invalid.
  28. *
  29. * If the readahead buffer is invalid, we need to mark it with an error and
  30. * clear the DONE status of the buffer so that a followup read will re-read it
  31. * from disk. We don't report the error otherwise to avoid warnings during log
  32. * recovery and we don't get unnecessary panics on debug kernels. We use EIO here
  33. * because all we want to do is say readahead failed; there is no-one to report
  34. * the error to, so this will distinguish it from a non-ra verifier failure.
  35. * Changes to this readahead error behaviour also need to be reflected in
  36. * xfs_dquot_buf_readahead_verify().
  37. */
  38. static void
  39. xfs_inode_buf_verify(
  40. struct xfs_buf *bp,
  41. bool readahead)
  42. {
  43. struct xfs_mount *mp = bp->b_mount;
  44. int i;
  45. int ni;
  46. /*
  47. * Validate the magic number and version of every inode in the buffer
  48. */
  49. ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
  50. for (i = 0; i < ni; i++) {
  51. struct xfs_dinode *dip;
  52. xfs_agino_t unlinked_ino;
  53. int di_ok;
  54. dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
  55. unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
  56. di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
  57. xfs_dinode_good_version(mp, dip->di_version) &&
  58. xfs_verify_agino_or_null(bp->b_pag, unlinked_ino);
  59. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  60. XFS_ERRTAG_ITOBP_INOTOBP))) {
  61. if (readahead) {
  62. bp->b_flags &= ~XBF_DONE;
  63. xfs_buf_ioerror(bp, -EIO);
  64. return;
  65. }
  66. #ifdef DEBUG
  67. xfs_alert(mp,
  68. "bad inode magic/vsn daddr %lld #%d (magic=%x)",
  69. (unsigned long long)xfs_buf_daddr(bp), i,
  70. be16_to_cpu(dip->di_magic));
  71. #endif
  72. xfs_buf_verifier_error(bp, -EFSCORRUPTED,
  73. __func__, dip, sizeof(*dip),
  74. NULL);
  75. return;
  76. }
  77. }
  78. }
  79. static void
  80. xfs_inode_buf_read_verify(
  81. struct xfs_buf *bp)
  82. {
  83. xfs_inode_buf_verify(bp, false);
  84. }
  85. static void
  86. xfs_inode_buf_readahead_verify(
  87. struct xfs_buf *bp)
  88. {
  89. xfs_inode_buf_verify(bp, true);
  90. }
  91. static void
  92. xfs_inode_buf_write_verify(
  93. struct xfs_buf *bp)
  94. {
  95. xfs_inode_buf_verify(bp, false);
  96. }
  97. const struct xfs_buf_ops xfs_inode_buf_ops = {
  98. .name = "xfs_inode",
  99. .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
  100. cpu_to_be16(XFS_DINODE_MAGIC) },
  101. .verify_read = xfs_inode_buf_read_verify,
  102. .verify_write = xfs_inode_buf_write_verify,
  103. };
  104. const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
  105. .name = "xfs_inode_ra",
  106. .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
  107. cpu_to_be16(XFS_DINODE_MAGIC) },
  108. .verify_read = xfs_inode_buf_readahead_verify,
  109. .verify_write = xfs_inode_buf_write_verify,
  110. };
  111. /*
  112. * This routine is called to map an inode to the buffer containing the on-disk
  113. * version of the inode. It returns a pointer to the buffer containing the
  114. * on-disk inode in the bpp parameter.
  115. */
  116. int
  117. xfs_imap_to_bp(
  118. struct xfs_mount *mp,
  119. struct xfs_trans *tp,
  120. struct xfs_imap *imap,
  121. struct xfs_buf **bpp)
  122. {
  123. int error;
  124. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
  125. imap->im_len, XBF_UNMAPPED, bpp, &xfs_inode_buf_ops);
  126. if (xfs_metadata_is_sick(error))
  127. xfs_agno_mark_sick(mp, xfs_daddr_to_agno(mp, imap->im_blkno),
  128. XFS_SICK_AG_INODES);
  129. return error;
  130. }
  131. static inline struct timespec64 xfs_inode_decode_bigtime(uint64_t ts)
  132. {
  133. struct timespec64 tv;
  134. uint32_t n;
  135. tv.tv_sec = xfs_bigtime_to_unix(div_u64_rem(ts, NSEC_PER_SEC, &n));
  136. tv.tv_nsec = n;
  137. return tv;
  138. }
  139. /* Convert an ondisk timestamp to an incore timestamp. */
  140. struct timespec64
  141. xfs_inode_from_disk_ts(
  142. struct xfs_dinode *dip,
  143. const xfs_timestamp_t ts)
  144. {
  145. struct timespec64 tv;
  146. struct xfs_legacy_timestamp *lts;
  147. if (xfs_dinode_has_bigtime(dip))
  148. return xfs_inode_decode_bigtime(be64_to_cpu(ts));
  149. lts = (struct xfs_legacy_timestamp *)&ts;
  150. tv.tv_sec = (int)be32_to_cpu(lts->t_sec);
  151. tv.tv_nsec = (int)be32_to_cpu(lts->t_nsec);
  152. return tv;
  153. }
  154. int
  155. xfs_inode_from_disk(
  156. struct xfs_inode *ip,
  157. struct xfs_dinode *from)
  158. {
  159. struct inode *inode = VFS_I(ip);
  160. int error;
  161. xfs_failaddr_t fa;
  162. ASSERT(ip->i_cowfp == NULL);
  163. fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
  164. if (fa) {
  165. xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", from,
  166. sizeof(*from), fa);
  167. return -EFSCORRUPTED;
  168. }
  169. /*
  170. * First get the permanent information that is needed to allocate an
  171. * inode. If the inode is unused, mode is zero and we shouldn't mess
  172. * with the uninitialized part of it.
  173. */
  174. if (!xfs_has_v3inodes(ip->i_mount))
  175. ip->i_flushiter = be16_to_cpu(from->di_flushiter);
  176. inode->i_generation = be32_to_cpu(from->di_gen);
  177. inode->i_mode = be16_to_cpu(from->di_mode);
  178. if (!inode->i_mode)
  179. return 0;
  180. /*
  181. * Convert v1 inodes immediately to v2 inode format as this is the
  182. * minimum inode version format we support in the rest of the code.
  183. * They will also be unconditionally written back to disk as v2 inodes.
  184. */
  185. if (unlikely(from->di_version == 1)) {
  186. set_nlink(inode, be16_to_cpu(from->di_onlink));
  187. ip->i_projid = 0;
  188. } else {
  189. set_nlink(inode, be32_to_cpu(from->di_nlink));
  190. ip->i_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
  191. be16_to_cpu(from->di_projid_lo);
  192. }
  193. i_uid_write(inode, be32_to_cpu(from->di_uid));
  194. i_gid_write(inode, be32_to_cpu(from->di_gid));
  195. /*
  196. * Time is signed, so need to convert to signed 32 bit before
  197. * storing in inode timestamp which may be 64 bit. Otherwise
  198. * a time before epoch is converted to a time long after epoch
  199. * on 64 bit systems.
  200. */
  201. inode_set_atime_to_ts(inode,
  202. xfs_inode_from_disk_ts(from, from->di_atime));
  203. inode_set_mtime_to_ts(inode,
  204. xfs_inode_from_disk_ts(from, from->di_mtime));
  205. inode_set_ctime_to_ts(inode,
  206. xfs_inode_from_disk_ts(from, from->di_ctime));
  207. ip->i_disk_size = be64_to_cpu(from->di_size);
  208. ip->i_nblocks = be64_to_cpu(from->di_nblocks);
  209. ip->i_extsize = be32_to_cpu(from->di_extsize);
  210. ip->i_forkoff = from->di_forkoff;
  211. ip->i_diflags = be16_to_cpu(from->di_flags);
  212. ip->i_next_unlinked = be32_to_cpu(from->di_next_unlinked);
  213. if (from->di_dmevmask || from->di_dmstate)
  214. xfs_iflags_set(ip, XFS_IPRESERVE_DM_FIELDS);
  215. if (xfs_has_v3inodes(ip->i_mount)) {
  216. inode_set_iversion_queried(inode,
  217. be64_to_cpu(from->di_changecount));
  218. ip->i_crtime = xfs_inode_from_disk_ts(from, from->di_crtime);
  219. ip->i_diflags2 = be64_to_cpu(from->di_flags2);
  220. ip->i_cowextsize = be32_to_cpu(from->di_cowextsize);
  221. }
  222. error = xfs_iformat_data_fork(ip, from);
  223. if (error)
  224. return error;
  225. if (from->di_forkoff) {
  226. error = xfs_iformat_attr_fork(ip, from);
  227. if (error)
  228. goto out_destroy_data_fork;
  229. }
  230. if (xfs_is_reflink_inode(ip))
  231. xfs_ifork_init_cow(ip);
  232. return 0;
  233. out_destroy_data_fork:
  234. xfs_idestroy_fork(&ip->i_df);
  235. return error;
  236. }
  237. /* Convert an incore timestamp to an ondisk timestamp. */
  238. static inline xfs_timestamp_t
  239. xfs_inode_to_disk_ts(
  240. struct xfs_inode *ip,
  241. const struct timespec64 tv)
  242. {
  243. struct xfs_legacy_timestamp *lts;
  244. xfs_timestamp_t ts;
  245. if (xfs_inode_has_bigtime(ip))
  246. return cpu_to_be64(xfs_inode_encode_bigtime(tv));
  247. lts = (struct xfs_legacy_timestamp *)&ts;
  248. lts->t_sec = cpu_to_be32(tv.tv_sec);
  249. lts->t_nsec = cpu_to_be32(tv.tv_nsec);
  250. return ts;
  251. }
  252. static inline void
  253. xfs_inode_to_disk_iext_counters(
  254. struct xfs_inode *ip,
  255. struct xfs_dinode *to)
  256. {
  257. if (xfs_inode_has_large_extent_counts(ip)) {
  258. to->di_big_nextents = cpu_to_be64(xfs_ifork_nextents(&ip->i_df));
  259. to->di_big_anextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_af));
  260. /*
  261. * We might be upgrading the inode to use larger extent counters
  262. * than was previously used. Hence zero the unused field.
  263. */
  264. to->di_nrext64_pad = cpu_to_be16(0);
  265. } else {
  266. to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
  267. to->di_anextents = cpu_to_be16(xfs_ifork_nextents(&ip->i_af));
  268. }
  269. }
  270. void
  271. xfs_inode_to_disk(
  272. struct xfs_inode *ip,
  273. struct xfs_dinode *to,
  274. xfs_lsn_t lsn)
  275. {
  276. struct inode *inode = VFS_I(ip);
  277. to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
  278. to->di_onlink = 0;
  279. to->di_format = xfs_ifork_format(&ip->i_df);
  280. to->di_uid = cpu_to_be32(i_uid_read(inode));
  281. to->di_gid = cpu_to_be32(i_gid_read(inode));
  282. to->di_projid_lo = cpu_to_be16(ip->i_projid & 0xffff);
  283. to->di_projid_hi = cpu_to_be16(ip->i_projid >> 16);
  284. to->di_atime = xfs_inode_to_disk_ts(ip, inode_get_atime(inode));
  285. to->di_mtime = xfs_inode_to_disk_ts(ip, inode_get_mtime(inode));
  286. to->di_ctime = xfs_inode_to_disk_ts(ip, inode_get_ctime(inode));
  287. to->di_nlink = cpu_to_be32(inode->i_nlink);
  288. to->di_gen = cpu_to_be32(inode->i_generation);
  289. to->di_mode = cpu_to_be16(inode->i_mode);
  290. to->di_size = cpu_to_be64(ip->i_disk_size);
  291. to->di_nblocks = cpu_to_be64(ip->i_nblocks);
  292. to->di_extsize = cpu_to_be32(ip->i_extsize);
  293. to->di_forkoff = ip->i_forkoff;
  294. to->di_aformat = xfs_ifork_format(&ip->i_af);
  295. to->di_flags = cpu_to_be16(ip->i_diflags);
  296. if (xfs_has_v3inodes(ip->i_mount)) {
  297. to->di_version = 3;
  298. to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
  299. to->di_crtime = xfs_inode_to_disk_ts(ip, ip->i_crtime);
  300. to->di_flags2 = cpu_to_be64(ip->i_diflags2);
  301. to->di_cowextsize = cpu_to_be32(ip->i_cowextsize);
  302. to->di_ino = cpu_to_be64(ip->i_ino);
  303. to->di_lsn = cpu_to_be64(lsn);
  304. memset(to->di_pad2, 0, sizeof(to->di_pad2));
  305. uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
  306. to->di_v3_pad = 0;
  307. } else {
  308. to->di_version = 2;
  309. to->di_flushiter = cpu_to_be16(ip->i_flushiter);
  310. memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
  311. }
  312. xfs_inode_to_disk_iext_counters(ip, to);
  313. }
  314. static xfs_failaddr_t
  315. xfs_dinode_verify_fork(
  316. struct xfs_dinode *dip,
  317. struct xfs_mount *mp,
  318. int whichfork)
  319. {
  320. xfs_extnum_t di_nextents;
  321. xfs_extnum_t max_extents;
  322. mode_t mode = be16_to_cpu(dip->di_mode);
  323. uint32_t fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
  324. uint32_t fork_format = XFS_DFORK_FORMAT(dip, whichfork);
  325. di_nextents = xfs_dfork_nextents(dip, whichfork);
  326. /*
  327. * For fork types that can contain local data, check that the fork
  328. * format matches the size of local data contained within the fork.
  329. */
  330. if (whichfork == XFS_DATA_FORK) {
  331. /*
  332. * A directory small enough to fit in the inode must be stored
  333. * in local format. The directory sf <-> extents conversion
  334. * code updates the directory size accordingly. Directories
  335. * being truncated have zero size and are not subject to this
  336. * check.
  337. */
  338. if (S_ISDIR(mode)) {
  339. if (dip->di_size &&
  340. be64_to_cpu(dip->di_size) <= fork_size &&
  341. fork_format != XFS_DINODE_FMT_LOCAL)
  342. return __this_address;
  343. }
  344. /*
  345. * A symlink with a target small enough to fit in the inode can
  346. * be stored in extents format if xattrs were added (thus
  347. * converting the data fork from shortform to remote format)
  348. * and then removed.
  349. */
  350. if (S_ISLNK(mode)) {
  351. if (be64_to_cpu(dip->di_size) <= fork_size &&
  352. fork_format != XFS_DINODE_FMT_EXTENTS &&
  353. fork_format != XFS_DINODE_FMT_LOCAL)
  354. return __this_address;
  355. }
  356. /*
  357. * For all types, check that when the size says the fork should
  358. * be in extent or btree format, the inode isn't claiming to be
  359. * in local format.
  360. */
  361. if (be64_to_cpu(dip->di_size) > fork_size &&
  362. fork_format == XFS_DINODE_FMT_LOCAL)
  363. return __this_address;
  364. }
  365. switch (fork_format) {
  366. case XFS_DINODE_FMT_LOCAL:
  367. /*
  368. * No local regular files yet.
  369. */
  370. if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
  371. return __this_address;
  372. if (di_nextents)
  373. return __this_address;
  374. break;
  375. case XFS_DINODE_FMT_EXTENTS:
  376. if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
  377. return __this_address;
  378. break;
  379. case XFS_DINODE_FMT_BTREE:
  380. max_extents = xfs_iext_max_nextents(
  381. xfs_dinode_has_large_extent_counts(dip),
  382. whichfork);
  383. if (di_nextents > max_extents)
  384. return __this_address;
  385. break;
  386. default:
  387. return __this_address;
  388. }
  389. return NULL;
  390. }
  391. static xfs_failaddr_t
  392. xfs_dinode_verify_forkoff(
  393. struct xfs_dinode *dip,
  394. struct xfs_mount *mp)
  395. {
  396. if (!dip->di_forkoff)
  397. return NULL;
  398. switch (dip->di_format) {
  399. case XFS_DINODE_FMT_DEV:
  400. if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
  401. return __this_address;
  402. break;
  403. case XFS_DINODE_FMT_LOCAL: /* fall through ... */
  404. case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
  405. case XFS_DINODE_FMT_BTREE:
  406. if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
  407. return __this_address;
  408. break;
  409. default:
  410. return __this_address;
  411. }
  412. return NULL;
  413. }
  414. static xfs_failaddr_t
  415. xfs_dinode_verify_nrext64(
  416. struct xfs_mount *mp,
  417. struct xfs_dinode *dip)
  418. {
  419. if (xfs_dinode_has_large_extent_counts(dip)) {
  420. if (!xfs_has_large_extent_counts(mp))
  421. return __this_address;
  422. if (dip->di_nrext64_pad != 0)
  423. return __this_address;
  424. } else if (dip->di_version >= 3) {
  425. if (dip->di_v3_pad != 0)
  426. return __this_address;
  427. }
  428. return NULL;
  429. }
  430. xfs_failaddr_t
  431. xfs_dinode_verify(
  432. struct xfs_mount *mp,
  433. xfs_ino_t ino,
  434. struct xfs_dinode *dip)
  435. {
  436. xfs_failaddr_t fa;
  437. uint16_t mode;
  438. uint16_t flags;
  439. uint64_t flags2;
  440. uint64_t di_size;
  441. xfs_extnum_t nextents;
  442. xfs_extnum_t naextents;
  443. xfs_filblks_t nblocks;
  444. if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
  445. return __this_address;
  446. /* Verify v3 integrity information first */
  447. if (dip->di_version >= 3) {
  448. if (!xfs_has_v3inodes(mp))
  449. return __this_address;
  450. if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
  451. XFS_DINODE_CRC_OFF))
  452. return __this_address;
  453. if (be64_to_cpu(dip->di_ino) != ino)
  454. return __this_address;
  455. if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
  456. return __this_address;
  457. }
  458. /*
  459. * Historical note: xfsprogs in the 3.2 era set up its incore inodes to
  460. * have di_nlink track the link count, even if the actual filesystem
  461. * only supported V1 inodes (i.e. di_onlink). When writing out the
  462. * ondisk inode, it would set both the ondisk di_nlink and di_onlink to
  463. * the the incore di_nlink value, which is why we cannot check for
  464. * di_nlink==0 on a V1 inode. V2/3 inodes would get written out with
  465. * di_onlink==0, so we can check that.
  466. */
  467. if (dip->di_version >= 2) {
  468. if (dip->di_onlink)
  469. return __this_address;
  470. }
  471. /* don't allow invalid i_size */
  472. di_size = be64_to_cpu(dip->di_size);
  473. if (di_size & (1ULL << 63))
  474. return __this_address;
  475. mode = be16_to_cpu(dip->di_mode);
  476. if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
  477. return __this_address;
  478. /*
  479. * No zero-length symlinks/dirs unless they're unlinked and hence being
  480. * inactivated.
  481. */
  482. if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0) {
  483. if (dip->di_version > 1) {
  484. if (dip->di_nlink)
  485. return __this_address;
  486. } else {
  487. if (dip->di_onlink)
  488. return __this_address;
  489. }
  490. }
  491. fa = xfs_dinode_verify_nrext64(mp, dip);
  492. if (fa)
  493. return fa;
  494. nextents = xfs_dfork_data_extents(dip);
  495. naextents = xfs_dfork_attr_extents(dip);
  496. nblocks = be64_to_cpu(dip->di_nblocks);
  497. /* Fork checks carried over from xfs_iformat_fork */
  498. if (mode && nextents + naextents > nblocks)
  499. return __this_address;
  500. if (nextents + naextents == 0 && nblocks != 0)
  501. return __this_address;
  502. if (S_ISDIR(mode) && nextents > mp->m_dir_geo->max_extents)
  503. return __this_address;
  504. if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
  505. return __this_address;
  506. flags = be16_to_cpu(dip->di_flags);
  507. if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
  508. return __this_address;
  509. /* check for illegal values of forkoff */
  510. fa = xfs_dinode_verify_forkoff(dip, mp);
  511. if (fa)
  512. return fa;
  513. /* Do we have appropriate data fork formats for the mode? */
  514. switch (mode & S_IFMT) {
  515. case S_IFIFO:
  516. case S_IFCHR:
  517. case S_IFBLK:
  518. case S_IFSOCK:
  519. if (dip->di_format != XFS_DINODE_FMT_DEV)
  520. return __this_address;
  521. break;
  522. case S_IFREG:
  523. case S_IFLNK:
  524. case S_IFDIR:
  525. fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
  526. if (fa)
  527. return fa;
  528. break;
  529. case 0:
  530. /* Uninitialized inode ok. */
  531. break;
  532. default:
  533. return __this_address;
  534. }
  535. if (dip->di_forkoff) {
  536. fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
  537. if (fa)
  538. return fa;
  539. } else {
  540. /*
  541. * If there is no fork offset, this may be a freshly-made inode
  542. * in a new disk cluster, in which case di_aformat is zeroed.
  543. * Otherwise, such an inode must be in EXTENTS format; this goes
  544. * for freed inodes as well.
  545. */
  546. switch (dip->di_aformat) {
  547. case 0:
  548. case XFS_DINODE_FMT_EXTENTS:
  549. break;
  550. default:
  551. return __this_address;
  552. }
  553. if (naextents)
  554. return __this_address;
  555. }
  556. /* extent size hint validation */
  557. fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
  558. mode, flags);
  559. if (fa)
  560. return fa;
  561. /* only version 3 or greater inodes are extensively verified here */
  562. if (dip->di_version < 3)
  563. return NULL;
  564. flags2 = be64_to_cpu(dip->di_flags2);
  565. /* don't allow reflink/cowextsize if we don't have reflink */
  566. if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
  567. !xfs_has_reflink(mp))
  568. return __this_address;
  569. /* only regular files get reflink */
  570. if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
  571. return __this_address;
  572. /* don't let reflink and realtime mix */
  573. if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
  574. return __this_address;
  575. /* COW extent size hint validation */
  576. fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
  577. mode, flags, flags2);
  578. if (fa)
  579. return fa;
  580. /* bigtime iflag can only happen on bigtime filesystems */
  581. if (xfs_dinode_has_bigtime(dip) &&
  582. !xfs_has_bigtime(mp))
  583. return __this_address;
  584. return NULL;
  585. }
  586. void
  587. xfs_dinode_calc_crc(
  588. struct xfs_mount *mp,
  589. struct xfs_dinode *dip)
  590. {
  591. uint32_t crc;
  592. if (dip->di_version < 3)
  593. return;
  594. ASSERT(xfs_has_crc(mp));
  595. crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
  596. XFS_DINODE_CRC_OFF);
  597. dip->di_crc = xfs_end_cksum(crc);
  598. }
  599. /*
  600. * Validate di_extsize hint.
  601. *
  602. * 1. Extent size hint is only valid for directories and regular files.
  603. * 2. FS_XFLAG_EXTSIZE is only valid for regular files.
  604. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  605. * 4. Hint cannot be larger than MAXTEXTLEN.
  606. * 5. Can be changed on directories at any time.
  607. * 6. Hint value of 0 turns off hints, clears inode flags.
  608. * 7. Extent size must be a multiple of the appropriate block size.
  609. * For realtime files, this is the rt extent size.
  610. * 8. For non-realtime files, the extent size hint must be limited
  611. * to half the AG size to avoid alignment extending the extent beyond the
  612. * limits of the AG.
  613. */
  614. xfs_failaddr_t
  615. xfs_inode_validate_extsize(
  616. struct xfs_mount *mp,
  617. uint32_t extsize,
  618. uint16_t mode,
  619. uint16_t flags)
  620. {
  621. bool rt_flag;
  622. bool hint_flag;
  623. bool inherit_flag;
  624. uint32_t extsize_bytes;
  625. uint32_t blocksize_bytes;
  626. rt_flag = (flags & XFS_DIFLAG_REALTIME);
  627. hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
  628. inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
  629. extsize_bytes = XFS_FSB_TO_B(mp, extsize);
  630. /*
  631. * This comment describes a historic gap in this verifier function.
  632. *
  633. * For a directory with both RTINHERIT and EXTSZINHERIT flags set, this
  634. * function has never checked that the extent size hint is an integer
  635. * multiple of the realtime extent size. Since we allow users to set
  636. * this combination on non-rt filesystems /and/ to change the rt
  637. * extent size when adding a rt device to a filesystem, the net effect
  638. * is that users can configure a filesystem anticipating one rt
  639. * geometry and change their minds later. Directories do not use the
  640. * extent size hint, so this is harmless for them.
  641. *
  642. * If a directory with a misaligned extent size hint is allowed to
  643. * propagate that hint into a new regular realtime file, the result
  644. * is that the inode cluster buffer verifier will trigger a corruption
  645. * shutdown the next time it is run, because the verifier has always
  646. * enforced the alignment rule for regular files.
  647. *
  648. * Because we allow administrators to set a new rt extent size when
  649. * adding a rt section, we cannot add a check to this verifier because
  650. * that will result a new source of directory corruption errors when
  651. * reading an existing filesystem. Instead, we rely on callers to
  652. * decide when alignment checks are appropriate, and fix things up as
  653. * needed.
  654. */
  655. if (rt_flag)
  656. blocksize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
  657. else
  658. blocksize_bytes = mp->m_sb.sb_blocksize;
  659. if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
  660. return __this_address;
  661. if (hint_flag && !S_ISREG(mode))
  662. return __this_address;
  663. if (inherit_flag && !S_ISDIR(mode))
  664. return __this_address;
  665. if ((hint_flag || inherit_flag) && extsize == 0)
  666. return __this_address;
  667. /* free inodes get flags set to zero but extsize remains */
  668. if (mode && !(hint_flag || inherit_flag) && extsize != 0)
  669. return __this_address;
  670. if (extsize_bytes % blocksize_bytes)
  671. return __this_address;
  672. if (extsize > XFS_MAX_BMBT_EXTLEN)
  673. return __this_address;
  674. if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
  675. return __this_address;
  676. return NULL;
  677. }
  678. /*
  679. * Validate di_cowextsize hint.
  680. *
  681. * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
  682. * The inode does not have to have any shared blocks, but it must be a v3.
  683. * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
  684. * for a directory, the hint is propagated to new files.
  685. * 3. Can be changed on files & directories at any time.
  686. * 4. Hint value of 0 turns off hints, clears inode flags.
  687. * 5. Extent size must be a multiple of the appropriate block size.
  688. * 6. The extent size hint must be limited to half the AG size to avoid
  689. * alignment extending the extent beyond the limits of the AG.
  690. */
  691. xfs_failaddr_t
  692. xfs_inode_validate_cowextsize(
  693. struct xfs_mount *mp,
  694. uint32_t cowextsize,
  695. uint16_t mode,
  696. uint16_t flags,
  697. uint64_t flags2)
  698. {
  699. bool rt_flag;
  700. bool hint_flag;
  701. uint32_t cowextsize_bytes;
  702. rt_flag = (flags & XFS_DIFLAG_REALTIME);
  703. hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
  704. cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
  705. if (hint_flag && !xfs_has_reflink(mp))
  706. return __this_address;
  707. if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
  708. return __this_address;
  709. if (hint_flag && cowextsize == 0)
  710. return __this_address;
  711. /* free inodes get flags set to zero but cowextsize remains */
  712. if (mode && !hint_flag && cowextsize != 0)
  713. return __this_address;
  714. if (hint_flag && rt_flag)
  715. return __this_address;
  716. if (cowextsize_bytes % mp->m_sb.sb_blocksize)
  717. return __this_address;
  718. if (cowextsize > XFS_MAX_BMBT_EXTLEN)
  719. return __this_address;
  720. if (cowextsize > mp->m_sb.sb_agblocks / 2)
  721. return __this_address;
  722. return NULL;
  723. }