xfs_inode_buf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_defer.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_error.h"
  28. #include "xfs_cksum.h"
  29. #include "xfs_icache.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_ialloc.h"
  32. /*
  33. * Check that none of the inode's in the buffer have a next
  34. * unlinked field of 0.
  35. */
  36. #if defined(DEBUG)
  37. void
  38. xfs_inobp_check(
  39. xfs_mount_t *mp,
  40. xfs_buf_t *bp)
  41. {
  42. int i;
  43. int j;
  44. xfs_dinode_t *dip;
  45. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  46. for (i = 0; i < j; i++) {
  47. dip = xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize);
  48. if (!dip->di_next_unlinked) {
  49. xfs_alert(mp,
  50. "Detected bogus zero next_unlinked field in inode %d buffer 0x%llx.",
  51. i, (long long)bp->b_bn);
  52. }
  53. }
  54. }
  55. #endif
  56. /*
  57. * If we are doing readahead on an inode buffer, we might be in log recovery
  58. * reading an inode allocation buffer that hasn't yet been replayed, and hence
  59. * has not had the inode cores stamped into it. Hence for readahead, the buffer
  60. * may be potentially invalid.
  61. *
  62. * If the readahead buffer is invalid, we need to mark it with an error and
  63. * clear the DONE status of the buffer so that a followup read will re-read it
  64. * from disk. We don't report the error otherwise to avoid warnings during log
  65. * recovery and we don't get unnecssary panics on debug kernels. We use EIO here
  66. * because all we want to do is say readahead failed; there is no-one to report
  67. * the error to, so this will distinguish it from a non-ra verifier failure.
  68. * Changes to this readahead error behavour also need to be reflected in
  69. * xfs_dquot_buf_readahead_verify().
  70. */
  71. static void
  72. xfs_inode_buf_verify(
  73. struct xfs_buf *bp,
  74. bool readahead)
  75. {
  76. struct xfs_mount *mp = bp->b_target->bt_mount;
  77. int i;
  78. int ni;
  79. /*
  80. * Validate the magic number and version of every inode in the buffer
  81. */
  82. ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
  83. for (i = 0; i < ni; i++) {
  84. int di_ok;
  85. xfs_dinode_t *dip;
  86. dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
  87. di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
  88. XFS_DINODE_GOOD_VERSION(dip->di_version);
  89. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  90. XFS_ERRTAG_ITOBP_INOTOBP,
  91. XFS_RANDOM_ITOBP_INOTOBP))) {
  92. if (readahead) {
  93. bp->b_flags &= ~XBF_DONE;
  94. xfs_buf_ioerror(bp, -EIO);
  95. return;
  96. }
  97. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  98. xfs_verifier_error(bp);
  99. #ifdef DEBUG
  100. xfs_alert(mp,
  101. "bad inode magic/vsn daddr %lld #%d (magic=%x)",
  102. (unsigned long long)bp->b_bn, i,
  103. be16_to_cpu(dip->di_magic));
  104. #endif
  105. }
  106. }
  107. xfs_inobp_check(mp, bp);
  108. }
  109. static void
  110. xfs_inode_buf_read_verify(
  111. struct xfs_buf *bp)
  112. {
  113. xfs_inode_buf_verify(bp, false);
  114. }
  115. static void
  116. xfs_inode_buf_readahead_verify(
  117. struct xfs_buf *bp)
  118. {
  119. xfs_inode_buf_verify(bp, true);
  120. }
  121. static void
  122. xfs_inode_buf_write_verify(
  123. struct xfs_buf *bp)
  124. {
  125. xfs_inode_buf_verify(bp, false);
  126. }
  127. const struct xfs_buf_ops xfs_inode_buf_ops = {
  128. .name = "xfs_inode",
  129. .verify_read = xfs_inode_buf_read_verify,
  130. .verify_write = xfs_inode_buf_write_verify,
  131. };
  132. const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
  133. .name = "xxfs_inode_ra",
  134. .verify_read = xfs_inode_buf_readahead_verify,
  135. .verify_write = xfs_inode_buf_write_verify,
  136. };
  137. /*
  138. * This routine is called to map an inode to the buffer containing the on-disk
  139. * version of the inode. It returns a pointer to the buffer containing the
  140. * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
  141. * pointer to the on-disk inode within that buffer.
  142. *
  143. * If a non-zero error is returned, then the contents of bpp and dipp are
  144. * undefined.
  145. */
  146. int
  147. xfs_imap_to_bp(
  148. struct xfs_mount *mp,
  149. struct xfs_trans *tp,
  150. struct xfs_imap *imap,
  151. struct xfs_dinode **dipp,
  152. struct xfs_buf **bpp,
  153. uint buf_flags,
  154. uint iget_flags)
  155. {
  156. struct xfs_buf *bp;
  157. int error;
  158. buf_flags |= XBF_UNMAPPED;
  159. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
  160. (int)imap->im_len, buf_flags, &bp,
  161. &xfs_inode_buf_ops);
  162. if (error) {
  163. if (error == -EAGAIN) {
  164. ASSERT(buf_flags & XBF_TRYLOCK);
  165. return error;
  166. }
  167. if (error == -EFSCORRUPTED &&
  168. (iget_flags & XFS_IGET_UNTRUSTED))
  169. return -EINVAL;
  170. xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
  171. __func__, error);
  172. return error;
  173. }
  174. *bpp = bp;
  175. *dipp = xfs_buf_offset(bp, imap->im_boffset);
  176. return 0;
  177. }
  178. void
  179. xfs_inode_from_disk(
  180. struct xfs_inode *ip,
  181. struct xfs_dinode *from)
  182. {
  183. struct xfs_icdinode *to = &ip->i_d;
  184. struct inode *inode = VFS_I(ip);
  185. /*
  186. * Convert v1 inodes immediately to v2 inode format as this is the
  187. * minimum inode version format we support in the rest of the code.
  188. */
  189. to->di_version = from->di_version;
  190. if (to->di_version == 1) {
  191. set_nlink(inode, be16_to_cpu(from->di_onlink));
  192. to->di_projid_lo = 0;
  193. to->di_projid_hi = 0;
  194. to->di_version = 2;
  195. } else {
  196. set_nlink(inode, be32_to_cpu(from->di_nlink));
  197. to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
  198. to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
  199. }
  200. to->di_format = from->di_format;
  201. to->di_uid = be32_to_cpu(from->di_uid);
  202. to->di_gid = be32_to_cpu(from->di_gid);
  203. to->di_flushiter = be16_to_cpu(from->di_flushiter);
  204. /*
  205. * Time is signed, so need to convert to signed 32 bit before
  206. * storing in inode timestamp which may be 64 bit. Otherwise
  207. * a time before epoch is converted to a time long after epoch
  208. * on 64 bit systems.
  209. */
  210. inode->i_atime.tv_sec = (int)be32_to_cpu(from->di_atime.t_sec);
  211. inode->i_atime.tv_nsec = (int)be32_to_cpu(from->di_atime.t_nsec);
  212. inode->i_mtime.tv_sec = (int)be32_to_cpu(from->di_mtime.t_sec);
  213. inode->i_mtime.tv_nsec = (int)be32_to_cpu(from->di_mtime.t_nsec);
  214. inode->i_ctime.tv_sec = (int)be32_to_cpu(from->di_ctime.t_sec);
  215. inode->i_ctime.tv_nsec = (int)be32_to_cpu(from->di_ctime.t_nsec);
  216. inode->i_generation = be32_to_cpu(from->di_gen);
  217. inode->i_mode = be16_to_cpu(from->di_mode);
  218. to->di_size = be64_to_cpu(from->di_size);
  219. to->di_nblocks = be64_to_cpu(from->di_nblocks);
  220. to->di_extsize = be32_to_cpu(from->di_extsize);
  221. to->di_nextents = be32_to_cpu(from->di_nextents);
  222. to->di_anextents = be16_to_cpu(from->di_anextents);
  223. to->di_forkoff = from->di_forkoff;
  224. to->di_aformat = from->di_aformat;
  225. to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
  226. to->di_dmstate = be16_to_cpu(from->di_dmstate);
  227. to->di_flags = be16_to_cpu(from->di_flags);
  228. if (to->di_version == 3) {
  229. inode->i_version = be64_to_cpu(from->di_changecount);
  230. to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
  231. to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
  232. to->di_flags2 = be64_to_cpu(from->di_flags2);
  233. }
  234. }
  235. void
  236. xfs_inode_to_disk(
  237. struct xfs_inode *ip,
  238. struct xfs_dinode *to,
  239. xfs_lsn_t lsn)
  240. {
  241. struct xfs_icdinode *from = &ip->i_d;
  242. struct inode *inode = VFS_I(ip);
  243. to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
  244. to->di_onlink = 0;
  245. to->di_version = from->di_version;
  246. to->di_format = from->di_format;
  247. to->di_uid = cpu_to_be32(from->di_uid);
  248. to->di_gid = cpu_to_be32(from->di_gid);
  249. to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
  250. to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
  251. memset(to->di_pad, 0, sizeof(to->di_pad));
  252. to->di_atime.t_sec = cpu_to_be32(inode->i_atime.tv_sec);
  253. to->di_atime.t_nsec = cpu_to_be32(inode->i_atime.tv_nsec);
  254. to->di_mtime.t_sec = cpu_to_be32(inode->i_mtime.tv_sec);
  255. to->di_mtime.t_nsec = cpu_to_be32(inode->i_mtime.tv_nsec);
  256. to->di_ctime.t_sec = cpu_to_be32(inode->i_ctime.tv_sec);
  257. to->di_ctime.t_nsec = cpu_to_be32(inode->i_ctime.tv_nsec);
  258. to->di_nlink = cpu_to_be32(inode->i_nlink);
  259. to->di_gen = cpu_to_be32(inode->i_generation);
  260. to->di_mode = cpu_to_be16(inode->i_mode);
  261. to->di_size = cpu_to_be64(from->di_size);
  262. to->di_nblocks = cpu_to_be64(from->di_nblocks);
  263. to->di_extsize = cpu_to_be32(from->di_extsize);
  264. to->di_nextents = cpu_to_be32(from->di_nextents);
  265. to->di_anextents = cpu_to_be16(from->di_anextents);
  266. to->di_forkoff = from->di_forkoff;
  267. to->di_aformat = from->di_aformat;
  268. to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
  269. to->di_dmstate = cpu_to_be16(from->di_dmstate);
  270. to->di_flags = cpu_to_be16(from->di_flags);
  271. if (from->di_version == 3) {
  272. to->di_changecount = cpu_to_be64(inode->i_version);
  273. to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
  274. to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
  275. to->di_flags2 = cpu_to_be64(from->di_flags2);
  276. to->di_ino = cpu_to_be64(ip->i_ino);
  277. to->di_lsn = cpu_to_be64(lsn);
  278. memset(to->di_pad2, 0, sizeof(to->di_pad2));
  279. uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
  280. to->di_flushiter = 0;
  281. } else {
  282. to->di_flushiter = cpu_to_be16(from->di_flushiter);
  283. }
  284. }
  285. void
  286. xfs_log_dinode_to_disk(
  287. struct xfs_log_dinode *from,
  288. struct xfs_dinode *to)
  289. {
  290. to->di_magic = cpu_to_be16(from->di_magic);
  291. to->di_mode = cpu_to_be16(from->di_mode);
  292. to->di_version = from->di_version;
  293. to->di_format = from->di_format;
  294. to->di_onlink = 0;
  295. to->di_uid = cpu_to_be32(from->di_uid);
  296. to->di_gid = cpu_to_be32(from->di_gid);
  297. to->di_nlink = cpu_to_be32(from->di_nlink);
  298. to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
  299. to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
  300. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  301. to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
  302. to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
  303. to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
  304. to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
  305. to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
  306. to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
  307. to->di_size = cpu_to_be64(from->di_size);
  308. to->di_nblocks = cpu_to_be64(from->di_nblocks);
  309. to->di_extsize = cpu_to_be32(from->di_extsize);
  310. to->di_nextents = cpu_to_be32(from->di_nextents);
  311. to->di_anextents = cpu_to_be16(from->di_anextents);
  312. to->di_forkoff = from->di_forkoff;
  313. to->di_aformat = from->di_aformat;
  314. to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
  315. to->di_dmstate = cpu_to_be16(from->di_dmstate);
  316. to->di_flags = cpu_to_be16(from->di_flags);
  317. to->di_gen = cpu_to_be32(from->di_gen);
  318. if (from->di_version == 3) {
  319. to->di_changecount = cpu_to_be64(from->di_changecount);
  320. to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
  321. to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
  322. to->di_flags2 = cpu_to_be64(from->di_flags2);
  323. to->di_ino = cpu_to_be64(from->di_ino);
  324. to->di_lsn = cpu_to_be64(from->di_lsn);
  325. memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
  326. uuid_copy(&to->di_uuid, &from->di_uuid);
  327. to->di_flushiter = 0;
  328. } else {
  329. to->di_flushiter = cpu_to_be16(from->di_flushiter);
  330. }
  331. }
  332. static bool
  333. xfs_dinode_verify(
  334. struct xfs_mount *mp,
  335. struct xfs_inode *ip,
  336. struct xfs_dinode *dip)
  337. {
  338. if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
  339. return false;
  340. /* only version 3 or greater inodes are extensively verified here */
  341. if (dip->di_version < 3)
  342. return true;
  343. if (!xfs_sb_version_hascrc(&mp->m_sb))
  344. return false;
  345. if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
  346. XFS_DINODE_CRC_OFF))
  347. return false;
  348. if (be64_to_cpu(dip->di_ino) != ip->i_ino)
  349. return false;
  350. if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
  351. return false;
  352. return true;
  353. }
  354. void
  355. xfs_dinode_calc_crc(
  356. struct xfs_mount *mp,
  357. struct xfs_dinode *dip)
  358. {
  359. __uint32_t crc;
  360. if (dip->di_version < 3)
  361. return;
  362. ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
  363. crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
  364. XFS_DINODE_CRC_OFF);
  365. dip->di_crc = xfs_end_cksum(crc);
  366. }
  367. /*
  368. * Read the disk inode attributes into the in-core inode structure.
  369. *
  370. * For version 5 superblocks, if we are initialising a new inode and we are not
  371. * utilising the XFS_MOUNT_IKEEP inode cluster mode, we can simple build the new
  372. * inode core with a random generation number. If we are keeping inodes around,
  373. * we need to read the inode cluster to get the existing generation number off
  374. * disk. Further, if we are using version 4 superblocks (i.e. v1/v2 inode
  375. * format) then log recovery is dependent on the di_flushiter field being
  376. * initialised from the current on-disk value and hence we must also read the
  377. * inode off disk.
  378. */
  379. int
  380. xfs_iread(
  381. xfs_mount_t *mp,
  382. xfs_trans_t *tp,
  383. xfs_inode_t *ip,
  384. uint iget_flags)
  385. {
  386. xfs_buf_t *bp;
  387. xfs_dinode_t *dip;
  388. int error;
  389. /*
  390. * Fill in the location information in the in-core inode.
  391. */
  392. error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
  393. if (error)
  394. return error;
  395. /* shortcut IO on inode allocation if possible */
  396. if ((iget_flags & XFS_IGET_CREATE) &&
  397. xfs_sb_version_hascrc(&mp->m_sb) &&
  398. !(mp->m_flags & XFS_MOUNT_IKEEP)) {
  399. /* initialise the on-disk inode core */
  400. memset(&ip->i_d, 0, sizeof(ip->i_d));
  401. VFS_I(ip)->i_generation = prandom_u32();
  402. if (xfs_sb_version_hascrc(&mp->m_sb))
  403. ip->i_d.di_version = 3;
  404. else
  405. ip->i_d.di_version = 2;
  406. return 0;
  407. }
  408. /*
  409. * Get pointers to the on-disk inode and the buffer containing it.
  410. */
  411. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
  412. if (error)
  413. return error;
  414. /* even unallocated inodes are verified */
  415. if (!xfs_dinode_verify(mp, ip, dip)) {
  416. xfs_alert(mp, "%s: validation failed for inode %lld failed",
  417. __func__, ip->i_ino);
  418. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
  419. error = -EFSCORRUPTED;
  420. goto out_brelse;
  421. }
  422. /*
  423. * If the on-disk inode is already linked to a directory
  424. * entry, copy all of the inode into the in-core inode.
  425. * xfs_iformat_fork() handles copying in the inode format
  426. * specific information.
  427. * Otherwise, just get the truly permanent information.
  428. */
  429. if (dip->di_mode) {
  430. xfs_inode_from_disk(ip, dip);
  431. error = xfs_iformat_fork(ip, dip);
  432. if (error) {
  433. #ifdef DEBUG
  434. xfs_alert(mp, "%s: xfs_iformat() returned error %d",
  435. __func__, error);
  436. #endif /* DEBUG */
  437. goto out_brelse;
  438. }
  439. } else {
  440. /*
  441. * Partial initialisation of the in-core inode. Just the bits
  442. * that xfs_ialloc won't overwrite or relies on being correct.
  443. */
  444. ip->i_d.di_version = dip->di_version;
  445. VFS_I(ip)->i_generation = be32_to_cpu(dip->di_gen);
  446. ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
  447. /*
  448. * Make sure to pull in the mode here as well in
  449. * case the inode is released without being used.
  450. * This ensures that xfs_inactive() will see that
  451. * the inode is already free and not try to mess
  452. * with the uninitialized part of it.
  453. */
  454. VFS_I(ip)->i_mode = 0;
  455. }
  456. ASSERT(ip->i_d.di_version >= 2);
  457. ip->i_delayed_blks = 0;
  458. /*
  459. * Mark the buffer containing the inode as something to keep
  460. * around for a while. This helps to keep recently accessed
  461. * meta-data in-core longer.
  462. */
  463. xfs_buf_set_ref(bp, XFS_INO_REF);
  464. /*
  465. * Use xfs_trans_brelse() to release the buffer containing the on-disk
  466. * inode, because it was acquired with xfs_trans_read_buf() in
  467. * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
  468. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  469. * will only release the buffer if it is not dirty within the
  470. * transaction. It will be OK to release the buffer in this case,
  471. * because inodes on disk are never destroyed and we will be locking the
  472. * new in-core inode before putting it in the cache where other
  473. * processes can find it. Thus we don't have to worry about the inode
  474. * being changed just because we released the buffer.
  475. */
  476. out_brelse:
  477. xfs_trans_brelse(tp, bp);
  478. return error;
  479. }