blob: 542927321a61b5e2ff52b30faca74dac268c1722 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110015#include "xfs_trans_priv.h"
16#include "xfs_inode_item.h"
Christoph Hellwig898621d2010-06-24 11:36:58 +100017#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Laytonf0e28282017-12-11 06:35:19 -050019#include <linux/iversion.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
Christoph Hellwig898621d2010-06-24 11:36:58 +100022 * Add a locked inode to the transaction.
23 *
24 * The inode must be locked, and it cannot be associated with any transaction.
Christoph Hellwigddc34152011-09-19 15:00:54 +000025 * If lock_flags is non-zero the inode will be unlocked on transaction commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27void
28xfs_trans_ijoin(
Christoph Hellwig898621d2010-06-24 11:36:58 +100029 struct xfs_trans *tp,
Christoph Hellwigddc34152011-09-19 15:00:54 +000030 struct xfs_inode *ip,
31 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 xfs_inode_log_item_t *iip;
34
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100035 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (ip->i_itemp == NULL)
37 xfs_inode_item_init(ip, ip->i_mount);
38 iip = ip->i_itemp;
Christoph Hellwigddc34152011-09-19 15:00:54 +000039
Christoph Hellwig898621d2010-06-24 11:36:58 +100040 ASSERT(iip->ili_lock_flags == 0);
Christoph Hellwigddc34152011-09-19 15:00:54 +000041 iip->ili_lock_flags = lock_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 /*
44 * Get a log_item_desc to point at the new item.
45 */
Christoph Hellwige98c4142010-06-23 18:11:15 +100046 xfs_trans_add_item(tp, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
Dave Chinnerdcd79a12010-09-28 12:27:25 +100050 * Transactional inode timestamp update. Requires the inode to be locked and
51 * joined to the transaction supplied. Relies on the transaction subsystem to
52 * track dirty state and update/writeback the inode accordingly.
53 */
54void
55xfs_trans_ichgtime(
56 struct xfs_trans *tp,
57 struct xfs_inode *ip,
58 int flags)
59{
60 struct inode *inode = VFS_I(ip);
Deepa Dinamani95582b02018-05-08 19:36:02 -070061 struct timespec64 tv;
Dave Chinnerdcd79a12010-09-28 12:27:25 +100062
63 ASSERT(tp);
64 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerdcd79a12010-09-28 12:27:25 +100065
Deepa Dinamanic2050a42016-09-14 07:48:06 -070066 tv = current_time(inode);
Dave Chinnerdcd79a12010-09-28 12:27:25 +100067
Dave Chinner39878482016-02-09 16:54:58 +110068 if (flags & XFS_ICHGTIME_MOD)
Dave Chinnerdcd79a12010-09-28 12:27:25 +100069 inode->i_mtime = tv;
Dave Chinner39878482016-02-09 16:54:58 +110070 if (flags & XFS_ICHGTIME_CHG)
Dave Chinnerdcd79a12010-09-28 12:27:25 +100071 inode->i_ctime = tv;
Dave Chinnerdcd79a12010-09-28 12:27:25 +100072}
73
74/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * This is called to mark the fields indicated in fieldmask as needing
76 * to be logged when the transaction is committed. The inode must
77 * already be associated with the given transaction.
78 *
79 * The values for fieldmask are defined in xfs_inode_item.h. We always
80 * log all of the core inode if any of it has changed, and we always log
81 * all of the inline data/extents/b-tree root if any of them has changed.
82 */
83void
84xfs_trans_log_inode(
85 xfs_trans_t *tp,
86 xfs_inode_t *ip,
87 uint flags)
88{
Christoph Hellwigc3b1b132018-03-06 17:04:00 -080089 struct inode *inode = VFS_I(ip);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100092 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Dave Chinnerdc037ad2013-06-27 16:04:59 +100094 /*
Christoph Hellwigc3b1b132018-03-06 17:04:00 -080095 * 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 | I_DIRTY_TIME_EXPIRED)) {
101 spin_lock(&inode->i_lock);
102 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
103 spin_unlock(&inode->i_lock);
104 }
105
106 /*
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100107 * Record the specific change for fdatasync optimisation. This
108 * allows fdatasync to skip log forces for inodes that are only
109 * timestamp dirty. We do this before the change count so that
110 * the core being logged in this case does not impact on fdatasync
111 * behaviour.
112 */
113 ip->i_itemp->ili_fsync_fields |= flags;
114
115 /*
Dave Chinnerdc037ad2013-06-27 16:04:59 +1000116 * First time we log the inode in a transaction, bump the inode change
Jeff Laytond17260f2017-12-11 06:35:23 -0500117 * counter if it is configured for this to occur. While we have the
118 * inode locked exclusively for metadata modification, we can usually
119 * avoid setting XFS_ILOG_CORE if no one has queried the value since
120 * the last time it was incremented. If we have XFS_ILOG_CORE already
121 * set however, then go ahead and bump the i_version counter
122 * unconditionally.
Dave Chinnerdc037ad2013-06-27 16:04:59 +1000123 */
Dave Chinnere6631f82018-05-09 07:49:37 -0700124 if (!test_and_set_bit(XFS_LI_DIRTY, &ip->i_itemp->ili_item.li_flags) &&
Dave Chinnerdc037ad2013-06-27 16:04:59 +1000125 IS_I_VERSION(VFS_I(ip))) {
Jeff Laytond17260f2017-12-11 06:35:23 -0500126 if (inode_maybe_inc_iversion(VFS_I(ip), flags & XFS_ILOG_CORE))
127 flags |= XFS_ILOG_CORE;
Dave Chinnerdc037ad2013-06-27 16:04:59 +1000128 }
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 tp->t_flags |= XFS_TRANS_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /*
133 * Always OR in the bits from the ili_last_fields field.
134 * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000135 * routines in the eventual clearing of the ili_fields bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * See the big comment in xfs_iflush() for an explanation of
Nathan Scottc41564b2006-03-29 08:55:14 +1000137 * this coordination mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
139 flags |= ip->i_itemp->ili_last_fields;
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000140 ip->i_itemp->ili_fields |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Christoph Hellwig411350d2017-08-28 10:21:03 -0700142
143int
144xfs_trans_roll_inode(
145 struct xfs_trans **tpp,
146 struct xfs_inode *ip)
147{
148 int error;
149
150 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
151 error = xfs_trans_roll(tpp);
152 if (!error)
153 xfs_trans_ijoin(*tpp, ip, 0);
154 return error;
155}