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