13f6d70e8SEric Sandeen // SPDX-License-Identifier: GPL-2.0
23f6d70e8SEric Sandeen /*
33f6d70e8SEric Sandeen * Copyright (c) 2000,2005 Silicon Graphics, Inc.
43f6d70e8SEric Sandeen * All Rights Reserved.
53f6d70e8SEric Sandeen */
63f6d70e8SEric Sandeen #include "xfs.h"
73f6d70e8SEric Sandeen #include "xfs_fs.h"
83f6d70e8SEric Sandeen #include "xfs_shared.h"
93f6d70e8SEric Sandeen #include "xfs_format.h"
103f6d70e8SEric Sandeen #include "xfs_log_format.h"
11298f7becSDave Chinner #include "xfs_trans_resv.h"
12298f7becSDave Chinner #include "xfs_mount.h"
133f6d70e8SEric Sandeen #include "xfs_inode.h"
143f6d70e8SEric Sandeen #include "xfs_trans.h"
153f6d70e8SEric Sandeen #include "xfs_trans_priv.h"
163f6d70e8SEric Sandeen #include "xfs_inode_item.h"
173f6d70e8SEric Sandeen
183f6d70e8SEric Sandeen #include <linux/iversion.h>
193f6d70e8SEric Sandeen
203f6d70e8SEric Sandeen /*
213f6d70e8SEric Sandeen * Add a locked inode to the transaction.
223f6d70e8SEric Sandeen *
233f6d70e8SEric Sandeen * The inode must be locked, and it cannot be associated with any transaction.
243f6d70e8SEric Sandeen * If lock_flags is non-zero the inode will be unlocked on transaction commit.
253f6d70e8SEric Sandeen */
263f6d70e8SEric Sandeen void
xfs_trans_ijoin(struct xfs_trans * tp,struct xfs_inode * ip,uint lock_flags)273f6d70e8SEric Sandeen xfs_trans_ijoin(
283f6d70e8SEric Sandeen struct xfs_trans *tp,
293f6d70e8SEric Sandeen struct xfs_inode *ip,
303f6d70e8SEric Sandeen uint lock_flags)
313f6d70e8SEric Sandeen {
32fd9cbe51SChristoph Hellwig struct xfs_inode_log_item *iip;
333f6d70e8SEric Sandeen
343fed24ffSMatthew Wilcox (Oracle) xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
353f6d70e8SEric Sandeen if (ip->i_itemp == NULL)
363f6d70e8SEric Sandeen xfs_inode_item_init(ip, ip->i_mount);
373f6d70e8SEric Sandeen iip = ip->i_itemp;
383f6d70e8SEric Sandeen
393f6d70e8SEric Sandeen ASSERT(iip->ili_lock_flags == 0);
403f6d70e8SEric Sandeen iip->ili_lock_flags = lock_flags;
4196355d5aSDave Chinner ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
423f6d70e8SEric Sandeen
4382842feeSDave Chinner /* Reset the per-tx dirty context and add the item to the tx. */
4482842feeSDave Chinner iip->ili_dirty_flags = 0;
453f6d70e8SEric Sandeen xfs_trans_add_item(tp, &iip->ili_item);
463f6d70e8SEric Sandeen }
473f6d70e8SEric Sandeen
483f6d70e8SEric Sandeen /*
493f6d70e8SEric Sandeen * Transactional inode timestamp update. Requires the inode to be locked and
503f6d70e8SEric Sandeen * joined to the transaction supplied. Relies on the transaction subsystem to
513f6d70e8SEric Sandeen * track dirty state and update/writeback the inode accordingly.
523f6d70e8SEric Sandeen */
533f6d70e8SEric Sandeen void
xfs_trans_ichgtime(struct xfs_trans * tp,struct xfs_inode * ip,int flags)543f6d70e8SEric Sandeen xfs_trans_ichgtime(
553f6d70e8SEric Sandeen struct xfs_trans *tp,
563f6d70e8SEric Sandeen struct xfs_inode *ip,
573f6d70e8SEric Sandeen int flags)
583f6d70e8SEric Sandeen {
593f6d70e8SEric Sandeen struct inode *inode = VFS_I(ip);
603f6d70e8SEric Sandeen struct timespec64 tv;
613f6d70e8SEric Sandeen
623f6d70e8SEric Sandeen ASSERT(tp);
633fed24ffSMatthew Wilcox (Oracle) xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
643f6d70e8SEric Sandeen
65f798accdSChristian Brauner tv = current_time(inode);
663f6d70e8SEric Sandeen
673f6d70e8SEric Sandeen if (flags & XFS_ICHGTIME_MOD)
6875d1e312SJeff Layton inode_set_mtime_to_ts(inode, tv);
69f798accdSChristian Brauner if (flags & XFS_ICHGTIME_CHG)
70f798accdSChristian Brauner inode_set_ctime_to_ts(inode, tv);
71*3d1dfb6dSDarrick J. Wong if (flags & XFS_ICHGTIME_ACCESS)
72*3d1dfb6dSDarrick J. Wong inode_set_atime_to_ts(inode, tv);
738d2d878dSChristoph Hellwig if (flags & XFS_ICHGTIME_CREATE)
74e98d5e88SChristoph Hellwig ip->i_crtime = tv;
753f6d70e8SEric Sandeen }
763f6d70e8SEric Sandeen
773f6d70e8SEric Sandeen /*
78298f7becSDave Chinner * This is called to mark the fields indicated in fieldmask as needing to be
79298f7becSDave Chinner * logged when the transaction is committed. The inode must already be
8082842feeSDave Chinner * associated with the given transaction. All we do here is record where the
8182842feeSDave Chinner * inode was dirtied and mark the transaction and inode log item dirty;
8282842feeSDave Chinner * everything else is done in the ->precommit log item operation after the
8382842feeSDave Chinner * changes in the transaction have been completed.
843f6d70e8SEric Sandeen */
853f6d70e8SEric Sandeen void
xfs_trans_log_inode(struct xfs_trans * tp,struct xfs_inode * ip,uint flags)863f6d70e8SEric Sandeen xfs_trans_log_inode(
871319ebefSDave Chinner struct xfs_trans *tp,
881319ebefSDave Chinner struct xfs_inode *ip,
893f6d70e8SEric Sandeen uint flags)
903f6d70e8SEric Sandeen {
911319ebefSDave Chinner struct xfs_inode_log_item *iip = ip->i_itemp;
923f6d70e8SEric Sandeen struct inode *inode = VFS_I(ip);
933f6d70e8SEric Sandeen
941319ebefSDave Chinner ASSERT(iip);
953fed24ffSMatthew Wilcox (Oracle) xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
9696355d5aSDave Chinner ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
973f6d70e8SEric Sandeen
981319ebefSDave Chinner tp->t_flags |= XFS_TRANS_DIRTY;
991319ebefSDave Chinner
1003f6d70e8SEric Sandeen /*
1013f6d70e8SEric Sandeen * First time we log the inode in a transaction, bump the inode change
1023f6d70e8SEric Sandeen * counter if it is configured for this to occur. While we have the
1033f6d70e8SEric Sandeen * inode locked exclusively for metadata modification, we can usually
1043f6d70e8SEric Sandeen * avoid setting XFS_ILOG_CORE if no one has queried the value since
1053f6d70e8SEric Sandeen * the last time it was incremented. If we have XFS_ILOG_CORE already
1063f6d70e8SEric Sandeen * set however, then go ahead and bump the i_version counter
1073f6d70e8SEric Sandeen * unconditionally.
1083f6d70e8SEric Sandeen */
1091319ebefSDave Chinner if (!test_and_set_bit(XFS_LI_DIRTY, &iip->ili_item.li_flags)) {
1101319ebefSDave Chinner if (IS_I_VERSION(inode) &&
1111319ebefSDave Chinner inode_maybe_inc_iversion(inode, flags & XFS_ILOG_CORE))
11282842feeSDave Chinner flags |= XFS_ILOG_IVERSION;
1133f6d70e8SEric Sandeen }
1143f6d70e8SEric Sandeen
11582842feeSDave Chinner iip->ili_dirty_flags |= flags;
1163f6d70e8SEric Sandeen }
1173f6d70e8SEric Sandeen
1183f6d70e8SEric Sandeen int
xfs_trans_roll_inode(struct xfs_trans ** tpp,struct xfs_inode * ip)1193f6d70e8SEric Sandeen xfs_trans_roll_inode(
1203f6d70e8SEric Sandeen struct xfs_trans **tpp,
1213f6d70e8SEric Sandeen struct xfs_inode *ip)
1223f6d70e8SEric Sandeen {
1233f6d70e8SEric Sandeen int error;
1243f6d70e8SEric Sandeen
1253f6d70e8SEric Sandeen xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1263f6d70e8SEric Sandeen error = xfs_trans_roll(tpp);
1273f6d70e8SEric Sandeen if (!error)
1283f6d70e8SEric Sandeen xfs_trans_ijoin(*tpp, ip, 0);
1293f6d70e8SEric Sandeen return error;
1303f6d70e8SEric Sandeen }
131