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