NFS: Define and create superblock-level objects
[linux-2.6/verdex.git] / fs / nfs / super.c
blob87f65ae07f323ab8998fa103ec0e1e165a242777
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"
63 #include "fscache.h"
65 #define NFSDBG_FACILITY NFSDBG_VFS
67 enum {
68 /* Mount options that take no arguments */
69 Opt_soft, Opt_hard,
70 Opt_posix, Opt_noposix,
71 Opt_cto, Opt_nocto,
72 Opt_ac, Opt_noac,
73 Opt_lock, Opt_nolock,
74 Opt_v2, Opt_v3,
75 Opt_udp, Opt_tcp, Opt_rdma,
76 Opt_acl, Opt_noacl,
77 Opt_rdirplus, Opt_nordirplus,
78 Opt_sharecache, Opt_nosharecache,
79 Opt_resvport, Opt_noresvport,
81 /* Mount options that take integer arguments */
82 Opt_port,
83 Opt_rsize, Opt_wsize, Opt_bsize,
84 Opt_timeo, Opt_retrans,
85 Opt_acregmin, Opt_acregmax,
86 Opt_acdirmin, Opt_acdirmax,
87 Opt_actimeo,
88 Opt_namelen,
89 Opt_mountport,
90 Opt_mountvers,
91 Opt_nfsvers,
93 /* Mount options that take string arguments */
94 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
95 Opt_addr, Opt_mountaddr, Opt_clientaddr,
96 Opt_lookupcache,
98 /* Special mount options */
99 Opt_userspace, Opt_deprecated, Opt_sloppy,
101 Opt_err
104 static const match_table_t nfs_mount_option_tokens = {
105 { Opt_userspace, "bg" },
106 { Opt_userspace, "fg" },
107 { Opt_userspace, "retry=%s" },
109 { Opt_sloppy, "sloppy" },
111 { Opt_soft, "soft" },
112 { Opt_hard, "hard" },
113 { Opt_deprecated, "intr" },
114 { Opt_deprecated, "nointr" },
115 { Opt_posix, "posix" },
116 { Opt_noposix, "noposix" },
117 { Opt_cto, "cto" },
118 { Opt_nocto, "nocto" },
119 { Opt_ac, "ac" },
120 { Opt_noac, "noac" },
121 { Opt_lock, "lock" },
122 { Opt_nolock, "nolock" },
123 { Opt_v2, "v2" },
124 { Opt_v3, "v3" },
125 { Opt_udp, "udp" },
126 { Opt_tcp, "tcp" },
127 { Opt_rdma, "rdma" },
128 { Opt_acl, "acl" },
129 { Opt_noacl, "noacl" },
130 { Opt_rdirplus, "rdirplus" },
131 { Opt_nordirplus, "nordirplus" },
132 { Opt_sharecache, "sharecache" },
133 { Opt_nosharecache, "nosharecache" },
134 { Opt_resvport, "resvport" },
135 { Opt_noresvport, "noresvport" },
137 { Opt_port, "port=%u" },
138 { Opt_rsize, "rsize=%u" },
139 { Opt_wsize, "wsize=%u" },
140 { Opt_bsize, "bsize=%u" },
141 { Opt_timeo, "timeo=%u" },
142 { Opt_retrans, "retrans=%u" },
143 { Opt_acregmin, "acregmin=%u" },
144 { Opt_acregmax, "acregmax=%u" },
145 { Opt_acdirmin, "acdirmin=%u" },
146 { Opt_acdirmax, "acdirmax=%u" },
147 { Opt_actimeo, "actimeo=%u" },
148 { Opt_namelen, "namlen=%u" },
149 { Opt_mountport, "mountport=%u" },
150 { Opt_mountvers, "mountvers=%u" },
151 { Opt_nfsvers, "nfsvers=%u" },
152 { Opt_nfsvers, "vers=%u" },
154 { Opt_sec, "sec=%s" },
155 { Opt_proto, "proto=%s" },
156 { Opt_mountproto, "mountproto=%s" },
157 { Opt_addr, "addr=%s" },
158 { Opt_clientaddr, "clientaddr=%s" },
159 { Opt_mounthost, "mounthost=%s" },
160 { Opt_mountaddr, "mountaddr=%s" },
162 { Opt_lookupcache, "lookupcache=%s" },
164 { Opt_err, NULL }
167 enum {
168 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
170 Opt_xprt_err
173 static const match_table_t nfs_xprt_protocol_tokens = {
174 { Opt_xprt_udp, "udp" },
175 { Opt_xprt_tcp, "tcp" },
176 { Opt_xprt_rdma, "rdma" },
178 { Opt_xprt_err, NULL }
181 enum {
182 Opt_sec_none, Opt_sec_sys,
183 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
184 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
185 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
187 Opt_sec_err
190 static const match_table_t nfs_secflavor_tokens = {
191 { Opt_sec_none, "none" },
192 { Opt_sec_none, "null" },
193 { Opt_sec_sys, "sys" },
195 { Opt_sec_krb5, "krb5" },
196 { Opt_sec_krb5i, "krb5i" },
197 { Opt_sec_krb5p, "krb5p" },
199 { Opt_sec_lkey, "lkey" },
200 { Opt_sec_lkeyi, "lkeyi" },
201 { Opt_sec_lkeyp, "lkeyp" },
203 { Opt_sec_spkm, "spkm3" },
204 { Opt_sec_spkmi, "spkm3i" },
205 { Opt_sec_spkmp, "spkm3p" },
207 { Opt_sec_err, NULL }
210 enum {
211 Opt_lookupcache_all, Opt_lookupcache_positive,
212 Opt_lookupcache_none,
214 Opt_lookupcache_err
217 static match_table_t nfs_lookupcache_tokens = {
218 { Opt_lookupcache_all, "all" },
219 { Opt_lookupcache_positive, "pos" },
220 { Opt_lookupcache_positive, "positive" },
221 { Opt_lookupcache_none, "none" },
223 { Opt_lookupcache_err, NULL }
227 static void nfs_umount_begin(struct super_block *);
228 static int nfs_statfs(struct dentry *, struct kstatfs *);
229 static int nfs_show_options(struct seq_file *, struct vfsmount *);
230 static int nfs_show_stats(struct seq_file *, struct vfsmount *);
231 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
232 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
233 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
234 static void nfs_kill_super(struct super_block *);
235 static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
237 static struct file_system_type nfs_fs_type = {
238 .owner = THIS_MODULE,
239 .name = "nfs",
240 .get_sb = nfs_get_sb,
241 .kill_sb = nfs_kill_super,
242 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
245 struct file_system_type nfs_xdev_fs_type = {
246 .owner = THIS_MODULE,
247 .name = "nfs",
248 .get_sb = nfs_xdev_get_sb,
249 .kill_sb = nfs_kill_super,
250 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
253 static const struct super_operations nfs_sops = {
254 .alloc_inode = nfs_alloc_inode,
255 .destroy_inode = nfs_destroy_inode,
256 .write_inode = nfs_write_inode,
257 .statfs = nfs_statfs,
258 .clear_inode = nfs_clear_inode,
259 .umount_begin = nfs_umount_begin,
260 .show_options = nfs_show_options,
261 .show_stats = nfs_show_stats,
262 .remount_fs = nfs_remount,
265 #ifdef CONFIG_NFS_V4
266 static int nfs4_get_sb(struct file_system_type *fs_type,
267 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
268 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
269 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
270 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
271 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
272 static void nfs4_kill_super(struct super_block *sb);
274 static struct file_system_type nfs4_fs_type = {
275 .owner = THIS_MODULE,
276 .name = "nfs4",
277 .get_sb = nfs4_get_sb,
278 .kill_sb = nfs4_kill_super,
279 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
282 struct file_system_type nfs4_xdev_fs_type = {
283 .owner = THIS_MODULE,
284 .name = "nfs4",
285 .get_sb = nfs4_xdev_get_sb,
286 .kill_sb = nfs4_kill_super,
287 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
290 struct file_system_type nfs4_referral_fs_type = {
291 .owner = THIS_MODULE,
292 .name = "nfs4",
293 .get_sb = nfs4_referral_get_sb,
294 .kill_sb = nfs4_kill_super,
295 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
298 static const struct super_operations nfs4_sops = {
299 .alloc_inode = nfs_alloc_inode,
300 .destroy_inode = nfs_destroy_inode,
301 .write_inode = nfs_write_inode,
302 .statfs = nfs_statfs,
303 .clear_inode = nfs4_clear_inode,
304 .umount_begin = nfs_umount_begin,
305 .show_options = nfs_show_options,
306 .show_stats = nfs_show_stats,
307 .remount_fs = nfs_remount,
309 #endif
311 static struct shrinker acl_shrinker = {
312 .shrink = nfs_access_cache_shrinker,
313 .seeks = DEFAULT_SEEKS,
317 * Register the NFS filesystems
319 int __init register_nfs_fs(void)
321 int ret;
323 ret = register_filesystem(&nfs_fs_type);
324 if (ret < 0)
325 goto error_0;
327 ret = nfs_register_sysctl();
328 if (ret < 0)
329 goto error_1;
330 #ifdef CONFIG_NFS_V4
331 ret = register_filesystem(&nfs4_fs_type);
332 if (ret < 0)
333 goto error_2;
334 #endif
335 register_shrinker(&acl_shrinker);
336 return 0;
338 #ifdef CONFIG_NFS_V4
339 error_2:
340 nfs_unregister_sysctl();
341 #endif
342 error_1:
343 unregister_filesystem(&nfs_fs_type);
344 error_0:
345 return ret;
349 * Unregister the NFS filesystems
351 void __exit unregister_nfs_fs(void)
353 unregister_shrinker(&acl_shrinker);
354 #ifdef CONFIG_NFS_V4
355 unregister_filesystem(&nfs4_fs_type);
356 #endif
357 nfs_unregister_sysctl();
358 unregister_filesystem(&nfs_fs_type);
361 void nfs_sb_active(struct super_block *sb)
363 struct nfs_server *server = NFS_SB(sb);
365 if (atomic_inc_return(&server->active) == 1)
366 atomic_inc(&sb->s_active);
369 void nfs_sb_deactive(struct super_block *sb)
371 struct nfs_server *server = NFS_SB(sb);
373 if (atomic_dec_and_test(&server->active))
374 deactivate_super(sb);
378 * Deliver file system statistics to userspace
380 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
382 struct nfs_server *server = NFS_SB(dentry->d_sb);
383 unsigned char blockbits;
384 unsigned long blockres;
385 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
386 struct nfs_fattr fattr;
387 struct nfs_fsstat res = {
388 .fattr = &fattr,
390 int error;
392 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
393 if (error < 0)
394 goto out_err;
395 buf->f_type = NFS_SUPER_MAGIC;
398 * Current versions of glibc do not correctly handle the
399 * case where f_frsize != f_bsize. Eventually we want to
400 * report the value of wtmult in this field.
402 buf->f_frsize = dentry->d_sb->s_blocksize;
405 * On most *nix systems, f_blocks, f_bfree, and f_bavail
406 * are reported in units of f_frsize. Linux hasn't had
407 * an f_frsize field in its statfs struct until recently,
408 * thus historically Linux's sys_statfs reports these
409 * fields in units of f_bsize.
411 buf->f_bsize = dentry->d_sb->s_blocksize;
412 blockbits = dentry->d_sb->s_blocksize_bits;
413 blockres = (1 << blockbits) - 1;
414 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
415 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
416 buf->f_bavail = (res.abytes + blockres) >> blockbits;
418 buf->f_files = res.tfiles;
419 buf->f_ffree = res.afiles;
421 buf->f_namelen = server->namelen;
423 return 0;
425 out_err:
426 dprintk("%s: statfs error = %d\n", __func__, -error);
427 return error;
431 * Map the security flavour number to a name
433 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
435 static const struct {
436 rpc_authflavor_t flavour;
437 const char *str;
438 } sec_flavours[] = {
439 { RPC_AUTH_NULL, "null" },
440 { RPC_AUTH_UNIX, "sys" },
441 { RPC_AUTH_GSS_KRB5, "krb5" },
442 { RPC_AUTH_GSS_KRB5I, "krb5i" },
443 { RPC_AUTH_GSS_KRB5P, "krb5p" },
444 { RPC_AUTH_GSS_LKEY, "lkey" },
445 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
446 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
447 { RPC_AUTH_GSS_SPKM, "spkm" },
448 { RPC_AUTH_GSS_SPKMI, "spkmi" },
449 { RPC_AUTH_GSS_SPKMP, "spkmp" },
450 { UINT_MAX, "unknown" }
452 int i;
454 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
455 if (sec_flavours[i].flavour == flavour)
456 break;
458 return sec_flavours[i].str;
461 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
462 int showdefaults)
464 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
466 switch (sap->sa_family) {
467 case AF_INET: {
468 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
469 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
470 break;
472 case AF_INET6: {
473 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
474 seq_printf(m, ",mountaddr=%pI6", &sin6->sin6_addr);
475 break;
477 default:
478 if (showdefaults)
479 seq_printf(m, ",mountaddr=unspecified");
482 if (nfss->mountd_version || showdefaults)
483 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
484 if (nfss->mountd_port || showdefaults)
485 seq_printf(m, ",mountport=%u", nfss->mountd_port);
487 switch (nfss->mountd_protocol) {
488 case IPPROTO_UDP:
489 seq_printf(m, ",mountproto=udp");
490 break;
491 case IPPROTO_TCP:
492 seq_printf(m, ",mountproto=tcp");
493 break;
494 default:
495 if (showdefaults)
496 seq_printf(m, ",mountproto=auto");
501 * Describe the mount options in force on this server representation
503 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
504 int showdefaults)
506 static const struct proc_nfs_info {
507 int flag;
508 const char *str;
509 const char *nostr;
510 } nfs_info[] = {
511 { NFS_MOUNT_SOFT, ",soft", ",hard" },
512 { NFS_MOUNT_INTR, ",intr", ",nointr" },
513 { NFS_MOUNT_POSIX, ",posix", "" },
514 { NFS_MOUNT_NOCTO, ",nocto", "" },
515 { NFS_MOUNT_NOAC, ",noac", "" },
516 { NFS_MOUNT_NONLM, ",nolock", "" },
517 { NFS_MOUNT_NOACL, ",noacl", "" },
518 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
519 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
520 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
521 { 0, NULL, NULL }
523 const struct proc_nfs_info *nfs_infop;
524 struct nfs_client *clp = nfss->nfs_client;
525 u32 version = clp->rpc_ops->version;
527 seq_printf(m, ",vers=%u", version);
528 seq_printf(m, ",rsize=%u", nfss->rsize);
529 seq_printf(m, ",wsize=%u", nfss->wsize);
530 if (nfss->bsize != 0)
531 seq_printf(m, ",bsize=%u", nfss->bsize);
532 seq_printf(m, ",namlen=%u", nfss->namelen);
533 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
534 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
535 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
536 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
537 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
538 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
539 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
540 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
541 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
542 if (nfss->flags & nfs_infop->flag)
543 seq_puts(m, nfs_infop->str);
544 else
545 seq_puts(m, nfs_infop->nostr);
547 seq_printf(m, ",proto=%s",
548 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
549 if (version == 4) {
550 if (nfss->port != NFS_PORT)
551 seq_printf(m, ",port=%u", nfss->port);
552 } else
553 if (nfss->port)
554 seq_printf(m, ",port=%u", nfss->port);
556 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
557 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
558 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
560 if (version != 4)
561 nfs_show_mountd_options(m, nfss, showdefaults);
563 #ifdef CONFIG_NFS_V4
564 if (clp->rpc_ops->version == 4)
565 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
566 #endif
570 * Describe the mount options on this VFS mountpoint
572 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
574 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
576 nfs_show_mount_options(m, nfss, 0);
578 seq_printf(m, ",addr=%s",
579 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
580 RPC_DISPLAY_ADDR));
582 return 0;
586 * Present statistical information for this VFS mountpoint
588 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
590 int i, cpu;
591 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
592 struct rpc_auth *auth = nfss->client->cl_auth;
593 struct nfs_iostats totals = { };
595 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
598 * Display all mount option settings
600 seq_printf(m, "\n\topts:\t");
601 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
602 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
603 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
604 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
605 nfs_show_mount_options(m, nfss, 1);
607 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
609 seq_printf(m, "\n\tcaps:\t");
610 seq_printf(m, "caps=0x%x", nfss->caps);
611 seq_printf(m, ",wtmult=%u", nfss->wtmult);
612 seq_printf(m, ",dtsize=%u", nfss->dtsize);
613 seq_printf(m, ",bsize=%u", nfss->bsize);
614 seq_printf(m, ",namlen=%u", nfss->namelen);
616 #ifdef CONFIG_NFS_V4
617 if (nfss->nfs_client->rpc_ops->version == 4) {
618 seq_printf(m, "\n\tnfsv4:\t");
619 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
620 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
621 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
623 #endif
626 * Display security flavor in effect for this mount
628 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
629 if (auth->au_flavor)
630 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
633 * Display superblock I/O counters
635 for_each_possible_cpu(cpu) {
636 struct nfs_iostats *stats;
638 preempt_disable();
639 stats = per_cpu_ptr(nfss->io_stats, cpu);
641 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
642 totals.events[i] += stats->events[i];
643 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
644 totals.bytes[i] += stats->bytes[i];
646 preempt_enable();
649 seq_printf(m, "\n\tevents:\t");
650 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
651 seq_printf(m, "%lu ", totals.events[i]);
652 seq_printf(m, "\n\tbytes:\t");
653 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
654 seq_printf(m, "%Lu ", totals.bytes[i]);
655 seq_printf(m, "\n");
657 rpc_print_iostats(m, nfss->client);
659 return 0;
663 * Begin unmount by attempting to remove all automounted mountpoints we added
664 * in response to xdev traversals and referrals
666 static void nfs_umount_begin(struct super_block *sb)
668 struct nfs_server *server = NFS_SB(sb);
669 struct rpc_clnt *rpc;
671 /* -EIO all pending I/O */
672 rpc = server->client_acl;
673 if (!IS_ERR(rpc))
674 rpc_killall_tasks(rpc);
675 rpc = server->client;
676 if (!IS_ERR(rpc))
677 rpc_killall_tasks(rpc);
681 * Sanity-check a server address provided by the mount command.
683 * Address family must be initialized, and address must not be
684 * the ANY address for that family.
686 static int nfs_verify_server_address(struct sockaddr *addr)
688 switch (addr->sa_family) {
689 case AF_INET: {
690 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
691 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
693 case AF_INET6: {
694 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
695 return !ipv6_addr_any(sa);
699 return 0;
702 static void nfs_parse_ipv4_address(char *string, size_t str_len,
703 struct sockaddr *sap, size_t *addr_len)
705 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
706 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
708 if (str_len <= INET_ADDRSTRLEN) {
709 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
710 (int)str_len, string);
712 sin->sin_family = AF_INET;
713 *addr_len = sizeof(*sin);
714 if (in4_pton(string, str_len, addr, '\0', NULL))
715 return;
718 sap->sa_family = AF_UNSPEC;
719 *addr_len = 0;
722 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
723 static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
724 const char *delim,
725 struct sockaddr_in6 *sin6)
727 char *p;
728 size_t len;
730 if ((string + str_len) == delim)
731 return 1;
733 if (*delim != IPV6_SCOPE_DELIMITER)
734 return 0;
736 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
737 return 0;
739 len = (string + str_len) - delim - 1;
740 p = kstrndup(delim + 1, len, GFP_KERNEL);
741 if (p) {
742 unsigned long scope_id = 0;
743 struct net_device *dev;
745 dev = dev_get_by_name(&init_net, p);
746 if (dev != NULL) {
747 scope_id = dev->ifindex;
748 dev_put(dev);
749 } else {
750 if (strict_strtoul(p, 10, &scope_id) == 0) {
751 kfree(p);
752 return 0;
756 kfree(p);
758 sin6->sin6_scope_id = scope_id;
759 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
760 return 1;
763 return 0;
766 static void nfs_parse_ipv6_address(char *string, size_t str_len,
767 struct sockaddr *sap, size_t *addr_len)
769 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
770 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
771 const char *delim;
773 if (str_len <= INET6_ADDRSTRLEN) {
774 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
775 (int)str_len, string);
777 sin6->sin6_family = AF_INET6;
778 *addr_len = sizeof(*sin6);
779 if (in6_pton(string, str_len, addr,
780 IPV6_SCOPE_DELIMITER, &delim) != 0) {
781 if (nfs_parse_ipv6_scope_id(string, str_len,
782 delim, sin6) != 0)
783 return;
787 sap->sa_family = AF_UNSPEC;
788 *addr_len = 0;
790 #else
791 static void nfs_parse_ipv6_address(char *string, size_t str_len,
792 struct sockaddr *sap, size_t *addr_len)
794 sap->sa_family = AF_UNSPEC;
795 *addr_len = 0;
797 #endif
800 * Construct a sockaddr based on the contents of a string that contains
801 * an IP address in presentation format.
803 * If there is a problem constructing the new sockaddr, set the address
804 * family to AF_UNSPEC.
806 void nfs_parse_ip_address(char *string, size_t str_len,
807 struct sockaddr *sap, size_t *addr_len)
809 unsigned int i, colons;
811 colons = 0;
812 for (i = 0; i < str_len; i++)
813 if (string[i] == ':')
814 colons++;
816 if (colons >= 2)
817 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
818 else
819 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
823 * Sanity check the NFS transport protocol.
826 static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
828 switch (mnt->nfs_server.protocol) {
829 case XPRT_TRANSPORT_UDP:
830 case XPRT_TRANSPORT_TCP:
831 case XPRT_TRANSPORT_RDMA:
832 break;
833 default:
834 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
839 * For text based NFSv2/v3 mounts, the mount protocol transport default
840 * settings should depend upon the specified NFS transport.
842 static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
844 nfs_validate_transport_protocol(mnt);
846 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
847 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
848 return;
849 switch (mnt->nfs_server.protocol) {
850 case XPRT_TRANSPORT_UDP:
851 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
852 break;
853 case XPRT_TRANSPORT_TCP:
854 case XPRT_TRANSPORT_RDMA:
855 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
860 * Parse the value of the 'sec=' option.
862 * The flavor_len setting is for v4 mounts.
864 static int nfs_parse_security_flavors(char *value,
865 struct nfs_parsed_mount_data *mnt)
867 substring_t args[MAX_OPT_ARGS];
869 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
871 switch (match_token(value, nfs_secflavor_tokens, args)) {
872 case Opt_sec_none:
873 mnt->auth_flavor_len = 0;
874 mnt->auth_flavors[0] = RPC_AUTH_NULL;
875 break;
876 case Opt_sec_sys:
877 mnt->auth_flavor_len = 0;
878 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
879 break;
880 case Opt_sec_krb5:
881 mnt->auth_flavor_len = 1;
882 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
883 break;
884 case Opt_sec_krb5i:
885 mnt->auth_flavor_len = 1;
886 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
887 break;
888 case Opt_sec_krb5p:
889 mnt->auth_flavor_len = 1;
890 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
891 break;
892 case Opt_sec_lkey:
893 mnt->auth_flavor_len = 1;
894 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
895 break;
896 case Opt_sec_lkeyi:
897 mnt->auth_flavor_len = 1;
898 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
899 break;
900 case Opt_sec_lkeyp:
901 mnt->auth_flavor_len = 1;
902 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
903 break;
904 case Opt_sec_spkm:
905 mnt->auth_flavor_len = 1;
906 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
907 break;
908 case Opt_sec_spkmi:
909 mnt->auth_flavor_len = 1;
910 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
911 break;
912 case Opt_sec_spkmp:
913 mnt->auth_flavor_len = 1;
914 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
915 break;
916 default:
917 return 0;
920 return 1;
923 static void nfs_parse_invalid_value(const char *option)
925 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
929 * Error-check and convert a string of mount options from user space into
930 * a data structure. The whole mount string is processed; bad options are
931 * skipped as they are encountered. If there were no errors, return 1;
932 * otherwise return 0 (zero).
934 static int nfs_parse_mount_options(char *raw,
935 struct nfs_parsed_mount_data *mnt)
937 char *p, *string, *secdata;
938 int rc, sloppy = 0, errors = 0;
940 if (!raw) {
941 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
942 return 1;
944 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
946 secdata = alloc_secdata();
947 if (!secdata)
948 goto out_nomem;
950 rc = security_sb_copy_data(raw, secdata);
951 if (rc)
952 goto out_security_failure;
954 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
955 if (rc)
956 goto out_security_failure;
958 free_secdata(secdata);
960 while ((p = strsep(&raw, ",")) != NULL) {
961 substring_t args[MAX_OPT_ARGS];
962 int option, token;
964 if (!*p)
965 continue;
967 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
969 token = match_token(p, nfs_mount_option_tokens, args);
970 switch (token) {
973 * boolean options: foo/nofoo
975 case Opt_soft:
976 mnt->flags |= NFS_MOUNT_SOFT;
977 break;
978 case Opt_hard:
979 mnt->flags &= ~NFS_MOUNT_SOFT;
980 break;
981 case Opt_posix:
982 mnt->flags |= NFS_MOUNT_POSIX;
983 break;
984 case Opt_noposix:
985 mnt->flags &= ~NFS_MOUNT_POSIX;
986 break;
987 case Opt_cto:
988 mnt->flags &= ~NFS_MOUNT_NOCTO;
989 break;
990 case Opt_nocto:
991 mnt->flags |= NFS_MOUNT_NOCTO;
992 break;
993 case Opt_ac:
994 mnt->flags &= ~NFS_MOUNT_NOAC;
995 break;
996 case Opt_noac:
997 mnt->flags |= NFS_MOUNT_NOAC;
998 break;
999 case Opt_lock:
1000 mnt->flags &= ~NFS_MOUNT_NONLM;
1001 break;
1002 case Opt_nolock:
1003 mnt->flags |= NFS_MOUNT_NONLM;
1004 break;
1005 case Opt_v2:
1006 mnt->flags &= ~NFS_MOUNT_VER3;
1007 break;
1008 case Opt_v3:
1009 mnt->flags |= NFS_MOUNT_VER3;
1010 break;
1011 case Opt_udp:
1012 mnt->flags &= ~NFS_MOUNT_TCP;
1013 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1014 break;
1015 case Opt_tcp:
1016 mnt->flags |= NFS_MOUNT_TCP;
1017 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1018 break;
1019 case Opt_rdma:
1020 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1021 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1022 xprt_load_transport(p);
1023 break;
1024 case Opt_acl:
1025 mnt->flags &= ~NFS_MOUNT_NOACL;
1026 break;
1027 case Opt_noacl:
1028 mnt->flags |= NFS_MOUNT_NOACL;
1029 break;
1030 case Opt_rdirplus:
1031 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1032 break;
1033 case Opt_nordirplus:
1034 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1035 break;
1036 case Opt_sharecache:
1037 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1038 break;
1039 case Opt_nosharecache:
1040 mnt->flags |= NFS_MOUNT_UNSHARED;
1041 break;
1042 case Opt_resvport:
1043 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1044 break;
1045 case Opt_noresvport:
1046 mnt->flags |= NFS_MOUNT_NORESVPORT;
1047 break;
1050 * options that take numeric values
1052 case Opt_port:
1053 if (match_int(args, &option) ||
1054 option < 0 || option > USHORT_MAX) {
1055 errors++;
1056 nfs_parse_invalid_value("port");
1057 } else
1058 mnt->nfs_server.port = option;
1059 break;
1060 case Opt_rsize:
1061 if (match_int(args, &option) || option < 0) {
1062 errors++;
1063 nfs_parse_invalid_value("rsize");
1064 } else
1065 mnt->rsize = option;
1066 break;
1067 case Opt_wsize:
1068 if (match_int(args, &option) || option < 0) {
1069 errors++;
1070 nfs_parse_invalid_value("wsize");
1071 } else
1072 mnt->wsize = option;
1073 break;
1074 case Opt_bsize:
1075 if (match_int(args, &option) || option < 0) {
1076 errors++;
1077 nfs_parse_invalid_value("bsize");
1078 } else
1079 mnt->bsize = option;
1080 break;
1081 case Opt_timeo:
1082 if (match_int(args, &option) || option <= 0) {
1083 errors++;
1084 nfs_parse_invalid_value("timeo");
1085 } else
1086 mnt->timeo = option;
1087 break;
1088 case Opt_retrans:
1089 if (match_int(args, &option) || option <= 0) {
1090 errors++;
1091 nfs_parse_invalid_value("retrans");
1092 } else
1093 mnt->retrans = option;
1094 break;
1095 case Opt_acregmin:
1096 if (match_int(args, &option) || option < 0) {
1097 errors++;
1098 nfs_parse_invalid_value("acregmin");
1099 } else
1100 mnt->acregmin = option;
1101 break;
1102 case Opt_acregmax:
1103 if (match_int(args, &option) || option < 0) {
1104 errors++;
1105 nfs_parse_invalid_value("acregmax");
1106 } else
1107 mnt->acregmax = option;
1108 break;
1109 case Opt_acdirmin:
1110 if (match_int(args, &option) || option < 0) {
1111 errors++;
1112 nfs_parse_invalid_value("acdirmin");
1113 } else
1114 mnt->acdirmin = option;
1115 break;
1116 case Opt_acdirmax:
1117 if (match_int(args, &option) || option < 0) {
1118 errors++;
1119 nfs_parse_invalid_value("acdirmax");
1120 } else
1121 mnt->acdirmax = option;
1122 break;
1123 case Opt_actimeo:
1124 if (match_int(args, &option) || option < 0) {
1125 errors++;
1126 nfs_parse_invalid_value("actimeo");
1127 } else
1128 mnt->acregmin = mnt->acregmax =
1129 mnt->acdirmin = mnt->acdirmax = option;
1130 break;
1131 case Opt_namelen:
1132 if (match_int(args, &option) || option < 0) {
1133 errors++;
1134 nfs_parse_invalid_value("namlen");
1135 } else
1136 mnt->namlen = option;
1137 break;
1138 case Opt_mountport:
1139 if (match_int(args, &option) ||
1140 option < 0 || option > USHORT_MAX) {
1141 errors++;
1142 nfs_parse_invalid_value("mountport");
1143 } else
1144 mnt->mount_server.port = option;
1145 break;
1146 case Opt_mountvers:
1147 if (match_int(args, &option) ||
1148 option < NFS_MNT_VERSION ||
1149 option > NFS_MNT3_VERSION) {
1150 errors++;
1151 nfs_parse_invalid_value("mountvers");
1152 } else
1153 mnt->mount_server.version = option;
1154 break;
1155 case Opt_nfsvers:
1156 if (match_int(args, &option)) {
1157 errors++;
1158 nfs_parse_invalid_value("nfsvers");
1159 break;
1161 switch (option) {
1162 case NFS2_VERSION:
1163 mnt->flags &= ~NFS_MOUNT_VER3;
1164 break;
1165 case NFS3_VERSION:
1166 mnt->flags |= NFS_MOUNT_VER3;
1167 break;
1168 default:
1169 errors++;
1170 nfs_parse_invalid_value("nfsvers");
1172 break;
1175 * options that take text values
1177 case Opt_sec:
1178 string = match_strdup(args);
1179 if (string == NULL)
1180 goto out_nomem;
1181 rc = nfs_parse_security_flavors(string, mnt);
1182 kfree(string);
1183 if (!rc) {
1184 errors++;
1185 dfprintk(MOUNT, "NFS: unrecognized "
1186 "security flavor\n");
1188 break;
1189 case Opt_proto:
1190 string = match_strdup(args);
1191 if (string == NULL)
1192 goto out_nomem;
1193 token = match_token(string,
1194 nfs_xprt_protocol_tokens, args);
1195 kfree(string);
1197 switch (token) {
1198 case Opt_xprt_udp:
1199 mnt->flags &= ~NFS_MOUNT_TCP;
1200 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1201 break;
1202 case Opt_xprt_tcp:
1203 mnt->flags |= NFS_MOUNT_TCP;
1204 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1205 break;
1206 case Opt_xprt_rdma:
1207 /* vector side protocols to TCP */
1208 mnt->flags |= NFS_MOUNT_TCP;
1209 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1210 xprt_load_transport(string);
1211 break;
1212 default:
1213 errors++;
1214 dfprintk(MOUNT, "NFS: unrecognized "
1215 "transport protocol\n");
1217 kfree(string);
1218 break;
1219 case Opt_mountproto:
1220 string = match_strdup(args);
1221 if (string == NULL)
1222 goto out_nomem;
1223 token = match_token(string,
1224 nfs_xprt_protocol_tokens, args);
1226 switch (token) {
1227 case Opt_xprt_udp:
1228 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1229 break;
1230 case Opt_xprt_tcp:
1231 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1232 break;
1233 case Opt_xprt_rdma: /* not used for side protocols */
1234 default:
1235 errors++;
1236 dfprintk(MOUNT, "NFS: unrecognized "
1237 "transport protocol\n");
1239 break;
1240 case Opt_addr:
1241 string = match_strdup(args);
1242 if (string == NULL)
1243 goto out_nomem;
1244 nfs_parse_ip_address(string, strlen(string),
1245 (struct sockaddr *)
1246 &mnt->nfs_server.address,
1247 &mnt->nfs_server.addrlen);
1248 kfree(string);
1249 break;
1250 case Opt_clientaddr:
1251 string = match_strdup(args);
1252 if (string == NULL)
1253 goto out_nomem;
1254 kfree(mnt->client_address);
1255 mnt->client_address = string;
1256 break;
1257 case Opt_mounthost:
1258 string = match_strdup(args);
1259 if (string == NULL)
1260 goto out_nomem;
1261 kfree(mnt->mount_server.hostname);
1262 mnt->mount_server.hostname = string;
1263 break;
1264 case Opt_mountaddr:
1265 string = match_strdup(args);
1266 if (string == NULL)
1267 goto out_nomem;
1268 nfs_parse_ip_address(string, strlen(string),
1269 (struct sockaddr *)
1270 &mnt->mount_server.address,
1271 &mnt->mount_server.addrlen);
1272 kfree(string);
1273 break;
1274 case Opt_lookupcache:
1275 string = match_strdup(args);
1276 if (string == NULL)
1277 goto out_nomem;
1278 token = match_token(string,
1279 nfs_lookupcache_tokens, args);
1280 kfree(string);
1281 switch (token) {
1282 case Opt_lookupcache_all:
1283 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1284 break;
1285 case Opt_lookupcache_positive:
1286 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1287 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1288 break;
1289 case Opt_lookupcache_none:
1290 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1291 break;
1292 default:
1293 errors++;
1294 dfprintk(MOUNT, "NFS: invalid "
1295 "lookupcache argument\n");
1297 break;
1300 * Special options
1302 case Opt_sloppy:
1303 sloppy = 1;
1304 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1305 break;
1306 case Opt_userspace:
1307 case Opt_deprecated:
1308 dfprintk(MOUNT, "NFS: ignoring mount option "
1309 "'%s'\n", p);
1310 break;
1312 default:
1313 errors++;
1314 dfprintk(MOUNT, "NFS: unrecognized mount option "
1315 "'%s'\n", p);
1319 if (errors > 0) {
1320 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1321 errors, (errors == 1 ? "" : "s"));
1322 if (!sloppy)
1323 return 0;
1325 return 1;
1327 out_nomem:
1328 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1329 return 0;
1330 out_security_failure:
1331 free_secdata(secdata);
1332 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1333 return 0;
1337 * Use the remote server's MOUNT service to request the NFS file handle
1338 * corresponding to the provided path.
1340 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1341 struct nfs_fh *root_fh)
1343 struct nfs_mount_request request = {
1344 .sap = (struct sockaddr *)
1345 &args->mount_server.address,
1346 .dirpath = args->nfs_server.export_path,
1347 .protocol = args->mount_server.protocol,
1348 .fh = root_fh,
1349 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
1351 int status;
1353 if (args->mount_server.version == 0) {
1354 if (args->flags & NFS_MOUNT_VER3)
1355 args->mount_server.version = NFS_MNT3_VERSION;
1356 else
1357 args->mount_server.version = NFS_MNT_VERSION;
1359 request.version = args->mount_server.version;
1361 if (args->mount_server.hostname)
1362 request.hostname = args->mount_server.hostname;
1363 else
1364 request.hostname = args->nfs_server.hostname;
1367 * Construct the mount server's address.
1369 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1370 memcpy(request.sap, &args->nfs_server.address,
1371 args->nfs_server.addrlen);
1372 args->mount_server.addrlen = args->nfs_server.addrlen;
1374 request.salen = args->mount_server.addrlen;
1377 * autobind will be used if mount_server.port == 0
1379 nfs_set_port(request.sap, args->mount_server.port);
1382 * Now ask the mount server to map our export path
1383 * to a file handle.
1385 status = nfs_mount(&request);
1386 if (status == 0)
1387 return 0;
1389 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1390 request.hostname, status);
1391 return status;
1394 static int nfs_parse_simple_hostname(const char *dev_name,
1395 char **hostname, size_t maxnamlen,
1396 char **export_path, size_t maxpathlen)
1398 size_t len;
1399 char *colon, *comma;
1401 colon = strchr(dev_name, ':');
1402 if (colon == NULL)
1403 goto out_bad_devname;
1405 len = colon - dev_name;
1406 if (len > maxnamlen)
1407 goto out_hostname;
1409 /* N.B. caller will free nfs_server.hostname in all cases */
1410 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1411 if (!*hostname)
1412 goto out_nomem;
1414 /* kill possible hostname list: not supported */
1415 comma = strchr(*hostname, ',');
1416 if (comma != NULL) {
1417 if (comma == *hostname)
1418 goto out_bad_devname;
1419 *comma = '\0';
1422 colon++;
1423 len = strlen(colon);
1424 if (len > maxpathlen)
1425 goto out_path;
1426 *export_path = kstrndup(colon, len, GFP_KERNEL);
1427 if (!*export_path)
1428 goto out_nomem;
1430 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1431 return 0;
1433 out_bad_devname:
1434 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1435 return -EINVAL;
1437 out_nomem:
1438 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1439 return -ENOMEM;
1441 out_hostname:
1442 dfprintk(MOUNT, "NFS: server hostname too long\n");
1443 return -ENAMETOOLONG;
1445 out_path:
1446 dfprintk(MOUNT, "NFS: export pathname too long\n");
1447 return -ENAMETOOLONG;
1451 * Hostname has square brackets around it because it contains one or
1452 * more colons. We look for the first closing square bracket, and a
1453 * colon must follow it.
1455 static int nfs_parse_protected_hostname(const char *dev_name,
1456 char **hostname, size_t maxnamlen,
1457 char **export_path, size_t maxpathlen)
1459 size_t len;
1460 char *start, *end;
1462 start = (char *)(dev_name + 1);
1464 end = strchr(start, ']');
1465 if (end == NULL)
1466 goto out_bad_devname;
1467 if (*(end + 1) != ':')
1468 goto out_bad_devname;
1470 len = end - start;
1471 if (len > maxnamlen)
1472 goto out_hostname;
1474 /* N.B. caller will free nfs_server.hostname in all cases */
1475 *hostname = kstrndup(start, len, GFP_KERNEL);
1476 if (*hostname == NULL)
1477 goto out_nomem;
1479 end += 2;
1480 len = strlen(end);
1481 if (len > maxpathlen)
1482 goto out_path;
1483 *export_path = kstrndup(end, len, GFP_KERNEL);
1484 if (!*export_path)
1485 goto out_nomem;
1487 return 0;
1489 out_bad_devname:
1490 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1491 return -EINVAL;
1493 out_nomem:
1494 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1495 return -ENOMEM;
1497 out_hostname:
1498 dfprintk(MOUNT, "NFS: server hostname too long\n");
1499 return -ENAMETOOLONG;
1501 out_path:
1502 dfprintk(MOUNT, "NFS: export pathname too long\n");
1503 return -ENAMETOOLONG;
1507 * Split "dev_name" into "hostname:export_path".
1509 * The leftmost colon demarks the split between the server's hostname
1510 * and the export path. If the hostname starts with a left square
1511 * bracket, then it may contain colons.
1513 * Note: caller frees hostname and export path, even on error.
1515 static int nfs_parse_devname(const char *dev_name,
1516 char **hostname, size_t maxnamlen,
1517 char **export_path, size_t maxpathlen)
1519 if (*dev_name == '[')
1520 return nfs_parse_protected_hostname(dev_name,
1521 hostname, maxnamlen,
1522 export_path, maxpathlen);
1524 return nfs_parse_simple_hostname(dev_name,
1525 hostname, maxnamlen,
1526 export_path, maxpathlen);
1530 * Validate the NFS2/NFS3 mount data
1531 * - fills in the mount root filehandle
1533 * For option strings, user space handles the following behaviors:
1535 * + DNS: mapping server host name to IP address ("addr=" option)
1537 * + failure mode: how to behave if a mount request can't be handled
1538 * immediately ("fg/bg" option)
1540 * + retry: how often to retry a mount request ("retry=" option)
1542 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1543 * mountproto=tcp after mountproto=udp, and so on
1545 static int nfs_validate_mount_data(void *options,
1546 struct nfs_parsed_mount_data *args,
1547 struct nfs_fh *mntfh,
1548 const char *dev_name)
1550 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1552 if (data == NULL)
1553 goto out_no_data;
1555 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1556 args->rsize = NFS_MAX_FILE_IO_SIZE;
1557 args->wsize = NFS_MAX_FILE_IO_SIZE;
1558 args->acregmin = NFS_DEF_ACREGMIN;
1559 args->acregmax = NFS_DEF_ACREGMAX;
1560 args->acdirmin = NFS_DEF_ACDIRMIN;
1561 args->acdirmax = NFS_DEF_ACDIRMAX;
1562 args->mount_server.port = 0; /* autobind unless user sets port */
1563 args->nfs_server.port = 0; /* autobind unless user sets port */
1564 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1565 args->auth_flavors[0] = RPC_AUTH_UNIX;
1567 switch (data->version) {
1568 case 1:
1569 data->namlen = 0;
1570 case 2:
1571 data->bsize = 0;
1572 case 3:
1573 if (data->flags & NFS_MOUNT_VER3)
1574 goto out_no_v3;
1575 data->root.size = NFS2_FHSIZE;
1576 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1577 case 4:
1578 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1579 goto out_no_sec;
1580 case 5:
1581 memset(data->context, 0, sizeof(data->context));
1582 case 6:
1583 if (data->flags & NFS_MOUNT_VER3) {
1584 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1585 goto out_invalid_fh;
1586 mntfh->size = data->root.size;
1587 } else
1588 mntfh->size = NFS2_FHSIZE;
1591 memcpy(mntfh->data, data->root.data, mntfh->size);
1592 if (mntfh->size < sizeof(mntfh->data))
1593 memset(mntfh->data + mntfh->size, 0,
1594 sizeof(mntfh->data) - mntfh->size);
1597 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1598 * can deal with.
1600 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
1601 args->rsize = data->rsize;
1602 args->wsize = data->wsize;
1603 args->timeo = data->timeo;
1604 args->retrans = data->retrans;
1605 args->acregmin = data->acregmin;
1606 args->acregmax = data->acregmax;
1607 args->acdirmin = data->acdirmin;
1608 args->acdirmax = data->acdirmax;
1610 memcpy(&args->nfs_server.address, &data->addr,
1611 sizeof(data->addr));
1612 args->nfs_server.addrlen = sizeof(data->addr);
1613 if (!nfs_verify_server_address((struct sockaddr *)
1614 &args->nfs_server.address))
1615 goto out_no_address;
1617 if (!(data->flags & NFS_MOUNT_TCP))
1618 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1619 /* N.B. caller will free nfs_server.hostname in all cases */
1620 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1621 args->namlen = data->namlen;
1622 args->bsize = data->bsize;
1624 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1625 args->auth_flavors[0] = data->pseudoflavor;
1626 if (!args->nfs_server.hostname)
1627 goto out_nomem;
1630 * The legacy version 6 binary mount data from userspace has a
1631 * field used only to transport selinux information into the
1632 * the kernel. To continue to support that functionality we
1633 * have a touch of selinux knowledge here in the NFS code. The
1634 * userspace code converted context=blah to just blah so we are
1635 * converting back to the full string selinux understands.
1637 if (data->context[0]){
1638 #ifdef CONFIG_SECURITY_SELINUX
1639 int rc;
1640 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1641 if (!opts_str)
1642 return -ENOMEM;
1643 strcpy(opts_str, "context=");
1644 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1645 strcat(opts_str, &data->context[0]);
1646 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1647 kfree(opts_str);
1648 if (rc)
1649 return rc;
1650 #else
1651 return -EINVAL;
1652 #endif
1655 break;
1656 default: {
1657 int status;
1659 if (nfs_parse_mount_options((char *)options, args) == 0)
1660 return -EINVAL;
1662 if (!nfs_verify_server_address((struct sockaddr *)
1663 &args->nfs_server.address))
1664 goto out_no_address;
1666 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1667 args->nfs_server.port);
1669 nfs_set_mount_transport_protocol(args);
1671 status = nfs_parse_devname(dev_name,
1672 &args->nfs_server.hostname,
1673 PAGE_SIZE,
1674 &args->nfs_server.export_path,
1675 NFS_MAXPATHLEN);
1676 if (!status)
1677 status = nfs_try_mount(args, mntfh);
1679 kfree(args->nfs_server.export_path);
1680 args->nfs_server.export_path = NULL;
1682 if (status)
1683 return status;
1685 break;
1689 #ifndef CONFIG_NFS_V3
1690 if (args->flags & NFS_MOUNT_VER3)
1691 goto out_v3_not_compiled;
1692 #endif /* !CONFIG_NFS_V3 */
1694 return 0;
1696 out_no_data:
1697 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1698 return -EINVAL;
1700 out_no_v3:
1701 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1702 data->version);
1703 return -EINVAL;
1705 out_no_sec:
1706 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1707 return -EINVAL;
1709 #ifndef CONFIG_NFS_V3
1710 out_v3_not_compiled:
1711 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1712 return -EPROTONOSUPPORT;
1713 #endif /* !CONFIG_NFS_V3 */
1715 out_nomem:
1716 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1717 return -ENOMEM;
1719 out_no_address:
1720 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1721 return -EINVAL;
1723 out_invalid_fh:
1724 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1725 return -EINVAL;
1728 static int
1729 nfs_compare_remount_data(struct nfs_server *nfss,
1730 struct nfs_parsed_mount_data *data)
1732 if (data->flags != nfss->flags ||
1733 data->rsize != nfss->rsize ||
1734 data->wsize != nfss->wsize ||
1735 data->retrans != nfss->client->cl_timeout->to_retries ||
1736 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1737 data->acregmin != nfss->acregmin / HZ ||
1738 data->acregmax != nfss->acregmax / HZ ||
1739 data->acdirmin != nfss->acdirmin / HZ ||
1740 data->acdirmax != nfss->acdirmax / HZ ||
1741 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1742 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1743 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1744 data->nfs_server.addrlen) != 0)
1745 return -EINVAL;
1747 return 0;
1750 static int
1751 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1753 int error;
1754 struct nfs_server *nfss = sb->s_fs_info;
1755 struct nfs_parsed_mount_data *data;
1756 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1757 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
1758 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
1761 * Userspace mount programs that send binary options generally send
1762 * them populated with default values. We have no way to know which
1763 * ones were explicitly specified. Fall back to legacy behavior and
1764 * just return success.
1766 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1767 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1768 options->version <= 6))))
1769 return 0;
1771 data = kzalloc(sizeof(*data), GFP_KERNEL);
1772 if (data == NULL)
1773 return -ENOMEM;
1775 /* fill out struct with values from existing mount */
1776 data->flags = nfss->flags;
1777 data->rsize = nfss->rsize;
1778 data->wsize = nfss->wsize;
1779 data->retrans = nfss->client->cl_timeout->to_retries;
1780 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1781 data->acregmin = nfss->acregmin / HZ;
1782 data->acregmax = nfss->acregmax / HZ;
1783 data->acdirmin = nfss->acdirmin / HZ;
1784 data->acdirmax = nfss->acdirmax / HZ;
1785 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1786 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1787 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1788 data->nfs_server.addrlen);
1790 /* overwrite those values with any that were specified */
1791 error = nfs_parse_mount_options((char *)options, data);
1792 if (error < 0)
1793 goto out;
1795 /* compare new mount options with old ones */
1796 error = nfs_compare_remount_data(nfss, data);
1797 out:
1798 kfree(data);
1799 return error;
1803 * Initialise the common bits of the superblock
1805 static inline void nfs_initialise_sb(struct super_block *sb)
1807 struct nfs_server *server = NFS_SB(sb);
1809 sb->s_magic = NFS_SUPER_MAGIC;
1811 /* We probably want something more informative here */
1812 snprintf(sb->s_id, sizeof(sb->s_id),
1813 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1815 if (sb->s_blocksize == 0)
1816 sb->s_blocksize = nfs_block_bits(server->wsize,
1817 &sb->s_blocksize_bits);
1819 if (server->flags & NFS_MOUNT_NOAC)
1820 sb->s_flags |= MS_SYNCHRONOUS;
1822 nfs_super_set_maxbytes(sb, server->maxfilesize);
1826 * Finish setting up an NFS2/3 superblock
1828 static void nfs_fill_super(struct super_block *sb,
1829 struct nfs_parsed_mount_data *data)
1831 struct nfs_server *server = NFS_SB(sb);
1833 sb->s_blocksize_bits = 0;
1834 sb->s_blocksize = 0;
1835 if (data->bsize)
1836 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1838 if (server->flags & NFS_MOUNT_VER3) {
1839 /* The VFS shouldn't apply the umask to mode bits. We will do
1840 * so ourselves when necessary.
1842 sb->s_flags |= MS_POSIXACL;
1843 sb->s_time_gran = 1;
1846 sb->s_op = &nfs_sops;
1847 nfs_initialise_sb(sb);
1851 * Finish setting up a cloned NFS2/3 superblock
1853 static void nfs_clone_super(struct super_block *sb,
1854 const struct super_block *old_sb)
1856 struct nfs_server *server = NFS_SB(sb);
1858 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1859 sb->s_blocksize = old_sb->s_blocksize;
1860 sb->s_maxbytes = old_sb->s_maxbytes;
1862 if (server->flags & NFS_MOUNT_VER3) {
1863 /* The VFS shouldn't apply the umask to mode bits. We will do
1864 * so ourselves when necessary.
1866 sb->s_flags |= MS_POSIXACL;
1867 sb->s_time_gran = 1;
1870 sb->s_op = old_sb->s_op;
1871 nfs_initialise_sb(sb);
1874 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1876 const struct nfs_server *a = s->s_fs_info;
1877 const struct rpc_clnt *clnt_a = a->client;
1878 const struct rpc_clnt *clnt_b = b->client;
1880 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1881 goto Ebusy;
1882 if (a->nfs_client != b->nfs_client)
1883 goto Ebusy;
1884 if (a->flags != b->flags)
1885 goto Ebusy;
1886 if (a->wsize != b->wsize)
1887 goto Ebusy;
1888 if (a->rsize != b->rsize)
1889 goto Ebusy;
1890 if (a->acregmin != b->acregmin)
1891 goto Ebusy;
1892 if (a->acregmax != b->acregmax)
1893 goto Ebusy;
1894 if (a->acdirmin != b->acdirmin)
1895 goto Ebusy;
1896 if (a->acdirmax != b->acdirmax)
1897 goto Ebusy;
1898 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1899 goto Ebusy;
1900 return 1;
1901 Ebusy:
1902 return 0;
1905 struct nfs_sb_mountdata {
1906 struct nfs_server *server;
1907 int mntflags;
1910 static int nfs_set_super(struct super_block *s, void *data)
1912 struct nfs_sb_mountdata *sb_mntdata = data;
1913 struct nfs_server *server = sb_mntdata->server;
1914 int ret;
1916 s->s_flags = sb_mntdata->mntflags;
1917 s->s_fs_info = server;
1918 ret = set_anon_super(s, server);
1919 if (ret == 0)
1920 server->s_dev = s->s_dev;
1921 return ret;
1924 static int nfs_compare_super_address(struct nfs_server *server1,
1925 struct nfs_server *server2)
1927 struct sockaddr *sap1, *sap2;
1929 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1930 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1932 if (sap1->sa_family != sap2->sa_family)
1933 return 0;
1935 switch (sap1->sa_family) {
1936 case AF_INET: {
1937 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1938 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1939 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1940 return 0;
1941 if (sin1->sin_port != sin2->sin_port)
1942 return 0;
1943 break;
1945 case AF_INET6: {
1946 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1947 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1948 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1949 return 0;
1950 if (sin1->sin6_port != sin2->sin6_port)
1951 return 0;
1952 break;
1954 default:
1955 return 0;
1958 return 1;
1961 static int nfs_compare_super(struct super_block *sb, void *data)
1963 struct nfs_sb_mountdata *sb_mntdata = data;
1964 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1965 int mntflags = sb_mntdata->mntflags;
1967 if (!nfs_compare_super_address(old, server))
1968 return 0;
1969 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1970 if (old->flags & NFS_MOUNT_UNSHARED)
1971 return 0;
1972 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1973 return 0;
1974 return nfs_compare_mount_options(sb, server, mntflags);
1977 static int nfs_bdi_register(struct nfs_server *server)
1979 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1982 static int nfs_get_sb(struct file_system_type *fs_type,
1983 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1985 struct nfs_server *server = NULL;
1986 struct super_block *s;
1987 struct nfs_parsed_mount_data *data;
1988 struct nfs_fh *mntfh;
1989 struct dentry *mntroot;
1990 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1991 struct nfs_sb_mountdata sb_mntdata = {
1992 .mntflags = flags,
1994 int error = -ENOMEM;
1996 data = kzalloc(sizeof(*data), GFP_KERNEL);
1997 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1998 if (data == NULL || mntfh == NULL)
1999 goto out_free_fh;
2001 security_init_mnt_opts(&data->lsm_opts);
2003 /* Validate the mount data */
2004 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
2005 if (error < 0)
2006 goto out;
2008 /* Get a volume representation */
2009 server = nfs_create_server(data, mntfh);
2010 if (IS_ERR(server)) {
2011 error = PTR_ERR(server);
2012 goto out;
2014 sb_mntdata.server = server;
2016 if (server->flags & NFS_MOUNT_UNSHARED)
2017 compare_super = NULL;
2019 /* Get a superblock - note that we may end up sharing one that already exists */
2020 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2021 if (IS_ERR(s)) {
2022 error = PTR_ERR(s);
2023 goto out_err_nosb;
2026 if (s->s_fs_info != server) {
2027 nfs_free_server(server);
2028 server = NULL;
2029 } else {
2030 error = nfs_bdi_register(server);
2031 if (error)
2032 goto error_splat_super;
2035 if (!s->s_root) {
2036 /* initial superblock/root creation */
2037 nfs_fill_super(s, data);
2038 nfs_fscache_get_super_cookie(s, data);
2041 mntroot = nfs_get_root(s, mntfh);
2042 if (IS_ERR(mntroot)) {
2043 error = PTR_ERR(mntroot);
2044 goto error_splat_super;
2047 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2048 if (error)
2049 goto error_splat_root;
2051 s->s_flags |= MS_ACTIVE;
2052 mnt->mnt_sb = s;
2053 mnt->mnt_root = mntroot;
2054 error = 0;
2056 out:
2057 kfree(data->nfs_server.hostname);
2058 kfree(data->mount_server.hostname);
2059 kfree(data->fscache_uniq);
2060 security_free_mnt_opts(&data->lsm_opts);
2061 out_free_fh:
2062 kfree(mntfh);
2063 kfree(data);
2064 return error;
2066 out_err_nosb:
2067 nfs_free_server(server);
2068 goto out;
2070 error_splat_root:
2071 dput(mntroot);
2072 error_splat_super:
2073 up_write(&s->s_umount);
2074 deactivate_super(s);
2075 goto out;
2079 * Destroy an NFS2/3 superblock
2081 static void nfs_kill_super(struct super_block *s)
2083 struct nfs_server *server = NFS_SB(s);
2085 bdi_unregister(&server->backing_dev_info);
2086 kill_anon_super(s);
2087 nfs_fscache_release_super_cookie(s);
2088 nfs_free_server(server);
2092 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2094 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2095 const char *dev_name, void *raw_data,
2096 struct vfsmount *mnt)
2098 struct nfs_clone_mount *data = raw_data;
2099 struct super_block *s;
2100 struct nfs_server *server;
2101 struct dentry *mntroot;
2102 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2103 struct nfs_sb_mountdata sb_mntdata = {
2104 .mntflags = flags,
2106 int error;
2108 dprintk("--> nfs_xdev_get_sb()\n");
2110 /* create a new volume representation */
2111 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2112 if (IS_ERR(server)) {
2113 error = PTR_ERR(server);
2114 goto out_err_noserver;
2116 sb_mntdata.server = server;
2118 if (server->flags & NFS_MOUNT_UNSHARED)
2119 compare_super = NULL;
2121 /* Get a superblock - note that we may end up sharing one that already exists */
2122 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2123 if (IS_ERR(s)) {
2124 error = PTR_ERR(s);
2125 goto out_err_nosb;
2128 if (s->s_fs_info != server) {
2129 nfs_free_server(server);
2130 server = NULL;
2131 } else {
2132 error = nfs_bdi_register(server);
2133 if (error)
2134 goto error_splat_super;
2137 if (!s->s_root) {
2138 /* initial superblock/root creation */
2139 nfs_clone_super(s, data->sb);
2142 mntroot = nfs_get_root(s, data->fh);
2143 if (IS_ERR(mntroot)) {
2144 error = PTR_ERR(mntroot);
2145 goto error_splat_super;
2147 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2148 dput(mntroot);
2149 error = -ESTALE;
2150 goto error_splat_super;
2153 s->s_flags |= MS_ACTIVE;
2154 mnt->mnt_sb = s;
2155 mnt->mnt_root = mntroot;
2157 /* clone any lsm security options from the parent to the new sb */
2158 security_sb_clone_mnt_opts(data->sb, s);
2160 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2161 return 0;
2163 out_err_nosb:
2164 nfs_free_server(server);
2165 out_err_noserver:
2166 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2167 return error;
2169 error_splat_super:
2170 up_write(&s->s_umount);
2171 deactivate_super(s);
2172 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2173 return error;
2176 #ifdef CONFIG_NFS_V4
2179 * Finish setting up a cloned NFS4 superblock
2181 static void nfs4_clone_super(struct super_block *sb,
2182 const struct super_block *old_sb)
2184 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2185 sb->s_blocksize = old_sb->s_blocksize;
2186 sb->s_maxbytes = old_sb->s_maxbytes;
2187 sb->s_time_gran = 1;
2188 sb->s_op = old_sb->s_op;
2189 nfs_initialise_sb(sb);
2193 * Set up an NFS4 superblock
2195 static void nfs4_fill_super(struct super_block *sb)
2197 sb->s_time_gran = 1;
2198 sb->s_op = &nfs4_sops;
2199 nfs_initialise_sb(sb);
2203 * Validate NFSv4 mount options
2205 static int nfs4_validate_mount_data(void *options,
2206 struct nfs_parsed_mount_data *args,
2207 const char *dev_name)
2209 struct sockaddr_in *ap;
2210 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2211 char *c;
2213 if (data == NULL)
2214 goto out_no_data;
2216 args->rsize = NFS_MAX_FILE_IO_SIZE;
2217 args->wsize = NFS_MAX_FILE_IO_SIZE;
2218 args->acregmin = NFS_DEF_ACREGMIN;
2219 args->acregmax = NFS_DEF_ACREGMAX;
2220 args->acdirmin = NFS_DEF_ACDIRMIN;
2221 args->acdirmax = NFS_DEF_ACDIRMAX;
2222 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
2223 args->auth_flavors[0] = RPC_AUTH_UNIX;
2224 args->auth_flavor_len = 0;
2226 switch (data->version) {
2227 case 1:
2228 ap = (struct sockaddr_in *)&args->nfs_server.address;
2229 if (data->host_addrlen > sizeof(args->nfs_server.address))
2230 goto out_no_address;
2231 if (data->host_addrlen == 0)
2232 goto out_no_address;
2233 args->nfs_server.addrlen = data->host_addrlen;
2234 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
2235 return -EFAULT;
2236 if (!nfs_verify_server_address((struct sockaddr *)
2237 &args->nfs_server.address))
2238 goto out_no_address;
2240 if (data->auth_flavourlen) {
2241 if (data->auth_flavourlen > 1)
2242 goto out_inval_auth;
2243 if (copy_from_user(&args->auth_flavors[0],
2244 data->auth_flavours,
2245 sizeof(args->auth_flavors[0])))
2246 return -EFAULT;
2249 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2250 if (IS_ERR(c))
2251 return PTR_ERR(c);
2252 args->nfs_server.hostname = c;
2254 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2255 if (IS_ERR(c))
2256 return PTR_ERR(c);
2257 args->nfs_server.export_path = c;
2258 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2260 c = strndup_user(data->client_addr.data, 16);
2261 if (IS_ERR(c))
2262 return PTR_ERR(c);
2263 args->client_address = c;
2266 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2267 * can deal with.
2270 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2271 args->rsize = data->rsize;
2272 args->wsize = data->wsize;
2273 args->timeo = data->timeo;
2274 args->retrans = data->retrans;
2275 args->acregmin = data->acregmin;
2276 args->acregmax = data->acregmax;
2277 args->acdirmin = data->acdirmin;
2278 args->acdirmax = data->acdirmax;
2279 args->nfs_server.protocol = data->proto;
2280 nfs_validate_transport_protocol(args);
2282 break;
2283 default: {
2284 int status;
2286 if (nfs_parse_mount_options((char *)options, args) == 0)
2287 return -EINVAL;
2289 if (!nfs_verify_server_address((struct sockaddr *)
2290 &args->nfs_server.address))
2291 return -EINVAL;
2293 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2294 args->nfs_server.port);
2296 nfs_validate_transport_protocol(args);
2298 if (args->auth_flavor_len > 1)
2299 goto out_inval_auth;
2301 if (args->client_address == NULL)
2302 goto out_no_client_address;
2304 status = nfs_parse_devname(dev_name,
2305 &args->nfs_server.hostname,
2306 NFS4_MAXNAMLEN,
2307 &args->nfs_server.export_path,
2308 NFS4_MAXPATHLEN);
2309 if (status < 0)
2310 return status;
2312 break;
2316 return 0;
2318 out_no_data:
2319 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2320 return -EINVAL;
2322 out_inval_auth:
2323 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2324 data->auth_flavourlen);
2325 return -EINVAL;
2327 out_no_address:
2328 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2329 return -EINVAL;
2331 out_no_client_address:
2332 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2333 return -EINVAL;
2337 * Get the superblock for an NFS4 mountpoint
2339 static int nfs4_get_sb(struct file_system_type *fs_type,
2340 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2342 struct nfs_parsed_mount_data *data;
2343 struct super_block *s;
2344 struct nfs_server *server;
2345 struct nfs_fh *mntfh;
2346 struct dentry *mntroot;
2347 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2348 struct nfs_sb_mountdata sb_mntdata = {
2349 .mntflags = flags,
2351 int error = -ENOMEM;
2353 data = kzalloc(sizeof(*data), GFP_KERNEL);
2354 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2355 if (data == NULL || mntfh == NULL)
2356 goto out_free_fh;
2358 security_init_mnt_opts(&data->lsm_opts);
2360 /* Validate the mount data */
2361 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2362 if (error < 0)
2363 goto out;
2365 /* Get a volume representation */
2366 server = nfs4_create_server(data, mntfh);
2367 if (IS_ERR(server)) {
2368 error = PTR_ERR(server);
2369 goto out;
2371 sb_mntdata.server = server;
2373 if (server->flags & NFS4_MOUNT_UNSHARED)
2374 compare_super = NULL;
2376 /* Get a superblock - note that we may end up sharing one that already exists */
2377 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2378 if (IS_ERR(s)) {
2379 error = PTR_ERR(s);
2380 goto out_free;
2383 if (s->s_fs_info != server) {
2384 nfs_free_server(server);
2385 server = NULL;
2386 } else {
2387 error = nfs_bdi_register(server);
2388 if (error)
2389 goto error_splat_super;
2392 if (!s->s_root) {
2393 /* initial superblock/root creation */
2394 nfs4_fill_super(s);
2395 nfs_fscache_get_super_cookie(s, data);
2398 mntroot = nfs4_get_root(s, mntfh);
2399 if (IS_ERR(mntroot)) {
2400 error = PTR_ERR(mntroot);
2401 goto error_splat_super;
2404 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2405 if (error)
2406 goto error_splat_root;
2408 s->s_flags |= MS_ACTIVE;
2409 mnt->mnt_sb = s;
2410 mnt->mnt_root = mntroot;
2411 error = 0;
2413 out:
2414 kfree(data->client_address);
2415 kfree(data->nfs_server.export_path);
2416 kfree(data->nfs_server.hostname);
2417 kfree(data->fscache_uniq);
2418 security_free_mnt_opts(&data->lsm_opts);
2419 out_free_fh:
2420 kfree(mntfh);
2421 kfree(data);
2422 return error;
2424 out_free:
2425 nfs_free_server(server);
2426 goto out;
2428 error_splat_root:
2429 dput(mntroot);
2430 error_splat_super:
2431 up_write(&s->s_umount);
2432 deactivate_super(s);
2433 goto out;
2436 static void nfs4_kill_super(struct super_block *sb)
2438 struct nfs_server *server = NFS_SB(sb);
2440 nfs_super_return_all_delegations(sb);
2441 kill_anon_super(sb);
2443 nfs4_renewd_prepare_shutdown(server);
2444 nfs_fscache_release_super_cookie(sb);
2445 nfs_free_server(server);
2449 * Clone an NFS4 server record on xdev traversal (FSID-change)
2451 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2452 const char *dev_name, void *raw_data,
2453 struct vfsmount *mnt)
2455 struct nfs_clone_mount *data = raw_data;
2456 struct super_block *s;
2457 struct nfs_server *server;
2458 struct dentry *mntroot;
2459 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2460 struct nfs_sb_mountdata sb_mntdata = {
2461 .mntflags = flags,
2463 int error;
2465 dprintk("--> nfs4_xdev_get_sb()\n");
2467 /* create a new volume representation */
2468 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2469 if (IS_ERR(server)) {
2470 error = PTR_ERR(server);
2471 goto out_err_noserver;
2473 sb_mntdata.server = server;
2475 if (server->flags & NFS4_MOUNT_UNSHARED)
2476 compare_super = NULL;
2478 /* Get a superblock - note that we may end up sharing one that already exists */
2479 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2480 if (IS_ERR(s)) {
2481 error = PTR_ERR(s);
2482 goto out_err_nosb;
2485 if (s->s_fs_info != server) {
2486 nfs_free_server(server);
2487 server = NULL;
2488 } else {
2489 error = nfs_bdi_register(server);
2490 if (error)
2491 goto error_splat_super;
2494 if (!s->s_root) {
2495 /* initial superblock/root creation */
2496 nfs4_clone_super(s, data->sb);
2499 mntroot = nfs4_get_root(s, data->fh);
2500 if (IS_ERR(mntroot)) {
2501 error = PTR_ERR(mntroot);
2502 goto error_splat_super;
2504 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2505 dput(mntroot);
2506 error = -ESTALE;
2507 goto error_splat_super;
2510 s->s_flags |= MS_ACTIVE;
2511 mnt->mnt_sb = s;
2512 mnt->mnt_root = mntroot;
2514 security_sb_clone_mnt_opts(data->sb, s);
2516 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2517 return 0;
2519 out_err_nosb:
2520 nfs_free_server(server);
2521 out_err_noserver:
2522 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2523 return error;
2525 error_splat_super:
2526 up_write(&s->s_umount);
2527 deactivate_super(s);
2528 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2529 return error;
2533 * Create an NFS4 server record on referral traversal
2535 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2536 const char *dev_name, void *raw_data,
2537 struct vfsmount *mnt)
2539 struct nfs_clone_mount *data = raw_data;
2540 struct super_block *s;
2541 struct nfs_server *server;
2542 struct dentry *mntroot;
2543 struct nfs_fh mntfh;
2544 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2545 struct nfs_sb_mountdata sb_mntdata = {
2546 .mntflags = flags,
2548 int error;
2550 dprintk("--> nfs4_referral_get_sb()\n");
2552 /* create a new volume representation */
2553 server = nfs4_create_referral_server(data, &mntfh);
2554 if (IS_ERR(server)) {
2555 error = PTR_ERR(server);
2556 goto out_err_noserver;
2558 sb_mntdata.server = server;
2560 if (server->flags & NFS4_MOUNT_UNSHARED)
2561 compare_super = NULL;
2563 /* Get a superblock - note that we may end up sharing one that already exists */
2564 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2565 if (IS_ERR(s)) {
2566 error = PTR_ERR(s);
2567 goto out_err_nosb;
2570 if (s->s_fs_info != server) {
2571 nfs_free_server(server);
2572 server = NULL;
2573 } else {
2574 error = nfs_bdi_register(server);
2575 if (error)
2576 goto error_splat_super;
2579 if (!s->s_root) {
2580 /* initial superblock/root creation */
2581 nfs4_fill_super(s);
2584 mntroot = nfs4_get_root(s, &mntfh);
2585 if (IS_ERR(mntroot)) {
2586 error = PTR_ERR(mntroot);
2587 goto error_splat_super;
2589 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2590 dput(mntroot);
2591 error = -ESTALE;
2592 goto error_splat_super;
2595 s->s_flags |= MS_ACTIVE;
2596 mnt->mnt_sb = s;
2597 mnt->mnt_root = mntroot;
2599 security_sb_clone_mnt_opts(data->sb, s);
2601 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2602 return 0;
2604 out_err_nosb:
2605 nfs_free_server(server);
2606 out_err_noserver:
2607 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2608 return error;
2610 error_splat_super:
2611 up_write(&s->s_umount);
2612 deactivate_super(s);
2613 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2614 return error;
2617 #endif /* CONFIG_NFS_V4 */