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