xref: /linux/fs/nfs/super.c (revision 4a5da3f5d35000bd8e5dd78732679a1e00fae719)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/nfs/super.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  *
7  *  nfs superblock handling functions
8  *
9  *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
10  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
11  *
12  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
13  *  J.S.Peatfield@damtp.cam.ac.uk
14  *
15  *  Split from inode.c by David Howells <dhowells@redhat.com>
16  *
17  * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
18  *   particular server are held in the same superblock
19  * - NFS superblocks can have several effective roots to the dentry tree
20  * - directory type roots are spliced into the tree when a path from one root reaches the root
21  *   of another (see nfs_lookup())
22  */
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 
27 #include <linux/time.h>
28 #include <linux/kernel.h>
29 #include <linux/mm.h>
30 #include <linux/string.h>
31 #include <linux/stat.h>
32 #include <linux/errno.h>
33 #include <linux/unistd.h>
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/addr.h>
36 #include <linux/sunrpc/stats.h>
37 #include <linux/sunrpc/metrics.h>
38 #include <linux/sunrpc/xprtsock.h>
39 #include <linux/sunrpc/xprtrdma.h>
40 #include <linux/nfs_fs.h>
41 #include <linux/nfs_mount.h>
42 #include <linux/nfs4_mount.h>
43 #include <linux/lockd/bind.h>
44 #include <linux/seq_file.h>
45 #include <linux/mount.h>
46 #include <linux/namei.h>
47 #include <linux/vfs.h>
48 #include <linux/inet.h>
49 #include <linux/in6.h>
50 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <net/ipv6.h>
53 #include <linux/netdevice.h>
54 #include <linux/nfs_xdr.h>
55 #include <linux/magic.h>
56 #include <linux/parser.h>
57 #include <linux/nsproxy.h>
58 #include <linux/rcupdate.h>
59 
60 #include <linux/uaccess.h>
61 #include <linux/nfs_ssc.h>
62 
63 #include <uapi/linux/tls.h>
64 
65 #include "nfs4_fs.h"
66 #include "callback.h"
67 #include "delegation.h"
68 #include "iostat.h"
69 #include "internal.h"
70 #include "fscache.h"
71 #include "nfs4session.h"
72 #include "pnfs.h"
73 #include "nfs.h"
74 #include "netns.h"
75 #include "sysfs.h"
76 #include "nfs4idmap.h"
77 
78 #define NFSDBG_FACILITY		NFSDBG_VFS
79 
80 const struct super_operations nfs_sops = {
81 	.alloc_inode	= nfs_alloc_inode,
82 	.free_inode	= nfs_free_inode,
83 	.write_inode	= nfs_write_inode,
84 	.drop_inode	= nfs_drop_inode,
85 	.statfs		= nfs_statfs,
86 	.evict_inode	= nfs_evict_inode,
87 	.umount_begin	= nfs_umount_begin,
88 	.show_options	= nfs_show_options,
89 	.show_devname	= nfs_show_devname,
90 	.show_path	= nfs_show_path,
91 	.show_stats	= nfs_show_stats,
92 };
93 EXPORT_SYMBOL_GPL(nfs_sops);
94 
95 #ifdef CONFIG_NFS_V4_2
96 static const struct nfs_ssc_client_ops nfs_ssc_clnt_ops_tbl = {
97 	.sco_sb_deactive = nfs_sb_deactive,
98 };
99 #endif
100 
101 #if IS_ENABLED(CONFIG_NFS_V4)
register_nfs4_fs(void)102 static int __init register_nfs4_fs(void)
103 {
104 	return register_filesystem(&nfs4_fs_type);
105 }
106 
unregister_nfs4_fs(void)107 static void unregister_nfs4_fs(void)
108 {
109 	unregister_filesystem(&nfs4_fs_type);
110 }
111 #else
register_nfs4_fs(void)112 static int __init register_nfs4_fs(void)
113 {
114 	return 0;
115 }
116 
unregister_nfs4_fs(void)117 static void unregister_nfs4_fs(void)
118 {
119 }
120 #endif
121 
122 #ifdef CONFIG_NFS_V4_2
nfs_ssc_register_ops(void)123 static void nfs_ssc_register_ops(void)
124 {
125 	nfs_ssc_register(&nfs_ssc_clnt_ops_tbl);
126 }
127 
nfs_ssc_unregister_ops(void)128 static void nfs_ssc_unregister_ops(void)
129 {
130 	nfs_ssc_unregister(&nfs_ssc_clnt_ops_tbl);
131 }
132 #endif /* CONFIG_NFS_V4_2 */
133 
134 static struct shrinker *acl_shrinker;
135 
136 /*
137  * Register the NFS filesystems
138  */
register_nfs_fs(void)139 int __init register_nfs_fs(void)
140 {
141 	int ret;
142 
143         ret = register_filesystem(&nfs_fs_type);
144 	if (ret < 0)
145 		goto error_0;
146 
147 	ret = register_nfs4_fs();
148 	if (ret < 0)
149 		goto error_1;
150 
151 	ret = nfs_register_sysctl();
152 	if (ret < 0)
153 		goto error_2;
154 
155 	acl_shrinker = shrinker_alloc(0, "nfs-acl");
156 	if (!acl_shrinker) {
157 		ret = -ENOMEM;
158 		goto error_3;
159 	}
160 
161 	acl_shrinker->count_objects = nfs_access_cache_count;
162 	acl_shrinker->scan_objects = nfs_access_cache_scan;
163 
164 	shrinker_register(acl_shrinker);
165 
166 #ifdef CONFIG_NFS_V4_2
167 	nfs_ssc_register_ops();
168 #endif
169 	return 0;
170 error_3:
171 	nfs_unregister_sysctl();
172 error_2:
173 	unregister_nfs4_fs();
174 error_1:
175 	unregister_filesystem(&nfs_fs_type);
176 error_0:
177 	return ret;
178 }
179 
180 /*
181  * Unregister the NFS filesystems
182  */
unregister_nfs_fs(void)183 void __exit unregister_nfs_fs(void)
184 {
185 	shrinker_free(acl_shrinker);
186 	nfs_unregister_sysctl();
187 	unregister_nfs4_fs();
188 #ifdef CONFIG_NFS_V4_2
189 	nfs_ssc_unregister_ops();
190 #endif
191 	unregister_filesystem(&nfs_fs_type);
192 }
193 
nfs_sb_active(struct super_block * sb)194 bool nfs_sb_active(struct super_block *sb)
195 {
196 	struct nfs_server *server = NFS_SB(sb);
197 
198 	if (!atomic_inc_not_zero(&sb->s_active))
199 		return false;
200 	if (atomic_inc_return(&server->active) != 1)
201 		atomic_dec(&sb->s_active);
202 	return true;
203 }
204 EXPORT_SYMBOL_GPL(nfs_sb_active);
205 
nfs_sb_deactive(struct super_block * sb)206 void nfs_sb_deactive(struct super_block *sb)
207 {
208 	struct nfs_server *server = NFS_SB(sb);
209 
210 	if (atomic_dec_and_test(&server->active))
211 		deactivate_super(sb);
212 }
213 EXPORT_SYMBOL_GPL(nfs_sb_deactive);
214 
__nfs_list_for_each_server(struct list_head * head,int (* fn)(struct nfs_server *,void *),void * data)215 static int __nfs_list_for_each_server(struct list_head *head,
216 		int (*fn)(struct nfs_server *, void *),
217 		void *data)
218 {
219 	struct nfs_server *server, *last = NULL;
220 	int ret = 0;
221 
222 	rcu_read_lock();
223 	list_for_each_entry_rcu(server, head, client_link) {
224 		if (!(server->super && nfs_sb_active(server->super)))
225 			continue;
226 		rcu_read_unlock();
227 		if (last)
228 			nfs_sb_deactive(last->super);
229 		last = server;
230 		ret = fn(server, data);
231 		if (ret)
232 			goto out;
233 		cond_resched();
234 		rcu_read_lock();
235 	}
236 	rcu_read_unlock();
237 out:
238 	if (last)
239 		nfs_sb_deactive(last->super);
240 	return ret;
241 }
242 
nfs_client_for_each_server(struct nfs_client * clp,int (* fn)(struct nfs_server *,void *),void * data)243 int nfs_client_for_each_server(struct nfs_client *clp,
244 		int (*fn)(struct nfs_server *, void *),
245 		void *data)
246 {
247 	return __nfs_list_for_each_server(&clp->cl_superblocks, fn, data);
248 }
249 EXPORT_SYMBOL_GPL(nfs_client_for_each_server);
250 
251 /*
252  * Deliver file system statistics to userspace
253  */
nfs_statfs(struct dentry * dentry,struct kstatfs * buf)254 int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
255 {
256 	struct nfs_server *server = NFS_SB(dentry->d_sb);
257 	unsigned char blockbits;
258 	unsigned long blockres;
259 	struct nfs_fh *fh = NFS_FH(d_inode(dentry));
260 	struct nfs_fsstat res;
261 	int error = -ENOMEM;
262 
263 	res.fattr = nfs_alloc_fattr();
264 	if (res.fattr == NULL)
265 		goto out_err;
266 
267 	error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
268 	if (unlikely(error == -ESTALE)) {
269 		struct dentry *pd_dentry;
270 
271 		pd_dentry = dget_parent(dentry);
272 		nfs_zap_caches(d_inode(pd_dentry));
273 		dput(pd_dentry);
274 	}
275 	nfs_free_fattr(res.fattr);
276 	if (error < 0)
277 		goto out_err;
278 
279 	buf->f_type = NFS_SUPER_MAGIC;
280 
281 	/*
282 	 * Current versions of glibc do not correctly handle the
283 	 * case where f_frsize != f_bsize.  Eventually we want to
284 	 * report the value of wtmult in this field.
285 	 */
286 	buf->f_frsize = dentry->d_sb->s_blocksize;
287 
288 	/*
289 	 * On most *nix systems, f_blocks, f_bfree, and f_bavail
290 	 * are reported in units of f_frsize.  Linux hasn't had
291 	 * an f_frsize field in its statfs struct until recently,
292 	 * thus historically Linux's sys_statfs reports these
293 	 * fields in units of f_bsize.
294 	 */
295 	buf->f_bsize = dentry->d_sb->s_blocksize;
296 	blockbits = dentry->d_sb->s_blocksize_bits;
297 	blockres = (1 << blockbits) - 1;
298 	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
299 	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
300 	buf->f_bavail = (res.abytes + blockres) >> blockbits;
301 
302 	buf->f_files = res.tfiles;
303 	buf->f_ffree = res.afiles;
304 
305 	buf->f_namelen = server->namelen;
306 
307 	return 0;
308 
309  out_err:
310 	dprintk("%s: statfs error = %d\n", __func__, -error);
311 	return error;
312 }
313 EXPORT_SYMBOL_GPL(nfs_statfs);
314 
315 /*
316  * Map the security flavour number to a name
317  */
nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)318 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
319 {
320 	static const struct {
321 		rpc_authflavor_t flavour;
322 		const char *str;
323 	} sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
324 		/* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
325 		{ RPC_AUTH_NULL, "null" },
326 		{ RPC_AUTH_UNIX, "sys" },
327 		{ RPC_AUTH_GSS_KRB5, "krb5" },
328 		{ RPC_AUTH_GSS_KRB5I, "krb5i" },
329 		{ RPC_AUTH_GSS_KRB5P, "krb5p" },
330 		{ RPC_AUTH_GSS_LKEY, "lkey" },
331 		{ RPC_AUTH_GSS_LKEYI, "lkeyi" },
332 		{ RPC_AUTH_GSS_LKEYP, "lkeyp" },
333 		{ RPC_AUTH_GSS_SPKM, "spkm" },
334 		{ RPC_AUTH_GSS_SPKMI, "spkmi" },
335 		{ RPC_AUTH_GSS_SPKMP, "spkmp" },
336 		{ UINT_MAX, "unknown" }
337 	};
338 	int i;
339 
340 	for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
341 		if (sec_flavours[i].flavour == flavour)
342 			break;
343 	}
344 	return sec_flavours[i].str;
345 }
346 
nfs_show_mountd_netid(struct seq_file * m,struct nfs_server * nfss,int showdefaults)347 static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
348 				  int showdefaults)
349 {
350 	struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
351 	char *proto = NULL;
352 
353 	switch (sap->sa_family) {
354 	case AF_INET:
355 		switch (nfss->mountd_protocol) {
356 		case IPPROTO_UDP:
357 			proto = RPCBIND_NETID_UDP;
358 			break;
359 		case IPPROTO_TCP:
360 			proto = RPCBIND_NETID_TCP;
361 			break;
362 		}
363 		break;
364 	case AF_INET6:
365 		switch (nfss->mountd_protocol) {
366 		case IPPROTO_UDP:
367 			proto = RPCBIND_NETID_UDP6;
368 			break;
369 		case IPPROTO_TCP:
370 			proto = RPCBIND_NETID_TCP6;
371 			break;
372 		}
373 		break;
374 	}
375 	if (proto || showdefaults)
376 		seq_printf(m, ",mountproto=%s", proto ?: "auto");
377 }
378 
nfs_show_mountd_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)379 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
380 				    int showdefaults)
381 {
382 	struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
383 
384 	if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
385 		return;
386 
387 	switch (sap->sa_family) {
388 	case AF_INET: {
389 		struct sockaddr_in *sin = (struct sockaddr_in *)sap;
390 		seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
391 		break;
392 	}
393 	case AF_INET6: {
394 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
395 		seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
396 		break;
397 	}
398 	default:
399 		if (showdefaults)
400 			seq_puts(m, ",mountaddr=unspecified");
401 	}
402 
403 	if (nfss->mountd_version || showdefaults)
404 		seq_printf(m, ",mountvers=%u", nfss->mountd_version);
405 	if ((nfss->mountd_port &&
406 		nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
407 		showdefaults)
408 		seq_printf(m, ",mountport=%u", nfss->mountd_port);
409 
410 	nfs_show_mountd_netid(m, nfss, showdefaults);
411 }
412 
413 #if IS_ENABLED(CONFIG_NFS_V4)
nfs_show_nfsv4_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)414 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
415 				    int showdefaults)
416 {
417 	struct nfs_client *clp = nfss->nfs_client;
418 
419 	seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
420 }
421 #else
nfs_show_nfsv4_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)422 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
423 				    int showdefaults)
424 {
425 }
426 #endif
427 
nfs_show_nfs_version(struct seq_file * m,unsigned int version,unsigned int minorversion)428 static void nfs_show_nfs_version(struct seq_file *m,
429 		unsigned int version,
430 		unsigned int minorversion)
431 {
432 	seq_printf(m, ",vers=%u", version);
433 	if (version == 4)
434 		seq_printf(m, ".%u", minorversion);
435 }
436 
437 /*
438  * Describe the mount options in force on this server representation
439  */
nfs_show_mount_options(struct seq_file * m,struct nfs_server * nfss,int showdefaults)440 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
441 				   int showdefaults)
442 {
443 	static const struct proc_nfs_info {
444 		int flag;
445 		const char *str;
446 		const char *nostr;
447 	} nfs_info[] = {
448 		{ NFS_MOUNT_SOFT, ",soft", "" },
449 		{ NFS_MOUNT_SOFTERR, ",softerr", "" },
450 		{ NFS_MOUNT_SOFTREVAL, ",softreval", "" },
451 		{ NFS_MOUNT_POSIX, ",posix", "" },
452 		{ NFS_MOUNT_NOCTO, ",nocto", "" },
453 		{ NFS_MOUNT_NOAC, ",noac", "" },
454 		{ NFS_MOUNT_NONLM, ",nolock", "" },
455 		{ NFS_MOUNT_NOACL, ",noacl", "" },
456 		{ NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
457 		{ NFS_MOUNT_UNSHARED, ",nosharecache", "" },
458 		{ NFS_MOUNT_NORESVPORT, ",noresvport", "" },
459 		{ 0, NULL, NULL }
460 	};
461 	const struct proc_nfs_info *nfs_infop;
462 	struct nfs_client *clp = nfss->nfs_client;
463 	u32 version = clp->rpc_ops->version;
464 	int local_flock, local_fcntl;
465 
466 	nfs_show_nfs_version(m, version, clp->cl_minorversion);
467 	seq_printf(m, ",rsize=%u", nfss->rsize);
468 	seq_printf(m, ",wsize=%u", nfss->wsize);
469 	if (nfss->bsize != 0)
470 		seq_printf(m, ",bsize=%u", nfss->bsize);
471 	seq_printf(m, ",namlen=%u", nfss->namelen);
472 	if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
473 		seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
474 	if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
475 		seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
476 	if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
477 		seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
478 	if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
479 		seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
480 	if (!(nfss->flags & (NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR)))
481 			seq_puts(m, ",hard");
482 	for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
483 		if (nfss->flags & nfs_infop->flag)
484 			seq_puts(m, nfs_infop->str);
485 		else
486 			seq_puts(m, nfs_infop->nostr);
487 	}
488 	rcu_read_lock();
489 	seq_printf(m, ",proto=%s",
490 		   rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
491 	rcu_read_unlock();
492 	if (clp->cl_nconnect > 0)
493 		seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
494 	if (version == 4) {
495 		if (clp->cl_max_connect > 1)
496 			seq_printf(m, ",max_connect=%u", clp->cl_max_connect);
497 		if (nfss->port != NFS_PORT)
498 			seq_printf(m, ",port=%u", nfss->port);
499 	} else
500 		if (nfss->port)
501 			seq_printf(m, ",port=%u", nfss->port);
502 
503 	seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
504 	seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
505 	seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
506 	switch (clp->cl_xprtsec.policy) {
507 	case RPC_XPRTSEC_TLS_ANON:
508 		seq_puts(m, ",xprtsec=tls");
509 		break;
510 	case RPC_XPRTSEC_TLS_X509:
511 		seq_puts(m, ",xprtsec=mtls");
512 		break;
513 	default:
514 		break;
515 	}
516 
517 	if (version != 4)
518 		nfs_show_mountd_options(m, nfss, showdefaults);
519 	else
520 		nfs_show_nfsv4_options(m, nfss, showdefaults);
521 
522 	if (nfss->options & NFS_OPTION_FSCACHE) {
523 #ifdef CONFIG_NFS_FSCACHE
524 		if (nfss->fscache_uniq)
525 			seq_printf(m, ",fsc=%s", nfss->fscache_uniq);
526 		else
527 			seq_puts(m, ",fsc");
528 #else
529 		seq_puts(m, ",fsc");
530 #endif
531 	}
532 
533 	if (nfss->options & NFS_OPTION_MIGRATION)
534 		seq_puts(m, ",migration");
535 
536 	if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
537 		if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
538 			seq_puts(m, ",lookupcache=none");
539 		else
540 			seq_puts(m, ",lookupcache=pos");
541 	}
542 
543 	local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
544 	local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
545 
546 	if (!local_flock && !local_fcntl)
547 		seq_puts(m, ",local_lock=none");
548 	else if (local_flock && local_fcntl)
549 		seq_puts(m, ",local_lock=all");
550 	else if (local_flock)
551 		seq_puts(m, ",local_lock=flock");
552 	else
553 		seq_puts(m, ",local_lock=posix");
554 
555 	if (nfss->flags & NFS_MOUNT_NO_ALIGNWRITE)
556 		seq_puts(m, ",noalignwrite");
557 
558 	if (nfss->flags & NFS_MOUNT_WRITE_EAGER) {
559 		if (nfss->flags & NFS_MOUNT_WRITE_WAIT)
560 			seq_puts(m, ",write=wait");
561 		else
562 			seq_puts(m, ",write=eager");
563 	}
564 }
565 
566 /*
567  * Describe the mount options on this VFS mountpoint
568  */
nfs_show_options(struct seq_file * m,struct dentry * root)569 int nfs_show_options(struct seq_file *m, struct dentry *root)
570 {
571 	struct nfs_server *nfss = NFS_SB(root->d_sb);
572 
573 	nfs_show_mount_options(m, nfss, 0);
574 
575 	rcu_read_lock();
576 	seq_printf(m, ",addr=%s",
577 			rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
578 							RPC_DISPLAY_ADDR));
579 	rcu_read_unlock();
580 
581 	return 0;
582 }
583 EXPORT_SYMBOL_GPL(nfs_show_options);
584 
585 #if IS_ENABLED(CONFIG_NFS_V4)
show_lease(struct seq_file * m,struct nfs_server * server)586 static void show_lease(struct seq_file *m, struct nfs_server *server)
587 {
588 	struct nfs_client *clp = server->nfs_client;
589 	unsigned long expire;
590 
591 	seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
592 	expire = clp->cl_last_renewal + clp->cl_lease_time;
593 	seq_printf(m, ",lease_expired=%ld",
594 		   time_after(expire, jiffies) ?  0 : (jiffies - expire) / HZ);
595 }
596 #ifdef CONFIG_NFS_V4_1
show_sessions(struct seq_file * m,struct nfs_server * server)597 static void show_sessions(struct seq_file *m, struct nfs_server *server)
598 {
599 	if (nfs4_has_session(server->nfs_client))
600 		seq_puts(m, ",sessions");
601 }
602 #else
show_sessions(struct seq_file * m,struct nfs_server * server)603 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
604 #endif
605 #endif
606 
607 #ifdef CONFIG_NFS_V4_1
show_pnfs(struct seq_file * m,struct nfs_server * server)608 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
609 {
610 	seq_printf(m, ",pnfs=");
611 	if (server->pnfs_curr_ld)
612 		seq_printf(m, "%s", server->pnfs_curr_ld->name);
613 	else
614 		seq_printf(m, "not configured");
615 }
616 
show_implementation_id(struct seq_file * m,struct nfs_server * nfss)617 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
618 {
619 	if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
620 		struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
621 		seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
622 			   "date='%llu,%u'",
623 			   impl_id->name, impl_id->domain,
624 			   impl_id->date.seconds, impl_id->date.nseconds);
625 	}
626 }
627 #else
628 #if IS_ENABLED(CONFIG_NFS_V4)
show_pnfs(struct seq_file * m,struct nfs_server * server)629 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
630 {
631 }
632 #endif
show_implementation_id(struct seq_file * m,struct nfs_server * nfss)633 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
634 {
635 }
636 #endif
637 
nfs_show_devname(struct seq_file * m,struct dentry * root)638 int nfs_show_devname(struct seq_file *m, struct dentry *root)
639 {
640 	char *page = (char *) __get_free_page(GFP_KERNEL);
641 	char *devname, *dummy;
642 	int err = 0;
643 	if (!page)
644 		return -ENOMEM;
645 	devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
646 	if (IS_ERR(devname))
647 		err = PTR_ERR(devname);
648 	else
649 		seq_escape(m, devname, " \t\n\\");
650 	free_page((unsigned long)page);
651 	return err;
652 }
653 EXPORT_SYMBOL_GPL(nfs_show_devname);
654 
nfs_show_path(struct seq_file * m,struct dentry * dentry)655 int nfs_show_path(struct seq_file *m, struct dentry *dentry)
656 {
657 	seq_puts(m, "/");
658 	return 0;
659 }
660 EXPORT_SYMBOL_GPL(nfs_show_path);
661 
662 /*
663  * Present statistical information for this VFS mountpoint
664  */
nfs_show_stats(struct seq_file * m,struct dentry * root)665 int nfs_show_stats(struct seq_file *m, struct dentry *root)
666 {
667 	int i, cpu;
668 	struct nfs_server *nfss = NFS_SB(root->d_sb);
669 	struct rpc_auth *auth = nfss->client->cl_auth;
670 	struct nfs_iostats totals = { };
671 
672 	seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
673 
674 	/*
675 	 * Display all mount option settings
676 	 */
677 	seq_puts(m, "\n\topts:\t");
678 	seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
679 	seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
680 	seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
681 	seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
682 	nfs_show_mount_options(m, nfss, 1);
683 
684 	seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
685 
686 	show_implementation_id(m, nfss);
687 
688 	seq_puts(m, "\n\tcaps:\t");
689 	seq_printf(m, "caps=0x%x", nfss->caps);
690 	seq_printf(m, ",wtmult=%u", nfss->wtmult);
691 	seq_printf(m, ",dtsize=%u", nfss->dtsize);
692 	seq_printf(m, ",bsize=%u", nfss->bsize);
693 	seq_printf(m, ",namlen=%u", nfss->namelen);
694 
695 #if IS_ENABLED(CONFIG_NFS_V4)
696 	if (nfss->nfs_client->rpc_ops->version == 4) {
697 		seq_puts(m, "\n\tnfsv4:\t");
698 		seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
699 		seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
700 		seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
701 		seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
702 		show_sessions(m, nfss);
703 		show_pnfs(m, nfss);
704 		show_lease(m, nfss);
705 	}
706 #endif
707 
708 	/*
709 	 * Display security flavor in effect for this mount
710 	 */
711 	seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
712 	if (auth->au_flavor)
713 		seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
714 
715 	/*
716 	 * Display superblock I/O counters
717 	 */
718 	for_each_possible_cpu(cpu) {
719 		struct nfs_iostats *stats;
720 
721 		preempt_disable();
722 		stats = per_cpu_ptr(nfss->io_stats, cpu);
723 
724 		for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
725 			totals.events[i] += stats->events[i];
726 		for (i = 0; i < __NFSIOS_BYTESMAX; i++)
727 			totals.bytes[i] += stats->bytes[i];
728 
729 		preempt_enable();
730 	}
731 
732 	seq_puts(m, "\n\tevents:\t");
733 	for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
734 		seq_printf(m, "%lu ", totals.events[i]);
735 	seq_puts(m, "\n\tbytes:\t");
736 	for (i = 0; i < __NFSIOS_BYTESMAX; i++)
737 		seq_printf(m, "%Lu ", totals.bytes[i]);
738 	seq_putc(m, '\n');
739 
740 	rpc_clnt_show_stats(m, nfss->client);
741 
742 	return 0;
743 }
744 EXPORT_SYMBOL_GPL(nfs_show_stats);
745 
746 /*
747  * Begin unmount by attempting to remove all automounted mountpoints we added
748  * in response to xdev traversals and referrals
749  */
nfs_umount_begin(struct super_block * sb)750 void nfs_umount_begin(struct super_block *sb)
751 {
752 	struct nfs_server *server;
753 	struct rpc_clnt *rpc;
754 
755 	server = NFS_SB(sb);
756 	/* -EIO all pending I/O */
757 	rpc = server->client_acl;
758 	if (!IS_ERR(rpc))
759 		rpc_killall_tasks(rpc);
760 	rpc = server->client;
761 	if (!IS_ERR(rpc))
762 		rpc_killall_tasks(rpc);
763 }
764 EXPORT_SYMBOL_GPL(nfs_umount_begin);
765 
766 /*
767  * Return true if 'match' is in auth_info or auth_info is empty.
768  * Return false otherwise.
769  */
nfs_auth_info_match(const struct nfs_auth_info * auth_info,rpc_authflavor_t match)770 bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
771 			 rpc_authflavor_t match)
772 {
773 	int i;
774 
775 	if (!auth_info->flavor_len)
776 		return true;
777 
778 	for (i = 0; i < auth_info->flavor_len; i++) {
779 		if (auth_info->flavors[i] == match)
780 			return true;
781 	}
782 	return false;
783 }
784 EXPORT_SYMBOL_GPL(nfs_auth_info_match);
785 
786 /*
787  * Ensure that a specified authtype in ctx->auth_info is supported by
788  * the server. Returns 0 and sets ctx->selected_flavor if it's ok, and
789  * -EACCES if not.
790  */
nfs_verify_authflavors(struct nfs_fs_context * ctx,rpc_authflavor_t * server_authlist,unsigned int count)791 static int nfs_verify_authflavors(struct nfs_fs_context *ctx,
792 				  rpc_authflavor_t *server_authlist,
793 				  unsigned int count)
794 {
795 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
796 	bool found_auth_null = false;
797 	unsigned int i;
798 
799 	/*
800 	 * If the sec= mount option is used, the specified flavor or AUTH_NULL
801 	 * must be in the list returned by the server.
802 	 *
803 	 * AUTH_NULL has a special meaning when it's in the server list - it
804 	 * means that the server will ignore the rpc creds, so any flavor
805 	 * can be used but still use the sec= that was specified.
806 	 *
807 	 * Note also that the MNT procedure in MNTv1 does not return a list
808 	 * of supported security flavors. In this case, nfs_mount() fabricates
809 	 * a security flavor list containing just AUTH_NULL.
810 	 */
811 	for (i = 0; i < count; i++) {
812 		flavor = server_authlist[i];
813 
814 		if (nfs_auth_info_match(&ctx->auth_info, flavor))
815 			goto out;
816 
817 		if (flavor == RPC_AUTH_NULL)
818 			found_auth_null = true;
819 	}
820 
821 	if (found_auth_null) {
822 		flavor = ctx->auth_info.flavors[0];
823 		goto out;
824 	}
825 
826 	dfprintk(MOUNT,
827 		 "NFS: specified auth flavors not supported by server\n");
828 	return -EACCES;
829 
830 out:
831 	ctx->selected_flavor = flavor;
832 	dfprintk(MOUNT, "NFS: using auth flavor %u\n", ctx->selected_flavor);
833 	return 0;
834 }
835 
836 /*
837  * Use the remote server's MOUNT service to request the NFS file handle
838  * corresponding to the provided path.
839  */
nfs_request_mount(struct fs_context * fc,struct nfs_fh * root_fh,rpc_authflavor_t * server_authlist,unsigned int * server_authlist_len)840 static int nfs_request_mount(struct fs_context *fc,
841 			     struct nfs_fh *root_fh,
842 			     rpc_authflavor_t *server_authlist,
843 			     unsigned int *server_authlist_len)
844 {
845 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
846 	struct nfs_mount_request request = {
847 		.sap		= &ctx->mount_server._address,
848 		.dirpath	= ctx->nfs_server.export_path,
849 		.protocol	= ctx->mount_server.protocol,
850 		.fh		= root_fh,
851 		.noresvport	= ctx->flags & NFS_MOUNT_NORESVPORT,
852 		.auth_flav_len	= server_authlist_len,
853 		.auth_flavs	= server_authlist,
854 		.net		= fc->net_ns,
855 	};
856 	int status;
857 
858 	if (ctx->mount_server.version == 0) {
859 		switch (ctx->version) {
860 			default:
861 				ctx->mount_server.version = NFS_MNT3_VERSION;
862 				break;
863 			case 2:
864 				ctx->mount_server.version = NFS_MNT_VERSION;
865 		}
866 	}
867 	request.version = ctx->mount_server.version;
868 
869 	if (ctx->mount_server.hostname)
870 		request.hostname = ctx->mount_server.hostname;
871 	else
872 		request.hostname = ctx->nfs_server.hostname;
873 
874 	/*
875 	 * Construct the mount server's address.
876 	 */
877 	if (ctx->mount_server.address.sa_family == AF_UNSPEC) {
878 		memcpy(request.sap, &ctx->nfs_server._address,
879 		       ctx->nfs_server.addrlen);
880 		ctx->mount_server.addrlen = ctx->nfs_server.addrlen;
881 	}
882 	request.salen = ctx->mount_server.addrlen;
883 	nfs_set_port(request.sap, &ctx->mount_server.port, 0);
884 
885 	/*
886 	 * Now ask the mount server to map our export path
887 	 * to a file handle.
888 	 */
889 	if ((request.protocol == XPRT_TRANSPORT_UDP) ==
890 	    !(ctx->flags & NFS_MOUNT_TCP))
891 		/*
892 		 * NFS protocol and mount protocol are both UDP or neither UDP
893 		 * so timeouts are compatible.  Use NFS timeouts for MOUNT
894 		 */
895 		status = nfs_mount(&request, ctx->timeo, ctx->retrans);
896 	else
897 		status = nfs_mount(&request, NFS_UNSPEC_TIMEO, NFS_UNSPEC_RETRANS);
898 	if (status != 0) {
899 		dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
900 				request.hostname, status);
901 		return status;
902 	}
903 
904 	return 0;
905 }
906 
nfs_try_mount_request(struct fs_context * fc)907 static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
908 {
909 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
910 	int status;
911 	unsigned int i;
912 	bool tried_auth_unix = false;
913 	bool auth_null_in_list = false;
914 	struct nfs_server *server = ERR_PTR(-EACCES);
915 	rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
916 	unsigned int authlist_len = ARRAY_SIZE(authlist);
917 
918 	/* make sure 'nolock'/'lock' override the 'local_lock' mount option */
919 	if (ctx->lock_status) {
920 		if (ctx->lock_status == NFS_LOCK_NOLOCK) {
921 			ctx->flags |= NFS_MOUNT_NONLM;
922 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
923 		} else {
924 			ctx->flags &= ~NFS_MOUNT_NONLM;
925 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
926 		}
927 	}
928 	status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
929 	if (status)
930 		return ERR_PTR(status);
931 
932 	/*
933 	 * Was a sec= authflavor specified in the options? First, verify
934 	 * whether the server supports it, and then just try to use it if so.
935 	 */
936 	if (ctx->auth_info.flavor_len > 0) {
937 		status = nfs_verify_authflavors(ctx, authlist, authlist_len);
938 		dfprintk(MOUNT, "NFS: using auth flavor %u\n",
939 			 ctx->selected_flavor);
940 		if (status)
941 			return ERR_PTR(status);
942 		return ctx->nfs_mod->rpc_ops->create_server(fc);
943 	}
944 
945 	/*
946 	 * No sec= option was provided. RFC 2623, section 2.7 suggests we
947 	 * SHOULD prefer the flavor listed first. However, some servers list
948 	 * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
949 	 */
950 	for (i = 0; i < authlist_len; ++i) {
951 		rpc_authflavor_t flavor;
952 		struct rpcsec_gss_info info;
953 
954 		flavor = authlist[i];
955 		switch (flavor) {
956 		case RPC_AUTH_UNIX:
957 			tried_auth_unix = true;
958 			break;
959 		case RPC_AUTH_NULL:
960 			auth_null_in_list = true;
961 			continue;
962 		default:
963 			if (rpcauth_get_gssinfo(flavor, &info) != 0)
964 				continue;
965 			break;
966 		}
967 		dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
968 		ctx->selected_flavor = flavor;
969 		server = ctx->nfs_mod->rpc_ops->create_server(fc);
970 		if (!IS_ERR(server))
971 			return server;
972 	}
973 
974 	/*
975 	 * Nothing we tried so far worked. At this point, give up if we've
976 	 * already tried AUTH_UNIX or if the server's list doesn't contain
977 	 * AUTH_NULL
978 	 */
979 	if (tried_auth_unix || !auth_null_in_list)
980 		return server;
981 
982 	/* Last chance! Try AUTH_UNIX */
983 	dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
984 	ctx->selected_flavor = RPC_AUTH_UNIX;
985 	return ctx->nfs_mod->rpc_ops->create_server(fc);
986 }
987 
nfs_try_get_tree(struct fs_context * fc)988 int nfs_try_get_tree(struct fs_context *fc)
989 {
990 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
991 
992 	if (ctx->need_mount)
993 		ctx->server = nfs_try_mount_request(fc);
994 	else
995 		ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
996 
997 	return nfs_get_tree_common(fc);
998 }
999 EXPORT_SYMBOL_GPL(nfs_try_get_tree);
1000 
1001 
1002 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
1003 		| NFS_MOUNT_SECURE \
1004 		| NFS_MOUNT_TCP \
1005 		| NFS_MOUNT_VER3 \
1006 		| NFS_MOUNT_KERBEROS \
1007 		| NFS_MOUNT_NONLM \
1008 		| NFS_MOUNT_BROKEN_SUID \
1009 		| NFS_MOUNT_STRICTLOCK \
1010 		| NFS_MOUNT_LEGACY_INTERFACE)
1011 
1012 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
1013 		~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
1014 
1015 static int
nfs_compare_remount_data(struct nfs_server * nfss,struct nfs_fs_context * ctx)1016 nfs_compare_remount_data(struct nfs_server *nfss,
1017 			 struct nfs_fs_context *ctx)
1018 {
1019 	if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
1020 	    ctx->rsize != nfss->rsize ||
1021 	    ctx->wsize != nfss->wsize ||
1022 	    ctx->version != nfss->nfs_client->rpc_ops->version ||
1023 	    ctx->minorversion != nfss->nfs_client->cl_minorversion ||
1024 	    ctx->retrans != nfss->client->cl_timeout->to_retries ||
1025 	    !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
1026 	    ctx->acregmin != nfss->acregmin / HZ ||
1027 	    ctx->acregmax != nfss->acregmax / HZ ||
1028 	    ctx->acdirmin != nfss->acdirmin / HZ ||
1029 	    ctx->acdirmax != nfss->acdirmax / HZ ||
1030 	    ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1031 	    (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
1032 	    ctx->nfs_server.port != nfss->port ||
1033 	    ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1034 	    !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
1035 			  (struct sockaddr *)&nfss->nfs_client->cl_addr))
1036 		return -EINVAL;
1037 
1038 	return 0;
1039 }
1040 
nfs_reconfigure(struct fs_context * fc)1041 int nfs_reconfigure(struct fs_context *fc)
1042 {
1043 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1044 	struct super_block *sb = fc->root->d_sb;
1045 	struct nfs_server *nfss = sb->s_fs_info;
1046 	int ret;
1047 
1048 	sync_filesystem(sb);
1049 
1050 	/*
1051 	 * Userspace mount programs that send binary options generally send
1052 	 * them populated with default values. We have no way to know which
1053 	 * ones were explicitly specified. Fall back to legacy behavior and
1054 	 * just return success.
1055 	 */
1056 	if (ctx->skip_reconfig_option_check)
1057 		return 0;
1058 
1059 	/*
1060 	 * noac is a special case. It implies -o sync, but that's not
1061 	 * necessarily reflected in the mtab options. reconfigure_super
1062 	 * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
1063 	 * remount options, so we have to explicitly reset it.
1064 	 */
1065 	if (ctx->flags & NFS_MOUNT_NOAC) {
1066 		fc->sb_flags |= SB_SYNCHRONOUS;
1067 		fc->sb_flags_mask |= SB_SYNCHRONOUS;
1068 	}
1069 
1070 	/* compare new mount options with old ones */
1071 	ret = nfs_compare_remount_data(nfss, ctx);
1072 	if (ret)
1073 		return ret;
1074 
1075 	return nfs_probe_server(nfss, NFS_FH(d_inode(fc->root)));
1076 }
1077 EXPORT_SYMBOL_GPL(nfs_reconfigure);
1078 
1079 /*
1080  * Finish setting up an NFS superblock
1081  */
nfs_fill_super(struct super_block * sb,struct nfs_fs_context * ctx)1082 static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
1083 {
1084 	struct nfs_server *server = NFS_SB(sb);
1085 
1086 	sb->s_blocksize_bits = 0;
1087 	sb->s_blocksize = 0;
1088 	sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1089 	sb->s_op = server->nfs_client->cl_nfs_mod->sops;
1090 	if (ctx->bsize)
1091 		sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
1092 
1093 	switch (server->nfs_client->rpc_ops->version) {
1094 	case 2:
1095 		sb->s_time_gran = 1000;
1096 		sb->s_time_min = 0;
1097 		sb->s_time_max = U32_MAX;
1098 		break;
1099 	case 3:
1100 		/*
1101 		 * The VFS shouldn't apply the umask to mode bits.
1102 		 * We will do so ourselves when necessary.
1103 		 */
1104 		sb->s_flags |= SB_POSIXACL;
1105 		sb->s_time_gran = 1;
1106 		sb->s_time_min = 0;
1107 		sb->s_time_max = U32_MAX;
1108 		sb->s_export_op = &nfs_export_ops;
1109 		break;
1110 	case 4:
1111 		sb->s_iflags |= SB_I_NOUMASK;
1112 		sb->s_time_gran = 1;
1113 		sb->s_time_min = S64_MIN;
1114 		sb->s_time_max = S64_MAX;
1115 		if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1116 			sb->s_export_op = &nfs_export_ops;
1117 		break;
1118 	}
1119 
1120 	sb->s_magic = NFS_SUPER_MAGIC;
1121 
1122 	/* We probably want something more informative here */
1123 	snprintf(sb->s_id, sizeof(sb->s_id),
1124 		 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1125 
1126 	if (sb->s_blocksize == 0)
1127 		sb->s_blocksize = nfs_block_bits(server->wsize,
1128 						 &sb->s_blocksize_bits);
1129 
1130 	nfs_super_set_maxbytes(sb, server->maxfilesize);
1131 	nfs_sysfs_move_server_to_sb(sb);
1132 	server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
1133 }
1134 
nfs_compare_mount_options(const struct super_block * s,const struct nfs_server * b,const struct fs_context * fc)1135 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
1136 				     const struct fs_context *fc)
1137 {
1138 	const struct nfs_server *a = s->s_fs_info;
1139 	const struct rpc_clnt *clnt_a = a->client;
1140 	const struct rpc_clnt *clnt_b = b->client;
1141 
1142 	if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
1143 		goto Ebusy;
1144 	if (a->nfs_client != b->nfs_client)
1145 		goto Ebusy;
1146 	if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
1147 		goto Ebusy;
1148 	if (a->wsize != b->wsize)
1149 		goto Ebusy;
1150 	if (a->rsize != b->rsize)
1151 		goto Ebusy;
1152 	if (a->acregmin != b->acregmin)
1153 		goto Ebusy;
1154 	if (a->acregmax != b->acregmax)
1155 		goto Ebusy;
1156 	if (a->acdirmin != b->acdirmin)
1157 		goto Ebusy;
1158 	if (a->acdirmax != b->acdirmax)
1159 		goto Ebusy;
1160 	if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1161 		goto Ebusy;
1162 	return 1;
1163 Ebusy:
1164 	return 0;
1165 }
1166 
nfs_set_super(struct super_block * s,struct fs_context * fc)1167 static int nfs_set_super(struct super_block *s, struct fs_context *fc)
1168 {
1169 	struct nfs_server *server = fc->s_fs_info;
1170 	int ret;
1171 
1172 	s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
1173 	ret = set_anon_super(s, server);
1174 	if (ret == 0)
1175 		server->s_dev = s->s_dev;
1176 	return ret;
1177 }
1178 
nfs_compare_super_address(struct nfs_server * server1,struct nfs_server * server2)1179 static int nfs_compare_super_address(struct nfs_server *server1,
1180 				     struct nfs_server *server2)
1181 {
1182 	struct sockaddr *sap1, *sap2;
1183 	struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1184 	struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1185 
1186 	if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1187 		return 0;
1188 
1189 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1190 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1191 
1192 	if (sap1->sa_family != sap2->sa_family)
1193 		return 0;
1194 
1195 	switch (sap1->sa_family) {
1196 	case AF_INET: {
1197 		struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1198 		struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1199 		if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1200 			return 0;
1201 		if (sin1->sin_port != sin2->sin_port)
1202 			return 0;
1203 		break;
1204 	}
1205 	case AF_INET6: {
1206 		struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1207 		struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1208 		if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1209 			return 0;
1210 		if (sin1->sin6_port != sin2->sin6_port)
1211 			return 0;
1212 		break;
1213 	}
1214 	default:
1215 		return 0;
1216 	}
1217 
1218 	return 1;
1219 }
1220 
nfs_compare_userns(const struct nfs_server * old,const struct nfs_server * new)1221 static int nfs_compare_userns(const struct nfs_server *old,
1222 		const struct nfs_server *new)
1223 {
1224 	const struct user_namespace *oldns = &init_user_ns;
1225 	const struct user_namespace *newns = &init_user_ns;
1226 
1227 	if (old->client && old->client->cl_cred)
1228 		oldns = old->client->cl_cred->user_ns;
1229 	if (new->client && new->client->cl_cred)
1230 		newns = new->client->cl_cred->user_ns;
1231 	if (oldns != newns)
1232 		return 0;
1233 	return 1;
1234 }
1235 
nfs_compare_super(struct super_block * sb,struct fs_context * fc)1236 static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
1237 {
1238 	struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
1239 
1240 	if (!nfs_compare_super_address(old, server))
1241 		return 0;
1242 	/* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1243 	if (old->flags & NFS_MOUNT_UNSHARED)
1244 		return 0;
1245 	if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1246 		return 0;
1247 	if (!nfs_compare_userns(old, server))
1248 		return 0;
1249 	if ((old->has_sec_mnt_opts || fc->security) &&
1250 			security_sb_mnt_opts_compat(sb, fc->security))
1251 		return 0;
1252 	return nfs_compare_mount_options(sb, server, fc);
1253 }
1254 
1255 #ifdef CONFIG_NFS_FSCACHE
nfs_get_cache_cookie(struct super_block * sb,struct nfs_fs_context * ctx)1256 static int nfs_get_cache_cookie(struct super_block *sb,
1257 				struct nfs_fs_context *ctx)
1258 {
1259 	struct nfs_server *nfss = NFS_SB(sb);
1260 	char *uniq = NULL;
1261 	int ulen = 0;
1262 
1263 	nfss->fscache = NULL;
1264 
1265 	if (!ctx)
1266 		return 0;
1267 
1268 	if (ctx->clone_data.sb) {
1269 		struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
1270 		if (!(mnt_s->options & NFS_OPTION_FSCACHE))
1271 			return 0;
1272 		if (mnt_s->fscache_uniq) {
1273 			uniq = mnt_s->fscache_uniq;
1274 			ulen = strlen(uniq);
1275 		}
1276 	} else {
1277 		if (!(ctx->options & NFS_OPTION_FSCACHE))
1278 			return 0;
1279 		if (ctx->fscache_uniq) {
1280 			uniq = ctx->fscache_uniq;
1281 			ulen = strlen(ctx->fscache_uniq);
1282 		}
1283 	}
1284 
1285 	return nfs_fscache_get_super_cookie(sb, uniq, ulen);
1286 }
1287 #else
nfs_get_cache_cookie(struct super_block * sb,struct nfs_fs_context * ctx)1288 static int nfs_get_cache_cookie(struct super_block *sb,
1289 				struct nfs_fs_context *ctx)
1290 {
1291 	return 0;
1292 }
1293 #endif
1294 
nfs_get_tree_common(struct fs_context * fc)1295 int nfs_get_tree_common(struct fs_context *fc)
1296 {
1297 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1298 	struct super_block *s;
1299 	int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
1300 	struct nfs_server *server = ctx->server;
1301 	int error;
1302 
1303 	ctx->server = NULL;
1304 	if (IS_ERR(server))
1305 		return PTR_ERR(server);
1306 
1307 	if (server->flags & NFS_MOUNT_UNSHARED)
1308 		compare_super = NULL;
1309 
1310 	/* -o noac implies -o sync */
1311 	if (server->flags & NFS_MOUNT_NOAC)
1312 		fc->sb_flags |= SB_SYNCHRONOUS;
1313 
1314 	if (ctx->clone_data.sb)
1315 		if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
1316 			fc->sb_flags |= SB_SYNCHRONOUS;
1317 
1318 	/* Get a superblock - note that we may end up sharing one that already exists */
1319 	fc->s_fs_info = server;
1320 	s = sget_fc(fc, compare_super, nfs_set_super);
1321 	fc->s_fs_info = NULL;
1322 	if (IS_ERR(s)) {
1323 		error = PTR_ERR(s);
1324 		nfs_errorf(fc, "NFS: Couldn't get superblock");
1325 		goto out_err_nosb;
1326 	}
1327 
1328 	if (s->s_fs_info != server) {
1329 		nfs_free_server(server);
1330 		server = NULL;
1331 	} else {
1332 		error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1333 					     MINOR(server->s_dev));
1334 		if (error)
1335 			goto error_splat_super;
1336 		s->s_bdi->io_pages = server->rpages;
1337 		server->super = s;
1338 	}
1339 
1340 	if (!s->s_root) {
1341 		unsigned bsize = ctx->clone_data.inherited_bsize;
1342 		/* initial superblock/root creation */
1343 		nfs_fill_super(s, ctx);
1344 		if (bsize) {
1345 			s->s_blocksize_bits = bsize;
1346 			s->s_blocksize = 1U << bsize;
1347 		}
1348 		error = nfs_get_cache_cookie(s, ctx);
1349 		if (error < 0)
1350 			goto error_splat_super;
1351 	}
1352 
1353 	error = nfs_get_root(s, fc);
1354 	if (error < 0) {
1355 		nfs_errorf(fc, "NFS: Couldn't get root dentry");
1356 		goto error_splat_super;
1357 	}
1358 
1359 	s->s_flags |= SB_ACTIVE;
1360 	error = 0;
1361 
1362 out:
1363 	return error;
1364 
1365 out_err_nosb:
1366 	nfs_free_server(server);
1367 	goto out;
1368 error_splat_super:
1369 	deactivate_locked_super(s);
1370 	goto out;
1371 }
1372 
1373 /*
1374  * Destroy an NFS superblock
1375  */
nfs_kill_super(struct super_block * s)1376 void nfs_kill_super(struct super_block *s)
1377 {
1378 	struct nfs_server *server = NFS_SB(s);
1379 
1380 	nfs_sysfs_move_sb_to_server(server);
1381 	kill_anon_super(s);
1382 
1383 	nfs_fscache_release_super_cookie(s);
1384 
1385 	nfs_free_server(server);
1386 }
1387 EXPORT_SYMBOL_GPL(nfs_kill_super);
1388 
1389 #if IS_ENABLED(CONFIG_NFS_V4)
1390 
1391 /*
1392  * NFS v4 module parameters need to stay in the
1393  * NFS client for backwards compatibility
1394  */
1395 unsigned int nfs_callback_set_tcpport;
1396 unsigned short nfs_callback_nr_threads;
1397 /* Default cache timeout is 10 minutes */
1398 unsigned int nfs_idmap_cache_timeout = 600;
1399 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1400 bool nfs4_disable_idmapping = true;
1401 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
1402 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
1403 unsigned short send_implementation_id = 1;
1404 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
1405 bool recover_lost_locks = false;
1406 short nfs_delay_retrans = -1;
1407 
1408 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
1409 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
1410 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1411 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1412 EXPORT_SYMBOL_GPL(max_session_slots);
1413 EXPORT_SYMBOL_GPL(max_session_cb_slots);
1414 EXPORT_SYMBOL_GPL(send_implementation_id);
1415 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
1416 EXPORT_SYMBOL_GPL(recover_lost_locks);
1417 EXPORT_SYMBOL_GPL(nfs_delay_retrans);
1418 
1419 #define NFS_CALLBACK_MAXPORTNR (65535U)
1420 
param_set_portnr(const char * val,const struct kernel_param * kp)1421 static int param_set_portnr(const char *val, const struct kernel_param *kp)
1422 {
1423 	unsigned long num;
1424 	int ret;
1425 
1426 	if (!val)
1427 		return -EINVAL;
1428 	ret = kstrtoul(val, 0, &num);
1429 	if (ret || num > NFS_CALLBACK_MAXPORTNR)
1430 		return -EINVAL;
1431 	*((unsigned int *)kp->arg) = num;
1432 	return 0;
1433 }
1434 static const struct kernel_param_ops param_ops_portnr = {
1435 	.set = param_set_portnr,
1436 	.get = param_get_uint,
1437 };
1438 #define param_check_portnr(name, p) __param_check(name, p, unsigned int)
1439 
1440 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
1441 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1442 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1443 		"assigned to the NFSv4 callback channels.");
1444 module_param(nfs_idmap_cache_timeout, int, 0644);
1445 module_param(nfs4_disable_idmapping, bool, 0644);
1446 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1447 			NFS4_CLIENT_ID_UNIQ_LEN, 0600);
1448 MODULE_PARM_DESC(nfs4_disable_idmapping,
1449 		"Turn off NFSv4 idmapping when using 'sec=sys'");
1450 module_param(max_session_slots, ushort, 0644);
1451 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1452 		"requests the client will negotiate");
1453 module_param(max_session_cb_slots, ushort, 0644);
1454 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
1455 		"callbacks the client will process for a given server");
1456 module_param(send_implementation_id, ushort, 0644);
1457 MODULE_PARM_DESC(send_implementation_id,
1458 		"Send implementation ID with NFSv4.1 exchange_id");
1459 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
1460 
1461 module_param(recover_lost_locks, bool, 0644);
1462 MODULE_PARM_DESC(recover_lost_locks,
1463 		 "If the server reports that a lock might be lost, "
1464 		 "try to recover it risking data corruption.");
1465 
1466 module_param_named(delay_retrans, nfs_delay_retrans, short, 0644);
1467 MODULE_PARM_DESC(delay_retrans,
1468 		 "Unless negative, specifies the number of times the NFSv4 "
1469 		 "client retries a request before returning an EAGAIN error, "
1470 		 "after a reply of NFS4ERR_DELAY from the server.");
1471 #endif /* CONFIG_NFS_V4 */
1472