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