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