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