![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 #ifndef _LINUX_IVERSION_H 0003 #define _LINUX_IVERSION_H 0004 0005 #include <linux/fs.h> 0006 0007 /* 0008 * The inode->i_version field: 0009 * --------------------------- 0010 * The change attribute (i_version) is mandated by NFSv4 and is mostly for 0011 * knfsd, but is also used for other purposes (e.g. IMA). The i_version must 0012 * appear different to observers if there was a change to the inode's data or 0013 * metadata since it was last queried. 0014 * 0015 * Observers see the i_version as a 64-bit number that never decreases. If it 0016 * remains the same since it was last checked, then nothing has changed in the 0017 * inode. If it's different then something has changed. Observers cannot infer 0018 * anything about the nature or magnitude of the changes from the value, only 0019 * that the inode has changed in some fashion. 0020 * 0021 * Not all filesystems properly implement the i_version counter. Subsystems that 0022 * want to use i_version field on an inode should first check whether the 0023 * filesystem sets the SB_I_VERSION flag (usually via the IS_I_VERSION macro). 0024 * 0025 * Those that set SB_I_VERSION will automatically have their i_version counter 0026 * incremented on writes to normal files. If the SB_I_VERSION is not set, then 0027 * the VFS will not touch it on writes, and the filesystem can use it how it 0028 * wishes. Note that the filesystem is always responsible for updating the 0029 * i_version on namespace changes in directories (mkdir, rmdir, unlink, etc.). 0030 * We consider these sorts of filesystems to have a kernel-managed i_version. 0031 * 0032 * It may be impractical for filesystems to keep i_version updates atomic with 0033 * respect to the changes that cause them. They should, however, guarantee 0034 * that i_version updates are never visible before the changes that caused 0035 * them. Also, i_version updates should never be delayed longer than it takes 0036 * the original change to reach disk. 0037 * 0038 * This implementation uses the low bit in the i_version field as a flag to 0039 * track when the value has been queried. If it has not been queried since it 0040 * was last incremented, we can skip the increment in most cases. 0041 * 0042 * In the event that we're updating the ctime, we will usually go ahead and 0043 * bump the i_version anyway. Since that has to go to stable storage in some 0044 * fashion, we might as well increment it as well. 0045 * 0046 * With this implementation, the value should always appear to observers to 0047 * increase over time if the file has changed. It's recommended to use 0048 * inode_eq_iversion() helper to compare values. 0049 * 0050 * Note that some filesystems (e.g. NFS and AFS) just use the field to store 0051 * a server-provided value (for the most part). For that reason, those 0052 * filesystems do not set SB_I_VERSION. These filesystems are considered to 0053 * have a self-managed i_version. 0054 * 0055 * Persistently storing the i_version 0056 * ---------------------------------- 0057 * Queries of the i_version field are not gated on them hitting the backing 0058 * store. It's always possible that the host could crash after allowing 0059 * a query of the value but before it has made it to disk. 0060 * 0061 * To mitigate this problem, filesystems should always use 0062 * inode_set_iversion_queried when loading an existing inode from disk. This 0063 * ensures that the next attempted inode increment will result in the value 0064 * changing. 0065 * 0066 * Storing the value to disk therefore does not count as a query, so those 0067 * filesystems should use inode_peek_iversion to grab the value to be stored. 0068 * There is no need to flag the value as having been queried in that case. 0069 */ 0070 0071 /* 0072 * We borrow the lowest bit in the i_version to use as a flag to tell whether 0073 * it has been queried since we last incremented it. If it has, then we must 0074 * increment it on the next change. After that, we can clear the flag and 0075 * avoid incrementing it again until it has again been queried. 0076 */ 0077 #define I_VERSION_QUERIED_SHIFT (1) 0078 #define I_VERSION_QUERIED (1ULL << (I_VERSION_QUERIED_SHIFT - 1)) 0079 #define I_VERSION_INCREMENT (1ULL << I_VERSION_QUERIED_SHIFT) 0080 0081 /** 0082 * inode_set_iversion_raw - set i_version to the specified raw value 0083 * @inode: inode to set 0084 * @val: new i_version value to set 0085 * 0086 * Set @inode's i_version field to @val. This function is for use by 0087 * filesystems that self-manage the i_version. 0088 * 0089 * For example, the NFS client stores its NFSv4 change attribute in this way, 0090 * and the AFS client stores the data_version from the server here. 0091 */ 0092 static inline void 0093 inode_set_iversion_raw(struct inode *inode, u64 val) 0094 { 0095 atomic64_set(&inode->i_version, val); 0096 } 0097 0098 /** 0099 * inode_peek_iversion_raw - grab a "raw" iversion value 0100 * @inode: inode from which i_version should be read 0101 * 0102 * Grab a "raw" inode->i_version value and return it. The i_version is not 0103 * flagged or converted in any way. This is mostly used to access a self-managed 0104 * i_version. 0105 * 0106 * With those filesystems, we want to treat the i_version as an entirely 0107 * opaque value. 0108 */ 0109 static inline u64 0110 inode_peek_iversion_raw(const struct inode *inode) 0111 { 0112 return atomic64_read(&inode->i_version); 0113 } 0114 0115 /** 0116 * inode_set_max_iversion_raw - update i_version new value is larger 0117 * @inode: inode to set 0118 * @val: new i_version to set 0119 * 0120 * Some self-managed filesystems (e.g Ceph) will only update the i_version 0121 * value if the new value is larger than the one we already have. 0122 */ 0123 static inline void 0124 inode_set_max_iversion_raw(struct inode *inode, u64 val) 0125 { 0126 u64 cur, old; 0127 0128 cur = inode_peek_iversion_raw(inode); 0129 for (;;) { 0130 if (cur > val) 0131 break; 0132 old = atomic64_cmpxchg(&inode->i_version, cur, val); 0133 if (likely(old == cur)) 0134 break; 0135 cur = old; 0136 } 0137 } 0138 0139 /** 0140 * inode_set_iversion - set i_version to a particular value 0141 * @inode: inode to set 0142 * @val: new i_version value to set 0143 * 0144 * Set @inode's i_version field to @val. This function is for filesystems with 0145 * a kernel-managed i_version, for initializing a newly-created inode from 0146 * scratch. 0147 * 0148 * In this case, we do not set the QUERIED flag since we know that this value 0149 * has never been queried. 0150 */ 0151 static inline void 0152 inode_set_iversion(struct inode *inode, u64 val) 0153 { 0154 inode_set_iversion_raw(inode, val << I_VERSION_QUERIED_SHIFT); 0155 } 0156 0157 /** 0158 * inode_set_iversion_queried - set i_version to a particular value as quereied 0159 * @inode: inode to set 0160 * @val: new i_version value to set 0161 * 0162 * Set @inode's i_version field to @val, and flag it for increment on the next 0163 * change. 0164 * 0165 * Filesystems that persistently store the i_version on disk should use this 0166 * when loading an existing inode from disk. 0167 * 0168 * When loading in an i_version value from a backing store, we can't be certain 0169 * that it wasn't previously viewed before being stored. Thus, we must assume 0170 * that it was, to ensure that we don't end up handing out the same value for 0171 * different versions of the same inode. 0172 */ 0173 static inline void 0174 inode_set_iversion_queried(struct inode *inode, u64 val) 0175 { 0176 inode_set_iversion_raw(inode, (val << I_VERSION_QUERIED_SHIFT) | 0177 I_VERSION_QUERIED); 0178 } 0179 0180 /** 0181 * inode_maybe_inc_iversion - increments i_version 0182 * @inode: inode with the i_version that should be updated 0183 * @force: increment the counter even if it's not necessary? 0184 * 0185 * Every time the inode is modified, the i_version field must be seen to have 0186 * changed by any observer. 0187 * 0188 * If "force" is set or the QUERIED flag is set, then ensure that we increment 0189 * the value, and clear the queried flag. 0190 * 0191 * In the common case where neither is set, then we can return "false" without 0192 * updating i_version. 0193 * 0194 * If this function returns false, and no other metadata has changed, then we 0195 * can avoid logging the metadata. 0196 */ 0197 static inline bool 0198 inode_maybe_inc_iversion(struct inode *inode, bool force) 0199 { 0200 u64 cur, old, new; 0201 0202 /* 0203 * The i_version field is not strictly ordered with any other inode 0204 * information, but the legacy inode_inc_iversion code used a spinlock 0205 * to serialize increments. 0206 * 0207 * Here, we add full memory barriers to ensure that any de-facto 0208 * ordering with other info is preserved. 0209 * 0210 * This barrier pairs with the barrier in inode_query_iversion() 0211 */ 0212 smp_mb(); 0213 cur = inode_peek_iversion_raw(inode); 0214 for (;;) { 0215 /* If flag is clear then we needn't do anything */ 0216 if (!force && !(cur & I_VERSION_QUERIED)) 0217 return false; 0218 0219 /* Since lowest bit is flag, add 2 to avoid it */ 0220 new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT; 0221 0222 old = atomic64_cmpxchg(&inode->i_version, cur, new); 0223 if (likely(old == cur)) 0224 break; 0225 cur = old; 0226 } 0227 return true; 0228 } 0229 0230 0231 /** 0232 * inode_inc_iversion - forcibly increment i_version 0233 * @inode: inode that needs to be updated 0234 * 0235 * Forcbily increment the i_version field. This always results in a change to 0236 * the observable value. 0237 */ 0238 static inline void 0239 inode_inc_iversion(struct inode *inode) 0240 { 0241 inode_maybe_inc_iversion(inode, true); 0242 } 0243 0244 /** 0245 * inode_iversion_need_inc - is the i_version in need of being incremented? 0246 * @inode: inode to check 0247 * 0248 * Returns whether the inode->i_version counter needs incrementing on the next 0249 * change. Just fetch the value and check the QUERIED flag. 0250 */ 0251 static inline bool 0252 inode_iversion_need_inc(struct inode *inode) 0253 { 0254 return inode_peek_iversion_raw(inode) & I_VERSION_QUERIED; 0255 } 0256 0257 /** 0258 * inode_inc_iversion_raw - forcibly increment raw i_version 0259 * @inode: inode that needs to be updated 0260 * 0261 * Forcbily increment the raw i_version field. This always results in a change 0262 * to the raw value. 0263 * 0264 * NFS will use the i_version field to store the value from the server. It 0265 * mostly treats it as opaque, but in the case where it holds a write 0266 * delegation, it must increment the value itself. This function does that. 0267 */ 0268 static inline void 0269 inode_inc_iversion_raw(struct inode *inode) 0270 { 0271 atomic64_inc(&inode->i_version); 0272 } 0273 0274 /** 0275 * inode_peek_iversion - read i_version without flagging it to be incremented 0276 * @inode: inode from which i_version should be read 0277 * 0278 * Read the inode i_version counter for an inode without registering it as a 0279 * query. 0280 * 0281 * This is typically used by local filesystems that need to store an i_version 0282 * on disk. In that situation, it's not necessary to flag it as having been 0283 * viewed, as the result won't be used to gauge changes from that point. 0284 */ 0285 static inline u64 0286 inode_peek_iversion(const struct inode *inode) 0287 { 0288 return inode_peek_iversion_raw(inode) >> I_VERSION_QUERIED_SHIFT; 0289 } 0290 0291 /** 0292 * inode_query_iversion - read i_version for later use 0293 * @inode: inode from which i_version should be read 0294 * 0295 * Read the inode i_version counter. This should be used by callers that wish 0296 * to store the returned i_version for later comparison. This will guarantee 0297 * that a later query of the i_version will result in a different value if 0298 * anything has changed. 0299 * 0300 * In this implementation, we fetch the current value, set the QUERIED flag and 0301 * then try to swap it into place with a cmpxchg, if it wasn't already set. If 0302 * that fails, we try again with the newly fetched value from the cmpxchg. 0303 */ 0304 static inline u64 0305 inode_query_iversion(struct inode *inode) 0306 { 0307 u64 cur, old, new; 0308 0309 cur = inode_peek_iversion_raw(inode); 0310 for (;;) { 0311 /* If flag is already set, then no need to swap */ 0312 if (cur & I_VERSION_QUERIED) { 0313 /* 0314 * This barrier (and the implicit barrier in the 0315 * cmpxchg below) pairs with the barrier in 0316 * inode_maybe_inc_iversion(). 0317 */ 0318 smp_mb(); 0319 break; 0320 } 0321 0322 new = cur | I_VERSION_QUERIED; 0323 old = atomic64_cmpxchg(&inode->i_version, cur, new); 0324 if (likely(old == cur)) 0325 break; 0326 cur = old; 0327 } 0328 return cur >> I_VERSION_QUERIED_SHIFT; 0329 } 0330 0331 /* 0332 * For filesystems without any sort of change attribute, the best we can 0333 * do is fake one up from the ctime: 0334 */ 0335 static inline u64 time_to_chattr(struct timespec64 *t) 0336 { 0337 u64 chattr = t->tv_sec; 0338 0339 chattr <<= 32; 0340 chattr += t->tv_nsec; 0341 return chattr; 0342 } 0343 0344 /** 0345 * inode_eq_iversion_raw - check whether the raw i_version counter has changed 0346 * @inode: inode to check 0347 * @old: old value to check against its i_version 0348 * 0349 * Compare the current raw i_version counter with a previous one. Returns true 0350 * if they are the same or false if they are different. 0351 */ 0352 static inline bool 0353 inode_eq_iversion_raw(const struct inode *inode, u64 old) 0354 { 0355 return inode_peek_iversion_raw(inode) == old; 0356 } 0357 0358 /** 0359 * inode_eq_iversion - check whether the i_version counter has changed 0360 * @inode: inode to check 0361 * @old: old value to check against its i_version 0362 * 0363 * Compare an i_version counter with a previous one. Returns true if they are 0364 * the same, and false if they are different. 0365 * 0366 * Note that we don't need to set the QUERIED flag in this case, as the value 0367 * in the inode is not being recorded for later use. 0368 */ 0369 static inline bool 0370 inode_eq_iversion(const struct inode *inode, u64 old) 0371 { 0372 return inode_peek_iversion(inode) == old; 0373 } 0374 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |