xfs_trans_inode.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000,2005 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_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_trans_priv.h"
  16. #include "xfs_inode_item.h"
  17. #include <linux/iversion.h>
  18. /*
  19. * Add a locked inode to the transaction.
  20. *
  21. * The inode must be locked, and it cannot be associated with any transaction.
  22. * If lock_flags is non-zero the inode will be unlocked on transaction commit.
  23. */
  24. void
  25. xfs_trans_ijoin(
  26. struct xfs_trans *tp,
  27. struct xfs_inode *ip,
  28. uint lock_flags)
  29. {
  30. struct xfs_inode_log_item *iip;
  31. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  32. if (ip->i_itemp == NULL)
  33. xfs_inode_item_init(ip, ip->i_mount);
  34. iip = ip->i_itemp;
  35. ASSERT(iip->ili_lock_flags == 0);
  36. iip->ili_lock_flags = lock_flags;
  37. ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
  38. /*
  39. * Get a log_item_desc to point at the new item.
  40. */
  41. xfs_trans_add_item(tp, &iip->ili_item);
  42. }
  43. /*
  44. * Transactional inode timestamp update. Requires the inode to be locked and
  45. * joined to the transaction supplied. Relies on the transaction subsystem to
  46. * track dirty state and update/writeback the inode accordingly.
  47. */
  48. void
  49. xfs_trans_ichgtime(
  50. struct xfs_trans *tp,
  51. struct xfs_inode *ip,
  52. int flags)
  53. {
  54. struct inode *inode = VFS_I(ip);
  55. struct timespec64 tv;
  56. ASSERT(tp);
  57. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  58. tv = current_time(inode);
  59. if (flags & XFS_ICHGTIME_MOD)
  60. inode->i_mtime = tv;
  61. if (flags & XFS_ICHGTIME_CHG)
  62. inode->i_ctime = tv;
  63. if (flags & XFS_ICHGTIME_CREATE)
  64. ip->i_crtime = tv;
  65. }
  66. /*
  67. * This is called to mark the fields indicated in fieldmask as needing to be
  68. * logged when the transaction is committed. The inode must already be
  69. * associated with the given transaction.
  70. *
  71. * The values for fieldmask are defined in xfs_inode_item.h. We always log all
  72. * of the core inode if any of it has changed, and we always log all of the
  73. * inline data/extents/b-tree root if any of them has changed.
  74. *
  75. * Grab and pin the cluster buffer associated with this inode to avoid RMW
  76. * cycles at inode writeback time. Avoid the need to add error handling to every
  77. * xfs_trans_log_inode() call by shutting down on read error. This will cause
  78. * transactions to fail and everything to error out, just like if we return a
  79. * read error in a dirty transaction and cancel it.
  80. */
  81. void
  82. xfs_trans_log_inode(
  83. struct xfs_trans *tp,
  84. struct xfs_inode *ip,
  85. uint flags)
  86. {
  87. struct xfs_inode_log_item *iip = ip->i_itemp;
  88. struct inode *inode = VFS_I(ip);
  89. uint iversion_flags = 0;
  90. ASSERT(iip);
  91. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  92. ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
  93. tp->t_flags |= XFS_TRANS_DIRTY;
  94. /*
  95. * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
  96. * don't matter - we either will need an extra transaction in 24 hours
  97. * to log the timestamps, or will clear already cleared fields in the
  98. * worst case.
  99. */
  100. if (inode->i_state & I_DIRTY_TIME) {
  101. spin_lock(&inode->i_lock);
  102. inode->i_state &= ~I_DIRTY_TIME;
  103. spin_unlock(&inode->i_lock);
  104. }
  105. /*
  106. * First time we log the inode in a transaction, bump the inode change
  107. * counter if it is configured for this to occur. While we have the
  108. * inode locked exclusively for metadata modification, we can usually
  109. * avoid setting XFS_ILOG_CORE if no one has queried the value since
  110. * the last time it was incremented. If we have XFS_ILOG_CORE already
  111. * set however, then go ahead and bump the i_version counter
  112. * unconditionally.
  113. */
  114. if (!test_and_set_bit(XFS_LI_DIRTY, &iip->ili_item.li_flags)) {
  115. if (IS_I_VERSION(inode) &&
  116. inode_maybe_inc_iversion(inode, flags & XFS_ILOG_CORE))
  117. iversion_flags = XFS_ILOG_CORE;
  118. }
  119. /*
  120. * If we're updating the inode core or the timestamps and it's possible
  121. * to upgrade this inode to bigtime format, do so now.
  122. */
  123. if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) &&
  124. xfs_has_bigtime(ip->i_mount) &&
  125. !xfs_inode_has_bigtime(ip)) {
  126. ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME;
  127. flags |= XFS_ILOG_CORE;
  128. }
  129. /*
  130. * Inode verifiers do not check that the extent size hint is an integer
  131. * multiple of the rt extent size on a directory with both rtinherit
  132. * and extszinherit flags set. If we're logging a directory that is
  133. * misconfigured in this way, clear the hint.
  134. */
  135. if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
  136. (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
  137. (ip->i_extsize % ip->i_mount->m_sb.sb_rextsize) > 0) {
  138. ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
  139. XFS_DIFLAG_EXTSZINHERIT);
  140. ip->i_extsize = 0;
  141. flags |= XFS_ILOG_CORE;
  142. }
  143. /*
  144. * Record the specific change for fdatasync optimisation. This allows
  145. * fdatasync to skip log forces for inodes that are only timestamp
  146. * dirty.
  147. */
  148. spin_lock(&iip->ili_lock);
  149. iip->ili_fsync_fields |= flags;
  150. if (!iip->ili_item.li_buf) {
  151. struct xfs_buf *bp;
  152. int error;
  153. /*
  154. * We hold the ILOCK here, so this inode is not going to be
  155. * flushed while we are here. Further, because there is no
  156. * buffer attached to the item, we know that there is no IO in
  157. * progress, so nothing will clear the ili_fields while we read
  158. * in the buffer. Hence we can safely drop the spin lock and
  159. * read the buffer knowing that the state will not change from
  160. * here.
  161. */
  162. spin_unlock(&iip->ili_lock);
  163. error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp);
  164. if (error) {
  165. xfs_force_shutdown(ip->i_mount, SHUTDOWN_META_IO_ERROR);
  166. return;
  167. }
  168. /*
  169. * We need an explicit buffer reference for the log item but
  170. * don't want the buffer to remain attached to the transaction.
  171. * Hold the buffer but release the transaction reference once
  172. * we've attached the inode log item to the buffer log item
  173. * list.
  174. */
  175. xfs_buf_hold(bp);
  176. spin_lock(&iip->ili_lock);
  177. iip->ili_item.li_buf = bp;
  178. bp->b_flags |= _XBF_INODES;
  179. list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list);
  180. xfs_trans_brelse(tp, bp);
  181. }
  182. /*
  183. * Always OR in the bits from the ili_last_fields field. This is to
  184. * coordinate with the xfs_iflush() and xfs_buf_inode_iodone() routines
  185. * in the eventual clearing of the ili_fields bits. See the big comment
  186. * in xfs_iflush() for an explanation of this coordination mechanism.
  187. */
  188. iip->ili_fields |= (flags | iip->ili_last_fields | iversion_flags);
  189. spin_unlock(&iip->ili_lock);
  190. }
  191. int
  192. xfs_trans_roll_inode(
  193. struct xfs_trans **tpp,
  194. struct xfs_inode *ip)
  195. {
  196. int error;
  197. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  198. error = xfs_trans_roll(tpp);
  199. if (!error)
  200. xfs_trans_ijoin(*tpp, ip, 0);
  201. return error;
  202. }