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