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