Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/fs/nfs/inode.c | |
4 | * | |
5 | * Copyright (C) 1992 Rick Sladkey | |
6 | * | |
7 | * nfs inode and superblock handling functions | |
8 | * | |
526719ba | 9 | * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some |
1da177e4 LT |
10 | * experimental NFS changes. Modularisation taken straight from SYS5 fs. |
11 | * | |
12 | * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts. | |
13 | * J.S.Peatfield@damtp.cam.ac.uk | |
14 | * | |
15 | */ | |
16 | ||
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/init.h> | |
174cd4b1 | 19 | #include <linux/sched/signal.h> |
1da177e4 LT |
20 | #include <linux/time.h> |
21 | #include <linux/kernel.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/string.h> | |
24 | #include <linux/stat.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/unistd.h> | |
27 | #include <linux/sunrpc/clnt.h> | |
28 | #include <linux/sunrpc/stats.h> | |
4ece3a2d | 29 | #include <linux/sunrpc/metrics.h> |
1da177e4 LT |
30 | #include <linux/nfs_fs.h> |
31 | #include <linux/nfs_mount.h> | |
32 | #include <linux/nfs4_mount.h> | |
33 | #include <linux/lockd/bind.h> | |
1da177e4 LT |
34 | #include <linux/seq_file.h> |
35 | #include <linux/mount.h> | |
1da177e4 | 36 | #include <linux/vfs.h> |
9cdb3883 MN |
37 | #include <linux/inet.h> |
38 | #include <linux/nfs_xdr.h> | |
5a0e3ad6 | 39 | #include <linux/slab.h> |
3fa0b4e2 | 40 | #include <linux/compat.h> |
d310310c | 41 | #include <linux/freezer.h> |
7c0f6ba6 | 42 | #include <linux/uaccess.h> |
1eb5d98f | 43 | #include <linux/iversion.h> |
1da177e4 | 44 | |
4ce79717 | 45 | #include "nfs4_fs.h" |
a72b4422 | 46 | #include "callback.h" |
1da177e4 | 47 | #include "delegation.h" |
d9ef5a8c | 48 | #include "iostat.h" |
f7b422b1 | 49 | #include "internal.h" |
8ec442ae | 50 | #include "fscache.h" |
e5e94017 | 51 | #include "pnfs.h" |
ab7017a3 | 52 | #include "nfs.h" |
1b340d01 | 53 | #include "netns.h" |
996bc4f4 | 54 | #include "sysfs.h" |
1da177e4 | 55 | |
f4ce1299 TM |
56 | #include "nfstrace.h" |
57 | ||
1da177e4 | 58 | #define NFSDBG_FACILITY NFSDBG_VFS |
1da177e4 | 59 | |
f43bf0be TM |
60 | #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1 |
61 | ||
62 | /* Default is to see 64-bit inode numbers */ | |
90ab5ee9 | 63 | static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED; |
f43bf0be | 64 | |
24aa1fe6 | 65 | static int nfs_update_inode(struct inode *, struct nfs_fattr *); |
1da177e4 | 66 | |
e18b890b | 67 | static struct kmem_cache * nfs_inode_cachep; |
b7fa0554 | 68 | |
1da177e4 LT |
69 | static inline unsigned long |
70 | nfs_fattr_to_ino_t(struct nfs_fattr *fattr) | |
71 | { | |
72 | return nfs_fileid_to_ino_t(fattr->fileid); | |
73 | } | |
74 | ||
f5d39b02 | 75 | int nfs_wait_bit_killable(struct wait_bit_key *key, int mode) |
72cb77f4 | 76 | { |
8d3ca331 TM |
77 | if (unlikely(nfs_current_task_exiting())) |
78 | return -EINTR; | |
f5d39b02 | 79 | schedule(); |
dfd01f02 PZ |
80 | if (signal_pending_state(mode, current)) |
81 | return -ERESTARTSYS; | |
72cb77f4 TM |
82 | return 0; |
83 | } | |
89d77c8f | 84 | EXPORT_SYMBOL_GPL(nfs_wait_bit_killable); |
72cb77f4 | 85 | |
f43bf0be TM |
86 | /** |
87 | * nfs_compat_user_ino64 - returns the user-visible inode number | |
88 | * @fileid: 64-bit fileid | |
89 | * | |
90 | * This function returns a 32-bit inode number if the boot parameter | |
91 | * nfs.enable_ino64 is zero. | |
92 | */ | |
93 | u64 nfs_compat_user_ino64(u64 fileid) | |
94 | { | |
3fa0b4e2 FF |
95 | #ifdef CONFIG_COMPAT |
96 | compat_ulong_t ino; | |
97 | #else | |
98 | unsigned long ino; | |
99 | #endif | |
f43bf0be TM |
100 | |
101 | if (enable_ino64) | |
102 | return fileid; | |
103 | ino = fileid; | |
104 | if (sizeof(ino) < sizeof(fileid)) | |
105 | ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8; | |
106 | return ino; | |
107 | } | |
108 | ||
eed99357 TM |
109 | int nfs_drop_inode(struct inode *inode) |
110 | { | |
111 | return NFS_STALE(inode) || generic_drop_inode(inode); | |
112 | } | |
113 | EXPORT_SYMBOL_GPL(nfs_drop_inode); | |
114 | ||
19d87ca3 | 115 | void nfs_clear_inode(struct inode *inode) |
1da177e4 | 116 | { |
da6d503a TM |
117 | /* |
118 | * The following should never happen... | |
119 | */ | |
f48407dd TM |
120 | WARN_ON_ONCE(nfs_have_writebacks(inode)); |
121 | WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files)); | |
ada70d94 | 122 | nfs_zap_acl_cache(inode); |
1c3c07e9 | 123 | nfs_access_zap_cache(inode); |
f1fe29b4 | 124 | nfs_fscache_clear_inode(inode); |
1da177e4 | 125 | } |
89d77c8f | 126 | EXPORT_SYMBOL_GPL(nfs_clear_inode); |
1da177e4 | 127 | |
b57922d9 AV |
128 | void nfs_evict_inode(struct inode *inode) |
129 | { | |
91b0abe3 | 130 | truncate_inode_pages_final(&inode->i_data); |
dbd5768f | 131 | clear_inode(inode); |
b57922d9 AV |
132 | nfs_clear_inode(inode); |
133 | } | |
134 | ||
9e1681c2 | 135 | int nfs_sync_inode(struct inode *inode) |
4d346bea | 136 | { |
95d9f6c3 | 137 | inode_dio_wait(inode); |
4d346bea TM |
138 | return nfs_wb_all(inode); |
139 | } | |
9e1681c2 | 140 | EXPORT_SYMBOL_GPL(nfs_sync_inode); |
4d346bea | 141 | |
29884df0 TM |
142 | /** |
143 | * nfs_sync_mapping - helper to flush all mmapped dirty data to disk | |
302fad7b | 144 | * @mapping: pointer to struct address_space |
29884df0 TM |
145 | */ |
146 | int nfs_sync_mapping(struct address_space *mapping) | |
147 | { | |
5cf95214 | 148 | int ret = 0; |
29884df0 | 149 | |
5cf95214 TM |
150 | if (mapping->nrpages != 0) { |
151 | unmap_mapping_range(mapping, 0, 0, 0); | |
152 | ret = nfs_wb_all(mapping->host); | |
153 | } | |
29884df0 TM |
154 | return ret; |
155 | } | |
156 | ||
187e593d TM |
157 | static int nfs_attribute_timeout(struct inode *inode) |
158 | { | |
159 | struct nfs_inode *nfsi = NFS_I(inode); | |
160 | ||
161 | return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo); | |
162 | } | |
163 | ||
13c0b082 TM |
164 | static bool nfs_check_cache_flags_invalid(struct inode *inode, |
165 | unsigned long flags) | |
61540bf6 TM |
166 | { |
167 | unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); | |
168 | ||
61540bf6 TM |
169 | return (cache_validity & flags) != 0; |
170 | } | |
171 | ||
61540bf6 TM |
172 | bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags) |
173 | { | |
13c0b082 TM |
174 | if (nfs_check_cache_flags_invalid(inode, flags)) |
175 | return true; | |
176 | return nfs_attribute_cache_expired(inode); | |
61540bf6 | 177 | } |
95ad37f9 | 178 | EXPORT_SYMBOL_GPL(nfs_check_cache_invalid); |
61540bf6 | 179 | |
848fdd62 TM |
180 | #ifdef CONFIG_NFS_V4_2 |
181 | static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi) | |
182 | { | |
183 | return nfsi->xattr_cache != NULL; | |
184 | } | |
185 | #else | |
186 | static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi) | |
187 | { | |
188 | return false; | |
189 | } | |
190 | #endif | |
191 | ||
fd6d3fee | 192 | void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) |
6edf9609 TM |
193 | { |
194 | struct nfs_inode *nfsi = NFS_I(inode); | |
3f0b3cf4 | 195 | |
4201916f | 196 | if (nfs_have_delegated_attributes(inode)) { |
3f0b3cf4 | 197 | if (!(flags & NFS_INO_REVAL_FORCED)) |
720869eb | 198 | flags &= ~(NFS_INO_INVALID_MODE | |
cc7f2dae TM |
199 | NFS_INO_INVALID_OTHER | |
200 | NFS_INO_INVALID_XATTR); | |
201 | flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE); | |
41e97b7f | 202 | } |
6edf9609 | 203 | |
848fdd62 TM |
204 | if (!nfs_has_xattr_cache(nfsi)) |
205 | flags &= ~NFS_INO_INVALID_XATTR; | |
beab450d | 206 | if (flags & NFS_INO_INVALID_DATA) |
a6b5a28e | 207 | nfs_fscache_invalidate(inode, 0); |
41e97b7f | 208 | flags &= ~NFS_INO_REVAL_FORCED; |
488796ec | 209 | |
867da60d MS |
210 | flags |= nfsi->cache_validity; |
211 | if (inode->i_mapping->nrpages == 0) | |
212 | flags &= ~NFS_INO_INVALID_DATA; | |
488796ec | 213 | |
867da60d MS |
214 | /* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */ |
215 | smp_store_release(&nfsi->cache_validity, flags); | |
216 | ||
217 | if (inode->i_mapping->nrpages == 0 || | |
218 | nfsi->cache_validity & NFS_INO_INVALID_DATA) { | |
3db63daa N |
219 | nfs_ooo_clear(nfsi); |
220 | } | |
93c2e5e0 | 221 | trace_nfs_set_cache_invalid(inode, 0); |
6edf9609 | 222 | } |
b6f80a2e | 223 | EXPORT_SYMBOL_GPL(nfs_set_cache_invalid); |
6edf9609 | 224 | |
1da177e4 LT |
225 | /* |
226 | * Invalidate the local caches | |
227 | */ | |
b37b03b7 | 228 | static void nfs_zap_caches_locked(struct inode *inode) |
1da177e4 LT |
229 | { |
230 | struct nfs_inode *nfsi = NFS_I(inode); | |
231 | int mode = inode->i_mode; | |
232 | ||
91d5b470 CL |
233 | nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); |
234 | ||
c7c20973 TM |
235 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
236 | nfsi->attrtimeo_timestamp = jiffies; | |
1da177e4 | 237 | |
88a6099f TM |
238 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) |
239 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR | | |
240 | NFS_INO_INVALID_DATA | | |
241 | NFS_INO_INVALID_ACCESS | | |
242 | NFS_INO_INVALID_ACL | | |
243 | NFS_INO_INVALID_XATTR); | |
244 | else | |
245 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR | | |
246 | NFS_INO_INVALID_ACCESS | | |
247 | NFS_INO_INVALID_ACL | | |
248 | NFS_INO_INVALID_XATTR); | |
fd1defc2 | 249 | nfs_zap_label_cache_locked(nfsi); |
b37b03b7 | 250 | } |
dc59250c | 251 | |
b37b03b7 TM |
252 | void nfs_zap_caches(struct inode *inode) |
253 | { | |
254 | spin_lock(&inode->i_lock); | |
255 | nfs_zap_caches_locked(inode); | |
dc59250c | 256 | spin_unlock(&inode->i_lock); |
ada70d94 TM |
257 | } |
258 | ||
cd9ae2b6 TM |
259 | void nfs_zap_mapping(struct inode *inode, struct address_space *mapping) |
260 | { | |
261 | if (mapping->nrpages != 0) { | |
262 | spin_lock(&inode->i_lock); | |
6edf9609 | 263 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
cd9ae2b6 TM |
264 | spin_unlock(&inode->i_lock); |
265 | } | |
266 | } | |
267 | ||
f41f7418 | 268 | void nfs_zap_acl_cache(struct inode *inode) |
ada70d94 TM |
269 | { |
270 | void (*clear_acl_cache)(struct inode *); | |
271 | ||
272 | clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache; | |
273 | if (clear_acl_cache != NULL) | |
274 | clear_acl_cache(inode); | |
dc59250c | 275 | spin_lock(&inode->i_lock); |
55296809 | 276 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL; |
dc59250c | 277 | spin_unlock(&inode->i_lock); |
1da177e4 | 278 | } |
1c606fb7 | 279 | EXPORT_SYMBOL_GPL(nfs_zap_acl_cache); |
1da177e4 | 280 | |
c4812998 TM |
281 | void nfs_invalidate_atime(struct inode *inode) |
282 | { | |
e12912d9 TM |
283 | if (nfs_have_delegated_atime(inode)) |
284 | return; | |
c4812998 | 285 | spin_lock(&inode->i_lock); |
6edf9609 | 286 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); |
c4812998 TM |
287 | spin_unlock(&inode->i_lock); |
288 | } | |
ddda8e0a | 289 | EXPORT_SYMBOL_GPL(nfs_invalidate_atime); |
c4812998 | 290 | |
1da177e4 | 291 | /* |
b37b03b7 TM |
292 | * Invalidate, but do not unhash, the inode. |
293 | * NB: must be called with inode->i_lock held! | |
1da177e4 | 294 | */ |
93ce4af7 | 295 | static void nfs_set_inode_stale_locked(struct inode *inode) |
1da177e4 | 296 | { |
3a10c30a | 297 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
b37b03b7 | 298 | nfs_zap_caches_locked(inode); |
93ce4af7 TM |
299 | trace_nfs_set_inode_stale(inode); |
300 | } | |
301 | ||
302 | void nfs_set_inode_stale(struct inode *inode) | |
303 | { | |
304 | spin_lock(&inode->i_lock); | |
305 | nfs_set_inode_stale_locked(inode); | |
306 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
307 | } |
308 | ||
309 | struct nfs_find_desc { | |
310 | struct nfs_fh *fh; | |
311 | struct nfs_fattr *fattr; | |
312 | }; | |
313 | ||
314 | /* | |
315 | * In NFSv3 we can have 64bit inode numbers. In order to support | |
316 | * this, and re-exported directories (also seen in NFSv2) | |
317 | * we are forced to allow 2 different inodes to have the same | |
318 | * i_ino. | |
319 | */ | |
320 | static int | |
321 | nfs_find_actor(struct inode *inode, void *opaque) | |
322 | { | |
7e7ce2cc | 323 | struct nfs_find_desc *desc = opaque; |
1da177e4 LT |
324 | struct nfs_fh *fh = desc->fh; |
325 | struct nfs_fattr *fattr = desc->fattr; | |
326 | ||
327 | if (NFS_FILEID(inode) != fattr->fileid) | |
328 | return 0; | |
6e3e2c43 | 329 | if (inode_wrong_type(inode, fattr->mode)) |
f6488c9b | 330 | return 0; |
1da177e4 LT |
331 | if (nfs_compare_fh(NFS_FH(inode), fh)) |
332 | return 0; | |
333 | if (is_bad_inode(inode) || NFS_STALE(inode)) | |
334 | return 0; | |
335 | return 1; | |
336 | } | |
337 | ||
338 | static int | |
339 | nfs_init_locked(struct inode *inode, void *opaque) | |
340 | { | |
7e7ce2cc | 341 | struct nfs_find_desc *desc = opaque; |
1da177e4 LT |
342 | struct nfs_fattr *fattr = desc->fattr; |
343 | ||
99fadcd7 | 344 | set_nfs_fileid(inode, fattr->fileid); |
916ec34d | 345 | inode->i_mode = fattr->mode; |
1da177e4 LT |
346 | nfs_copy_fh(NFS_FH(inode), desc->fh); |
347 | return 0; | |
348 | } | |
349 | ||
e058f70b | 350 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
fd1defc2 TM |
351 | static void nfs_clear_label_invalid(struct inode *inode) |
352 | { | |
353 | spin_lock(&inode->i_lock); | |
354 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL; | |
355 | spin_unlock(&inode->i_lock); | |
356 | } | |
357 | ||
dd225cb3 | 358 | void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr) |
aa9c2669 DQ |
359 | { |
360 | int error; | |
361 | ||
dd225cb3 | 362 | if (fattr->label == NULL) |
aa9c2669 DQ |
363 | return; |
364 | ||
aa9c2669 | 365 | if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) { |
dd225cb3 AS |
366 | error = security_inode_notifysecctx(inode, fattr->label->label, |
367 | fattr->label->len); | |
aa9c2669 DQ |
368 | if (error) |
369 | printk(KERN_ERR "%s() %s %d " | |
370 | "security_inode_notifysecctx() %d\n", | |
371 | __func__, | |
dd225cb3 AS |
372 | (char *)fattr->label->label, |
373 | fattr->label->len, error); | |
fd1defc2 | 374 | nfs_clear_label_invalid(inode); |
aa9c2669 DQ |
375 | } |
376 | } | |
377 | ||
e058f70b SD |
378 | struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) |
379 | { | |
e48c81bb | 380 | struct nfs4_label *label; |
e058f70b SD |
381 | |
382 | if (!(server->caps & NFS_CAP_SECURITY_LABEL)) | |
e48c81bb | 383 | return NULL; |
e058f70b SD |
384 | |
385 | label = kzalloc(sizeof(struct nfs4_label), flags); | |
386 | if (label == NULL) | |
387 | return ERR_PTR(-ENOMEM); | |
388 | ||
389 | label->label = kzalloc(NFS4_MAXLABELLEN, flags); | |
390 | if (label->label == NULL) { | |
391 | kfree(label); | |
392 | return ERR_PTR(-ENOMEM); | |
393 | } | |
394 | label->len = NFS4_MAXLABELLEN; | |
395 | ||
396 | return label; | |
397 | } | |
398 | EXPORT_SYMBOL_GPL(nfs4_label_alloc); | |
aa9c2669 | 399 | #else |
dd225cb3 | 400 | void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr) |
aa9c2669 DQ |
401 | { |
402 | } | |
e058f70b | 403 | #endif |
aa9c2669 | 404 | EXPORT_SYMBOL_GPL(nfs_setsecurity); |
e058f70b | 405 | |
f174ff7a PT |
406 | /* Search for inode identified by fh, fileid and i_mode in inode cache. */ |
407 | struct inode * | |
408 | nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh) | |
409 | { | |
410 | struct nfs_find_desc desc = { | |
411 | .fh = fh, | |
412 | .fattr = fattr, | |
413 | }; | |
414 | struct inode *inode; | |
415 | unsigned long hash; | |
416 | ||
417 | if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) || | |
418 | !(fattr->valid & NFS_ATTR_FATTR_TYPE)) | |
419 | return NULL; | |
420 | ||
421 | hash = nfs_fattr_to_ino_t(fattr); | |
422 | inode = ilookup5(sb, hash, nfs_find_actor, &desc); | |
423 | ||
424 | dprintk("%s: returning %p\n", __func__, inode); | |
425 | return inode; | |
426 | } | |
427 | ||
e591b298 TM |
428 | static void nfs_inode_init_regular(struct nfs_inode *nfsi) |
429 | { | |
430 | atomic_long_set(&nfsi->nrequests, 0); | |
67f4b5dc | 431 | atomic_long_set(&nfsi->redirtied_pages, 0); |
e591b298 TM |
432 | INIT_LIST_HEAD(&nfsi->commit_info.list); |
433 | atomic_long_set(&nfsi->commit_info.ncommit, 0); | |
434 | atomic_set(&nfsi->commit_info.rpcs_out, 0); | |
435 | mutex_init(&nfsi->commit_mutex); | |
436 | } | |
437 | ||
438 | static void nfs_inode_init_dir(struct nfs_inode *nfsi) | |
439 | { | |
440 | nfsi->cache_change_attribute = 0; | |
441 | memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf)); | |
442 | init_rwsem(&nfsi->rmdir_sem); | |
443 | } | |
444 | ||
1da177e4 LT |
445 | /* |
446 | * This is our front-end to iget that looks up inodes by file handle | |
447 | * instead of inode number. | |
448 | */ | |
449 | struct inode * | |
cf7ab00a | 450 | nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr) |
1da177e4 LT |
451 | { |
452 | struct nfs_find_desc desc = { | |
453 | .fh = fh, | |
454 | .fattr = fattr | |
455 | }; | |
03f28e3a | 456 | struct inode *inode = ERR_PTR(-ENOENT); |
ce62b114 | 457 | u64 fattr_supported = NFS_SB(sb)->fattr_valid; |
1da177e4 LT |
458 | unsigned long hash; |
459 | ||
7ebb9315 BS |
460 | nfs_attr_check_mountpoint(sb, fattr); |
461 | ||
2ef47eb1 AS |
462 | if (nfs_attr_use_mounted_on_fileid(fattr)) |
463 | fattr->fileid = fattr->mounted_on_fileid; | |
464 | else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) | |
1da177e4 | 465 | goto out_no_inode; |
9e6e70f8 | 466 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0) |
1da177e4 | 467 | goto out_no_inode; |
1da177e4 LT |
468 | |
469 | hash = nfs_fattr_to_ino_t(fattr); | |
470 | ||
03f28e3a TM |
471 | inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc); |
472 | if (inode == NULL) { | |
473 | inode = ERR_PTR(-ENOMEM); | |
1da177e4 | 474 | goto out_no_inode; |
03f28e3a | 475 | } |
1da177e4 LT |
476 | |
477 | if (inode->i_state & I_NEW) { | |
478 | struct nfs_inode *nfsi = NFS_I(inode); | |
b0c4fddc | 479 | unsigned long now = jiffies; |
1da177e4 LT |
480 | |
481 | /* We set i_ino for the few things that still rely on it, | |
482 | * such as stat(2) */ | |
483 | inode->i_ino = hash; | |
484 | ||
485 | /* We can't support update_atime(), since the server will reset it */ | |
486 | inode->i_flags |= S_NOATIME|S_NOCMTIME; | |
487 | inode->i_mode = fattr->mode; | |
821a868a | 488 | nfsi->cache_validity = 0; |
62ab460c | 489 | if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0 |
ce62b114 | 490 | && (fattr_supported & NFS_ATTR_FATTR_MODE)) |
720869eb | 491 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE); |
1da177e4 LT |
492 | /* Why so? Because we want revalidate for devices/FIFOs, and |
493 | * that's precisely what we have in nfs_file_inode_operations. | |
494 | */ | |
8fa5c000 | 495 | inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops; |
1da177e4 | 496 | if (S_ISREG(inode->i_mode)) { |
1788ea6e | 497 | inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops; |
1da177e4 | 498 | inode->i_data.a_ops = &nfs_file_aops; |
e591b298 | 499 | nfs_inode_init_regular(nfsi); |
49b29a57 | 500 | mapping_set_large_folios(inode->i_mapping); |
1da177e4 | 501 | } else if (S_ISDIR(inode->i_mode)) { |
8fa5c000 | 502 | inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops; |
1da177e4 | 503 | inode->i_fop = &nfs_dir_operations; |
11de3b11 | 504 | inode->i_data.a_ops = &nfs_dir_aops; |
e591b298 | 505 | nfs_inode_init_dir(nfsi); |
55a97593 | 506 | /* Deal with crossing mountpoints */ |
7ebb9315 BS |
507 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT || |
508 | fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) { | |
6b97fd3d MN |
509 | if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) |
510 | inode->i_op = &nfs_referral_inode_operations; | |
511 | else | |
512 | inode->i_op = &nfs_mountpoint_inode_operations; | |
55a97593 | 513 | inode->i_fop = NULL; |
36d43a43 | 514 | inode->i_flags |= S_AUTOMOUNT; |
55a97593 | 515 | } |
21fc61c7 | 516 | } else if (S_ISLNK(inode->i_mode)) { |
1da177e4 | 517 | inode->i_op = &nfs_symlink_inode_operations; |
21fc61c7 AV |
518 | inode_nohighmem(inode); |
519 | } else | |
1da177e4 LT |
520 | init_special_inode(inode, inode->i_mode, fattr->rdev); |
521 | ||
41d581a9 JL |
522 | inode_set_atime(inode, 0, 0); |
523 | inode_set_mtime(inode, 0, 0); | |
55e04e9c | 524 | inode_set_ctime(inode, 0, 0); |
1eb5d98f | 525 | inode_set_iversion_raw(inode, 0); |
9e6e70f8 | 526 | inode->i_size = 0; |
6d6b77f1 | 527 | clear_nlink(inode); |
9ff593c4 EB |
528 | inode->i_uid = make_kuid(&init_user_ns, -2); |
529 | inode->i_gid = make_kgid(&init_user_ns, -2); | |
9e6e70f8 | 530 | inode->i_blocks = 0; |
2701d086 AA |
531 | nfsi->write_io = 0; |
532 | nfsi->read_io = 0; | |
9e6e70f8 | 533 | |
33801147 | 534 | nfsi->read_cache_jiffies = fattr->time_start; |
4704f0e2 | 535 | nfsi->attr_gencount = fattr->gencount; |
9e6e70f8 | 536 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) |
41d581a9 | 537 | inode_set_atime_to_ts(inode, fattr->atime); |
ce62b114 | 538 | else if (fattr_supported & NFS_ATTR_FATTR_ATIME) |
16e14375 | 539 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); |
9e6e70f8 | 540 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) |
41d581a9 | 541 | inode_set_mtime_to_ts(inode, fattr->mtime); |
ce62b114 | 542 | else if (fattr_supported & NFS_ATTR_FATTR_MTIME) |
16e14375 | 543 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); |
9e6e70f8 | 544 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
55e04e9c | 545 | inode_set_ctime_to_ts(inode, fattr->ctime); |
ce62b114 | 546 | else if (fattr_supported & NFS_ATTR_FATTR_CTIME) |
16e14375 | 547 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME); |
9e6e70f8 | 548 | if (fattr->valid & NFS_ATTR_FATTR_CHANGE) |
1eb5d98f | 549 | inode_set_iversion_raw(inode, fattr->change_attr); |
cd812599 | 550 | else |
16e14375 | 551 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); |
9e6e70f8 TM |
552 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) |
553 | inode->i_size = nfs_size_to_loff_t(fattr->size); | |
62ab460c | 554 | else |
16e14375 | 555 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE); |
9e6e70f8 | 556 | if (fattr->valid & NFS_ATTR_FATTR_NLINK) |
bfe86848 | 557 | set_nlink(inode, fattr->nlink); |
ce62b114 | 558 | else if (fattr_supported & NFS_ATTR_FATTR_NLINK) |
fabf2b34 | 559 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK); |
3a306535 HY |
560 | else |
561 | set_nlink(inode, 1); | |
9e6e70f8 TM |
562 | if (fattr->valid & NFS_ATTR_FATTR_OWNER) |
563 | inode->i_uid = fattr->uid; | |
ce62b114 | 564 | else if (fattr_supported & NFS_ATTR_FATTR_OWNER) |
16e14375 | 565 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER); |
9e6e70f8 TM |
566 | if (fattr->valid & NFS_ATTR_FATTR_GROUP) |
567 | inode->i_gid = fattr->gid; | |
ce62b114 | 568 | else if (fattr_supported & NFS_ATTR_FATTR_GROUP) |
16e14375 | 569 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER); |
9e6e70f8 TM |
570 | if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) |
571 | inode->i_blocks = fattr->du.nfs2.blocks; | |
ce62b114 TM |
572 | else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED && |
573 | fattr->size != 0) | |
574 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); | |
9e6e70f8 | 575 | if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { |
1da177e4 LT |
576 | /* |
577 | * report the blocks in 512byte units | |
578 | */ | |
579 | inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); | |
ce62b114 TM |
580 | } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED && |
581 | fattr->size != 0) | |
4cdfeb64 | 582 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); |
821a868a | 583 | |
dd225cb3 | 584 | nfs_setsecurity(inode, fattr); |
aa9c2669 | 585 | |
1da177e4 | 586 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
b0c4fddc | 587 | nfsi->attrtimeo_timestamp = now; |
1c3c07e9 | 588 | nfsi->access_cache = RB_ROOT; |
1da177e4 | 589 | |
f1fe29b4 | 590 | nfs_fscache_init_inode(inode); |
ef79c097 | 591 | |
1da177e4 | 592 | unlock_new_inode(inode); |
26fde4df N |
593 | } else { |
594 | int err = nfs_refresh_inode(inode, fattr); | |
595 | if (err < 0) { | |
596 | iput(inode); | |
597 | inode = ERR_PTR(err); | |
598 | goto out_no_inode; | |
599 | } | |
600 | } | |
1e8968c5 | 601 | dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n", |
1da177e4 | 602 | inode->i_sb->s_id, |
1e8968c5 | 603 | (unsigned long long)NFS_FILEID(inode), |
4f1abd22 | 604 | nfs_display_fhandle_hash(fh), |
1da177e4 LT |
605 | atomic_read(&inode->i_count)); |
606 | ||
607 | out: | |
608 | return inode; | |
609 | ||
610 | out_no_inode: | |
03f28e3a | 611 | dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode)); |
1da177e4 LT |
612 | goto out; |
613 | } | |
89d77c8f | 614 | EXPORT_SYMBOL_GPL(nfs_fhget); |
1da177e4 | 615 | |
0a741f59 TM |
616 | static void |
617 | nfs_fattr_fixup_delegated(struct inode *inode, struct nfs_fattr *fattr) | |
618 | { | |
619 | unsigned long cache_validity = NFS_I(inode)->cache_validity; | |
620 | ||
621 | if (nfs_have_delegated_mtime(inode)) { | |
622 | if (!(cache_validity & NFS_INO_INVALID_CTIME)) | |
623 | fattr->valid &= ~(NFS_ATTR_FATTR_PRECTIME | | |
624 | NFS_ATTR_FATTR_CTIME); | |
625 | ||
626 | if (!(cache_validity & NFS_INO_INVALID_MTIME)) | |
627 | fattr->valid &= ~(NFS_ATTR_FATTR_PREMTIME | | |
628 | NFS_ATTR_FATTR_MTIME); | |
629 | ||
630 | if (!(cache_validity & NFS_INO_INVALID_ATIME)) | |
631 | fattr->valid &= ~NFS_ATTR_FATTR_ATIME; | |
632 | } else if (nfs_have_delegated_atime(inode)) { | |
633 | if (!(cache_validity & NFS_INO_INVALID_ATIME)) | |
634 | fattr->valid &= ~NFS_ATTR_FATTR_ATIME; | |
635 | } | |
636 | } | |
637 | ||
aba41e90 SG |
638 | static void nfs_set_timestamps_to_ts(struct inode *inode, struct iattr *attr) |
639 | { | |
640 | unsigned int cache_flags = 0; | |
641 | ||
642 | if (attr->ia_valid & ATTR_MTIME_SET) { | |
643 | struct timespec64 ctime = inode_get_ctime(inode); | |
644 | struct timespec64 mtime = inode_get_mtime(inode); | |
645 | struct timespec64 now; | |
646 | int updated = 0; | |
647 | ||
648 | now = inode_set_ctime_current(inode); | |
649 | if (!timespec64_equal(&now, &ctime)) | |
650 | updated |= S_CTIME; | |
651 | ||
652 | inode_set_mtime_to_ts(inode, attr->ia_mtime); | |
653 | if (!timespec64_equal(&now, &mtime)) | |
654 | updated |= S_MTIME; | |
655 | ||
656 | inode_maybe_inc_iversion(inode, updated); | |
657 | cache_flags |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME; | |
658 | } | |
659 | if (attr->ia_valid & ATTR_ATIME_SET) { | |
660 | inode_set_atime_to_ts(inode, attr->ia_atime); | |
661 | cache_flags |= NFS_INO_INVALID_ATIME; | |
662 | } | |
663 | NFS_I(inode)->cache_validity &= ~cache_flags; | |
664 | } | |
665 | ||
40f45ab3 TM |
666 | static void nfs_update_timestamps(struct inode *inode, unsigned int ia_valid) |
667 | { | |
668 | enum file_time_flags time_flags = 0; | |
669 | unsigned int cache_flags = 0; | |
670 | ||
671 | if (ia_valid & ATTR_MTIME) { | |
672 | time_flags |= S_MTIME | S_CTIME; | |
673 | cache_flags |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME; | |
674 | } | |
675 | if (ia_valid & ATTR_ATIME) { | |
676 | time_flags |= S_ATIME; | |
677 | cache_flags |= NFS_INO_INVALID_ATIME; | |
678 | } | |
679 | inode_update_timestamps(inode, time_flags); | |
680 | NFS_I(inode)->cache_validity &= ~cache_flags; | |
681 | } | |
682 | ||
e12912d9 TM |
683 | void nfs_update_delegated_atime(struct inode *inode) |
684 | { | |
685 | spin_lock(&inode->i_lock); | |
40f45ab3 TM |
686 | if (nfs_have_delegated_atime(inode)) |
687 | nfs_update_timestamps(inode, ATTR_ATIME); | |
e12912d9 TM |
688 | spin_unlock(&inode->i_lock); |
689 | } | |
690 | ||
691 | void nfs_update_delegated_mtime_locked(struct inode *inode) | |
692 | { | |
40f45ab3 TM |
693 | if (nfs_have_delegated_mtime(inode)) |
694 | nfs_update_timestamps(inode, ATTR_MTIME); | |
e12912d9 TM |
695 | } |
696 | ||
697 | void nfs_update_delegated_mtime(struct inode *inode) | |
698 | { | |
699 | spin_lock(&inode->i_lock); | |
700 | nfs_update_delegated_mtime_locked(inode); | |
701 | spin_unlock(&inode->i_lock); | |
702 | } | |
703 | EXPORT_SYMBOL_GPL(nfs_update_delegated_mtime); | |
704 | ||
536e43d1 | 705 | #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN) |
1da177e4 LT |
706 | |
707 | int | |
c1632a0f | 708 | nfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 709 | struct iattr *attr) |
1da177e4 | 710 | { |
2b0143b5 | 711 | struct inode *inode = d_inode(dentry); |
987f8dfc | 712 | struct nfs_fattr *fattr; |
ae57ca0f | 713 | int error = 0; |
1da177e4 | 714 | |
91d5b470 CL |
715 | nfs_inc_stats(inode, NFSIOS_VFSSETATTR); |
716 | ||
188b95dd JL |
717 | /* skip mode change if it's just for clearing setuid/setgid */ |
718 | if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) | |
719 | attr->ia_valid &= ~ATTR_MODE; | |
720 | ||
1da177e4 | 721 | if (attr->ia_valid & ATTR_SIZE) { |
08a899d5 CH |
722 | BUG_ON(!S_ISREG(inode->i_mode)); |
723 | ||
ae57ca0f KM |
724 | error = inode_newsize_ok(inode, attr->ia_size); |
725 | if (error) | |
726 | return error; | |
727 | ||
728 | if (attr->ia_size == i_size_read(inode)) | |
1da177e4 LT |
729 | attr->ia_valid &= ~ATTR_SIZE; |
730 | } | |
731 | ||
40f45ab3 TM |
732 | if (nfs_have_delegated_mtime(inode) && attr->ia_valid & ATTR_MTIME) { |
733 | spin_lock(&inode->i_lock); | |
aba41e90 SG |
734 | if (attr->ia_valid & ATTR_MTIME_SET) { |
735 | nfs_set_timestamps_to_ts(inode, attr); | |
736 | attr->ia_valid &= ~(ATTR_MTIME|ATTR_MTIME_SET| | |
737 | ATTR_ATIME|ATTR_ATIME_SET); | |
738 | } else { | |
739 | nfs_update_timestamps(inode, attr->ia_valid); | |
740 | attr->ia_valid &= ~(ATTR_MTIME|ATTR_ATIME); | |
741 | } | |
40f45ab3 | 742 | spin_unlock(&inode->i_lock); |
40f45ab3 TM |
743 | } else if (nfs_have_delegated_atime(inode) && |
744 | attr->ia_valid & ATTR_ATIME && | |
745 | !(attr->ia_valid & ATTR_MTIME)) { | |
aba41e90 SG |
746 | if (attr->ia_valid & ATTR_ATIME_SET) { |
747 | spin_lock(&inode->i_lock); | |
748 | nfs_set_timestamps_to_ts(inode, attr); | |
749 | spin_unlock(&inode->i_lock); | |
750 | attr->ia_valid &= ~(ATTR_ATIME|ATTR_ATIME_SET); | |
751 | } else { | |
752 | nfs_update_delegated_atime(inode); | |
753 | attr->ia_valid &= ~ATTR_ATIME; | |
754 | } | |
e12912d9 TM |
755 | } |
756 | ||
1da177e4 | 757 | /* Optimization: if the end result is no change, don't RPC */ |
1f9f4328 | 758 | if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0) |
1da177e4 LT |
759 | return 0; |
760 | ||
f4ce1299 TM |
761 | trace_nfs_setattr_enter(inode); |
762 | ||
755c1e20 | 763 | /* Write all dirty data */ |
4d346bea TM |
764 | if (S_ISREG(inode->i_mode)) |
765 | nfs_sync_inode(inode); | |
987f8dfc | 766 | |
1b00ad65 | 767 | fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); |
ae57ca0f KM |
768 | if (fattr == NULL) { |
769 | error = -ENOMEM; | |
987f8dfc | 770 | goto out; |
ae57ca0f KM |
771 | } |
772 | ||
987f8dfc | 773 | error = NFS_PROTO(inode)->setattr(dentry, fattr, attr); |
65e4308d | 774 | if (error == 0) |
aa9c2669 | 775 | error = nfs_refresh_inode(inode, fattr); |
987f8dfc TM |
776 | nfs_free_fattr(fattr); |
777 | out: | |
f4ce1299 | 778 | trace_nfs_setattr_exit(inode, error); |
65e4308d TM |
779 | return error; |
780 | } | |
ddda8e0a | 781 | EXPORT_SYMBOL_GPL(nfs_setattr); |
65e4308d | 782 | |
a3d01454 TM |
783 | /** |
784 | * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall | |
785 | * @inode: inode of the file used | |
786 | * @offset: file offset to start truncating | |
787 | * | |
788 | * This is a copy of the common vmtruncate, but with the locking | |
789 | * corrected to take into account the fact that NFS requires | |
790 | * inode->i_size to be updated under the inode->i_lock. | |
f044636d | 791 | * Note: must be called with inode->i_lock held! |
a3d01454 TM |
792 | */ |
793 | static int nfs_vmtruncate(struct inode * inode, loff_t offset) | |
794 | { | |
c08d3b0e | 795 | int err; |
a3d01454 | 796 | |
c08d3b0e | 797 | err = inode_newsize_ok(inode, offset); |
798 | if (err) | |
799 | goto out; | |
a3d01454 | 800 | |
110cb2d2 | 801 | trace_nfs_size_truncate(inode, offset); |
c08d3b0e | 802 | i_size_write(inode, offset); |
6edf9609 | 803 | /* Optimisation */ |
3db63daa N |
804 | if (offset == 0) { |
805 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA; | |
806 | nfs_ooo_clear(NFS_I(inode)); | |
807 | } | |
f6cdfa6d | 808 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE; |
c08d3b0e | 809 | |
f044636d | 810 | spin_unlock(&inode->i_lock); |
7caef267 | 811 | truncate_pagecache(inode, offset); |
e12912d9 | 812 | nfs_update_delegated_mtime_locked(inode); |
f044636d | 813 | spin_lock(&inode->i_lock); |
c08d3b0e | 814 | out: |
815 | return err; | |
a3d01454 TM |
816 | } |
817 | ||
65e4308d TM |
818 | /** |
819 | * nfs_setattr_update_inode - Update inode metadata after a setattr call. | |
820 | * @inode: pointer to struct inode | |
821 | * @attr: pointer to struct iattr | |
16e14375 | 822 | * @fattr: pointer to struct nfs_fattr |
65e4308d TM |
823 | * |
824 | * Note: we do this in the *proc.c in order to ensure that | |
825 | * it works for things like exclusive creates too. | |
826 | */ | |
f044636d TM |
827 | void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr, |
828 | struct nfs_fattr *fattr) | |
65e4308d | 829 | { |
f044636d TM |
830 | /* Barrier: bump the attribute generation count. */ |
831 | nfs_fattr_set_barrier(fattr); | |
832 | ||
833 | spin_lock(&inode->i_lock); | |
834 | NFS_I(inode)->attr_gencount = fattr->gencount; | |
6a97d02d | 835 | if ((attr->ia_valid & ATTR_SIZE) != 0) { |
e12912d9 TM |
836 | if (!nfs_have_delegated_mtime(inode)) |
837 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); | |
838 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); | |
6a97d02d TM |
839 | nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC); |
840 | nfs_vmtruncate(inode, attr->ia_size); | |
841 | } | |
65e4308d | 842 | if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) { |
6a97d02d | 843 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME; |
1f9f4328 TM |
844 | if ((attr->ia_valid & ATTR_KILL_SUID) != 0 && |
845 | inode->i_mode & S_ISUID) | |
846 | inode->i_mode &= ~S_ISUID; | |
4f704d9a | 847 | if (setattr_should_drop_sgid(&nop_mnt_idmap, inode)) |
1f9f4328 | 848 | inode->i_mode &= ~S_ISGID; |
1da177e4 | 849 | if ((attr->ia_valid & ATTR_MODE) != 0) { |
65e4308d TM |
850 | int mode = attr->ia_mode & S_IALLUGO; |
851 | mode |= inode->i_mode & ~S_IALLUGO; | |
1da177e4 LT |
852 | inode->i_mode = mode; |
853 | } | |
854 | if ((attr->ia_valid & ATTR_UID) != 0) | |
855 | inode->i_uid = attr->ia_uid; | |
856 | if ((attr->ia_valid & ATTR_GID) != 0) | |
857 | inode->i_gid = attr->ia_gid; | |
6a97d02d | 858 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
55e04e9c | 859 | inode_set_ctime_to_ts(inode, fattr->ctime); |
6a97d02d TM |
860 | else |
861 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
862 | | NFS_INO_INVALID_CTIME); | |
6edf9609 TM |
863 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS |
864 | | NFS_INO_INVALID_ACL); | |
65e4308d | 865 | } |
6a97d02d TM |
866 | if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) { |
867 | NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME | |
868 | | NFS_INO_INVALID_CTIME); | |
869 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) | |
41d581a9 | 870 | inode_set_atime_to_ts(inode, fattr->atime); |
6a97d02d | 871 | else if (attr->ia_valid & ATTR_ATIME_SET) |
41d581a9 | 872 | inode_set_atime_to_ts(inode, attr->ia_atime); |
6a97d02d TM |
873 | else |
874 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); | |
875 | ||
876 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
55e04e9c | 877 | inode_set_ctime_to_ts(inode, fattr->ctime); |
6a97d02d TM |
878 | else |
879 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
880 | | NFS_INO_INVALID_CTIME); | |
881 | } | |
882 | if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) { | |
883 | NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME | |
884 | | NFS_INO_INVALID_CTIME); | |
885 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) | |
41d581a9 | 886 | inode_set_mtime_to_ts(inode, fattr->mtime); |
6a97d02d | 887 | else if (attr->ia_valid & ATTR_MTIME_SET) |
41d581a9 | 888 | inode_set_mtime_to_ts(inode, attr->ia_mtime); |
6a97d02d TM |
889 | else |
890 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); | |
891 | ||
892 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
55e04e9c | 893 | inode_set_ctime_to_ts(inode, fattr->ctime); |
6a97d02d TM |
894 | else |
895 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
896 | | NFS_INO_INVALID_CTIME); | |
65e4308d | 897 | } |
616c3196 JL |
898 | if (fattr->valid) |
899 | nfs_update_inode(inode, fattr); | |
f044636d | 900 | spin_unlock(&inode->i_lock); |
1da177e4 | 901 | } |
ddda8e0a | 902 | EXPORT_SYMBOL_GPL(nfs_setattr_update_inode); |
1da177e4 | 903 | |
ad1e109a TM |
904 | /* |
905 | * Don't request help from readdirplus if the file is being written to, | |
906 | * or if attribute caching is turned off | |
907 | */ | |
908 | static bool nfs_getattr_readdirplus_enable(const struct inode *inode) | |
311324ad | 909 | { |
ad1e109a TM |
910 | return nfs_server_capable(inode, NFS_CAP_READDIRPLUS) && |
911 | !nfs_have_writebacks(inode) && NFS_MAXATTRTIMEO(inode) > 5 * HZ; | |
912 | } | |
311324ad | 913 | |
ad1e109a TM |
914 | static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry) |
915 | { | |
916 | if (!IS_ROOT(dentry)) { | |
917 | struct dentry *parent = dget_parent(dentry); | |
918 | nfs_readdir_record_entry_cache_miss(d_inode(parent)); | |
919 | dput(parent); | |
920 | } | |
311324ad TM |
921 | } |
922 | ||
1bcf4c5c TM |
923 | static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry) |
924 | { | |
ad1e109a TM |
925 | if (!IS_ROOT(dentry)) { |
926 | struct dentry *parent = dget_parent(dentry); | |
927 | nfs_readdir_record_entry_cache_hit(d_inode(parent)); | |
928 | dput(parent); | |
929 | } | |
1bcf4c5c TM |
930 | } |
931 | ||
63cdd7ed | 932 | static u32 nfs_get_valid_attrmask(struct inode *inode) |
311324ad | 933 | { |
63cdd7ed TM |
934 | unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); |
935 | u32 reply_mask = STATX_INO | STATX_TYPE; | |
936 | ||
937 | if (!(cache_validity & NFS_INO_INVALID_ATIME)) | |
938 | reply_mask |= STATX_ATIME; | |
13c0b082 TM |
939 | if (!(cache_validity & NFS_INO_INVALID_CTIME)) |
940 | reply_mask |= STATX_CTIME; | |
941 | if (!(cache_validity & NFS_INO_INVALID_MTIME)) | |
942 | reply_mask |= STATX_MTIME; | |
943 | if (!(cache_validity & NFS_INO_INVALID_SIZE)) | |
944 | reply_mask |= STATX_SIZE; | |
fabf2b34 TM |
945 | if (!(cache_validity & NFS_INO_INVALID_NLINK)) |
946 | reply_mask |= STATX_NLINK; | |
720869eb TM |
947 | if (!(cache_validity & NFS_INO_INVALID_MODE)) |
948 | reply_mask |= STATX_MODE; | |
63cdd7ed | 949 | if (!(cache_validity & NFS_INO_INVALID_OTHER)) |
720869eb | 950 | reply_mask |= STATX_UID | STATX_GID; |
63cdd7ed TM |
951 | if (!(cache_validity & NFS_INO_INVALID_BLOCKS)) |
952 | reply_mask |= STATX_BLOCKS; | |
61a968b4 JL |
953 | if (!(cache_validity & NFS_INO_INVALID_CHANGE)) |
954 | reply_mask |= STATX_CHANGE_COOKIE; | |
63cdd7ed | 955 | return reply_mask; |
311324ad TM |
956 | } |
957 | ||
b74d24f7 | 958 | int nfs_getattr(struct mnt_idmap *idmap, const struct path *path, |
549c7297 | 959 | struct kstat *stat, u32 request_mask, unsigned int query_flags) |
1da177e4 | 960 | { |
a528d35e | 961 | struct inode *inode = d_inode(path->dentry); |
9ccee940 TM |
962 | struct nfs_server *server = NFS_SERVER(inode); |
963 | unsigned long cache_validity; | |
16caf5b6 | 964 | int err = 0; |
9ccee940 TM |
965 | bool force_sync = query_flags & AT_STATX_FORCE_SYNC; |
966 | bool do_update = false; | |
ad1e109a | 967 | bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode); |
1da177e4 | 968 | |
f4ce1299 | 969 | trace_nfs_getattr_enter(inode); |
9ccee940 | 970 | |
4eb6a823 TM |
971 | request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID | |
972 | STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME | | |
cded49ba | 973 | STATX_INO | STATX_SIZE | STATX_BLOCKS | |
61a968b4 | 974 | STATX_CHANGE_COOKIE; |
4eb6a823 | 975 | |
ac7cbb22 | 976 | if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) { |
ad1e109a TM |
977 | if (readdirplus_enabled) |
978 | nfs_readdirplus_parent_cache_hit(path->dentry); | |
63cdd7ed | 979 | goto out_no_revalidate; |
ac7cbb22 | 980 | } |
9ccee940 | 981 | |
61a968b4 JL |
982 | /* Flush out writes to the server in order to update c/mtime/version. */ |
983 | if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_CHANGE_COOKIE)) && | |
e12912d9 TM |
984 | S_ISREG(inode->i_mode)) { |
985 | if (nfs_have_delegated_mtime(inode)) | |
986 | filemap_fdatawrite(inode->i_mapping); | |
987 | else | |
988 | filemap_write_and_wait(inode->i_mapping); | |
989 | } | |
fc33a7bb CH |
990 | |
991 | /* | |
992 | * We may force a getattr if the user cares about atime. | |
993 | * | |
994 | * Note that we only have to check the vfsmount flags here: | |
995 | * - NFS always sets S_NOATIME by so checking it would give a | |
996 | * bogus result | |
1751e8a6 | 997 | * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is |
fc33a7bb CH |
998 | * no point in checking those. |
999 | */ | |
a528d35e DH |
1000 | if ((path->mnt->mnt_flags & MNT_NOATIME) || |
1001 | ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
9ccee940 TM |
1002 | request_mask &= ~STATX_ATIME; |
1003 | ||
1004 | /* Is the user requesting attributes that might need revalidation? */ | |
1005 | if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME| | |
1006 | STATX_MTIME|STATX_UID|STATX_GID| | |
61a968b4 JL |
1007 | STATX_SIZE|STATX_BLOCKS| |
1008 | STATX_CHANGE_COOKIE))) | |
9ccee940 TM |
1009 | goto out_no_revalidate; |
1010 | ||
1011 | /* Check whether the cached attributes are stale */ | |
1012 | do_update |= force_sync || nfs_attribute_cache_expired(inode); | |
1013 | cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); | |
e8764a6f | 1014 | do_update |= cache_validity & NFS_INO_INVALID_CHANGE; |
9ccee940 TM |
1015 | if (request_mask & STATX_ATIME) |
1016 | do_update |= cache_validity & NFS_INO_INVALID_ATIME; | |
e8764a6f TM |
1017 | if (request_mask & STATX_CTIME) |
1018 | do_update |= cache_validity & NFS_INO_INVALID_CTIME; | |
1019 | if (request_mask & STATX_MTIME) | |
1020 | do_update |= cache_validity & NFS_INO_INVALID_MTIME; | |
1021 | if (request_mask & STATX_SIZE) | |
1022 | do_update |= cache_validity & NFS_INO_INVALID_SIZE; | |
fabf2b34 TM |
1023 | if (request_mask & STATX_NLINK) |
1024 | do_update |= cache_validity & NFS_INO_INVALID_NLINK; | |
720869eb TM |
1025 | if (request_mask & STATX_MODE) |
1026 | do_update |= cache_validity & NFS_INO_INVALID_MODE; | |
1027 | if (request_mask & (STATX_UID | STATX_GID)) | |
e8764a6f | 1028 | do_update |= cache_validity & NFS_INO_INVALID_OTHER; |
3a39e778 ZB |
1029 | if (request_mask & STATX_BLOCKS) |
1030 | do_update |= cache_validity & NFS_INO_INVALID_BLOCKS; | |
63cdd7ed | 1031 | |
9ccee940 | 1032 | if (do_update) { |
ad1e109a | 1033 | if (readdirplus_enabled) |
59b86d85 | 1034 | nfs_readdirplus_parent_cache_miss(path->dentry); |
311324ad | 1035 | err = __nfs_revalidate_inode(server, inode); |
9ccee940 TM |
1036 | if (err) |
1037 | goto out; | |
ad1e109a | 1038 | } else if (readdirplus_enabled) |
a528d35e | 1039 | nfs_readdirplus_parent_cache_hit(path->dentry); |
9ccee940 TM |
1040 | out_no_revalidate: |
1041 | /* Only return attributes that were revalidated. */ | |
63cdd7ed TM |
1042 | stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask; |
1043 | ||
0d72b928 | 1044 | generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); |
9ccee940 | 1045 | stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode)); |
61a968b4 JL |
1046 | stat->change_cookie = inode_peek_iversion_raw(inode); |
1047 | stat->attributes_mask |= STATX_ATTR_CHANGE_MONOTONIC; | |
1048 | if (server->change_attr_type != NFS4_CHANGE_TYPE_IS_UNDEFINED) | |
1049 | stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC; | |
9ccee940 TM |
1050 | if (S_ISDIR(inode->i_mode)) |
1051 | stat->blksize = NFS_SERVER(inode)->dtsize; | |
acdc53b2 | 1052 | out: |
f4ce1299 | 1053 | trace_nfs_getattr_exit(inode, err); |
1da177e4 LT |
1054 | return err; |
1055 | } | |
ddda8e0a | 1056 | EXPORT_SYMBOL_GPL(nfs_getattr); |
1da177e4 | 1057 | |
f11ac8db TM |
1058 | static void nfs_init_lock_context(struct nfs_lock_context *l_ctx) |
1059 | { | |
2f62b5aa | 1060 | refcount_set(&l_ctx->count, 1); |
d51fdb87 | 1061 | l_ctx->lockowner = current->files; |
f11ac8db | 1062 | INIT_LIST_HEAD(&l_ctx->list); |
210c7c17 | 1063 | atomic_set(&l_ctx->io_count, 0); |
f11ac8db TM |
1064 | } |
1065 | ||
1066 | static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx) | |
1067 | { | |
1db97eaa | 1068 | struct nfs_lock_context *pos; |
f11ac8db | 1069 | |
1db97eaa | 1070 | list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) { |
d51fdb87 | 1071 | if (pos->lockowner != current->files) |
f11ac8db | 1072 | continue; |
1db97eaa TM |
1073 | if (refcount_inc_not_zero(&pos->count)) |
1074 | return pos; | |
1075 | } | |
f11ac8db TM |
1076 | return NULL; |
1077 | } | |
1078 | ||
1079 | struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx) | |
1080 | { | |
1081 | struct nfs_lock_context *res, *new = NULL; | |
2b0143b5 | 1082 | struct inode *inode = d_inode(ctx->dentry); |
f11ac8db | 1083 | |
1db97eaa | 1084 | rcu_read_lock(); |
f11ac8db | 1085 | res = __nfs_find_lock_context(ctx); |
1db97eaa | 1086 | rcu_read_unlock(); |
f11ac8db | 1087 | if (res == NULL) { |
d7867712 | 1088 | new = kmalloc(sizeof(*new), GFP_KERNEL_ACCOUNT); |
f11ac8db | 1089 | if (new == NULL) |
b3c54de6 | 1090 | return ERR_PTR(-ENOMEM); |
f11ac8db TM |
1091 | nfs_init_lock_context(new); |
1092 | spin_lock(&inode->i_lock); | |
1093 | res = __nfs_find_lock_context(ctx); | |
1094 | if (res == NULL) { | |
15494511 TM |
1095 | new->open_context = get_nfs_open_context(ctx); |
1096 | if (new->open_context) { | |
1097 | list_add_tail_rcu(&new->list, | |
1098 | &ctx->lock_context.list); | |
1099 | res = new; | |
1100 | new = NULL; | |
1101 | } else | |
1102 | res = ERR_PTR(-EBADF); | |
f11ac8db | 1103 | } |
1db97eaa TM |
1104 | spin_unlock(&inode->i_lock); |
1105 | kfree(new); | |
f11ac8db | 1106 | } |
f11ac8db TM |
1107 | return res; |
1108 | } | |
1c6dcbe5 | 1109 | EXPORT_SYMBOL_GPL(nfs_get_lock_context); |
f11ac8db TM |
1110 | |
1111 | void nfs_put_lock_context(struct nfs_lock_context *l_ctx) | |
1112 | { | |
1113 | struct nfs_open_context *ctx = l_ctx->open_context; | |
2b0143b5 | 1114 | struct inode *inode = d_inode(ctx->dentry); |
f11ac8db | 1115 | |
2f62b5aa | 1116 | if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock)) |
f11ac8db | 1117 | return; |
1db97eaa | 1118 | list_del_rcu(&l_ctx->list); |
f11ac8db | 1119 | spin_unlock(&inode->i_lock); |
15494511 | 1120 | put_nfs_open_context(ctx); |
1db97eaa | 1121 | kfree_rcu(l_ctx, rcu_head); |
f11ac8db | 1122 | } |
1c6dcbe5 | 1123 | EXPORT_SYMBOL_GPL(nfs_put_lock_context); |
f11ac8db | 1124 | |
7fe5c398 TM |
1125 | /** |
1126 | * nfs_close_context - Common close_context() routine NFSv2/v3 | |
1127 | * @ctx: pointer to context | |
1128 | * @is_sync: is this a synchronous close | |
1129 | * | |
5cf9d706 TM |
1130 | * Ensure that the attributes are up to date if we're mounted |
1131 | * with close-to-open semantics and we have cached data that will | |
1132 | * need to be revalidated on open. | |
7fe5c398 TM |
1133 | */ |
1134 | void nfs_close_context(struct nfs_open_context *ctx, int is_sync) | |
1135 | { | |
5cf9d706 | 1136 | struct nfs_inode *nfsi; |
7fe5c398 | 1137 | struct inode *inode; |
7fe5c398 TM |
1138 | |
1139 | if (!(ctx->mode & FMODE_WRITE)) | |
1140 | return; | |
1141 | if (!is_sync) | |
1142 | return; | |
2b0143b5 | 1143 | inode = d_inode(ctx->dentry); |
4201916f | 1144 | if (nfs_have_read_or_write_delegation(inode)) |
58ff4184 | 1145 | return; |
5cf9d706 TM |
1146 | nfsi = NFS_I(inode); |
1147 | if (inode->i_mapping->nrpages == 0) | |
1148 | return; | |
1149 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA) | |
1150 | return; | |
1151 | if (!list_empty(&nfsi->open_files)) | |
7fe5c398 | 1152 | return; |
1f3208b2 | 1153 | if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO) |
7fe5c398 | 1154 | return; |
1f3208b2 TM |
1155 | nfs_revalidate_inode(inode, |
1156 | NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE); | |
7fe5c398 | 1157 | } |
ddda8e0a | 1158 | EXPORT_SYMBOL_GPL(nfs_close_context); |
7fe5c398 | 1159 | |
532d4def N |
1160 | struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, |
1161 | fmode_t f_mode, | |
1162 | struct file *filp) | |
1da177e4 LT |
1163 | { |
1164 | struct nfs_open_context *ctx; | |
1165 | ||
d7867712 | 1166 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); |
1d179d6b | 1167 | if (!ctx) |
5ede7b1c | 1168 | return ERR_PTR(-ENOMEM); |
5ede7b1c AV |
1169 | nfs_sb_active(dentry->d_sb); |
1170 | ctx->dentry = dget(dentry); | |
1d179d6b TM |
1171 | if (filp) |
1172 | ctx->cred = get_cred(filp->f_cred); | |
1173 | else | |
1174 | ctx->cred = get_current_cred(); | |
ca05cbae | 1175 | rcu_assign_pointer(ctx->ll_cred, NULL); |
5ede7b1c AV |
1176 | ctx->state = NULL; |
1177 | ctx->mode = f_mode; | |
1178 | ctx->flags = 0; | |
1179 | ctx->error = 0; | |
532d4def | 1180 | ctx->flock_owner = (fl_owner_t)filp; |
5ede7b1c AV |
1181 | nfs_init_lock_context(&ctx->lock_context); |
1182 | ctx->lock_context.open_context = ctx; | |
1183 | INIT_LIST_HEAD(&ctx->list); | |
82be417a | 1184 | ctx->mdsthreshold = NULL; |
86e00412 MS |
1185 | nfs_localio_file_init(&ctx->nfl); |
1186 | ||
1da177e4 LT |
1187 | return ctx; |
1188 | } | |
89d77c8f | 1189 | EXPORT_SYMBOL_GPL(alloc_nfs_open_context); |
1da177e4 LT |
1190 | |
1191 | struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx) | |
1192 | { | |
0de43976 TM |
1193 | if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count)) |
1194 | return ctx; | |
1195 | return NULL; | |
1da177e4 | 1196 | } |
89d77c8f | 1197 | EXPORT_SYMBOL_GPL(get_nfs_open_context); |
1da177e4 | 1198 | |
7fe5c398 | 1199 | static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync) |
1da177e4 | 1200 | { |
2b0143b5 | 1201 | struct inode *inode = d_inode(ctx->dentry); |
3d4ff43d | 1202 | struct super_block *sb = ctx->dentry->d_sb; |
3bec63db | 1203 | |
0de43976 TM |
1204 | if (!refcount_dec_and_test(&ctx->lock_context.count)) |
1205 | return; | |
5c78f58e | 1206 | if (!list_empty(&ctx->list)) { |
0de43976 TM |
1207 | spin_lock(&inode->i_lock); |
1208 | list_del_rcu(&ctx->list); | |
ef84303e | 1209 | spin_unlock(&inode->i_lock); |
0de43976 | 1210 | } |
5c78f58e TM |
1211 | if (inode != NULL) |
1212 | NFS_PROTO(inode)->close_context(ctx, is_sync); | |
a52458b4 | 1213 | put_cred(ctx->cred); |
3d4ff43d | 1214 | dput(ctx->dentry); |
322b2b90 | 1215 | nfs_sb_deactive(sb); |
ca05cbae | 1216 | put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1)); |
82be417a | 1217 | kfree(ctx->mdsthreshold); |
86e00412 | 1218 | nfs_close_local_fh(&ctx->nfl); |
0de43976 | 1219 | kfree_rcu(ctx, rcu_head); |
3bec63db TM |
1220 | } |
1221 | ||
a49c3c77 TM |
1222 | void put_nfs_open_context(struct nfs_open_context *ctx) |
1223 | { | |
1224 | __put_nfs_open_context(ctx, 0); | |
1225 | } | |
89d77c8f | 1226 | EXPORT_SYMBOL_GPL(put_nfs_open_context); |
a49c3c77 | 1227 | |
4eae5014 TM |
1228 | static void put_nfs_open_context_sync(struct nfs_open_context *ctx) |
1229 | { | |
1230 | __put_nfs_open_context(ctx, 1); | |
1231 | } | |
1232 | ||
1da177e4 LT |
1233 | /* |
1234 | * Ensure that mmap has a recent RPC credential for use when writing out | |
1235 | * shared pages | |
1236 | */ | |
c45ffdd2 | 1237 | void nfs_inode_attach_open_context(struct nfs_open_context *ctx) |
1da177e4 | 1238 | { |
2b0143b5 | 1239 | struct inode *inode = d_inode(ctx->dentry); |
1da177e4 LT |
1240 | struct nfs_inode *nfsi = NFS_I(inode); |
1241 | ||
1da177e4 | 1242 | spin_lock(&inode->i_lock); |
1c341b77 | 1243 | if (list_empty(&nfsi->open_files) && |
3db63daa | 1244 | nfs_ooo_test(nfsi)) |
ac46b3d7 TM |
1245 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA | |
1246 | NFS_INO_REVAL_FORCED); | |
0de43976 | 1247 | list_add_tail_rcu(&ctx->list, &nfsi->open_files); |
1da177e4 LT |
1248 | spin_unlock(&inode->i_lock); |
1249 | } | |
c45ffdd2 TM |
1250 | EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context); |
1251 | ||
1252 | void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx) | |
1253 | { | |
1254 | filp->private_data = get_nfs_open_context(ctx); | |
e97bc663 | 1255 | set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags); |
c45ffdd2 TM |
1256 | if (list_empty(&ctx->list)) |
1257 | nfs_inode_attach_open_context(ctx); | |
1258 | } | |
89d77c8f | 1259 | EXPORT_SYMBOL_GPL(nfs_file_set_open_context); |
1da177e4 | 1260 | |
d530838b TM |
1261 | /* |
1262 | * Given an inode, search for an open context with the desired characteristics | |
1263 | */ | |
a52458b4 | 1264 | struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode) |
1da177e4 LT |
1265 | { |
1266 | struct nfs_inode *nfsi = NFS_I(inode); | |
1267 | struct nfs_open_context *pos, *ctx = NULL; | |
1268 | ||
0de43976 TM |
1269 | rcu_read_lock(); |
1270 | list_for_each_entry_rcu(pos, &nfsi->open_files, list) { | |
65f51603 | 1271 | if (cred != NULL && cred_fscmp(pos->cred, cred) != 0) |
d530838b | 1272 | continue; |
1544fa0f TM |
1273 | if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode) |
1274 | continue; | |
e97bc663 TM |
1275 | if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags)) |
1276 | continue; | |
1544fa0f | 1277 | ctx = get_nfs_open_context(pos); |
0de43976 TM |
1278 | if (ctx) |
1279 | break; | |
1da177e4 | 1280 | } |
0de43976 | 1281 | rcu_read_unlock(); |
1da177e4 LT |
1282 | return ctx; |
1283 | } | |
1284 | ||
aff8d8dc | 1285 | void nfs_file_clear_open_context(struct file *filp) |
1da177e4 | 1286 | { |
cd3758e3 | 1287 | struct nfs_open_context *ctx = nfs_file_open_context(filp); |
1da177e4 LT |
1288 | |
1289 | if (ctx) { | |
2b0143b5 | 1290 | struct inode *inode = d_inode(ctx->dentry); |
c45ffdd2 | 1291 | |
e97bc663 | 1292 | clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags); |
0bcbf039 PT |
1293 | /* |
1294 | * We fatal error on write before. Try to writeback | |
1295 | * every page again. | |
1296 | */ | |
1297 | if (ctx->error < 0) | |
1298 | invalidate_inode_pages2(inode->i_mapping); | |
1da177e4 | 1299 | filp->private_data = NULL; |
4eae5014 | 1300 | put_nfs_open_context_sync(ctx); |
1da177e4 LT |
1301 | } |
1302 | } | |
1303 | ||
1304 | /* | |
1305 | * These allocate and release file read/write context information. | |
1306 | */ | |
1307 | int nfs_open(struct inode *inode, struct file *filp) | |
1308 | { | |
1309 | struct nfs_open_context *ctx; | |
1da177e4 | 1310 | |
6f1c1d95 C |
1311 | ctx = alloc_nfs_open_context(file_dentry(filp), |
1312 | flags_to_mode(filp->f_flags), filp); | |
5ede7b1c AV |
1313 | if (IS_ERR(ctx)) |
1314 | return PTR_ERR(ctx); | |
1da177e4 LT |
1315 | nfs_file_set_open_context(filp, ctx); |
1316 | put_nfs_open_context(ctx); | |
f1fe29b4 | 1317 | nfs_fscache_open_file(inode, filp); |
1da177e4 LT |
1318 | return 0; |
1319 | } | |
1320 | ||
1da177e4 LT |
1321 | /* |
1322 | * This function is called whenever some part of NFS notices that | |
1323 | * the cached attributes have to be refreshed. | |
1324 | */ | |
1325 | int | |
1326 | __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode) | |
1327 | { | |
1328 | int status = -ESTALE; | |
a3cba2aa | 1329 | struct nfs_fattr *fattr = NULL; |
1da177e4 | 1330 | struct nfs_inode *nfsi = NFS_I(inode); |
1da177e4 | 1331 | |
1e8968c5 NV |
1332 | dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n", |
1333 | inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode)); | |
1da177e4 | 1334 | |
f4ce1299 TM |
1335 | trace_nfs_revalidate_inode_enter(inode); |
1336 | ||
85233a7a | 1337 | if (is_bad_inode(inode)) |
691beb13 | 1338 | goto out; |
1da177e4 | 1339 | if (NFS_STALE(inode)) |
412d582e | 1340 | goto out; |
7fdc49c4 | 1341 | |
ac46bd37 TM |
1342 | /* pNFS: Attributes aren't updated until we layoutcommit */ |
1343 | if (S_ISREG(inode->i_mode)) { | |
1344 | status = pnfs_sync_inode(inode, false); | |
1345 | if (status) | |
1346 | goto out; | |
1347 | } | |
1348 | ||
a3cba2aa | 1349 | status = -ENOMEM; |
2ef61e0e | 1350 | fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); |
a3cba2aa TM |
1351 | if (fattr == NULL) |
1352 | goto out; | |
1353 | ||
691beb13 | 1354 | nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE); |
14c43f76 | 1355 | |
2ef61e0e | 1356 | status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode); |
1da177e4 | 1357 | if (status != 0) { |
1e8968c5 | 1358 | dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n", |
1da177e4 | 1359 | inode->i_sb->s_id, |
1e8968c5 | 1360 | (unsigned long long)NFS_FILEID(inode), status); |
c74dfe97 TM |
1361 | switch (status) { |
1362 | case -ETIMEDOUT: | |
1363 | /* A soft timeout occurred. Use cached information? */ | |
1364 | if (server->flags & NFS_MOUNT_SOFTREVAL) | |
1365 | status = 0; | |
1366 | break; | |
1367 | case -ESTALE: | |
1da177e4 | 1368 | if (!S_ISDIR(inode->i_mode)) |
93ce4af7 TM |
1369 | nfs_set_inode_stale(inode); |
1370 | else | |
1371 | nfs_zap_caches(inode); | |
1da177e4 | 1372 | } |
2ef61e0e | 1373 | goto out; |
1da177e4 LT |
1374 | } |
1375 | ||
a3cba2aa | 1376 | status = nfs_refresh_inode(inode, fattr); |
1da177e4 | 1377 | if (status) { |
1e8968c5 | 1378 | dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n", |
1da177e4 | 1379 | inode->i_sb->s_id, |
1e8968c5 | 1380 | (unsigned long long)NFS_FILEID(inode), status); |
2ef61e0e | 1381 | goto out; |
1da177e4 | 1382 | } |
55296809 | 1383 | |
24aa1fe6 | 1384 | if (nfsi->cache_validity & NFS_INO_INVALID_ACL) |
ada70d94 | 1385 | nfs_zap_acl_cache(inode); |
55296809 | 1386 | |
dd225cb3 | 1387 | nfs_setsecurity(inode, fattr); |
3da580aa | 1388 | |
1e8968c5 | 1389 | dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n", |
1da177e4 | 1390 | inode->i_sb->s_id, |
1e8968c5 | 1391 | (unsigned long long)NFS_FILEID(inode)); |
1da177e4 | 1392 | |
14c43f76 | 1393 | out: |
a3cba2aa | 1394 | nfs_free_fattr(fattr); |
f4ce1299 | 1395 | trace_nfs_revalidate_inode_exit(inode, status); |
1da177e4 LT |
1396 | return status; |
1397 | } | |
1398 | ||
43f291cd | 1399 | int nfs_attribute_cache_expired(struct inode *inode) |
d7cf8dd0 | 1400 | { |
b4d2314b | 1401 | if (nfs_have_delegated_attributes(inode)) |
1da177e4 | 1402 | return 0; |
d7cf8dd0 | 1403 | return nfs_attribute_timeout(inode); |
1da177e4 LT |
1404 | } |
1405 | ||
1406 | /** | |
1407 | * nfs_revalidate_inode - Revalidate the inode attributes | |
302fad7b | 1408 | * @inode: pointer to inode struct |
1f3208b2 | 1409 | * @flags: cache flags to check |
1da177e4 LT |
1410 | * |
1411 | * Updates inode attribute information by retrieving the data from the server. | |
1412 | */ | |
1f3208b2 | 1413 | int nfs_revalidate_inode(struct inode *inode, unsigned long flags) |
1da177e4 | 1414 | { |
1f3208b2 | 1415 | if (!nfs_check_cache_invalid(inode, flags)) |
1da177e4 | 1416 | return NFS_STALE(inode) ? -ESTALE : 0; |
1f3208b2 | 1417 | return __nfs_revalidate_inode(NFS_SERVER(inode), inode); |
1da177e4 | 1418 | } |
1c606fb7 | 1419 | EXPORT_SYMBOL_GPL(nfs_revalidate_inode); |
1da177e4 | 1420 | |
1cda707d | 1421 | static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping) |
717d44e8 | 1422 | { |
f8806c84 TM |
1423 | int ret; |
1424 | ||
a6b5a28e | 1425 | nfs_fscache_invalidate(inode, 0); |
717d44e8 | 1426 | if (mapping->nrpages != 0) { |
f8806c84 TM |
1427 | if (S_ISREG(inode->i_mode)) { |
1428 | ret = nfs_sync_mapping(mapping); | |
1429 | if (ret < 0) | |
1430 | return ret; | |
1431 | } | |
1432 | ret = invalidate_inode_pages2(mapping); | |
717d44e8 TM |
1433 | if (ret < 0) |
1434 | return ret; | |
1435 | } | |
717d44e8 | 1436 | nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE); |
f4ce1299 | 1437 | |
1e8968c5 NV |
1438 | dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n", |
1439 | inode->i_sb->s_id, | |
1440 | (unsigned long long)NFS_FILEID(inode)); | |
717d44e8 TM |
1441 | return 0; |
1442 | } | |
1443 | ||
717d44e8 | 1444 | /** |
28aa2f9e | 1445 | * nfs_clear_invalid_mapping - Conditionally clear a mapping |
302fad7b | 1446 | * @mapping: pointer to mapping |
28aa2f9e TM |
1447 | * |
1448 | * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping. | |
717d44e8 | 1449 | */ |
28aa2f9e | 1450 | int nfs_clear_invalid_mapping(struct address_space *mapping) |
717d44e8 | 1451 | { |
28aa2f9e | 1452 | struct inode *inode = mapping->host; |
717d44e8 | 1453 | struct nfs_inode *nfsi = NFS_I(inode); |
d529ef83 | 1454 | unsigned long *bitlock = &nfsi->flags; |
717d44e8 | 1455 | int ret = 0; |
dc59250c | 1456 | |
d529ef83 JL |
1457 | /* |
1458 | * We must clear NFS_INO_INVALID_DATA first to ensure that | |
1459 | * invalidations that come in while we're shooting down the mappings | |
1460 | * are respected. But, that leaves a race window where one revalidator | |
1461 | * can clear the flag, and then another checks it before the mapping | |
1462 | * gets invalidated. Fix that by serializing access to this part of | |
1463 | * the function. | |
1464 | * | |
1465 | * At the same time, we need to allow other tasks to see whether we | |
1466 | * might be in the middle of invalidating the pages, so we only set | |
1467 | * the bit lock here if it looks like we're going to be doing that. | |
1468 | */ | |
1469 | for (;;) { | |
74316201 | 1470 | ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING, |
f5d39b02 PZ |
1471 | nfs_wait_bit_killable, |
1472 | TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); | |
d529ef83 JL |
1473 | if (ret) |
1474 | goto out; | |
867da60d MS |
1475 | smp_rmb(); /* pairs with smp_wmb() below */ |
1476 | if (test_bit(NFS_INO_INVALIDATING, bitlock)) | |
1477 | continue; | |
1478 | /* pairs with nfs_set_cache_invalid()'s smp_store_release() */ | |
1479 | if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA)) | |
1480 | goto out; | |
1481 | /* Slow-path that double-checks with spinlock held */ | |
17dfeb91 TM |
1482 | spin_lock(&inode->i_lock); |
1483 | if (test_bit(NFS_INO_INVALIDATING, bitlock)) { | |
1484 | spin_unlock(&inode->i_lock); | |
1485 | continue; | |
1486 | } | |
1487 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA) | |
d529ef83 | 1488 | break; |
d529ef83 | 1489 | spin_unlock(&inode->i_lock); |
17dfeb91 | 1490 | goto out; |
f4ce1299 TM |
1491 | } |
1492 | ||
17dfeb91 | 1493 | set_bit(NFS_INO_INVALIDATING, bitlock); |
4db72b40 | 1494 | smp_wmb(); |
3db63daa N |
1495 | nfsi->cache_validity &= ~NFS_INO_INVALID_DATA; |
1496 | nfs_ooo_clear(nfsi); | |
17dfeb91 TM |
1497 | spin_unlock(&inode->i_lock); |
1498 | trace_nfs_invalidate_mapping_enter(inode); | |
be527494 | 1499 | ret = nfs_invalidate_mapping(inode, mapping); |
17dfeb91 TM |
1500 | trace_nfs_invalidate_mapping_exit(inode, ret); |
1501 | ||
d529ef83 | 1502 | clear_bit_unlock(NFS_INO_INVALIDATING, bitlock); |
4e857c58 | 1503 | smp_mb__after_atomic(); |
d529ef83 | 1504 | wake_up_bit(bitlock, NFS_INO_INVALIDATING); |
cd9ae2b6 | 1505 | out: |
44b11874 | 1506 | return ret; |
7d52e862 TM |
1507 | } |
1508 | ||
28aa2f9e TM |
1509 | bool nfs_mapping_need_revalidate_inode(struct inode *inode) |
1510 | { | |
13c0b082 | 1511 | return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) || |
28aa2f9e TM |
1512 | NFS_STALE(inode); |
1513 | } | |
1514 | ||
1515 | int nfs_revalidate_mapping_rcu(struct inode *inode) | |
1516 | { | |
1517 | struct nfs_inode *nfsi = NFS_I(inode); | |
1518 | unsigned long *bitlock = &nfsi->flags; | |
1519 | int ret = 0; | |
1520 | ||
1521 | if (IS_SWAPFILE(inode)) | |
1522 | goto out; | |
1523 | if (nfs_mapping_need_revalidate_inode(inode)) { | |
1524 | ret = -ECHILD; | |
1525 | goto out; | |
1526 | } | |
1527 | spin_lock(&inode->i_lock); | |
1528 | if (test_bit(NFS_INO_INVALIDATING, bitlock) || | |
1529 | (nfsi->cache_validity & NFS_INO_INVALID_DATA)) | |
1530 | ret = -ECHILD; | |
1531 | spin_unlock(&inode->i_lock); | |
1532 | out: | |
1533 | return ret; | |
1534 | } | |
1535 | ||
1536 | /** | |
1537 | * nfs_revalidate_mapping - Revalidate the pagecache | |
1538 | * @inode: pointer to host inode | |
1539 | * @mapping: pointer to mapping | |
1540 | */ | |
1541 | int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping) | |
1542 | { | |
1543 | /* swapfiles are not supposed to be shared. */ | |
1544 | if (IS_SWAPFILE(inode)) | |
1545 | return 0; | |
1546 | ||
1547 | if (nfs_mapping_need_revalidate_inode(inode)) { | |
1548 | int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode); | |
1549 | if (ret < 0) | |
1550 | return ret; | |
1551 | } | |
1552 | ||
1553 | return nfs_clear_invalid_mapping(mapping); | |
1554 | } | |
1555 | ||
ca0daa27 | 1556 | static bool nfs_file_has_writers(struct nfs_inode *nfsi) |
874f9463 | 1557 | { |
ca0daa27 TM |
1558 | struct inode *inode = &nfsi->vfs_inode; |
1559 | ||
ca0daa27 TM |
1560 | if (!S_ISREG(inode->i_mode)) |
1561 | return false; | |
1562 | if (list_empty(&nfsi->open_files)) | |
1563 | return false; | |
6ba0c4e5 | 1564 | return inode_is_open_for_write(inode); |
874f9463 TM |
1565 | } |
1566 | ||
651b0e70 | 1567 | static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi) |
874f9463 | 1568 | { |
651b0e70 | 1569 | return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi); |
874f9463 TM |
1570 | } |
1571 | ||
783b194c | 1572 | static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr) |
a895b4a1 | 1573 | { |
e86d5a02 | 1574 | struct timespec64 ts; |
95582b00 | 1575 | |
9e6e70f8 TM |
1576 | if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE) |
1577 | && (fattr->valid & NFS_ATTR_FATTR_CHANGE) | |
c472c07b | 1578 | && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) { |
1eb5d98f | 1579 | inode_set_iversion_raw(inode, fattr->change_attr); |
70ca8852 | 1580 | if (S_ISDIR(inode->i_mode)) |
6edf9609 | 1581 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
0f44da51 FL |
1582 | else if (nfs_server_capable(inode, NFS_CAP_XATTR)) |
1583 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR); | |
70ca8852 | 1584 | } |
a895b4a1 | 1585 | /* If we have atomic WCC data, we may update some attributes */ |
55e04e9c | 1586 | ts = inode_get_ctime(inode); |
9e6e70f8 TM |
1587 | if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME) |
1588 | && (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
e86d5a02 | 1589 | && timespec64_equal(&ts, &fattr->pre_ctime)) { |
55e04e9c | 1590 | inode_set_ctime_to_ts(inode, fattr->ctime); |
27dc1cd3 | 1591 | } |
9e6e70f8 | 1592 | |
41d581a9 | 1593 | ts = inode_get_mtime(inode); |
9e6e70f8 TM |
1594 | if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME) |
1595 | && (fattr->valid & NFS_ATTR_FATTR_MTIME) | |
e86d5a02 | 1596 | && timespec64_equal(&ts, &fattr->pre_mtime)) { |
41d581a9 | 1597 | inode_set_mtime_to_ts(inode, fattr->mtime); |
a895b4a1 | 1598 | } |
9e6e70f8 TM |
1599 | if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE) |
1600 | && (fattr->valid & NFS_ATTR_FATTR_SIZE) | |
1601 | && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size) | |
a6b6d5b8 | 1602 | && !nfs_have_writebacks(inode)) { |
110cb2d2 | 1603 | trace_nfs_size_wcc(inode, fattr->size); |
27dc1cd3 | 1604 | i_size_write(inode, nfs_size_to_loff_t(fattr->size)); |
27dc1cd3 | 1605 | } |
a895b4a1 TM |
1606 | } |
1607 | ||
1da177e4 | 1608 | /** |
33801147 | 1609 | * nfs_check_inode_attributes - verify consistency of the inode attribute cache |
302fad7b TM |
1610 | * @inode: pointer to inode |
1611 | * @fattr: updated attributes | |
1da177e4 LT |
1612 | * |
1613 | * Verifies the attribute cache. If we have just changed the attributes, | |
1614 | * so that fattr carries weak cache consistency data, then it may | |
1615 | * also update the ctime/mtime/change_attribute. | |
1616 | */ | |
33801147 | 1617 | static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr) |
1da177e4 LT |
1618 | { |
1619 | struct nfs_inode *nfsi = NFS_I(inode); | |
1620 | loff_t cur_size, new_isize; | |
2a3f5fd4 | 1621 | unsigned long invalid = 0; |
e86d5a02 | 1622 | struct timespec64 ts; |
dc59250c | 1623 | |
4201916f | 1624 | if (nfs_have_delegated_attributes(inode)) |
01da47bd | 1625 | return 0; |
4ebe83af | 1626 | |
eb3d8f42 TM |
1627 | if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { |
1628 | /* Only a mounted-on-fileid? Just exit */ | |
1629 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) | |
1630 | return 0; | |
ca62b9c3 | 1631 | /* Has the inode gone and changed behind our back? */ |
eb3d8f42 | 1632 | } else if (nfsi->fileid != fattr->fileid) { |
7e10cc25 TM |
1633 | /* Is this perhaps the mounted-on fileid? */ |
1634 | if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) && | |
1635 | nfsi->fileid == fattr->mounted_on_fileid) | |
1636 | return 0; | |
cc89684c | 1637 | return -ESTALE; |
7e10cc25 | 1638 | } |
6e3e2c43 | 1639 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) |
cc89684c | 1640 | return -ESTALE; |
ca62b9c3 | 1641 | |
7e10cc25 | 1642 | |
651b0e70 | 1643 | if (!nfs_file_has_buffered_writers(nfsi)) { |
ca0daa27 | 1644 | /* Verify a few of the more important attributes */ |
c472c07b | 1645 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr)) |
04c63498 | 1646 | invalid |= NFS_INO_INVALID_CHANGE; |
1da177e4 | 1647 | |
41d581a9 | 1648 | ts = inode_get_mtime(inode); |
e86d5a02 | 1649 | if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime)) |
16e14375 | 1650 | invalid |= NFS_INO_INVALID_MTIME; |
ca62b9c3 | 1651 | |
55e04e9c | 1652 | ts = inode_get_ctime(inode); |
e86d5a02 | 1653 | if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime)) |
16e14375 | 1654 | invalid |= NFS_INO_INVALID_CTIME; |
ca0daa27 TM |
1655 | |
1656 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) { | |
1657 | cur_size = i_size_read(inode); | |
1658 | new_isize = nfs_size_to_loff_t(fattr->size); | |
1659 | if (cur_size != new_isize) | |
04c63498 | 1660 | invalid |= NFS_INO_INVALID_SIZE; |
ca0daa27 | 1661 | } |
9e6e70f8 | 1662 | } |
1da177e4 LT |
1663 | |
1664 | /* Have any file permissions changed? */ | |
9e6e70f8 | 1665 | if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) |
04c63498 | 1666 | invalid |= NFS_INO_INVALID_MODE; |
9ff593c4 | 1667 | if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid)) |
04c63498 | 1668 | invalid |= NFS_INO_INVALID_OTHER; |
9ff593c4 | 1669 | if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid)) |
04c63498 | 1670 | invalid |= NFS_INO_INVALID_OTHER; |
1da177e4 LT |
1671 | |
1672 | /* Has the link count changed? */ | |
9e6e70f8 | 1673 | if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink) |
fabf2b34 | 1674 | invalid |= NFS_INO_INVALID_NLINK; |
1da177e4 | 1675 | |
41d581a9 | 1676 | ts = inode_get_atime(inode); |
e86d5a02 | 1677 | if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime)) |
2a3f5fd4 TM |
1678 | invalid |= NFS_INO_INVALID_ATIME; |
1679 | ||
1680 | if (invalid != 0) | |
4ebe83af | 1681 | nfs_set_cache_invalid(inode, invalid); |
1da177e4 | 1682 | |
33801147 | 1683 | nfsi->read_cache_jiffies = fattr->time_start; |
1da177e4 LT |
1684 | return 0; |
1685 | } | |
1686 | ||
ae05f269 | 1687 | static atomic_long_t nfs_attr_generation_counter; |
4704f0e2 TM |
1688 | |
1689 | static unsigned long nfs_read_attr_generation_counter(void) | |
1690 | { | |
ae05f269 | 1691 | return atomic_long_read(&nfs_attr_generation_counter); |
4704f0e2 TM |
1692 | } |
1693 | ||
1694 | unsigned long nfs_inc_attr_generation_counter(void) | |
1695 | { | |
ae05f269 | 1696 | return atomic_long_inc_return(&nfs_attr_generation_counter); |
4704f0e2 | 1697 | } |
3235b403 | 1698 | EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter); |
4704f0e2 TM |
1699 | |
1700 | void nfs_fattr_init(struct nfs_fattr *fattr) | |
1701 | { | |
1702 | fattr->valid = 0; | |
1703 | fattr->time_start = jiffies; | |
1704 | fattr->gencount = nfs_inc_attr_generation_counter(); | |
6926afd1 TM |
1705 | fattr->owner_name = NULL; |
1706 | fattr->group_name = NULL; | |
dc270d71 | 1707 | fattr->mdsthreshold = NULL; |
4704f0e2 | 1708 | } |
ddda8e0a | 1709 | EXPORT_SYMBOL_GPL(nfs_fattr_init); |
4704f0e2 | 1710 | |
140e049c TM |
1711 | /** |
1712 | * nfs_fattr_set_barrier | |
1713 | * @fattr: attributes | |
1714 | * | |
1715 | * Used to set a barrier after an attribute was updated. This | |
1716 | * barrier ensures that older attributes from RPC calls that may | |
1717 | * have raced with our update cannot clobber these new values. | |
1718 | * Note that you are still responsible for ensuring that other | |
1719 | * operations which change the attribute on the server do not | |
1720 | * collide. | |
1721 | */ | |
1722 | void nfs_fattr_set_barrier(struct nfs_fattr *fattr) | |
1723 | { | |
1724 | fattr->gencount = nfs_inc_attr_generation_counter(); | |
1725 | } | |
1726 | ||
2d36bfde TM |
1727 | struct nfs_fattr *nfs_alloc_fattr(void) |
1728 | { | |
1729 | struct nfs_fattr *fattr; | |
1730 | ||
da48f267 | 1731 | fattr = kmalloc(sizeof(*fattr), GFP_KERNEL); |
d4a95a7e | 1732 | if (fattr != NULL) { |
2d36bfde | 1733 | nfs_fattr_init(fattr); |
d4a95a7e TM |
1734 | fattr->label = NULL; |
1735 | } | |
2d36bfde TM |
1736 | return fattr; |
1737 | } | |
ddda8e0a | 1738 | EXPORT_SYMBOL_GPL(nfs_alloc_fattr); |
2d36bfde | 1739 | |
d755ad8d AS |
1740 | struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server) |
1741 | { | |
1742 | struct nfs_fattr *fattr = nfs_alloc_fattr(); | |
1743 | ||
1744 | if (!fattr) | |
1745 | return NULL; | |
1746 | ||
da48f267 | 1747 | fattr->label = nfs4_label_alloc(server, GFP_KERNEL); |
d755ad8d AS |
1748 | if (IS_ERR(fattr->label)) { |
1749 | kfree(fattr); | |
1750 | return NULL; | |
1751 | } | |
1752 | ||
1753 | return fattr; | |
1754 | } | |
1755 | EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label); | |
1756 | ||
2d36bfde TM |
1757 | struct nfs_fh *nfs_alloc_fhandle(void) |
1758 | { | |
1759 | struct nfs_fh *fh; | |
1760 | ||
da48f267 | 1761 | fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL); |
2d36bfde TM |
1762 | if (fh != NULL) |
1763 | fh->size = 0; | |
1764 | return fh; | |
1765 | } | |
ddda8e0a | 1766 | EXPORT_SYMBOL_GPL(nfs_alloc_fhandle); |
2d36bfde | 1767 | |
e27d359e | 1768 | #ifdef NFS_DEBUG |
d8e0539e WAA |
1769 | /* |
1770 | * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle | |
1771 | * in the same way that wireshark does | |
1772 | * | |
1773 | * @fh: file handle | |
1774 | * | |
1775 | * For debugging only. | |
1776 | */ | |
1777 | u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh) | |
1778 | { | |
1779 | /* wireshark uses 32-bit AUTODIN crc and does a bitwise | |
1780 | * not on the result */ | |
1264a2f0 | 1781 | return nfs_fhandle_hash(fh); |
d8e0539e | 1782 | } |
9e6ee76d | 1783 | EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash); |
d8e0539e WAA |
1784 | |
1785 | /* | |
20d27e92 CL |
1786 | * _nfs_display_fhandle - display an NFS file handle on the console |
1787 | * | |
1788 | * @fh: file handle to display | |
1789 | * @caption: display caption | |
1790 | * | |
1791 | * For debugging only. | |
1792 | */ | |
20d27e92 CL |
1793 | void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption) |
1794 | { | |
1795 | unsigned short i; | |
1796 | ||
fa68a1ba | 1797 | if (fh == NULL || fh->size == 0) { |
20d27e92 CL |
1798 | printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh); |
1799 | return; | |
1800 | } | |
1801 | ||
d8e0539e WAA |
1802 | printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n", |
1803 | caption, fh, fh->size, _nfs_display_fhandle_hash(fh)); | |
20d27e92 CL |
1804 | for (i = 0; i < fh->size; i += 16) { |
1805 | __be32 *pos = (__be32 *)&fh->data[i]; | |
1806 | ||
1807 | switch ((fh->size - i - 1) >> 2) { | |
1808 | case 0: | |
1809 | printk(KERN_DEFAULT " %08x\n", | |
1810 | be32_to_cpup(pos)); | |
1811 | break; | |
1812 | case 1: | |
1813 | printk(KERN_DEFAULT " %08x %08x\n", | |
1814 | be32_to_cpup(pos), be32_to_cpup(pos + 1)); | |
1815 | break; | |
1816 | case 2: | |
1817 | printk(KERN_DEFAULT " %08x %08x %08x\n", | |
1818 | be32_to_cpup(pos), be32_to_cpup(pos + 1), | |
1819 | be32_to_cpup(pos + 2)); | |
1820 | break; | |
1821 | default: | |
1822 | printk(KERN_DEFAULT " %08x %08x %08x %08x\n", | |
1823 | be32_to_cpup(pos), be32_to_cpup(pos + 1), | |
1824 | be32_to_cpup(pos + 2), be32_to_cpup(pos + 3)); | |
1825 | } | |
1826 | } | |
1827 | } | |
9e6ee76d | 1828 | EXPORT_SYMBOL_GPL(_nfs_display_fhandle); |
20d27e92 CL |
1829 | #endif |
1830 | ||
a10ad176 | 1831 | /** |
6f9be83d | 1832 | * nfs_inode_attrs_cmp_generic - compare attributes |
302fad7b | 1833 | * @fattr: attributes |
302fad7b | 1834 | * @inode: pointer to inode |
a10ad176 TM |
1835 | * |
1836 | * Attempt to divine whether or not an RPC call reply carrying stale | |
1837 | * attributes got scheduled after another call carrying updated ones. | |
4704f0e2 | 1838 | * Note also the check for wraparound of 'attr_gencount' |
a10ad176 | 1839 | * |
6f9be83d TM |
1840 | * The function returns '1' if it thinks the attributes in @fattr are |
1841 | * more recent than the ones cached in @inode. Otherwise it returns | |
1842 | * the value '0'. | |
a10ad176 | 1843 | */ |
6f9be83d TM |
1844 | static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr, |
1845 | const struct inode *inode) | |
a10ad176 | 1846 | { |
9fdbfad1 | 1847 | unsigned long attr_gencount = NFS_I(inode)->attr_gencount; |
a10ad176 | 1848 | |
9fdbfad1 TM |
1849 | return (long)(fattr->gencount - attr_gencount) > 0 || |
1850 | (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0; | |
a10ad176 TM |
1851 | } |
1852 | ||
6f9be83d TM |
1853 | /** |
1854 | * nfs_inode_attrs_cmp_monotonic - compare attributes | |
302fad7b | 1855 | * @fattr: attributes |
6f9be83d | 1856 | * @inode: pointer to inode |
a10ad176 TM |
1857 | * |
1858 | * Attempt to divine whether or not an RPC call reply carrying stale | |
1859 | * attributes got scheduled after another call carrying updated ones. | |
1860 | * | |
6f9be83d TM |
1861 | * We assume that the server observes monotonic semantics for |
1862 | * the change attribute, so a larger value means that the attributes in | |
1863 | * @fattr are more recent, in which case the function returns the | |
1864 | * value '1'. | |
1865 | * A return value of '0' indicates no measurable change | |
1866 | * A return value of '-1' means that the attributes in @inode are | |
1867 | * more recent. | |
1868 | */ | |
1869 | static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr, | |
1870 | const struct inode *inode) | |
a10ad176 | 1871 | { |
6f9be83d TM |
1872 | s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode); |
1873 | if (diff > 0) | |
1874 | return 1; | |
1875 | return diff == 0 ? 0 : -1; | |
1876 | } | |
1877 | ||
1878 | /** | |
1879 | * nfs_inode_attrs_cmp_strict_monotonic - compare attributes | |
1880 | * @fattr: attributes | |
1881 | * @inode: pointer to inode | |
a10ad176 | 1882 | * |
6f9be83d TM |
1883 | * Attempt to divine whether or not an RPC call reply carrying stale |
1884 | * attributes got scheduled after another call carrying updated ones. | |
a10ad176 | 1885 | * |
6f9be83d TM |
1886 | * We assume that the server observes strictly monotonic semantics for |
1887 | * the change attribute, so a larger value means that the attributes in | |
1888 | * @fattr are more recent, in which case the function returns the | |
1889 | * value '1'. | |
1890 | * A return value of '-1' means that the attributes in @inode are | |
1891 | * more recent or unchanged. | |
a10ad176 | 1892 | */ |
6f9be83d TM |
1893 | static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr, |
1894 | const struct inode *inode) | |
a10ad176 | 1895 | { |
6f9be83d TM |
1896 | return nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1; |
1897 | } | |
a10ad176 | 1898 | |
6f9be83d TM |
1899 | /** |
1900 | * nfs_inode_attrs_cmp - compare attributes | |
1901 | * @fattr: attributes | |
1902 | * @inode: pointer to inode | |
1903 | * | |
1904 | * This function returns '1' if it thinks the attributes in @fattr are | |
1905 | * more recent than the ones cached in @inode. It returns '-1' if | |
1906 | * the attributes in @inode are more recent than the ones in @fattr, | |
1907 | * and it returns 0 if not sure. | |
1908 | */ | |
1909 | static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr, | |
1910 | const struct inode *inode) | |
1911 | { | |
1912 | if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0) | |
1913 | return 1; | |
1914 | switch (NFS_SERVER(inode)->change_attr_type) { | |
1915 | case NFS4_CHANGE_TYPE_IS_UNDEFINED: | |
1916 | break; | |
1917 | case NFS4_CHANGE_TYPE_IS_TIME_METADATA: | |
1918 | if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE)) | |
1919 | break; | |
1920 | return nfs_inode_attrs_cmp_monotonic(fattr, inode); | |
1921 | default: | |
1922 | if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE)) | |
1923 | break; | |
1924 | return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode); | |
1925 | } | |
1926 | return 0; | |
1927 | } | |
1928 | ||
7b24dacf TM |
1929 | /** |
1930 | * nfs_inode_finish_partial_attr_update - complete a previous inode update | |
1931 | * @fattr: attributes | |
1932 | * @inode: pointer to inode | |
1933 | * | |
1934 | * Returns '1' if the last attribute update left the inode cached | |
1935 | * attributes in a partially unrevalidated state, and @fattr | |
1936 | * matches the change attribute of that partial update. | |
1937 | * Otherwise returns '0'. | |
1938 | */ | |
1939 | static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr, | |
1940 | const struct inode *inode) | |
1941 | { | |
1942 | const unsigned long check_valid = | |
1943 | NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME | | |
1944 | NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | | |
1945 | NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER | | |
1946 | NFS_INO_INVALID_NLINK; | |
1947 | unsigned long cache_validity = NFS_I(inode)->cache_validity; | |
eea41330 | 1948 | enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type; |
7b24dacf | 1949 | |
eea41330 TM |
1950 | if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED && |
1951 | !(cache_validity & NFS_INO_INVALID_CHANGE) && | |
7b24dacf TM |
1952 | (cache_validity & check_valid) != 0 && |
1953 | (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && | |
1954 | nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0) | |
1955 | return 1; | |
1956 | return 0; | |
a10ad176 TM |
1957 | } |
1958 | ||
3db63daa N |
1959 | static void nfs_ooo_merge(struct nfs_inode *nfsi, |
1960 | u64 start, u64 end) | |
1961 | { | |
1962 | int i, cnt; | |
1963 | ||
1964 | if (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER) | |
1965 | /* No point merging anything */ | |
1966 | return; | |
1967 | ||
1968 | if (!nfsi->ooo) { | |
1969 | nfsi->ooo = kmalloc(sizeof(*nfsi->ooo), GFP_ATOMIC); | |
1970 | if (!nfsi->ooo) { | |
1971 | nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER; | |
1972 | return; | |
1973 | } | |
1974 | nfsi->ooo->cnt = 0; | |
1975 | } | |
1976 | ||
1977 | /* add this range, merging if possible */ | |
1978 | cnt = nfsi->ooo->cnt; | |
1979 | for (i = 0; i < cnt; i++) { | |
1980 | if (end == nfsi->ooo->gap[i].start) | |
1981 | end = nfsi->ooo->gap[i].end; | |
1982 | else if (start == nfsi->ooo->gap[i].end) | |
1983 | start = nfsi->ooo->gap[i].start; | |
1984 | else | |
1985 | continue; | |
1986 | /* Remove 'i' from table and loop to insert the new range */ | |
1987 | cnt -= 1; | |
1988 | nfsi->ooo->gap[i] = nfsi->ooo->gap[cnt]; | |
1989 | i = -1; | |
1990 | } | |
1991 | if (start != end) { | |
1992 | if (cnt >= ARRAY_SIZE(nfsi->ooo->gap)) { | |
1993 | nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER; | |
1994 | kfree(nfsi->ooo); | |
1995 | nfsi->ooo = NULL; | |
1996 | return; | |
1997 | } | |
1998 | nfsi->ooo->gap[cnt].start = start; | |
1999 | nfsi->ooo->gap[cnt].end = end; | |
2000 | cnt += 1; | |
2001 | } | |
2002 | nfsi->ooo->cnt = cnt; | |
2003 | } | |
2004 | ||
2005 | static void nfs_ooo_record(struct nfs_inode *nfsi, | |
2006 | struct nfs_fattr *fattr) | |
2007 | { | |
2008 | /* This reply was out-of-order, so record in the | |
2009 | * pre/post change id, possibly cancelling | |
2010 | * gaps created when iversion was jumpped forward. | |
2011 | */ | |
2012 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) && | |
2013 | (fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) | |
2014 | nfs_ooo_merge(nfsi, | |
2015 | fattr->change_attr, | |
2016 | fattr->pre_change_attr); | |
2017 | } | |
2018 | ||
6f9be83d TM |
2019 | static int nfs_refresh_inode_locked(struct inode *inode, |
2020 | struct nfs_fattr *fattr) | |
a10ad176 | 2021 | { |
6f9be83d TM |
2022 | int attr_cmp = nfs_inode_attrs_cmp(fattr, inode); |
2023 | int ret = 0; | |
f4ce1299 TM |
2024 | |
2025 | trace_nfs_refresh_inode_enter(inode); | |
2026 | ||
7b24dacf | 2027 | if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode)) |
f4ce1299 | 2028 | ret = nfs_update_inode(inode, fattr); |
3db63daa N |
2029 | else { |
2030 | nfs_ooo_record(NFS_I(inode), fattr); | |
2031 | ||
2032 | if (attr_cmp == 0) | |
2033 | ret = nfs_check_inode_attributes(inode, fattr); | |
2034 | } | |
f4ce1299 TM |
2035 | |
2036 | trace_nfs_refresh_inode_exit(inode, ret); | |
2037 | return ret; | |
870a5be8 TM |
2038 | } |
2039 | ||
33801147 TM |
2040 | /** |
2041 | * nfs_refresh_inode - try to update the inode attribute cache | |
302fad7b TM |
2042 | * @inode: pointer to inode |
2043 | * @fattr: updated attributes | |
33801147 TM |
2044 | * |
2045 | * Check that an RPC call that returned attributes has not overlapped with | |
2046 | * other recent updates of the inode metadata, then decide whether it is | |
2047 | * safe to do a full update of the inode attributes, or whether just to | |
2048 | * call nfs_check_inode_attributes. | |
2049 | */ | |
2050 | int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr) | |
2051 | { | |
33801147 TM |
2052 | int status; |
2053 | ||
2054 | if ((fattr->valid & NFS_ATTR_FATTR) == 0) | |
2055 | return 0; | |
2056 | spin_lock(&inode->i_lock); | |
870a5be8 | 2057 | status = nfs_refresh_inode_locked(inode, fattr); |
33801147 | 2058 | spin_unlock(&inode->i_lock); |
ef79c097 | 2059 | |
33801147 TM |
2060 | return status; |
2061 | } | |
ddda8e0a | 2062 | EXPORT_SYMBOL_GPL(nfs_refresh_inode); |
33801147 | 2063 | |
16e14375 TM |
2064 | static int nfs_post_op_update_inode_locked(struct inode *inode, |
2065 | struct nfs_fattr *fattr, unsigned int invalid) | |
d65f557f | 2066 | { |
6edf9609 TM |
2067 | if (S_ISDIR(inode->i_mode)) |
2068 | invalid |= NFS_INO_INVALID_DATA; | |
2069 | nfs_set_cache_invalid(inode, invalid); | |
d65f557f TM |
2070 | if ((fattr->valid & NFS_ATTR_FATTR) == 0) |
2071 | return 0; | |
2072 | return nfs_refresh_inode_locked(inode, fattr); | |
2073 | } | |
2074 | ||
decf491f TM |
2075 | /** |
2076 | * nfs_post_op_update_inode - try to update the inode attribute cache | |
302fad7b TM |
2077 | * @inode: pointer to inode |
2078 | * @fattr: updated attributes | |
decf491f TM |
2079 | * |
2080 | * After an operation that has changed the inode metadata, mark the | |
2081 | * attribute cache as being invalid, then try to update it. | |
f551e44f CL |
2082 | * |
2083 | * NB: if the server didn't return any post op attributes, this | |
2084 | * function will force the retrieval of attributes before the next | |
2085 | * NFS request. Thus it should be used only for operations that | |
2086 | * are expected to change one or more attributes, to avoid | |
2087 | * unnecessary NFS requests and trips through nfs_update_inode(). | |
decf491f TM |
2088 | */ |
2089 | int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr) | |
2090 | { | |
d65f557f | 2091 | int status; |
decf491f | 2092 | |
7668fdbe | 2093 | spin_lock(&inode->i_lock); |
92d64e47 | 2094 | nfs_fattr_set_barrier(fattr); |
16e14375 TM |
2095 | status = nfs_post_op_update_inode_locked(inode, fattr, |
2096 | NFS_INO_INVALID_CHANGE | |
d554168f TM |
2097 | | NFS_INO_INVALID_CTIME |
2098 | | NFS_INO_REVAL_FORCED); | |
7668fdbe | 2099 | spin_unlock(&inode->i_lock); |
aa9c2669 | 2100 | |
870a5be8 | 2101 | return status; |
decf491f | 2102 | } |
1c606fb7 | 2103 | EXPORT_SYMBOL_GPL(nfs_post_op_update_inode); |
decf491f | 2104 | |
70ca8852 | 2105 | /** |
a08a8cd3 | 2106 | * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache |
302fad7b TM |
2107 | * @inode: pointer to inode |
2108 | * @fattr: updated attributes | |
70ca8852 TM |
2109 | * |
2110 | * After an operation that has changed the inode metadata, mark the | |
2111 | * attribute cache as being invalid, then try to update it. Fake up | |
2112 | * weak cache consistency data, if none exist. | |
2113 | * | |
2114 | * This function is mainly designed to be used by the ->write_done() functions. | |
2115 | */ | |
a08a8cd3 | 2116 | int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr) |
70ca8852 | 2117 | { |
6f9be83d | 2118 | int attr_cmp = nfs_inode_attrs_cmp(fattr, inode); |
d65f557f TM |
2119 | int status; |
2120 | ||
d65f557f | 2121 | /* Don't do a WCC update if these attributes are already stale */ |
6f9be83d TM |
2122 | if (attr_cmp < 0) |
2123 | return 0; | |
2124 | if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) { | |
3db63daa N |
2125 | /* Record the pre/post change info before clearing PRECHANGE */ |
2126 | nfs_ooo_record(NFS_I(inode), fattr); | |
9e6e70f8 TM |
2127 | fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE |
2128 | | NFS_ATTR_FATTR_PRESIZE | |
2129 | | NFS_ATTR_FATTR_PREMTIME | |
2130 | | NFS_ATTR_FATTR_PRECTIME); | |
d65f557f TM |
2131 | goto out_noforce; |
2132 | } | |
9e6e70f8 TM |
2133 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && |
2134 | (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) { | |
1eb5d98f | 2135 | fattr->pre_change_attr = inode_peek_iversion_raw(inode); |
9e6e70f8 | 2136 | fattr->valid |= NFS_ATTR_FATTR_PRECHANGE; |
70ca8852 | 2137 | } |
9e6e70f8 TM |
2138 | if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 && |
2139 | (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) { | |
55e04e9c | 2140 | fattr->pre_ctime = inode_get_ctime(inode); |
9e6e70f8 TM |
2141 | fattr->valid |= NFS_ATTR_FATTR_PRECTIME; |
2142 | } | |
2143 | if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 && | |
2144 | (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) { | |
41d581a9 | 2145 | fattr->pre_mtime = inode_get_mtime(inode); |
9e6e70f8 TM |
2146 | fattr->valid |= NFS_ATTR_FATTR_PREMTIME; |
2147 | } | |
2148 | if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 && | |
2149 | (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) { | |
a3d01454 | 2150 | fattr->pre_size = i_size_read(inode); |
9e6e70f8 | 2151 | fattr->valid |= NFS_ATTR_FATTR_PRESIZE; |
70ca8852 | 2152 | } |
d65f557f | 2153 | out_noforce: |
16e14375 TM |
2154 | status = nfs_post_op_update_inode_locked(inode, fattr, |
2155 | NFS_INO_INVALID_CHANGE | |
2156 | | NFS_INO_INVALID_CTIME | |
3a39e778 ZB |
2157 | | NFS_INO_INVALID_MTIME |
2158 | | NFS_INO_INVALID_BLOCKS); | |
a08a8cd3 TM |
2159 | return status; |
2160 | } | |
2161 | ||
2162 | /** | |
2163 | * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache | |
302fad7b TM |
2164 | * @inode: pointer to inode |
2165 | * @fattr: updated attributes | |
a08a8cd3 TM |
2166 | * |
2167 | * After an operation that has changed the inode metadata, mark the | |
2168 | * attribute cache as being invalid, then try to update it. Fake up | |
2169 | * weak cache consistency data, if none exist. | |
2170 | * | |
2171 | * This function is mainly designed to be used by the ->write_done() functions. | |
2172 | */ | |
2173 | int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr) | |
2174 | { | |
2175 | int status; | |
2176 | ||
2177 | spin_lock(&inode->i_lock); | |
8f8ba1d7 | 2178 | nfs_fattr_set_barrier(fattr); |
a08a8cd3 | 2179 | status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr); |
d65f557f TM |
2180 | spin_unlock(&inode->i_lock); |
2181 | return status; | |
70ca8852 | 2182 | } |
ddda8e0a | 2183 | EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc); |
70ca8852 | 2184 | |
ea96d1ec | 2185 | |
1da177e4 LT |
2186 | /* |
2187 | * Many nfs protocol calls return the new file attributes after | |
2188 | * an operation. Here we update the inode to reflect the state | |
2189 | * of the server's inode. | |
2190 | * | |
2191 | * This is a bit tricky because we have to make sure all dirty pages | |
2192 | * have been sent off to the server before calling invalidate_inode_pages. | |
2193 | * To make sure no other process adds more write requests while we try | |
2194 | * our best to flush them, we make them sleep during the attribute refresh. | |
2195 | * | |
2196 | * A very similar scenario holds for the dir cache. | |
2197 | */ | |
24aa1fe6 | 2198 | static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr) |
1da177e4 | 2199 | { |
ce62b114 | 2200 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 | 2201 | struct nfs_inode *nfsi = NFS_I(inode); |
951a143b | 2202 | loff_t cur_isize, new_isize; |
ce62b114 | 2203 | u64 fattr_supported = server->fattr_valid; |
2a3f5fd4 | 2204 | unsigned long invalid = 0; |
3e7d950a | 2205 | unsigned long now = jiffies; |
62ab460c | 2206 | unsigned long save_cache_validity; |
651b0e70 | 2207 | bool have_writers = nfs_file_has_buffered_writers(nfsi); |
944171cb | 2208 | bool cache_revalidated = true; |
0b467264 | 2209 | bool attr_changed = false; |
c80d17c5 | 2210 | bool have_delegation; |
1da177e4 | 2211 | |
1e8968c5 | 2212 | dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n", |
3110ff80 | 2213 | __func__, inode->i_sb->s_id, inode->i_ino, |
4f1abd22 | 2214 | nfs_display_fhandle_hash(NFS_FH(inode)), |
1da177e4 LT |
2215 | atomic_read(&inode->i_count), fattr->valid); |
2216 | ||
eb3d8f42 TM |
2217 | if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { |
2218 | /* Only a mounted-on-fileid? Just exit */ | |
2219 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) | |
2220 | return 0; | |
7e10cc25 | 2221 | /* Has the inode gone and changed behind our back? */ |
eb3d8f42 | 2222 | } else if (nfsi->fileid != fattr->fileid) { |
7e10cc25 TM |
2223 | /* Is this perhaps the mounted-on fileid? */ |
2224 | if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) && | |
2225 | nfsi->fileid == fattr->mounted_on_fileid) | |
2226 | return 0; | |
e73e6c9e MT |
2227 | printk(KERN_ERR "NFS: server %s error: fileid changed\n" |
2228 | "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n", | |
2229 | NFS_SERVER(inode)->nfs_client->cl_hostname, | |
2230 | inode->i_sb->s_id, (long long)nfsi->fileid, | |
2231 | (long long)fattr->fileid); | |
2232 | goto out_err; | |
2233 | } | |
1da177e4 LT |
2234 | |
2235 | /* | |
2236 | * Make sure the inode's type hasn't changed. | |
2237 | */ | |
6e3e2c43 | 2238 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) { |
e73e6c9e MT |
2239 | /* |
2240 | * Big trouble! The inode has become a different object. | |
2241 | */ | |
1e8968c5 | 2242 | printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n", |
e73e6c9e MT |
2243 | __func__, inode->i_ino, inode->i_mode, fattr->mode); |
2244 | goto out_err; | |
2245 | } | |
1da177e4 | 2246 | |
a0356862 | 2247 | /* Update the fsid? */ |
9e6e70f8 | 2248 | if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) && |
c37dcd33 | 2249 | !nfs_fsid_equal(&server->fsid, &fattr->fsid) && |
36d43a43 | 2250 | !IS_AUTOMOUNT(inode)) |
8b4bdcf8 TM |
2251 | server->fsid = fattr->fsid; |
2252 | ||
c80d17c5 TM |
2253 | /* Save the delegation state before clearing cache_validity */ |
2254 | have_delegation = nfs_have_delegated_attributes(inode); | |
2255 | ||
1da177e4 LT |
2256 | /* |
2257 | * Update the read time so we don't revalidate too often. | |
2258 | */ | |
33801147 | 2259 | nfsi->read_cache_jiffies = fattr->time_start; |
3e7d950a | 2260 | |
0a741f59 TM |
2261 | /* Fix up any delegated attributes in the struct nfs_fattr */ |
2262 | nfs_fattr_fixup_delegated(inode, fattr); | |
2263 | ||
62ab460c TM |
2264 | save_cache_validity = nfsi->cache_validity; |
2265 | nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR | |
2266 | | NFS_INO_INVALID_ATIME | |
2267 | | NFS_INO_REVAL_FORCED | |
3a39e778 | 2268 | | NFS_INO_INVALID_BLOCKS); |
1da177e4 | 2269 | |
a895b4a1 | 2270 | /* Do atomic weak cache consistency updates */ |
783b194c | 2271 | nfs_wcc_update_inode(inode, fattr); |
a895b4a1 | 2272 | |
944171cb | 2273 | if (pnfs_layoutcommit_outstanding(inode)) { |
709fa576 TM |
2274 | nfsi->cache_validity |= |
2275 | save_cache_validity & | |
2276 | (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | | |
2277 | NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | | |
cc7f2dae | 2278 | NFS_INO_INVALID_BLOCKS); |
944171cb BC |
2279 | cache_revalidated = false; |
2280 | } | |
10b7e9ad | 2281 | |
47aabaa7 | 2282 | /* More cache consistency checks */ |
9e6e70f8 | 2283 | if (fattr->valid & NFS_ATTR_FATTR_CHANGE) { |
3db63daa N |
2284 | if (!have_writers && nfsi->ooo && nfsi->ooo->cnt == 1 && |
2285 | nfsi->ooo->gap[0].end == inode_peek_iversion_raw(inode)) { | |
2286 | /* There is one remaining gap that hasn't been | |
2287 | * merged into iversion - do that now. | |
2288 | */ | |
2289 | inode_set_iversion_raw(inode, nfsi->ooo->gap[0].start); | |
2290 | kfree(nfsi->ooo); | |
2291 | nfsi->ooo = NULL; | |
2292 | } | |
c472c07b | 2293 | if (!inode_eq_iversion_raw(inode, fattr->change_attr)) { |
38512aa9 | 2294 | /* Could it be a race with writeback? */ |
c80d17c5 | 2295 | if (!(have_writers || have_delegation)) { |
0b467264 | 2296 | invalid |= NFS_INO_INVALID_DATA |
38512aa9 | 2297 | | NFS_INO_INVALID_ACCESS |
95ad37f9 FL |
2298 | | NFS_INO_INVALID_ACL |
2299 | | NFS_INO_INVALID_XATTR; | |
cac88f94 | 2300 | /* Force revalidate of all attributes */ |
16e14375 TM |
2301 | save_cache_validity |= NFS_INO_INVALID_CTIME |
2302 | | NFS_INO_INVALID_MTIME | |
2303 | | NFS_INO_INVALID_SIZE | |
4cdfeb64 | 2304 | | NFS_INO_INVALID_BLOCKS |
fabf2b34 | 2305 | | NFS_INO_INVALID_NLINK |
720869eb | 2306 | | NFS_INO_INVALID_MODE |
16e14375 | 2307 | | NFS_INO_INVALID_OTHER; |
38512aa9 TM |
2308 | if (S_ISDIR(inode->i_mode)) |
2309 | nfs_force_lookup_revalidate(inode); | |
a9601ac5 | 2310 | attr_changed = true; |
c80d17c5 TM |
2311 | dprintk("NFS: change_attr change on server for file %s/%ld\n", |
2312 | inode->i_sb->s_id, | |
2313 | inode->i_ino); | |
3db63daa N |
2314 | } else if (!have_delegation) { |
2315 | nfs_ooo_record(nfsi, fattr); | |
2316 | nfs_ooo_merge(nfsi, inode_peek_iversion_raw(inode), | |
2317 | fattr->change_attr); | |
2318 | } | |
1eb5d98f | 2319 | inode_set_iversion_raw(inode, fattr->change_attr); |
9e6e70f8 | 2320 | } |
ade14a7d | 2321 | } else { |
cc7f2dae TM |
2322 | nfsi->cache_validity |= |
2323 | save_cache_validity & NFS_INO_INVALID_CHANGE; | |
213bb584 TM |
2324 | if (!have_delegation || |
2325 | (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0) | |
2326 | cache_revalidated = false; | |
ade14a7d | 2327 | } |
9e6e70f8 | 2328 | |
213bb584 | 2329 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) |
41d581a9 | 2330 | inode_set_mtime_to_ts(inode, fattr->mtime); |
213bb584 | 2331 | else if (fattr_supported & NFS_ATTR_FATTR_MTIME) |
cc7f2dae TM |
2332 | nfsi->cache_validity |= |
2333 | save_cache_validity & NFS_INO_INVALID_MTIME; | |
62ab460c | 2334 | |
213bb584 | 2335 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
55e04e9c | 2336 | inode_set_ctime_to_ts(inode, fattr->ctime); |
213bb584 | 2337 | else if (fattr_supported & NFS_ATTR_FATTR_CTIME) |
cc7f2dae TM |
2338 | nfsi->cache_validity |= |
2339 | save_cache_validity & NFS_INO_INVALID_CTIME; | |
47aabaa7 | 2340 | |
951a143b | 2341 | /* Check if our cached file size is stale */ |
9e6e70f8 TM |
2342 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) { |
2343 | new_isize = nfs_size_to_loff_t(fattr->size); | |
2344 | cur_isize = i_size_read(inode); | |
c80d17c5 | 2345 | if (new_isize != cur_isize && !have_delegation) { |
9e6e70f8 TM |
2346 | /* Do we perhaps have any outstanding writes, or has |
2347 | * the file grown beyond our last write? */ | |
a6b6d5b8 | 2348 | if (!nfs_have_writebacks(inode) || new_isize > cur_isize) { |
110cb2d2 | 2349 | trace_nfs_size_update(inode, new_isize); |
9e6e70f8 | 2350 | i_size_write(inode, new_isize); |
ca0daa27 | 2351 | if (!have_writers) |
16e14375 | 2352 | invalid |= NFS_INO_INVALID_DATA; |
9e6e70f8 | 2353 | } |
1da177e4 | 2354 | } |
4cdfeb64 TM |
2355 | if (new_isize == 0 && |
2356 | !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED | | |
2357 | NFS_ATTR_FATTR_BLOCKS_USED))) { | |
2358 | fattr->du.nfs3.used = 0; | |
2359 | fattr->valid |= NFS_ATTR_FATTR_SPACE_USED; | |
2360 | } | |
213bb584 | 2361 | } else |
cc7f2dae TM |
2362 | nfsi->cache_validity |= |
2363 | save_cache_validity & NFS_INO_INVALID_SIZE; | |
1da177e4 | 2364 | |
9e6e70f8 | 2365 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) |
41d581a9 | 2366 | inode_set_atime_to_ts(inode, fattr->atime); |
213bb584 | 2367 | else if (fattr_supported & NFS_ATTR_FATTR_ATIME) |
cc7f2dae TM |
2368 | nfsi->cache_validity |= |
2369 | save_cache_validity & NFS_INO_INVALID_ATIME; | |
1da177e4 | 2370 | |
9e6e70f8 TM |
2371 | if (fattr->valid & NFS_ATTR_FATTR_MODE) { |
2372 | if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) { | |
9b4b3513 TM |
2373 | umode_t newmode = inode->i_mode & S_IFMT; |
2374 | newmode |= fattr->mode & S_IALLUGO; | |
2375 | inode->i_mode = newmode; | |
16e14375 | 2376 | invalid |= NFS_INO_INVALID_ACCESS |
0b467264 | 2377 | | NFS_INO_INVALID_ACL; |
9e6e70f8 | 2378 | } |
213bb584 | 2379 | } else if (fattr_supported & NFS_ATTR_FATTR_MODE) |
cc7f2dae TM |
2380 | nfsi->cache_validity |= |
2381 | save_cache_validity & NFS_INO_INVALID_MODE; | |
62ab460c | 2382 | |
9e6e70f8 | 2383 | if (fattr->valid & NFS_ATTR_FATTR_OWNER) { |
9ff593c4 | 2384 | if (!uid_eq(inode->i_uid, fattr->uid)) { |
16e14375 | 2385 | invalid |= NFS_INO_INVALID_ACCESS |
0b467264 | 2386 | | NFS_INO_INVALID_ACL; |
9e6e70f8 TM |
2387 | inode->i_uid = fattr->uid; |
2388 | } | |
213bb584 | 2389 | } else if (fattr_supported & NFS_ATTR_FATTR_OWNER) |
cc7f2dae TM |
2390 | nfsi->cache_validity |= |
2391 | save_cache_validity & NFS_INO_INVALID_OTHER; | |
62ab460c | 2392 | |
9e6e70f8 | 2393 | if (fattr->valid & NFS_ATTR_FATTR_GROUP) { |
9ff593c4 | 2394 | if (!gid_eq(inode->i_gid, fattr->gid)) { |
16e14375 | 2395 | invalid |= NFS_INO_INVALID_ACCESS |
0b467264 | 2396 | | NFS_INO_INVALID_ACL; |
9e6e70f8 TM |
2397 | inode->i_gid = fattr->gid; |
2398 | } | |
213bb584 | 2399 | } else if (fattr_supported & NFS_ATTR_FATTR_GROUP) |
cc7f2dae TM |
2400 | nfsi->cache_validity |= |
2401 | save_cache_validity & NFS_INO_INVALID_OTHER; | |
921615f1 | 2402 | |
9e6e70f8 | 2403 | if (fattr->valid & NFS_ATTR_FATTR_NLINK) { |
36a10a3c | 2404 | if (inode->i_nlink != fattr->nlink) |
bfe86848 | 2405 | set_nlink(inode, fattr->nlink); |
213bb584 | 2406 | } else if (fattr_supported & NFS_ATTR_FATTR_NLINK) |
cc7f2dae TM |
2407 | nfsi->cache_validity |= |
2408 | save_cache_validity & NFS_INO_INVALID_NLINK; | |
1da177e4 | 2409 | |
9e6e70f8 | 2410 | if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { |
1da177e4 LT |
2411 | /* |
2412 | * report the blocks in 512byte units | |
2413 | */ | |
2414 | inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); | |
213bb584 | 2415 | } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED) |
ce62b114 TM |
2416 | nfsi->cache_validity |= |
2417 | save_cache_validity & NFS_INO_INVALID_BLOCKS; | |
ce62b114 | 2418 | |
213bb584 | 2419 | if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) |
9e6e70f8 | 2420 | inode->i_blocks = fattr->du.nfs2.blocks; |
213bb584 | 2421 | else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED) |
cc7f2dae TM |
2422 | nfsi->cache_validity |= |
2423 | save_cache_validity & NFS_INO_INVALID_BLOCKS; | |
1da177e4 LT |
2424 | |
2425 | /* Update attrtimeo value if we're out of the unstable period */ | |
0b467264 | 2426 | if (attr_changed) { |
91d5b470 | 2427 | nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); |
1da177e4 | 2428 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
3e7d950a | 2429 | nfsi->attrtimeo_timestamp = now; |
f5062003 | 2430 | /* Set barrier to be more recent than all outstanding updates */ |
4704f0e2 | 2431 | nfsi->attr_gencount = nfs_inc_attr_generation_counter(); |
6d2b2966 | 2432 | } else { |
ade14a7d TM |
2433 | if (cache_revalidated) { |
2434 | if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, | |
2435 | nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) { | |
2436 | nfsi->attrtimeo <<= 1; | |
2437 | if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode)) | |
2438 | nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode); | |
2439 | } | |
6d2b2966 TM |
2440 | nfsi->attrtimeo_timestamp = now; |
2441 | } | |
f5062003 | 2442 | /* Set the barrier to be more recent than this fattr */ |
9fdbfad1 | 2443 | if ((long)(fattr->gencount - nfsi->attr_gencount) > 0) |
f5062003 | 2444 | nfsi->attr_gencount = fattr->gencount; |
1da177e4 | 2445 | } |
c812012f | 2446 | |
1da177e4 LT |
2447 | /* Don't invalidate the data if we were to blame */ |
2448 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) | |
2449 | || S_ISLNK(inode->i_mode))) | |
2450 | invalid &= ~NFS_INO_INVALID_DATA; | |
c80d17c5 | 2451 | nfs_set_cache_invalid(inode, invalid); |
de242c0b | 2452 | |
1da177e4 | 2453 | return 0; |
b37b03b7 | 2454 | out_err: |
1da177e4 LT |
2455 | /* |
2456 | * No need to worry about unhashing the dentry, as the | |
2457 | * lookup validation will know that the inode is bad. | |
2458 | * (But we fall through to invalidate the caches.) | |
2459 | */ | |
93ce4af7 | 2460 | nfs_set_inode_stale_locked(inode); |
1da177e4 LT |
2461 | return -ESTALE; |
2462 | } | |
2463 | ||
f7b422b1 | 2464 | struct inode *nfs_alloc_inode(struct super_block *sb) |
1da177e4 LT |
2465 | { |
2466 | struct nfs_inode *nfsi; | |
fd60b288 | 2467 | nfsi = alloc_inode_sb(sb, nfs_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
2468 | if (!nfsi) |
2469 | return NULL; | |
55296809 CL |
2470 | nfsi->flags = 0UL; |
2471 | nfsi->cache_validity = 0UL; | |
3db63daa | 2472 | nfsi->ooo = NULL; |
89d77c8f | 2473 | #if IS_ENABLED(CONFIG_NFS_V4) |
e50a1c2e BF |
2474 | nfsi->nfs4_acl = NULL; |
2475 | #endif /* CONFIG_NFS_V4 */ | |
95ad37f9 FL |
2476 | #ifdef CONFIG_NFS_V4_2 |
2477 | nfsi->xattr_cache = NULL; | |
2478 | #endif | |
000dbe0b DW |
2479 | nfs_netfs_inode_init(nfsi); |
2480 | ||
1da177e4 LT |
2481 | return &nfsi->vfs_inode; |
2482 | } | |
89d77c8f | 2483 | EXPORT_SYMBOL_GPL(nfs_alloc_inode); |
1da177e4 | 2484 | |
ca1a199e | 2485 | void nfs_free_inode(struct inode *inode) |
1da177e4 | 2486 | { |
3db63daa | 2487 | kfree(NFS_I(inode)->ooo); |
1da177e4 LT |
2488 | kmem_cache_free(nfs_inode_cachep, NFS_I(inode)); |
2489 | } | |
ca1a199e | 2490 | EXPORT_SYMBOL_GPL(nfs_free_inode); |
fa0d7e3d | 2491 | |
d75d5414 AM |
2492 | static inline void nfs4_init_once(struct nfs_inode *nfsi) |
2493 | { | |
89d77c8f | 2494 | #if IS_ENABLED(CONFIG_NFS_V4) |
d75d5414 AM |
2495 | INIT_LIST_HEAD(&nfsi->open_states); |
2496 | nfsi->delegation = NULL; | |
d75d5414 | 2497 | init_rwsem(&nfsi->rwsem); |
e5e94017 | 2498 | nfsi->layout = NULL; |
d75d5414 AM |
2499 | #endif |
2500 | } | |
f7b422b1 | 2501 | |
51cc5068 | 2502 | static void init_once(void *foo) |
1da177e4 | 2503 | { |
7e7ce2cc | 2504 | struct nfs_inode *nfsi = foo; |
1da177e4 | 2505 | |
a35afb83 | 2506 | inode_init_once(&nfsi->vfs_inode); |
a35afb83 CL |
2507 | INIT_LIST_HEAD(&nfsi->open_files); |
2508 | INIT_LIST_HEAD(&nfsi->access_cache_entry_lru); | |
2509 | INIT_LIST_HEAD(&nfsi->access_cache_inode_lru); | |
a35afb83 | 2510 | nfs4_init_once(nfsi); |
1da177e4 | 2511 | } |
20c2df83 | 2512 | |
f7b422b1 | 2513 | static int __init nfs_init_inodecache(void) |
1da177e4 LT |
2514 | { |
2515 | nfs_inode_cachep = kmem_cache_create("nfs_inode_cache", | |
2516 | sizeof(struct nfs_inode), | |
fffb60f9 | 2517 | 0, (SLAB_RECLAIM_ACCOUNT| |
f88c3fb8 | 2518 | SLAB_ACCOUNT), |
20c2df83 | 2519 | init_once); |
1da177e4 LT |
2520 | if (nfs_inode_cachep == NULL) |
2521 | return -ENOMEM; | |
2522 | ||
2523 | return 0; | |
2524 | } | |
2525 | ||
266bee88 | 2526 | static void nfs_destroy_inodecache(void) |
1da177e4 | 2527 | { |
8c0a8537 KS |
2528 | /* |
2529 | * Make sure all delayed rcu free inodes are flushed before we | |
2530 | * destroy cache. | |
2531 | */ | |
2532 | rcu_barrier(); | |
1a1d92c1 | 2533 | kmem_cache_destroy(nfs_inode_cachep); |
1da177e4 LT |
2534 | } |
2535 | ||
b9f5dd57 | 2536 | struct workqueue_struct *nfslocaliod_workqueue; |
5746006f | 2537 | struct workqueue_struct *nfsiod_workqueue; |
89d77c8f | 2538 | EXPORT_SYMBOL_GPL(nfsiod_workqueue); |
5746006f TM |
2539 | |
2540 | /* | |
b9f5dd57 | 2541 | * Destroy the nfsiod workqueues |
5746006f | 2542 | */ |
b9f5dd57 | 2543 | static void nfsiod_stop(void) |
5746006f TM |
2544 | { |
2545 | struct workqueue_struct *wq; | |
b9f5dd57 TM |
2546 | |
2547 | wq = nfsiod_workqueue; | |
2548 | if (wq != NULL) { | |
2549 | nfsiod_workqueue = NULL; | |
2550 | destroy_workqueue(wq); | |
2551 | } | |
2552 | #if IS_ENABLED(CONFIG_NFS_LOCALIO) | |
2553 | wq = nfslocaliod_workqueue; | |
2554 | if (wq != NULL) { | |
2555 | nfslocaliod_workqueue = NULL; | |
2556 | destroy_workqueue(wq); | |
2557 | } | |
2558 | #endif /* CONFIG_NFS_LOCALIO */ | |
5746006f TM |
2559 | } |
2560 | ||
2561 | /* | |
b9f5dd57 | 2562 | * Start the nfsiod workqueues |
5746006f | 2563 | */ |
b9f5dd57 | 2564 | static int nfsiod_start(void) |
5746006f | 2565 | { |
b9f5dd57 TM |
2566 | dprintk("RPC: creating workqueue nfsiod\n"); |
2567 | nfsiod_workqueue = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); | |
2568 | if (nfsiod_workqueue == NULL) | |
2569 | return -ENOMEM; | |
2570 | #if IS_ENABLED(CONFIG_NFS_LOCALIO) | |
2571 | /* | |
2572 | * localio writes need to use a normal (non-memreclaim) workqueue. | |
2573 | * When we start getting low on space, XFS goes and calls flush_work() on | |
2574 | * a non-memreclaim work queue, which causes a priority inversion problem. | |
2575 | */ | |
2576 | dprintk("RPC: creating workqueue nfslocaliod\n"); | |
2577 | nfslocaliod_workqueue = alloc_workqueue("nfslocaliod", WQ_UNBOUND, 0); | |
2578 | if (unlikely(nfslocaliod_workqueue == NULL)) { | |
2579 | nfsiod_stop(); | |
2580 | return -ENOMEM; | |
2581 | } | |
2582 | #endif /* CONFIG_NFS_LOCALIO */ | |
2583 | return 0; | |
5746006f TM |
2584 | } |
2585 | ||
c7d03a00 | 2586 | unsigned int nfs_net_id; |
9e2e74db | 2587 | EXPORT_SYMBOL_GPL(nfs_net_id); |
1b340d01 SK |
2588 | |
2589 | static int nfs_net_init(struct net *net) | |
2590 | { | |
1548036e | 2591 | struct nfs_net *nn = net_generic(net, nfs_net_id); |
e8d6f3ab | 2592 | int err; |
1548036e | 2593 | |
6b13168b | 2594 | nfs_clients_init(net); |
24457f1b KI |
2595 | |
2596 | if (!rpc_proc_register(net, &nn->rpcstats)) { | |
e8d6f3ab KI |
2597 | err = -ENOMEM; |
2598 | goto err_proc_rpc; | |
24457f1b KI |
2599 | } |
2600 | ||
e8d6f3ab KI |
2601 | err = nfs_fs_proc_net_init(net); |
2602 | if (err) | |
2603 | goto err_proc_nfs; | |
2604 | ||
2605 | return 0; | |
2606 | ||
2607 | err_proc_nfs: | |
2608 | rpc_proc_unregister(net, "nfs"); | |
2609 | err_proc_rpc: | |
2610 | nfs_clients_exit(net); | |
2611 | return err; | |
1b340d01 SK |
2612 | } |
2613 | ||
2614 | static void nfs_net_exit(struct net *net) | |
2615 | { | |
d47151b7 | 2616 | rpc_proc_unregister(net, "nfs"); |
65b38851 | 2617 | nfs_fs_proc_net_exit(net); |
10b7a70c | 2618 | nfs_clients_exit(net); |
1b340d01 SK |
2619 | } |
2620 | ||
2621 | static struct pernet_operations nfs_net_ops = { | |
2622 | .init = nfs_net_init, | |
2623 | .exit = nfs_net_exit, | |
2624 | .id = &nfs_net_id, | |
2625 | .size = sizeof(struct nfs_net), | |
2626 | }; | |
2627 | ||
1da177e4 LT |
2628 | /* |
2629 | * Initialize NFS | |
2630 | */ | |
2631 | static int __init init_nfs_fs(void) | |
2632 | { | |
2633 | int err; | |
2634 | ||
996bc4f4 TM |
2635 | err = nfs_sysfs_init(); |
2636 | if (err < 0) | |
2637 | goto out10; | |
2638 | ||
1b340d01 | 2639 | err = register_pernet_subsys(&nfs_net_ops); |
e571cbf1 | 2640 | if (err < 0) |
89d77c8f | 2641 | goto out9; |
e571cbf1 | 2642 | |
5746006f TM |
2643 | err = nfsiod_start(); |
2644 | if (err) | |
89d77c8f | 2645 | goto out7; |
5746006f | 2646 | |
6aaca566 DH |
2647 | err = nfs_fs_proc_init(); |
2648 | if (err) | |
89d77c8f | 2649 | goto out6; |
6aaca566 | 2650 | |
1da177e4 LT |
2651 | err = nfs_init_nfspagecache(); |
2652 | if (err) | |
89d77c8f | 2653 | goto out5; |
1da177e4 LT |
2654 | |
2655 | err = nfs_init_inodecache(); | |
2656 | if (err) | |
89d77c8f | 2657 | goto out4; |
1da177e4 LT |
2658 | |
2659 | err = nfs_init_readpagecache(); | |
2660 | if (err) | |
89d77c8f | 2661 | goto out3; |
1da177e4 LT |
2662 | |
2663 | err = nfs_init_writepagecache(); | |
2664 | if (err) | |
89d77c8f | 2665 | goto out2; |
1da177e4 | 2666 | |
1da177e4 LT |
2667 | err = nfs_init_directcache(); |
2668 | if (err) | |
89d77c8f | 2669 | goto out1; |
1da177e4 | 2670 | |
cd738ee9 KM |
2671 | err = register_nfs_fs(); |
2672 | if (err) | |
129d1977 BS |
2673 | goto out0; |
2674 | ||
1da177e4 | 2675 | return 0; |
129d1977 | 2676 | out0: |
1da177e4 | 2677 | nfs_destroy_directcache(); |
89d77c8f | 2678 | out1: |
129d1977 | 2679 | nfs_destroy_writepagecache(); |
89d77c8f | 2680 | out2: |
129d1977 | 2681 | nfs_destroy_readpagecache(); |
89d77c8f | 2682 | out3: |
129d1977 | 2683 | nfs_destroy_inodecache(); |
89d77c8f | 2684 | out4: |
129d1977 | 2685 | nfs_destroy_nfspagecache(); |
89d77c8f | 2686 | out5: |
129d1977 | 2687 | nfs_fs_proc_exit(); |
89d77c8f | 2688 | out6: |
129d1977 | 2689 | nfsiod_stop(); |
89d77c8f | 2690 | out7: |
129d1977 | 2691 | unregister_pernet_subsys(&nfs_net_ops); |
89d77c8f | 2692 | out9: |
996bc4f4 TM |
2693 | nfs_sysfs_exit(); |
2694 | out10: | |
1da177e4 LT |
2695 | return err; |
2696 | } | |
2697 | ||
2698 | static void __exit exit_nfs_fs(void) | |
2699 | { | |
1da177e4 | 2700 | nfs_destroy_directcache(); |
1da177e4 LT |
2701 | nfs_destroy_writepagecache(); |
2702 | nfs_destroy_readpagecache(); | |
2703 | nfs_destroy_inodecache(); | |
2704 | nfs_destroy_nfspagecache(); | |
1b340d01 | 2705 | unregister_pernet_subsys(&nfs_net_ops); |
f7b422b1 | 2706 | unregister_nfs_fs(); |
6aaca566 | 2707 | nfs_fs_proc_exit(); |
5746006f | 2708 | nfsiod_stop(); |
996bc4f4 | 2709 | nfs_sysfs_exit(); |
1da177e4 LT |
2710 | } |
2711 | ||
2712 | /* Not quite true; I just maintain it */ | |
2713 | MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); | |
d3318990 | 2714 | MODULE_DESCRIPTION("NFS client support"); |
1da177e4 | 2715 | MODULE_LICENSE("GPL"); |
f43bf0be | 2716 | module_param(enable_ino64, bool, 0644); |
1da177e4 LT |
2717 | |
2718 | module_init(init_nfs_fs) | |
2719 | module_exit(exit_nfs_fs) |