net: replace %p6 with %pI6
[linux-2.6/linux-2.6-openrd.git] / fs / nfs / super.c
blobeb391d8d70ba023f94d9212d585f64f19abd27ab
1 /*
2 * linux/fs/nfs/super.c
4 * Copyright (C) 1992 Rick Sladkey
6 * nfs superblock handling functions
8 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
14 * Split from inode.c by David Howells <dhowells@redhat.com>
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())
23 #include <linux/module.h>
24 #include <linux/init.h>
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/nfs_idmap.h>
46 #include <linux/vfs.h>
47 #include <linux/inet.h>
48 #include <linux/in6.h>
49 #include <net/ipv6.h>
50 #include <linux/netdevice.h>
51 #include <linux/nfs_xdr.h>
52 #include <linux/magic.h>
53 #include <linux/parser.h>
55 #include <asm/system.h>
56 #include <asm/uaccess.h>
58 #include "nfs4_fs.h"
59 #include "callback.h"
60 #include "delegation.h"
61 #include "iostat.h"
62 #include "internal.h"
64 #define NFSDBG_FACILITY NFSDBG_VFS
66 enum {
67 /* Mount options that take no arguments */
68 Opt_soft, Opt_hard,
69 Opt_posix, Opt_noposix,
70 Opt_cto, Opt_nocto,
71 Opt_ac, Opt_noac,
72 Opt_lock, Opt_nolock,
73 Opt_v2, Opt_v3,
74 Opt_udp, Opt_tcp, Opt_rdma,
75 Opt_acl, Opt_noacl,
76 Opt_rdirplus, Opt_nordirplus,
77 Opt_sharecache, Opt_nosharecache,
79 /* Mount options that take integer arguments */
80 Opt_port,
81 Opt_rsize, Opt_wsize, Opt_bsize,
82 Opt_timeo, Opt_retrans,
83 Opt_acregmin, Opt_acregmax,
84 Opt_acdirmin, Opt_acdirmax,
85 Opt_actimeo,
86 Opt_namelen,
87 Opt_mountport,
88 Opt_mountvers,
89 Opt_nfsvers,
91 /* Mount options that take string arguments */
92 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
93 Opt_addr, Opt_mountaddr, Opt_clientaddr,
94 Opt_lookupcache,
96 /* Special mount options */
97 Opt_userspace, Opt_deprecated, Opt_sloppy,
99 Opt_err
102 static const match_table_t nfs_mount_option_tokens = {
103 { Opt_userspace, "bg" },
104 { Opt_userspace, "fg" },
105 { Opt_userspace, "retry=%s" },
107 { Opt_sloppy, "sloppy" },
109 { Opt_soft, "soft" },
110 { Opt_hard, "hard" },
111 { Opt_deprecated, "intr" },
112 { Opt_deprecated, "nointr" },
113 { Opt_posix, "posix" },
114 { Opt_noposix, "noposix" },
115 { Opt_cto, "cto" },
116 { Opt_nocto, "nocto" },
117 { Opt_ac, "ac" },
118 { Opt_noac, "noac" },
119 { Opt_lock, "lock" },
120 { Opt_nolock, "nolock" },
121 { Opt_v2, "v2" },
122 { Opt_v3, "v3" },
123 { Opt_udp, "udp" },
124 { Opt_tcp, "tcp" },
125 { Opt_rdma, "rdma" },
126 { Opt_acl, "acl" },
127 { Opt_noacl, "noacl" },
128 { Opt_rdirplus, "rdirplus" },
129 { Opt_nordirplus, "nordirplus" },
130 { Opt_sharecache, "sharecache" },
131 { Opt_nosharecache, "nosharecache" },
133 { Opt_port, "port=%u" },
134 { Opt_rsize, "rsize=%u" },
135 { Opt_wsize, "wsize=%u" },
136 { Opt_bsize, "bsize=%u" },
137 { Opt_timeo, "timeo=%u" },
138 { Opt_retrans, "retrans=%u" },
139 { Opt_acregmin, "acregmin=%u" },
140 { Opt_acregmax, "acregmax=%u" },
141 { Opt_acdirmin, "acdirmin=%u" },
142 { Opt_acdirmax, "acdirmax=%u" },
143 { Opt_actimeo, "actimeo=%u" },
144 { Opt_namelen, "namlen=%u" },
145 { Opt_mountport, "mountport=%u" },
146 { Opt_mountvers, "mountvers=%u" },
147 { Opt_nfsvers, "nfsvers=%u" },
148 { Opt_nfsvers, "vers=%u" },
150 { Opt_sec, "sec=%s" },
151 { Opt_proto, "proto=%s" },
152 { Opt_mountproto, "mountproto=%s" },
153 { Opt_addr, "addr=%s" },
154 { Opt_clientaddr, "clientaddr=%s" },
155 { Opt_mounthost, "mounthost=%s" },
156 { Opt_mountaddr, "mountaddr=%s" },
158 { Opt_lookupcache, "lookupcache=%s" },
160 { Opt_err, NULL }
163 enum {
164 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
166 Opt_xprt_err
169 static const match_table_t nfs_xprt_protocol_tokens = {
170 { Opt_xprt_udp, "udp" },
171 { Opt_xprt_tcp, "tcp" },
172 { Opt_xprt_rdma, "rdma" },
174 { Opt_xprt_err, NULL }
177 enum {
178 Opt_sec_none, Opt_sec_sys,
179 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
180 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
181 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
183 Opt_sec_err
186 static const match_table_t nfs_secflavor_tokens = {
187 { Opt_sec_none, "none" },
188 { Opt_sec_none, "null" },
189 { Opt_sec_sys, "sys" },
191 { Opt_sec_krb5, "krb5" },
192 { Opt_sec_krb5i, "krb5i" },
193 { Opt_sec_krb5p, "krb5p" },
195 { Opt_sec_lkey, "lkey" },
196 { Opt_sec_lkeyi, "lkeyi" },
197 { Opt_sec_lkeyp, "lkeyp" },
199 { Opt_sec_spkm, "spkm3" },
200 { Opt_sec_spkmi, "spkm3i" },
201 { Opt_sec_spkmp, "spkm3p" },
203 { Opt_sec_err, NULL }
206 enum {
207 Opt_lookupcache_all, Opt_lookupcache_positive,
208 Opt_lookupcache_none,
210 Opt_lookupcache_err
213 static match_table_t nfs_lookupcache_tokens = {
214 { Opt_lookupcache_all, "all" },
215 { Opt_lookupcache_positive, "pos" },
216 { Opt_lookupcache_positive, "positive" },
217 { Opt_lookupcache_none, "none" },
219 { Opt_lookupcache_err, NULL }
223 static void nfs_umount_begin(struct super_block *);
224 static int nfs_statfs(struct dentry *, struct kstatfs *);
225 static int nfs_show_options(struct seq_file *, struct vfsmount *);
226 static int nfs_show_stats(struct seq_file *, struct vfsmount *);
227 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
228 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
229 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
230 static void nfs_kill_super(struct super_block *);
231 static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
233 static struct file_system_type nfs_fs_type = {
234 .owner = THIS_MODULE,
235 .name = "nfs",
236 .get_sb = nfs_get_sb,
237 .kill_sb = nfs_kill_super,
238 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
241 struct file_system_type nfs_xdev_fs_type = {
242 .owner = THIS_MODULE,
243 .name = "nfs",
244 .get_sb = nfs_xdev_get_sb,
245 .kill_sb = nfs_kill_super,
246 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
249 static const struct super_operations nfs_sops = {
250 .alloc_inode = nfs_alloc_inode,
251 .destroy_inode = nfs_destroy_inode,
252 .write_inode = nfs_write_inode,
253 .statfs = nfs_statfs,
254 .clear_inode = nfs_clear_inode,
255 .umount_begin = nfs_umount_begin,
256 .show_options = nfs_show_options,
257 .show_stats = nfs_show_stats,
258 .remount_fs = nfs_remount,
261 #ifdef CONFIG_NFS_V4
262 static int nfs4_get_sb(struct file_system_type *fs_type,
263 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
264 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
265 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
266 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
267 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
268 static void nfs4_kill_super(struct super_block *sb);
270 static struct file_system_type nfs4_fs_type = {
271 .owner = THIS_MODULE,
272 .name = "nfs4",
273 .get_sb = nfs4_get_sb,
274 .kill_sb = nfs4_kill_super,
275 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
278 struct file_system_type nfs4_xdev_fs_type = {
279 .owner = THIS_MODULE,
280 .name = "nfs4",
281 .get_sb = nfs4_xdev_get_sb,
282 .kill_sb = nfs4_kill_super,
283 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
286 struct file_system_type nfs4_referral_fs_type = {
287 .owner = THIS_MODULE,
288 .name = "nfs4",
289 .get_sb = nfs4_referral_get_sb,
290 .kill_sb = nfs4_kill_super,
291 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
294 static const struct super_operations nfs4_sops = {
295 .alloc_inode = nfs_alloc_inode,
296 .destroy_inode = nfs_destroy_inode,
297 .write_inode = nfs_write_inode,
298 .statfs = nfs_statfs,
299 .clear_inode = nfs4_clear_inode,
300 .umount_begin = nfs_umount_begin,
301 .show_options = nfs_show_options,
302 .show_stats = nfs_show_stats,
303 .remount_fs = nfs_remount,
305 #endif
307 static struct shrinker acl_shrinker = {
308 .shrink = nfs_access_cache_shrinker,
309 .seeks = DEFAULT_SEEKS,
313 * Register the NFS filesystems
315 int __init register_nfs_fs(void)
317 int ret;
319 ret = register_filesystem(&nfs_fs_type);
320 if (ret < 0)
321 goto error_0;
323 ret = nfs_register_sysctl();
324 if (ret < 0)
325 goto error_1;
326 #ifdef CONFIG_NFS_V4
327 ret = register_filesystem(&nfs4_fs_type);
328 if (ret < 0)
329 goto error_2;
330 #endif
331 register_shrinker(&acl_shrinker);
332 return 0;
334 #ifdef CONFIG_NFS_V4
335 error_2:
336 nfs_unregister_sysctl();
337 #endif
338 error_1:
339 unregister_filesystem(&nfs_fs_type);
340 error_0:
341 return ret;
345 * Unregister the NFS filesystems
347 void __exit unregister_nfs_fs(void)
349 unregister_shrinker(&acl_shrinker);
350 #ifdef CONFIG_NFS_V4
351 unregister_filesystem(&nfs4_fs_type);
352 #endif
353 nfs_unregister_sysctl();
354 unregister_filesystem(&nfs_fs_type);
357 void nfs_sb_active(struct super_block *sb)
359 struct nfs_server *server = NFS_SB(sb);
361 if (atomic_inc_return(&server->active) == 1)
362 atomic_inc(&sb->s_active);
365 void nfs_sb_deactive(struct super_block *sb)
367 struct nfs_server *server = NFS_SB(sb);
369 if (atomic_dec_and_test(&server->active))
370 deactivate_super(sb);
374 * Deliver file system statistics to userspace
376 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
378 struct nfs_server *server = NFS_SB(dentry->d_sb);
379 unsigned char blockbits;
380 unsigned long blockres;
381 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
382 struct nfs_fattr fattr;
383 struct nfs_fsstat res = {
384 .fattr = &fattr,
386 int error;
388 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
389 if (error < 0)
390 goto out_err;
391 buf->f_type = NFS_SUPER_MAGIC;
394 * Current versions of glibc do not correctly handle the
395 * case where f_frsize != f_bsize. Eventually we want to
396 * report the value of wtmult in this field.
398 buf->f_frsize = dentry->d_sb->s_blocksize;
401 * On most *nix systems, f_blocks, f_bfree, and f_bavail
402 * are reported in units of f_frsize. Linux hasn't had
403 * an f_frsize field in its statfs struct until recently,
404 * thus historically Linux's sys_statfs reports these
405 * fields in units of f_bsize.
407 buf->f_bsize = dentry->d_sb->s_blocksize;
408 blockbits = dentry->d_sb->s_blocksize_bits;
409 blockres = (1 << blockbits) - 1;
410 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
411 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
412 buf->f_bavail = (res.abytes + blockres) >> blockbits;
414 buf->f_files = res.tfiles;
415 buf->f_ffree = res.afiles;
417 buf->f_namelen = server->namelen;
419 return 0;
421 out_err:
422 dprintk("%s: statfs error = %d\n", __func__, -error);
423 return error;
427 * Map the security flavour number to a name
429 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
431 static const struct {
432 rpc_authflavor_t flavour;
433 const char *str;
434 } sec_flavours[] = {
435 { RPC_AUTH_NULL, "null" },
436 { RPC_AUTH_UNIX, "sys" },
437 { RPC_AUTH_GSS_KRB5, "krb5" },
438 { RPC_AUTH_GSS_KRB5I, "krb5i" },
439 { RPC_AUTH_GSS_KRB5P, "krb5p" },
440 { RPC_AUTH_GSS_LKEY, "lkey" },
441 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
442 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
443 { RPC_AUTH_GSS_SPKM, "spkm" },
444 { RPC_AUTH_GSS_SPKMI, "spkmi" },
445 { RPC_AUTH_GSS_SPKMP, "spkmp" },
446 { UINT_MAX, "unknown" }
448 int i;
450 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
451 if (sec_flavours[i].flavour == flavour)
452 break;
454 return sec_flavours[i].str;
457 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
458 int showdefaults)
460 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
462 switch (sap->sa_family) {
463 case AF_INET: {
464 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
465 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
466 NIPQUAD(sin->sin_addr.s_addr));
467 break;
469 case AF_INET6: {
470 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
471 seq_printf(m, ",mountaddr=%pI6", &sin6->sin6_addr);
472 break;
474 default:
475 if (showdefaults)
476 seq_printf(m, ",mountaddr=unspecified");
479 if (nfss->mountd_version || showdefaults)
480 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
481 if (nfss->mountd_port || showdefaults)
482 seq_printf(m, ",mountport=%u", nfss->mountd_port);
484 switch (nfss->mountd_protocol) {
485 case IPPROTO_UDP:
486 seq_printf(m, ",mountproto=udp");
487 break;
488 case IPPROTO_TCP:
489 seq_printf(m, ",mountproto=tcp");
490 break;
491 default:
492 if (showdefaults)
493 seq_printf(m, ",mountproto=auto");
498 * Describe the mount options in force on this server representation
500 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
501 int showdefaults)
503 static const struct proc_nfs_info {
504 int flag;
505 const char *str;
506 const char *nostr;
507 } nfs_info[] = {
508 { NFS_MOUNT_SOFT, ",soft", ",hard" },
509 { NFS_MOUNT_INTR, ",intr", ",nointr" },
510 { NFS_MOUNT_POSIX, ",posix", "" },
511 { NFS_MOUNT_NOCTO, ",nocto", "" },
512 { NFS_MOUNT_NOAC, ",noac", "" },
513 { NFS_MOUNT_NONLM, ",nolock", "" },
514 { NFS_MOUNT_NOACL, ",noacl", "" },
515 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
516 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
517 { 0, NULL, NULL }
519 const struct proc_nfs_info *nfs_infop;
520 struct nfs_client *clp = nfss->nfs_client;
521 u32 version = clp->rpc_ops->version;
523 seq_printf(m, ",vers=%u", version);
524 seq_printf(m, ",rsize=%u", nfss->rsize);
525 seq_printf(m, ",wsize=%u", nfss->wsize);
526 if (nfss->bsize != 0)
527 seq_printf(m, ",bsize=%u", nfss->bsize);
528 seq_printf(m, ",namlen=%u", nfss->namelen);
529 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
530 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
531 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
532 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
533 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
534 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
535 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
536 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
537 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
538 if (nfss->flags & nfs_infop->flag)
539 seq_puts(m, nfs_infop->str);
540 else
541 seq_puts(m, nfs_infop->nostr);
543 seq_printf(m, ",proto=%s",
544 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
545 if (version == 4) {
546 if (nfss->port != NFS_PORT)
547 seq_printf(m, ",port=%u", nfss->port);
548 } else
549 if (nfss->port)
550 seq_printf(m, ",port=%u", nfss->port);
552 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
553 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
554 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
556 if (version != 4)
557 nfs_show_mountd_options(m, nfss, showdefaults);
559 #ifdef CONFIG_NFS_V4
560 if (clp->rpc_ops->version == 4)
561 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
562 #endif
566 * Describe the mount options on this VFS mountpoint
568 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
570 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
572 nfs_show_mount_options(m, nfss, 0);
574 seq_printf(m, ",addr=%s",
575 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
576 RPC_DISPLAY_ADDR));
578 return 0;
582 * Present statistical information for this VFS mountpoint
584 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
586 int i, cpu;
587 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
588 struct rpc_auth *auth = nfss->client->cl_auth;
589 struct nfs_iostats totals = { };
591 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
594 * Display all mount option settings
596 seq_printf(m, "\n\topts:\t");
597 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
598 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
599 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
600 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
601 nfs_show_mount_options(m, nfss, 1);
603 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
605 seq_printf(m, "\n\tcaps:\t");
606 seq_printf(m, "caps=0x%x", nfss->caps);
607 seq_printf(m, ",wtmult=%u", nfss->wtmult);
608 seq_printf(m, ",dtsize=%u", nfss->dtsize);
609 seq_printf(m, ",bsize=%u", nfss->bsize);
610 seq_printf(m, ",namlen=%u", nfss->namelen);
612 #ifdef CONFIG_NFS_V4
613 if (nfss->nfs_client->rpc_ops->version == 4) {
614 seq_printf(m, "\n\tnfsv4:\t");
615 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
616 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
617 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
619 #endif
622 * Display security flavor in effect for this mount
624 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
625 if (auth->au_flavor)
626 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
629 * Display superblock I/O counters
631 for_each_possible_cpu(cpu) {
632 struct nfs_iostats *stats;
634 preempt_disable();
635 stats = per_cpu_ptr(nfss->io_stats, cpu);
637 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
638 totals.events[i] += stats->events[i];
639 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
640 totals.bytes[i] += stats->bytes[i];
642 preempt_enable();
645 seq_printf(m, "\n\tevents:\t");
646 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
647 seq_printf(m, "%lu ", totals.events[i]);
648 seq_printf(m, "\n\tbytes:\t");
649 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
650 seq_printf(m, "%Lu ", totals.bytes[i]);
651 seq_printf(m, "\n");
653 rpc_print_iostats(m, nfss->client);
655 return 0;
659 * Begin unmount by attempting to remove all automounted mountpoints we added
660 * in response to xdev traversals and referrals
662 static void nfs_umount_begin(struct super_block *sb)
664 struct nfs_server *server = NFS_SB(sb);
665 struct rpc_clnt *rpc;
667 /* -EIO all pending I/O */
668 rpc = server->client_acl;
669 if (!IS_ERR(rpc))
670 rpc_killall_tasks(rpc);
671 rpc = server->client;
672 if (!IS_ERR(rpc))
673 rpc_killall_tasks(rpc);
677 * Sanity-check a server address provided by the mount command.
679 * Address family must be initialized, and address must not be
680 * the ANY address for that family.
682 static int nfs_verify_server_address(struct sockaddr *addr)
684 switch (addr->sa_family) {
685 case AF_INET: {
686 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
687 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
689 case AF_INET6: {
690 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
691 return !ipv6_addr_any(sa);
695 return 0;
698 static void nfs_parse_ipv4_address(char *string, size_t str_len,
699 struct sockaddr *sap, size_t *addr_len)
701 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
702 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
704 if (str_len <= INET_ADDRSTRLEN) {
705 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
706 (int)str_len, string);
708 sin->sin_family = AF_INET;
709 *addr_len = sizeof(*sin);
710 if (in4_pton(string, str_len, addr, '\0', NULL))
711 return;
714 sap->sa_family = AF_UNSPEC;
715 *addr_len = 0;
718 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
719 static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
720 const char *delim,
721 struct sockaddr_in6 *sin6)
723 char *p;
724 size_t len;
726 if ((string + str_len) == delim)
727 return 1;
729 if (*delim != IPV6_SCOPE_DELIMITER)
730 return 0;
732 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
733 return 0;
735 len = (string + str_len) - delim - 1;
736 p = kstrndup(delim + 1, len, GFP_KERNEL);
737 if (p) {
738 unsigned long scope_id = 0;
739 struct net_device *dev;
741 dev = dev_get_by_name(&init_net, p);
742 if (dev != NULL) {
743 scope_id = dev->ifindex;
744 dev_put(dev);
745 } else {
746 if (strict_strtoul(p, 10, &scope_id) == 0) {
747 kfree(p);
748 return 0;
752 kfree(p);
754 sin6->sin6_scope_id = scope_id;
755 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
756 return 1;
759 return 0;
762 static void nfs_parse_ipv6_address(char *string, size_t str_len,
763 struct sockaddr *sap, size_t *addr_len)
765 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
766 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
767 const char *delim;
769 if (str_len <= INET6_ADDRSTRLEN) {
770 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
771 (int)str_len, string);
773 sin6->sin6_family = AF_INET6;
774 *addr_len = sizeof(*sin6);
775 if (in6_pton(string, str_len, addr,
776 IPV6_SCOPE_DELIMITER, &delim) != 0) {
777 if (nfs_parse_ipv6_scope_id(string, str_len,
778 delim, sin6) != 0)
779 return;
783 sap->sa_family = AF_UNSPEC;
784 *addr_len = 0;
786 #else
787 static void nfs_parse_ipv6_address(char *string, size_t str_len,
788 struct sockaddr *sap, size_t *addr_len)
790 sap->sa_family = AF_UNSPEC;
791 *addr_len = 0;
793 #endif
796 * Construct a sockaddr based on the contents of a string that contains
797 * an IP address in presentation format.
799 * If there is a problem constructing the new sockaddr, set the address
800 * family to AF_UNSPEC.
802 void nfs_parse_ip_address(char *string, size_t str_len,
803 struct sockaddr *sap, size_t *addr_len)
805 unsigned int i, colons;
807 colons = 0;
808 for (i = 0; i < str_len; i++)
809 if (string[i] == ':')
810 colons++;
812 if (colons >= 2)
813 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
814 else
815 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
819 * Sanity check the NFS transport protocol.
822 static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
824 switch (mnt->nfs_server.protocol) {
825 case XPRT_TRANSPORT_UDP:
826 case XPRT_TRANSPORT_TCP:
827 case XPRT_TRANSPORT_RDMA:
828 break;
829 default:
830 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
835 * For text based NFSv2/v3 mounts, the mount protocol transport default
836 * settings should depend upon the specified NFS transport.
838 static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
840 nfs_validate_transport_protocol(mnt);
842 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
843 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
844 return;
845 switch (mnt->nfs_server.protocol) {
846 case XPRT_TRANSPORT_UDP:
847 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
848 break;
849 case XPRT_TRANSPORT_TCP:
850 case XPRT_TRANSPORT_RDMA:
851 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
856 * Parse the value of the 'sec=' option.
858 * The flavor_len setting is for v4 mounts.
860 static int nfs_parse_security_flavors(char *value,
861 struct nfs_parsed_mount_data *mnt)
863 substring_t args[MAX_OPT_ARGS];
865 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
867 switch (match_token(value, nfs_secflavor_tokens, args)) {
868 case Opt_sec_none:
869 mnt->auth_flavor_len = 0;
870 mnt->auth_flavors[0] = RPC_AUTH_NULL;
871 break;
872 case Opt_sec_sys:
873 mnt->auth_flavor_len = 0;
874 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
875 break;
876 case Opt_sec_krb5:
877 mnt->auth_flavor_len = 1;
878 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
879 break;
880 case Opt_sec_krb5i:
881 mnt->auth_flavor_len = 1;
882 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
883 break;
884 case Opt_sec_krb5p:
885 mnt->auth_flavor_len = 1;
886 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
887 break;
888 case Opt_sec_lkey:
889 mnt->auth_flavor_len = 1;
890 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
891 break;
892 case Opt_sec_lkeyi:
893 mnt->auth_flavor_len = 1;
894 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
895 break;
896 case Opt_sec_lkeyp:
897 mnt->auth_flavor_len = 1;
898 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
899 break;
900 case Opt_sec_spkm:
901 mnt->auth_flavor_len = 1;
902 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
903 break;
904 case Opt_sec_spkmi:
905 mnt->auth_flavor_len = 1;
906 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
907 break;
908 case Opt_sec_spkmp:
909 mnt->auth_flavor_len = 1;
910 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
911 break;
912 default:
913 return 0;
916 return 1;
919 static void nfs_parse_invalid_value(const char *option)
921 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
925 * Error-check and convert a string of mount options from user space into
926 * a data structure. The whole mount string is processed; bad options are
927 * skipped as they are encountered. If there were no errors, return 1;
928 * otherwise return 0 (zero).
930 static int nfs_parse_mount_options(char *raw,
931 struct nfs_parsed_mount_data *mnt)
933 char *p, *string, *secdata;
934 int rc, sloppy = 0, errors = 0;
936 if (!raw) {
937 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
938 return 1;
940 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
942 secdata = alloc_secdata();
943 if (!secdata)
944 goto out_nomem;
946 rc = security_sb_copy_data(raw, secdata);
947 if (rc)
948 goto out_security_failure;
950 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
951 if (rc)
952 goto out_security_failure;
954 free_secdata(secdata);
956 while ((p = strsep(&raw, ",")) != NULL) {
957 substring_t args[MAX_OPT_ARGS];
958 int option, token;
960 if (!*p)
961 continue;
963 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
965 token = match_token(p, nfs_mount_option_tokens, args);
966 switch (token) {
969 * boolean options: foo/nofoo
971 case Opt_soft:
972 mnt->flags |= NFS_MOUNT_SOFT;
973 break;
974 case Opt_hard:
975 mnt->flags &= ~NFS_MOUNT_SOFT;
976 break;
977 case Opt_posix:
978 mnt->flags |= NFS_MOUNT_POSIX;
979 break;
980 case Opt_noposix:
981 mnt->flags &= ~NFS_MOUNT_POSIX;
982 break;
983 case Opt_cto:
984 mnt->flags &= ~NFS_MOUNT_NOCTO;
985 break;
986 case Opt_nocto:
987 mnt->flags |= NFS_MOUNT_NOCTO;
988 break;
989 case Opt_ac:
990 mnt->flags &= ~NFS_MOUNT_NOAC;
991 break;
992 case Opt_noac:
993 mnt->flags |= NFS_MOUNT_NOAC;
994 break;
995 case Opt_lock:
996 mnt->flags &= ~NFS_MOUNT_NONLM;
997 break;
998 case Opt_nolock:
999 mnt->flags |= NFS_MOUNT_NONLM;
1000 break;
1001 case Opt_v2:
1002 mnt->flags &= ~NFS_MOUNT_VER3;
1003 break;
1004 case Opt_v3:
1005 mnt->flags |= NFS_MOUNT_VER3;
1006 break;
1007 case Opt_udp:
1008 mnt->flags &= ~NFS_MOUNT_TCP;
1009 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1010 break;
1011 case Opt_tcp:
1012 mnt->flags |= NFS_MOUNT_TCP;
1013 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1014 break;
1015 case Opt_rdma:
1016 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1017 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1018 break;
1019 case Opt_acl:
1020 mnt->flags &= ~NFS_MOUNT_NOACL;
1021 break;
1022 case Opt_noacl:
1023 mnt->flags |= NFS_MOUNT_NOACL;
1024 break;
1025 case Opt_rdirplus:
1026 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1027 break;
1028 case Opt_nordirplus:
1029 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1030 break;
1031 case Opt_sharecache:
1032 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1033 break;
1034 case Opt_nosharecache:
1035 mnt->flags |= NFS_MOUNT_UNSHARED;
1036 break;
1039 * options that take numeric values
1041 case Opt_port:
1042 if (match_int(args, &option) ||
1043 option < 0 || option > USHORT_MAX) {
1044 errors++;
1045 nfs_parse_invalid_value("port");
1046 } else
1047 mnt->nfs_server.port = option;
1048 break;
1049 case Opt_rsize:
1050 if (match_int(args, &option) || option < 0) {
1051 errors++;
1052 nfs_parse_invalid_value("rsize");
1053 } else
1054 mnt->rsize = option;
1055 break;
1056 case Opt_wsize:
1057 if (match_int(args, &option) || option < 0) {
1058 errors++;
1059 nfs_parse_invalid_value("wsize");
1060 } else
1061 mnt->wsize = option;
1062 break;
1063 case Opt_bsize:
1064 if (match_int(args, &option) || option < 0) {
1065 errors++;
1066 nfs_parse_invalid_value("bsize");
1067 } else
1068 mnt->bsize = option;
1069 break;
1070 case Opt_timeo:
1071 if (match_int(args, &option) || option <= 0) {
1072 errors++;
1073 nfs_parse_invalid_value("timeo");
1074 } else
1075 mnt->timeo = option;
1076 break;
1077 case Opt_retrans:
1078 if (match_int(args, &option) || option <= 0) {
1079 errors++;
1080 nfs_parse_invalid_value("retrans");
1081 } else
1082 mnt->retrans = option;
1083 break;
1084 case Opt_acregmin:
1085 if (match_int(args, &option) || option < 0) {
1086 errors++;
1087 nfs_parse_invalid_value("acregmin");
1088 } else
1089 mnt->acregmin = option;
1090 break;
1091 case Opt_acregmax:
1092 if (match_int(args, &option) || option < 0) {
1093 errors++;
1094 nfs_parse_invalid_value("acregmax");
1095 } else
1096 mnt->acregmax = option;
1097 break;
1098 case Opt_acdirmin:
1099 if (match_int(args, &option) || option < 0) {
1100 errors++;
1101 nfs_parse_invalid_value("acdirmin");
1102 } else
1103 mnt->acdirmin = option;
1104 break;
1105 case Opt_acdirmax:
1106 if (match_int(args, &option) || option < 0) {
1107 errors++;
1108 nfs_parse_invalid_value("acdirmax");
1109 } else
1110 mnt->acdirmax = option;
1111 break;
1112 case Opt_actimeo:
1113 if (match_int(args, &option) || option < 0) {
1114 errors++;
1115 nfs_parse_invalid_value("actimeo");
1116 } else
1117 mnt->acregmin = mnt->acregmax =
1118 mnt->acdirmin = mnt->acdirmax = option;
1119 break;
1120 case Opt_namelen:
1121 if (match_int(args, &option) || option < 0) {
1122 errors++;
1123 nfs_parse_invalid_value("namlen");
1124 } else
1125 mnt->namlen = option;
1126 break;
1127 case Opt_mountport:
1128 if (match_int(args, &option) ||
1129 option < 0 || option > USHORT_MAX) {
1130 errors++;
1131 nfs_parse_invalid_value("mountport");
1132 } else
1133 mnt->mount_server.port = option;
1134 break;
1135 case Opt_mountvers:
1136 if (match_int(args, &option) ||
1137 option < NFS_MNT_VERSION ||
1138 option > NFS_MNT3_VERSION) {
1139 errors++;
1140 nfs_parse_invalid_value("mountvers");
1141 } else
1142 mnt->mount_server.version = option;
1143 break;
1144 case Opt_nfsvers:
1145 if (match_int(args, &option)) {
1146 errors++;
1147 nfs_parse_invalid_value("nfsvers");
1148 break;
1150 switch (option) {
1151 case NFS2_VERSION:
1152 mnt->flags &= ~NFS_MOUNT_VER3;
1153 break;
1154 case NFS3_VERSION:
1155 mnt->flags |= NFS_MOUNT_VER3;
1156 break;
1157 default:
1158 errors++;
1159 nfs_parse_invalid_value("nfsvers");
1161 break;
1164 * options that take text values
1166 case Opt_sec:
1167 string = match_strdup(args);
1168 if (string == NULL)
1169 goto out_nomem;
1170 rc = nfs_parse_security_flavors(string, mnt);
1171 kfree(string);
1172 if (!rc) {
1173 errors++;
1174 dfprintk(MOUNT, "NFS: unrecognized "
1175 "security flavor\n");
1177 break;
1178 case Opt_proto:
1179 string = match_strdup(args);
1180 if (string == NULL)
1181 goto out_nomem;
1182 token = match_token(string,
1183 nfs_xprt_protocol_tokens, args);
1184 kfree(string);
1186 switch (token) {
1187 case Opt_xprt_udp:
1188 mnt->flags &= ~NFS_MOUNT_TCP;
1189 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1190 break;
1191 case Opt_xprt_tcp:
1192 mnt->flags |= NFS_MOUNT_TCP;
1193 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1194 break;
1195 case Opt_xprt_rdma:
1196 /* vector side protocols to TCP */
1197 mnt->flags |= NFS_MOUNT_TCP;
1198 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1199 break;
1200 default:
1201 errors++;
1202 dfprintk(MOUNT, "NFS: unrecognized "
1203 "transport protocol\n");
1205 break;
1206 case Opt_mountproto:
1207 string = match_strdup(args);
1208 if (string == NULL)
1209 goto out_nomem;
1210 token = match_token(string,
1211 nfs_xprt_protocol_tokens, args);
1212 kfree(string);
1214 switch (token) {
1215 case Opt_xprt_udp:
1216 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1217 break;
1218 case Opt_xprt_tcp:
1219 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1220 break;
1221 case Opt_xprt_rdma: /* not used for side protocols */
1222 default:
1223 errors++;
1224 dfprintk(MOUNT, "NFS: unrecognized "
1225 "transport protocol\n");
1227 break;
1228 case Opt_addr:
1229 string = match_strdup(args);
1230 if (string == NULL)
1231 goto out_nomem;
1232 nfs_parse_ip_address(string, strlen(string),
1233 (struct sockaddr *)
1234 &mnt->nfs_server.address,
1235 &mnt->nfs_server.addrlen);
1236 kfree(string);
1237 break;
1238 case Opt_clientaddr:
1239 string = match_strdup(args);
1240 if (string == NULL)
1241 goto out_nomem;
1242 kfree(mnt->client_address);
1243 mnt->client_address = string;
1244 break;
1245 case Opt_mounthost:
1246 string = match_strdup(args);
1247 if (string == NULL)
1248 goto out_nomem;
1249 kfree(mnt->mount_server.hostname);
1250 mnt->mount_server.hostname = string;
1251 break;
1252 case Opt_mountaddr:
1253 string = match_strdup(args);
1254 if (string == NULL)
1255 goto out_nomem;
1256 nfs_parse_ip_address(string, strlen(string),
1257 (struct sockaddr *)
1258 &mnt->mount_server.address,
1259 &mnt->mount_server.addrlen);
1260 kfree(string);
1261 break;
1262 case Opt_lookupcache:
1263 string = match_strdup(args);
1264 if (string == NULL)
1265 goto out_nomem;
1266 token = match_token(string,
1267 nfs_lookupcache_tokens, args);
1268 kfree(string);
1269 switch (token) {
1270 case Opt_lookupcache_all:
1271 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1272 break;
1273 case Opt_lookupcache_positive:
1274 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1275 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1276 break;
1277 case Opt_lookupcache_none:
1278 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1279 break;
1280 default:
1281 errors++;
1282 dfprintk(MOUNT, "NFS: invalid "
1283 "lookupcache argument\n");
1285 break;
1288 * Special options
1290 case Opt_sloppy:
1291 sloppy = 1;
1292 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1293 break;
1294 case Opt_userspace:
1295 case Opt_deprecated:
1296 dfprintk(MOUNT, "NFS: ignoring mount option "
1297 "'%s'\n", p);
1298 break;
1300 default:
1301 errors++;
1302 dfprintk(MOUNT, "NFS: unrecognized mount option "
1303 "'%s'\n", p);
1307 if (errors > 0) {
1308 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1309 errors, (errors == 1 ? "" : "s"));
1310 if (!sloppy)
1311 return 0;
1313 return 1;
1315 out_nomem:
1316 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1317 return 0;
1318 out_security_failure:
1319 free_secdata(secdata);
1320 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1321 return 0;
1325 * Use the remote server's MOUNT service to request the NFS file handle
1326 * corresponding to the provided path.
1328 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1329 struct nfs_fh *root_fh)
1331 struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
1332 char *hostname;
1333 int status;
1335 if (args->mount_server.version == 0) {
1336 if (args->flags & NFS_MOUNT_VER3)
1337 args->mount_server.version = NFS_MNT3_VERSION;
1338 else
1339 args->mount_server.version = NFS_MNT_VERSION;
1342 if (args->mount_server.hostname)
1343 hostname = args->mount_server.hostname;
1344 else
1345 hostname = args->nfs_server.hostname;
1348 * Construct the mount server's address.
1350 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1351 memcpy(sap, &args->nfs_server.address,
1352 args->nfs_server.addrlen);
1353 args->mount_server.addrlen = args->nfs_server.addrlen;
1357 * autobind will be used if mount_server.port == 0
1359 nfs_set_port(sap, args->mount_server.port);
1362 * Now ask the mount server to map our export path
1363 * to a file handle.
1365 status = nfs_mount(sap,
1366 args->mount_server.addrlen,
1367 hostname,
1368 args->nfs_server.export_path,
1369 args->mount_server.version,
1370 args->mount_server.protocol,
1371 root_fh);
1372 if (status == 0)
1373 return 0;
1375 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1376 hostname, status);
1377 return status;
1380 static int nfs_parse_simple_hostname(const char *dev_name,
1381 char **hostname, size_t maxnamlen,
1382 char **export_path, size_t maxpathlen)
1384 size_t len;
1385 char *colon, *comma;
1387 colon = strchr(dev_name, ':');
1388 if (colon == NULL)
1389 goto out_bad_devname;
1391 len = colon - dev_name;
1392 if (len > maxnamlen)
1393 goto out_hostname;
1395 /* N.B. caller will free nfs_server.hostname in all cases */
1396 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1397 if (!*hostname)
1398 goto out_nomem;
1400 /* kill possible hostname list: not supported */
1401 comma = strchr(*hostname, ',');
1402 if (comma != NULL) {
1403 if (comma == *hostname)
1404 goto out_bad_devname;
1405 *comma = '\0';
1408 colon++;
1409 len = strlen(colon);
1410 if (len > maxpathlen)
1411 goto out_path;
1412 *export_path = kstrndup(colon, len, GFP_KERNEL);
1413 if (!*export_path)
1414 goto out_nomem;
1416 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1417 return 0;
1419 out_bad_devname:
1420 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1421 return -EINVAL;
1423 out_nomem:
1424 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1425 return -ENOMEM;
1427 out_hostname:
1428 dfprintk(MOUNT, "NFS: server hostname too long\n");
1429 return -ENAMETOOLONG;
1431 out_path:
1432 dfprintk(MOUNT, "NFS: export pathname too long\n");
1433 return -ENAMETOOLONG;
1437 * Hostname has square brackets around it because it contains one or
1438 * more colons. We look for the first closing square bracket, and a
1439 * colon must follow it.
1441 static int nfs_parse_protected_hostname(const char *dev_name,
1442 char **hostname, size_t maxnamlen,
1443 char **export_path, size_t maxpathlen)
1445 size_t len;
1446 char *start, *end;
1448 start = (char *)(dev_name + 1);
1450 end = strchr(start, ']');
1451 if (end == NULL)
1452 goto out_bad_devname;
1453 if (*(end + 1) != ':')
1454 goto out_bad_devname;
1456 len = end - start;
1457 if (len > maxnamlen)
1458 goto out_hostname;
1460 /* N.B. caller will free nfs_server.hostname in all cases */
1461 *hostname = kstrndup(start, len, GFP_KERNEL);
1462 if (*hostname == NULL)
1463 goto out_nomem;
1465 end += 2;
1466 len = strlen(end);
1467 if (len > maxpathlen)
1468 goto out_path;
1469 *export_path = kstrndup(end, len, GFP_KERNEL);
1470 if (!*export_path)
1471 goto out_nomem;
1473 return 0;
1475 out_bad_devname:
1476 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1477 return -EINVAL;
1479 out_nomem:
1480 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1481 return -ENOMEM;
1483 out_hostname:
1484 dfprintk(MOUNT, "NFS: server hostname too long\n");
1485 return -ENAMETOOLONG;
1487 out_path:
1488 dfprintk(MOUNT, "NFS: export pathname too long\n");
1489 return -ENAMETOOLONG;
1493 * Split "dev_name" into "hostname:export_path".
1495 * The leftmost colon demarks the split between the server's hostname
1496 * and the export path. If the hostname starts with a left square
1497 * bracket, then it may contain colons.
1499 * Note: caller frees hostname and export path, even on error.
1501 static int nfs_parse_devname(const char *dev_name,
1502 char **hostname, size_t maxnamlen,
1503 char **export_path, size_t maxpathlen)
1505 if (*dev_name == '[')
1506 return nfs_parse_protected_hostname(dev_name,
1507 hostname, maxnamlen,
1508 export_path, maxpathlen);
1510 return nfs_parse_simple_hostname(dev_name,
1511 hostname, maxnamlen,
1512 export_path, maxpathlen);
1516 * Validate the NFS2/NFS3 mount data
1517 * - fills in the mount root filehandle
1519 * For option strings, user space handles the following behaviors:
1521 * + DNS: mapping server host name to IP address ("addr=" option)
1523 * + failure mode: how to behave if a mount request can't be handled
1524 * immediately ("fg/bg" option)
1526 * + retry: how often to retry a mount request ("retry=" option)
1528 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1529 * mountproto=tcp after mountproto=udp, and so on
1531 static int nfs_validate_mount_data(void *options,
1532 struct nfs_parsed_mount_data *args,
1533 struct nfs_fh *mntfh,
1534 const char *dev_name)
1536 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1538 if (data == NULL)
1539 goto out_no_data;
1541 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1542 args->rsize = NFS_MAX_FILE_IO_SIZE;
1543 args->wsize = NFS_MAX_FILE_IO_SIZE;
1544 args->acregmin = NFS_DEF_ACREGMIN;
1545 args->acregmax = NFS_DEF_ACREGMAX;
1546 args->acdirmin = NFS_DEF_ACDIRMIN;
1547 args->acdirmax = NFS_DEF_ACDIRMAX;
1548 args->mount_server.port = 0; /* autobind unless user sets port */
1549 args->nfs_server.port = 0; /* autobind unless user sets port */
1550 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1551 args->auth_flavors[0] = RPC_AUTH_UNIX;
1553 switch (data->version) {
1554 case 1:
1555 data->namlen = 0;
1556 case 2:
1557 data->bsize = 0;
1558 case 3:
1559 if (data->flags & NFS_MOUNT_VER3)
1560 goto out_no_v3;
1561 data->root.size = NFS2_FHSIZE;
1562 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1563 case 4:
1564 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1565 goto out_no_sec;
1566 case 5:
1567 memset(data->context, 0, sizeof(data->context));
1568 case 6:
1569 if (data->flags & NFS_MOUNT_VER3) {
1570 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1571 goto out_invalid_fh;
1572 mntfh->size = data->root.size;
1573 } else
1574 mntfh->size = NFS2_FHSIZE;
1577 memcpy(mntfh->data, data->root.data, mntfh->size);
1578 if (mntfh->size < sizeof(mntfh->data))
1579 memset(mntfh->data + mntfh->size, 0,
1580 sizeof(mntfh->data) - mntfh->size);
1583 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1584 * can deal with.
1586 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
1587 args->rsize = data->rsize;
1588 args->wsize = data->wsize;
1589 args->timeo = data->timeo;
1590 args->retrans = data->retrans;
1591 args->acregmin = data->acregmin;
1592 args->acregmax = data->acregmax;
1593 args->acdirmin = data->acdirmin;
1594 args->acdirmax = data->acdirmax;
1596 memcpy(&args->nfs_server.address, &data->addr,
1597 sizeof(data->addr));
1598 args->nfs_server.addrlen = sizeof(data->addr);
1599 if (!nfs_verify_server_address((struct sockaddr *)
1600 &args->nfs_server.address))
1601 goto out_no_address;
1603 if (!(data->flags & NFS_MOUNT_TCP))
1604 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1605 /* N.B. caller will free nfs_server.hostname in all cases */
1606 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1607 args->namlen = data->namlen;
1608 args->bsize = data->bsize;
1610 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1611 args->auth_flavors[0] = data->pseudoflavor;
1612 if (!args->nfs_server.hostname)
1613 goto out_nomem;
1616 * The legacy version 6 binary mount data from userspace has a
1617 * field used only to transport selinux information into the
1618 * the kernel. To continue to support that functionality we
1619 * have a touch of selinux knowledge here in the NFS code. The
1620 * userspace code converted context=blah to just blah so we are
1621 * converting back to the full string selinux understands.
1623 if (data->context[0]){
1624 #ifdef CONFIG_SECURITY_SELINUX
1625 int rc;
1626 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1627 if (!opts_str)
1628 return -ENOMEM;
1629 strcpy(opts_str, "context=");
1630 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1631 strcat(opts_str, &data->context[0]);
1632 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1633 kfree(opts_str);
1634 if (rc)
1635 return rc;
1636 #else
1637 return -EINVAL;
1638 #endif
1641 break;
1642 default: {
1643 int status;
1645 if (nfs_parse_mount_options((char *)options, args) == 0)
1646 return -EINVAL;
1648 if (!nfs_verify_server_address((struct sockaddr *)
1649 &args->nfs_server.address))
1650 goto out_no_address;
1652 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1653 args->nfs_server.port);
1655 nfs_set_mount_transport_protocol(args);
1657 status = nfs_parse_devname(dev_name,
1658 &args->nfs_server.hostname,
1659 PAGE_SIZE,
1660 &args->nfs_server.export_path,
1661 NFS_MAXPATHLEN);
1662 if (!status)
1663 status = nfs_try_mount(args, mntfh);
1665 kfree(args->nfs_server.export_path);
1666 args->nfs_server.export_path = NULL;
1668 if (status)
1669 return status;
1671 break;
1675 #ifndef CONFIG_NFS_V3
1676 if (args->flags & NFS_MOUNT_VER3)
1677 goto out_v3_not_compiled;
1678 #endif /* !CONFIG_NFS_V3 */
1680 return 0;
1682 out_no_data:
1683 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1684 return -EINVAL;
1686 out_no_v3:
1687 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1688 data->version);
1689 return -EINVAL;
1691 out_no_sec:
1692 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1693 return -EINVAL;
1695 #ifndef CONFIG_NFS_V3
1696 out_v3_not_compiled:
1697 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1698 return -EPROTONOSUPPORT;
1699 #endif /* !CONFIG_NFS_V3 */
1701 out_nomem:
1702 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1703 return -ENOMEM;
1705 out_no_address:
1706 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1707 return -EINVAL;
1709 out_invalid_fh:
1710 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1711 return -EINVAL;
1714 static int
1715 nfs_compare_remount_data(struct nfs_server *nfss,
1716 struct nfs_parsed_mount_data *data)
1718 if (data->flags != nfss->flags ||
1719 data->rsize != nfss->rsize ||
1720 data->wsize != nfss->wsize ||
1721 data->retrans != nfss->client->cl_timeout->to_retries ||
1722 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1723 data->acregmin != nfss->acregmin / HZ ||
1724 data->acregmax != nfss->acregmax / HZ ||
1725 data->acdirmin != nfss->acdirmin / HZ ||
1726 data->acdirmax != nfss->acdirmax / HZ ||
1727 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1728 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1729 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1730 data->nfs_server.addrlen) != 0)
1731 return -EINVAL;
1733 return 0;
1736 static int
1737 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1739 int error;
1740 struct nfs_server *nfss = sb->s_fs_info;
1741 struct nfs_parsed_mount_data *data;
1742 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1743 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
1744 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
1747 * Userspace mount programs that send binary options generally send
1748 * them populated with default values. We have no way to know which
1749 * ones were explicitly specified. Fall back to legacy behavior and
1750 * just return success.
1752 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1753 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1754 options->version <= 6))))
1755 return 0;
1757 data = kzalloc(sizeof(*data), GFP_KERNEL);
1758 if (data == NULL)
1759 return -ENOMEM;
1761 /* fill out struct with values from existing mount */
1762 data->flags = nfss->flags;
1763 data->rsize = nfss->rsize;
1764 data->wsize = nfss->wsize;
1765 data->retrans = nfss->client->cl_timeout->to_retries;
1766 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1767 data->acregmin = nfss->acregmin / HZ;
1768 data->acregmax = nfss->acregmax / HZ;
1769 data->acdirmin = nfss->acdirmin / HZ;
1770 data->acdirmax = nfss->acdirmax / HZ;
1771 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1772 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1773 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1774 data->nfs_server.addrlen);
1776 /* overwrite those values with any that were specified */
1777 error = nfs_parse_mount_options((char *)options, data);
1778 if (error < 0)
1779 goto out;
1781 /* compare new mount options with old ones */
1782 error = nfs_compare_remount_data(nfss, data);
1783 out:
1784 kfree(data);
1785 return error;
1789 * Initialise the common bits of the superblock
1791 static inline void nfs_initialise_sb(struct super_block *sb)
1793 struct nfs_server *server = NFS_SB(sb);
1795 sb->s_magic = NFS_SUPER_MAGIC;
1797 /* We probably want something more informative here */
1798 snprintf(sb->s_id, sizeof(sb->s_id),
1799 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1801 if (sb->s_blocksize == 0)
1802 sb->s_blocksize = nfs_block_bits(server->wsize,
1803 &sb->s_blocksize_bits);
1805 if (server->flags & NFS_MOUNT_NOAC)
1806 sb->s_flags |= MS_SYNCHRONOUS;
1808 nfs_super_set_maxbytes(sb, server->maxfilesize);
1812 * Finish setting up an NFS2/3 superblock
1814 static void nfs_fill_super(struct super_block *sb,
1815 struct nfs_parsed_mount_data *data)
1817 struct nfs_server *server = NFS_SB(sb);
1819 sb->s_blocksize_bits = 0;
1820 sb->s_blocksize = 0;
1821 if (data->bsize)
1822 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1824 if (server->flags & NFS_MOUNT_VER3) {
1825 /* The VFS shouldn't apply the umask to mode bits. We will do
1826 * so ourselves when necessary.
1828 sb->s_flags |= MS_POSIXACL;
1829 sb->s_time_gran = 1;
1832 sb->s_op = &nfs_sops;
1833 nfs_initialise_sb(sb);
1837 * Finish setting up a cloned NFS2/3 superblock
1839 static void nfs_clone_super(struct super_block *sb,
1840 const struct super_block *old_sb)
1842 struct nfs_server *server = NFS_SB(sb);
1844 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1845 sb->s_blocksize = old_sb->s_blocksize;
1846 sb->s_maxbytes = old_sb->s_maxbytes;
1848 if (server->flags & NFS_MOUNT_VER3) {
1849 /* The VFS shouldn't apply the umask to mode bits. We will do
1850 * so ourselves when necessary.
1852 sb->s_flags |= MS_POSIXACL;
1853 sb->s_time_gran = 1;
1856 sb->s_op = old_sb->s_op;
1857 nfs_initialise_sb(sb);
1860 #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1862 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1864 const struct nfs_server *a = s->s_fs_info;
1865 const struct rpc_clnt *clnt_a = a->client;
1866 const struct rpc_clnt *clnt_b = b->client;
1868 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1869 goto Ebusy;
1870 if (a->nfs_client != b->nfs_client)
1871 goto Ebusy;
1872 if (a->flags != b->flags)
1873 goto Ebusy;
1874 if (a->wsize != b->wsize)
1875 goto Ebusy;
1876 if (a->rsize != b->rsize)
1877 goto Ebusy;
1878 if (a->acregmin != b->acregmin)
1879 goto Ebusy;
1880 if (a->acregmax != b->acregmax)
1881 goto Ebusy;
1882 if (a->acdirmin != b->acdirmin)
1883 goto Ebusy;
1884 if (a->acdirmax != b->acdirmax)
1885 goto Ebusy;
1886 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1887 goto Ebusy;
1888 return 1;
1889 Ebusy:
1890 return 0;
1893 struct nfs_sb_mountdata {
1894 struct nfs_server *server;
1895 int mntflags;
1898 static int nfs_set_super(struct super_block *s, void *data)
1900 struct nfs_sb_mountdata *sb_mntdata = data;
1901 struct nfs_server *server = sb_mntdata->server;
1902 int ret;
1904 s->s_flags = sb_mntdata->mntflags;
1905 s->s_fs_info = server;
1906 ret = set_anon_super(s, server);
1907 if (ret == 0)
1908 server->s_dev = s->s_dev;
1909 return ret;
1912 static int nfs_compare_super_address(struct nfs_server *server1,
1913 struct nfs_server *server2)
1915 struct sockaddr *sap1, *sap2;
1917 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1918 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1920 if (sap1->sa_family != sap2->sa_family)
1921 return 0;
1923 switch (sap1->sa_family) {
1924 case AF_INET: {
1925 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1926 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1927 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1928 return 0;
1929 if (sin1->sin_port != sin2->sin_port)
1930 return 0;
1931 break;
1933 case AF_INET6: {
1934 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1935 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1936 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1937 return 0;
1938 if (sin1->sin6_port != sin2->sin6_port)
1939 return 0;
1940 break;
1942 default:
1943 return 0;
1946 return 1;
1949 static int nfs_compare_super(struct super_block *sb, void *data)
1951 struct nfs_sb_mountdata *sb_mntdata = data;
1952 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1953 int mntflags = sb_mntdata->mntflags;
1955 if (!nfs_compare_super_address(old, server))
1956 return 0;
1957 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1958 if (old->flags & NFS_MOUNT_UNSHARED)
1959 return 0;
1960 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1961 return 0;
1962 return nfs_compare_mount_options(sb, server, mntflags);
1965 static int nfs_bdi_register(struct nfs_server *server)
1967 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1970 static int nfs_get_sb(struct file_system_type *fs_type,
1971 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1973 struct nfs_server *server = NULL;
1974 struct super_block *s;
1975 struct nfs_parsed_mount_data *data;
1976 struct nfs_fh *mntfh;
1977 struct dentry *mntroot;
1978 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1979 struct nfs_sb_mountdata sb_mntdata = {
1980 .mntflags = flags,
1982 int error = -ENOMEM;
1984 data = kzalloc(sizeof(*data), GFP_KERNEL);
1985 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1986 if (data == NULL || mntfh == NULL)
1987 goto out_free_fh;
1989 security_init_mnt_opts(&data->lsm_opts);
1991 /* Validate the mount data */
1992 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
1993 if (error < 0)
1994 goto out;
1996 /* Get a volume representation */
1997 server = nfs_create_server(data, mntfh);
1998 if (IS_ERR(server)) {
1999 error = PTR_ERR(server);
2000 goto out;
2002 sb_mntdata.server = server;
2004 if (server->flags & NFS_MOUNT_UNSHARED)
2005 compare_super = NULL;
2007 /* Get a superblock - note that we may end up sharing one that already exists */
2008 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2009 if (IS_ERR(s)) {
2010 error = PTR_ERR(s);
2011 goto out_err_nosb;
2014 if (s->s_fs_info != server) {
2015 nfs_free_server(server);
2016 server = NULL;
2017 } else {
2018 error = nfs_bdi_register(server);
2019 if (error)
2020 goto error_splat_super;
2023 if (!s->s_root) {
2024 /* initial superblock/root creation */
2025 nfs_fill_super(s, data);
2028 mntroot = nfs_get_root(s, mntfh);
2029 if (IS_ERR(mntroot)) {
2030 error = PTR_ERR(mntroot);
2031 goto error_splat_super;
2034 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2035 if (error)
2036 goto error_splat_root;
2038 s->s_flags |= MS_ACTIVE;
2039 mnt->mnt_sb = s;
2040 mnt->mnt_root = mntroot;
2041 error = 0;
2043 out:
2044 kfree(data->nfs_server.hostname);
2045 kfree(data->mount_server.hostname);
2046 security_free_mnt_opts(&data->lsm_opts);
2047 out_free_fh:
2048 kfree(mntfh);
2049 kfree(data);
2050 return error;
2052 out_err_nosb:
2053 nfs_free_server(server);
2054 goto out;
2056 error_splat_root:
2057 dput(mntroot);
2058 error_splat_super:
2059 up_write(&s->s_umount);
2060 deactivate_super(s);
2061 goto out;
2065 * Destroy an NFS2/3 superblock
2067 static void nfs_kill_super(struct super_block *s)
2069 struct nfs_server *server = NFS_SB(s);
2071 bdi_unregister(&server->backing_dev_info);
2072 kill_anon_super(s);
2073 nfs_free_server(server);
2077 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2079 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2080 const char *dev_name, void *raw_data,
2081 struct vfsmount *mnt)
2083 struct nfs_clone_mount *data = raw_data;
2084 struct super_block *s;
2085 struct nfs_server *server;
2086 struct dentry *mntroot;
2087 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2088 struct nfs_sb_mountdata sb_mntdata = {
2089 .mntflags = flags,
2091 int error;
2093 dprintk("--> nfs_xdev_get_sb()\n");
2095 /* create a new volume representation */
2096 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2097 if (IS_ERR(server)) {
2098 error = PTR_ERR(server);
2099 goto out_err_noserver;
2101 sb_mntdata.server = server;
2103 if (server->flags & NFS_MOUNT_UNSHARED)
2104 compare_super = NULL;
2106 /* Get a superblock - note that we may end up sharing one that already exists */
2107 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2108 if (IS_ERR(s)) {
2109 error = PTR_ERR(s);
2110 goto out_err_nosb;
2113 if (s->s_fs_info != server) {
2114 nfs_free_server(server);
2115 server = NULL;
2116 } else {
2117 error = nfs_bdi_register(server);
2118 if (error)
2119 goto error_splat_super;
2122 if (!s->s_root) {
2123 /* initial superblock/root creation */
2124 nfs_clone_super(s, data->sb);
2127 mntroot = nfs_get_root(s, data->fh);
2128 if (IS_ERR(mntroot)) {
2129 error = PTR_ERR(mntroot);
2130 goto error_splat_super;
2132 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2133 dput(mntroot);
2134 error = -ESTALE;
2135 goto error_splat_super;
2138 s->s_flags |= MS_ACTIVE;
2139 mnt->mnt_sb = s;
2140 mnt->mnt_root = mntroot;
2142 /* clone any lsm security options from the parent to the new sb */
2143 security_sb_clone_mnt_opts(data->sb, s);
2145 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2146 return 0;
2148 out_err_nosb:
2149 nfs_free_server(server);
2150 out_err_noserver:
2151 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2152 return error;
2154 error_splat_super:
2155 up_write(&s->s_umount);
2156 deactivate_super(s);
2157 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2158 return error;
2161 #ifdef CONFIG_NFS_V4
2164 * Finish setting up a cloned NFS4 superblock
2166 static void nfs4_clone_super(struct super_block *sb,
2167 const struct super_block *old_sb)
2169 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2170 sb->s_blocksize = old_sb->s_blocksize;
2171 sb->s_maxbytes = old_sb->s_maxbytes;
2172 sb->s_time_gran = 1;
2173 sb->s_op = old_sb->s_op;
2174 nfs_initialise_sb(sb);
2178 * Set up an NFS4 superblock
2180 static void nfs4_fill_super(struct super_block *sb)
2182 sb->s_time_gran = 1;
2183 sb->s_op = &nfs4_sops;
2184 nfs_initialise_sb(sb);
2188 * Validate NFSv4 mount options
2190 static int nfs4_validate_mount_data(void *options,
2191 struct nfs_parsed_mount_data *args,
2192 const char *dev_name)
2194 struct sockaddr_in *ap;
2195 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2196 char *c;
2198 if (data == NULL)
2199 goto out_no_data;
2201 args->rsize = NFS_MAX_FILE_IO_SIZE;
2202 args->wsize = NFS_MAX_FILE_IO_SIZE;
2203 args->acregmin = NFS_DEF_ACREGMIN;
2204 args->acregmax = NFS_DEF_ACREGMAX;
2205 args->acdirmin = NFS_DEF_ACDIRMIN;
2206 args->acdirmax = NFS_DEF_ACDIRMAX;
2207 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
2208 args->auth_flavors[0] = RPC_AUTH_UNIX;
2209 args->auth_flavor_len = 0;
2211 switch (data->version) {
2212 case 1:
2213 ap = (struct sockaddr_in *)&args->nfs_server.address;
2214 if (data->host_addrlen > sizeof(args->nfs_server.address))
2215 goto out_no_address;
2216 if (data->host_addrlen == 0)
2217 goto out_no_address;
2218 args->nfs_server.addrlen = data->host_addrlen;
2219 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
2220 return -EFAULT;
2221 if (!nfs_verify_server_address((struct sockaddr *)
2222 &args->nfs_server.address))
2223 goto out_no_address;
2225 if (data->auth_flavourlen) {
2226 if (data->auth_flavourlen > 1)
2227 goto out_inval_auth;
2228 if (copy_from_user(&args->auth_flavors[0],
2229 data->auth_flavours,
2230 sizeof(args->auth_flavors[0])))
2231 return -EFAULT;
2234 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2235 if (IS_ERR(c))
2236 return PTR_ERR(c);
2237 args->nfs_server.hostname = c;
2239 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2240 if (IS_ERR(c))
2241 return PTR_ERR(c);
2242 args->nfs_server.export_path = c;
2243 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2245 c = strndup_user(data->client_addr.data, 16);
2246 if (IS_ERR(c))
2247 return PTR_ERR(c);
2248 args->client_address = c;
2251 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2252 * can deal with.
2255 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2256 args->rsize = data->rsize;
2257 args->wsize = data->wsize;
2258 args->timeo = data->timeo;
2259 args->retrans = data->retrans;
2260 args->acregmin = data->acregmin;
2261 args->acregmax = data->acregmax;
2262 args->acdirmin = data->acdirmin;
2263 args->acdirmax = data->acdirmax;
2264 args->nfs_server.protocol = data->proto;
2265 nfs_validate_transport_protocol(args);
2267 break;
2268 default: {
2269 int status;
2271 if (nfs_parse_mount_options((char *)options, args) == 0)
2272 return -EINVAL;
2274 if (!nfs_verify_server_address((struct sockaddr *)
2275 &args->nfs_server.address))
2276 return -EINVAL;
2278 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2279 args->nfs_server.port);
2281 nfs_validate_transport_protocol(args);
2283 if (args->auth_flavor_len > 1)
2284 goto out_inval_auth;
2286 if (args->client_address == NULL)
2287 goto out_no_client_address;
2289 status = nfs_parse_devname(dev_name,
2290 &args->nfs_server.hostname,
2291 NFS4_MAXNAMLEN,
2292 &args->nfs_server.export_path,
2293 NFS4_MAXPATHLEN);
2294 if (status < 0)
2295 return status;
2297 break;
2301 return 0;
2303 out_no_data:
2304 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2305 return -EINVAL;
2307 out_inval_auth:
2308 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2309 data->auth_flavourlen);
2310 return -EINVAL;
2312 out_no_address:
2313 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2314 return -EINVAL;
2316 out_no_client_address:
2317 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2318 return -EINVAL;
2322 * Get the superblock for an NFS4 mountpoint
2324 static int nfs4_get_sb(struct file_system_type *fs_type,
2325 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2327 struct nfs_parsed_mount_data *data;
2328 struct super_block *s;
2329 struct nfs_server *server;
2330 struct nfs_fh *mntfh;
2331 struct dentry *mntroot;
2332 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2333 struct nfs_sb_mountdata sb_mntdata = {
2334 .mntflags = flags,
2336 int error = -ENOMEM;
2338 data = kzalloc(sizeof(*data), GFP_KERNEL);
2339 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2340 if (data == NULL || mntfh == NULL)
2341 goto out_free_fh;
2343 security_init_mnt_opts(&data->lsm_opts);
2345 /* Validate the mount data */
2346 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2347 if (error < 0)
2348 goto out;
2350 /* Get a volume representation */
2351 server = nfs4_create_server(data, mntfh);
2352 if (IS_ERR(server)) {
2353 error = PTR_ERR(server);
2354 goto out;
2356 sb_mntdata.server = server;
2358 if (server->flags & NFS4_MOUNT_UNSHARED)
2359 compare_super = NULL;
2361 /* Get a superblock - note that we may end up sharing one that already exists */
2362 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2363 if (IS_ERR(s)) {
2364 error = PTR_ERR(s);
2365 goto out_free;
2368 if (s->s_fs_info != server) {
2369 nfs_free_server(server);
2370 server = NULL;
2371 } else {
2372 error = nfs_bdi_register(server);
2373 if (error)
2374 goto error_splat_super;
2377 if (!s->s_root) {
2378 /* initial superblock/root creation */
2379 nfs4_fill_super(s);
2382 mntroot = nfs4_get_root(s, mntfh);
2383 if (IS_ERR(mntroot)) {
2384 error = PTR_ERR(mntroot);
2385 goto error_splat_super;
2388 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2389 if (error)
2390 goto error_splat_root;
2392 s->s_flags |= MS_ACTIVE;
2393 mnt->mnt_sb = s;
2394 mnt->mnt_root = mntroot;
2395 error = 0;
2397 out:
2398 kfree(data->client_address);
2399 kfree(data->nfs_server.export_path);
2400 kfree(data->nfs_server.hostname);
2401 security_free_mnt_opts(&data->lsm_opts);
2402 out_free_fh:
2403 kfree(mntfh);
2404 kfree(data);
2405 return error;
2407 out_free:
2408 nfs_free_server(server);
2409 goto out;
2411 error_splat_root:
2412 dput(mntroot);
2413 error_splat_super:
2414 up_write(&s->s_umount);
2415 deactivate_super(s);
2416 goto out;
2419 static void nfs4_kill_super(struct super_block *sb)
2421 struct nfs_server *server = NFS_SB(sb);
2423 nfs_return_all_delegations(sb);
2424 kill_anon_super(sb);
2426 nfs4_renewd_prepare_shutdown(server);
2427 nfs_free_server(server);
2431 * Clone an NFS4 server record on xdev traversal (FSID-change)
2433 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2434 const char *dev_name, void *raw_data,
2435 struct vfsmount *mnt)
2437 struct nfs_clone_mount *data = raw_data;
2438 struct super_block *s;
2439 struct nfs_server *server;
2440 struct dentry *mntroot;
2441 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2442 struct nfs_sb_mountdata sb_mntdata = {
2443 .mntflags = flags,
2445 int error;
2447 dprintk("--> nfs4_xdev_get_sb()\n");
2449 /* create a new volume representation */
2450 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2451 if (IS_ERR(server)) {
2452 error = PTR_ERR(server);
2453 goto out_err_noserver;
2455 sb_mntdata.server = server;
2457 if (server->flags & NFS4_MOUNT_UNSHARED)
2458 compare_super = NULL;
2460 /* Get a superblock - note that we may end up sharing one that already exists */
2461 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2462 if (IS_ERR(s)) {
2463 error = PTR_ERR(s);
2464 goto out_err_nosb;
2467 if (s->s_fs_info != server) {
2468 nfs_free_server(server);
2469 server = NULL;
2470 } else {
2471 error = nfs_bdi_register(server);
2472 if (error)
2473 goto error_splat_super;
2476 if (!s->s_root) {
2477 /* initial superblock/root creation */
2478 nfs4_clone_super(s, data->sb);
2481 mntroot = nfs4_get_root(s, data->fh);
2482 if (IS_ERR(mntroot)) {
2483 error = PTR_ERR(mntroot);
2484 goto error_splat_super;
2486 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2487 dput(mntroot);
2488 error = -ESTALE;
2489 goto error_splat_super;
2492 s->s_flags |= MS_ACTIVE;
2493 mnt->mnt_sb = s;
2494 mnt->mnt_root = mntroot;
2496 security_sb_clone_mnt_opts(data->sb, s);
2498 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2499 return 0;
2501 out_err_nosb:
2502 nfs_free_server(server);
2503 out_err_noserver:
2504 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2505 return error;
2507 error_splat_super:
2508 up_write(&s->s_umount);
2509 deactivate_super(s);
2510 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2511 return error;
2515 * Create an NFS4 server record on referral traversal
2517 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2518 const char *dev_name, void *raw_data,
2519 struct vfsmount *mnt)
2521 struct nfs_clone_mount *data = raw_data;
2522 struct super_block *s;
2523 struct nfs_server *server;
2524 struct dentry *mntroot;
2525 struct nfs_fh mntfh;
2526 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2527 struct nfs_sb_mountdata sb_mntdata = {
2528 .mntflags = flags,
2530 int error;
2532 dprintk("--> nfs4_referral_get_sb()\n");
2534 /* create a new volume representation */
2535 server = nfs4_create_referral_server(data, &mntfh);
2536 if (IS_ERR(server)) {
2537 error = PTR_ERR(server);
2538 goto out_err_noserver;
2540 sb_mntdata.server = server;
2542 if (server->flags & NFS4_MOUNT_UNSHARED)
2543 compare_super = NULL;
2545 /* Get a superblock - note that we may end up sharing one that already exists */
2546 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2547 if (IS_ERR(s)) {
2548 error = PTR_ERR(s);
2549 goto out_err_nosb;
2552 if (s->s_fs_info != server) {
2553 nfs_free_server(server);
2554 server = NULL;
2555 } else {
2556 error = nfs_bdi_register(server);
2557 if (error)
2558 goto error_splat_super;
2561 if (!s->s_root) {
2562 /* initial superblock/root creation */
2563 nfs4_fill_super(s);
2566 mntroot = nfs4_get_root(s, &mntfh);
2567 if (IS_ERR(mntroot)) {
2568 error = PTR_ERR(mntroot);
2569 goto error_splat_super;
2571 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2572 dput(mntroot);
2573 error = -ESTALE;
2574 goto error_splat_super;
2577 s->s_flags |= MS_ACTIVE;
2578 mnt->mnt_sb = s;
2579 mnt->mnt_root = mntroot;
2581 security_sb_clone_mnt_opts(data->sb, s);
2583 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2584 return 0;
2586 out_err_nosb:
2587 nfs_free_server(server);
2588 out_err_noserver:
2589 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2590 return error;
2592 error_splat_super:
2593 up_write(&s->s_umount);
2594 deactivate_super(s);
2595 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2596 return error;
2599 #endif /* CONFIG_NFS_V4 */