blob: f27ecc2e490f23ee62aaa15493df9f1025d96614 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/fs/nfs/inode.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
6 *
7 * nfs inode and superblock handling functions
8 *
9 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
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
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/sched/signal.h>
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>
29#include <linux/sunrpc/metrics.h>
30#include <linux/nfs_fs.h>
31#include <linux/nfs_mount.h>
32#include <linux/nfs4_mount.h>
33#include <linux/lockd/bind.h>
34#include <linux/seq_file.h>
35#include <linux/mount.h>
36#include <linux/vfs.h>
37#include <linux/inet.h>
38#include <linux/nfs_xdr.h>
39#include <linux/slab.h>
40#include <linux/compat.h>
41#include <linux/freezer.h>
42#include <linux/uaccess.h>
43#include <linux/iversion.h>
44
45#include "nfs4_fs.h"
46#include "callback.h"
47#include "delegation.h"
48#include "iostat.h"
49#include "internal.h"
50#include "fscache.h"
51#include "pnfs.h"
52#include "nfs.h"
53#include "netns.h"
David Brazdil0f672f62019-12-10 10:32:29 +000054#include "sysfs.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
56#include "nfstrace.h"
57
58#define NFSDBG_FACILITY NFSDBG_VFS
59
60#define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
61
62/* Default is to see 64-bit inode numbers */
63static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
64
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065static int nfs_update_inode(struct inode *, struct nfs_fattr *);
66
67static struct kmem_cache * nfs_inode_cachep;
68
69static inline unsigned long
70nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
71{
72 return nfs_fileid_to_ino_t(fattr->fileid);
73}
74
75static int nfs_wait_killable(int mode)
76{
77 freezable_schedule_unsafe();
78 if (signal_pending_state(mode, current))
79 return -ERESTARTSYS;
80 return 0;
81}
82
83int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
84{
85 return nfs_wait_killable(mode);
86}
87EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
88
89/**
90 * nfs_compat_user_ino64 - returns the user-visible inode number
91 * @fileid: 64-bit fileid
92 *
93 * This function returns a 32-bit inode number if the boot parameter
94 * nfs.enable_ino64 is zero.
95 */
96u64 nfs_compat_user_ino64(u64 fileid)
97{
98#ifdef CONFIG_COMPAT
99 compat_ulong_t ino;
100#else
101 unsigned long ino;
102#endif
103
104 if (enable_ino64)
105 return fileid;
106 ino = fileid;
107 if (sizeof(ino) < sizeof(fileid))
108 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
109 return ino;
110}
111
112int nfs_drop_inode(struct inode *inode)
113{
114 return NFS_STALE(inode) || generic_drop_inode(inode);
115}
116EXPORT_SYMBOL_GPL(nfs_drop_inode);
117
118void nfs_clear_inode(struct inode *inode)
119{
120 /*
121 * The following should never happen...
122 */
123 WARN_ON_ONCE(nfs_have_writebacks(inode));
124 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
125 nfs_zap_acl_cache(inode);
126 nfs_access_zap_cache(inode);
127 nfs_fscache_clear_inode(inode);
128}
129EXPORT_SYMBOL_GPL(nfs_clear_inode);
130
131void nfs_evict_inode(struct inode *inode)
132{
133 truncate_inode_pages_final(&inode->i_data);
134 clear_inode(inode);
135 nfs_clear_inode(inode);
136}
137
138int nfs_sync_inode(struct inode *inode)
139{
140 inode_dio_wait(inode);
141 return nfs_wb_all(inode);
142}
143EXPORT_SYMBOL_GPL(nfs_sync_inode);
144
145/**
146 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
David Brazdil0f672f62019-12-10 10:32:29 +0000147 * @mapping: pointer to struct address_space
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 */
149int nfs_sync_mapping(struct address_space *mapping)
150{
151 int ret = 0;
152
153 if (mapping->nrpages != 0) {
154 unmap_mapping_range(mapping, 0, 0, 0);
155 ret = nfs_wb_all(mapping->host);
156 }
157 return ret;
158}
159
160static int nfs_attribute_timeout(struct inode *inode)
161{
162 struct nfs_inode *nfsi = NFS_I(inode);
163
164 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
165}
166
167static bool nfs_check_cache_invalid_delegated(struct inode *inode, unsigned long flags)
168{
169 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
170
171 /* Special case for the pagecache or access cache */
172 if (flags == NFS_INO_REVAL_PAGECACHE &&
173 !(cache_validity & NFS_INO_REVAL_FORCED))
174 return false;
175 return (cache_validity & flags) != 0;
176}
177
178static bool nfs_check_cache_invalid_not_delegated(struct inode *inode, unsigned long flags)
179{
180 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
181
182 if ((cache_validity & flags) != 0)
183 return true;
184 if (nfs_attribute_timeout(inode))
185 return true;
186 return false;
187}
188
189bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
190{
191 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
192 return nfs_check_cache_invalid_delegated(inode, flags);
193
194 return nfs_check_cache_invalid_not_delegated(inode, flags);
195}
Olivier Deprez157378f2022-04-04 15:47:50 +0200196EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
197
198#ifdef CONFIG_NFS_V4_2
199static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
200{
201 return nfsi->xattr_cache != NULL;
202}
203#else
204static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
205{
206 return false;
207}
208#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000209
210static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
211{
212 struct nfs_inode *nfsi = NFS_I(inode);
213 bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
214
215 if (have_delegation) {
216 if (!(flags & NFS_INO_REVAL_FORCED))
217 flags &= ~NFS_INO_INVALID_OTHER;
218 flags &= ~(NFS_INO_INVALID_CHANGE
219 | NFS_INO_INVALID_SIZE
Olivier Deprez157378f2022-04-04 15:47:50 +0200220 | NFS_INO_REVAL_PAGECACHE
221 | NFS_INO_INVALID_XATTR);
222 } else if (flags & NFS_INO_REVAL_PAGECACHE)
223 flags |= NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224
Olivier Deprez157378f2022-04-04 15:47:50 +0200225 if (!nfs_has_xattr_cache(nfsi))
226 flags &= ~NFS_INO_INVALID_XATTR;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 if (inode->i_mapping->nrpages == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000228 flags &= ~(NFS_INO_INVALID_DATA|NFS_INO_DATA_INVAL_DEFER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 nfsi->cache_validity |= flags;
230 if (flags & NFS_INO_INVALID_DATA)
231 nfs_fscache_invalidate(inode);
232}
233
234/*
235 * Invalidate the local caches
236 */
237static void nfs_zap_caches_locked(struct inode *inode)
238{
239 struct nfs_inode *nfsi = NFS_I(inode);
240 int mode = inode->i_mode;
241
242 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
243
244 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
245 nfsi->attrtimeo_timestamp = jiffies;
246
247 memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
248 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
249 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
250 | NFS_INO_INVALID_DATA
251 | NFS_INO_INVALID_ACCESS
252 | NFS_INO_INVALID_ACL
Olivier Deprez157378f2022-04-04 15:47:50 +0200253 | NFS_INO_INVALID_XATTR
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 | NFS_INO_REVAL_PAGECACHE);
255 } else
256 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
257 | NFS_INO_INVALID_ACCESS
258 | NFS_INO_INVALID_ACL
Olivier Deprez157378f2022-04-04 15:47:50 +0200259 | NFS_INO_INVALID_XATTR
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260 | NFS_INO_REVAL_PAGECACHE);
261 nfs_zap_label_cache_locked(nfsi);
262}
263
264void nfs_zap_caches(struct inode *inode)
265{
266 spin_lock(&inode->i_lock);
267 nfs_zap_caches_locked(inode);
268 spin_unlock(&inode->i_lock);
269}
270
271void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
272{
273 if (mapping->nrpages != 0) {
274 spin_lock(&inode->i_lock);
275 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
276 spin_unlock(&inode->i_lock);
277 }
278}
279
280void nfs_zap_acl_cache(struct inode *inode)
281{
282 void (*clear_acl_cache)(struct inode *);
283
284 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
285 if (clear_acl_cache != NULL)
286 clear_acl_cache(inode);
287 spin_lock(&inode->i_lock);
288 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
289 spin_unlock(&inode->i_lock);
290}
291EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
292
293void nfs_invalidate_atime(struct inode *inode)
294{
295 spin_lock(&inode->i_lock);
296 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
297 spin_unlock(&inode->i_lock);
298}
299EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
300
301/*
302 * Invalidate, but do not unhash, the inode.
303 * NB: must be called with inode->i_lock held!
304 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200305static void nfs_set_inode_stale_locked(struct inode *inode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306{
307 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
308 nfs_zap_caches_locked(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +0200309 trace_nfs_set_inode_stale(inode);
310}
311
312void nfs_set_inode_stale(struct inode *inode)
313{
314 spin_lock(&inode->i_lock);
315 nfs_set_inode_stale_locked(inode);
316 spin_unlock(&inode->i_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317}
318
319struct nfs_find_desc {
320 struct nfs_fh *fh;
321 struct nfs_fattr *fattr;
322};
323
324/*
325 * In NFSv3 we can have 64bit inode numbers. In order to support
326 * this, and re-exported directories (also seen in NFSv2)
327 * we are forced to allow 2 different inodes to have the same
328 * i_ino.
329 */
330static int
331nfs_find_actor(struct inode *inode, void *opaque)
332{
333 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
334 struct nfs_fh *fh = desc->fh;
335 struct nfs_fattr *fattr = desc->fattr;
336
337 if (NFS_FILEID(inode) != fattr->fileid)
338 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200339 if (inode_wrong_type(inode, fattr->mode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340 return 0;
341 if (nfs_compare_fh(NFS_FH(inode), fh))
342 return 0;
343 if (is_bad_inode(inode) || NFS_STALE(inode))
344 return 0;
345 return 1;
346}
347
348static int
349nfs_init_locked(struct inode *inode, void *opaque)
350{
351 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
352 struct nfs_fattr *fattr = desc->fattr;
353
354 set_nfs_fileid(inode, fattr->fileid);
355 inode->i_mode = fattr->mode;
356 nfs_copy_fh(NFS_FH(inode), desc->fh);
357 return 0;
358}
359
360#ifdef CONFIG_NFS_V4_SECURITY_LABEL
361static void nfs_clear_label_invalid(struct inode *inode)
362{
363 spin_lock(&inode->i_lock);
364 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
365 spin_unlock(&inode->i_lock);
366}
367
368void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
369 struct nfs4_label *label)
370{
371 int error;
372
373 if (label == NULL)
374 return;
375
376 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
377 error = security_inode_notifysecctx(inode, label->label,
378 label->len);
379 if (error)
380 printk(KERN_ERR "%s() %s %d "
381 "security_inode_notifysecctx() %d\n",
382 __func__,
383 (char *)label->label,
384 label->len, error);
385 nfs_clear_label_invalid(inode);
386 }
387}
388
389struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
390{
391 struct nfs4_label *label = NULL;
392 int minor_version = server->nfs_client->cl_minorversion;
393
394 if (minor_version < 2)
395 return label;
396
397 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
398 return label;
399
400 label = kzalloc(sizeof(struct nfs4_label), flags);
401 if (label == NULL)
402 return ERR_PTR(-ENOMEM);
403
404 label->label = kzalloc(NFS4_MAXLABELLEN, flags);
405 if (label->label == NULL) {
406 kfree(label);
407 return ERR_PTR(-ENOMEM);
408 }
409 label->len = NFS4_MAXLABELLEN;
410
411 return label;
412}
413EXPORT_SYMBOL_GPL(nfs4_label_alloc);
414#else
415void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
416 struct nfs4_label *label)
417{
418}
419#endif
420EXPORT_SYMBOL_GPL(nfs_setsecurity);
421
422/* Search for inode identified by fh, fileid and i_mode in inode cache. */
423struct inode *
424nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
425{
426 struct nfs_find_desc desc = {
427 .fh = fh,
428 .fattr = fattr,
429 };
430 struct inode *inode;
431 unsigned long hash;
432
433 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
434 !(fattr->valid & NFS_ATTR_FATTR_TYPE))
435 return NULL;
436
437 hash = nfs_fattr_to_ino_t(fattr);
438 inode = ilookup5(sb, hash, nfs_find_actor, &desc);
439
440 dprintk("%s: returning %p\n", __func__, inode);
441 return inode;
442}
443
444/*
445 * This is our front-end to iget that looks up inodes by file handle
446 * instead of inode number.
447 */
448struct inode *
449nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
450{
451 struct nfs_find_desc desc = {
452 .fh = fh,
453 .fattr = fattr
454 };
455 struct inode *inode = ERR_PTR(-ENOENT);
456 unsigned long hash;
457
458 nfs_attr_check_mountpoint(sb, fattr);
459
460 if (nfs_attr_use_mounted_on_fileid(fattr))
461 fattr->fileid = fattr->mounted_on_fileid;
462 else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
463 goto out_no_inode;
464 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
465 goto out_no_inode;
466
467 hash = nfs_fattr_to_ino_t(fattr);
468
469 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
470 if (inode == NULL) {
471 inode = ERR_PTR(-ENOMEM);
472 goto out_no_inode;
473 }
474
475 if (inode->i_state & I_NEW) {
476 struct nfs_inode *nfsi = NFS_I(inode);
477 unsigned long now = jiffies;
478
479 /* We set i_ino for the few things that still rely on it,
480 * such as stat(2) */
481 inode->i_ino = hash;
482
483 /* We can't support update_atime(), since the server will reset it */
484 inode->i_flags |= S_NOATIME|S_NOCMTIME;
485 inode->i_mode = fattr->mode;
486 nfsi->cache_validity = 0;
487 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
488 && nfs_server_capable(inode, NFS_CAP_MODE))
489 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
490 /* Why so? Because we want revalidate for devices/FIFOs, and
491 * that's precisely what we have in nfs_file_inode_operations.
492 */
493 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
494 if (S_ISREG(inode->i_mode)) {
495 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
496 inode->i_data.a_ops = &nfs_file_aops;
497 } else if (S_ISDIR(inode->i_mode)) {
498 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
499 inode->i_fop = &nfs_dir_operations;
500 inode->i_data.a_ops = &nfs_dir_aops;
501 /* Deal with crossing mountpoints */
502 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
503 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
504 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
505 inode->i_op = &nfs_referral_inode_operations;
506 else
507 inode->i_op = &nfs_mountpoint_inode_operations;
508 inode->i_fop = NULL;
509 inode->i_flags |= S_AUTOMOUNT;
510 }
511 } else if (S_ISLNK(inode->i_mode)) {
512 inode->i_op = &nfs_symlink_inode_operations;
513 inode_nohighmem(inode);
514 } else
515 init_special_inode(inode, inode->i_mode, fattr->rdev);
516
517 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
518 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
519 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
520 inode_set_iversion_raw(inode, 0);
521 inode->i_size = 0;
522 clear_nlink(inode);
523 inode->i_uid = make_kuid(&init_user_ns, -2);
524 inode->i_gid = make_kgid(&init_user_ns, -2);
525 inode->i_blocks = 0;
526 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
527 nfsi->write_io = 0;
528 nfsi->read_io = 0;
529
530 nfsi->read_cache_jiffies = fattr->time_start;
531 nfsi->attr_gencount = fattr->gencount;
532 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200533 inode->i_atime = fattr->atime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
535 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
536 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200537 inode->i_mtime = fattr->mtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
539 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
540 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200541 inode->i_ctime = fattr->ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
543 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
544 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
545 inode_set_iversion_raw(inode, fattr->change_attr);
546 else
547 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
548 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
549 inode->i_size = nfs_size_to_loff_t(fattr->size);
550 else
551 nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
552 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
553 set_nlink(inode, fattr->nlink);
554 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
555 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
556 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
557 inode->i_uid = fattr->uid;
558 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
559 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
560 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
561 inode->i_gid = fattr->gid;
562 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
563 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Olivier Deprez157378f2022-04-04 15:47:50 +0200564 if (nfs_server_capable(inode, NFS_CAP_XATTR))
565 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000566 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
567 inode->i_blocks = fattr->du.nfs2.blocks;
568 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
569 /*
570 * report the blocks in 512byte units
571 */
572 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
573 }
574
575 if (nfsi->cache_validity != 0)
576 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
577
578 nfs_setsecurity(inode, fattr, label);
579
580 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
581 nfsi->attrtimeo_timestamp = now;
582 nfsi->access_cache = RB_ROOT;
583
584 nfs_fscache_init_inode(inode);
585
586 unlock_new_inode(inode);
587 } else {
588 int err = nfs_refresh_inode(inode, fattr);
589 if (err < 0) {
590 iput(inode);
591 inode = ERR_PTR(err);
592 goto out_no_inode;
593 }
594 }
595 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
596 inode->i_sb->s_id,
597 (unsigned long long)NFS_FILEID(inode),
598 nfs_display_fhandle_hash(fh),
599 atomic_read(&inode->i_count));
600
601out:
602 return inode;
603
604out_no_inode:
605 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
606 goto out;
607}
608EXPORT_SYMBOL_GPL(nfs_fhget);
609
610#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)
611
612int
613nfs_setattr(struct dentry *dentry, struct iattr *attr)
614{
615 struct inode *inode = d_inode(dentry);
616 struct nfs_fattr *fattr;
617 int error = 0;
618
619 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
620
621 /* skip mode change if it's just for clearing setuid/setgid */
622 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
623 attr->ia_valid &= ~ATTR_MODE;
624
625 if (attr->ia_valid & ATTR_SIZE) {
626 BUG_ON(!S_ISREG(inode->i_mode));
627
628 error = inode_newsize_ok(inode, attr->ia_size);
629 if (error)
630 return error;
631
632 if (attr->ia_size == i_size_read(inode))
633 attr->ia_valid &= ~ATTR_SIZE;
634 }
635
636 /* Optimization: if the end result is no change, don't RPC */
637 attr->ia_valid &= NFS_VALID_ATTRS;
638 if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
639 return 0;
640
641 trace_nfs_setattr_enter(inode);
642
643 /* Write all dirty data */
644 if (S_ISREG(inode->i_mode))
645 nfs_sync_inode(inode);
646
647 fattr = nfs_alloc_fattr();
648 if (fattr == NULL) {
649 error = -ENOMEM;
650 goto out;
651 }
652
653 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
654 if (error == 0)
655 error = nfs_refresh_inode(inode, fattr);
656 nfs_free_fattr(fattr);
657out:
658 trace_nfs_setattr_exit(inode, error);
659 return error;
660}
661EXPORT_SYMBOL_GPL(nfs_setattr);
662
663/**
664 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
665 * @inode: inode of the file used
666 * @offset: file offset to start truncating
667 *
668 * This is a copy of the common vmtruncate, but with the locking
669 * corrected to take into account the fact that NFS requires
670 * inode->i_size to be updated under the inode->i_lock.
671 * Note: must be called with inode->i_lock held!
672 */
673static int nfs_vmtruncate(struct inode * inode, loff_t offset)
674{
675 int err;
676
677 err = inode_newsize_ok(inode, offset);
678 if (err)
679 goto out;
680
681 i_size_write(inode, offset);
682 /* Optimisation */
683 if (offset == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000684 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_DATA |
685 NFS_INO_DATA_INVAL_DEFER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000686 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
687
688 spin_unlock(&inode->i_lock);
689 truncate_pagecache(inode, offset);
690 spin_lock(&inode->i_lock);
691out:
692 return err;
693}
694
695/**
696 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
697 * @inode: pointer to struct inode
698 * @attr: pointer to struct iattr
699 * @fattr: pointer to struct nfs_fattr
700 *
701 * Note: we do this in the *proc.c in order to ensure that
702 * it works for things like exclusive creates too.
703 */
704void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
705 struct nfs_fattr *fattr)
706{
707 /* Barrier: bump the attribute generation count. */
708 nfs_fattr_set_barrier(fattr);
709
710 spin_lock(&inode->i_lock);
711 NFS_I(inode)->attr_gencount = fattr->gencount;
712 if ((attr->ia_valid & ATTR_SIZE) != 0) {
713 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
714 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
715 nfs_vmtruncate(inode, attr->ia_size);
716 }
717 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
718 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
719 if ((attr->ia_valid & ATTR_MODE) != 0) {
720 int mode = attr->ia_mode & S_IALLUGO;
721 mode |= inode->i_mode & ~S_IALLUGO;
722 inode->i_mode = mode;
723 }
724 if ((attr->ia_valid & ATTR_UID) != 0)
725 inode->i_uid = attr->ia_uid;
726 if ((attr->ia_valid & ATTR_GID) != 0)
727 inode->i_gid = attr->ia_gid;
728 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200729 inode->i_ctime = fattr->ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000730 else
731 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
732 | NFS_INO_INVALID_CTIME);
733 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
734 | NFS_INO_INVALID_ACL);
735 }
736 if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
737 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
738 | NFS_INO_INVALID_CTIME);
739 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200740 inode->i_atime = fattr->atime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741 else if (attr->ia_valid & ATTR_ATIME_SET)
742 inode->i_atime = attr->ia_atime;
743 else
744 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
745
746 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200747 inode->i_ctime = fattr->ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000748 else
749 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
750 | NFS_INO_INVALID_CTIME);
751 }
752 if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
753 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
754 | NFS_INO_INVALID_CTIME);
755 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200756 inode->i_mtime = fattr->mtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757 else if (attr->ia_valid & ATTR_MTIME_SET)
758 inode->i_mtime = attr->ia_mtime;
759 else
760 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
761
762 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +0200763 inode->i_ctime = fattr->ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000764 else
765 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
766 | NFS_INO_INVALID_CTIME);
767 }
768 if (fattr->valid)
769 nfs_update_inode(inode, fattr);
770 spin_unlock(&inode->i_lock);
771}
772EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
773
774static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
775{
776 struct dentry *parent;
777
778 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
779 return;
780 parent = dget_parent(dentry);
781 nfs_force_use_readdirplus(d_inode(parent));
782 dput(parent);
783}
784
785static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
786{
787 struct dentry *parent;
788
789 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
790 return;
791 parent = dget_parent(dentry);
792 nfs_advise_use_readdirplus(d_inode(parent));
793 dput(parent);
794}
795
796static bool nfs_need_revalidate_inode(struct inode *inode)
797{
798 if (NFS_I(inode)->cache_validity &
799 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
800 return true;
801 if (nfs_attribute_cache_expired(inode))
802 return true;
803 return false;
804}
805
806int nfs_getattr(const struct path *path, struct kstat *stat,
807 u32 request_mask, unsigned int query_flags)
808{
809 struct inode *inode = d_inode(path->dentry);
810 struct nfs_server *server = NFS_SERVER(inode);
811 unsigned long cache_validity;
812 int err = 0;
813 bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
814 bool do_update = false;
815
816 trace_nfs_getattr_enter(inode);
817
Olivier Deprez157378f2022-04-04 15:47:50 +0200818 if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
819 nfs_readdirplus_parent_cache_hit(path->dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 goto out_no_update;
Olivier Deprez157378f2022-04-04 15:47:50 +0200821 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000822
823 /* Flush out writes to the server in order to update c/mtime. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200824 if ((request_mask & (STATX_CTIME | STATX_MTIME)) &&
825 S_ISREG(inode->i_mode))
826 filemap_write_and_wait(inode->i_mapping);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000827
828 /*
829 * We may force a getattr if the user cares about atime.
830 *
831 * Note that we only have to check the vfsmount flags here:
832 * - NFS always sets S_NOATIME by so checking it would give a
833 * bogus result
834 * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
835 * no point in checking those.
836 */
837 if ((path->mnt->mnt_flags & MNT_NOATIME) ||
838 ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
839 request_mask &= ~STATX_ATIME;
840
841 /* Is the user requesting attributes that might need revalidation? */
842 if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
843 STATX_MTIME|STATX_UID|STATX_GID|
844 STATX_SIZE|STATX_BLOCKS)))
845 goto out_no_revalidate;
846
847 /* Check whether the cached attributes are stale */
848 do_update |= force_sync || nfs_attribute_cache_expired(inode);
849 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
850 do_update |= cache_validity &
851 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL);
852 if (request_mask & STATX_ATIME)
853 do_update |= cache_validity & NFS_INO_INVALID_ATIME;
854 if (request_mask & (STATX_CTIME|STATX_MTIME))
855 do_update |= cache_validity & NFS_INO_REVAL_PAGECACHE;
Olivier Deprez0e641232021-09-23 10:07:05 +0200856 if (request_mask & STATX_BLOCKS)
857 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000858 if (do_update) {
859 /* Update the attribute cache */
860 if (!(server->flags & NFS_MOUNT_NOAC))
861 nfs_readdirplus_parent_cache_miss(path->dentry);
862 else
863 nfs_readdirplus_parent_cache_hit(path->dentry);
864 err = __nfs_revalidate_inode(server, inode);
865 if (err)
866 goto out;
867 } else
868 nfs_readdirplus_parent_cache_hit(path->dentry);
869out_no_revalidate:
870 /* Only return attributes that were revalidated. */
871 stat->result_mask &= request_mask;
872out_no_update:
873 generic_fillattr(inode, stat);
874 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
875 if (S_ISDIR(inode->i_mode))
876 stat->blksize = NFS_SERVER(inode)->dtsize;
877out:
878 trace_nfs_getattr_exit(inode, err);
879 return err;
880}
881EXPORT_SYMBOL_GPL(nfs_getattr);
882
883static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
884{
885 refcount_set(&l_ctx->count, 1);
886 l_ctx->lockowner = current->files;
887 INIT_LIST_HEAD(&l_ctx->list);
888 atomic_set(&l_ctx->io_count, 0);
889}
890
891static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
892{
David Brazdil0f672f62019-12-10 10:32:29 +0000893 struct nfs_lock_context *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000894
David Brazdil0f672f62019-12-10 10:32:29 +0000895 list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000896 if (pos->lockowner != current->files)
897 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000898 if (refcount_inc_not_zero(&pos->count))
899 return pos;
900 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 return NULL;
902}
903
904struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
905{
906 struct nfs_lock_context *res, *new = NULL;
907 struct inode *inode = d_inode(ctx->dentry);
908
David Brazdil0f672f62019-12-10 10:32:29 +0000909 rcu_read_lock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000910 res = __nfs_find_lock_context(ctx);
David Brazdil0f672f62019-12-10 10:32:29 +0000911 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000912 if (res == NULL) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 new = kmalloc(sizeof(*new), GFP_KERNEL);
914 if (new == NULL)
915 return ERR_PTR(-ENOMEM);
916 nfs_init_lock_context(new);
917 spin_lock(&inode->i_lock);
918 res = __nfs_find_lock_context(ctx);
919 if (res == NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000920 new->open_context = get_nfs_open_context(ctx);
921 if (new->open_context) {
922 list_add_tail_rcu(&new->list,
923 &ctx->lock_context.list);
924 res = new;
925 new = NULL;
926 } else
927 res = ERR_PTR(-EBADF);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928 }
David Brazdil0f672f62019-12-10 10:32:29 +0000929 spin_unlock(&inode->i_lock);
930 kfree(new);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000931 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000932 return res;
933}
934EXPORT_SYMBOL_GPL(nfs_get_lock_context);
935
936void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
937{
938 struct nfs_open_context *ctx = l_ctx->open_context;
939 struct inode *inode = d_inode(ctx->dentry);
940
941 if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
942 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000943 list_del_rcu(&l_ctx->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944 spin_unlock(&inode->i_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000945 put_nfs_open_context(ctx);
946 kfree_rcu(l_ctx, rcu_head);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000947}
948EXPORT_SYMBOL_GPL(nfs_put_lock_context);
949
950/**
951 * nfs_close_context - Common close_context() routine NFSv2/v3
952 * @ctx: pointer to context
953 * @is_sync: is this a synchronous close
954 *
955 * Ensure that the attributes are up to date if we're mounted
956 * with close-to-open semantics and we have cached data that will
957 * need to be revalidated on open.
958 */
959void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
960{
961 struct nfs_inode *nfsi;
962 struct inode *inode;
963 struct nfs_server *server;
964
965 if (!(ctx->mode & FMODE_WRITE))
966 return;
967 if (!is_sync)
968 return;
969 inode = d_inode(ctx->dentry);
970 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
971 return;
972 nfsi = NFS_I(inode);
973 if (inode->i_mapping->nrpages == 0)
974 return;
975 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
976 return;
977 if (!list_empty(&nfsi->open_files))
978 return;
979 server = NFS_SERVER(inode);
980 if (server->flags & NFS_MOUNT_NOCTO)
981 return;
982 nfs_revalidate_inode(server, inode);
983}
984EXPORT_SYMBOL_GPL(nfs_close_context);
985
986struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
987 fmode_t f_mode,
988 struct file *filp)
989{
990 struct nfs_open_context *ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991
992 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
Olivier Deprez0e641232021-09-23 10:07:05 +0200993 if (!ctx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000994 return ERR_PTR(-ENOMEM);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000995 nfs_sb_active(dentry->d_sb);
996 ctx->dentry = dget(dentry);
Olivier Deprez0e641232021-09-23 10:07:05 +0200997 if (filp)
998 ctx->cred = get_cred(filp->f_cred);
999 else
1000 ctx->cred = get_current_cred();
David Brazdil0f672f62019-12-10 10:32:29 +00001001 ctx->ll_cred = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001002 ctx->state = NULL;
1003 ctx->mode = f_mode;
1004 ctx->flags = 0;
1005 ctx->error = 0;
1006 ctx->flock_owner = (fl_owner_t)filp;
1007 nfs_init_lock_context(&ctx->lock_context);
1008 ctx->lock_context.open_context = ctx;
1009 INIT_LIST_HEAD(&ctx->list);
1010 ctx->mdsthreshold = NULL;
1011 return ctx;
1012}
1013EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
1014
1015struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1016{
David Brazdil0f672f62019-12-10 10:32:29 +00001017 if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1018 return ctx;
1019 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001020}
1021EXPORT_SYMBOL_GPL(get_nfs_open_context);
1022
1023static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1024{
1025 struct inode *inode = d_inode(ctx->dentry);
1026 struct super_block *sb = ctx->dentry->d_sb;
1027
David Brazdil0f672f62019-12-10 10:32:29 +00001028 if (!refcount_dec_and_test(&ctx->lock_context.count))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029 return;
David Brazdil0f672f62019-12-10 10:32:29 +00001030 if (!list_empty(&ctx->list)) {
1031 spin_lock(&inode->i_lock);
1032 list_del_rcu(&ctx->list);
1033 spin_unlock(&inode->i_lock);
1034 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035 if (inode != NULL)
1036 NFS_PROTO(inode)->close_context(ctx, is_sync);
David Brazdil0f672f62019-12-10 10:32:29 +00001037 put_cred(ctx->cred);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 dput(ctx->dentry);
1039 nfs_sb_deactive(sb);
David Brazdil0f672f62019-12-10 10:32:29 +00001040 put_rpccred(ctx->ll_cred);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001041 kfree(ctx->mdsthreshold);
David Brazdil0f672f62019-12-10 10:32:29 +00001042 kfree_rcu(ctx, rcu_head);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001043}
1044
1045void put_nfs_open_context(struct nfs_open_context *ctx)
1046{
1047 __put_nfs_open_context(ctx, 0);
1048}
1049EXPORT_SYMBOL_GPL(put_nfs_open_context);
1050
1051static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1052{
1053 __put_nfs_open_context(ctx, 1);
1054}
1055
1056/*
1057 * Ensure that mmap has a recent RPC credential for use when writing out
1058 * shared pages
1059 */
1060void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
1061{
1062 struct inode *inode = d_inode(ctx->dentry);
1063 struct nfs_inode *nfsi = NFS_I(inode);
1064
1065 spin_lock(&inode->i_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001066 if (list_empty(&nfsi->open_files) &&
1067 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
1068 nfsi->cache_validity |= NFS_INO_INVALID_DATA |
1069 NFS_INO_REVAL_FORCED;
1070 list_add_tail_rcu(&ctx->list, &nfsi->open_files);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071 spin_unlock(&inode->i_lock);
1072}
1073EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1074
1075void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1076{
1077 filp->private_data = get_nfs_open_context(ctx);
Olivier Deprez0e641232021-09-23 10:07:05 +02001078 set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001079 if (list_empty(&ctx->list))
1080 nfs_inode_attach_open_context(ctx);
1081}
1082EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
1083
1084/*
1085 * Given an inode, search for an open context with the desired characteristics
1086 */
David Brazdil0f672f62019-12-10 10:32:29 +00001087struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001088{
1089 struct nfs_inode *nfsi = NFS_I(inode);
1090 struct nfs_open_context *pos, *ctx = NULL;
1091
David Brazdil0f672f62019-12-10 10:32:29 +00001092 rcu_read_lock();
1093 list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001094 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001095 continue;
1096 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1097 continue;
Olivier Deprez0e641232021-09-23 10:07:05 +02001098 if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
1099 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100 ctx = get_nfs_open_context(pos);
David Brazdil0f672f62019-12-10 10:32:29 +00001101 if (ctx)
1102 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001103 }
David Brazdil0f672f62019-12-10 10:32:29 +00001104 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001105 return ctx;
1106}
1107
1108void nfs_file_clear_open_context(struct file *filp)
1109{
1110 struct nfs_open_context *ctx = nfs_file_open_context(filp);
1111
1112 if (ctx) {
1113 struct inode *inode = d_inode(ctx->dentry);
1114
Olivier Deprez0e641232021-09-23 10:07:05 +02001115 clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116 /*
1117 * We fatal error on write before. Try to writeback
1118 * every page again.
1119 */
1120 if (ctx->error < 0)
1121 invalidate_inode_pages2(inode->i_mapping);
1122 filp->private_data = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001123 put_nfs_open_context_sync(ctx);
1124 }
1125}
1126
1127/*
1128 * These allocate and release file read/write context information.
1129 */
1130int nfs_open(struct inode *inode, struct file *filp)
1131{
1132 struct nfs_open_context *ctx;
1133
1134 ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
1135 if (IS_ERR(ctx))
1136 return PTR_ERR(ctx);
1137 nfs_file_set_open_context(filp, ctx);
1138 put_nfs_open_context(ctx);
1139 nfs_fscache_open_file(inode, filp);
1140 return 0;
1141}
David Brazdil0f672f62019-12-10 10:32:29 +00001142EXPORT_SYMBOL_GPL(nfs_open);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143
1144/*
1145 * This function is called whenever some part of NFS notices that
1146 * the cached attributes have to be refreshed.
1147 */
1148int
1149__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1150{
1151 int status = -ESTALE;
1152 struct nfs4_label *label = NULL;
1153 struct nfs_fattr *fattr = NULL;
1154 struct nfs_inode *nfsi = NFS_I(inode);
1155
1156 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1157 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
1158
1159 trace_nfs_revalidate_inode_enter(inode);
1160
1161 if (is_bad_inode(inode))
1162 goto out;
1163 if (NFS_STALE(inode))
1164 goto out;
1165
1166 /* pNFS: Attributes aren't updated until we layoutcommit */
1167 if (S_ISREG(inode->i_mode)) {
1168 status = pnfs_sync_inode(inode, false);
1169 if (status)
1170 goto out;
1171 }
1172
1173 status = -ENOMEM;
1174 fattr = nfs_alloc_fattr();
1175 if (fattr == NULL)
1176 goto out;
1177
1178 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1179
1180 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1181 if (IS_ERR(label)) {
1182 status = PTR_ERR(label);
1183 goto out;
1184 }
1185
1186 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
1187 label, inode);
1188 if (status != 0) {
1189 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
1190 inode->i_sb->s_id,
1191 (unsigned long long)NFS_FILEID(inode), status);
Olivier Deprez157378f2022-04-04 15:47:50 +02001192 switch (status) {
1193 case -ETIMEDOUT:
1194 /* A soft timeout occurred. Use cached information? */
1195 if (server->flags & NFS_MOUNT_SOFTREVAL)
1196 status = 0;
1197 break;
1198 case -ESTALE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001199 if (!S_ISDIR(inode->i_mode))
Olivier Deprez157378f2022-04-04 15:47:50 +02001200 nfs_set_inode_stale(inode);
1201 else
1202 nfs_zap_caches(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001203 }
1204 goto err_out;
1205 }
1206
1207 status = nfs_refresh_inode(inode, fattr);
1208 if (status) {
1209 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
1210 inode->i_sb->s_id,
1211 (unsigned long long)NFS_FILEID(inode), status);
1212 goto err_out;
1213 }
1214
1215 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
1216 nfs_zap_acl_cache(inode);
1217
1218 nfs_setsecurity(inode, fattr, label);
1219
1220 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
1221 inode->i_sb->s_id,
1222 (unsigned long long)NFS_FILEID(inode));
1223
1224err_out:
1225 nfs4_label_free(label);
1226out:
1227 nfs_free_fattr(fattr);
1228 trace_nfs_revalidate_inode_exit(inode, status);
1229 return status;
1230}
1231
1232int nfs_attribute_cache_expired(struct inode *inode)
1233{
1234 if (nfs_have_delegated_attributes(inode))
1235 return 0;
1236 return nfs_attribute_timeout(inode);
1237}
1238
1239/**
1240 * nfs_revalidate_inode - Revalidate the inode attributes
David Brazdil0f672f62019-12-10 10:32:29 +00001241 * @server: pointer to nfs_server struct
1242 * @inode: pointer to inode struct
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243 *
1244 * Updates inode attribute information by retrieving the data from the server.
1245 */
1246int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1247{
1248 if (!nfs_need_revalidate_inode(inode))
1249 return NFS_STALE(inode) ? -ESTALE : 0;
1250 return __nfs_revalidate_inode(server, inode);
1251}
1252EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
1253
1254static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
1255{
1256 struct nfs_inode *nfsi = NFS_I(inode);
1257 int ret;
1258
1259 if (mapping->nrpages != 0) {
1260 if (S_ISREG(inode->i_mode)) {
1261 ret = nfs_sync_mapping(mapping);
1262 if (ret < 0)
1263 return ret;
1264 }
1265 ret = invalidate_inode_pages2(mapping);
1266 if (ret < 0)
1267 return ret;
1268 }
1269 if (S_ISDIR(inode->i_mode)) {
1270 spin_lock(&inode->i_lock);
1271 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
1272 spin_unlock(&inode->i_lock);
1273 }
1274 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
1275 nfs_fscache_wait_on_invalidate(inode);
1276
1277 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1278 inode->i_sb->s_id,
1279 (unsigned long long)NFS_FILEID(inode));
1280 return 0;
1281}
1282
1283bool nfs_mapping_need_revalidate_inode(struct inode *inode)
1284{
1285 return nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE) ||
1286 NFS_STALE(inode);
1287}
1288
1289int nfs_revalidate_mapping_rcu(struct inode *inode)
1290{
1291 struct nfs_inode *nfsi = NFS_I(inode);
1292 unsigned long *bitlock = &nfsi->flags;
1293 int ret = 0;
1294
1295 if (IS_SWAPFILE(inode))
1296 goto out;
1297 if (nfs_mapping_need_revalidate_inode(inode)) {
1298 ret = -ECHILD;
1299 goto out;
1300 }
1301 spin_lock(&inode->i_lock);
1302 if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1303 (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1304 ret = -ECHILD;
1305 spin_unlock(&inode->i_lock);
1306out:
1307 return ret;
1308}
1309
1310/**
1311 * nfs_revalidate_mapping - Revalidate the pagecache
David Brazdil0f672f62019-12-10 10:32:29 +00001312 * @inode: pointer to host inode
1313 * @mapping: pointer to mapping
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001314 */
1315int nfs_revalidate_mapping(struct inode *inode,
1316 struct address_space *mapping)
1317{
1318 struct nfs_inode *nfsi = NFS_I(inode);
1319 unsigned long *bitlock = &nfsi->flags;
1320 int ret = 0;
1321
1322 /* swapfiles are not supposed to be shared. */
1323 if (IS_SWAPFILE(inode))
1324 goto out;
1325
1326 if (nfs_mapping_need_revalidate_inode(inode)) {
1327 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1328 if (ret < 0)
1329 goto out;
1330 }
1331
1332 /*
1333 * We must clear NFS_INO_INVALID_DATA first to ensure that
1334 * invalidations that come in while we're shooting down the mappings
1335 * are respected. But, that leaves a race window where one revalidator
1336 * can clear the flag, and then another checks it before the mapping
1337 * gets invalidated. Fix that by serializing access to this part of
1338 * the function.
1339 *
1340 * At the same time, we need to allow other tasks to see whether we
1341 * might be in the middle of invalidating the pages, so we only set
1342 * the bit lock here if it looks like we're going to be doing that.
1343 */
1344 for (;;) {
1345 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1346 nfs_wait_bit_killable, TASK_KILLABLE);
1347 if (ret)
1348 goto out;
1349 spin_lock(&inode->i_lock);
1350 if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1351 spin_unlock(&inode->i_lock);
1352 continue;
1353 }
1354 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1355 break;
1356 spin_unlock(&inode->i_lock);
1357 goto out;
1358 }
1359
1360 set_bit(NFS_INO_INVALIDATING, bitlock);
1361 smp_wmb();
David Brazdil0f672f62019-12-10 10:32:29 +00001362 nfsi->cache_validity &= ~(NFS_INO_INVALID_DATA|
1363 NFS_INO_DATA_INVAL_DEFER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001364 spin_unlock(&inode->i_lock);
1365 trace_nfs_invalidate_mapping_enter(inode);
1366 ret = nfs_invalidate_mapping(inode, mapping);
1367 trace_nfs_invalidate_mapping_exit(inode, ret);
1368
1369 clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
1370 smp_mb__after_atomic();
1371 wake_up_bit(bitlock, NFS_INO_INVALIDATING);
1372out:
1373 return ret;
1374}
1375
1376static bool nfs_file_has_writers(struct nfs_inode *nfsi)
1377{
1378 struct inode *inode = &nfsi->vfs_inode;
1379
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001380 if (!S_ISREG(inode->i_mode))
1381 return false;
1382 if (list_empty(&nfsi->open_files))
1383 return false;
David Brazdil0f672f62019-12-10 10:32:29 +00001384 return inode_is_open_for_write(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001385}
1386
1387static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
1388{
1389 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
1390}
1391
1392static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1393{
Olivier Deprez157378f2022-04-04 15:47:50 +02001394 struct timespec64 ts;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001395
1396 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1397 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
1398 && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
1399 inode_set_iversion_raw(inode, fattr->change_attr);
1400 if (S_ISDIR(inode->i_mode))
1401 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Olivier Deprez157378f2022-04-04 15:47:50 +02001402 else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1403 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001404 }
1405 /* If we have atomic WCC data, we may update some attributes */
Olivier Deprez157378f2022-04-04 15:47:50 +02001406 ts = inode->i_ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001407 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1408 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +02001409 && timespec64_equal(&ts, &fattr->pre_ctime)) {
1410 inode->i_ctime = fattr->ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001411 }
1412
Olivier Deprez157378f2022-04-04 15:47:50 +02001413 ts = inode->i_mtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001414 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1415 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
Olivier Deprez157378f2022-04-04 15:47:50 +02001416 && timespec64_equal(&ts, &fattr->pre_mtime)) {
1417 inode->i_mtime = fattr->mtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418 if (S_ISDIR(inode->i_mode))
1419 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1420 }
1421 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1422 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
1423 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
1424 && !nfs_have_writebacks(inode)) {
1425 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
1426 }
1427}
1428
1429/**
1430 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
David Brazdil0f672f62019-12-10 10:32:29 +00001431 * @inode: pointer to inode
1432 * @fattr: updated attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433 *
1434 * Verifies the attribute cache. If we have just changed the attributes,
1435 * so that fattr carries weak cache consistency data, then it may
1436 * also update the ctime/mtime/change_attribute.
1437 */
1438static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1439{
1440 struct nfs_inode *nfsi = NFS_I(inode);
1441 loff_t cur_size, new_isize;
1442 unsigned long invalid = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001443 struct timespec64 ts;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001444
1445 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1446 return 0;
1447
David Brazdil0f672f62019-12-10 10:32:29 +00001448 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1449 /* Only a mounted-on-fileid? Just exit */
1450 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1451 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001452 /* Has the inode gone and changed behind our back? */
David Brazdil0f672f62019-12-10 10:32:29 +00001453 } else if (nfsi->fileid != fattr->fileid) {
1454 /* Is this perhaps the mounted-on fileid? */
1455 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1456 nfsi->fileid == fattr->mounted_on_fileid)
1457 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001458 return -ESTALE;
David Brazdil0f672f62019-12-10 10:32:29 +00001459 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001460 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001461 return -ESTALE;
1462
David Brazdil0f672f62019-12-10 10:32:29 +00001463
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001464 if (!nfs_file_has_buffered_writers(nfsi)) {
1465 /* Verify a few of the more important attributes */
1466 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
1467 invalid |= NFS_INO_INVALID_CHANGE
1468 | NFS_INO_REVAL_PAGECACHE;
1469
Olivier Deprez157378f2022-04-04 15:47:50 +02001470 ts = inode->i_mtime;
1471 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001472 invalid |= NFS_INO_INVALID_MTIME;
1473
Olivier Deprez157378f2022-04-04 15:47:50 +02001474 ts = inode->i_ctime;
1475 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001476 invalid |= NFS_INO_INVALID_CTIME;
1477
1478 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1479 cur_size = i_size_read(inode);
1480 new_isize = nfs_size_to_loff_t(fattr->size);
1481 if (cur_size != new_isize)
1482 invalid |= NFS_INO_INVALID_SIZE
1483 | NFS_INO_REVAL_PAGECACHE;
1484 }
1485 }
1486
1487 /* Have any file permissions changed? */
1488 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
1489 invalid |= NFS_INO_INVALID_ACCESS
1490 | NFS_INO_INVALID_ACL
1491 | NFS_INO_INVALID_OTHER;
1492 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
1493 invalid |= NFS_INO_INVALID_ACCESS
1494 | NFS_INO_INVALID_ACL
1495 | NFS_INO_INVALID_OTHER;
1496 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
1497 invalid |= NFS_INO_INVALID_ACCESS
1498 | NFS_INO_INVALID_ACL
1499 | NFS_INO_INVALID_OTHER;
1500
1501 /* Has the link count changed? */
1502 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
1503 invalid |= NFS_INO_INVALID_OTHER;
1504
Olivier Deprez157378f2022-04-04 15:47:50 +02001505 ts = inode->i_atime;
1506 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001507 invalid |= NFS_INO_INVALID_ATIME;
1508
1509 if (invalid != 0)
1510 nfs_set_cache_invalid(inode, invalid);
1511
1512 nfsi->read_cache_jiffies = fattr->time_start;
1513 return 0;
1514}
1515
1516static atomic_long_t nfs_attr_generation_counter;
1517
1518static unsigned long nfs_read_attr_generation_counter(void)
1519{
1520 return atomic_long_read(&nfs_attr_generation_counter);
1521}
1522
1523unsigned long nfs_inc_attr_generation_counter(void)
1524{
1525 return atomic_long_inc_return(&nfs_attr_generation_counter);
1526}
1527EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
1528
1529void nfs_fattr_init(struct nfs_fattr *fattr)
1530{
1531 fattr->valid = 0;
1532 fattr->time_start = jiffies;
1533 fattr->gencount = nfs_inc_attr_generation_counter();
1534 fattr->owner_name = NULL;
1535 fattr->group_name = NULL;
1536}
1537EXPORT_SYMBOL_GPL(nfs_fattr_init);
1538
1539/**
1540 * nfs_fattr_set_barrier
1541 * @fattr: attributes
1542 *
1543 * Used to set a barrier after an attribute was updated. This
1544 * barrier ensures that older attributes from RPC calls that may
1545 * have raced with our update cannot clobber these new values.
1546 * Note that you are still responsible for ensuring that other
1547 * operations which change the attribute on the server do not
1548 * collide.
1549 */
1550void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1551{
1552 fattr->gencount = nfs_inc_attr_generation_counter();
1553}
1554
1555struct nfs_fattr *nfs_alloc_fattr(void)
1556{
1557 struct nfs_fattr *fattr;
1558
1559 fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
1560 if (fattr != NULL)
1561 nfs_fattr_init(fattr);
1562 return fattr;
1563}
1564EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
1565
1566struct nfs_fh *nfs_alloc_fhandle(void)
1567{
1568 struct nfs_fh *fh;
1569
1570 fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
1571 if (fh != NULL)
1572 fh->size = 0;
1573 return fh;
1574}
1575EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
1576
1577#ifdef NFS_DEBUG
1578/*
1579 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1580 * in the same way that wireshark does
1581 *
1582 * @fh: file handle
1583 *
1584 * For debugging only.
1585 */
1586u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1587{
1588 /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1589 * not on the result */
1590 return nfs_fhandle_hash(fh);
1591}
1592EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
1593
1594/*
1595 * _nfs_display_fhandle - display an NFS file handle on the console
1596 *
1597 * @fh: file handle to display
1598 * @caption: display caption
1599 *
1600 * For debugging only.
1601 */
1602void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1603{
1604 unsigned short i;
1605
1606 if (fh == NULL || fh->size == 0) {
1607 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1608 return;
1609 }
1610
1611 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1612 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
1613 for (i = 0; i < fh->size; i += 16) {
1614 __be32 *pos = (__be32 *)&fh->data[i];
1615
1616 switch ((fh->size - i - 1) >> 2) {
1617 case 0:
1618 printk(KERN_DEFAULT " %08x\n",
1619 be32_to_cpup(pos));
1620 break;
1621 case 1:
1622 printk(KERN_DEFAULT " %08x %08x\n",
1623 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1624 break;
1625 case 2:
1626 printk(KERN_DEFAULT " %08x %08x %08x\n",
1627 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1628 be32_to_cpup(pos + 2));
1629 break;
1630 default:
1631 printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1632 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1633 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1634 }
1635 }
1636}
1637EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
1638#endif
1639
1640/**
1641 * nfs_inode_attrs_need_update - check if the inode attributes need updating
David Brazdil0f672f62019-12-10 10:32:29 +00001642 * @inode: pointer to inode
1643 * @fattr: attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001644 *
1645 * Attempt to divine whether or not an RPC call reply carrying stale
1646 * attributes got scheduled after another call carrying updated ones.
1647 *
1648 * To do so, the function first assumes that a more recent ctime means
1649 * that the attributes in fattr are newer, however it also attempt to
1650 * catch the case where ctime either didn't change, or went backwards
1651 * (if someone reset the clock on the server) by looking at whether
1652 * or not this RPC call was started after the inode was last updated.
1653 * Note also the check for wraparound of 'attr_gencount'
1654 *
1655 * The function returns 'true' if it thinks the attributes in 'fattr' are
1656 * more recent than the ones cached in the inode.
1657 *
1658 */
1659static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1660{
Olivier Deprez0e641232021-09-23 10:07:05 +02001661 unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001662
Olivier Deprez0e641232021-09-23 10:07:05 +02001663 return (long)(fattr->gencount - attr_gencount) > 0 ||
1664 (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001665}
1666
1667static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1668{
1669 int ret;
1670
1671 trace_nfs_refresh_inode_enter(inode);
1672
1673 if (nfs_inode_attrs_need_update(inode, fattr))
1674 ret = nfs_update_inode(inode, fattr);
1675 else
1676 ret = nfs_check_inode_attributes(inode, fattr);
1677
1678 trace_nfs_refresh_inode_exit(inode, ret);
1679 return ret;
1680}
1681
1682/**
1683 * nfs_refresh_inode - try to update the inode attribute cache
David Brazdil0f672f62019-12-10 10:32:29 +00001684 * @inode: pointer to inode
1685 * @fattr: updated attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001686 *
1687 * Check that an RPC call that returned attributes has not overlapped with
1688 * other recent updates of the inode metadata, then decide whether it is
1689 * safe to do a full update of the inode attributes, or whether just to
1690 * call nfs_check_inode_attributes.
1691 */
1692int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1693{
1694 int status;
1695
1696 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1697 return 0;
1698 spin_lock(&inode->i_lock);
1699 status = nfs_refresh_inode_locked(inode, fattr);
1700 spin_unlock(&inode->i_lock);
1701
1702 return status;
1703}
1704EXPORT_SYMBOL_GPL(nfs_refresh_inode);
1705
1706static int nfs_post_op_update_inode_locked(struct inode *inode,
1707 struct nfs_fattr *fattr, unsigned int invalid)
1708{
1709 if (S_ISDIR(inode->i_mode))
1710 invalid |= NFS_INO_INVALID_DATA;
1711 nfs_set_cache_invalid(inode, invalid);
1712 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1713 return 0;
1714 return nfs_refresh_inode_locked(inode, fattr);
1715}
1716
1717/**
1718 * nfs_post_op_update_inode - try to update the inode attribute cache
David Brazdil0f672f62019-12-10 10:32:29 +00001719 * @inode: pointer to inode
1720 * @fattr: updated attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001721 *
1722 * After an operation that has changed the inode metadata, mark the
1723 * attribute cache as being invalid, then try to update it.
1724 *
1725 * NB: if the server didn't return any post op attributes, this
1726 * function will force the retrieval of attributes before the next
1727 * NFS request. Thus it should be used only for operations that
1728 * are expected to change one or more attributes, to avoid
1729 * unnecessary NFS requests and trips through nfs_update_inode().
1730 */
1731int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1732{
1733 int status;
1734
1735 spin_lock(&inode->i_lock);
1736 nfs_fattr_set_barrier(fattr);
1737 status = nfs_post_op_update_inode_locked(inode, fattr,
1738 NFS_INO_INVALID_CHANGE
1739 | NFS_INO_INVALID_CTIME
1740 | NFS_INO_REVAL_FORCED);
1741 spin_unlock(&inode->i_lock);
1742
1743 return status;
1744}
1745EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
1746
1747/**
1748 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
David Brazdil0f672f62019-12-10 10:32:29 +00001749 * @inode: pointer to inode
1750 * @fattr: updated attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001751 *
1752 * After an operation that has changed the inode metadata, mark the
1753 * attribute cache as being invalid, then try to update it. Fake up
1754 * weak cache consistency data, if none exist.
1755 *
1756 * This function is mainly designed to be used by the ->write_done() functions.
1757 */
1758int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
1759{
1760 int status;
1761
1762 /* Don't do a WCC update if these attributes are already stale */
1763 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1764 !nfs_inode_attrs_need_update(inode, fattr)) {
1765 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1766 | NFS_ATTR_FATTR_PRESIZE
1767 | NFS_ATTR_FATTR_PREMTIME
1768 | NFS_ATTR_FATTR_PRECTIME);
1769 goto out_noforce;
1770 }
1771 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1772 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
1773 fattr->pre_change_attr = inode_peek_iversion_raw(inode);
1774 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
1775 }
1776 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1777 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001778 fattr->pre_ctime = inode->i_ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001779 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1780 }
1781 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1782 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001783 fattr->pre_mtime = inode->i_mtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001784 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1785 }
1786 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1787 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
1788 fattr->pre_size = i_size_read(inode);
1789 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
1790 }
1791out_noforce:
1792 status = nfs_post_op_update_inode_locked(inode, fattr,
1793 NFS_INO_INVALID_CHANGE
1794 | NFS_INO_INVALID_CTIME
Olivier Deprez0e641232021-09-23 10:07:05 +02001795 | NFS_INO_INVALID_MTIME
1796 | NFS_INO_INVALID_BLOCKS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001797 return status;
1798}
1799
1800/**
1801 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
David Brazdil0f672f62019-12-10 10:32:29 +00001802 * @inode: pointer to inode
1803 * @fattr: updated attributes
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001804 *
1805 * After an operation that has changed the inode metadata, mark the
1806 * attribute cache as being invalid, then try to update it. Fake up
1807 * weak cache consistency data, if none exist.
1808 *
1809 * This function is mainly designed to be used by the ->write_done() functions.
1810 */
1811int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1812{
1813 int status;
1814
1815 spin_lock(&inode->i_lock);
1816 nfs_fattr_set_barrier(fattr);
1817 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1818 spin_unlock(&inode->i_lock);
1819 return status;
1820}
1821EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
1822
1823
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001824/*
1825 * Many nfs protocol calls return the new file attributes after
1826 * an operation. Here we update the inode to reflect the state
1827 * of the server's inode.
1828 *
1829 * This is a bit tricky because we have to make sure all dirty pages
1830 * have been sent off to the server before calling invalidate_inode_pages.
1831 * To make sure no other process adds more write requests while we try
1832 * our best to flush them, we make them sleep during the attribute refresh.
1833 *
1834 * A very similar scenario holds for the dir cache.
1835 */
1836static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1837{
1838 struct nfs_server *server;
1839 struct nfs_inode *nfsi = NFS_I(inode);
1840 loff_t cur_isize, new_isize;
1841 unsigned long invalid = 0;
1842 unsigned long now = jiffies;
1843 unsigned long save_cache_validity;
1844 bool have_writers = nfs_file_has_buffered_writers(nfsi);
1845 bool cache_revalidated = true;
1846 bool attr_changed = false;
1847 bool have_delegation;
1848
1849 dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
1850 __func__, inode->i_sb->s_id, inode->i_ino,
1851 nfs_display_fhandle_hash(NFS_FH(inode)),
1852 atomic_read(&inode->i_count), fattr->valid);
1853
David Brazdil0f672f62019-12-10 10:32:29 +00001854 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1855 /* Only a mounted-on-fileid? Just exit */
1856 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1857 return 0;
1858 /* Has the inode gone and changed behind our back? */
1859 } else if (nfsi->fileid != fattr->fileid) {
1860 /* Is this perhaps the mounted-on fileid? */
1861 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1862 nfsi->fileid == fattr->mounted_on_fileid)
1863 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1865 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1866 NFS_SERVER(inode)->nfs_client->cl_hostname,
1867 inode->i_sb->s_id, (long long)nfsi->fileid,
1868 (long long)fattr->fileid);
1869 goto out_err;
1870 }
1871
1872 /*
1873 * Make sure the inode's type hasn't changed.
1874 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001875 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001876 /*
1877 * Big trouble! The inode has become a different object.
1878 */
1879 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
1880 __func__, inode->i_ino, inode->i_mode, fattr->mode);
1881 goto out_err;
1882 }
1883
1884 server = NFS_SERVER(inode);
1885 /* Update the fsid? */
1886 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
1887 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1888 !IS_AUTOMOUNT(inode))
1889 server->fsid = fattr->fsid;
1890
1891 /* Save the delegation state before clearing cache_validity */
1892 have_delegation = nfs_have_delegated_attributes(inode);
1893
1894 /*
1895 * Update the read time so we don't revalidate too often.
1896 */
1897 nfsi->read_cache_jiffies = fattr->time_start;
1898
1899 save_cache_validity = nfsi->cache_validity;
1900 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1901 | NFS_INO_INVALID_ATIME
1902 | NFS_INO_REVAL_FORCED
Olivier Deprez0e641232021-09-23 10:07:05 +02001903 | NFS_INO_REVAL_PAGECACHE
1904 | NFS_INO_INVALID_BLOCKS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001905
1906 /* Do atomic weak cache consistency updates */
1907 nfs_wcc_update_inode(inode, fattr);
1908
1909 if (pnfs_layoutcommit_outstanding(inode)) {
1910 nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
1911 cache_revalidated = false;
1912 }
1913
1914 /* More cache consistency checks */
1915 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1916 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
1917 /* Could it be a race with writeback? */
1918 if (!(have_writers || have_delegation)) {
1919 invalid |= NFS_INO_INVALID_DATA
1920 | NFS_INO_INVALID_ACCESS
Olivier Deprez157378f2022-04-04 15:47:50 +02001921 | NFS_INO_INVALID_ACL
1922 | NFS_INO_INVALID_XATTR;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001923 /* Force revalidate of all attributes */
1924 save_cache_validity |= NFS_INO_INVALID_CTIME
1925 | NFS_INO_INVALID_MTIME
1926 | NFS_INO_INVALID_SIZE
1927 | NFS_INO_INVALID_OTHER;
1928 if (S_ISDIR(inode->i_mode))
1929 nfs_force_lookup_revalidate(inode);
1930 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1931 inode->i_sb->s_id,
1932 inode->i_ino);
David Brazdil0f672f62019-12-10 10:32:29 +00001933 } else if (!have_delegation)
1934 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001935 inode_set_iversion_raw(inode, fattr->change_attr);
1936 attr_changed = true;
1937 }
1938 } else {
1939 nfsi->cache_validity |= save_cache_validity &
1940 (NFS_INO_INVALID_CHANGE
1941 | NFS_INO_REVAL_PAGECACHE
1942 | NFS_INO_REVAL_FORCED);
1943 cache_revalidated = false;
1944 }
1945
1946 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001947 inode->i_mtime = fattr->mtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001948 } else if (server->caps & NFS_CAP_MTIME) {
1949 nfsi->cache_validity |= save_cache_validity &
1950 (NFS_INO_INVALID_MTIME
1951 | NFS_INO_REVAL_FORCED);
1952 cache_revalidated = false;
1953 }
1954
1955 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001956 inode->i_ctime = fattr->ctime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001957 } else if (server->caps & NFS_CAP_CTIME) {
1958 nfsi->cache_validity |= save_cache_validity &
1959 (NFS_INO_INVALID_CTIME
1960 | NFS_INO_REVAL_FORCED);
1961 cache_revalidated = false;
1962 }
1963
1964 /* Check if our cached file size is stale */
1965 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1966 new_isize = nfs_size_to_loff_t(fattr->size);
1967 cur_isize = i_size_read(inode);
1968 if (new_isize != cur_isize && !have_delegation) {
1969 /* Do we perhaps have any outstanding writes, or has
1970 * the file grown beyond our last write? */
1971 if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
1972 i_size_write(inode, new_isize);
1973 if (!have_writers)
1974 invalid |= NFS_INO_INVALID_DATA;
1975 attr_changed = true;
1976 }
1977 dprintk("NFS: isize change on server for file %s/%ld "
1978 "(%Ld to %Ld)\n",
1979 inode->i_sb->s_id,
1980 inode->i_ino,
1981 (long long)cur_isize,
1982 (long long)new_isize);
1983 }
1984 } else {
1985 nfsi->cache_validity |= save_cache_validity &
1986 (NFS_INO_INVALID_SIZE
1987 | NFS_INO_REVAL_PAGECACHE
1988 | NFS_INO_REVAL_FORCED);
1989 cache_revalidated = false;
1990 }
1991
1992
1993 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Olivier Deprez157378f2022-04-04 15:47:50 +02001994 inode->i_atime = fattr->atime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001995 else if (server->caps & NFS_CAP_ATIME) {
1996 nfsi->cache_validity |= save_cache_validity &
1997 (NFS_INO_INVALID_ATIME
1998 | NFS_INO_REVAL_FORCED);
1999 cache_revalidated = false;
2000 }
2001
2002 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
2003 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
2004 umode_t newmode = inode->i_mode & S_IFMT;
2005 newmode |= fattr->mode & S_IALLUGO;
2006 inode->i_mode = newmode;
2007 invalid |= NFS_INO_INVALID_ACCESS
2008 | NFS_INO_INVALID_ACL;
2009 attr_changed = true;
2010 }
2011 } else if (server->caps & NFS_CAP_MODE) {
2012 nfsi->cache_validity |= save_cache_validity &
2013 (NFS_INO_INVALID_OTHER
2014 | NFS_INO_REVAL_FORCED);
2015 cache_revalidated = false;
2016 }
2017
2018 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
2019 if (!uid_eq(inode->i_uid, fattr->uid)) {
2020 invalid |= NFS_INO_INVALID_ACCESS
2021 | NFS_INO_INVALID_ACL;
2022 inode->i_uid = fattr->uid;
2023 attr_changed = true;
2024 }
2025 } else if (server->caps & NFS_CAP_OWNER) {
2026 nfsi->cache_validity |= save_cache_validity &
2027 (NFS_INO_INVALID_OTHER
2028 | NFS_INO_REVAL_FORCED);
2029 cache_revalidated = false;
2030 }
2031
2032 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
2033 if (!gid_eq(inode->i_gid, fattr->gid)) {
2034 invalid |= NFS_INO_INVALID_ACCESS
2035 | NFS_INO_INVALID_ACL;
2036 inode->i_gid = fattr->gid;
2037 attr_changed = true;
2038 }
2039 } else if (server->caps & NFS_CAP_OWNER_GROUP) {
2040 nfsi->cache_validity |= save_cache_validity &
2041 (NFS_INO_INVALID_OTHER
2042 | NFS_INO_REVAL_FORCED);
2043 cache_revalidated = false;
2044 }
2045
2046 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2047 if (inode->i_nlink != fattr->nlink) {
2048 if (S_ISDIR(inode->i_mode))
2049 invalid |= NFS_INO_INVALID_DATA;
2050 set_nlink(inode, fattr->nlink);
2051 attr_changed = true;
2052 }
2053 } else if (server->caps & NFS_CAP_NLINK) {
2054 nfsi->cache_validity |= save_cache_validity &
2055 (NFS_INO_INVALID_OTHER
2056 | NFS_INO_REVAL_FORCED);
2057 cache_revalidated = false;
2058 }
2059
2060 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
2061 /*
2062 * report the blocks in 512byte units
2063 */
2064 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
2065 } else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
2066 inode->i_blocks = fattr->du.nfs2.blocks;
Olivier Deprez0e641232021-09-23 10:07:05 +02002067 else {
2068 nfsi->cache_validity |= save_cache_validity &
2069 (NFS_INO_INVALID_BLOCKS
2070 | NFS_INO_REVAL_FORCED);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002071 cache_revalidated = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02002072 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002073
2074 /* Update attrtimeo value if we're out of the unstable period */
2075 if (attr_changed) {
2076 invalid &= ~NFS_INO_INVALID_ATTR;
2077 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
2078 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
2079 nfsi->attrtimeo_timestamp = now;
2080 /* Set barrier to be more recent than all outstanding updates */
2081 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
2082 } else {
2083 if (cache_revalidated) {
2084 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2085 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2086 nfsi->attrtimeo <<= 1;
2087 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2088 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2089 }
2090 nfsi->attrtimeo_timestamp = now;
2091 }
2092 /* Set the barrier to be more recent than this fattr */
Olivier Deprez0e641232021-09-23 10:07:05 +02002093 if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002094 nfsi->attr_gencount = fattr->gencount;
2095 }
2096
2097 /* Don't invalidate the data if we were to blame */
2098 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2099 || S_ISLNK(inode->i_mode)))
2100 invalid &= ~NFS_INO_INVALID_DATA;
2101 nfs_set_cache_invalid(inode, invalid);
2102
2103 return 0;
2104 out_err:
2105 /*
2106 * No need to worry about unhashing the dentry, as the
2107 * lookup validation will know that the inode is bad.
2108 * (But we fall through to invalidate the caches.)
2109 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002110 nfs_set_inode_stale_locked(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002111 return -ESTALE;
2112}
2113
2114struct inode *nfs_alloc_inode(struct super_block *sb)
2115{
2116 struct nfs_inode *nfsi;
2117 nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
2118 if (!nfsi)
2119 return NULL;
2120 nfsi->flags = 0UL;
2121 nfsi->cache_validity = 0UL;
2122#if IS_ENABLED(CONFIG_NFS_V4)
2123 nfsi->nfs4_acl = NULL;
2124#endif /* CONFIG_NFS_V4 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002125#ifdef CONFIG_NFS_V4_2
2126 nfsi->xattr_cache = NULL;
2127#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002128 return &nfsi->vfs_inode;
2129}
2130EXPORT_SYMBOL_GPL(nfs_alloc_inode);
2131
David Brazdil0f672f62019-12-10 10:32:29 +00002132void nfs_free_inode(struct inode *inode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002133{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002134 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2135}
David Brazdil0f672f62019-12-10 10:32:29 +00002136EXPORT_SYMBOL_GPL(nfs_free_inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002137
2138static inline void nfs4_init_once(struct nfs_inode *nfsi)
2139{
2140#if IS_ENABLED(CONFIG_NFS_V4)
2141 INIT_LIST_HEAD(&nfsi->open_states);
2142 nfsi->delegation = NULL;
2143 init_rwsem(&nfsi->rwsem);
2144 nfsi->layout = NULL;
2145#endif
2146}
2147
2148static void init_once(void *foo)
2149{
2150 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2151
2152 inode_init_once(&nfsi->vfs_inode);
2153 INIT_LIST_HEAD(&nfsi->open_files);
2154 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2155 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
2156 INIT_LIST_HEAD(&nfsi->commit_info.list);
2157 atomic_long_set(&nfsi->nrequests, 0);
2158 atomic_long_set(&nfsi->commit_info.ncommit, 0);
2159 atomic_set(&nfsi->commit_info.rpcs_out, 0);
2160 init_rwsem(&nfsi->rmdir_sem);
2161 mutex_init(&nfsi->commit_mutex);
2162 nfs4_init_once(nfsi);
Olivier Deprez157378f2022-04-04 15:47:50 +02002163 nfsi->cache_change_attribute = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002164}
2165
2166static int __init nfs_init_inodecache(void)
2167{
2168 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2169 sizeof(struct nfs_inode),
2170 0, (SLAB_RECLAIM_ACCOUNT|
2171 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2172 init_once);
2173 if (nfs_inode_cachep == NULL)
2174 return -ENOMEM;
2175
2176 return 0;
2177}
2178
2179static void nfs_destroy_inodecache(void)
2180{
2181 /*
2182 * Make sure all delayed rcu free inodes are flushed before we
2183 * destroy cache.
2184 */
2185 rcu_barrier();
2186 kmem_cache_destroy(nfs_inode_cachep);
2187}
2188
2189struct workqueue_struct *nfsiod_workqueue;
2190EXPORT_SYMBOL_GPL(nfsiod_workqueue);
2191
2192/*
2193 * start up the nfsiod workqueue
2194 */
2195static int nfsiod_start(void)
2196{
2197 struct workqueue_struct *wq;
2198 dprintk("RPC: creating workqueue nfsiod\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02002199 wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002200 if (wq == NULL)
2201 return -ENOMEM;
2202 nfsiod_workqueue = wq;
2203 return 0;
2204}
2205
2206/*
2207 * Destroy the nfsiod workqueue
2208 */
2209static void nfsiod_stop(void)
2210{
2211 struct workqueue_struct *wq;
2212
2213 wq = nfsiod_workqueue;
2214 if (wq == NULL)
2215 return;
2216 nfsiod_workqueue = NULL;
2217 destroy_workqueue(wq);
2218}
2219
2220unsigned int nfs_net_id;
2221EXPORT_SYMBOL_GPL(nfs_net_id);
2222
2223static int nfs_net_init(struct net *net)
2224{
2225 nfs_clients_init(net);
2226 return nfs_fs_proc_net_init(net);
2227}
2228
2229static void nfs_net_exit(struct net *net)
2230{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002231 nfs_fs_proc_net_exit(net);
David Brazdil0f672f62019-12-10 10:32:29 +00002232 nfs_clients_exit(net);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002233}
2234
2235static struct pernet_operations nfs_net_ops = {
2236 .init = nfs_net_init,
2237 .exit = nfs_net_exit,
2238 .id = &nfs_net_id,
2239 .size = sizeof(struct nfs_net),
2240};
2241
2242/*
2243 * Initialize NFS
2244 */
2245static int __init init_nfs_fs(void)
2246{
2247 int err;
2248
David Brazdil0f672f62019-12-10 10:32:29 +00002249 err = nfs_sysfs_init();
2250 if (err < 0)
2251 goto out10;
2252
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002253 err = register_pernet_subsys(&nfs_net_ops);
2254 if (err < 0)
2255 goto out9;
2256
2257 err = nfs_fscache_register();
2258 if (err < 0)
2259 goto out8;
2260
2261 err = nfsiod_start();
2262 if (err)
2263 goto out7;
2264
2265 err = nfs_fs_proc_init();
2266 if (err)
2267 goto out6;
2268
2269 err = nfs_init_nfspagecache();
2270 if (err)
2271 goto out5;
2272
2273 err = nfs_init_inodecache();
2274 if (err)
2275 goto out4;
2276
2277 err = nfs_init_readpagecache();
2278 if (err)
2279 goto out3;
2280
2281 err = nfs_init_writepagecache();
2282 if (err)
2283 goto out2;
2284
2285 err = nfs_init_directcache();
2286 if (err)
2287 goto out1;
2288
2289 rpc_proc_register(&init_net, &nfs_rpcstat);
2290
2291 err = register_nfs_fs();
2292 if (err)
2293 goto out0;
2294
2295 return 0;
2296out0:
2297 rpc_proc_unregister(&init_net, "nfs");
2298 nfs_destroy_directcache();
2299out1:
2300 nfs_destroy_writepagecache();
2301out2:
2302 nfs_destroy_readpagecache();
2303out3:
2304 nfs_destroy_inodecache();
2305out4:
2306 nfs_destroy_nfspagecache();
2307out5:
2308 nfs_fs_proc_exit();
2309out6:
2310 nfsiod_stop();
2311out7:
2312 nfs_fscache_unregister();
2313out8:
2314 unregister_pernet_subsys(&nfs_net_ops);
2315out9:
David Brazdil0f672f62019-12-10 10:32:29 +00002316 nfs_sysfs_exit();
2317out10:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002318 return err;
2319}
2320
2321static void __exit exit_nfs_fs(void)
2322{
2323 nfs_destroy_directcache();
2324 nfs_destroy_writepagecache();
2325 nfs_destroy_readpagecache();
2326 nfs_destroy_inodecache();
2327 nfs_destroy_nfspagecache();
2328 nfs_fscache_unregister();
2329 unregister_pernet_subsys(&nfs_net_ops);
2330 rpc_proc_unregister(&init_net, "nfs");
2331 unregister_nfs_fs();
2332 nfs_fs_proc_exit();
2333 nfsiod_stop();
David Brazdil0f672f62019-12-10 10:32:29 +00002334 nfs_sysfs_exit();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002335}
2336
2337/* Not quite true; I just maintain it */
2338MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2339MODULE_LICENSE("GPL");
2340module_param(enable_ino64, bool, 0644);
2341
2342module_init(init_nfs_fs)
2343module_exit(exit_nfs_fs)