blob: 50ed035cabc41e04e3a418ee8c110038b0d085f1 [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,
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400103 Opt_local_lock,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400104
Chuck Leverf45663c2008-06-24 19:28:02 -0400105 /* Special mount options */
106 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400107
108 Opt_err
109};
110
Steven Whitehousea447c092008-10-13 10:46:57 +0100111static const match_table_t nfs_mount_option_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400112 { Opt_userspace, "bg" },
113 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400114 { Opt_userspace, "retry=%s" },
115
Chuck Leverf45663c2008-06-24 19:28:02 -0400116 { Opt_sloppy, "sloppy" },
117
Chuck Leverbf0fd762007-07-01 12:13:44 -0400118 { Opt_soft, "soft" },
119 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400120 { Opt_deprecated, "intr" },
121 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400122 { Opt_posix, "posix" },
123 { Opt_noposix, "noposix" },
124 { Opt_cto, "cto" },
125 { Opt_nocto, "nocto" },
126 { Opt_ac, "ac" },
127 { Opt_noac, "noac" },
128 { Opt_lock, "lock" },
129 { Opt_nolock, "nolock" },
130 { Opt_v2, "v2" },
131 { Opt_v3, "v3" },
Chuck Lever764302c2009-09-08 19:50:03 -0400132 { Opt_v4, "v4" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400133 { Opt_udp, "udp" },
134 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400135 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400136 { Opt_acl, "acl" },
137 { Opt_noacl, "noacl" },
138 { Opt_rdirplus, "rdirplus" },
139 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400140 { Opt_sharecache, "sharecache" },
141 { Opt_nosharecache, "nosharecache" },
Chuck Leverd7403512008-12-23 15:21:37 -0500142 { Opt_resvport, "resvport" },
143 { Opt_noresvport, "noresvport" },
David Howellsb797cac2009-04-03 16:42:48 +0100144 { Opt_fscache, "fsc" },
David Howellsb797cac2009-04-03 16:42:48 +0100145 { 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" },
Chuck Levera6d5ff62010-05-07 13:33:58 -0400174 { Opt_fscache_uniq, "fsc=%s" },
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400175 { Opt_local_lock, "local_lock=%s" },
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400176
Chuck Leverbf0fd762007-07-01 12:13:44 -0400177 { Opt_err, NULL }
178};
179
180enum {
Jeff Laytonee671b02009-12-03 15:58:56 -0500181 Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400182
183 Opt_xprt_err
184};
185
Steven Whitehousea447c092008-10-13 10:46:57 +0100186static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400187 { Opt_xprt_udp, "udp" },
Jeff Laytonee671b02009-12-03 15:58:56 -0500188 { Opt_xprt_udp6, "udp6" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400189 { Opt_xprt_tcp, "tcp" },
Jeff Laytonee671b02009-12-03 15:58:56 -0500190 { Opt_xprt_tcp6, "tcp6" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400191 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400192
193 { Opt_xprt_err, NULL }
194};
195
196enum {
197 Opt_sec_none, Opt_sec_sys,
198 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
199 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
200 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
201
202 Opt_sec_err
203};
204
Steven Whitehousea447c092008-10-13 10:46:57 +0100205static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400206 { Opt_sec_none, "none" },
207 { Opt_sec_none, "null" },
208 { Opt_sec_sys, "sys" },
209
210 { Opt_sec_krb5, "krb5" },
211 { Opt_sec_krb5i, "krb5i" },
212 { Opt_sec_krb5p, "krb5p" },
213
214 { Opt_sec_lkey, "lkey" },
215 { Opt_sec_lkeyi, "lkeyi" },
216 { Opt_sec_lkeyp, "lkeyp" },
217
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500218 { Opt_sec_spkm, "spkm3" },
219 { Opt_sec_spkmi, "spkm3i" },
220 { Opt_sec_spkmp, "spkm3p" },
221
Chuck Leverbf0fd762007-07-01 12:13:44 -0400222 { Opt_sec_err, NULL }
223};
224
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400225enum {
226 Opt_lookupcache_all, Opt_lookupcache_positive,
227 Opt_lookupcache_none,
228
229 Opt_lookupcache_err
230};
231
232static match_table_t nfs_lookupcache_tokens = {
233 { Opt_lookupcache_all, "all" },
234 { Opt_lookupcache_positive, "pos" },
235 { Opt_lookupcache_positive, "positive" },
236 { Opt_lookupcache_none, "none" },
237
238 { Opt_lookupcache_err, NULL }
239};
240
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400241enum {
242 Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
243 Opt_local_lock_none,
244
245 Opt_local_lock_err
246};
247
248static match_table_t nfs_local_lock_tokens = {
249 { Opt_local_lock_all, "all" },
250 { Opt_local_lock_flock, "flock" },
251 { Opt_local_lock_posix, "posix" },
252 { Opt_local_lock_none, "none" },
253
254 { Opt_local_lock_err, NULL }
255};
256
Chuck Leverbf0fd762007-07-01 12:13:44 -0400257
Al Viro42faad92008-04-24 07:21:56 -0400258static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400259static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400260static int nfs_show_options(struct seq_file *, struct vfsmount *);
261static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400262static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400263static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400264 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebust387c1492010-02-03 08:27:35 -0500265static void nfs_put_super(struct super_block *);
David Howellsf7b422b2006-06-09 09:34:33 -0400266static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400267static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400268
269static struct file_system_type nfs_fs_type = {
270 .owner = THIS_MODULE,
271 .name = "nfs",
272 .get_sb = nfs_get_sb,
273 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700274 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400275};
276
David Howells54ceac42006-08-22 20:06:13 -0400277struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400278 .owner = THIS_MODULE,
279 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400280 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400281 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700282 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400283};
284
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800285static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400286 .alloc_inode = nfs_alloc_inode,
287 .destroy_inode = nfs_destroy_inode,
288 .write_inode = nfs_write_inode,
Trond Myklebust387c1492010-02-03 08:27:35 -0500289 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400290 .statfs = nfs_statfs,
Al Virob57922d2010-06-07 14:34:48 -0400291 .evict_inode = nfs_evict_inode,
David Howellsf7b422b2006-06-09 09:34:33 -0400292 .umount_begin = nfs_umount_begin,
293 .show_options = nfs_show_options,
294 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400295 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400296};
297
298#ifdef CONFIG_NFS_V4
Chuck Lever7630c852009-09-08 19:49:57 -0400299static int nfs4_validate_text_mount_data(void *options,
300 struct nfs_parsed_mount_data *args, const char *dev_name);
Chuck Levera6fe23b2009-09-08 19:50:00 -0400301static int nfs4_try_mount(int flags, const char *dev_name,
302 struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
Trond Myklebust816724e2006-06-24 08:41:41 -0400303static int nfs4_get_sb(struct file_system_type *fs_type,
304 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400305static int nfs4_remote_get_sb(struct file_system_type *fs_type,
306 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400307static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
308 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
309static int nfs4_referral_get_sb(struct file_system_type *fs_type,
310 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400311static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
312 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400313static void nfs4_kill_super(struct super_block *sb);
314
315static struct file_system_type nfs4_fs_type = {
316 .owner = THIS_MODULE,
317 .name = "nfs4",
318 .get_sb = nfs4_get_sb,
319 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700320 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400321};
322
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400323static struct file_system_type nfs4_remote_fs_type = {
324 .owner = THIS_MODULE,
325 .name = "nfs4",
326 .get_sb = nfs4_remote_get_sb,
327 .kill_sb = nfs4_kill_super,
328 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
329};
330
David Howells54ceac42006-08-22 20:06:13 -0400331struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400332 .owner = THIS_MODULE,
333 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400334 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400335 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700336 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400337};
338
Trond Myklebustc02d7ad2009-06-22 15:09:14 -0400339static struct file_system_type nfs4_remote_referral_fs_type = {
340 .owner = THIS_MODULE,
341 .name = "nfs4",
342 .get_sb = nfs4_remote_referral_get_sb,
343 .kill_sb = nfs4_kill_super,
344 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
345};
346
David Howells54ceac42006-08-22 20:06:13 -0400347struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400348 .owner = THIS_MODULE,
349 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400350 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400351 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700352 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400353};
354
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800355static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400356 .alloc_inode = nfs_alloc_inode,
357 .destroy_inode = nfs_destroy_inode,
358 .write_inode = nfs_write_inode,
Trond Myklebust387c1492010-02-03 08:27:35 -0500359 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400360 .statfs = nfs_statfs,
Al Virob57922d2010-06-07 14:34:48 -0400361 .evict_inode = nfs4_evict_inode,
David Howellsf7b422b2006-06-09 09:34:33 -0400362 .umount_begin = nfs_umount_begin,
363 .show_options = nfs_show_options,
364 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400365 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400366};
367#endif
368
Rusty Russell8e1f9362007-07-17 04:03:17 -0700369static struct shrinker acl_shrinker = {
370 .shrink = nfs_access_cache_shrinker,
371 .seeks = DEFAULT_SEEKS,
372};
Trond Myklebust979df722006-07-25 11:28:19 -0400373
David Howellsf7b422b2006-06-09 09:34:33 -0400374/*
375 * Register the NFS filesystems
376 */
377int __init register_nfs_fs(void)
378{
379 int ret;
380
381 ret = register_filesystem(&nfs_fs_type);
382 if (ret < 0)
383 goto error_0;
384
David Howellsf7b422b2006-06-09 09:34:33 -0400385 ret = nfs_register_sysctl();
386 if (ret < 0)
387 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800388#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400389 ret = register_filesystem(&nfs4_fs_type);
390 if (ret < 0)
391 goto error_2;
392#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700393 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400394 return 0;
395
396#ifdef CONFIG_NFS_V4
397error_2:
398 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800399#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400400error_1:
401 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400402error_0:
403 return ret;
404}
405
406/*
407 * Unregister the NFS filesystems
408 */
409void __exit unregister_nfs_fs(void)
410{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700411 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400412#ifdef CONFIG_NFS_V4
413 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400414#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700415 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400416 unregister_filesystem(&nfs_fs_type);
417}
418
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400419void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500420{
421 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400422
423 if (atomic_inc_return(&server->active) == 1)
424 atomic_inc(&sb->s_active);
425}
426
427void nfs_sb_deactive(struct super_block *sb)
428{
429 struct nfs_server *server = NFS_SB(sb);
430
431 if (atomic_dec_and_test(&server->active))
432 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500433}
434
David Howellsf7b422b2006-06-09 09:34:33 -0400435/*
436 * Deliver file system statistics to userspace
437 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400438static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400439{
David Howells0c7d90c2006-08-22 20:06:10 -0400440 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400441 unsigned char blockbits;
442 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400443 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
Trond Myklebustca7e9a02010-04-16 16:22:52 -0400444 struct nfs_fsstat res;
445 int error = -ENOMEM;
446
447 res.fattr = nfs_alloc_fattr();
448 if (res.fattr == NULL)
449 goto out_err;
David Howellsf7b422b2006-06-09 09:34:33 -0400450
David Howells8fa5c002006-08-22 20:06:12 -0400451 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
Trond Myklebustca7e9a02010-04-16 16:22:52 -0400452
453 nfs_free_fattr(res.fattr);
David Howellsf7b422b2006-06-09 09:34:33 -0400454 if (error < 0)
455 goto out_err;
Trond Myklebustca7e9a02010-04-16 16:22:52 -0400456
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700457 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400458
459 /*
460 * Current versions of glibc do not correctly handle the
461 * case where f_frsize != f_bsize. Eventually we want to
462 * report the value of wtmult in this field.
463 */
David Howells0c7d90c2006-08-22 20:06:10 -0400464 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400465
466 /*
467 * On most *nix systems, f_blocks, f_bfree, and f_bavail
468 * are reported in units of f_frsize. Linux hasn't had
469 * an f_frsize field in its statfs struct until recently,
470 * thus historically Linux's sys_statfs reports these
471 * fields in units of f_bsize.
472 */
David Howells0c7d90c2006-08-22 20:06:10 -0400473 buf->f_bsize = dentry->d_sb->s_blocksize;
474 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400475 blockres = (1 << blockbits) - 1;
476 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
477 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
478 buf->f_bavail = (res.abytes + blockres) >> blockbits;
479
480 buf->f_files = res.tfiles;
481 buf->f_ffree = res.afiles;
482
483 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700484
David Howellsf7b422b2006-06-09 09:34:33 -0400485 return 0;
486
487 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700488 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700489 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400490}
491
David Howells7d4e2742006-08-22 20:06:07 -0400492/*
493 * Map the security flavour number to a name
494 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400495static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
496{
David Howells7d4e2742006-08-22 20:06:07 -0400497 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400498 rpc_authflavor_t flavour;
499 const char *str;
500 } sec_flavours[] = {
501 { RPC_AUTH_NULL, "null" },
502 { RPC_AUTH_UNIX, "sys" },
503 { RPC_AUTH_GSS_KRB5, "krb5" },
504 { RPC_AUTH_GSS_KRB5I, "krb5i" },
505 { RPC_AUTH_GSS_KRB5P, "krb5p" },
506 { RPC_AUTH_GSS_LKEY, "lkey" },
507 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
508 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
509 { RPC_AUTH_GSS_SPKM, "spkm" },
510 { RPC_AUTH_GSS_SPKMI, "spkmi" },
511 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400512 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400513 };
514 int i;
515
Chuck Lever4d81cd12007-07-01 12:12:40 -0400516 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400517 if (sec_flavours[i].flavour == flavour)
518 break;
519 }
520 return sec_flavours[i].str;
521}
522
Jeff Laytonee671b02009-12-03 15:58:56 -0500523static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
524 int showdefaults)
525{
526 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
527
528 seq_printf(m, ",mountproto=");
529 switch (sap->sa_family) {
530 case AF_INET:
531 switch (nfss->mountd_protocol) {
532 case IPPROTO_UDP:
533 seq_printf(m, RPCBIND_NETID_UDP);
534 break;
535 case IPPROTO_TCP:
536 seq_printf(m, RPCBIND_NETID_TCP);
537 break;
538 default:
539 if (showdefaults)
540 seq_printf(m, "auto");
541 }
542 break;
543 case AF_INET6:
544 switch (nfss->mountd_protocol) {
545 case IPPROTO_UDP:
546 seq_printf(m, RPCBIND_NETID_UDP6);
547 break;
548 case IPPROTO_TCP:
549 seq_printf(m, RPCBIND_NETID_TCP6);
550 break;
551 default:
552 if (showdefaults)
553 seq_printf(m, "auto");
554 }
555 break;
556 default:
557 if (showdefaults)
558 seq_printf(m, "auto");
559 }
560}
561
Chuck Lever82d101d2008-03-14 14:10:37 -0400562static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
563 int showdefaults)
564{
565 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
566
Bryan Schumakerd5eff1a2010-08-03 13:04:00 -0400567 if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
568 return;
569
Chuck Lever82d101d2008-03-14 14:10:37 -0400570 switch (sap->sa_family) {
571 case AF_INET: {
572 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrisonbe859402008-10-31 00:56:28 -0700573 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400574 break;
575 }
576 case AF_INET6: {
577 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Chuck Leverd250e192009-12-03 15:58:56 -0500578 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400579 break;
580 }
581 default:
582 if (showdefaults)
583 seq_printf(m, ",mountaddr=unspecified");
584 }
585
586 if (nfss->mountd_version || showdefaults)
587 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
588 if (nfss->mountd_port || showdefaults)
589 seq_printf(m, ",mountport=%u", nfss->mountd_port);
590
Jeff Laytonee671b02009-12-03 15:58:56 -0500591 nfs_show_mountd_netid(m, nfss, showdefaults);
Chuck Lever82d101d2008-03-14 14:10:37 -0400592}
593
Trond Myklebust0be81892010-06-18 12:23:58 -0400594#ifdef CONFIG_NFS_V4
595static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
596 int showdefaults)
597{
598 struct nfs_client *clp = nfss->nfs_client;
599
600 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
601 seq_printf(m, ",minorversion=%u", clp->cl_minorversion);
602}
603#else
604static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
605 int showdefaults)
606{
607}
608#endif
609
David Howellsf7b422b2006-06-09 09:34:33 -0400610/*
611 * Describe the mount options in force on this server representation
612 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400613static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
614 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400615{
David Howells509de812006-08-22 20:06:11 -0400616 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400617 int flag;
David Howells509de812006-08-22 20:06:11 -0400618 const char *str;
619 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400620 } nfs_info[] = {
621 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400622 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400623 { NFS_MOUNT_NOCTO, ",nocto", "" },
624 { NFS_MOUNT_NOAC, ",noac", "" },
625 { NFS_MOUNT_NONLM, ",nolock", "" },
626 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400627 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500628 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
629 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400630 { 0, NULL, NULL }
631 };
David Howells509de812006-08-22 20:06:11 -0400632 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400633 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400634 u32 version = clp->rpc_ops->version;
Suresh Jayaraman7c563cc2010-09-23 14:26:48 -0400635 int local_flock, local_fcntl;
David Howellsf7b422b2006-06-09 09:34:33 -0400636
Chuck Lever82d101d2008-03-14 14:10:37 -0400637 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400638 seq_printf(m, ",rsize=%u", nfss->rsize);
639 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400640 if (nfss->bsize != 0)
641 seq_printf(m, ",bsize=%u", nfss->bsize);
642 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400643 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400644 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400645 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400646 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400647 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400648 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400649 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400650 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400651 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
652 if (nfss->flags & nfs_infop->flag)
653 seq_puts(m, nfs_infop->str);
654 else
655 seq_puts(m, nfs_infop->nostr);
656 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400657 seq_printf(m, ",proto=%s",
Jeff Laytonee671b02009-12-03 15:58:56 -0500658 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
Chuck Lever82d101d2008-03-14 14:10:37 -0400659 if (version == 4) {
660 if (nfss->port != NFS_PORT)
661 seq_printf(m, ",port=%u", nfss->port);
662 } else
663 if (nfss->port)
664 seq_printf(m, ",port=%u", nfss->port);
665
Trond Myklebust33170232007-12-20 16:03:59 -0500666 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
667 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400668 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400669
670 if (version != 4)
671 nfs_show_mountd_options(m, nfss, showdefaults);
Trond Myklebust0be81892010-06-18 12:23:58 -0400672 else
673 nfs_show_nfsv4_options(m, nfss, showdefaults);
Chuck Lever82d101d2008-03-14 14:10:37 -0400674
David Howellsb797cac2009-04-03 16:42:48 +0100675 if (nfss->options & NFS_OPTION_FSCACHE)
676 seq_printf(m, ",fsc");
Patrick J. LoPresti9b00c642010-08-10 17:28:01 -0400677
678 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
679 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
680 seq_printf(m, ",lookupcache=none");
681 else
682 seq_printf(m, ",lookupcache=pos");
683 }
Suresh Jayaraman7c563cc2010-09-23 14:26:48 -0400684
685 local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
686 local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
687
688 if (!local_flock && !local_fcntl)
689 seq_printf(m, ",local_lock=none");
690 else if (local_flock && local_fcntl)
691 seq_printf(m, ",local_lock=all");
692 else if (local_flock)
693 seq_printf(m, ",local_lock=flock");
694 else
695 seq_printf(m, ",local_lock=posix");
David Howellsf7b422b2006-06-09 09:34:33 -0400696}
697
698/*
699 * Describe the mount options on this VFS mountpoint
700 */
701static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
702{
703 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
704
705 nfs_show_mount_options(m, nfss, 0);
706
Chuck Lever5d8515c2007-12-10 14:57:16 -0500707 seq_printf(m, ",addr=%s",
708 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
709 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400710
711 return 0;
712}
713
714/*
715 * Present statistical information for this VFS mountpoint
716 */
717static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
718{
719 int i, cpu;
720 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
721 struct rpc_auth *auth = nfss->client->cl_auth;
722 struct nfs_iostats totals = { };
723
724 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
725
726 /*
727 * Display all mount option settings
728 */
729 seq_printf(m, "\n\topts:\t");
730 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
731 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
732 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
733 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
734 nfs_show_mount_options(m, nfss, 1);
735
736 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
737
738 seq_printf(m, "\n\tcaps:\t");
739 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400740 seq_printf(m, ",wtmult=%u", nfss->wtmult);
741 seq_printf(m, ",dtsize=%u", nfss->dtsize);
742 seq_printf(m, ",bsize=%u", nfss->bsize);
743 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400744
745#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500746 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400747 seq_printf(m, "\n\tnfsv4:\t");
748 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
749 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
750 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
751 }
752#endif
753
754 /*
755 * Display security flavor in effect for this mount
756 */
Chuck Lever2d767432008-03-14 14:10:08 -0400757 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400758 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400759 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400760
761 /*
762 * Display superblock I/O counters
763 */
764 for_each_possible_cpu(cpu) {
765 struct nfs_iostats *stats;
766
767 preempt_disable();
768 stats = per_cpu_ptr(nfss->io_stats, cpu);
769
770 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
771 totals.events[i] += stats->events[i];
772 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
773 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100774#ifdef CONFIG_NFS_FSCACHE
775 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
776 totals.fscache[i] += stats->fscache[i];
777#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400778
779 preempt_enable();
780 }
781
782 seq_printf(m, "\n\tevents:\t");
783 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
784 seq_printf(m, "%lu ", totals.events[i]);
785 seq_printf(m, "\n\tbytes:\t");
786 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
787 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100788#ifdef CONFIG_NFS_FSCACHE
789 if (nfss->options & NFS_OPTION_FSCACHE) {
790 seq_printf(m, "\n\tfsc:\t");
791 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
792 seq_printf(m, "%Lu ", totals.bytes[i]);
793 }
794#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400795 seq_printf(m, "\n");
796
797 rpc_print_iostats(m, nfss->client);
798
799 return 0;
800}
801
802/*
803 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400804 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400805 */
Al Viro42faad92008-04-24 07:21:56 -0400806static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400807{
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200808 struct nfs_server *server;
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400809 struct rpc_clnt *rpc;
810
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200811 server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400812 /* -EIO all pending I/O */
813 rpc = server->client_acl;
814 if (!IS_ERR(rpc))
815 rpc_killall_tasks(rpc);
816 rpc = server->client;
817 if (!IS_ERR(rpc))
818 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400819}
820
Trond Myklebustc5811db2009-10-06 15:40:15 -0400821static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
Chuck Lever9423a082009-09-23 14:36:38 -0400822{
823 struct nfs_parsed_mount_data *data;
824
825 data = kzalloc(sizeof(*data), GFP_KERNEL);
826 if (data) {
Chuck Lever9423a082009-09-23 14:36:38 -0400827 data->acregmin = NFS_DEF_ACREGMIN;
828 data->acregmax = NFS_DEF_ACREGMAX;
829 data->acdirmin = NFS_DEF_ACDIRMIN;
830 data->acdirmax = NFS_DEF_ACDIRMAX;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400831 data->mount_server.port = NFS_UNSPEC_PORT;
Chuck Lever9423a082009-09-23 14:36:38 -0400832 data->nfs_server.port = NFS_UNSPEC_PORT;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400833 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Lever9423a082009-09-23 14:36:38 -0400834 data->auth_flavors[0] = RPC_AUTH_UNIX;
835 data->auth_flavor_len = 1;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400836 data->version = version;
Chuck Lever9423a082009-09-23 14:36:38 -0400837 data->minorversion = 0;
838 }
839 return data;
840}
841
David Howellsf7b422b2006-06-09 09:34:33 -0400842/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500843 * Sanity-check a server address provided by the mount command.
844 *
845 * Address family must be initialized, and address must not be
846 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400847 */
848static int nfs_verify_server_address(struct sockaddr *addr)
849{
850 switch (addr->sa_family) {
851 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500852 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700853 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500854 }
855 case AF_INET6: {
856 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
857 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400858 }
859 }
860
Chuck Lever53a0b9c2009-08-09 15:09:36 -0400861 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
Chuck Leverfc50d582007-07-01 12:12:46 -0400862 return 0;
863}
864
Chuck Lever9412b922007-12-10 14:59:21 -0500865/*
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400866 * Select between a default port value and a user-specified port value.
867 * If a zero value is set, then autobind will be used.
868 */
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400869static void nfs_set_port(struct sockaddr *sap, int *port,
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400870 const unsigned short default_port)
871{
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400872 if (*port == NFS_UNSPEC_PORT)
873 *port = default_port;
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400874
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400875 rpc_set_port(sap, *port);
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400876}
877
878/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400879 * Sanity check the NFS transport protocol.
880 *
881 */
882static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
883{
884 switch (mnt->nfs_server.protocol) {
885 case XPRT_TRANSPORT_UDP:
886 case XPRT_TRANSPORT_TCP:
887 case XPRT_TRANSPORT_RDMA:
888 break;
889 default:
890 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
891 }
892}
893
894/*
895 * For text based NFSv2/v3 mounts, the mount protocol transport default
896 * settings should depend upon the specified NFS transport.
897 */
898static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
899{
900 nfs_validate_transport_protocol(mnt);
901
902 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
903 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
904 return;
905 switch (mnt->nfs_server.protocol) {
906 case XPRT_TRANSPORT_UDP:
907 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
908 break;
909 case XPRT_TRANSPORT_TCP:
910 case XPRT_TRANSPORT_RDMA:
911 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
912 }
913}
914
915/*
Chuck Lever01060c82008-06-24 16:33:38 -0400916 * Parse the value of the 'sec=' option.
Chuck Lever01060c82008-06-24 16:33:38 -0400917 */
918static int nfs_parse_security_flavors(char *value,
919 struct nfs_parsed_mount_data *mnt)
920{
921 substring_t args[MAX_OPT_ARGS];
922
923 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
924
925 switch (match_token(value, nfs_secflavor_tokens, args)) {
926 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400927 mnt->auth_flavors[0] = RPC_AUTH_NULL;
928 break;
929 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400930 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
931 break;
932 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400933 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
934 break;
935 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400936 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
937 break;
938 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400939 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
940 break;
941 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400942 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
943 break;
944 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400945 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
946 break;
947 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400948 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
949 break;
950 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400951 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
952 break;
953 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400954 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
955 break;
956 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400957 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
958 break;
959 default:
960 return 0;
961 }
962
Chuck Lever059f90b2009-08-09 15:09:31 -0400963 mnt->auth_flavor_len = 1;
Chuck Lever01060c82008-06-24 16:33:38 -0400964 return 1;
965}
966
967/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400968 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400969 * a data structure. The whole mount string is processed; bad options are
970 * skipped as they are encountered. If there were no errors, return 1;
971 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400972 */
973static int nfs_parse_mount_options(char *raw,
974 struct nfs_parsed_mount_data *mnt)
975{
Eric Parisf9c3a382008-03-05 14:20:18 -0500976 char *p, *string, *secdata;
Chuck Leverd23c45f2009-06-17 18:02:13 -0700977 int rc, sloppy = 0, invalid_option = 0;
Jeff Laytonee671b02009-12-03 15:58:56 -0500978 unsigned short protofamily = AF_UNSPEC;
979 unsigned short mountfamily = AF_UNSPEC;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400980
981 if (!raw) {
982 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
983 return 1;
984 }
985 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
986
Eric Parisf9c3a382008-03-05 14:20:18 -0500987 secdata = alloc_secdata();
988 if (!secdata)
989 goto out_nomem;
990
991 rc = security_sb_copy_data(raw, secdata);
992 if (rc)
993 goto out_security_failure;
994
995 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
996 if (rc)
997 goto out_security_failure;
998
999 free_secdata(secdata);
1000
Chuck Leverbf0fd762007-07-01 12:13:44 -04001001 while ((p = strsep(&raw, ",")) != NULL) {
1002 substring_t args[MAX_OPT_ARGS];
Chuck Levera5a16ba2009-06-17 18:02:14 -07001003 unsigned long option;
1004 int token;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001005
1006 if (!*p)
1007 continue;
1008
1009 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
1010
1011 token = match_token(p, nfs_mount_option_tokens, args);
1012 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001013
1014 /*
1015 * boolean options: foo/nofoo
1016 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001017 case Opt_soft:
1018 mnt->flags |= NFS_MOUNT_SOFT;
1019 break;
1020 case Opt_hard:
1021 mnt->flags &= ~NFS_MOUNT_SOFT;
1022 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001023 case Opt_posix:
1024 mnt->flags |= NFS_MOUNT_POSIX;
1025 break;
1026 case Opt_noposix:
1027 mnt->flags &= ~NFS_MOUNT_POSIX;
1028 break;
1029 case Opt_cto:
1030 mnt->flags &= ~NFS_MOUNT_NOCTO;
1031 break;
1032 case Opt_nocto:
1033 mnt->flags |= NFS_MOUNT_NOCTO;
1034 break;
1035 case Opt_ac:
1036 mnt->flags &= ~NFS_MOUNT_NOAC;
1037 break;
1038 case Opt_noac:
1039 mnt->flags |= NFS_MOUNT_NOAC;
1040 break;
1041 case Opt_lock:
1042 mnt->flags &= ~NFS_MOUNT_NONLM;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001043 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1044 NFS_MOUNT_LOCAL_FCNTL);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001045 break;
1046 case Opt_nolock:
1047 mnt->flags |= NFS_MOUNT_NONLM;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001048 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1049 NFS_MOUNT_LOCAL_FCNTL);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001050 break;
1051 case Opt_v2:
1052 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001053 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001054 break;
1055 case Opt_v3:
1056 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001057 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001058 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001059#ifdef CONFIG_NFS_V4
1060 case Opt_v4:
1061 mnt->flags &= ~NFS_MOUNT_VER3;
1062 mnt->version = 4;
1063 break;
1064#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001065 case Opt_udp:
1066 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001067 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001068 break;
1069 case Opt_tcp:
1070 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001071 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001072 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001073 case Opt_rdma:
1074 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1075 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001076 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001077 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001078 case Opt_acl:
1079 mnt->flags &= ~NFS_MOUNT_NOACL;
1080 break;
1081 case Opt_noacl:
1082 mnt->flags |= NFS_MOUNT_NOACL;
1083 break;
1084 case Opt_rdirplus:
1085 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1086 break;
1087 case Opt_nordirplus:
1088 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1089 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001090 case Opt_sharecache:
1091 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1092 break;
1093 case Opt_nosharecache:
1094 mnt->flags |= NFS_MOUNT_UNSHARED;
1095 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001096 case Opt_resvport:
1097 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1098 break;
1099 case Opt_noresvport:
1100 mnt->flags |= NFS_MOUNT_NORESVPORT;
1101 break;
David Howellsb797cac2009-04-03 16:42:48 +01001102 case Opt_fscache:
1103 mnt->options |= NFS_OPTION_FSCACHE;
1104 kfree(mnt->fscache_uniq);
1105 mnt->fscache_uniq = NULL;
1106 break;
1107 case Opt_nofscache:
1108 mnt->options &= ~NFS_OPTION_FSCACHE;
1109 kfree(mnt->fscache_uniq);
1110 mnt->fscache_uniq = NULL;
1111 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001112
Chuck Leverf45663c2008-06-24 19:28:02 -04001113 /*
1114 * options that take numeric values
1115 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001116 case Opt_port:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001117 string = match_strdup(args);
1118 if (string == NULL)
1119 goto out_nomem;
1120 rc = strict_strtoul(string, 10, &option);
1121 kfree(string);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001122 if (rc != 0 || option > USHRT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001123 goto out_invalid_value;
1124 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001125 break;
1126 case Opt_rsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001127 string = match_strdup(args);
1128 if (string == NULL)
1129 goto out_nomem;
1130 rc = strict_strtoul(string, 10, &option);
1131 kfree(string);
1132 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001133 goto out_invalid_value;
1134 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001135 break;
1136 case Opt_wsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001137 string = match_strdup(args);
1138 if (string == NULL)
1139 goto out_nomem;
1140 rc = strict_strtoul(string, 10, &option);
1141 kfree(string);
1142 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001143 goto out_invalid_value;
1144 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001145 break;
1146 case Opt_bsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001147 string = match_strdup(args);
1148 if (string == NULL)
1149 goto out_nomem;
1150 rc = strict_strtoul(string, 10, &option);
1151 kfree(string);
1152 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001153 goto out_invalid_value;
1154 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001155 break;
1156 case Opt_timeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001157 string = match_strdup(args);
1158 if (string == NULL)
1159 goto out_nomem;
1160 rc = strict_strtoul(string, 10, &option);
1161 kfree(string);
1162 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001163 goto out_invalid_value;
1164 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001165 break;
1166 case Opt_retrans:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001167 string = match_strdup(args);
1168 if (string == NULL)
1169 goto out_nomem;
1170 rc = strict_strtoul(string, 10, &option);
1171 kfree(string);
1172 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001173 goto out_invalid_value;
1174 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001175 break;
1176 case Opt_acregmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001177 string = match_strdup(args);
1178 if (string == NULL)
1179 goto out_nomem;
1180 rc = strict_strtoul(string, 10, &option);
1181 kfree(string);
1182 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001183 goto out_invalid_value;
1184 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001185 break;
1186 case Opt_acregmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001187 string = match_strdup(args);
1188 if (string == NULL)
1189 goto out_nomem;
1190 rc = strict_strtoul(string, 10, &option);
1191 kfree(string);
1192 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001193 goto out_invalid_value;
1194 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001195 break;
1196 case Opt_acdirmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001197 string = match_strdup(args);
1198 if (string == NULL)
1199 goto out_nomem;
1200 rc = strict_strtoul(string, 10, &option);
1201 kfree(string);
1202 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001203 goto out_invalid_value;
1204 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001205 break;
1206 case Opt_acdirmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001207 string = match_strdup(args);
1208 if (string == NULL)
1209 goto out_nomem;
1210 rc = strict_strtoul(string, 10, &option);
1211 kfree(string);
1212 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001213 goto out_invalid_value;
1214 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001215 break;
1216 case Opt_actimeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001217 string = match_strdup(args);
1218 if (string == NULL)
1219 goto out_nomem;
1220 rc = strict_strtoul(string, 10, &option);
1221 kfree(string);
1222 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001223 goto out_invalid_value;
1224 mnt->acregmin = mnt->acregmax =
1225 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001226 break;
1227 case Opt_namelen:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001228 string = match_strdup(args);
1229 if (string == NULL)
1230 goto out_nomem;
1231 rc = strict_strtoul(string, 10, &option);
1232 kfree(string);
1233 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001234 goto out_invalid_value;
1235 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001236 break;
1237 case Opt_mountport:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001238 string = match_strdup(args);
1239 if (string == NULL)
1240 goto out_nomem;
1241 rc = strict_strtoul(string, 10, &option);
1242 kfree(string);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001243 if (rc != 0 || option > USHRT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001244 goto out_invalid_value;
1245 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001246 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001247 case Opt_mountvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001248 string = match_strdup(args);
1249 if (string == NULL)
1250 goto out_nomem;
1251 rc = strict_strtoul(string, 10, &option);
1252 kfree(string);
1253 if (rc != 0 ||
Chuck Leverf45663c2008-06-24 19:28:02 -04001254 option < NFS_MNT_VERSION ||
Chuck Leverd23c45f2009-06-17 18:02:13 -07001255 option > NFS_MNT3_VERSION)
1256 goto out_invalid_value;
1257 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001258 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001259 case Opt_nfsvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001260 string = match_strdup(args);
1261 if (string == NULL)
1262 goto out_nomem;
1263 rc = strict_strtoul(string, 10, &option);
1264 kfree(string);
1265 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001266 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001267 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001268 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001269 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001270 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001271 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001272 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001273 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001274 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001275 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001276#ifdef CONFIG_NFS_V4
1277 case NFS4_VERSION:
1278 mnt->flags &= ~NFS_MOUNT_VER3;
1279 mnt->version = 4;
1280 break;
1281#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001282 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001283 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001284 }
1285 break;
Mike Sager3fd5be92009-04-01 09:21:48 -04001286 case Opt_minorversion:
Chuck Leverf3f4f4e2009-08-09 15:09:29 -04001287 string = match_strdup(args);
1288 if (string == NULL)
1289 goto out_nomem;
1290 rc = strict_strtoul(string, 10, &option);
1291 kfree(string);
1292 if (rc != 0)
1293 goto out_invalid_value;
1294 if (option > NFS4_MAX_MINOR_VERSION)
1295 goto out_invalid_value;
1296 mnt->minorversion = option;
Mike Sager3fd5be92009-04-01 09:21:48 -04001297 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001298
Chuck Leverf45663c2008-06-24 19:28:02 -04001299 /*
1300 * options that take text values
1301 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001302 case Opt_sec:
1303 string = match_strdup(args);
1304 if (string == NULL)
1305 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001306 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001307 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001308 if (!rc) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001309 dfprintk(MOUNT, "NFS: unrecognized "
1310 "security flavor\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001311 return 0;
Chuck Leverf45663c2008-06-24 19:28:02 -04001312 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001313 break;
1314 case Opt_proto:
1315 string = match_strdup(args);
1316 if (string == NULL)
1317 goto out_nomem;
1318 token = match_token(string,
1319 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001320
Jeff Laytonee671b02009-12-03 15:58:56 -05001321 protofamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001322 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001323 case Opt_xprt_udp6:
1324 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001325 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001326 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001327 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001328 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001329 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001330 case Opt_xprt_tcp6:
1331 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001332 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001333 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001334 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001335 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001336 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001337 case Opt_xprt_rdma:
1338 /* vector side protocols to TCP */
1339 mnt->flags |= NFS_MOUNT_TCP;
1340 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001341 xprt_load_transport(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001342 kfree(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001343 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001344 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001345 dfprintk(MOUNT, "NFS: unrecognized "
1346 "transport protocol\n");
Yinghai Lu4223a4a2009-10-20 14:13:46 +09001347 kfree(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001348 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001349 }
1350 break;
1351 case Opt_mountproto:
1352 string = match_strdup(args);
1353 if (string == NULL)
1354 goto out_nomem;
1355 token = match_token(string,
1356 nfs_xprt_protocol_tokens, args);
Trond Myklebustd508afb2009-04-06 16:41:35 -07001357 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001358
Jeff Laytonee671b02009-12-03 15:58:56 -05001359 mountfamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001360 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001361 case Opt_xprt_udp6:
1362 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001363 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001364 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001365 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001366 case Opt_xprt_tcp6:
1367 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001368 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001369 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001370 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001371 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001372 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001373 dfprintk(MOUNT, "NFS: unrecognized "
1374 "transport protocol\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001375 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001376 }
1377 break;
1378 case Opt_addr:
1379 string = match_strdup(args);
1380 if (string == NULL)
1381 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001382 mnt->nfs_server.addrlen =
1383 rpc_pton(string, strlen(string),
1384 (struct sockaddr *)
1385 &mnt->nfs_server.address,
1386 sizeof(mnt->nfs_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001387 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001388 if (mnt->nfs_server.addrlen == 0)
1389 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001390 break;
1391 case Opt_clientaddr:
1392 string = match_strdup(args);
1393 if (string == NULL)
1394 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001395 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001396 mnt->client_address = string;
1397 break;
Chuck Lever33832032007-12-10 14:59:13 -05001398 case Opt_mounthost:
1399 string = match_strdup(args);
1400 if (string == NULL)
1401 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001402 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001403 mnt->mount_server.hostname = string;
1404 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001405 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001406 string = match_strdup(args);
1407 if (string == NULL)
1408 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001409 mnt->mount_server.addrlen =
1410 rpc_pton(string, strlen(string),
1411 (struct sockaddr *)
1412 &mnt->mount_server.address,
1413 sizeof(mnt->mount_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001414 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001415 if (mnt->mount_server.addrlen == 0)
1416 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001417 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001418 case Opt_lookupcache:
1419 string = match_strdup(args);
1420 if (string == NULL)
1421 goto out_nomem;
1422 token = match_token(string,
1423 nfs_lookupcache_tokens, args);
1424 kfree(string);
1425 switch (token) {
1426 case Opt_lookupcache_all:
1427 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1428 break;
1429 case Opt_lookupcache_positive:
1430 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1431 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1432 break;
1433 case Opt_lookupcache_none:
1434 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1435 break;
1436 default:
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001437 dfprintk(MOUNT, "NFS: invalid "
1438 "lookupcache argument\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001439 return 0;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001440 };
1441 break;
Chuck Levera6d5ff62010-05-07 13:33:58 -04001442 case Opt_fscache_uniq:
1443 string = match_strdup(args);
1444 if (string == NULL)
1445 goto out_nomem;
1446 kfree(mnt->fscache_uniq);
1447 mnt->fscache_uniq = string;
1448 mnt->options |= NFS_OPTION_FSCACHE;
1449 break;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001450 case Opt_local_lock:
1451 string = match_strdup(args);
1452 if (string == NULL)
1453 goto out_nomem;
1454 token = match_token(string, nfs_local_lock_tokens,
1455 args);
1456 kfree(string);
1457 switch (token) {
1458 case Opt_local_lock_all:
1459 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1460 NFS_MOUNT_LOCAL_FCNTL);
1461 break;
1462 case Opt_local_lock_flock:
1463 mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1464 break;
1465 case Opt_local_lock_posix:
1466 mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1467 break;
1468 case Opt_local_lock_none:
1469 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1470 NFS_MOUNT_LOCAL_FCNTL);
1471 break;
1472 default:
1473 dfprintk(MOUNT, "NFS: invalid "
1474 "local_lock argument\n");
1475 return 0;
1476 };
1477 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001478
Chuck Leverf45663c2008-06-24 19:28:02 -04001479 /*
1480 * Special options
1481 */
1482 case Opt_sloppy:
1483 sloppy = 1;
1484 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1485 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001486 case Opt_userspace:
1487 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001488 dfprintk(MOUNT, "NFS: ignoring mount option "
1489 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001490 break;
1491
1492 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001493 invalid_option = 1;
Chuck Leverf45663c2008-06-24 19:28:02 -04001494 dfprintk(MOUNT, "NFS: unrecognized mount option "
1495 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001496 }
1497 }
1498
Chuck Leverd23c45f2009-06-17 18:02:13 -07001499 if (!sloppy && invalid_option)
1500 return 0;
1501
Jeff Laytonee671b02009-12-03 15:58:56 -05001502 /*
1503 * verify that any proto=/mountproto= options match the address
1504 * familiies in the addr=/mountaddr= options.
1505 */
1506 if (protofamily != AF_UNSPEC &&
1507 protofamily != mnt->nfs_server.address.ss_family)
1508 goto out_proto_mismatch;
1509
1510 if (mountfamily != AF_UNSPEC) {
1511 if (mnt->mount_server.addrlen) {
1512 if (mountfamily != mnt->mount_server.address.ss_family)
1513 goto out_mountproto_mismatch;
1514 } else {
1515 if (mountfamily != mnt->nfs_server.address.ss_family)
1516 goto out_mountproto_mismatch;
1517 }
1518 }
1519
Chuck Leverbf0fd762007-07-01 12:13:44 -04001520 return 1;
1521
Jeff Laytonee671b02009-12-03 15:58:56 -05001522out_mountproto_mismatch:
1523 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1524 "option\n");
1525 return 0;
1526out_proto_mismatch:
1527 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1528 return 0;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001529out_invalid_address:
1530 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1531 return 0;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001532out_invalid_value:
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001533 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001534 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001535out_nomem:
1536 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1537 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001538out_security_failure:
1539 free_secdata(secdata);
1540 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1541 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001542}
1543
1544/*
Chuck Leverec88f282009-08-09 15:09:32 -04001545 * Match the requested auth flavors with the list returned by
1546 * the server. Returns zero and sets the mount's authentication
1547 * flavor on success; returns -EACCES if server does not support
1548 * the requested flavor.
1549 */
1550static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1551 struct nfs_mount_request *request)
1552{
1553 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1554
1555 /*
Chuck Lever5eecfde2009-08-21 17:50:30 -04001556 * Certain releases of Linux's mountd return an empty
1557 * flavor list. To prevent behavioral regression with
1558 * these servers (ie. rejecting mounts that used to
1559 * succeed), revert to pre-2.6.32 behavior (no checking)
1560 * if the returned flavor list is empty.
1561 */
1562 if (server_authlist_len == 0)
1563 return 0;
1564
1565 /*
Chuck Leverec88f282009-08-09 15:09:32 -04001566 * We avoid sophisticated negotiating here, as there are
1567 * plenty of cases where we can get it wrong, providing
1568 * either too little or too much security.
1569 *
1570 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1571 * flavor listed first. However, some servers list
1572 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1573 * preferred default, in args->auth_flavors[0] if user
1574 * didn't specify sec= mount option.
1575 */
1576 for (i = 0; i < args->auth_flavor_len; i++)
1577 for (j = 0; j < server_authlist_len; j++)
1578 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1579 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1580 request->auth_flavs[j]);
1581 args->auth_flavors[0] = request->auth_flavs[j];
1582 return 0;
1583 }
1584
1585 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1586 nfs_umount(request);
1587 return -EACCES;
1588}
1589
1590/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001591 * Use the remote server's MOUNT service to request the NFS file handle
1592 * corresponding to the provided path.
1593 */
1594static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1595 struct nfs_fh *root_fh)
1596{
Chuck Leverec88f282009-08-09 15:09:32 -04001597 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1598 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
Chuck Leverc5d120f2008-12-23 15:21:35 -05001599 struct nfs_mount_request request = {
1600 .sap = (struct sockaddr *)
1601 &args->mount_server.address,
1602 .dirpath = args->nfs_server.export_path,
1603 .protocol = args->mount_server.protocol,
1604 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001605 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverec88f282009-08-09 15:09:32 -04001606 .auth_flav_len = &server_authlist_len,
1607 .auth_flavs = server_authlist,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001608 };
Chuck Lever4c568012007-12-10 14:59:28 -05001609 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001610
1611 if (args->mount_server.version == 0) {
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001612 switch (args->version) {
1613 default:
1614 args->mount_server.version = NFS_MNT3_VERSION;
1615 break;
1616 case 2:
1617 args->mount_server.version = NFS_MNT_VERSION;
1618 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001619 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001620 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001621
Chuck Lever33832032007-12-10 14:59:13 -05001622 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001623 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001624 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001625 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001626
Chuck Lever0076d7b2007-07-01 12:13:49 -04001627 /*
1628 * Construct the mount server's address.
1629 */
Chuck Lever4c568012007-12-10 14:59:28 -05001630 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001631 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001632 args->nfs_server.addrlen);
1633 args->mount_server.addrlen = args->nfs_server.addrlen;
1634 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001635 request.salen = args->mount_server.addrlen;
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001636 nfs_set_port(request.sap, &args->mount_server.port, 0);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001637
1638 /*
1639 * Now ask the mount server to map our export path
1640 * to a file handle.
1641 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001642 status = nfs_mount(&request);
Chuck Leverec88f282009-08-09 15:09:32 -04001643 if (status != 0) {
1644 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1645 request.hostname, status);
1646 return status;
1647 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001648
Chuck Leverec88f282009-08-09 15:09:32 -04001649 /*
1650 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1651 */
1652 if (args->mount_server.version != NFS_MNT3_VERSION)
1653 return 0;
1654 return nfs_walk_authlist(args, &request);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001655}
1656
Chuck Leverd1aa0822008-06-23 12:36:45 -04001657static int nfs_parse_simple_hostname(const char *dev_name,
1658 char **hostname, size_t maxnamlen,
1659 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001660{
1661 size_t len;
1662 char *colon, *comma;
1663
1664 colon = strchr(dev_name, ':');
1665 if (colon == NULL)
1666 goto out_bad_devname;
1667
1668 len = colon - dev_name;
1669 if (len > maxnamlen)
1670 goto out_hostname;
1671
1672 /* N.B. caller will free nfs_server.hostname in all cases */
1673 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1674 if (!*hostname)
1675 goto out_nomem;
1676
1677 /* kill possible hostname list: not supported */
1678 comma = strchr(*hostname, ',');
1679 if (comma != NULL) {
1680 if (comma == *hostname)
1681 goto out_bad_devname;
1682 *comma = '\0';
1683 }
1684
1685 colon++;
1686 len = strlen(colon);
1687 if (len > maxpathlen)
1688 goto out_path;
1689 *export_path = kstrndup(colon, len, GFP_KERNEL);
1690 if (!*export_path)
1691 goto out_nomem;
1692
1693 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1694 return 0;
1695
1696out_bad_devname:
1697 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1698 return -EINVAL;
1699
1700out_nomem:
1701 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1702 return -ENOMEM;
1703
1704out_hostname:
1705 dfprintk(MOUNT, "NFS: server hostname too long\n");
1706 return -ENAMETOOLONG;
1707
1708out_path:
1709 dfprintk(MOUNT, "NFS: export pathname too long\n");
1710 return -ENAMETOOLONG;
1711}
1712
1713/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001714 * Hostname has square brackets around it because it contains one or
1715 * more colons. We look for the first closing square bracket, and a
1716 * colon must follow it.
1717 */
1718static int nfs_parse_protected_hostname(const char *dev_name,
1719 char **hostname, size_t maxnamlen,
1720 char **export_path, size_t maxpathlen)
1721{
1722 size_t len;
1723 char *start, *end;
1724
1725 start = (char *)(dev_name + 1);
1726
1727 end = strchr(start, ']');
1728 if (end == NULL)
1729 goto out_bad_devname;
1730 if (*(end + 1) != ':')
1731 goto out_bad_devname;
1732
1733 len = end - start;
1734 if (len > maxnamlen)
1735 goto out_hostname;
1736
1737 /* N.B. caller will free nfs_server.hostname in all cases */
1738 *hostname = kstrndup(start, len, GFP_KERNEL);
1739 if (*hostname == NULL)
1740 goto out_nomem;
1741
1742 end += 2;
1743 len = strlen(end);
1744 if (len > maxpathlen)
1745 goto out_path;
1746 *export_path = kstrndup(end, len, GFP_KERNEL);
1747 if (!*export_path)
1748 goto out_nomem;
1749
1750 return 0;
1751
1752out_bad_devname:
1753 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1754 return -EINVAL;
1755
1756out_nomem:
1757 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1758 return -ENOMEM;
1759
1760out_hostname:
1761 dfprintk(MOUNT, "NFS: server hostname too long\n");
1762 return -ENAMETOOLONG;
1763
1764out_path:
1765 dfprintk(MOUNT, "NFS: export pathname too long\n");
1766 return -ENAMETOOLONG;
1767}
1768
1769/*
1770 * Split "dev_name" into "hostname:export_path".
1771 *
1772 * The leftmost colon demarks the split between the server's hostname
1773 * and the export path. If the hostname starts with a left square
1774 * bracket, then it may contain colons.
1775 *
1776 * Note: caller frees hostname and export path, even on error.
1777 */
1778static int nfs_parse_devname(const char *dev_name,
1779 char **hostname, size_t maxnamlen,
1780 char **export_path, size_t maxpathlen)
1781{
1782 if (*dev_name == '[')
1783 return nfs_parse_protected_hostname(dev_name,
1784 hostname, maxnamlen,
1785 export_path, maxpathlen);
1786
1787 return nfs_parse_simple_hostname(dev_name,
1788 hostname, maxnamlen,
1789 export_path, maxpathlen);
1790}
1791
1792/*
David Howells54ceac42006-08-22 20:06:13 -04001793 * Validate the NFS2/NFS3 mount data
1794 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001795 *
1796 * For option strings, user space handles the following behaviors:
1797 *
1798 * + DNS: mapping server host name to IP address ("addr=" option)
1799 *
1800 * + failure mode: how to behave if a mount request can't be handled
1801 * immediately ("fg/bg" option)
1802 *
1803 * + retry: how often to retry a mount request ("retry=" option)
1804 *
1805 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1806 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001807 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001808static int nfs_validate_mount_data(void *options,
1809 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001810 struct nfs_fh *mntfh,
1811 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001812{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001813 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001814 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
Chuck Lever136d5582007-07-01 12:13:54 -04001815
Chuck Lever5df36e72007-07-01 12:12:56 -04001816 if (data == NULL)
1817 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001818
David Howellsf7b422b2006-06-09 09:34:33 -04001819 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001820 case 1:
1821 data->namlen = 0;
1822 case 2:
1823 data->bsize = 0;
1824 case 3:
1825 if (data->flags & NFS_MOUNT_VER3)
1826 goto out_no_v3;
1827 data->root.size = NFS2_FHSIZE;
1828 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1829 case 4:
1830 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1831 goto out_no_sec;
1832 case 5:
1833 memset(data->context, 0, sizeof(data->context));
1834 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001835 if (data->flags & NFS_MOUNT_VER3) {
1836 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1837 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001838 mntfh->size = data->root.size;
Chuck Lever764302c2009-09-08 19:50:03 -04001839 args->version = 3;
1840 } else {
Chuck Lever5df36e72007-07-01 12:12:56 -04001841 mntfh->size = NFS2_FHSIZE;
Chuck Lever764302c2009-09-08 19:50:03 -04001842 args->version = 2;
1843 }
Chuck Lever5df36e72007-07-01 12:12:56 -04001844
Chuck Lever5df36e72007-07-01 12:12:56 -04001845
1846 memcpy(mntfh->data, data->root.data, mntfh->size);
1847 if (mntfh->size < sizeof(mntfh->data))
1848 memset(mntfh->data + mntfh->size, 0,
1849 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001850
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001851 /*
1852 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1853 * can deal with.
1854 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001855 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
Bryan Schumakerd5eff1a2010-08-03 13:04:00 -04001856 args->flags |= NFS_MOUNT_LEGACY_INTERFACE;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001857 args->rsize = data->rsize;
1858 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001859 args->timeo = data->timeo;
1860 args->retrans = data->retrans;
1861 args->acregmin = data->acregmin;
1862 args->acregmax = data->acregmax;
1863 args->acdirmin = data->acdirmin;
1864 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001865
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001866 memcpy(sap, &data->addr, sizeof(data->addr));
Chuck Lever4c568012007-12-10 14:59:28 -05001867 args->nfs_server.addrlen = sizeof(data->addr);
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001868 if (!nfs_verify_server_address(sap))
Chuck Lever4c568012007-12-10 14:59:28 -05001869 goto out_no_address;
1870
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001871 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001872 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001873 /* N.B. caller will free nfs_server.hostname in all cases */
1874 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1875 args->namlen = data->namlen;
1876 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001877
1878 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1879 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001880 if (!args->nfs_server.hostname)
1881 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001882
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001883 if (!(data->flags & NFS_MOUNT_NONLM))
1884 args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1885 NFS_MOUNT_LOCAL_FCNTL);
1886 else
1887 args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1888 NFS_MOUNT_LOCAL_FCNTL);
Eric Parisf9c3a382008-03-05 14:20:18 -05001889 /*
1890 * The legacy version 6 binary mount data from userspace has a
1891 * field used only to transport selinux information into the
1892 * the kernel. To continue to support that functionality we
1893 * have a touch of selinux knowledge here in the NFS code. The
1894 * userspace code converted context=blah to just blah so we are
1895 * converting back to the full string selinux understands.
1896 */
1897 if (data->context[0]){
1898#ifdef CONFIG_SECURITY_SELINUX
1899 int rc;
1900 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1901 if (!opts_str)
1902 return -ENOMEM;
1903 strcpy(opts_str, "context=");
1904 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1905 strcat(opts_str, &data->context[0]);
1906 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1907 kfree(opts_str);
1908 if (rc)
1909 return rc;
1910#else
1911 return -EINVAL;
1912#endif
1913 }
1914
Chuck Lever5df36e72007-07-01 12:12:56 -04001915 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001916 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001917 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001918
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001919 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001920 return -EINVAL;
1921
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001922 if (!nfs_verify_server_address(sap))
Chuck Lever6e88e062007-09-24 15:39:50 -04001923 goto out_no_address;
1924
Chuck Lever764302c2009-09-08 19:50:03 -04001925 if (args->version == 4)
1926#ifdef CONFIG_NFS_V4
1927 return nfs4_validate_text_mount_data(options,
1928 args, dev_name);
1929#else
1930 goto out_v4_not_compiled;
1931#endif
1932
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001933 nfs_set_port(sap, &args->nfs_server.port, 0);
Chuck Levered596a82008-06-26 17:47:05 -04001934
Trond Myklebust259875e2008-07-02 14:43:47 -04001935 nfs_set_mount_transport_protocol(args);
1936
Chuck Leverdc045892008-06-23 12:36:37 -04001937 status = nfs_parse_devname(dev_name,
1938 &args->nfs_server.hostname,
1939 PAGE_SIZE,
1940 &args->nfs_server.export_path,
1941 NFS_MAXPATHLEN);
1942 if (!status)
1943 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001944
Chuck Leverdc045892008-06-23 12:36:37 -04001945 kfree(args->nfs_server.export_path);
1946 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001947
Chuck Lever136d5582007-07-01 12:13:54 -04001948 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001949 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001950
Chuck Lever136d5582007-07-01 12:13:54 -04001951 break;
1952 }
David Howellsf7b422b2006-06-09 09:34:33 -04001953 }
David Howells54ceac42006-08-22 20:06:13 -04001954
David Howellsf7b422b2006-06-09 09:34:33 -04001955#ifndef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001956 if (args->version == 3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001957 goto out_v3_not_compiled;
1958#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001959
David Howells54ceac42006-08-22 20:06:13 -04001960 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001961
1962out_no_data:
1963 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1964 return -EINVAL;
1965
1966out_no_v3:
1967 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1968 data->version);
1969 return -EINVAL;
1970
1971out_no_sec:
1972 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1973 return -EINVAL;
1974
Chuck Lever5df36e72007-07-01 12:12:56 -04001975#ifndef CONFIG_NFS_V3
1976out_v3_not_compiled:
1977 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1978 return -EPROTONOSUPPORT;
1979#endif /* !CONFIG_NFS_V3 */
1980
Chuck Lever764302c2009-09-08 19:50:03 -04001981#ifndef CONFIG_NFS_V4
1982out_v4_not_compiled:
1983 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1984 return -EPROTONOSUPPORT;
1985#endif /* !CONFIG_NFS_V4 */
1986
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001987out_nomem:
1988 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1989 return -ENOMEM;
1990
Chuck Lever5df36e72007-07-01 12:12:56 -04001991out_no_address:
1992 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1993 return -EINVAL;
1994
1995out_invalid_fh:
1996 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1997 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001998}
1999
Jeff Layton48b605f2008-06-10 15:38:39 -04002000static int
2001nfs_compare_remount_data(struct nfs_server *nfss,
2002 struct nfs_parsed_mount_data *data)
2003{
2004 if (data->flags != nfss->flags ||
2005 data->rsize != nfss->rsize ||
2006 data->wsize != nfss->wsize ||
2007 data->retrans != nfss->client->cl_timeout->to_retries ||
2008 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
2009 data->acregmin != nfss->acregmin / HZ ||
2010 data->acregmax != nfss->acregmax / HZ ||
2011 data->acdirmin != nfss->acdirmin / HZ ||
2012 data->acdirmax != nfss->acdirmax / HZ ||
2013 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04002014 data->nfs_server.port != nfss->port ||
Jeff Layton48b605f2008-06-10 15:38:39 -04002015 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
Stefan Richtera1be9ee2009-10-12 11:26:12 -04002016 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2017 (struct sockaddr *)&nfss->nfs_client->cl_addr))
Jeff Layton48b605f2008-06-10 15:38:39 -04002018 return -EINVAL;
2019
2020 return 0;
2021}
2022
2023static int
2024nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2025{
2026 int error;
2027 struct nfs_server *nfss = sb->s_fs_info;
2028 struct nfs_parsed_mount_data *data;
2029 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2030 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04002031 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04002032
2033 /*
2034 * Userspace mount programs that send binary options generally send
2035 * them populated with default values. We have no way to know which
2036 * ones were explicitly specified. Fall back to legacy behavior and
2037 * just return success.
2038 */
Marc Zyngier31c94462008-07-17 13:21:55 +02002039 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2040 (nfsvers <= 3 && (!options || (options->version >= 1 &&
2041 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04002042 return 0;
2043
2044 data = kzalloc(sizeof(*data), GFP_KERNEL);
2045 if (data == NULL)
2046 return -ENOMEM;
2047
2048 /* fill out struct with values from existing mount */
2049 data->flags = nfss->flags;
2050 data->rsize = nfss->rsize;
2051 data->wsize = nfss->wsize;
2052 data->retrans = nfss->client->cl_timeout->to_retries;
2053 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
2054 data->acregmin = nfss->acregmin / HZ;
2055 data->acregmax = nfss->acregmax / HZ;
2056 data->acdirmin = nfss->acdirmin / HZ;
2057 data->acdirmax = nfss->acdirmax / HZ;
2058 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04002059 data->nfs_server.port = nfss->port;
Jeff Layton48b605f2008-06-10 15:38:39 -04002060 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2061 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2062 data->nfs_server.addrlen);
2063
2064 /* overwrite those values with any that were specified */
2065 error = nfs_parse_mount_options((char *)options, data);
2066 if (error < 0)
2067 goto out;
2068
2069 /* compare new mount options with old ones */
2070 error = nfs_compare_remount_data(nfss, data);
2071out:
2072 kfree(data);
2073 return error;
2074}
2075
David Howells54ceac42006-08-22 20:06:13 -04002076/*
2077 * Initialise the common bits of the superblock
2078 */
2079static inline void nfs_initialise_sb(struct super_block *sb)
2080{
2081 struct nfs_server *server = NFS_SB(sb);
2082
2083 sb->s_magic = NFS_SUPER_MAGIC;
2084
2085 /* We probably want something more informative here */
2086 snprintf(sb->s_id, sizeof(sb->s_id),
2087 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2088
2089 if (sb->s_blocksize == 0)
2090 sb->s_blocksize = nfs_block_bits(server->wsize,
2091 &sb->s_blocksize_bits);
2092
2093 if (server->flags & NFS_MOUNT_NOAC)
2094 sb->s_flags |= MS_SYNCHRONOUS;
2095
Jens Axboe32a88aa2009-09-16 15:02:33 +02002096 sb->s_bdi = &server->backing_dev_info;
2097
David Howells54ceac42006-08-22 20:06:13 -04002098 nfs_super_set_maxbytes(sb, server->maxfilesize);
2099}
2100
2101/*
2102 * Finish setting up an NFS2/3 superblock
2103 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04002104static void nfs_fill_super(struct super_block *sb,
2105 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04002106{
2107 struct nfs_server *server = NFS_SB(sb);
2108
2109 sb->s_blocksize_bits = 0;
2110 sb->s_blocksize = 0;
2111 if (data->bsize)
2112 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2113
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002114 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002115 /* The VFS shouldn't apply the umask to mode bits. We will do
2116 * so ourselves when necessary.
2117 */
2118 sb->s_flags |= MS_POSIXACL;
2119 sb->s_time_gran = 1;
2120 }
2121
2122 sb->s_op = &nfs_sops;
2123 nfs_initialise_sb(sb);
2124}
2125
2126/*
2127 * Finish setting up a cloned NFS2/3 superblock
2128 */
2129static void nfs_clone_super(struct super_block *sb,
2130 const struct super_block *old_sb)
2131{
2132 struct nfs_server *server = NFS_SB(sb);
2133
2134 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2135 sb->s_blocksize = old_sb->s_blocksize;
2136 sb->s_maxbytes = old_sb->s_maxbytes;
2137
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002138 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002139 /* The VFS shouldn't apply the umask to mode bits. We will do
2140 * so ourselves when necessary.
2141 */
2142 sb->s_flags |= MS_POSIXACL;
2143 sb->s_time_gran = 1;
2144 }
2145
2146 sb->s_op = old_sb->s_op;
2147 nfs_initialise_sb(sb);
2148}
2149
Trond Myklebust275a5d22007-05-16 16:53:28 -04002150static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2151{
2152 const struct nfs_server *a = s->s_fs_info;
2153 const struct rpc_clnt *clnt_a = a->client;
2154 const struct rpc_clnt *clnt_b = b->client;
2155
2156 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2157 goto Ebusy;
2158 if (a->nfs_client != b->nfs_client)
2159 goto Ebusy;
2160 if (a->flags != b->flags)
2161 goto Ebusy;
2162 if (a->wsize != b->wsize)
2163 goto Ebusy;
2164 if (a->rsize != b->rsize)
2165 goto Ebusy;
2166 if (a->acregmin != b->acregmin)
2167 goto Ebusy;
2168 if (a->acregmax != b->acregmax)
2169 goto Ebusy;
2170 if (a->acdirmin != b->acdirmin)
2171 goto Ebusy;
2172 if (a->acdirmax != b->acdirmax)
2173 goto Ebusy;
2174 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2175 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002176 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04002177Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002178 return 0;
2179}
2180
2181struct nfs_sb_mountdata {
2182 struct nfs_server *server;
2183 int mntflags;
2184};
2185
2186static int nfs_set_super(struct super_block *s, void *data)
2187{
2188 struct nfs_sb_mountdata *sb_mntdata = data;
2189 struct nfs_server *server = sb_mntdata->server;
2190 int ret;
2191
2192 s->s_flags = sb_mntdata->mntflags;
2193 s->s_fs_info = server;
2194 ret = set_anon_super(s, server);
2195 if (ret == 0)
2196 server->s_dev = s->s_dev;
2197 return ret;
2198}
2199
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002200static int nfs_compare_super_address(struct nfs_server *server1,
2201 struct nfs_server *server2)
2202{
2203 struct sockaddr *sap1, *sap2;
2204
2205 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2206 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2207
2208 if (sap1->sa_family != sap2->sa_family)
2209 return 0;
2210
2211 switch (sap1->sa_family) {
2212 case AF_INET: {
2213 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2214 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2215 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2216 return 0;
2217 if (sin1->sin_port != sin2->sin_port)
2218 return 0;
2219 break;
2220 }
2221 case AF_INET6: {
2222 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2223 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2224 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2225 return 0;
2226 if (sin1->sin6_port != sin2->sin6_port)
2227 return 0;
2228 break;
2229 }
2230 default:
2231 return 0;
2232 }
2233
2234 return 1;
2235}
2236
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002237static int nfs_compare_super(struct super_block *sb, void *data)
2238{
2239 struct nfs_sb_mountdata *sb_mntdata = data;
2240 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2241 int mntflags = sb_mntdata->mntflags;
2242
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002243 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002244 return 0;
2245 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2246 if (old->flags & NFS_MOUNT_UNSHARED)
2247 return 0;
2248 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2249 return 0;
2250 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002251}
2252
Miklos Szeredifa799752008-04-30 00:54:33 -07002253static int nfs_bdi_register(struct nfs_server *server)
2254{
2255 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2256}
2257
David Howells54ceac42006-08-22 20:06:13 -04002258static int nfs_get_sb(struct file_system_type *fs_type,
2259 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2260{
2261 struct nfs_server *server = NULL;
2262 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002263 struct nfs_parsed_mount_data *data;
2264 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002265 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002266 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002267 struct nfs_sb_mountdata sb_mntdata = {
2268 .mntflags = flags,
2269 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002270 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002271
Trond Myklebustc5811db2009-10-06 15:40:15 -04002272 data = nfs_alloc_parsed_mount_data(3);
Trond Myklebustb157b062010-04-19 19:05:48 -04002273 mntfh = nfs_alloc_fhandle();
Trond Myklebust33852a12008-06-19 14:20:11 -04002274 if (data == NULL || mntfh == NULL)
2275 goto out_free_fh;
2276
2277 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002278
David Howells54ceac42006-08-22 20:06:13 -04002279 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002280 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002281 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002282 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002283
Chuck Lever764302c2009-09-08 19:50:03 -04002284#ifdef CONFIG_NFS_V4
2285 if (data->version == 4) {
2286 error = nfs4_try_mount(flags, dev_name, data, mnt);
2287 kfree(data->client_address);
Xiaotian Feng9699eda2010-04-22 18:56:17 +08002288 kfree(data->nfs_server.export_path);
Chuck Lever764302c2009-09-08 19:50:03 -04002289 goto out;
2290 }
2291#endif /* CONFIG_NFS_V4 */
2292
David Howells54ceac42006-08-22 20:06:13 -04002293 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002294 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002295 if (IS_ERR(server)) {
2296 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002297 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002298 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002299 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002300
Trond Myklebust75180df2007-05-16 16:53:28 -04002301 if (server->flags & NFS_MOUNT_UNSHARED)
2302 compare_super = NULL;
2303
David Howells54ceac42006-08-22 20:06:13 -04002304 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002305 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002306 if (IS_ERR(s)) {
2307 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002308 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002309 }
2310
David Howells54ceac42006-08-22 20:06:13 -04002311 if (s->s_fs_info != server) {
2312 nfs_free_server(server);
2313 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002314 } else {
2315 error = nfs_bdi_register(server);
2316 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002317 goto error_splat_bdi;
David Howellsf7b422b2006-06-09 09:34:33 -04002318 }
David Howells54ceac42006-08-22 20:06:13 -04002319
2320 if (!s->s_root) {
2321 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002322 nfs_fill_super(s, data);
David Howells2df54802009-09-23 14:36:39 -04002323 nfs_fscache_get_super_cookie(
2324 s, data ? data->fscache_uniq : NULL, NULL);
David Howells54ceac42006-08-22 20:06:13 -04002325 }
2326
Trond Myklebust33852a12008-06-19 14:20:11 -04002327 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002328 if (IS_ERR(mntroot)) {
2329 error = PTR_ERR(mntroot);
2330 goto error_splat_super;
2331 }
2332
Trond Myklebust33852a12008-06-19 14:20:11 -04002333 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002334 if (error)
2335 goto error_splat_root;
2336
David Howellsf7b422b2006-06-09 09:34:33 -04002337 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002338 mnt->mnt_sb = s;
2339 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002340 error = 0;
2341
2342out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002343 kfree(data->nfs_server.hostname);
2344 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002345 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002346 security_free_mnt_opts(&data->lsm_opts);
2347out_free_fh:
Trond Myklebustb157b062010-04-19 19:05:48 -04002348 nfs_free_fhandle(mntfh);
Trond Myklebust33852a12008-06-19 14:20:11 -04002349 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002350 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002351
David Howells54ceac42006-08-22 20:06:13 -04002352out_err_nosb:
2353 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002354 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002355
Eric Parisf9c3a382008-03-05 14:20:18 -05002356error_splat_root:
2357 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002358error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002359 if (server && !s->s_root)
2360 bdi_unregister(&server->backing_dev_info);
2361error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002362 deactivate_locked_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002363 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002364}
2365
David Howells54ceac42006-08-22 20:06:13 -04002366/*
Trond Myklebust387c1492010-02-03 08:27:35 -05002367 * Ensure that we unregister the bdi before kill_anon_super
2368 * releases the device name
2369 */
2370static void nfs_put_super(struct super_block *s)
2371{
2372 struct nfs_server *server = NFS_SB(s);
2373
2374 bdi_unregister(&server->backing_dev_info);
2375}
2376
2377/*
David Howells54ceac42006-08-22 20:06:13 -04002378 * Destroy an NFS2/3 superblock
2379 */
David Howellsf7b422b2006-06-09 09:34:33 -04002380static void nfs_kill_super(struct super_block *s)
2381{
2382 struct nfs_server *server = NFS_SB(s);
2383
2384 kill_anon_super(s);
David Howells08734042009-04-03 16:42:42 +01002385 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002386 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002387}
2388
David Howells54ceac42006-08-22 20:06:13 -04002389/*
2390 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2391 */
2392static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2393 const char *dev_name, void *raw_data,
2394 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002395{
2396 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002397 struct super_block *s;
2398 struct nfs_server *server;
2399 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002400 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002401 struct nfs_sb_mountdata sb_mntdata = {
2402 .mntflags = flags,
2403 };
David Howells54ceac42006-08-22 20:06:13 -04002404 int error;
2405
2406 dprintk("--> nfs_xdev_get_sb()\n");
2407
2408 /* create a new volume representation */
2409 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2410 if (IS_ERR(server)) {
2411 error = PTR_ERR(server);
2412 goto out_err_noserver;
2413 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002414 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002415
Trond Myklebust75180df2007-05-16 16:53:28 -04002416 if (server->flags & NFS_MOUNT_UNSHARED)
2417 compare_super = NULL;
2418
David Howells54ceac42006-08-22 20:06:13 -04002419 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002420 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002421 if (IS_ERR(s)) {
2422 error = PTR_ERR(s);
2423 goto out_err_nosb;
2424 }
2425
2426 if (s->s_fs_info != server) {
2427 nfs_free_server(server);
2428 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002429 } else {
2430 error = nfs_bdi_register(server);
2431 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002432 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002433 }
2434
2435 if (!s->s_root) {
2436 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002437 nfs_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002438 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002439 }
2440
2441 mntroot = nfs_get_root(s, data->fh);
2442 if (IS_ERR(mntroot)) {
2443 error = PTR_ERR(mntroot);
2444 goto error_splat_super;
2445 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002446 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002447 dput(mntroot);
2448 error = -ESTALE;
2449 goto error_splat_super;
2450 }
David Howells54ceac42006-08-22 20:06:13 -04002451
2452 s->s_flags |= MS_ACTIVE;
2453 mnt->mnt_sb = s;
2454 mnt->mnt_root = mntroot;
2455
Eric Parisf9c3a382008-03-05 14:20:18 -05002456 /* clone any lsm security options from the parent to the new sb */
2457 security_sb_clone_mnt_opts(data->sb, s);
2458
David Howells54ceac42006-08-22 20:06:13 -04002459 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2460 return 0;
2461
2462out_err_nosb:
2463 nfs_free_server(server);
2464out_err_noserver:
2465 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2466 return error;
2467
2468error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002469 if (server && !s->s_root)
2470 bdi_unregister(&server->backing_dev_info);
2471error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002472 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002473 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2474 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002475}
2476
2477#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002478
2479/*
2480 * Finish setting up a cloned NFS4 superblock
2481 */
2482static void nfs4_clone_super(struct super_block *sb,
2483 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002484{
David Howells54ceac42006-08-22 20:06:13 -04002485 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2486 sb->s_blocksize = old_sb->s_blocksize;
2487 sb->s_maxbytes = old_sb->s_maxbytes;
2488 sb->s_time_gran = 1;
2489 sb->s_op = old_sb->s_op;
2490 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002491}
2492
2493/*
2494 * Set up an NFS4 superblock
2495 */
David Howells54ceac42006-08-22 20:06:13 -04002496static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002497{
David Howellsf7b422b2006-06-09 09:34:33 -04002498 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002499 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002500 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002501}
2502
Trond Myklebust01c3f052009-06-17 13:22:58 -07002503static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2504{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04002505 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2506 NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
Trond Myklebust01c3f052009-06-17 13:22:58 -07002507}
2508
Chuck Lever7630c852009-09-08 19:49:57 -04002509static int nfs4_validate_text_mount_data(void *options,
2510 struct nfs_parsed_mount_data *args,
2511 const char *dev_name)
2512{
2513 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2514
Trond Myklebustf5855fe2009-10-06 15:40:37 -04002515 nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
Chuck Lever7630c852009-09-08 19:49:57 -04002516
2517 nfs_validate_transport_protocol(args);
2518
2519 nfs4_validate_mount_flags(args);
2520
Trond Myklebust2ecda722009-09-08 19:50:07 -04002521 if (args->version != 4) {
2522 dfprintk(MOUNT,
2523 "NFS4: Illegal mount version\n");
2524 return -EINVAL;
2525 }
2526
Chuck Lever7630c852009-09-08 19:49:57 -04002527 if (args->auth_flavor_len > 1) {
2528 dfprintk(MOUNT,
2529 "NFS4: Too many RPC auth flavours specified\n");
2530 return -EINVAL;
2531 }
2532
2533 if (args->client_address == NULL) {
2534 dfprintk(MOUNT,
2535 "NFS4: mount program didn't pass callback address\n");
2536 return -EINVAL;
2537 }
2538
2539 return nfs_parse_devname(dev_name,
2540 &args->nfs_server.hostname,
2541 NFS4_MAXNAMLEN,
2542 &args->nfs_server.export_path,
2543 NFS4_MAXPATHLEN);
2544}
2545
David Howells54ceac42006-08-22 20:06:13 -04002546/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002547 * Validate NFSv4 mount options
2548 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002549static int nfs4_validate_mount_data(void *options,
2550 struct nfs_parsed_mount_data *args,
2551 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002552{
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002553 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002554 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002555 char *c;
2556
2557 if (data == NULL)
2558 goto out_no_data;
2559
2560 switch (data->version) {
2561 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002562 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002563 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002564 if (data->host_addrlen == 0)
2565 goto out_no_address;
2566 args->nfs_server.addrlen = data->host_addrlen;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002567 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002568 return -EFAULT;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002569 if (!nfs_verify_server_address(sap))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002570 goto out_no_address;
2571
Chuck Lever6738b252008-06-24 16:33:54 -04002572 if (data->auth_flavourlen) {
2573 if (data->auth_flavourlen > 1)
2574 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002575 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002576 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002577 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002578 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002579 }
2580
2581 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2582 if (IS_ERR(c))
2583 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002584 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002585
2586 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2587 if (IS_ERR(c))
2588 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002589 args->nfs_server.export_path = c;
2590 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002591
2592 c = strndup_user(data->client_addr.data, 16);
2593 if (IS_ERR(c))
2594 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002595 args->client_address = c;
2596
2597 /*
2598 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2599 * can deal with.
2600 */
2601
2602 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2603 args->rsize = data->rsize;
2604 args->wsize = data->wsize;
2605 args->timeo = data->timeo;
2606 args->retrans = data->retrans;
2607 args->acregmin = data->acregmin;
2608 args->acregmax = data->acregmax;
2609 args->acdirmin = data->acdirmin;
2610 args->acdirmax = data->acdirmax;
2611 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002612 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002613
2614 break;
Chuck Lever7630c852009-09-08 19:49:57 -04002615 default:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002616 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002617 return -EINVAL;
2618
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002619 if (!nfs_verify_server_address(sap))
Chuck Lever80071222007-07-01 12:13:59 -04002620 return -EINVAL;
Chuck Levered596a82008-06-26 17:47:05 -04002621
Chuck Lever7630c852009-09-08 19:49:57 -04002622 return nfs4_validate_text_mount_data(options, args, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002623 }
2624
2625 return 0;
2626
2627out_no_data:
2628 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2629 return -EINVAL;
2630
2631out_inval_auth:
2632 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2633 data->auth_flavourlen);
2634 return -EINVAL;
2635
2636out_no_address:
2637 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2638 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002639}
2640
2641/*
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002642 * Get the superblock for the NFS4 root partition
David Howells54ceac42006-08-22 20:06:13 -04002643 */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002644static int nfs4_remote_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -04002645 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002646{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002647 struct nfs_parsed_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002648 struct super_block *s;
2649 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002650 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002651 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002652 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002653 struct nfs_sb_mountdata sb_mntdata = {
2654 .mntflags = flags,
2655 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002656 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002657
Trond Myklebustb157b062010-04-19 19:05:48 -04002658 mntfh = nfs_alloc_fhandle();
Trond Myklebust33852a12008-06-19 14:20:11 -04002659 if (data == NULL || mntfh == NULL)
2660 goto out_free_fh;
2661
2662 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002663
David Howells54ceac42006-08-22 20:06:13 -04002664 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002665 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002666 if (IS_ERR(server)) {
2667 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002668 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002669 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002670 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002671
Trond Myklebust75180df2007-05-16 16:53:28 -04002672 if (server->flags & NFS4_MOUNT_UNSHARED)
2673 compare_super = NULL;
2674
David Howells54ceac42006-08-22 20:06:13 -04002675 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002676 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002677 if (IS_ERR(s)) {
2678 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002679 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002680 }
2681
Trond Myklebust5dd31772006-08-24 01:03:05 -04002682 if (s->s_fs_info != server) {
2683 nfs_free_server(server);
2684 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002685 } else {
2686 error = nfs_bdi_register(server);
2687 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002688 goto error_splat_bdi;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002689 }
2690
David Howells54ceac42006-08-22 20:06:13 -04002691 if (!s->s_root) {
2692 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002693 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002694 nfs_fscache_get_super_cookie(
2695 s, data ? data->fscache_uniq : NULL, NULL);
Trond Myklebust816724e2006-06-24 08:41:41 -04002696 }
David Howellsf7b422b2006-06-09 09:34:33 -04002697
Trond Myklebust33852a12008-06-19 14:20:11 -04002698 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002699 if (IS_ERR(mntroot)) {
2700 error = PTR_ERR(mntroot);
2701 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002702 }
David Howells54ceac42006-08-22 20:06:13 -04002703
Trond Myklebust33852a12008-06-19 14:20:11 -04002704 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002705 if (error)
2706 goto error_splat_root;
2707
David Howellsf7b422b2006-06-09 09:34:33 -04002708 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002709 mnt->mnt_sb = s;
2710 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002711 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002712
Chuck Lever29eb9812007-07-01 12:12:30 -04002713out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002714 security_free_mnt_opts(&data->lsm_opts);
2715out_free_fh:
Trond Myklebustb157b062010-04-19 19:05:48 -04002716 nfs_free_fhandle(mntfh);
Trond Myklebust816724e2006-06-24 08:41:41 -04002717 return error;
David Howells54ceac42006-08-22 20:06:13 -04002718
Chuck Lever29eb9812007-07-01 12:12:30 -04002719out_free:
2720 nfs_free_server(server);
2721 goto out;
2722
Eric Paris46c8ac72008-05-02 13:42:42 -07002723error_splat_root:
2724 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002725error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002726 if (server && !s->s_root)
2727 bdi_unregister(&server->backing_dev_info);
2728error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002729 deactivate_locked_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002730 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002731}
2732
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002733static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2734 int flags, void *data, const char *hostname)
2735{
2736 struct vfsmount *root_mnt;
2737 char *root_devname;
2738 size_t len;
2739
2740 len = strlen(hostname) + 3;
2741 root_devname = kmalloc(len, GFP_KERNEL);
2742 if (root_devname == NULL)
2743 return ERR_PTR(-ENOMEM);
2744 snprintf(root_devname, len, "%s:/", hostname);
2745 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2746 kfree(root_devname);
2747 return root_mnt;
2748}
2749
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002750static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2751{
2752 char *page = (char *) __get_free_page(GFP_KERNEL);
2753 char *devname, *tmp;
2754
2755 if (page == NULL)
2756 return;
2757 devname = nfs_path(path->mnt->mnt_devname,
2758 path->mnt->mnt_root, path->dentry,
2759 page, PAGE_SIZE);
Dan Carpentercdd29ec2010-04-22 15:35:56 -04002760 if (IS_ERR(devname))
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002761 goto out_freepage;
2762 tmp = kstrdup(devname, GFP_KERNEL);
2763 if (tmp == NULL)
2764 goto out_freepage;
2765 kfree(mnt->mnt_devname);
2766 mnt->mnt_devname = tmp;
2767out_freepage:
2768 free_page((unsigned long)page);
2769}
2770
Trond Myklebustce587e02010-04-16 16:22:52 -04002771struct nfs_referral_count {
2772 struct list_head list;
2773 const struct task_struct *task;
2774 unsigned int referral_count;
2775};
2776
2777static LIST_HEAD(nfs_referral_count_list);
2778static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
2779
2780static struct nfs_referral_count *nfs_find_referral_count(void)
2781{
2782 struct nfs_referral_count *p;
2783
2784 list_for_each_entry(p, &nfs_referral_count_list, list) {
2785 if (p->task == current)
2786 return p;
2787 }
2788 return NULL;
2789}
2790
2791#define NFS_MAX_NESTED_REFERRALS 2
2792
2793static int nfs_referral_loop_protect(void)
2794{
2795 struct nfs_referral_count *p, *new;
2796 int ret = -ENOMEM;
2797
2798 new = kmalloc(sizeof(*new), GFP_KERNEL);
2799 if (!new)
2800 goto out;
2801 new->task = current;
2802 new->referral_count = 1;
2803
2804 ret = 0;
2805 spin_lock(&nfs_referral_count_list_lock);
2806 p = nfs_find_referral_count();
2807 if (p != NULL) {
2808 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
2809 ret = -ELOOP;
2810 else
2811 p->referral_count++;
2812 } else {
2813 list_add(&new->list, &nfs_referral_count_list);
2814 new = NULL;
2815 }
2816 spin_unlock(&nfs_referral_count_list_lock);
2817 kfree(new);
2818out:
2819 return ret;
2820}
2821
2822static void nfs_referral_loop_unprotect(void)
2823{
2824 struct nfs_referral_count *p;
2825
2826 spin_lock(&nfs_referral_count_list_lock);
2827 p = nfs_find_referral_count();
2828 p->referral_count--;
2829 if (p->referral_count == 0)
2830 list_del(&p->list);
2831 else
2832 p = NULL;
2833 spin_unlock(&nfs_referral_count_list_lock);
2834 kfree(p);
2835}
2836
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002837static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2838 const char *export_path, struct vfsmount *mnt_target)
2839{
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002840 struct nameidata *nd = NULL;
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002841 struct mnt_namespace *ns_private;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002842 struct super_block *s;
2843 int ret;
2844
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002845 nd = kmalloc(sizeof(*nd), GFP_KERNEL);
2846 if (nd == NULL)
2847 return -ENOMEM;
2848
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002849 ns_private = create_mnt_ns(root_mnt);
2850 ret = PTR_ERR(ns_private);
2851 if (IS_ERR(ns_private))
2852 goto out_mntput;
2853
Trond Myklebustce587e02010-04-16 16:22:52 -04002854 ret = nfs_referral_loop_protect();
2855 if (ret != 0)
2856 goto out_put_mnt_ns;
2857
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002858 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002859 export_path, LOOKUP_FOLLOW, nd);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002860
Trond Myklebustce587e02010-04-16 16:22:52 -04002861 nfs_referral_loop_unprotect();
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002862 put_mnt_ns(ns_private);
2863
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002864 if (ret != 0)
2865 goto out_err;
2866
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002867 s = nd->path.mnt->mnt_sb;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002868 atomic_inc(&s->s_active);
2869 mnt_target->mnt_sb = s;
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002870 mnt_target->mnt_root = dget(nd->path.dentry);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002871
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002872 /* Correct the device pathname */
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002873 nfs_fix_devname(&nd->path, mnt_target);
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002874
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002875 path_put(&nd->path);
2876 kfree(nd);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002877 down_write(&s->s_umount);
2878 return 0;
Trond Myklebustce587e02010-04-16 16:22:52 -04002879out_put_mnt_ns:
2880 put_mnt_ns(ns_private);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002881out_mntput:
2882 mntput(root_mnt);
2883out_err:
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002884 kfree(nd);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002885 return ret;
2886}
2887
Chuck Levera6fe23b2009-09-08 19:50:00 -04002888static int nfs4_try_mount(int flags, const char *dev_name,
2889 struct nfs_parsed_mount_data *data,
2890 struct vfsmount *mnt)
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002891{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002892 char *export_path;
2893 struct vfsmount *root_mnt;
Chuck Levera6fe23b2009-09-08 19:50:00 -04002894 int error;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002895
Chuck Levera6fe23b2009-09-08 19:50:00 -04002896 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002897
2898 export_path = data->nfs_server.export_path;
2899 data->nfs_server.export_path = "/";
2900 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2901 data->nfs_server.hostname);
2902 data->nfs_server.export_path = export_path;
2903
2904 error = PTR_ERR(root_mnt);
2905 if (IS_ERR(root_mnt))
2906 goto out;
2907
2908 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2909
2910out:
Chuck Levera6fe23b2009-09-08 19:50:00 -04002911 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2912 error != 0 ? " [error]" : "");
2913 return error;
2914}
2915
2916/*
2917 * Get the superblock for an NFS4 mountpoint
2918 */
2919static int nfs4_get_sb(struct file_system_type *fs_type,
2920 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2921{
2922 struct nfs_parsed_mount_data *data;
2923 int error = -ENOMEM;
2924
Trond Myklebustc5811db2009-10-06 15:40:15 -04002925 data = nfs_alloc_parsed_mount_data(4);
Chuck Levera6fe23b2009-09-08 19:50:00 -04002926 if (data == NULL)
2927 goto out_free_data;
2928
2929 /* Validate the mount data */
2930 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2931 if (error < 0)
2932 goto out;
2933
2934 error = nfs4_try_mount(flags, dev_name, data, mnt);
2935
2936out:
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002937 kfree(data->client_address);
2938 kfree(data->nfs_server.export_path);
2939 kfree(data->nfs_server.hostname);
2940 kfree(data->fscache_uniq);
2941out_free_data:
2942 kfree(data);
2943 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2944 error != 0 ? " [error]" : "");
2945 return error;
2946}
2947
David Howellsf7b422b2006-06-09 09:34:33 -04002948static void nfs4_kill_super(struct super_block *sb)
2949{
2950 struct nfs_server *server = NFS_SB(sb);
2951
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002952 dprintk("--> %s\n", __func__);
Trond Myklebust515d8612008-12-23 15:21:46 -05002953 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002954 kill_anon_super(sb);
David Howells08734042009-04-03 16:42:42 +01002955 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002956 nfs_free_server(server);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002957 dprintk("<-- %s\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -04002958}
2959
2960/*
David Howells54ceac42006-08-22 20:06:13 -04002961 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002962 */
David Howells54ceac42006-08-22 20:06:13 -04002963static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2964 const char *dev_name, void *raw_data,
2965 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002966{
2967 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002968 struct super_block *s;
2969 struct nfs_server *server;
2970 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002971 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002972 struct nfs_sb_mountdata sb_mntdata = {
2973 .mntflags = flags,
2974 };
David Howells54ceac42006-08-22 20:06:13 -04002975 int error;
2976
2977 dprintk("--> nfs4_xdev_get_sb()\n");
2978
2979 /* create a new volume representation */
2980 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2981 if (IS_ERR(server)) {
2982 error = PTR_ERR(server);
2983 goto out_err_noserver;
2984 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002985 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002986
Trond Myklebust75180df2007-05-16 16:53:28 -04002987 if (server->flags & NFS4_MOUNT_UNSHARED)
2988 compare_super = NULL;
2989
David Howells54ceac42006-08-22 20:06:13 -04002990 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002991 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002992 if (IS_ERR(s)) {
2993 error = PTR_ERR(s);
2994 goto out_err_nosb;
2995 }
2996
2997 if (s->s_fs_info != server) {
2998 nfs_free_server(server);
2999 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07003000 } else {
3001 error = nfs_bdi_register(server);
3002 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11003003 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04003004 }
3005
3006 if (!s->s_root) {
3007 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04003008 nfs4_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04003009 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04003010 }
3011
3012 mntroot = nfs4_get_root(s, data->fh);
3013 if (IS_ERR(mntroot)) {
3014 error = PTR_ERR(mntroot);
3015 goto error_splat_super;
3016 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05003017 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3018 dput(mntroot);
3019 error = -ESTALE;
3020 goto error_splat_super;
3021 }
David Howells54ceac42006-08-22 20:06:13 -04003022
3023 s->s_flags |= MS_ACTIVE;
3024 mnt->mnt_sb = s;
3025 mnt->mnt_root = mntroot;
3026
Eric Paris46c8ac72008-05-02 13:42:42 -07003027 security_sb_clone_mnt_opts(data->sb, s);
3028
David Howells54ceac42006-08-22 20:06:13 -04003029 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
3030 return 0;
3031
3032out_err_nosb:
3033 nfs_free_server(server);
3034out_err_noserver:
3035 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
3036 return error;
3037
3038error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11003039 if (server && !s->s_root)
3040 bdi_unregister(&server->backing_dev_info);
3041error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04003042 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04003043 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
3044 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04003045}
3046
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04003047static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
3048 int flags, const char *dev_name, void *raw_data,
3049 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04003050{
3051 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04003052 struct super_block *s;
3053 struct nfs_server *server;
3054 struct dentry *mntroot;
Trond Myklebust4f727292010-04-16 16:22:48 -04003055 struct nfs_fh *mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04003056 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04003057 struct nfs_sb_mountdata sb_mntdata = {
3058 .mntflags = flags,
3059 };
Trond Myklebust4f727292010-04-16 16:22:48 -04003060 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04003061
3062 dprintk("--> nfs4_referral_get_sb()\n");
3063
Trond Myklebust4f727292010-04-16 16:22:48 -04003064 mntfh = nfs_alloc_fhandle();
3065 if (mntfh == NULL)
3066 goto out_err_nofh;
3067
David Howells54ceac42006-08-22 20:06:13 -04003068 /* create a new volume representation */
Trond Myklebust4f727292010-04-16 16:22:48 -04003069 server = nfs4_create_referral_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003070 if (IS_ERR(server)) {
3071 error = PTR_ERR(server);
3072 goto out_err_noserver;
3073 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04003074 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04003075
Trond Myklebust75180df2007-05-16 16:53:28 -04003076 if (server->flags & NFS4_MOUNT_UNSHARED)
3077 compare_super = NULL;
3078
David Howells54ceac42006-08-22 20:06:13 -04003079 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04003080 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04003081 if (IS_ERR(s)) {
3082 error = PTR_ERR(s);
3083 goto out_err_nosb;
3084 }
3085
3086 if (s->s_fs_info != server) {
3087 nfs_free_server(server);
3088 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07003089 } else {
3090 error = nfs_bdi_register(server);
3091 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11003092 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04003093 }
3094
3095 if (!s->s_root) {
3096 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04003097 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04003098 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04003099 }
3100
Trond Myklebust4f727292010-04-16 16:22:48 -04003101 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003102 if (IS_ERR(mntroot)) {
3103 error = PTR_ERR(mntroot);
3104 goto error_splat_super;
3105 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05003106 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3107 dput(mntroot);
3108 error = -ESTALE;
3109 goto error_splat_super;
3110 }
David Howells54ceac42006-08-22 20:06:13 -04003111
3112 s->s_flags |= MS_ACTIVE;
3113 mnt->mnt_sb = s;
3114 mnt->mnt_root = mntroot;
3115
Eric Paris46c8ac72008-05-02 13:42:42 -07003116 security_sb_clone_mnt_opts(data->sb, s);
3117
Trond Myklebust4f727292010-04-16 16:22:48 -04003118 nfs_free_fhandle(mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003119 dprintk("<-- nfs4_referral_get_sb() = 0\n");
3120 return 0;
3121
3122out_err_nosb:
3123 nfs_free_server(server);
3124out_err_noserver:
Trond Myklebust4f727292010-04-16 16:22:48 -04003125 nfs_free_fhandle(mntfh);
3126out_err_nofh:
David Howells54ceac42006-08-22 20:06:13 -04003127 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
3128 return error;
3129
3130error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11003131 if (server && !s->s_root)
3132 bdi_unregister(&server->backing_dev_info);
3133error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04003134 deactivate_locked_super(s);
Trond Myklebust4f727292010-04-16 16:22:48 -04003135 nfs_free_fhandle(mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003136 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
3137 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04003138}
3139
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04003140/*
3141 * Create an NFS4 server record on referral traversal
3142 */
3143static int nfs4_referral_get_sb(struct file_system_type *fs_type,
3144 int flags, const char *dev_name, void *raw_data,
3145 struct vfsmount *mnt)
3146{
3147 struct nfs_clone_mount *data = raw_data;
3148 char *export_path;
3149 struct vfsmount *root_mnt;
3150 int error;
3151
3152 dprintk("--> nfs4_referral_get_sb()\n");
3153
3154 export_path = data->mnt_path;
3155 data->mnt_path = "/";
3156
3157 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
3158 flags, data, data->hostname);
3159 data->mnt_path = export_path;
3160
3161 error = PTR_ERR(root_mnt);
3162 if (IS_ERR(root_mnt))
3163 goto out;
3164
3165 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
3166out:
3167 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
3168 error != 0 ? " [error]" : "");
3169 return error;
3170}
3171
David Howells54ceac42006-08-22 20:06:13 -04003172#endif /* CONFIG_NFS_V4 */