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