xref: /linux-6.15/include/linux/iversion.h (revision 263453d9)
1ae5e165dSJeff Layton /* SPDX-License-Identifier: GPL-2.0 */
2ae5e165dSJeff Layton #ifndef _LINUX_IVERSION_H
3ae5e165dSJeff Layton #define _LINUX_IVERSION_H
4ae5e165dSJeff Layton 
5ae5e165dSJeff Layton #include <linux/fs.h>
6ae5e165dSJeff Layton 
7ae5e165dSJeff Layton /*
8f02a9ad1SJeff Layton  * The inode->i_version field:
9f02a9ad1SJeff Layton  * ---------------------------
10ae5e165dSJeff Layton  * The change attribute (i_version) is mandated by NFSv4 and is mostly for
11ae5e165dSJeff Layton  * knfsd, but is also used for other purposes (e.g. IMA). The i_version must
12a3bb7103SJeff Layton  * appear larger to observers if there was an explicit change to the inode's
13a3bb7103SJeff Layton  * data or metadata since it was last queried.
14a3bb7103SJeff Layton  *
15a3bb7103SJeff Layton  * An explicit change is one that would ordinarily result in a change to the
16a3bb7103SJeff Layton  * inode status change time (aka ctime). i_version must appear to change, even
17a3bb7103SJeff Layton  * if the ctime does not (since the whole point is to avoid missing updates due
18a3bb7103SJeff Layton  * to timestamp granularity). If POSIX or other relevant spec mandates that the
19a3bb7103SJeff Layton  * ctime must change due to an operation, then the i_version counter must be
20a3bb7103SJeff Layton  * incremented as well.
21a3bb7103SJeff Layton  *
22a3bb7103SJeff Layton  * Making the i_version update completely atomic with the operation itself would
23a3bb7103SJeff Layton  * be prohibitively expensive. Traditionally the kernel has updated the times on
24a3bb7103SJeff Layton  * directories after an operation that changes its contents. For regular files,
25a3bb7103SJeff Layton  * the ctime is usually updated before the data is copied into the cache for a
26a3bb7103SJeff Layton  * write. This means that there is a window of time when an observer can
27a3bb7103SJeff Layton  * associate a new timestamp with old file contents. Since the purpose of the
28a3bb7103SJeff Layton  * i_version is to allow for better cache coherency, the i_version must always
29a3bb7103SJeff Layton  * be updated after the results of the operation are visible. Updating it before
30a3bb7103SJeff Layton  * and after a change is also permitted. (Note that no filesystems currently do
31a3bb7103SJeff Layton  * this. Fixing that is a work-in-progress).
32ae5e165dSJeff Layton  *
33ae5e165dSJeff Layton  * Observers see the i_version as a 64-bit number that never decreases. If it
34ae5e165dSJeff Layton  * remains the same since it was last checked, then nothing has changed in the
35ae5e165dSJeff Layton  * inode. If it's different then something has changed. Observers cannot infer
36ae5e165dSJeff Layton  * anything about the nature or magnitude of the changes from the value, only
37ae5e165dSJeff Layton  * that the inode has changed in some fashion.
38ae5e165dSJeff Layton  *
39ae5e165dSJeff Layton  * Not all filesystems properly implement the i_version counter. Subsystems that
40ae5e165dSJeff Layton  * want to use i_version field on an inode should first check whether the
41ae5e165dSJeff Layton  * filesystem sets the SB_I_VERSION flag (usually via the IS_I_VERSION macro).
42ae5e165dSJeff Layton  *
43ae5e165dSJeff Layton  * Those that set SB_I_VERSION will automatically have their i_version counter
44ae5e165dSJeff Layton  * incremented on writes to normal files. If the SB_I_VERSION is not set, then
45ae5e165dSJeff Layton  * the VFS will not touch it on writes, and the filesystem can use it how it
46ae5e165dSJeff Layton  * wishes. Note that the filesystem is always responsible for updating the
47ae5e165dSJeff Layton  * i_version on namespace changes in directories (mkdir, rmdir, unlink, etc.).
48ae5e165dSJeff Layton  * We consider these sorts of filesystems to have a kernel-managed i_version.
49ae5e165dSJeff Layton  *
50ae5e165dSJeff Layton  * It may be impractical for filesystems to keep i_version updates atomic with
51ae5e165dSJeff Layton  * respect to the changes that cause them.  They should, however, guarantee
52ae5e165dSJeff Layton  * that i_version updates are never visible before the changes that caused
53ae5e165dSJeff Layton  * them.  Also, i_version updates should never be delayed longer than it takes
54ae5e165dSJeff Layton  * the original change to reach disk.
55ae5e165dSJeff Layton  *
56f02a9ad1SJeff Layton  * This implementation uses the low bit in the i_version field as a flag to
57f02a9ad1SJeff Layton  * track when the value has been queried. If it has not been queried since it
58f02a9ad1SJeff Layton  * was last incremented, we can skip the increment in most cases.
59f02a9ad1SJeff Layton  *
60f02a9ad1SJeff Layton  * In the event that we're updating the ctime, we will usually go ahead and
61f02a9ad1SJeff Layton  * bump the i_version anyway. Since that has to go to stable storage in some
62f02a9ad1SJeff Layton  * fashion, we might as well increment it as well.
63f02a9ad1SJeff Layton  *
64f02a9ad1SJeff Layton  * With this implementation, the value should always appear to observers to
65f02a9ad1SJeff Layton  * increase over time if the file has changed. It's recommended to use
66c472c07bSGoffredo Baroncelli  * inode_eq_iversion() helper to compare values.
67f02a9ad1SJeff Layton  *
68ae5e165dSJeff Layton  * Note that some filesystems (e.g. NFS and AFS) just use the field to store
69ae5e165dSJeff Layton  * a server-provided value (for the most part). For that reason, those
70ae5e165dSJeff Layton  * filesystems do not set SB_I_VERSION. These filesystems are considered to
71ae5e165dSJeff Layton  * have a self-managed i_version.
72f02a9ad1SJeff Layton  *
73f02a9ad1SJeff Layton  * Persistently storing the i_version
74f02a9ad1SJeff Layton  * ----------------------------------
75f02a9ad1SJeff Layton  * Queries of the i_version field are not gated on them hitting the backing
76f02a9ad1SJeff Layton  * store. It's always possible that the host could crash after allowing
77f02a9ad1SJeff Layton  * a query of the value but before it has made it to disk.
78f02a9ad1SJeff Layton  *
79f02a9ad1SJeff Layton  * To mitigate this problem, filesystems should always use
80f02a9ad1SJeff Layton  * inode_set_iversion_queried when loading an existing inode from disk. This
81f02a9ad1SJeff Layton  * ensures that the next attempted inode increment will result in the value
82f02a9ad1SJeff Layton  * changing.
83f02a9ad1SJeff Layton  *
84f02a9ad1SJeff Layton  * Storing the value to disk therefore does not count as a query, so those
85f02a9ad1SJeff Layton  * filesystems should use inode_peek_iversion to grab the value to be stored.
86f02a9ad1SJeff Layton  * There is no need to flag the value as having been queried in that case.
87ae5e165dSJeff Layton  */
88ae5e165dSJeff Layton 
89f02a9ad1SJeff Layton /*
90f02a9ad1SJeff Layton  * We borrow the lowest bit in the i_version to use as a flag to tell whether
91f02a9ad1SJeff Layton  * it has been queried since we last incremented it. If it has, then we must
92f02a9ad1SJeff Layton  * increment it on the next change. After that, we can clear the flag and
93f02a9ad1SJeff Layton  * avoid incrementing it again until it has again been queried.
94f02a9ad1SJeff Layton  */
95f02a9ad1SJeff Layton #define I_VERSION_QUERIED_SHIFT	(1)
96f02a9ad1SJeff Layton #define I_VERSION_QUERIED	(1ULL << (I_VERSION_QUERIED_SHIFT - 1))
97f02a9ad1SJeff Layton #define I_VERSION_INCREMENT	(1ULL << I_VERSION_QUERIED_SHIFT)
98f02a9ad1SJeff Layton 
99ae5e165dSJeff Layton /**
100ae5e165dSJeff Layton  * inode_set_iversion_raw - set i_version to the specified raw value
101ae5e165dSJeff Layton  * @inode: inode to set
102f02a9ad1SJeff Layton  * @val: new i_version value to set
103ae5e165dSJeff Layton  *
104f02a9ad1SJeff Layton  * Set @inode's i_version field to @val. This function is for use by
105ae5e165dSJeff Layton  * filesystems that self-manage the i_version.
106ae5e165dSJeff Layton  *
107ae5e165dSJeff Layton  * For example, the NFS client stores its NFSv4 change attribute in this way,
108ae5e165dSJeff Layton  * and the AFS client stores the data_version from the server here.
109ae5e165dSJeff Layton  */
110ae5e165dSJeff Layton static inline void
inode_set_iversion_raw(struct inode * inode,u64 val)111f02a9ad1SJeff Layton inode_set_iversion_raw(struct inode *inode, u64 val)
112ae5e165dSJeff Layton {
113f02a9ad1SJeff Layton 	atomic64_set(&inode->i_version, val);
114f02a9ad1SJeff Layton }
115f02a9ad1SJeff Layton 
116f02a9ad1SJeff Layton /**
117f02a9ad1SJeff Layton  * inode_peek_iversion_raw - grab a "raw" iversion value
118f02a9ad1SJeff Layton  * @inode: inode from which i_version should be read
119f02a9ad1SJeff Layton  *
120f02a9ad1SJeff Layton  * Grab a "raw" inode->i_version value and return it. The i_version is not
121f02a9ad1SJeff Layton  * flagged or converted in any way. This is mostly used to access a self-managed
122f02a9ad1SJeff Layton  * i_version.
123f02a9ad1SJeff Layton  *
124f02a9ad1SJeff Layton  * With those filesystems, we want to treat the i_version as an entirely
125f02a9ad1SJeff Layton  * opaque value.
126f02a9ad1SJeff Layton  */
127f02a9ad1SJeff Layton static inline u64
inode_peek_iversion_raw(const struct inode * inode)128f02a9ad1SJeff Layton inode_peek_iversion_raw(const struct inode *inode)
129f02a9ad1SJeff Layton {
130f02a9ad1SJeff Layton 	return atomic64_read(&inode->i_version);
131ae5e165dSJeff Layton }
132ae5e165dSJeff Layton 
133ae5e165dSJeff Layton /**
134441d3676SJeff Layton  * inode_set_max_iversion_raw - update i_version new value is larger
135441d3676SJeff Layton  * @inode: inode to set
136441d3676SJeff Layton  * @val: new i_version to set
137441d3676SJeff Layton  *
138441d3676SJeff Layton  * Some self-managed filesystems (e.g Ceph) will only update the i_version
139441d3676SJeff Layton  * value if the new value is larger than the one we already have.
140441d3676SJeff Layton  */
141441d3676SJeff Layton static inline void
inode_set_max_iversion_raw(struct inode * inode,u64 val)142441d3676SJeff Layton inode_set_max_iversion_raw(struct inode *inode, u64 val)
143441d3676SJeff Layton {
144da3f52baSUros Bizjak 	u64 cur = inode_peek_iversion_raw(inode);
145441d3676SJeff Layton 
146da3f52baSUros Bizjak 	do {
147441d3676SJeff Layton 		if (cur > val)
148441d3676SJeff Layton 			break;
149da3f52baSUros Bizjak 	} while (!atomic64_try_cmpxchg(&inode->i_version, &cur, val));
150441d3676SJeff Layton }
151441d3676SJeff Layton 
152441d3676SJeff Layton /**
153ae5e165dSJeff Layton  * inode_set_iversion - set i_version to a particular value
154ae5e165dSJeff Layton  * @inode: inode to set
155f02a9ad1SJeff Layton  * @val: new i_version value to set
156ae5e165dSJeff Layton  *
157f02a9ad1SJeff Layton  * Set @inode's i_version field to @val. This function is for filesystems with
158f02a9ad1SJeff Layton  * a kernel-managed i_version, for initializing a newly-created inode from
159f02a9ad1SJeff Layton  * scratch.
160ae5e165dSJeff Layton  *
161f02a9ad1SJeff Layton  * In this case, we do not set the QUERIED flag since we know that this value
162f02a9ad1SJeff Layton  * has never been queried.
163ae5e165dSJeff Layton  */
164ae5e165dSJeff Layton static inline void
inode_set_iversion(struct inode * inode,u64 val)165f02a9ad1SJeff Layton inode_set_iversion(struct inode *inode, u64 val)
166ae5e165dSJeff Layton {
167f02a9ad1SJeff Layton 	inode_set_iversion_raw(inode, val << I_VERSION_QUERIED_SHIFT);
168ae5e165dSJeff Layton }
169ae5e165dSJeff Layton 
170ae5e165dSJeff Layton /**
171f02a9ad1SJeff Layton  * inode_set_iversion_queried - set i_version to a particular value as quereied
172ae5e165dSJeff Layton  * @inode: inode to set
173f02a9ad1SJeff Layton  * @val: new i_version value to set
174ae5e165dSJeff Layton  *
175f02a9ad1SJeff Layton  * Set @inode's i_version field to @val, and flag it for increment on the next
176f02a9ad1SJeff Layton  * change.
177ae5e165dSJeff Layton  *
178f02a9ad1SJeff Layton  * Filesystems that persistently store the i_version on disk should use this
179f02a9ad1SJeff Layton  * when loading an existing inode from disk.
180ae5e165dSJeff Layton  *
181f02a9ad1SJeff Layton  * When loading in an i_version value from a backing store, we can't be certain
182f02a9ad1SJeff Layton  * that it wasn't previously viewed before being stored. Thus, we must assume
183f02a9ad1SJeff Layton  * that it was, to ensure that we don't end up handing out the same value for
184f02a9ad1SJeff Layton  * different versions of the same inode.
185ae5e165dSJeff Layton  */
186ae5e165dSJeff Layton static inline void
inode_set_iversion_queried(struct inode * inode,u64 val)187f02a9ad1SJeff Layton inode_set_iversion_queried(struct inode *inode, u64 val)
188ae5e165dSJeff Layton {
189f02a9ad1SJeff Layton 	inode_set_iversion_raw(inode, (val << I_VERSION_QUERIED_SHIFT) |
190f02a9ad1SJeff Layton 				I_VERSION_QUERIED);
191ae5e165dSJeff Layton }
192ae5e165dSJeff Layton 
1935ca14835SAndrew Morton bool inode_maybe_inc_iversion(struct inode *inode, bool force);
1947594c461SJeff Layton 
195ae5e165dSJeff Layton /**
196ae5e165dSJeff Layton  * inode_inc_iversion - forcibly increment i_version
197ae5e165dSJeff Layton  * @inode: inode that needs to be updated
198ae5e165dSJeff Layton  *
199ae5e165dSJeff Layton  * Forcbily increment the i_version field. This always results in a change to
200ae5e165dSJeff Layton  * the observable value.
201ae5e165dSJeff Layton  */
202ae5e165dSJeff Layton static inline void
inode_inc_iversion(struct inode * inode)203ae5e165dSJeff Layton inode_inc_iversion(struct inode *inode)
204ae5e165dSJeff Layton {
205ae5e165dSJeff Layton 	inode_maybe_inc_iversion(inode, true);
206ae5e165dSJeff Layton }
207ae5e165dSJeff Layton 
208ae5e165dSJeff Layton /**
209ae5e165dSJeff Layton  * inode_iversion_need_inc - is the i_version in need of being incremented?
210ae5e165dSJeff Layton  * @inode: inode to check
211ae5e165dSJeff Layton  *
212ae5e165dSJeff Layton  * Returns whether the inode->i_version counter needs incrementing on the next
213f02a9ad1SJeff Layton  * change. Just fetch the value and check the QUERIED flag.
214ae5e165dSJeff Layton  */
215ae5e165dSJeff Layton static inline bool
inode_iversion_need_inc(struct inode * inode)216ae5e165dSJeff Layton inode_iversion_need_inc(struct inode *inode)
217ae5e165dSJeff Layton {
218f02a9ad1SJeff Layton 	return inode_peek_iversion_raw(inode) & I_VERSION_QUERIED;
219ae5e165dSJeff Layton }
220ae5e165dSJeff Layton 
221ae5e165dSJeff Layton /**
222ae5e165dSJeff Layton  * inode_inc_iversion_raw - forcibly increment raw i_version
223ae5e165dSJeff Layton  * @inode: inode that needs to be updated
224ae5e165dSJeff Layton  *
225ae5e165dSJeff Layton  * Forcbily increment the raw i_version field. This always results in a change
226ae5e165dSJeff Layton  * to the raw value.
227ae5e165dSJeff Layton  *
228ae5e165dSJeff Layton  * NFS will use the i_version field to store the value from the server. It
229ae5e165dSJeff Layton  * mostly treats it as opaque, but in the case where it holds a write
230ae5e165dSJeff Layton  * delegation, it must increment the value itself. This function does that.
231ae5e165dSJeff Layton  */
232ae5e165dSJeff Layton static inline void
inode_inc_iversion_raw(struct inode * inode)233ae5e165dSJeff Layton inode_inc_iversion_raw(struct inode *inode)
234ae5e165dSJeff Layton {
235f02a9ad1SJeff Layton 	atomic64_inc(&inode->i_version);
236ae5e165dSJeff Layton }
237ae5e165dSJeff Layton 
238ae5e165dSJeff Layton /**
239ae5e165dSJeff Layton  * inode_peek_iversion - read i_version without flagging it to be incremented
240ae5e165dSJeff Layton  * @inode: inode from which i_version should be read
241ae5e165dSJeff Layton  *
242ae5e165dSJeff Layton  * Read the inode i_version counter for an inode without registering it as a
243ae5e165dSJeff Layton  * query.
244ae5e165dSJeff Layton  *
245ae5e165dSJeff Layton  * This is typically used by local filesystems that need to store an i_version
246ae5e165dSJeff Layton  * on disk. In that situation, it's not necessary to flag it as having been
247ae5e165dSJeff Layton  * viewed, as the result won't be used to gauge changes from that point.
248ae5e165dSJeff Layton  */
249ae5e165dSJeff Layton static inline u64
inode_peek_iversion(const struct inode * inode)250ae5e165dSJeff Layton inode_peek_iversion(const struct inode *inode)
251ae5e165dSJeff Layton {
252f02a9ad1SJeff Layton 	return inode_peek_iversion_raw(inode) >> I_VERSION_QUERIED_SHIFT;
253ae5e165dSJeff Layton }
254ae5e165dSJeff Layton 
2554b03d997SJ. Bruce Fields /*
2564b03d997SJ. Bruce Fields  * For filesystems without any sort of change attribute, the best we can
2574b03d997SJ. Bruce Fields  * do is fake one up from the ctime:
2584b03d997SJ. Bruce Fields  */
time_to_chattr(const struct timespec64 * t)259*263453d9SChuck Lever static inline u64 time_to_chattr(const struct timespec64 *t)
2604b03d997SJ. Bruce Fields {
2614b03d997SJ. Bruce Fields 	u64 chattr = t->tv_sec;
2624b03d997SJ. Bruce Fields 
2634b03d997SJ. Bruce Fields 	chattr <<= 32;
2644b03d997SJ. Bruce Fields 	chattr += t->tv_nsec;
2654b03d997SJ. Bruce Fields 	return chattr;
2664b03d997SJ. Bruce Fields }
2674b03d997SJ. Bruce Fields 
268c5bc1b3fSJeff Layton u64 inode_query_iversion(struct inode *inode);
269c5bc1b3fSJeff Layton 
270ae5e165dSJeff Layton /**
271c472c07bSGoffredo Baroncelli  * inode_eq_iversion_raw - check whether the raw i_version counter has changed
272ae5e165dSJeff Layton  * @inode: inode to check
273ae5e165dSJeff Layton  * @old: old value to check against its i_version
274ae5e165dSJeff Layton  *
275c472c07bSGoffredo Baroncelli  * Compare the current raw i_version counter with a previous one. Returns true
276c472c07bSGoffredo Baroncelli  * if they are the same or false if they are different.
277ae5e165dSJeff Layton  */
278c0cef30eSJeff Layton static inline bool
inode_eq_iversion_raw(const struct inode * inode,u64 old)279c472c07bSGoffredo Baroncelli inode_eq_iversion_raw(const struct inode *inode, u64 old)
280ae5e165dSJeff Layton {
281c472c07bSGoffredo Baroncelli 	return inode_peek_iversion_raw(inode) == old;
282ae5e165dSJeff Layton }
283ae5e165dSJeff Layton 
284ae5e165dSJeff Layton /**
285c472c07bSGoffredo Baroncelli  * inode_eq_iversion - check whether the i_version counter has changed
286ae5e165dSJeff Layton  * @inode: inode to check
287ae5e165dSJeff Layton  * @old: old value to check against its i_version
288ae5e165dSJeff Layton  *
289c472c07bSGoffredo Baroncelli  * Compare an i_version counter with a previous one. Returns true if they are
290c472c07bSGoffredo Baroncelli  * the same, and false if they are different.
291f02a9ad1SJeff Layton  *
292f02a9ad1SJeff Layton  * Note that we don't need to set the QUERIED flag in this case, as the value
293f02a9ad1SJeff Layton  * in the inode is not being recorded for later use.
294ae5e165dSJeff Layton  */
295c0cef30eSJeff Layton static inline bool
inode_eq_iversion(const struct inode * inode,u64 old)296c472c07bSGoffredo Baroncelli inode_eq_iversion(const struct inode *inode, u64 old)
297ae5e165dSJeff Layton {
298c472c07bSGoffredo Baroncelli 	return inode_peek_iversion(inode) == old;
299ae5e165dSJeff Layton }
300ae5e165dSJeff Layton #endif
301