blob: 837032731bb65ea794dba959a2a8c223440a3ee5 [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,
Chuck Lever764302c2009-09-08 19:50:03 -040076 Opt_v2, Opt_v3, Opt_v4,
\"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" },
Chuck Lever764302c2009-09-08 19:50:03 -0400130 { Opt_v4, "v4" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400131 { Opt_udp, "udp" },
132 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400133 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400134 { Opt_acl, "acl" },
135 { Opt_noacl, "noacl" },
136 { Opt_rdirplus, "rdirplus" },
137 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400138 { Opt_sharecache, "sharecache" },
139 { Opt_nosharecache, "nosharecache" },
Chuck Leverd7403512008-12-23 15:21:37 -0500140 { Opt_resvport, "resvport" },
141 { Opt_noresvport, "noresvport" },
David Howellsb797cac2009-04-03 16:42:48 +0100142 { Opt_fscache, "fsc" },
143 { Opt_fscache_uniq, "fsc=%s" },
144 { Opt_nofscache, "nofsc" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400145
Chuck Levera5a16ba2009-06-17 18:02:14 -0700146 { Opt_port, "port=%s" },
147 { Opt_rsize, "rsize=%s" },
148 { Opt_wsize, "wsize=%s" },
149 { Opt_bsize, "bsize=%s" },
150 { Opt_timeo, "timeo=%s" },
151 { Opt_retrans, "retrans=%s" },
152 { Opt_acregmin, "acregmin=%s" },
153 { Opt_acregmax, "acregmax=%s" },
154 { Opt_acdirmin, "acdirmin=%s" },
155 { Opt_acdirmax, "acdirmax=%s" },
156 { Opt_actimeo, "actimeo=%s" },
157 { Opt_namelen, "namlen=%s" },
158 { Opt_mountport, "mountport=%s" },
159 { Opt_mountvers, "mountvers=%s" },
160 { Opt_nfsvers, "nfsvers=%s" },
161 { Opt_nfsvers, "vers=%s" },
Chuck Leverf3f4f4e2009-08-09 15:09:29 -0400162 { Opt_minorversion, "minorversion=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400163
164 { Opt_sec, "sec=%s" },
165 { Opt_proto, "proto=%s" },
166 { Opt_mountproto, "mountproto=%s" },
167 { Opt_addr, "addr=%s" },
168 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500169 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400170 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400171
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400172 { Opt_lookupcache, "lookupcache=%s" },
173
Chuck Leverbf0fd762007-07-01 12:13:44 -0400174 { Opt_err, NULL }
175};
176
177enum {
Jeff Laytonee671b02009-12-03 15:58:56 -0500178 Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400179
180 Opt_xprt_err
181};
182
Steven Whitehousea447c092008-10-13 10:46:57 +0100183static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400184 { Opt_xprt_udp, "udp" },
Jeff Laytonee671b02009-12-03 15:58:56 -0500185 { Opt_xprt_udp6, "udp6" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400186 { Opt_xprt_tcp, "tcp" },
Jeff Laytonee671b02009-12-03 15:58:56 -0500187 { Opt_xprt_tcp6, "tcp6" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400188 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400189
190 { Opt_xprt_err, NULL }
191};
192
193enum {
194 Opt_sec_none, Opt_sec_sys,
195 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
196 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
197 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
198
199 Opt_sec_err
200};
201
Steven Whitehousea447c092008-10-13 10:46:57 +0100202static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400203 { Opt_sec_none, "none" },
204 { Opt_sec_none, "null" },
205 { Opt_sec_sys, "sys" },
206
207 { Opt_sec_krb5, "krb5" },
208 { Opt_sec_krb5i, "krb5i" },
209 { Opt_sec_krb5p, "krb5p" },
210
211 { Opt_sec_lkey, "lkey" },
212 { Opt_sec_lkeyi, "lkeyi" },
213 { Opt_sec_lkeyp, "lkeyp" },
214
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500215 { Opt_sec_spkm, "spkm3" },
216 { Opt_sec_spkmi, "spkm3i" },
217 { Opt_sec_spkmp, "spkm3p" },
218
Chuck Leverbf0fd762007-07-01 12:13:44 -0400219 { Opt_sec_err, NULL }
220};
221
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400222enum {
223 Opt_lookupcache_all, Opt_lookupcache_positive,
224 Opt_lookupcache_none,
225
226 Opt_lookupcache_err
227};
228
229static match_table_t nfs_lookupcache_tokens = {
230 { Opt_lookupcache_all, "all" },
231 { Opt_lookupcache_positive, "pos" },
232 { Opt_lookupcache_positive, "positive" },
233 { Opt_lookupcache_none, "none" },
234
235 { Opt_lookupcache_err, NULL }
236};
237
Chuck Leverbf0fd762007-07-01 12:13:44 -0400238
Al Viro42faad92008-04-24 07:21:56 -0400239static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400240static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400241static int nfs_show_options(struct seq_file *, struct vfsmount *);
242static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400243static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400244static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400245 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400246static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400247static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400248
249static struct file_system_type nfs_fs_type = {
250 .owner = THIS_MODULE,
251 .name = "nfs",
252 .get_sb = nfs_get_sb,
253 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700254 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400255};
256
David Howells54ceac42006-08-22 20:06:13 -0400257struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400258 .owner = THIS_MODULE,
259 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400260 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400261 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700262 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400263};
264
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800265static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400266 .alloc_inode = nfs_alloc_inode,
267 .destroy_inode = nfs_destroy_inode,
268 .write_inode = nfs_write_inode,
269 .statfs = nfs_statfs,
270 .clear_inode = nfs_clear_inode,
271 .umount_begin = nfs_umount_begin,
272 .show_options = nfs_show_options,
273 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400274 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400275};
276
277#ifdef CONFIG_NFS_V4
Chuck Lever7630c852009-09-08 19:49:57 -0400278static int nfs4_validate_text_mount_data(void *options,
279 struct nfs_parsed_mount_data *args, const char *dev_name);
Chuck Levera6fe23b2009-09-08 19:50:00 -0400280static int nfs4_try_mount(int flags, const char *dev_name,
281 struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
Trond Myklebust816724e2006-06-24 08:41:41 -0400282static int nfs4_get_sb(struct file_system_type *fs_type,
283 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400284static int nfs4_remote_get_sb(struct file_system_type *fs_type,
285 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400286static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
287 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
288static int nfs4_referral_get_sb(struct file_system_type *fs_type,
289 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400290static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
291 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400292static void nfs4_kill_super(struct super_block *sb);
293
294static struct file_system_type nfs4_fs_type = {
295 .owner = THIS_MODULE,
296 .name = "nfs4",
297 .get_sb = nfs4_get_sb,
298 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700299 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400300};
301
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400302static struct file_system_type nfs4_remote_fs_type = {
303 .owner = THIS_MODULE,
304 .name = "nfs4",
305 .get_sb = nfs4_remote_get_sb,
306 .kill_sb = nfs4_kill_super,
307 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
308};
309
David Howells54ceac42006-08-22 20:06:13 -0400310struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400311 .owner = THIS_MODULE,
312 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400313 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400314 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700315 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400316};
317
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400318static struct file_system_type nfs4_remote_referral_fs_type = {
319 .owner = THIS_MODULE,
320 .name = "nfs4",
321 .get_sb = nfs4_remote_referral_get_sb,
322 .kill_sb = nfs4_kill_super,
323 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
324};
325
David Howells54ceac42006-08-22 20:06:13 -0400326struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400327 .owner = THIS_MODULE,
328 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400329 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400330 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700331 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400332};
333
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800334static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400335 .alloc_inode = nfs_alloc_inode,
336 .destroy_inode = nfs_destroy_inode,
337 .write_inode = nfs_write_inode,
338 .statfs = nfs_statfs,
339 .clear_inode = nfs4_clear_inode,
340 .umount_begin = nfs_umount_begin,
341 .show_options = nfs_show_options,
342 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400343 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400344};
345#endif
346
Rusty Russell8e1f9362007-07-17 04:03:17 -0700347static struct shrinker acl_shrinker = {
348 .shrink = nfs_access_cache_shrinker,
349 .seeks = DEFAULT_SEEKS,
350};
Trond Myklebust979df722006-07-25 11:28:19 -0400351
David Howellsf7b422b2006-06-09 09:34:33 -0400352/*
353 * Register the NFS filesystems
354 */
355int __init register_nfs_fs(void)
356{
357 int ret;
358
359 ret = register_filesystem(&nfs_fs_type);
360 if (ret < 0)
361 goto error_0;
362
David Howellsf7b422b2006-06-09 09:34:33 -0400363 ret = nfs_register_sysctl();
364 if (ret < 0)
365 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800366#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400367 ret = register_filesystem(&nfs4_fs_type);
368 if (ret < 0)
369 goto error_2;
370#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700371 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400372 return 0;
373
374#ifdef CONFIG_NFS_V4
375error_2:
376 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800377#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400378error_1:
379 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400380error_0:
381 return ret;
382}
383
384/*
385 * Unregister the NFS filesystems
386 */
387void __exit unregister_nfs_fs(void)
388{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700389 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400390#ifdef CONFIG_NFS_V4
391 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400392#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700393 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400394 unregister_filesystem(&nfs_fs_type);
395}
396
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400397void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500398{
399 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400400
401 if (atomic_inc_return(&server->active) == 1)
402 atomic_inc(&sb->s_active);
403}
404
405void nfs_sb_deactive(struct super_block *sb)
406{
407 struct nfs_server *server = NFS_SB(sb);
408
409 if (atomic_dec_and_test(&server->active))
410 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500411}
412
David Howellsf7b422b2006-06-09 09:34:33 -0400413/*
414 * Deliver file system statistics to userspace
415 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400416static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400417{
David Howells0c7d90c2006-08-22 20:06:10 -0400418 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400419 unsigned char blockbits;
420 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400421 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400422 struct nfs_fattr fattr;
423 struct nfs_fsstat res = {
424 .fattr = &fattr,
425 };
426 int error;
427
David Howells8fa5c002006-08-22 20:06:12 -0400428 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400429 if (error < 0)
430 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700431 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400432
433 /*
434 * Current versions of glibc do not correctly handle the
435 * case where f_frsize != f_bsize. Eventually we want to
436 * report the value of wtmult in this field.
437 */
David Howells0c7d90c2006-08-22 20:06:10 -0400438 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400439
440 /*
441 * On most *nix systems, f_blocks, f_bfree, and f_bavail
442 * are reported in units of f_frsize. Linux hasn't had
443 * an f_frsize field in its statfs struct until recently,
444 * thus historically Linux's sys_statfs reports these
445 * fields in units of f_bsize.
446 */
David Howells0c7d90c2006-08-22 20:06:10 -0400447 buf->f_bsize = dentry->d_sb->s_blocksize;
448 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400449 blockres = (1 << blockbits) - 1;
450 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
451 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
452 buf->f_bavail = (res.abytes + blockres) >> blockbits;
453
454 buf->f_files = res.tfiles;
455 buf->f_ffree = res.afiles;
456
457 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700458
David Howellsf7b422b2006-06-09 09:34:33 -0400459 return 0;
460
461 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700462 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700463 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400464}
465
David Howells7d4e2742006-08-22 20:06:07 -0400466/*
467 * Map the security flavour number to a name
468 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400469static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
470{
David Howells7d4e2742006-08-22 20:06:07 -0400471 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400472 rpc_authflavor_t flavour;
473 const char *str;
474 } sec_flavours[] = {
475 { RPC_AUTH_NULL, "null" },
476 { RPC_AUTH_UNIX, "sys" },
477 { RPC_AUTH_GSS_KRB5, "krb5" },
478 { RPC_AUTH_GSS_KRB5I, "krb5i" },
479 { RPC_AUTH_GSS_KRB5P, "krb5p" },
480 { RPC_AUTH_GSS_LKEY, "lkey" },
481 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
482 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
483 { RPC_AUTH_GSS_SPKM, "spkm" },
484 { RPC_AUTH_GSS_SPKMI, "spkmi" },
485 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400486 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400487 };
488 int i;
489
Chuck Lever4d81cd12007-07-01 12:12:40 -0400490 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400491 if (sec_flavours[i].flavour == flavour)
492 break;
493 }
494 return sec_flavours[i].str;
495}
496
Jeff Laytonee671b02009-12-03 15:58:56 -0500497static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
498 int showdefaults)
499{
500 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
501
502 seq_printf(m, ",mountproto=");
503 switch (sap->sa_family) {
504 case AF_INET:
505 switch (nfss->mountd_protocol) {
506 case IPPROTO_UDP:
507 seq_printf(m, RPCBIND_NETID_UDP);
508 break;
509 case IPPROTO_TCP:
510 seq_printf(m, RPCBIND_NETID_TCP);
511 break;
512 default:
513 if (showdefaults)
514 seq_printf(m, "auto");
515 }
516 break;
517 case AF_INET6:
518 switch (nfss->mountd_protocol) {
519 case IPPROTO_UDP:
520 seq_printf(m, RPCBIND_NETID_UDP6);
521 break;
522 case IPPROTO_TCP:
523 seq_printf(m, RPCBIND_NETID_TCP6);
524 break;
525 default:
526 if (showdefaults)
527 seq_printf(m, "auto");
528 }
529 break;
530 default:
531 if (showdefaults)
532 seq_printf(m, "auto");
533 }
534}
535
Chuck Lever82d101d2008-03-14 14:10:37 -0400536static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
537 int showdefaults)
538{
539 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
540
541 switch (sap->sa_family) {
542 case AF_INET: {
543 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrisonbe859402008-10-31 00:56:28 -0700544 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400545 break;
546 }
547 case AF_INET6: {
548 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Chuck Leverd250e192009-12-03 15:58:56 -0500549 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400550 break;
551 }
552 default:
553 if (showdefaults)
554 seq_printf(m, ",mountaddr=unspecified");
555 }
556
557 if (nfss->mountd_version || showdefaults)
558 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
559 if (nfss->mountd_port || showdefaults)
560 seq_printf(m, ",mountport=%u", nfss->mountd_port);
561
Jeff Laytonee671b02009-12-03 15:58:56 -0500562 nfs_show_mountd_netid(m, nfss, showdefaults);
Chuck Lever82d101d2008-03-14 14:10:37 -0400563}
564
David Howellsf7b422b2006-06-09 09:34:33 -0400565/*
566 * Describe the mount options in force on this server representation
567 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400568static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
569 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400570{
David Howells509de812006-08-22 20:06:11 -0400571 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400572 int flag;
David Howells509de812006-08-22 20:06:11 -0400573 const char *str;
574 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400575 } nfs_info[] = {
576 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400577 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400578 { NFS_MOUNT_NOCTO, ",nocto", "" },
579 { NFS_MOUNT_NOAC, ",noac", "" },
580 { NFS_MOUNT_NONLM, ",nolock", "" },
581 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400582 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500583 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
584 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400585 { 0, NULL, NULL }
586 };
David Howells509de812006-08-22 20:06:11 -0400587 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400588 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400589 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400590
Chuck Lever82d101d2008-03-14 14:10:37 -0400591 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400592 seq_printf(m, ",rsize=%u", nfss->rsize);
593 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400594 if (nfss->bsize != 0)
595 seq_printf(m, ",bsize=%u", nfss->bsize);
596 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400597 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400598 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400599 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400600 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400601 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400602 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400603 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400604 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400605 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
606 if (nfss->flags & nfs_infop->flag)
607 seq_puts(m, nfs_infop->str);
608 else
609 seq_puts(m, nfs_infop->nostr);
610 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400611 seq_printf(m, ",proto=%s",
Jeff Laytonee671b02009-12-03 15:58:56 -0500612 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
Chuck Lever82d101d2008-03-14 14:10:37 -0400613 if (version == 4) {
614 if (nfss->port != NFS_PORT)
615 seq_printf(m, ",port=%u", nfss->port);
616 } else
617 if (nfss->port)
618 seq_printf(m, ",port=%u", nfss->port);
619
Trond Myklebust33170232007-12-20 16:03:59 -0500620 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
621 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400622 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400623
624 if (version != 4)
625 nfs_show_mountd_options(m, nfss, showdefaults);
626
627#ifdef CONFIG_NFS_V4
628 if (clp->rpc_ops->version == 4)
629 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
630#endif
David Howellsb797cac2009-04-03 16:42:48 +0100631 if (nfss->options & NFS_OPTION_FSCACHE)
632 seq_printf(m, ",fsc");
David Howellsf7b422b2006-06-09 09:34:33 -0400633}
634
635/*
636 * Describe the mount options on this VFS mountpoint
637 */
638static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
639{
640 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
641
642 nfs_show_mount_options(m, nfss, 0);
643
Chuck Lever5d8515c2007-12-10 14:57:16 -0500644 seq_printf(m, ",addr=%s",
645 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
646 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400647
648 return 0;
649}
650
651/*
652 * Present statistical information for this VFS mountpoint
653 */
654static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
655{
656 int i, cpu;
657 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
658 struct rpc_auth *auth = nfss->client->cl_auth;
659 struct nfs_iostats totals = { };
660
661 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
662
663 /*
664 * Display all mount option settings
665 */
666 seq_printf(m, "\n\topts:\t");
667 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
668 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
669 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
670 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
671 nfs_show_mount_options(m, nfss, 1);
672
673 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
674
675 seq_printf(m, "\n\tcaps:\t");
676 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400677 seq_printf(m, ",wtmult=%u", nfss->wtmult);
678 seq_printf(m, ",dtsize=%u", nfss->dtsize);
679 seq_printf(m, ",bsize=%u", nfss->bsize);
680 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400681
682#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500683 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400684 seq_printf(m, "\n\tnfsv4:\t");
685 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
686 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
687 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
688 }
689#endif
690
691 /*
692 * Display security flavor in effect for this mount
693 */
Chuck Lever2d767432008-03-14 14:10:08 -0400694 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400695 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400696 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400697
698 /*
699 * Display superblock I/O counters
700 */
701 for_each_possible_cpu(cpu) {
702 struct nfs_iostats *stats;
703
704 preempt_disable();
705 stats = per_cpu_ptr(nfss->io_stats, cpu);
706
707 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
708 totals.events[i] += stats->events[i];
709 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
710 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100711#ifdef CONFIG_NFS_FSCACHE
712 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
713 totals.fscache[i] += stats->fscache[i];
714#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400715
716 preempt_enable();
717 }
718
719 seq_printf(m, "\n\tevents:\t");
720 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
721 seq_printf(m, "%lu ", totals.events[i]);
722 seq_printf(m, "\n\tbytes:\t");
723 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
724 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100725#ifdef CONFIG_NFS_FSCACHE
726 if (nfss->options & NFS_OPTION_FSCACHE) {
727 seq_printf(m, "\n\tfsc:\t");
728 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
729 seq_printf(m, "%Lu ", totals.bytes[i]);
730 }
731#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400732 seq_printf(m, "\n");
733
734 rpc_print_iostats(m, nfss->client);
735
736 return 0;
737}
738
739/*
740 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400741 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400742 */
Al Viro42faad92008-04-24 07:21:56 -0400743static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400744{
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200745 struct nfs_server *server;
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400746 struct rpc_clnt *rpc;
747
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200748 server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400749 /* -EIO all pending I/O */
750 rpc = server->client_acl;
751 if (!IS_ERR(rpc))
752 rpc_killall_tasks(rpc);
753 rpc = server->client;
754 if (!IS_ERR(rpc))
755 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400756}
757
Trond Myklebustc5811db2009-10-06 15:40:15 -0400758static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
Chuck Lever9423a082009-09-23 14:36:38 -0400759{
760 struct nfs_parsed_mount_data *data;
761
762 data = kzalloc(sizeof(*data), GFP_KERNEL);
763 if (data) {
Chuck Lever9423a082009-09-23 14:36:38 -0400764 data->rsize = NFS_MAX_FILE_IO_SIZE;
765 data->wsize = NFS_MAX_FILE_IO_SIZE;
766 data->acregmin = NFS_DEF_ACREGMIN;
767 data->acregmax = NFS_DEF_ACREGMAX;
768 data->acdirmin = NFS_DEF_ACDIRMIN;
769 data->acdirmax = NFS_DEF_ACDIRMAX;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400770 data->mount_server.port = NFS_UNSPEC_PORT;
Chuck Lever9423a082009-09-23 14:36:38 -0400771 data->nfs_server.port = NFS_UNSPEC_PORT;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400772 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Lever9423a082009-09-23 14:36:38 -0400773 data->auth_flavors[0] = RPC_AUTH_UNIX;
774 data->auth_flavor_len = 1;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400775 data->version = version;
Chuck Lever9423a082009-09-23 14:36:38 -0400776 data->minorversion = 0;
777 }
778 return data;
779}
780
David Howellsf7b422b2006-06-09 09:34:33 -0400781/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500782 * Sanity-check a server address provided by the mount command.
783 *
784 * Address family must be initialized, and address must not be
785 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400786 */
787static int nfs_verify_server_address(struct sockaddr *addr)
788{
789 switch (addr->sa_family) {
790 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500791 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700792 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500793 }
794 case AF_INET6: {
795 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
796 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400797 }
798 }
799
Chuck Lever53a0b9c2009-08-09 15:09:36 -0400800 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
Chuck Leverfc50d582007-07-01 12:12:46 -0400801 return 0;
802}
803
Chuck Lever9412b922007-12-10 14:59:21 -0500804/*
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400805 * Select between a default port value and a user-specified port value.
806 * If a zero value is set, then autobind will be used.
807 */
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400808static void nfs_set_port(struct sockaddr *sap, int *port,
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400809 const unsigned short default_port)
810{
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400811 if (*port == NFS_UNSPEC_PORT)
812 *port = default_port;
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400813
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400814 rpc_set_port(sap, *port);
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400815}
816
817/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400818 * Sanity check the NFS transport protocol.
819 *
820 */
821static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
822{
823 switch (mnt->nfs_server.protocol) {
824 case XPRT_TRANSPORT_UDP:
825 case XPRT_TRANSPORT_TCP:
826 case XPRT_TRANSPORT_RDMA:
827 break;
828 default:
829 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
830 }
831}
832
833/*
834 * For text based NFSv2/v3 mounts, the mount protocol transport default
835 * settings should depend upon the specified NFS transport.
836 */
837static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
838{
839 nfs_validate_transport_protocol(mnt);
840
841 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
842 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
843 return;
844 switch (mnt->nfs_server.protocol) {
845 case XPRT_TRANSPORT_UDP:
846 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
847 break;
848 case XPRT_TRANSPORT_TCP:
849 case XPRT_TRANSPORT_RDMA:
850 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
851 }
852}
853
854/*
Chuck Lever01060c82008-06-24 16:33:38 -0400855 * Parse the value of the 'sec=' option.
Chuck Lever01060c82008-06-24 16:33:38 -0400856 */
857static int nfs_parse_security_flavors(char *value,
858 struct nfs_parsed_mount_data *mnt)
859{
860 substring_t args[MAX_OPT_ARGS];
861
862 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
863
864 switch (match_token(value, nfs_secflavor_tokens, args)) {
865 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400866 mnt->auth_flavors[0] = RPC_AUTH_NULL;
867 break;
868 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400869 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
870 break;
871 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400872 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
873 break;
874 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400875 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
876 break;
877 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400878 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
879 break;
880 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400881 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
882 break;
883 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400884 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
885 break;
886 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400887 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
888 break;
889 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400890 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
891 break;
892 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400893 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
894 break;
895 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400896 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
897 break;
898 default:
899 return 0;
900 }
901
Chuck Lever059f90b2009-08-09 15:09:31 -0400902 mnt->auth_flavor_len = 1;
Chuck Lever01060c82008-06-24 16:33:38 -0400903 return 1;
904}
905
906/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400907 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400908 * a data structure. The whole mount string is processed; bad options are
909 * skipped as they are encountered. If there were no errors, return 1;
910 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400911 */
912static int nfs_parse_mount_options(char *raw,
913 struct nfs_parsed_mount_data *mnt)
914{
Eric Parisf9c3a382008-03-05 14:20:18 -0500915 char *p, *string, *secdata;
Chuck Leverd23c45f2009-06-17 18:02:13 -0700916 int rc, sloppy = 0, invalid_option = 0;
Jeff Laytonee671b02009-12-03 15:58:56 -0500917 unsigned short protofamily = AF_UNSPEC;
918 unsigned short mountfamily = AF_UNSPEC;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400919
920 if (!raw) {
921 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
922 return 1;
923 }
924 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
925
Eric Parisf9c3a382008-03-05 14:20:18 -0500926 secdata = alloc_secdata();
927 if (!secdata)
928 goto out_nomem;
929
930 rc = security_sb_copy_data(raw, secdata);
931 if (rc)
932 goto out_security_failure;
933
934 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
935 if (rc)
936 goto out_security_failure;
937
938 free_secdata(secdata);
939
Chuck Leverbf0fd762007-07-01 12:13:44 -0400940 while ((p = strsep(&raw, ",")) != NULL) {
941 substring_t args[MAX_OPT_ARGS];
Chuck Levera5a16ba2009-06-17 18:02:14 -0700942 unsigned long option;
943 int token;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400944
945 if (!*p)
946 continue;
947
948 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
949
950 token = match_token(p, nfs_mount_option_tokens, args);
951 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400952
953 /*
954 * boolean options: foo/nofoo
955 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400956 case Opt_soft:
957 mnt->flags |= NFS_MOUNT_SOFT;
958 break;
959 case Opt_hard:
960 mnt->flags &= ~NFS_MOUNT_SOFT;
961 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400962 case Opt_posix:
963 mnt->flags |= NFS_MOUNT_POSIX;
964 break;
965 case Opt_noposix:
966 mnt->flags &= ~NFS_MOUNT_POSIX;
967 break;
968 case Opt_cto:
969 mnt->flags &= ~NFS_MOUNT_NOCTO;
970 break;
971 case Opt_nocto:
972 mnt->flags |= NFS_MOUNT_NOCTO;
973 break;
974 case Opt_ac:
975 mnt->flags &= ~NFS_MOUNT_NOAC;
976 break;
977 case Opt_noac:
978 mnt->flags |= NFS_MOUNT_NOAC;
979 break;
980 case Opt_lock:
981 mnt->flags &= ~NFS_MOUNT_NONLM;
982 break;
983 case Opt_nolock:
984 mnt->flags |= NFS_MOUNT_NONLM;
985 break;
986 case Opt_v2:
987 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -0400988 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400989 break;
990 case Opt_v3:
991 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -0400992 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400993 break;
Chuck Lever764302c2009-09-08 19:50:03 -0400994#ifdef CONFIG_NFS_V4
995 case Opt_v4:
996 mnt->flags &= ~NFS_MOUNT_VER3;
997 mnt->version = 4;
998 break;
999#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001000 case Opt_udp:
1001 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001002 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001003 break;
1004 case Opt_tcp:
1005 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001006 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001007 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001008 case Opt_rdma:
1009 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1010 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001011 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001012 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001013 case Opt_acl:
1014 mnt->flags &= ~NFS_MOUNT_NOACL;
1015 break;
1016 case Opt_noacl:
1017 mnt->flags |= NFS_MOUNT_NOACL;
1018 break;
1019 case Opt_rdirplus:
1020 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1021 break;
1022 case Opt_nordirplus:
1023 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1024 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001025 case Opt_sharecache:
1026 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1027 break;
1028 case Opt_nosharecache:
1029 mnt->flags |= NFS_MOUNT_UNSHARED;
1030 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001031 case Opt_resvport:
1032 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1033 break;
1034 case Opt_noresvport:
1035 mnt->flags |= NFS_MOUNT_NORESVPORT;
1036 break;
David Howellsb797cac2009-04-03 16:42:48 +01001037 case Opt_fscache:
1038 mnt->options |= NFS_OPTION_FSCACHE;
1039 kfree(mnt->fscache_uniq);
1040 mnt->fscache_uniq = NULL;
1041 break;
1042 case Opt_nofscache:
1043 mnt->options &= ~NFS_OPTION_FSCACHE;
1044 kfree(mnt->fscache_uniq);
1045 mnt->fscache_uniq = NULL;
1046 break;
1047 case Opt_fscache_uniq:
1048 string = match_strdup(args);
1049 if (!string)
1050 goto out_nomem;
1051 kfree(mnt->fscache_uniq);
1052 mnt->fscache_uniq = string;
1053 mnt->options |= NFS_OPTION_FSCACHE;
1054 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001055
Chuck Leverf45663c2008-06-24 19:28:02 -04001056 /*
1057 * options that take numeric values
1058 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001059 case Opt_port:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001060 string = match_strdup(args);
1061 if (string == NULL)
1062 goto out_nomem;
1063 rc = strict_strtoul(string, 10, &option);
1064 kfree(string);
1065 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001066 goto out_invalid_value;
1067 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001068 break;
1069 case Opt_rsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001070 string = match_strdup(args);
1071 if (string == NULL)
1072 goto out_nomem;
1073 rc = strict_strtoul(string, 10, &option);
1074 kfree(string);
1075 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001076 goto out_invalid_value;
1077 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001078 break;
1079 case Opt_wsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001080 string = match_strdup(args);
1081 if (string == NULL)
1082 goto out_nomem;
1083 rc = strict_strtoul(string, 10, &option);
1084 kfree(string);
1085 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001086 goto out_invalid_value;
1087 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001088 break;
1089 case Opt_bsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001090 string = match_strdup(args);
1091 if (string == NULL)
1092 goto out_nomem;
1093 rc = strict_strtoul(string, 10, &option);
1094 kfree(string);
1095 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001096 goto out_invalid_value;
1097 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001098 break;
1099 case Opt_timeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001100 string = match_strdup(args);
1101 if (string == NULL)
1102 goto out_nomem;
1103 rc = strict_strtoul(string, 10, &option);
1104 kfree(string);
1105 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001106 goto out_invalid_value;
1107 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001108 break;
1109 case Opt_retrans:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001110 string = match_strdup(args);
1111 if (string == NULL)
1112 goto out_nomem;
1113 rc = strict_strtoul(string, 10, &option);
1114 kfree(string);
1115 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001116 goto out_invalid_value;
1117 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001118 break;
1119 case Opt_acregmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001120 string = match_strdup(args);
1121 if (string == NULL)
1122 goto out_nomem;
1123 rc = strict_strtoul(string, 10, &option);
1124 kfree(string);
1125 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001126 goto out_invalid_value;
1127 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001128 break;
1129 case Opt_acregmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001130 string = match_strdup(args);
1131 if (string == NULL)
1132 goto out_nomem;
1133 rc = strict_strtoul(string, 10, &option);
1134 kfree(string);
1135 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001136 goto out_invalid_value;
1137 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001138 break;
1139 case Opt_acdirmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001140 string = match_strdup(args);
1141 if (string == NULL)
1142 goto out_nomem;
1143 rc = strict_strtoul(string, 10, &option);
1144 kfree(string);
1145 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001146 goto out_invalid_value;
1147 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001148 break;
1149 case Opt_acdirmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001150 string = match_strdup(args);
1151 if (string == NULL)
1152 goto out_nomem;
1153 rc = strict_strtoul(string, 10, &option);
1154 kfree(string);
1155 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001156 goto out_invalid_value;
1157 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001158 break;
1159 case Opt_actimeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001160 string = match_strdup(args);
1161 if (string == NULL)
1162 goto out_nomem;
1163 rc = strict_strtoul(string, 10, &option);
1164 kfree(string);
1165 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001166 goto out_invalid_value;
1167 mnt->acregmin = mnt->acregmax =
1168 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001169 break;
1170 case Opt_namelen:
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->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001179 break;
1180 case Opt_mountport:
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 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001187 goto out_invalid_value;
1188 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001189 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001190 case Opt_mountvers:
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 Leverf45663c2008-06-24 19:28:02 -04001197 option < NFS_MNT_VERSION ||
Chuck Leverd23c45f2009-06-17 18:02:13 -07001198 option > NFS_MNT3_VERSION)
1199 goto out_invalid_value;
1200 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001201 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001202 case Opt_nfsvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001203 string = match_strdup(args);
1204 if (string == NULL)
1205 goto out_nomem;
1206 rc = strict_strtoul(string, 10, &option);
1207 kfree(string);
1208 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001209 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001210 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001211 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001212 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001213 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001214 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001215 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001216 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001217 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001218 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001219#ifdef CONFIG_NFS_V4
1220 case NFS4_VERSION:
1221 mnt->flags &= ~NFS_MOUNT_VER3;
1222 mnt->version = 4;
1223 break;
1224#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001225 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001226 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001227 }
1228 break;
Mike Sager3fd5be92009-04-01 09:21:48 -04001229 case Opt_minorversion:
Chuck Leverf3f4f4e2009-08-09 15:09:29 -04001230 string = match_strdup(args);
1231 if (string == NULL)
1232 goto out_nomem;
1233 rc = strict_strtoul(string, 10, &option);
1234 kfree(string);
1235 if (rc != 0)
1236 goto out_invalid_value;
1237 if (option > NFS4_MAX_MINOR_VERSION)
1238 goto out_invalid_value;
1239 mnt->minorversion = option;
Mike Sager3fd5be92009-04-01 09:21:48 -04001240 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001241
Chuck Leverf45663c2008-06-24 19:28:02 -04001242 /*
1243 * options that take text values
1244 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001245 case Opt_sec:
1246 string = match_strdup(args);
1247 if (string == NULL)
1248 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001249 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001250 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001251 if (!rc) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001252 dfprintk(MOUNT, "NFS: unrecognized "
1253 "security flavor\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001254 return 0;
Chuck Leverf45663c2008-06-24 19:28:02 -04001255 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001256 break;
1257 case Opt_proto:
1258 string = match_strdup(args);
1259 if (string == NULL)
1260 goto out_nomem;
1261 token = match_token(string,
1262 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001263
Jeff Laytonee671b02009-12-03 15:58:56 -05001264 protofamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001265 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001266 case Opt_xprt_udp6:
1267 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001268 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001269 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001270 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001271 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001272 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001273 case Opt_xprt_tcp6:
1274 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001275 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001276 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001277 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001278 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001279 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001280 case Opt_xprt_rdma:
1281 /* vector side protocols to TCP */
1282 mnt->flags |= NFS_MOUNT_TCP;
1283 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001284 xprt_load_transport(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001285 kfree(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001286 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001287 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001288 dfprintk(MOUNT, "NFS: unrecognized "
1289 "transport protocol\n");
Yinghai Lu4223a4a2009-10-20 14:13:46 +09001290 kfree(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001291 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001292 }
1293 break;
1294 case Opt_mountproto:
1295 string = match_strdup(args);
1296 if (string == NULL)
1297 goto out_nomem;
1298 token = match_token(string,
1299 nfs_xprt_protocol_tokens, args);
Trond Myklebustd508afb2009-04-06 16:41:35 -07001300 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001301
Jeff Laytonee671b02009-12-03 15:58:56 -05001302 mountfamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001303 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001304 case Opt_xprt_udp6:
1305 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001306 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001307 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001308 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001309 case Opt_xprt_tcp6:
1310 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001311 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001312 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001313 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001314 case Opt_xprt_rdma: /* not used for side protocols */
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_addr:
1322 string = match_strdup(args);
1323 if (string == NULL)
1324 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001325 mnt->nfs_server.addrlen =
1326 rpc_pton(string, strlen(string),
1327 (struct sockaddr *)
1328 &mnt->nfs_server.address,
1329 sizeof(mnt->nfs_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001330 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001331 if (mnt->nfs_server.addrlen == 0)
1332 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001333 break;
1334 case Opt_clientaddr:
1335 string = match_strdup(args);
1336 if (string == NULL)
1337 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001338 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001339 mnt->client_address = string;
1340 break;
Chuck Lever33832032007-12-10 14:59:13 -05001341 case Opt_mounthost:
1342 string = match_strdup(args);
1343 if (string == NULL)
1344 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001345 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001346 mnt->mount_server.hostname = string;
1347 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001348 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001349 string = match_strdup(args);
1350 if (string == NULL)
1351 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001352 mnt->mount_server.addrlen =
1353 rpc_pton(string, strlen(string),
1354 (struct sockaddr *)
1355 &mnt->mount_server.address,
1356 sizeof(mnt->mount_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001357 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001358 if (mnt->mount_server.addrlen == 0)
1359 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001360 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001361 case Opt_lookupcache:
1362 string = match_strdup(args);
1363 if (string == NULL)
1364 goto out_nomem;
1365 token = match_token(string,
1366 nfs_lookupcache_tokens, args);
1367 kfree(string);
1368 switch (token) {
1369 case Opt_lookupcache_all:
1370 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1371 break;
1372 case Opt_lookupcache_positive:
1373 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1374 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1375 break;
1376 case Opt_lookupcache_none:
1377 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1378 break;
1379 default:
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001380 dfprintk(MOUNT, "NFS: invalid "
1381 "lookupcache argument\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001382 return 0;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001383 };
1384 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001385
Chuck Leverf45663c2008-06-24 19:28:02 -04001386 /*
1387 * Special options
1388 */
1389 case Opt_sloppy:
1390 sloppy = 1;
1391 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1392 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001393 case Opt_userspace:
1394 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001395 dfprintk(MOUNT, "NFS: ignoring mount option "
1396 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001397 break;
1398
1399 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001400 invalid_option = 1;
Chuck Leverf45663c2008-06-24 19:28:02 -04001401 dfprintk(MOUNT, "NFS: unrecognized mount option "
1402 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001403 }
1404 }
1405
Chuck Leverd23c45f2009-06-17 18:02:13 -07001406 if (!sloppy && invalid_option)
1407 return 0;
1408
Jeff Laytonee671b02009-12-03 15:58:56 -05001409 /*
1410 * verify that any proto=/mountproto= options match the address
1411 * familiies in the addr=/mountaddr= options.
1412 */
1413 if (protofamily != AF_UNSPEC &&
1414 protofamily != mnt->nfs_server.address.ss_family)
1415 goto out_proto_mismatch;
1416
1417 if (mountfamily != AF_UNSPEC) {
1418 if (mnt->mount_server.addrlen) {
1419 if (mountfamily != mnt->mount_server.address.ss_family)
1420 goto out_mountproto_mismatch;
1421 } else {
1422 if (mountfamily != mnt->nfs_server.address.ss_family)
1423 goto out_mountproto_mismatch;
1424 }
1425 }
1426
Chuck Leverbf0fd762007-07-01 12:13:44 -04001427 return 1;
1428
Jeff Laytonee671b02009-12-03 15:58:56 -05001429out_mountproto_mismatch:
1430 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1431 "option\n");
1432 return 0;
1433out_proto_mismatch:
1434 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1435 return 0;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001436out_invalid_address:
1437 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1438 return 0;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001439out_invalid_value:
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001440 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001441 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001442out_nomem:
1443 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1444 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001445out_security_failure:
1446 free_secdata(secdata);
1447 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1448 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001449}
1450
1451/*
Chuck Leverec88f282009-08-09 15:09:32 -04001452 * Match the requested auth flavors with the list returned by
1453 * the server. Returns zero and sets the mount's authentication
1454 * flavor on success; returns -EACCES if server does not support
1455 * the requested flavor.
1456 */
1457static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1458 struct nfs_mount_request *request)
1459{
1460 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1461
1462 /*
Chuck Lever5eecfde2009-08-21 17:50:30 -04001463 * Certain releases of Linux's mountd return an empty
1464 * flavor list. To prevent behavioral regression with
1465 * these servers (ie. rejecting mounts that used to
1466 * succeed), revert to pre-2.6.32 behavior (no checking)
1467 * if the returned flavor list is empty.
1468 */
1469 if (server_authlist_len == 0)
1470 return 0;
1471
1472 /*
Chuck Leverec88f282009-08-09 15:09:32 -04001473 * We avoid sophisticated negotiating here, as there are
1474 * plenty of cases where we can get it wrong, providing
1475 * either too little or too much security.
1476 *
1477 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1478 * flavor listed first. However, some servers list
1479 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1480 * preferred default, in args->auth_flavors[0] if user
1481 * didn't specify sec= mount option.
1482 */
1483 for (i = 0; i < args->auth_flavor_len; i++)
1484 for (j = 0; j < server_authlist_len; j++)
1485 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1486 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1487 request->auth_flavs[j]);
1488 args->auth_flavors[0] = request->auth_flavs[j];
1489 return 0;
1490 }
1491
1492 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1493 nfs_umount(request);
1494 return -EACCES;
1495}
1496
1497/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001498 * Use the remote server's MOUNT service to request the NFS file handle
1499 * corresponding to the provided path.
1500 */
1501static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1502 struct nfs_fh *root_fh)
1503{
Chuck Leverec88f282009-08-09 15:09:32 -04001504 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1505 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
Chuck Leverc5d120f2008-12-23 15:21:35 -05001506 struct nfs_mount_request request = {
1507 .sap = (struct sockaddr *)
1508 &args->mount_server.address,
1509 .dirpath = args->nfs_server.export_path,
1510 .protocol = args->mount_server.protocol,
1511 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001512 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverec88f282009-08-09 15:09:32 -04001513 .auth_flav_len = &server_authlist_len,
1514 .auth_flavs = server_authlist,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001515 };
Chuck Lever4c568012007-12-10 14:59:28 -05001516 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001517
1518 if (args->mount_server.version == 0) {
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001519 switch (args->version) {
1520 default:
1521 args->mount_server.version = NFS_MNT3_VERSION;
1522 break;
1523 case 2:
1524 args->mount_server.version = NFS_MNT_VERSION;
1525 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001526 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001527 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001528
Chuck Lever33832032007-12-10 14:59:13 -05001529 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001530 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001531 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001532 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001533
Chuck Lever0076d7b2007-07-01 12:13:49 -04001534 /*
1535 * Construct the mount server's address.
1536 */
Chuck Lever4c568012007-12-10 14:59:28 -05001537 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001538 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001539 args->nfs_server.addrlen);
1540 args->mount_server.addrlen = args->nfs_server.addrlen;
1541 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001542 request.salen = args->mount_server.addrlen;
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001543 nfs_set_port(request.sap, &args->mount_server.port, 0);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001544
1545 /*
1546 * Now ask the mount server to map our export path
1547 * to a file handle.
1548 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001549 status = nfs_mount(&request);
Chuck Leverec88f282009-08-09 15:09:32 -04001550 if (status != 0) {
1551 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1552 request.hostname, status);
1553 return status;
1554 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001555
Chuck Leverec88f282009-08-09 15:09:32 -04001556 /*
1557 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1558 */
1559 if (args->mount_server.version != NFS_MNT3_VERSION)
1560 return 0;
1561 return nfs_walk_authlist(args, &request);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001562}
1563
Chuck Leverd1aa0822008-06-23 12:36:45 -04001564static int nfs_parse_simple_hostname(const char *dev_name,
1565 char **hostname, size_t maxnamlen,
1566 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001567{
1568 size_t len;
1569 char *colon, *comma;
1570
1571 colon = strchr(dev_name, ':');
1572 if (colon == NULL)
1573 goto out_bad_devname;
1574
1575 len = colon - dev_name;
1576 if (len > maxnamlen)
1577 goto out_hostname;
1578
1579 /* N.B. caller will free nfs_server.hostname in all cases */
1580 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1581 if (!*hostname)
1582 goto out_nomem;
1583
1584 /* kill possible hostname list: not supported */
1585 comma = strchr(*hostname, ',');
1586 if (comma != NULL) {
1587 if (comma == *hostname)
1588 goto out_bad_devname;
1589 *comma = '\0';
1590 }
1591
1592 colon++;
1593 len = strlen(colon);
1594 if (len > maxpathlen)
1595 goto out_path;
1596 *export_path = kstrndup(colon, len, GFP_KERNEL);
1597 if (!*export_path)
1598 goto out_nomem;
1599
1600 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1601 return 0;
1602
1603out_bad_devname:
1604 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1605 return -EINVAL;
1606
1607out_nomem:
1608 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1609 return -ENOMEM;
1610
1611out_hostname:
1612 dfprintk(MOUNT, "NFS: server hostname too long\n");
1613 return -ENAMETOOLONG;
1614
1615out_path:
1616 dfprintk(MOUNT, "NFS: export pathname too long\n");
1617 return -ENAMETOOLONG;
1618}
1619
1620/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001621 * Hostname has square brackets around it because it contains one or
1622 * more colons. We look for the first closing square bracket, and a
1623 * colon must follow it.
1624 */
1625static int nfs_parse_protected_hostname(const char *dev_name,
1626 char **hostname, size_t maxnamlen,
1627 char **export_path, size_t maxpathlen)
1628{
1629 size_t len;
1630 char *start, *end;
1631
1632 start = (char *)(dev_name + 1);
1633
1634 end = strchr(start, ']');
1635 if (end == NULL)
1636 goto out_bad_devname;
1637 if (*(end + 1) != ':')
1638 goto out_bad_devname;
1639
1640 len = end - start;
1641 if (len > maxnamlen)
1642 goto out_hostname;
1643
1644 /* N.B. caller will free nfs_server.hostname in all cases */
1645 *hostname = kstrndup(start, len, GFP_KERNEL);
1646 if (*hostname == NULL)
1647 goto out_nomem;
1648
1649 end += 2;
1650 len = strlen(end);
1651 if (len > maxpathlen)
1652 goto out_path;
1653 *export_path = kstrndup(end, len, GFP_KERNEL);
1654 if (!*export_path)
1655 goto out_nomem;
1656
1657 return 0;
1658
1659out_bad_devname:
1660 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1661 return -EINVAL;
1662
1663out_nomem:
1664 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1665 return -ENOMEM;
1666
1667out_hostname:
1668 dfprintk(MOUNT, "NFS: server hostname too long\n");
1669 return -ENAMETOOLONG;
1670
1671out_path:
1672 dfprintk(MOUNT, "NFS: export pathname too long\n");
1673 return -ENAMETOOLONG;
1674}
1675
1676/*
1677 * Split "dev_name" into "hostname:export_path".
1678 *
1679 * The leftmost colon demarks the split between the server's hostname
1680 * and the export path. If the hostname starts with a left square
1681 * bracket, then it may contain colons.
1682 *
1683 * Note: caller frees hostname and export path, even on error.
1684 */
1685static int nfs_parse_devname(const char *dev_name,
1686 char **hostname, size_t maxnamlen,
1687 char **export_path, size_t maxpathlen)
1688{
1689 if (*dev_name == '[')
1690 return nfs_parse_protected_hostname(dev_name,
1691 hostname, maxnamlen,
1692 export_path, maxpathlen);
1693
1694 return nfs_parse_simple_hostname(dev_name,
1695 hostname, maxnamlen,
1696 export_path, maxpathlen);
1697}
1698
1699/*
David Howells54ceac42006-08-22 20:06:13 -04001700 * Validate the NFS2/NFS3 mount data
1701 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001702 *
1703 * For option strings, user space handles the following behaviors:
1704 *
1705 * + DNS: mapping server host name to IP address ("addr=" option)
1706 *
1707 * + failure mode: how to behave if a mount request can't be handled
1708 * immediately ("fg/bg" option)
1709 *
1710 * + retry: how often to retry a mount request ("retry=" option)
1711 *
1712 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1713 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001714 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001715static int nfs_validate_mount_data(void *options,
1716 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001717 struct nfs_fh *mntfh,
1718 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001719{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001720 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001721 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
Chuck Lever136d5582007-07-01 12:13:54 -04001722
Chuck Lever5df36e72007-07-01 12:12:56 -04001723 if (data == NULL)
1724 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001725
David Howellsf7b422b2006-06-09 09:34:33 -04001726 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001727 case 1:
1728 data->namlen = 0;
1729 case 2:
1730 data->bsize = 0;
1731 case 3:
1732 if (data->flags & NFS_MOUNT_VER3)
1733 goto out_no_v3;
1734 data->root.size = NFS2_FHSIZE;
1735 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1736 case 4:
1737 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1738 goto out_no_sec;
1739 case 5:
1740 memset(data->context, 0, sizeof(data->context));
1741 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001742 if (data->flags & NFS_MOUNT_VER3) {
1743 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1744 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001745 mntfh->size = data->root.size;
Chuck Lever764302c2009-09-08 19:50:03 -04001746 args->version = 3;
1747 } else {
Chuck Lever5df36e72007-07-01 12:12:56 -04001748 mntfh->size = NFS2_FHSIZE;
Chuck Lever764302c2009-09-08 19:50:03 -04001749 args->version = 2;
1750 }
Chuck Lever5df36e72007-07-01 12:12:56 -04001751
Chuck Lever5df36e72007-07-01 12:12:56 -04001752
1753 memcpy(mntfh->data, data->root.data, mntfh->size);
1754 if (mntfh->size < sizeof(mntfh->data))
1755 memset(mntfh->data + mntfh->size, 0,
1756 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001757
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001758 /*
1759 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1760 * can deal with.
1761 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001762 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001763 args->rsize = data->rsize;
1764 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001765 args->timeo = data->timeo;
1766 args->retrans = data->retrans;
1767 args->acregmin = data->acregmin;
1768 args->acregmax = data->acregmax;
1769 args->acdirmin = data->acdirmin;
1770 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001771
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001772 memcpy(sap, &data->addr, sizeof(data->addr));
Chuck Lever4c568012007-12-10 14:59:28 -05001773 args->nfs_server.addrlen = sizeof(data->addr);
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001774 if (!nfs_verify_server_address(sap))
Chuck Lever4c568012007-12-10 14:59:28 -05001775 goto out_no_address;
1776
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001777 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001778 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001779 /* N.B. caller will free nfs_server.hostname in all cases */
1780 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1781 args->namlen = data->namlen;
1782 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001783
1784 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1785 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001786 if (!args->nfs_server.hostname)
1787 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001788
1789 /*
1790 * The legacy version 6 binary mount data from userspace has a
1791 * field used only to transport selinux information into the
1792 * the kernel. To continue to support that functionality we
1793 * have a touch of selinux knowledge here in the NFS code. The
1794 * userspace code converted context=blah to just blah so we are
1795 * converting back to the full string selinux understands.
1796 */
1797 if (data->context[0]){
1798#ifdef CONFIG_SECURITY_SELINUX
1799 int rc;
1800 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1801 if (!opts_str)
1802 return -ENOMEM;
1803 strcpy(opts_str, "context=");
1804 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1805 strcat(opts_str, &data->context[0]);
1806 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1807 kfree(opts_str);
1808 if (rc)
1809 return rc;
1810#else
1811 return -EINVAL;
1812#endif
1813 }
1814
Chuck Lever5df36e72007-07-01 12:12:56 -04001815 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001816 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001817 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001818
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001819 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001820 return -EINVAL;
1821
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001822 if (!nfs_verify_server_address(sap))
Chuck Lever6e88e062007-09-24 15:39:50 -04001823 goto out_no_address;
1824
Chuck Lever764302c2009-09-08 19:50:03 -04001825 if (args->version == 4)
1826#ifdef CONFIG_NFS_V4
1827 return nfs4_validate_text_mount_data(options,
1828 args, dev_name);
1829#else
1830 goto out_v4_not_compiled;
1831#endif
1832
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001833 nfs_set_port(sap, &args->nfs_server.port, 0);
Chuck Levered596a82008-06-26 17:47:05 -04001834
Trond Myklebust259875e2008-07-02 14:43:47 -04001835 nfs_set_mount_transport_protocol(args);
1836
Chuck Leverdc045892008-06-23 12:36:37 -04001837 status = nfs_parse_devname(dev_name,
1838 &args->nfs_server.hostname,
1839 PAGE_SIZE,
1840 &args->nfs_server.export_path,
1841 NFS_MAXPATHLEN);
1842 if (!status)
1843 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001844
Chuck Leverdc045892008-06-23 12:36:37 -04001845 kfree(args->nfs_server.export_path);
1846 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001847
Chuck Lever136d5582007-07-01 12:13:54 -04001848 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001849 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001850
Chuck Lever136d5582007-07-01 12:13:54 -04001851 break;
1852 }
David Howellsf7b422b2006-06-09 09:34:33 -04001853 }
David Howells54ceac42006-08-22 20:06:13 -04001854
David Howellsf7b422b2006-06-09 09:34:33 -04001855#ifndef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001856 if (args->version == 3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001857 goto out_v3_not_compiled;
1858#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001859
David Howells54ceac42006-08-22 20:06:13 -04001860 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001861
1862out_no_data:
1863 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1864 return -EINVAL;
1865
1866out_no_v3:
1867 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1868 data->version);
1869 return -EINVAL;
1870
1871out_no_sec:
1872 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1873 return -EINVAL;
1874
Chuck Lever5df36e72007-07-01 12:12:56 -04001875#ifndef CONFIG_NFS_V3
1876out_v3_not_compiled:
1877 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1878 return -EPROTONOSUPPORT;
1879#endif /* !CONFIG_NFS_V3 */
1880
Chuck Lever764302c2009-09-08 19:50:03 -04001881#ifndef CONFIG_NFS_V4
1882out_v4_not_compiled:
1883 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1884 return -EPROTONOSUPPORT;
1885#endif /* !CONFIG_NFS_V4 */
1886
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001887out_nomem:
1888 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1889 return -ENOMEM;
1890
Chuck Lever5df36e72007-07-01 12:12:56 -04001891out_no_address:
1892 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1893 return -EINVAL;
1894
1895out_invalid_fh:
1896 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1897 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001898}
1899
Jeff Layton48b605f2008-06-10 15:38:39 -04001900static int
1901nfs_compare_remount_data(struct nfs_server *nfss,
1902 struct nfs_parsed_mount_data *data)
1903{
1904 if (data->flags != nfss->flags ||
1905 data->rsize != nfss->rsize ||
1906 data->wsize != nfss->wsize ||
1907 data->retrans != nfss->client->cl_timeout->to_retries ||
1908 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1909 data->acregmin != nfss->acregmin / HZ ||
1910 data->acregmax != nfss->acregmax / HZ ||
1911 data->acdirmin != nfss->acdirmin / HZ ||
1912 data->acdirmax != nfss->acdirmax / HZ ||
1913 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04001914 data->nfs_server.port != nfss->port ||
Jeff Layton48b605f2008-06-10 15:38:39 -04001915 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
Stefan Richtera1be9ee2009-10-12 11:26:12 -04001916 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
1917 (struct sockaddr *)&nfss->nfs_client->cl_addr))
Jeff Layton48b605f2008-06-10 15:38:39 -04001918 return -EINVAL;
1919
1920 return 0;
1921}
1922
1923static int
1924nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1925{
1926 int error;
1927 struct nfs_server *nfss = sb->s_fs_info;
1928 struct nfs_parsed_mount_data *data;
1929 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1930 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001931 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001932
1933 /*
1934 * Userspace mount programs that send binary options generally send
1935 * them populated with default values. We have no way to know which
1936 * ones were explicitly specified. Fall back to legacy behavior and
1937 * just return success.
1938 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001939 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1940 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1941 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001942 return 0;
1943
1944 data = kzalloc(sizeof(*data), GFP_KERNEL);
1945 if (data == NULL)
1946 return -ENOMEM;
1947
1948 /* fill out struct with values from existing mount */
1949 data->flags = nfss->flags;
1950 data->rsize = nfss->rsize;
1951 data->wsize = nfss->wsize;
1952 data->retrans = nfss->client->cl_timeout->to_retries;
1953 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1954 data->acregmin = nfss->acregmin / HZ;
1955 data->acregmax = nfss->acregmax / HZ;
1956 data->acdirmin = nfss->acdirmin / HZ;
1957 data->acdirmax = nfss->acdirmax / HZ;
1958 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04001959 data->nfs_server.port = nfss->port;
Jeff Layton48b605f2008-06-10 15:38:39 -04001960 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1961 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1962 data->nfs_server.addrlen);
1963
1964 /* overwrite those values with any that were specified */
1965 error = nfs_parse_mount_options((char *)options, data);
1966 if (error < 0)
1967 goto out;
1968
1969 /* compare new mount options with old ones */
1970 error = nfs_compare_remount_data(nfss, data);
1971out:
1972 kfree(data);
1973 return error;
1974}
1975
David Howells54ceac42006-08-22 20:06:13 -04001976/*
1977 * Initialise the common bits of the superblock
1978 */
1979static inline void nfs_initialise_sb(struct super_block *sb)
1980{
1981 struct nfs_server *server = NFS_SB(sb);
1982
1983 sb->s_magic = NFS_SUPER_MAGIC;
1984
1985 /* We probably want something more informative here */
1986 snprintf(sb->s_id, sizeof(sb->s_id),
1987 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1988
1989 if (sb->s_blocksize == 0)
1990 sb->s_blocksize = nfs_block_bits(server->wsize,
1991 &sb->s_blocksize_bits);
1992
1993 if (server->flags & NFS_MOUNT_NOAC)
1994 sb->s_flags |= MS_SYNCHRONOUS;
1995
Jens Axboe32a88aa2009-09-16 15:02:33 +02001996 sb->s_bdi = &server->backing_dev_info;
1997
David Howells54ceac42006-08-22 20:06:13 -04001998 nfs_super_set_maxbytes(sb, server->maxfilesize);
1999}
2000
2001/*
2002 * Finish setting up an NFS2/3 superblock
2003 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04002004static void nfs_fill_super(struct super_block *sb,
2005 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04002006{
2007 struct nfs_server *server = NFS_SB(sb);
2008
2009 sb->s_blocksize_bits = 0;
2010 sb->s_blocksize = 0;
2011 if (data->bsize)
2012 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2013
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002014 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002015 /* The VFS shouldn't apply the umask to mode bits. We will do
2016 * so ourselves when necessary.
2017 */
2018 sb->s_flags |= MS_POSIXACL;
2019 sb->s_time_gran = 1;
2020 }
2021
2022 sb->s_op = &nfs_sops;
2023 nfs_initialise_sb(sb);
2024}
2025
2026/*
2027 * Finish setting up a cloned NFS2/3 superblock
2028 */
2029static void nfs_clone_super(struct super_block *sb,
2030 const struct super_block *old_sb)
2031{
2032 struct nfs_server *server = NFS_SB(sb);
2033
2034 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2035 sb->s_blocksize = old_sb->s_blocksize;
2036 sb->s_maxbytes = old_sb->s_maxbytes;
2037
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002038 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002039 /* The VFS shouldn't apply the umask to mode bits. We will do
2040 * so ourselves when necessary.
2041 */
2042 sb->s_flags |= MS_POSIXACL;
2043 sb->s_time_gran = 1;
2044 }
2045
2046 sb->s_op = old_sb->s_op;
2047 nfs_initialise_sb(sb);
2048}
2049
Trond Myklebust275a5d22007-05-16 16:53:28 -04002050static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2051{
2052 const struct nfs_server *a = s->s_fs_info;
2053 const struct rpc_clnt *clnt_a = a->client;
2054 const struct rpc_clnt *clnt_b = b->client;
2055
2056 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2057 goto Ebusy;
2058 if (a->nfs_client != b->nfs_client)
2059 goto Ebusy;
2060 if (a->flags != b->flags)
2061 goto Ebusy;
2062 if (a->wsize != b->wsize)
2063 goto Ebusy;
2064 if (a->rsize != b->rsize)
2065 goto Ebusy;
2066 if (a->acregmin != b->acregmin)
2067 goto Ebusy;
2068 if (a->acregmax != b->acregmax)
2069 goto Ebusy;
2070 if (a->acdirmin != b->acdirmin)
2071 goto Ebusy;
2072 if (a->acdirmax != b->acdirmax)
2073 goto Ebusy;
2074 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2075 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002076 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04002077Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002078 return 0;
2079}
2080
2081struct nfs_sb_mountdata {
2082 struct nfs_server *server;
2083 int mntflags;
2084};
2085
2086static int nfs_set_super(struct super_block *s, void *data)
2087{
2088 struct nfs_sb_mountdata *sb_mntdata = data;
2089 struct nfs_server *server = sb_mntdata->server;
2090 int ret;
2091
2092 s->s_flags = sb_mntdata->mntflags;
2093 s->s_fs_info = server;
2094 ret = set_anon_super(s, server);
2095 if (ret == 0)
2096 server->s_dev = s->s_dev;
2097 return ret;
2098}
2099
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002100static int nfs_compare_super_address(struct nfs_server *server1,
2101 struct nfs_server *server2)
2102{
2103 struct sockaddr *sap1, *sap2;
2104
2105 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2106 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2107
2108 if (sap1->sa_family != sap2->sa_family)
2109 return 0;
2110
2111 switch (sap1->sa_family) {
2112 case AF_INET: {
2113 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2114 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2115 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2116 return 0;
2117 if (sin1->sin_port != sin2->sin_port)
2118 return 0;
2119 break;
2120 }
2121 case AF_INET6: {
2122 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2123 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2124 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2125 return 0;
2126 if (sin1->sin6_port != sin2->sin6_port)
2127 return 0;
2128 break;
2129 }
2130 default:
2131 return 0;
2132 }
2133
2134 return 1;
2135}
2136
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002137static int nfs_compare_super(struct super_block *sb, void *data)
2138{
2139 struct nfs_sb_mountdata *sb_mntdata = data;
2140 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2141 int mntflags = sb_mntdata->mntflags;
2142
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002143 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002144 return 0;
2145 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2146 if (old->flags & NFS_MOUNT_UNSHARED)
2147 return 0;
2148 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2149 return 0;
2150 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002151}
2152
Miklos Szeredifa799752008-04-30 00:54:33 -07002153static int nfs_bdi_register(struct nfs_server *server)
2154{
2155 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2156}
2157
David Howells54ceac42006-08-22 20:06:13 -04002158static int nfs_get_sb(struct file_system_type *fs_type,
2159 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2160{
2161 struct nfs_server *server = NULL;
2162 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002163 struct nfs_parsed_mount_data *data;
2164 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002165 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002166 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002167 struct nfs_sb_mountdata sb_mntdata = {
2168 .mntflags = flags,
2169 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002170 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002171
Trond Myklebustc5811db2009-10-06 15:40:15 -04002172 data = nfs_alloc_parsed_mount_data(3);
Trond Myklebust33852a12008-06-19 14:20:11 -04002173 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2174 if (data == NULL || mntfh == NULL)
2175 goto out_free_fh;
2176
2177 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002178
David Howells54ceac42006-08-22 20:06:13 -04002179 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002180 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002181 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002182 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002183
Chuck Lever764302c2009-09-08 19:50:03 -04002184#ifdef CONFIG_NFS_V4
2185 if (data->version == 4) {
2186 error = nfs4_try_mount(flags, dev_name, data, mnt);
2187 kfree(data->client_address);
2188 goto out;
2189 }
2190#endif /* CONFIG_NFS_V4 */
2191
David Howells54ceac42006-08-22 20:06:13 -04002192 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002193 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002194 if (IS_ERR(server)) {
2195 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002196 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002197 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002198 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002199
Trond Myklebust75180df2007-05-16 16:53:28 -04002200 if (server->flags & NFS_MOUNT_UNSHARED)
2201 compare_super = NULL;
2202
David Howells54ceac42006-08-22 20:06:13 -04002203 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002204 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002205 if (IS_ERR(s)) {
2206 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002207 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002208 }
2209
David Howells54ceac42006-08-22 20:06:13 -04002210 if (s->s_fs_info != server) {
2211 nfs_free_server(server);
2212 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002213 } else {
2214 error = nfs_bdi_register(server);
2215 if (error)
2216 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002217 }
David Howells54ceac42006-08-22 20:06:13 -04002218
2219 if (!s->s_root) {
2220 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002221 nfs_fill_super(s, data);
David Howells2df54802009-09-23 14:36:39 -04002222 nfs_fscache_get_super_cookie(
2223 s, data ? data->fscache_uniq : NULL, NULL);
David Howells54ceac42006-08-22 20:06:13 -04002224 }
2225
Trond Myklebust33852a12008-06-19 14:20:11 -04002226 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002227 if (IS_ERR(mntroot)) {
2228 error = PTR_ERR(mntroot);
2229 goto error_splat_super;
2230 }
2231
Trond Myklebust33852a12008-06-19 14:20:11 -04002232 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002233 if (error)
2234 goto error_splat_root;
2235
David Howellsf7b422b2006-06-09 09:34:33 -04002236 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002237 mnt->mnt_sb = s;
2238 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002239 error = 0;
2240
2241out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002242 kfree(data->nfs_server.hostname);
2243 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002244 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002245 security_free_mnt_opts(&data->lsm_opts);
2246out_free_fh:
2247 kfree(mntfh);
2248 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002249 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002250
David Howells54ceac42006-08-22 20:06:13 -04002251out_err_nosb:
2252 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002253 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002254
Eric Parisf9c3a382008-03-05 14:20:18 -05002255error_splat_root:
2256 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002257error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002258 deactivate_locked_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002259 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002260}
2261
David Howells54ceac42006-08-22 20:06:13 -04002262/*
2263 * Destroy an NFS2/3 superblock
2264 */
David Howellsf7b422b2006-06-09 09:34:33 -04002265static void nfs_kill_super(struct super_block *s)
2266{
2267 struct nfs_server *server = NFS_SB(s);
2268
2269 kill_anon_super(s);
Jens Axboe92f25052009-09-17 14:51:44 +02002270 bdi_unregister(&server->backing_dev_info);
David Howells08734042009-04-03 16:42:42 +01002271 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002272 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002273}
2274
David Howells54ceac42006-08-22 20:06:13 -04002275/*
2276 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2277 */
2278static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2279 const char *dev_name, void *raw_data,
2280 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002281{
2282 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002283 struct super_block *s;
2284 struct nfs_server *server;
2285 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002286 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002287 struct nfs_sb_mountdata sb_mntdata = {
2288 .mntflags = flags,
2289 };
David Howells54ceac42006-08-22 20:06:13 -04002290 int error;
2291
2292 dprintk("--> nfs_xdev_get_sb()\n");
2293
2294 /* create a new volume representation */
2295 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2296 if (IS_ERR(server)) {
2297 error = PTR_ERR(server);
2298 goto out_err_noserver;
2299 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002300 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002301
Trond Myklebust75180df2007-05-16 16:53:28 -04002302 if (server->flags & NFS_MOUNT_UNSHARED)
2303 compare_super = NULL;
2304
David Howells54ceac42006-08-22 20:06:13 -04002305 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002306 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002307 if (IS_ERR(s)) {
2308 error = PTR_ERR(s);
2309 goto out_err_nosb;
2310 }
2311
2312 if (s->s_fs_info != server) {
2313 nfs_free_server(server);
2314 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002315 } else {
2316 error = nfs_bdi_register(server);
2317 if (error)
2318 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002319 }
2320
2321 if (!s->s_root) {
2322 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002323 nfs_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002324 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002325 }
2326
2327 mntroot = nfs_get_root(s, data->fh);
2328 if (IS_ERR(mntroot)) {
2329 error = PTR_ERR(mntroot);
2330 goto error_splat_super;
2331 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002332 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002333 dput(mntroot);
2334 error = -ESTALE;
2335 goto error_splat_super;
2336 }
David Howells54ceac42006-08-22 20:06:13 -04002337
2338 s->s_flags |= MS_ACTIVE;
2339 mnt->mnt_sb = s;
2340 mnt->mnt_root = mntroot;
2341
Eric Parisf9c3a382008-03-05 14:20:18 -05002342 /* clone any lsm security options from the parent to the new sb */
2343 security_sb_clone_mnt_opts(data->sb, s);
2344
David Howells54ceac42006-08-22 20:06:13 -04002345 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2346 return 0;
2347
2348out_err_nosb:
2349 nfs_free_server(server);
2350out_err_noserver:
2351 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2352 return error;
2353
2354error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002355 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002356 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2357 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002358}
2359
2360#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002361
2362/*
2363 * Finish setting up a cloned NFS4 superblock
2364 */
2365static void nfs4_clone_super(struct super_block *sb,
2366 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002367{
David Howells54ceac42006-08-22 20:06:13 -04002368 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2369 sb->s_blocksize = old_sb->s_blocksize;
2370 sb->s_maxbytes = old_sb->s_maxbytes;
2371 sb->s_time_gran = 1;
2372 sb->s_op = old_sb->s_op;
2373 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002374}
2375
2376/*
2377 * Set up an NFS4 superblock
2378 */
David Howells54ceac42006-08-22 20:06:13 -04002379static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002380{
David Howellsf7b422b2006-06-09 09:34:33 -04002381 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002382 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002383 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002384}
2385
Trond Myklebust01c3f052009-06-17 13:22:58 -07002386static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2387{
2388 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
2389}
2390
Chuck Lever7630c852009-09-08 19:49:57 -04002391static int nfs4_validate_text_mount_data(void *options,
2392 struct nfs_parsed_mount_data *args,
2393 const char *dev_name)
2394{
2395 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2396
Trond Myklebustf5855fe2009-10-06 15:40:37 -04002397 nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
Chuck Lever7630c852009-09-08 19:49:57 -04002398
2399 nfs_validate_transport_protocol(args);
2400
2401 nfs4_validate_mount_flags(args);
2402
Trond Myklebust2ecda722009-09-08 19:50:07 -04002403 if (args->version != 4) {
2404 dfprintk(MOUNT,
2405 "NFS4: Illegal mount version\n");
2406 return -EINVAL;
2407 }
2408
Chuck Lever7630c852009-09-08 19:49:57 -04002409 if (args->auth_flavor_len > 1) {
2410 dfprintk(MOUNT,
2411 "NFS4: Too many RPC auth flavours specified\n");
2412 return -EINVAL;
2413 }
2414
2415 if (args->client_address == NULL) {
2416 dfprintk(MOUNT,
2417 "NFS4: mount program didn't pass callback address\n");
2418 return -EINVAL;
2419 }
2420
2421 return nfs_parse_devname(dev_name,
2422 &args->nfs_server.hostname,
2423 NFS4_MAXNAMLEN,
2424 &args->nfs_server.export_path,
2425 NFS4_MAXPATHLEN);
2426}
2427
David Howells54ceac42006-08-22 20:06:13 -04002428/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002429 * Validate NFSv4 mount options
2430 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002431static int nfs4_validate_mount_data(void *options,
2432 struct nfs_parsed_mount_data *args,
2433 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002434{
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002435 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002436 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002437 char *c;
2438
2439 if (data == NULL)
2440 goto out_no_data;
2441
2442 switch (data->version) {
2443 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002444 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002445 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002446 if (data->host_addrlen == 0)
2447 goto out_no_address;
2448 args->nfs_server.addrlen = data->host_addrlen;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002449 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002450 return -EFAULT;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002451 if (!nfs_verify_server_address(sap))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002452 goto out_no_address;
2453
Chuck Lever6738b252008-06-24 16:33:54 -04002454 if (data->auth_flavourlen) {
2455 if (data->auth_flavourlen > 1)
2456 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002457 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002458 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002459 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002460 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002461 }
2462
2463 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2464 if (IS_ERR(c))
2465 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002466 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002467
2468 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2469 if (IS_ERR(c))
2470 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002471 args->nfs_server.export_path = c;
2472 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002473
2474 c = strndup_user(data->client_addr.data, 16);
2475 if (IS_ERR(c))
2476 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002477 args->client_address = c;
2478
2479 /*
2480 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2481 * can deal with.
2482 */
2483
2484 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2485 args->rsize = data->rsize;
2486 args->wsize = data->wsize;
2487 args->timeo = data->timeo;
2488 args->retrans = data->retrans;
2489 args->acregmin = data->acregmin;
2490 args->acregmax = data->acregmax;
2491 args->acdirmin = data->acdirmin;
2492 args->acdirmax = data->acdirmax;
2493 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002494 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002495
2496 break;
Chuck Lever7630c852009-09-08 19:49:57 -04002497 default:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002498 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002499 return -EINVAL;
2500
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002501 if (!nfs_verify_server_address(sap))
Chuck Lever80071222007-07-01 12:13:59 -04002502 return -EINVAL;
Chuck Levered596a82008-06-26 17:47:05 -04002503
Chuck Lever7630c852009-09-08 19:49:57 -04002504 return nfs4_validate_text_mount_data(options, args, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002505 }
2506
2507 return 0;
2508
2509out_no_data:
2510 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2511 return -EINVAL;
2512
2513out_inval_auth:
2514 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2515 data->auth_flavourlen);
2516 return -EINVAL;
2517
2518out_no_address:
2519 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2520 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002521}
2522
2523/*
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002524 * Get the superblock for the NFS4 root partition
David Howells54ceac42006-08-22 20:06:13 -04002525 */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002526static int nfs4_remote_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -04002527 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002528{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002529 struct nfs_parsed_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002530 struct super_block *s;
2531 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002532 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002533 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002534 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002535 struct nfs_sb_mountdata sb_mntdata = {
2536 .mntflags = flags,
2537 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002538 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002539
Trond Myklebust33852a12008-06-19 14:20:11 -04002540 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2541 if (data == NULL || mntfh == NULL)
2542 goto out_free_fh;
2543
2544 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002545
David Howells54ceac42006-08-22 20:06:13 -04002546 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002547 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002548 if (IS_ERR(server)) {
2549 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002550 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002551 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002552 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002553
Trond Myklebust75180df2007-05-16 16:53:28 -04002554 if (server->flags & NFS4_MOUNT_UNSHARED)
2555 compare_super = NULL;
2556
David Howells54ceac42006-08-22 20:06:13 -04002557 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002558 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002559 if (IS_ERR(s)) {
2560 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002561 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002562 }
2563
Trond Myklebust5dd31772006-08-24 01:03:05 -04002564 if (s->s_fs_info != server) {
2565 nfs_free_server(server);
2566 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002567 } else {
2568 error = nfs_bdi_register(server);
2569 if (error)
2570 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002571 }
2572
David Howells54ceac42006-08-22 20:06:13 -04002573 if (!s->s_root) {
2574 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002575 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002576 nfs_fscache_get_super_cookie(
2577 s, data ? data->fscache_uniq : NULL, NULL);
Trond Myklebust816724e2006-06-24 08:41:41 -04002578 }
David Howellsf7b422b2006-06-09 09:34:33 -04002579
Trond Myklebust33852a12008-06-19 14:20:11 -04002580 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002581 if (IS_ERR(mntroot)) {
2582 error = PTR_ERR(mntroot);
2583 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002584 }
David Howells54ceac42006-08-22 20:06:13 -04002585
Trond Myklebust33852a12008-06-19 14:20:11 -04002586 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002587 if (error)
2588 goto error_splat_root;
2589
David Howellsf7b422b2006-06-09 09:34:33 -04002590 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002591 mnt->mnt_sb = s;
2592 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002593 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002594
Chuck Lever29eb9812007-07-01 12:12:30 -04002595out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002596 security_free_mnt_opts(&data->lsm_opts);
2597out_free_fh:
2598 kfree(mntfh);
Trond Myklebust816724e2006-06-24 08:41:41 -04002599 return error;
David Howells54ceac42006-08-22 20:06:13 -04002600
Chuck Lever29eb9812007-07-01 12:12:30 -04002601out_free:
2602 nfs_free_server(server);
2603 goto out;
2604
Eric Paris46c8ac72008-05-02 13:42:42 -07002605error_splat_root:
2606 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002607error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002608 deactivate_locked_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002609 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002610}
2611
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002612static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2613 int flags, void *data, const char *hostname)
2614{
2615 struct vfsmount *root_mnt;
2616 char *root_devname;
2617 size_t len;
2618
2619 len = strlen(hostname) + 3;
2620 root_devname = kmalloc(len, GFP_KERNEL);
2621 if (root_devname == NULL)
2622 return ERR_PTR(-ENOMEM);
2623 snprintf(root_devname, len, "%s:/", hostname);
2624 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2625 kfree(root_devname);
2626 return root_mnt;
2627}
2628
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002629static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2630{
2631 char *page = (char *) __get_free_page(GFP_KERNEL);
2632 char *devname, *tmp;
2633
2634 if (page == NULL)
2635 return;
2636 devname = nfs_path(path->mnt->mnt_devname,
2637 path->mnt->mnt_root, path->dentry,
2638 page, PAGE_SIZE);
2639 if (devname == NULL)
2640 goto out_freepage;
2641 tmp = kstrdup(devname, GFP_KERNEL);
2642 if (tmp == NULL)
2643 goto out_freepage;
2644 kfree(mnt->mnt_devname);
2645 mnt->mnt_devname = tmp;
2646out_freepage:
2647 free_page((unsigned long)page);
2648}
2649
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002650static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2651 const char *export_path, struct vfsmount *mnt_target)
2652{
2653 struct mnt_namespace *ns_private;
2654 struct nameidata nd;
2655 struct super_block *s;
2656 int ret;
2657
2658 ns_private = create_mnt_ns(root_mnt);
2659 ret = PTR_ERR(ns_private);
2660 if (IS_ERR(ns_private))
2661 goto out_mntput;
2662
2663 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2664 export_path, LOOKUP_FOLLOW, &nd);
2665
2666 put_mnt_ns(ns_private);
2667
2668 if (ret != 0)
2669 goto out_err;
2670
2671 s = nd.path.mnt->mnt_sb;
2672 atomic_inc(&s->s_active);
2673 mnt_target->mnt_sb = s;
2674 mnt_target->mnt_root = dget(nd.path.dentry);
2675
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002676 /* Correct the device pathname */
2677 nfs_fix_devname(&nd.path, mnt_target);
2678
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002679 path_put(&nd.path);
2680 down_write(&s->s_umount);
2681 return 0;
2682out_mntput:
2683 mntput(root_mnt);
2684out_err:
2685 return ret;
2686}
2687
Chuck Levera6fe23b2009-09-08 19:50:00 -04002688static int nfs4_try_mount(int flags, const char *dev_name,
2689 struct nfs_parsed_mount_data *data,
2690 struct vfsmount *mnt)
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002691{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002692 char *export_path;
2693 struct vfsmount *root_mnt;
Chuck Levera6fe23b2009-09-08 19:50:00 -04002694 int error;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002695
Chuck Levera6fe23b2009-09-08 19:50:00 -04002696 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002697
2698 export_path = data->nfs_server.export_path;
2699 data->nfs_server.export_path = "/";
2700 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2701 data->nfs_server.hostname);
2702 data->nfs_server.export_path = export_path;
2703
2704 error = PTR_ERR(root_mnt);
2705 if (IS_ERR(root_mnt))
2706 goto out;
2707
2708 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2709
2710out:
Chuck Levera6fe23b2009-09-08 19:50:00 -04002711 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2712 error != 0 ? " [error]" : "");
2713 return error;
2714}
2715
2716/*
2717 * Get the superblock for an NFS4 mountpoint
2718 */
2719static int nfs4_get_sb(struct file_system_type *fs_type,
2720 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2721{
2722 struct nfs_parsed_mount_data *data;
2723 int error = -ENOMEM;
2724
Trond Myklebustc5811db2009-10-06 15:40:15 -04002725 data = nfs_alloc_parsed_mount_data(4);
Chuck Levera6fe23b2009-09-08 19:50:00 -04002726 if (data == NULL)
2727 goto out_free_data;
2728
2729 /* Validate the mount data */
2730 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2731 if (error < 0)
2732 goto out;
2733
2734 error = nfs4_try_mount(flags, dev_name, data, mnt);
2735
2736out:
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002737 kfree(data->client_address);
2738 kfree(data->nfs_server.export_path);
2739 kfree(data->nfs_server.hostname);
2740 kfree(data->fscache_uniq);
2741out_free_data:
2742 kfree(data);
2743 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2744 error != 0 ? " [error]" : "");
2745 return error;
2746}
2747
David Howellsf7b422b2006-06-09 09:34:33 -04002748static void nfs4_kill_super(struct super_block *sb)
2749{
2750 struct nfs_server *server = NFS_SB(sb);
2751
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002752 dprintk("--> %s\n", __func__);
Trond Myklebust515d8612008-12-23 15:21:46 -05002753 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002754 kill_anon_super(sb);
David Howells08734042009-04-03 16:42:42 +01002755 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002756 nfs_free_server(server);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002757 dprintk("<-- %s\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -04002758}
2759
2760/*
David Howells54ceac42006-08-22 20:06:13 -04002761 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002762 */
David Howells54ceac42006-08-22 20:06:13 -04002763static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2764 const char *dev_name, void *raw_data,
2765 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002766{
2767 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002768 struct super_block *s;
2769 struct nfs_server *server;
2770 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002771 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002772 struct nfs_sb_mountdata sb_mntdata = {
2773 .mntflags = flags,
2774 };
David Howells54ceac42006-08-22 20:06:13 -04002775 int error;
2776
2777 dprintk("--> nfs4_xdev_get_sb()\n");
2778
2779 /* create a new volume representation */
2780 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2781 if (IS_ERR(server)) {
2782 error = PTR_ERR(server);
2783 goto out_err_noserver;
2784 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002785 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002786
Trond Myklebust75180df2007-05-16 16:53:28 -04002787 if (server->flags & NFS4_MOUNT_UNSHARED)
2788 compare_super = NULL;
2789
David Howells54ceac42006-08-22 20:06:13 -04002790 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002791 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002792 if (IS_ERR(s)) {
2793 error = PTR_ERR(s);
2794 goto out_err_nosb;
2795 }
2796
2797 if (s->s_fs_info != server) {
2798 nfs_free_server(server);
2799 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002800 } else {
2801 error = nfs_bdi_register(server);
2802 if (error)
2803 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002804 }
2805
2806 if (!s->s_root) {
2807 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002808 nfs4_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002809 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002810 }
2811
2812 mntroot = nfs4_get_root(s, data->fh);
2813 if (IS_ERR(mntroot)) {
2814 error = PTR_ERR(mntroot);
2815 goto error_splat_super;
2816 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002817 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2818 dput(mntroot);
2819 error = -ESTALE;
2820 goto error_splat_super;
2821 }
David Howells54ceac42006-08-22 20:06:13 -04002822
2823 s->s_flags |= MS_ACTIVE;
2824 mnt->mnt_sb = s;
2825 mnt->mnt_root = mntroot;
2826
Eric Paris46c8ac72008-05-02 13:42:42 -07002827 security_sb_clone_mnt_opts(data->sb, s);
2828
David Howells54ceac42006-08-22 20:06:13 -04002829 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2830 return 0;
2831
2832out_err_nosb:
2833 nfs_free_server(server);
2834out_err_noserver:
2835 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2836 return error;
2837
2838error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002839 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002840 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2841 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002842}
2843
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002844static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
2845 int flags, const char *dev_name, void *raw_data,
2846 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002847{
2848 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002849 struct super_block *s;
2850 struct nfs_server *server;
2851 struct dentry *mntroot;
2852 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002853 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002854 struct nfs_sb_mountdata sb_mntdata = {
2855 .mntflags = flags,
2856 };
David Howells54ceac42006-08-22 20:06:13 -04002857 int error;
2858
2859 dprintk("--> nfs4_referral_get_sb()\n");
2860
2861 /* create a new volume representation */
2862 server = nfs4_create_referral_server(data, &mntfh);
2863 if (IS_ERR(server)) {
2864 error = PTR_ERR(server);
2865 goto out_err_noserver;
2866 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002867 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002868
Trond Myklebust75180df2007-05-16 16:53:28 -04002869 if (server->flags & NFS4_MOUNT_UNSHARED)
2870 compare_super = NULL;
2871
David Howells54ceac42006-08-22 20:06:13 -04002872 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002873 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002874 if (IS_ERR(s)) {
2875 error = PTR_ERR(s);
2876 goto out_err_nosb;
2877 }
2878
2879 if (s->s_fs_info != server) {
2880 nfs_free_server(server);
2881 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002882 } else {
2883 error = nfs_bdi_register(server);
2884 if (error)
2885 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002886 }
2887
2888 if (!s->s_root) {
2889 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002890 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002891 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002892 }
2893
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002894 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002895 if (IS_ERR(mntroot)) {
2896 error = PTR_ERR(mntroot);
2897 goto error_splat_super;
2898 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002899 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2900 dput(mntroot);
2901 error = -ESTALE;
2902 goto error_splat_super;
2903 }
David Howells54ceac42006-08-22 20:06:13 -04002904
2905 s->s_flags |= MS_ACTIVE;
2906 mnt->mnt_sb = s;
2907 mnt->mnt_root = mntroot;
2908
Eric Paris46c8ac72008-05-02 13:42:42 -07002909 security_sb_clone_mnt_opts(data->sb, s);
2910
David Howells54ceac42006-08-22 20:06:13 -04002911 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2912 return 0;
2913
2914out_err_nosb:
2915 nfs_free_server(server);
2916out_err_noserver:
2917 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2918 return error;
2919
2920error_splat_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04002921 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002922 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2923 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002924}
2925
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002926/*
2927 * Create an NFS4 server record on referral traversal
2928 */
2929static int nfs4_referral_get_sb(struct file_system_type *fs_type,
2930 int flags, const char *dev_name, void *raw_data,
2931 struct vfsmount *mnt)
2932{
2933 struct nfs_clone_mount *data = raw_data;
2934 char *export_path;
2935 struct vfsmount *root_mnt;
2936 int error;
2937
2938 dprintk("--> nfs4_referral_get_sb()\n");
2939
2940 export_path = data->mnt_path;
2941 data->mnt_path = "/";
2942
2943 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
2944 flags, data, data->hostname);
2945 data->mnt_path = export_path;
2946
2947 error = PTR_ERR(root_mnt);
2948 if (IS_ERR(root_mnt))
2949 goto out;
2950
2951 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2952out:
2953 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
2954 error != 0 ? " [error]" : "");
2955 return error;
2956}
2957
David Howells54ceac42006-08-22 20:06:13 -04002958#endif /* CONFIG_NFS_V4 */