GNU Linux-libre 5.15.137-gnu
[releases.git] / fs / nfs / inode.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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"
54 #include "sysfs.h"
55
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 */
63 static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
64
65 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
66
67 static struct kmem_cache * nfs_inode_cachep;
68
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
75 static 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
83 int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
84 {
85         return nfs_wait_killable(mode);
86 }
87 EXPORT_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  */
96 u64 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
112 int nfs_drop_inode(struct inode *inode)
113 {
114         return NFS_STALE(inode) || generic_drop_inode(inode);
115 }
116 EXPORT_SYMBOL_GPL(nfs_drop_inode);
117
118 void 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 }
129 EXPORT_SYMBOL_GPL(nfs_clear_inode);
130
131 void 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
138 int nfs_sync_inode(struct inode *inode)
139 {
140         inode_dio_wait(inode);
141         return nfs_wb_all(inode);
142 }
143 EXPORT_SYMBOL_GPL(nfs_sync_inode);
144
145 /**
146  * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
147  * @mapping: pointer to struct address_space
148  */
149 int 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
160 static 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
167 static bool nfs_check_cache_flags_invalid(struct inode *inode,
168                                           unsigned long flags)
169 {
170         unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
171
172         return (cache_validity & flags) != 0;
173 }
174
175 bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
176 {
177         if (nfs_check_cache_flags_invalid(inode, flags))
178                 return true;
179         return nfs_attribute_cache_expired(inode);
180 }
181 EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
182
183 #ifdef CONFIG_NFS_V4_2
184 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
185 {
186         return nfsi->xattr_cache != NULL;
187 }
188 #else
189 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
190 {
191         return false;
192 }
193 #endif
194
195 void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
196 {
197         struct nfs_inode *nfsi = NFS_I(inode);
198         bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
199
200         if (have_delegation) {
201                 if (!(flags & NFS_INO_REVAL_FORCED))
202                         flags &= ~(NFS_INO_INVALID_MODE |
203                                    NFS_INO_INVALID_OTHER |
204                                    NFS_INO_INVALID_XATTR);
205                 flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
206         } else if (flags & NFS_INO_REVAL_PAGECACHE)
207                 flags |= NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE;
208
209         if (!nfs_has_xattr_cache(nfsi))
210                 flags &= ~NFS_INO_INVALID_XATTR;
211         if (flags & NFS_INO_INVALID_DATA)
212                 nfs_fscache_invalidate(inode);
213         flags &= ~(NFS_INO_REVAL_PAGECACHE | NFS_INO_REVAL_FORCED);
214
215         nfsi->cache_validity |= flags;
216
217         if (inode->i_mapping->nrpages == 0)
218                 nfsi->cache_validity &= ~(NFS_INO_INVALID_DATA |
219                                           NFS_INO_DATA_INVAL_DEFER);
220         else if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
221                 nfsi->cache_validity &= ~NFS_INO_DATA_INVAL_DEFER;
222 }
223 EXPORT_SYMBOL_GPL(nfs_set_cache_invalid);
224
225 /*
226  * Invalidate the local caches
227  */
228 static void nfs_zap_caches_locked(struct inode *inode)
229 {
230         struct nfs_inode *nfsi = NFS_I(inode);
231         int mode = inode->i_mode;
232
233         nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
234
235         nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
236         nfsi->attrtimeo_timestamp = jiffies;
237
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                                         | NFS_INO_REVAL_PAGECACHE);
245         } else
246                 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
247                                         | NFS_INO_INVALID_ACCESS
248                                         | NFS_INO_INVALID_ACL
249                                         | NFS_INO_INVALID_XATTR
250                                         | NFS_INO_REVAL_PAGECACHE);
251         nfs_zap_label_cache_locked(nfsi);
252 }
253
254 void nfs_zap_caches(struct inode *inode)
255 {
256         spin_lock(&inode->i_lock);
257         nfs_zap_caches_locked(inode);
258         spin_unlock(&inode->i_lock);
259 }
260
261 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
262 {
263         if (mapping->nrpages != 0) {
264                 spin_lock(&inode->i_lock);
265                 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
266                 spin_unlock(&inode->i_lock);
267         }
268 }
269
270 void nfs_zap_acl_cache(struct inode *inode)
271 {
272         void (*clear_acl_cache)(struct inode *);
273
274         clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
275         if (clear_acl_cache != NULL)
276                 clear_acl_cache(inode);
277         spin_lock(&inode->i_lock);
278         NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
279         spin_unlock(&inode->i_lock);
280 }
281 EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
282
283 void nfs_invalidate_atime(struct inode *inode)
284 {
285         spin_lock(&inode->i_lock);
286         nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
287         spin_unlock(&inode->i_lock);
288 }
289 EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
290
291 /*
292  * Invalidate, but do not unhash, the inode.
293  * NB: must be called with inode->i_lock held!
294  */
295 static void nfs_set_inode_stale_locked(struct inode *inode)
296 {
297         set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
298         nfs_zap_caches_locked(inode);
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);
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 {
323         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
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;
329         if (inode_wrong_type(inode, fattr->mode))
330                 return 0;
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 {
341         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
342         struct nfs_fattr        *fattr = desc->fattr;
343
344         set_nfs_fileid(inode, fattr->fileid);
345         inode->i_mode = fattr->mode;
346         nfs_copy_fh(NFS_FH(inode), desc->fh);
347         return 0;
348 }
349
350 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
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
358 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
359                                         struct nfs4_label *label)
360 {
361         int error;
362
363         if (label == NULL)
364                 return;
365
366         if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
367                 error = security_inode_notifysecctx(inode, label->label,
368                                 label->len);
369                 if (error)
370                         printk(KERN_ERR "%s() %s %d "
371                                         "security_inode_notifysecctx() %d\n",
372                                         __func__,
373                                         (char *)label->label,
374                                         label->len, error);
375                 nfs_clear_label_invalid(inode);
376         }
377 }
378
379 struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
380 {
381         struct nfs4_label *label = NULL;
382         int minor_version = server->nfs_client->cl_minorversion;
383
384         if (minor_version < 2)
385                 return label;
386
387         if (!(server->caps & NFS_CAP_SECURITY_LABEL))
388                 return label;
389
390         label = kzalloc(sizeof(struct nfs4_label), flags);
391         if (label == NULL)
392                 return ERR_PTR(-ENOMEM);
393
394         label->label = kzalloc(NFS4_MAXLABELLEN, flags);
395         if (label->label == NULL) {
396                 kfree(label);
397                 return ERR_PTR(-ENOMEM);
398         }
399         label->len = NFS4_MAXLABELLEN;
400
401         return label;
402 }
403 EXPORT_SYMBOL_GPL(nfs4_label_alloc);
404 #else
405 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
406                                         struct nfs4_label *label)
407 {
408 }
409 #endif
410 EXPORT_SYMBOL_GPL(nfs_setsecurity);
411
412 /* Search for inode identified by fh, fileid and i_mode in inode cache. */
413 struct inode *
414 nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
415 {
416         struct nfs_find_desc desc = {
417                 .fh     = fh,
418                 .fattr  = fattr,
419         };
420         struct inode *inode;
421         unsigned long hash;
422
423         if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
424             !(fattr->valid & NFS_ATTR_FATTR_TYPE))
425                 return NULL;
426
427         hash = nfs_fattr_to_ino_t(fattr);
428         inode = ilookup5(sb, hash, nfs_find_actor, &desc);
429
430         dprintk("%s: returning %p\n", __func__, inode);
431         return inode;
432 }
433
434 static void nfs_inode_init_regular(struct nfs_inode *nfsi)
435 {
436         atomic_long_set(&nfsi->nrequests, 0);
437         atomic_long_set(&nfsi->redirtied_pages, 0);
438         INIT_LIST_HEAD(&nfsi->commit_info.list);
439         atomic_long_set(&nfsi->commit_info.ncommit, 0);
440         atomic_set(&nfsi->commit_info.rpcs_out, 0);
441         mutex_init(&nfsi->commit_mutex);
442 }
443
444 static void nfs_inode_init_dir(struct nfs_inode *nfsi)
445 {
446         nfsi->cache_change_attribute = 0;
447         memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
448         init_rwsem(&nfsi->rmdir_sem);
449 }
450
451 /*
452  * This is our front-end to iget that looks up inodes by file handle
453  * instead of inode number.
454  */
455 struct inode *
456 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
457 {
458         struct nfs_find_desc desc = {
459                 .fh     = fh,
460                 .fattr  = fattr
461         };
462         struct inode *inode = ERR_PTR(-ENOENT);
463         u64 fattr_supported = NFS_SB(sb)->fattr_valid;
464         unsigned long hash;
465
466         nfs_attr_check_mountpoint(sb, fattr);
467
468         if (nfs_attr_use_mounted_on_fileid(fattr))
469                 fattr->fileid = fattr->mounted_on_fileid;
470         else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
471                 goto out_no_inode;
472         if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
473                 goto out_no_inode;
474
475         hash = nfs_fattr_to_ino_t(fattr);
476
477         inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
478         if (inode == NULL) {
479                 inode = ERR_PTR(-ENOMEM);
480                 goto out_no_inode;
481         }
482
483         if (inode->i_state & I_NEW) {
484                 struct nfs_inode *nfsi = NFS_I(inode);
485                 unsigned long now = jiffies;
486
487                 /* We set i_ino for the few things that still rely on it,
488                  * such as stat(2) */
489                 inode->i_ino = hash;
490
491                 /* We can't support update_atime(), since the server will reset it */
492                 inode->i_flags |= S_NOATIME|S_NOCMTIME;
493                 inode->i_mode = fattr->mode;
494                 nfsi->cache_validity = 0;
495                 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
496                                 && (fattr_supported & NFS_ATTR_FATTR_MODE))
497                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
498                 /* Why so? Because we want revalidate for devices/FIFOs, and
499                  * that's precisely what we have in nfs_file_inode_operations.
500                  */
501                 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
502                 if (S_ISREG(inode->i_mode)) {
503                         inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
504                         inode->i_data.a_ops = &nfs_file_aops;
505                         nfs_inode_init_regular(nfsi);
506                 } else if (S_ISDIR(inode->i_mode)) {
507                         inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
508                         inode->i_fop = &nfs_dir_operations;
509                         inode->i_data.a_ops = &nfs_dir_aops;
510                         nfs_inode_init_dir(nfsi);
511                         /* Deal with crossing mountpoints */
512                         if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
513                                         fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
514                                 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
515                                         inode->i_op = &nfs_referral_inode_operations;
516                                 else
517                                         inode->i_op = &nfs_mountpoint_inode_operations;
518                                 inode->i_fop = NULL;
519                                 inode->i_flags |= S_AUTOMOUNT;
520                         }
521                 } else if (S_ISLNK(inode->i_mode)) {
522                         inode->i_op = &nfs_symlink_inode_operations;
523                         inode_nohighmem(inode);
524                 } else
525                         init_special_inode(inode, inode->i_mode, fattr->rdev);
526
527                 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
528                 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
529                 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
530                 inode_set_iversion_raw(inode, 0);
531                 inode->i_size = 0;
532                 clear_nlink(inode);
533                 inode->i_uid = make_kuid(&init_user_ns, -2);
534                 inode->i_gid = make_kgid(&init_user_ns, -2);
535                 inode->i_blocks = 0;
536                 nfsi->write_io = 0;
537                 nfsi->read_io = 0;
538
539                 nfsi->read_cache_jiffies = fattr->time_start;
540                 nfsi->attr_gencount = fattr->gencount;
541                 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
542                         inode->i_atime = fattr->atime;
543                 else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
544                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
545                 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
546                         inode->i_mtime = fattr->mtime;
547                 else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
548                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
549                 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
550                         inode->i_ctime = fattr->ctime;
551                 else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
552                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
553                 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
554                         inode_set_iversion_raw(inode, fattr->change_attr);
555                 else
556                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
557                 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
558                         inode->i_size = nfs_size_to_loff_t(fattr->size);
559                 else
560                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
561                 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
562                         set_nlink(inode, fattr->nlink);
563                 else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
564                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
565                 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
566                         inode->i_uid = fattr->uid;
567                 else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
568                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
569                 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
570                         inode->i_gid = fattr->gid;
571                 else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
572                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
573                 if (nfs_server_capable(inode, NFS_CAP_XATTR))
574                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
575                 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
576                         inode->i_blocks = fattr->du.nfs2.blocks;
577                 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED &&
578                          fattr->size != 0)
579                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
580                 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
581                         /*
582                          * report the blocks in 512byte units
583                          */
584                         inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
585                 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED &&
586                            fattr->size != 0)
587                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
588
589                 nfs_setsecurity(inode, fattr, label);
590
591                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
592                 nfsi->attrtimeo_timestamp = now;
593                 nfsi->access_cache = RB_ROOT;
594
595                 nfs_fscache_init_inode(inode);
596
597                 unlock_new_inode(inode);
598         } else {
599                 int err = nfs_refresh_inode(inode, fattr);
600                 if (err < 0) {
601                         iput(inode);
602                         inode = ERR_PTR(err);
603                         goto out_no_inode;
604                 }
605         }
606         dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
607                 inode->i_sb->s_id,
608                 (unsigned long long)NFS_FILEID(inode),
609                 nfs_display_fhandle_hash(fh),
610                 atomic_read(&inode->i_count));
611
612 out:
613         return inode;
614
615 out_no_inode:
616         dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
617         goto out;
618 }
619 EXPORT_SYMBOL_GPL(nfs_fhget);
620
621 #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)
622
623 int
624 nfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
625             struct iattr *attr)
626 {
627         struct inode *inode = d_inode(dentry);
628         struct nfs_fattr *fattr;
629         int error = 0;
630
631         nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
632
633         /* skip mode change if it's just for clearing setuid/setgid */
634         if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
635                 attr->ia_valid &= ~ATTR_MODE;
636
637         if (attr->ia_valid & ATTR_SIZE) {
638                 BUG_ON(!S_ISREG(inode->i_mode));
639
640                 error = inode_newsize_ok(inode, attr->ia_size);
641                 if (error)
642                         return error;
643
644                 if (attr->ia_size == i_size_read(inode))
645                         attr->ia_valid &= ~ATTR_SIZE;
646         }
647
648         /* Optimization: if the end result is no change, don't RPC */
649         if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0)
650                 return 0;
651
652         trace_nfs_setattr_enter(inode);
653
654         /* Write all dirty data */
655         if (S_ISREG(inode->i_mode))
656                 nfs_sync_inode(inode);
657
658         fattr = nfs_alloc_fattr();
659         if (fattr == NULL) {
660                 error = -ENOMEM;
661                 goto out;
662         }
663
664         error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
665         if (error == 0)
666                 error = nfs_refresh_inode(inode, fattr);
667         nfs_free_fattr(fattr);
668 out:
669         trace_nfs_setattr_exit(inode, error);
670         return error;
671 }
672 EXPORT_SYMBOL_GPL(nfs_setattr);
673
674 /**
675  * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
676  * @inode: inode of the file used
677  * @offset: file offset to start truncating
678  *
679  * This is a copy of the common vmtruncate, but with the locking
680  * corrected to take into account the fact that NFS requires
681  * inode->i_size to be updated under the inode->i_lock.
682  * Note: must be called with inode->i_lock held!
683  */
684 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
685 {
686         int err;
687
688         err = inode_newsize_ok(inode, offset);
689         if (err)
690                 goto out;
691
692         i_size_write(inode, offset);
693         /* Optimisation */
694         if (offset == 0)
695                 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_DATA |
696                                 NFS_INO_DATA_INVAL_DEFER);
697         NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
698
699         spin_unlock(&inode->i_lock);
700         truncate_pagecache(inode, offset);
701         spin_lock(&inode->i_lock);
702 out:
703         return err;
704 }
705
706 /**
707  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
708  * @inode: pointer to struct inode
709  * @attr: pointer to struct iattr
710  * @fattr: pointer to struct nfs_fattr
711  *
712  * Note: we do this in the *proc.c in order to ensure that
713  *       it works for things like exclusive creates too.
714  */
715 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
716                 struct nfs_fattr *fattr)
717 {
718         /* Barrier: bump the attribute generation count. */
719         nfs_fattr_set_barrier(fattr);
720
721         spin_lock(&inode->i_lock);
722         NFS_I(inode)->attr_gencount = fattr->gencount;
723         if ((attr->ia_valid & ATTR_SIZE) != 0) {
724                 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME |
725                                                      NFS_INO_INVALID_BLOCKS);
726                 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
727                 nfs_vmtruncate(inode, attr->ia_size);
728         }
729         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
730                 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
731                 if ((attr->ia_valid & ATTR_KILL_SUID) != 0 &&
732                     inode->i_mode & S_ISUID)
733                         inode->i_mode &= ~S_ISUID;
734                 if (setattr_should_drop_sgid(&init_user_ns, inode))
735                         inode->i_mode &= ~S_ISGID;
736                 if ((attr->ia_valid & ATTR_MODE) != 0) {
737                         int mode = attr->ia_mode & S_IALLUGO;
738                         mode |= inode->i_mode & ~S_IALLUGO;
739                         inode->i_mode = mode;
740                 }
741                 if ((attr->ia_valid & ATTR_UID) != 0)
742                         inode->i_uid = attr->ia_uid;
743                 if ((attr->ia_valid & ATTR_GID) != 0)
744                         inode->i_gid = attr->ia_gid;
745                 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
746                         inode->i_ctime = fattr->ctime;
747                 else
748                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
749                                         | NFS_INO_INVALID_CTIME);
750                 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
751                                 | NFS_INO_INVALID_ACL);
752         }
753         if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
754                 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
755                                 | NFS_INO_INVALID_CTIME);
756                 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
757                         inode->i_atime = fattr->atime;
758                 else if (attr->ia_valid & ATTR_ATIME_SET)
759                         inode->i_atime = attr->ia_atime;
760                 else
761                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
762
763                 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
764                         inode->i_ctime = fattr->ctime;
765                 else
766                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
767                                         | NFS_INO_INVALID_CTIME);
768         }
769         if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
770                 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
771                                 | NFS_INO_INVALID_CTIME);
772                 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
773                         inode->i_mtime = fattr->mtime;
774                 else if (attr->ia_valid & ATTR_MTIME_SET)
775                         inode->i_mtime = attr->ia_mtime;
776                 else
777                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
778
779                 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
780                         inode->i_ctime = fattr->ctime;
781                 else
782                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
783                                         | NFS_INO_INVALID_CTIME);
784         }
785         if (fattr->valid)
786                 nfs_update_inode(inode, fattr);
787         spin_unlock(&inode->i_lock);
788 }
789 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
790
791 static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
792 {
793         struct dentry *parent;
794
795         if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
796                 return;
797         parent = dget_parent(dentry);
798         nfs_force_use_readdirplus(d_inode(parent));
799         dput(parent);
800 }
801
802 static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
803 {
804         struct dentry *parent;
805
806         if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
807                 return;
808         parent = dget_parent(dentry);
809         nfs_advise_use_readdirplus(d_inode(parent));
810         dput(parent);
811 }
812
813 static u32 nfs_get_valid_attrmask(struct inode *inode)
814 {
815         unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
816         u32 reply_mask = STATX_INO | STATX_TYPE;
817
818         if (!(cache_validity & NFS_INO_INVALID_ATIME))
819                 reply_mask |= STATX_ATIME;
820         if (!(cache_validity & NFS_INO_INVALID_CTIME))
821                 reply_mask |= STATX_CTIME;
822         if (!(cache_validity & NFS_INO_INVALID_MTIME))
823                 reply_mask |= STATX_MTIME;
824         if (!(cache_validity & NFS_INO_INVALID_SIZE))
825                 reply_mask |= STATX_SIZE;
826         if (!(cache_validity & NFS_INO_INVALID_NLINK))
827                 reply_mask |= STATX_NLINK;
828         if (!(cache_validity & NFS_INO_INVALID_MODE))
829                 reply_mask |= STATX_MODE;
830         if (!(cache_validity & NFS_INO_INVALID_OTHER))
831                 reply_mask |= STATX_UID | STATX_GID;
832         if (!(cache_validity & NFS_INO_INVALID_BLOCKS))
833                 reply_mask |= STATX_BLOCKS;
834         return reply_mask;
835 }
836
837 int nfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
838                 struct kstat *stat, u32 request_mask, unsigned int query_flags)
839 {
840         struct inode *inode = d_inode(path->dentry);
841         struct nfs_server *server = NFS_SERVER(inode);
842         unsigned long cache_validity;
843         int err = 0;
844         bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
845         bool do_update = false;
846
847         trace_nfs_getattr_enter(inode);
848
849         request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID |
850                         STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME |
851                         STATX_INO | STATX_SIZE | STATX_BLOCKS;
852
853         if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
854                 nfs_readdirplus_parent_cache_hit(path->dentry);
855                 goto out_no_revalidate;
856         }
857
858         /* Flush out writes to the server in order to update c/mtime.  */
859         if ((request_mask & (STATX_CTIME | STATX_MTIME)) &&
860             S_ISREG(inode->i_mode))
861                 filemap_write_and_wait(inode->i_mapping);
862
863         /*
864          * We may force a getattr if the user cares about atime.
865          *
866          * Note that we only have to check the vfsmount flags here:
867          *  - NFS always sets S_NOATIME by so checking it would give a
868          *    bogus result
869          *  - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
870          *    no point in checking those.
871          */
872         if ((path->mnt->mnt_flags & MNT_NOATIME) ||
873             ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
874                 request_mask &= ~STATX_ATIME;
875
876         /* Is the user requesting attributes that might need revalidation? */
877         if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
878                                         STATX_MTIME|STATX_UID|STATX_GID|
879                                         STATX_SIZE|STATX_BLOCKS)))
880                 goto out_no_revalidate;
881
882         /* Check whether the cached attributes are stale */
883         do_update |= force_sync || nfs_attribute_cache_expired(inode);
884         cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
885         do_update |= cache_validity & NFS_INO_INVALID_CHANGE;
886         if (request_mask & STATX_ATIME)
887                 do_update |= cache_validity & NFS_INO_INVALID_ATIME;
888         if (request_mask & STATX_CTIME)
889                 do_update |= cache_validity & NFS_INO_INVALID_CTIME;
890         if (request_mask & STATX_MTIME)
891                 do_update |= cache_validity & NFS_INO_INVALID_MTIME;
892         if (request_mask & STATX_SIZE)
893                 do_update |= cache_validity & NFS_INO_INVALID_SIZE;
894         if (request_mask & STATX_NLINK)
895                 do_update |= cache_validity & NFS_INO_INVALID_NLINK;
896         if (request_mask & STATX_MODE)
897                 do_update |= cache_validity & NFS_INO_INVALID_MODE;
898         if (request_mask & (STATX_UID | STATX_GID))
899                 do_update |= cache_validity & NFS_INO_INVALID_OTHER;
900         if (request_mask & STATX_BLOCKS)
901                 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
902
903         if (do_update) {
904                 /* Update the attribute cache */
905                 if (!(server->flags & NFS_MOUNT_NOAC))
906                         nfs_readdirplus_parent_cache_miss(path->dentry);
907                 else
908                         nfs_readdirplus_parent_cache_hit(path->dentry);
909                 err = __nfs_revalidate_inode(server, inode);
910                 if (err)
911                         goto out;
912         } else
913                 nfs_readdirplus_parent_cache_hit(path->dentry);
914 out_no_revalidate:
915         /* Only return attributes that were revalidated. */
916         stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask;
917
918         generic_fillattr(&init_user_ns, inode, stat);
919         stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
920         if (S_ISDIR(inode->i_mode))
921                 stat->blksize = NFS_SERVER(inode)->dtsize;
922 out:
923         trace_nfs_getattr_exit(inode, err);
924         return err;
925 }
926 EXPORT_SYMBOL_GPL(nfs_getattr);
927
928 static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
929 {
930         refcount_set(&l_ctx->count, 1);
931         l_ctx->lockowner = current->files;
932         INIT_LIST_HEAD(&l_ctx->list);
933         atomic_set(&l_ctx->io_count, 0);
934 }
935
936 static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
937 {
938         struct nfs_lock_context *pos;
939
940         list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
941                 if (pos->lockowner != current->files)
942                         continue;
943                 if (refcount_inc_not_zero(&pos->count))
944                         return pos;
945         }
946         return NULL;
947 }
948
949 struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
950 {
951         struct nfs_lock_context *res, *new = NULL;
952         struct inode *inode = d_inode(ctx->dentry);
953
954         rcu_read_lock();
955         res = __nfs_find_lock_context(ctx);
956         rcu_read_unlock();
957         if (res == NULL) {
958                 new = kmalloc(sizeof(*new), GFP_KERNEL);
959                 if (new == NULL)
960                         return ERR_PTR(-ENOMEM);
961                 nfs_init_lock_context(new);
962                 spin_lock(&inode->i_lock);
963                 res = __nfs_find_lock_context(ctx);
964                 if (res == NULL) {
965                         new->open_context = get_nfs_open_context(ctx);
966                         if (new->open_context) {
967                                 list_add_tail_rcu(&new->list,
968                                                 &ctx->lock_context.list);
969                                 res = new;
970                                 new = NULL;
971                         } else
972                                 res = ERR_PTR(-EBADF);
973                 }
974                 spin_unlock(&inode->i_lock);
975                 kfree(new);
976         }
977         return res;
978 }
979 EXPORT_SYMBOL_GPL(nfs_get_lock_context);
980
981 void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
982 {
983         struct nfs_open_context *ctx = l_ctx->open_context;
984         struct inode *inode = d_inode(ctx->dentry);
985
986         if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
987                 return;
988         list_del_rcu(&l_ctx->list);
989         spin_unlock(&inode->i_lock);
990         put_nfs_open_context(ctx);
991         kfree_rcu(l_ctx, rcu_head);
992 }
993 EXPORT_SYMBOL_GPL(nfs_put_lock_context);
994
995 /**
996  * nfs_close_context - Common close_context() routine NFSv2/v3
997  * @ctx: pointer to context
998  * @is_sync: is this a synchronous close
999  *
1000  * Ensure that the attributes are up to date if we're mounted
1001  * with close-to-open semantics and we have cached data that will
1002  * need to be revalidated on open.
1003  */
1004 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
1005 {
1006         struct nfs_inode *nfsi;
1007         struct inode *inode;
1008
1009         if (!(ctx->mode & FMODE_WRITE))
1010                 return;
1011         if (!is_sync)
1012                 return;
1013         inode = d_inode(ctx->dentry);
1014         if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1015                 return;
1016         nfsi = NFS_I(inode);
1017         if (inode->i_mapping->nrpages == 0)
1018                 return;
1019         if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1020                 return;
1021         if (!list_empty(&nfsi->open_files))
1022                 return;
1023         if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO)
1024                 return;
1025         nfs_revalidate_inode(inode,
1026                              NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
1027 }
1028 EXPORT_SYMBOL_GPL(nfs_close_context);
1029
1030 struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
1031                                                 fmode_t f_mode,
1032                                                 struct file *filp)
1033 {
1034         struct nfs_open_context *ctx;
1035
1036         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
1037         if (!ctx)
1038                 return ERR_PTR(-ENOMEM);
1039         nfs_sb_active(dentry->d_sb);
1040         ctx->dentry = dget(dentry);
1041         if (filp)
1042                 ctx->cred = get_cred(filp->f_cred);
1043         else
1044                 ctx->cred = get_current_cred();
1045         ctx->ll_cred = NULL;
1046         ctx->state = NULL;
1047         ctx->mode = f_mode;
1048         ctx->flags = 0;
1049         ctx->error = 0;
1050         ctx->flock_owner = (fl_owner_t)filp;
1051         nfs_init_lock_context(&ctx->lock_context);
1052         ctx->lock_context.open_context = ctx;
1053         INIT_LIST_HEAD(&ctx->list);
1054         ctx->mdsthreshold = NULL;
1055         return ctx;
1056 }
1057 EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
1058
1059 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1060 {
1061         if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1062                 return ctx;
1063         return NULL;
1064 }
1065 EXPORT_SYMBOL_GPL(get_nfs_open_context);
1066
1067 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1068 {
1069         struct inode *inode = d_inode(ctx->dentry);
1070         struct super_block *sb = ctx->dentry->d_sb;
1071
1072         if (!refcount_dec_and_test(&ctx->lock_context.count))
1073                 return;
1074         if (!list_empty(&ctx->list)) {
1075                 spin_lock(&inode->i_lock);
1076                 list_del_rcu(&ctx->list);
1077                 spin_unlock(&inode->i_lock);
1078         }
1079         if (inode != NULL)
1080                 NFS_PROTO(inode)->close_context(ctx, is_sync);
1081         put_cred(ctx->cred);
1082         dput(ctx->dentry);
1083         nfs_sb_deactive(sb);
1084         put_rpccred(ctx->ll_cred);
1085         kfree(ctx->mdsthreshold);
1086         kfree_rcu(ctx, rcu_head);
1087 }
1088
1089 void put_nfs_open_context(struct nfs_open_context *ctx)
1090 {
1091         __put_nfs_open_context(ctx, 0);
1092 }
1093 EXPORT_SYMBOL_GPL(put_nfs_open_context);
1094
1095 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1096 {
1097         __put_nfs_open_context(ctx, 1);
1098 }
1099
1100 /*
1101  * Ensure that mmap has a recent RPC credential for use when writing out
1102  * shared pages
1103  */
1104 void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
1105 {
1106         struct inode *inode = d_inode(ctx->dentry);
1107         struct nfs_inode *nfsi = NFS_I(inode);
1108
1109         spin_lock(&inode->i_lock);
1110         if (list_empty(&nfsi->open_files) &&
1111             (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
1112                 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA |
1113                                                      NFS_INO_REVAL_FORCED);
1114         list_add_tail_rcu(&ctx->list, &nfsi->open_files);
1115         spin_unlock(&inode->i_lock);
1116 }
1117 EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1118
1119 void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1120 {
1121         filp->private_data = get_nfs_open_context(ctx);
1122         set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1123         if (list_empty(&ctx->list))
1124                 nfs_inode_attach_open_context(ctx);
1125 }
1126 EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
1127
1128 /*
1129  * Given an inode, search for an open context with the desired characteristics
1130  */
1131 struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
1132 {
1133         struct nfs_inode *nfsi = NFS_I(inode);
1134         struct nfs_open_context *pos, *ctx = NULL;
1135
1136         rcu_read_lock();
1137         list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
1138                 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
1139                         continue;
1140                 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1141                         continue;
1142                 if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
1143                         continue;
1144                 ctx = get_nfs_open_context(pos);
1145                 if (ctx)
1146                         break;
1147         }
1148         rcu_read_unlock();
1149         return ctx;
1150 }
1151
1152 void nfs_file_clear_open_context(struct file *filp)
1153 {
1154         struct nfs_open_context *ctx = nfs_file_open_context(filp);
1155
1156         if (ctx) {
1157                 struct inode *inode = d_inode(ctx->dentry);
1158
1159                 clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
1160                 /*
1161                  * We fatal error on write before. Try to writeback
1162                  * every page again.
1163                  */
1164                 if (ctx->error < 0)
1165                         invalidate_inode_pages2(inode->i_mapping);
1166                 filp->private_data = NULL;
1167                 put_nfs_open_context_sync(ctx);
1168         }
1169 }
1170
1171 /*
1172  * These allocate and release file read/write context information.
1173  */
1174 int nfs_open(struct inode *inode, struct file *filp)
1175 {
1176         struct nfs_open_context *ctx;
1177
1178         ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
1179         if (IS_ERR(ctx))
1180                 return PTR_ERR(ctx);
1181         nfs_file_set_open_context(filp, ctx);
1182         put_nfs_open_context(ctx);
1183         nfs_fscache_open_file(inode, filp);
1184         return 0;
1185 }
1186
1187 /*
1188  * This function is called whenever some part of NFS notices that
1189  * the cached attributes have to be refreshed.
1190  */
1191 int
1192 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1193 {
1194         int              status = -ESTALE;
1195         struct nfs4_label *label = NULL;
1196         struct nfs_fattr *fattr = NULL;
1197         struct nfs_inode *nfsi = NFS_I(inode);
1198
1199         dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1200                 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
1201
1202         trace_nfs_revalidate_inode_enter(inode);
1203
1204         if (is_bad_inode(inode))
1205                 goto out;
1206         if (NFS_STALE(inode))
1207                 goto out;
1208
1209         /* pNFS: Attributes aren't updated until we layoutcommit */
1210         if (S_ISREG(inode->i_mode)) {
1211                 status = pnfs_sync_inode(inode, false);
1212                 if (status)
1213                         goto out;
1214         }
1215
1216         status = -ENOMEM;
1217         fattr = nfs_alloc_fattr();
1218         if (fattr == NULL)
1219                 goto out;
1220
1221         nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1222
1223         label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1224         if (IS_ERR(label)) {
1225                 status = PTR_ERR(label);
1226                 goto out;
1227         }
1228
1229         status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
1230                         label, inode);
1231         if (status != 0) {
1232                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
1233                          inode->i_sb->s_id,
1234                          (unsigned long long)NFS_FILEID(inode), status);
1235                 switch (status) {
1236                 case -ETIMEDOUT:
1237                         /* A soft timeout occurred. Use cached information? */
1238                         if (server->flags & NFS_MOUNT_SOFTREVAL)
1239                                 status = 0;
1240                         break;
1241                 case -ESTALE:
1242                         if (!S_ISDIR(inode->i_mode))
1243                                 nfs_set_inode_stale(inode);
1244                         else
1245                                 nfs_zap_caches(inode);
1246                 }
1247                 goto err_out;
1248         }
1249
1250         status = nfs_refresh_inode(inode, fattr);
1251         if (status) {
1252                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
1253                          inode->i_sb->s_id,
1254                          (unsigned long long)NFS_FILEID(inode), status);
1255                 goto err_out;
1256         }
1257
1258         if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
1259                 nfs_zap_acl_cache(inode);
1260
1261         nfs_setsecurity(inode, fattr, label);
1262
1263         dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
1264                 inode->i_sb->s_id,
1265                 (unsigned long long)NFS_FILEID(inode));
1266
1267 err_out:
1268         nfs4_label_free(label);
1269 out:
1270         nfs_free_fattr(fattr);
1271         trace_nfs_revalidate_inode_exit(inode, status);
1272         return status;
1273 }
1274
1275 int nfs_attribute_cache_expired(struct inode *inode)
1276 {
1277         if (nfs_have_delegated_attributes(inode))
1278                 return 0;
1279         return nfs_attribute_timeout(inode);
1280 }
1281
1282 /**
1283  * nfs_revalidate_inode - Revalidate the inode attributes
1284  * @inode: pointer to inode struct
1285  * @flags: cache flags to check
1286  *
1287  * Updates inode attribute information by retrieving the data from the server.
1288  */
1289 int nfs_revalidate_inode(struct inode *inode, unsigned long flags)
1290 {
1291         if (!nfs_check_cache_invalid(inode, flags))
1292                 return NFS_STALE(inode) ? -ESTALE : 0;
1293         return __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1294 }
1295 EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
1296
1297 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
1298 {
1299         int ret;
1300
1301         if (mapping->nrpages != 0) {
1302                 if (S_ISREG(inode->i_mode)) {
1303                         ret = nfs_sync_mapping(mapping);
1304                         if (ret < 0)
1305                                 return ret;
1306                 }
1307                 ret = invalidate_inode_pages2(mapping);
1308                 if (ret < 0)
1309                         return ret;
1310         }
1311         nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
1312         nfs_fscache_wait_on_invalidate(inode);
1313
1314         dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1315                         inode->i_sb->s_id,
1316                         (unsigned long long)NFS_FILEID(inode));
1317         return 0;
1318 }
1319
1320 /**
1321  * nfs_clear_invalid_mapping - Conditionally clear a mapping
1322  * @mapping: pointer to mapping
1323  *
1324  * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping.
1325  */
1326 int nfs_clear_invalid_mapping(struct address_space *mapping)
1327 {
1328         struct inode *inode = mapping->host;
1329         struct nfs_inode *nfsi = NFS_I(inode);
1330         unsigned long *bitlock = &nfsi->flags;
1331         int ret = 0;
1332
1333         /*
1334          * We must clear NFS_INO_INVALID_DATA first to ensure that
1335          * invalidations that come in while we're shooting down the mappings
1336          * are respected. But, that leaves a race window where one revalidator
1337          * can clear the flag, and then another checks it before the mapping
1338          * gets invalidated. Fix that by serializing access to this part of
1339          * the function.
1340          *
1341          * At the same time, we need to allow other tasks to see whether we
1342          * might be in the middle of invalidating the pages, so we only set
1343          * the bit lock here if it looks like we're going to be doing that.
1344          */
1345         for (;;) {
1346                 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1347                                          nfs_wait_bit_killable, TASK_KILLABLE);
1348                 if (ret)
1349                         goto out;
1350                 spin_lock(&inode->i_lock);
1351                 if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1352                         spin_unlock(&inode->i_lock);
1353                         continue;
1354                 }
1355                 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1356                         break;
1357                 spin_unlock(&inode->i_lock);
1358                 goto out;
1359         }
1360
1361         set_bit(NFS_INO_INVALIDATING, bitlock);
1362         smp_wmb();
1363         nfsi->cache_validity &=
1364                 ~(NFS_INO_INVALID_DATA | NFS_INO_DATA_INVAL_DEFER);
1365         spin_unlock(&inode->i_lock);
1366         trace_nfs_invalidate_mapping_enter(inode);
1367         ret = nfs_invalidate_mapping(inode, mapping);
1368         trace_nfs_invalidate_mapping_exit(inode, ret);
1369
1370         clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
1371         smp_mb__after_atomic();
1372         wake_up_bit(bitlock, NFS_INO_INVALIDATING);
1373 out:
1374         return ret;
1375 }
1376
1377 bool nfs_mapping_need_revalidate_inode(struct inode *inode)
1378 {
1379         return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) ||
1380                 NFS_STALE(inode);
1381 }
1382
1383 int nfs_revalidate_mapping_rcu(struct inode *inode)
1384 {
1385         struct nfs_inode *nfsi = NFS_I(inode);
1386         unsigned long *bitlock = &nfsi->flags;
1387         int ret = 0;
1388
1389         if (IS_SWAPFILE(inode))
1390                 goto out;
1391         if (nfs_mapping_need_revalidate_inode(inode)) {
1392                 ret = -ECHILD;
1393                 goto out;
1394         }
1395         spin_lock(&inode->i_lock);
1396         if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1397             (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1398                 ret = -ECHILD;
1399         spin_unlock(&inode->i_lock);
1400 out:
1401         return ret;
1402 }
1403
1404 /**
1405  * nfs_revalidate_mapping - Revalidate the pagecache
1406  * @inode: pointer to host inode
1407  * @mapping: pointer to mapping
1408  */
1409 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
1410 {
1411         /* swapfiles are not supposed to be shared. */
1412         if (IS_SWAPFILE(inode))
1413                 return 0;
1414
1415         if (nfs_mapping_need_revalidate_inode(inode)) {
1416                 int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1417                 if (ret < 0)
1418                         return ret;
1419         }
1420
1421         return nfs_clear_invalid_mapping(mapping);
1422 }
1423
1424 static bool nfs_file_has_writers(struct nfs_inode *nfsi)
1425 {
1426         struct inode *inode = &nfsi->vfs_inode;
1427
1428         if (!S_ISREG(inode->i_mode))
1429                 return false;
1430         if (list_empty(&nfsi->open_files))
1431                 return false;
1432         return inode_is_open_for_write(inode);
1433 }
1434
1435 static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
1436 {
1437         return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
1438 }
1439
1440 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1441 {
1442         struct timespec64 ts;
1443
1444         if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1445                         && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
1446                         && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
1447                 inode_set_iversion_raw(inode, fattr->change_attr);
1448                 if (S_ISDIR(inode->i_mode))
1449                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1450                 else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1451                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
1452         }
1453         /* If we have atomic WCC data, we may update some attributes */
1454         ts = inode->i_ctime;
1455         if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1456                         && (fattr->valid & NFS_ATTR_FATTR_CTIME)
1457                         && timespec64_equal(&ts, &fattr->pre_ctime)) {
1458                 inode->i_ctime = fattr->ctime;
1459         }
1460
1461         ts = inode->i_mtime;
1462         if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1463                         && (fattr->valid & NFS_ATTR_FATTR_MTIME)
1464                         && timespec64_equal(&ts, &fattr->pre_mtime)) {
1465                 inode->i_mtime = fattr->mtime;
1466                 if (S_ISDIR(inode->i_mode))
1467                         nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
1468         }
1469         if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1470                         && (fattr->valid & NFS_ATTR_FATTR_SIZE)
1471                         && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
1472                         && !nfs_have_writebacks(inode)) {
1473                 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
1474         }
1475 }
1476
1477 /**
1478  * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1479  * @inode: pointer to inode
1480  * @fattr: updated attributes
1481  *
1482  * Verifies the attribute cache. If we have just changed the attributes,
1483  * so that fattr carries weak cache consistency data, then it may
1484  * also update the ctime/mtime/change_attribute.
1485  */
1486 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1487 {
1488         struct nfs_inode *nfsi = NFS_I(inode);
1489         loff_t cur_size, new_isize;
1490         unsigned long invalid = 0;
1491         struct timespec64 ts;
1492
1493         if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1494                 return 0;
1495
1496         if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1497                 /* Only a mounted-on-fileid? Just exit */
1498                 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1499                         return 0;
1500         /* Has the inode gone and changed behind our back? */
1501         } else if (nfsi->fileid != fattr->fileid) {
1502                 /* Is this perhaps the mounted-on fileid? */
1503                 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1504                     nfsi->fileid == fattr->mounted_on_fileid)
1505                         return 0;
1506                 return -ESTALE;
1507         }
1508         if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
1509                 return -ESTALE;
1510
1511
1512         if (!nfs_file_has_buffered_writers(nfsi)) {
1513                 /* Verify a few of the more important attributes */
1514                 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
1515                         invalid |= NFS_INO_INVALID_CHANGE;
1516
1517                 ts = inode->i_mtime;
1518                 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
1519                         invalid |= NFS_INO_INVALID_MTIME;
1520
1521                 ts = inode->i_ctime;
1522                 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
1523                         invalid |= NFS_INO_INVALID_CTIME;
1524
1525                 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1526                         cur_size = i_size_read(inode);
1527                         new_isize = nfs_size_to_loff_t(fattr->size);
1528                         if (cur_size != new_isize)
1529                                 invalid |= NFS_INO_INVALID_SIZE;
1530                 }
1531         }
1532
1533         /* Have any file permissions changed? */
1534         if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
1535                 invalid |= NFS_INO_INVALID_MODE;
1536         if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
1537                 invalid |= NFS_INO_INVALID_OTHER;
1538         if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
1539                 invalid |= NFS_INO_INVALID_OTHER;
1540
1541         /* Has the link count changed? */
1542         if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
1543                 invalid |= NFS_INO_INVALID_NLINK;
1544
1545         ts = inode->i_atime;
1546         if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
1547                 invalid |= NFS_INO_INVALID_ATIME;
1548
1549         if (invalid != 0)
1550                 nfs_set_cache_invalid(inode, invalid);
1551
1552         nfsi->read_cache_jiffies = fattr->time_start;
1553         return 0;
1554 }
1555
1556 static atomic_long_t nfs_attr_generation_counter;
1557
1558 static unsigned long nfs_read_attr_generation_counter(void)
1559 {
1560         return atomic_long_read(&nfs_attr_generation_counter);
1561 }
1562
1563 unsigned long nfs_inc_attr_generation_counter(void)
1564 {
1565         return atomic_long_inc_return(&nfs_attr_generation_counter);
1566 }
1567 EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
1568
1569 void nfs_fattr_init(struct nfs_fattr *fattr)
1570 {
1571         fattr->valid = 0;
1572         fattr->time_start = jiffies;
1573         fattr->gencount = nfs_inc_attr_generation_counter();
1574         fattr->owner_name = NULL;
1575         fattr->group_name = NULL;
1576 }
1577 EXPORT_SYMBOL_GPL(nfs_fattr_init);
1578
1579 /**
1580  * nfs_fattr_set_barrier
1581  * @fattr: attributes
1582  *
1583  * Used to set a barrier after an attribute was updated. This
1584  * barrier ensures that older attributes from RPC calls that may
1585  * have raced with our update cannot clobber these new values.
1586  * Note that you are still responsible for ensuring that other
1587  * operations which change the attribute on the server do not
1588  * collide.
1589  */
1590 void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1591 {
1592         fattr->gencount = nfs_inc_attr_generation_counter();
1593 }
1594
1595 struct nfs_fattr *nfs_alloc_fattr(void)
1596 {
1597         struct nfs_fattr *fattr;
1598
1599         fattr = kmalloc(sizeof(*fattr), GFP_KERNEL);
1600         if (fattr != NULL) {
1601                 nfs_fattr_init(fattr);
1602                 fattr->label = NULL;
1603         }
1604         return fattr;
1605 }
1606 EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
1607
1608 struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server)
1609 {
1610         struct nfs_fattr *fattr = nfs_alloc_fattr();
1611
1612         if (!fattr)
1613                 return NULL;
1614
1615         fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
1616         if (IS_ERR(fattr->label)) {
1617                 kfree(fattr);
1618                 return NULL;
1619         }
1620
1621         return fattr;
1622 }
1623 EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label);
1624
1625 struct nfs_fh *nfs_alloc_fhandle(void)
1626 {
1627         struct nfs_fh *fh;
1628
1629         fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
1630         if (fh != NULL)
1631                 fh->size = 0;
1632         return fh;
1633 }
1634 EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
1635
1636 #ifdef NFS_DEBUG
1637 /*
1638  * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1639  *                             in the same way that wireshark does
1640  *
1641  * @fh: file handle
1642  *
1643  * For debugging only.
1644  */
1645 u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1646 {
1647         /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1648          * not on the result */
1649         return nfs_fhandle_hash(fh);
1650 }
1651 EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
1652
1653 /*
1654  * _nfs_display_fhandle - display an NFS file handle on the console
1655  *
1656  * @fh: file handle to display
1657  * @caption: display caption
1658  *
1659  * For debugging only.
1660  */
1661 void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1662 {
1663         unsigned short i;
1664
1665         if (fh == NULL || fh->size == 0) {
1666                 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1667                 return;
1668         }
1669
1670         printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1671                caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
1672         for (i = 0; i < fh->size; i += 16) {
1673                 __be32 *pos = (__be32 *)&fh->data[i];
1674
1675                 switch ((fh->size - i - 1) >> 2) {
1676                 case 0:
1677                         printk(KERN_DEFAULT " %08x\n",
1678                                 be32_to_cpup(pos));
1679                         break;
1680                 case 1:
1681                         printk(KERN_DEFAULT " %08x %08x\n",
1682                                 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1683                         break;
1684                 case 2:
1685                         printk(KERN_DEFAULT " %08x %08x %08x\n",
1686                                 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1687                                 be32_to_cpup(pos + 2));
1688                         break;
1689                 default:
1690                         printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1691                                 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1692                                 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1693                 }
1694         }
1695 }
1696 EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
1697 #endif
1698
1699 /**
1700  * nfs_inode_attrs_cmp_generic - compare attributes
1701  * @fattr: attributes
1702  * @inode: pointer to inode
1703  *
1704  * Attempt to divine whether or not an RPC call reply carrying stale
1705  * attributes got scheduled after another call carrying updated ones.
1706  * Note also the check for wraparound of 'attr_gencount'
1707  *
1708  * The function returns '1' if it thinks the attributes in @fattr are
1709  * more recent than the ones cached in @inode. Otherwise it returns
1710  * the value '0'.
1711  */
1712 static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr,
1713                                        const struct inode *inode)
1714 {
1715         unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
1716
1717         return (long)(fattr->gencount - attr_gencount) > 0 ||
1718                (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
1719 }
1720
1721 /**
1722  * nfs_inode_attrs_cmp_monotonic - compare attributes
1723  * @fattr: attributes
1724  * @inode: pointer to inode
1725  *
1726  * Attempt to divine whether or not an RPC call reply carrying stale
1727  * attributes got scheduled after another call carrying updated ones.
1728  *
1729  * We assume that the server observes monotonic semantics for
1730  * the change attribute, so a larger value means that the attributes in
1731  * @fattr are more recent, in which case the function returns the
1732  * value '1'.
1733  * A return value of '0' indicates no measurable change
1734  * A return value of '-1' means that the attributes in @inode are
1735  * more recent.
1736  */
1737 static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr,
1738                                          const struct inode *inode)
1739 {
1740         s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode);
1741         if (diff > 0)
1742                 return 1;
1743         return diff == 0 ? 0 : -1;
1744 }
1745
1746 /**
1747  * nfs_inode_attrs_cmp_strict_monotonic - compare attributes
1748  * @fattr: attributes
1749  * @inode: pointer to inode
1750  *
1751  * Attempt to divine whether or not an RPC call reply carrying stale
1752  * attributes got scheduled after another call carrying updated ones.
1753  *
1754  * We assume that the server observes strictly monotonic semantics for
1755  * the change attribute, so a larger value means that the attributes in
1756  * @fattr are more recent, in which case the function returns the
1757  * value '1'.
1758  * A return value of '-1' means that the attributes in @inode are
1759  * more recent or unchanged.
1760  */
1761 static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr,
1762                                                 const struct inode *inode)
1763 {
1764         return  nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1;
1765 }
1766
1767 /**
1768  * nfs_inode_attrs_cmp - compare attributes
1769  * @fattr: attributes
1770  * @inode: pointer to inode
1771  *
1772  * This function returns '1' if it thinks the attributes in @fattr are
1773  * more recent than the ones cached in @inode. It returns '-1' if
1774  * the attributes in @inode are more recent than the ones in @fattr,
1775  * and it returns 0 if not sure.
1776  */
1777 static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr,
1778                                const struct inode *inode)
1779 {
1780         if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0)
1781                 return 1;
1782         switch (NFS_SERVER(inode)->change_attr_type) {
1783         case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1784                 break;
1785         case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1786                 if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
1787                         break;
1788                 return nfs_inode_attrs_cmp_monotonic(fattr, inode);
1789         default:
1790                 if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE))
1791                         break;
1792                 return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode);
1793         }
1794         return 0;
1795 }
1796
1797 /**
1798  * nfs_inode_finish_partial_attr_update - complete a previous inode update
1799  * @fattr: attributes
1800  * @inode: pointer to inode
1801  *
1802  * Returns '1' if the last attribute update left the inode cached
1803  * attributes in a partially unrevalidated state, and @fattr
1804  * matches the change attribute of that partial update.
1805  * Otherwise returns '0'.
1806  */
1807 static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr,
1808                                                 const struct inode *inode)
1809 {
1810         const unsigned long check_valid =
1811                 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
1812                 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
1813                 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER |
1814                 NFS_INO_INVALID_NLINK;
1815         unsigned long cache_validity = NFS_I(inode)->cache_validity;
1816         enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type;
1817
1818         if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED &&
1819             !(cache_validity & NFS_INO_INVALID_CHANGE) &&
1820             (cache_validity & check_valid) != 0 &&
1821             (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1822             nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0)
1823                 return 1;
1824         return 0;
1825 }
1826
1827 static int nfs_refresh_inode_locked(struct inode *inode,
1828                                     struct nfs_fattr *fattr)
1829 {
1830         int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
1831         int ret = 0;
1832
1833         trace_nfs_refresh_inode_enter(inode);
1834
1835         if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode))
1836                 ret = nfs_update_inode(inode, fattr);
1837         else if (attr_cmp == 0)
1838                 ret = nfs_check_inode_attributes(inode, fattr);
1839
1840         trace_nfs_refresh_inode_exit(inode, ret);
1841         return ret;
1842 }
1843
1844 /**
1845  * nfs_refresh_inode - try to update the inode attribute cache
1846  * @inode: pointer to inode
1847  * @fattr: updated attributes
1848  *
1849  * Check that an RPC call that returned attributes has not overlapped with
1850  * other recent updates of the inode metadata, then decide whether it is
1851  * safe to do a full update of the inode attributes, or whether just to
1852  * call nfs_check_inode_attributes.
1853  */
1854 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1855 {
1856         int status;
1857
1858         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1859                 return 0;
1860         spin_lock(&inode->i_lock);
1861         status = nfs_refresh_inode_locked(inode, fattr);
1862         spin_unlock(&inode->i_lock);
1863
1864         return status;
1865 }
1866 EXPORT_SYMBOL_GPL(nfs_refresh_inode);
1867
1868 static int nfs_post_op_update_inode_locked(struct inode *inode,
1869                 struct nfs_fattr *fattr, unsigned int invalid)
1870 {
1871         if (S_ISDIR(inode->i_mode))
1872                 invalid |= NFS_INO_INVALID_DATA;
1873         nfs_set_cache_invalid(inode, invalid);
1874         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1875                 return 0;
1876         return nfs_refresh_inode_locked(inode, fattr);
1877 }
1878
1879 /**
1880  * nfs_post_op_update_inode - try to update the inode attribute cache
1881  * @inode: pointer to inode
1882  * @fattr: updated attributes
1883  *
1884  * After an operation that has changed the inode metadata, mark the
1885  * attribute cache as being invalid, then try to update it.
1886  *
1887  * NB: if the server didn't return any post op attributes, this
1888  * function will force the retrieval of attributes before the next
1889  * NFS request.  Thus it should be used only for operations that
1890  * are expected to change one or more attributes, to avoid
1891  * unnecessary NFS requests and trips through nfs_update_inode().
1892  */
1893 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1894 {
1895         int status;
1896
1897         spin_lock(&inode->i_lock);
1898         nfs_fattr_set_barrier(fattr);
1899         status = nfs_post_op_update_inode_locked(inode, fattr,
1900                         NFS_INO_INVALID_CHANGE
1901                         | NFS_INO_INVALID_CTIME
1902                         | NFS_INO_REVAL_FORCED);
1903         spin_unlock(&inode->i_lock);
1904
1905         return status;
1906 }
1907 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
1908
1909 /**
1910  * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
1911  * @inode: pointer to inode
1912  * @fattr: updated attributes
1913  *
1914  * After an operation that has changed the inode metadata, mark the
1915  * attribute cache as being invalid, then try to update it. Fake up
1916  * weak cache consistency data, if none exist.
1917  *
1918  * This function is mainly designed to be used by the ->write_done() functions.
1919  */
1920 int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
1921 {
1922         int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
1923         int status;
1924
1925         /* Don't do a WCC update if these attributes are already stale */
1926         if (attr_cmp < 0)
1927                 return 0;
1928         if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) {
1929                 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1930                                 | NFS_ATTR_FATTR_PRESIZE
1931                                 | NFS_ATTR_FATTR_PREMTIME
1932                                 | NFS_ATTR_FATTR_PRECTIME);
1933                 goto out_noforce;
1934         }
1935         if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1936                         (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
1937                 fattr->pre_change_attr = inode_peek_iversion_raw(inode);
1938                 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
1939         }
1940         if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1941                         (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
1942                 fattr->pre_ctime = inode->i_ctime;
1943                 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1944         }
1945         if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1946                         (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
1947                 fattr->pre_mtime = inode->i_mtime;
1948                 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1949         }
1950         if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1951                         (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
1952                 fattr->pre_size = i_size_read(inode);
1953                 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
1954         }
1955 out_noforce:
1956         status = nfs_post_op_update_inode_locked(inode, fattr,
1957                         NFS_INO_INVALID_CHANGE
1958                         | NFS_INO_INVALID_CTIME
1959                         | NFS_INO_INVALID_MTIME
1960                         | NFS_INO_INVALID_BLOCKS);
1961         return status;
1962 }
1963
1964 /**
1965  * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1966  * @inode: pointer to inode
1967  * @fattr: updated attributes
1968  *
1969  * After an operation that has changed the inode metadata, mark the
1970  * attribute cache as being invalid, then try to update it. Fake up
1971  * weak cache consistency data, if none exist.
1972  *
1973  * This function is mainly designed to be used by the ->write_done() functions.
1974  */
1975 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1976 {
1977         int status;
1978
1979         spin_lock(&inode->i_lock);
1980         nfs_fattr_set_barrier(fattr);
1981         status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1982         spin_unlock(&inode->i_lock);
1983         return status;
1984 }
1985 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
1986
1987
1988 /*
1989  * Many nfs protocol calls return the new file attributes after
1990  * an operation.  Here we update the inode to reflect the state
1991  * of the server's inode.
1992  *
1993  * This is a bit tricky because we have to make sure all dirty pages
1994  * have been sent off to the server before calling invalidate_inode_pages.
1995  * To make sure no other process adds more write requests while we try
1996  * our best to flush them, we make them sleep during the attribute refresh.
1997  *
1998  * A very similar scenario holds for the dir cache.
1999  */
2000 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
2001 {
2002         struct nfs_server *server = NFS_SERVER(inode);
2003         struct nfs_inode *nfsi = NFS_I(inode);
2004         loff_t cur_isize, new_isize;
2005         u64 fattr_supported = server->fattr_valid;
2006         unsigned long invalid = 0;
2007         unsigned long now = jiffies;
2008         unsigned long save_cache_validity;
2009         bool have_writers = nfs_file_has_buffered_writers(nfsi);
2010         bool cache_revalidated = true;
2011         bool attr_changed = false;
2012         bool have_delegation;
2013
2014         dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
2015                         __func__, inode->i_sb->s_id, inode->i_ino,
2016                         nfs_display_fhandle_hash(NFS_FH(inode)),
2017                         atomic_read(&inode->i_count), fattr->valid);
2018
2019         if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
2020                 /* Only a mounted-on-fileid? Just exit */
2021                 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
2022                         return 0;
2023         /* Has the inode gone and changed behind our back? */
2024         } else if (nfsi->fileid != fattr->fileid) {
2025                 /* Is this perhaps the mounted-on fileid? */
2026                 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
2027                     nfsi->fileid == fattr->mounted_on_fileid)
2028                         return 0;
2029                 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
2030                         "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
2031                         NFS_SERVER(inode)->nfs_client->cl_hostname,
2032                         inode->i_sb->s_id, (long long)nfsi->fileid,
2033                         (long long)fattr->fileid);
2034                 goto out_err;
2035         }
2036
2037         /*
2038          * Make sure the inode's type hasn't changed.
2039          */
2040         if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
2041                 /*
2042                 * Big trouble! The inode has become a different object.
2043                 */
2044                 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
2045                                 __func__, inode->i_ino, inode->i_mode, fattr->mode);
2046                 goto out_err;
2047         }
2048
2049         /* Update the fsid? */
2050         if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
2051                         !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
2052                         !IS_AUTOMOUNT(inode))
2053                 server->fsid = fattr->fsid;
2054
2055         /* Save the delegation state before clearing cache_validity */
2056         have_delegation = nfs_have_delegated_attributes(inode);
2057
2058         /*
2059          * Update the read time so we don't revalidate too often.
2060          */
2061         nfsi->read_cache_jiffies = fattr->time_start;
2062
2063         save_cache_validity = nfsi->cache_validity;
2064         nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
2065                         | NFS_INO_INVALID_ATIME
2066                         | NFS_INO_REVAL_FORCED
2067                         | NFS_INO_INVALID_BLOCKS);
2068
2069         /* Do atomic weak cache consistency updates */
2070         nfs_wcc_update_inode(inode, fattr);
2071
2072         if (pnfs_layoutcommit_outstanding(inode)) {
2073                 nfsi->cache_validity |=
2074                         save_cache_validity &
2075                         (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
2076                          NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
2077                          NFS_INO_INVALID_BLOCKS);
2078                 cache_revalidated = false;
2079         }
2080
2081         /* More cache consistency checks */
2082         if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
2083                 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
2084                         /* Could it be a race with writeback? */
2085                         if (!(have_writers || have_delegation)) {
2086                                 invalid |= NFS_INO_INVALID_DATA
2087                                         | NFS_INO_INVALID_ACCESS
2088                                         | NFS_INO_INVALID_ACL
2089                                         | NFS_INO_INVALID_XATTR;
2090                                 /* Force revalidate of all attributes */
2091                                 save_cache_validity |= NFS_INO_INVALID_CTIME
2092                                         | NFS_INO_INVALID_MTIME
2093                                         | NFS_INO_INVALID_SIZE
2094                                         | NFS_INO_INVALID_BLOCKS
2095                                         | NFS_INO_INVALID_NLINK
2096                                         | NFS_INO_INVALID_MODE
2097                                         | NFS_INO_INVALID_OTHER;
2098                                 if (S_ISDIR(inode->i_mode))
2099                                         nfs_force_lookup_revalidate(inode);
2100                                 attr_changed = true;
2101                                 dprintk("NFS: change_attr change on server for file %s/%ld\n",
2102                                                 inode->i_sb->s_id,
2103                                                 inode->i_ino);
2104                         } else if (!have_delegation)
2105                                 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
2106                         inode_set_iversion_raw(inode, fattr->change_attr);
2107                 }
2108         } else {
2109                 nfsi->cache_validity |=
2110                         save_cache_validity & NFS_INO_INVALID_CHANGE;
2111                 if (!have_delegation ||
2112                     (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0)
2113                         cache_revalidated = false;
2114         }
2115
2116         if (fattr->valid & NFS_ATTR_FATTR_MTIME)
2117                 inode->i_mtime = fattr->mtime;
2118         else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
2119                 nfsi->cache_validity |=
2120                         save_cache_validity & NFS_INO_INVALID_MTIME;
2121
2122         if (fattr->valid & NFS_ATTR_FATTR_CTIME)
2123                 inode->i_ctime = fattr->ctime;
2124         else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
2125                 nfsi->cache_validity |=
2126                         save_cache_validity & NFS_INO_INVALID_CTIME;
2127
2128         /* Check if our cached file size is stale */
2129         if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
2130                 new_isize = nfs_size_to_loff_t(fattr->size);
2131                 cur_isize = i_size_read(inode);
2132                 if (new_isize != cur_isize && !have_delegation) {
2133                         /* Do we perhaps have any outstanding writes, or has
2134                          * the file grown beyond our last write? */
2135                         if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
2136                                 i_size_write(inode, new_isize);
2137                                 if (!have_writers)
2138                                         invalid |= NFS_INO_INVALID_DATA;
2139                         }
2140                         dprintk("NFS: isize change on server for file %s/%ld "
2141                                         "(%Ld to %Ld)\n",
2142                                         inode->i_sb->s_id,
2143                                         inode->i_ino,
2144                                         (long long)cur_isize,
2145                                         (long long)new_isize);
2146                 }
2147                 if (new_isize == 0 &&
2148                     !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED |
2149                                       NFS_ATTR_FATTR_BLOCKS_USED))) {
2150                         fattr->du.nfs3.used = 0;
2151                         fattr->valid |= NFS_ATTR_FATTR_SPACE_USED;
2152                 }
2153         } else
2154                 nfsi->cache_validity |=
2155                         save_cache_validity & NFS_INO_INVALID_SIZE;
2156
2157         if (fattr->valid & NFS_ATTR_FATTR_ATIME)
2158                 inode->i_atime = fattr->atime;
2159         else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
2160                 nfsi->cache_validity |=
2161                         save_cache_validity & NFS_INO_INVALID_ATIME;
2162
2163         if (fattr->valid & NFS_ATTR_FATTR_MODE) {
2164                 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
2165                         umode_t newmode = inode->i_mode & S_IFMT;
2166                         newmode |= fattr->mode & S_IALLUGO;
2167                         inode->i_mode = newmode;
2168                         invalid |= NFS_INO_INVALID_ACCESS
2169                                 | NFS_INO_INVALID_ACL;
2170                 }
2171         } else if (fattr_supported & NFS_ATTR_FATTR_MODE)
2172                 nfsi->cache_validity |=
2173                         save_cache_validity & NFS_INO_INVALID_MODE;
2174
2175         if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
2176                 if (!uid_eq(inode->i_uid, fattr->uid)) {
2177                         invalid |= NFS_INO_INVALID_ACCESS
2178                                 | NFS_INO_INVALID_ACL;
2179                         inode->i_uid = fattr->uid;
2180                 }
2181         } else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
2182                 nfsi->cache_validity |=
2183                         save_cache_validity & NFS_INO_INVALID_OTHER;
2184
2185         if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
2186                 if (!gid_eq(inode->i_gid, fattr->gid)) {
2187                         invalid |= NFS_INO_INVALID_ACCESS
2188                                 | NFS_INO_INVALID_ACL;
2189                         inode->i_gid = fattr->gid;
2190                 }
2191         } else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
2192                 nfsi->cache_validity |=
2193                         save_cache_validity & NFS_INO_INVALID_OTHER;
2194
2195         if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2196                 if (inode->i_nlink != fattr->nlink) {
2197                         if (S_ISDIR(inode->i_mode))
2198                                 invalid |= NFS_INO_INVALID_DATA;
2199                         set_nlink(inode, fattr->nlink);
2200                 }
2201         } else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
2202                 nfsi->cache_validity |=
2203                         save_cache_validity & NFS_INO_INVALID_NLINK;
2204
2205         if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
2206                 /*
2207                  * report the blocks in 512byte units
2208                  */
2209                 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
2210         } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED)
2211                 nfsi->cache_validity |=
2212                         save_cache_validity & NFS_INO_INVALID_BLOCKS;
2213
2214         if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
2215                 inode->i_blocks = fattr->du.nfs2.blocks;
2216         else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED)
2217                 nfsi->cache_validity |=
2218                         save_cache_validity & NFS_INO_INVALID_BLOCKS;
2219
2220         /* Update attrtimeo value if we're out of the unstable period */
2221         if (attr_changed) {
2222                 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
2223                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
2224                 nfsi->attrtimeo_timestamp = now;
2225                 /* Set barrier to be more recent than all outstanding updates */
2226                 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
2227         } else {
2228                 if (cache_revalidated) {
2229                         if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2230                                 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2231                                 nfsi->attrtimeo <<= 1;
2232                                 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2233                                         nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2234                         }
2235                         nfsi->attrtimeo_timestamp = now;
2236                 }
2237                 /* Set the barrier to be more recent than this fattr */
2238                 if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
2239                         nfsi->attr_gencount = fattr->gencount;
2240         }
2241
2242         /* Don't invalidate the data if we were to blame */
2243         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2244                                 || S_ISLNK(inode->i_mode)))
2245                 invalid &= ~NFS_INO_INVALID_DATA;
2246         nfs_set_cache_invalid(inode, invalid);
2247
2248         return 0;
2249  out_err:
2250         /*
2251          * No need to worry about unhashing the dentry, as the
2252          * lookup validation will know that the inode is bad.
2253          * (But we fall through to invalidate the caches.)
2254          */
2255         nfs_set_inode_stale_locked(inode);
2256         return -ESTALE;
2257 }
2258
2259 struct inode *nfs_alloc_inode(struct super_block *sb)
2260 {
2261         struct nfs_inode *nfsi;
2262         nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
2263         if (!nfsi)
2264                 return NULL;
2265         nfsi->flags = 0UL;
2266         nfsi->cache_validity = 0UL;
2267 #if IS_ENABLED(CONFIG_NFS_V4)
2268         nfsi->nfs4_acl = NULL;
2269 #endif /* CONFIG_NFS_V4 */
2270 #ifdef CONFIG_NFS_V4_2
2271         nfsi->xattr_cache = NULL;
2272 #endif
2273         return &nfsi->vfs_inode;
2274 }
2275 EXPORT_SYMBOL_GPL(nfs_alloc_inode);
2276
2277 void nfs_free_inode(struct inode *inode)
2278 {
2279         kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2280 }
2281 EXPORT_SYMBOL_GPL(nfs_free_inode);
2282
2283 static inline void nfs4_init_once(struct nfs_inode *nfsi)
2284 {
2285 #if IS_ENABLED(CONFIG_NFS_V4)
2286         INIT_LIST_HEAD(&nfsi->open_states);
2287         nfsi->delegation = NULL;
2288         init_rwsem(&nfsi->rwsem);
2289         nfsi->layout = NULL;
2290 #endif
2291 }
2292
2293 static void init_once(void *foo)
2294 {
2295         struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2296
2297         inode_init_once(&nfsi->vfs_inode);
2298         INIT_LIST_HEAD(&nfsi->open_files);
2299         INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2300         INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
2301         nfs4_init_once(nfsi);
2302 }
2303
2304 static int __init nfs_init_inodecache(void)
2305 {
2306         nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2307                                              sizeof(struct nfs_inode),
2308                                              0, (SLAB_RECLAIM_ACCOUNT|
2309                                                 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2310                                              init_once);
2311         if (nfs_inode_cachep == NULL)
2312                 return -ENOMEM;
2313
2314         return 0;
2315 }
2316
2317 static void nfs_destroy_inodecache(void)
2318 {
2319         /*
2320          * Make sure all delayed rcu free inodes are flushed before we
2321          * destroy cache.
2322          */
2323         rcu_barrier();
2324         kmem_cache_destroy(nfs_inode_cachep);
2325 }
2326
2327 struct workqueue_struct *nfsiod_workqueue;
2328 EXPORT_SYMBOL_GPL(nfsiod_workqueue);
2329
2330 /*
2331  * start up the nfsiod workqueue
2332  */
2333 static int nfsiod_start(void)
2334 {
2335         struct workqueue_struct *wq;
2336         dprintk("RPC:       creating workqueue nfsiod\n");
2337         wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
2338         if (wq == NULL)
2339                 return -ENOMEM;
2340         nfsiod_workqueue = wq;
2341         return 0;
2342 }
2343
2344 /*
2345  * Destroy the nfsiod workqueue
2346  */
2347 static void nfsiod_stop(void)
2348 {
2349         struct workqueue_struct *wq;
2350
2351         wq = nfsiod_workqueue;
2352         if (wq == NULL)
2353                 return;
2354         nfsiod_workqueue = NULL;
2355         destroy_workqueue(wq);
2356 }
2357
2358 unsigned int nfs_net_id;
2359 EXPORT_SYMBOL_GPL(nfs_net_id);
2360
2361 static int nfs_net_init(struct net *net)
2362 {
2363         nfs_clients_init(net);
2364         return nfs_fs_proc_net_init(net);
2365 }
2366
2367 static void nfs_net_exit(struct net *net)
2368 {
2369         nfs_fs_proc_net_exit(net);
2370         nfs_clients_exit(net);
2371 }
2372
2373 static struct pernet_operations nfs_net_ops = {
2374         .init = nfs_net_init,
2375         .exit = nfs_net_exit,
2376         .id   = &nfs_net_id,
2377         .size = sizeof(struct nfs_net),
2378 };
2379
2380 /*
2381  * Initialize NFS
2382  */
2383 static int __init init_nfs_fs(void)
2384 {
2385         int err;
2386
2387         err = nfs_sysfs_init();
2388         if (err < 0)
2389                 goto out10;
2390
2391         err = register_pernet_subsys(&nfs_net_ops);
2392         if (err < 0)
2393                 goto out9;
2394
2395         err = nfs_fscache_register();
2396         if (err < 0)
2397                 goto out8;
2398
2399         err = nfsiod_start();
2400         if (err)
2401                 goto out7;
2402
2403         err = nfs_fs_proc_init();
2404         if (err)
2405                 goto out6;
2406
2407         err = nfs_init_nfspagecache();
2408         if (err)
2409                 goto out5;
2410
2411         err = nfs_init_inodecache();
2412         if (err)
2413                 goto out4;
2414
2415         err = nfs_init_readpagecache();
2416         if (err)
2417                 goto out3;
2418
2419         err = nfs_init_writepagecache();
2420         if (err)
2421                 goto out2;
2422
2423         err = nfs_init_directcache();
2424         if (err)
2425                 goto out1;
2426
2427         rpc_proc_register(&init_net, &nfs_rpcstat);
2428
2429         err = register_nfs_fs();
2430         if (err)
2431                 goto out0;
2432
2433         return 0;
2434 out0:
2435         rpc_proc_unregister(&init_net, "nfs");
2436         nfs_destroy_directcache();
2437 out1:
2438         nfs_destroy_writepagecache();
2439 out2:
2440         nfs_destroy_readpagecache();
2441 out3:
2442         nfs_destroy_inodecache();
2443 out4:
2444         nfs_destroy_nfspagecache();
2445 out5:
2446         nfs_fs_proc_exit();
2447 out6:
2448         nfsiod_stop();
2449 out7:
2450         nfs_fscache_unregister();
2451 out8:
2452         unregister_pernet_subsys(&nfs_net_ops);
2453 out9:
2454         nfs_sysfs_exit();
2455 out10:
2456         return err;
2457 }
2458
2459 static void __exit exit_nfs_fs(void)
2460 {
2461         nfs_destroy_directcache();
2462         nfs_destroy_writepagecache();
2463         nfs_destroy_readpagecache();
2464         nfs_destroy_inodecache();
2465         nfs_destroy_nfspagecache();
2466         nfs_fscache_unregister();
2467         unregister_pernet_subsys(&nfs_net_ops);
2468         rpc_proc_unregister(&init_net, "nfs");
2469         unregister_nfs_fs();
2470         nfs_fs_proc_exit();
2471         nfsiod_stop();
2472         nfs_sysfs_exit();
2473 }
2474
2475 /* Not quite true; I just maintain it */
2476 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2477 MODULE_LICENSE("GPL");
2478 module_param(enable_ino64, bool, 0644);
2479
2480 module_init(init_nfs_fs)
2481 module_exit(exit_nfs_fs)