xfs_inode_buf.c 21 KB

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