blob: e27bd4f55d840e656a0aa81bd0240a721f147ad8 [file] [log] [blame]
Jeff Laytonae5e1652018-01-29 06:41:30 -05001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_IVERSION_H
3#define _LINUX_IVERSION_H
4
5#include <linux/fs.h>
6
7/*
Jeff Laytonf02a9ad2017-12-21 07:45:44 -05008 * The inode->i_version field:
9 * ---------------------------
Jeff Laytonae5e1652018-01-29 06:41:30 -050010 * The change attribute (i_version) is mandated by NFSv4 and is mostly for
11 * knfsd, but is also used for other purposes (e.g. IMA). The i_version must
12 * appear different to observers if there was a change to the inode's data or
13 * metadata since it was last queried.
14 *
15 * Observers see the i_version as a 64-bit number that never decreases. If it
16 * remains the same since it was last checked, then nothing has changed in the
17 * inode. If it's different then something has changed. Observers cannot infer
18 * anything about the nature or magnitude of the changes from the value, only
19 * that the inode has changed in some fashion.
20 *
21 * Not all filesystems properly implement the i_version counter. Subsystems that
22 * want to use i_version field on an inode should first check whether the
23 * filesystem sets the SB_I_VERSION flag (usually via the IS_I_VERSION macro).
24 *
25 * Those that set SB_I_VERSION will automatically have their i_version counter
26 * incremented on writes to normal files. If the SB_I_VERSION is not set, then
27 * the VFS will not touch it on writes, and the filesystem can use it how it
28 * wishes. Note that the filesystem is always responsible for updating the
29 * i_version on namespace changes in directories (mkdir, rmdir, unlink, etc.).
30 * We consider these sorts of filesystems to have a kernel-managed i_version.
31 *
32 * It may be impractical for filesystems to keep i_version updates atomic with
33 * respect to the changes that cause them. They should, however, guarantee
34 * that i_version updates are never visible before the changes that caused
35 * them. Also, i_version updates should never be delayed longer than it takes
36 * the original change to reach disk.
37 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050038 * This implementation uses the low bit in the i_version field as a flag to
39 * track when the value has been queried. If it has not been queried since it
40 * was last incremented, we can skip the increment in most cases.
41 *
42 * In the event that we're updating the ctime, we will usually go ahead and
43 * bump the i_version anyway. Since that has to go to stable storage in some
44 * fashion, we might as well increment it as well.
45 *
46 * With this implementation, the value should always appear to observers to
47 * increase over time if the file has changed. It's recommended to use
Goffredo Baroncellic472c072018-02-01 08:15:25 -050048 * inode_eq_iversion() helper to compare values.
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050049 *
Jeff Laytonae5e1652018-01-29 06:41:30 -050050 * Note that some filesystems (e.g. NFS and AFS) just use the field to store
51 * a server-provided value (for the most part). For that reason, those
52 * filesystems do not set SB_I_VERSION. These filesystems are considered to
53 * have a self-managed i_version.
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050054 *
55 * Persistently storing the i_version
56 * ----------------------------------
57 * Queries of the i_version field are not gated on them hitting the backing
58 * store. It's always possible that the host could crash after allowing
59 * a query of the value but before it has made it to disk.
60 *
61 * To mitigate this problem, filesystems should always use
62 * inode_set_iversion_queried when loading an existing inode from disk. This
63 * ensures that the next attempted inode increment will result in the value
64 * changing.
65 *
66 * Storing the value to disk therefore does not count as a query, so those
67 * filesystems should use inode_peek_iversion to grab the value to be stored.
68 * There is no need to flag the value as having been queried in that case.
Jeff Laytonae5e1652018-01-29 06:41:30 -050069 */
70
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050071/*
72 * We borrow the lowest bit in the i_version to use as a flag to tell whether
73 * it has been queried since we last incremented it. If it has, then we must
74 * increment it on the next change. After that, we can clear the flag and
75 * avoid incrementing it again until it has again been queried.
76 */
77#define I_VERSION_QUERIED_SHIFT (1)
78#define I_VERSION_QUERIED (1ULL << (I_VERSION_QUERIED_SHIFT - 1))
79#define I_VERSION_INCREMENT (1ULL << I_VERSION_QUERIED_SHIFT)
80
Jeff Laytonae5e1652018-01-29 06:41:30 -050081/**
82 * inode_set_iversion_raw - set i_version to the specified raw value
83 * @inode: inode to set
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050084 * @val: new i_version value to set
Jeff Laytonae5e1652018-01-29 06:41:30 -050085 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050086 * Set @inode's i_version field to @val. This function is for use by
Jeff Laytonae5e1652018-01-29 06:41:30 -050087 * filesystems that self-manage the i_version.
88 *
89 * For example, the NFS client stores its NFSv4 change attribute in this way,
90 * and the AFS client stores the data_version from the server here.
91 */
92static inline void
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050093inode_set_iversion_raw(struct inode *inode, u64 val)
Jeff Laytonae5e1652018-01-29 06:41:30 -050094{
Jeff Laytonf02a9ad2017-12-21 07:45:44 -050095 atomic64_set(&inode->i_version, val);
96}
97
98/**
99 * inode_peek_iversion_raw - grab a "raw" iversion value
100 * @inode: inode from which i_version should be read
101 *
102 * Grab a "raw" inode->i_version value and return it. The i_version is not
103 * flagged or converted in any way. This is mostly used to access a self-managed
104 * i_version.
105 *
106 * With those filesystems, we want to treat the i_version as an entirely
107 * opaque value.
108 */
109static inline u64
110inode_peek_iversion_raw(const struct inode *inode)
111{
112 return atomic64_read(&inode->i_version);
Jeff Laytonae5e1652018-01-29 06:41:30 -0500113}
114
115/**
Jeff Layton441d3672019-06-05 17:24:22 -0400116 * inode_set_max_iversion_raw - update i_version new value is larger
117 * @inode: inode to set
118 * @val: new i_version to set
119 *
120 * Some self-managed filesystems (e.g Ceph) will only update the i_version
121 * value if the new value is larger than the one we already have.
122 */
123static inline void
124inode_set_max_iversion_raw(struct inode *inode, u64 val)
125{
Uros Bizjakda3f52b2022-08-21 21:30:11 +0200126 u64 cur = inode_peek_iversion_raw(inode);
Jeff Layton441d3672019-06-05 17:24:22 -0400127
Uros Bizjakda3f52b2022-08-21 21:30:11 +0200128 do {
Jeff Layton441d3672019-06-05 17:24:22 -0400129 if (cur > val)
130 break;
Uros Bizjakda3f52b2022-08-21 21:30:11 +0200131 } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, val));
Jeff Layton441d3672019-06-05 17:24:22 -0400132}
133
134/**
Jeff Laytonae5e1652018-01-29 06:41:30 -0500135 * inode_set_iversion - set i_version to a particular value
136 * @inode: inode to set
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500137 * @val: new i_version value to set
Jeff Laytonae5e1652018-01-29 06:41:30 -0500138 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500139 * Set @inode's i_version field to @val. This function is for filesystems with
140 * a kernel-managed i_version, for initializing a newly-created inode from
141 * scratch.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500142 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500143 * In this case, we do not set the QUERIED flag since we know that this value
144 * has never been queried.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500145 */
146static inline void
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500147inode_set_iversion(struct inode *inode, u64 val)
Jeff Laytonae5e1652018-01-29 06:41:30 -0500148{
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500149 inode_set_iversion_raw(inode, val << I_VERSION_QUERIED_SHIFT);
Jeff Laytonae5e1652018-01-29 06:41:30 -0500150}
151
152/**
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500153 * inode_set_iversion_queried - set i_version to a particular value as quereied
Jeff Laytonae5e1652018-01-29 06:41:30 -0500154 * @inode: inode to set
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500155 * @val: new i_version value to set
Jeff Laytonae5e1652018-01-29 06:41:30 -0500156 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500157 * Set @inode's i_version field to @val, and flag it for increment on the next
158 * change.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500159 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500160 * Filesystems that persistently store the i_version on disk should use this
161 * when loading an existing inode from disk.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500162 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500163 * When loading in an i_version value from a backing store, we can't be certain
164 * that it wasn't previously viewed before being stored. Thus, we must assume
165 * that it was, to ensure that we don't end up handing out the same value for
166 * different versions of the same inode.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500167 */
168static inline void
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500169inode_set_iversion_queried(struct inode *inode, u64 val)
Jeff Laytonae5e1652018-01-29 06:41:30 -0500170{
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500171 inode_set_iversion_raw(inode, (val << I_VERSION_QUERIED_SHIFT) |
172 I_VERSION_QUERIED);
Jeff Laytonae5e1652018-01-29 06:41:30 -0500173}
174
Andrew Morton5ca14832022-09-09 13:57:41 -0700175bool inode_maybe_inc_iversion(struct inode *inode, bool force);
Jeff Layton7594c462017-12-18 06:25:31 -0500176
Jeff Laytonae5e1652018-01-29 06:41:30 -0500177/**
178 * inode_inc_iversion - forcibly increment i_version
179 * @inode: inode that needs to be updated
180 *
181 * Forcbily increment the i_version field. This always results in a change to
182 * the observable value.
183 */
184static inline void
185inode_inc_iversion(struct inode *inode)
186{
187 inode_maybe_inc_iversion(inode, true);
188}
189
190/**
191 * inode_iversion_need_inc - is the i_version in need of being incremented?
192 * @inode: inode to check
193 *
194 * Returns whether the inode->i_version counter needs incrementing on the next
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500195 * change. Just fetch the value and check the QUERIED flag.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500196 */
197static inline bool
198inode_iversion_need_inc(struct inode *inode)
199{
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500200 return inode_peek_iversion_raw(inode) & I_VERSION_QUERIED;
Jeff Laytonae5e1652018-01-29 06:41:30 -0500201}
202
203/**
204 * inode_inc_iversion_raw - forcibly increment raw i_version
205 * @inode: inode that needs to be updated
206 *
207 * Forcbily increment the raw i_version field. This always results in a change
208 * to the raw value.
209 *
210 * NFS will use the i_version field to store the value from the server. It
211 * mostly treats it as opaque, but in the case where it holds a write
212 * delegation, it must increment the value itself. This function does that.
213 */
214static inline void
215inode_inc_iversion_raw(struct inode *inode)
216{
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500217 atomic64_inc(&inode->i_version);
Jeff Laytonae5e1652018-01-29 06:41:30 -0500218}
219
220/**
221 * inode_peek_iversion - read i_version without flagging it to be incremented
222 * @inode: inode from which i_version should be read
223 *
224 * Read the inode i_version counter for an inode without registering it as a
225 * query.
226 *
227 * This is typically used by local filesystems that need to store an i_version
228 * on disk. In that situation, it's not necessary to flag it as having been
229 * viewed, as the result won't be used to gauge changes from that point.
230 */
231static inline u64
232inode_peek_iversion(const struct inode *inode)
233{
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500234 return inode_peek_iversion_raw(inode) >> I_VERSION_QUERIED_SHIFT;
Jeff Laytonae5e1652018-01-29 06:41:30 -0500235}
236
237/**
238 * inode_query_iversion - read i_version for later use
239 * @inode: inode from which i_version should be read
240 *
241 * Read the inode i_version counter. This should be used by callers that wish
242 * to store the returned i_version for later comparison. This will guarantee
243 * that a later query of the i_version will result in a different value if
244 * anything has changed.
245 *
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500246 * In this implementation, we fetch the current value, set the QUERIED flag and
247 * then try to swap it into place with a cmpxchg, if it wasn't already set. If
248 * that fails, we try again with the newly fetched value from the cmpxchg.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500249 */
250static inline u64
251inode_query_iversion(struct inode *inode)
252{
Uros Bizjakda3f52b2022-08-21 21:30:11 +0200253 u64 cur, new;
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500254
255 cur = inode_peek_iversion_raw(inode);
Uros Bizjakda3f52b2022-08-21 21:30:11 +0200256 do {
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500257 /* If flag is already set, then no need to swap */
258 if (cur & I_VERSION_QUERIED) {
259 /*
260 * This barrier (and the implicit barrier in the
261 * cmpxchg below) pairs with the barrier in
262 * inode_maybe_inc_iversion().
263 */
264 smp_mb();
265 break;
266 }
267
268 new = cur | I_VERSION_QUERIED;
Uros Bizjakda3f52b2022-08-21 21:30:11 +0200269 } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500270 return cur >> I_VERSION_QUERIED_SHIFT;
Jeff Laytonae5e1652018-01-29 06:41:30 -0500271}
272
J. Bruce Fields4b03d992020-11-30 17:46:16 -0500273/*
274 * For filesystems without any sort of change attribute, the best we can
275 * do is fake one up from the ctime:
276 */
277static inline u64 time_to_chattr(struct timespec64 *t)
278{
279 u64 chattr = t->tv_sec;
280
281 chattr <<= 32;
282 chattr += t->tv_nsec;
283 return chattr;
284}
285
Jeff Laytonae5e1652018-01-29 06:41:30 -0500286/**
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500287 * inode_eq_iversion_raw - check whether the raw i_version counter has changed
Jeff Laytonae5e1652018-01-29 06:41:30 -0500288 * @inode: inode to check
289 * @old: old value to check against its i_version
290 *
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500291 * Compare the current raw i_version counter with a previous one. Returns true
292 * if they are the same or false if they are different.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500293 */
Jeff Laytonc0cef302018-01-30 15:32:21 -0500294static inline bool
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500295inode_eq_iversion_raw(const struct inode *inode, u64 old)
Jeff Laytonae5e1652018-01-29 06:41:30 -0500296{
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500297 return inode_peek_iversion_raw(inode) == old;
Jeff Laytonae5e1652018-01-29 06:41:30 -0500298}
299
300/**
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500301 * inode_eq_iversion - check whether the i_version counter has changed
Jeff Laytonae5e1652018-01-29 06:41:30 -0500302 * @inode: inode to check
303 * @old: old value to check against its i_version
304 *
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500305 * Compare an i_version counter with a previous one. Returns true if they are
306 * the same, and false if they are different.
Jeff Laytonf02a9ad2017-12-21 07:45:44 -0500307 *
308 * Note that we don't need to set the QUERIED flag in this case, as the value
309 * in the inode is not being recorded for later use.
Jeff Laytonae5e1652018-01-29 06:41:30 -0500310 */
Jeff Laytonc0cef302018-01-30 15:32:21 -0500311static inline bool
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500312inode_eq_iversion(const struct inode *inode, u64 old)
Jeff Laytonae5e1652018-01-29 06:41:30 -0500313{
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500314 return inode_peek_iversion(inode) == old;
Jeff Laytonae5e1652018-01-29 06:41:30 -0500315}
316#endif