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