xref: /linux/fs/nfs/super.c (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/nfs/super.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  *
7  *  nfs 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  *  Split from inode.c by David Howells <dhowells@redhat.com>
16  *
17  * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
18  *   particular server are held in the same superblock
19  * - NFS superblocks can have several effective roots to the dentry tree
20  * - directory type roots are spliced into the tree when a path from one root reaches the root
21  *   of another (see nfs_lookup())
22  */
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 
27 #include <linux/time.h>
28 #include <linux/kernel.h>
29 #include <linux/mm.h>
30 #include <linux/string.h>
31 #include <linux/stat.h>
32 #include <linux/errno.h>
33 #include <linux/unistd.h>
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/addr.h>
36 #include <linux/sunrpc/stats.h>
37 #include <linux/sunrpc/metrics.h>
38 #include <linux/sunrpc/xprtsock.h>
39 #include <linux/sunrpc/xprtrdma.h>
40 #include <linux/nfs_fs.h>
41 #include <linux/nfs_mount.h>
42 #include <linux/nfs4_mount.h>
43 #include <linux/lockd/bind.h>
44 #include <linux/seq_file.h>
45 #include <linux/mount.h>
46 #include <linux/namei.h>
47 #include <linux/vfs.h>
48 #include <linux/inet.h>
49 #include <linux/in6.h>
50 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <net/ipv6.h>
53 #include <linux/netdevice.h>
54 #include <linux/nfs_xdr.h>
55 #include <linux/magic.h>
56 #include <linux/parser.h>
57 #include <linux/nsproxy.h>
58 #include <linux/rcupdate.h>
59 
60 #include <linux/uaccess.h>
61 #include <linux/nfs_ssc.h>
62 
63 #include <uapi/linux/tls.h>
64 
65 #include "nfs4_fs.h"
66 #include "callback.h"
67 #include "delegation.h"
68 #include "iostat.h"
69 #include "internal.h"
70 #include "fscache.h"
71 #include "nfs4session.h"
72 #include "pnfs.h"
73 #include "nfs.h"
74 #include "netns.h"
75 #include "sysfs.h"
76 
77 #define NFSDBG_FACILITY		NFSDBG_VFS
78 
79 const struct super_operations nfs_sops = {
80 	.alloc_inode	= nfs_alloc_inode,
81 	.free_inode	= nfs_free_inode,
82 	.write_inode	= nfs_write_inode,
83 	.drop_inode	= nfs_drop_inode,
84 	.statfs		= nfs_statfs,
85 	.evict_inode	= nfs_evict_inode,
86 	.umount_begin	= nfs_umount_begin,
87 	.show_options	= nfs_show_options,
88 	.show_devname	= nfs_show_devname,
89 	.show_path	= nfs_show_path,
90 	.show_stats	= nfs_show_stats,
91 };
92 EXPORT_SYMBOL_GPL(nfs_sops);
93 
94 #ifdef CONFIG_NFS_V4_2
95 static const struct nfs_ssc_client_ops nfs_ssc_clnt_ops_tbl = {
96 	.sco_sb_deactive = nfs_sb_deactive,
97 };
98 #endif
99 
100 #if IS_ENABLED(CONFIG_NFS_V4)
101 static int __init register_nfs4_fs(void)
102 {
103 	return register_filesystem(&nfs4_fs_type);
104 }
105 
106 static void unregister_nfs4_fs(void)
107 {
108 	unregister_filesystem(&nfs4_fs_type);
109 }
110 #else
111 static int __init register_nfs4_fs(void)
112 {
113 	return 0;
114 }
115 
116 static void unregister_nfs4_fs(void)
117 {
118 }
119 #endif
120 
121 #ifdef CONFIG_NFS_V4_2
122 static void nfs_ssc_register_ops(void)
123 {
124 	nfs_ssc_register(&nfs_ssc_clnt_ops_tbl);
125 }
126 
127 static void nfs_ssc_unregister_ops(void)
128 {
129 	nfs_ssc_unregister(&nfs_ssc_clnt_ops_tbl);
130 }
131 #endif /* CONFIG_NFS_V4_2 */
132 
133 static struct shrinker *acl_shrinker;
134 
135 /*
136  * Register the NFS filesystems
137  */
138 int __init register_nfs_fs(void)
139 {
140 	int ret;
141 
142         ret = register_filesystem(&nfs_fs_type);
143 	if (ret < 0)
144 		goto error_0;
145 
146 	ret = register_nfs4_fs();
147 	if (ret < 0)
148 		goto error_1;
149 
150 	ret = nfs_register_sysctl();
151 	if (ret < 0)
152 		goto error_2;
153 
154 	acl_shrinker = shrinker_alloc(0, "nfs-acl");
155 	if (!acl_shrinker) {
156 		ret = -ENOMEM;
157 		goto error_3;
158 	}
159 
160 	acl_shrinker->count_objects = nfs_access_cache_count;
161 	acl_shrinker->scan_objects = nfs_access_cache_scan;
162 
163 	shrinker_register(acl_shrinker);
164 
165 #ifdef CONFIG_NFS_V4_2
166 	nfs_ssc_register_ops();
167 #endif
168 	return 0;
169 error_3:
170 	nfs_unregister_sysctl();
171 error_2:
172 	unregister_nfs4_fs();
173 error_1:
174 	unregister_filesystem(&nfs_fs_type);
175 error_0:
176 	return ret;
177 }
178 
179 /*
180  * Unregister the NFS filesystems
181  */
182 void __exit unregister_nfs_fs(void)
183 {
184 	shrinker_free(acl_shrinker);
185 	nfs_unregister_sysctl();
186 	unregister_nfs4_fs();
187 #ifdef CONFIG_NFS_V4_2
188 	nfs_ssc_unregister_ops();
189 #endif
190 	unregister_filesystem(&nfs_fs_type);
191 }
192 
193 bool nfs_sb_active(struct super_block *sb)
194 {
195 	struct nfs_server *server = NFS_SB(sb);
196 
197 	if (!atomic_inc_not_zero(&sb->s_active))
198 		return false;
199 	if (atomic_inc_return(&server->active) != 1)
200 		atomic_dec(&sb->s_active);
201 	return true;
202 }
203 EXPORT_SYMBOL_GPL(nfs_sb_active);
204 
205 void nfs_sb_deactive(struct super_block *sb)
206 {
207 	struct nfs_server *server = NFS_SB(sb);
208 
209 	if (atomic_dec_and_test(&server->active))
210 		deactivate_super(sb);
211 }
212 EXPORT_SYMBOL_GPL(nfs_sb_deactive);
213 
214 static int __nfs_list_for_each_server(struct list_head *head,
215 		int (*fn)(struct nfs_server *, void *),
216 		void *data)
217 {
218 	struct nfs_server *server, *last = NULL;
219 	int ret = 0;
220 
221 	rcu_read_lock();
222 	list_for_each_entry_rcu(server, head, client_link) {
223 		if (!(server->super && nfs_sb_active(server->super)))
224 			continue;
225 		rcu_read_unlock();
226 		if (last)
227 			nfs_sb_deactive(last->super);
228 		last = server;
229 		ret = fn(server, data);
230 		if (ret)
231 			goto out;
232 		cond_resched();
233 		rcu_read_lock();
234 	}
235 	rcu_read_unlock();
236 out:
237 	if (last)
238 		nfs_sb_deactive(last->super);
239 	return ret;
240 }
241 
242 int nfs_client_for_each_server(struct nfs_client *clp,
243 		int (*fn)(struct nfs_server *, void *),
244 		void *data)
245 {
246 	return __nfs_list_for_each_server(&clp->cl_superblocks, fn, data);
247 }
248 EXPORT_SYMBOL_GPL(nfs_client_for_each_server);
249 
250 /*
251  * Deliver file system statistics to userspace
252  */
253 int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
254 {
255 	struct nfs_server *server = NFS_SB(dentry->d_sb);
256 	unsigned char blockbits;
257 	unsigned long blockres;
258 	struct nfs_fh *fh = NFS_FH(d_inode(dentry));
259 	struct nfs_fsstat res;
260 	int error = -ENOMEM;
261 
262 	res.fattr = nfs_alloc_fattr();
263 	if (res.fattr == NULL)
264 		goto out_err;
265 
266 	error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
267 	if (unlikely(error == -ESTALE)) {
268 		struct dentry *pd_dentry;
269 
270 		pd_dentry = dget_parent(dentry);
271 		nfs_zap_caches(d_inode(pd_dentry));
272 		dput(pd_dentry);
273 	}
274 	nfs_free_fattr(res.fattr);
275 	if (error < 0)
276 		goto out_err;
277 
278 	buf->f_type = NFS_SUPER_MAGIC;
279 
280 	/*
281 	 * Current versions of glibc do not correctly handle the
282 	 * case where f_frsize != f_bsize.  Eventually we want to
283 	 * report the value of wtmult in this field.
284 	 */
285 	buf->f_frsize = dentry->d_sb->s_blocksize;
286 
287 	/*
288 	 * On most *nix systems, f_blocks, f_bfree, and f_bavail
289 	 * are reported in units of f_frsize.  Linux hasn't had
290 	 * an f_frsize field in its statfs struct until recently,
291 	 * thus historically Linux's sys_statfs reports these
292 	 * fields in units of f_bsize.
293 	 */
294 	buf->f_bsize = dentry->d_sb->s_blocksize;
295 	blockbits = dentry->d_sb->s_blocksize_bits;
296 	blockres = (1 << blockbits) - 1;
297 	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
298 	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
299 	buf->f_bavail = (res.abytes + blockres) >> blockbits;
300 
301 	buf->f_files = res.tfiles;
302 	buf->f_ffree = res.afiles;
303 
304 	buf->f_namelen = server->namelen;
305 
306 	return 0;
307 
308  out_err:
309 	dprintk("%s: statfs error = %d\n", __func__, -error);
310 	return error;
311 }
312 EXPORT_SYMBOL_GPL(nfs_statfs);
313 
314 /*
315  * Map the security flavour number to a name
316  */
317 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
318 {
319 	static const struct {
320 		rpc_authflavor_t flavour;
321 		const char *str;
322 	} sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
323 		/* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
324 		{ RPC_AUTH_NULL, "null" },
325 		{ RPC_AUTH_UNIX, "sys" },
326 		{ RPC_AUTH_GSS_KRB5, "krb5" },
327 		{ RPC_AUTH_GSS_KRB5I, "krb5i" },
328 		{ RPC_AUTH_GSS_KRB5P, "krb5p" },
329 		{ RPC_AUTH_GSS_LKEY, "lkey" },
330 		{ RPC_AUTH_GSS_LKEYI, "lkeyi" },
331 		{ RPC_AUTH_GSS_LKEYP, "lkeyp" },
332 		{ RPC_AUTH_GSS_SPKM, "spkm" },
333 		{ RPC_AUTH_GSS_SPKMI, "spkmi" },
334 		{ RPC_AUTH_GSS_SPKMP, "spkmp" },
335 		{ UINT_MAX, "unknown" }
336 	};
337 	int i;
338 
339 	for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
340 		if (sec_flavours[i].flavour == flavour)
341 			break;
342 	}
343 	return sec_flavours[i].str;
344 }
345 
346 static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
347 				  int showdefaults)
348 {
349 	struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
350 	char *proto = NULL;
351 
352 	switch (sap->sa_family) {
353 	case AF_INET:
354 		switch (nfss->mountd_protocol) {
355 		case IPPROTO_UDP:
356 			proto = RPCBIND_NETID_UDP;
357 			break;
358 		case IPPROTO_TCP:
359 			proto = RPCBIND_NETID_TCP;
360 			break;
361 		}
362 		break;
363 	case AF_INET6:
364 		switch (nfss->mountd_protocol) {
365 		case IPPROTO_UDP:
366 			proto = RPCBIND_NETID_UDP6;
367 			break;
368 		case IPPROTO_TCP:
369 			proto = RPCBIND_NETID_TCP6;
370 			break;
371 		}
372 		break;
373 	}
374 	if (proto || showdefaults)
375 		seq_printf(m, ",mountproto=%s", proto ?: "auto");
376 }
377 
378 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
379 				    int showdefaults)
380 {
381 	struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
382 
383 	if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
384 		return;
385 
386 	switch (sap->sa_family) {
387 	case AF_INET: {
388 		struct sockaddr_in *sin = (struct sockaddr_in *)sap;
389 		seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
390 		break;
391 	}
392 	case AF_INET6: {
393 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
394 		seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
395 		break;
396 	}
397 	default:
398 		if (showdefaults)
399 			seq_puts(m, ",mountaddr=unspecified");
400 	}
401 
402 	if (nfss->mountd_version || showdefaults)
403 		seq_printf(m, ",mountvers=%u", nfss->mountd_version);
404 	if ((nfss->mountd_port &&
405 		nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
406 		showdefaults)
407 		seq_printf(m, ",mountport=%u", nfss->mountd_port);
408 
409 	nfs_show_mountd_netid(m, nfss, showdefaults);
410 }
411 
412 #if IS_ENABLED(CONFIG_NFS_V4)
413 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
414 				    int showdefaults)
415 {
416 	struct nfs_client *clp = nfss->nfs_client;
417 
418 	seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
419 }
420 #else
421 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
422 				    int showdefaults)
423 {
424 }
425 #endif
426 
427 static void nfs_show_nfs_version(struct seq_file *m,
428 		unsigned int version,
429 		unsigned int minorversion)
430 {
431 	seq_printf(m, ",vers=%u", version);
432 	if (version == 4)
433 		seq_printf(m, ".%u", minorversion);
434 }
435 
436 /*
437  * Describe the mount options in force on this server representation
438  */
439 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
440 				   int showdefaults)
441 {
442 	static const struct proc_nfs_info {
443 		int flag;
444 		const char *str;
445 		const char *nostr;
446 	} nfs_info[] = {
447 		{ NFS_MOUNT_SOFT, ",soft", "" },
448 		{ NFS_MOUNT_SOFTERR, ",softerr", "" },
449 		{ NFS_MOUNT_SOFTREVAL, ",softreval", "" },
450 		{ NFS_MOUNT_POSIX, ",posix", "" },
451 		{ NFS_MOUNT_NOCTO, ",nocto", "" },
452 		{ NFS_MOUNT_NOAC, ",noac", "" },
453 		{ NFS_MOUNT_NONLM, ",nolock", "" },
454 		{ NFS_MOUNT_NOACL, ",noacl", "" },
455 		{ NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
456 		{ NFS_MOUNT_UNSHARED, ",nosharecache", "" },
457 		{ NFS_MOUNT_NORESVPORT, ",noresvport", "" },
458 		{ 0, NULL, NULL }
459 	};
460 	const struct proc_nfs_info *nfs_infop;
461 	struct nfs_client *clp = nfss->nfs_client;
462 	u32 version = clp->rpc_ops->version;
463 	int local_flock, local_fcntl;
464 
465 	nfs_show_nfs_version(m, version, clp->cl_minorversion);
466 	seq_printf(m, ",rsize=%u", nfss->rsize);
467 	seq_printf(m, ",wsize=%u", nfss->wsize);
468 	if (nfss->bsize != 0)
469 		seq_printf(m, ",bsize=%u", nfss->bsize);
470 	seq_printf(m, ",namlen=%u", nfss->namelen);
471 	if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
472 		seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
473 	if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
474 		seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
475 	if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
476 		seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
477 	if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
478 		seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
479 	if (!(nfss->flags & (NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR)))
480 			seq_puts(m, ",hard");
481 	for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
482 		if (nfss->flags & nfs_infop->flag)
483 			seq_puts(m, nfs_infop->str);
484 		else
485 			seq_puts(m, nfs_infop->nostr);
486 	}
487 	rcu_read_lock();
488 	seq_printf(m, ",proto=%s",
489 		   rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
490 	rcu_read_unlock();
491 	if (clp->cl_nconnect > 0)
492 		seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
493 	if (version == 4) {
494 		if (clp->cl_max_connect > 1)
495 			seq_printf(m, ",max_connect=%u", clp->cl_max_connect);
496 		if (nfss->port != NFS_PORT)
497 			seq_printf(m, ",port=%u", nfss->port);
498 	} else
499 		if (nfss->port)
500 			seq_printf(m, ",port=%u", nfss->port);
501 
502 	seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
503 	seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
504 	seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
505 	switch (clp->cl_xprtsec.policy) {
506 	case RPC_XPRTSEC_TLS_ANON:
507 		seq_puts(m, ",xprtsec=tls");
508 		break;
509 	case RPC_XPRTSEC_TLS_X509:
510 		seq_puts(m, ",xprtsec=mtls");
511 		break;
512 	default:
513 		break;
514 	}
515 
516 	if (version != 4)
517 		nfs_show_mountd_options(m, nfss, showdefaults);
518 	else
519 		nfs_show_nfsv4_options(m, nfss, showdefaults);
520 
521 	if (nfss->options & NFS_OPTION_FSCACHE) {
522 #ifdef CONFIG_NFS_FSCACHE
523 		if (nfss->fscache_uniq)
524 			seq_printf(m, ",fsc=%s", nfss->fscache_uniq);
525 		else
526 			seq_puts(m, ",fsc");
527 #else
528 		seq_puts(m, ",fsc");
529 #endif
530 	}
531 
532 	if (nfss->options & NFS_OPTION_MIGRATION)
533 		seq_puts(m, ",migration");
534 
535 	if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
536 		if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
537 			seq_puts(m, ",lookupcache=none");
538 		else
539 			seq_puts(m, ",lookupcache=pos");
540 	}
541 
542 	local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
543 	local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
544 
545 	if (!local_flock && !local_fcntl)
546 		seq_puts(m, ",local_lock=none");
547 	else if (local_flock && local_fcntl)
548 		seq_puts(m, ",local_lock=all");
549 	else if (local_flock)
550 		seq_puts(m, ",local_lock=flock");
551 	else
552 		seq_puts(m, ",local_lock=posix");
553 
554 	if (nfss->flags & NFS_MOUNT_WRITE_EAGER) {
555 		if (nfss->flags & NFS_MOUNT_WRITE_WAIT)
556 			seq_puts(m, ",write=wait");
557 		else
558 			seq_puts(m, ",write=eager");
559 	}
560 }
561 
562 /*
563  * Describe the mount options on this VFS mountpoint
564  */
565 int nfs_show_options(struct seq_file *m, struct dentry *root)
566 {
567 	struct nfs_server *nfss = NFS_SB(root->d_sb);
568 
569 	nfs_show_mount_options(m, nfss, 0);
570 
571 	rcu_read_lock();
572 	seq_printf(m, ",addr=%s",
573 			rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
574 							RPC_DISPLAY_ADDR));
575 	rcu_read_unlock();
576 
577 	return 0;
578 }
579 EXPORT_SYMBOL_GPL(nfs_show_options);
580 
581 #if IS_ENABLED(CONFIG_NFS_V4)
582 static void show_lease(struct seq_file *m, struct nfs_server *server)
583 {
584 	struct nfs_client *clp = server->nfs_client;
585 	unsigned long expire;
586 
587 	seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
588 	expire = clp->cl_last_renewal + clp->cl_lease_time;
589 	seq_printf(m, ",lease_expired=%ld",
590 		   time_after(expire, jiffies) ?  0 : (jiffies - expire) / HZ);
591 }
592 #ifdef CONFIG_NFS_V4_1
593 static void show_sessions(struct seq_file *m, struct nfs_server *server)
594 {
595 	if (nfs4_has_session(server->nfs_client))
596 		seq_puts(m, ",sessions");
597 }
598 #else
599 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
600 #endif
601 #endif
602 
603 #ifdef CONFIG_NFS_V4_1
604 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
605 {
606 	seq_printf(m, ",pnfs=");
607 	if (server->pnfs_curr_ld)
608 		seq_printf(m, "%s", server->pnfs_curr_ld->name);
609 	else
610 		seq_printf(m, "not configured");
611 }
612 
613 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
614 {
615 	if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
616 		struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
617 		seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
618 			   "date='%llu,%u'",
619 			   impl_id->name, impl_id->domain,
620 			   impl_id->date.seconds, impl_id->date.nseconds);
621 	}
622 }
623 #else
624 #if IS_ENABLED(CONFIG_NFS_V4)
625 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
626 {
627 }
628 #endif
629 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
630 {
631 }
632 #endif
633 
634 int nfs_show_devname(struct seq_file *m, struct dentry *root)
635 {
636 	char *page = (char *) __get_free_page(GFP_KERNEL);
637 	char *devname, *dummy;
638 	int err = 0;
639 	if (!page)
640 		return -ENOMEM;
641 	devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
642 	if (IS_ERR(devname))
643 		err = PTR_ERR(devname);
644 	else
645 		seq_escape(m, devname, " \t\n\\");
646 	free_page((unsigned long)page);
647 	return err;
648 }
649 EXPORT_SYMBOL_GPL(nfs_show_devname);
650 
651 int nfs_show_path(struct seq_file *m, struct dentry *dentry)
652 {
653 	seq_puts(m, "/");
654 	return 0;
655 }
656 EXPORT_SYMBOL_GPL(nfs_show_path);
657 
658 /*
659  * Present statistical information for this VFS mountpoint
660  */
661 int nfs_show_stats(struct seq_file *m, struct dentry *root)
662 {
663 	int i, cpu;
664 	struct nfs_server *nfss = NFS_SB(root->d_sb);
665 	struct rpc_auth *auth = nfss->client->cl_auth;
666 	struct nfs_iostats totals = { };
667 
668 	seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
669 
670 	/*
671 	 * Display all mount option settings
672 	 */
673 	seq_puts(m, "\n\topts:\t");
674 	seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
675 	seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
676 	seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
677 	seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
678 	nfs_show_mount_options(m, nfss, 1);
679 
680 	seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
681 
682 	show_implementation_id(m, nfss);
683 
684 	seq_puts(m, "\n\tcaps:\t");
685 	seq_printf(m, "caps=0x%x", nfss->caps);
686 	seq_printf(m, ",wtmult=%u", nfss->wtmult);
687 	seq_printf(m, ",dtsize=%u", nfss->dtsize);
688 	seq_printf(m, ",bsize=%u", nfss->bsize);
689 	seq_printf(m, ",namlen=%u", nfss->namelen);
690 
691 #if IS_ENABLED(CONFIG_NFS_V4)
692 	if (nfss->nfs_client->rpc_ops->version == 4) {
693 		seq_puts(m, "\n\tnfsv4:\t");
694 		seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
695 		seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
696 		seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
697 		seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
698 		show_sessions(m, nfss);
699 		show_pnfs(m, nfss);
700 		show_lease(m, nfss);
701 	}
702 #endif
703 
704 	/*
705 	 * Display security flavor in effect for this mount
706 	 */
707 	seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
708 	if (auth->au_flavor)
709 		seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
710 
711 	/*
712 	 * Display superblock I/O counters
713 	 */
714 	for_each_possible_cpu(cpu) {
715 		struct nfs_iostats *stats;
716 
717 		preempt_disable();
718 		stats = per_cpu_ptr(nfss->io_stats, cpu);
719 
720 		for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
721 			totals.events[i] += stats->events[i];
722 		for (i = 0; i < __NFSIOS_BYTESMAX; i++)
723 			totals.bytes[i] += stats->bytes[i];
724 
725 		preempt_enable();
726 	}
727 
728 	seq_puts(m, "\n\tevents:\t");
729 	for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
730 		seq_printf(m, "%lu ", totals.events[i]);
731 	seq_puts(m, "\n\tbytes:\t");
732 	for (i = 0; i < __NFSIOS_BYTESMAX; i++)
733 		seq_printf(m, "%Lu ", totals.bytes[i]);
734 	seq_putc(m, '\n');
735 
736 	rpc_clnt_show_stats(m, nfss->client);
737 
738 	return 0;
739 }
740 EXPORT_SYMBOL_GPL(nfs_show_stats);
741 
742 /*
743  * Begin unmount by attempting to remove all automounted mountpoints we added
744  * in response to xdev traversals and referrals
745  */
746 void nfs_umount_begin(struct super_block *sb)
747 {
748 	struct nfs_server *server;
749 	struct rpc_clnt *rpc;
750 
751 	server = NFS_SB(sb);
752 	/* -EIO all pending I/O */
753 	rpc = server->client_acl;
754 	if (!IS_ERR(rpc))
755 		rpc_killall_tasks(rpc);
756 	rpc = server->client;
757 	if (!IS_ERR(rpc))
758 		rpc_killall_tasks(rpc);
759 }
760 EXPORT_SYMBOL_GPL(nfs_umount_begin);
761 
762 /*
763  * Return true if 'match' is in auth_info or auth_info is empty.
764  * Return false otherwise.
765  */
766 bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
767 			 rpc_authflavor_t match)
768 {
769 	int i;
770 
771 	if (!auth_info->flavor_len)
772 		return true;
773 
774 	for (i = 0; i < auth_info->flavor_len; i++) {
775 		if (auth_info->flavors[i] == match)
776 			return true;
777 	}
778 	return false;
779 }
780 EXPORT_SYMBOL_GPL(nfs_auth_info_match);
781 
782 /*
783  * Ensure that a specified authtype in ctx->auth_info is supported by
784  * the server. Returns 0 and sets ctx->selected_flavor if it's ok, and
785  * -EACCES if not.
786  */
787 static int nfs_verify_authflavors(struct nfs_fs_context *ctx,
788 				  rpc_authflavor_t *server_authlist,
789 				  unsigned int count)
790 {
791 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
792 	bool found_auth_null = false;
793 	unsigned int i;
794 
795 	/*
796 	 * If the sec= mount option is used, the specified flavor or AUTH_NULL
797 	 * must be in the list returned by the server.
798 	 *
799 	 * AUTH_NULL has a special meaning when it's in the server list - it
800 	 * means that the server will ignore the rpc creds, so any flavor
801 	 * can be used but still use the sec= that was specified.
802 	 *
803 	 * Note also that the MNT procedure in MNTv1 does not return a list
804 	 * of supported security flavors. In this case, nfs_mount() fabricates
805 	 * a security flavor list containing just AUTH_NULL.
806 	 */
807 	for (i = 0; i < count; i++) {
808 		flavor = server_authlist[i];
809 
810 		if (nfs_auth_info_match(&ctx->auth_info, flavor))
811 			goto out;
812 
813 		if (flavor == RPC_AUTH_NULL)
814 			found_auth_null = true;
815 	}
816 
817 	if (found_auth_null) {
818 		flavor = ctx->auth_info.flavors[0];
819 		goto out;
820 	}
821 
822 	dfprintk(MOUNT,
823 		 "NFS: specified auth flavors not supported by server\n");
824 	return -EACCES;
825 
826 out:
827 	ctx->selected_flavor = flavor;
828 	dfprintk(MOUNT, "NFS: using auth flavor %u\n", ctx->selected_flavor);
829 	return 0;
830 }
831 
832 /*
833  * Use the remote server's MOUNT service to request the NFS file handle
834  * corresponding to the provided path.
835  */
836 static int nfs_request_mount(struct fs_context *fc,
837 			     struct nfs_fh *root_fh,
838 			     rpc_authflavor_t *server_authlist,
839 			     unsigned int *server_authlist_len)
840 {
841 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
842 	struct nfs_mount_request request = {
843 		.sap		= &ctx->mount_server._address,
844 		.dirpath	= ctx->nfs_server.export_path,
845 		.protocol	= ctx->mount_server.protocol,
846 		.fh		= root_fh,
847 		.noresvport	= ctx->flags & NFS_MOUNT_NORESVPORT,
848 		.auth_flav_len	= server_authlist_len,
849 		.auth_flavs	= server_authlist,
850 		.net		= fc->net_ns,
851 	};
852 	int status;
853 
854 	if (ctx->mount_server.version == 0) {
855 		switch (ctx->version) {
856 			default:
857 				ctx->mount_server.version = NFS_MNT3_VERSION;
858 				break;
859 			case 2:
860 				ctx->mount_server.version = NFS_MNT_VERSION;
861 		}
862 	}
863 	request.version = ctx->mount_server.version;
864 
865 	if (ctx->mount_server.hostname)
866 		request.hostname = ctx->mount_server.hostname;
867 	else
868 		request.hostname = ctx->nfs_server.hostname;
869 
870 	/*
871 	 * Construct the mount server's address.
872 	 */
873 	if (ctx->mount_server.address.sa_family == AF_UNSPEC) {
874 		memcpy(request.sap, &ctx->nfs_server._address,
875 		       ctx->nfs_server.addrlen);
876 		ctx->mount_server.addrlen = ctx->nfs_server.addrlen;
877 	}
878 	request.salen = ctx->mount_server.addrlen;
879 	nfs_set_port(request.sap, &ctx->mount_server.port, 0);
880 
881 	/*
882 	 * Now ask the mount server to map our export path
883 	 * to a file handle.
884 	 */
885 	status = nfs_mount(&request, ctx->timeo, ctx->retrans);
886 	if (status != 0) {
887 		dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
888 				request.hostname, status);
889 		return status;
890 	}
891 
892 	return 0;
893 }
894 
895 static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
896 {
897 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
898 	int status;
899 	unsigned int i;
900 	bool tried_auth_unix = false;
901 	bool auth_null_in_list = false;
902 	struct nfs_server *server = ERR_PTR(-EACCES);
903 	rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
904 	unsigned int authlist_len = ARRAY_SIZE(authlist);
905 
906 	/* make sure 'nolock'/'lock' override the 'local_lock' mount option */
907 	if (ctx->lock_status) {
908 		if (ctx->lock_status == NFS_LOCK_NOLOCK) {
909 			ctx->flags |= NFS_MOUNT_NONLM;
910 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
911 		} else {
912 			ctx->flags &= ~NFS_MOUNT_NONLM;
913 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
914 		}
915 	}
916 	status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
917 	if (status)
918 		return ERR_PTR(status);
919 
920 	/*
921 	 * Was a sec= authflavor specified in the options? First, verify
922 	 * whether the server supports it, and then just try to use it if so.
923 	 */
924 	if (ctx->auth_info.flavor_len > 0) {
925 		status = nfs_verify_authflavors(ctx, authlist, authlist_len);
926 		dfprintk(MOUNT, "NFS: using auth flavor %u\n",
927 			 ctx->selected_flavor);
928 		if (status)
929 			return ERR_PTR(status);
930 		return ctx->nfs_mod->rpc_ops->create_server(fc);
931 	}
932 
933 	/*
934 	 * No sec= option was provided. RFC 2623, section 2.7 suggests we
935 	 * SHOULD prefer the flavor listed first. However, some servers list
936 	 * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
937 	 */
938 	for (i = 0; i < authlist_len; ++i) {
939 		rpc_authflavor_t flavor;
940 		struct rpcsec_gss_info info;
941 
942 		flavor = authlist[i];
943 		switch (flavor) {
944 		case RPC_AUTH_UNIX:
945 			tried_auth_unix = true;
946 			break;
947 		case RPC_AUTH_NULL:
948 			auth_null_in_list = true;
949 			continue;
950 		default:
951 			if (rpcauth_get_gssinfo(flavor, &info) != 0)
952 				continue;
953 			break;
954 		}
955 		dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
956 		ctx->selected_flavor = flavor;
957 		server = ctx->nfs_mod->rpc_ops->create_server(fc);
958 		if (!IS_ERR(server))
959 			return server;
960 	}
961 
962 	/*
963 	 * Nothing we tried so far worked. At this point, give up if we've
964 	 * already tried AUTH_UNIX or if the server's list doesn't contain
965 	 * AUTH_NULL
966 	 */
967 	if (tried_auth_unix || !auth_null_in_list)
968 		return server;
969 
970 	/* Last chance! Try AUTH_UNIX */
971 	dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
972 	ctx->selected_flavor = RPC_AUTH_UNIX;
973 	return ctx->nfs_mod->rpc_ops->create_server(fc);
974 }
975 
976 int nfs_try_get_tree(struct fs_context *fc)
977 {
978 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
979 
980 	if (ctx->need_mount)
981 		ctx->server = nfs_try_mount_request(fc);
982 	else
983 		ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
984 
985 	return nfs_get_tree_common(fc);
986 }
987 EXPORT_SYMBOL_GPL(nfs_try_get_tree);
988 
989 
990 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
991 		| NFS_MOUNT_SECURE \
992 		| NFS_MOUNT_TCP \
993 		| NFS_MOUNT_VER3 \
994 		| NFS_MOUNT_KERBEROS \
995 		| NFS_MOUNT_NONLM \
996 		| NFS_MOUNT_BROKEN_SUID \
997 		| NFS_MOUNT_STRICTLOCK \
998 		| NFS_MOUNT_LEGACY_INTERFACE)
999 
1000 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
1001 		~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
1002 
1003 static int
1004 nfs_compare_remount_data(struct nfs_server *nfss,
1005 			 struct nfs_fs_context *ctx)
1006 {
1007 	if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
1008 	    ctx->rsize != nfss->rsize ||
1009 	    ctx->wsize != nfss->wsize ||
1010 	    ctx->version != nfss->nfs_client->rpc_ops->version ||
1011 	    ctx->minorversion != nfss->nfs_client->cl_minorversion ||
1012 	    ctx->retrans != nfss->client->cl_timeout->to_retries ||
1013 	    !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
1014 	    ctx->acregmin != nfss->acregmin / HZ ||
1015 	    ctx->acregmax != nfss->acregmax / HZ ||
1016 	    ctx->acdirmin != nfss->acdirmin / HZ ||
1017 	    ctx->acdirmax != nfss->acdirmax / HZ ||
1018 	    ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1019 	    (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
1020 	    ctx->nfs_server.port != nfss->port ||
1021 	    ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1022 	    !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
1023 			  (struct sockaddr *)&nfss->nfs_client->cl_addr))
1024 		return -EINVAL;
1025 
1026 	return 0;
1027 }
1028 
1029 int nfs_reconfigure(struct fs_context *fc)
1030 {
1031 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1032 	struct super_block *sb = fc->root->d_sb;
1033 	struct nfs_server *nfss = sb->s_fs_info;
1034 	int ret;
1035 
1036 	sync_filesystem(sb);
1037 
1038 	/*
1039 	 * Userspace mount programs that send binary options generally send
1040 	 * them populated with default values. We have no way to know which
1041 	 * ones were explicitly specified. Fall back to legacy behavior and
1042 	 * just return success.
1043 	 */
1044 	if (ctx->skip_reconfig_option_check)
1045 		return 0;
1046 
1047 	/*
1048 	 * noac is a special case. It implies -o sync, but that's not
1049 	 * necessarily reflected in the mtab options. reconfigure_super
1050 	 * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
1051 	 * remount options, so we have to explicitly reset it.
1052 	 */
1053 	if (ctx->flags & NFS_MOUNT_NOAC) {
1054 		fc->sb_flags |= SB_SYNCHRONOUS;
1055 		fc->sb_flags_mask |= SB_SYNCHRONOUS;
1056 	}
1057 
1058 	/* compare new mount options with old ones */
1059 	ret = nfs_compare_remount_data(nfss, ctx);
1060 	if (ret)
1061 		return ret;
1062 
1063 	return nfs_probe_server(nfss, NFS_FH(d_inode(fc->root)));
1064 }
1065 EXPORT_SYMBOL_GPL(nfs_reconfigure);
1066 
1067 /*
1068  * Finish setting up an NFS superblock
1069  */
1070 static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
1071 {
1072 	struct nfs_server *server = NFS_SB(sb);
1073 
1074 	sb->s_blocksize_bits = 0;
1075 	sb->s_blocksize = 0;
1076 	sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1077 	sb->s_op = server->nfs_client->cl_nfs_mod->sops;
1078 	if (ctx->bsize)
1079 		sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
1080 
1081 	switch (server->nfs_client->rpc_ops->version) {
1082 	case 2:
1083 		sb->s_time_gran = 1000;
1084 		sb->s_time_min = 0;
1085 		sb->s_time_max = U32_MAX;
1086 		break;
1087 	case 3:
1088 		/*
1089 		 * The VFS shouldn't apply the umask to mode bits.
1090 		 * We will do so ourselves when necessary.
1091 		 */
1092 		sb->s_flags |= SB_POSIXACL;
1093 		sb->s_time_gran = 1;
1094 		sb->s_time_min = 0;
1095 		sb->s_time_max = U32_MAX;
1096 		sb->s_export_op = &nfs_export_ops;
1097 		break;
1098 	case 4:
1099 		sb->s_iflags |= SB_I_NOUMASK;
1100 		sb->s_time_gran = 1;
1101 		sb->s_time_min = S64_MIN;
1102 		sb->s_time_max = S64_MAX;
1103 		if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1104 			sb->s_export_op = &nfs_export_ops;
1105 		break;
1106 	}
1107 
1108 	sb->s_magic = NFS_SUPER_MAGIC;
1109 
1110 	/* We probably want something more informative here */
1111 	snprintf(sb->s_id, sizeof(sb->s_id),
1112 		 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1113 
1114 	if (sb->s_blocksize == 0)
1115 		sb->s_blocksize = nfs_block_bits(server->wsize,
1116 						 &sb->s_blocksize_bits);
1117 
1118 	nfs_super_set_maxbytes(sb, server->maxfilesize);
1119 	nfs_sysfs_move_server_to_sb(sb);
1120 	server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
1121 }
1122 
1123 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
1124 				     const struct fs_context *fc)
1125 {
1126 	const struct nfs_server *a = s->s_fs_info;
1127 	const struct rpc_clnt *clnt_a = a->client;
1128 	const struct rpc_clnt *clnt_b = b->client;
1129 
1130 	if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
1131 		goto Ebusy;
1132 	if (a->nfs_client != b->nfs_client)
1133 		goto Ebusy;
1134 	if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
1135 		goto Ebusy;
1136 	if (a->wsize != b->wsize)
1137 		goto Ebusy;
1138 	if (a->rsize != b->rsize)
1139 		goto Ebusy;
1140 	if (a->acregmin != b->acregmin)
1141 		goto Ebusy;
1142 	if (a->acregmax != b->acregmax)
1143 		goto Ebusy;
1144 	if (a->acdirmin != b->acdirmin)
1145 		goto Ebusy;
1146 	if (a->acdirmax != b->acdirmax)
1147 		goto Ebusy;
1148 	if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1149 		goto Ebusy;
1150 	return 1;
1151 Ebusy:
1152 	return 0;
1153 }
1154 
1155 static int nfs_set_super(struct super_block *s, struct fs_context *fc)
1156 {
1157 	struct nfs_server *server = fc->s_fs_info;
1158 	int ret;
1159 
1160 	s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
1161 	ret = set_anon_super(s, server);
1162 	if (ret == 0)
1163 		server->s_dev = s->s_dev;
1164 	return ret;
1165 }
1166 
1167 static int nfs_compare_super_address(struct nfs_server *server1,
1168 				     struct nfs_server *server2)
1169 {
1170 	struct sockaddr *sap1, *sap2;
1171 	struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1172 	struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1173 
1174 	if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1175 		return 0;
1176 
1177 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1178 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1179 
1180 	if (sap1->sa_family != sap2->sa_family)
1181 		return 0;
1182 
1183 	switch (sap1->sa_family) {
1184 	case AF_INET: {
1185 		struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1186 		struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1187 		if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1188 			return 0;
1189 		if (sin1->sin_port != sin2->sin_port)
1190 			return 0;
1191 		break;
1192 	}
1193 	case AF_INET6: {
1194 		struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1195 		struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1196 		if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1197 			return 0;
1198 		if (sin1->sin6_port != sin2->sin6_port)
1199 			return 0;
1200 		break;
1201 	}
1202 	default:
1203 		return 0;
1204 	}
1205 
1206 	return 1;
1207 }
1208 
1209 static int nfs_compare_userns(const struct nfs_server *old,
1210 		const struct nfs_server *new)
1211 {
1212 	const struct user_namespace *oldns = &init_user_ns;
1213 	const struct user_namespace *newns = &init_user_ns;
1214 
1215 	if (old->client && old->client->cl_cred)
1216 		oldns = old->client->cl_cred->user_ns;
1217 	if (new->client && new->client->cl_cred)
1218 		newns = new->client->cl_cred->user_ns;
1219 	if (oldns != newns)
1220 		return 0;
1221 	return 1;
1222 }
1223 
1224 static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
1225 {
1226 	struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
1227 
1228 	if (!nfs_compare_super_address(old, server))
1229 		return 0;
1230 	/* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1231 	if (old->flags & NFS_MOUNT_UNSHARED)
1232 		return 0;
1233 	if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1234 		return 0;
1235 	if (!nfs_compare_userns(old, server))
1236 		return 0;
1237 	if ((old->has_sec_mnt_opts || fc->security) &&
1238 			security_sb_mnt_opts_compat(sb, fc->security))
1239 		return 0;
1240 	return nfs_compare_mount_options(sb, server, fc);
1241 }
1242 
1243 #ifdef CONFIG_NFS_FSCACHE
1244 static int nfs_get_cache_cookie(struct super_block *sb,
1245 				struct nfs_fs_context *ctx)
1246 {
1247 	struct nfs_server *nfss = NFS_SB(sb);
1248 	char *uniq = NULL;
1249 	int ulen = 0;
1250 
1251 	nfss->fscache = NULL;
1252 
1253 	if (!ctx)
1254 		return 0;
1255 
1256 	if (ctx->clone_data.sb) {
1257 		struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
1258 		if (!(mnt_s->options & NFS_OPTION_FSCACHE))
1259 			return 0;
1260 		if (mnt_s->fscache_uniq) {
1261 			uniq = mnt_s->fscache_uniq;
1262 			ulen = strlen(uniq);
1263 		}
1264 	} else {
1265 		if (!(ctx->options & NFS_OPTION_FSCACHE))
1266 			return 0;
1267 		if (ctx->fscache_uniq) {
1268 			uniq = ctx->fscache_uniq;
1269 			ulen = strlen(ctx->fscache_uniq);
1270 		}
1271 	}
1272 
1273 	return nfs_fscache_get_super_cookie(sb, uniq, ulen);
1274 }
1275 #else
1276 static int nfs_get_cache_cookie(struct super_block *sb,
1277 				struct nfs_fs_context *ctx)
1278 {
1279 	return 0;
1280 }
1281 #endif
1282 
1283 int nfs_get_tree_common(struct fs_context *fc)
1284 {
1285 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1286 	struct super_block *s;
1287 	int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
1288 	struct nfs_server *server = ctx->server;
1289 	int error;
1290 
1291 	ctx->server = NULL;
1292 	if (IS_ERR(server))
1293 		return PTR_ERR(server);
1294 
1295 	if (server->flags & NFS_MOUNT_UNSHARED)
1296 		compare_super = NULL;
1297 
1298 	/* -o noac implies -o sync */
1299 	if (server->flags & NFS_MOUNT_NOAC)
1300 		fc->sb_flags |= SB_SYNCHRONOUS;
1301 
1302 	if (ctx->clone_data.sb)
1303 		if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
1304 			fc->sb_flags |= SB_SYNCHRONOUS;
1305 
1306 	/* Get a superblock - note that we may end up sharing one that already exists */
1307 	fc->s_fs_info = server;
1308 	s = sget_fc(fc, compare_super, nfs_set_super);
1309 	fc->s_fs_info = NULL;
1310 	if (IS_ERR(s)) {
1311 		error = PTR_ERR(s);
1312 		nfs_errorf(fc, "NFS: Couldn't get superblock");
1313 		goto out_err_nosb;
1314 	}
1315 
1316 	if (s->s_fs_info != server) {
1317 		nfs_free_server(server);
1318 		server = NULL;
1319 	} else {
1320 		error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1321 					     MINOR(server->s_dev));
1322 		if (error)
1323 			goto error_splat_super;
1324 		s->s_bdi->io_pages = server->rpages;
1325 		server->super = s;
1326 	}
1327 
1328 	if (!s->s_root) {
1329 		unsigned bsize = ctx->clone_data.inherited_bsize;
1330 		/* initial superblock/root creation */
1331 		nfs_fill_super(s, ctx);
1332 		if (bsize) {
1333 			s->s_blocksize_bits = bsize;
1334 			s->s_blocksize = 1U << bsize;
1335 		}
1336 		error = nfs_get_cache_cookie(s, ctx);
1337 		if (error < 0)
1338 			goto error_splat_super;
1339 	}
1340 
1341 	error = nfs_get_root(s, fc);
1342 	if (error < 0) {
1343 		nfs_errorf(fc, "NFS: Couldn't get root dentry");
1344 		goto error_splat_super;
1345 	}
1346 
1347 	s->s_flags |= SB_ACTIVE;
1348 	error = 0;
1349 
1350 out:
1351 	return error;
1352 
1353 out_err_nosb:
1354 	nfs_free_server(server);
1355 	goto out;
1356 error_splat_super:
1357 	deactivate_locked_super(s);
1358 	goto out;
1359 }
1360 
1361 /*
1362  * Destroy an NFS superblock
1363  */
1364 void nfs_kill_super(struct super_block *s)
1365 {
1366 	struct nfs_server *server = NFS_SB(s);
1367 
1368 	nfs_sysfs_move_sb_to_server(server);
1369 	kill_anon_super(s);
1370 
1371 	nfs_fscache_release_super_cookie(s);
1372 
1373 	nfs_free_server(server);
1374 }
1375 EXPORT_SYMBOL_GPL(nfs_kill_super);
1376 
1377 #if IS_ENABLED(CONFIG_NFS_V4)
1378 
1379 /*
1380  * NFS v4 module parameters need to stay in the
1381  * NFS client for backwards compatibility
1382  */
1383 unsigned int nfs_callback_set_tcpport;
1384 unsigned short nfs_callback_nr_threads;
1385 /* Default cache timeout is 10 minutes */
1386 unsigned int nfs_idmap_cache_timeout = 600;
1387 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1388 bool nfs4_disable_idmapping = true;
1389 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
1390 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
1391 unsigned short send_implementation_id = 1;
1392 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
1393 bool recover_lost_locks = false;
1394 short nfs_delay_retrans = -1;
1395 
1396 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
1397 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
1398 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1399 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1400 EXPORT_SYMBOL_GPL(max_session_slots);
1401 EXPORT_SYMBOL_GPL(max_session_cb_slots);
1402 EXPORT_SYMBOL_GPL(send_implementation_id);
1403 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
1404 EXPORT_SYMBOL_GPL(recover_lost_locks);
1405 EXPORT_SYMBOL_GPL(nfs_delay_retrans);
1406 
1407 #define NFS_CALLBACK_MAXPORTNR (65535U)
1408 
1409 static int param_set_portnr(const char *val, const struct kernel_param *kp)
1410 {
1411 	unsigned long num;
1412 	int ret;
1413 
1414 	if (!val)
1415 		return -EINVAL;
1416 	ret = kstrtoul(val, 0, &num);
1417 	if (ret || num > NFS_CALLBACK_MAXPORTNR)
1418 		return -EINVAL;
1419 	*((unsigned int *)kp->arg) = num;
1420 	return 0;
1421 }
1422 static const struct kernel_param_ops param_ops_portnr = {
1423 	.set = param_set_portnr,
1424 	.get = param_get_uint,
1425 };
1426 #define param_check_portnr(name, p) __param_check(name, p, unsigned int)
1427 
1428 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
1429 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1430 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1431 		"assigned to the NFSv4 callback channels.");
1432 module_param(nfs_idmap_cache_timeout, int, 0644);
1433 module_param(nfs4_disable_idmapping, bool, 0644);
1434 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1435 			NFS4_CLIENT_ID_UNIQ_LEN, 0600);
1436 MODULE_PARM_DESC(nfs4_disable_idmapping,
1437 		"Turn off NFSv4 idmapping when using 'sec=sys'");
1438 module_param(max_session_slots, ushort, 0644);
1439 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1440 		"requests the client will negotiate");
1441 module_param(max_session_cb_slots, ushort, 0644);
1442 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
1443 		"callbacks the client will process for a given server");
1444 module_param(send_implementation_id, ushort, 0644);
1445 MODULE_PARM_DESC(send_implementation_id,
1446 		"Send implementation ID with NFSv4.1 exchange_id");
1447 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
1448 
1449 module_param(recover_lost_locks, bool, 0644);
1450 MODULE_PARM_DESC(recover_lost_locks,
1451 		 "If the server reports that a lock might be lost, "
1452 		 "try to recover it risking data corruption.");
1453 
1454 module_param_named(delay_retrans, nfs_delay_retrans, short, 0644);
1455 MODULE_PARM_DESC(delay_retrans,
1456 		 "Unless negative, specifies the number of times the NFSv4 "
1457 		 "client retries a request before returning an EAGAIN error, "
1458 		 "after a reply of NFS4ERR_DELAY from the server.");
1459 #endif /* CONFIG_NFS_V4 */
1460