blob: 2e866d86c220392b4da5d072c6cefa59102b437a [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;
David Howellsf7b422b2006-06-09 09:34:33 -0400635
Chuck Lever82d101d2008-03-14 14:10:37 -0400636 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400637 seq_printf(m, ",rsize=%u", nfss->rsize);
638 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400639 if (nfss->bsize != 0)
640 seq_printf(m, ",bsize=%u", nfss->bsize);
641 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400642 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400643 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400644 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400645 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400646 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400647 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400648 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400649 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400650 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
651 if (nfss->flags & nfs_infop->flag)
652 seq_puts(m, nfs_infop->str);
653 else
654 seq_puts(m, nfs_infop->nostr);
655 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400656 seq_printf(m, ",proto=%s",
Jeff Laytonee671b02009-12-03 15:58:56 -0500657 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
Chuck Lever82d101d2008-03-14 14:10:37 -0400658 if (version == 4) {
659 if (nfss->port != NFS_PORT)
660 seq_printf(m, ",port=%u", nfss->port);
661 } else
662 if (nfss->port)
663 seq_printf(m, ",port=%u", nfss->port);
664
Trond Myklebust33170232007-12-20 16:03:59 -0500665 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
666 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400667 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400668
669 if (version != 4)
670 nfs_show_mountd_options(m, nfss, showdefaults);
Trond Myklebust0be81892010-06-18 12:23:58 -0400671 else
672 nfs_show_nfsv4_options(m, nfss, showdefaults);
Chuck Lever82d101d2008-03-14 14:10:37 -0400673
David Howellsb797cac2009-04-03 16:42:48 +0100674 if (nfss->options & NFS_OPTION_FSCACHE)
675 seq_printf(m, ",fsc");
Patrick J. LoPresti9b00c642010-08-10 17:28:01 -0400676
677 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
678 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
679 seq_printf(m, ",lookupcache=none");
680 else
681 seq_printf(m, ",lookupcache=pos");
682 }
David Howellsf7b422b2006-06-09 09:34:33 -0400683}
684
685/*
686 * Describe the mount options on this VFS mountpoint
687 */
688static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
689{
690 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
691
692 nfs_show_mount_options(m, nfss, 0);
693
Chuck Lever5d8515c2007-12-10 14:57:16 -0500694 seq_printf(m, ",addr=%s",
695 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
696 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400697
698 return 0;
699}
700
701/*
702 * Present statistical information for this VFS mountpoint
703 */
704static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
705{
706 int i, cpu;
707 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
708 struct rpc_auth *auth = nfss->client->cl_auth;
709 struct nfs_iostats totals = { };
710
711 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
712
713 /*
714 * Display all mount option settings
715 */
716 seq_printf(m, "\n\topts:\t");
717 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
718 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
719 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
720 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
721 nfs_show_mount_options(m, nfss, 1);
722
723 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
724
725 seq_printf(m, "\n\tcaps:\t");
726 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400727 seq_printf(m, ",wtmult=%u", nfss->wtmult);
728 seq_printf(m, ",dtsize=%u", nfss->dtsize);
729 seq_printf(m, ",bsize=%u", nfss->bsize);
730 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400731
732#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500733 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400734 seq_printf(m, "\n\tnfsv4:\t");
735 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
736 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
737 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
738 }
739#endif
740
741 /*
742 * Display security flavor in effect for this mount
743 */
Chuck Lever2d767432008-03-14 14:10:08 -0400744 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400745 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400746 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400747
748 /*
749 * Display superblock I/O counters
750 */
751 for_each_possible_cpu(cpu) {
752 struct nfs_iostats *stats;
753
754 preempt_disable();
755 stats = per_cpu_ptr(nfss->io_stats, cpu);
756
757 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
758 totals.events[i] += stats->events[i];
759 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
760 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100761#ifdef CONFIG_NFS_FSCACHE
762 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
763 totals.fscache[i] += stats->fscache[i];
764#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400765
766 preempt_enable();
767 }
768
769 seq_printf(m, "\n\tevents:\t");
770 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
771 seq_printf(m, "%lu ", totals.events[i]);
772 seq_printf(m, "\n\tbytes:\t");
773 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
774 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100775#ifdef CONFIG_NFS_FSCACHE
776 if (nfss->options & NFS_OPTION_FSCACHE) {
777 seq_printf(m, "\n\tfsc:\t");
778 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
779 seq_printf(m, "%Lu ", totals.bytes[i]);
780 }
781#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400782 seq_printf(m, "\n");
783
784 rpc_print_iostats(m, nfss->client);
785
786 return 0;
787}
788
789/*
790 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400791 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400792 */
Al Viro42faad92008-04-24 07:21:56 -0400793static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400794{
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200795 struct nfs_server *server;
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400796 struct rpc_clnt *rpc;
797
Alessio Igor Bogani67e55202009-04-24 09:06:53 +0200798 server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400799 /* -EIO all pending I/O */
800 rpc = server->client_acl;
801 if (!IS_ERR(rpc))
802 rpc_killall_tasks(rpc);
803 rpc = server->client;
804 if (!IS_ERR(rpc))
805 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400806}
807
Trond Myklebustc5811db2009-10-06 15:40:15 -0400808static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
Chuck Lever9423a082009-09-23 14:36:38 -0400809{
810 struct nfs_parsed_mount_data *data;
811
812 data = kzalloc(sizeof(*data), GFP_KERNEL);
813 if (data) {
Chuck Lever9423a082009-09-23 14:36:38 -0400814 data->acregmin = NFS_DEF_ACREGMIN;
815 data->acregmax = NFS_DEF_ACREGMAX;
816 data->acdirmin = NFS_DEF_ACDIRMIN;
817 data->acdirmax = NFS_DEF_ACDIRMAX;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400818 data->mount_server.port = NFS_UNSPEC_PORT;
Chuck Lever9423a082009-09-23 14:36:38 -0400819 data->nfs_server.port = NFS_UNSPEC_PORT;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400820 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Lever9423a082009-09-23 14:36:38 -0400821 data->auth_flavors[0] = RPC_AUTH_UNIX;
822 data->auth_flavor_len = 1;
Trond Myklebustc5811db2009-10-06 15:40:15 -0400823 data->version = version;
Chuck Lever9423a082009-09-23 14:36:38 -0400824 data->minorversion = 0;
825 }
826 return data;
827}
828
David Howellsf7b422b2006-06-09 09:34:33 -0400829/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500830 * Sanity-check a server address provided by the mount command.
831 *
832 * Address family must be initialized, and address must not be
833 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400834 */
835static int nfs_verify_server_address(struct sockaddr *addr)
836{
837 switch (addr->sa_family) {
838 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500839 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700840 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500841 }
842 case AF_INET6: {
843 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
844 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400845 }
846 }
847
Chuck Lever53a0b9c2009-08-09 15:09:36 -0400848 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
Chuck Leverfc50d582007-07-01 12:12:46 -0400849 return 0;
850}
851
Chuck Lever9412b922007-12-10 14:59:21 -0500852/*
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400853 * Select between a default port value and a user-specified port value.
854 * If a zero value is set, then autobind will be used.
855 */
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400856static void nfs_set_port(struct sockaddr *sap, int *port,
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400857 const unsigned short default_port)
858{
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400859 if (*port == NFS_UNSPEC_PORT)
860 *port = default_port;
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400861
Trond Myklebustf5855fe2009-10-06 15:40:37 -0400862 rpc_set_port(sap, *port);
Chuck Lever4cfd74f2009-09-08 19:49:47 -0400863}
864
865/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400866 * Sanity check the NFS transport protocol.
867 *
868 */
869static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
870{
871 switch (mnt->nfs_server.protocol) {
872 case XPRT_TRANSPORT_UDP:
873 case XPRT_TRANSPORT_TCP:
874 case XPRT_TRANSPORT_RDMA:
875 break;
876 default:
877 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
878 }
879}
880
881/*
882 * For text based NFSv2/v3 mounts, the mount protocol transport default
883 * settings should depend upon the specified NFS transport.
884 */
885static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
886{
887 nfs_validate_transport_protocol(mnt);
888
889 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
890 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
891 return;
892 switch (mnt->nfs_server.protocol) {
893 case XPRT_TRANSPORT_UDP:
894 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
895 break;
896 case XPRT_TRANSPORT_TCP:
897 case XPRT_TRANSPORT_RDMA:
898 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
899 }
900}
901
902/*
Chuck Lever01060c82008-06-24 16:33:38 -0400903 * Parse the value of the 'sec=' option.
Chuck Lever01060c82008-06-24 16:33:38 -0400904 */
905static int nfs_parse_security_flavors(char *value,
906 struct nfs_parsed_mount_data *mnt)
907{
908 substring_t args[MAX_OPT_ARGS];
909
910 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
911
912 switch (match_token(value, nfs_secflavor_tokens, args)) {
913 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400914 mnt->auth_flavors[0] = RPC_AUTH_NULL;
915 break;
916 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400917 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
918 break;
919 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400920 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
921 break;
922 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400923 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
924 break;
925 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400926 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
927 break;
928 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400929 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
930 break;
931 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400932 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
933 break;
934 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400935 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
936 break;
937 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400938 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
939 break;
940 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400941 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
942 break;
943 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400944 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
945 break;
946 default:
947 return 0;
948 }
949
Chuck Lever059f90b2009-08-09 15:09:31 -0400950 mnt->auth_flavor_len = 1;
Chuck Lever01060c82008-06-24 16:33:38 -0400951 return 1;
952}
953
954/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400955 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400956 * a data structure. The whole mount string is processed; bad options are
957 * skipped as they are encountered. If there were no errors, return 1;
958 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400959 */
960static int nfs_parse_mount_options(char *raw,
961 struct nfs_parsed_mount_data *mnt)
962{
Eric Parisf9c3a382008-03-05 14:20:18 -0500963 char *p, *string, *secdata;
Chuck Leverd23c45f2009-06-17 18:02:13 -0700964 int rc, sloppy = 0, invalid_option = 0;
Jeff Laytonee671b02009-12-03 15:58:56 -0500965 unsigned short protofamily = AF_UNSPEC;
966 unsigned short mountfamily = AF_UNSPEC;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400967
968 if (!raw) {
969 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
970 return 1;
971 }
972 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
973
Eric Parisf9c3a382008-03-05 14:20:18 -0500974 secdata = alloc_secdata();
975 if (!secdata)
976 goto out_nomem;
977
978 rc = security_sb_copy_data(raw, secdata);
979 if (rc)
980 goto out_security_failure;
981
982 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
983 if (rc)
984 goto out_security_failure;
985
986 free_secdata(secdata);
987
Chuck Leverbf0fd762007-07-01 12:13:44 -0400988 while ((p = strsep(&raw, ",")) != NULL) {
989 substring_t args[MAX_OPT_ARGS];
Chuck Levera5a16ba2009-06-17 18:02:14 -0700990 unsigned long option;
991 int token;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400992
993 if (!*p)
994 continue;
995
996 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
997
998 token = match_token(p, nfs_mount_option_tokens, args);
999 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001000
1001 /*
1002 * boolean options: foo/nofoo
1003 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001004 case Opt_soft:
1005 mnt->flags |= NFS_MOUNT_SOFT;
1006 break;
1007 case Opt_hard:
1008 mnt->flags &= ~NFS_MOUNT_SOFT;
1009 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001010 case Opt_posix:
1011 mnt->flags |= NFS_MOUNT_POSIX;
1012 break;
1013 case Opt_noposix:
1014 mnt->flags &= ~NFS_MOUNT_POSIX;
1015 break;
1016 case Opt_cto:
1017 mnt->flags &= ~NFS_MOUNT_NOCTO;
1018 break;
1019 case Opt_nocto:
1020 mnt->flags |= NFS_MOUNT_NOCTO;
1021 break;
1022 case Opt_ac:
1023 mnt->flags &= ~NFS_MOUNT_NOAC;
1024 break;
1025 case Opt_noac:
1026 mnt->flags |= NFS_MOUNT_NOAC;
1027 break;
1028 case Opt_lock:
1029 mnt->flags &= ~NFS_MOUNT_NONLM;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001030 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1031 NFS_MOUNT_LOCAL_FCNTL);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001032 break;
1033 case Opt_nolock:
1034 mnt->flags |= NFS_MOUNT_NONLM;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001035 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1036 NFS_MOUNT_LOCAL_FCNTL);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001037 break;
1038 case Opt_v2:
1039 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001040 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001041 break;
1042 case Opt_v3:
1043 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001044 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001045 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001046#ifdef CONFIG_NFS_V4
1047 case Opt_v4:
1048 mnt->flags &= ~NFS_MOUNT_VER3;
1049 mnt->version = 4;
1050 break;
1051#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001052 case Opt_udp:
1053 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001054 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001055 break;
1056 case Opt_tcp:
1057 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001058 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001059 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001060 case Opt_rdma:
1061 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1062 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001063 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001064 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001065 case Opt_acl:
1066 mnt->flags &= ~NFS_MOUNT_NOACL;
1067 break;
1068 case Opt_noacl:
1069 mnt->flags |= NFS_MOUNT_NOACL;
1070 break;
1071 case Opt_rdirplus:
1072 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1073 break;
1074 case Opt_nordirplus:
1075 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1076 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001077 case Opt_sharecache:
1078 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1079 break;
1080 case Opt_nosharecache:
1081 mnt->flags |= NFS_MOUNT_UNSHARED;
1082 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001083 case Opt_resvport:
1084 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1085 break;
1086 case Opt_noresvport:
1087 mnt->flags |= NFS_MOUNT_NORESVPORT;
1088 break;
David Howellsb797cac2009-04-03 16:42:48 +01001089 case Opt_fscache:
1090 mnt->options |= NFS_OPTION_FSCACHE;
1091 kfree(mnt->fscache_uniq);
1092 mnt->fscache_uniq = NULL;
1093 break;
1094 case Opt_nofscache:
1095 mnt->options &= ~NFS_OPTION_FSCACHE;
1096 kfree(mnt->fscache_uniq);
1097 mnt->fscache_uniq = NULL;
1098 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001099
Chuck Leverf45663c2008-06-24 19:28:02 -04001100 /*
1101 * options that take numeric values
1102 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001103 case Opt_port:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001104 string = match_strdup(args);
1105 if (string == NULL)
1106 goto out_nomem;
1107 rc = strict_strtoul(string, 10, &option);
1108 kfree(string);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001109 if (rc != 0 || option > USHRT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001110 goto out_invalid_value;
1111 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001112 break;
1113 case Opt_rsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001114 string = match_strdup(args);
1115 if (string == NULL)
1116 goto out_nomem;
1117 rc = strict_strtoul(string, 10, &option);
1118 kfree(string);
1119 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001120 goto out_invalid_value;
1121 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001122 break;
1123 case Opt_wsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001124 string = match_strdup(args);
1125 if (string == NULL)
1126 goto out_nomem;
1127 rc = strict_strtoul(string, 10, &option);
1128 kfree(string);
1129 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001130 goto out_invalid_value;
1131 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001132 break;
1133 case Opt_bsize:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001134 string = match_strdup(args);
1135 if (string == NULL)
1136 goto out_nomem;
1137 rc = strict_strtoul(string, 10, &option);
1138 kfree(string);
1139 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001140 goto out_invalid_value;
1141 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001142 break;
1143 case Opt_timeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001144 string = match_strdup(args);
1145 if (string == NULL)
1146 goto out_nomem;
1147 rc = strict_strtoul(string, 10, &option);
1148 kfree(string);
1149 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001150 goto out_invalid_value;
1151 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001152 break;
1153 case Opt_retrans:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001154 string = match_strdup(args);
1155 if (string == NULL)
1156 goto out_nomem;
1157 rc = strict_strtoul(string, 10, &option);
1158 kfree(string);
1159 if (rc != 0 || option == 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001160 goto out_invalid_value;
1161 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001162 break;
1163 case Opt_acregmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001164 string = match_strdup(args);
1165 if (string == NULL)
1166 goto out_nomem;
1167 rc = strict_strtoul(string, 10, &option);
1168 kfree(string);
1169 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001170 goto out_invalid_value;
1171 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001172 break;
1173 case Opt_acregmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001174 string = match_strdup(args);
1175 if (string == NULL)
1176 goto out_nomem;
1177 rc = strict_strtoul(string, 10, &option);
1178 kfree(string);
1179 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001180 goto out_invalid_value;
1181 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001182 break;
1183 case Opt_acdirmin:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001184 string = match_strdup(args);
1185 if (string == NULL)
1186 goto out_nomem;
1187 rc = strict_strtoul(string, 10, &option);
1188 kfree(string);
1189 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001190 goto out_invalid_value;
1191 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001192 break;
1193 case Opt_acdirmax:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001194 string = match_strdup(args);
1195 if (string == NULL)
1196 goto out_nomem;
1197 rc = strict_strtoul(string, 10, &option);
1198 kfree(string);
1199 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001200 goto out_invalid_value;
1201 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001202 break;
1203 case Opt_actimeo:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001204 string = match_strdup(args);
1205 if (string == NULL)
1206 goto out_nomem;
1207 rc = strict_strtoul(string, 10, &option);
1208 kfree(string);
1209 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001210 goto out_invalid_value;
1211 mnt->acregmin = mnt->acregmax =
1212 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001213 break;
1214 case Opt_namelen:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001215 string = match_strdup(args);
1216 if (string == NULL)
1217 goto out_nomem;
1218 rc = strict_strtoul(string, 10, &option);
1219 kfree(string);
1220 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001221 goto out_invalid_value;
1222 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001223 break;
1224 case Opt_mountport:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001225 string = match_strdup(args);
1226 if (string == NULL)
1227 goto out_nomem;
1228 rc = strict_strtoul(string, 10, &option);
1229 kfree(string);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001230 if (rc != 0 || option > USHRT_MAX)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001231 goto out_invalid_value;
1232 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001233 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001234 case Opt_mountvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001235 string = match_strdup(args);
1236 if (string == NULL)
1237 goto out_nomem;
1238 rc = strict_strtoul(string, 10, &option);
1239 kfree(string);
1240 if (rc != 0 ||
Chuck Leverf45663c2008-06-24 19:28:02 -04001241 option < NFS_MNT_VERSION ||
Chuck Leverd23c45f2009-06-17 18:02:13 -07001242 option > NFS_MNT3_VERSION)
1243 goto out_invalid_value;
1244 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001245 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001246 case Opt_nfsvers:
Chuck Levera5a16ba2009-06-17 18:02:14 -07001247 string = match_strdup(args);
1248 if (string == NULL)
1249 goto out_nomem;
1250 rc = strict_strtoul(string, 10, &option);
1251 kfree(string);
1252 if (rc != 0)
Chuck Leverd23c45f2009-06-17 18:02:13 -07001253 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001254 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001255 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001256 mnt->flags &= ~NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001257 mnt->version = 2;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001258 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001259 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001260 mnt->flags |= NFS_MOUNT_VER3;
Chuck Lever764302c2009-09-08 19:50:03 -04001261 mnt->version = 3;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001262 break;
Chuck Lever764302c2009-09-08 19:50:03 -04001263#ifdef CONFIG_NFS_V4
1264 case NFS4_VERSION:
1265 mnt->flags &= ~NFS_MOUNT_VER3;
1266 mnt->version = 4;
1267 break;
1268#endif
Chuck Leverbf0fd762007-07-01 12:13:44 -04001269 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001270 goto out_invalid_value;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001271 }
1272 break;
Mike Sager3fd5be92009-04-01 09:21:48 -04001273 case Opt_minorversion:
Chuck Leverf3f4f4e2009-08-09 15:09:29 -04001274 string = match_strdup(args);
1275 if (string == NULL)
1276 goto out_nomem;
1277 rc = strict_strtoul(string, 10, &option);
1278 kfree(string);
1279 if (rc != 0)
1280 goto out_invalid_value;
1281 if (option > NFS4_MAX_MINOR_VERSION)
1282 goto out_invalid_value;
1283 mnt->minorversion = option;
Mike Sager3fd5be92009-04-01 09:21:48 -04001284 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001285
Chuck Leverf45663c2008-06-24 19:28:02 -04001286 /*
1287 * options that take text values
1288 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001289 case Opt_sec:
1290 string = match_strdup(args);
1291 if (string == NULL)
1292 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001293 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001294 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001295 if (!rc) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001296 dfprintk(MOUNT, "NFS: unrecognized "
1297 "security flavor\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001298 return 0;
Chuck Leverf45663c2008-06-24 19:28:02 -04001299 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001300 break;
1301 case Opt_proto:
1302 string = match_strdup(args);
1303 if (string == NULL)
1304 goto out_nomem;
1305 token = match_token(string,
1306 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001307
Jeff Laytonee671b02009-12-03 15:58:56 -05001308 protofamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001309 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001310 case Opt_xprt_udp6:
1311 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001312 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001313 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001314 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001315 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001316 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001317 case Opt_xprt_tcp6:
1318 protofamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001319 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001320 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001321 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001322 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001323 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001324 case Opt_xprt_rdma:
1325 /* vector side protocols to TCP */
1326 mnt->flags |= NFS_MOUNT_TCP;
1327 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001328 xprt_load_transport(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001329 kfree(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001330 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001331 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001332 dfprintk(MOUNT, "NFS: unrecognized "
1333 "transport protocol\n");
Yinghai Lu4223a4a2009-10-20 14:13:46 +09001334 kfree(string);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001335 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001336 }
1337 break;
1338 case Opt_mountproto:
1339 string = match_strdup(args);
1340 if (string == NULL)
1341 goto out_nomem;
1342 token = match_token(string,
1343 nfs_xprt_protocol_tokens, args);
Trond Myklebustd508afb2009-04-06 16:41:35 -07001344 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001345
Jeff Laytonee671b02009-12-03 15:58:56 -05001346 mountfamily = AF_INET;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001347 switch (token) {
Jeff Laytonee671b02009-12-03 15:58:56 -05001348 case Opt_xprt_udp6:
1349 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001350 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001351 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001352 break;
Jeff Laytonee671b02009-12-03 15:58:56 -05001353 case Opt_xprt_tcp6:
1354 mountfamily = AF_INET6;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001355 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001356 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001357 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001358 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001359 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001360 dfprintk(MOUNT, "NFS: unrecognized "
1361 "transport protocol\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001362 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001363 }
1364 break;
1365 case Opt_addr:
1366 string = match_strdup(args);
1367 if (string == NULL)
1368 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001369 mnt->nfs_server.addrlen =
1370 rpc_pton(string, strlen(string),
1371 (struct sockaddr *)
1372 &mnt->nfs_server.address,
1373 sizeof(mnt->nfs_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001374 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001375 if (mnt->nfs_server.addrlen == 0)
1376 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001377 break;
1378 case Opt_clientaddr:
1379 string = match_strdup(args);
1380 if (string == NULL)
1381 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001382 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001383 mnt->client_address = string;
1384 break;
Chuck Lever33832032007-12-10 14:59:13 -05001385 case Opt_mounthost:
1386 string = match_strdup(args);
1387 if (string == NULL)
1388 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001389 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001390 mnt->mount_server.hostname = string;
1391 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001392 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001393 string = match_strdup(args);
1394 if (string == NULL)
1395 goto out_nomem;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001396 mnt->mount_server.addrlen =
1397 rpc_pton(string, strlen(string),
1398 (struct sockaddr *)
1399 &mnt->mount_server.address,
1400 sizeof(mnt->mount_server.address));
Chuck Leverbf0fd762007-07-01 12:13:44 -04001401 kfree(string);
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001402 if (mnt->mount_server.addrlen == 0)
1403 goto out_invalid_address;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001404 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001405 case Opt_lookupcache:
1406 string = match_strdup(args);
1407 if (string == NULL)
1408 goto out_nomem;
1409 token = match_token(string,
1410 nfs_lookupcache_tokens, args);
1411 kfree(string);
1412 switch (token) {
1413 case Opt_lookupcache_all:
1414 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1415 break;
1416 case Opt_lookupcache_positive:
1417 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1418 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1419 break;
1420 case Opt_lookupcache_none:
1421 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1422 break;
1423 default:
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001424 dfprintk(MOUNT, "NFS: invalid "
1425 "lookupcache argument\n");
Chuck Leverd23c45f2009-06-17 18:02:13 -07001426 return 0;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001427 };
1428 break;
Chuck Levera6d5ff62010-05-07 13:33:58 -04001429 case Opt_fscache_uniq:
1430 string = match_strdup(args);
1431 if (string == NULL)
1432 goto out_nomem;
1433 kfree(mnt->fscache_uniq);
1434 mnt->fscache_uniq = string;
1435 mnt->options |= NFS_OPTION_FSCACHE;
1436 break;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001437 case Opt_local_lock:
1438 string = match_strdup(args);
1439 if (string == NULL)
1440 goto out_nomem;
1441 token = match_token(string, nfs_local_lock_tokens,
1442 args);
1443 kfree(string);
1444 switch (token) {
1445 case Opt_local_lock_all:
1446 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1447 NFS_MOUNT_LOCAL_FCNTL);
1448 break;
1449 case Opt_local_lock_flock:
1450 mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1451 break;
1452 case Opt_local_lock_posix:
1453 mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1454 break;
1455 case Opt_local_lock_none:
1456 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1457 NFS_MOUNT_LOCAL_FCNTL);
1458 break;
1459 default:
1460 dfprintk(MOUNT, "NFS: invalid "
1461 "local_lock argument\n");
1462 return 0;
1463 };
1464 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001465
Chuck Leverf45663c2008-06-24 19:28:02 -04001466 /*
1467 * Special options
1468 */
1469 case Opt_sloppy:
1470 sloppy = 1;
1471 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1472 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001473 case Opt_userspace:
1474 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001475 dfprintk(MOUNT, "NFS: ignoring mount option "
1476 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001477 break;
1478
1479 default:
Chuck Leverd23c45f2009-06-17 18:02:13 -07001480 invalid_option = 1;
Chuck Leverf45663c2008-06-24 19:28:02 -04001481 dfprintk(MOUNT, "NFS: unrecognized mount option "
1482 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001483 }
1484 }
1485
Chuck Leverd23c45f2009-06-17 18:02:13 -07001486 if (!sloppy && invalid_option)
1487 return 0;
1488
Jeff Laytonee671b02009-12-03 15:58:56 -05001489 /*
1490 * verify that any proto=/mountproto= options match the address
1491 * familiies in the addr=/mountaddr= options.
1492 */
1493 if (protofamily != AF_UNSPEC &&
1494 protofamily != mnt->nfs_server.address.ss_family)
1495 goto out_proto_mismatch;
1496
1497 if (mountfamily != AF_UNSPEC) {
1498 if (mnt->mount_server.addrlen) {
1499 if (mountfamily != mnt->mount_server.address.ss_family)
1500 goto out_mountproto_mismatch;
1501 } else {
1502 if (mountfamily != mnt->nfs_server.address.ss_family)
1503 goto out_mountproto_mismatch;
1504 }
1505 }
1506
Chuck Leverbf0fd762007-07-01 12:13:44 -04001507 return 1;
1508
Jeff Laytonee671b02009-12-03 15:58:56 -05001509out_mountproto_mismatch:
1510 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1511 "option\n");
1512 return 0;
1513out_proto_mismatch:
1514 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1515 return 0;
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001516out_invalid_address:
1517 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1518 return 0;
Chuck Leverd23c45f2009-06-17 18:02:13 -07001519out_invalid_value:
Chuck Lever53a0b9c2009-08-09 15:09:36 -04001520 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
Chuck Leverd23c45f2009-06-17 18:02:13 -07001521 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001522out_nomem:
1523 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1524 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001525out_security_failure:
1526 free_secdata(secdata);
1527 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1528 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001529}
1530
1531/*
Chuck Leverec88f282009-08-09 15:09:32 -04001532 * Match the requested auth flavors with the list returned by
1533 * the server. Returns zero and sets the mount's authentication
1534 * flavor on success; returns -EACCES if server does not support
1535 * the requested flavor.
1536 */
1537static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1538 struct nfs_mount_request *request)
1539{
1540 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1541
1542 /*
Chuck Lever5eecfde2009-08-21 17:50:30 -04001543 * Certain releases of Linux's mountd return an empty
1544 * flavor list. To prevent behavioral regression with
1545 * these servers (ie. rejecting mounts that used to
1546 * succeed), revert to pre-2.6.32 behavior (no checking)
1547 * if the returned flavor list is empty.
1548 */
1549 if (server_authlist_len == 0)
1550 return 0;
1551
1552 /*
Chuck Leverec88f282009-08-09 15:09:32 -04001553 * We avoid sophisticated negotiating here, as there are
1554 * plenty of cases where we can get it wrong, providing
1555 * either too little or too much security.
1556 *
1557 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1558 * flavor listed first. However, some servers list
1559 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1560 * preferred default, in args->auth_flavors[0] if user
1561 * didn't specify sec= mount option.
1562 */
1563 for (i = 0; i < args->auth_flavor_len; i++)
1564 for (j = 0; j < server_authlist_len; j++)
1565 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1566 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1567 request->auth_flavs[j]);
1568 args->auth_flavors[0] = request->auth_flavs[j];
1569 return 0;
1570 }
1571
1572 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1573 nfs_umount(request);
1574 return -EACCES;
1575}
1576
1577/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001578 * Use the remote server's MOUNT service to request the NFS file handle
1579 * corresponding to the provided path.
1580 */
1581static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1582 struct nfs_fh *root_fh)
1583{
Chuck Leverec88f282009-08-09 15:09:32 -04001584 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1585 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
Chuck Leverc5d120f2008-12-23 15:21:35 -05001586 struct nfs_mount_request request = {
1587 .sap = (struct sockaddr *)
1588 &args->mount_server.address,
1589 .dirpath = args->nfs_server.export_path,
1590 .protocol = args->mount_server.protocol,
1591 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001592 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverec88f282009-08-09 15:09:32 -04001593 .auth_flav_len = &server_authlist_len,
1594 .auth_flavs = server_authlist,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001595 };
Chuck Lever4c568012007-12-10 14:59:28 -05001596 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001597
1598 if (args->mount_server.version == 0) {
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001599 switch (args->version) {
1600 default:
1601 args->mount_server.version = NFS_MNT3_VERSION;
1602 break;
1603 case 2:
1604 args->mount_server.version = NFS_MNT_VERSION;
1605 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001606 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001607 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001608
Chuck Lever33832032007-12-10 14:59:13 -05001609 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001610 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001611 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001612 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001613
Chuck Lever0076d7b2007-07-01 12:13:49 -04001614 /*
1615 * Construct the mount server's address.
1616 */
Chuck Lever4c568012007-12-10 14:59:28 -05001617 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001618 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001619 args->nfs_server.addrlen);
1620 args->mount_server.addrlen = args->nfs_server.addrlen;
1621 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001622 request.salen = args->mount_server.addrlen;
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001623 nfs_set_port(request.sap, &args->mount_server.port, 0);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001624
1625 /*
1626 * Now ask the mount server to map our export path
1627 * to a file handle.
1628 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001629 status = nfs_mount(&request);
Chuck Leverec88f282009-08-09 15:09:32 -04001630 if (status != 0) {
1631 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1632 request.hostname, status);
1633 return status;
1634 }
Chuck Lever0076d7b2007-07-01 12:13:49 -04001635
Chuck Leverec88f282009-08-09 15:09:32 -04001636 /*
1637 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1638 */
1639 if (args->mount_server.version != NFS_MNT3_VERSION)
1640 return 0;
1641 return nfs_walk_authlist(args, &request);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001642}
1643
Chuck Leverd1aa0822008-06-23 12:36:45 -04001644static int nfs_parse_simple_hostname(const char *dev_name,
1645 char **hostname, size_t maxnamlen,
1646 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001647{
1648 size_t len;
1649 char *colon, *comma;
1650
1651 colon = strchr(dev_name, ':');
1652 if (colon == NULL)
1653 goto out_bad_devname;
1654
1655 len = colon - dev_name;
1656 if (len > maxnamlen)
1657 goto out_hostname;
1658
1659 /* N.B. caller will free nfs_server.hostname in all cases */
1660 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1661 if (!*hostname)
1662 goto out_nomem;
1663
1664 /* kill possible hostname list: not supported */
1665 comma = strchr(*hostname, ',');
1666 if (comma != NULL) {
1667 if (comma == *hostname)
1668 goto out_bad_devname;
1669 *comma = '\0';
1670 }
1671
1672 colon++;
1673 len = strlen(colon);
1674 if (len > maxpathlen)
1675 goto out_path;
1676 *export_path = kstrndup(colon, len, GFP_KERNEL);
1677 if (!*export_path)
1678 goto out_nomem;
1679
1680 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1681 return 0;
1682
1683out_bad_devname:
1684 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1685 return -EINVAL;
1686
1687out_nomem:
1688 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1689 return -ENOMEM;
1690
1691out_hostname:
1692 dfprintk(MOUNT, "NFS: server hostname too long\n");
1693 return -ENAMETOOLONG;
1694
1695out_path:
1696 dfprintk(MOUNT, "NFS: export pathname too long\n");
1697 return -ENAMETOOLONG;
1698}
1699
1700/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001701 * Hostname has square brackets around it because it contains one or
1702 * more colons. We look for the first closing square bracket, and a
1703 * colon must follow it.
1704 */
1705static int nfs_parse_protected_hostname(const char *dev_name,
1706 char **hostname, size_t maxnamlen,
1707 char **export_path, size_t maxpathlen)
1708{
1709 size_t len;
1710 char *start, *end;
1711
1712 start = (char *)(dev_name + 1);
1713
1714 end = strchr(start, ']');
1715 if (end == NULL)
1716 goto out_bad_devname;
1717 if (*(end + 1) != ':')
1718 goto out_bad_devname;
1719
1720 len = end - start;
1721 if (len > maxnamlen)
1722 goto out_hostname;
1723
1724 /* N.B. caller will free nfs_server.hostname in all cases */
1725 *hostname = kstrndup(start, len, GFP_KERNEL);
1726 if (*hostname == NULL)
1727 goto out_nomem;
1728
1729 end += 2;
1730 len = strlen(end);
1731 if (len > maxpathlen)
1732 goto out_path;
1733 *export_path = kstrndup(end, len, GFP_KERNEL);
1734 if (!*export_path)
1735 goto out_nomem;
1736
1737 return 0;
1738
1739out_bad_devname:
1740 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1741 return -EINVAL;
1742
1743out_nomem:
1744 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1745 return -ENOMEM;
1746
1747out_hostname:
1748 dfprintk(MOUNT, "NFS: server hostname too long\n");
1749 return -ENAMETOOLONG;
1750
1751out_path:
1752 dfprintk(MOUNT, "NFS: export pathname too long\n");
1753 return -ENAMETOOLONG;
1754}
1755
1756/*
1757 * Split "dev_name" into "hostname:export_path".
1758 *
1759 * The leftmost colon demarks the split between the server's hostname
1760 * and the export path. If the hostname starts with a left square
1761 * bracket, then it may contain colons.
1762 *
1763 * Note: caller frees hostname and export path, even on error.
1764 */
1765static int nfs_parse_devname(const char *dev_name,
1766 char **hostname, size_t maxnamlen,
1767 char **export_path, size_t maxpathlen)
1768{
1769 if (*dev_name == '[')
1770 return nfs_parse_protected_hostname(dev_name,
1771 hostname, maxnamlen,
1772 export_path, maxpathlen);
1773
1774 return nfs_parse_simple_hostname(dev_name,
1775 hostname, maxnamlen,
1776 export_path, maxpathlen);
1777}
1778
1779/*
David Howells54ceac42006-08-22 20:06:13 -04001780 * Validate the NFS2/NFS3 mount data
1781 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001782 *
1783 * For option strings, user space handles the following behaviors:
1784 *
1785 * + DNS: mapping server host name to IP address ("addr=" option)
1786 *
1787 * + failure mode: how to behave if a mount request can't be handled
1788 * immediately ("fg/bg" option)
1789 *
1790 * + retry: how often to retry a mount request ("retry=" option)
1791 *
1792 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1793 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001794 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001795static int nfs_validate_mount_data(void *options,
1796 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001797 struct nfs_fh *mntfh,
1798 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001799{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001800 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001801 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
Chuck Lever136d5582007-07-01 12:13:54 -04001802
Chuck Lever5df36e72007-07-01 12:12:56 -04001803 if (data == NULL)
1804 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001805
David Howellsf7b422b2006-06-09 09:34:33 -04001806 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001807 case 1:
1808 data->namlen = 0;
1809 case 2:
1810 data->bsize = 0;
1811 case 3:
1812 if (data->flags & NFS_MOUNT_VER3)
1813 goto out_no_v3;
1814 data->root.size = NFS2_FHSIZE;
1815 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1816 case 4:
1817 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1818 goto out_no_sec;
1819 case 5:
1820 memset(data->context, 0, sizeof(data->context));
1821 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001822 if (data->flags & NFS_MOUNT_VER3) {
1823 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1824 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001825 mntfh->size = data->root.size;
Chuck Lever764302c2009-09-08 19:50:03 -04001826 args->version = 3;
1827 } else {
Chuck Lever5df36e72007-07-01 12:12:56 -04001828 mntfh->size = NFS2_FHSIZE;
Chuck Lever764302c2009-09-08 19:50:03 -04001829 args->version = 2;
1830 }
Chuck Lever5df36e72007-07-01 12:12:56 -04001831
Chuck Lever5df36e72007-07-01 12:12:56 -04001832
1833 memcpy(mntfh->data, data->root.data, mntfh->size);
1834 if (mntfh->size < sizeof(mntfh->data))
1835 memset(mntfh->data + mntfh->size, 0,
1836 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001837
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001838 /*
1839 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1840 * can deal with.
1841 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001842 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
Bryan Schumakerd5eff1a2010-08-03 13:04:00 -04001843 args->flags |= NFS_MOUNT_LEGACY_INTERFACE;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001844 args->rsize = data->rsize;
1845 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001846 args->timeo = data->timeo;
1847 args->retrans = data->retrans;
1848 args->acregmin = data->acregmin;
1849 args->acregmax = data->acregmax;
1850 args->acdirmin = data->acdirmin;
1851 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001852
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001853 memcpy(sap, &data->addr, sizeof(data->addr));
Chuck Lever4c568012007-12-10 14:59:28 -05001854 args->nfs_server.addrlen = sizeof(data->addr);
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001855 if (!nfs_verify_server_address(sap))
Chuck Lever4c568012007-12-10 14:59:28 -05001856 goto out_no_address;
1857
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001858 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001859 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001860 /* N.B. caller will free nfs_server.hostname in all cases */
1861 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1862 args->namlen = data->namlen;
1863 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001864
1865 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1866 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001867 if (!args->nfs_server.hostname)
1868 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001869
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04001870 if (!(data->flags & NFS_MOUNT_NONLM))
1871 args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1872 NFS_MOUNT_LOCAL_FCNTL);
1873 else
1874 args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1875 NFS_MOUNT_LOCAL_FCNTL);
Eric Parisf9c3a382008-03-05 14:20:18 -05001876 /*
1877 * The legacy version 6 binary mount data from userspace has a
1878 * field used only to transport selinux information into the
1879 * the kernel. To continue to support that functionality we
1880 * have a touch of selinux knowledge here in the NFS code. The
1881 * userspace code converted context=blah to just blah so we are
1882 * converting back to the full string selinux understands.
1883 */
1884 if (data->context[0]){
1885#ifdef CONFIG_SECURITY_SELINUX
1886 int rc;
1887 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1888 if (!opts_str)
1889 return -ENOMEM;
1890 strcpy(opts_str, "context=");
1891 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1892 strcat(opts_str, &data->context[0]);
1893 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1894 kfree(opts_str);
1895 if (rc)
1896 return rc;
1897#else
1898 return -EINVAL;
1899#endif
1900 }
1901
Chuck Lever5df36e72007-07-01 12:12:56 -04001902 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001903 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001904 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001905
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001906 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001907 return -EINVAL;
1908
Chuck Lever4cfd74f2009-09-08 19:49:47 -04001909 if (!nfs_verify_server_address(sap))
Chuck Lever6e88e062007-09-24 15:39:50 -04001910 goto out_no_address;
1911
Chuck Lever764302c2009-09-08 19:50:03 -04001912 if (args->version == 4)
1913#ifdef CONFIG_NFS_V4
1914 return nfs4_validate_text_mount_data(options,
1915 args, dev_name);
1916#else
1917 goto out_v4_not_compiled;
1918#endif
1919
Trond Myklebustf5855fe2009-10-06 15:40:37 -04001920 nfs_set_port(sap, &args->nfs_server.port, 0);
Chuck Levered596a82008-06-26 17:47:05 -04001921
Trond Myklebust259875e2008-07-02 14:43:47 -04001922 nfs_set_mount_transport_protocol(args);
1923
Chuck Leverdc045892008-06-23 12:36:37 -04001924 status = nfs_parse_devname(dev_name,
1925 &args->nfs_server.hostname,
1926 PAGE_SIZE,
1927 &args->nfs_server.export_path,
1928 NFS_MAXPATHLEN);
1929 if (!status)
1930 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001931
Chuck Leverdc045892008-06-23 12:36:37 -04001932 kfree(args->nfs_server.export_path);
1933 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001934
Chuck Lever136d5582007-07-01 12:13:54 -04001935 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001936 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001937
Chuck Lever136d5582007-07-01 12:13:54 -04001938 break;
1939 }
David Howellsf7b422b2006-06-09 09:34:33 -04001940 }
David Howells54ceac42006-08-22 20:06:13 -04001941
David Howellsf7b422b2006-06-09 09:34:33 -04001942#ifndef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04001943 if (args->version == 3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001944 goto out_v3_not_compiled;
1945#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001946
David Howells54ceac42006-08-22 20:06:13 -04001947 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001948
1949out_no_data:
1950 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1951 return -EINVAL;
1952
1953out_no_v3:
1954 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1955 data->version);
1956 return -EINVAL;
1957
1958out_no_sec:
1959 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1960 return -EINVAL;
1961
Chuck Lever5df36e72007-07-01 12:12:56 -04001962#ifndef CONFIG_NFS_V3
1963out_v3_not_compiled:
1964 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1965 return -EPROTONOSUPPORT;
1966#endif /* !CONFIG_NFS_V3 */
1967
Chuck Lever764302c2009-09-08 19:50:03 -04001968#ifndef CONFIG_NFS_V4
1969out_v4_not_compiled:
1970 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1971 return -EPROTONOSUPPORT;
1972#endif /* !CONFIG_NFS_V4 */
1973
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001974out_nomem:
1975 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1976 return -ENOMEM;
1977
Chuck Lever5df36e72007-07-01 12:12:56 -04001978out_no_address:
1979 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1980 return -EINVAL;
1981
1982out_invalid_fh:
1983 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1984 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001985}
1986
Jeff Layton48b605f2008-06-10 15:38:39 -04001987static int
1988nfs_compare_remount_data(struct nfs_server *nfss,
1989 struct nfs_parsed_mount_data *data)
1990{
1991 if (data->flags != nfss->flags ||
1992 data->rsize != nfss->rsize ||
1993 data->wsize != nfss->wsize ||
1994 data->retrans != nfss->client->cl_timeout->to_retries ||
1995 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1996 data->acregmin != nfss->acregmin / HZ ||
1997 data->acregmax != nfss->acregmax / HZ ||
1998 data->acdirmin != nfss->acdirmin / HZ ||
1999 data->acdirmax != nfss->acdirmax / HZ ||
2000 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04002001 data->nfs_server.port != nfss->port ||
Jeff Layton48b605f2008-06-10 15:38:39 -04002002 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
Stefan Richtera1be9ee2009-10-12 11:26:12 -04002003 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2004 (struct sockaddr *)&nfss->nfs_client->cl_addr))
Jeff Layton48b605f2008-06-10 15:38:39 -04002005 return -EINVAL;
2006
2007 return 0;
2008}
2009
2010static int
2011nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2012{
2013 int error;
2014 struct nfs_server *nfss = sb->s_fs_info;
2015 struct nfs_parsed_mount_data *data;
2016 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2017 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04002018 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04002019
2020 /*
2021 * Userspace mount programs that send binary options generally send
2022 * them populated with default values. We have no way to know which
2023 * ones were explicitly specified. Fall back to legacy behavior and
2024 * just return success.
2025 */
Marc Zyngier31c94462008-07-17 13:21:55 +02002026 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2027 (nfsvers <= 3 && (!options || (options->version >= 1 &&
2028 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04002029 return 0;
2030
2031 data = kzalloc(sizeof(*data), GFP_KERNEL);
2032 if (data == NULL)
2033 return -ENOMEM;
2034
2035 /* fill out struct with values from existing mount */
2036 data->flags = nfss->flags;
2037 data->rsize = nfss->rsize;
2038 data->wsize = nfss->wsize;
2039 data->retrans = nfss->client->cl_timeout->to_retries;
2040 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
2041 data->acregmin = nfss->acregmin / HZ;
2042 data->acregmax = nfss->acregmax / HZ;
2043 data->acdirmin = nfss->acdirmin / HZ;
2044 data->acdirmax = nfss->acdirmax / HZ;
2045 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
Trond Myklebustbcd2ea12009-10-06 15:41:22 -04002046 data->nfs_server.port = nfss->port;
Jeff Layton48b605f2008-06-10 15:38:39 -04002047 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2048 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2049 data->nfs_server.addrlen);
2050
2051 /* overwrite those values with any that were specified */
2052 error = nfs_parse_mount_options((char *)options, data);
2053 if (error < 0)
2054 goto out;
2055
2056 /* compare new mount options with old ones */
2057 error = nfs_compare_remount_data(nfss, data);
2058out:
2059 kfree(data);
2060 return error;
2061}
2062
David Howells54ceac42006-08-22 20:06:13 -04002063/*
2064 * Initialise the common bits of the superblock
2065 */
2066static inline void nfs_initialise_sb(struct super_block *sb)
2067{
2068 struct nfs_server *server = NFS_SB(sb);
2069
2070 sb->s_magic = NFS_SUPER_MAGIC;
2071
2072 /* We probably want something more informative here */
2073 snprintf(sb->s_id, sizeof(sb->s_id),
2074 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2075
2076 if (sb->s_blocksize == 0)
2077 sb->s_blocksize = nfs_block_bits(server->wsize,
2078 &sb->s_blocksize_bits);
2079
2080 if (server->flags & NFS_MOUNT_NOAC)
2081 sb->s_flags |= MS_SYNCHRONOUS;
2082
Jens Axboe32a88aa2009-09-16 15:02:33 +02002083 sb->s_bdi = &server->backing_dev_info;
2084
David Howells54ceac42006-08-22 20:06:13 -04002085 nfs_super_set_maxbytes(sb, server->maxfilesize);
2086}
2087
2088/*
2089 * Finish setting up an NFS2/3 superblock
2090 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04002091static void nfs_fill_super(struct super_block *sb,
2092 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04002093{
2094 struct nfs_server *server = NFS_SB(sb);
2095
2096 sb->s_blocksize_bits = 0;
2097 sb->s_blocksize = 0;
2098 if (data->bsize)
2099 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2100
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002101 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002102 /* The VFS shouldn't apply the umask to mode bits. We will do
2103 * so ourselves when necessary.
2104 */
2105 sb->s_flags |= MS_POSIXACL;
2106 sb->s_time_gran = 1;
2107 }
2108
2109 sb->s_op = &nfs_sops;
2110 nfs_initialise_sb(sb);
2111}
2112
2113/*
2114 * Finish setting up a cloned NFS2/3 superblock
2115 */
2116static void nfs_clone_super(struct super_block *sb,
2117 const struct super_block *old_sb)
2118{
2119 struct nfs_server *server = NFS_SB(sb);
2120
2121 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2122 sb->s_blocksize = old_sb->s_blocksize;
2123 sb->s_maxbytes = old_sb->s_maxbytes;
2124
Trond Myklebust8a6e5de2009-09-23 14:36:37 -04002125 if (server->nfs_client->rpc_ops->version == 3) {
David Howells54ceac42006-08-22 20:06:13 -04002126 /* The VFS shouldn't apply the umask to mode bits. We will do
2127 * so ourselves when necessary.
2128 */
2129 sb->s_flags |= MS_POSIXACL;
2130 sb->s_time_gran = 1;
2131 }
2132
2133 sb->s_op = old_sb->s_op;
2134 nfs_initialise_sb(sb);
2135}
2136
Trond Myklebust275a5d22007-05-16 16:53:28 -04002137static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2138{
2139 const struct nfs_server *a = s->s_fs_info;
2140 const struct rpc_clnt *clnt_a = a->client;
2141 const struct rpc_clnt *clnt_b = b->client;
2142
2143 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2144 goto Ebusy;
2145 if (a->nfs_client != b->nfs_client)
2146 goto Ebusy;
2147 if (a->flags != b->flags)
2148 goto Ebusy;
2149 if (a->wsize != b->wsize)
2150 goto Ebusy;
2151 if (a->rsize != b->rsize)
2152 goto Ebusy;
2153 if (a->acregmin != b->acregmin)
2154 goto Ebusy;
2155 if (a->acregmax != b->acregmax)
2156 goto Ebusy;
2157 if (a->acdirmin != b->acdirmin)
2158 goto Ebusy;
2159 if (a->acdirmax != b->acdirmax)
2160 goto Ebusy;
2161 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2162 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002163 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04002164Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002165 return 0;
2166}
2167
2168struct nfs_sb_mountdata {
2169 struct nfs_server *server;
2170 int mntflags;
2171};
2172
2173static int nfs_set_super(struct super_block *s, void *data)
2174{
2175 struct nfs_sb_mountdata *sb_mntdata = data;
2176 struct nfs_server *server = sb_mntdata->server;
2177 int ret;
2178
2179 s->s_flags = sb_mntdata->mntflags;
2180 s->s_fs_info = server;
2181 ret = set_anon_super(s, server);
2182 if (ret == 0)
2183 server->s_dev = s->s_dev;
2184 return ret;
2185}
2186
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002187static int nfs_compare_super_address(struct nfs_server *server1,
2188 struct nfs_server *server2)
2189{
2190 struct sockaddr *sap1, *sap2;
2191
2192 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2193 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2194
2195 if (sap1->sa_family != sap2->sa_family)
2196 return 0;
2197
2198 switch (sap1->sa_family) {
2199 case AF_INET: {
2200 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2201 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2202 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2203 return 0;
2204 if (sin1->sin_port != sin2->sin_port)
2205 return 0;
2206 break;
2207 }
2208 case AF_INET6: {
2209 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2210 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2211 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2212 return 0;
2213 if (sin1->sin6_port != sin2->sin6_port)
2214 return 0;
2215 break;
2216 }
2217 default:
2218 return 0;
2219 }
2220
2221 return 1;
2222}
2223
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002224static int nfs_compare_super(struct super_block *sb, void *data)
2225{
2226 struct nfs_sb_mountdata *sb_mntdata = data;
2227 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2228 int mntflags = sb_mntdata->mntflags;
2229
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002230 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002231 return 0;
2232 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2233 if (old->flags & NFS_MOUNT_UNSHARED)
2234 return 0;
2235 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2236 return 0;
2237 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002238}
2239
Miklos Szeredifa799752008-04-30 00:54:33 -07002240static int nfs_bdi_register(struct nfs_server *server)
2241{
2242 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2243}
2244
David Howells54ceac42006-08-22 20:06:13 -04002245static int nfs_get_sb(struct file_system_type *fs_type,
2246 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2247{
2248 struct nfs_server *server = NULL;
2249 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002250 struct nfs_parsed_mount_data *data;
2251 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002252 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002253 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002254 struct nfs_sb_mountdata sb_mntdata = {
2255 .mntflags = flags,
2256 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002257 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002258
Trond Myklebustc5811db2009-10-06 15:40:15 -04002259 data = nfs_alloc_parsed_mount_data(3);
Trond Myklebustb157b062010-04-19 19:05:48 -04002260 mntfh = nfs_alloc_fhandle();
Trond Myklebust33852a12008-06-19 14:20:11 -04002261 if (data == NULL || mntfh == NULL)
2262 goto out_free_fh;
2263
2264 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002265
David Howells54ceac42006-08-22 20:06:13 -04002266 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002267 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002268 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002269 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002270
Chuck Lever764302c2009-09-08 19:50:03 -04002271#ifdef CONFIG_NFS_V4
2272 if (data->version == 4) {
2273 error = nfs4_try_mount(flags, dev_name, data, mnt);
2274 kfree(data->client_address);
Xiaotian Feng9699eda2010-04-22 18:56:17 +08002275 kfree(data->nfs_server.export_path);
Chuck Lever764302c2009-09-08 19:50:03 -04002276 goto out;
2277 }
2278#endif /* CONFIG_NFS_V4 */
2279
David Howells54ceac42006-08-22 20:06:13 -04002280 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002281 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002282 if (IS_ERR(server)) {
2283 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002284 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002285 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002286 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002287
Trond Myklebust75180df2007-05-16 16:53:28 -04002288 if (server->flags & NFS_MOUNT_UNSHARED)
2289 compare_super = NULL;
2290
David Howells54ceac42006-08-22 20:06:13 -04002291 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002292 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002293 if (IS_ERR(s)) {
2294 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002295 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002296 }
2297
David Howells54ceac42006-08-22 20:06:13 -04002298 if (s->s_fs_info != server) {
2299 nfs_free_server(server);
2300 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002301 } else {
2302 error = nfs_bdi_register(server);
2303 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002304 goto error_splat_bdi;
David Howellsf7b422b2006-06-09 09:34:33 -04002305 }
David Howells54ceac42006-08-22 20:06:13 -04002306
2307 if (!s->s_root) {
2308 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002309 nfs_fill_super(s, data);
David Howells2df54802009-09-23 14:36:39 -04002310 nfs_fscache_get_super_cookie(
2311 s, data ? data->fscache_uniq : NULL, NULL);
David Howells54ceac42006-08-22 20:06:13 -04002312 }
2313
Trond Myklebust33852a12008-06-19 14:20:11 -04002314 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002315 if (IS_ERR(mntroot)) {
2316 error = PTR_ERR(mntroot);
2317 goto error_splat_super;
2318 }
2319
Trond Myklebust33852a12008-06-19 14:20:11 -04002320 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002321 if (error)
2322 goto error_splat_root;
2323
David Howellsf7b422b2006-06-09 09:34:33 -04002324 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002325 mnt->mnt_sb = s;
2326 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002327 error = 0;
2328
2329out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002330 kfree(data->nfs_server.hostname);
2331 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002332 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002333 security_free_mnt_opts(&data->lsm_opts);
2334out_free_fh:
Trond Myklebustb157b062010-04-19 19:05:48 -04002335 nfs_free_fhandle(mntfh);
Trond Myklebust33852a12008-06-19 14:20:11 -04002336 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002337 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002338
David Howells54ceac42006-08-22 20:06:13 -04002339out_err_nosb:
2340 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002341 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002342
Eric Parisf9c3a382008-03-05 14:20:18 -05002343error_splat_root:
2344 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002345error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002346 if (server && !s->s_root)
2347 bdi_unregister(&server->backing_dev_info);
2348error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002349 deactivate_locked_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002350 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002351}
2352
David Howells54ceac42006-08-22 20:06:13 -04002353/*
Trond Myklebust387c1492010-02-03 08:27:35 -05002354 * Ensure that we unregister the bdi before kill_anon_super
2355 * releases the device name
2356 */
2357static void nfs_put_super(struct super_block *s)
2358{
2359 struct nfs_server *server = NFS_SB(s);
2360
2361 bdi_unregister(&server->backing_dev_info);
2362}
2363
2364/*
David Howells54ceac42006-08-22 20:06:13 -04002365 * Destroy an NFS2/3 superblock
2366 */
David Howellsf7b422b2006-06-09 09:34:33 -04002367static void nfs_kill_super(struct super_block *s)
2368{
2369 struct nfs_server *server = NFS_SB(s);
2370
2371 kill_anon_super(s);
David Howells08734042009-04-03 16:42:42 +01002372 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002373 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002374}
2375
David Howells54ceac42006-08-22 20:06:13 -04002376/*
2377 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2378 */
2379static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2380 const char *dev_name, void *raw_data,
2381 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002382{
2383 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002384 struct super_block *s;
2385 struct nfs_server *server;
2386 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002387 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002388 struct nfs_sb_mountdata sb_mntdata = {
2389 .mntflags = flags,
2390 };
David Howells54ceac42006-08-22 20:06:13 -04002391 int error;
2392
2393 dprintk("--> nfs_xdev_get_sb()\n");
2394
2395 /* create a new volume representation */
2396 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2397 if (IS_ERR(server)) {
2398 error = PTR_ERR(server);
2399 goto out_err_noserver;
2400 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002401 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002402
Trond Myklebust75180df2007-05-16 16:53:28 -04002403 if (server->flags & NFS_MOUNT_UNSHARED)
2404 compare_super = NULL;
2405
David Howells54ceac42006-08-22 20:06:13 -04002406 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002407 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002408 if (IS_ERR(s)) {
2409 error = PTR_ERR(s);
2410 goto out_err_nosb;
2411 }
2412
2413 if (s->s_fs_info != server) {
2414 nfs_free_server(server);
2415 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002416 } else {
2417 error = nfs_bdi_register(server);
2418 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002419 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002420 }
2421
2422 if (!s->s_root) {
2423 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002424 nfs_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002425 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002426 }
2427
2428 mntroot = nfs_get_root(s, data->fh);
2429 if (IS_ERR(mntroot)) {
2430 error = PTR_ERR(mntroot);
2431 goto error_splat_super;
2432 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002433 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002434 dput(mntroot);
2435 error = -ESTALE;
2436 goto error_splat_super;
2437 }
David Howells54ceac42006-08-22 20:06:13 -04002438
2439 s->s_flags |= MS_ACTIVE;
2440 mnt->mnt_sb = s;
2441 mnt->mnt_root = mntroot;
2442
Eric Parisf9c3a382008-03-05 14:20:18 -05002443 /* clone any lsm security options from the parent to the new sb */
2444 security_sb_clone_mnt_opts(data->sb, s);
2445
David Howells54ceac42006-08-22 20:06:13 -04002446 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2447 return 0;
2448
2449out_err_nosb:
2450 nfs_free_server(server);
2451out_err_noserver:
2452 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2453 return error;
2454
2455error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002456 if (server && !s->s_root)
2457 bdi_unregister(&server->backing_dev_info);
2458error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002459 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002460 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2461 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002462}
2463
2464#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002465
2466/*
2467 * Finish setting up a cloned NFS4 superblock
2468 */
2469static void nfs4_clone_super(struct super_block *sb,
2470 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002471{
David Howells54ceac42006-08-22 20:06:13 -04002472 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2473 sb->s_blocksize = old_sb->s_blocksize;
2474 sb->s_maxbytes = old_sb->s_maxbytes;
2475 sb->s_time_gran = 1;
2476 sb->s_op = old_sb->s_op;
2477 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002478}
2479
2480/*
2481 * Set up an NFS4 superblock
2482 */
David Howells54ceac42006-08-22 20:06:13 -04002483static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002484{
David Howellsf7b422b2006-06-09 09:34:33 -04002485 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002486 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002487 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002488}
2489
Trond Myklebust01c3f052009-06-17 13:22:58 -07002490static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2491{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -04002492 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2493 NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
Trond Myklebust01c3f052009-06-17 13:22:58 -07002494}
2495
Chuck Lever7630c852009-09-08 19:49:57 -04002496static int nfs4_validate_text_mount_data(void *options,
2497 struct nfs_parsed_mount_data *args,
2498 const char *dev_name)
2499{
2500 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2501
Trond Myklebustf5855fe2009-10-06 15:40:37 -04002502 nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
Chuck Lever7630c852009-09-08 19:49:57 -04002503
2504 nfs_validate_transport_protocol(args);
2505
2506 nfs4_validate_mount_flags(args);
2507
Trond Myklebust2ecda722009-09-08 19:50:07 -04002508 if (args->version != 4) {
2509 dfprintk(MOUNT,
2510 "NFS4: Illegal mount version\n");
2511 return -EINVAL;
2512 }
2513
Chuck Lever7630c852009-09-08 19:49:57 -04002514 if (args->auth_flavor_len > 1) {
2515 dfprintk(MOUNT,
2516 "NFS4: Too many RPC auth flavours specified\n");
2517 return -EINVAL;
2518 }
2519
2520 if (args->client_address == NULL) {
2521 dfprintk(MOUNT,
2522 "NFS4: mount program didn't pass callback address\n");
2523 return -EINVAL;
2524 }
2525
2526 return nfs_parse_devname(dev_name,
2527 &args->nfs_server.hostname,
2528 NFS4_MAXNAMLEN,
2529 &args->nfs_server.export_path,
2530 NFS4_MAXPATHLEN);
2531}
2532
David Howells54ceac42006-08-22 20:06:13 -04002533/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002534 * Validate NFSv4 mount options
2535 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002536static int nfs4_validate_mount_data(void *options,
2537 struct nfs_parsed_mount_data *args,
2538 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002539{
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002540 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002541 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002542 char *c;
2543
2544 if (data == NULL)
2545 goto out_no_data;
2546
2547 switch (data->version) {
2548 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002549 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002550 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002551 if (data->host_addrlen == 0)
2552 goto out_no_address;
2553 args->nfs_server.addrlen = data->host_addrlen;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002554 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002555 return -EFAULT;
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002556 if (!nfs_verify_server_address(sap))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002557 goto out_no_address;
2558
Chuck Lever6738b252008-06-24 16:33:54 -04002559 if (data->auth_flavourlen) {
2560 if (data->auth_flavourlen > 1)
2561 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002562 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002563 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002564 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002565 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002566 }
2567
2568 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2569 if (IS_ERR(c))
2570 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002571 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002572
2573 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2574 if (IS_ERR(c))
2575 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002576 args->nfs_server.export_path = c;
2577 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002578
2579 c = strndup_user(data->client_addr.data, 16);
2580 if (IS_ERR(c))
2581 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002582 args->client_address = c;
2583
2584 /*
2585 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2586 * can deal with.
2587 */
2588
2589 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2590 args->rsize = data->rsize;
2591 args->wsize = data->wsize;
2592 args->timeo = data->timeo;
2593 args->retrans = data->retrans;
2594 args->acregmin = data->acregmin;
2595 args->acregmax = data->acregmax;
2596 args->acdirmin = data->acdirmin;
2597 args->acdirmax = data->acdirmax;
2598 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002599 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002600
2601 break;
Chuck Lever7630c852009-09-08 19:49:57 -04002602 default:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002603 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002604 return -EINVAL;
2605
Chuck Lever4cfd74f2009-09-08 19:49:47 -04002606 if (!nfs_verify_server_address(sap))
Chuck Lever80071222007-07-01 12:13:59 -04002607 return -EINVAL;
Chuck Levered596a82008-06-26 17:47:05 -04002608
Chuck Lever7630c852009-09-08 19:49:57 -04002609 return nfs4_validate_text_mount_data(options, args, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002610 }
2611
2612 return 0;
2613
2614out_no_data:
2615 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2616 return -EINVAL;
2617
2618out_inval_auth:
2619 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2620 data->auth_flavourlen);
2621 return -EINVAL;
2622
2623out_no_address:
2624 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2625 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002626}
2627
2628/*
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002629 * Get the superblock for the NFS4 root partition
David Howells54ceac42006-08-22 20:06:13 -04002630 */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002631static int nfs4_remote_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -04002632 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002633{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002634 struct nfs_parsed_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002635 struct super_block *s;
2636 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002637 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002638 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002639 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002640 struct nfs_sb_mountdata sb_mntdata = {
2641 .mntflags = flags,
2642 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002643 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002644
Trond Myklebustb157b062010-04-19 19:05:48 -04002645 mntfh = nfs_alloc_fhandle();
Trond Myklebust33852a12008-06-19 14:20:11 -04002646 if (data == NULL || mntfh == NULL)
2647 goto out_free_fh;
2648
2649 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002650
David Howells54ceac42006-08-22 20:06:13 -04002651 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002652 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002653 if (IS_ERR(server)) {
2654 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002655 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002656 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002657 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002658
Trond Myklebust75180df2007-05-16 16:53:28 -04002659 if (server->flags & NFS4_MOUNT_UNSHARED)
2660 compare_super = NULL;
2661
David Howells54ceac42006-08-22 20:06:13 -04002662 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002663 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002664 if (IS_ERR(s)) {
2665 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002666 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002667 }
2668
Trond Myklebust5dd31772006-08-24 01:03:05 -04002669 if (s->s_fs_info != server) {
2670 nfs_free_server(server);
2671 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002672 } else {
2673 error = nfs_bdi_register(server);
2674 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002675 goto error_splat_bdi;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002676 }
2677
David Howells54ceac42006-08-22 20:06:13 -04002678 if (!s->s_root) {
2679 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002680 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04002681 nfs_fscache_get_super_cookie(
2682 s, data ? data->fscache_uniq : NULL, NULL);
Trond Myklebust816724e2006-06-24 08:41:41 -04002683 }
David Howellsf7b422b2006-06-09 09:34:33 -04002684
Trond Myklebust33852a12008-06-19 14:20:11 -04002685 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002686 if (IS_ERR(mntroot)) {
2687 error = PTR_ERR(mntroot);
2688 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002689 }
David Howells54ceac42006-08-22 20:06:13 -04002690
Trond Myklebust33852a12008-06-19 14:20:11 -04002691 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002692 if (error)
2693 goto error_splat_root;
2694
David Howellsf7b422b2006-06-09 09:34:33 -04002695 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002696 mnt->mnt_sb = s;
2697 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002698 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002699
Chuck Lever29eb9812007-07-01 12:12:30 -04002700out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002701 security_free_mnt_opts(&data->lsm_opts);
2702out_free_fh:
Trond Myklebustb157b062010-04-19 19:05:48 -04002703 nfs_free_fhandle(mntfh);
Trond Myklebust816724e2006-06-24 08:41:41 -04002704 return error;
David Howells54ceac42006-08-22 20:06:13 -04002705
Chuck Lever29eb9812007-07-01 12:12:30 -04002706out_free:
2707 nfs_free_server(server);
2708 goto out;
2709
Eric Paris46c8ac72008-05-02 13:42:42 -07002710error_splat_root:
2711 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002712error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11002713 if (server && !s->s_root)
2714 bdi_unregister(&server->backing_dev_info);
2715error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04002716 deactivate_locked_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002717 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002718}
2719
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002720static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2721 int flags, void *data, const char *hostname)
2722{
2723 struct vfsmount *root_mnt;
2724 char *root_devname;
2725 size_t len;
2726
2727 len = strlen(hostname) + 3;
2728 root_devname = kmalloc(len, GFP_KERNEL);
2729 if (root_devname == NULL)
2730 return ERR_PTR(-ENOMEM);
2731 snprintf(root_devname, len, "%s:/", hostname);
2732 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2733 kfree(root_devname);
2734 return root_mnt;
2735}
2736
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002737static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2738{
2739 char *page = (char *) __get_free_page(GFP_KERNEL);
2740 char *devname, *tmp;
2741
2742 if (page == NULL)
2743 return;
2744 devname = nfs_path(path->mnt->mnt_devname,
2745 path->mnt->mnt_root, path->dentry,
2746 page, PAGE_SIZE);
Dan Carpentercdd29ec2010-04-22 15:35:56 -04002747 if (IS_ERR(devname))
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002748 goto out_freepage;
2749 tmp = kstrdup(devname, GFP_KERNEL);
2750 if (tmp == NULL)
2751 goto out_freepage;
2752 kfree(mnt->mnt_devname);
2753 mnt->mnt_devname = tmp;
2754out_freepage:
2755 free_page((unsigned long)page);
2756}
2757
Trond Myklebustce587e02010-04-16 16:22:52 -04002758struct nfs_referral_count {
2759 struct list_head list;
2760 const struct task_struct *task;
2761 unsigned int referral_count;
2762};
2763
2764static LIST_HEAD(nfs_referral_count_list);
2765static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
2766
2767static struct nfs_referral_count *nfs_find_referral_count(void)
2768{
2769 struct nfs_referral_count *p;
2770
2771 list_for_each_entry(p, &nfs_referral_count_list, list) {
2772 if (p->task == current)
2773 return p;
2774 }
2775 return NULL;
2776}
2777
2778#define NFS_MAX_NESTED_REFERRALS 2
2779
2780static int nfs_referral_loop_protect(void)
2781{
2782 struct nfs_referral_count *p, *new;
2783 int ret = -ENOMEM;
2784
2785 new = kmalloc(sizeof(*new), GFP_KERNEL);
2786 if (!new)
2787 goto out;
2788 new->task = current;
2789 new->referral_count = 1;
2790
2791 ret = 0;
2792 spin_lock(&nfs_referral_count_list_lock);
2793 p = nfs_find_referral_count();
2794 if (p != NULL) {
2795 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
2796 ret = -ELOOP;
2797 else
2798 p->referral_count++;
2799 } else {
2800 list_add(&new->list, &nfs_referral_count_list);
2801 new = NULL;
2802 }
2803 spin_unlock(&nfs_referral_count_list_lock);
2804 kfree(new);
2805out:
2806 return ret;
2807}
2808
2809static void nfs_referral_loop_unprotect(void)
2810{
2811 struct nfs_referral_count *p;
2812
2813 spin_lock(&nfs_referral_count_list_lock);
2814 p = nfs_find_referral_count();
2815 p->referral_count--;
2816 if (p->referral_count == 0)
2817 list_del(&p->list);
2818 else
2819 p = NULL;
2820 spin_unlock(&nfs_referral_count_list_lock);
2821 kfree(p);
2822}
2823
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002824static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2825 const char *export_path, struct vfsmount *mnt_target)
2826{
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002827 struct nameidata *nd = NULL;
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002828 struct mnt_namespace *ns_private;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002829 struct super_block *s;
2830 int ret;
2831
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002832 nd = kmalloc(sizeof(*nd), GFP_KERNEL);
2833 if (nd == NULL)
2834 return -ENOMEM;
2835
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002836 ns_private = create_mnt_ns(root_mnt);
2837 ret = PTR_ERR(ns_private);
2838 if (IS_ERR(ns_private))
2839 goto out_mntput;
2840
Trond Myklebustce587e02010-04-16 16:22:52 -04002841 ret = nfs_referral_loop_protect();
2842 if (ret != 0)
2843 goto out_put_mnt_ns;
2844
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002845 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002846 export_path, LOOKUP_FOLLOW, nd);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002847
Trond Myklebustce587e02010-04-16 16:22:52 -04002848 nfs_referral_loop_unprotect();
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002849 put_mnt_ns(ns_private);
2850
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002851 if (ret != 0)
2852 goto out_err;
2853
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002854 s = nd->path.mnt->mnt_sb;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002855 atomic_inc(&s->s_active);
2856 mnt_target->mnt_sb = s;
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002857 mnt_target->mnt_root = dget(nd->path.dentry);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002858
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002859 /* Correct the device pathname */
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002860 nfs_fix_devname(&nd->path, mnt_target);
Trond Myklebustb88f8a52009-06-22 15:09:14 -04002861
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002862 path_put(&nd->path);
2863 kfree(nd);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002864 down_write(&s->s_umount);
2865 return 0;
Trond Myklebustce587e02010-04-16 16:22:52 -04002866out_put_mnt_ns:
2867 put_mnt_ns(ns_private);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002868out_mntput:
2869 mntput(root_mnt);
2870out_err:
Trond Myklebust04ffdbe2010-04-16 16:22:48 -04002871 kfree(nd);
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002872 return ret;
2873}
2874
Chuck Levera6fe23b2009-09-08 19:50:00 -04002875static int nfs4_try_mount(int flags, const char *dev_name,
2876 struct nfs_parsed_mount_data *data,
2877 struct vfsmount *mnt)
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002878{
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002879 char *export_path;
2880 struct vfsmount *root_mnt;
Chuck Levera6fe23b2009-09-08 19:50:00 -04002881 int error;
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002882
Chuck Levera6fe23b2009-09-08 19:50:00 -04002883 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002884
2885 export_path = data->nfs_server.export_path;
2886 data->nfs_server.export_path = "/";
2887 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2888 data->nfs_server.hostname);
2889 data->nfs_server.export_path = export_path;
2890
2891 error = PTR_ERR(root_mnt);
2892 if (IS_ERR(root_mnt))
2893 goto out;
2894
2895 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2896
2897out:
Chuck Levera6fe23b2009-09-08 19:50:00 -04002898 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2899 error != 0 ? " [error]" : "");
2900 return error;
2901}
2902
2903/*
2904 * Get the superblock for an NFS4 mountpoint
2905 */
2906static int nfs4_get_sb(struct file_system_type *fs_type,
2907 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2908{
2909 struct nfs_parsed_mount_data *data;
2910 int error = -ENOMEM;
2911
Trond Myklebustc5811db2009-10-06 15:40:15 -04002912 data = nfs_alloc_parsed_mount_data(4);
Chuck Levera6fe23b2009-09-08 19:50:00 -04002913 if (data == NULL)
2914 goto out_free_data;
2915
2916 /* Validate the mount data */
2917 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2918 if (error < 0)
2919 goto out;
2920
2921 error = nfs4_try_mount(flags, dev_name, data, mnt);
2922
2923out:
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04002924 kfree(data->client_address);
2925 kfree(data->nfs_server.export_path);
2926 kfree(data->nfs_server.hostname);
2927 kfree(data->fscache_uniq);
2928out_free_data:
2929 kfree(data);
2930 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2931 error != 0 ? " [error]" : "");
2932 return error;
2933}
2934
David Howellsf7b422b2006-06-09 09:34:33 -04002935static void nfs4_kill_super(struct super_block *sb)
2936{
2937 struct nfs_server *server = NFS_SB(sb);
2938
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002939 dprintk("--> %s\n", __func__);
Trond Myklebust515d8612008-12-23 15:21:46 -05002940 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002941 kill_anon_super(sb);
David Howells08734042009-04-03 16:42:42 +01002942 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002943 nfs_free_server(server);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04002944 dprintk("<-- %s\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -04002945}
2946
2947/*
David Howells54ceac42006-08-22 20:06:13 -04002948 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002949 */
David Howells54ceac42006-08-22 20:06:13 -04002950static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2951 const char *dev_name, void *raw_data,
2952 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002953{
2954 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002955 struct super_block *s;
2956 struct nfs_server *server;
2957 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002958 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002959 struct nfs_sb_mountdata sb_mntdata = {
2960 .mntflags = flags,
2961 };
David Howells54ceac42006-08-22 20:06:13 -04002962 int error;
2963
2964 dprintk("--> nfs4_xdev_get_sb()\n");
2965
2966 /* create a new volume representation */
2967 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2968 if (IS_ERR(server)) {
2969 error = PTR_ERR(server);
2970 goto out_err_noserver;
2971 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002972 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002973
Trond Myklebust75180df2007-05-16 16:53:28 -04002974 if (server->flags & NFS4_MOUNT_UNSHARED)
2975 compare_super = NULL;
2976
David Howells54ceac42006-08-22 20:06:13 -04002977 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002978 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002979 if (IS_ERR(s)) {
2980 error = PTR_ERR(s);
2981 goto out_err_nosb;
2982 }
2983
2984 if (s->s_fs_info != server) {
2985 nfs_free_server(server);
2986 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002987 } else {
2988 error = nfs_bdi_register(server);
2989 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11002990 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04002991 }
2992
2993 if (!s->s_root) {
2994 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002995 nfs4_clone_super(s, data->sb);
David Howells2df54802009-09-23 14:36:39 -04002996 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04002997 }
2998
2999 mntroot = nfs4_get_root(s, data->fh);
3000 if (IS_ERR(mntroot)) {
3001 error = PTR_ERR(mntroot);
3002 goto error_splat_super;
3003 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05003004 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3005 dput(mntroot);
3006 error = -ESTALE;
3007 goto error_splat_super;
3008 }
David Howells54ceac42006-08-22 20:06:13 -04003009
3010 s->s_flags |= MS_ACTIVE;
3011 mnt->mnt_sb = s;
3012 mnt->mnt_root = mntroot;
3013
Eric Paris46c8ac72008-05-02 13:42:42 -07003014 security_sb_clone_mnt_opts(data->sb, s);
3015
David Howells54ceac42006-08-22 20:06:13 -04003016 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
3017 return 0;
3018
3019out_err_nosb:
3020 nfs_free_server(server);
3021out_err_noserver:
3022 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
3023 return error;
3024
3025error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11003026 if (server && !s->s_root)
3027 bdi_unregister(&server->backing_dev_info);
3028error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04003029 deactivate_locked_super(s);
David Howells54ceac42006-08-22 20:06:13 -04003030 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
3031 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04003032}
3033
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04003034static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
3035 int flags, const char *dev_name, void *raw_data,
3036 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04003037{
3038 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04003039 struct super_block *s;
3040 struct nfs_server *server;
3041 struct dentry *mntroot;
Trond Myklebust4f727292010-04-16 16:22:48 -04003042 struct nfs_fh *mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04003043 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04003044 struct nfs_sb_mountdata sb_mntdata = {
3045 .mntflags = flags,
3046 };
Trond Myklebust4f727292010-04-16 16:22:48 -04003047 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04003048
3049 dprintk("--> nfs4_referral_get_sb()\n");
3050
Trond Myklebust4f727292010-04-16 16:22:48 -04003051 mntfh = nfs_alloc_fhandle();
3052 if (mntfh == NULL)
3053 goto out_err_nofh;
3054
David Howells54ceac42006-08-22 20:06:13 -04003055 /* create a new volume representation */
Trond Myklebust4f727292010-04-16 16:22:48 -04003056 server = nfs4_create_referral_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003057 if (IS_ERR(server)) {
3058 error = PTR_ERR(server);
3059 goto out_err_noserver;
3060 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04003061 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04003062
Trond Myklebust75180df2007-05-16 16:53:28 -04003063 if (server->flags & NFS4_MOUNT_UNSHARED)
3064 compare_super = NULL;
3065
David Howells54ceac42006-08-22 20:06:13 -04003066 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04003067 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04003068 if (IS_ERR(s)) {
3069 error = PTR_ERR(s);
3070 goto out_err_nosb;
3071 }
3072
3073 if (s->s_fs_info != server) {
3074 nfs_free_server(server);
3075 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07003076 } else {
3077 error = nfs_bdi_register(server);
3078 if (error)
NeilBrowncfbc0682010-03-11 11:20:17 +11003079 goto error_splat_bdi;
David Howells54ceac42006-08-22 20:06:13 -04003080 }
3081
3082 if (!s->s_root) {
3083 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04003084 nfs4_fill_super(s);
David Howells2df54802009-09-23 14:36:39 -04003085 nfs_fscache_get_super_cookie(s, NULL, data);
David Howells54ceac42006-08-22 20:06:13 -04003086 }
3087
Trond Myklebust4f727292010-04-16 16:22:48 -04003088 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003089 if (IS_ERR(mntroot)) {
3090 error = PTR_ERR(mntroot);
3091 goto error_splat_super;
3092 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05003093 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3094 dput(mntroot);
3095 error = -ESTALE;
3096 goto error_splat_super;
3097 }
David Howells54ceac42006-08-22 20:06:13 -04003098
3099 s->s_flags |= MS_ACTIVE;
3100 mnt->mnt_sb = s;
3101 mnt->mnt_root = mntroot;
3102
Eric Paris46c8ac72008-05-02 13:42:42 -07003103 security_sb_clone_mnt_opts(data->sb, s);
3104
Trond Myklebust4f727292010-04-16 16:22:48 -04003105 nfs_free_fhandle(mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003106 dprintk("<-- nfs4_referral_get_sb() = 0\n");
3107 return 0;
3108
3109out_err_nosb:
3110 nfs_free_server(server);
3111out_err_noserver:
Trond Myklebust4f727292010-04-16 16:22:48 -04003112 nfs_free_fhandle(mntfh);
3113out_err_nofh:
David Howells54ceac42006-08-22 20:06:13 -04003114 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
3115 return error;
3116
3117error_splat_super:
NeilBrowncfbc0682010-03-11 11:20:17 +11003118 if (server && !s->s_root)
3119 bdi_unregister(&server->backing_dev_info);
3120error_splat_bdi:
Al Viro6f5bbff2009-05-06 01:34:22 -04003121 deactivate_locked_super(s);
Trond Myklebust4f727292010-04-16 16:22:48 -04003122 nfs_free_fhandle(mntfh);
David Howells54ceac42006-08-22 20:06:13 -04003123 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
3124 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04003125}
3126
Trond Myklebustc02d7ad2009-06-22 15:09:14 -04003127/*
3128 * Create an NFS4 server record on referral traversal
3129 */
3130static int nfs4_referral_get_sb(struct file_system_type *fs_type,
3131 int flags, const char *dev_name, void *raw_data,
3132 struct vfsmount *mnt)
3133{
3134 struct nfs_clone_mount *data = raw_data;
3135 char *export_path;
3136 struct vfsmount *root_mnt;
3137 int error;
3138
3139 dprintk("--> nfs4_referral_get_sb()\n");
3140
3141 export_path = data->mnt_path;
3142 data->mnt_path = "/";
3143
3144 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
3145 flags, data, data->hostname);
3146 data->mnt_path = export_path;
3147
3148 error = PTR_ERR(root_mnt);
3149 if (IS_ERR(root_mnt))
3150 goto out;
3151
3152 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
3153out:
3154 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
3155 error != 0 ? " [error]" : "");
3156 return error;
3157}
3158
David Howells54ceac42006-08-22 20:06:13 -04003159#endif /* CONFIG_NFS_V4 */