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