xfs_trans_inode.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "xfs_trace.h"
  18. #include <linux/iversion.h>
  19. /*
  20. * Add a locked inode to the transaction.
  21. *
  22. * The inode must be locked, and it cannot be associated with any transaction.
  23. * If lock_flags is non-zero the inode will be unlocked on transaction commit.
  24. */
  25. void
  26. xfs_trans_ijoin(
  27. struct xfs_trans *tp,
  28. struct xfs_inode *ip,
  29. uint lock_flags)
  30. {
  31. xfs_inode_log_item_t *iip;
  32. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  33. if (ip->i_itemp == NULL)
  34. xfs_inode_item_init(ip, ip->i_mount);
  35. iip = ip->i_itemp;
  36. ASSERT(iip->ili_lock_flags == 0);
  37. iip->ili_lock_flags = lock_flags;
  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. }
  64. /*
  65. * This is called to mark the fields indicated in fieldmask as needing
  66. * to be logged when the transaction is committed. The inode must
  67. * already be associated with the given transaction.
  68. *
  69. * The values for fieldmask are defined in xfs_inode_item.h. We always
  70. * log all of the core inode if any of it has changed, and we always log
  71. * all of the inline data/extents/b-tree root if any of them has changed.
  72. */
  73. void
  74. xfs_trans_log_inode(
  75. xfs_trans_t *tp,
  76. xfs_inode_t *ip,
  77. uint flags)
  78. {
  79. struct inode *inode = VFS_I(ip);
  80. ASSERT(ip->i_itemp != NULL);
  81. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  82. /*
  83. * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
  84. * don't matter - we either will need an extra transaction in 24 hours
  85. * to log the timestamps, or will clear already cleared fields in the
  86. * worst case.
  87. */
  88. if (inode->i_state & (I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED)) {
  89. spin_lock(&inode->i_lock);
  90. inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
  91. spin_unlock(&inode->i_lock);
  92. }
  93. /*
  94. * Record the specific change for fdatasync optimisation. This
  95. * allows fdatasync to skip log forces for inodes that are only
  96. * timestamp dirty. We do this before the change count so that
  97. * the core being logged in this case does not impact on fdatasync
  98. * behaviour.
  99. */
  100. ip->i_itemp->ili_fsync_fields |= flags;
  101. /*
  102. * First time we log the inode in a transaction, bump the inode change
  103. * counter if it is configured for this to occur. While we have the
  104. * inode locked exclusively for metadata modification, we can usually
  105. * avoid setting XFS_ILOG_CORE if no one has queried the value since
  106. * the last time it was incremented. If we have XFS_ILOG_CORE already
  107. * set however, then go ahead and bump the i_version counter
  108. * unconditionally.
  109. */
  110. if (!test_and_set_bit(XFS_LI_DIRTY, &ip->i_itemp->ili_item.li_flags) &&
  111. IS_I_VERSION(VFS_I(ip))) {
  112. if (inode_maybe_inc_iversion(VFS_I(ip), flags & XFS_ILOG_CORE))
  113. flags |= XFS_ILOG_CORE;
  114. }
  115. tp->t_flags |= XFS_TRANS_DIRTY;
  116. /*
  117. * Always OR in the bits from the ili_last_fields field.
  118. * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
  119. * routines in the eventual clearing of the ili_fields bits.
  120. * See the big comment in xfs_iflush() for an explanation of
  121. * this coordination mechanism.
  122. */
  123. flags |= ip->i_itemp->ili_last_fields;
  124. ip->i_itemp->ili_fields |= flags;
  125. }
  126. int
  127. xfs_trans_roll_inode(
  128. struct xfs_trans **tpp,
  129. struct xfs_inode *ip)
  130. {
  131. int error;
  132. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  133. error = xfs_trans_roll(tpp);
  134. if (!error)
  135. xfs_trans_ijoin(*tpp, ip, 0);
  136. return error;
  137. }