blob: e01637240eeb59fb4032b926e3c709c771fdcedd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Chuck Leverfd00a8f2007-12-10 14:57:38 -050052#include <net/ipv6.h>
Chuck Leverd8e77482008-06-23 12:37:01 -040053#include <linux/netdevice.h>
David Howellsf7b422b2006-06-09 09:34:33 -040054#include <linux/nfs_xdr.h>
Adrian Bunkb5d5dfb2007-02-12 00:53:40 -080055#include <linux/magic.h>
Chuck Leverbf0fd762007-07-01 12:13:44 -040056#include <linux/parser.h>
David Howellsf7b422b2006-06-09 09:34:33 -040057
58#include <asm/system.h>
59#include <asm/uaccess.h>
60
61#include "nfs4_fs.h"
62#include "callback.h"
63#include "delegation.h"
64#include "iostat.h"
65#include "internal.h"
David Howells08734042009-04-03 16:42:42 +010066#include "fscache.h"
David Howellsf7b422b2006-06-09 09:34:33 -040067
68#define NFSDBG_FACILITY NFSDBG_VFS
69
Chuck Leverbf0fd762007-07-01 12:13:44 -040070enum {
71 /* Mount options that take no arguments */
72 Opt_soft, Opt_hard,
Chuck Leverbf0fd762007-07-01 12:13:44 -040073 Opt_posix, Opt_noposix,
74 Opt_cto, Opt_nocto,
75 Opt_ac, Opt_noac,
76 Opt_lock, Opt_nolock,
Chuck Lever764302c2009-09-08 19:50:03 -040077 Opt_v2, Opt_v3, Opt_v4,
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040078 Opt_udp, Opt_tcp, Opt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -040079 Opt_acl, Opt_noacl,
80 Opt_rdirplus, Opt_nordirplus,
Trond Myklebust75180df2007-05-16 16:53:28 -040081 Opt_sharecache, Opt_nosharecache,
Chuck Leverd7403512008-12-23 15:21:37 -050082 Opt_resvport, Opt_noresvport,
David Howellsb797cac2009-04-03 16:42:48 +010083 Opt_fscache, Opt_nofscache,
Chuck Leverbf0fd762007-07-01 12:13:44 -040084
85 /* Mount options that take integer arguments */
86 Opt_port,
87 Opt_rsize, Opt_wsize, Opt_bsize,
88 Opt_timeo, Opt_retrans,
89 Opt_acregmin, Opt_acregmax,
90 Opt_acdirmin, Opt_acdirmax,
91 Opt_actimeo,
92 Opt_namelen,
93 Opt_mountport,
Chuck Levere887cbc2007-10-26 13:32:29 -040094 Opt_mountvers,
Chuck Leverad879ce2007-10-26 13:32:24 -040095 Opt_nfsvers,
Mike Sager3fd5be92009-04-01 09:21:48 -040096 Opt_minorversion,
Chuck Leverbf0fd762007-07-01 12:13:44 -040097
98 /* Mount options that take string arguments */
Chuck Lever33832032007-12-10 14:59:13 -050099 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
Chuck Lever0ac83772007-09-11 18:01:04 -0400100 Opt_addr, Opt_mountaddr, Opt_clientaddr,
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400101 Opt_lookupcache,
David Howellsb797cac2009-04-03 16:42:48 +0100102 Opt_fscache_uniq,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400103
Chuck Leverf45663c2008-06-24 19:28:02 -0400104 /* Special mount options */
105 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400106
107 Opt_err
108};
109
Steven Whitehousea447c092008-10-13 10:46:57 +0100110static const match_table_t nfs_mount_option_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400111 { Opt_userspace, "bg" },
112 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400113 { Opt_userspace, "retry=%s" },
114
Chuck Leverf45663c2008-06-24 19:28:02 -0400115 { Opt_sloppy, "sloppy" },
116
Chuck Leverbf0fd762007-07-01 12:13:44 -0400117 { Opt_soft, "soft" },
118 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400119 { Opt_deprecated, "intr" },
120 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400121 { Opt_posix, "posix" },
122 { Opt_noposix, "noposix" },
123 { Opt_cto, "cto" },
124 { Opt_nocto, "nocto" },
125 { Opt_ac, "ac" },
126 { Opt_noac, "noac" },
127 { Opt_lock, "lock" },
128 { Opt_nolock, "nolock" },
129 { Opt_v2, "v2" },
130 { Opt_v3, "v3" },
Chuck Lever764302c2009-09-08 19:50:03 -0400131 { Opt_v4, "v4" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400132 { Opt_udp, "udp" },
133 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400134 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400135 { Opt_acl, "acl" },
136 { Opt_noacl, "noacl" },
137 { Opt_rdirplus, "rdirplus" },
138 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400139 { Opt_sharecache, "sharecache" },
140 { Opt_nosharecache, "nosharecache" },
Chuck Leverd7403512008-12-23 15:21:37 -0500141 { Opt_resvport, "resvport" },
142 { Opt_noresvport, "noresvport" },
David Howellsb797cac2009-04-03 16:42:48 +0100143 { Opt_fscache, "fsc" },
144 { Opt_fscache_uniq, "fsc=%s" },
145 { Opt_nofscache, "nofsc" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400146
Chuck Levera5a16ba2009-06-17 18:02:14 -0700147 { Opt_port, "port=%s" },
148 { Opt_rsize, "rsize=%s" },
149 { Opt_wsize, "wsize=%s" },
150 { Opt_bsize, "bsize=%s" },
151 { Opt_timeo, "timeo=%s" },
152 { Opt_retrans, "retrans=%s" },
153 { Opt_acregmin, "acregmin=%s" },
154 { Opt_acregmax, "acregmax=%s" },
155 { Opt_acdirmin, "acdirmin=%s" },
156 { Opt_acdirmax, "acdirmax=%s" },
157 { Opt_actimeo, "actimeo=%s" },
158 { Opt_namelen, "namlen=%s" },
159 { Opt_mountport, "mountport=%s" },
160 { Opt_mountvers, "mountvers=%s" },
161 { Opt_nfsvers, "nfsvers=%s" },
162 { Opt_nfsvers, "vers=%s" },
Chuck Leverf3f4f4e2009-08-09 15:09:29 -0400163 { Opt_minorversion, "minorversion=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400164
165 { Opt_sec, "sec=%s" },
166 { Opt_proto, "proto=%s" },
167 { Opt_mountproto, "mountproto=%s" },
168 { Opt_addr, "addr=%s" },
169 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500170 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400171 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400172
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400173 { Opt_lookupcache, "lookupcache=%s" },
174
Chuck Leverbf0fd762007-07-01 12:13:44 -0400175 { Opt_err, NULL }
176};
177
178enum {
Jeff Laytonee671b02009-12-03 15:58:56 -0500179 Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400180
181 Opt_xprt_err
182};
183
Steven Whitehousea447c092008-10-13 10:46:57 +0100184static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400185 { Opt_xprt_udp, "udp" },
Jeff Laytonee671b02009-12-03 15:58:56 -0500186 { Opt_xprt_udp6, "udp6" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400187 { Opt_xprt_tcp, "tcp" },
Jeff Laytonee671b02009-12-03 15:58:56 -0500188 { Opt_xprt_tcp6, "tcp6" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400189 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400190
191 { Opt_xprt_err, NULL }
192};
193
194enum {
195 Opt_sec_none, Opt_sec_sys,
196 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
197 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
198 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
199
200 Opt_sec_err
201};
202
Steven Whitehousea447c092008-10-13 10:46:57 +0100203static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400204 { Opt_sec_none, "none" },
205 { Opt_sec_none, "null" },
206 { Opt_sec_sys, "sys" },
207
208 { Opt_sec_krb5, "krb5" },
209 { Opt_sec_krb5i, "krb5i" },
210 { Opt_sec_krb5p, "krb5p" },
211
212 { Opt_sec_lkey, "lkey" },
213 { Opt_sec_lkeyi, "lkeyi" },
214 { Opt_sec_lkeyp, "lkeyp" },
215
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500216 { Opt_sec_spkm, "spkm3" },
217 { Opt_sec_spkmi, "spkm3i" },
218 { Opt_sec_spkmp, "spkm3p" },
219
Chuck Leverbf0fd762007-07-01 12:13:44 -0400220 { Opt_sec_err, NULL }
221};
222
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400223enum {
224 Opt_lookupcache_all, Opt_lookupcache_positive,
225 Opt_lookupcache_none,
226
227 Opt_lookupcache_err
228};
229
230static match_table_t nfs_lookupcache_tokens = {
231 { Opt_lookupcache_all, "all" },
232 { Opt_lookupcache_positive, "pos" },
233 { Opt_lookupcache_positive, "positive" },
234 { Opt_lookupcache_none, "none" },
235
236 { Opt_lookupcache_err, NULL }
237};
238
Chuck Leverbf0fd762007-07-01 12:13:44 -0400239
Al Viro42faad92008-04-24 07:21:56 -0400240static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400241static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400242static int nfs_show_options(struct seq_file *, struct vfsmount *);
243static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400244static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400245static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400246 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebust387c1492010-02-03 08:27:35 -0500247static void nfs_put_super(struct super_block *);
David Howellsf7b422b2006-06-09 09:34:33 -0400248static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400249static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400250
251static struct file_system_type nfs_fs_type = {
252 .owner = THIS_MODULE,
253 .name = "nfs",
254 .get_sb = nfs_get_sb,
255 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700256 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400257};
258
David Howells54ceac42006-08-22 20:06:13 -0400259struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400260 .owner = THIS_MODULE,
261 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400262 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400263 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700264 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400265};
266
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800267static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400268 .alloc_inode = nfs_alloc_inode,
269 .destroy_inode = nfs_destroy_inode,
270 .write_inode = nfs_write_inode,
Trond Myklebust387c1492010-02-03 08:27:35 -0500271 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400272 .statfs = nfs_statfs,
273 .clear_inode = nfs_clear_inode,
274 .umount_begin = nfs_umount_begin,
275 .show_options = nfs_show_options,
276 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400277 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400278};
279
280#ifdef CONFIG_NFS_V4
Chuck Lever7630c852009-09-08 19:49:57 -0400281static int nfs4_validate_text_mount_data(void *options,
282 struct nfs_parsed_mount_data *args, const char *dev_name);
Chuck Levera6fe23b2009-09-08 19:50:00 -0400283static int nfs4_try_mount(int flags, const char *dev_name,
284 struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
Trond Myklebust816724e2006-06-24 08:41:41 -0400285static int nfs4_get_sb(struct file_system_type *fs_type,
286 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400287static int nfs4_remote_get_sb(struct file_system_type *fs_type,
288 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400289static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
290 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
291static int nfs4_referral_get_sb(struct file_system_type *fs_type,
292 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400293static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
294 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400295static void nfs4_kill_super(struct super_block *sb);
296
297static struct file_system_type nfs4_fs_type = {
298 .owner = THIS_MODULE,
299 .name = "nfs4",
300 .get_sb = nfs4_get_sb,
301 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700302 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400303};
304
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400305static struct file_system_type nfs4_remote_fs_type = {
306 .owner = THIS_MODULE,
307 .name = "nfs4",
308 .get_sb = nfs4_remote_get_sb,
309 .kill_sb = nfs4_kill_super,
310 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
311};
312
David Howells54ceac42006-08-22 20:06:13 -0400313struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400314 .owner = THIS_MODULE,
315 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400316 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400317 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700318 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400319};
320
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400321static struct file_system_type nfs4_remote_referral_fs_type = {
322 .owner = THIS_MODULE,
323 .name = "nfs4",
324 .get_sb = nfs4_remote_referral_get_sb,
325 .kill_sb = nfs4_kill_super,
326 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
327};
328
David Howells54ceac42006-08-22 20:06:13 -0400329struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400330 .owner = THIS_MODULE,
331 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400332 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400333 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700334 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400335};
336
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800337static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400338 .alloc_inode = nfs_alloc_inode,
339 .destroy_inode = nfs_destroy_inode,
340 .write_inode = nfs_write_inode,
Trond Myklebust387c1492010-02-03 08:27:35 -0500341 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400342 .statfs = nfs_statfs,
343 .clear_inode = nfs4_clear_inode,
344 .umount_begin = nfs_umount_begin,
345 .show_options = nfs_show_options,
346 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400347 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400348};
349#endif
350
Rusty Russell8e1f9362007-07-17 04:03:17 -0700351static struct shrinker acl_shrinker = {
352 .shrink = nfs_access_cache_shrinker,
353 .seeks = DEFAULT_SEEKS,
354};
Trond Myklebust979df722006-07-25 11:28:19 -0400355
David Howellsf7b422b2006-06-09 09:34:33 -0400356/*
357 * Register the NFS filesystems
358 */
359int __init register_nfs_fs(void)
360{
361 int ret;
362
363 ret = register_filesystem(&nfs_fs_type);
364 if (ret < 0)
365 goto error_0;
366
David Howellsf7b422b2006-06-09 09:34:33 -0400367 ret = nfs_register_sysctl();
368 if (ret < 0)
369 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800370#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400371 ret = register_filesystem(&nfs4_fs_type);
372 if (ret < 0)
373 goto error_2;
374#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700375 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400376 return 0;
377
378#ifdef CONFIG_NFS_V4
379error_2:
380 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800381#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400382error_1:
383 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400384error_0:
385 return ret;
386}
387
388/*
389 * Unregister the NFS filesystems
390 */
391void __exit unregister_nfs_fs(void)
392{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700393 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400394#ifdef CONFIG_NFS_V4
395 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400396#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700397 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400398 unregister_filesystem(&nfs_fs_type);
399}
400
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400401void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500402{
403 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400404
405 if (atomic_inc_return(&server->active) == 1)
406 atomic_inc(&sb->s_active);
407}
408
409void nfs_sb_deactive(struct super_block *sb)
410{
411 struct nfs_server *server = NFS_SB(sb);
412
413 if (atomic_dec_and_test(&server->active))
414 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500415}
416
David Howellsf7b422b2006-06-09 09:34:33 -0400417/*
418 * Deliver file system statistics to userspace
419 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400420static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400421{
David Howells0c7d90c2006-08-22 20:06:10 -0400422 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400423 unsigned char blockbits;
424 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400425 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400426 struct nfs_fattr fattr;
427 struct nfs_fsstat res = {
428 .fattr = &fattr,
429 };
430 int error;
431
David Howells8fa5c002006-08-22 20:06:12 -0400432 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400433 if (error < 0)
434 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700435 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400436
437 /*
438 * Current versions of glibc do not correctly handle the
439 * case where f_frsize != f_bsize. Eventually we want to
440 * report the value of wtmult in this field.
441 */
David Howells0c7d90c2006-08-22 20:06:10 -0400442 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400443
444 /*
445 * On most *nix systems, f_blocks, f_bfree, and f_bavail
446 * are reported in units of f_frsize. Linux hasn't had
447 * an f_frsize field in its statfs struct until recently,
448 * thus historically Linux's sys_statfs reports these
449 * fields in units of f_bsize.
450 */
David Howells0c7d90c2006-08-22 20:06:10 -0400451 buf->f_bsize = dentry->d_sb->s_blocksize;
452 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400453 blockres = (1 << blockbits) - 1;
454 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
455 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
456 buf->f_bavail = (res.abytes + blockres) >> blockbits;
457
458 buf->f_files = res.tfiles;
459 buf->f_ffree = res.afiles;
460
461 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700462
David Howellsf7b422b2006-06-09 09:34:33 -0400463 return 0;
464
465 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700466 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700467 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400468}
469
David Howells7d4e2742006-08-22 20:06:07 -0400470/*
471 * Map the security flavour number to a name
472 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400473static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
474{
David Howells7d4e2742006-08-22 20:06:07 -0400475 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400476 rpc_authflavor_t flavour;
477 const char *str;
478 } sec_flavours[] = {
479 { RPC_AUTH_NULL, "null" },
480 { RPC_AUTH_UNIX, "sys" },
481 { RPC_AUTH_GSS_KRB5, "krb5" },
482 { RPC_AUTH_GSS_KRB5I, "krb5i" },
483 { RPC_AUTH_GSS_KRB5P, "krb5p" },
484 { RPC_AUTH_GSS_LKEY, "lkey" },
485 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
486 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
487 { RPC_AUTH_GSS_SPKM, "spkm" },
488 { RPC_AUTH_GSS_SPKMI, "spkmi" },
489 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400490 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400491 };
492 int i;
493
Chuck Lever4d81cd12007-07-01 12:12:40 -0400494 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400495 if (sec_flavours[i].flavour == flavour)
496 break;
497 }
498 return sec_flavours[i].str;
499}
500
Jeff Laytonee671b02009-12-03 15:58:56 -0500501static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
502 int showdefaults)
503{
504 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
505
506 seq_printf(m, ",mountproto=");
507 switch (sap->sa_family) {
508 case AF_INET:
509 switch (nfss->mountd_protocol) {
510 case IPPROTO_UDP:
511 seq_printf(m, RPCBIND_NETID_UDP);
512 break;
513 case IPPROTO_TCP:
514 seq_printf(m, RPCBIND_NETID_TCP);
515 break;
516 default:
517 if (showdefaults)
518 seq_printf(m, "auto");
519 }
520 break;
521 case AF_INET6:
522 switch (nfss->mountd_protocol) {
523 case IPPROTO_UDP:
524 seq_printf(m, RPCBIND_NETID_UDP6);
525 break;
526 case IPPROTO_TCP:
527 seq_printf(m, RPCBIND_NETID_TCP6);
528 break;
529 default:
530 if (showdefaults)
531 seq_printf(m, "auto");
532 }
533 break;
534 default:
535 if (showdefaults)
536 seq_printf(m, "auto");
537 }
538}
539
Chuck Lever82d101d2008-03-14 14:10:37 -0400540static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
541 int showdefaults)
542{
543 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
544
545 switch (sap->sa_family) {
546 case AF_INET: {
547 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrisonbe859402008-10-31 00:56:28 -0700548 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400549 break;
550 }
551 case AF_INET6: {
552 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Chuck Leverd250e192009-12-03 15:58:56 -0500553 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400554 break;
555 }
556 default:
557 if (showdefaults)
558 seq_printf(m, ",mountaddr=unspecified");
559 }
560
561 if (nfss->mountd_version || showdefaults)
562 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
563 if (nfss->mountd_port || showdefaults)
564 seq_printf(m, ",mountport=%u", nfss->mountd_port);
565
Jeff Laytonee671b02009-12-03 15:58:56 -0500566 nfs_show_mountd_netid(m, nfss, showdefaults);
Chuck Lever82d101d2008-03-14 14:10:37 -0400567}
568
David Howellsf7b422b2006-06-09 09:34:33 -0400569/*
570 * Describe the mount options in force on this server representation
571 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400572static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
573 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400574{
David Howells509de812006-08-22 20:06:11 -0400575 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400576 int flag;
David Howells509de812006-08-22 20:06:11 -0400577 const char *str;
578 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400579 } nfs_info[] = {
580 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400581 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400582 { NFS_MOUNT_NOCTO, ",nocto", "" },
583 { NFS_MOUNT_NOAC, ",noac", "" },
584 { NFS_MOUNT_NONLM, ",nolock", "" },
585 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400586 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500587 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
588 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400589 { 0, NULL, NULL }
590 };
David Howells509de812006-08-22 20:06:11 -0400591 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400592 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400593 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400594
Chuck Lever82d101d2008-03-14 14:10:37 -0400595 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400596 seq_printf(m, ",rsize=%u", nfss->rsize);
597 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400598 if (nfss->bsize != 0)
599 seq_printf(m, ",bsize=%u", nfss->bsize);
600 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400601 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400602 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400603 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400604 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400605 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400606 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400607 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400608 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400609 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
610 if (nfss->flags & nfs_infop->flag)
611 seq_puts(m, nfs_infop->str);
612 else
613 seq_puts(m, nfs_infop->nostr);
614 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400615 seq_printf(m, ",proto=%s",
Jeff Laytonee671b02009-12-03 15:58:56 -0500616 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
Chuck Lever82d101d2008-03-14 14:10:37 -0400617 if (version == 4) {
618 if (nfss->port != NFS_PORT)
619 seq_printf(m, ",port=%u", nfss->port);
620 } else
621 if (nfss->port)
622 seq_printf(m, ",port=%u", nfss->port);
623
Trond Myklebust33170232007-12-20 16:03:59 -0500624 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
625 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400626 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400627
628 if (version != 4)
629 nfs_show_mountd_options(m, nfss, showdefaults);
630
631#ifdef CONFIG_NFS_V4
632 if (clp->rpc_ops->version == 4)
633 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
634#endif
David Howellsb797cac2009-04-03 16:42:48 +0100635 if (nfss->options & NFS_OPTION_FSCACHE)
636 seq_printf(m, ",fsc");
David Howellsf7b422b2006-06-09 09:34:33 -0400637}
638
639/*
640 * Describe the mount options on this VFS mountpoint
641 */
642static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
643{
644 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
645
646 nfs_show_mount_options(m, nfss, 0);
647
Chuck Lever5d8515c2007-12-10 14:57:16 -0500648 seq_printf(m, ",addr=%s",
649 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
650 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400651
652 return 0;
653}
654
655/*
656 * Present statistical information for this VFS mountpoint
657 */
658static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
659{
660 int i, cpu;
661 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
662 struct rpc_auth *auth = nfss->client->cl_auth;
663 struct nfs_iostats totals = { };
664
665 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
666
667 /*
668 * Display all mount option settings
669 */
670 seq_printf(m, "\n\topts:\t");
671 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
672 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
673 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
674 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
675 nfs_show_mount_options(m, nfss, 1);
676
677 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
678
679 seq_printf(m, "\n\tcaps:\t");
680 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400681 seq_printf(m, ",wtmult=%u", nfss->wtmult);
682 seq_printf(m, ",dtsize=%u", nfss->dtsize);
683 seq_printf(m, ",bsize=%u", nfss->bsize);
684 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400685
686#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500687 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400688 seq_printf(m, "\n\tnfsv4:\t");
689 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
690 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
691 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
692 }
693#endif
694
695 /*
696 * Display security flavor in effect for this mount
697 */
Chuck Lever2d767432008-03-14 14:10:08 -0400698 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400699 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400700 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400701
702 /*
703 * Display superblock I/O counters
704 */
705 for_each_possible_cpu(cpu) {
706 struct nfs_iostats *stats;
707
708 preempt_disable();
709 stats = per_cpu_ptr(nfss->io_stats, cpu);
710
711 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
712 totals.events[i] += stats->events[i];
713 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
714 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100715#ifdef CONFIG_NFS_FSCACHE
716 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
717 totals.fscache[i] += stats->fscache[i];
718#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400719
720 preempt_enable();
721 }
722
723 seq_printf(m, "\n\tevents:\t");
724 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
725 seq_printf(m, "%lu ", totals.events[i]);
726 seq_printf(m, "\n\tbytes:\t");
727 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
728 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100729#ifdef CONFIG_NFS_FSCACHE
730 if (nfss->options & NFS_OPTION_FSCACHE) {
731 seq_printf(m, "\n\tfsc:\t");
732 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
733 seq_printf(m, "%Lu ", totals.bytes[i]);
734 }
735#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400736 seq_printf(m, "\n");
737
738 rpc_print_iostats(m, nfss->client);
739
740 return 0;
741}
742
743/*
744 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400745 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400746 */
Al Viro42faad92008-04-24 07:21:56 -0400747static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400748{
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200749 struct nfs_server *server;
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400750 struct rpc_clnt *rpc;
751
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200752 server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400753 /* -EIO all pending I/O */
754 rpc = server->client_acl;
755 if (!IS_ERR(rpc))
756 rpc_killall_tasks(rpc);
757 rpc = server->client;
758 if (!IS_ERR(rpc))
759 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400760}
761
Trond Myklebustc5811db2009-10-06 15:40:15 -0400762static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
Chuck Lever9423a082009-09-23 14:36:38 -0400763{
764 struct nfs_parsed_mount_data *data;
765
766 data = kzalloc(sizeof(*data), GFP_KERNEL);
767 if (data) {
Chuck Lever9423a082009-09-23 14:36:38 -0400768 data->acregmin = NFS_DEF_ACREGMIN;
769 data->acregmax = NFS_DEF_ACREGMAX;
770 data->acdirmin = NFS_DEF_ACDIRMIN;
771 data->acdirmax = NFS_DEF_ACDIRMAX;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400772 data->mount_server.port = NFS_UNSPEC_PORT;
Chuck Lever9423a082009-09-23 14:36:38 -0400773 data->nfs_server.port = NFS_UNSPEC_PORT;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400774 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Lever9423a082009-09-23 14:36:38 -0400775 data->auth_flavors[0] = RPC_AUTH_UNIX;
776 data->auth_flavor_len = 1;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400777 data->version = version;
Chuck Lever9423a082009-09-23 14:36:38 -0400778 data->minorversion = 0;
779 }
780 return data;
781}
782
David Howellsf7b422b2006-06-09 09:34:33 -0400783/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500784 * Sanity-check a server address provided by the mount command.
785 *
786 * Address family must be initialized, and address must not be
787 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400788 */
789static int nfs_verify_server_address(struct sockaddr *addr)
790{
791 switch (addr->sa_family) {
792 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500793 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700794 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500795 }
796 case AF_INET6: {
797 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
798 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400799 }
800 }
801
Chuck Lever53a0b9c2009-08-09 15:09:36 -0400802 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
Chuck Leverfc50d582007-07-01 12:12:46 -0400803 return 0;
804}
805
Chuck Lever9412b922007-12-10 14:59:21 -0500806/*
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400807 * Select between a default port value and a user-specified port value.
808 * If a zero value is set, then autobind will be used.
809 */
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400810static void nfs_set_port(struct sockaddr *sap, int *port,
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400811 const unsigned short default_port)
812{
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400813 if (*port == NFS_UNSPEC_PORT)
814 *port = default_port;
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400815
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400816 rpc_set_port(sap, *port);
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400817}
818
819/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400820 * Sanity check the NFS transport protocol.
821 *
822 */
823static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
824{
825 switch (mnt->nfs_server.protocol) {
826 case XPRT_TRANSPORT_UDP:
827 case XPRT_TRANSPORT_TCP:
828 case XPRT_TRANSPORT_RDMA:
829 break;
830 default:
831 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
832 }
833}
834
835/*
836 * For text based NFSv2/v3 mounts, the mount protocol transport default
837 * settings should depend upon the specified NFS transport.
838 */
839static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
840{
841 nfs_validate_transport_protocol(mnt);
842
843 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
844 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
845 return;
846 switch (mnt->nfs_server.protocol) {
847 case XPRT_TRANSPORT_UDP:
848 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
849 break;
850 case XPRT_TRANSPORT_TCP:
851 case XPRT_TRANSPORT_RDMA:
852 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
853 }
854}
855
856/*
Chuck Lever01060c82008-06-24 16:33:38 -0400857 * Parse the value of the 'sec=' option.
Chuck Lever01060c82008-06-24 16:33:38 -0400858 */
859static int nfs_parse_security_flavors(char *value,
860 struct nfs_parsed_mount_data *mnt)
861{
862 substring_t args[MAX_OPT_ARGS];
863
864 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
865
866 switch (match_token(value, nfs_secflavor_tokens, args)) {
867 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400868 mnt->auth_flavors[0] = RPC_AUTH_NULL;
869 break;
870 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400871 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
872 break;
873 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400874 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
875 break;
876 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400877 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
878 break;
879 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400880 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
881 break;
882 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400883 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
884 break;
885 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400886 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
887 break;
888 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400889 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
890 break;
891 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400892 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
893 break;
894 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400895 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
896 break;
897 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400898 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
899 break;
900 default:
901 return 0;
902 }
903
Chuck Lever059f90b2009-08-09 15:09:31 -0400904 mnt->auth_flavor_len = 1;
Chuck Lever01060c82008-06-24 16:33:38 -0400905 return 1;
906}
907
908/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400909 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400910 * a data structure. The whole mount string is processed; bad options are
911 * skipped as they are encountered. If there were no errors, return 1;
912 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400913 */
914static int nfs_parse_mount_options(char *raw,
915 struct nfs_parsed_mount_data *mnt)
916{
Eric Parisf9c3a382008-03-05 14:20:18 -0500917 char *p, *string, *secdata;
Chuck Leverd23c45f2009-06-17 18:02:13 -0700918 int rc, sloppy = 0, invalid_option = 0;
Jeff Laytonee671b02009-12-03 15:58:56 -0500919 unsigned short protofamily = AF_UNSPEC;
920 unsigned short mountfamily = AF_UNSPEC;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400921
922 if (!raw) {
923 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
924 return 1;
925 }
926 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
927
Eric Parisf9c3a382008-03-05 14:20:18 -0500928 secdata = alloc_secdata();
929 if (!secdata)
930 goto out_nomem;
931
932 rc = security_sb_copy_data(raw, secdata);
933 if (rc)
934 goto out_security_failure;
935
936 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
937 if (rc)
938 goto out_security_failure;
939
940 free_secdata(secdata);
941
Chuck Leverbf0fd762007-07-01 12:13:44 -0400942 while ((p = strsep(&raw, ",")) != NULL) {
943 substring_t args[MAX_OPT_ARGS];
Chuck Levera5a16ba2009-06-17 18:02:14 -0700944 unsigned long option;
945 int token;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400946
947 if (!*p)
948 continue;
949
950 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
951
952 token = match_token(p, nfs_mount_option_tokens, args);
953 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400954
955 /*
956 * boolean options: foo/nofoo
957 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400958 case Opt_soft:
959 mnt->flags |= NFS_MOUNT_SOFT;
960 break;
961 case Opt_hard:
962 mnt->flags &= ~NFS_MOUNT_SOFT;
963 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400964 case Opt_posix:
965 mnt->flags |= NFS_MOUNT_POSIX;
966 break;
967 case Opt_noposix:
968 mnt->flags &= ~NFS_MOUNT_POSIX;
969 break;
970 case Opt_cto:
971 mnt->flags &= ~NFS_MOUNT_NOCTO;
972 break;
973 case Opt_nocto:
974 mnt->flags |= NFS_MOUNT_NOCTO;
975 break;
976 case Opt_ac:
977 mnt->flags &= ~NFS_MOUNT_NOAC;
978 break;
979 case Opt_noac:
980 mnt->flags |= NFS_MOUNT_NOAC;
981 break;
982 case Opt_lock:
983 mnt->flags &= ~NFS_MOUNT_NONLM;
984 break;
985 case Opt_nolock:
986 mnt->flags |= NFS_MOUNT_NONLM;
987 break;
988 case Opt_v2:
989 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -0400990 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400991 break;
992 case Opt_v3:
993 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -0400994 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400995 break;
Chuck Lever764302c2009-09-08 19:50:03 -0400996#ifdef CONFIG_NFS_V4
997 case Opt_v4:
998 mnt->flags &= ~NFS_MOUNT_VER3;
999 mnt->version = 4;
1000 break;
1001#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001002 case Opt_udp:
1003 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001004 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001005 break;
1006 case Opt_tcp:
1007 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001008 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001009 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001010 case Opt_rdma:
1011 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1012 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001013 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001014 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001015 case Opt_acl:
1016 mnt->flags &= ~NFS_MOUNT_NOACL;
1017 break;
1018 case Opt_noacl:
1019 mnt->flags |= NFS_MOUNT_NOACL;
1020 break;
1021 case Opt_rdirplus:
1022 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1023 break;
1024 case Opt_nordirplus:
1025 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1026 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001027 case Opt_sharecache:
1028 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1029 break;
1030 case Opt_nosharecache:
1031 mnt->flags |= NFS_MOUNT_UNSHARED;
1032 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001033 case Opt_resvport:
1034 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1035 break;
1036 case Opt_noresvport:
1037 mnt->flags |= NFS_MOUNT_NORESVPORT;
1038 break;
David Howellsb797cac2009-04-03 16:42:48 +01001039 case Opt_fscache:
1040 mnt->options |= NFS_OPTION_FSCACHE;
1041 kfree(mnt->fscache_uniq);
1042 mnt->fscache_uniq = NULL;
1043 break;
1044 case Opt_nofscache:
1045 mnt->options &= ~NFS_OPTION_FSCACHE;
1046 kfree(mnt->fscache_uniq);
1047 mnt->fscache_uniq = NULL;
1048 break;
1049 case Opt_fscache_uniq:
1050 string = match_strdup(args);
1051 if (!string)
1052 goto out_nomem;
1053 kfree(mnt->fscache_uniq);
1054 mnt->fscache_uniq = string;
1055 mnt->options |= NFS_OPTION_FSCACHE;
1056 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001057
Chuck Leverf45663c2008-06-24 19:28:02 -04001058 /*
1059 * options that take numeric values
1060 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001061 case Opt_port:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001062 string = match_strdup(args);
1063 if (string == NULL)
1064 goto out_nomem;
1065 rc = strict_strtoul(string, 10, &option);
1066 kfree(string);
1067 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001068 goto out_invalid_value;
1069 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001070 break;
1071 case Opt_rsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001072 string = match_strdup(args);
1073 if (string == NULL)
1074 goto out_nomem;
1075 rc = strict_strtoul(string, 10, &option);
1076 kfree(string);
1077 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001078 goto out_invalid_value;
1079 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001080 break;
1081 case Opt_wsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001082 string = match_strdup(args);
1083 if (string == NULL)
1084 goto out_nomem;
1085 rc = strict_strtoul(string, 10, &option);
1086 kfree(string);
1087 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001088 goto out_invalid_value;
1089 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001090 break;
1091 case Opt_bsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001092 string = match_strdup(args);
1093 if (string == NULL)
1094 goto out_nomem;
1095 rc = strict_strtoul(string, 10, &option);
1096 kfree(string);
1097 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001098 goto out_invalid_value;
1099 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001100 break;
1101 case Opt_timeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001102 string = match_strdup(args);
1103 if (string == NULL)
1104 goto out_nomem;
1105 rc = strict_strtoul(string, 10, &option);
1106 kfree(string);
1107 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001108 goto out_invalid_value;
1109 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001110 break;
1111 case Opt_retrans:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001112 string = match_strdup(args);
1113 if (string == NULL)
1114 goto out_nomem;
1115 rc = strict_strtoul(string, 10, &option);
1116 kfree(string);
1117 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001118 goto out_invalid_value;
1119 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001120 break;
1121 case Opt_acregmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001122 string = match_strdup(args);
1123 if (string == NULL)
1124 goto out_nomem;
1125 rc = strict_strtoul(string, 10, &option);
1126 kfree(string);
1127 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001128 goto out_invalid_value;
1129 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001130 break;
1131 case Opt_acregmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001132 string = match_strdup(args);
1133 if (string == NULL)
1134 goto out_nomem;
1135 rc = strict_strtoul(string, 10, &option);
1136 kfree(string);
1137 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001138 goto out_invalid_value;
1139 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001140 break;
1141 case Opt_acdirmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001142 string = match_strdup(args);
1143 if (string == NULL)
1144 goto out_nomem;
1145 rc = strict_strtoul(string, 10, &option);
1146 kfree(string);
1147 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001148 goto out_invalid_value;
1149 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001150 break;
1151 case Opt_acdirmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001152 string = match_strdup(args);
1153 if (string == NULL)
1154 goto out_nomem;
1155 rc = strict_strtoul(string, 10, &option);
1156 kfree(string);
1157 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001158 goto out_invalid_value;
1159 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001160 break;
1161 case Opt_actimeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001162 string = match_strdup(args);
1163 if (string == NULL)
1164 goto out_nomem;
1165 rc = strict_strtoul(string, 10, &option);
1166 kfree(string);
1167 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001168 goto out_invalid_value;
1169 mnt->acregmin = mnt->acregmax =
1170 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001171 break;
1172 case Opt_namelen:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001173 string = match_strdup(args);
1174 if (string == NULL)
1175 goto out_nomem;
1176 rc = strict_strtoul(string, 10, &option);
1177 kfree(string);
1178 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001179 goto out_invalid_value;
1180 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001181 break;
1182 case Opt_mountport:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001183 string = match_strdup(args);
1184 if (string == NULL)
1185 goto out_nomem;
1186 rc = strict_strtoul(string, 10, &option);
1187 kfree(string);
1188 if (rc != 0 || option > USHORT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001189 goto out_invalid_value;
1190 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001192 case Opt_mountvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001193 string = match_strdup(args);
1194 if (string == NULL)
1195 goto out_nomem;
1196 rc = strict_strtoul(string, 10, &option);
1197 kfree(string);
1198 if (rc != 0 ||
Chuck Leverf45663c2008-06-24 19:28:02 -04001199 option < NFS_MNT_VERSION ||
Chuck Leverd23c45f2009-06-17 18:02:13 -07001200 option > NFS_MNT3_VERSION)
1201 goto out_invalid_value;
1202 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001203 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001204 case Opt_nfsvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001205 string = match_strdup(args);
1206 if (string == NULL)
1207 goto out_nomem;
1208 rc = strict_strtoul(string, 10, &option);
1209 kfree(string);
1210 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001211 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001212 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001213 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001214 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001215 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001216 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001217 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001218 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001219 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001220 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001221#ifdef CONFIG_NFS_V4
1222 case NFS4_VERSION:
1223 mnt->flags &= ~NFS_MOUNT_VER3;
1224 mnt->version = 4;
1225 break;
1226#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001227 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001228 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001229 }
1230 break;
Mike Sager3fd5be92009-04-01 09:21:48 -04001231 case Opt_minorversion:
Chuck Leverf3f4f4e2009-08-09 15:09:29 -04001232 string = match_strdup(args);
1233 if (string == NULL)
1234 goto out_nomem;
1235 rc = strict_strtoul(string, 10, &option);
1236 kfree(string);
1237 if (rc != 0)
1238 goto out_invalid_value;
1239 if (option > NFS4_MAX_MINOR_VERSION)
1240 goto out_invalid_value;
1241 mnt->minorversion = option;
Mike Sager3fd5be92009-04-01 09:21:48 -04001242 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001243
Chuck Leverf45663c2008-06-24 19:28:02 -04001244 /*
1245 * options that take text values
1246 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001247 case Opt_sec:
1248 string = match_strdup(args);
1249 if (string == NULL)
1250 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001251 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001252 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001253 if (!rc) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001254 dfprintk(MOUNT, "NFS: unrecognized "
1255 "security flavor\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001256 return 0;
Chuck Leverf45663c2008-06-24 19:28:02 -04001257 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001258 break;
1259 case Opt_proto:
1260 string = match_strdup(args);
1261 if (string == NULL)
1262 goto out_nomem;
1263 token = match_token(string,
1264 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001265
Jeff Laytonee671b02009-12-03 15:58:56 -05001266 protofamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001267 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001268 case Opt_xprt_udp6:
1269 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001270 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001271 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001272 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001273 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001274 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001275 case Opt_xprt_tcp6:
1276 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001277 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001278 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001279 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001280 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001281 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001282 case Opt_xprt_rdma:
1283 /* vector side protocols to TCP */
1284 mnt->flags |= NFS_MOUNT_TCP;
1285 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001286 xprt_load_transport(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001287 kfree(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001288 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001289 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001290 dfprintk(MOUNT, "NFS: unrecognized "
1291 "transport protocol\n");
Yinghai Lu4223a4a2009-10-20 14:13:46 +09001292 kfree(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001293 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001294 }
1295 break;
1296 case Opt_mountproto:
1297 string = match_strdup(args);
1298 if (string == NULL)
1299 goto out_nomem;
1300 token = match_token(string,
1301 nfs_xprt_protocol_tokens, args);
Trond Myklebustd508afb2009-04-06 16:41:35 -07001302 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001303
Jeff Laytonee671b02009-12-03 15:58:56 -05001304 mountfamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001305 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001306 case Opt_xprt_udp6:
1307 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001308 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001309 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001310 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001311 case Opt_xprt_tcp6:
1312 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001313 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001314 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001315 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001316 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001317 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001318 dfprintk(MOUNT, "NFS: unrecognized "
1319 "transport protocol\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001320 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001321 }
1322 break;
1323 case Opt_addr:
1324 string = match_strdup(args);
1325 if (string == NULL)
1326 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001327 mnt->nfs_server.addrlen =
1328 rpc_pton(string, strlen(string),
1329 (struct sockaddr *)
1330 &mnt->nfs_server.address,
1331 sizeof(mnt->nfs_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001332 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001333 if (mnt->nfs_server.addrlen == 0)
1334 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001335 break;
1336 case Opt_clientaddr:
1337 string = match_strdup(args);
1338 if (string == NULL)
1339 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001340 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001341 mnt->client_address = string;
1342 break;
Chuck Lever33832032007-12-10 14:59:13 -05001343 case Opt_mounthost:
1344 string = match_strdup(args);
1345 if (string == NULL)
1346 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001347 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001348 mnt->mount_server.hostname = string;
1349 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001350 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001351 string = match_strdup(args);
1352 if (string == NULL)
1353 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001354 mnt->mount_server.addrlen =
1355 rpc_pton(string, strlen(string),
1356 (struct sockaddr *)
1357 &mnt->mount_server.address,
1358 sizeof(mnt->mount_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001359 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001360 if (mnt->mount_server.addrlen == 0)
1361 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001362 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001363 case Opt_lookupcache:
1364 string = match_strdup(args);
1365 if (string == NULL)
1366 goto out_nomem;
1367 token = match_token(string,
1368 nfs_lookupcache_tokens, args);
1369 kfree(string);
1370 switch (token) {
1371 case Opt_lookupcache_all:
1372 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1373 break;
1374 case Opt_lookupcache_positive:
1375 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1376 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1377 break;
1378 case Opt_lookupcache_none:
1379 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1380 break;
1381 default:
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001382 dfprintk(MOUNT, "NFS: invalid "
1383 "lookupcache argument\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001384 return 0;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001385 };
1386 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001387
Chuck Leverf45663c2008-06-24 19:28:02 -04001388 /*
1389 * Special options
1390 */
1391 case Opt_sloppy:
1392 sloppy = 1;
1393 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1394 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001395 case Opt_userspace:
1396 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001397 dfprintk(MOUNT, "NFS: ignoring mount option "
1398 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001399 break;
1400
1401 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001402 invalid_option = 1;
Chuck Leverf45663c2008-06-24 19:28:02 -04001403 dfprintk(MOUNT, "NFS: unrecognized mount option "
1404 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001405 }
1406 }
1407
Chuck Leverd23c45f2009-06-17 18:02:13 -07001408 if (!sloppy && invalid_option)
1409 return 0;
1410
Jeff Laytonee671b02009-12-03 15:58:56 -05001411 /*
1412 * verify that any proto=/mountproto= options match the address
1413 * familiies in the addr=/mountaddr= options.
1414 */
1415 if (protofamily != AF_UNSPEC &&
1416 protofamily != mnt->nfs_server.address.ss_family)
1417 goto out_proto_mismatch;
1418
1419 if (mountfamily != AF_UNSPEC) {
1420 if (mnt->mount_server.addrlen) {
1421 if (mountfamily != mnt->mount_server.address.ss_family)
1422 goto out_mountproto_mismatch;
1423 } else {
1424 if (mountfamily != mnt->nfs_server.address.ss_family)
1425 goto out_mountproto_mismatch;
1426 }
1427 }
1428
Chuck Leverbf0fd762007-07-01 12:13:44 -04001429 return 1;
1430
Jeff Laytonee671b02009-12-03 15:58:56 -05001431out_mountproto_mismatch:
1432 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1433 "option\n");
1434 return 0;
1435out_proto_mismatch:
1436 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1437 return 0;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001438out_invalid_address:
1439 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1440 return 0;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001441out_invalid_value:
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001442 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001443 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001444out_nomem:
1445 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1446 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001447out_security_failure:
1448 free_secdata(secdata);
1449 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1450 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001451}
1452
1453/*
Chuck Leverec88f282009-08-09 15:09:32 -04001454 * Match the requested auth flavors with the list returned by
1455 * the server. Returns zero and sets the mount's authentication
1456 * flavor on success; returns -EACCES if server does not support
1457 * the requested flavor.
1458 */
1459static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1460 struct nfs_mount_request *request)
1461{
1462 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1463
1464 /*
Chuck Lever5eecfde2009-08-21 17:50:30 -04001465 * Certain releases of Linux's mountd return an empty
1466 * flavor list. To prevent behavioral regression with
1467 * these servers (ie. rejecting mounts that used to
1468 * succeed), revert to pre-2.6.32 behavior (no checking)
1469 * if the returned flavor list is empty.
1470 */
1471 if (server_authlist_len == 0)
1472 return 0;
1473
1474 /*
Chuck Leverec88f282009-08-09 15:09:32 -04001475 * We avoid sophisticated negotiating here, as there are
1476 * plenty of cases where we can get it wrong, providing
1477 * either too little or too much security.
1478 *
1479 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1480 * flavor listed first. However, some servers list
1481 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1482 * preferred default, in args->auth_flavors[0] if user
1483 * didn't specify sec= mount option.
1484 */
1485 for (i = 0; i < args->auth_flavor_len; i++)
1486 for (j = 0; j < server_authlist_len; j++)
1487 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1488 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1489 request->auth_flavs[j]);
1490 args->auth_flavors[0] = request->auth_flavs[j];
1491 return 0;
1492 }
1493
1494 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1495 nfs_umount(request);
1496 return -EACCES;
1497}
1498
1499/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001500 * Use the remote server's MOUNT service to request the NFS file handle
1501 * corresponding to the provided path.
1502 */
1503static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1504 struct nfs_fh *root_fh)
1505{
Chuck Leverec88f282009-08-09 15:09:32 -04001506 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1507 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
Chuck Leverc5d120f2008-12-23 15:21:35 -05001508 struct nfs_mount_request request = {
1509 .sap = (struct sockaddr *)
1510 &args->mount_server.address,
1511 .dirpath = args->nfs_server.export_path,
1512 .protocol = args->mount_server.protocol,
1513 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001514 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverec88f282009-08-09 15:09:32 -04001515 .auth_flav_len = &server_authlist_len,
1516 .auth_flavs = server_authlist,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001517 };
Chuck Lever4c568012007-12-10 14:59:28 -05001518 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001519
1520 if (args->mount_server.version == 0) {
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001521 switch (args->version) {
1522 default:
1523 args->mount_server.version = NFS_MNT3_VERSION;
1524 break;
1525 case 2:
1526 args->mount_server.version = NFS_MNT_VERSION;
1527 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001528 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001529 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001530
Chuck Lever33832032007-12-10 14:59:13 -05001531 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001532 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001533 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001534 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001535
Chuck Lever0076d7b2007-07-01 12:13:49 -04001536 /*
1537 * Construct the mount server's address.
1538 */
Chuck Lever4c568012007-12-10 14:59:28 -05001539 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001540 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001541 args->nfs_server.addrlen);
1542 args->mount_server.addrlen = args->nfs_server.addrlen;
1543 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001544 request.salen = args->mount_server.addrlen;
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001545 nfs_set_port(request.sap, &args->mount_server.port, 0);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001546
1547 /*
1548 * Now ask the mount server to map our export path
1549 * to a file handle.
1550 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001551 status = nfs_mount(&request);
Chuck Leverec88f282009-08-09 15:09:32 -04001552 if (status != 0) {
1553 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1554 request.hostname, status);
1555 return status;
1556 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001557
Chuck Leverec88f282009-08-09 15:09:32 -04001558 /*
1559 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1560 */
1561 if (args->mount_server.version != NFS_MNT3_VERSION)
1562 return 0;
1563 return nfs_walk_authlist(args, &request);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001564}
1565
Chuck Leverd1aa0822008-06-23 12:36:45 -04001566static int nfs_parse_simple_hostname(const char *dev_name,
1567 char **hostname, size_t maxnamlen,
1568 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001569{
1570 size_t len;
1571 char *colon, *comma;
1572
1573 colon = strchr(dev_name, ':');
1574 if (colon == NULL)
1575 goto out_bad_devname;
1576
1577 len = colon - dev_name;
1578 if (len > maxnamlen)
1579 goto out_hostname;
1580
1581 /* N.B. caller will free nfs_server.hostname in all cases */
1582 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1583 if (!*hostname)
1584 goto out_nomem;
1585
1586 /* kill possible hostname list: not supported */
1587 comma = strchr(*hostname, ',');
1588 if (comma != NULL) {
1589 if (comma == *hostname)
1590 goto out_bad_devname;
1591 *comma = '\0';
1592 }
1593
1594 colon++;
1595 len = strlen(colon);
1596 if (len > maxpathlen)
1597 goto out_path;
1598 *export_path = kstrndup(colon, len, GFP_KERNEL);
1599 if (!*export_path)
1600 goto out_nomem;
1601
1602 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1603 return 0;
1604
1605out_bad_devname:
1606 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1607 return -EINVAL;
1608
1609out_nomem:
1610 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1611 return -ENOMEM;
1612
1613out_hostname:
1614 dfprintk(MOUNT, "NFS: server hostname too long\n");
1615 return -ENAMETOOLONG;
1616
1617out_path:
1618 dfprintk(MOUNT, "NFS: export pathname too long\n");
1619 return -ENAMETOOLONG;
1620}
1621
1622/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001623 * Hostname has square brackets around it because it contains one or
1624 * more colons. We look for the first closing square bracket, and a
1625 * colon must follow it.
1626 */
1627static int nfs_parse_protected_hostname(const char *dev_name,
1628 char **hostname, size_t maxnamlen,
1629 char **export_path, size_t maxpathlen)
1630{
1631 size_t len;
1632 char *start, *end;
1633
1634 start = (char *)(dev_name + 1);
1635
1636 end = strchr(start, ']');
1637 if (end == NULL)
1638 goto out_bad_devname;
1639 if (*(end + 1) != ':')
1640 goto out_bad_devname;
1641
1642 len = end - start;
1643 if (len > maxnamlen)
1644 goto out_hostname;
1645
1646 /* N.B. caller will free nfs_server.hostname in all cases */
1647 *hostname = kstrndup(start, len, GFP_KERNEL);
1648 if (*hostname == NULL)
1649 goto out_nomem;
1650
1651 end += 2;
1652 len = strlen(end);
1653 if (len > maxpathlen)
1654 goto out_path;
1655 *export_path = kstrndup(end, len, GFP_KERNEL);
1656 if (!*export_path)
1657 goto out_nomem;
1658
1659 return 0;
1660
1661out_bad_devname:
1662 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1663 return -EINVAL;
1664
1665out_nomem:
1666 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1667 return -ENOMEM;
1668
1669out_hostname:
1670 dfprintk(MOUNT, "NFS: server hostname too long\n");
1671 return -ENAMETOOLONG;
1672
1673out_path:
1674 dfprintk(MOUNT, "NFS: export pathname too long\n");
1675 return -ENAMETOOLONG;
1676}
1677
1678/*
1679 * Split "dev_name" into "hostname:export_path".
1680 *
1681 * The leftmost colon demarks the split between the server's hostname
1682 * and the export path. If the hostname starts with a left square
1683 * bracket, then it may contain colons.
1684 *
1685 * Note: caller frees hostname and export path, even on error.
1686 */
1687static int nfs_parse_devname(const char *dev_name,
1688 char **hostname, size_t maxnamlen,
1689 char **export_path, size_t maxpathlen)
1690{
1691 if (*dev_name == '[')
1692 return nfs_parse_protected_hostname(dev_name,
1693 hostname, maxnamlen,
1694 export_path, maxpathlen);
1695
1696 return nfs_parse_simple_hostname(dev_name,
1697 hostname, maxnamlen,
1698 export_path, maxpathlen);
1699}
1700
1701/*
David Howells54ceac42006-08-22 20:06:13 -04001702 * Validate the NFS2/NFS3 mount data
1703 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001704 *
1705 * For option strings, user space handles the following behaviors:
1706 *
1707 * + DNS: mapping server host name to IP address ("addr=" option)
1708 *
1709 * + failure mode: how to behave if a mount request can't be handled
1710 * immediately ("fg/bg" option)
1711 *
1712 * + retry: how often to retry a mount request ("retry=" option)
1713 *
1714 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1715 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001716 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001717static int nfs_validate_mount_data(void *options,
1718 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001719 struct nfs_fh *mntfh,
1720 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001721{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001722 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001723 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
Chuck Lever136d5582007-07-01 12:13:54 -04001724
Chuck Lever5df36e72007-07-01 12:12:56 -04001725 if (data == NULL)
1726 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001727
David Howellsf7b422b2006-06-09 09:34:33 -04001728 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001729 case 1:
1730 data->namlen = 0;
1731 case 2:
1732 data->bsize = 0;
1733 case 3:
1734 if (data->flags & NFS_MOUNT_VER3)
1735 goto out_no_v3;
1736 data->root.size = NFS2_FHSIZE;
1737 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1738 case 4:
1739 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1740 goto out_no_sec;
1741 case 5:
1742 memset(data->context, 0, sizeof(data->context));
1743 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001744 if (data->flags & NFS_MOUNT_VER3) {
1745 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1746 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001747 mntfh->size = data->root.size;
Chuck Lever764302c2009-09-08 19:50:03 -04001748 args->version = 3;
1749 } else {
Chuck Lever5df36e72007-07-01 12:12:56 -04001750 mntfh->size = NFS2_FHSIZE;
Chuck Lever764302c2009-09-08 19:50:03 -04001751 args->version = 2;
1752 }
Chuck Lever5df36e72007-07-01 12:12:56 -04001753
Chuck Lever5df36e72007-07-01 12:12:56 -04001754
1755 memcpy(mntfh->data, data->root.data, mntfh->size);
1756 if (mntfh->size < sizeof(mntfh->data))
1757 memset(mntfh->data + mntfh->size, 0,
1758 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001759
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001760 /*
1761 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1762 * can deal with.
1763 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001764 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001765 args->rsize = data->rsize;
1766 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001767 args->timeo = data->timeo;
1768 args->retrans = data->retrans;
1769 args->acregmin = data->acregmin;
1770 args->acregmax = data->acregmax;
1771 args->acdirmin = data->acdirmin;
1772 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001773
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001774 memcpy(sap, &data->addr, sizeof(data->addr));
Chuck Lever4c568012007-12-10 14:59:28 -05001775 args->nfs_server.addrlen = sizeof(data->addr);
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001776 if (!nfs_verify_server_address(sap))
Chuck Lever4c568012007-12-10 14:59:28 -05001777 goto out_no_address;
1778
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001779 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001780 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001781 /* N.B. caller will free nfs_server.hostname in all cases */
1782 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1783 args->namlen = data->namlen;
1784 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001785
1786 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1787 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001788 if (!args->nfs_server.hostname)
1789 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001790
1791 /*
1792 * The legacy version 6 binary mount data from userspace has a
1793 * field used only to transport selinux information into the
1794 * the kernel. To continue to support that functionality we
1795 * have a touch of selinux knowledge here in the NFS code. The
1796 * userspace code converted context=blah to just blah so we are
1797 * converting back to the full string selinux understands.
1798 */
1799 if (data->context[0]){
1800#ifdef CONFIG_SECURITY_SELINUX
1801 int rc;
1802 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1803 if (!opts_str)
1804 return -ENOMEM;
1805 strcpy(opts_str, "context=");
1806 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1807 strcat(opts_str, &data->context[0]);
1808 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1809 kfree(opts_str);
1810 if (rc)
1811 return rc;
1812#else
1813 return -EINVAL;
1814#endif
1815 }
1816
Chuck Lever5df36e72007-07-01 12:12:56 -04001817 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001818 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001819 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001820
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001821 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001822 return -EINVAL;
1823
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001824 if (!nfs_verify_server_address(sap))
Chuck Lever6e88e062007-09-24 15:39:50 -04001825 goto out_no_address;
1826
Chuck Lever764302c2009-09-08 19:50:03 -04001827 if (args->version == 4)
1828#ifdef CONFIG_NFS_V4
1829 return nfs4_validate_text_mount_data(options,
1830 args, dev_name);
1831#else
1832 goto out_v4_not_compiled;
1833#endif
1834
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001835 nfs_set_port(sap, &args->nfs_server.port, 0);
Chuck Levered596a82008-06-26 17:47:05 -04001836
Trond Myklebust259875e2008-07-02 14:43:47 -04001837 nfs_set_mount_transport_protocol(args);
1838
Chuck Leverdc045892008-06-23 12:36:37 -04001839 status = nfs_parse_devname(dev_name,
1840 &args->nfs_server.hostname,
1841 PAGE_SIZE,
1842 &args->nfs_server.export_path,
1843 NFS_MAXPATHLEN);
1844 if (!status)
1845 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001846
Chuck Leverdc045892008-06-23 12:36:37 -04001847 kfree(args->nfs_server.export_path);
1848 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001849
Chuck Lever136d5582007-07-01 12:13:54 -04001850 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001851 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001852
Chuck Lever136d5582007-07-01 12:13:54 -04001853 break;
1854 }
David Howellsf7b422b2006-06-09 09:34:33 -04001855 }
David Howells54ceac42006-08-22 20:06:13 -04001856
David Howellsf7b422b2006-06-09 09:34:33 -04001857#ifndef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001858 if (args->version == 3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001859 goto out_v3_not_compiled;
1860#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001861
David Howells54ceac42006-08-22 20:06:13 -04001862 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001863
1864out_no_data:
1865 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1866 return -EINVAL;
1867
1868out_no_v3:
1869 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1870 data->version);
1871 return -EINVAL;
1872
1873out_no_sec:
1874 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1875 return -EINVAL;
1876
Chuck Lever5df36e72007-07-01 12:12:56 -04001877#ifndef CONFIG_NFS_V3
1878out_v3_not_compiled:
1879 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1880 return -EPROTONOSUPPORT;
1881#endif /* !CONFIG_NFS_V3 */
1882
Chuck Lever764302c2009-09-08 19:50:03 -04001883#ifndef CONFIG_NFS_V4
1884out_v4_not_compiled:
1885 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1886 return -EPROTONOSUPPORT;
1887#endif /* !CONFIG_NFS_V4 */
1888
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001889out_nomem:
1890 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1891 return -ENOMEM;
1892
Chuck Lever5df36e72007-07-01 12:12:56 -04001893out_no_address:
1894 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1895 return -EINVAL;
1896
1897out_invalid_fh:
1898 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1899 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001900}
1901
Jeff Layton48b605f2008-06-10 15:38:39 -04001902static int
1903nfs_compare_remount_data(struct nfs_server *nfss,
1904 struct nfs_parsed_mount_data *data)
1905{
1906 if (data->flags != nfss->flags ||
1907 data->rsize != nfss->rsize ||
1908 data->wsize != nfss->wsize ||
1909 data->retrans != nfss->client->cl_timeout->to_retries ||
1910 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1911 data->acregmin != nfss->acregmin / HZ ||
1912 data->acregmax != nfss->acregmax / HZ ||
1913 data->acdirmin != nfss->acdirmin / HZ ||
1914 data->acdirmax != nfss->acdirmax / HZ ||
1915 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04001916 data->nfs_server.port != nfss->port ||
Jeff Layton48b605f2008-06-10 15:38:39 -04001917 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
Stefan Richtera1be9ee2009-10-12 11:26:12 -04001918 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
1919 (struct sockaddr *)&nfss->nfs_client->cl_addr))
Jeff Layton48b605f2008-06-10 15:38:39 -04001920 return -EINVAL;
1921
1922 return 0;
1923}
1924
1925static int
1926nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1927{
1928 int error;
1929 struct nfs_server *nfss = sb->s_fs_info;
1930 struct nfs_parsed_mount_data *data;
1931 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1932 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001933 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001934
1935 /*
1936 * Userspace mount programs that send binary options generally send
1937 * them populated with default values. We have no way to know which
1938 * ones were explicitly specified. Fall back to legacy behavior and
1939 * just return success.
1940 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001941 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1942 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1943 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001944 return 0;
1945
1946 data = kzalloc(sizeof(*data), GFP_KERNEL);
1947 if (data == NULL)
1948 return -ENOMEM;
1949
1950 /* fill out struct with values from existing mount */
1951 data->flags = nfss->flags;
1952 data->rsize = nfss->rsize;
1953 data->wsize = nfss->wsize;
1954 data->retrans = nfss->client->cl_timeout->to_retries;
1955 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1956 data->acregmin = nfss->acregmin / HZ;
1957 data->acregmax = nfss->acregmax / HZ;
1958 data->acdirmin = nfss->acdirmin / HZ;
1959 data->acdirmax = nfss->acdirmax / HZ;
1960 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04001961 data->nfs_server.port = nfss->port;
Jeff Layton48b605f2008-06-10 15:38:39 -04001962 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1963 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1964 data->nfs_server.addrlen);
1965
1966 /* overwrite those values with any that were specified */
1967 error = nfs_parse_mount_options((char *)options, data);
1968 if (error < 0)
1969 goto out;
1970
1971 /* compare new mount options with old ones */
1972 error = nfs_compare_remount_data(nfss, data);
1973out:
1974 kfree(data);
1975 return error;
1976}
1977
David Howells54ceac42006-08-22 20:06:13 -04001978/*
1979 * Initialise the common bits of the superblock
1980 */
1981static inline void nfs_initialise_sb(struct super_block *sb)
1982{
1983 struct nfs_server *server = NFS_SB(sb);
1984
1985 sb->s_magic = NFS_SUPER_MAGIC;
1986
1987 /* We probably want something more informative here */
1988 snprintf(sb->s_id, sizeof(sb->s_id),
1989 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1990
1991 if (sb->s_blocksize == 0)
1992 sb->s_blocksize = nfs_block_bits(server->wsize,
1993 &sb->s_blocksize_bits);
1994
1995 if (server->flags & NFS_MOUNT_NOAC)
1996 sb->s_flags |= MS_SYNCHRONOUS;
1997
Jens Axboe32a88aa2009-09-16 15:02:33 +02001998 sb->s_bdi = &server->backing_dev_info;
1999
David Howells54ceac42006-08-22 20:06:13 -04002000 nfs_super_set_maxbytes(sb, server->maxfilesize);
2001}
2002
2003/*
2004 * Finish setting up an NFS2/3 superblock
2005 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04002006static void nfs_fill_super(struct super_block *sb,
2007 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04002008{
2009 struct nfs_server *server = NFS_SB(sb);
2010
2011 sb->s_blocksize_bits = 0;
2012 sb->s_blocksize = 0;
2013 if (data->bsize)
2014 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2015
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002016 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002017 /* The VFS shouldn't apply the umask to mode bits. We will do
2018 * so ourselves when necessary.
2019 */
2020 sb->s_flags |= MS_POSIXACL;
2021 sb->s_time_gran = 1;
2022 }
2023
2024 sb->s_op = &nfs_sops;
2025 nfs_initialise_sb(sb);
2026}
2027
2028/*
2029 * Finish setting up a cloned NFS2/3 superblock
2030 */
2031static void nfs_clone_super(struct super_block *sb,
2032 const struct super_block *old_sb)
2033{
2034 struct nfs_server *server = NFS_SB(sb);
2035
2036 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2037 sb->s_blocksize = old_sb->s_blocksize;
2038 sb->s_maxbytes = old_sb->s_maxbytes;
2039
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002040 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002041 /* The VFS shouldn't apply the umask to mode bits. We will do
2042 * so ourselves when necessary.
2043 */
2044 sb->s_flags |= MS_POSIXACL;
2045 sb->s_time_gran = 1;
2046 }
2047
2048 sb->s_op = old_sb->s_op;
2049 nfs_initialise_sb(sb);
2050}
2051
Trond Myklebust275a5d22007-05-16 16:53:28 -04002052static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2053{
2054 const struct nfs_server *a = s->s_fs_info;
2055 const struct rpc_clnt *clnt_a = a->client;
2056 const struct rpc_clnt *clnt_b = b->client;
2057
2058 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2059 goto Ebusy;
2060 if (a->nfs_client != b->nfs_client)
2061 goto Ebusy;
2062 if (a->flags != b->flags)
2063 goto Ebusy;
2064 if (a->wsize != b->wsize)
2065 goto Ebusy;
2066 if (a->rsize != b->rsize)
2067 goto Ebusy;
2068 if (a->acregmin != b->acregmin)
2069 goto Ebusy;
2070 if (a->acregmax != b->acregmax)
2071 goto Ebusy;
2072 if (a->acdirmin != b->acdirmin)
2073 goto Ebusy;
2074 if (a->acdirmax != b->acdirmax)
2075 goto Ebusy;
2076 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2077 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002078 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04002079Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002080 return 0;
2081}
2082
2083struct nfs_sb_mountdata {
2084 struct nfs_server *server;
2085 int mntflags;
2086};
2087
2088static int nfs_set_super(struct super_block *s, void *data)
2089{
2090 struct nfs_sb_mountdata *sb_mntdata = data;
2091 struct nfs_server *server = sb_mntdata->server;
2092 int ret;
2093
2094 s->s_flags = sb_mntdata->mntflags;
2095 s->s_fs_info = server;
2096 ret = set_anon_super(s, server);
2097 if (ret == 0)
2098 server->s_dev = s->s_dev;
2099 return ret;
2100}
2101
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002102static int nfs_compare_super_address(struct nfs_server *server1,
2103 struct nfs_server *server2)
2104{
2105 struct sockaddr *sap1, *sap2;
2106
2107 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2108 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2109
2110 if (sap1->sa_family != sap2->sa_family)
2111 return 0;
2112
2113 switch (sap1->sa_family) {
2114 case AF_INET: {
2115 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2116 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2117 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2118 return 0;
2119 if (sin1->sin_port != sin2->sin_port)
2120 return 0;
2121 break;
2122 }
2123 case AF_INET6: {
2124 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2125 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2126 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2127 return 0;
2128 if (sin1->sin6_port != sin2->sin6_port)
2129 return 0;
2130 break;
2131 }
2132 default:
2133 return 0;
2134 }
2135
2136 return 1;
2137}
2138
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002139static int nfs_compare_super(struct super_block *sb, void *data)
2140{
2141 struct nfs_sb_mountdata *sb_mntdata = data;
2142 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2143 int mntflags = sb_mntdata->mntflags;
2144
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002145 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002146 return 0;
2147 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2148 if (old->flags & NFS_MOUNT_UNSHARED)
2149 return 0;
2150 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2151 return 0;
2152 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002153}
2154
Miklos Szeredifa799752008-04-30 00:54:33 -07002155static int nfs_bdi_register(struct nfs_server *server)
2156{
2157 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2158}
2159
David Howells54ceac42006-08-22 20:06:13 -04002160static int nfs_get_sb(struct file_system_type *fs_type,
2161 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2162{
2163 struct nfs_server *server = NULL;
2164 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002165 struct nfs_parsed_mount_data *data;
2166 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002167 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002168 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002169 struct nfs_sb_mountdata sb_mntdata = {
2170 .mntflags = flags,
2171 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002172 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002173
Trond Myklebustc5811db2009-10-06 15:40:15 -04002174 data = nfs_alloc_parsed_mount_data(3);
Trond Myklebust33852a12008-06-19 14:20:11 -04002175 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2176 if (data == NULL || mntfh == NULL)
2177 goto out_free_fh;
2178
2179 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002180
David Howells54ceac42006-08-22 20:06:13 -04002181 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002182 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002183 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002184 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002185
Chuck Lever764302c2009-09-08 19:50:03 -04002186#ifdef CONFIG_NFS_V4
2187 if (data->version == 4) {
2188 error = nfs4_try_mount(flags, dev_name, data, mnt);
2189 kfree(data->client_address);
2190 goto out;
2191 }
2192#endif /* CONFIG_NFS_V4 */
2193
David Howells54ceac42006-08-22 20:06:13 -04002194 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002195 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002196 if (IS_ERR(server)) {
2197 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002198 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002199 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002200 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002201
Trond Myklebust75180df2007-05-16 16:53:28 -04002202 if (server->flags & NFS_MOUNT_UNSHARED)
2203 compare_super = NULL;
2204
David Howells54ceac42006-08-22 20:06:13 -04002205 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002206 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002207 if (IS_ERR(s)) {
2208 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002209 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002210 }
2211
David Howells54ceac42006-08-22 20:06:13 -04002212 if (s->s_fs_info != server) {
2213 nfs_free_server(server);
2214 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002215 } else {
2216 error = nfs_bdi_register(server);
2217 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002218 goto error_splat_bdi;
David Howellsf7b422b2006-06-09 09:34:33 -04002219 }
David Howells54ceac42006-08-22 20:06:13 -04002220
2221 if (!s->s_root) {
2222 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002223 nfs_fill_super(s, data);
David Howells2df54802009-09-23 14:36:39 -04002224 nfs_fscache_get_super_cookie(
2225 s, data ? data->fscache_uniq : NULL, NULL);
David Howells54ceac42006-08-22 20:06:13 -04002226 }
2227
Trond Myklebust33852a12008-06-19 14:20:11 -04002228 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002229 if (IS_ERR(mntroot)) {
2230 error = PTR_ERR(mntroot);
2231 goto error_splat_super;
2232 }
2233
Trond Myklebust33852a12008-06-19 14:20:11 -04002234 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002235 if (error)
2236 goto error_splat_root;
2237
David Howellsf7b422b2006-06-09 09:34:33 -04002238 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002239 mnt->mnt_sb = s;
2240 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002241 error = 0;
2242
2243out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002244 kfree(data->nfs_server.hostname);
2245 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002246 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002247 security_free_mnt_opts(&data->lsm_opts);
2248out_free_fh:
2249 kfree(mntfh);
2250 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002251 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002252
David Howells54ceac42006-08-22 20:06:13 -04002253out_err_nosb:
2254 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002255 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002256
Eric Parisf9c3a382008-03-05 14:20:18 -05002257error_splat_root:
2258 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002259error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002260 if (server && !s->s_root)
2261 bdi_unregister(&server->backing_dev_info);
2262error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002263 deactivate_locked_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002264 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002265}
2266
David Howells54ceac42006-08-22 20:06:13 -04002267/*
Trond Myklebust387c1492010-02-03 08:27:35 -05002268 * Ensure that we unregister the bdi before kill_anon_super
2269 * releases the device name
2270 */
2271static void nfs_put_super(struct super_block *s)
2272{
2273 struct nfs_server *server = NFS_SB(s);
2274
2275 bdi_unregister(&server->backing_dev_info);
2276}
2277
2278/*
David Howells54ceac42006-08-22 20:06:13 -04002279 * Destroy an NFS2/3 superblock
2280 */
David Howellsf7b422b2006-06-09 09:34:33 -04002281static void nfs_kill_super(struct super_block *s)
2282{
2283 struct nfs_server *server = NFS_SB(s);
2284
2285 kill_anon_super(s);
David Howells08734042009-04-03 16:42:42 +01002286 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002287 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002288}
2289
David Howells54ceac42006-08-22 20:06:13 -04002290/*
2291 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2292 */
2293static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2294 const char *dev_name, void *raw_data,
2295 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002296{
2297 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002298 struct super_block *s;
2299 struct nfs_server *server;
2300 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002301 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002302 struct nfs_sb_mountdata sb_mntdata = {
2303 .mntflags = flags,
2304 };
David Howells54ceac42006-08-22 20:06:13 -04002305 int error;
2306
2307 dprintk("--> nfs_xdev_get_sb()\n");
2308
2309 /* create a new volume representation */
2310 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2311 if (IS_ERR(server)) {
2312 error = PTR_ERR(server);
2313 goto out_err_noserver;
2314 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002315 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002316
Trond Myklebust75180df2007-05-16 16:53:28 -04002317 if (server->flags & NFS_MOUNT_UNSHARED)
2318 compare_super = NULL;
2319
David Howells54ceac42006-08-22 20:06:13 -04002320 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002321 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002322 if (IS_ERR(s)) {
2323 error = PTR_ERR(s);
2324 goto out_err_nosb;
2325 }
2326
2327 if (s->s_fs_info != server) {
2328 nfs_free_server(server);
2329 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002330 } else {
2331 error = nfs_bdi_register(server);
2332 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002333 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002334 }
2335
2336 if (!s->s_root) {
2337 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002338 nfs_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002339 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002340 }
2341
2342 mntroot = nfs_get_root(s, data->fh);
2343 if (IS_ERR(mntroot)) {
2344 error = PTR_ERR(mntroot);
2345 goto error_splat_super;
2346 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002347 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002348 dput(mntroot);
2349 error = -ESTALE;
2350 goto error_splat_super;
2351 }
David Howells54ceac42006-08-22 20:06:13 -04002352
2353 s->s_flags |= MS_ACTIVE;
2354 mnt->mnt_sb = s;
2355 mnt->mnt_root = mntroot;
2356
Eric Parisf9c3a382008-03-05 14:20:18 -05002357 /* clone any lsm security options from the parent to the new sb */
2358 security_sb_clone_mnt_opts(data->sb, s);
2359
David Howells54ceac42006-08-22 20:06:13 -04002360 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2361 return 0;
2362
2363out_err_nosb:
2364 nfs_free_server(server);
2365out_err_noserver:
2366 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2367 return error;
2368
2369error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002370 if (server && !s->s_root)
2371 bdi_unregister(&server->backing_dev_info);
2372error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002373 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002374 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2375 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002376}
2377
2378#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002379
2380/*
2381 * Finish setting up a cloned NFS4 superblock
2382 */
2383static void nfs4_clone_super(struct super_block *sb,
2384 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002385{
David Howells54ceac42006-08-22 20:06:13 -04002386 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2387 sb->s_blocksize = old_sb->s_blocksize;
2388 sb->s_maxbytes = old_sb->s_maxbytes;
2389 sb->s_time_gran = 1;
2390 sb->s_op = old_sb->s_op;
2391 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002392}
2393
2394/*
2395 * Set up an NFS4 superblock
2396 */
David Howells54ceac42006-08-22 20:06:13 -04002397static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002398{
David Howellsf7b422b2006-06-09 09:34:33 -04002399 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002400 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002401 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002402}
2403
Trond Myklebust01c3f052009-06-17 13:22:58 -07002404static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2405{
2406 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
2407}
2408
Chuck Lever7630c852009-09-08 19:49:57 -04002409static int nfs4_validate_text_mount_data(void *options,
2410 struct nfs_parsed_mount_data *args,
2411 const char *dev_name)
2412{
2413 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2414
Trond Myklebustf5855fe2009-10-06 15:40:37 -04002415 nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
Chuck Lever7630c852009-09-08 19:49:57 -04002416
2417 nfs_validate_transport_protocol(args);
2418
2419 nfs4_validate_mount_flags(args);
2420
Trond Myklebust2ecda722009-09-08 19:50:07 -04002421 if (args->version != 4) {
2422 dfprintk(MOUNT,
2423 "NFS4: Illegal mount version\n");
2424 return -EINVAL;
2425 }
2426
Chuck Lever7630c852009-09-08 19:49:57 -04002427 if (args->auth_flavor_len > 1) {
2428 dfprintk(MOUNT,
2429 "NFS4: Too many RPC auth flavours specified\n");
2430 return -EINVAL;
2431 }
2432
2433 if (args->client_address == NULL) {
2434 dfprintk(MOUNT,
2435 "NFS4: mount program didn't pass callback address\n");
2436 return -EINVAL;
2437 }
2438
2439 return nfs_parse_devname(dev_name,
2440 &args->nfs_server.hostname,
2441 NFS4_MAXNAMLEN,
2442 &args->nfs_server.export_path,
2443 NFS4_MAXPATHLEN);
2444}
2445
David Howells54ceac42006-08-22 20:06:13 -04002446/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002447 * Validate NFSv4 mount options
2448 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002449static int nfs4_validate_mount_data(void *options,
2450 struct nfs_parsed_mount_data *args,
2451 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002452{
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002453 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002454 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002455 char *c;
2456
2457 if (data == NULL)
2458 goto out_no_data;
2459
2460 switch (data->version) {
2461 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002462 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002463 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002464 if (data->host_addrlen == 0)
2465 goto out_no_address;
2466 args->nfs_server.addrlen = data->host_addrlen;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002467 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002468 return -EFAULT;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002469 if (!nfs_verify_server_address(sap))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002470 goto out_no_address;
2471
Chuck Lever6738b252008-06-24 16:33:54 -04002472 if (data->auth_flavourlen) {
2473 if (data->auth_flavourlen > 1)
2474 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002475 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002476 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002477 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002478 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002479 }
2480
2481 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2482 if (IS_ERR(c))
2483 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002484 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002485
2486 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2487 if (IS_ERR(c))
2488 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002489 args->nfs_server.export_path = c;
2490 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002491
2492 c = strndup_user(data->client_addr.data, 16);
2493 if (IS_ERR(c))
2494 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002495 args->client_address = c;
2496
2497 /*
2498 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2499 * can deal with.
2500 */
2501
2502 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2503 args->rsize = data->rsize;
2504 args->wsize = data->wsize;
2505 args->timeo = data->timeo;
2506 args->retrans = data->retrans;
2507 args->acregmin = data->acregmin;
2508 args->acregmax = data->acregmax;
2509 args->acdirmin = data->acdirmin;
2510 args->acdirmax = data->acdirmax;
2511 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002512 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002513
2514 break;
Chuck Lever7630c852009-09-08 19:49:57 -04002515 default:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002516 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002517 return -EINVAL;
2518
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002519 if (!nfs_verify_server_address(sap))
Chuck Lever80071222007-07-01 12:13:59 -04002520 return -EINVAL;
Chuck Levered596a82008-06-26 17:47:05 -04002521
Chuck Lever7630c852009-09-08 19:49:57 -04002522 return nfs4_validate_text_mount_data(options, args, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002523 }
2524
2525 return 0;
2526
2527out_no_data:
2528 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2529 return -EINVAL;
2530
2531out_inval_auth:
2532 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2533 data->auth_flavourlen);
2534 return -EINVAL;
2535
2536out_no_address:
2537 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2538 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002539}
2540
2541/*
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002542 * Get the superblock for the NFS4 root partition
David Howells54ceac42006-08-22 20:06:13 -04002543 */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002544static int nfs4_remote_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -04002545 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002546{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002547 struct nfs_parsed_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002548 struct super_block *s;
2549 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002550 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002551 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002552 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002553 struct nfs_sb_mountdata sb_mntdata = {
2554 .mntflags = flags,
2555 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002556 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002557
Trond Myklebust33852a12008-06-19 14:20:11 -04002558 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2559 if (data == NULL || mntfh == NULL)
2560 goto out_free_fh;
2561
2562 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002563
David Howells54ceac42006-08-22 20:06:13 -04002564 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002565 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002566 if (IS_ERR(server)) {
2567 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002568 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002569 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002570 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002571
Trond Myklebust75180df2007-05-16 16:53:28 -04002572 if (server->flags & NFS4_MOUNT_UNSHARED)
2573 compare_super = NULL;
2574
David Howells54ceac42006-08-22 20:06:13 -04002575 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002576 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002577 if (IS_ERR(s)) {
2578 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002579 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002580 }
2581
Trond Myklebust5dd31772006-08-24 01:03:05 -04002582 if (s->s_fs_info != server) {
2583 nfs_free_server(server);
2584 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002585 } else {
2586 error = nfs_bdi_register(server);
2587 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002588 goto error_splat_bdi;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002589 }
2590
David Howells54ceac42006-08-22 20:06:13 -04002591 if (!s->s_root) {
2592 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002593 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002594 nfs_fscache_get_super_cookie(
2595 s, data ? data->fscache_uniq : NULL, NULL);
Trond Myklebust816724e2006-06-24 08:41:41 -04002596 }
David Howellsf7b422b2006-06-09 09:34:33 -04002597
Trond Myklebust33852a12008-06-19 14:20:11 -04002598 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002599 if (IS_ERR(mntroot)) {
2600 error = PTR_ERR(mntroot);
2601 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002602 }
David Howells54ceac42006-08-22 20:06:13 -04002603
Trond Myklebust33852a12008-06-19 14:20:11 -04002604 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002605 if (error)
2606 goto error_splat_root;
2607
David Howellsf7b422b2006-06-09 09:34:33 -04002608 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002609 mnt->mnt_sb = s;
2610 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002611 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002612
Chuck Lever29eb9812007-07-01 12:12:30 -04002613out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002614 security_free_mnt_opts(&data->lsm_opts);
2615out_free_fh:
2616 kfree(mntfh);
Trond Myklebust816724e2006-06-24 08:41:41 -04002617 return error;
David Howells54ceac42006-08-22 20:06:13 -04002618
Chuck Lever29eb9812007-07-01 12:12:30 -04002619out_free:
2620 nfs_free_server(server);
2621 goto out;
2622
Eric Paris46c8ac72008-05-02 13:42:42 -07002623error_splat_root:
2624 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002625error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002626 if (server && !s->s_root)
2627 bdi_unregister(&server->backing_dev_info);
2628error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002629 deactivate_locked_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002630 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002631}
2632
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002633static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2634 int flags, void *data, const char *hostname)
2635{
2636 struct vfsmount *root_mnt;
2637 char *root_devname;
2638 size_t len;
2639
2640 len = strlen(hostname) + 3;
2641 root_devname = kmalloc(len, GFP_KERNEL);
2642 if (root_devname == NULL)
2643 return ERR_PTR(-ENOMEM);
2644 snprintf(root_devname, len, "%s:/", hostname);
2645 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2646 kfree(root_devname);
2647 return root_mnt;
2648}
2649
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002650static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2651{
2652 char *page = (char *) __get_free_page(GFP_KERNEL);
2653 char *devname, *tmp;
2654
2655 if (page == NULL)
2656 return;
2657 devname = nfs_path(path->mnt->mnt_devname,
2658 path->mnt->mnt_root, path->dentry,
2659 page, PAGE_SIZE);
2660 if (devname == NULL)
2661 goto out_freepage;
2662 tmp = kstrdup(devname, GFP_KERNEL);
2663 if (tmp == NULL)
2664 goto out_freepage;
2665 kfree(mnt->mnt_devname);
2666 mnt->mnt_devname = tmp;
2667out_freepage:
2668 free_page((unsigned long)page);
2669}
2670
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002671static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2672 const char *export_path, struct vfsmount *mnt_target)
2673{
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002674 struct mnt_namespace *ns_private;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002675 struct nameidata nd;
2676 struct super_block *s;
2677 int ret;
2678
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002679 ns_private = create_mnt_ns(root_mnt);
2680 ret = PTR_ERR(ns_private);
2681 if (IS_ERR(ns_private))
2682 goto out_mntput;
2683
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002684 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2685 export_path, LOOKUP_FOLLOW, &nd);
2686
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002687 put_mnt_ns(ns_private);
2688
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002689 if (ret != 0)
2690 goto out_err;
2691
2692 s = nd.path.mnt->mnt_sb;
2693 atomic_inc(&s->s_active);
2694 mnt_target->mnt_sb = s;
2695 mnt_target->mnt_root = dget(nd.path.dentry);
2696
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002697 /* Correct the device pathname */
2698 nfs_fix_devname(&nd.path, mnt_target);
2699
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002700 path_put(&nd.path);
2701 down_write(&s->s_umount);
2702 return 0;
2703out_mntput:
2704 mntput(root_mnt);
2705out_err:
2706 return ret;
2707}
2708
Chuck Levera6fe23b2009-09-08 19:50:00 -04002709static int nfs4_try_mount(int flags, const char *dev_name,
2710 struct nfs_parsed_mount_data *data,
2711 struct vfsmount *mnt)
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002712{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002713 char *export_path;
2714 struct vfsmount *root_mnt;
Chuck Levera6fe23b2009-09-08 19:50:00 -04002715 int error;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002716
Chuck Levera6fe23b2009-09-08 19:50:00 -04002717 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002718
2719 export_path = data->nfs_server.export_path;
2720 data->nfs_server.export_path = "/";
2721 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2722 data->nfs_server.hostname);
2723 data->nfs_server.export_path = export_path;
2724
2725 error = PTR_ERR(root_mnt);
2726 if (IS_ERR(root_mnt))
2727 goto out;
2728
2729 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2730
2731out:
Chuck Levera6fe23b2009-09-08 19:50:00 -04002732 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2733 error != 0 ? " [error]" : "");
2734 return error;
2735}
2736
2737/*
2738 * Get the superblock for an NFS4 mountpoint
2739 */
2740static int nfs4_get_sb(struct file_system_type *fs_type,
2741 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2742{
2743 struct nfs_parsed_mount_data *data;
2744 int error = -ENOMEM;
2745
Trond Myklebustc5811db2009-10-06 15:40:15 -04002746 data = nfs_alloc_parsed_mount_data(4);
Chuck Levera6fe23b2009-09-08 19:50:00 -04002747 if (data == NULL)
2748 goto out_free_data;
2749
2750 /* Validate the mount data */
2751 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2752 if (error < 0)
2753 goto out;
2754
2755 error = nfs4_try_mount(flags, dev_name, data, mnt);
2756
2757out:
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002758 kfree(data->client_address);
2759 kfree(data->nfs_server.export_path);
2760 kfree(data->nfs_server.hostname);
2761 kfree(data->fscache_uniq);
2762out_free_data:
2763 kfree(data);
2764 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2765 error != 0 ? " [error]" : "");
2766 return error;
2767}
2768
David Howellsf7b422b2006-06-09 09:34:33 -04002769static void nfs4_kill_super(struct super_block *sb)
2770{
2771 struct nfs_server *server = NFS_SB(sb);
2772
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002773 dprintk("--> %s\n", __func__);
Trond Myklebust515d8612008-12-23 15:21:46 -05002774 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002775 kill_anon_super(sb);
David Howells08734042009-04-03 16:42:42 +01002776 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002777 nfs_free_server(server);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002778 dprintk("<-- %s\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -04002779}
2780
2781/*
David Howells54ceac42006-08-22 20:06:13 -04002782 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002783 */
David Howells54ceac42006-08-22 20:06:13 -04002784static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2785 const char *dev_name, void *raw_data,
2786 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002787{
2788 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002789 struct super_block *s;
2790 struct nfs_server *server;
2791 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002792 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002793 struct nfs_sb_mountdata sb_mntdata = {
2794 .mntflags = flags,
2795 };
David Howells54ceac42006-08-22 20:06:13 -04002796 int error;
2797
2798 dprintk("--> nfs4_xdev_get_sb()\n");
2799
2800 /* create a new volume representation */
2801 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2802 if (IS_ERR(server)) {
2803 error = PTR_ERR(server);
2804 goto out_err_noserver;
2805 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002806 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002807
Trond Myklebust75180df2007-05-16 16:53:28 -04002808 if (server->flags & NFS4_MOUNT_UNSHARED)
2809 compare_super = NULL;
2810
David Howells54ceac42006-08-22 20:06:13 -04002811 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002812 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002813 if (IS_ERR(s)) {
2814 error = PTR_ERR(s);
2815 goto out_err_nosb;
2816 }
2817
2818 if (s->s_fs_info != server) {
2819 nfs_free_server(server);
2820 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002821 } else {
2822 error = nfs_bdi_register(server);
2823 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002824 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002825 }
2826
2827 if (!s->s_root) {
2828 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002829 nfs4_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002830 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002831 }
2832
2833 mntroot = nfs4_get_root(s, data->fh);
2834 if (IS_ERR(mntroot)) {
2835 error = PTR_ERR(mntroot);
2836 goto error_splat_super;
2837 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002838 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2839 dput(mntroot);
2840 error = -ESTALE;
2841 goto error_splat_super;
2842 }
David Howells54ceac42006-08-22 20:06:13 -04002843
2844 s->s_flags |= MS_ACTIVE;
2845 mnt->mnt_sb = s;
2846 mnt->mnt_root = mntroot;
2847
Eric Paris46c8ac72008-05-02 13:42:42 -07002848 security_sb_clone_mnt_opts(data->sb, s);
2849
David Howells54ceac42006-08-22 20:06:13 -04002850 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2851 return 0;
2852
2853out_err_nosb:
2854 nfs_free_server(server);
2855out_err_noserver:
2856 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2857 return error;
2858
2859error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002860 if (server && !s->s_root)
2861 bdi_unregister(&server->backing_dev_info);
2862error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002863 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002864 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2865 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002866}
2867
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002868static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
2869 int flags, const char *dev_name, void *raw_data,
2870 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002871{
2872 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002873 struct super_block *s;
2874 struct nfs_server *server;
2875 struct dentry *mntroot;
2876 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002877 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002878 struct nfs_sb_mountdata sb_mntdata = {
2879 .mntflags = flags,
2880 };
David Howells54ceac42006-08-22 20:06:13 -04002881 int error;
2882
2883 dprintk("--> nfs4_referral_get_sb()\n");
2884
2885 /* create a new volume representation */
2886 server = nfs4_create_referral_server(data, &mntfh);
2887 if (IS_ERR(server)) {
2888 error = PTR_ERR(server);
2889 goto out_err_noserver;
2890 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002891 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002892
Trond Myklebust75180df2007-05-16 16:53:28 -04002893 if (server->flags & NFS4_MOUNT_UNSHARED)
2894 compare_super = NULL;
2895
David Howells54ceac42006-08-22 20:06:13 -04002896 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002897 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002898 if (IS_ERR(s)) {
2899 error = PTR_ERR(s);
2900 goto out_err_nosb;
2901 }
2902
2903 if (s->s_fs_info != server) {
2904 nfs_free_server(server);
2905 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002906 } else {
2907 error = nfs_bdi_register(server);
2908 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002909 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002910 }
2911
2912 if (!s->s_root) {
2913 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002914 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002915 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002916 }
2917
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002918 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002919 if (IS_ERR(mntroot)) {
2920 error = PTR_ERR(mntroot);
2921 goto error_splat_super;
2922 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002923 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2924 dput(mntroot);
2925 error = -ESTALE;
2926 goto error_splat_super;
2927 }
David Howells54ceac42006-08-22 20:06:13 -04002928
2929 s->s_flags |= MS_ACTIVE;
2930 mnt->mnt_sb = s;
2931 mnt->mnt_root = mntroot;
2932
Eric Paris46c8ac72008-05-02 13:42:42 -07002933 security_sb_clone_mnt_opts(data->sb, s);
2934
David Howells54ceac42006-08-22 20:06:13 -04002935 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2936 return 0;
2937
2938out_err_nosb:
2939 nfs_free_server(server);
2940out_err_noserver:
2941 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2942 return error;
2943
2944error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002945 if (server && !s->s_root)
2946 bdi_unregister(&server->backing_dev_info);
2947error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002948 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002949 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2950 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002951}
2952
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002953/*
2954 * Create an NFS4 server record on referral traversal
2955 */
2956static int nfs4_referral_get_sb(struct file_system_type *fs_type,
2957 int flags, const char *dev_name, void *raw_data,
2958 struct vfsmount *mnt)
2959{
2960 struct nfs_clone_mount *data = raw_data;
2961 char *export_path;
2962 struct vfsmount *root_mnt;
2963 int error;
2964
2965 dprintk("--> nfs4_referral_get_sb()\n");
2966
2967 export_path = data->mnt_path;
2968 data->mnt_path = "/";
2969
2970 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
2971 flags, data, data->hostname);
2972 data->mnt_path = export_path;
2973
2974 error = PTR_ERR(root_mnt);
2975 if (IS_ERR(root_mnt))
2976 goto out;
2977
2978 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2979out:
2980 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
2981 error != 0 ? " [error]" : "");
2982 return error;
2983}
2984
David Howells54ceac42006-08-22 20:06:13 -04002985#endif /* CONFIG_NFS_V4 */