162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2000,2005 Silicon Graphics, Inc.
462306a36Sopenharmony_ci * All Rights Reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#include "xfs.h"
762306a36Sopenharmony_ci#include "xfs_fs.h"
862306a36Sopenharmony_ci#include "xfs_shared.h"
962306a36Sopenharmony_ci#include "xfs_format.h"
1062306a36Sopenharmony_ci#include "xfs_log_format.h"
1162306a36Sopenharmony_ci#include "xfs_trans_resv.h"
1262306a36Sopenharmony_ci#include "xfs_mount.h"
1362306a36Sopenharmony_ci#include "xfs_inode.h"
1462306a36Sopenharmony_ci#include "xfs_trans.h"
1562306a36Sopenharmony_ci#include "xfs_trans_priv.h"
1662306a36Sopenharmony_ci#include "xfs_inode_item.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include <linux/iversion.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * Add a locked inode to the transaction.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * The inode must be locked, and it cannot be associated with any transaction.
2462306a36Sopenharmony_ci * If lock_flags is non-zero the inode will be unlocked on transaction commit.
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_civoid
2762306a36Sopenharmony_cixfs_trans_ijoin(
2862306a36Sopenharmony_ci	struct xfs_trans	*tp,
2962306a36Sopenharmony_ci	struct xfs_inode	*ip,
3062306a36Sopenharmony_ci	uint			lock_flags)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	struct xfs_inode_log_item *iip;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
3562306a36Sopenharmony_ci	if (ip->i_itemp == NULL)
3662306a36Sopenharmony_ci		xfs_inode_item_init(ip, ip->i_mount);
3762306a36Sopenharmony_ci	iip = ip->i_itemp;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	ASSERT(iip->ili_lock_flags == 0);
4062306a36Sopenharmony_ci	iip->ili_lock_flags = lock_flags;
4162306a36Sopenharmony_ci	ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	/* Reset the per-tx dirty context and add the item to the tx. */
4462306a36Sopenharmony_ci	iip->ili_dirty_flags = 0;
4562306a36Sopenharmony_ci	xfs_trans_add_item(tp, &iip->ili_item);
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/*
4962306a36Sopenharmony_ci * Transactional inode timestamp update. Requires the inode to be locked and
5062306a36Sopenharmony_ci * joined to the transaction supplied. Relies on the transaction subsystem to
5162306a36Sopenharmony_ci * track dirty state and update/writeback the inode accordingly.
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_civoid
5462306a36Sopenharmony_cixfs_trans_ichgtime(
5562306a36Sopenharmony_ci	struct xfs_trans	*tp,
5662306a36Sopenharmony_ci	struct xfs_inode	*ip,
5762306a36Sopenharmony_ci	int			flags)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	struct inode		*inode = VFS_I(ip);
6062306a36Sopenharmony_ci	struct timespec64	tv;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	ASSERT(tp);
6362306a36Sopenharmony_ci	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	tv = current_time(inode);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	if (flags & XFS_ICHGTIME_MOD)
6862306a36Sopenharmony_ci		inode->i_mtime = tv;
6962306a36Sopenharmony_ci	if (flags & XFS_ICHGTIME_CHG)
7062306a36Sopenharmony_ci		inode_set_ctime_to_ts(inode, tv);
7162306a36Sopenharmony_ci	if (flags & XFS_ICHGTIME_CREATE)
7262306a36Sopenharmony_ci		ip->i_crtime = tv;
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/*
7662306a36Sopenharmony_ci * This is called to mark the fields indicated in fieldmask as needing to be
7762306a36Sopenharmony_ci * logged when the transaction is committed.  The inode must already be
7862306a36Sopenharmony_ci * associated with the given transaction. All we do here is record where the
7962306a36Sopenharmony_ci * inode was dirtied and mark the transaction and inode log item dirty;
8062306a36Sopenharmony_ci * everything else is done in the ->precommit log item operation after the
8162306a36Sopenharmony_ci * changes in the transaction have been completed.
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_civoid
8462306a36Sopenharmony_cixfs_trans_log_inode(
8562306a36Sopenharmony_ci	struct xfs_trans	*tp,
8662306a36Sopenharmony_ci	struct xfs_inode	*ip,
8762306a36Sopenharmony_ci	uint			flags)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	struct xfs_inode_log_item *iip = ip->i_itemp;
9062306a36Sopenharmony_ci	struct inode		*inode = VFS_I(ip);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	ASSERT(iip);
9362306a36Sopenharmony_ci	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
9462306a36Sopenharmony_ci	ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	tp->t_flags |= XFS_TRANS_DIRTY;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/*
9962306a36Sopenharmony_ci	 * First time we log the inode in a transaction, bump the inode change
10062306a36Sopenharmony_ci	 * counter if it is configured for this to occur. While we have the
10162306a36Sopenharmony_ci	 * inode locked exclusively for metadata modification, we can usually
10262306a36Sopenharmony_ci	 * avoid setting XFS_ILOG_CORE if no one has queried the value since
10362306a36Sopenharmony_ci	 * the last time it was incremented. If we have XFS_ILOG_CORE already
10462306a36Sopenharmony_ci	 * set however, then go ahead and bump the i_version counter
10562306a36Sopenharmony_ci	 * unconditionally.
10662306a36Sopenharmony_ci	 */
10762306a36Sopenharmony_ci	if (!test_and_set_bit(XFS_LI_DIRTY, &iip->ili_item.li_flags)) {
10862306a36Sopenharmony_ci		if (IS_I_VERSION(inode) &&
10962306a36Sopenharmony_ci		    inode_maybe_inc_iversion(inode, flags & XFS_ILOG_CORE))
11062306a36Sopenharmony_ci			flags |= XFS_ILOG_IVERSION;
11162306a36Sopenharmony_ci	}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	iip->ili_dirty_flags |= flags;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciint
11762306a36Sopenharmony_cixfs_trans_roll_inode(
11862306a36Sopenharmony_ci	struct xfs_trans	**tpp,
11962306a36Sopenharmony_ci	struct xfs_inode	*ip)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	int			error;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
12462306a36Sopenharmony_ci	error = xfs_trans_roll(tpp);
12562306a36Sopenharmony_ci	if (!error)
12662306a36Sopenharmony_ci		xfs_trans_ijoin(*tpp, ip, 0);
12762306a36Sopenharmony_ci	return error;
12862306a36Sopenharmony_ci}
129