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