Merge tag 'trace-v5.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-block.git] / fs / xfs / libxfs / xfs_trans_inode.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include "xfs.h"
a844f451 7#include "xfs_fs.h"
70a9883c 8#include "xfs_shared.h"
a4fbe6ab 9#include "xfs_format.h"
239880ef 10#include "xfs_log_format.h"
298f7bec
DC
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
1da177e4 13#include "xfs_inode.h"
239880ef 14#include "xfs_trans.h"
a844f451
NS
15#include "xfs_trans_priv.h"
16#include "xfs_inode_item.h"
1da177e4 17
f0e28280
JL
18#include <linux/iversion.h>
19
1da177e4 20/*
898621d5
CH
21 * Add a locked inode to the transaction.
22 *
23 * The inode must be locked, and it cannot be associated with any transaction.
ddc3415a 24 * If lock_flags is non-zero the inode will be unlocked on transaction commit.
1da177e4
LT
25 */
26void
27xfs_trans_ijoin(
898621d5 28 struct xfs_trans *tp,
ddc3415a
CH
29 struct xfs_inode *ip,
30 uint lock_flags)
1da177e4 31{
fd9cbe51 32 struct xfs_inode_log_item *iip;
1da177e4 33
579aa9ca 34 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
35 if (ip->i_itemp == NULL)
36 xfs_inode_item_init(ip, ip->i_mount);
37 iip = ip->i_itemp;
ddc3415a 38
898621d5 39 ASSERT(iip->ili_lock_flags == 0);
ddc3415a 40 iip->ili_lock_flags = lock_flags;
96355d5a 41 ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
1da177e4
LT
42
43 /*
44 * Get a log_item_desc to point at the new item.
45 */
e98c414f 46 xfs_trans_add_item(tp, &iip->ili_item);
1da177e4
LT
47}
48
dcd79a14
DC
49/*
50 * 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);
8d2d878d 61 struct timespec64 tv;
dcd79a14
DC
62
63 ASSERT(tp);
64 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
dcd79a14 65
c2050a45 66 tv = current_time(inode);
dcd79a14 67
3987848c 68 if (flags & XFS_ICHGTIME_MOD)
dcd79a14 69 inode->i_mtime = tv;
3987848c 70 if (flags & XFS_ICHGTIME_CHG)
dcd79a14 71 inode->i_ctime = tv;
8d2d878d 72 if (flags & XFS_ICHGTIME_CREATE)
e98d5e88 73 ip->i_crtime = tv;
dcd79a14
DC
74}
75
1da177e4 76/*
298f7bec
DC
77 * This is called to mark the fields indicated in fieldmask as needing to be
78 * logged when the transaction is committed. The inode must already be
79 * associated with the given transaction.
1da177e4 80 *
298f7bec
DC
81 * The values for fieldmask are defined in xfs_inode_item.h. We always log all
82 * of the core inode if any of it has changed, and we always log all of the
83 * inline data/extents/b-tree root if any of them has changed.
84 *
85 * Grab and pin the cluster buffer associated with this inode to avoid RMW
86 * cycles at inode writeback time. Avoid the need to add error handling to every
87 * xfs_trans_log_inode() call by shutting down on read error. This will cause
88 * transactions to fail and everything to error out, just like if we return a
89 * read error in a dirty transaction and cancel it.
1da177e4
LT
90 */
91void
92xfs_trans_log_inode(
1319ebef
DC
93 struct xfs_trans *tp,
94 struct xfs_inode *ip,
95 uint flags)
1da177e4 96{
1319ebef
DC
97 struct xfs_inode_log_item *iip = ip->i_itemp;
98 struct inode *inode = VFS_I(ip);
99 uint iversion_flags = 0;
c3b1b131 100
1319ebef 101 ASSERT(iip);
579aa9ca 102 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
96355d5a 103 ASSERT(!xfs_iflags_test(ip, XFS_ISTALE));
1da177e4 104
1319ebef
DC
105 tp->t_flags |= XFS_TRANS_DIRTY;
106
c3b1b131
CH
107 /*
108 * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
109 * don't matter - we either will need an extra transaction in 24 hours
110 * to log the timestamps, or will clear already cleared fields in the
111 * worst case.
112 */
5fcd5750 113 if (inode->i_state & I_DIRTY_TIME) {
c3b1b131 114 spin_lock(&inode->i_lock);
5fcd5750 115 inode->i_state &= ~I_DIRTY_TIME;
c3b1b131
CH
116 spin_unlock(&inode->i_lock);
117 }
118
dc037ad7
DC
119 /*
120 * First time we log the inode in a transaction, bump the inode change
d17260fd
JL
121 * counter if it is configured for this to occur. While we have the
122 * inode locked exclusively for metadata modification, we can usually
123 * avoid setting XFS_ILOG_CORE if no one has queried the value since
124 * the last time it was incremented. If we have XFS_ILOG_CORE already
125 * set however, then go ahead and bump the i_version counter
126 * unconditionally.
dc037ad7 127 */
1319ebef
DC
128 if (!test_and_set_bit(XFS_LI_DIRTY, &iip->ili_item.li_flags)) {
129 if (IS_I_VERSION(inode) &&
130 inode_maybe_inc_iversion(inode, flags & XFS_ILOG_CORE))
131 iversion_flags = XFS_ILOG_CORE;
dc037ad7
DC
132 }
133
f93e5436
DW
134 /*
135 * If we're updating the inode core or the timestamps and it's possible
136 * to upgrade this inode to bigtime format, do so now.
137 */
138 if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) &&
ebd9027d 139 xfs_has_bigtime(ip->i_mount) &&
f93e5436 140 !xfs_inode_has_bigtime(ip)) {
3e09ab8f 141 ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME;
f93e5436
DW
142 flags |= XFS_ILOG_CORE;
143 }
144
603f000b 145 /*
83193e5e
DW
146 * Inode verifiers do not check that the extent size hint is an integer
147 * multiple of the rt extent size on a directory with both rtinherit
148 * and extszinherit flags set. If we're logging a directory that is
149 * misconfigured in this way, clear the hint.
603f000b
DW
150 */
151 if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
152 (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
153 (ip->i_extsize % ip->i_mount->m_sb.sb_rextsize) > 0) {
603f000b
DW
154 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
155 XFS_DIFLAG_EXTSZINHERIT);
156 ip->i_extsize = 0;
157 flags |= XFS_ILOG_CORE;
158 }
159
1319ebef
DC
160 /*
161 * Record the specific change for fdatasync optimisation. This allows
162 * fdatasync to skip log forces for inodes that are only timestamp
163 * dirty.
164 */
165 spin_lock(&iip->ili_lock);
166 iip->ili_fsync_fields |= flags;
1da177e4 167
298f7bec
DC
168 if (!iip->ili_item.li_buf) {
169 struct xfs_buf *bp;
170 int error;
171
172 /*
173 * We hold the ILOCK here, so this inode is not going to be
174 * flushed while we are here. Further, because there is no
175 * buffer attached to the item, we know that there is no IO in
176 * progress, so nothing will clear the ili_fields while we read
177 * in the buffer. Hence we can safely drop the spin lock and
178 * read the buffer knowing that the state will not change from
179 * here.
180 */
181 spin_unlock(&iip->ili_lock);
af9dcdde 182 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp);
298f7bec
DC
183 if (error) {
184 xfs_force_shutdown(ip->i_mount, SHUTDOWN_META_IO_ERROR);
185 return;
186 }
187
188 /*
189 * We need an explicit buffer reference for the log item but
190 * don't want the buffer to remain attached to the transaction.
48d55e2a
DC
191 * Hold the buffer but release the transaction reference once
192 * we've attached the inode log item to the buffer log item
193 * list.
298f7bec
DC
194 */
195 xfs_buf_hold(bp);
298f7bec
DC
196 spin_lock(&iip->ili_lock);
197 iip->ili_item.li_buf = bp;
48d55e2a
DC
198 bp->b_flags |= _XBF_INODES;
199 list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list);
200 xfs_trans_brelse(tp, bp);
298f7bec
DC
201 }
202
1da177e4 203 /*
1319ebef 204 * Always OR in the bits from the ili_last_fields field. This is to
664ffb8a
CH
205 * coordinate with the xfs_iflush() and xfs_buf_inode_iodone() routines
206 * in the eventual clearing of the ili_fields bits. See the big comment
207 * in xfs_iflush() for an explanation of this coordination mechanism.
1da177e4 208 */
1319ebef
DC
209 iip->ili_fields |= (flags | iip->ili_last_fields | iversion_flags);
210 spin_unlock(&iip->ili_lock);
1da177e4 211}
411350df
CH
212
213int
214xfs_trans_roll_inode(
215 struct xfs_trans **tpp,
216 struct xfs_inode *ip)
217{
218 int error;
219
220 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
221 error = xfs_trans_roll(tpp);
222 if (!error)
223 xfs_trans_ijoin(*tpp, ip, 0);
224 return error;
225}