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