xref: /openbmc/linux/fs/nfs/super.c (revision 54cbac81)
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
10  *
11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12  *  J.S.Peatfield@damtp.cam.ac.uk
13  *
14  *  Split from inode.c by David Howells <dhowells@redhat.com>
15  *
16  * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17  *   particular server are held in the same superblock
18  * - NFS superblocks can have several effective roots to the dentry tree
19  * - directory type roots are spliced into the tree when a path from one root reaches the root
20  *   of another (see nfs_lookup())
21  */
22 
23 #include <linux/module.h>
24 #include <linux/init.h>
25 
26 #include <linux/time.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/unistd.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/stats.h>
35 #include <linux/sunrpc/metrics.h>
36 #include <linux/sunrpc/xprtsock.h>
37 #include <linux/sunrpc/xprtrdma.h>
38 #include <linux/nfs_fs.h>
39 #include <linux/nfs_mount.h>
40 #include <linux/nfs4_mount.h>
41 #include <linux/lockd/bind.h>
42 #include <linux/seq_file.h>
43 #include <linux/mount.h>
44 #include <linux/namei.h>
45 #include <linux/nfs_idmap.h>
46 #include <linux/vfs.h>
47 #include <linux/inet.h>
48 #include <linux/in6.h>
49 #include <linux/slab.h>
50 #include <net/ipv6.h>
51 #include <linux/netdevice.h>
52 #include <linux/nfs_xdr.h>
53 #include <linux/magic.h>
54 #include <linux/parser.h>
55 #include <linux/nsproxy.h>
56 #include <linux/rcupdate.h>
57 #include <linux/kthread.h>
58 
59 #include <asm/uaccess.h>
60 
61 #include "nfs4_fs.h"
62 #include "callback.h"
63 #include "delegation.h"
64 #include "iostat.h"
65 #include "internal.h"
66 #include "fscache.h"
67 #include "nfs4session.h"
68 #include "pnfs.h"
69 #include "nfs.h"
70 
71 #define NFSDBG_FACILITY		NFSDBG_VFS
72 #define NFS_TEXT_DATA		1
73 
74 #if IS_ENABLED(CONFIG_NFS_V3)
75 #define NFS_DEFAULT_VERSION 3
76 #else
77 #define NFS_DEFAULT_VERSION 2
78 #endif
79 
80 enum {
81 	/* Mount options that take no arguments */
82 	Opt_soft, Opt_hard,
83 	Opt_posix, Opt_noposix,
84 	Opt_cto, Opt_nocto,
85 	Opt_ac, Opt_noac,
86 	Opt_lock, Opt_nolock,
87 	Opt_udp, Opt_tcp, Opt_rdma,
88 	Opt_acl, Opt_noacl,
89 	Opt_rdirplus, Opt_nordirplus,
90 	Opt_sharecache, Opt_nosharecache,
91 	Opt_resvport, Opt_noresvport,
92 	Opt_fscache, Opt_nofscache,
93 	Opt_migration, Opt_nomigration,
94 
95 	/* Mount options that take integer arguments */
96 	Opt_port,
97 	Opt_rsize, Opt_wsize, Opt_bsize,
98 	Opt_timeo, Opt_retrans,
99 	Opt_acregmin, Opt_acregmax,
100 	Opt_acdirmin, Opt_acdirmax,
101 	Opt_actimeo,
102 	Opt_namelen,
103 	Opt_mountport,
104 	Opt_mountvers,
105 	Opt_minorversion,
106 
107 	/* Mount options that take string arguments */
108 	Opt_nfsvers,
109 	Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
110 	Opt_addr, Opt_mountaddr, Opt_clientaddr,
111 	Opt_lookupcache,
112 	Opt_fscache_uniq,
113 	Opt_local_lock,
114 
115 	/* Special mount options */
116 	Opt_userspace, Opt_deprecated, Opt_sloppy,
117 
118 	Opt_err
119 };
120 
121 static const match_table_t nfs_mount_option_tokens = {
122 	{ Opt_userspace, "bg" },
123 	{ Opt_userspace, "fg" },
124 	{ Opt_userspace, "retry=%s" },
125 
126 	{ Opt_sloppy, "sloppy" },
127 
128 	{ Opt_soft, "soft" },
129 	{ Opt_hard, "hard" },
130 	{ Opt_deprecated, "intr" },
131 	{ Opt_deprecated, "nointr" },
132 	{ Opt_posix, "posix" },
133 	{ Opt_noposix, "noposix" },
134 	{ Opt_cto, "cto" },
135 	{ Opt_nocto, "nocto" },
136 	{ Opt_ac, "ac" },
137 	{ Opt_noac, "noac" },
138 	{ Opt_lock, "lock" },
139 	{ Opt_nolock, "nolock" },
140 	{ Opt_udp, "udp" },
141 	{ Opt_tcp, "tcp" },
142 	{ Opt_rdma, "rdma" },
143 	{ Opt_acl, "acl" },
144 	{ Opt_noacl, "noacl" },
145 	{ Opt_rdirplus, "rdirplus" },
146 	{ Opt_nordirplus, "nordirplus" },
147 	{ Opt_sharecache, "sharecache" },
148 	{ Opt_nosharecache, "nosharecache" },
149 	{ Opt_resvport, "resvport" },
150 	{ Opt_noresvport, "noresvport" },
151 	{ Opt_fscache, "fsc" },
152 	{ Opt_nofscache, "nofsc" },
153 	{ Opt_migration, "migration" },
154 	{ Opt_nomigration, "nomigration" },
155 
156 	{ Opt_port, "port=%s" },
157 	{ Opt_rsize, "rsize=%s" },
158 	{ Opt_wsize, "wsize=%s" },
159 	{ Opt_bsize, "bsize=%s" },
160 	{ Opt_timeo, "timeo=%s" },
161 	{ Opt_retrans, "retrans=%s" },
162 	{ Opt_acregmin, "acregmin=%s" },
163 	{ Opt_acregmax, "acregmax=%s" },
164 	{ Opt_acdirmin, "acdirmin=%s" },
165 	{ Opt_acdirmax, "acdirmax=%s" },
166 	{ Opt_actimeo, "actimeo=%s" },
167 	{ Opt_namelen, "namlen=%s" },
168 	{ Opt_mountport, "mountport=%s" },
169 	{ Opt_mountvers, "mountvers=%s" },
170 	{ Opt_minorversion, "minorversion=%s" },
171 
172 	{ Opt_nfsvers, "nfsvers=%s" },
173 	{ Opt_nfsvers, "vers=%s" },
174 
175 	{ Opt_sec, "sec=%s" },
176 	{ Opt_proto, "proto=%s" },
177 	{ Opt_mountproto, "mountproto=%s" },
178 	{ Opt_addr, "addr=%s" },
179 	{ Opt_clientaddr, "clientaddr=%s" },
180 	{ Opt_mounthost, "mounthost=%s" },
181 	{ Opt_mountaddr, "mountaddr=%s" },
182 
183 	{ Opt_lookupcache, "lookupcache=%s" },
184 	{ Opt_fscache_uniq, "fsc=%s" },
185 	{ Opt_local_lock, "local_lock=%s" },
186 
187 	/* The following needs to be listed after all other options */
188 	{ Opt_nfsvers, "v%s" },
189 
190 	{ Opt_err, NULL }
191 };
192 
193 enum {
194 	Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
195 
196 	Opt_xprt_err
197 };
198 
199 static const match_table_t nfs_xprt_protocol_tokens = {
200 	{ Opt_xprt_udp, "udp" },
201 	{ Opt_xprt_udp6, "udp6" },
202 	{ Opt_xprt_tcp, "tcp" },
203 	{ Opt_xprt_tcp6, "tcp6" },
204 	{ Opt_xprt_rdma, "rdma" },
205 
206 	{ Opt_xprt_err, NULL }
207 };
208 
209 enum {
210 	Opt_sec_none, Opt_sec_sys,
211 	Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
212 	Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
213 	Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
214 
215 	Opt_sec_err
216 };
217 
218 static const match_table_t nfs_secflavor_tokens = {
219 	{ Opt_sec_none, "none" },
220 	{ Opt_sec_none, "null" },
221 	{ Opt_sec_sys, "sys" },
222 
223 	{ Opt_sec_krb5, "krb5" },
224 	{ Opt_sec_krb5i, "krb5i" },
225 	{ Opt_sec_krb5p, "krb5p" },
226 
227 	{ Opt_sec_lkey, "lkey" },
228 	{ Opt_sec_lkeyi, "lkeyi" },
229 	{ Opt_sec_lkeyp, "lkeyp" },
230 
231 	{ Opt_sec_spkm, "spkm3" },
232 	{ Opt_sec_spkmi, "spkm3i" },
233 	{ Opt_sec_spkmp, "spkm3p" },
234 
235 	{ Opt_sec_err, NULL }
236 };
237 
238 enum {
239 	Opt_lookupcache_all, Opt_lookupcache_positive,
240 	Opt_lookupcache_none,
241 
242 	Opt_lookupcache_err
243 };
244 
245 static match_table_t nfs_lookupcache_tokens = {
246 	{ Opt_lookupcache_all, "all" },
247 	{ Opt_lookupcache_positive, "pos" },
248 	{ Opt_lookupcache_positive, "positive" },
249 	{ Opt_lookupcache_none, "none" },
250 
251 	{ Opt_lookupcache_err, NULL }
252 };
253 
254 enum {
255 	Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
256 	Opt_local_lock_none,
257 
258 	Opt_local_lock_err
259 };
260 
261 static match_table_t nfs_local_lock_tokens = {
262 	{ Opt_local_lock_all, "all" },
263 	{ Opt_local_lock_flock, "flock" },
264 	{ Opt_local_lock_posix, "posix" },
265 	{ Opt_local_lock_none, "none" },
266 
267 	{ Opt_local_lock_err, NULL }
268 };
269 
270 enum {
271 	Opt_vers_2, Opt_vers_3, Opt_vers_4, Opt_vers_4_0,
272 	Opt_vers_4_1,
273 
274 	Opt_vers_err
275 };
276 
277 static match_table_t nfs_vers_tokens = {
278 	{ Opt_vers_2, "2" },
279 	{ Opt_vers_3, "3" },
280 	{ Opt_vers_4, "4" },
281 	{ Opt_vers_4_0, "4.0" },
282 	{ Opt_vers_4_1, "4.1" },
283 
284 	{ Opt_vers_err, NULL }
285 };
286 
287 static struct dentry *nfs_xdev_mount(struct file_system_type *fs_type,
288 		int flags, const char *dev_name, void *raw_data);
289 
290 struct file_system_type nfs_fs_type = {
291 	.owner		= THIS_MODULE,
292 	.name		= "nfs",
293 	.mount		= nfs_fs_mount,
294 	.kill_sb	= nfs_kill_super,
295 	.fs_flags	= FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
296 };
297 EXPORT_SYMBOL_GPL(nfs_fs_type);
298 
299 struct file_system_type nfs_xdev_fs_type = {
300 	.owner		= THIS_MODULE,
301 	.name		= "nfs",
302 	.mount		= nfs_xdev_mount,
303 	.kill_sb	= nfs_kill_super,
304 	.fs_flags	= FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
305 };
306 
307 const struct super_operations nfs_sops = {
308 	.alloc_inode	= nfs_alloc_inode,
309 	.destroy_inode	= nfs_destroy_inode,
310 	.write_inode	= nfs_write_inode,
311 	.drop_inode	= nfs_drop_inode,
312 	.put_super	= nfs_put_super,
313 	.statfs		= nfs_statfs,
314 	.evict_inode	= nfs_evict_inode,
315 	.umount_begin	= nfs_umount_begin,
316 	.show_options	= nfs_show_options,
317 	.show_devname	= nfs_show_devname,
318 	.show_path	= nfs_show_path,
319 	.show_stats	= nfs_show_stats,
320 	.remount_fs	= nfs_remount,
321 };
322 EXPORT_SYMBOL_GPL(nfs_sops);
323 
324 #if IS_ENABLED(CONFIG_NFS_V4)
325 static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *);
326 static int nfs4_validate_mount_data(void *options,
327 	struct nfs_parsed_mount_data *args, const char *dev_name);
328 
329 struct file_system_type nfs4_fs_type = {
330 	.owner		= THIS_MODULE,
331 	.name		= "nfs4",
332 	.mount		= nfs_fs_mount,
333 	.kill_sb	= nfs_kill_super,
334 	.fs_flags	= FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
335 };
336 EXPORT_SYMBOL_GPL(nfs4_fs_type);
337 
338 static int __init register_nfs4_fs(void)
339 {
340 	return register_filesystem(&nfs4_fs_type);
341 }
342 
343 static void unregister_nfs4_fs(void)
344 {
345 	unregister_filesystem(&nfs4_fs_type);
346 }
347 #else
348 static int __init register_nfs4_fs(void)
349 {
350 	return 0;
351 }
352 
353 static void unregister_nfs4_fs(void)
354 {
355 }
356 #endif
357 
358 static struct shrinker acl_shrinker = {
359 	.shrink		= nfs_access_cache_shrinker,
360 	.seeks		= DEFAULT_SEEKS,
361 };
362 
363 /*
364  * Register the NFS filesystems
365  */
366 int __init register_nfs_fs(void)
367 {
368 	int ret;
369 
370         ret = register_filesystem(&nfs_fs_type);
371 	if (ret < 0)
372 		goto error_0;
373 
374 	ret = register_nfs4_fs();
375 	if (ret < 0)
376 		goto error_1;
377 
378 	ret = nfs_register_sysctl();
379 	if (ret < 0)
380 		goto error_2;
381 	register_shrinker(&acl_shrinker);
382 	return 0;
383 
384 error_2:
385 	unregister_nfs4_fs();
386 error_1:
387 	unregister_filesystem(&nfs_fs_type);
388 error_0:
389 	return ret;
390 }
391 
392 /*
393  * Unregister the NFS filesystems
394  */
395 void __exit unregister_nfs_fs(void)
396 {
397 	unregister_shrinker(&acl_shrinker);
398 	nfs_unregister_sysctl();
399 	unregister_nfs4_fs();
400 	unregister_filesystem(&nfs_fs_type);
401 }
402 
403 void nfs_sb_active(struct super_block *sb)
404 {
405 	struct nfs_server *server = NFS_SB(sb);
406 
407 	if (atomic_inc_return(&server->active) == 1)
408 		atomic_inc(&sb->s_active);
409 }
410 EXPORT_SYMBOL_GPL(nfs_sb_active);
411 
412 void nfs_sb_deactive(struct super_block *sb)
413 {
414 	struct nfs_server *server = NFS_SB(sb);
415 
416 	if (atomic_dec_and_test(&server->active))
417 		deactivate_super(sb);
418 }
419 EXPORT_SYMBOL_GPL(nfs_sb_deactive);
420 
421 static int nfs_deactivate_super_async_work(void *ptr)
422 {
423 	struct super_block *sb = ptr;
424 
425 	deactivate_super(sb);
426 	module_put_and_exit(0);
427 	return 0;
428 }
429 
430 /*
431  * same effect as deactivate_super, but will do final unmount in kthread
432  * context
433  */
434 static void nfs_deactivate_super_async(struct super_block *sb)
435 {
436 	struct task_struct *task;
437 	char buf[INET6_ADDRSTRLEN + 1];
438 	struct nfs_server *server = NFS_SB(sb);
439 	struct nfs_client *clp = server->nfs_client;
440 
441 	if (!atomic_add_unless(&sb->s_active, -1, 1)) {
442 		rcu_read_lock();
443 		snprintf(buf, sizeof(buf),
444 			rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
445 		rcu_read_unlock();
446 
447 		__module_get(THIS_MODULE);
448 		task = kthread_run(nfs_deactivate_super_async_work, sb,
449 				"%s-deactivate-super", buf);
450 		if (IS_ERR(task)) {
451 			pr_err("%s: kthread_run: %ld\n",
452 				__func__, PTR_ERR(task));
453 			/* make synchronous call and hope for the best */
454 			deactivate_super(sb);
455 			module_put(THIS_MODULE);
456 		}
457 	}
458 }
459 
460 void nfs_sb_deactive_async(struct super_block *sb)
461 {
462 	struct nfs_server *server = NFS_SB(sb);
463 
464 	if (atomic_dec_and_test(&server->active))
465 		nfs_deactivate_super_async(sb);
466 }
467 EXPORT_SYMBOL_GPL(nfs_sb_deactive_async);
468 
469 /*
470  * Deliver file system statistics to userspace
471  */
472 int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
473 {
474 	struct nfs_server *server = NFS_SB(dentry->d_sb);
475 	unsigned char blockbits;
476 	unsigned long blockres;
477 	struct nfs_fh *fh = NFS_FH(dentry->d_inode);
478 	struct nfs_fsstat res;
479 	int error = -ENOMEM;
480 
481 	res.fattr = nfs_alloc_fattr();
482 	if (res.fattr == NULL)
483 		goto out_err;
484 
485 	error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
486 	if (unlikely(error == -ESTALE)) {
487 		struct dentry *pd_dentry;
488 
489 		pd_dentry = dget_parent(dentry);
490 		if (pd_dentry != NULL) {
491 			nfs_zap_caches(pd_dentry->d_inode);
492 			dput(pd_dentry);
493 		}
494 	}
495 	nfs_free_fattr(res.fattr);
496 	if (error < 0)
497 		goto out_err;
498 
499 	buf->f_type = NFS_SUPER_MAGIC;
500 
501 	/*
502 	 * Current versions of glibc do not correctly handle the
503 	 * case where f_frsize != f_bsize.  Eventually we want to
504 	 * report the value of wtmult in this field.
505 	 */
506 	buf->f_frsize = dentry->d_sb->s_blocksize;
507 
508 	/*
509 	 * On most *nix systems, f_blocks, f_bfree, and f_bavail
510 	 * are reported in units of f_frsize.  Linux hasn't had
511 	 * an f_frsize field in its statfs struct until recently,
512 	 * thus historically Linux's sys_statfs reports these
513 	 * fields in units of f_bsize.
514 	 */
515 	buf->f_bsize = dentry->d_sb->s_blocksize;
516 	blockbits = dentry->d_sb->s_blocksize_bits;
517 	blockres = (1 << blockbits) - 1;
518 	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
519 	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
520 	buf->f_bavail = (res.abytes + blockres) >> blockbits;
521 
522 	buf->f_files = res.tfiles;
523 	buf->f_ffree = res.afiles;
524 
525 	buf->f_namelen = server->namelen;
526 
527 	return 0;
528 
529  out_err:
530 	dprintk("%s: statfs error = %d\n", __func__, -error);
531 	return error;
532 }
533 EXPORT_SYMBOL_GPL(nfs_statfs);
534 
535 /*
536  * Map the security flavour number to a name
537  */
538 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
539 {
540 	static const struct {
541 		rpc_authflavor_t flavour;
542 		const char *str;
543 	} sec_flavours[] = {
544 		{ RPC_AUTH_NULL, "null" },
545 		{ RPC_AUTH_UNIX, "sys" },
546 		{ RPC_AUTH_GSS_KRB5, "krb5" },
547 		{ RPC_AUTH_GSS_KRB5I, "krb5i" },
548 		{ RPC_AUTH_GSS_KRB5P, "krb5p" },
549 		{ RPC_AUTH_GSS_LKEY, "lkey" },
550 		{ RPC_AUTH_GSS_LKEYI, "lkeyi" },
551 		{ RPC_AUTH_GSS_LKEYP, "lkeyp" },
552 		{ RPC_AUTH_GSS_SPKM, "spkm" },
553 		{ RPC_AUTH_GSS_SPKMI, "spkmi" },
554 		{ RPC_AUTH_GSS_SPKMP, "spkmp" },
555 		{ UINT_MAX, "unknown" }
556 	};
557 	int i;
558 
559 	for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
560 		if (sec_flavours[i].flavour == flavour)
561 			break;
562 	}
563 	return sec_flavours[i].str;
564 }
565 
566 static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
567 				  int showdefaults)
568 {
569 	struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
570 
571 	seq_printf(m, ",mountproto=");
572 	switch (sap->sa_family) {
573 	case AF_INET:
574 		switch (nfss->mountd_protocol) {
575 		case IPPROTO_UDP:
576 			seq_printf(m, RPCBIND_NETID_UDP);
577 			break;
578 		case IPPROTO_TCP:
579 			seq_printf(m, RPCBIND_NETID_TCP);
580 			break;
581 		default:
582 			if (showdefaults)
583 				seq_printf(m, "auto");
584 		}
585 		break;
586 	case AF_INET6:
587 		switch (nfss->mountd_protocol) {
588 		case IPPROTO_UDP:
589 			seq_printf(m, RPCBIND_NETID_UDP6);
590 			break;
591 		case IPPROTO_TCP:
592 			seq_printf(m, RPCBIND_NETID_TCP6);
593 			break;
594 		default:
595 			if (showdefaults)
596 				seq_printf(m, "auto");
597 		}
598 		break;
599 	default:
600 		if (showdefaults)
601 			seq_printf(m, "auto");
602 	}
603 }
604 
605 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
606 				    int showdefaults)
607 {
608 	struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
609 
610 	if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
611 		return;
612 
613 	switch (sap->sa_family) {
614 	case AF_INET: {
615 		struct sockaddr_in *sin = (struct sockaddr_in *)sap;
616 		seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
617 		break;
618 	}
619 	case AF_INET6: {
620 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
621 		seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
622 		break;
623 	}
624 	default:
625 		if (showdefaults)
626 			seq_printf(m, ",mountaddr=unspecified");
627 	}
628 
629 	if (nfss->mountd_version || showdefaults)
630 		seq_printf(m, ",mountvers=%u", nfss->mountd_version);
631 	if ((nfss->mountd_port &&
632 		nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
633 		showdefaults)
634 		seq_printf(m, ",mountport=%u", nfss->mountd_port);
635 
636 	nfs_show_mountd_netid(m, nfss, showdefaults);
637 }
638 
639 #if IS_ENABLED(CONFIG_NFS_V4)
640 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
641 				    int showdefaults)
642 {
643 	struct nfs_client *clp = nfss->nfs_client;
644 
645 	seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
646 }
647 #else
648 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
649 				    int showdefaults)
650 {
651 }
652 #endif
653 
654 static void nfs_show_nfs_version(struct seq_file *m,
655 		unsigned int version,
656 		unsigned int minorversion)
657 {
658 	seq_printf(m, ",vers=%u", version);
659 	if (version == 4)
660 		seq_printf(m, ".%u", minorversion);
661 }
662 
663 /*
664  * Describe the mount options in force on this server representation
665  */
666 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
667 				   int showdefaults)
668 {
669 	static const struct proc_nfs_info {
670 		int flag;
671 		const char *str;
672 		const char *nostr;
673 	} nfs_info[] = {
674 		{ NFS_MOUNT_SOFT, ",soft", ",hard" },
675 		{ NFS_MOUNT_POSIX, ",posix", "" },
676 		{ NFS_MOUNT_NOCTO, ",nocto", "" },
677 		{ NFS_MOUNT_NOAC, ",noac", "" },
678 		{ NFS_MOUNT_NONLM, ",nolock", "" },
679 		{ NFS_MOUNT_NOACL, ",noacl", "" },
680 		{ NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
681 		{ NFS_MOUNT_UNSHARED, ",nosharecache", "" },
682 		{ NFS_MOUNT_NORESVPORT, ",noresvport", "" },
683 		{ 0, NULL, NULL }
684 	};
685 	const struct proc_nfs_info *nfs_infop;
686 	struct nfs_client *clp = nfss->nfs_client;
687 	u32 version = clp->rpc_ops->version;
688 	int local_flock, local_fcntl;
689 
690 	nfs_show_nfs_version(m, version, clp->cl_minorversion);
691 	seq_printf(m, ",rsize=%u", nfss->rsize);
692 	seq_printf(m, ",wsize=%u", nfss->wsize);
693 	if (nfss->bsize != 0)
694 		seq_printf(m, ",bsize=%u", nfss->bsize);
695 	seq_printf(m, ",namlen=%u", nfss->namelen);
696 	if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
697 		seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
698 	if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
699 		seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
700 	if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
701 		seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
702 	if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
703 		seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
704 	for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
705 		if (nfss->flags & nfs_infop->flag)
706 			seq_puts(m, nfs_infop->str);
707 		else
708 			seq_puts(m, nfs_infop->nostr);
709 	}
710 	rcu_read_lock();
711 	seq_printf(m, ",proto=%s",
712 		   rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
713 	rcu_read_unlock();
714 	if (version == 4) {
715 		if (nfss->port != NFS_PORT)
716 			seq_printf(m, ",port=%u", nfss->port);
717 	} else
718 		if (nfss->port)
719 			seq_printf(m, ",port=%u", nfss->port);
720 
721 	seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
722 	seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
723 	seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
724 
725 	if (version != 4)
726 		nfs_show_mountd_options(m, nfss, showdefaults);
727 	else
728 		nfs_show_nfsv4_options(m, nfss, showdefaults);
729 
730 	if (nfss->options & NFS_OPTION_FSCACHE)
731 		seq_printf(m, ",fsc");
732 
733 	if (nfss->options & NFS_OPTION_MIGRATION)
734 		seq_printf(m, ",migration");
735 
736 	if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
737 		if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
738 			seq_printf(m, ",lookupcache=none");
739 		else
740 			seq_printf(m, ",lookupcache=pos");
741 	}
742 
743 	local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
744 	local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
745 
746 	if (!local_flock && !local_fcntl)
747 		seq_printf(m, ",local_lock=none");
748 	else if (local_flock && local_fcntl)
749 		seq_printf(m, ",local_lock=all");
750 	else if (local_flock)
751 		seq_printf(m, ",local_lock=flock");
752 	else
753 		seq_printf(m, ",local_lock=posix");
754 }
755 
756 /*
757  * Describe the mount options on this VFS mountpoint
758  */
759 int nfs_show_options(struct seq_file *m, struct dentry *root)
760 {
761 	struct nfs_server *nfss = NFS_SB(root->d_sb);
762 
763 	nfs_show_mount_options(m, nfss, 0);
764 
765 	rcu_read_lock();
766 	seq_printf(m, ",addr=%s",
767 			rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
768 							RPC_DISPLAY_ADDR));
769 	rcu_read_unlock();
770 
771 	return 0;
772 }
773 EXPORT_SYMBOL_GPL(nfs_show_options);
774 
775 #if IS_ENABLED(CONFIG_NFS_V4)
776 #ifdef CONFIG_NFS_V4_1
777 static void show_sessions(struct seq_file *m, struct nfs_server *server)
778 {
779 	if (nfs4_has_session(server->nfs_client))
780 		seq_printf(m, ",sessions");
781 }
782 #else
783 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
784 #endif
785 #endif
786 
787 #ifdef CONFIG_NFS_V4_1
788 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
789 {
790 	seq_printf(m, ",pnfs=");
791 	if (server->pnfs_curr_ld)
792 		seq_printf(m, "%s", server->pnfs_curr_ld->name);
793 	else
794 		seq_printf(m, "not configured");
795 }
796 
797 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
798 {
799 	if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
800 		struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
801 		seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
802 			   "date='%llu,%u'",
803 			   impl_id->name, impl_id->domain,
804 			   impl_id->date.seconds, impl_id->date.nseconds);
805 	}
806 }
807 #else
808 #if IS_ENABLED(CONFIG_NFS_V4)
809 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
810 {
811 }
812 #endif
813 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
814 {
815 }
816 #endif
817 
818 int nfs_show_devname(struct seq_file *m, struct dentry *root)
819 {
820 	char *page = (char *) __get_free_page(GFP_KERNEL);
821 	char *devname, *dummy;
822 	int err = 0;
823 	if (!page)
824 		return -ENOMEM;
825 	devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
826 	if (IS_ERR(devname))
827 		err = PTR_ERR(devname);
828 	else
829 		seq_escape(m, devname, " \t\n\\");
830 	free_page((unsigned long)page);
831 	return err;
832 }
833 EXPORT_SYMBOL_GPL(nfs_show_devname);
834 
835 int nfs_show_path(struct seq_file *m, struct dentry *dentry)
836 {
837 	seq_puts(m, "/");
838 	return 0;
839 }
840 EXPORT_SYMBOL_GPL(nfs_show_path);
841 
842 /*
843  * Present statistical information for this VFS mountpoint
844  */
845 int nfs_show_stats(struct seq_file *m, struct dentry *root)
846 {
847 	int i, cpu;
848 	struct nfs_server *nfss = NFS_SB(root->d_sb);
849 	struct rpc_auth *auth = nfss->client->cl_auth;
850 	struct nfs_iostats totals = { };
851 
852 	seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
853 
854 	/*
855 	 * Display all mount option settings
856 	 */
857 	seq_printf(m, "\n\topts:\t");
858 	seq_puts(m, root->d_sb->s_flags & MS_RDONLY ? "ro" : "rw");
859 	seq_puts(m, root->d_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
860 	seq_puts(m, root->d_sb->s_flags & MS_NOATIME ? ",noatime" : "");
861 	seq_puts(m, root->d_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
862 	nfs_show_mount_options(m, nfss, 1);
863 
864 	seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
865 
866 	show_implementation_id(m, nfss);
867 
868 	seq_printf(m, "\n\tcaps:\t");
869 	seq_printf(m, "caps=0x%x", nfss->caps);
870 	seq_printf(m, ",wtmult=%u", nfss->wtmult);
871 	seq_printf(m, ",dtsize=%u", nfss->dtsize);
872 	seq_printf(m, ",bsize=%u", nfss->bsize);
873 	seq_printf(m, ",namlen=%u", nfss->namelen);
874 
875 #if IS_ENABLED(CONFIG_NFS_V4)
876 	if (nfss->nfs_client->rpc_ops->version == 4) {
877 		seq_printf(m, "\n\tnfsv4:\t");
878 		seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
879 		seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
880 		seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
881 		show_sessions(m, nfss);
882 		show_pnfs(m, nfss);
883 	}
884 #endif
885 
886 	/*
887 	 * Display security flavor in effect for this mount
888 	 */
889 	seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
890 	if (auth->au_flavor)
891 		seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
892 
893 	/*
894 	 * Display superblock I/O counters
895 	 */
896 	for_each_possible_cpu(cpu) {
897 		struct nfs_iostats *stats;
898 
899 		preempt_disable();
900 		stats = per_cpu_ptr(nfss->io_stats, cpu);
901 
902 		for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
903 			totals.events[i] += stats->events[i];
904 		for (i = 0; i < __NFSIOS_BYTESMAX; i++)
905 			totals.bytes[i] += stats->bytes[i];
906 #ifdef CONFIG_NFS_FSCACHE
907 		for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
908 			totals.fscache[i] += stats->fscache[i];
909 #endif
910 
911 		preempt_enable();
912 	}
913 
914 	seq_printf(m, "\n\tevents:\t");
915 	for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
916 		seq_printf(m, "%lu ", totals.events[i]);
917 	seq_printf(m, "\n\tbytes:\t");
918 	for (i = 0; i < __NFSIOS_BYTESMAX; i++)
919 		seq_printf(m, "%Lu ", totals.bytes[i]);
920 #ifdef CONFIG_NFS_FSCACHE
921 	if (nfss->options & NFS_OPTION_FSCACHE) {
922 		seq_printf(m, "\n\tfsc:\t");
923 		for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
924 			seq_printf(m, "%Lu ", totals.bytes[i]);
925 	}
926 #endif
927 	seq_printf(m, "\n");
928 
929 	rpc_print_iostats(m, nfss->client);
930 
931 	return 0;
932 }
933 EXPORT_SYMBOL_GPL(nfs_show_stats);
934 
935 /*
936  * Begin unmount by attempting to remove all automounted mountpoints we added
937  * in response to xdev traversals and referrals
938  */
939 void nfs_umount_begin(struct super_block *sb)
940 {
941 	struct nfs_server *server;
942 	struct rpc_clnt *rpc;
943 
944 	server = NFS_SB(sb);
945 	/* -EIO all pending I/O */
946 	rpc = server->client_acl;
947 	if (!IS_ERR(rpc))
948 		rpc_killall_tasks(rpc);
949 	rpc = server->client;
950 	if (!IS_ERR(rpc))
951 		rpc_killall_tasks(rpc);
952 }
953 EXPORT_SYMBOL_GPL(nfs_umount_begin);
954 
955 static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(void)
956 {
957 	struct nfs_parsed_mount_data *data;
958 
959 	data = kzalloc(sizeof(*data), GFP_KERNEL);
960 	if (data) {
961 		data->acregmin		= NFS_DEF_ACREGMIN;
962 		data->acregmax		= NFS_DEF_ACREGMAX;
963 		data->acdirmin		= NFS_DEF_ACDIRMIN;
964 		data->acdirmax		= NFS_DEF_ACDIRMAX;
965 		data->mount_server.port	= NFS_UNSPEC_PORT;
966 		data->nfs_server.port	= NFS_UNSPEC_PORT;
967 		data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
968 		data->auth_flavors[0]	= RPC_AUTH_UNIX;
969 		data->auth_flavor_len	= 1;
970 		data->minorversion	= 0;
971 		data->need_mount	= true;
972 		data->net		= current->nsproxy->net_ns;
973 		security_init_mnt_opts(&data->lsm_opts);
974 	}
975 	return data;
976 }
977 
978 static void nfs_free_parsed_mount_data(struct nfs_parsed_mount_data *data)
979 {
980 	if (data) {
981 		kfree(data->client_address);
982 		kfree(data->mount_server.hostname);
983 		kfree(data->nfs_server.export_path);
984 		kfree(data->nfs_server.hostname);
985 		kfree(data->fscache_uniq);
986 		security_free_mnt_opts(&data->lsm_opts);
987 		kfree(data);
988 	}
989 }
990 
991 /*
992  * Sanity-check a server address provided by the mount command.
993  *
994  * Address family must be initialized, and address must not be
995  * the ANY address for that family.
996  */
997 static int nfs_verify_server_address(struct sockaddr *addr)
998 {
999 	switch (addr->sa_family) {
1000 	case AF_INET: {
1001 		struct sockaddr_in *sa = (struct sockaddr_in *)addr;
1002 		return sa->sin_addr.s_addr != htonl(INADDR_ANY);
1003 	}
1004 	case AF_INET6: {
1005 		struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
1006 		return !ipv6_addr_any(sa);
1007 	}
1008 	}
1009 
1010 	dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
1011 	return 0;
1012 }
1013 
1014 /*
1015  * Select between a default port value and a user-specified port value.
1016  * If a zero value is set, then autobind will be used.
1017  */
1018 static void nfs_set_port(struct sockaddr *sap, int *port,
1019 				 const unsigned short default_port)
1020 {
1021 	if (*port == NFS_UNSPEC_PORT)
1022 		*port = default_port;
1023 
1024 	rpc_set_port(sap, *port);
1025 }
1026 
1027 /*
1028  * Sanity check the NFS transport protocol.
1029  *
1030  */
1031 static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
1032 {
1033 	switch (mnt->nfs_server.protocol) {
1034 	case XPRT_TRANSPORT_UDP:
1035 	case XPRT_TRANSPORT_TCP:
1036 	case XPRT_TRANSPORT_RDMA:
1037 		break;
1038 	default:
1039 		mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1040 	}
1041 }
1042 
1043 /*
1044  * For text based NFSv2/v3 mounts, the mount protocol transport default
1045  * settings should depend upon the specified NFS transport.
1046  */
1047 static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
1048 {
1049 	nfs_validate_transport_protocol(mnt);
1050 
1051 	if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
1052 	    mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
1053 			return;
1054 	switch (mnt->nfs_server.protocol) {
1055 	case XPRT_TRANSPORT_UDP:
1056 		mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1057 		break;
1058 	case XPRT_TRANSPORT_TCP:
1059 	case XPRT_TRANSPORT_RDMA:
1060 		mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1061 	}
1062 }
1063 
1064 /*
1065  * Parse the value of the 'sec=' option.
1066  */
1067 static int nfs_parse_security_flavors(char *value,
1068 				      struct nfs_parsed_mount_data *mnt)
1069 {
1070 	substring_t args[MAX_OPT_ARGS];
1071 
1072 	dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
1073 
1074 	switch (match_token(value, nfs_secflavor_tokens, args)) {
1075 	case Opt_sec_none:
1076 		mnt->auth_flavors[0] = RPC_AUTH_NULL;
1077 		break;
1078 	case Opt_sec_sys:
1079 		mnt->auth_flavors[0] = RPC_AUTH_UNIX;
1080 		break;
1081 	case Opt_sec_krb5:
1082 		mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
1083 		break;
1084 	case Opt_sec_krb5i:
1085 		mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
1086 		break;
1087 	case Opt_sec_krb5p:
1088 		mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
1089 		break;
1090 	case Opt_sec_lkey:
1091 		mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
1092 		break;
1093 	case Opt_sec_lkeyi:
1094 		mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
1095 		break;
1096 	case Opt_sec_lkeyp:
1097 		mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
1098 		break;
1099 	case Opt_sec_spkm:
1100 		mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
1101 		break;
1102 	case Opt_sec_spkmi:
1103 		mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
1104 		break;
1105 	case Opt_sec_spkmp:
1106 		mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
1107 		break;
1108 	default:
1109 		return 0;
1110 	}
1111 
1112 	mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1113 	mnt->auth_flavor_len = 1;
1114 	return 1;
1115 }
1116 
1117 static int nfs_parse_version_string(char *string,
1118 		struct nfs_parsed_mount_data *mnt,
1119 		substring_t *args)
1120 {
1121 	mnt->flags &= ~NFS_MOUNT_VER3;
1122 	switch (match_token(string, nfs_vers_tokens, args)) {
1123 	case Opt_vers_2:
1124 		mnt->version = 2;
1125 		break;
1126 	case Opt_vers_3:
1127 		mnt->flags |= NFS_MOUNT_VER3;
1128 		mnt->version = 3;
1129 		break;
1130 	case Opt_vers_4:
1131 		/* Backward compatibility option. In future,
1132 		 * the mount program should always supply
1133 		 * a NFSv4 minor version number.
1134 		 */
1135 		mnt->version = 4;
1136 		break;
1137 	case Opt_vers_4_0:
1138 		mnt->version = 4;
1139 		mnt->minorversion = 0;
1140 		break;
1141 	case Opt_vers_4_1:
1142 		mnt->version = 4;
1143 		mnt->minorversion = 1;
1144 		break;
1145 	default:
1146 		return 0;
1147 	}
1148 	return 1;
1149 }
1150 
1151 static int nfs_get_option_str(substring_t args[], char **option)
1152 {
1153 	kfree(*option);
1154 	*option = match_strdup(args);
1155 	return !*option;
1156 }
1157 
1158 static int nfs_get_option_ul(substring_t args[], unsigned long *option)
1159 {
1160 	int rc;
1161 	char *string;
1162 
1163 	string = match_strdup(args);
1164 	if (string == NULL)
1165 		return -ENOMEM;
1166 	rc = kstrtoul(string, 10, option);
1167 	kfree(string);
1168 
1169 	return rc;
1170 }
1171 
1172 /*
1173  * Error-check and convert a string of mount options from user space into
1174  * a data structure.  The whole mount string is processed; bad options are
1175  * skipped as they are encountered.  If there were no errors, return 1;
1176  * otherwise return 0 (zero).
1177  */
1178 static int nfs_parse_mount_options(char *raw,
1179 				   struct nfs_parsed_mount_data *mnt)
1180 {
1181 	char *p, *string, *secdata;
1182 	int rc, sloppy = 0, invalid_option = 0;
1183 	unsigned short protofamily = AF_UNSPEC;
1184 	unsigned short mountfamily = AF_UNSPEC;
1185 
1186 	if (!raw) {
1187 		dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
1188 		return 1;
1189 	}
1190 	dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
1191 
1192 	secdata = alloc_secdata();
1193 	if (!secdata)
1194 		goto out_nomem;
1195 
1196 	rc = security_sb_copy_data(raw, secdata);
1197 	if (rc)
1198 		goto out_security_failure;
1199 
1200 	rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
1201 	if (rc)
1202 		goto out_security_failure;
1203 
1204 	free_secdata(secdata);
1205 
1206 	while ((p = strsep(&raw, ",")) != NULL) {
1207 		substring_t args[MAX_OPT_ARGS];
1208 		unsigned long option;
1209 		int token;
1210 
1211 		if (!*p)
1212 			continue;
1213 
1214 		dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
1215 
1216 		token = match_token(p, nfs_mount_option_tokens, args);
1217 		switch (token) {
1218 
1219 		/*
1220 		 * boolean options:  foo/nofoo
1221 		 */
1222 		case Opt_soft:
1223 			mnt->flags |= NFS_MOUNT_SOFT;
1224 			break;
1225 		case Opt_hard:
1226 			mnt->flags &= ~NFS_MOUNT_SOFT;
1227 			break;
1228 		case Opt_posix:
1229 			mnt->flags |= NFS_MOUNT_POSIX;
1230 			break;
1231 		case Opt_noposix:
1232 			mnt->flags &= ~NFS_MOUNT_POSIX;
1233 			break;
1234 		case Opt_cto:
1235 			mnt->flags &= ~NFS_MOUNT_NOCTO;
1236 			break;
1237 		case Opt_nocto:
1238 			mnt->flags |= NFS_MOUNT_NOCTO;
1239 			break;
1240 		case Opt_ac:
1241 			mnt->flags &= ~NFS_MOUNT_NOAC;
1242 			break;
1243 		case Opt_noac:
1244 			mnt->flags |= NFS_MOUNT_NOAC;
1245 			break;
1246 		case Opt_lock:
1247 			mnt->flags &= ~NFS_MOUNT_NONLM;
1248 			mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1249 					NFS_MOUNT_LOCAL_FCNTL);
1250 			break;
1251 		case Opt_nolock:
1252 			mnt->flags |= NFS_MOUNT_NONLM;
1253 			mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1254 				       NFS_MOUNT_LOCAL_FCNTL);
1255 			break;
1256 		case Opt_udp:
1257 			mnt->flags &= ~NFS_MOUNT_TCP;
1258 			mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1259 			break;
1260 		case Opt_tcp:
1261 			mnt->flags |= NFS_MOUNT_TCP;
1262 			mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1263 			break;
1264 		case Opt_rdma:
1265 			mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1266 			mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1267 			xprt_load_transport(p);
1268 			break;
1269 		case Opt_acl:
1270 			mnt->flags &= ~NFS_MOUNT_NOACL;
1271 			break;
1272 		case Opt_noacl:
1273 			mnt->flags |= NFS_MOUNT_NOACL;
1274 			break;
1275 		case Opt_rdirplus:
1276 			mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1277 			break;
1278 		case Opt_nordirplus:
1279 			mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1280 			break;
1281 		case Opt_sharecache:
1282 			mnt->flags &= ~NFS_MOUNT_UNSHARED;
1283 			break;
1284 		case Opt_nosharecache:
1285 			mnt->flags |= NFS_MOUNT_UNSHARED;
1286 			break;
1287 		case Opt_resvport:
1288 			mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1289 			break;
1290 		case Opt_noresvport:
1291 			mnt->flags |= NFS_MOUNT_NORESVPORT;
1292 			break;
1293 		case Opt_fscache:
1294 			mnt->options |= NFS_OPTION_FSCACHE;
1295 			kfree(mnt->fscache_uniq);
1296 			mnt->fscache_uniq = NULL;
1297 			break;
1298 		case Opt_nofscache:
1299 			mnt->options &= ~NFS_OPTION_FSCACHE;
1300 			kfree(mnt->fscache_uniq);
1301 			mnt->fscache_uniq = NULL;
1302 			break;
1303 		case Opt_migration:
1304 			mnt->options |= NFS_OPTION_MIGRATION;
1305 			break;
1306 		case Opt_nomigration:
1307 			mnt->options &= NFS_OPTION_MIGRATION;
1308 			break;
1309 
1310 		/*
1311 		 * options that take numeric values
1312 		 */
1313 		case Opt_port:
1314 			if (nfs_get_option_ul(args, &option) ||
1315 			    option > USHRT_MAX)
1316 				goto out_invalid_value;
1317 			mnt->nfs_server.port = option;
1318 			break;
1319 		case Opt_rsize:
1320 			if (nfs_get_option_ul(args, &option))
1321 				goto out_invalid_value;
1322 			mnt->rsize = option;
1323 			break;
1324 		case Opt_wsize:
1325 			if (nfs_get_option_ul(args, &option))
1326 				goto out_invalid_value;
1327 			mnt->wsize = option;
1328 			break;
1329 		case Opt_bsize:
1330 			if (nfs_get_option_ul(args, &option))
1331 				goto out_invalid_value;
1332 			mnt->bsize = option;
1333 			break;
1334 		case Opt_timeo:
1335 			if (nfs_get_option_ul(args, &option) || option == 0)
1336 				goto out_invalid_value;
1337 			mnt->timeo = option;
1338 			break;
1339 		case Opt_retrans:
1340 			if (nfs_get_option_ul(args, &option) || option == 0)
1341 				goto out_invalid_value;
1342 			mnt->retrans = option;
1343 			break;
1344 		case Opt_acregmin:
1345 			if (nfs_get_option_ul(args, &option))
1346 				goto out_invalid_value;
1347 			mnt->acregmin = option;
1348 			break;
1349 		case Opt_acregmax:
1350 			if (nfs_get_option_ul(args, &option))
1351 				goto out_invalid_value;
1352 			mnt->acregmax = option;
1353 			break;
1354 		case Opt_acdirmin:
1355 			if (nfs_get_option_ul(args, &option))
1356 				goto out_invalid_value;
1357 			mnt->acdirmin = option;
1358 			break;
1359 		case Opt_acdirmax:
1360 			if (nfs_get_option_ul(args, &option))
1361 				goto out_invalid_value;
1362 			mnt->acdirmax = option;
1363 			break;
1364 		case Opt_actimeo:
1365 			if (nfs_get_option_ul(args, &option))
1366 				goto out_invalid_value;
1367 			mnt->acregmin = mnt->acregmax =
1368 			mnt->acdirmin = mnt->acdirmax = option;
1369 			break;
1370 		case Opt_namelen:
1371 			if (nfs_get_option_ul(args, &option))
1372 				goto out_invalid_value;
1373 			mnt->namlen = option;
1374 			break;
1375 		case Opt_mountport:
1376 			if (nfs_get_option_ul(args, &option) ||
1377 			    option > USHRT_MAX)
1378 				goto out_invalid_value;
1379 			mnt->mount_server.port = option;
1380 			break;
1381 		case Opt_mountvers:
1382 			if (nfs_get_option_ul(args, &option) ||
1383 			    option < NFS_MNT_VERSION ||
1384 			    option > NFS_MNT3_VERSION)
1385 				goto out_invalid_value;
1386 			mnt->mount_server.version = option;
1387 			break;
1388 		case Opt_minorversion:
1389 			if (nfs_get_option_ul(args, &option))
1390 				goto out_invalid_value;
1391 			if (option > NFS4_MAX_MINOR_VERSION)
1392 				goto out_invalid_value;
1393 			mnt->minorversion = option;
1394 			break;
1395 
1396 		/*
1397 		 * options that take text values
1398 		 */
1399 		case Opt_nfsvers:
1400 			string = match_strdup(args);
1401 			if (string == NULL)
1402 				goto out_nomem;
1403 			rc = nfs_parse_version_string(string, mnt, args);
1404 			kfree(string);
1405 			if (!rc)
1406 				goto out_invalid_value;
1407 			break;
1408 		case Opt_sec:
1409 			string = match_strdup(args);
1410 			if (string == NULL)
1411 				goto out_nomem;
1412 			rc = nfs_parse_security_flavors(string, mnt);
1413 			kfree(string);
1414 			if (!rc) {
1415 				dfprintk(MOUNT, "NFS:   unrecognized "
1416 						"security flavor\n");
1417 				return 0;
1418 			}
1419 			break;
1420 		case Opt_proto:
1421 			string = match_strdup(args);
1422 			if (string == NULL)
1423 				goto out_nomem;
1424 			token = match_token(string,
1425 					    nfs_xprt_protocol_tokens, args);
1426 
1427 			protofamily = AF_INET;
1428 			switch (token) {
1429 			case Opt_xprt_udp6:
1430 				protofamily = AF_INET6;
1431 			case Opt_xprt_udp:
1432 				mnt->flags &= ~NFS_MOUNT_TCP;
1433 				mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1434 				break;
1435 			case Opt_xprt_tcp6:
1436 				protofamily = AF_INET6;
1437 			case Opt_xprt_tcp:
1438 				mnt->flags |= NFS_MOUNT_TCP;
1439 				mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1440 				break;
1441 			case Opt_xprt_rdma:
1442 				/* vector side protocols to TCP */
1443 				mnt->flags |= NFS_MOUNT_TCP;
1444 				mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1445 				xprt_load_transport(string);
1446 				break;
1447 			default:
1448 				dfprintk(MOUNT, "NFS:   unrecognized "
1449 						"transport protocol\n");
1450 				kfree(string);
1451 				return 0;
1452 			}
1453 			kfree(string);
1454 			break;
1455 		case Opt_mountproto:
1456 			string = match_strdup(args);
1457 			if (string == NULL)
1458 				goto out_nomem;
1459 			token = match_token(string,
1460 					    nfs_xprt_protocol_tokens, args);
1461 			kfree(string);
1462 
1463 			mountfamily = AF_INET;
1464 			switch (token) {
1465 			case Opt_xprt_udp6:
1466 				mountfamily = AF_INET6;
1467 			case Opt_xprt_udp:
1468 				mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1469 				break;
1470 			case Opt_xprt_tcp6:
1471 				mountfamily = AF_INET6;
1472 			case Opt_xprt_tcp:
1473 				mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1474 				break;
1475 			case Opt_xprt_rdma: /* not used for side protocols */
1476 			default:
1477 				dfprintk(MOUNT, "NFS:   unrecognized "
1478 						"transport protocol\n");
1479 				return 0;
1480 			}
1481 			break;
1482 		case Opt_addr:
1483 			string = match_strdup(args);
1484 			if (string == NULL)
1485 				goto out_nomem;
1486 			mnt->nfs_server.addrlen =
1487 				rpc_pton(mnt->net, string, strlen(string),
1488 					(struct sockaddr *)
1489 					&mnt->nfs_server.address,
1490 					sizeof(mnt->nfs_server.address));
1491 			kfree(string);
1492 			if (mnt->nfs_server.addrlen == 0)
1493 				goto out_invalid_address;
1494 			break;
1495 		case Opt_clientaddr:
1496 			if (nfs_get_option_str(args, &mnt->client_address))
1497 				goto out_nomem;
1498 			break;
1499 		case Opt_mounthost:
1500 			if (nfs_get_option_str(args,
1501 					       &mnt->mount_server.hostname))
1502 				goto out_nomem;
1503 			break;
1504 		case Opt_mountaddr:
1505 			string = match_strdup(args);
1506 			if (string == NULL)
1507 				goto out_nomem;
1508 			mnt->mount_server.addrlen =
1509 				rpc_pton(mnt->net, string, strlen(string),
1510 					(struct sockaddr *)
1511 					&mnt->mount_server.address,
1512 					sizeof(mnt->mount_server.address));
1513 			kfree(string);
1514 			if (mnt->mount_server.addrlen == 0)
1515 				goto out_invalid_address;
1516 			break;
1517 		case Opt_lookupcache:
1518 			string = match_strdup(args);
1519 			if (string == NULL)
1520 				goto out_nomem;
1521 			token = match_token(string,
1522 					nfs_lookupcache_tokens, args);
1523 			kfree(string);
1524 			switch (token) {
1525 				case Opt_lookupcache_all:
1526 					mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1527 					break;
1528 				case Opt_lookupcache_positive:
1529 					mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1530 					mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1531 					break;
1532 				case Opt_lookupcache_none:
1533 					mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1534 					break;
1535 				default:
1536 					dfprintk(MOUNT, "NFS:   invalid "
1537 							"lookupcache argument\n");
1538 					return 0;
1539 			};
1540 			break;
1541 		case Opt_fscache_uniq:
1542 			if (nfs_get_option_str(args, &mnt->fscache_uniq))
1543 				goto out_nomem;
1544 			mnt->options |= NFS_OPTION_FSCACHE;
1545 			break;
1546 		case Opt_local_lock:
1547 			string = match_strdup(args);
1548 			if (string == NULL)
1549 				goto out_nomem;
1550 			token = match_token(string, nfs_local_lock_tokens,
1551 					args);
1552 			kfree(string);
1553 			switch (token) {
1554 			case Opt_local_lock_all:
1555 				mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1556 					       NFS_MOUNT_LOCAL_FCNTL);
1557 				break;
1558 			case Opt_local_lock_flock:
1559 				mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1560 				break;
1561 			case Opt_local_lock_posix:
1562 				mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1563 				break;
1564 			case Opt_local_lock_none:
1565 				mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1566 						NFS_MOUNT_LOCAL_FCNTL);
1567 				break;
1568 			default:
1569 				dfprintk(MOUNT, "NFS:	invalid	"
1570 						"local_lock argument\n");
1571 				return 0;
1572 			};
1573 			break;
1574 
1575 		/*
1576 		 * Special options
1577 		 */
1578 		case Opt_sloppy:
1579 			sloppy = 1;
1580 			dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
1581 			break;
1582 		case Opt_userspace:
1583 		case Opt_deprecated:
1584 			dfprintk(MOUNT, "NFS:   ignoring mount option "
1585 					"'%s'\n", p);
1586 			break;
1587 
1588 		default:
1589 			invalid_option = 1;
1590 			dfprintk(MOUNT, "NFS:   unrecognized mount option "
1591 					"'%s'\n", p);
1592 		}
1593 	}
1594 
1595 	if (!sloppy && invalid_option)
1596 		return 0;
1597 
1598 	if (mnt->minorversion && mnt->version != 4)
1599 		goto out_minorversion_mismatch;
1600 
1601 	if (mnt->options & NFS_OPTION_MIGRATION &&
1602 	    mnt->version != 4 && mnt->minorversion != 0)
1603 		goto out_migration_misuse;
1604 
1605 	/*
1606 	 * verify that any proto=/mountproto= options match the address
1607 	 * families in the addr=/mountaddr= options.
1608 	 */
1609 	if (protofamily != AF_UNSPEC &&
1610 	    protofamily != mnt->nfs_server.address.ss_family)
1611 		goto out_proto_mismatch;
1612 
1613 	if (mountfamily != AF_UNSPEC) {
1614 		if (mnt->mount_server.addrlen) {
1615 			if (mountfamily != mnt->mount_server.address.ss_family)
1616 				goto out_mountproto_mismatch;
1617 		} else {
1618 			if (mountfamily != mnt->nfs_server.address.ss_family)
1619 				goto out_mountproto_mismatch;
1620 		}
1621 	}
1622 
1623 	return 1;
1624 
1625 out_mountproto_mismatch:
1626 	printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1627 			 "option\n");
1628 	return 0;
1629 out_proto_mismatch:
1630 	printk(KERN_INFO "NFS: server address does not match proto= option\n");
1631 	return 0;
1632 out_invalid_address:
1633 	printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1634 	return 0;
1635 out_invalid_value:
1636 	printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
1637 	return 0;
1638 out_minorversion_mismatch:
1639 	printk(KERN_INFO "NFS: mount option vers=%u does not support "
1640 			 "minorversion=%u\n", mnt->version, mnt->minorversion);
1641 	return 0;
1642 out_migration_misuse:
1643 	printk(KERN_INFO
1644 		"NFS: 'migration' not supported for this NFS version\n");
1645 	return 0;
1646 out_nomem:
1647 	printk(KERN_INFO "NFS: not enough memory to parse option\n");
1648 	return 0;
1649 out_security_failure:
1650 	free_secdata(secdata);
1651 	printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1652 	return 0;
1653 }
1654 
1655 /*
1656  * Match the requested auth flavors with the list returned by
1657  * the server.  Returns zero and sets the mount's authentication
1658  * flavor on success; returns -EACCES if server does not support
1659  * the requested flavor.
1660  */
1661 static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1662 			     struct nfs_mount_request *request)
1663 {
1664 	unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1665 
1666 	/*
1667 	 * Certain releases of Linux's mountd return an empty
1668 	 * flavor list.  To prevent behavioral regression with
1669 	 * these servers (ie. rejecting mounts that used to
1670 	 * succeed), revert to pre-2.6.32 behavior (no checking)
1671 	 * if the returned flavor list is empty.
1672 	 */
1673 	if (server_authlist_len == 0)
1674 		return 0;
1675 
1676 	/*
1677 	 * We avoid sophisticated negotiating here, as there are
1678 	 * plenty of cases where we can get it wrong, providing
1679 	 * either too little or too much security.
1680 	 *
1681 	 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1682 	 * flavor listed first.  However, some servers list
1683 	 * AUTH_NULL first.  Our caller plants AUTH_SYS, the
1684 	 * preferred default, in args->auth_flavors[0] if user
1685 	 * didn't specify sec= mount option.
1686 	 */
1687 	for (i = 0; i < args->auth_flavor_len; i++)
1688 		for (j = 0; j < server_authlist_len; j++)
1689 			if (args->auth_flavors[i] == request->auth_flavs[j]) {
1690 				dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1691 					request->auth_flavs[j]);
1692 				args->auth_flavors[0] = request->auth_flavs[j];
1693 				return 0;
1694 			}
1695 
1696 	dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1697 	nfs_umount(request);
1698 	return -EACCES;
1699 }
1700 
1701 /*
1702  * Use the remote server's MOUNT service to request the NFS file handle
1703  * corresponding to the provided path.
1704  */
1705 static int nfs_request_mount(struct nfs_parsed_mount_data *args,
1706 			     struct nfs_fh *root_fh)
1707 {
1708 	rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1709 	unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
1710 	struct nfs_mount_request request = {
1711 		.sap		= (struct sockaddr *)
1712 						&args->mount_server.address,
1713 		.dirpath	= args->nfs_server.export_path,
1714 		.protocol	= args->mount_server.protocol,
1715 		.fh		= root_fh,
1716 		.noresvport	= args->flags & NFS_MOUNT_NORESVPORT,
1717 		.auth_flav_len	= &server_authlist_len,
1718 		.auth_flavs	= server_authlist,
1719 		.net		= args->net,
1720 	};
1721 	int status;
1722 
1723 	if (args->mount_server.version == 0) {
1724 		switch (args->version) {
1725 			default:
1726 				args->mount_server.version = NFS_MNT3_VERSION;
1727 				break;
1728 			case 2:
1729 				args->mount_server.version = NFS_MNT_VERSION;
1730 		}
1731 	}
1732 	request.version = args->mount_server.version;
1733 
1734 	if (args->mount_server.hostname)
1735 		request.hostname = args->mount_server.hostname;
1736 	else
1737 		request.hostname = args->nfs_server.hostname;
1738 
1739 	/*
1740 	 * Construct the mount server's address.
1741 	 */
1742 	if (args->mount_server.address.ss_family == AF_UNSPEC) {
1743 		memcpy(request.sap, &args->nfs_server.address,
1744 		       args->nfs_server.addrlen);
1745 		args->mount_server.addrlen = args->nfs_server.addrlen;
1746 	}
1747 	request.salen = args->mount_server.addrlen;
1748 	nfs_set_port(request.sap, &args->mount_server.port, 0);
1749 
1750 	/*
1751 	 * Now ask the mount server to map our export path
1752 	 * to a file handle.
1753 	 */
1754 	status = nfs_mount(&request);
1755 	if (status != 0) {
1756 		dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1757 				request.hostname, status);
1758 		return status;
1759 	}
1760 
1761 	/*
1762 	 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1763 	 */
1764 	if (args->mount_server.version != NFS_MNT3_VERSION)
1765 		return 0;
1766 	return nfs_walk_authlist(args, &request);
1767 }
1768 
1769 struct dentry *nfs_try_mount(int flags, const char *dev_name,
1770 			     struct nfs_mount_info *mount_info,
1771 			     struct nfs_subversion *nfs_mod)
1772 {
1773 	int status;
1774 	struct nfs_server *server;
1775 
1776 	if (mount_info->parsed->need_mount) {
1777 		status = nfs_request_mount(mount_info->parsed, mount_info->mntfh);
1778 		if (status)
1779 			return ERR_PTR(status);
1780 	}
1781 
1782 	/* Get a volume representation */
1783 	server = nfs_mod->rpc_ops->create_server(mount_info, nfs_mod);
1784 	if (IS_ERR(server))
1785 		return ERR_CAST(server);
1786 
1787 	return nfs_fs_mount_common(server, flags, dev_name, mount_info, nfs_mod);
1788 }
1789 EXPORT_SYMBOL_GPL(nfs_try_mount);
1790 
1791 /*
1792  * Split "dev_name" into "hostname:export_path".
1793  *
1794  * The leftmost colon demarks the split between the server's hostname
1795  * and the export path.  If the hostname starts with a left square
1796  * bracket, then it may contain colons.
1797  *
1798  * Note: caller frees hostname and export path, even on error.
1799  */
1800 static int nfs_parse_devname(const char *dev_name,
1801 			     char **hostname, size_t maxnamlen,
1802 			     char **export_path, size_t maxpathlen)
1803 {
1804 	size_t len;
1805 	char *end;
1806 
1807 	/* Is the host name protected with square brakcets? */
1808 	if (*dev_name == '[') {
1809 		end = strchr(++dev_name, ']');
1810 		if (end == NULL || end[1] != ':')
1811 			goto out_bad_devname;
1812 
1813 		len = end - dev_name;
1814 		end++;
1815 	} else {
1816 		char *comma;
1817 
1818 		end = strchr(dev_name, ':');
1819 		if (end == NULL)
1820 			goto out_bad_devname;
1821 		len = end - dev_name;
1822 
1823 		/* kill possible hostname list: not supported */
1824 		comma = strchr(dev_name, ',');
1825 		if (comma != NULL && comma < end)
1826 			*comma = 0;
1827 	}
1828 
1829 	if (len > maxnamlen)
1830 		goto out_hostname;
1831 
1832 	/* N.B. caller will free nfs_server.hostname in all cases */
1833 	*hostname = kstrndup(dev_name, len, GFP_KERNEL);
1834 	if (*hostname == NULL)
1835 		goto out_nomem;
1836 	len = strlen(++end);
1837 	if (len > maxpathlen)
1838 		goto out_path;
1839 	*export_path = kstrndup(end, len, GFP_KERNEL);
1840 	if (!*export_path)
1841 		goto out_nomem;
1842 
1843 	dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1844 	return 0;
1845 
1846 out_bad_devname:
1847 	dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1848 	return -EINVAL;
1849 
1850 out_nomem:
1851 	dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1852 	return -ENOMEM;
1853 
1854 out_hostname:
1855 	dfprintk(MOUNT, "NFS: server hostname too long\n");
1856 	return -ENAMETOOLONG;
1857 
1858 out_path:
1859 	dfprintk(MOUNT, "NFS: export pathname too long\n");
1860 	return -ENAMETOOLONG;
1861 }
1862 
1863 /*
1864  * Validate the NFS2/NFS3 mount data
1865  * - fills in the mount root filehandle
1866  *
1867  * For option strings, user space handles the following behaviors:
1868  *
1869  * + DNS: mapping server host name to IP address ("addr=" option)
1870  *
1871  * + failure mode: how to behave if a mount request can't be handled
1872  *   immediately ("fg/bg" option)
1873  *
1874  * + retry: how often to retry a mount request ("retry=" option)
1875  *
1876  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1877  *   mountproto=tcp after mountproto=udp, and so on
1878  */
1879 static int nfs23_validate_mount_data(void *options,
1880 				     struct nfs_parsed_mount_data *args,
1881 				     struct nfs_fh *mntfh,
1882 				     const char *dev_name)
1883 {
1884 	struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1885 	struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
1886 
1887 	if (data == NULL)
1888 		goto out_no_data;
1889 
1890 	args->version = NFS_DEFAULT_VERSION;
1891 	switch (data->version) {
1892 	case 1:
1893 		data->namlen = 0;
1894 	case 2:
1895 		data->bsize = 0;
1896 	case 3:
1897 		if (data->flags & NFS_MOUNT_VER3)
1898 			goto out_no_v3;
1899 		data->root.size = NFS2_FHSIZE;
1900 		memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1901 	case 4:
1902 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
1903 			goto out_no_sec;
1904 	case 5:
1905 		memset(data->context, 0, sizeof(data->context));
1906 	case 6:
1907 		if (data->flags & NFS_MOUNT_VER3) {
1908 			if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1909 				goto out_invalid_fh;
1910 			mntfh->size = data->root.size;
1911 			args->version = 3;
1912 		} else {
1913 			mntfh->size = NFS2_FHSIZE;
1914 			args->version = 2;
1915 		}
1916 
1917 
1918 		memcpy(mntfh->data, data->root.data, mntfh->size);
1919 		if (mntfh->size < sizeof(mntfh->data))
1920 			memset(mntfh->data + mntfh->size, 0,
1921 			       sizeof(mntfh->data) - mntfh->size);
1922 
1923 		/*
1924 		 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1925 		 * can deal with.
1926 		 */
1927 		args->flags		= data->flags & NFS_MOUNT_FLAGMASK;
1928 		args->flags		|= NFS_MOUNT_LEGACY_INTERFACE;
1929 		args->rsize		= data->rsize;
1930 		args->wsize		= data->wsize;
1931 		args->timeo		= data->timeo;
1932 		args->retrans		= data->retrans;
1933 		args->acregmin		= data->acregmin;
1934 		args->acregmax		= data->acregmax;
1935 		args->acdirmin		= data->acdirmin;
1936 		args->acdirmax		= data->acdirmax;
1937 		args->need_mount	= false;
1938 
1939 		memcpy(sap, &data->addr, sizeof(data->addr));
1940 		args->nfs_server.addrlen = sizeof(data->addr);
1941 		args->nfs_server.port = ntohs(data->addr.sin_port);
1942 		if (!nfs_verify_server_address(sap))
1943 			goto out_no_address;
1944 
1945 		if (!(data->flags & NFS_MOUNT_TCP))
1946 			args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1947 		/* N.B. caller will free nfs_server.hostname in all cases */
1948 		args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1949 		args->namlen		= data->namlen;
1950 		args->bsize		= data->bsize;
1951 
1952 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
1953 			args->auth_flavors[0] = data->pseudoflavor;
1954 		if (!args->nfs_server.hostname)
1955 			goto out_nomem;
1956 
1957 		if (!(data->flags & NFS_MOUNT_NONLM))
1958 			args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1959 					 NFS_MOUNT_LOCAL_FCNTL);
1960 		else
1961 			args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1962 					NFS_MOUNT_LOCAL_FCNTL);
1963 		/*
1964 		 * The legacy version 6 binary mount data from userspace has a
1965 		 * field used only to transport selinux information into the
1966 		 * the kernel.  To continue to support that functionality we
1967 		 * have a touch of selinux knowledge here in the NFS code. The
1968 		 * userspace code converted context=blah to just blah so we are
1969 		 * converting back to the full string selinux understands.
1970 		 */
1971 		if (data->context[0]){
1972 #ifdef CONFIG_SECURITY_SELINUX
1973 			int rc;
1974 			char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1975 			if (!opts_str)
1976 				return -ENOMEM;
1977 			strcpy(opts_str, "context=");
1978 			data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1979 			strcat(opts_str, &data->context[0]);
1980 			rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1981 			kfree(opts_str);
1982 			if (rc)
1983 				return rc;
1984 #else
1985 			return -EINVAL;
1986 #endif
1987 		}
1988 
1989 		break;
1990 	default:
1991 		return NFS_TEXT_DATA;
1992 	}
1993 
1994 #if !IS_ENABLED(CONFIG_NFS_V3)
1995 	if (args->version == 3)
1996 		goto out_v3_not_compiled;
1997 #endif /* !CONFIG_NFS_V3 */
1998 
1999 	return 0;
2000 
2001 out_no_data:
2002 	dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
2003 	return -EINVAL;
2004 
2005 out_no_v3:
2006 	dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
2007 		 data->version);
2008 	return -EINVAL;
2009 
2010 out_no_sec:
2011 	dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
2012 	return -EINVAL;
2013 
2014 #if !IS_ENABLED(CONFIG_NFS_V3)
2015 out_v3_not_compiled:
2016 	dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
2017 	return -EPROTONOSUPPORT;
2018 #endif /* !CONFIG_NFS_V3 */
2019 
2020 out_nomem:
2021 	dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
2022 	return -ENOMEM;
2023 
2024 out_no_address:
2025 	dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2026 	return -EINVAL;
2027 
2028 out_invalid_fh:
2029 	dfprintk(MOUNT, "NFS: invalid root filehandle\n");
2030 	return -EINVAL;
2031 }
2032 
2033 #if IS_ENABLED(CONFIG_NFS_V4)
2034 static int nfs_validate_mount_data(struct file_system_type *fs_type,
2035 				   void *options,
2036 				   struct nfs_parsed_mount_data *args,
2037 				   struct nfs_fh *mntfh,
2038 				   const char *dev_name)
2039 {
2040 	if (fs_type == &nfs_fs_type)
2041 		return nfs23_validate_mount_data(options, args, mntfh, dev_name);
2042 	return nfs4_validate_mount_data(options, args, dev_name);
2043 }
2044 #else
2045 static int nfs_validate_mount_data(struct file_system_type *fs_type,
2046 				   void *options,
2047 				   struct nfs_parsed_mount_data *args,
2048 				   struct nfs_fh *mntfh,
2049 				   const char *dev_name)
2050 {
2051 	return nfs23_validate_mount_data(options, args, mntfh, dev_name);
2052 }
2053 #endif
2054 
2055 static int nfs_validate_text_mount_data(void *options,
2056 					struct nfs_parsed_mount_data *args,
2057 					const char *dev_name)
2058 {
2059 	int port = 0;
2060 	int max_namelen = PAGE_SIZE;
2061 	int max_pathlen = NFS_MAXPATHLEN;
2062 	struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2063 
2064 	if (nfs_parse_mount_options((char *)options, args) == 0)
2065 		return -EINVAL;
2066 
2067 	if (!nfs_verify_server_address(sap))
2068 		goto out_no_address;
2069 
2070 	if (args->version == 4) {
2071 #if IS_ENABLED(CONFIG_NFS_V4)
2072 		port = NFS_PORT;
2073 		max_namelen = NFS4_MAXNAMLEN;
2074 		max_pathlen = NFS4_MAXPATHLEN;
2075 		nfs_validate_transport_protocol(args);
2076 		nfs4_validate_mount_flags(args);
2077 #else
2078 		goto out_v4_not_compiled;
2079 #endif /* CONFIG_NFS_V4 */
2080 	} else
2081 		nfs_set_mount_transport_protocol(args);
2082 
2083 	nfs_set_port(sap, &args->nfs_server.port, port);
2084 
2085 	if (args->auth_flavor_len > 1)
2086 		goto out_bad_auth;
2087 
2088 	return nfs_parse_devname(dev_name,
2089 				   &args->nfs_server.hostname,
2090 				   max_namelen,
2091 				   &args->nfs_server.export_path,
2092 				   max_pathlen);
2093 
2094 #if !IS_ENABLED(CONFIG_NFS_V4)
2095 out_v4_not_compiled:
2096 	dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
2097 	return -EPROTONOSUPPORT;
2098 #endif /* !CONFIG_NFS_V4 */
2099 
2100 out_no_address:
2101 	dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2102 	return -EINVAL;
2103 
2104 out_bad_auth:
2105 	dfprintk(MOUNT, "NFS: Too many RPC auth flavours specified\n");
2106 	return -EINVAL;
2107 }
2108 
2109 static int
2110 nfs_compare_remount_data(struct nfs_server *nfss,
2111 			 struct nfs_parsed_mount_data *data)
2112 {
2113 	if (data->flags != nfss->flags ||
2114 	    data->rsize != nfss->rsize ||
2115 	    data->wsize != nfss->wsize ||
2116 	    data->retrans != nfss->client->cl_timeout->to_retries ||
2117 	    data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
2118 	    data->acregmin != nfss->acregmin / HZ ||
2119 	    data->acregmax != nfss->acregmax / HZ ||
2120 	    data->acdirmin != nfss->acdirmin / HZ ||
2121 	    data->acdirmax != nfss->acdirmax / HZ ||
2122 	    data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
2123 	    data->nfs_server.port != nfss->port ||
2124 	    data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
2125 	    !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2126 			  (struct sockaddr *)&nfss->nfs_client->cl_addr))
2127 		return -EINVAL;
2128 
2129 	return 0;
2130 }
2131 
2132 int
2133 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2134 {
2135 	int error;
2136 	struct nfs_server *nfss = sb->s_fs_info;
2137 	struct nfs_parsed_mount_data *data;
2138 	struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2139 	struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
2140 	u32 nfsvers = nfss->nfs_client->rpc_ops->version;
2141 
2142 	/*
2143 	 * Userspace mount programs that send binary options generally send
2144 	 * them populated with default values. We have no way to know which
2145 	 * ones were explicitly specified. Fall back to legacy behavior and
2146 	 * just return success.
2147 	 */
2148 	if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2149 	    (nfsvers <= 3 && (!options || (options->version >= 1 &&
2150 					   options->version <= 6))))
2151 		return 0;
2152 
2153 	data = kzalloc(sizeof(*data), GFP_KERNEL);
2154 	if (data == NULL)
2155 		return -ENOMEM;
2156 
2157 	/* fill out struct with values from existing mount */
2158 	data->flags = nfss->flags;
2159 	data->rsize = nfss->rsize;
2160 	data->wsize = nfss->wsize;
2161 	data->retrans = nfss->client->cl_timeout->to_retries;
2162 	data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
2163 	data->acregmin = nfss->acregmin / HZ;
2164 	data->acregmax = nfss->acregmax / HZ;
2165 	data->acdirmin = nfss->acdirmin / HZ;
2166 	data->acdirmax = nfss->acdirmax / HZ;
2167 	data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
2168 	data->nfs_server.port = nfss->port;
2169 	data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2170 	memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2171 		data->nfs_server.addrlen);
2172 
2173 	/* overwrite those values with any that were specified */
2174 	error = nfs_parse_mount_options((char *)options, data);
2175 	if (error < 0)
2176 		goto out;
2177 
2178 	/*
2179 	 * noac is a special case. It implies -o sync, but that's not
2180 	 * necessarily reflected in the mtab options. do_remount_sb
2181 	 * will clear MS_SYNCHRONOUS if -o sync wasn't specified in the
2182 	 * remount options, so we have to explicitly reset it.
2183 	 */
2184 	if (data->flags & NFS_MOUNT_NOAC)
2185 		*flags |= MS_SYNCHRONOUS;
2186 
2187 	/* compare new mount options with old ones */
2188 	error = nfs_compare_remount_data(nfss, data);
2189 out:
2190 	kfree(data);
2191 	return error;
2192 }
2193 EXPORT_SYMBOL_GPL(nfs_remount);
2194 
2195 /*
2196  * Initialise the common bits of the superblock
2197  */
2198 inline void nfs_initialise_sb(struct super_block *sb)
2199 {
2200 	struct nfs_server *server = NFS_SB(sb);
2201 
2202 	sb->s_magic = NFS_SUPER_MAGIC;
2203 
2204 	/* We probably want something more informative here */
2205 	snprintf(sb->s_id, sizeof(sb->s_id),
2206 		 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2207 
2208 	if (sb->s_blocksize == 0)
2209 		sb->s_blocksize = nfs_block_bits(server->wsize,
2210 						 &sb->s_blocksize_bits);
2211 
2212 	sb->s_bdi = &server->backing_dev_info;
2213 
2214 	nfs_super_set_maxbytes(sb, server->maxfilesize);
2215 }
2216 
2217 /*
2218  * Finish setting up an NFS2/3 superblock
2219  */
2220 void nfs_fill_super(struct super_block *sb, struct nfs_mount_info *mount_info)
2221 {
2222 	struct nfs_parsed_mount_data *data = mount_info->parsed;
2223 	struct nfs_server *server = NFS_SB(sb);
2224 
2225 	sb->s_blocksize_bits = 0;
2226 	sb->s_blocksize = 0;
2227 	sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
2228 	sb->s_op = server->nfs_client->cl_nfs_mod->sops;
2229 	if (data && data->bsize)
2230 		sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2231 
2232 	if (server->nfs_client->rpc_ops->version != 2) {
2233 		/* The VFS shouldn't apply the umask to mode bits. We will do
2234 		 * so ourselves when necessary.
2235 		 */
2236 		sb->s_flags |= MS_POSIXACL;
2237 		sb->s_time_gran = 1;
2238 	}
2239 
2240  	nfs_initialise_sb(sb);
2241 }
2242 EXPORT_SYMBOL_GPL(nfs_fill_super);
2243 
2244 /*
2245  * Finish setting up a cloned NFS2/3/4 superblock
2246  */
2247 void nfs_clone_super(struct super_block *sb, struct nfs_mount_info *mount_info)
2248 {
2249 	const struct super_block *old_sb = mount_info->cloned->sb;
2250 	struct nfs_server *server = NFS_SB(sb);
2251 
2252 	sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2253 	sb->s_blocksize = old_sb->s_blocksize;
2254 	sb->s_maxbytes = old_sb->s_maxbytes;
2255 	sb->s_xattr = old_sb->s_xattr;
2256 	sb->s_op = old_sb->s_op;
2257 	sb->s_time_gran = 1;
2258 
2259 	if (server->nfs_client->rpc_ops->version != 2) {
2260 		/* The VFS shouldn't apply the umask to mode bits. We will do
2261 		 * so ourselves when necessary.
2262 		 */
2263 		sb->s_flags |= MS_POSIXACL;
2264 	}
2265 
2266  	nfs_initialise_sb(sb);
2267 }
2268 
2269 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2270 {
2271 	const struct nfs_server *a = s->s_fs_info;
2272 	const struct rpc_clnt *clnt_a = a->client;
2273 	const struct rpc_clnt *clnt_b = b->client;
2274 
2275 	if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2276 		goto Ebusy;
2277 	if (a->nfs_client != b->nfs_client)
2278 		goto Ebusy;
2279 	if (a->flags != b->flags)
2280 		goto Ebusy;
2281 	if (a->wsize != b->wsize)
2282 		goto Ebusy;
2283 	if (a->rsize != b->rsize)
2284 		goto Ebusy;
2285 	if (a->acregmin != b->acregmin)
2286 		goto Ebusy;
2287 	if (a->acregmax != b->acregmax)
2288 		goto Ebusy;
2289 	if (a->acdirmin != b->acdirmin)
2290 		goto Ebusy;
2291 	if (a->acdirmax != b->acdirmax)
2292 		goto Ebusy;
2293 	if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2294 		goto Ebusy;
2295 	return 1;
2296 Ebusy:
2297 	return 0;
2298 }
2299 
2300 struct nfs_sb_mountdata {
2301 	struct nfs_server *server;
2302 	int mntflags;
2303 };
2304 
2305 static int nfs_set_super(struct super_block *s, void *data)
2306 {
2307 	struct nfs_sb_mountdata *sb_mntdata = data;
2308 	struct nfs_server *server = sb_mntdata->server;
2309 	int ret;
2310 
2311 	s->s_flags = sb_mntdata->mntflags;
2312 	s->s_fs_info = server;
2313 	s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
2314 	ret = set_anon_super(s, server);
2315 	if (ret == 0)
2316 		server->s_dev = s->s_dev;
2317 	return ret;
2318 }
2319 
2320 static int nfs_compare_super_address(struct nfs_server *server1,
2321 				     struct nfs_server *server2)
2322 {
2323 	struct sockaddr *sap1, *sap2;
2324 
2325 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2326 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2327 
2328 	if (sap1->sa_family != sap2->sa_family)
2329 		return 0;
2330 
2331 	switch (sap1->sa_family) {
2332 	case AF_INET: {
2333 		struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2334 		struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2335 		if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2336 			return 0;
2337 		if (sin1->sin_port != sin2->sin_port)
2338 			return 0;
2339 		break;
2340 	}
2341 	case AF_INET6: {
2342 		struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2343 		struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2344 		if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2345 			return 0;
2346 		if (sin1->sin6_port != sin2->sin6_port)
2347 			return 0;
2348 		break;
2349 	}
2350 	default:
2351 		return 0;
2352 	}
2353 
2354 	return 1;
2355 }
2356 
2357 static int nfs_compare_super(struct super_block *sb, void *data)
2358 {
2359 	struct nfs_sb_mountdata *sb_mntdata = data;
2360 	struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2361 	int mntflags = sb_mntdata->mntflags;
2362 
2363 	if (!nfs_compare_super_address(old, server))
2364 		return 0;
2365 	/* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2366 	if (old->flags & NFS_MOUNT_UNSHARED)
2367 		return 0;
2368 	if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2369 		return 0;
2370 	return nfs_compare_mount_options(sb, server, mntflags);
2371 }
2372 
2373 #ifdef CONFIG_NFS_FSCACHE
2374 static void nfs_get_cache_cookie(struct super_block *sb,
2375 				 struct nfs_parsed_mount_data *parsed,
2376 				 struct nfs_clone_mount *cloned)
2377 {
2378 	struct nfs_server *nfss = NFS_SB(sb);
2379 	char *uniq = NULL;
2380 	int ulen = 0;
2381 
2382 	nfss->fscache_key = NULL;
2383 	nfss->fscache = NULL;
2384 
2385 	if (parsed) {
2386 		if (!(parsed->options & NFS_OPTION_FSCACHE))
2387 			return;
2388 		if (parsed->fscache_uniq) {
2389 			uniq = parsed->fscache_uniq;
2390 			ulen = strlen(parsed->fscache_uniq);
2391 		}
2392 	} else if (cloned) {
2393 		struct nfs_server *mnt_s = NFS_SB(cloned->sb);
2394 		if (!(mnt_s->options & NFS_OPTION_FSCACHE))
2395 			return;
2396 		if (mnt_s->fscache_key) {
2397 			uniq = mnt_s->fscache_key->key.uniquifier;
2398 			ulen = mnt_s->fscache_key->key.uniq_len;
2399 		};
2400 	} else
2401 		return;
2402 
2403 	nfs_fscache_get_super_cookie(sb, uniq, ulen);
2404 }
2405 #else
2406 static void nfs_get_cache_cookie(struct super_block *sb,
2407 				 struct nfs_parsed_mount_data *parsed,
2408 				 struct nfs_clone_mount *cloned)
2409 {
2410 }
2411 #endif
2412 
2413 static int nfs_bdi_register(struct nfs_server *server)
2414 {
2415 	return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2416 }
2417 
2418 int nfs_set_sb_security(struct super_block *s, struct dentry *mntroot,
2419 			struct nfs_mount_info *mount_info)
2420 {
2421 	return security_sb_set_mnt_opts(s, &mount_info->parsed->lsm_opts);
2422 }
2423 EXPORT_SYMBOL_GPL(nfs_set_sb_security);
2424 
2425 int nfs_clone_sb_security(struct super_block *s, struct dentry *mntroot,
2426 			  struct nfs_mount_info *mount_info)
2427 {
2428 	/* clone any lsm security options from the parent to the new sb */
2429 	security_sb_clone_mnt_opts(mount_info->cloned->sb, s);
2430 	if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops)
2431 		return -ESTALE;
2432 	return 0;
2433 }
2434 EXPORT_SYMBOL_GPL(nfs_clone_sb_security);
2435 
2436 struct dentry *nfs_fs_mount_common(struct nfs_server *server,
2437 				   int flags, const char *dev_name,
2438 				   struct nfs_mount_info *mount_info,
2439 				   struct nfs_subversion *nfs_mod)
2440 {
2441 	struct super_block *s;
2442 	struct dentry *mntroot = ERR_PTR(-ENOMEM);
2443 	int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2444 	struct nfs_sb_mountdata sb_mntdata = {
2445 		.mntflags = flags,
2446 		.server = server,
2447 	};
2448 	int error;
2449 
2450 	if (server->flags & NFS_MOUNT_UNSHARED)
2451 		compare_super = NULL;
2452 
2453 	/* -o noac implies -o sync */
2454 	if (server->flags & NFS_MOUNT_NOAC)
2455 		sb_mntdata.mntflags |= MS_SYNCHRONOUS;
2456 
2457 	/* Get a superblock - note that we may end up sharing one that already exists */
2458 	s = sget(nfs_mod->nfs_fs, compare_super, nfs_set_super, flags, &sb_mntdata);
2459 	if (IS_ERR(s)) {
2460 		mntroot = ERR_CAST(s);
2461 		goto out_err_nosb;
2462 	}
2463 
2464 	if (s->s_fs_info != server) {
2465 		nfs_free_server(server);
2466 		server = NULL;
2467 	} else {
2468 		error = nfs_bdi_register(server);
2469 		if (error) {
2470 			mntroot = ERR_PTR(error);
2471 			goto error_splat_bdi;
2472 		}
2473 	}
2474 
2475 	if (!s->s_root) {
2476 		/* initial superblock/root creation */
2477 		mount_info->fill_super(s, mount_info);
2478 		nfs_get_cache_cookie(s, mount_info->parsed, mount_info->cloned);
2479 	}
2480 
2481 	mntroot = nfs_get_root(s, mount_info->mntfh, dev_name);
2482 	if (IS_ERR(mntroot))
2483 		goto error_splat_super;
2484 
2485 	error = mount_info->set_security(s, mntroot, mount_info);
2486 	if (error)
2487 		goto error_splat_root;
2488 
2489 	s->s_flags |= MS_ACTIVE;
2490 
2491 out:
2492 	return mntroot;
2493 
2494 out_err_nosb:
2495 	nfs_free_server(server);
2496 	goto out;
2497 
2498 error_splat_root:
2499 	dput(mntroot);
2500 	mntroot = ERR_PTR(error);
2501 error_splat_super:
2502 	if (server && !s->s_root)
2503 		bdi_unregister(&server->backing_dev_info);
2504 error_splat_bdi:
2505 	deactivate_locked_super(s);
2506 	goto out;
2507 }
2508 EXPORT_SYMBOL_GPL(nfs_fs_mount_common);
2509 
2510 struct dentry *nfs_fs_mount(struct file_system_type *fs_type,
2511 	int flags, const char *dev_name, void *raw_data)
2512 {
2513 	struct nfs_mount_info mount_info = {
2514 		.fill_super = nfs_fill_super,
2515 		.set_security = nfs_set_sb_security,
2516 	};
2517 	struct dentry *mntroot = ERR_PTR(-ENOMEM);
2518 	struct nfs_subversion *nfs_mod;
2519 	int error;
2520 
2521 	mount_info.parsed = nfs_alloc_parsed_mount_data();
2522 	mount_info.mntfh = nfs_alloc_fhandle();
2523 	if (mount_info.parsed == NULL || mount_info.mntfh == NULL)
2524 		goto out;
2525 
2526 	/* Validate the mount data */
2527 	error = nfs_validate_mount_data(fs_type, raw_data, mount_info.parsed, mount_info.mntfh, dev_name);
2528 	if (error == NFS_TEXT_DATA)
2529 		error = nfs_validate_text_mount_data(raw_data, mount_info.parsed, dev_name);
2530 	if (error < 0) {
2531 		mntroot = ERR_PTR(error);
2532 		goto out;
2533 	}
2534 
2535 	nfs_mod = get_nfs_version(mount_info.parsed->version);
2536 	if (IS_ERR(nfs_mod)) {
2537 		mntroot = ERR_CAST(nfs_mod);
2538 		goto out;
2539 	}
2540 
2541 	mntroot = nfs_mod->rpc_ops->try_mount(flags, dev_name, &mount_info, nfs_mod);
2542 
2543 	put_nfs_version(nfs_mod);
2544 out:
2545 	nfs_free_parsed_mount_data(mount_info.parsed);
2546 	nfs_free_fhandle(mount_info.mntfh);
2547 	return mntroot;
2548 }
2549 EXPORT_SYMBOL_GPL(nfs_fs_mount);
2550 
2551 /*
2552  * Ensure that we unregister the bdi before kill_anon_super
2553  * releases the device name
2554  */
2555 void nfs_put_super(struct super_block *s)
2556 {
2557 	struct nfs_server *server = NFS_SB(s);
2558 
2559 	bdi_unregister(&server->backing_dev_info);
2560 }
2561 EXPORT_SYMBOL_GPL(nfs_put_super);
2562 
2563 /*
2564  * Destroy an NFS2/3 superblock
2565  */
2566 void nfs_kill_super(struct super_block *s)
2567 {
2568 	struct nfs_server *server = NFS_SB(s);
2569 
2570 	kill_anon_super(s);
2571 	nfs_fscache_release_super_cookie(s);
2572 	nfs_free_server(server);
2573 }
2574 EXPORT_SYMBOL_GPL(nfs_kill_super);
2575 
2576 /*
2577  * Clone an NFS2/3/4 server record on xdev traversal (FSID-change)
2578  */
2579 static struct dentry *
2580 nfs_xdev_mount(struct file_system_type *fs_type, int flags,
2581 		const char *dev_name, void *raw_data)
2582 {
2583 	struct nfs_clone_mount *data = raw_data;
2584 	struct nfs_mount_info mount_info = {
2585 		.fill_super = nfs_clone_super,
2586 		.set_security = nfs_clone_sb_security,
2587 		.cloned = data,
2588 	};
2589 	struct nfs_server *server;
2590 	struct dentry *mntroot = ERR_PTR(-ENOMEM);
2591 	struct nfs_subversion *nfs_mod = NFS_SB(data->sb)->nfs_client->cl_nfs_mod;
2592 	int error;
2593 
2594 	dprintk("--> nfs_xdev_mount_common()\n");
2595 
2596 	mount_info.mntfh = mount_info.cloned->fh;
2597 
2598 	/* create a new volume representation */
2599 	server = nfs_mod->rpc_ops->clone_server(NFS_SB(data->sb), data->fh, data->fattr, data->authflavor);
2600 	if (IS_ERR(server)) {
2601 		error = PTR_ERR(server);
2602 		goto out_err;
2603 	}
2604 
2605 	mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, nfs_mod);
2606 	dprintk("<-- nfs_xdev_mount_common() = 0\n");
2607 out:
2608 	return mntroot;
2609 
2610 out_err:
2611 	dprintk("<-- nfs_xdev_mount_common() = %d [error]\n", error);
2612 	goto out;
2613 }
2614 
2615 #if IS_ENABLED(CONFIG_NFS_V4)
2616 
2617 static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2618 {
2619 	args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2620 			 NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
2621 }
2622 
2623 /*
2624  * Validate NFSv4 mount options
2625  */
2626 static int nfs4_validate_mount_data(void *options,
2627 				    struct nfs_parsed_mount_data *args,
2628 				    const char *dev_name)
2629 {
2630 	struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2631 	struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2632 	char *c;
2633 
2634 	if (data == NULL)
2635 		goto out_no_data;
2636 
2637 	args->version = 4;
2638 
2639 	switch (data->version) {
2640 	case 1:
2641 		if (data->host_addrlen > sizeof(args->nfs_server.address))
2642 			goto out_no_address;
2643 		if (data->host_addrlen == 0)
2644 			goto out_no_address;
2645 		args->nfs_server.addrlen = data->host_addrlen;
2646 		if (copy_from_user(sap, data->host_addr, data->host_addrlen))
2647 			return -EFAULT;
2648 		if (!nfs_verify_server_address(sap))
2649 			goto out_no_address;
2650 		args->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2651 
2652 		if (data->auth_flavourlen) {
2653 			if (data->auth_flavourlen > 1)
2654 				goto out_inval_auth;
2655 			if (copy_from_user(&args->auth_flavors[0],
2656 					   data->auth_flavours,
2657 					   sizeof(args->auth_flavors[0])))
2658 				return -EFAULT;
2659 		}
2660 
2661 		c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2662 		if (IS_ERR(c))
2663 			return PTR_ERR(c);
2664 		args->nfs_server.hostname = c;
2665 
2666 		c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2667 		if (IS_ERR(c))
2668 			return PTR_ERR(c);
2669 		args->nfs_server.export_path = c;
2670 		dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2671 
2672 		c = strndup_user(data->client_addr.data, 16);
2673 		if (IS_ERR(c))
2674 			return PTR_ERR(c);
2675 		args->client_address = c;
2676 
2677 		/*
2678 		 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2679 		 * can deal with.
2680 		 */
2681 
2682 		args->flags	= data->flags & NFS4_MOUNT_FLAGMASK;
2683 		args->rsize	= data->rsize;
2684 		args->wsize	= data->wsize;
2685 		args->timeo	= data->timeo;
2686 		args->retrans	= data->retrans;
2687 		args->acregmin	= data->acregmin;
2688 		args->acregmax	= data->acregmax;
2689 		args->acdirmin	= data->acdirmin;
2690 		args->acdirmax	= data->acdirmax;
2691 		args->nfs_server.protocol = data->proto;
2692 		nfs_validate_transport_protocol(args);
2693 
2694 		break;
2695 	default:
2696 		return NFS_TEXT_DATA;
2697 	}
2698 
2699 	return 0;
2700 
2701 out_no_data:
2702 	dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2703 	return -EINVAL;
2704 
2705 out_inval_auth:
2706 	dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2707 		 data->auth_flavourlen);
2708 	return -EINVAL;
2709 
2710 out_no_address:
2711 	dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2712 	return -EINVAL;
2713 }
2714 
2715 /*
2716  * NFS v4 module parameters need to stay in the
2717  * NFS client for backwards compatibility
2718  */
2719 unsigned int nfs_callback_set_tcpport;
2720 unsigned short nfs_callback_tcpport;
2721 /* Default cache timeout is 10 minutes */
2722 unsigned int nfs_idmap_cache_timeout = 600;
2723 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
2724 bool nfs4_disable_idmapping = true;
2725 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
2726 unsigned short send_implementation_id = 1;
2727 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
2728 
2729 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
2730 EXPORT_SYMBOL_GPL(nfs_callback_tcpport);
2731 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
2732 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
2733 EXPORT_SYMBOL_GPL(max_session_slots);
2734 EXPORT_SYMBOL_GPL(send_implementation_id);
2735 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
2736 
2737 #define NFS_CALLBACK_MAXPORTNR (65535U)
2738 
2739 static int param_set_portnr(const char *val, const struct kernel_param *kp)
2740 {
2741 	unsigned long num;
2742 	int ret;
2743 
2744 	if (!val)
2745 		return -EINVAL;
2746 	ret = kstrtoul(val, 0, &num);
2747 	if (ret == -EINVAL || num > NFS_CALLBACK_MAXPORTNR)
2748 		return -EINVAL;
2749 	*((unsigned int *)kp->arg) = num;
2750 	return 0;
2751 }
2752 static struct kernel_param_ops param_ops_portnr = {
2753 	.set = param_set_portnr,
2754 	.get = param_get_uint,
2755 };
2756 #define param_check_portnr(name, p) __param_check(name, p, unsigned int);
2757 
2758 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
2759 module_param(nfs_idmap_cache_timeout, int, 0644);
2760 module_param(nfs4_disable_idmapping, bool, 0644);
2761 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
2762 			NFS4_CLIENT_ID_UNIQ_LEN, 0600);
2763 MODULE_PARM_DESC(nfs4_disable_idmapping,
2764 		"Turn off NFSv4 idmapping when using 'sec=sys'");
2765 module_param(max_session_slots, ushort, 0644);
2766 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
2767 		"requests the client will negotiate");
2768 module_param(send_implementation_id, ushort, 0644);
2769 MODULE_PARM_DESC(send_implementation_id,
2770 		"Send implementation ID with NFSv4.1 exchange_id");
2771 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
2772 MODULE_ALIAS("nfs4");
2773 
2774 #endif /* CONFIG_NFS_V4 */
2775