xref: /linux/fs/nfs/super.c (revision 55f1b540d893da740a81200450014c45a8103f54)
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_NO_ALIGNWRITE)
555 		seq_puts(m, ",noalignwrite");
556 
557 	if (nfss->flags & NFS_MOUNT_WRITE_EAGER) {
558 		if (nfss->flags & NFS_MOUNT_WRITE_WAIT)
559 			seq_puts(m, ",write=wait");
560 		else
561 			seq_puts(m, ",write=eager");
562 	}
563 }
564 
565 /*
566  * Describe the mount options on this VFS mountpoint
567  */
568 int nfs_show_options(struct seq_file *m, struct dentry *root)
569 {
570 	struct nfs_server *nfss = NFS_SB(root->d_sb);
571 
572 	nfs_show_mount_options(m, nfss, 0);
573 
574 	rcu_read_lock();
575 	seq_printf(m, ",addr=%s",
576 			rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
577 							RPC_DISPLAY_ADDR));
578 	rcu_read_unlock();
579 
580 	return 0;
581 }
582 EXPORT_SYMBOL_GPL(nfs_show_options);
583 
584 #if IS_ENABLED(CONFIG_NFS_V4)
585 static void show_lease(struct seq_file *m, struct nfs_server *server)
586 {
587 	struct nfs_client *clp = server->nfs_client;
588 	unsigned long expire;
589 
590 	seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
591 	expire = clp->cl_last_renewal + clp->cl_lease_time;
592 	seq_printf(m, ",lease_expired=%ld",
593 		   time_after(expire, jiffies) ?  0 : (jiffies - expire) / HZ);
594 }
595 #ifdef CONFIG_NFS_V4_1
596 static void show_sessions(struct seq_file *m, struct nfs_server *server)
597 {
598 	if (nfs4_has_session(server->nfs_client))
599 		seq_puts(m, ",sessions");
600 }
601 #else
602 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
603 #endif
604 #endif
605 
606 #ifdef CONFIG_NFS_V4_1
607 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
608 {
609 	seq_printf(m, ",pnfs=");
610 	if (server->pnfs_curr_ld)
611 		seq_printf(m, "%s", server->pnfs_curr_ld->name);
612 	else
613 		seq_printf(m, "not configured");
614 }
615 
616 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
617 {
618 	if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
619 		struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
620 		seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
621 			   "date='%llu,%u'",
622 			   impl_id->name, impl_id->domain,
623 			   impl_id->date.seconds, impl_id->date.nseconds);
624 	}
625 }
626 #else
627 #if IS_ENABLED(CONFIG_NFS_V4)
628 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
629 {
630 }
631 #endif
632 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
633 {
634 }
635 #endif
636 
637 int nfs_show_devname(struct seq_file *m, struct dentry *root)
638 {
639 	char *page = (char *) __get_free_page(GFP_KERNEL);
640 	char *devname, *dummy;
641 	int err = 0;
642 	if (!page)
643 		return -ENOMEM;
644 	devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
645 	if (IS_ERR(devname))
646 		err = PTR_ERR(devname);
647 	else
648 		seq_escape(m, devname, " \t\n\\");
649 	free_page((unsigned long)page);
650 	return err;
651 }
652 EXPORT_SYMBOL_GPL(nfs_show_devname);
653 
654 int nfs_show_path(struct seq_file *m, struct dentry *dentry)
655 {
656 	seq_puts(m, "/");
657 	return 0;
658 }
659 EXPORT_SYMBOL_GPL(nfs_show_path);
660 
661 /*
662  * Present statistical information for this VFS mountpoint
663  */
664 int nfs_show_stats(struct seq_file *m, struct dentry *root)
665 {
666 	int i, cpu;
667 	struct nfs_server *nfss = NFS_SB(root->d_sb);
668 	struct rpc_auth *auth = nfss->client->cl_auth;
669 	struct nfs_iostats totals = { };
670 
671 	seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
672 
673 	/*
674 	 * Display all mount option settings
675 	 */
676 	seq_puts(m, "\n\topts:\t");
677 	seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
678 	seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
679 	seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
680 	seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
681 	nfs_show_mount_options(m, nfss, 1);
682 
683 	seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
684 
685 	show_implementation_id(m, nfss);
686 
687 	seq_puts(m, "\n\tcaps:\t");
688 	seq_printf(m, "caps=0x%x", nfss->caps);
689 	seq_printf(m, ",wtmult=%u", nfss->wtmult);
690 	seq_printf(m, ",dtsize=%u", nfss->dtsize);
691 	seq_printf(m, ",bsize=%u", nfss->bsize);
692 	seq_printf(m, ",namlen=%u", nfss->namelen);
693 
694 #if IS_ENABLED(CONFIG_NFS_V4)
695 	if (nfss->nfs_client->rpc_ops->version == 4) {
696 		seq_puts(m, "\n\tnfsv4:\t");
697 		seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
698 		seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
699 		seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
700 		seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
701 		show_sessions(m, nfss);
702 		show_pnfs(m, nfss);
703 		show_lease(m, nfss);
704 	}
705 #endif
706 
707 	/*
708 	 * Display security flavor in effect for this mount
709 	 */
710 	seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
711 	if (auth->au_flavor)
712 		seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
713 
714 	/*
715 	 * Display superblock I/O counters
716 	 */
717 	for_each_possible_cpu(cpu) {
718 		struct nfs_iostats *stats;
719 
720 		preempt_disable();
721 		stats = per_cpu_ptr(nfss->io_stats, cpu);
722 
723 		for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
724 			totals.events[i] += stats->events[i];
725 		for (i = 0; i < __NFSIOS_BYTESMAX; i++)
726 			totals.bytes[i] += stats->bytes[i];
727 
728 		preempt_enable();
729 	}
730 
731 	seq_puts(m, "\n\tevents:\t");
732 	for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
733 		seq_printf(m, "%lu ", totals.events[i]);
734 	seq_puts(m, "\n\tbytes:\t");
735 	for (i = 0; i < __NFSIOS_BYTESMAX; i++)
736 		seq_printf(m, "%Lu ", totals.bytes[i]);
737 	seq_putc(m, '\n');
738 
739 	rpc_clnt_show_stats(m, nfss->client);
740 
741 	return 0;
742 }
743 EXPORT_SYMBOL_GPL(nfs_show_stats);
744 
745 /*
746  * Begin unmount by attempting to remove all automounted mountpoints we added
747  * in response to xdev traversals and referrals
748  */
749 void nfs_umount_begin(struct super_block *sb)
750 {
751 	struct nfs_server *server;
752 	struct rpc_clnt *rpc;
753 
754 	server = NFS_SB(sb);
755 	/* -EIO all pending I/O */
756 	rpc = server->client_acl;
757 	if (!IS_ERR(rpc))
758 		rpc_killall_tasks(rpc);
759 	rpc = server->client;
760 	if (!IS_ERR(rpc))
761 		rpc_killall_tasks(rpc);
762 }
763 EXPORT_SYMBOL_GPL(nfs_umount_begin);
764 
765 /*
766  * Return true if 'match' is in auth_info or auth_info is empty.
767  * Return false otherwise.
768  */
769 bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
770 			 rpc_authflavor_t match)
771 {
772 	int i;
773 
774 	if (!auth_info->flavor_len)
775 		return true;
776 
777 	for (i = 0; i < auth_info->flavor_len; i++) {
778 		if (auth_info->flavors[i] == match)
779 			return true;
780 	}
781 	return false;
782 }
783 EXPORT_SYMBOL_GPL(nfs_auth_info_match);
784 
785 /*
786  * Ensure that a specified authtype in ctx->auth_info is supported by
787  * the server. Returns 0 and sets ctx->selected_flavor if it's ok, and
788  * -EACCES if not.
789  */
790 static int nfs_verify_authflavors(struct nfs_fs_context *ctx,
791 				  rpc_authflavor_t *server_authlist,
792 				  unsigned int count)
793 {
794 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
795 	bool found_auth_null = false;
796 	unsigned int i;
797 
798 	/*
799 	 * If the sec= mount option is used, the specified flavor or AUTH_NULL
800 	 * must be in the list returned by the server.
801 	 *
802 	 * AUTH_NULL has a special meaning when it's in the server list - it
803 	 * means that the server will ignore the rpc creds, so any flavor
804 	 * can be used but still use the sec= that was specified.
805 	 *
806 	 * Note also that the MNT procedure in MNTv1 does not return a list
807 	 * of supported security flavors. In this case, nfs_mount() fabricates
808 	 * a security flavor list containing just AUTH_NULL.
809 	 */
810 	for (i = 0; i < count; i++) {
811 		flavor = server_authlist[i];
812 
813 		if (nfs_auth_info_match(&ctx->auth_info, flavor))
814 			goto out;
815 
816 		if (flavor == RPC_AUTH_NULL)
817 			found_auth_null = true;
818 	}
819 
820 	if (found_auth_null) {
821 		flavor = ctx->auth_info.flavors[0];
822 		goto out;
823 	}
824 
825 	dfprintk(MOUNT,
826 		 "NFS: specified auth flavors not supported by server\n");
827 	return -EACCES;
828 
829 out:
830 	ctx->selected_flavor = flavor;
831 	dfprintk(MOUNT, "NFS: using auth flavor %u\n", ctx->selected_flavor);
832 	return 0;
833 }
834 
835 /*
836  * Use the remote server's MOUNT service to request the NFS file handle
837  * corresponding to the provided path.
838  */
839 static int nfs_request_mount(struct fs_context *fc,
840 			     struct nfs_fh *root_fh,
841 			     rpc_authflavor_t *server_authlist,
842 			     unsigned int *server_authlist_len)
843 {
844 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
845 	struct nfs_mount_request request = {
846 		.sap		= &ctx->mount_server._address,
847 		.dirpath	= ctx->nfs_server.export_path,
848 		.protocol	= ctx->mount_server.protocol,
849 		.fh		= root_fh,
850 		.noresvport	= ctx->flags & NFS_MOUNT_NORESVPORT,
851 		.auth_flav_len	= server_authlist_len,
852 		.auth_flavs	= server_authlist,
853 		.net		= fc->net_ns,
854 	};
855 	int status;
856 
857 	if (ctx->mount_server.version == 0) {
858 		switch (ctx->version) {
859 			default:
860 				ctx->mount_server.version = NFS_MNT3_VERSION;
861 				break;
862 			case 2:
863 				ctx->mount_server.version = NFS_MNT_VERSION;
864 		}
865 	}
866 	request.version = ctx->mount_server.version;
867 
868 	if (ctx->mount_server.hostname)
869 		request.hostname = ctx->mount_server.hostname;
870 	else
871 		request.hostname = ctx->nfs_server.hostname;
872 
873 	/*
874 	 * Construct the mount server's address.
875 	 */
876 	if (ctx->mount_server.address.sa_family == AF_UNSPEC) {
877 		memcpy(request.sap, &ctx->nfs_server._address,
878 		       ctx->nfs_server.addrlen);
879 		ctx->mount_server.addrlen = ctx->nfs_server.addrlen;
880 	}
881 	request.salen = ctx->mount_server.addrlen;
882 	nfs_set_port(request.sap, &ctx->mount_server.port, 0);
883 
884 	/*
885 	 * Now ask the mount server to map our export path
886 	 * to a file handle.
887 	 */
888 	status = nfs_mount(&request, ctx->timeo, ctx->retrans);
889 	if (status != 0) {
890 		dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
891 				request.hostname, status);
892 		return status;
893 	}
894 
895 	return 0;
896 }
897 
898 static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
899 {
900 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
901 	int status;
902 	unsigned int i;
903 	bool tried_auth_unix = false;
904 	bool auth_null_in_list = false;
905 	struct nfs_server *server = ERR_PTR(-EACCES);
906 	rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
907 	unsigned int authlist_len = ARRAY_SIZE(authlist);
908 
909 	/* make sure 'nolock'/'lock' override the 'local_lock' mount option */
910 	if (ctx->lock_status) {
911 		if (ctx->lock_status == NFS_LOCK_NOLOCK) {
912 			ctx->flags |= NFS_MOUNT_NONLM;
913 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
914 		} else {
915 			ctx->flags &= ~NFS_MOUNT_NONLM;
916 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
917 		}
918 	}
919 	status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
920 	if (status)
921 		return ERR_PTR(status);
922 
923 	/*
924 	 * Was a sec= authflavor specified in the options? First, verify
925 	 * whether the server supports it, and then just try to use it if so.
926 	 */
927 	if (ctx->auth_info.flavor_len > 0) {
928 		status = nfs_verify_authflavors(ctx, authlist, authlist_len);
929 		dfprintk(MOUNT, "NFS: using auth flavor %u\n",
930 			 ctx->selected_flavor);
931 		if (status)
932 			return ERR_PTR(status);
933 		return ctx->nfs_mod->rpc_ops->create_server(fc);
934 	}
935 
936 	/*
937 	 * No sec= option was provided. RFC 2623, section 2.7 suggests we
938 	 * SHOULD prefer the flavor listed first. However, some servers list
939 	 * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
940 	 */
941 	for (i = 0; i < authlist_len; ++i) {
942 		rpc_authflavor_t flavor;
943 		struct rpcsec_gss_info info;
944 
945 		flavor = authlist[i];
946 		switch (flavor) {
947 		case RPC_AUTH_UNIX:
948 			tried_auth_unix = true;
949 			break;
950 		case RPC_AUTH_NULL:
951 			auth_null_in_list = true;
952 			continue;
953 		default:
954 			if (rpcauth_get_gssinfo(flavor, &info) != 0)
955 				continue;
956 			break;
957 		}
958 		dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
959 		ctx->selected_flavor = flavor;
960 		server = ctx->nfs_mod->rpc_ops->create_server(fc);
961 		if (!IS_ERR(server))
962 			return server;
963 	}
964 
965 	/*
966 	 * Nothing we tried so far worked. At this point, give up if we've
967 	 * already tried AUTH_UNIX or if the server's list doesn't contain
968 	 * AUTH_NULL
969 	 */
970 	if (tried_auth_unix || !auth_null_in_list)
971 		return server;
972 
973 	/* Last chance! Try AUTH_UNIX */
974 	dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
975 	ctx->selected_flavor = RPC_AUTH_UNIX;
976 	return ctx->nfs_mod->rpc_ops->create_server(fc);
977 }
978 
979 int nfs_try_get_tree(struct fs_context *fc)
980 {
981 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
982 
983 	if (ctx->need_mount)
984 		ctx->server = nfs_try_mount_request(fc);
985 	else
986 		ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
987 
988 	return nfs_get_tree_common(fc);
989 }
990 EXPORT_SYMBOL_GPL(nfs_try_get_tree);
991 
992 
993 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
994 		| NFS_MOUNT_SECURE \
995 		| NFS_MOUNT_TCP \
996 		| NFS_MOUNT_VER3 \
997 		| NFS_MOUNT_KERBEROS \
998 		| NFS_MOUNT_NONLM \
999 		| NFS_MOUNT_BROKEN_SUID \
1000 		| NFS_MOUNT_STRICTLOCK \
1001 		| NFS_MOUNT_LEGACY_INTERFACE)
1002 
1003 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
1004 		~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
1005 
1006 static int
1007 nfs_compare_remount_data(struct nfs_server *nfss,
1008 			 struct nfs_fs_context *ctx)
1009 {
1010 	if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
1011 	    ctx->rsize != nfss->rsize ||
1012 	    ctx->wsize != nfss->wsize ||
1013 	    ctx->version != nfss->nfs_client->rpc_ops->version ||
1014 	    ctx->minorversion != nfss->nfs_client->cl_minorversion ||
1015 	    ctx->retrans != nfss->client->cl_timeout->to_retries ||
1016 	    !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
1017 	    ctx->acregmin != nfss->acregmin / HZ ||
1018 	    ctx->acregmax != nfss->acregmax / HZ ||
1019 	    ctx->acdirmin != nfss->acdirmin / HZ ||
1020 	    ctx->acdirmax != nfss->acdirmax / HZ ||
1021 	    ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1022 	    (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
1023 	    ctx->nfs_server.port != nfss->port ||
1024 	    ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1025 	    !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
1026 			  (struct sockaddr *)&nfss->nfs_client->cl_addr))
1027 		return -EINVAL;
1028 
1029 	return 0;
1030 }
1031 
1032 int nfs_reconfigure(struct fs_context *fc)
1033 {
1034 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1035 	struct super_block *sb = fc->root->d_sb;
1036 	struct nfs_server *nfss = sb->s_fs_info;
1037 	int ret;
1038 
1039 	sync_filesystem(sb);
1040 
1041 	/*
1042 	 * Userspace mount programs that send binary options generally send
1043 	 * them populated with default values. We have no way to know which
1044 	 * ones were explicitly specified. Fall back to legacy behavior and
1045 	 * just return success.
1046 	 */
1047 	if (ctx->skip_reconfig_option_check)
1048 		return 0;
1049 
1050 	/*
1051 	 * noac is a special case. It implies -o sync, but that's not
1052 	 * necessarily reflected in the mtab options. reconfigure_super
1053 	 * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
1054 	 * remount options, so we have to explicitly reset it.
1055 	 */
1056 	if (ctx->flags & NFS_MOUNT_NOAC) {
1057 		fc->sb_flags |= SB_SYNCHRONOUS;
1058 		fc->sb_flags_mask |= SB_SYNCHRONOUS;
1059 	}
1060 
1061 	/* compare new mount options with old ones */
1062 	ret = nfs_compare_remount_data(nfss, ctx);
1063 	if (ret)
1064 		return ret;
1065 
1066 	return nfs_probe_server(nfss, NFS_FH(d_inode(fc->root)));
1067 }
1068 EXPORT_SYMBOL_GPL(nfs_reconfigure);
1069 
1070 /*
1071  * Finish setting up an NFS superblock
1072  */
1073 static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
1074 {
1075 	struct nfs_server *server = NFS_SB(sb);
1076 
1077 	sb->s_blocksize_bits = 0;
1078 	sb->s_blocksize = 0;
1079 	sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1080 	sb->s_op = server->nfs_client->cl_nfs_mod->sops;
1081 	if (ctx->bsize)
1082 		sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
1083 
1084 	switch (server->nfs_client->rpc_ops->version) {
1085 	case 2:
1086 		sb->s_time_gran = 1000;
1087 		sb->s_time_min = 0;
1088 		sb->s_time_max = U32_MAX;
1089 		break;
1090 	case 3:
1091 		/*
1092 		 * The VFS shouldn't apply the umask to mode bits.
1093 		 * We will do so ourselves when necessary.
1094 		 */
1095 		sb->s_flags |= SB_POSIXACL;
1096 		sb->s_time_gran = 1;
1097 		sb->s_time_min = 0;
1098 		sb->s_time_max = U32_MAX;
1099 		sb->s_export_op = &nfs_export_ops;
1100 		break;
1101 	case 4:
1102 		sb->s_iflags |= SB_I_NOUMASK;
1103 		sb->s_time_gran = 1;
1104 		sb->s_time_min = S64_MIN;
1105 		sb->s_time_max = S64_MAX;
1106 		if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1107 			sb->s_export_op = &nfs_export_ops;
1108 		break;
1109 	}
1110 
1111 	sb->s_magic = NFS_SUPER_MAGIC;
1112 
1113 	/* We probably want something more informative here */
1114 	snprintf(sb->s_id, sizeof(sb->s_id),
1115 		 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1116 
1117 	if (sb->s_blocksize == 0)
1118 		sb->s_blocksize = nfs_block_bits(server->wsize,
1119 						 &sb->s_blocksize_bits);
1120 
1121 	nfs_super_set_maxbytes(sb, server->maxfilesize);
1122 	nfs_sysfs_move_server_to_sb(sb);
1123 	server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
1124 }
1125 
1126 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
1127 				     const struct fs_context *fc)
1128 {
1129 	const struct nfs_server *a = s->s_fs_info;
1130 	const struct rpc_clnt *clnt_a = a->client;
1131 	const struct rpc_clnt *clnt_b = b->client;
1132 
1133 	if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
1134 		goto Ebusy;
1135 	if (a->nfs_client != b->nfs_client)
1136 		goto Ebusy;
1137 	if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
1138 		goto Ebusy;
1139 	if (a->wsize != b->wsize)
1140 		goto Ebusy;
1141 	if (a->rsize != b->rsize)
1142 		goto Ebusy;
1143 	if (a->acregmin != b->acregmin)
1144 		goto Ebusy;
1145 	if (a->acregmax != b->acregmax)
1146 		goto Ebusy;
1147 	if (a->acdirmin != b->acdirmin)
1148 		goto Ebusy;
1149 	if (a->acdirmax != b->acdirmax)
1150 		goto Ebusy;
1151 	if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1152 		goto Ebusy;
1153 	return 1;
1154 Ebusy:
1155 	return 0;
1156 }
1157 
1158 static int nfs_set_super(struct super_block *s, struct fs_context *fc)
1159 {
1160 	struct nfs_server *server = fc->s_fs_info;
1161 	int ret;
1162 
1163 	s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
1164 	ret = set_anon_super(s, server);
1165 	if (ret == 0)
1166 		server->s_dev = s->s_dev;
1167 	return ret;
1168 }
1169 
1170 static int nfs_compare_super_address(struct nfs_server *server1,
1171 				     struct nfs_server *server2)
1172 {
1173 	struct sockaddr *sap1, *sap2;
1174 	struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1175 	struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1176 
1177 	if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1178 		return 0;
1179 
1180 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1181 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1182 
1183 	if (sap1->sa_family != sap2->sa_family)
1184 		return 0;
1185 
1186 	switch (sap1->sa_family) {
1187 	case AF_INET: {
1188 		struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1189 		struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1190 		if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1191 			return 0;
1192 		if (sin1->sin_port != sin2->sin_port)
1193 			return 0;
1194 		break;
1195 	}
1196 	case AF_INET6: {
1197 		struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1198 		struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1199 		if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1200 			return 0;
1201 		if (sin1->sin6_port != sin2->sin6_port)
1202 			return 0;
1203 		break;
1204 	}
1205 	default:
1206 		return 0;
1207 	}
1208 
1209 	return 1;
1210 }
1211 
1212 static int nfs_compare_userns(const struct nfs_server *old,
1213 		const struct nfs_server *new)
1214 {
1215 	const struct user_namespace *oldns = &init_user_ns;
1216 	const struct user_namespace *newns = &init_user_ns;
1217 
1218 	if (old->client && old->client->cl_cred)
1219 		oldns = old->client->cl_cred->user_ns;
1220 	if (new->client && new->client->cl_cred)
1221 		newns = new->client->cl_cred->user_ns;
1222 	if (oldns != newns)
1223 		return 0;
1224 	return 1;
1225 }
1226 
1227 static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
1228 {
1229 	struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
1230 
1231 	if (!nfs_compare_super_address(old, server))
1232 		return 0;
1233 	/* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1234 	if (old->flags & NFS_MOUNT_UNSHARED)
1235 		return 0;
1236 	if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1237 		return 0;
1238 	if (!nfs_compare_userns(old, server))
1239 		return 0;
1240 	if ((old->has_sec_mnt_opts || fc->security) &&
1241 			security_sb_mnt_opts_compat(sb, fc->security))
1242 		return 0;
1243 	return nfs_compare_mount_options(sb, server, fc);
1244 }
1245 
1246 #ifdef CONFIG_NFS_FSCACHE
1247 static int nfs_get_cache_cookie(struct super_block *sb,
1248 				struct nfs_fs_context *ctx)
1249 {
1250 	struct nfs_server *nfss = NFS_SB(sb);
1251 	char *uniq = NULL;
1252 	int ulen = 0;
1253 
1254 	nfss->fscache = NULL;
1255 
1256 	if (!ctx)
1257 		return 0;
1258 
1259 	if (ctx->clone_data.sb) {
1260 		struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
1261 		if (!(mnt_s->options & NFS_OPTION_FSCACHE))
1262 			return 0;
1263 		if (mnt_s->fscache_uniq) {
1264 			uniq = mnt_s->fscache_uniq;
1265 			ulen = strlen(uniq);
1266 		}
1267 	} else {
1268 		if (!(ctx->options & NFS_OPTION_FSCACHE))
1269 			return 0;
1270 		if (ctx->fscache_uniq) {
1271 			uniq = ctx->fscache_uniq;
1272 			ulen = strlen(ctx->fscache_uniq);
1273 		}
1274 	}
1275 
1276 	return nfs_fscache_get_super_cookie(sb, uniq, ulen);
1277 }
1278 #else
1279 static int nfs_get_cache_cookie(struct super_block *sb,
1280 				struct nfs_fs_context *ctx)
1281 {
1282 	return 0;
1283 }
1284 #endif
1285 
1286 int nfs_get_tree_common(struct fs_context *fc)
1287 {
1288 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1289 	struct super_block *s;
1290 	int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
1291 	struct nfs_server *server = ctx->server;
1292 	int error;
1293 
1294 	ctx->server = NULL;
1295 	if (IS_ERR(server))
1296 		return PTR_ERR(server);
1297 
1298 	if (server->flags & NFS_MOUNT_UNSHARED)
1299 		compare_super = NULL;
1300 
1301 	/* -o noac implies -o sync */
1302 	if (server->flags & NFS_MOUNT_NOAC)
1303 		fc->sb_flags |= SB_SYNCHRONOUS;
1304 
1305 	if (ctx->clone_data.sb)
1306 		if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
1307 			fc->sb_flags |= SB_SYNCHRONOUS;
1308 
1309 	/* Get a superblock - note that we may end up sharing one that already exists */
1310 	fc->s_fs_info = server;
1311 	s = sget_fc(fc, compare_super, nfs_set_super);
1312 	fc->s_fs_info = NULL;
1313 	if (IS_ERR(s)) {
1314 		error = PTR_ERR(s);
1315 		nfs_errorf(fc, "NFS: Couldn't get superblock");
1316 		goto out_err_nosb;
1317 	}
1318 
1319 	if (s->s_fs_info != server) {
1320 		nfs_free_server(server);
1321 		server = NULL;
1322 	} else {
1323 		error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1324 					     MINOR(server->s_dev));
1325 		if (error)
1326 			goto error_splat_super;
1327 		s->s_bdi->io_pages = server->rpages;
1328 		server->super = s;
1329 	}
1330 
1331 	if (!s->s_root) {
1332 		unsigned bsize = ctx->clone_data.inherited_bsize;
1333 		/* initial superblock/root creation */
1334 		nfs_fill_super(s, ctx);
1335 		if (bsize) {
1336 			s->s_blocksize_bits = bsize;
1337 			s->s_blocksize = 1U << bsize;
1338 		}
1339 		error = nfs_get_cache_cookie(s, ctx);
1340 		if (error < 0)
1341 			goto error_splat_super;
1342 	}
1343 
1344 	error = nfs_get_root(s, fc);
1345 	if (error < 0) {
1346 		nfs_errorf(fc, "NFS: Couldn't get root dentry");
1347 		goto error_splat_super;
1348 	}
1349 
1350 	s->s_flags |= SB_ACTIVE;
1351 	error = 0;
1352 
1353 out:
1354 	return error;
1355 
1356 out_err_nosb:
1357 	nfs_free_server(server);
1358 	goto out;
1359 error_splat_super:
1360 	deactivate_locked_super(s);
1361 	goto out;
1362 }
1363 
1364 /*
1365  * Destroy an NFS superblock
1366  */
1367 void nfs_kill_super(struct super_block *s)
1368 {
1369 	struct nfs_server *server = NFS_SB(s);
1370 
1371 	nfs_sysfs_move_sb_to_server(server);
1372 	kill_anon_super(s);
1373 
1374 	nfs_fscache_release_super_cookie(s);
1375 
1376 	nfs_free_server(server);
1377 }
1378 EXPORT_SYMBOL_GPL(nfs_kill_super);
1379 
1380 #if IS_ENABLED(CONFIG_NFS_V4)
1381 
1382 /*
1383  * NFS v4 module parameters need to stay in the
1384  * NFS client for backwards compatibility
1385  */
1386 unsigned int nfs_callback_set_tcpport;
1387 unsigned short nfs_callback_nr_threads;
1388 /* Default cache timeout is 10 minutes */
1389 unsigned int nfs_idmap_cache_timeout = 600;
1390 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1391 bool nfs4_disable_idmapping = true;
1392 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
1393 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
1394 unsigned short send_implementation_id = 1;
1395 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
1396 bool recover_lost_locks = false;
1397 short nfs_delay_retrans = -1;
1398 
1399 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
1400 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
1401 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1402 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1403 EXPORT_SYMBOL_GPL(max_session_slots);
1404 EXPORT_SYMBOL_GPL(max_session_cb_slots);
1405 EXPORT_SYMBOL_GPL(send_implementation_id);
1406 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
1407 EXPORT_SYMBOL_GPL(recover_lost_locks);
1408 EXPORT_SYMBOL_GPL(nfs_delay_retrans);
1409 
1410 #define NFS_CALLBACK_MAXPORTNR (65535U)
1411 
1412 static int param_set_portnr(const char *val, const struct kernel_param *kp)
1413 {
1414 	unsigned long num;
1415 	int ret;
1416 
1417 	if (!val)
1418 		return -EINVAL;
1419 	ret = kstrtoul(val, 0, &num);
1420 	if (ret || num > NFS_CALLBACK_MAXPORTNR)
1421 		return -EINVAL;
1422 	*((unsigned int *)kp->arg) = num;
1423 	return 0;
1424 }
1425 static const struct kernel_param_ops param_ops_portnr = {
1426 	.set = param_set_portnr,
1427 	.get = param_get_uint,
1428 };
1429 #define param_check_portnr(name, p) __param_check(name, p, unsigned int)
1430 
1431 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
1432 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1433 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1434 		"assigned to the NFSv4 callback channels.");
1435 module_param(nfs_idmap_cache_timeout, int, 0644);
1436 module_param(nfs4_disable_idmapping, bool, 0644);
1437 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1438 			NFS4_CLIENT_ID_UNIQ_LEN, 0600);
1439 MODULE_PARM_DESC(nfs4_disable_idmapping,
1440 		"Turn off NFSv4 idmapping when using 'sec=sys'");
1441 module_param(max_session_slots, ushort, 0644);
1442 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1443 		"requests the client will negotiate");
1444 module_param(max_session_cb_slots, ushort, 0644);
1445 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
1446 		"callbacks the client will process for a given server");
1447 module_param(send_implementation_id, ushort, 0644);
1448 MODULE_PARM_DESC(send_implementation_id,
1449 		"Send implementation ID with NFSv4.1 exchange_id");
1450 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
1451 
1452 module_param(recover_lost_locks, bool, 0644);
1453 MODULE_PARM_DESC(recover_lost_locks,
1454 		 "If the server reports that a lock might be lost, "
1455 		 "try to recover it risking data corruption.");
1456 
1457 module_param_named(delay_retrans, nfs_delay_retrans, short, 0644);
1458 MODULE_PARM_DESC(delay_retrans,
1459 		 "Unless negative, specifies the number of times the NFSv4 "
1460 		 "client retries a request before returning an EAGAIN error, "
1461 		 "after a reply of NFS4ERR_DELAY from the server.");
1462 #endif /* CONFIG_NFS_V4 */
1463