xfs_trans_inode.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
  39. /*
  40. * Get a log_item_desc to point at the new item.
  41. */
  42. xfs_trans_add_item(tp, &iip->ili_item);
  43. }
  44. /*
  45. * Transactional inode timestamp update. Requires the inode to be locked and
  46. * joined to the transaction supplied. Relies on the transaction subsystem to
  47. * track dirty state and update/writeback the inode accordingly.
  48. */
  49. void
  50. xfs_trans_ichgtime(
  51. struct xfs_trans *tp,
  52. struct xfs_inode *ip,
  53. int flags)
  54. {
  55. struct inode *inode = VFS_I(ip);
  56. struct timespec64 tv;
  57. ASSERT(tp);
  58. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  59. tv = current_time(inode);
  60. if (flags & XFS_ICHGTIME_MOD)
  61. inode->i_mtime = tv;
  62. if (flags & XFS_ICHGTIME_CHG)
  63. inode->i_ctime = tv;
  64. }
  65. /*
  66. * This is called to mark the fields indicated in fieldmask as needing
  67. * to be logged when the transaction is committed. The inode must
  68. * already be associated with the given transaction.
  69. *
  70. * The values for fieldmask are defined in xfs_inode_item.h. We always
  71. * log all of the core inode if any of it has changed, and we always log
  72. * all of the inline data/extents/b-tree root if any of them has changed.
  73. */
  74. void
  75. xfs_trans_log_inode(
  76. xfs_trans_t *tp,
  77. xfs_inode_t *ip,
  78. uint flags)
  79. {
  80. struct inode *inode = VFS_I(ip);
  81. ASSERT(ip->i_itemp != NULL);
  82. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  83. ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
  84. /*
  85. * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
  86. * don't matter - we either will need an extra transaction in 24 hours
  87. * to log the timestamps, or will clear already cleared fields in the
  88. * worst case.
  89. */
  90. if (inode->i_state & I_DIRTY_TIME) {
  91. spin_lock(&inode->i_lock);
  92. inode->i_state &= ~I_DIRTY_TIME;
  93. spin_unlock(&inode->i_lock);
  94. }
  95. /*
  96. * Record the specific change for fdatasync optimisation. This
  97. * allows fdatasync to skip log forces for inodes that are only
  98. * timestamp dirty. We do this before the change count so that
  99. * the core being logged in this case does not impact on fdatasync
  100. * behaviour.
  101. */
  102. ip->i_itemp->ili_fsync_fields |= flags;
  103. /*
  104. * First time we log the inode in a transaction, bump the inode change
  105. * counter if it is configured for this to occur. While we have the
  106. * inode locked exclusively for metadata modification, we can usually
  107. * avoid setting XFS_ILOG_CORE if no one has queried the value since
  108. * the last time it was incremented. If we have XFS_ILOG_CORE already
  109. * set however, then go ahead and bump the i_version counter
  110. * unconditionally.
  111. */
  112. if (!test_and_set_bit(XFS_LI_DIRTY, &ip->i_itemp->ili_item.li_flags) &&
  113. IS_I_VERSION(VFS_I(ip))) {
  114. if (inode_maybe_inc_iversion(VFS_I(ip), flags & XFS_ILOG_CORE))
  115. flags |= XFS_ILOG_CORE;
  116. }
  117. tp->t_flags |= XFS_TRANS_DIRTY;
  118. /*
  119. * Always OR in the bits from the ili_last_fields field.
  120. * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
  121. * routines in the eventual clearing of the ili_fields bits.
  122. * See the big comment in xfs_iflush() for an explanation of
  123. * this coordination mechanism.
  124. */
  125. flags |= ip->i_itemp->ili_last_fields;
  126. ip->i_itemp->ili_fields |= flags;
  127. }
  128. int
  129. xfs_trans_roll_inode(
  130. struct xfs_trans **tpp,
  131. struct xfs_inode *ip)
  132. {
  133. int error;
  134. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  135. error = xfs_trans_roll(tpp);
  136. if (!error)
  137. xfs_trans_ijoin(*tpp, ip, 0);
  138. return error;
  139. }