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