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)
register_nfs4_fs(void)101 static int __init register_nfs4_fs(void)
102 {
103 	return register_filesystem(&nfs4_fs_type);
104 }
105 
unregister_nfs4_fs(void)106 static void unregister_nfs4_fs(void)
107 {
108 	unregister_filesystem(&nfs4_fs_type);
109 }
110 #else
register_nfs4_fs(void)111 static int __init register_nfs4_fs(void)
112 {
113 	return 0;
114 }
115 
unregister_nfs4_fs(void)116 static void unregister_nfs4_fs(void)
117 {
118 }
119 #endif
120 
121 #ifdef CONFIG_NFS_V4_2
nfs_ssc_register_ops(void)122 static void nfs_ssc_register_ops(void)
123 {
124 	nfs_ssc_register(&nfs_ssc_clnt_ops_tbl);
125 }
126 
nfs_ssc_unregister_ops(void)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  */
register_nfs_fs(void)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  */
unregister_nfs_fs(void)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 
nfs_sb_active(struct super_block * sb)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 
nfs_sb_deactive(struct super_block * sb)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 
__nfs_list_for_each_server(struct list_head * head,int (* fn)(struct nfs_server *,void *),void * data)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 
nfs_client_for_each_server(struct nfs_client * clp,int (* fn)(struct nfs_server *,void *),void * data)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  */
nfs_statfs(struct dentry * dentry,struct kstatfs * buf)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  */
nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)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 
nfs_show_mountd_netid(struct seq_file * m,struct nfs_server * nfss,int showdefaults)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 
nfs_show_mountd_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)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)
nfs_show_nfsv4_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)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
nfs_show_nfsv4_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)421 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
422 				    int showdefaults)
423 {
424 }
425 #endif
426 
nfs_show_nfs_version(struct seq_file * m,unsigned int version,unsigned int minorversion)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  */
nfs_show_mount_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)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  */
nfs_show_options(struct seq_file * m,struct dentry * root)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)
show_lease(struct seq_file * m,struct nfs_server * server)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
show_sessions(struct seq_file * m,struct nfs_server * server)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
show_sessions(struct seq_file * m,struct nfs_server * server)602 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
603 #endif
604 #endif
605 
606 #ifdef CONFIG_NFS_V4_1
show_pnfs(struct seq_file * m,struct nfs_server * server)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 
show_implementation_id(struct seq_file * m,struct nfs_server * nfss)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)
show_pnfs(struct seq_file * m,struct nfs_server * server)628 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
629 {
630 }
631 #endif
show_implementation_id(struct seq_file * m,struct nfs_server * nfss)632 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
633 {
634 }
635 #endif
636 
nfs_show_devname(struct seq_file * m,struct dentry * root)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 
nfs_show_path(struct seq_file * m,struct dentry * dentry)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  */
nfs_show_stats(struct seq_file * m,struct dentry * root)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  */
nfs_umount_begin(struct super_block * sb)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  */
nfs_auth_info_match(const struct nfs_auth_info * auth_info,rpc_authflavor_t match)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  */
nfs_verify_authflavors(struct nfs_fs_context * ctx,rpc_authflavor_t * server_authlist,unsigned int count)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  */
nfs_request_mount(struct fs_context * fc,struct nfs_fh * root_fh,rpc_authflavor_t * server_authlist,unsigned int * server_authlist_len)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 	if ((request.protocol == XPRT_TRANSPORT_UDP) ==
889 	    !(ctx->flags & NFS_MOUNT_TCP))
890 		/*
891 		 * NFS protocol and mount protocol are both UDP or neither UDP
892 		 * so timeouts are compatible.  Use NFS timeouts for MOUNT
893 		 */
894 		status = nfs_mount(&request, ctx->timeo, ctx->retrans);
895 	else
896 		status = nfs_mount(&request, NFS_UNSPEC_TIMEO, NFS_UNSPEC_RETRANS);
897 	if (status != 0) {
898 		dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
899 				request.hostname, status);
900 		return status;
901 	}
902 
903 	return 0;
904 }
905 
nfs_try_mount_request(struct fs_context * fc)906 static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
907 {
908 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
909 	int status;
910 	unsigned int i;
911 	bool tried_auth_unix = false;
912 	bool auth_null_in_list = false;
913 	struct nfs_server *server = ERR_PTR(-EACCES);
914 	rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
915 	unsigned int authlist_len = ARRAY_SIZE(authlist);
916 
917 	/* make sure 'nolock'/'lock' override the 'local_lock' mount option */
918 	if (ctx->lock_status) {
919 		if (ctx->lock_status == NFS_LOCK_NOLOCK) {
920 			ctx->flags |= NFS_MOUNT_NONLM;
921 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
922 		} else {
923 			ctx->flags &= ~NFS_MOUNT_NONLM;
924 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
925 		}
926 	}
927 	status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
928 	if (status)
929 		return ERR_PTR(status);
930 
931 	/*
932 	 * Was a sec= authflavor specified in the options? First, verify
933 	 * whether the server supports it, and then just try to use it if so.
934 	 */
935 	if (ctx->auth_info.flavor_len > 0) {
936 		status = nfs_verify_authflavors(ctx, authlist, authlist_len);
937 		dfprintk(MOUNT, "NFS: using auth flavor %u\n",
938 			 ctx->selected_flavor);
939 		if (status)
940 			return ERR_PTR(status);
941 		return ctx->nfs_mod->rpc_ops->create_server(fc);
942 	}
943 
944 	/*
945 	 * No sec= option was provided. RFC 2623, section 2.7 suggests we
946 	 * SHOULD prefer the flavor listed first. However, some servers list
947 	 * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
948 	 */
949 	for (i = 0; i < authlist_len; ++i) {
950 		rpc_authflavor_t flavor;
951 		struct rpcsec_gss_info info;
952 
953 		flavor = authlist[i];
954 		switch (flavor) {
955 		case RPC_AUTH_UNIX:
956 			tried_auth_unix = true;
957 			break;
958 		case RPC_AUTH_NULL:
959 			auth_null_in_list = true;
960 			continue;
961 		default:
962 			if (rpcauth_get_gssinfo(flavor, &info) != 0)
963 				continue;
964 			break;
965 		}
966 		dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
967 		ctx->selected_flavor = flavor;
968 		server = ctx->nfs_mod->rpc_ops->create_server(fc);
969 		if (!IS_ERR(server))
970 			return server;
971 	}
972 
973 	/*
974 	 * Nothing we tried so far worked. At this point, give up if we've
975 	 * already tried AUTH_UNIX or if the server's list doesn't contain
976 	 * AUTH_NULL
977 	 */
978 	if (tried_auth_unix || !auth_null_in_list)
979 		return server;
980 
981 	/* Last chance! Try AUTH_UNIX */
982 	dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
983 	ctx->selected_flavor = RPC_AUTH_UNIX;
984 	return ctx->nfs_mod->rpc_ops->create_server(fc);
985 }
986 
nfs_try_get_tree(struct fs_context * fc)987 int nfs_try_get_tree(struct fs_context *fc)
988 {
989 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
990 
991 	if (ctx->need_mount)
992 		ctx->server = nfs_try_mount_request(fc);
993 	else
994 		ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
995 
996 	return nfs_get_tree_common(fc);
997 }
998 EXPORT_SYMBOL_GPL(nfs_try_get_tree);
999 
1000 
1001 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
1002 		| NFS_MOUNT_SECURE \
1003 		| NFS_MOUNT_TCP \
1004 		| NFS_MOUNT_VER3 \
1005 		| NFS_MOUNT_KERBEROS \
1006 		| NFS_MOUNT_NONLM \
1007 		| NFS_MOUNT_BROKEN_SUID \
1008 		| NFS_MOUNT_STRICTLOCK \
1009 		| NFS_MOUNT_LEGACY_INTERFACE)
1010 
1011 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
1012 		~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
1013 
1014 static int
nfs_compare_remount_data(struct nfs_server * nfss,struct nfs_fs_context * ctx)1015 nfs_compare_remount_data(struct nfs_server *nfss,
1016 			 struct nfs_fs_context *ctx)
1017 {
1018 	if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
1019 	    ctx->rsize != nfss->rsize ||
1020 	    ctx->wsize != nfss->wsize ||
1021 	    ctx->version != nfss->nfs_client->rpc_ops->version ||
1022 	    ctx->minorversion != nfss->nfs_client->cl_minorversion ||
1023 	    ctx->retrans != nfss->client->cl_timeout->to_retries ||
1024 	    !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
1025 	    ctx->acregmin != nfss->acregmin / HZ ||
1026 	    ctx->acregmax != nfss->acregmax / HZ ||
1027 	    ctx->acdirmin != nfss->acdirmin / HZ ||
1028 	    ctx->acdirmax != nfss->acdirmax / HZ ||
1029 	    ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1030 	    (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
1031 	    ctx->nfs_server.port != nfss->port ||
1032 	    ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1033 	    !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
1034 			  (struct sockaddr *)&nfss->nfs_client->cl_addr))
1035 		return -EINVAL;
1036 
1037 	return 0;
1038 }
1039 
nfs_reconfigure(struct fs_context * fc)1040 int nfs_reconfigure(struct fs_context *fc)
1041 {
1042 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1043 	struct super_block *sb = fc->root->d_sb;
1044 	struct nfs_server *nfss = sb->s_fs_info;
1045 	int ret;
1046 
1047 	sync_filesystem(sb);
1048 
1049 	/*
1050 	 * Userspace mount programs that send binary options generally send
1051 	 * them populated with default values. We have no way to know which
1052 	 * ones were explicitly specified. Fall back to legacy behavior and
1053 	 * just return success.
1054 	 */
1055 	if (ctx->skip_reconfig_option_check)
1056 		return 0;
1057 
1058 	/*
1059 	 * noac is a special case. It implies -o sync, but that's not
1060 	 * necessarily reflected in the mtab options. reconfigure_super
1061 	 * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
1062 	 * remount options, so we have to explicitly reset it.
1063 	 */
1064 	if (ctx->flags & NFS_MOUNT_NOAC) {
1065 		fc->sb_flags |= SB_SYNCHRONOUS;
1066 		fc->sb_flags_mask |= SB_SYNCHRONOUS;
1067 	}
1068 
1069 	/* compare new mount options with old ones */
1070 	ret = nfs_compare_remount_data(nfss, ctx);
1071 	if (ret)
1072 		return ret;
1073 
1074 	return nfs_probe_server(nfss, NFS_FH(d_inode(fc->root)));
1075 }
1076 EXPORT_SYMBOL_GPL(nfs_reconfigure);
1077 
1078 /*
1079  * Finish setting up an NFS superblock
1080  */
nfs_fill_super(struct super_block * sb,struct nfs_fs_context * ctx)1081 static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
1082 {
1083 	struct nfs_server *server = NFS_SB(sb);
1084 
1085 	sb->s_blocksize_bits = 0;
1086 	sb->s_blocksize = 0;
1087 	sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1088 	sb->s_op = server->nfs_client->cl_nfs_mod->sops;
1089 	if (ctx->bsize)
1090 		sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
1091 
1092 	switch (server->nfs_client->rpc_ops->version) {
1093 	case 2:
1094 		sb->s_time_gran = 1000;
1095 		sb->s_time_min = 0;
1096 		sb->s_time_max = U32_MAX;
1097 		break;
1098 	case 3:
1099 		/*
1100 		 * The VFS shouldn't apply the umask to mode bits.
1101 		 * We will do so ourselves when necessary.
1102 		 */
1103 		sb->s_flags |= SB_POSIXACL;
1104 		sb->s_time_gran = 1;
1105 		sb->s_time_min = 0;
1106 		sb->s_time_max = U32_MAX;
1107 		sb->s_export_op = &nfs_export_ops;
1108 		break;
1109 	case 4:
1110 		sb->s_iflags |= SB_I_NOUMASK;
1111 		sb->s_time_gran = 1;
1112 		sb->s_time_min = S64_MIN;
1113 		sb->s_time_max = S64_MAX;
1114 		if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1115 			sb->s_export_op = &nfs_export_ops;
1116 		break;
1117 	}
1118 
1119 	sb->s_magic = NFS_SUPER_MAGIC;
1120 
1121 	/* We probably want something more informative here */
1122 	snprintf(sb->s_id, sizeof(sb->s_id),
1123 		 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1124 
1125 	if (sb->s_blocksize == 0)
1126 		sb->s_blocksize = nfs_block_bits(server->wsize,
1127 						 &sb->s_blocksize_bits);
1128 
1129 	nfs_super_set_maxbytes(sb, server->maxfilesize);
1130 	nfs_sysfs_move_server_to_sb(sb);
1131 	server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
1132 }
1133 
nfs_compare_mount_options(const struct super_block * s,const struct nfs_server * b,const struct fs_context * fc)1134 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
1135 				     const struct fs_context *fc)
1136 {
1137 	const struct nfs_server *a = s->s_fs_info;
1138 	const struct rpc_clnt *clnt_a = a->client;
1139 	const struct rpc_clnt *clnt_b = b->client;
1140 
1141 	if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
1142 		goto Ebusy;
1143 	if (a->nfs_client != b->nfs_client)
1144 		goto Ebusy;
1145 	if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
1146 		goto Ebusy;
1147 	if (a->wsize != b->wsize)
1148 		goto Ebusy;
1149 	if (a->rsize != b->rsize)
1150 		goto Ebusy;
1151 	if (a->acregmin != b->acregmin)
1152 		goto Ebusy;
1153 	if (a->acregmax != b->acregmax)
1154 		goto Ebusy;
1155 	if (a->acdirmin != b->acdirmin)
1156 		goto Ebusy;
1157 	if (a->acdirmax != b->acdirmax)
1158 		goto Ebusy;
1159 	if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1160 		goto Ebusy;
1161 	return 1;
1162 Ebusy:
1163 	return 0;
1164 }
1165 
nfs_set_super(struct super_block * s,struct fs_context * fc)1166 static int nfs_set_super(struct super_block *s, struct fs_context *fc)
1167 {
1168 	struct nfs_server *server = fc->s_fs_info;
1169 	int ret;
1170 
1171 	s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
1172 	ret = set_anon_super(s, server);
1173 	if (ret == 0)
1174 		server->s_dev = s->s_dev;
1175 	return ret;
1176 }
1177 
nfs_compare_super_address(struct nfs_server * server1,struct nfs_server * server2)1178 static int nfs_compare_super_address(struct nfs_server *server1,
1179 				     struct nfs_server *server2)
1180 {
1181 	struct sockaddr *sap1, *sap2;
1182 	struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1183 	struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1184 
1185 	if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1186 		return 0;
1187 
1188 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1189 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1190 
1191 	if (sap1->sa_family != sap2->sa_family)
1192 		return 0;
1193 
1194 	switch (sap1->sa_family) {
1195 	case AF_INET: {
1196 		struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1197 		struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1198 		if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1199 			return 0;
1200 		if (sin1->sin_port != sin2->sin_port)
1201 			return 0;
1202 		break;
1203 	}
1204 	case AF_INET6: {
1205 		struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1206 		struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1207 		if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1208 			return 0;
1209 		if (sin1->sin6_port != sin2->sin6_port)
1210 			return 0;
1211 		break;
1212 	}
1213 	default:
1214 		return 0;
1215 	}
1216 
1217 	return 1;
1218 }
1219 
nfs_compare_userns(const struct nfs_server * old,const struct nfs_server * new)1220 static int nfs_compare_userns(const struct nfs_server *old,
1221 		const struct nfs_server *new)
1222 {
1223 	const struct user_namespace *oldns = &init_user_ns;
1224 	const struct user_namespace *newns = &init_user_ns;
1225 
1226 	if (old->client && old->client->cl_cred)
1227 		oldns = old->client->cl_cred->user_ns;
1228 	if (new->client && new->client->cl_cred)
1229 		newns = new->client->cl_cred->user_ns;
1230 	if (oldns != newns)
1231 		return 0;
1232 	return 1;
1233 }
1234 
nfs_compare_super(struct super_block * sb,struct fs_context * fc)1235 static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
1236 {
1237 	struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
1238 
1239 	if (!nfs_compare_super_address(old, server))
1240 		return 0;
1241 	/* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1242 	if (old->flags & NFS_MOUNT_UNSHARED)
1243 		return 0;
1244 	if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1245 		return 0;
1246 	if (!nfs_compare_userns(old, server))
1247 		return 0;
1248 	if ((old->has_sec_mnt_opts || fc->security) &&
1249 			security_sb_mnt_opts_compat(sb, fc->security))
1250 		return 0;
1251 	return nfs_compare_mount_options(sb, server, fc);
1252 }
1253 
1254 #ifdef CONFIG_NFS_FSCACHE
nfs_get_cache_cookie(struct super_block * sb,struct nfs_fs_context * ctx)1255 static int nfs_get_cache_cookie(struct super_block *sb,
1256 				struct nfs_fs_context *ctx)
1257 {
1258 	struct nfs_server *nfss = NFS_SB(sb);
1259 	char *uniq = NULL;
1260 	int ulen = 0;
1261 
1262 	nfss->fscache = NULL;
1263 
1264 	if (!ctx)
1265 		return 0;
1266 
1267 	if (ctx->clone_data.sb) {
1268 		struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
1269 		if (!(mnt_s->options & NFS_OPTION_FSCACHE))
1270 			return 0;
1271 		if (mnt_s->fscache_uniq) {
1272 			uniq = mnt_s->fscache_uniq;
1273 			ulen = strlen(uniq);
1274 		}
1275 	} else {
1276 		if (!(ctx->options & NFS_OPTION_FSCACHE))
1277 			return 0;
1278 		if (ctx->fscache_uniq) {
1279 			uniq = ctx->fscache_uniq;
1280 			ulen = strlen(ctx->fscache_uniq);
1281 		}
1282 	}
1283 
1284 	return nfs_fscache_get_super_cookie(sb, uniq, ulen);
1285 }
1286 #else
nfs_get_cache_cookie(struct super_block * sb,struct nfs_fs_context * ctx)1287 static int nfs_get_cache_cookie(struct super_block *sb,
1288 				struct nfs_fs_context *ctx)
1289 {
1290 	return 0;
1291 }
1292 #endif
1293 
nfs_get_tree_common(struct fs_context * fc)1294 int nfs_get_tree_common(struct fs_context *fc)
1295 {
1296 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1297 	struct super_block *s;
1298 	int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
1299 	struct nfs_server *server = ctx->server;
1300 	int error;
1301 
1302 	ctx->server = NULL;
1303 	if (IS_ERR(server))
1304 		return PTR_ERR(server);
1305 
1306 	if (server->flags & NFS_MOUNT_UNSHARED)
1307 		compare_super = NULL;
1308 
1309 	/* -o noac implies -o sync */
1310 	if (server->flags & NFS_MOUNT_NOAC)
1311 		fc->sb_flags |= SB_SYNCHRONOUS;
1312 
1313 	if (ctx->clone_data.sb)
1314 		if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
1315 			fc->sb_flags |= SB_SYNCHRONOUS;
1316 
1317 	/* Get a superblock - note that we may end up sharing one that already exists */
1318 	fc->s_fs_info = server;
1319 	s = sget_fc(fc, compare_super, nfs_set_super);
1320 	fc->s_fs_info = NULL;
1321 	if (IS_ERR(s)) {
1322 		error = PTR_ERR(s);
1323 		nfs_errorf(fc, "NFS: Couldn't get superblock");
1324 		goto out_err_nosb;
1325 	}
1326 
1327 	if (s->s_fs_info != server) {
1328 		nfs_free_server(server);
1329 		server = NULL;
1330 	} else {
1331 		error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1332 					     MINOR(server->s_dev));
1333 		if (error)
1334 			goto error_splat_super;
1335 		s->s_bdi->io_pages = server->rpages;
1336 		server->super = s;
1337 	}
1338 
1339 	if (!s->s_root) {
1340 		unsigned bsize = ctx->clone_data.inherited_bsize;
1341 		/* initial superblock/root creation */
1342 		nfs_fill_super(s, ctx);
1343 		if (bsize) {
1344 			s->s_blocksize_bits = bsize;
1345 			s->s_blocksize = 1U << bsize;
1346 		}
1347 		error = nfs_get_cache_cookie(s, ctx);
1348 		if (error < 0)
1349 			goto error_splat_super;
1350 	}
1351 
1352 	error = nfs_get_root(s, fc);
1353 	if (error < 0) {
1354 		nfs_errorf(fc, "NFS: Couldn't get root dentry");
1355 		goto error_splat_super;
1356 	}
1357 
1358 	s->s_flags |= SB_ACTIVE;
1359 	error = 0;
1360 
1361 out:
1362 	return error;
1363 
1364 out_err_nosb:
1365 	nfs_free_server(server);
1366 	goto out;
1367 error_splat_super:
1368 	deactivate_locked_super(s);
1369 	goto out;
1370 }
1371 
1372 /*
1373  * Destroy an NFS superblock
1374  */
nfs_kill_super(struct super_block * s)1375 void nfs_kill_super(struct super_block *s)
1376 {
1377 	struct nfs_server *server = NFS_SB(s);
1378 
1379 	nfs_sysfs_move_sb_to_server(server);
1380 	kill_anon_super(s);
1381 
1382 	nfs_fscache_release_super_cookie(s);
1383 
1384 	nfs_free_server(server);
1385 }
1386 EXPORT_SYMBOL_GPL(nfs_kill_super);
1387 
1388 #if IS_ENABLED(CONFIG_NFS_V4)
1389 
1390 /*
1391  * NFS v4 module parameters need to stay in the
1392  * NFS client for backwards compatibility
1393  */
1394 unsigned int nfs_callback_set_tcpport;
1395 unsigned short nfs_callback_nr_threads;
1396 /* Default cache timeout is 10 minutes */
1397 unsigned int nfs_idmap_cache_timeout = 600;
1398 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1399 bool nfs4_disable_idmapping = true;
1400 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
1401 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
1402 unsigned short send_implementation_id = 1;
1403 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
1404 bool recover_lost_locks = false;
1405 short nfs_delay_retrans = -1;
1406 
1407 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
1408 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
1409 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1410 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1411 EXPORT_SYMBOL_GPL(max_session_slots);
1412 EXPORT_SYMBOL_GPL(max_session_cb_slots);
1413 EXPORT_SYMBOL_GPL(send_implementation_id);
1414 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
1415 EXPORT_SYMBOL_GPL(recover_lost_locks);
1416 EXPORT_SYMBOL_GPL(nfs_delay_retrans);
1417 
1418 #define NFS_CALLBACK_MAXPORTNR (65535U)
1419 
param_set_portnr(const char * val,const struct kernel_param * kp)1420 static int param_set_portnr(const char *val, const struct kernel_param *kp)
1421 {
1422 	unsigned long num;
1423 	int ret;
1424 
1425 	if (!val)
1426 		return -EINVAL;
1427 	ret = kstrtoul(val, 0, &num);
1428 	if (ret || num > NFS_CALLBACK_MAXPORTNR)
1429 		return -EINVAL;
1430 	*((unsigned int *)kp->arg) = num;
1431 	return 0;
1432 }
1433 static const struct kernel_param_ops param_ops_portnr = {
1434 	.set = param_set_portnr,
1435 	.get = param_get_uint,
1436 };
1437 #define param_check_portnr(name, p) __param_check(name, p, unsigned int)
1438 
1439 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
1440 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1441 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1442 		"assigned to the NFSv4 callback channels.");
1443 module_param(nfs_idmap_cache_timeout, int, 0644);
1444 module_param(nfs4_disable_idmapping, bool, 0644);
1445 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1446 			NFS4_CLIENT_ID_UNIQ_LEN, 0600);
1447 MODULE_PARM_DESC(nfs4_disable_idmapping,
1448 		"Turn off NFSv4 idmapping when using 'sec=sys'");
1449 module_param(max_session_slots, ushort, 0644);
1450 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1451 		"requests the client will negotiate");
1452 module_param(max_session_cb_slots, ushort, 0644);
1453 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
1454 		"callbacks the client will process for a given server");
1455 module_param(send_implementation_id, ushort, 0644);
1456 MODULE_PARM_DESC(send_implementation_id,
1457 		"Send implementation ID with NFSv4.1 exchange_id");
1458 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
1459 
1460 module_param(recover_lost_locks, bool, 0644);
1461 MODULE_PARM_DESC(recover_lost_locks,
1462 		 "If the server reports that a lock might be lost, "
1463 		 "try to recover it risking data corruption.");
1464 
1465 module_param_named(delay_retrans, nfs_delay_retrans, short, 0644);
1466 MODULE_PARM_DESC(delay_retrans,
1467 		 "Unless negative, specifies the number of times the NFSv4 "
1468 		 "client retries a request before returning an EAGAIN error, "
1469 		 "after a reply of NFS4ERR_DELAY from the server.");
1470 #endif /* CONFIG_NFS_V4 */
1471