blob: a33e608713e9757d1fb80e4f9feddcd82df32b51 [file] [log] [blame]
David Howellsf7b422b2006-06-09 09:34:33 -04001/*
2 * linux/fs/nfs/super.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs superblock handling functions
7 *
Alan Cox526719b2008-10-27 15:19:48 +00008 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
David Howellsf7b422b2006-06-09 09:34:33 -04009 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
10 *
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
13 *
14 * Split from inode.c by David Howells <dhowells@redhat.com>
15 *
David Howells54ceac42006-08-22 20:06:13 -040016 * - 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())
David Howellsf7b422b2006-06-09 09:34:33 -040021 */
22
David Howellsf7b422b2006-06-09 09:34:33 -040023#include <linux/module.h>
24#include <linux/init.h>
25
26#include <linux/time.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/string.h>
30#include <linux/stat.h>
31#include <linux/errno.h>
32#include <linux/unistd.h>
33#include <linux/sunrpc/clnt.h>
34#include <linux/sunrpc/stats.h>
35#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040036#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040037#include <linux/sunrpc/xprtrdma.h>
David Howellsf7b422b2006-06-09 09:34:33 -040038#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>
Trond Myklebustc02d7ad2009-06-22 15:09:14 -040045#include <linux/mnt_namespace.h>
46#include <linux/namei.h>
David Howellsf7b422b2006-06-09 09:34:33 -040047#include <linux/nfs_idmap.h>
48#include <linux/vfs.h>
49#include <linux/inet.h>
Chuck Leverfd00a8f2007-12-10 14:57:38 -050050#include <linux/in6.h>
51#include <net/ipv6.h>
Chuck Leverd8e77482008-06-23 12:37:01 -040052#include <linux/netdevice.h>
David Howellsf7b422b2006-06-09 09:34:33 -040053#include <linux/nfs_xdr.h>
Adrian Bunkb5d5dfb2007-02-12 00:53:40 -080054#include <linux/magic.h>
Chuck Leverbf0fd762007-07-01 12:13:44 -040055#include <linux/parser.h>
David Howellsf7b422b2006-06-09 09:34:33 -040056
57#include <asm/system.h>
58#include <asm/uaccess.h>
59
60#include "nfs4_fs.h"
61#include "callback.h"
62#include "delegation.h"
63#include "iostat.h"
64#include "internal.h"
David Howells08734042009-04-03 16:42:42 +010065#include "fscache.h"
David Howellsf7b422b2006-06-09 09:34:33 -040066
67#define NFSDBG_FACILITY NFSDBG_VFS
68
Chuck Leverbf0fd762007-07-01 12:13:44 -040069enum {
70 /* Mount options that take no arguments */
71 Opt_soft, Opt_hard,
Chuck Leverbf0fd762007-07-01 12:13:44 -040072 Opt_posix, Opt_noposix,
73 Opt_cto, Opt_nocto,
74 Opt_ac, Opt_noac,
75 Opt_lock, Opt_nolock,
76 Opt_v2, Opt_v3,
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040077 Opt_udp, Opt_tcp, Opt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -040078 Opt_acl, Opt_noacl,
79 Opt_rdirplus, Opt_nordirplus,
Trond Myklebust75180df2007-05-16 16:53:28 -040080 Opt_sharecache, Opt_nosharecache,
Chuck Leverd7403512008-12-23 15:21:37 -050081 Opt_resvport, Opt_noresvport,
David Howellsb797cac2009-04-03 16:42:48 +010082 Opt_fscache, Opt_nofscache,
Chuck Leverbf0fd762007-07-01 12:13:44 -040083
84 /* Mount options that take integer arguments */
85 Opt_port,
86 Opt_rsize, Opt_wsize, Opt_bsize,
87 Opt_timeo, Opt_retrans,
88 Opt_acregmin, Opt_acregmax,
89 Opt_acdirmin, Opt_acdirmax,
90 Opt_actimeo,
91 Opt_namelen,
92 Opt_mountport,
Chuck Levere887cbc2007-10-26 13:32:29 -040093 Opt_mountvers,
Chuck Leverad879ce2007-10-26 13:32:24 -040094 Opt_nfsvers,
Mike Sager3fd5be92009-04-01 09:21:48 -040095 Opt_minorversion,
Chuck Leverbf0fd762007-07-01 12:13:44 -040096
97 /* Mount options that take string arguments */
Chuck Lever33832032007-12-10 14:59:13 -050098 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
Chuck Lever0ac83772007-09-11 18:01:04 -040099 Opt_addr, Opt_mountaddr, Opt_clientaddr,
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400100 Opt_lookupcache,
David Howellsb797cac2009-04-03 16:42:48 +0100101 Opt_fscache_uniq,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400102
Chuck Leverf45663c2008-06-24 19:28:02 -0400103 /* Special mount options */
104 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400105
106 Opt_err
107};
108
Steven Whitehousea447c092008-10-13 10:46:57 +0100109static const match_table_t nfs_mount_option_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400110 { Opt_userspace, "bg" },
111 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400112 { Opt_userspace, "retry=%s" },
113
Chuck Leverf45663c2008-06-24 19:28:02 -0400114 { Opt_sloppy, "sloppy" },
115
Chuck Leverbf0fd762007-07-01 12:13:44 -0400116 { Opt_soft, "soft" },
117 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400118 { Opt_deprecated, "intr" },
119 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400120 { Opt_posix, "posix" },
121 { Opt_noposix, "noposix" },
122 { Opt_cto, "cto" },
123 { Opt_nocto, "nocto" },
124 { Opt_ac, "ac" },
125 { Opt_noac, "noac" },
126 { Opt_lock, "lock" },
127 { Opt_nolock, "nolock" },
128 { Opt_v2, "v2" },
129 { Opt_v3, "v3" },
130 { Opt_udp, "udp" },
131 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400132 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400133 { Opt_acl, "acl" },
134 { Opt_noacl, "noacl" },
135 { Opt_rdirplus, "rdirplus" },
136 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400137 { Opt_sharecache, "sharecache" },
138 { Opt_nosharecache, "nosharecache" },
Chuck Leverd7403512008-12-23 15:21:37 -0500139 { Opt_resvport, "resvport" },
140 { Opt_noresvport, "noresvport" },
David Howellsb797cac2009-04-03 16:42:48 +0100141 { Opt_fscache, "fsc" },
142 { Opt_fscache_uniq, "fsc=%s" },
143 { Opt_nofscache, "nofsc" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400144
Chuck Levera5a16ba2009-06-17 18:02:14 -0700145 { Opt_port, "port=%s" },
146 { Opt_rsize, "rsize=%s" },
147 { Opt_wsize, "wsize=%s" },
148 { Opt_bsize, "bsize=%s" },
149 { Opt_timeo, "timeo=%s" },
150 { Opt_retrans, "retrans=%s" },
151 { Opt_acregmin, "acregmin=%s" },
152 { Opt_acregmax, "acregmax=%s" },
153 { Opt_acdirmin, "acdirmin=%s" },
154 { Opt_acdirmax, "acdirmax=%s" },
155 { Opt_actimeo, "actimeo=%s" },
156 { Opt_namelen, "namlen=%s" },
157 { Opt_mountport, "mountport=%s" },
158 { Opt_mountvers, "mountvers=%s" },
159 { Opt_nfsvers, "nfsvers=%s" },
160 { Opt_nfsvers, "vers=%s" },
Chuck Leverf3f4f4e2009-08-09 15:09:29 -0400161 { Opt_minorversion, "minorversion=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400162
163 { Opt_sec, "sec=%s" },
164 { Opt_proto, "proto=%s" },
165 { Opt_mountproto, "mountproto=%s" },
166 { Opt_addr, "addr=%s" },
167 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500168 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400169 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400170
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400171 { Opt_lookupcache, "lookupcache=%s" },
172
Chuck Leverbf0fd762007-07-01 12:13:44 -0400173 { Opt_err, NULL }
174};
175
176enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400177 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400178
179 Opt_xprt_err
180};
181
Steven Whitehousea447c092008-10-13 10:46:57 +0100182static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400183 { Opt_xprt_udp, "udp" },
184 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400185 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400186
187 { Opt_xprt_err, NULL }
188};
189
190enum {
191 Opt_sec_none, Opt_sec_sys,
192 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
193 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
194 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
195
196 Opt_sec_err
197};
198
Steven Whitehousea447c092008-10-13 10:46:57 +0100199static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400200 { Opt_sec_none, "none" },
201 { Opt_sec_none, "null" },
202 { Opt_sec_sys, "sys" },
203
204 { Opt_sec_krb5, "krb5" },
205 { Opt_sec_krb5i, "krb5i" },
206 { Opt_sec_krb5p, "krb5p" },
207
208 { Opt_sec_lkey, "lkey" },
209 { Opt_sec_lkeyi, "lkeyi" },
210 { Opt_sec_lkeyp, "lkeyp" },
211
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500212 { Opt_sec_spkm, "spkm3" },
213 { Opt_sec_spkmi, "spkm3i" },
214 { Opt_sec_spkmp, "spkm3p" },
215
Chuck Leverbf0fd762007-07-01 12:13:44 -0400216 { Opt_sec_err, NULL }
217};
218
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400219enum {
220 Opt_lookupcache_all, Opt_lookupcache_positive,
221 Opt_lookupcache_none,
222
223 Opt_lookupcache_err
224};
225
226static match_table_t nfs_lookupcache_tokens = {
227 { Opt_lookupcache_all, "all" },
228 { Opt_lookupcache_positive, "pos" },
229 { Opt_lookupcache_positive, "positive" },
230 { Opt_lookupcache_none, "none" },
231
232 { Opt_lookupcache_err, NULL }
233};
234
Chuck Leverbf0fd762007-07-01 12:13:44 -0400235
Al Viro42faad92008-04-24 07:21:56 -0400236static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400237static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400238static int nfs_show_options(struct seq_file *, struct vfsmount *);
239static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400240static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400241static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400242 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400243static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400244static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400245
246static struct file_system_type nfs_fs_type = {
247 .owner = THIS_MODULE,
248 .name = "nfs",
249 .get_sb = nfs_get_sb,
250 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700251 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400252};
253
David Howells54ceac42006-08-22 20:06:13 -0400254struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400255 .owner = THIS_MODULE,
256 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400257 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400258 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700259 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400260};
261
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800262static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400263 .alloc_inode = nfs_alloc_inode,
264 .destroy_inode = nfs_destroy_inode,
265 .write_inode = nfs_write_inode,
266 .statfs = nfs_statfs,
267 .clear_inode = nfs_clear_inode,
268 .umount_begin = nfs_umount_begin,
269 .show_options = nfs_show_options,
270 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400271 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400272};
273
274#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400275static int nfs4_get_sb(struct file_system_type *fs_type,
276 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400277static int nfs4_remote_get_sb(struct file_system_type *fs_type,
278 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400279static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
280 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
281static int nfs4_referral_get_sb(struct file_system_type *fs_type,
282 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400283static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
284 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400285static void nfs4_kill_super(struct super_block *sb);
286
287static struct file_system_type nfs4_fs_type = {
288 .owner = THIS_MODULE,
289 .name = "nfs4",
290 .get_sb = nfs4_get_sb,
291 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700292 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400293};
294
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400295static struct file_system_type nfs4_remote_fs_type = {
296 .owner = THIS_MODULE,
297 .name = "nfs4",
298 .get_sb = nfs4_remote_get_sb,
299 .kill_sb = nfs4_kill_super,
300 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
301};
302
David Howells54ceac42006-08-22 20:06:13 -0400303struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400304 .owner = THIS_MODULE,
305 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400306 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400307 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700308 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400309};
310
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400311static struct file_system_type nfs4_remote_referral_fs_type = {
312 .owner = THIS_MODULE,
313 .name = "nfs4",
314 .get_sb = nfs4_remote_referral_get_sb,
315 .kill_sb = nfs4_kill_super,
316 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
317};
318
David Howells54ceac42006-08-22 20:06:13 -0400319struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400320 .owner = THIS_MODULE,
321 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400322 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400323 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700324 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400325};
326
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800327static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400328 .alloc_inode = nfs_alloc_inode,
329 .destroy_inode = nfs_destroy_inode,
330 .write_inode = nfs_write_inode,
331 .statfs = nfs_statfs,
332 .clear_inode = nfs4_clear_inode,
333 .umount_begin = nfs_umount_begin,
334 .show_options = nfs_show_options,
335 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400336 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400337};
338#endif
339
Rusty Russell8e1f9362007-07-17 04:03:17 -0700340static struct shrinker acl_shrinker = {
341 .shrink = nfs_access_cache_shrinker,
342 .seeks = DEFAULT_SEEKS,
343};
Trond Myklebust979df722006-07-25 11:28:19 -0400344
David Howellsf7b422b2006-06-09 09:34:33 -0400345/*
346 * Register the NFS filesystems
347 */
348int __init register_nfs_fs(void)
349{
350 int ret;
351
352 ret = register_filesystem(&nfs_fs_type);
353 if (ret < 0)
354 goto error_0;
355
David Howellsf7b422b2006-06-09 09:34:33 -0400356 ret = nfs_register_sysctl();
357 if (ret < 0)
358 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800359#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400360 ret = register_filesystem(&nfs4_fs_type);
361 if (ret < 0)
362 goto error_2;
363#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700364 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400365 return 0;
366
367#ifdef CONFIG_NFS_V4
368error_2:
369 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800370#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400371error_1:
372 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400373error_0:
374 return ret;
375}
376
377/*
378 * Unregister the NFS filesystems
379 */
380void __exit unregister_nfs_fs(void)
381{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700382 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400383#ifdef CONFIG_NFS_V4
384 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400385#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700386 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400387 unregister_filesystem(&nfs_fs_type);
388}
389
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400390void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500391{
392 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400393
394 if (atomic_inc_return(&server->active) == 1)
395 atomic_inc(&sb->s_active);
396}
397
398void nfs_sb_deactive(struct super_block *sb)
399{
400 struct nfs_server *server = NFS_SB(sb);
401
402 if (atomic_dec_and_test(&server->active))
403 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500404}
405
David Howellsf7b422b2006-06-09 09:34:33 -0400406/*
407 * Deliver file system statistics to userspace
408 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400409static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400410{
David Howells0c7d90c2006-08-22 20:06:10 -0400411 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400412 unsigned char blockbits;
413 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400414 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400415 struct nfs_fattr fattr;
416 struct nfs_fsstat res = {
417 .fattr = &fattr,
418 };
419 int error;
420
David Howells8fa5c002006-08-22 20:06:12 -0400421 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400422 if (error < 0)
423 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700424 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400425
426 /*
427 * Current versions of glibc do not correctly handle the
428 * case where f_frsize != f_bsize. Eventually we want to
429 * report the value of wtmult in this field.
430 */
David Howells0c7d90c2006-08-22 20:06:10 -0400431 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400432
433 /*
434 * On most *nix systems, f_blocks, f_bfree, and f_bavail
435 * are reported in units of f_frsize. Linux hasn't had
436 * an f_frsize field in its statfs struct until recently,
437 * thus historically Linux's sys_statfs reports these
438 * fields in units of f_bsize.
439 */
David Howells0c7d90c2006-08-22 20:06:10 -0400440 buf->f_bsize = dentry->d_sb->s_blocksize;
441 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400442 blockres = (1 << blockbits) - 1;
443 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
444 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
445 buf->f_bavail = (res.abytes + blockres) >> blockbits;
446
447 buf->f_files = res.tfiles;
448 buf->f_ffree = res.afiles;
449
450 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700451
David Howellsf7b422b2006-06-09 09:34:33 -0400452 return 0;
453
454 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700455 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700456 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400457}
458
David Howells7d4e2742006-08-22 20:06:07 -0400459/*
460 * Map the security flavour number to a name
461 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400462static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
463{
David Howells7d4e2742006-08-22 20:06:07 -0400464 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400465 rpc_authflavor_t flavour;
466 const char *str;
467 } sec_flavours[] = {
468 { RPC_AUTH_NULL, "null" },
469 { RPC_AUTH_UNIX, "sys" },
470 { RPC_AUTH_GSS_KRB5, "krb5" },
471 { RPC_AUTH_GSS_KRB5I, "krb5i" },
472 { RPC_AUTH_GSS_KRB5P, "krb5p" },
473 { RPC_AUTH_GSS_LKEY, "lkey" },
474 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
475 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
476 { RPC_AUTH_GSS_SPKM, "spkm" },
477 { RPC_AUTH_GSS_SPKMI, "spkmi" },
478 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400479 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400480 };
481 int i;
482
Chuck Lever4d81cd12007-07-01 12:12:40 -0400483 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400484 if (sec_flavours[i].flavour == flavour)
485 break;
486 }
487 return sec_flavours[i].str;
488}
489
Chuck Lever82d101d2008-03-14 14:10:37 -0400490static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
491 int showdefaults)
492{
493 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
494
495 switch (sap->sa_family) {
496 case AF_INET: {
497 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrisonbe859402008-10-31 00:56:28 -0700498 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400499 break;
500 }
501 case AF_INET6: {
502 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700503 seq_printf(m, ",mountaddr=%pI6", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400504 break;
505 }
506 default:
507 if (showdefaults)
508 seq_printf(m, ",mountaddr=unspecified");
509 }
510
511 if (nfss->mountd_version || showdefaults)
512 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
513 if (nfss->mountd_port || showdefaults)
514 seq_printf(m, ",mountport=%u", nfss->mountd_port);
515
516 switch (nfss->mountd_protocol) {
517 case IPPROTO_UDP:
518 seq_printf(m, ",mountproto=udp");
519 break;
520 case IPPROTO_TCP:
521 seq_printf(m, ",mountproto=tcp");
522 break;
523 default:
524 if (showdefaults)
525 seq_printf(m, ",mountproto=auto");
526 }
527}
528
David Howellsf7b422b2006-06-09 09:34:33 -0400529/*
530 * Describe the mount options in force on this server representation
531 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400532static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
533 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400534{
David Howells509de812006-08-22 20:06:11 -0400535 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400536 int flag;
David Howells509de812006-08-22 20:06:11 -0400537 const char *str;
538 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400539 } nfs_info[] = {
540 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400541 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400542 { NFS_MOUNT_NOCTO, ",nocto", "" },
543 { NFS_MOUNT_NOAC, ",noac", "" },
544 { NFS_MOUNT_NONLM, ",nolock", "" },
545 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400546 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500547 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
548 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400549 { 0, NULL, NULL }
550 };
David Howells509de812006-08-22 20:06:11 -0400551 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400552 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400553 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400554
Chuck Lever82d101d2008-03-14 14:10:37 -0400555 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400556 seq_printf(m, ",rsize=%u", nfss->rsize);
557 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400558 if (nfss->bsize != 0)
559 seq_printf(m, ",bsize=%u", nfss->bsize);
560 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400561 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400562 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400563 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400564 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400565 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400566 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400567 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400568 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400569 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
570 if (nfss->flags & nfs_infop->flag)
571 seq_puts(m, nfs_infop->str);
572 else
573 seq_puts(m, nfs_infop->nostr);
574 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400575 seq_printf(m, ",proto=%s",
576 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400577 if (version == 4) {
578 if (nfss->port != NFS_PORT)
579 seq_printf(m, ",port=%u", nfss->port);
580 } else
581 if (nfss->port)
582 seq_printf(m, ",port=%u", nfss->port);
583
Trond Myklebust33170232007-12-20 16:03:59 -0500584 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
585 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400586 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400587
588 if (version != 4)
589 nfs_show_mountd_options(m, nfss, showdefaults);
590
591#ifdef CONFIG_NFS_V4
592 if (clp->rpc_ops->version == 4)
593 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
594#endif
David Howellsb797cac2009-04-03 16:42:48 +0100595 if (nfss->options & NFS_OPTION_FSCACHE)
596 seq_printf(m, ",fsc");
David Howellsf7b422b2006-06-09 09:34:33 -0400597}
598
599/*
600 * Describe the mount options on this VFS mountpoint
601 */
602static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
603{
604 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
605
606 nfs_show_mount_options(m, nfss, 0);
607
Chuck Lever5d8515c2007-12-10 14:57:16 -0500608 seq_printf(m, ",addr=%s",
609 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
610 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400611
612 return 0;
613}
614
615/*
616 * Present statistical information for this VFS mountpoint
617 */
618static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
619{
620 int i, cpu;
621 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
622 struct rpc_auth *auth = nfss->client->cl_auth;
623 struct nfs_iostats totals = { };
624
625 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
626
627 /*
628 * Display all mount option settings
629 */
630 seq_printf(m, "\n\topts:\t");
631 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
632 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
633 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
634 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
635 nfs_show_mount_options(m, nfss, 1);
636
637 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
638
639 seq_printf(m, "\n\tcaps:\t");
640 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400641 seq_printf(m, ",wtmult=%u", nfss->wtmult);
642 seq_printf(m, ",dtsize=%u", nfss->dtsize);
643 seq_printf(m, ",bsize=%u", nfss->bsize);
644 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400645
646#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500647 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400648 seq_printf(m, "\n\tnfsv4:\t");
649 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
650 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
651 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
652 }
653#endif
654
655 /*
656 * Display security flavor in effect for this mount
657 */
Chuck Lever2d767432008-03-14 14:10:08 -0400658 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400659 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400660 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400661
662 /*
663 * Display superblock I/O counters
664 */
665 for_each_possible_cpu(cpu) {
666 struct nfs_iostats *stats;
667
668 preempt_disable();
669 stats = per_cpu_ptr(nfss->io_stats, cpu);
670
671 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
672 totals.events[i] += stats->events[i];
673 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
674 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100675#ifdef CONFIG_NFS_FSCACHE
676 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
677 totals.fscache[i] += stats->fscache[i];
678#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400679
680 preempt_enable();
681 }
682
683 seq_printf(m, "\n\tevents:\t");
684 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
685 seq_printf(m, "%lu ", totals.events[i]);
686 seq_printf(m, "\n\tbytes:\t");
687 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
688 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100689#ifdef CONFIG_NFS_FSCACHE
690 if (nfss->options & NFS_OPTION_FSCACHE) {
691 seq_printf(m, "\n\tfsc:\t");
692 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
693 seq_printf(m, "%Lu ", totals.bytes[i]);
694 }
695#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400696 seq_printf(m, "\n");
697
698 rpc_print_iostats(m, nfss->client);
699
700 return 0;
701}
702
703/*
704 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400705 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400706 */
Al Viro42faad92008-04-24 07:21:56 -0400707static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400708{
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200709 struct nfs_server *server;
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400710 struct rpc_clnt *rpc;
711
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200712 lock_kernel();
713
714 server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400715 /* -EIO all pending I/O */
716 rpc = server->client_acl;
717 if (!IS_ERR(rpc))
718 rpc_killall_tasks(rpc);
719 rpc = server->client;
720 if (!IS_ERR(rpc))
721 rpc_killall_tasks(rpc);
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200722
723 unlock_kernel();
David Howellsf7b422b2006-06-09 09:34:33 -0400724}
725
726/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500727 * Sanity-check a server address provided by the mount command.
728 *
729 * Address family must be initialized, and address must not be
730 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400731 */
732static int nfs_verify_server_address(struct sockaddr *addr)
733{
734 switch (addr->sa_family) {
735 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500736 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700737 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500738 }
739 case AF_INET6: {
740 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
741 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400742 }
743 }
744
745 return 0;
746}
747
Chuck Leverce3b7e12008-06-23 12:36:53 -0400748static void nfs_parse_ipv4_address(char *string, size_t str_len,
749 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500750{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400751 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
752 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500753
Chuck Leverce3b7e12008-06-23 12:36:53 -0400754 if (str_len <= INET_ADDRSTRLEN) {
755 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
756 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500757
Chuck Leverce3b7e12008-06-23 12:36:53 -0400758 sin->sin_family = AF_INET;
759 *addr_len = sizeof(*sin);
760 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500761 return;
762 }
Chuck Lever9412b922007-12-10 14:59:21 -0500763
764 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400765 *addr_len = 0;
766}
767
768#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400769static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
770 const char *delim,
771 struct sockaddr_in6 *sin6)
Chuck Leverd8e77482008-06-23 12:37:01 -0400772{
773 char *p;
774 size_t len;
775
Chuck Lever5e2e7722008-10-08 15:38:10 -0400776 if ((string + str_len) == delim)
777 return 1;
778
Chuck Leverd8e77482008-06-23 12:37:01 -0400779 if (*delim != IPV6_SCOPE_DELIMITER)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400780 return 0;
781
782 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
783 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400784
785 len = (string + str_len) - delim - 1;
786 p = kstrndup(delim + 1, len, GFP_KERNEL);
787 if (p) {
788 unsigned long scope_id = 0;
789 struct net_device *dev;
790
791 dev = dev_get_by_name(&init_net, p);
792 if (dev != NULL) {
793 scope_id = dev->ifindex;
794 dev_put(dev);
795 } else {
Chuck Lever5e2e7722008-10-08 15:38:10 -0400796 if (strict_strtoul(p, 10, &scope_id) == 0) {
797 kfree(p);
798 return 0;
799 }
Chuck Leverd8e77482008-06-23 12:37:01 -0400800 }
801
802 kfree(p);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400803
Chuck Leverd8e77482008-06-23 12:37:01 -0400804 sin6->sin6_scope_id = scope_id;
805 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400806 return 1;
Chuck Leverd8e77482008-06-23 12:37:01 -0400807 }
Chuck Lever5e2e7722008-10-08 15:38:10 -0400808
809 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400810}
811
Chuck Leverce3b7e12008-06-23 12:36:53 -0400812static void nfs_parse_ipv6_address(char *string, size_t str_len,
813 struct sockaddr *sap, size_t *addr_len)
814{
815 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
816 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400817 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400818
819 if (str_len <= INET6_ADDRSTRLEN) {
820 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
821 (int)str_len, string);
822
823 sin6->sin6_family = AF_INET6;
824 *addr_len = sizeof(*sin6);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400825 if (in6_pton(string, str_len, addr,
826 IPV6_SCOPE_DELIMITER, &delim) != 0) {
827 if (nfs_parse_ipv6_scope_id(string, str_len,
828 delim, sin6) != 0)
829 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400830 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400831 }
832
833 sap->sa_family = AF_UNSPEC;
834 *addr_len = 0;
835}
836#else
837static void nfs_parse_ipv6_address(char *string, size_t str_len,
838 struct sockaddr *sap, size_t *addr_len)
839{
840 sap->sa_family = AF_UNSPEC;
841 *addr_len = 0;
842}
843#endif
844
845/*
846 * Construct a sockaddr based on the contents of a string that contains
847 * an IP address in presentation format.
848 *
849 * If there is a problem constructing the new sockaddr, set the address
850 * family to AF_UNSPEC.
851 */
J. Bruce Fieldsea31a4432008-08-20 16:10:23 -0400852void nfs_parse_ip_address(char *string, size_t str_len,
Chuck Leverce3b7e12008-06-23 12:36:53 -0400853 struct sockaddr *sap, size_t *addr_len)
854{
855 unsigned int i, colons;
856
857 colons = 0;
858 for (i = 0; i < str_len; i++)
859 if (string[i] == ':')
860 colons++;
861
862 if (colons >= 2)
863 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
864 else
865 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500866}
867
868/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400869 * Sanity check the NFS transport protocol.
870 *
871 */
872static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
873{
874 switch (mnt->nfs_server.protocol) {
875 case XPRT_TRANSPORT_UDP:
876 case XPRT_TRANSPORT_TCP:
877 case XPRT_TRANSPORT_RDMA:
878 break;
879 default:
880 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
881 }
882}
883
884/*
885 * For text based NFSv2/v3 mounts, the mount protocol transport default
886 * settings should depend upon the specified NFS transport.
887 */
888static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
889{
890 nfs_validate_transport_protocol(mnt);
891
892 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
893 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
894 return;
895 switch (mnt->nfs_server.protocol) {
896 case XPRT_TRANSPORT_UDP:
897 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
898 break;
899 case XPRT_TRANSPORT_TCP:
900 case XPRT_TRANSPORT_RDMA:
901 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
902 }
903}
904
905/*
Chuck Lever01060c82008-06-24 16:33:38 -0400906 * Parse the value of the 'sec=' option.
Chuck Lever01060c82008-06-24 16:33:38 -0400907 */
908static int nfs_parse_security_flavors(char *value,
909 struct nfs_parsed_mount_data *mnt)
910{
911 substring_t args[MAX_OPT_ARGS];
912
913 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
914
915 switch (match_token(value, nfs_secflavor_tokens, args)) {
916 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400917 mnt->auth_flavors[0] = RPC_AUTH_NULL;
918 break;
919 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400920 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
921 break;
922 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400923 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
924 break;
925 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400926 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
927 break;
928 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400929 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
930 break;
931 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400932 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
933 break;
934 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400935 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
936 break;
937 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400938 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
939 break;
940 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400941 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
942 break;
943 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400944 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
945 break;
946 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400947 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
948 break;
949 default:
950 return 0;
951 }
952
Chuck Lever059f90b2009-08-09 15:09:31 -0400953 mnt->auth_flavor_len = 1;
Chuck Lever01060c82008-06-24 16:33:38 -0400954 return 1;
955}
956
957/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400958 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400959 * a data structure. The whole mount string is processed; bad options are
960 * skipped as they are encountered. If there were no errors, return 1;
961 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400962 */
963static int nfs_parse_mount_options(char *raw,
964 struct nfs_parsed_mount_data *mnt)
965{
Eric Parisf9c3a382008-03-05 14:20:18 -0500966 char *p, *string, *secdata;
Chuck Leverd23c45f2009-06-17 18:02:13 -0700967 int rc, sloppy = 0, invalid_option = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400968
969 if (!raw) {
970 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
971 return 1;
972 }
973 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
974
Eric Parisf9c3a382008-03-05 14:20:18 -0500975 secdata = alloc_secdata();
976 if (!secdata)
977 goto out_nomem;
978
979 rc = security_sb_copy_data(raw, secdata);
980 if (rc)
981 goto out_security_failure;
982
983 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
984 if (rc)
985 goto out_security_failure;
986
987 free_secdata(secdata);
988
Chuck Leverbf0fd762007-07-01 12:13:44 -0400989 while ((p = strsep(&raw, ",")) != NULL) {
990 substring_t args[MAX_OPT_ARGS];
Chuck Levera5a16ba2009-06-17 18:02:14 -0700991 unsigned long option;
992 int token;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400993
994 if (!*p)
995 continue;
996
997 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
998
999 token = match_token(p, nfs_mount_option_tokens, args);
1000 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001001
1002 /*
1003 * boolean options: foo/nofoo
1004 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001005 case Opt_soft:
1006 mnt->flags |= NFS_MOUNT_SOFT;
1007 break;
1008 case Opt_hard:
1009 mnt->flags &= ~NFS_MOUNT_SOFT;
1010 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001011 case Opt_posix:
1012 mnt->flags |= NFS_MOUNT_POSIX;
1013 break;
1014 case Opt_noposix:
1015 mnt->flags &= ~NFS_MOUNT_POSIX;
1016 break;
1017 case Opt_cto:
1018 mnt->flags &= ~NFS_MOUNT_NOCTO;
1019 break;
1020 case Opt_nocto:
1021 mnt->flags |= NFS_MOUNT_NOCTO;
1022 break;
1023 case Opt_ac:
1024 mnt->flags &= ~NFS_MOUNT_NOAC;
1025 break;
1026 case Opt_noac:
1027 mnt->flags |= NFS_MOUNT_NOAC;
1028 break;
1029 case Opt_lock:
1030 mnt->flags &= ~NFS_MOUNT_NONLM;
1031 break;
1032 case Opt_nolock:
1033 mnt->flags |= NFS_MOUNT_NONLM;
1034 break;
1035 case Opt_v2:
1036 mnt->flags &= ~NFS_MOUNT_VER3;
1037 break;
1038 case Opt_v3:
1039 mnt->flags |= NFS_MOUNT_VER3;
1040 break;
1041 case Opt_udp:
1042 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001043 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001044 break;
1045 case Opt_tcp:
1046 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001047 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001048 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001049 case Opt_rdma:
1050 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1051 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001052 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001053 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001054 case Opt_acl:
1055 mnt->flags &= ~NFS_MOUNT_NOACL;
1056 break;
1057 case Opt_noacl:
1058 mnt->flags |= NFS_MOUNT_NOACL;
1059 break;
1060 case Opt_rdirplus:
1061 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1062 break;
1063 case Opt_nordirplus:
1064 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1065 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001066 case Opt_sharecache:
1067 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1068 break;
1069 case Opt_nosharecache:
1070 mnt->flags |= NFS_MOUNT_UNSHARED;
1071 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001072 case Opt_resvport:
1073 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1074 break;
1075 case Opt_noresvport:
1076 mnt->flags |= NFS_MOUNT_NORESVPORT;
1077 break;
David Howellsb797cac2009-04-03 16:42:48 +01001078 case Opt_fscache:
1079 mnt->options |= NFS_OPTION_FSCACHE;
1080 kfree(mnt->fscache_uniq);
1081 mnt->fscache_uniq = NULL;
1082 break;
1083 case Opt_nofscache:
1084 mnt->options &= ~NFS_OPTION_FSCACHE;
1085 kfree(mnt->fscache_uniq);
1086 mnt->fscache_uniq = NULL;
1087 break;
1088 case Opt_fscache_uniq:
1089 string = match_strdup(args);
1090 if (!string)
1091 goto out_nomem;
1092 kfree(mnt->fscache_uniq);
1093 mnt->fscache_uniq = string;
1094 mnt->options |= NFS_OPTION_FSCACHE;
1095 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001096
Chuck Leverf45663c2008-06-24 19:28:02 -04001097 /*
1098 * options that take numeric values
1099 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001100 case Opt_port:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001101 string = match_strdup(args);
1102 if (string == NULL)
1103 goto out_nomem;
1104 rc = strict_strtoul(string, 10, &option);
1105 kfree(string);
1106 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001107 goto out_invalid_value;
1108 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001109 break;
1110 case Opt_rsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001111 string = match_strdup(args);
1112 if (string == NULL)
1113 goto out_nomem;
1114 rc = strict_strtoul(string, 10, &option);
1115 kfree(string);
1116 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001117 goto out_invalid_value;
1118 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001119 break;
1120 case Opt_wsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001121 string = match_strdup(args);
1122 if (string == NULL)
1123 goto out_nomem;
1124 rc = strict_strtoul(string, 10, &option);
1125 kfree(string);
1126 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001127 goto out_invalid_value;
1128 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001129 break;
1130 case Opt_bsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001131 string = match_strdup(args);
1132 if (string == NULL)
1133 goto out_nomem;
1134 rc = strict_strtoul(string, 10, &option);
1135 kfree(string);
1136 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001137 goto out_invalid_value;
1138 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001139 break;
1140 case Opt_timeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001141 string = match_strdup(args);
1142 if (string == NULL)
1143 goto out_nomem;
1144 rc = strict_strtoul(string, 10, &option);
1145 kfree(string);
1146 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001147 goto out_invalid_value;
1148 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001149 break;
1150 case Opt_retrans:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001151 string = match_strdup(args);
1152 if (string == NULL)
1153 goto out_nomem;
1154 rc = strict_strtoul(string, 10, &option);
1155 kfree(string);
1156 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001157 goto out_invalid_value;
1158 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001159 break;
1160 case Opt_acregmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001161 string = match_strdup(args);
1162 if (string == NULL)
1163 goto out_nomem;
1164 rc = strict_strtoul(string, 10, &option);
1165 kfree(string);
1166 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001167 goto out_invalid_value;
1168 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001169 break;
1170 case Opt_acregmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001171 string = match_strdup(args);
1172 if (string == NULL)
1173 goto out_nomem;
1174 rc = strict_strtoul(string, 10, &option);
1175 kfree(string);
1176 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001177 goto out_invalid_value;
1178 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001179 break;
1180 case Opt_acdirmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001181 string = match_strdup(args);
1182 if (string == NULL)
1183 goto out_nomem;
1184 rc = strict_strtoul(string, 10, &option);
1185 kfree(string);
1186 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001187 goto out_invalid_value;
1188 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001189 break;
1190 case Opt_acdirmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001191 string = match_strdup(args);
1192 if (string == NULL)
1193 goto out_nomem;
1194 rc = strict_strtoul(string, 10, &option);
1195 kfree(string);
1196 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001197 goto out_invalid_value;
1198 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001199 break;
1200 case Opt_actimeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001201 string = match_strdup(args);
1202 if (string == NULL)
1203 goto out_nomem;
1204 rc = strict_strtoul(string, 10, &option);
1205 kfree(string);
1206 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001207 goto out_invalid_value;
1208 mnt->acregmin = mnt->acregmax =
1209 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001210 break;
1211 case Opt_namelen:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001212 string = match_strdup(args);
1213 if (string == NULL)
1214 goto out_nomem;
1215 rc = strict_strtoul(string, 10, &option);
1216 kfree(string);
1217 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001218 goto out_invalid_value;
1219 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001220 break;
1221 case Opt_mountport:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001222 string = match_strdup(args);
1223 if (string == NULL)
1224 goto out_nomem;
1225 rc = strict_strtoul(string, 10, &option);
1226 kfree(string);
1227 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001228 goto out_invalid_value;
1229 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001230 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001231 case Opt_mountvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001232 string = match_strdup(args);
1233 if (string == NULL)
1234 goto out_nomem;
1235 rc = strict_strtoul(string, 10, &option);
1236 kfree(string);
1237 if (rc != 0 ||
Chuck Leverf45663c2008-06-24 19:28:02 -04001238 option < NFS_MNT_VERSION ||
Chuck Leverd23c45f2009-06-17 18:02:13 -07001239 option > NFS_MNT3_VERSION)
1240 goto out_invalid_value;
1241 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001242 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001243 case Opt_nfsvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001244 string = match_strdup(args);
1245 if (string == NULL)
1246 goto out_nomem;
1247 rc = strict_strtoul(string, 10, &option);
1248 kfree(string);
1249 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001250 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001251 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001252 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001253 mnt->flags &= ~NFS_MOUNT_VER3;
1254 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001255 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001256 mnt->flags |= NFS_MOUNT_VER3;
1257 break;
1258 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001259 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001260 }
1261 break;
Mike Sager3fd5be92009-04-01 09:21:48 -04001262 case Opt_minorversion:
Chuck Leverf3f4f4e2009-08-09 15:09:29 -04001263 string = match_strdup(args);
1264 if (string == NULL)
1265 goto out_nomem;
1266 rc = strict_strtoul(string, 10, &option);
1267 kfree(string);
1268 if (rc != 0)
1269 goto out_invalid_value;
1270 if (option > NFS4_MAX_MINOR_VERSION)
1271 goto out_invalid_value;
1272 mnt->minorversion = option;
Mike Sager3fd5be92009-04-01 09:21:48 -04001273 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001274
Chuck Leverf45663c2008-06-24 19:28:02 -04001275 /*
1276 * options that take text values
1277 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001278 case Opt_sec:
1279 string = match_strdup(args);
1280 if (string == NULL)
1281 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001282 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001283 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001284 if (!rc) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001285 dfprintk(MOUNT, "NFS: unrecognized "
1286 "security flavor\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001287 return 0;
Chuck Leverf45663c2008-06-24 19:28:02 -04001288 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001289 break;
1290 case Opt_proto:
1291 string = match_strdup(args);
1292 if (string == NULL)
1293 goto out_nomem;
1294 token = match_token(string,
1295 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001296
1297 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001298 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001299 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001300 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001301 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001302 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001303 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001304 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001305 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001306 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001307 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001308 case Opt_xprt_rdma:
1309 /* vector side protocols to TCP */
1310 mnt->flags |= NFS_MOUNT_TCP;
1311 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001312 xprt_load_transport(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001313 kfree(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001314 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001315 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001316 dfprintk(MOUNT, "NFS: unrecognized "
1317 "transport protocol\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001318 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001319 }
1320 break;
1321 case Opt_mountproto:
1322 string = match_strdup(args);
1323 if (string == NULL)
1324 goto out_nomem;
1325 token = match_token(string,
1326 nfs_xprt_protocol_tokens, args);
Trond Myklebustd508afb2009-04-06 16:41:35 -07001327 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001328
1329 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001330 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001331 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001332 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001333 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001334 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001335 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001336 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001337 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001338 dfprintk(MOUNT, "NFS: unrecognized "
1339 "transport protocol\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001340 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001341 }
1342 break;
1343 case Opt_addr:
1344 string = match_strdup(args);
1345 if (string == NULL)
1346 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001347 nfs_parse_ip_address(string, strlen(string),
1348 (struct sockaddr *)
1349 &mnt->nfs_server.address,
1350 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001351 kfree(string);
1352 break;
1353 case Opt_clientaddr:
1354 string = match_strdup(args);
1355 if (string == NULL)
1356 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001357 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001358 mnt->client_address = string;
1359 break;
Chuck Lever33832032007-12-10 14:59:13 -05001360 case Opt_mounthost:
1361 string = match_strdup(args);
1362 if (string == NULL)
1363 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001364 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001365 mnt->mount_server.hostname = string;
1366 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001367 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001368 string = match_strdup(args);
1369 if (string == NULL)
1370 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001371 nfs_parse_ip_address(string, strlen(string),
1372 (struct sockaddr *)
1373 &mnt->mount_server.address,
1374 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001375 kfree(string);
1376 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001377 case Opt_lookupcache:
1378 string = match_strdup(args);
1379 if (string == NULL)
1380 goto out_nomem;
1381 token = match_token(string,
1382 nfs_lookupcache_tokens, args);
1383 kfree(string);
1384 switch (token) {
1385 case Opt_lookupcache_all:
1386 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1387 break;
1388 case Opt_lookupcache_positive:
1389 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1390 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1391 break;
1392 case Opt_lookupcache_none:
1393 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1394 break;
1395 default:
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001396 dfprintk(MOUNT, "NFS: invalid "
1397 "lookupcache argument\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001398 return 0;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001399 };
1400 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001401
Chuck Leverf45663c2008-06-24 19:28:02 -04001402 /*
1403 * Special options
1404 */
1405 case Opt_sloppy:
1406 sloppy = 1;
1407 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1408 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001409 case Opt_userspace:
1410 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001411 dfprintk(MOUNT, "NFS: ignoring mount option "
1412 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001413 break;
1414
1415 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001416 invalid_option = 1;
Chuck Leverf45663c2008-06-24 19:28:02 -04001417 dfprintk(MOUNT, "NFS: unrecognized mount option "
1418 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001419 }
1420 }
1421
Chuck Leverd23c45f2009-06-17 18:02:13 -07001422 if (!sloppy && invalid_option)
1423 return 0;
1424
Chuck Leverbf0fd762007-07-01 12:13:44 -04001425 return 1;
1426
Chuck Leverd23c45f2009-06-17 18:02:13 -07001427out_invalid_value:
1428 printk(KERN_INFO "NFS: bad mount option value specified: %s \n", p);
1429 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001430out_nomem:
1431 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1432 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001433out_security_failure:
1434 free_secdata(secdata);
1435 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1436 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001437}
1438
1439/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001440 * Use the remote server's MOUNT service to request the NFS file handle
1441 * corresponding to the provided path.
1442 */
1443static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1444 struct nfs_fh *root_fh)
1445{
Chuck Lever8e02f6b2009-06-17 18:02:13 -07001446 unsigned int auth_flavor_len = 0;
Chuck Leverc5d120f2008-12-23 15:21:35 -05001447 struct nfs_mount_request request = {
1448 .sap = (struct sockaddr *)
1449 &args->mount_server.address,
1450 .dirpath = args->nfs_server.export_path,
1451 .protocol = args->mount_server.protocol,
1452 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001453 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Lever8e02f6b2009-06-17 18:02:13 -07001454 .auth_flav_len = &auth_flavor_len,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001455 };
Chuck Lever4c568012007-12-10 14:59:28 -05001456 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001457
1458 if (args->mount_server.version == 0) {
1459 if (args->flags & NFS_MOUNT_VER3)
1460 args->mount_server.version = NFS_MNT3_VERSION;
1461 else
1462 args->mount_server.version = NFS_MNT_VERSION;
1463 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001464 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001465
Chuck Lever33832032007-12-10 14:59:13 -05001466 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001467 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001468 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001469 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001470
Chuck Lever0076d7b2007-07-01 12:13:49 -04001471 /*
1472 * Construct the mount server's address.
1473 */
Chuck Lever4c568012007-12-10 14:59:28 -05001474 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001475 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001476 args->nfs_server.addrlen);
1477 args->mount_server.addrlen = args->nfs_server.addrlen;
1478 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001479 request.salen = args->mount_server.addrlen;
Chuck Lever4c568012007-12-10 14:59:28 -05001480
James Lentiniaad70002007-09-24 17:32:49 -04001481 /*
1482 * autobind will be used if mount_server.port == 0
1483 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001484 nfs_set_port(request.sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001485
1486 /*
1487 * Now ask the mount server to map our export path
1488 * to a file handle.
1489 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001490 status = nfs_mount(&request);
Chuck Leverefd83402007-09-11 18:00:58 -04001491 if (status == 0)
1492 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001493
Chuck Lever396cee972008-06-12 12:37:49 -04001494 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Leverc5d120f2008-12-23 15:21:35 -05001495 request.hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001496 return status;
1497}
1498
Chuck Leverd1aa0822008-06-23 12:36:45 -04001499static int nfs_parse_simple_hostname(const char *dev_name,
1500 char **hostname, size_t maxnamlen,
1501 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001502{
1503 size_t len;
1504 char *colon, *comma;
1505
1506 colon = strchr(dev_name, ':');
1507 if (colon == NULL)
1508 goto out_bad_devname;
1509
1510 len = colon - dev_name;
1511 if (len > maxnamlen)
1512 goto out_hostname;
1513
1514 /* N.B. caller will free nfs_server.hostname in all cases */
1515 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1516 if (!*hostname)
1517 goto out_nomem;
1518
1519 /* kill possible hostname list: not supported */
1520 comma = strchr(*hostname, ',');
1521 if (comma != NULL) {
1522 if (comma == *hostname)
1523 goto out_bad_devname;
1524 *comma = '\0';
1525 }
1526
1527 colon++;
1528 len = strlen(colon);
1529 if (len > maxpathlen)
1530 goto out_path;
1531 *export_path = kstrndup(colon, len, GFP_KERNEL);
1532 if (!*export_path)
1533 goto out_nomem;
1534
1535 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1536 return 0;
1537
1538out_bad_devname:
1539 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1540 return -EINVAL;
1541
1542out_nomem:
1543 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1544 return -ENOMEM;
1545
1546out_hostname:
1547 dfprintk(MOUNT, "NFS: server hostname too long\n");
1548 return -ENAMETOOLONG;
1549
1550out_path:
1551 dfprintk(MOUNT, "NFS: export pathname too long\n");
1552 return -ENAMETOOLONG;
1553}
1554
1555/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001556 * Hostname has square brackets around it because it contains one or
1557 * more colons. We look for the first closing square bracket, and a
1558 * colon must follow it.
1559 */
1560static int nfs_parse_protected_hostname(const char *dev_name,
1561 char **hostname, size_t maxnamlen,
1562 char **export_path, size_t maxpathlen)
1563{
1564 size_t len;
1565 char *start, *end;
1566
1567 start = (char *)(dev_name + 1);
1568
1569 end = strchr(start, ']');
1570 if (end == NULL)
1571 goto out_bad_devname;
1572 if (*(end + 1) != ':')
1573 goto out_bad_devname;
1574
1575 len = end - start;
1576 if (len > maxnamlen)
1577 goto out_hostname;
1578
1579 /* N.B. caller will free nfs_server.hostname in all cases */
1580 *hostname = kstrndup(start, len, GFP_KERNEL);
1581 if (*hostname == NULL)
1582 goto out_nomem;
1583
1584 end += 2;
1585 len = strlen(end);
1586 if (len > maxpathlen)
1587 goto out_path;
1588 *export_path = kstrndup(end, len, GFP_KERNEL);
1589 if (!*export_path)
1590 goto out_nomem;
1591
1592 return 0;
1593
1594out_bad_devname:
1595 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1596 return -EINVAL;
1597
1598out_nomem:
1599 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1600 return -ENOMEM;
1601
1602out_hostname:
1603 dfprintk(MOUNT, "NFS: server hostname too long\n");
1604 return -ENAMETOOLONG;
1605
1606out_path:
1607 dfprintk(MOUNT, "NFS: export pathname too long\n");
1608 return -ENAMETOOLONG;
1609}
1610
1611/*
1612 * Split "dev_name" into "hostname:export_path".
1613 *
1614 * The leftmost colon demarks the split between the server's hostname
1615 * and the export path. If the hostname starts with a left square
1616 * bracket, then it may contain colons.
1617 *
1618 * Note: caller frees hostname and export path, even on error.
1619 */
1620static int nfs_parse_devname(const char *dev_name,
1621 char **hostname, size_t maxnamlen,
1622 char **export_path, size_t maxpathlen)
1623{
1624 if (*dev_name == '[')
1625 return nfs_parse_protected_hostname(dev_name,
1626 hostname, maxnamlen,
1627 export_path, maxpathlen);
1628
1629 return nfs_parse_simple_hostname(dev_name,
1630 hostname, maxnamlen,
1631 export_path, maxpathlen);
1632}
1633
1634/*
David Howells54ceac42006-08-22 20:06:13 -04001635 * Validate the NFS2/NFS3 mount data
1636 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001637 *
1638 * For option strings, user space handles the following behaviors:
1639 *
1640 * + DNS: mapping server host name to IP address ("addr=" option)
1641 *
1642 * + failure mode: how to behave if a mount request can't be handled
1643 * immediately ("fg/bg" option)
1644 *
1645 * + retry: how often to retry a mount request ("retry=" option)
1646 *
1647 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1648 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001649 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001650static int nfs_validate_mount_data(void *options,
1651 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001652 struct nfs_fh *mntfh,
1653 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001654{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001655 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001656
Chuck Lever5df36e72007-07-01 12:12:56 -04001657 if (data == NULL)
1658 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001659
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001660 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1661 args->rsize = NFS_MAX_FILE_IO_SIZE;
1662 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001663 args->acregmin = NFS_DEF_ACREGMIN;
1664 args->acregmax = NFS_DEF_ACREGMAX;
1665 args->acdirmin = NFS_DEF_ACDIRMIN;
1666 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001667 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001668 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001669 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001670 args->auth_flavors[0] = RPC_AUTH_UNIX;
Chuck Lever059f90b2009-08-09 15:09:31 -04001671 args->auth_flavor_len = 1;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001672
David Howellsf7b422b2006-06-09 09:34:33 -04001673 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001674 case 1:
1675 data->namlen = 0;
1676 case 2:
1677 data->bsize = 0;
1678 case 3:
1679 if (data->flags & NFS_MOUNT_VER3)
1680 goto out_no_v3;
1681 data->root.size = NFS2_FHSIZE;
1682 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1683 case 4:
1684 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1685 goto out_no_sec;
1686 case 5:
1687 memset(data->context, 0, sizeof(data->context));
1688 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001689 if (data->flags & NFS_MOUNT_VER3) {
1690 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1691 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001692 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001693 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001694 mntfh->size = NFS2_FHSIZE;
1695
Chuck Lever5df36e72007-07-01 12:12:56 -04001696
1697 memcpy(mntfh->data, data->root.data, mntfh->size);
1698 if (mntfh->size < sizeof(mntfh->data))
1699 memset(mntfh->data + mntfh->size, 0,
1700 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001701
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001702 /*
1703 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1704 * can deal with.
1705 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001706 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001707 args->rsize = data->rsize;
1708 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001709 args->timeo = data->timeo;
1710 args->retrans = data->retrans;
1711 args->acregmin = data->acregmin;
1712 args->acregmax = data->acregmax;
1713 args->acdirmin = data->acdirmin;
1714 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001715
1716 memcpy(&args->nfs_server.address, &data->addr,
1717 sizeof(data->addr));
1718 args->nfs_server.addrlen = sizeof(data->addr);
1719 if (!nfs_verify_server_address((struct sockaddr *)
1720 &args->nfs_server.address))
1721 goto out_no_address;
1722
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001723 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001724 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001725 /* N.B. caller will free nfs_server.hostname in all cases */
1726 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1727 args->namlen = data->namlen;
1728 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001729
1730 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1731 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001732 if (!args->nfs_server.hostname)
1733 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001734
1735 /*
1736 * The legacy version 6 binary mount data from userspace has a
1737 * field used only to transport selinux information into the
1738 * the kernel. To continue to support that functionality we
1739 * have a touch of selinux knowledge here in the NFS code. The
1740 * userspace code converted context=blah to just blah so we are
1741 * converting back to the full string selinux understands.
1742 */
1743 if (data->context[0]){
1744#ifdef CONFIG_SECURITY_SELINUX
1745 int rc;
1746 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1747 if (!opts_str)
1748 return -ENOMEM;
1749 strcpy(opts_str, "context=");
1750 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1751 strcat(opts_str, &data->context[0]);
1752 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1753 kfree(opts_str);
1754 if (rc)
1755 return rc;
1756#else
1757 return -EINVAL;
1758#endif
1759 }
1760
Chuck Lever5df36e72007-07-01 12:12:56 -04001761 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001762 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001763 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001764
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001765 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001766 return -EINVAL;
1767
Chuck Lever6e88e062007-09-24 15:39:50 -04001768 if (!nfs_verify_server_address((struct sockaddr *)
1769 &args->nfs_server.address))
1770 goto out_no_address;
1771
Chuck Levered596a82008-06-26 17:47:05 -04001772 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1773 args->nfs_server.port);
1774
Trond Myklebust259875e2008-07-02 14:43:47 -04001775 nfs_set_mount_transport_protocol(args);
1776
Chuck Leverdc045892008-06-23 12:36:37 -04001777 status = nfs_parse_devname(dev_name,
1778 &args->nfs_server.hostname,
1779 PAGE_SIZE,
1780 &args->nfs_server.export_path,
1781 NFS_MAXPATHLEN);
1782 if (!status)
1783 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001784
Chuck Leverdc045892008-06-23 12:36:37 -04001785 kfree(args->nfs_server.export_path);
1786 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001787
Chuck Lever136d5582007-07-01 12:13:54 -04001788 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001789 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001790
Chuck Lever136d5582007-07-01 12:13:54 -04001791 break;
1792 }
David Howellsf7b422b2006-06-09 09:34:33 -04001793 }
David Howells54ceac42006-08-22 20:06:13 -04001794
David Howellsf7b422b2006-06-09 09:34:33 -04001795#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001796 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001797 goto out_v3_not_compiled;
1798#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001799
David Howells54ceac42006-08-22 20:06:13 -04001800 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001801
1802out_no_data:
1803 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1804 return -EINVAL;
1805
1806out_no_v3:
1807 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1808 data->version);
1809 return -EINVAL;
1810
1811out_no_sec:
1812 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1813 return -EINVAL;
1814
Chuck Lever5df36e72007-07-01 12:12:56 -04001815#ifndef CONFIG_NFS_V3
1816out_v3_not_compiled:
1817 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1818 return -EPROTONOSUPPORT;
1819#endif /* !CONFIG_NFS_V3 */
1820
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001821out_nomem:
1822 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1823 return -ENOMEM;
1824
Chuck Lever5df36e72007-07-01 12:12:56 -04001825out_no_address:
1826 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1827 return -EINVAL;
1828
1829out_invalid_fh:
1830 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1831 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001832}
1833
Jeff Layton48b605f2008-06-10 15:38:39 -04001834static int
1835nfs_compare_remount_data(struct nfs_server *nfss,
1836 struct nfs_parsed_mount_data *data)
1837{
1838 if (data->flags != nfss->flags ||
1839 data->rsize != nfss->rsize ||
1840 data->wsize != nfss->wsize ||
1841 data->retrans != nfss->client->cl_timeout->to_retries ||
1842 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1843 data->acregmin != nfss->acregmin / HZ ||
1844 data->acregmax != nfss->acregmax / HZ ||
1845 data->acdirmin != nfss->acdirmin / HZ ||
1846 data->acdirmax != nfss->acdirmax / HZ ||
1847 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1848 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1849 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1850 data->nfs_server.addrlen) != 0)
1851 return -EINVAL;
1852
1853 return 0;
1854}
1855
1856static int
1857nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1858{
1859 int error;
1860 struct nfs_server *nfss = sb->s_fs_info;
1861 struct nfs_parsed_mount_data *data;
1862 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1863 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001864 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001865
1866 /*
1867 * Userspace mount programs that send binary options generally send
1868 * them populated with default values. We have no way to know which
1869 * ones were explicitly specified. Fall back to legacy behavior and
1870 * just return success.
1871 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001872 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1873 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1874 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001875 return 0;
1876
1877 data = kzalloc(sizeof(*data), GFP_KERNEL);
1878 if (data == NULL)
1879 return -ENOMEM;
1880
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001881 lock_kernel();
Jeff Layton48b605f2008-06-10 15:38:39 -04001882 /* fill out struct with values from existing mount */
1883 data->flags = nfss->flags;
1884 data->rsize = nfss->rsize;
1885 data->wsize = nfss->wsize;
1886 data->retrans = nfss->client->cl_timeout->to_retries;
1887 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1888 data->acregmin = nfss->acregmin / HZ;
1889 data->acregmax = nfss->acregmax / HZ;
1890 data->acdirmin = nfss->acdirmin / HZ;
1891 data->acdirmax = nfss->acdirmax / HZ;
1892 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1893 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1894 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1895 data->nfs_server.addrlen);
1896
1897 /* overwrite those values with any that were specified */
1898 error = nfs_parse_mount_options((char *)options, data);
1899 if (error < 0)
1900 goto out;
1901
1902 /* compare new mount options with old ones */
1903 error = nfs_compare_remount_data(nfss, data);
1904out:
1905 kfree(data);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001906 unlock_kernel();
Jeff Layton48b605f2008-06-10 15:38:39 -04001907 return error;
1908}
1909
David Howells54ceac42006-08-22 20:06:13 -04001910/*
1911 * Initialise the common bits of the superblock
1912 */
1913static inline void nfs_initialise_sb(struct super_block *sb)
1914{
1915 struct nfs_server *server = NFS_SB(sb);
1916
1917 sb->s_magic = NFS_SUPER_MAGIC;
1918
1919 /* We probably want something more informative here */
1920 snprintf(sb->s_id, sizeof(sb->s_id),
1921 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1922
1923 if (sb->s_blocksize == 0)
1924 sb->s_blocksize = nfs_block_bits(server->wsize,
1925 &sb->s_blocksize_bits);
1926
1927 if (server->flags & NFS_MOUNT_NOAC)
1928 sb->s_flags |= MS_SYNCHRONOUS;
1929
1930 nfs_super_set_maxbytes(sb, server->maxfilesize);
1931}
1932
1933/*
1934 * Finish setting up an NFS2/3 superblock
1935 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001936static void nfs_fill_super(struct super_block *sb,
1937 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001938{
1939 struct nfs_server *server = NFS_SB(sb);
1940
1941 sb->s_blocksize_bits = 0;
1942 sb->s_blocksize = 0;
1943 if (data->bsize)
1944 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1945
1946 if (server->flags & NFS_MOUNT_VER3) {
1947 /* The VFS shouldn't apply the umask to mode bits. We will do
1948 * so ourselves when necessary.
1949 */
1950 sb->s_flags |= MS_POSIXACL;
1951 sb->s_time_gran = 1;
1952 }
1953
1954 sb->s_op = &nfs_sops;
1955 nfs_initialise_sb(sb);
1956}
1957
1958/*
1959 * Finish setting up a cloned NFS2/3 superblock
1960 */
1961static void nfs_clone_super(struct super_block *sb,
1962 const struct super_block *old_sb)
1963{
1964 struct nfs_server *server = NFS_SB(sb);
1965
1966 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1967 sb->s_blocksize = old_sb->s_blocksize;
1968 sb->s_maxbytes = old_sb->s_maxbytes;
1969
1970 if (server->flags & NFS_MOUNT_VER3) {
1971 /* The VFS shouldn't apply the umask to mode bits. We will do
1972 * so ourselves when necessary.
1973 */
1974 sb->s_flags |= MS_POSIXACL;
1975 sb->s_time_gran = 1;
1976 }
1977
1978 sb->s_op = old_sb->s_op;
1979 nfs_initialise_sb(sb);
1980}
1981
Trond Myklebust275a5d22007-05-16 16:53:28 -04001982static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1983{
1984 const struct nfs_server *a = s->s_fs_info;
1985 const struct rpc_clnt *clnt_a = a->client;
1986 const struct rpc_clnt *clnt_b = b->client;
1987
1988 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1989 goto Ebusy;
1990 if (a->nfs_client != b->nfs_client)
1991 goto Ebusy;
1992 if (a->flags != b->flags)
1993 goto Ebusy;
1994 if (a->wsize != b->wsize)
1995 goto Ebusy;
1996 if (a->rsize != b->rsize)
1997 goto Ebusy;
1998 if (a->acregmin != b->acregmin)
1999 goto Ebusy;
2000 if (a->acregmax != b->acregmax)
2001 goto Ebusy;
2002 if (a->acdirmin != b->acdirmin)
2003 goto Ebusy;
2004 if (a->acdirmax != b->acdirmax)
2005 goto Ebusy;
2006 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2007 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002008 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04002009Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002010 return 0;
2011}
2012
2013struct nfs_sb_mountdata {
2014 struct nfs_server *server;
2015 int mntflags;
2016};
2017
2018static int nfs_set_super(struct super_block *s, void *data)
2019{
2020 struct nfs_sb_mountdata *sb_mntdata = data;
2021 struct nfs_server *server = sb_mntdata->server;
2022 int ret;
2023
2024 s->s_flags = sb_mntdata->mntflags;
2025 s->s_fs_info = server;
2026 ret = set_anon_super(s, server);
2027 if (ret == 0)
2028 server->s_dev = s->s_dev;
2029 return ret;
2030}
2031
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002032static int nfs_compare_super_address(struct nfs_server *server1,
2033 struct nfs_server *server2)
2034{
2035 struct sockaddr *sap1, *sap2;
2036
2037 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2038 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2039
2040 if (sap1->sa_family != sap2->sa_family)
2041 return 0;
2042
2043 switch (sap1->sa_family) {
2044 case AF_INET: {
2045 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2046 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2047 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2048 return 0;
2049 if (sin1->sin_port != sin2->sin_port)
2050 return 0;
2051 break;
2052 }
2053 case AF_INET6: {
2054 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2055 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2056 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2057 return 0;
2058 if (sin1->sin6_port != sin2->sin6_port)
2059 return 0;
2060 break;
2061 }
2062 default:
2063 return 0;
2064 }
2065
2066 return 1;
2067}
2068
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002069static int nfs_compare_super(struct super_block *sb, void *data)
2070{
2071 struct nfs_sb_mountdata *sb_mntdata = data;
2072 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2073 int mntflags = sb_mntdata->mntflags;
2074
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002075 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002076 return 0;
2077 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2078 if (old->flags & NFS_MOUNT_UNSHARED)
2079 return 0;
2080 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2081 return 0;
2082 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002083}
2084
Miklos Szeredifa799752008-04-30 00:54:33 -07002085static int nfs_bdi_register(struct nfs_server *server)
2086{
2087 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2088}
2089
David Howells54ceac42006-08-22 20:06:13 -04002090static int nfs_get_sb(struct file_system_type *fs_type,
2091 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2092{
2093 struct nfs_server *server = NULL;
2094 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002095 struct nfs_parsed_mount_data *data;
2096 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002097 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002098 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002099 struct nfs_sb_mountdata sb_mntdata = {
2100 .mntflags = flags,
2101 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002102 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002103
Trond Myklebust33852a12008-06-19 14:20:11 -04002104 data = kzalloc(sizeof(*data), GFP_KERNEL);
2105 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2106 if (data == NULL || mntfh == NULL)
2107 goto out_free_fh;
2108
2109 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002110
David Howells54ceac42006-08-22 20:06:13 -04002111 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002112 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002113 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002114 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002115
2116 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002117 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002118 if (IS_ERR(server)) {
2119 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002120 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002121 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002122 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002123
Trond Myklebust75180df2007-05-16 16:53:28 -04002124 if (server->flags & NFS_MOUNT_UNSHARED)
2125 compare_super = NULL;
2126
David Howells54ceac42006-08-22 20:06:13 -04002127 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002128 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002129 if (IS_ERR(s)) {
2130 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002131 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002132 }
2133
David Howells54ceac42006-08-22 20:06:13 -04002134 if (s->s_fs_info != server) {
2135 nfs_free_server(server);
2136 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002137 } else {
2138 error = nfs_bdi_register(server);
2139 if (error)
2140 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002141 }
David Howells54ceac42006-08-22 20:06:13 -04002142
2143 if (!s->s_root) {
2144 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002145 nfs_fill_super(s, data);
David Howells08734042009-04-03 16:42:42 +01002146 nfs_fscache_get_super_cookie(s, data);
David Howells54ceac42006-08-22 20:06:13 -04002147 }
2148
Trond Myklebust33852a12008-06-19 14:20:11 -04002149 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002150 if (IS_ERR(mntroot)) {
2151 error = PTR_ERR(mntroot);
2152 goto error_splat_super;
2153 }
2154
Trond Myklebust33852a12008-06-19 14:20:11 -04002155 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002156 if (error)
2157 goto error_splat_root;
2158
David Howellsf7b422b2006-06-09 09:34:33 -04002159 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002160 mnt->mnt_sb = s;
2161 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002162 error = 0;
2163
2164out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002165 kfree(data->nfs_server.hostname);
2166 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002167 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002168 security_free_mnt_opts(&data->lsm_opts);
2169out_free_fh:
2170 kfree(mntfh);
2171 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002172 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002173
David Howells54ceac42006-08-22 20:06:13 -04002174out_err_nosb:
2175 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002176 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002177
Eric Parisf9c3a382008-03-05 14:20:18 -05002178error_splat_root:
2179 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002180error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002181 deactivate_locked_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002182 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002183}
2184
David Howells54ceac42006-08-22 20:06:13 -04002185/*
2186 * Destroy an NFS2/3 superblock
2187 */
David Howellsf7b422b2006-06-09 09:34:33 -04002188static void nfs_kill_super(struct super_block *s)
2189{
2190 struct nfs_server *server = NFS_SB(s);
2191
Miklos Szeredifa799752008-04-30 00:54:33 -07002192 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002193 kill_anon_super(s);
David Howells08734042009-04-03 16:42:42 +01002194 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002195 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002196}
2197
David Howells54ceac42006-08-22 20:06:13 -04002198/*
2199 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2200 */
2201static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2202 const char *dev_name, void *raw_data,
2203 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002204{
2205 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002206 struct super_block *s;
2207 struct nfs_server *server;
2208 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002209 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002210 struct nfs_sb_mountdata sb_mntdata = {
2211 .mntflags = flags,
2212 };
David Howells54ceac42006-08-22 20:06:13 -04002213 int error;
2214
2215 dprintk("--> nfs_xdev_get_sb()\n");
2216
2217 /* create a new volume representation */
2218 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2219 if (IS_ERR(server)) {
2220 error = PTR_ERR(server);
2221 goto out_err_noserver;
2222 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002223 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002224
Trond Myklebust75180df2007-05-16 16:53:28 -04002225 if (server->flags & NFS_MOUNT_UNSHARED)
2226 compare_super = NULL;
2227
David Howells54ceac42006-08-22 20:06:13 -04002228 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002229 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002230 if (IS_ERR(s)) {
2231 error = PTR_ERR(s);
2232 goto out_err_nosb;
2233 }
2234
2235 if (s->s_fs_info != server) {
2236 nfs_free_server(server);
2237 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002238 } else {
2239 error = nfs_bdi_register(server);
2240 if (error)
2241 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002242 }
2243
2244 if (!s->s_root) {
2245 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002246 nfs_clone_super(s, data->sb);
2247 }
2248
2249 mntroot = nfs_get_root(s, data->fh);
2250 if (IS_ERR(mntroot)) {
2251 error = PTR_ERR(mntroot);
2252 goto error_splat_super;
2253 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002254 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002255 dput(mntroot);
2256 error = -ESTALE;
2257 goto error_splat_super;
2258 }
David Howells54ceac42006-08-22 20:06:13 -04002259
2260 s->s_flags |= MS_ACTIVE;
2261 mnt->mnt_sb = s;
2262 mnt->mnt_root = mntroot;
2263
Eric Parisf9c3a382008-03-05 14:20:18 -05002264 /* clone any lsm security options from the parent to the new sb */
2265 security_sb_clone_mnt_opts(data->sb, s);
2266
David Howells54ceac42006-08-22 20:06:13 -04002267 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2268 return 0;
2269
2270out_err_nosb:
2271 nfs_free_server(server);
2272out_err_noserver:
2273 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2274 return error;
2275
2276error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002277 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002278 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2279 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002280}
2281
2282#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002283
2284/*
2285 * Finish setting up a cloned NFS4 superblock
2286 */
2287static void nfs4_clone_super(struct super_block *sb,
2288 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002289{
David Howells54ceac42006-08-22 20:06:13 -04002290 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2291 sb->s_blocksize = old_sb->s_blocksize;
2292 sb->s_maxbytes = old_sb->s_maxbytes;
2293 sb->s_time_gran = 1;
2294 sb->s_op = old_sb->s_op;
2295 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002296}
2297
2298/*
2299 * Set up an NFS4 superblock
2300 */
David Howells54ceac42006-08-22 20:06:13 -04002301static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002302{
David Howellsf7b422b2006-06-09 09:34:33 -04002303 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002304 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002305 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002306}
2307
Trond Myklebust01c3f052009-06-17 13:22:58 -07002308static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2309{
2310 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
2311}
2312
David Howells54ceac42006-08-22 20:06:13 -04002313/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002314 * Validate NFSv4 mount options
2315 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002316static int nfs4_validate_mount_data(void *options,
2317 struct nfs_parsed_mount_data *args,
2318 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002319{
Chuck Lever4c568012007-12-10 14:59:28 -05002320 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002321 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002322 char *c;
2323
2324 if (data == NULL)
2325 goto out_no_data;
2326
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002327 args->rsize = NFS_MAX_FILE_IO_SIZE;
2328 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002329 args->acregmin = NFS_DEF_ACREGMIN;
2330 args->acregmax = NFS_DEF_ACREGMAX;
2331 args->acdirmin = NFS_DEF_ACDIRMIN;
2332 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002333 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002334 args->auth_flavors[0] = RPC_AUTH_UNIX;
Chuck Lever059f90b2009-08-09 15:09:31 -04002335 args->auth_flavor_len = 1;
Mike Sager3fd5be92009-04-01 09:21:48 -04002336 args->minorversion = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002337
Chuck Leverf0768eb2007-07-01 12:13:01 -04002338 switch (data->version) {
2339 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002340 ap = (struct sockaddr_in *)&args->nfs_server.address;
2341 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002342 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002343 if (data->host_addrlen == 0)
2344 goto out_no_address;
2345 args->nfs_server.addrlen = data->host_addrlen;
2346 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002347 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002348 if (!nfs_verify_server_address((struct sockaddr *)
2349 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002350 goto out_no_address;
2351
Chuck Lever6738b252008-06-24 16:33:54 -04002352 if (data->auth_flavourlen) {
2353 if (data->auth_flavourlen > 1)
2354 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002355 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002356 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002357 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002358 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002359 }
2360
2361 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2362 if (IS_ERR(c))
2363 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002364 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002365
2366 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2367 if (IS_ERR(c))
2368 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002369 args->nfs_server.export_path = c;
2370 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002371
2372 c = strndup_user(data->client_addr.data, 16);
2373 if (IS_ERR(c))
2374 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002375 args->client_address = c;
2376
2377 /*
2378 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2379 * can deal with.
2380 */
2381
2382 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2383 args->rsize = data->rsize;
2384 args->wsize = data->wsize;
2385 args->timeo = data->timeo;
2386 args->retrans = data->retrans;
2387 args->acregmin = data->acregmin;
2388 args->acregmax = data->acregmax;
2389 args->acdirmin = data->acdirmin;
2390 args->acdirmax = data->acdirmax;
2391 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002392 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002393
2394 break;
Chuck Lever80071222007-07-01 12:13:59 -04002395 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002396 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002397
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002398 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002399 return -EINVAL;
2400
2401 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002402 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002403 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002404
Chuck Levered596a82008-06-26 17:47:05 -04002405 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2406 args->nfs_server.port);
2407
Trond Myklebust259875e2008-07-02 14:43:47 -04002408 nfs_validate_transport_protocol(args);
2409
Trond Myklebust01c3f052009-06-17 13:22:58 -07002410 nfs4_validate_mount_flags(args);
2411
Chuck Lever6738b252008-06-24 16:33:54 -04002412 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002413 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002414
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002415 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002416 goto out_no_client_address;
2417
Chuck Leverdc045892008-06-23 12:36:37 -04002418 status = nfs_parse_devname(dev_name,
2419 &args->nfs_server.hostname,
2420 NFS4_MAXNAMLEN,
2421 &args->nfs_server.export_path,
2422 NFS4_MAXPATHLEN);
2423 if (status < 0)
2424 return status;
2425
Chuck Lever80071222007-07-01 12:13:59 -04002426 break;
2427 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002428 }
2429
2430 return 0;
2431
2432out_no_data:
2433 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2434 return -EINVAL;
2435
2436out_inval_auth:
2437 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2438 data->auth_flavourlen);
2439 return -EINVAL;
2440
2441out_no_address:
2442 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2443 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002444
2445out_no_client_address:
2446 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2447 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002448}
2449
2450/*
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002451 * Get the superblock for the NFS4 root partition
David Howells54ceac42006-08-22 20:06:13 -04002452 */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002453static int nfs4_remote_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -04002454 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002455{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002456 struct nfs_parsed_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002457 struct super_block *s;
2458 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002459 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002460 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002461 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002462 struct nfs_sb_mountdata sb_mntdata = {
2463 .mntflags = flags,
2464 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002465 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002466
Trond Myklebust33852a12008-06-19 14:20:11 -04002467 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2468 if (data == NULL || mntfh == NULL)
2469 goto out_free_fh;
2470
2471 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002472
David Howells54ceac42006-08-22 20:06:13 -04002473 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002474 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002475 if (IS_ERR(server)) {
2476 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002477 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002478 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002479 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002480
Trond Myklebust75180df2007-05-16 16:53:28 -04002481 if (server->flags & NFS4_MOUNT_UNSHARED)
2482 compare_super = NULL;
2483
David Howells54ceac42006-08-22 20:06:13 -04002484 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002485 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002486 if (IS_ERR(s)) {
2487 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002488 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002489 }
2490
Trond Myklebust5dd31772006-08-24 01:03:05 -04002491 if (s->s_fs_info != server) {
2492 nfs_free_server(server);
2493 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002494 } else {
2495 error = nfs_bdi_register(server);
2496 if (error)
2497 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002498 }
2499
David Howells54ceac42006-08-22 20:06:13 -04002500 if (!s->s_root) {
2501 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002502 nfs4_fill_super(s);
David Howells08734042009-04-03 16:42:42 +01002503 nfs_fscache_get_super_cookie(s, data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002504 }
David Howellsf7b422b2006-06-09 09:34:33 -04002505
Trond Myklebust33852a12008-06-19 14:20:11 -04002506 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002507 if (IS_ERR(mntroot)) {
2508 error = PTR_ERR(mntroot);
2509 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002510 }
David Howells54ceac42006-08-22 20:06:13 -04002511
Trond Myklebust33852a12008-06-19 14:20:11 -04002512 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002513 if (error)
2514 goto error_splat_root;
2515
David Howellsf7b422b2006-06-09 09:34:33 -04002516 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002517 mnt->mnt_sb = s;
2518 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002519 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002520
Chuck Lever29eb9812007-07-01 12:12:30 -04002521out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002522 security_free_mnt_opts(&data->lsm_opts);
2523out_free_fh:
2524 kfree(mntfh);
Trond Myklebust816724e2006-06-24 08:41:41 -04002525 return error;
David Howells54ceac42006-08-22 20:06:13 -04002526
Chuck Lever29eb9812007-07-01 12:12:30 -04002527out_free:
2528 nfs_free_server(server);
2529 goto out;
2530
Eric Paris46c8ac72008-05-02 13:42:42 -07002531error_splat_root:
2532 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002533error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002534 deactivate_locked_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002535 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002536}
2537
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002538static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2539 int flags, void *data, const char *hostname)
2540{
2541 struct vfsmount *root_mnt;
2542 char *root_devname;
2543 size_t len;
2544
2545 len = strlen(hostname) + 3;
2546 root_devname = kmalloc(len, GFP_KERNEL);
2547 if (root_devname == NULL)
2548 return ERR_PTR(-ENOMEM);
2549 snprintf(root_devname, len, "%s:/", hostname);
2550 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2551 kfree(root_devname);
2552 return root_mnt;
2553}
2554
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002555static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2556{
2557 char *page = (char *) __get_free_page(GFP_KERNEL);
2558 char *devname, *tmp;
2559
2560 if (page == NULL)
2561 return;
2562 devname = nfs_path(path->mnt->mnt_devname,
2563 path->mnt->mnt_root, path->dentry,
2564 page, PAGE_SIZE);
2565 if (devname == NULL)
2566 goto out_freepage;
2567 tmp = kstrdup(devname, GFP_KERNEL);
2568 if (tmp == NULL)
2569 goto out_freepage;
2570 kfree(mnt->mnt_devname);
2571 mnt->mnt_devname = tmp;
2572out_freepage:
2573 free_page((unsigned long)page);
2574}
2575
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002576static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2577 const char *export_path, struct vfsmount *mnt_target)
2578{
2579 struct mnt_namespace *ns_private;
2580 struct nameidata nd;
2581 struct super_block *s;
2582 int ret;
2583
2584 ns_private = create_mnt_ns(root_mnt);
2585 ret = PTR_ERR(ns_private);
2586 if (IS_ERR(ns_private))
2587 goto out_mntput;
2588
2589 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2590 export_path, LOOKUP_FOLLOW, &nd);
2591
2592 put_mnt_ns(ns_private);
2593
2594 if (ret != 0)
2595 goto out_err;
2596
2597 s = nd.path.mnt->mnt_sb;
2598 atomic_inc(&s->s_active);
2599 mnt_target->mnt_sb = s;
2600 mnt_target->mnt_root = dget(nd.path.dentry);
2601
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002602 /* Correct the device pathname */
2603 nfs_fix_devname(&nd.path, mnt_target);
2604
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002605 path_put(&nd.path);
2606 down_write(&s->s_umount);
2607 return 0;
2608out_mntput:
2609 mntput(root_mnt);
2610out_err:
2611 return ret;
2612}
2613
2614/*
2615 * Get the superblock for an NFS4 mountpoint
2616 */
2617static int nfs4_get_sb(struct file_system_type *fs_type,
2618 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2619{
2620 struct nfs_parsed_mount_data *data;
2621 char *export_path;
2622 struct vfsmount *root_mnt;
2623 int error = -ENOMEM;
2624
2625 data = kzalloc(sizeof(*data), GFP_KERNEL);
2626 if (data == NULL)
2627 goto out_free_data;
2628
2629 /* Validate the mount data */
2630 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2631 if (error < 0)
2632 goto out;
2633
2634 export_path = data->nfs_server.export_path;
2635 data->nfs_server.export_path = "/";
2636 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2637 data->nfs_server.hostname);
2638 data->nfs_server.export_path = export_path;
2639
2640 error = PTR_ERR(root_mnt);
2641 if (IS_ERR(root_mnt))
2642 goto out;
2643
2644 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2645
2646out:
2647 kfree(data->client_address);
2648 kfree(data->nfs_server.export_path);
2649 kfree(data->nfs_server.hostname);
2650 kfree(data->fscache_uniq);
2651out_free_data:
2652 kfree(data);
2653 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2654 error != 0 ? " [error]" : "");
2655 return error;
2656}
2657
David Howellsf7b422b2006-06-09 09:34:33 -04002658static void nfs4_kill_super(struct super_block *sb)
2659{
2660 struct nfs_server *server = NFS_SB(sb);
2661
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002662 dprintk("--> %s\n", __func__);
Trond Myklebust515d8612008-12-23 15:21:46 -05002663 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002664 kill_anon_super(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002665 nfs4_renewd_prepare_shutdown(server);
David Howells08734042009-04-03 16:42:42 +01002666 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002667 nfs_free_server(server);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002668 dprintk("<-- %s\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -04002669}
2670
2671/*
David Howells54ceac42006-08-22 20:06:13 -04002672 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002673 */
David Howells54ceac42006-08-22 20:06:13 -04002674static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2675 const char *dev_name, void *raw_data,
2676 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002677{
2678 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002679 struct super_block *s;
2680 struct nfs_server *server;
2681 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002682 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002683 struct nfs_sb_mountdata sb_mntdata = {
2684 .mntflags = flags,
2685 };
David Howells54ceac42006-08-22 20:06:13 -04002686 int error;
2687
2688 dprintk("--> nfs4_xdev_get_sb()\n");
2689
2690 /* create a new volume representation */
2691 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2692 if (IS_ERR(server)) {
2693 error = PTR_ERR(server);
2694 goto out_err_noserver;
2695 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002696 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002697
Trond Myklebust75180df2007-05-16 16:53:28 -04002698 if (server->flags & NFS4_MOUNT_UNSHARED)
2699 compare_super = NULL;
2700
David Howells54ceac42006-08-22 20:06:13 -04002701 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002702 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002703 if (IS_ERR(s)) {
2704 error = PTR_ERR(s);
2705 goto out_err_nosb;
2706 }
2707
2708 if (s->s_fs_info != server) {
2709 nfs_free_server(server);
2710 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002711 } else {
2712 error = nfs_bdi_register(server);
2713 if (error)
2714 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002715 }
2716
2717 if (!s->s_root) {
2718 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002719 nfs4_clone_super(s, data->sb);
2720 }
2721
2722 mntroot = nfs4_get_root(s, data->fh);
2723 if (IS_ERR(mntroot)) {
2724 error = PTR_ERR(mntroot);
2725 goto error_splat_super;
2726 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002727 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2728 dput(mntroot);
2729 error = -ESTALE;
2730 goto error_splat_super;
2731 }
David Howells54ceac42006-08-22 20:06:13 -04002732
2733 s->s_flags |= MS_ACTIVE;
2734 mnt->mnt_sb = s;
2735 mnt->mnt_root = mntroot;
2736
Eric Paris46c8ac72008-05-02 13:42:42 -07002737 security_sb_clone_mnt_opts(data->sb, s);
2738
David Howells54ceac42006-08-22 20:06:13 -04002739 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2740 return 0;
2741
2742out_err_nosb:
2743 nfs_free_server(server);
2744out_err_noserver:
2745 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2746 return error;
2747
2748error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002749 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002750 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2751 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002752}
2753
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002754static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
2755 int flags, const char *dev_name, void *raw_data,
2756 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002757{
2758 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002759 struct super_block *s;
2760 struct nfs_server *server;
2761 struct dentry *mntroot;
2762 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002763 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002764 struct nfs_sb_mountdata sb_mntdata = {
2765 .mntflags = flags,
2766 };
David Howells54ceac42006-08-22 20:06:13 -04002767 int error;
2768
2769 dprintk("--> nfs4_referral_get_sb()\n");
2770
2771 /* create a new volume representation */
2772 server = nfs4_create_referral_server(data, &mntfh);
2773 if (IS_ERR(server)) {
2774 error = PTR_ERR(server);
2775 goto out_err_noserver;
2776 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002777 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002778
Trond Myklebust75180df2007-05-16 16:53:28 -04002779 if (server->flags & NFS4_MOUNT_UNSHARED)
2780 compare_super = NULL;
2781
David Howells54ceac42006-08-22 20:06:13 -04002782 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002783 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002784 if (IS_ERR(s)) {
2785 error = PTR_ERR(s);
2786 goto out_err_nosb;
2787 }
2788
2789 if (s->s_fs_info != server) {
2790 nfs_free_server(server);
2791 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002792 } else {
2793 error = nfs_bdi_register(server);
2794 if (error)
2795 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002796 }
2797
2798 if (!s->s_root) {
2799 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002800 nfs4_fill_super(s);
2801 }
2802
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002803 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002804 if (IS_ERR(mntroot)) {
2805 error = PTR_ERR(mntroot);
2806 goto error_splat_super;
2807 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002808 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2809 dput(mntroot);
2810 error = -ESTALE;
2811 goto error_splat_super;
2812 }
David Howells54ceac42006-08-22 20:06:13 -04002813
2814 s->s_flags |= MS_ACTIVE;
2815 mnt->mnt_sb = s;
2816 mnt->mnt_root = mntroot;
2817
Eric Paris46c8ac72008-05-02 13:42:42 -07002818 security_sb_clone_mnt_opts(data->sb, s);
2819
David Howells54ceac42006-08-22 20:06:13 -04002820 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2821 return 0;
2822
2823out_err_nosb:
2824 nfs_free_server(server);
2825out_err_noserver:
2826 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2827 return error;
2828
2829error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002830 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002831 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2832 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002833}
2834
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002835/*
2836 * Create an NFS4 server record on referral traversal
2837 */
2838static int nfs4_referral_get_sb(struct file_system_type *fs_type,
2839 int flags, const char *dev_name, void *raw_data,
2840 struct vfsmount *mnt)
2841{
2842 struct nfs_clone_mount *data = raw_data;
2843 char *export_path;
2844 struct vfsmount *root_mnt;
2845 int error;
2846
2847 dprintk("--> nfs4_referral_get_sb()\n");
2848
2849 export_path = data->mnt_path;
2850 data->mnt_path = "/";
2851
2852 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
2853 flags, data, data->hostname);
2854 data->mnt_path = export_path;
2855
2856 error = PTR_ERR(root_mnt);
2857 if (IS_ERR(root_mnt))
2858 goto out;
2859
2860 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2861out:
2862 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
2863 error != 0 ? " [error]" : "");
2864 return error;
2865}
2866
David Howells54ceac42006-08-22 20:06:13 -04002867#endif /* CONFIG_NFS_V4 */