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