blob: 6baf9a3934660d9fa3721ee963743c90c8ebd3e8 [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);
Trond Myklebust387c1492010-02-03 08:27:35 -0500246static void nfs_put_super(struct super_block *);
David Howellsf7b422b2006-06-09 09:34:33 -0400247static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400248static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400249
250static struct file_system_type nfs_fs_type = {
251 .owner = THIS_MODULE,
252 .name = "nfs",
253 .get_sb = nfs_get_sb,
254 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700255 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400256};
257
David Howells54ceac42006-08-22 20:06:13 -0400258struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400259 .owner = THIS_MODULE,
260 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400261 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400262 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700263 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400264};
265
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800266static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400267 .alloc_inode = nfs_alloc_inode,
268 .destroy_inode = nfs_destroy_inode,
269 .write_inode = nfs_write_inode,
Trond Myklebust387c1492010-02-03 08:27:35 -0500270 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400271 .statfs = nfs_statfs,
272 .clear_inode = nfs_clear_inode,
273 .umount_begin = nfs_umount_begin,
274 .show_options = nfs_show_options,
275 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400276 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400277};
278
279#ifdef CONFIG_NFS_V4
Chuck Lever7630c852009-09-08 19:49:57 -0400280static int nfs4_validate_text_mount_data(void *options,
281 struct nfs_parsed_mount_data *args, const char *dev_name);
Chuck Levera6fe23b2009-09-08 19:50:00 -0400282static int nfs4_try_mount(int flags, const char *dev_name,
283 struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
Trond Myklebust816724e2006-06-24 08:41:41 -0400284static int nfs4_get_sb(struct file_system_type *fs_type,
285 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400286static int nfs4_remote_get_sb(struct file_system_type *fs_type,
287 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400288static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
289 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
290static int nfs4_referral_get_sb(struct file_system_type *fs_type,
291 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400292static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
293 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400294static void nfs4_kill_super(struct super_block *sb);
295
296static struct file_system_type nfs4_fs_type = {
297 .owner = THIS_MODULE,
298 .name = "nfs4",
299 .get_sb = nfs4_get_sb,
300 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700301 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400302};
303
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400304static struct file_system_type nfs4_remote_fs_type = {
305 .owner = THIS_MODULE,
306 .name = "nfs4",
307 .get_sb = nfs4_remote_get_sb,
308 .kill_sb = nfs4_kill_super,
309 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
310};
311
David Howells54ceac42006-08-22 20:06:13 -0400312struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400313 .owner = THIS_MODULE,
314 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400315 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400316 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700317 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400318};
319
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400320static struct file_system_type nfs4_remote_referral_fs_type = {
321 .owner = THIS_MODULE,
322 .name = "nfs4",
323 .get_sb = nfs4_remote_referral_get_sb,
324 .kill_sb = nfs4_kill_super,
325 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
326};
327
David Howells54ceac42006-08-22 20:06:13 -0400328struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400329 .owner = THIS_MODULE,
330 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400331 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400332 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700333 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400334};
335
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800336static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400337 .alloc_inode = nfs_alloc_inode,
338 .destroy_inode = nfs_destroy_inode,
339 .write_inode = nfs_write_inode,
Trond Myklebust387c1492010-02-03 08:27:35 -0500340 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400341 .statfs = nfs_statfs,
342 .clear_inode = nfs4_clear_inode,
343 .umount_begin = nfs_umount_begin,
344 .show_options = nfs_show_options,
345 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400346 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400347};
348#endif
349
Rusty Russell8e1f9362007-07-17 04:03:17 -0700350static struct shrinker acl_shrinker = {
351 .shrink = nfs_access_cache_shrinker,
352 .seeks = DEFAULT_SEEKS,
353};
Trond Myklebust979df722006-07-25 11:28:19 -0400354
David Howellsf7b422b2006-06-09 09:34:33 -0400355/*
356 * Register the NFS filesystems
357 */
358int __init register_nfs_fs(void)
359{
360 int ret;
361
362 ret = register_filesystem(&nfs_fs_type);
363 if (ret < 0)
364 goto error_0;
365
David Howellsf7b422b2006-06-09 09:34:33 -0400366 ret = nfs_register_sysctl();
367 if (ret < 0)
368 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800369#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400370 ret = register_filesystem(&nfs4_fs_type);
371 if (ret < 0)
372 goto error_2;
373#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700374 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400375 return 0;
376
377#ifdef CONFIG_NFS_V4
378error_2:
379 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800380#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400381error_1:
382 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400383error_0:
384 return ret;
385}
386
387/*
388 * Unregister the NFS filesystems
389 */
390void __exit unregister_nfs_fs(void)
391{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700392 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400393#ifdef CONFIG_NFS_V4
394 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400395#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700396 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400397 unregister_filesystem(&nfs_fs_type);
398}
399
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400400void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500401{
402 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400403
404 if (atomic_inc_return(&server->active) == 1)
405 atomic_inc(&sb->s_active);
406}
407
408void nfs_sb_deactive(struct super_block *sb)
409{
410 struct nfs_server *server = NFS_SB(sb);
411
412 if (atomic_dec_and_test(&server->active))
413 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500414}
415
David Howellsf7b422b2006-06-09 09:34:33 -0400416/*
417 * Deliver file system statistics to userspace
418 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400419static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400420{
David Howells0c7d90c2006-08-22 20:06:10 -0400421 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400422 unsigned char blockbits;
423 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400424 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400425 struct nfs_fattr fattr;
426 struct nfs_fsstat res = {
427 .fattr = &fattr,
428 };
429 int error;
430
David Howells8fa5c002006-08-22 20:06:12 -0400431 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400432 if (error < 0)
433 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700434 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400435
436 /*
437 * Current versions of glibc do not correctly handle the
438 * case where f_frsize != f_bsize. Eventually we want to
439 * report the value of wtmult in this field.
440 */
David Howells0c7d90c2006-08-22 20:06:10 -0400441 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400442
443 /*
444 * On most *nix systems, f_blocks, f_bfree, and f_bavail
445 * are reported in units of f_frsize. Linux hasn't had
446 * an f_frsize field in its statfs struct until recently,
447 * thus historically Linux's sys_statfs reports these
448 * fields in units of f_bsize.
449 */
David Howells0c7d90c2006-08-22 20:06:10 -0400450 buf->f_bsize = dentry->d_sb->s_blocksize;
451 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400452 blockres = (1 << blockbits) - 1;
453 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
454 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
455 buf->f_bavail = (res.abytes + blockres) >> blockbits;
456
457 buf->f_files = res.tfiles;
458 buf->f_ffree = res.afiles;
459
460 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700461
David Howellsf7b422b2006-06-09 09:34:33 -0400462 return 0;
463
464 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700465 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700466 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400467}
468
David Howells7d4e2742006-08-22 20:06:07 -0400469/*
470 * Map the security flavour number to a name
471 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400472static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
473{
David Howells7d4e2742006-08-22 20:06:07 -0400474 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400475 rpc_authflavor_t flavour;
476 const char *str;
477 } sec_flavours[] = {
478 { RPC_AUTH_NULL, "null" },
479 { RPC_AUTH_UNIX, "sys" },
480 { RPC_AUTH_GSS_KRB5, "krb5" },
481 { RPC_AUTH_GSS_KRB5I, "krb5i" },
482 { RPC_AUTH_GSS_KRB5P, "krb5p" },
483 { RPC_AUTH_GSS_LKEY, "lkey" },
484 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
485 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
486 { RPC_AUTH_GSS_SPKM, "spkm" },
487 { RPC_AUTH_GSS_SPKMI, "spkmi" },
488 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400489 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400490 };
491 int i;
492
Chuck Lever4d81cd12007-07-01 12:12:40 -0400493 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400494 if (sec_flavours[i].flavour == flavour)
495 break;
496 }
497 return sec_flavours[i].str;
498}
499
Jeff Laytonee671b02009-12-03 15:58:56 -0500500static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
501 int showdefaults)
502{
503 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
504
505 seq_printf(m, ",mountproto=");
506 switch (sap->sa_family) {
507 case AF_INET:
508 switch (nfss->mountd_protocol) {
509 case IPPROTO_UDP:
510 seq_printf(m, RPCBIND_NETID_UDP);
511 break;
512 case IPPROTO_TCP:
513 seq_printf(m, RPCBIND_NETID_TCP);
514 break;
515 default:
516 if (showdefaults)
517 seq_printf(m, "auto");
518 }
519 break;
520 case AF_INET6:
521 switch (nfss->mountd_protocol) {
522 case IPPROTO_UDP:
523 seq_printf(m, RPCBIND_NETID_UDP6);
524 break;
525 case IPPROTO_TCP:
526 seq_printf(m, RPCBIND_NETID_TCP6);
527 break;
528 default:
529 if (showdefaults)
530 seq_printf(m, "auto");
531 }
532 break;
533 default:
534 if (showdefaults)
535 seq_printf(m, "auto");
536 }
537}
538
Chuck Lever82d101d2008-03-14 14:10:37 -0400539static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
540 int showdefaults)
541{
542 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
543
544 switch (sap->sa_family) {
545 case AF_INET: {
546 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrisonbe859402008-10-31 00:56:28 -0700547 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400548 break;
549 }
550 case AF_INET6: {
551 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Chuck Leverd250e192009-12-03 15:58:56 -0500552 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400553 break;
554 }
555 default:
556 if (showdefaults)
557 seq_printf(m, ",mountaddr=unspecified");
558 }
559
560 if (nfss->mountd_version || showdefaults)
561 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
562 if (nfss->mountd_port || showdefaults)
563 seq_printf(m, ",mountport=%u", nfss->mountd_port);
564
Jeff Laytonee671b02009-12-03 15:58:56 -0500565 nfs_show_mountd_netid(m, nfss, showdefaults);
Chuck Lever82d101d2008-03-14 14:10:37 -0400566}
567
David Howellsf7b422b2006-06-09 09:34:33 -0400568/*
569 * Describe the mount options in force on this server representation
570 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400571static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
572 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400573{
David Howells509de812006-08-22 20:06:11 -0400574 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400575 int flag;
David Howells509de812006-08-22 20:06:11 -0400576 const char *str;
577 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400578 } nfs_info[] = {
579 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400580 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400581 { NFS_MOUNT_NOCTO, ",nocto", "" },
582 { NFS_MOUNT_NOAC, ",noac", "" },
583 { NFS_MOUNT_NONLM, ",nolock", "" },
584 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400585 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500586 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
587 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400588 { 0, NULL, NULL }
589 };
David Howells509de812006-08-22 20:06:11 -0400590 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400591 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400592 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400593
Chuck Lever82d101d2008-03-14 14:10:37 -0400594 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400595 seq_printf(m, ",rsize=%u", nfss->rsize);
596 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400597 if (nfss->bsize != 0)
598 seq_printf(m, ",bsize=%u", nfss->bsize);
599 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400600 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400601 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400602 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400603 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400604 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400605 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400606 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400607 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400608 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
609 if (nfss->flags & nfs_infop->flag)
610 seq_puts(m, nfs_infop->str);
611 else
612 seq_puts(m, nfs_infop->nostr);
613 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400614 seq_printf(m, ",proto=%s",
Jeff Laytonee671b02009-12-03 15:58:56 -0500615 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
Chuck Lever82d101d2008-03-14 14:10:37 -0400616 if (version == 4) {
617 if (nfss->port != NFS_PORT)
618 seq_printf(m, ",port=%u", nfss->port);
619 } else
620 if (nfss->port)
621 seq_printf(m, ",port=%u", nfss->port);
622
Trond Myklebust33170232007-12-20 16:03:59 -0500623 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
624 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400625 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400626
627 if (version != 4)
628 nfs_show_mountd_options(m, nfss, showdefaults);
629
630#ifdef CONFIG_NFS_V4
631 if (clp->rpc_ops->version == 4)
632 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
633#endif
David Howellsb797cac2009-04-03 16:42:48 +0100634 if (nfss->options & NFS_OPTION_FSCACHE)
635 seq_printf(m, ",fsc");
David Howellsf7b422b2006-06-09 09:34:33 -0400636}
637
638/*
639 * Describe the mount options on this VFS mountpoint
640 */
641static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
642{
643 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
644
645 nfs_show_mount_options(m, nfss, 0);
646
Chuck Lever5d8515c2007-12-10 14:57:16 -0500647 seq_printf(m, ",addr=%s",
648 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
649 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400650
651 return 0;
652}
653
654/*
655 * Present statistical information for this VFS mountpoint
656 */
657static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
658{
659 int i, cpu;
660 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
661 struct rpc_auth *auth = nfss->client->cl_auth;
662 struct nfs_iostats totals = { };
663
664 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
665
666 /*
667 * Display all mount option settings
668 */
669 seq_printf(m, "\n\topts:\t");
670 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
671 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
672 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
673 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
674 nfs_show_mount_options(m, nfss, 1);
675
676 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
677
678 seq_printf(m, "\n\tcaps:\t");
679 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400680 seq_printf(m, ",wtmult=%u", nfss->wtmult);
681 seq_printf(m, ",dtsize=%u", nfss->dtsize);
682 seq_printf(m, ",bsize=%u", nfss->bsize);
683 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400684
685#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500686 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400687 seq_printf(m, "\n\tnfsv4:\t");
688 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
689 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
690 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
691 }
692#endif
693
694 /*
695 * Display security flavor in effect for this mount
696 */
Chuck Lever2d767432008-03-14 14:10:08 -0400697 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400698 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400699 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400700
701 /*
702 * Display superblock I/O counters
703 */
704 for_each_possible_cpu(cpu) {
705 struct nfs_iostats *stats;
706
707 preempt_disable();
708 stats = per_cpu_ptr(nfss->io_stats, cpu);
709
710 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
711 totals.events[i] += stats->events[i];
712 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
713 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100714#ifdef CONFIG_NFS_FSCACHE
715 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
716 totals.fscache[i] += stats->fscache[i];
717#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400718
719 preempt_enable();
720 }
721
722 seq_printf(m, "\n\tevents:\t");
723 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
724 seq_printf(m, "%lu ", totals.events[i]);
725 seq_printf(m, "\n\tbytes:\t");
726 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
727 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100728#ifdef CONFIG_NFS_FSCACHE
729 if (nfss->options & NFS_OPTION_FSCACHE) {
730 seq_printf(m, "\n\tfsc:\t");
731 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
732 seq_printf(m, "%Lu ", totals.bytes[i]);
733 }
734#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400735 seq_printf(m, "\n");
736
737 rpc_print_iostats(m, nfss->client);
738
739 return 0;
740}
741
742/*
743 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400744 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400745 */
Al Viro42faad92008-04-24 07:21:56 -0400746static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400747{
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200748 struct nfs_server *server;
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400749 struct rpc_clnt *rpc;
750
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200751 server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400752 /* -EIO all pending I/O */
753 rpc = server->client_acl;
754 if (!IS_ERR(rpc))
755 rpc_killall_tasks(rpc);
756 rpc = server->client;
757 if (!IS_ERR(rpc))
758 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400759}
760
Trond Myklebustc5811db2009-10-06 15:40:15 -0400761static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
Chuck Lever9423a082009-09-23 14:36:38 -0400762{
763 struct nfs_parsed_mount_data *data;
764
765 data = kzalloc(sizeof(*data), GFP_KERNEL);
766 if (data) {
Chuck Lever9423a082009-09-23 14:36:38 -0400767 data->acregmin = NFS_DEF_ACREGMIN;
768 data->acregmax = NFS_DEF_ACREGMAX;
769 data->acdirmin = NFS_DEF_ACDIRMIN;
770 data->acdirmax = NFS_DEF_ACDIRMAX;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400771 data->mount_server.port = NFS_UNSPEC_PORT;
Chuck Lever9423a082009-09-23 14:36:38 -0400772 data->nfs_server.port = NFS_UNSPEC_PORT;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400773 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Lever9423a082009-09-23 14:36:38 -0400774 data->auth_flavors[0] = RPC_AUTH_UNIX;
775 data->auth_flavor_len = 1;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400776 data->version = version;
Chuck Lever9423a082009-09-23 14:36:38 -0400777 data->minorversion = 0;
778 }
779 return data;
780}
781
David Howellsf7b422b2006-06-09 09:34:33 -0400782/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500783 * Sanity-check a server address provided by the mount command.
784 *
785 * Address family must be initialized, and address must not be
786 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400787 */
788static int nfs_verify_server_address(struct sockaddr *addr)
789{
790 switch (addr->sa_family) {
791 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500792 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700793 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500794 }
795 case AF_INET6: {
796 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
797 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400798 }
799 }
800
Chuck Lever53a0b9c2009-08-09 15:09:36 -0400801 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
Chuck Leverfc50d582007-07-01 12:12:46 -0400802 return 0;
803}
804
Chuck Lever9412b922007-12-10 14:59:21 -0500805/*
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400806 * Select between a default port value and a user-specified port value.
807 * If a zero value is set, then autobind will be used.
808 */
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400809static void nfs_set_port(struct sockaddr *sap, int *port,
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400810 const unsigned short default_port)
811{
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400812 if (*port == NFS_UNSPEC_PORT)
813 *port = default_port;
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400814
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400815 rpc_set_port(sap, *port);
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400816}
817
818/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400819 * Sanity check the NFS transport protocol.
820 *
821 */
822static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
823{
824 switch (mnt->nfs_server.protocol) {
825 case XPRT_TRANSPORT_UDP:
826 case XPRT_TRANSPORT_TCP:
827 case XPRT_TRANSPORT_RDMA:
828 break;
829 default:
830 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
831 }
832}
833
834/*
835 * For text based NFSv2/v3 mounts, the mount protocol transport default
836 * settings should depend upon the specified NFS transport.
837 */
838static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
839{
840 nfs_validate_transport_protocol(mnt);
841
842 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
843 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
844 return;
845 switch (mnt->nfs_server.protocol) {
846 case XPRT_TRANSPORT_UDP:
847 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
848 break;
849 case XPRT_TRANSPORT_TCP:
850 case XPRT_TRANSPORT_RDMA:
851 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
852 }
853}
854
855/*
Chuck Lever01060c82008-06-24 16:33:38 -0400856 * Parse the value of the 'sec=' option.
Chuck Lever01060c82008-06-24 16:33:38 -0400857 */
858static int nfs_parse_security_flavors(char *value,
859 struct nfs_parsed_mount_data *mnt)
860{
861 substring_t args[MAX_OPT_ARGS];
862
863 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
864
865 switch (match_token(value, nfs_secflavor_tokens, args)) {
866 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400867 mnt->auth_flavors[0] = RPC_AUTH_NULL;
868 break;
869 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400870 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
871 break;
872 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400873 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
874 break;
875 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400876 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
877 break;
878 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400879 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
880 break;
881 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400882 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
883 break;
884 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400885 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
886 break;
887 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400888 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
889 break;
890 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400891 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
892 break;
893 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400894 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
895 break;
896 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400897 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
898 break;
899 default:
900 return 0;
901 }
902
Chuck Lever059f90b2009-08-09 15:09:31 -0400903 mnt->auth_flavor_len = 1;
Chuck Lever01060c82008-06-24 16:33:38 -0400904 return 1;
905}
906
907/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400908 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400909 * a data structure. The whole mount string is processed; bad options are
910 * skipped as they are encountered. If there were no errors, return 1;
911 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400912 */
913static int nfs_parse_mount_options(char *raw,
914 struct nfs_parsed_mount_data *mnt)
915{
Eric Parisf9c3a382008-03-05 14:20:18 -0500916 char *p, *string, *secdata;
Chuck Leverd23c45f2009-06-17 18:02:13 -0700917 int rc, sloppy = 0, invalid_option = 0;
Jeff Laytonee671b02009-12-03 15:58:56 -0500918 unsigned short protofamily = AF_UNSPEC;
919 unsigned short mountfamily = AF_UNSPEC;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400920
921 if (!raw) {
922 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
923 return 1;
924 }
925 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
926
Eric Parisf9c3a382008-03-05 14:20:18 -0500927 secdata = alloc_secdata();
928 if (!secdata)
929 goto out_nomem;
930
931 rc = security_sb_copy_data(raw, secdata);
932 if (rc)
933 goto out_security_failure;
934
935 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
936 if (rc)
937 goto out_security_failure;
938
939 free_secdata(secdata);
940
Chuck Leverbf0fd762007-07-01 12:13:44 -0400941 while ((p = strsep(&raw, ",")) != NULL) {
942 substring_t args[MAX_OPT_ARGS];
Chuck Levera5a16ba2009-06-17 18:02:14 -0700943 unsigned long option;
944 int token;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400945
946 if (!*p)
947 continue;
948
949 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
950
951 token = match_token(p, nfs_mount_option_tokens, args);
952 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400953
954 /*
955 * boolean options: foo/nofoo
956 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400957 case Opt_soft:
958 mnt->flags |= NFS_MOUNT_SOFT;
959 break;
960 case Opt_hard:
961 mnt->flags &= ~NFS_MOUNT_SOFT;
962 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400963 case Opt_posix:
964 mnt->flags |= NFS_MOUNT_POSIX;
965 break;
966 case Opt_noposix:
967 mnt->flags &= ~NFS_MOUNT_POSIX;
968 break;
969 case Opt_cto:
970 mnt->flags &= ~NFS_MOUNT_NOCTO;
971 break;
972 case Opt_nocto:
973 mnt->flags |= NFS_MOUNT_NOCTO;
974 break;
975 case Opt_ac:
976 mnt->flags &= ~NFS_MOUNT_NOAC;
977 break;
978 case Opt_noac:
979 mnt->flags |= NFS_MOUNT_NOAC;
980 break;
981 case Opt_lock:
982 mnt->flags &= ~NFS_MOUNT_NONLM;
983 break;
984 case Opt_nolock:
985 mnt->flags |= NFS_MOUNT_NONLM;
986 break;
987 case Opt_v2:
988 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -0400989 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400990 break;
991 case Opt_v3:
992 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -0400993 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400994 break;
Chuck Lever764302c2009-09-08 19:50:03 -0400995#ifdef CONFIG_NFS_V4
996 case Opt_v4:
997 mnt->flags &= ~NFS_MOUNT_VER3;
998 mnt->version = 4;
999 break;
1000#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001001 case Opt_udp:
1002 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001003 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001004 break;
1005 case Opt_tcp:
1006 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001007 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001008 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001009 case Opt_rdma:
1010 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1011 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001012 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001013 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001014 case Opt_acl:
1015 mnt->flags &= ~NFS_MOUNT_NOACL;
1016 break;
1017 case Opt_noacl:
1018 mnt->flags |= NFS_MOUNT_NOACL;
1019 break;
1020 case Opt_rdirplus:
1021 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1022 break;
1023 case Opt_nordirplus:
1024 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1025 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001026 case Opt_sharecache:
1027 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1028 break;
1029 case Opt_nosharecache:
1030 mnt->flags |= NFS_MOUNT_UNSHARED;
1031 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001032 case Opt_resvport:
1033 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1034 break;
1035 case Opt_noresvport:
1036 mnt->flags |= NFS_MOUNT_NORESVPORT;
1037 break;
David Howellsb797cac2009-04-03 16:42:48 +01001038 case Opt_fscache:
1039 mnt->options |= NFS_OPTION_FSCACHE;
1040 kfree(mnt->fscache_uniq);
1041 mnt->fscache_uniq = NULL;
1042 break;
1043 case Opt_nofscache:
1044 mnt->options &= ~NFS_OPTION_FSCACHE;
1045 kfree(mnt->fscache_uniq);
1046 mnt->fscache_uniq = NULL;
1047 break;
1048 case Opt_fscache_uniq:
1049 string = match_strdup(args);
1050 if (!string)
1051 goto out_nomem;
1052 kfree(mnt->fscache_uniq);
1053 mnt->fscache_uniq = string;
1054 mnt->options |= NFS_OPTION_FSCACHE;
1055 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001056
Chuck Leverf45663c2008-06-24 19:28:02 -04001057 /*
1058 * options that take numeric values
1059 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001060 case Opt_port:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001061 string = match_strdup(args);
1062 if (string == NULL)
1063 goto out_nomem;
1064 rc = strict_strtoul(string, 10, &option);
1065 kfree(string);
1066 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001067 goto out_invalid_value;
1068 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001069 break;
1070 case Opt_rsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001071 string = match_strdup(args);
1072 if (string == NULL)
1073 goto out_nomem;
1074 rc = strict_strtoul(string, 10, &option);
1075 kfree(string);
1076 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001077 goto out_invalid_value;
1078 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001079 break;
1080 case Opt_wsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001081 string = match_strdup(args);
1082 if (string == NULL)
1083 goto out_nomem;
1084 rc = strict_strtoul(string, 10, &option);
1085 kfree(string);
1086 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001087 goto out_invalid_value;
1088 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001089 break;
1090 case Opt_bsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001091 string = match_strdup(args);
1092 if (string == NULL)
1093 goto out_nomem;
1094 rc = strict_strtoul(string, 10, &option);
1095 kfree(string);
1096 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001097 goto out_invalid_value;
1098 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001099 break;
1100 case Opt_timeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001101 string = match_strdup(args);
1102 if (string == NULL)
1103 goto out_nomem;
1104 rc = strict_strtoul(string, 10, &option);
1105 kfree(string);
1106 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001107 goto out_invalid_value;
1108 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001109 break;
1110 case Opt_retrans:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001111 string = match_strdup(args);
1112 if (string == NULL)
1113 goto out_nomem;
1114 rc = strict_strtoul(string, 10, &option);
1115 kfree(string);
1116 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001117 goto out_invalid_value;
1118 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001119 break;
1120 case Opt_acregmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001121 string = match_strdup(args);
1122 if (string == NULL)
1123 goto out_nomem;
1124 rc = strict_strtoul(string, 10, &option);
1125 kfree(string);
1126 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001127 goto out_invalid_value;
1128 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001129 break;
1130 case Opt_acregmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001131 string = match_strdup(args);
1132 if (string == NULL)
1133 goto out_nomem;
1134 rc = strict_strtoul(string, 10, &option);
1135 kfree(string);
1136 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001137 goto out_invalid_value;
1138 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001139 break;
1140 case Opt_acdirmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001141 string = match_strdup(args);
1142 if (string == NULL)
1143 goto out_nomem;
1144 rc = strict_strtoul(string, 10, &option);
1145 kfree(string);
1146 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001147 goto out_invalid_value;
1148 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001149 break;
1150 case Opt_acdirmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001151 string = match_strdup(args);
1152 if (string == NULL)
1153 goto out_nomem;
1154 rc = strict_strtoul(string, 10, &option);
1155 kfree(string);
1156 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001157 goto out_invalid_value;
1158 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001159 break;
1160 case Opt_actimeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001161 string = match_strdup(args);
1162 if (string == NULL)
1163 goto out_nomem;
1164 rc = strict_strtoul(string, 10, &option);
1165 kfree(string);
1166 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001167 goto out_invalid_value;
1168 mnt->acregmin = mnt->acregmax =
1169 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001170 break;
1171 case Opt_namelen:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001172 string = match_strdup(args);
1173 if (string == NULL)
1174 goto out_nomem;
1175 rc = strict_strtoul(string, 10, &option);
1176 kfree(string);
1177 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001178 goto out_invalid_value;
1179 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001180 break;
1181 case Opt_mountport:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001182 string = match_strdup(args);
1183 if (string == NULL)
1184 goto out_nomem;
1185 rc = strict_strtoul(string, 10, &option);
1186 kfree(string);
1187 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001188 goto out_invalid_value;
1189 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001190 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 case Opt_mountvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001192 string = match_strdup(args);
1193 if (string == NULL)
1194 goto out_nomem;
1195 rc = strict_strtoul(string, 10, &option);
1196 kfree(string);
1197 if (rc != 0 ||
Chuck Leverf45663c2008-06-24 19:28:02 -04001198 option < NFS_MNT_VERSION ||
Chuck Leverd23c45f2009-06-17 18:02:13 -07001199 option > NFS_MNT3_VERSION)
1200 goto out_invalid_value;
1201 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001202 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001203 case Opt_nfsvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001204 string = match_strdup(args);
1205 if (string == NULL)
1206 goto out_nomem;
1207 rc = strict_strtoul(string, 10, &option);
1208 kfree(string);
1209 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001210 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001211 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001212 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001213 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001214 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001215 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001216 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001217 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001218 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001219 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001220#ifdef CONFIG_NFS_V4
1221 case NFS4_VERSION:
1222 mnt->flags &= ~NFS_MOUNT_VER3;
1223 mnt->version = 4;
1224 break;
1225#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001226 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001227 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001228 }
1229 break;
Mike Sager3fd5be92009-04-01 09:21:48 -04001230 case Opt_minorversion:
Chuck Leverf3f4f4e2009-08-09 15:09:29 -04001231 string = match_strdup(args);
1232 if (string == NULL)
1233 goto out_nomem;
1234 rc = strict_strtoul(string, 10, &option);
1235 kfree(string);
1236 if (rc != 0)
1237 goto out_invalid_value;
1238 if (option > NFS4_MAX_MINOR_VERSION)
1239 goto out_invalid_value;
1240 mnt->minorversion = option;
Mike Sager3fd5be92009-04-01 09:21:48 -04001241 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001242
Chuck Leverf45663c2008-06-24 19:28:02 -04001243 /*
1244 * options that take text values
1245 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001246 case Opt_sec:
1247 string = match_strdup(args);
1248 if (string == NULL)
1249 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001250 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001251 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001252 if (!rc) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001253 dfprintk(MOUNT, "NFS: unrecognized "
1254 "security flavor\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001255 return 0;
Chuck Leverf45663c2008-06-24 19:28:02 -04001256 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001257 break;
1258 case Opt_proto:
1259 string = match_strdup(args);
1260 if (string == NULL)
1261 goto out_nomem;
1262 token = match_token(string,
1263 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001264
Jeff Laytonee671b02009-12-03 15:58:56 -05001265 protofamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001266 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001267 case Opt_xprt_udp6:
1268 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001269 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001270 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001271 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001272 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001273 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001274 case Opt_xprt_tcp6:
1275 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001276 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001277 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001278 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001279 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001280 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001281 case Opt_xprt_rdma:
1282 /* vector side protocols to TCP */
1283 mnt->flags |= NFS_MOUNT_TCP;
1284 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001285 xprt_load_transport(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001286 kfree(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001287 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001288 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001289 dfprintk(MOUNT, "NFS: unrecognized "
1290 "transport protocol\n");
Yinghai Lu4223a4a2009-10-20 14:13:46 +09001291 kfree(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001292 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001293 }
1294 break;
1295 case Opt_mountproto:
1296 string = match_strdup(args);
1297 if (string == NULL)
1298 goto out_nomem;
1299 token = match_token(string,
1300 nfs_xprt_protocol_tokens, args);
Trond Myklebustd508afb2009-04-06 16:41:35 -07001301 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001302
Jeff Laytonee671b02009-12-03 15:58:56 -05001303 mountfamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001304 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001305 case Opt_xprt_udp6:
1306 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001307 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001308 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001309 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001310 case Opt_xprt_tcp6:
1311 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001312 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001313 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001314 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001315 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001316 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001317 dfprintk(MOUNT, "NFS: unrecognized "
1318 "transport protocol\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001319 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001320 }
1321 break;
1322 case Opt_addr:
1323 string = match_strdup(args);
1324 if (string == NULL)
1325 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001326 mnt->nfs_server.addrlen =
1327 rpc_pton(string, strlen(string),
1328 (struct sockaddr *)
1329 &mnt->nfs_server.address,
1330 sizeof(mnt->nfs_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001331 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001332 if (mnt->nfs_server.addrlen == 0)
1333 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001334 break;
1335 case Opt_clientaddr:
1336 string = match_strdup(args);
1337 if (string == NULL)
1338 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001339 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001340 mnt->client_address = string;
1341 break;
Chuck Lever33832032007-12-10 14:59:13 -05001342 case Opt_mounthost:
1343 string = match_strdup(args);
1344 if (string == NULL)
1345 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001346 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001347 mnt->mount_server.hostname = string;
1348 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001349 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001350 string = match_strdup(args);
1351 if (string == NULL)
1352 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001353 mnt->mount_server.addrlen =
1354 rpc_pton(string, strlen(string),
1355 (struct sockaddr *)
1356 &mnt->mount_server.address,
1357 sizeof(mnt->mount_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001358 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001359 if (mnt->mount_server.addrlen == 0)
1360 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001361 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001362 case Opt_lookupcache:
1363 string = match_strdup(args);
1364 if (string == NULL)
1365 goto out_nomem;
1366 token = match_token(string,
1367 nfs_lookupcache_tokens, args);
1368 kfree(string);
1369 switch (token) {
1370 case Opt_lookupcache_all:
1371 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1372 break;
1373 case Opt_lookupcache_positive:
1374 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1375 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1376 break;
1377 case Opt_lookupcache_none:
1378 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1379 break;
1380 default:
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001381 dfprintk(MOUNT, "NFS: invalid "
1382 "lookupcache argument\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001383 return 0;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001384 };
1385 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001386
Chuck Leverf45663c2008-06-24 19:28:02 -04001387 /*
1388 * Special options
1389 */
1390 case Opt_sloppy:
1391 sloppy = 1;
1392 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1393 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001394 case Opt_userspace:
1395 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001396 dfprintk(MOUNT, "NFS: ignoring mount option "
1397 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001398 break;
1399
1400 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001401 invalid_option = 1;
Chuck Leverf45663c2008-06-24 19:28:02 -04001402 dfprintk(MOUNT, "NFS: unrecognized mount option "
1403 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001404 }
1405 }
1406
Chuck Leverd23c45f2009-06-17 18:02:13 -07001407 if (!sloppy && invalid_option)
1408 return 0;
1409
Jeff Laytonee671b02009-12-03 15:58:56 -05001410 /*
1411 * verify that any proto=/mountproto= options match the address
1412 * familiies in the addr=/mountaddr= options.
1413 */
1414 if (protofamily != AF_UNSPEC &&
1415 protofamily != mnt->nfs_server.address.ss_family)
1416 goto out_proto_mismatch;
1417
1418 if (mountfamily != AF_UNSPEC) {
1419 if (mnt->mount_server.addrlen) {
1420 if (mountfamily != mnt->mount_server.address.ss_family)
1421 goto out_mountproto_mismatch;
1422 } else {
1423 if (mountfamily != mnt->nfs_server.address.ss_family)
1424 goto out_mountproto_mismatch;
1425 }
1426 }
1427
Chuck Leverbf0fd762007-07-01 12:13:44 -04001428 return 1;
1429
Jeff Laytonee671b02009-12-03 15:58:56 -05001430out_mountproto_mismatch:
1431 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1432 "option\n");
1433 return 0;
1434out_proto_mismatch:
1435 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1436 return 0;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001437out_invalid_address:
1438 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1439 return 0;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001440out_invalid_value:
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001441 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001442 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001443out_nomem:
1444 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1445 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001446out_security_failure:
1447 free_secdata(secdata);
1448 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1449 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001450}
1451
1452/*
Chuck Leverec88f282009-08-09 15:09:32 -04001453 * Match the requested auth flavors with the list returned by
1454 * the server. Returns zero and sets the mount's authentication
1455 * flavor on success; returns -EACCES if server does not support
1456 * the requested flavor.
1457 */
1458static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1459 struct nfs_mount_request *request)
1460{
1461 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1462
1463 /*
Chuck Lever5eecfde2009-08-21 17:50:30 -04001464 * Certain releases of Linux's mountd return an empty
1465 * flavor list. To prevent behavioral regression with
1466 * these servers (ie. rejecting mounts that used to
1467 * succeed), revert to pre-2.6.32 behavior (no checking)
1468 * if the returned flavor list is empty.
1469 */
1470 if (server_authlist_len == 0)
1471 return 0;
1472
1473 /*
Chuck Leverec88f282009-08-09 15:09:32 -04001474 * We avoid sophisticated negotiating here, as there are
1475 * plenty of cases where we can get it wrong, providing
1476 * either too little or too much security.
1477 *
1478 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1479 * flavor listed first. However, some servers list
1480 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1481 * preferred default, in args->auth_flavors[0] if user
1482 * didn't specify sec= mount option.
1483 */
1484 for (i = 0; i < args->auth_flavor_len; i++)
1485 for (j = 0; j < server_authlist_len; j++)
1486 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1487 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1488 request->auth_flavs[j]);
1489 args->auth_flavors[0] = request->auth_flavs[j];
1490 return 0;
1491 }
1492
1493 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1494 nfs_umount(request);
1495 return -EACCES;
1496}
1497
1498/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001499 * Use the remote server's MOUNT service to request the NFS file handle
1500 * corresponding to the provided path.
1501 */
1502static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1503 struct nfs_fh *root_fh)
1504{
Chuck Leverec88f282009-08-09 15:09:32 -04001505 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1506 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
Chuck Leverc5d120f2008-12-23 15:21:35 -05001507 struct nfs_mount_request request = {
1508 .sap = (struct sockaddr *)
1509 &args->mount_server.address,
1510 .dirpath = args->nfs_server.export_path,
1511 .protocol = args->mount_server.protocol,
1512 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001513 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverec88f282009-08-09 15:09:32 -04001514 .auth_flav_len = &server_authlist_len,
1515 .auth_flavs = server_authlist,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001516 };
Chuck Lever4c568012007-12-10 14:59:28 -05001517 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001518
1519 if (args->mount_server.version == 0) {
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001520 switch (args->version) {
1521 default:
1522 args->mount_server.version = NFS_MNT3_VERSION;
1523 break;
1524 case 2:
1525 args->mount_server.version = NFS_MNT_VERSION;
1526 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001527 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001528 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001529
Chuck Lever33832032007-12-10 14:59:13 -05001530 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001531 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001532 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001533 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001534
Chuck Lever0076d7b2007-07-01 12:13:49 -04001535 /*
1536 * Construct the mount server's address.
1537 */
Chuck Lever4c568012007-12-10 14:59:28 -05001538 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001539 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001540 args->nfs_server.addrlen);
1541 args->mount_server.addrlen = args->nfs_server.addrlen;
1542 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001543 request.salen = args->mount_server.addrlen;
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001544 nfs_set_port(request.sap, &args->mount_server.port, 0);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001545
1546 /*
1547 * Now ask the mount server to map our export path
1548 * to a file handle.
1549 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001550 status = nfs_mount(&request);
Chuck Leverec88f282009-08-09 15:09:32 -04001551 if (status != 0) {
1552 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1553 request.hostname, status);
1554 return status;
1555 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001556
Chuck Leverec88f282009-08-09 15:09:32 -04001557 /*
1558 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1559 */
1560 if (args->mount_server.version != NFS_MNT3_VERSION)
1561 return 0;
1562 return nfs_walk_authlist(args, &request);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001563}
1564
Chuck Leverd1aa0822008-06-23 12:36:45 -04001565static int nfs_parse_simple_hostname(const char *dev_name,
1566 char **hostname, size_t maxnamlen,
1567 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001568{
1569 size_t len;
1570 char *colon, *comma;
1571
1572 colon = strchr(dev_name, ':');
1573 if (colon == NULL)
1574 goto out_bad_devname;
1575
1576 len = colon - dev_name;
1577 if (len > maxnamlen)
1578 goto out_hostname;
1579
1580 /* N.B. caller will free nfs_server.hostname in all cases */
1581 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1582 if (!*hostname)
1583 goto out_nomem;
1584
1585 /* kill possible hostname list: not supported */
1586 comma = strchr(*hostname, ',');
1587 if (comma != NULL) {
1588 if (comma == *hostname)
1589 goto out_bad_devname;
1590 *comma = '\0';
1591 }
1592
1593 colon++;
1594 len = strlen(colon);
1595 if (len > maxpathlen)
1596 goto out_path;
1597 *export_path = kstrndup(colon, len, GFP_KERNEL);
1598 if (!*export_path)
1599 goto out_nomem;
1600
1601 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1602 return 0;
1603
1604out_bad_devname:
1605 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1606 return -EINVAL;
1607
1608out_nomem:
1609 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1610 return -ENOMEM;
1611
1612out_hostname:
1613 dfprintk(MOUNT, "NFS: server hostname too long\n");
1614 return -ENAMETOOLONG;
1615
1616out_path:
1617 dfprintk(MOUNT, "NFS: export pathname too long\n");
1618 return -ENAMETOOLONG;
1619}
1620
1621/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001622 * Hostname has square brackets around it because it contains one or
1623 * more colons. We look for the first closing square bracket, and a
1624 * colon must follow it.
1625 */
1626static int nfs_parse_protected_hostname(const char *dev_name,
1627 char **hostname, size_t maxnamlen,
1628 char **export_path, size_t maxpathlen)
1629{
1630 size_t len;
1631 char *start, *end;
1632
1633 start = (char *)(dev_name + 1);
1634
1635 end = strchr(start, ']');
1636 if (end == NULL)
1637 goto out_bad_devname;
1638 if (*(end + 1) != ':')
1639 goto out_bad_devname;
1640
1641 len = end - start;
1642 if (len > maxnamlen)
1643 goto out_hostname;
1644
1645 /* N.B. caller will free nfs_server.hostname in all cases */
1646 *hostname = kstrndup(start, len, GFP_KERNEL);
1647 if (*hostname == NULL)
1648 goto out_nomem;
1649
1650 end += 2;
1651 len = strlen(end);
1652 if (len > maxpathlen)
1653 goto out_path;
1654 *export_path = kstrndup(end, len, GFP_KERNEL);
1655 if (!*export_path)
1656 goto out_nomem;
1657
1658 return 0;
1659
1660out_bad_devname:
1661 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1662 return -EINVAL;
1663
1664out_nomem:
1665 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1666 return -ENOMEM;
1667
1668out_hostname:
1669 dfprintk(MOUNT, "NFS: server hostname too long\n");
1670 return -ENAMETOOLONG;
1671
1672out_path:
1673 dfprintk(MOUNT, "NFS: export pathname too long\n");
1674 return -ENAMETOOLONG;
1675}
1676
1677/*
1678 * Split "dev_name" into "hostname:export_path".
1679 *
1680 * The leftmost colon demarks the split between the server's hostname
1681 * and the export path. If the hostname starts with a left square
1682 * bracket, then it may contain colons.
1683 *
1684 * Note: caller frees hostname and export path, even on error.
1685 */
1686static int nfs_parse_devname(const char *dev_name,
1687 char **hostname, size_t maxnamlen,
1688 char **export_path, size_t maxpathlen)
1689{
1690 if (*dev_name == '[')
1691 return nfs_parse_protected_hostname(dev_name,
1692 hostname, maxnamlen,
1693 export_path, maxpathlen);
1694
1695 return nfs_parse_simple_hostname(dev_name,
1696 hostname, maxnamlen,
1697 export_path, maxpathlen);
1698}
1699
1700/*
David Howells54ceac42006-08-22 20:06:13 -04001701 * Validate the NFS2/NFS3 mount data
1702 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001703 *
1704 * For option strings, user space handles the following behaviors:
1705 *
1706 * + DNS: mapping server host name to IP address ("addr=" option)
1707 *
1708 * + failure mode: how to behave if a mount request can't be handled
1709 * immediately ("fg/bg" option)
1710 *
1711 * + retry: how often to retry a mount request ("retry=" option)
1712 *
1713 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1714 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001715 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001716static int nfs_validate_mount_data(void *options,
1717 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001718 struct nfs_fh *mntfh,
1719 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001720{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001721 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001722 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
Chuck Lever136d5582007-07-01 12:13:54 -04001723
Chuck Lever5df36e72007-07-01 12:12:56 -04001724 if (data == NULL)
1725 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001726
David Howellsf7b422b2006-06-09 09:34:33 -04001727 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001728 case 1:
1729 data->namlen = 0;
1730 case 2:
1731 data->bsize = 0;
1732 case 3:
1733 if (data->flags & NFS_MOUNT_VER3)
1734 goto out_no_v3;
1735 data->root.size = NFS2_FHSIZE;
1736 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1737 case 4:
1738 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1739 goto out_no_sec;
1740 case 5:
1741 memset(data->context, 0, sizeof(data->context));
1742 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001743 if (data->flags & NFS_MOUNT_VER3) {
1744 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1745 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001746 mntfh->size = data->root.size;
Chuck Lever764302c2009-09-08 19:50:03 -04001747 args->version = 3;
1748 } else {
Chuck Lever5df36e72007-07-01 12:12:56 -04001749 mntfh->size = NFS2_FHSIZE;
Chuck Lever764302c2009-09-08 19:50:03 -04001750 args->version = 2;
1751 }
Chuck Lever5df36e72007-07-01 12:12:56 -04001752
Chuck Lever5df36e72007-07-01 12:12:56 -04001753
1754 memcpy(mntfh->data, data->root.data, mntfh->size);
1755 if (mntfh->size < sizeof(mntfh->data))
1756 memset(mntfh->data + mntfh->size, 0,
1757 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001758
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001759 /*
1760 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1761 * can deal with.
1762 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001763 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001764 args->rsize = data->rsize;
1765 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001766 args->timeo = data->timeo;
1767 args->retrans = data->retrans;
1768 args->acregmin = data->acregmin;
1769 args->acregmax = data->acregmax;
1770 args->acdirmin = data->acdirmin;
1771 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001772
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001773 memcpy(sap, &data->addr, sizeof(data->addr));
Chuck Lever4c568012007-12-10 14:59:28 -05001774 args->nfs_server.addrlen = sizeof(data->addr);
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001775 if (!nfs_verify_server_address(sap))
Chuck Lever4c568012007-12-10 14:59:28 -05001776 goto out_no_address;
1777
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001778 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001779 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001780 /* N.B. caller will free nfs_server.hostname in all cases */
1781 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1782 args->namlen = data->namlen;
1783 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001784
1785 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1786 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001787 if (!args->nfs_server.hostname)
1788 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001789
1790 /*
1791 * The legacy version 6 binary mount data from userspace has a
1792 * field used only to transport selinux information into the
1793 * the kernel. To continue to support that functionality we
1794 * have a touch of selinux knowledge here in the NFS code. The
1795 * userspace code converted context=blah to just blah so we are
1796 * converting back to the full string selinux understands.
1797 */
1798 if (data->context[0]){
1799#ifdef CONFIG_SECURITY_SELINUX
1800 int rc;
1801 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1802 if (!opts_str)
1803 return -ENOMEM;
1804 strcpy(opts_str, "context=");
1805 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1806 strcat(opts_str, &data->context[0]);
1807 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1808 kfree(opts_str);
1809 if (rc)
1810 return rc;
1811#else
1812 return -EINVAL;
1813#endif
1814 }
1815
Chuck Lever5df36e72007-07-01 12:12:56 -04001816 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001817 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001818 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001819
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001820 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001821 return -EINVAL;
1822
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001823 if (!nfs_verify_server_address(sap))
Chuck Lever6e88e062007-09-24 15:39:50 -04001824 goto out_no_address;
1825
Chuck Lever764302c2009-09-08 19:50:03 -04001826 if (args->version == 4)
1827#ifdef CONFIG_NFS_V4
1828 return nfs4_validate_text_mount_data(options,
1829 args, dev_name);
1830#else
1831 goto out_v4_not_compiled;
1832#endif
1833
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001834 nfs_set_port(sap, &args->nfs_server.port, 0);
Chuck Levered596a82008-06-26 17:47:05 -04001835
Trond Myklebust259875e2008-07-02 14:43:47 -04001836 nfs_set_mount_transport_protocol(args);
1837
Chuck Leverdc045892008-06-23 12:36:37 -04001838 status = nfs_parse_devname(dev_name,
1839 &args->nfs_server.hostname,
1840 PAGE_SIZE,
1841 &args->nfs_server.export_path,
1842 NFS_MAXPATHLEN);
1843 if (!status)
1844 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001845
Chuck Leverdc045892008-06-23 12:36:37 -04001846 kfree(args->nfs_server.export_path);
1847 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001848
Chuck Lever136d5582007-07-01 12:13:54 -04001849 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001850 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001851
Chuck Lever136d5582007-07-01 12:13:54 -04001852 break;
1853 }
David Howellsf7b422b2006-06-09 09:34:33 -04001854 }
David Howells54ceac42006-08-22 20:06:13 -04001855
David Howellsf7b422b2006-06-09 09:34:33 -04001856#ifndef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001857 if (args->version == 3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001858 goto out_v3_not_compiled;
1859#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001860
David Howells54ceac42006-08-22 20:06:13 -04001861 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001862
1863out_no_data:
1864 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1865 return -EINVAL;
1866
1867out_no_v3:
1868 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1869 data->version);
1870 return -EINVAL;
1871
1872out_no_sec:
1873 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1874 return -EINVAL;
1875
Chuck Lever5df36e72007-07-01 12:12:56 -04001876#ifndef CONFIG_NFS_V3
1877out_v3_not_compiled:
1878 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1879 return -EPROTONOSUPPORT;
1880#endif /* !CONFIG_NFS_V3 */
1881
Chuck Lever764302c2009-09-08 19:50:03 -04001882#ifndef CONFIG_NFS_V4
1883out_v4_not_compiled:
1884 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1885 return -EPROTONOSUPPORT;
1886#endif /* !CONFIG_NFS_V4 */
1887
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001888out_nomem:
1889 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1890 return -ENOMEM;
1891
Chuck Lever5df36e72007-07-01 12:12:56 -04001892out_no_address:
1893 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1894 return -EINVAL;
1895
1896out_invalid_fh:
1897 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1898 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001899}
1900
Jeff Layton48b605f2008-06-10 15:38:39 -04001901static int
1902nfs_compare_remount_data(struct nfs_server *nfss,
1903 struct nfs_parsed_mount_data *data)
1904{
1905 if (data->flags != nfss->flags ||
1906 data->rsize != nfss->rsize ||
1907 data->wsize != nfss->wsize ||
1908 data->retrans != nfss->client->cl_timeout->to_retries ||
1909 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1910 data->acregmin != nfss->acregmin / HZ ||
1911 data->acregmax != nfss->acregmax / HZ ||
1912 data->acdirmin != nfss->acdirmin / HZ ||
1913 data->acdirmax != nfss->acdirmax / HZ ||
1914 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04001915 data->nfs_server.port != nfss->port ||
Jeff Layton48b605f2008-06-10 15:38:39 -04001916 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
Stefan Richtera1be9ee2009-10-12 11:26:12 -04001917 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
1918 (struct sockaddr *)&nfss->nfs_client->cl_addr))
Jeff Layton48b605f2008-06-10 15:38:39 -04001919 return -EINVAL;
1920
1921 return 0;
1922}
1923
1924static int
1925nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1926{
1927 int error;
1928 struct nfs_server *nfss = sb->s_fs_info;
1929 struct nfs_parsed_mount_data *data;
1930 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1931 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001932 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001933
1934 /*
1935 * Userspace mount programs that send binary options generally send
1936 * them populated with default values. We have no way to know which
1937 * ones were explicitly specified. Fall back to legacy behavior and
1938 * just return success.
1939 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001940 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1941 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1942 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001943 return 0;
1944
1945 data = kzalloc(sizeof(*data), GFP_KERNEL);
1946 if (data == NULL)
1947 return -ENOMEM;
1948
1949 /* fill out struct with values from existing mount */
1950 data->flags = nfss->flags;
1951 data->rsize = nfss->rsize;
1952 data->wsize = nfss->wsize;
1953 data->retrans = nfss->client->cl_timeout->to_retries;
1954 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1955 data->acregmin = nfss->acregmin / HZ;
1956 data->acregmax = nfss->acregmax / HZ;
1957 data->acdirmin = nfss->acdirmin / HZ;
1958 data->acdirmax = nfss->acdirmax / HZ;
1959 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04001960 data->nfs_server.port = nfss->port;
Jeff Layton48b605f2008-06-10 15:38:39 -04001961 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1962 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1963 data->nfs_server.addrlen);
1964
1965 /* overwrite those values with any that were specified */
1966 error = nfs_parse_mount_options((char *)options, data);
1967 if (error < 0)
1968 goto out;
1969
1970 /* compare new mount options with old ones */
1971 error = nfs_compare_remount_data(nfss, data);
1972out:
1973 kfree(data);
1974 return error;
1975}
1976
David Howells54ceac42006-08-22 20:06:13 -04001977/*
1978 * Initialise the common bits of the superblock
1979 */
1980static inline void nfs_initialise_sb(struct super_block *sb)
1981{
1982 struct nfs_server *server = NFS_SB(sb);
1983
1984 sb->s_magic = NFS_SUPER_MAGIC;
1985
1986 /* We probably want something more informative here */
1987 snprintf(sb->s_id, sizeof(sb->s_id),
1988 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1989
1990 if (sb->s_blocksize == 0)
1991 sb->s_blocksize = nfs_block_bits(server->wsize,
1992 &sb->s_blocksize_bits);
1993
1994 if (server->flags & NFS_MOUNT_NOAC)
1995 sb->s_flags |= MS_SYNCHRONOUS;
1996
Jens Axboe32a88aa2009-09-16 15:02:33 +02001997 sb->s_bdi = &server->backing_dev_info;
1998
David Howells54ceac42006-08-22 20:06:13 -04001999 nfs_super_set_maxbytes(sb, server->maxfilesize);
2000}
2001
2002/*
2003 * Finish setting up an NFS2/3 superblock
2004 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04002005static void nfs_fill_super(struct super_block *sb,
2006 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04002007{
2008 struct nfs_server *server = NFS_SB(sb);
2009
2010 sb->s_blocksize_bits = 0;
2011 sb->s_blocksize = 0;
2012 if (data->bsize)
2013 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2014
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002015 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002016 /* The VFS shouldn't apply the umask to mode bits. We will do
2017 * so ourselves when necessary.
2018 */
2019 sb->s_flags |= MS_POSIXACL;
2020 sb->s_time_gran = 1;
2021 }
2022
2023 sb->s_op = &nfs_sops;
2024 nfs_initialise_sb(sb);
2025}
2026
2027/*
2028 * Finish setting up a cloned NFS2/3 superblock
2029 */
2030static void nfs_clone_super(struct super_block *sb,
2031 const struct super_block *old_sb)
2032{
2033 struct nfs_server *server = NFS_SB(sb);
2034
2035 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2036 sb->s_blocksize = old_sb->s_blocksize;
2037 sb->s_maxbytes = old_sb->s_maxbytes;
2038
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002039 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002040 /* The VFS shouldn't apply the umask to mode bits. We will do
2041 * so ourselves when necessary.
2042 */
2043 sb->s_flags |= MS_POSIXACL;
2044 sb->s_time_gran = 1;
2045 }
2046
2047 sb->s_op = old_sb->s_op;
2048 nfs_initialise_sb(sb);
2049}
2050
Trond Myklebust275a5d22007-05-16 16:53:28 -04002051static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2052{
2053 const struct nfs_server *a = s->s_fs_info;
2054 const struct rpc_clnt *clnt_a = a->client;
2055 const struct rpc_clnt *clnt_b = b->client;
2056
2057 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2058 goto Ebusy;
2059 if (a->nfs_client != b->nfs_client)
2060 goto Ebusy;
2061 if (a->flags != b->flags)
2062 goto Ebusy;
2063 if (a->wsize != b->wsize)
2064 goto Ebusy;
2065 if (a->rsize != b->rsize)
2066 goto Ebusy;
2067 if (a->acregmin != b->acregmin)
2068 goto Ebusy;
2069 if (a->acregmax != b->acregmax)
2070 goto Ebusy;
2071 if (a->acdirmin != b->acdirmin)
2072 goto Ebusy;
2073 if (a->acdirmax != b->acdirmax)
2074 goto Ebusy;
2075 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2076 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002077 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04002078Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002079 return 0;
2080}
2081
2082struct nfs_sb_mountdata {
2083 struct nfs_server *server;
2084 int mntflags;
2085};
2086
2087static int nfs_set_super(struct super_block *s, void *data)
2088{
2089 struct nfs_sb_mountdata *sb_mntdata = data;
2090 struct nfs_server *server = sb_mntdata->server;
2091 int ret;
2092
2093 s->s_flags = sb_mntdata->mntflags;
2094 s->s_fs_info = server;
2095 ret = set_anon_super(s, server);
2096 if (ret == 0)
2097 server->s_dev = s->s_dev;
2098 return ret;
2099}
2100
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002101static int nfs_compare_super_address(struct nfs_server *server1,
2102 struct nfs_server *server2)
2103{
2104 struct sockaddr *sap1, *sap2;
2105
2106 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2107 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2108
2109 if (sap1->sa_family != sap2->sa_family)
2110 return 0;
2111
2112 switch (sap1->sa_family) {
2113 case AF_INET: {
2114 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2115 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2116 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2117 return 0;
2118 if (sin1->sin_port != sin2->sin_port)
2119 return 0;
2120 break;
2121 }
2122 case AF_INET6: {
2123 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2124 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2125 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2126 return 0;
2127 if (sin1->sin6_port != sin2->sin6_port)
2128 return 0;
2129 break;
2130 }
2131 default:
2132 return 0;
2133 }
2134
2135 return 1;
2136}
2137
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002138static int nfs_compare_super(struct super_block *sb, void *data)
2139{
2140 struct nfs_sb_mountdata *sb_mntdata = data;
2141 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2142 int mntflags = sb_mntdata->mntflags;
2143
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002144 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002145 return 0;
2146 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2147 if (old->flags & NFS_MOUNT_UNSHARED)
2148 return 0;
2149 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2150 return 0;
2151 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002152}
2153
Miklos Szeredifa799752008-04-30 00:54:33 -07002154static int nfs_bdi_register(struct nfs_server *server)
2155{
2156 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2157}
2158
David Howells54ceac42006-08-22 20:06:13 -04002159static int nfs_get_sb(struct file_system_type *fs_type,
2160 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2161{
2162 struct nfs_server *server = NULL;
2163 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002164 struct nfs_parsed_mount_data *data;
2165 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002166 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002167 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002168 struct nfs_sb_mountdata sb_mntdata = {
2169 .mntflags = flags,
2170 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002171 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002172
Trond Myklebustc5811db2009-10-06 15:40:15 -04002173 data = nfs_alloc_parsed_mount_data(3);
Trond Myklebust33852a12008-06-19 14:20:11 -04002174 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2175 if (data == NULL || mntfh == NULL)
2176 goto out_free_fh;
2177
2178 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002179
David Howells54ceac42006-08-22 20:06:13 -04002180 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002181 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002182 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002183 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002184
Chuck Lever764302c2009-09-08 19:50:03 -04002185#ifdef CONFIG_NFS_V4
2186 if (data->version == 4) {
2187 error = nfs4_try_mount(flags, dev_name, data, mnt);
2188 kfree(data->client_address);
2189 goto out;
2190 }
2191#endif /* CONFIG_NFS_V4 */
2192
David Howells54ceac42006-08-22 20:06:13 -04002193 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002194 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002195 if (IS_ERR(server)) {
2196 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002197 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002198 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002199 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002200
Trond Myklebust75180df2007-05-16 16:53:28 -04002201 if (server->flags & NFS_MOUNT_UNSHARED)
2202 compare_super = NULL;
2203
David Howells54ceac42006-08-22 20:06:13 -04002204 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002205 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002206 if (IS_ERR(s)) {
2207 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002208 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002209 }
2210
David Howells54ceac42006-08-22 20:06:13 -04002211 if (s->s_fs_info != server) {
2212 nfs_free_server(server);
2213 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002214 } else {
2215 error = nfs_bdi_register(server);
2216 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002217 goto error_splat_bdi;
David Howellsf7b422b2006-06-09 09:34:33 -04002218 }
David Howells54ceac42006-08-22 20:06:13 -04002219
2220 if (!s->s_root) {
2221 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002222 nfs_fill_super(s, data);
David Howells2df54802009-09-23 14:36:39 -04002223 nfs_fscache_get_super_cookie(
2224 s, data ? data->fscache_uniq : NULL, NULL);
David Howells54ceac42006-08-22 20:06:13 -04002225 }
2226
Trond Myklebust33852a12008-06-19 14:20:11 -04002227 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002228 if (IS_ERR(mntroot)) {
2229 error = PTR_ERR(mntroot);
2230 goto error_splat_super;
2231 }
2232
Trond Myklebust33852a12008-06-19 14:20:11 -04002233 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002234 if (error)
2235 goto error_splat_root;
2236
David Howellsf7b422b2006-06-09 09:34:33 -04002237 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002238 mnt->mnt_sb = s;
2239 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002240 error = 0;
2241
2242out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002243 kfree(data->nfs_server.hostname);
2244 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002245 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002246 security_free_mnt_opts(&data->lsm_opts);
2247out_free_fh:
2248 kfree(mntfh);
2249 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002250 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002251
David Howells54ceac42006-08-22 20:06:13 -04002252out_err_nosb:
2253 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002254 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002255
Eric Parisf9c3a382008-03-05 14:20:18 -05002256error_splat_root:
2257 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002258error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002259 if (server && !s->s_root)
2260 bdi_unregister(&server->backing_dev_info);
2261error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002262 deactivate_locked_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002263 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002264}
2265
David Howells54ceac42006-08-22 20:06:13 -04002266/*
Trond Myklebust387c1492010-02-03 08:27:35 -05002267 * Ensure that we unregister the bdi before kill_anon_super
2268 * releases the device name
2269 */
2270static void nfs_put_super(struct super_block *s)
2271{
2272 struct nfs_server *server = NFS_SB(s);
2273
2274 bdi_unregister(&server->backing_dev_info);
2275}
2276
2277/*
David Howells54ceac42006-08-22 20:06:13 -04002278 * Destroy an NFS2/3 superblock
2279 */
David Howellsf7b422b2006-06-09 09:34:33 -04002280static void nfs_kill_super(struct super_block *s)
2281{
2282 struct nfs_server *server = NFS_SB(s);
2283
2284 kill_anon_super(s);
David Howells08734042009-04-03 16:42:42 +01002285 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002286 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002287}
2288
David Howells54ceac42006-08-22 20:06:13 -04002289/*
2290 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2291 */
2292static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2293 const char *dev_name, void *raw_data,
2294 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002295{
2296 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002297 struct super_block *s;
2298 struct nfs_server *server;
2299 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002300 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002301 struct nfs_sb_mountdata sb_mntdata = {
2302 .mntflags = flags,
2303 };
David Howells54ceac42006-08-22 20:06:13 -04002304 int error;
2305
2306 dprintk("--> nfs_xdev_get_sb()\n");
2307
2308 /* create a new volume representation */
2309 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2310 if (IS_ERR(server)) {
2311 error = PTR_ERR(server);
2312 goto out_err_noserver;
2313 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002314 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002315
Trond Myklebust75180df2007-05-16 16:53:28 -04002316 if (server->flags & NFS_MOUNT_UNSHARED)
2317 compare_super = NULL;
2318
David Howells54ceac42006-08-22 20:06:13 -04002319 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002320 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002321 if (IS_ERR(s)) {
2322 error = PTR_ERR(s);
2323 goto out_err_nosb;
2324 }
2325
2326 if (s->s_fs_info != server) {
2327 nfs_free_server(server);
2328 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002329 } else {
2330 error = nfs_bdi_register(server);
2331 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002332 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002333 }
2334
2335 if (!s->s_root) {
2336 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002337 nfs_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002338 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002339 }
2340
2341 mntroot = nfs_get_root(s, data->fh);
2342 if (IS_ERR(mntroot)) {
2343 error = PTR_ERR(mntroot);
2344 goto error_splat_super;
2345 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002346 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002347 dput(mntroot);
2348 error = -ESTALE;
2349 goto error_splat_super;
2350 }
David Howells54ceac42006-08-22 20:06:13 -04002351
2352 s->s_flags |= MS_ACTIVE;
2353 mnt->mnt_sb = s;
2354 mnt->mnt_root = mntroot;
2355
Eric Parisf9c3a382008-03-05 14:20:18 -05002356 /* clone any lsm security options from the parent to the new sb */
2357 security_sb_clone_mnt_opts(data->sb, s);
2358
David Howells54ceac42006-08-22 20:06:13 -04002359 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2360 return 0;
2361
2362out_err_nosb:
2363 nfs_free_server(server);
2364out_err_noserver:
2365 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2366 return error;
2367
2368error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002369 if (server && !s->s_root)
2370 bdi_unregister(&server->backing_dev_info);
2371error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002372 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002373 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2374 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002375}
2376
2377#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002378
2379/*
2380 * Finish setting up a cloned NFS4 superblock
2381 */
2382static void nfs4_clone_super(struct super_block *sb,
2383 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002384{
David Howells54ceac42006-08-22 20:06:13 -04002385 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2386 sb->s_blocksize = old_sb->s_blocksize;
2387 sb->s_maxbytes = old_sb->s_maxbytes;
2388 sb->s_time_gran = 1;
2389 sb->s_op = old_sb->s_op;
2390 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002391}
2392
2393/*
2394 * Set up an NFS4 superblock
2395 */
David Howells54ceac42006-08-22 20:06:13 -04002396static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002397{
David Howellsf7b422b2006-06-09 09:34:33 -04002398 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002399 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002400 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002401}
2402
Trond Myklebust01c3f052009-06-17 13:22:58 -07002403static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2404{
2405 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
2406}
2407
Chuck Lever7630c852009-09-08 19:49:57 -04002408static int nfs4_validate_text_mount_data(void *options,
2409 struct nfs_parsed_mount_data *args,
2410 const char *dev_name)
2411{
2412 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2413
Trond Myklebustf5855fe2009-10-06 15:40:37 -04002414 nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
Chuck Lever7630c852009-09-08 19:49:57 -04002415
2416 nfs_validate_transport_protocol(args);
2417
2418 nfs4_validate_mount_flags(args);
2419
Trond Myklebust2ecda722009-09-08 19:50:07 -04002420 if (args->version != 4) {
2421 dfprintk(MOUNT,
2422 "NFS4: Illegal mount version\n");
2423 return -EINVAL;
2424 }
2425
Chuck Lever7630c852009-09-08 19:49:57 -04002426 if (args->auth_flavor_len > 1) {
2427 dfprintk(MOUNT,
2428 "NFS4: Too many RPC auth flavours specified\n");
2429 return -EINVAL;
2430 }
2431
2432 if (args->client_address == NULL) {
2433 dfprintk(MOUNT,
2434 "NFS4: mount program didn't pass callback address\n");
2435 return -EINVAL;
2436 }
2437
2438 return nfs_parse_devname(dev_name,
2439 &args->nfs_server.hostname,
2440 NFS4_MAXNAMLEN,
2441 &args->nfs_server.export_path,
2442 NFS4_MAXPATHLEN);
2443}
2444
David Howells54ceac42006-08-22 20:06:13 -04002445/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002446 * Validate NFSv4 mount options
2447 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002448static int nfs4_validate_mount_data(void *options,
2449 struct nfs_parsed_mount_data *args,
2450 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002451{
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002452 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002453 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002454 char *c;
2455
2456 if (data == NULL)
2457 goto out_no_data;
2458
2459 switch (data->version) {
2460 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002461 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002462 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002463 if (data->host_addrlen == 0)
2464 goto out_no_address;
2465 args->nfs_server.addrlen = data->host_addrlen;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002466 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002467 return -EFAULT;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002468 if (!nfs_verify_server_address(sap))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002469 goto out_no_address;
2470
Chuck Lever6738b252008-06-24 16:33:54 -04002471 if (data->auth_flavourlen) {
2472 if (data->auth_flavourlen > 1)
2473 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002474 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002475 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002476 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002477 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002478 }
2479
2480 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2481 if (IS_ERR(c))
2482 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002483 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002484
2485 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2486 if (IS_ERR(c))
2487 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002488 args->nfs_server.export_path = c;
2489 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002490
2491 c = strndup_user(data->client_addr.data, 16);
2492 if (IS_ERR(c))
2493 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002494 args->client_address = c;
2495
2496 /*
2497 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2498 * can deal with.
2499 */
2500
2501 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2502 args->rsize = data->rsize;
2503 args->wsize = data->wsize;
2504 args->timeo = data->timeo;
2505 args->retrans = data->retrans;
2506 args->acregmin = data->acregmin;
2507 args->acregmax = data->acregmax;
2508 args->acdirmin = data->acdirmin;
2509 args->acdirmax = data->acdirmax;
2510 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002511 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002512
2513 break;
Chuck Lever7630c852009-09-08 19:49:57 -04002514 default:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002515 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002516 return -EINVAL;
2517
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002518 if (!nfs_verify_server_address(sap))
Chuck Lever80071222007-07-01 12:13:59 -04002519 return -EINVAL;
Chuck Levered596a82008-06-26 17:47:05 -04002520
Chuck Lever7630c852009-09-08 19:49:57 -04002521 return nfs4_validate_text_mount_data(options, args, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002522 }
2523
2524 return 0;
2525
2526out_no_data:
2527 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2528 return -EINVAL;
2529
2530out_inval_auth:
2531 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2532 data->auth_flavourlen);
2533 return -EINVAL;
2534
2535out_no_address:
2536 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2537 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002538}
2539
2540/*
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002541 * Get the superblock for the NFS4 root partition
David Howells54ceac42006-08-22 20:06:13 -04002542 */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002543static int nfs4_remote_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -04002544 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002545{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002546 struct nfs_parsed_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002547 struct super_block *s;
2548 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002549 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002550 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002551 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002552 struct nfs_sb_mountdata sb_mntdata = {
2553 .mntflags = flags,
2554 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002555 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002556
Trond Myklebust33852a12008-06-19 14:20:11 -04002557 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2558 if (data == NULL || mntfh == NULL)
2559 goto out_free_fh;
2560
2561 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002562
David Howells54ceac42006-08-22 20:06:13 -04002563 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002564 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002565 if (IS_ERR(server)) {
2566 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002567 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002568 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002569 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002570
Trond Myklebust75180df2007-05-16 16:53:28 -04002571 if (server->flags & NFS4_MOUNT_UNSHARED)
2572 compare_super = NULL;
2573
David Howells54ceac42006-08-22 20:06:13 -04002574 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002575 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002576 if (IS_ERR(s)) {
2577 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002578 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002579 }
2580
Trond Myklebust5dd31772006-08-24 01:03:05 -04002581 if (s->s_fs_info != server) {
2582 nfs_free_server(server);
2583 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002584 } else {
2585 error = nfs_bdi_register(server);
2586 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002587 goto error_splat_bdi;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002588 }
2589
David Howells54ceac42006-08-22 20:06:13 -04002590 if (!s->s_root) {
2591 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002592 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002593 nfs_fscache_get_super_cookie(
2594 s, data ? data->fscache_uniq : NULL, NULL);
Trond Myklebust816724e2006-06-24 08:41:41 -04002595 }
David Howellsf7b422b2006-06-09 09:34:33 -04002596
Trond Myklebust33852a12008-06-19 14:20:11 -04002597 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002598 if (IS_ERR(mntroot)) {
2599 error = PTR_ERR(mntroot);
2600 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002601 }
David Howells54ceac42006-08-22 20:06:13 -04002602
Trond Myklebust33852a12008-06-19 14:20:11 -04002603 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002604 if (error)
2605 goto error_splat_root;
2606
David Howellsf7b422b2006-06-09 09:34:33 -04002607 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002608 mnt->mnt_sb = s;
2609 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002610 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002611
Chuck Lever29eb9812007-07-01 12:12:30 -04002612out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002613 security_free_mnt_opts(&data->lsm_opts);
2614out_free_fh:
2615 kfree(mntfh);
Trond Myklebust816724e2006-06-24 08:41:41 -04002616 return error;
David Howells54ceac42006-08-22 20:06:13 -04002617
Chuck Lever29eb9812007-07-01 12:12:30 -04002618out_free:
2619 nfs_free_server(server);
2620 goto out;
2621
Eric Paris46c8ac72008-05-02 13:42:42 -07002622error_splat_root:
2623 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002624error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002625 if (server && !s->s_root)
2626 bdi_unregister(&server->backing_dev_info);
2627error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002628 deactivate_locked_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002629 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002630}
2631
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002632static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2633 int flags, void *data, const char *hostname)
2634{
2635 struct vfsmount *root_mnt;
2636 char *root_devname;
2637 size_t len;
2638
2639 len = strlen(hostname) + 3;
2640 root_devname = kmalloc(len, GFP_KERNEL);
2641 if (root_devname == NULL)
2642 return ERR_PTR(-ENOMEM);
2643 snprintf(root_devname, len, "%s:/", hostname);
2644 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2645 kfree(root_devname);
2646 return root_mnt;
2647}
2648
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002649static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2650{
2651 char *page = (char *) __get_free_page(GFP_KERNEL);
2652 char *devname, *tmp;
2653
2654 if (page == NULL)
2655 return;
2656 devname = nfs_path(path->mnt->mnt_devname,
2657 path->mnt->mnt_root, path->dentry,
2658 page, PAGE_SIZE);
2659 if (devname == NULL)
2660 goto out_freepage;
2661 tmp = kstrdup(devname, GFP_KERNEL);
2662 if (tmp == NULL)
2663 goto out_freepage;
2664 kfree(mnt->mnt_devname);
2665 mnt->mnt_devname = tmp;
2666out_freepage:
2667 free_page((unsigned long)page);
2668}
2669
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002670static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2671 const char *export_path, struct vfsmount *mnt_target)
2672{
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002673 struct mnt_namespace *ns_private;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002674 struct nameidata nd;
2675 struct super_block *s;
2676 int ret;
2677
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002678 ns_private = create_mnt_ns(root_mnt);
2679 ret = PTR_ERR(ns_private);
2680 if (IS_ERR(ns_private))
2681 goto out_mntput;
2682
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002683 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2684 export_path, LOOKUP_FOLLOW, &nd);
2685
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002686 put_mnt_ns(ns_private);
2687
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002688 if (ret != 0)
2689 goto out_err;
2690
2691 s = nd.path.mnt->mnt_sb;
2692 atomic_inc(&s->s_active);
2693 mnt_target->mnt_sb = s;
2694 mnt_target->mnt_root = dget(nd.path.dentry);
2695
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002696 /* Correct the device pathname */
2697 nfs_fix_devname(&nd.path, mnt_target);
2698
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002699 path_put(&nd.path);
2700 down_write(&s->s_umount);
2701 return 0;
2702out_mntput:
2703 mntput(root_mnt);
2704out_err:
2705 return ret;
2706}
2707
Chuck Levera6fe23b2009-09-08 19:50:00 -04002708static int nfs4_try_mount(int flags, const char *dev_name,
2709 struct nfs_parsed_mount_data *data,
2710 struct vfsmount *mnt)
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002711{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002712 char *export_path;
2713 struct vfsmount *root_mnt;
Chuck Levera6fe23b2009-09-08 19:50:00 -04002714 int error;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002715
Chuck Levera6fe23b2009-09-08 19:50:00 -04002716 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002717
2718 export_path = data->nfs_server.export_path;
2719 data->nfs_server.export_path = "/";
2720 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2721 data->nfs_server.hostname);
2722 data->nfs_server.export_path = export_path;
2723
2724 error = PTR_ERR(root_mnt);
2725 if (IS_ERR(root_mnt))
2726 goto out;
2727
2728 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2729
2730out:
Chuck Levera6fe23b2009-09-08 19:50:00 -04002731 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2732 error != 0 ? " [error]" : "");
2733 return error;
2734}
2735
2736/*
2737 * Get the superblock for an NFS4 mountpoint
2738 */
2739static int nfs4_get_sb(struct file_system_type *fs_type,
2740 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2741{
2742 struct nfs_parsed_mount_data *data;
2743 int error = -ENOMEM;
2744
Trond Myklebustc5811db2009-10-06 15:40:15 -04002745 data = nfs_alloc_parsed_mount_data(4);
Chuck Levera6fe23b2009-09-08 19:50:00 -04002746 if (data == NULL)
2747 goto out_free_data;
2748
2749 /* Validate the mount data */
2750 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2751 if (error < 0)
2752 goto out;
2753
2754 error = nfs4_try_mount(flags, dev_name, data, mnt);
2755
2756out:
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002757 kfree(data->client_address);
2758 kfree(data->nfs_server.export_path);
2759 kfree(data->nfs_server.hostname);
2760 kfree(data->fscache_uniq);
2761out_free_data:
2762 kfree(data);
2763 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2764 error != 0 ? " [error]" : "");
2765 return error;
2766}
2767
David Howellsf7b422b2006-06-09 09:34:33 -04002768static void nfs4_kill_super(struct super_block *sb)
2769{
2770 struct nfs_server *server = NFS_SB(sb);
2771
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002772 dprintk("--> %s\n", __func__);
Trond Myklebust515d8612008-12-23 15:21:46 -05002773 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002774 kill_anon_super(sb);
David Howells08734042009-04-03 16:42:42 +01002775 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002776 nfs_free_server(server);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002777 dprintk("<-- %s\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -04002778}
2779
2780/*
David Howells54ceac42006-08-22 20:06:13 -04002781 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002782 */
David Howells54ceac42006-08-22 20:06:13 -04002783static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2784 const char *dev_name, void *raw_data,
2785 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002786{
2787 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002788 struct super_block *s;
2789 struct nfs_server *server;
2790 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002791 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002792 struct nfs_sb_mountdata sb_mntdata = {
2793 .mntflags = flags,
2794 };
David Howells54ceac42006-08-22 20:06:13 -04002795 int error;
2796
2797 dprintk("--> nfs4_xdev_get_sb()\n");
2798
2799 /* create a new volume representation */
2800 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2801 if (IS_ERR(server)) {
2802 error = PTR_ERR(server);
2803 goto out_err_noserver;
2804 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002805 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002806
Trond Myklebust75180df2007-05-16 16:53:28 -04002807 if (server->flags & NFS4_MOUNT_UNSHARED)
2808 compare_super = NULL;
2809
David Howells54ceac42006-08-22 20:06:13 -04002810 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002811 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002812 if (IS_ERR(s)) {
2813 error = PTR_ERR(s);
2814 goto out_err_nosb;
2815 }
2816
2817 if (s->s_fs_info != server) {
2818 nfs_free_server(server);
2819 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002820 } else {
2821 error = nfs_bdi_register(server);
2822 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002823 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002824 }
2825
2826 if (!s->s_root) {
2827 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002828 nfs4_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002829 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002830 }
2831
2832 mntroot = nfs4_get_root(s, data->fh);
2833 if (IS_ERR(mntroot)) {
2834 error = PTR_ERR(mntroot);
2835 goto error_splat_super;
2836 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002837 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2838 dput(mntroot);
2839 error = -ESTALE;
2840 goto error_splat_super;
2841 }
David Howells54ceac42006-08-22 20:06:13 -04002842
2843 s->s_flags |= MS_ACTIVE;
2844 mnt->mnt_sb = s;
2845 mnt->mnt_root = mntroot;
2846
Eric Paris46c8ac72008-05-02 13:42:42 -07002847 security_sb_clone_mnt_opts(data->sb, s);
2848
David Howells54ceac42006-08-22 20:06:13 -04002849 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2850 return 0;
2851
2852out_err_nosb:
2853 nfs_free_server(server);
2854out_err_noserver:
2855 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2856 return error;
2857
2858error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002859 if (server && !s->s_root)
2860 bdi_unregister(&server->backing_dev_info);
2861error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002862 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002863 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2864 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002865}
2866
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002867static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
2868 int flags, const char *dev_name, void *raw_data,
2869 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002870{
2871 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002872 struct super_block *s;
2873 struct nfs_server *server;
2874 struct dentry *mntroot;
2875 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002876 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002877 struct nfs_sb_mountdata sb_mntdata = {
2878 .mntflags = flags,
2879 };
David Howells54ceac42006-08-22 20:06:13 -04002880 int error;
2881
2882 dprintk("--> nfs4_referral_get_sb()\n");
2883
2884 /* create a new volume representation */
2885 server = nfs4_create_referral_server(data, &mntfh);
2886 if (IS_ERR(server)) {
2887 error = PTR_ERR(server);
2888 goto out_err_noserver;
2889 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002890 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002891
Trond Myklebust75180df2007-05-16 16:53:28 -04002892 if (server->flags & NFS4_MOUNT_UNSHARED)
2893 compare_super = NULL;
2894
David Howells54ceac42006-08-22 20:06:13 -04002895 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002896 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002897 if (IS_ERR(s)) {
2898 error = PTR_ERR(s);
2899 goto out_err_nosb;
2900 }
2901
2902 if (s->s_fs_info != server) {
2903 nfs_free_server(server);
2904 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002905 } else {
2906 error = nfs_bdi_register(server);
2907 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002908 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002909 }
2910
2911 if (!s->s_root) {
2912 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002913 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002914 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002915 }
2916
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002917 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002918 if (IS_ERR(mntroot)) {
2919 error = PTR_ERR(mntroot);
2920 goto error_splat_super;
2921 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002922 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2923 dput(mntroot);
2924 error = -ESTALE;
2925 goto error_splat_super;
2926 }
David Howells54ceac42006-08-22 20:06:13 -04002927
2928 s->s_flags |= MS_ACTIVE;
2929 mnt->mnt_sb = s;
2930 mnt->mnt_root = mntroot;
2931
Eric Paris46c8ac72008-05-02 13:42:42 -07002932 security_sb_clone_mnt_opts(data->sb, s);
2933
David Howells54ceac42006-08-22 20:06:13 -04002934 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2935 return 0;
2936
2937out_err_nosb:
2938 nfs_free_server(server);
2939out_err_noserver:
2940 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2941 return error;
2942
2943error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002944 if (server && !s->s_root)
2945 bdi_unregister(&server->backing_dev_info);
2946error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002947 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002948 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2949 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002950}
2951
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002952/*
2953 * Create an NFS4 server record on referral traversal
2954 */
2955static int nfs4_referral_get_sb(struct file_system_type *fs_type,
2956 int flags, const char *dev_name, void *raw_data,
2957 struct vfsmount *mnt)
2958{
2959 struct nfs_clone_mount *data = raw_data;
2960 char *export_path;
2961 struct vfsmount *root_mnt;
2962 int error;
2963
2964 dprintk("--> nfs4_referral_get_sb()\n");
2965
2966 export_path = data->mnt_path;
2967 data->mnt_path = "/";
2968
2969 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
2970 flags, data, data->hostname);
2971 data->mnt_path = export_path;
2972
2973 error = PTR_ERR(root_mnt);
2974 if (IS_ERR(root_mnt))
2975 goto out;
2976
2977 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2978out:
2979 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
2980 error != 0 ? " [error]" : "");
2981 return error;
2982}
2983
David Howells54ceac42006-08-22 20:06:13 -04002984#endif /* CONFIG_NFS_V4 */