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