blob: 82eaadbff40872f6fbae90e52a8acf28bca52d7c [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>
45#include <linux/nfs_idmap.h>
46#include <linux/vfs.h>
47#include <linux/inet.h>
Chuck Leverfd00a8f2007-12-10 14:57:38 -050048#include <linux/in6.h>
49#include <net/ipv6.h>
Chuck Leverd8e77482008-06-23 12:37:01 -040050#include <linux/netdevice.h>
David Howellsf7b422b2006-06-09 09:34:33 -040051#include <linux/nfs_xdr.h>
Adrian Bunkb5d5dfb2007-02-12 00:53:40 -080052#include <linux/magic.h>
Chuck Leverbf0fd762007-07-01 12:13:44 -040053#include <linux/parser.h>
David Howellsf7b422b2006-06-09 09:34:33 -040054
55#include <asm/system.h>
56#include <asm/uaccess.h>
57
58#include "nfs4_fs.h"
59#include "callback.h"
60#include "delegation.h"
61#include "iostat.h"
62#include "internal.h"
David Howells08734042009-04-03 16:42:42 +010063#include "fscache.h"
David Howellsf7b422b2006-06-09 09:34:33 -040064
65#define NFSDBG_FACILITY NFSDBG_VFS
66
Chuck Leverbf0fd762007-07-01 12:13:44 -040067enum {
68 /* Mount options that take no arguments */
69 Opt_soft, Opt_hard,
Chuck Leverbf0fd762007-07-01 12:13:44 -040070 Opt_posix, Opt_noposix,
71 Opt_cto, Opt_nocto,
72 Opt_ac, Opt_noac,
73 Opt_lock, Opt_nolock,
74 Opt_v2, Opt_v3,
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040075 Opt_udp, Opt_tcp, Opt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -040076 Opt_acl, Opt_noacl,
77 Opt_rdirplus, Opt_nordirplus,
Trond Myklebust75180df2007-05-16 16:53:28 -040078 Opt_sharecache, Opt_nosharecache,
Chuck Leverd7403512008-12-23 15:21:37 -050079 Opt_resvport, Opt_noresvport,
David Howellsb797cac2009-04-03 16:42:48 +010080 Opt_fscache, Opt_nofscache,
Chuck Leverbf0fd762007-07-01 12:13:44 -040081
82 /* Mount options that take integer arguments */
83 Opt_port,
84 Opt_rsize, Opt_wsize, Opt_bsize,
85 Opt_timeo, Opt_retrans,
86 Opt_acregmin, Opt_acregmax,
87 Opt_acdirmin, Opt_acdirmax,
88 Opt_actimeo,
89 Opt_namelen,
90 Opt_mountport,
Chuck Levere887cbc2007-10-26 13:32:29 -040091 Opt_mountvers,
Chuck Leverad879ce2007-10-26 13:32:24 -040092 Opt_nfsvers,
Chuck Leverbf0fd762007-07-01 12:13:44 -040093
94 /* Mount options that take string arguments */
Chuck Lever33832032007-12-10 14:59:13 -050095 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
Chuck Lever0ac83772007-09-11 18:01:04 -040096 Opt_addr, Opt_mountaddr, Opt_clientaddr,
Trond Myklebust7973c1f2008-07-15 17:58:14 -040097 Opt_lookupcache,
David Howellsb797cac2009-04-03 16:42:48 +010098 Opt_fscache_uniq,
Chuck Leverbf0fd762007-07-01 12:13:44 -040099
Chuck Leverf45663c2008-06-24 19:28:02 -0400100 /* Special mount options */
101 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400102
103 Opt_err
104};
105
Steven Whitehousea447c092008-10-13 10:46:57 +0100106static const match_table_t nfs_mount_option_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400107 { Opt_userspace, "bg" },
108 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400109 { Opt_userspace, "retry=%s" },
110
Chuck Leverf45663c2008-06-24 19:28:02 -0400111 { Opt_sloppy, "sloppy" },
112
Chuck Leverbf0fd762007-07-01 12:13:44 -0400113 { Opt_soft, "soft" },
114 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400115 { Opt_deprecated, "intr" },
116 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400117 { Opt_posix, "posix" },
118 { Opt_noposix, "noposix" },
119 { Opt_cto, "cto" },
120 { Opt_nocto, "nocto" },
121 { Opt_ac, "ac" },
122 { Opt_noac, "noac" },
123 { Opt_lock, "lock" },
124 { Opt_nolock, "nolock" },
125 { Opt_v2, "v2" },
126 { Opt_v3, "v3" },
127 { Opt_udp, "udp" },
128 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400129 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400130 { Opt_acl, "acl" },
131 { Opt_noacl, "noacl" },
132 { Opt_rdirplus, "rdirplus" },
133 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400134 { Opt_sharecache, "sharecache" },
135 { Opt_nosharecache, "nosharecache" },
Chuck Leverd7403512008-12-23 15:21:37 -0500136 { Opt_resvport, "resvport" },
137 { Opt_noresvport, "noresvport" },
David Howellsb797cac2009-04-03 16:42:48 +0100138 { Opt_fscache, "fsc" },
139 { Opt_fscache_uniq, "fsc=%s" },
140 { Opt_nofscache, "nofsc" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400141
142 { Opt_port, "port=%u" },
143 { Opt_rsize, "rsize=%u" },
144 { Opt_wsize, "wsize=%u" },
145 { Opt_bsize, "bsize=%u" },
146 { Opt_timeo, "timeo=%u" },
147 { Opt_retrans, "retrans=%u" },
148 { Opt_acregmin, "acregmin=%u" },
149 { Opt_acregmax, "acregmax=%u" },
150 { Opt_acdirmin, "acdirmin=%u" },
151 { Opt_acdirmax, "acdirmax=%u" },
152 { Opt_actimeo, "actimeo=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400153 { Opt_namelen, "namlen=%u" },
154 { Opt_mountport, "mountport=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400155 { Opt_mountvers, "mountvers=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400156 { Opt_nfsvers, "nfsvers=%u" },
157 { Opt_nfsvers, "vers=%u" },
158
159 { Opt_sec, "sec=%s" },
160 { Opt_proto, "proto=%s" },
161 { Opt_mountproto, "mountproto=%s" },
162 { Opt_addr, "addr=%s" },
163 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500164 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400165 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400166
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400167 { Opt_lookupcache, "lookupcache=%s" },
168
Chuck Leverbf0fd762007-07-01 12:13:44 -0400169 { Opt_err, NULL }
170};
171
172enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400173 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400174
175 Opt_xprt_err
176};
177
Steven Whitehousea447c092008-10-13 10:46:57 +0100178static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400179 { Opt_xprt_udp, "udp" },
180 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400181 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400182
183 { Opt_xprt_err, NULL }
184};
185
186enum {
187 Opt_sec_none, Opt_sec_sys,
188 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
189 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
190 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
191
192 Opt_sec_err
193};
194
Steven Whitehousea447c092008-10-13 10:46:57 +0100195static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400196 { Opt_sec_none, "none" },
197 { Opt_sec_none, "null" },
198 { Opt_sec_sys, "sys" },
199
200 { Opt_sec_krb5, "krb5" },
201 { Opt_sec_krb5i, "krb5i" },
202 { Opt_sec_krb5p, "krb5p" },
203
204 { Opt_sec_lkey, "lkey" },
205 { Opt_sec_lkeyi, "lkeyi" },
206 { Opt_sec_lkeyp, "lkeyp" },
207
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500208 { Opt_sec_spkm, "spkm3" },
209 { Opt_sec_spkmi, "spkm3i" },
210 { Opt_sec_spkmp, "spkm3p" },
211
Chuck Leverbf0fd762007-07-01 12:13:44 -0400212 { Opt_sec_err, NULL }
213};
214
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400215enum {
216 Opt_lookupcache_all, Opt_lookupcache_positive,
217 Opt_lookupcache_none,
218
219 Opt_lookupcache_err
220};
221
222static match_table_t nfs_lookupcache_tokens = {
223 { Opt_lookupcache_all, "all" },
224 { Opt_lookupcache_positive, "pos" },
225 { Opt_lookupcache_positive, "positive" },
226 { Opt_lookupcache_none, "none" },
227
228 { Opt_lookupcache_err, NULL }
229};
230
Chuck Leverbf0fd762007-07-01 12:13:44 -0400231
Al Viro42faad92008-04-24 07:21:56 -0400232static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400233static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400234static int nfs_show_options(struct seq_file *, struct vfsmount *);
235static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400236static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400237static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400238 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400239static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400240static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400241
242static struct file_system_type nfs_fs_type = {
243 .owner = THIS_MODULE,
244 .name = "nfs",
245 .get_sb = nfs_get_sb,
246 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700247 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400248};
249
David Howells54ceac42006-08-22 20:06:13 -0400250struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400251 .owner = THIS_MODULE,
252 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400253 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400254 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700255 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400256};
257
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800258static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400259 .alloc_inode = nfs_alloc_inode,
260 .destroy_inode = nfs_destroy_inode,
261 .write_inode = nfs_write_inode,
262 .statfs = nfs_statfs,
263 .clear_inode = nfs_clear_inode,
264 .umount_begin = nfs_umount_begin,
265 .show_options = nfs_show_options,
266 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400267 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400268};
269
270#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400271static int nfs4_get_sb(struct file_system_type *fs_type,
272 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400273static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
274 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
275static int nfs4_referral_get_sb(struct file_system_type *fs_type,
276 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400277static void nfs4_kill_super(struct super_block *sb);
278
279static struct file_system_type nfs4_fs_type = {
280 .owner = THIS_MODULE,
281 .name = "nfs4",
282 .get_sb = nfs4_get_sb,
283 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700284 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400285};
286
David Howells54ceac42006-08-22 20:06:13 -0400287struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400288 .owner = THIS_MODULE,
289 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400290 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400291 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700292 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400293};
294
David Howells54ceac42006-08-22 20:06:13 -0400295struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400296 .owner = THIS_MODULE,
297 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400298 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400299 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700300 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400301};
302
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800303static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400304 .alloc_inode = nfs_alloc_inode,
305 .destroy_inode = nfs_destroy_inode,
306 .write_inode = nfs_write_inode,
307 .statfs = nfs_statfs,
308 .clear_inode = nfs4_clear_inode,
309 .umount_begin = nfs_umount_begin,
310 .show_options = nfs_show_options,
311 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400312 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400313};
314#endif
315
Rusty Russell8e1f9362007-07-17 04:03:17 -0700316static struct shrinker acl_shrinker = {
317 .shrink = nfs_access_cache_shrinker,
318 .seeks = DEFAULT_SEEKS,
319};
Trond Myklebust979df722006-07-25 11:28:19 -0400320
David Howellsf7b422b2006-06-09 09:34:33 -0400321/*
322 * Register the NFS filesystems
323 */
324int __init register_nfs_fs(void)
325{
326 int ret;
327
328 ret = register_filesystem(&nfs_fs_type);
329 if (ret < 0)
330 goto error_0;
331
David Howellsf7b422b2006-06-09 09:34:33 -0400332 ret = nfs_register_sysctl();
333 if (ret < 0)
334 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800335#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400336 ret = register_filesystem(&nfs4_fs_type);
337 if (ret < 0)
338 goto error_2;
339#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700340 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400341 return 0;
342
343#ifdef CONFIG_NFS_V4
344error_2:
345 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800346#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400347error_1:
348 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400349error_0:
350 return ret;
351}
352
353/*
354 * Unregister the NFS filesystems
355 */
356void __exit unregister_nfs_fs(void)
357{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700358 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400359#ifdef CONFIG_NFS_V4
360 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400361#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700362 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400363 unregister_filesystem(&nfs_fs_type);
364}
365
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400366void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500367{
368 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400369
370 if (atomic_inc_return(&server->active) == 1)
371 atomic_inc(&sb->s_active);
372}
373
374void nfs_sb_deactive(struct super_block *sb)
375{
376 struct nfs_server *server = NFS_SB(sb);
377
378 if (atomic_dec_and_test(&server->active))
379 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500380}
381
David Howellsf7b422b2006-06-09 09:34:33 -0400382/*
383 * Deliver file system statistics to userspace
384 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400385static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400386{
David Howells0c7d90c2006-08-22 20:06:10 -0400387 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400388 unsigned char blockbits;
389 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400390 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400391 struct nfs_fattr fattr;
392 struct nfs_fsstat res = {
393 .fattr = &fattr,
394 };
395 int error;
396
David Howells8fa5c002006-08-22 20:06:12 -0400397 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400398 if (error < 0)
399 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700400 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400401
402 /*
403 * Current versions of glibc do not correctly handle the
404 * case where f_frsize != f_bsize. Eventually we want to
405 * report the value of wtmult in this field.
406 */
David Howells0c7d90c2006-08-22 20:06:10 -0400407 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400408
409 /*
410 * On most *nix systems, f_blocks, f_bfree, and f_bavail
411 * are reported in units of f_frsize. Linux hasn't had
412 * an f_frsize field in its statfs struct until recently,
413 * thus historically Linux's sys_statfs reports these
414 * fields in units of f_bsize.
415 */
David Howells0c7d90c2006-08-22 20:06:10 -0400416 buf->f_bsize = dentry->d_sb->s_blocksize;
417 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400418 blockres = (1 << blockbits) - 1;
419 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
420 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
421 buf->f_bavail = (res.abytes + blockres) >> blockbits;
422
423 buf->f_files = res.tfiles;
424 buf->f_ffree = res.afiles;
425
426 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700427
David Howellsf7b422b2006-06-09 09:34:33 -0400428 return 0;
429
430 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700431 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700432 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400433}
434
David Howells7d4e2742006-08-22 20:06:07 -0400435/*
436 * Map the security flavour number to a name
437 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400438static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
439{
David Howells7d4e2742006-08-22 20:06:07 -0400440 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400441 rpc_authflavor_t flavour;
442 const char *str;
443 } sec_flavours[] = {
444 { RPC_AUTH_NULL, "null" },
445 { RPC_AUTH_UNIX, "sys" },
446 { RPC_AUTH_GSS_KRB5, "krb5" },
447 { RPC_AUTH_GSS_KRB5I, "krb5i" },
448 { RPC_AUTH_GSS_KRB5P, "krb5p" },
449 { RPC_AUTH_GSS_LKEY, "lkey" },
450 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
451 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
452 { RPC_AUTH_GSS_SPKM, "spkm" },
453 { RPC_AUTH_GSS_SPKMI, "spkmi" },
454 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400455 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400456 };
457 int i;
458
Chuck Lever4d81cd12007-07-01 12:12:40 -0400459 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400460 if (sec_flavours[i].flavour == flavour)
461 break;
462 }
463 return sec_flavours[i].str;
464}
465
Chuck Lever82d101d2008-03-14 14:10:37 -0400466static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
467 int showdefaults)
468{
469 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
470
471 switch (sap->sa_family) {
472 case AF_INET: {
473 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrisonbe859402008-10-31 00:56:28 -0700474 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400475 break;
476 }
477 case AF_INET6: {
478 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700479 seq_printf(m, ",mountaddr=%pI6", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400480 break;
481 }
482 default:
483 if (showdefaults)
484 seq_printf(m, ",mountaddr=unspecified");
485 }
486
487 if (nfss->mountd_version || showdefaults)
488 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
489 if (nfss->mountd_port || showdefaults)
490 seq_printf(m, ",mountport=%u", nfss->mountd_port);
491
492 switch (nfss->mountd_protocol) {
493 case IPPROTO_UDP:
494 seq_printf(m, ",mountproto=udp");
495 break;
496 case IPPROTO_TCP:
497 seq_printf(m, ",mountproto=tcp");
498 break;
499 default:
500 if (showdefaults)
501 seq_printf(m, ",mountproto=auto");
502 }
503}
504
David Howellsf7b422b2006-06-09 09:34:33 -0400505/*
506 * Describe the mount options in force on this server representation
507 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400508static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
509 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400510{
David Howells509de812006-08-22 20:06:11 -0400511 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400512 int flag;
David Howells509de812006-08-22 20:06:11 -0400513 const char *str;
514 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400515 } nfs_info[] = {
516 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400517 { NFS_MOUNT_INTR, ",intr", ",nointr" },
518 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400519 { NFS_MOUNT_NOCTO, ",nocto", "" },
520 { NFS_MOUNT_NOAC, ",noac", "" },
521 { NFS_MOUNT_NONLM, ",nolock", "" },
522 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400523 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500524 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
525 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400526 { 0, NULL, NULL }
527 };
David Howells509de812006-08-22 20:06:11 -0400528 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400529 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400530 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400531
Chuck Lever82d101d2008-03-14 14:10:37 -0400532 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400533 seq_printf(m, ",rsize=%u", nfss->rsize);
534 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400535 if (nfss->bsize != 0)
536 seq_printf(m, ",bsize=%u", nfss->bsize);
537 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400538 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400539 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400540 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400541 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400542 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400543 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400544 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400545 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400546 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
547 if (nfss->flags & nfs_infop->flag)
548 seq_puts(m, nfs_infop->str);
549 else
550 seq_puts(m, nfs_infop->nostr);
551 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400552 seq_printf(m, ",proto=%s",
553 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400554 if (version == 4) {
555 if (nfss->port != NFS_PORT)
556 seq_printf(m, ",port=%u", nfss->port);
557 } else
558 if (nfss->port)
559 seq_printf(m, ",port=%u", nfss->port);
560
Trond Myklebust33170232007-12-20 16:03:59 -0500561 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
562 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400563 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400564
565 if (version != 4)
566 nfs_show_mountd_options(m, nfss, showdefaults);
567
568#ifdef CONFIG_NFS_V4
569 if (clp->rpc_ops->version == 4)
570 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
571#endif
David Howellsb797cac2009-04-03 16:42:48 +0100572 if (nfss->options & NFS_OPTION_FSCACHE)
573 seq_printf(m, ",fsc");
David Howellsf7b422b2006-06-09 09:34:33 -0400574}
575
576/*
577 * Describe the mount options on this VFS mountpoint
578 */
579static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
580{
581 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
582
583 nfs_show_mount_options(m, nfss, 0);
584
Chuck Lever5d8515c2007-12-10 14:57:16 -0500585 seq_printf(m, ",addr=%s",
586 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
587 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400588
589 return 0;
590}
591
592/*
593 * Present statistical information for this VFS mountpoint
594 */
595static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
596{
597 int i, cpu;
598 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
599 struct rpc_auth *auth = nfss->client->cl_auth;
600 struct nfs_iostats totals = { };
601
602 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
603
604 /*
605 * Display all mount option settings
606 */
607 seq_printf(m, "\n\topts:\t");
608 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
609 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
610 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
611 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
612 nfs_show_mount_options(m, nfss, 1);
613
614 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
615
616 seq_printf(m, "\n\tcaps:\t");
617 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400618 seq_printf(m, ",wtmult=%u", nfss->wtmult);
619 seq_printf(m, ",dtsize=%u", nfss->dtsize);
620 seq_printf(m, ",bsize=%u", nfss->bsize);
621 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400622
623#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500624 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400625 seq_printf(m, "\n\tnfsv4:\t");
626 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
627 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
628 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
629 }
630#endif
631
632 /*
633 * Display security flavor in effect for this mount
634 */
Chuck Lever2d767432008-03-14 14:10:08 -0400635 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400636 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400637 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400638
639 /*
640 * Display superblock I/O counters
641 */
642 for_each_possible_cpu(cpu) {
643 struct nfs_iostats *stats;
644
645 preempt_disable();
646 stats = per_cpu_ptr(nfss->io_stats, cpu);
647
648 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
649 totals.events[i] += stats->events[i];
650 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
651 totals.bytes[i] += stats->bytes[i];
David Howells6a510912009-04-03 16:42:43 +0100652#ifdef CONFIG_NFS_FSCACHE
653 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
654 totals.fscache[i] += stats->fscache[i];
655#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400656
657 preempt_enable();
658 }
659
660 seq_printf(m, "\n\tevents:\t");
661 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
662 seq_printf(m, "%lu ", totals.events[i]);
663 seq_printf(m, "\n\tbytes:\t");
664 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
665 seq_printf(m, "%Lu ", totals.bytes[i]);
David Howells6a510912009-04-03 16:42:43 +0100666#ifdef CONFIG_NFS_FSCACHE
667 if (nfss->options & NFS_OPTION_FSCACHE) {
668 seq_printf(m, "\n\tfsc:\t");
669 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
670 seq_printf(m, "%Lu ", totals.bytes[i]);
671 }
672#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400673 seq_printf(m, "\n");
674
675 rpc_print_iostats(m, nfss->client);
676
677 return 0;
678}
679
680/*
681 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400682 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400683 */
Al Viro42faad92008-04-24 07:21:56 -0400684static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400685{
Al Viro42faad92008-04-24 07:21:56 -0400686 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400687 struct rpc_clnt *rpc;
688
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400689 /* -EIO all pending I/O */
690 rpc = server->client_acl;
691 if (!IS_ERR(rpc))
692 rpc_killall_tasks(rpc);
693 rpc = server->client;
694 if (!IS_ERR(rpc))
695 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400696}
697
698/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500699 * Sanity-check a server address provided by the mount command.
700 *
701 * Address family must be initialized, and address must not be
702 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400703 */
704static int nfs_verify_server_address(struct sockaddr *addr)
705{
706 switch (addr->sa_family) {
707 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500708 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700709 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500710 }
711 case AF_INET6: {
712 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
713 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400714 }
715 }
716
717 return 0;
718}
719
Chuck Leverce3b7e12008-06-23 12:36:53 -0400720static void nfs_parse_ipv4_address(char *string, size_t str_len,
721 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500722{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400723 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
724 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500725
Chuck Leverce3b7e12008-06-23 12:36:53 -0400726 if (str_len <= INET_ADDRSTRLEN) {
727 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
728 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500729
Chuck Leverce3b7e12008-06-23 12:36:53 -0400730 sin->sin_family = AF_INET;
731 *addr_len = sizeof(*sin);
732 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500733 return;
734 }
Chuck Lever9412b922007-12-10 14:59:21 -0500735
736 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400737 *addr_len = 0;
738}
739
740#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400741static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
742 const char *delim,
743 struct sockaddr_in6 *sin6)
Chuck Leverd8e77482008-06-23 12:37:01 -0400744{
745 char *p;
746 size_t len;
747
Chuck Lever5e2e7722008-10-08 15:38:10 -0400748 if ((string + str_len) == delim)
749 return 1;
750
Chuck Leverd8e77482008-06-23 12:37:01 -0400751 if (*delim != IPV6_SCOPE_DELIMITER)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400752 return 0;
753
754 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
755 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400756
757 len = (string + str_len) - delim - 1;
758 p = kstrndup(delim + 1, len, GFP_KERNEL);
759 if (p) {
760 unsigned long scope_id = 0;
761 struct net_device *dev;
762
763 dev = dev_get_by_name(&init_net, p);
764 if (dev != NULL) {
765 scope_id = dev->ifindex;
766 dev_put(dev);
767 } else {
Chuck Lever5e2e7722008-10-08 15:38:10 -0400768 if (strict_strtoul(p, 10, &scope_id) == 0) {
769 kfree(p);
770 return 0;
771 }
Chuck Leverd8e77482008-06-23 12:37:01 -0400772 }
773
774 kfree(p);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400775
Chuck Leverd8e77482008-06-23 12:37:01 -0400776 sin6->sin6_scope_id = scope_id;
777 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400778 return 1;
Chuck Leverd8e77482008-06-23 12:37:01 -0400779 }
Chuck Lever5e2e7722008-10-08 15:38:10 -0400780
781 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400782}
783
Chuck Leverce3b7e12008-06-23 12:36:53 -0400784static void nfs_parse_ipv6_address(char *string, size_t str_len,
785 struct sockaddr *sap, size_t *addr_len)
786{
787 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
788 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400789 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400790
791 if (str_len <= INET6_ADDRSTRLEN) {
792 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
793 (int)str_len, string);
794
795 sin6->sin6_family = AF_INET6;
796 *addr_len = sizeof(*sin6);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400797 if (in6_pton(string, str_len, addr,
798 IPV6_SCOPE_DELIMITER, &delim) != 0) {
799 if (nfs_parse_ipv6_scope_id(string, str_len,
800 delim, sin6) != 0)
801 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400802 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400803 }
804
805 sap->sa_family = AF_UNSPEC;
806 *addr_len = 0;
807}
808#else
809static void nfs_parse_ipv6_address(char *string, size_t str_len,
810 struct sockaddr *sap, size_t *addr_len)
811{
812 sap->sa_family = AF_UNSPEC;
813 *addr_len = 0;
814}
815#endif
816
817/*
818 * Construct a sockaddr based on the contents of a string that contains
819 * an IP address in presentation format.
820 *
821 * If there is a problem constructing the new sockaddr, set the address
822 * family to AF_UNSPEC.
823 */
J. Bruce Fieldsea31a4432008-08-20 16:10:23 -0400824void nfs_parse_ip_address(char *string, size_t str_len,
Chuck Leverce3b7e12008-06-23 12:36:53 -0400825 struct sockaddr *sap, size_t *addr_len)
826{
827 unsigned int i, colons;
828
829 colons = 0;
830 for (i = 0; i < str_len; i++)
831 if (string[i] == ':')
832 colons++;
833
834 if (colons >= 2)
835 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
836 else
837 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500838}
839
840/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400841 * Sanity check the NFS transport protocol.
842 *
843 */
844static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
845{
846 switch (mnt->nfs_server.protocol) {
847 case XPRT_TRANSPORT_UDP:
848 case XPRT_TRANSPORT_TCP:
849 case XPRT_TRANSPORT_RDMA:
850 break;
851 default:
852 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
853 }
854}
855
856/*
857 * For text based NFSv2/v3 mounts, the mount protocol transport default
858 * settings should depend upon the specified NFS transport.
859 */
860static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
861{
862 nfs_validate_transport_protocol(mnt);
863
864 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
865 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
866 return;
867 switch (mnt->nfs_server.protocol) {
868 case XPRT_TRANSPORT_UDP:
869 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
870 break;
871 case XPRT_TRANSPORT_TCP:
872 case XPRT_TRANSPORT_RDMA:
873 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
874 }
875}
876
877/*
Chuck Lever01060c82008-06-24 16:33:38 -0400878 * Parse the value of the 'sec=' option.
879 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400880 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400881 */
882static int nfs_parse_security_flavors(char *value,
883 struct nfs_parsed_mount_data *mnt)
884{
885 substring_t args[MAX_OPT_ARGS];
886
887 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
888
889 switch (match_token(value, nfs_secflavor_tokens, args)) {
890 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400891 mnt->auth_flavor_len = 0;
892 mnt->auth_flavors[0] = RPC_AUTH_NULL;
893 break;
894 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400895 mnt->auth_flavor_len = 0;
896 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
897 break;
898 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400899 mnt->auth_flavor_len = 1;
900 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
901 break;
902 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400903 mnt->auth_flavor_len = 1;
904 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
905 break;
906 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400907 mnt->auth_flavor_len = 1;
908 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
909 break;
910 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400911 mnt->auth_flavor_len = 1;
912 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
913 break;
914 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400915 mnt->auth_flavor_len = 1;
916 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
917 break;
918 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400919 mnt->auth_flavor_len = 1;
920 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
921 break;
922 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400923 mnt->auth_flavor_len = 1;
924 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
925 break;
926 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400927 mnt->auth_flavor_len = 1;
928 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
929 break;
930 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400931 mnt->auth_flavor_len = 1;
932 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
933 break;
934 default:
935 return 0;
936 }
937
938 return 1;
939}
940
Chuck Leverf45663c2008-06-24 19:28:02 -0400941static void nfs_parse_invalid_value(const char *option)
942{
943 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
944}
945
Chuck Lever01060c82008-06-24 16:33:38 -0400946/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400947 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400948 * a data structure. The whole mount string is processed; bad options are
949 * skipped as they are encountered. If there were no errors, return 1;
950 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400951 */
952static int nfs_parse_mount_options(char *raw,
953 struct nfs_parsed_mount_data *mnt)
954{
Eric Parisf9c3a382008-03-05 14:20:18 -0500955 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400956 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400957
958 if (!raw) {
959 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
960 return 1;
961 }
962 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
963
Eric Parisf9c3a382008-03-05 14:20:18 -0500964 secdata = alloc_secdata();
965 if (!secdata)
966 goto out_nomem;
967
968 rc = security_sb_copy_data(raw, secdata);
969 if (rc)
970 goto out_security_failure;
971
972 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
973 if (rc)
974 goto out_security_failure;
975
976 free_secdata(secdata);
977
Chuck Leverbf0fd762007-07-01 12:13:44 -0400978 while ((p = strsep(&raw, ",")) != NULL) {
979 substring_t args[MAX_OPT_ARGS];
980 int option, token;
981
982 if (!*p)
983 continue;
984
985 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
986
987 token = match_token(p, nfs_mount_option_tokens, args);
988 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400989
990 /*
991 * boolean options: foo/nofoo
992 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400993 case Opt_soft:
994 mnt->flags |= NFS_MOUNT_SOFT;
995 break;
996 case Opt_hard:
997 mnt->flags &= ~NFS_MOUNT_SOFT;
998 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400999 case Opt_posix:
1000 mnt->flags |= NFS_MOUNT_POSIX;
1001 break;
1002 case Opt_noposix:
1003 mnt->flags &= ~NFS_MOUNT_POSIX;
1004 break;
1005 case Opt_cto:
1006 mnt->flags &= ~NFS_MOUNT_NOCTO;
1007 break;
1008 case Opt_nocto:
1009 mnt->flags |= NFS_MOUNT_NOCTO;
1010 break;
1011 case Opt_ac:
1012 mnt->flags &= ~NFS_MOUNT_NOAC;
1013 break;
1014 case Opt_noac:
1015 mnt->flags |= NFS_MOUNT_NOAC;
1016 break;
1017 case Opt_lock:
1018 mnt->flags &= ~NFS_MOUNT_NONLM;
1019 break;
1020 case Opt_nolock:
1021 mnt->flags |= NFS_MOUNT_NONLM;
1022 break;
1023 case Opt_v2:
1024 mnt->flags &= ~NFS_MOUNT_VER3;
1025 break;
1026 case Opt_v3:
1027 mnt->flags |= NFS_MOUNT_VER3;
1028 break;
1029 case Opt_udp:
1030 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001031 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001032 break;
1033 case Opt_tcp:
1034 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001035 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001036 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001037 case Opt_rdma:
1038 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1039 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001040 xprt_load_transport(p);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001041 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001042 case Opt_acl:
1043 mnt->flags &= ~NFS_MOUNT_NOACL;
1044 break;
1045 case Opt_noacl:
1046 mnt->flags |= NFS_MOUNT_NOACL;
1047 break;
1048 case Opt_rdirplus:
1049 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1050 break;
1051 case Opt_nordirplus:
1052 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1053 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001054 case Opt_sharecache:
1055 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1056 break;
1057 case Opt_nosharecache:
1058 mnt->flags |= NFS_MOUNT_UNSHARED;
1059 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001060 case Opt_resvport:
1061 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1062 break;
1063 case Opt_noresvport:
1064 mnt->flags |= NFS_MOUNT_NORESVPORT;
1065 break;
David Howellsb797cac2009-04-03 16:42:48 +01001066 case Opt_fscache:
1067 mnt->options |= NFS_OPTION_FSCACHE;
1068 kfree(mnt->fscache_uniq);
1069 mnt->fscache_uniq = NULL;
1070 break;
1071 case Opt_nofscache:
1072 mnt->options &= ~NFS_OPTION_FSCACHE;
1073 kfree(mnt->fscache_uniq);
1074 mnt->fscache_uniq = NULL;
1075 break;
1076 case Opt_fscache_uniq:
1077 string = match_strdup(args);
1078 if (!string)
1079 goto out_nomem;
1080 kfree(mnt->fscache_uniq);
1081 mnt->fscache_uniq = string;
1082 mnt->options |= NFS_OPTION_FSCACHE;
1083 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001084
Chuck Leverf45663c2008-06-24 19:28:02 -04001085 /*
1086 * options that take numeric values
1087 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001088 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001089 if (match_int(args, &option) ||
1090 option < 0 || option > USHORT_MAX) {
1091 errors++;
1092 nfs_parse_invalid_value("port");
1093 } else
1094 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001095 break;
1096 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001097 if (match_int(args, &option) || option < 0) {
1098 errors++;
1099 nfs_parse_invalid_value("rsize");
1100 } else
1101 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001102 break;
1103 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001104 if (match_int(args, &option) || option < 0) {
1105 errors++;
1106 nfs_parse_invalid_value("wsize");
1107 } else
1108 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001109 break;
1110 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001111 if (match_int(args, &option) || option < 0) {
1112 errors++;
1113 nfs_parse_invalid_value("bsize");
1114 } else
1115 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001116 break;
1117 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001118 if (match_int(args, &option) || option <= 0) {
1119 errors++;
1120 nfs_parse_invalid_value("timeo");
1121 } else
1122 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001123 break;
1124 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001125 if (match_int(args, &option) || option <= 0) {
1126 errors++;
1127 nfs_parse_invalid_value("retrans");
1128 } else
1129 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001130 break;
1131 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001132 if (match_int(args, &option) || option < 0) {
1133 errors++;
1134 nfs_parse_invalid_value("acregmin");
1135 } else
1136 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001137 break;
1138 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001139 if (match_int(args, &option) || option < 0) {
1140 errors++;
1141 nfs_parse_invalid_value("acregmax");
1142 } else
1143 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001144 break;
1145 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001146 if (match_int(args, &option) || option < 0) {
1147 errors++;
1148 nfs_parse_invalid_value("acdirmin");
1149 } else
1150 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001151 break;
1152 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001153 if (match_int(args, &option) || option < 0) {
1154 errors++;
1155 nfs_parse_invalid_value("acdirmax");
1156 } else
1157 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001158 break;
1159 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001160 if (match_int(args, &option) || option < 0) {
1161 errors++;
1162 nfs_parse_invalid_value("actimeo");
1163 } else
1164 mnt->acregmin = mnt->acregmax =
1165 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001166 break;
1167 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001168 if (match_int(args, &option) || option < 0) {
1169 errors++;
1170 nfs_parse_invalid_value("namlen");
1171 } else
1172 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001173 break;
1174 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001175 if (match_int(args, &option) ||
1176 option < 0 || option > USHORT_MAX) {
1177 errors++;
1178 nfs_parse_invalid_value("mountport");
1179 } else
1180 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001181 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001182 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001183 if (match_int(args, &option) ||
1184 option < NFS_MNT_VERSION ||
1185 option > NFS_MNT3_VERSION) {
1186 errors++;
1187 nfs_parse_invalid_value("mountvers");
1188 } else
1189 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001190 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001192 if (match_int(args, &option)) {
1193 errors++;
1194 nfs_parse_invalid_value("nfsvers");
1195 break;
1196 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001197 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001198 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001199 mnt->flags &= ~NFS_MOUNT_VER3;
1200 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001201 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001202 mnt->flags |= NFS_MOUNT_VER3;
1203 break;
1204 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001205 errors++;
1206 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001207 }
1208 break;
1209
Chuck Leverf45663c2008-06-24 19:28:02 -04001210 /*
1211 * options that take text values
1212 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001213 case Opt_sec:
1214 string = match_strdup(args);
1215 if (string == NULL)
1216 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001217 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001218 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001219 if (!rc) {
1220 errors++;
1221 dfprintk(MOUNT, "NFS: unrecognized "
1222 "security flavor\n");
1223 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001224 break;
1225 case Opt_proto:
1226 string = match_strdup(args);
1227 if (string == NULL)
1228 goto out_nomem;
1229 token = match_token(string,
1230 nfs_xprt_protocol_tokens, args);
1231 kfree(string);
1232
1233 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001234 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001235 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001236 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001237 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001238 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001239 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001240 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001241 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001242 case Opt_xprt_rdma:
1243 /* vector side protocols to TCP */
1244 mnt->flags |= NFS_MOUNT_TCP;
1245 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Tom Talpeya67d18f2009-03-11 14:37:56 -04001246 xprt_load_transport(string);
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001247 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001248 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001249 errors++;
1250 dfprintk(MOUNT, "NFS: unrecognized "
1251 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001252 }
Tom Talpeya67d18f2009-03-11 14:37:56 -04001253 kfree(string);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001254 break;
1255 case Opt_mountproto:
1256 string = match_strdup(args);
1257 if (string == NULL)
1258 goto out_nomem;
1259 token = match_token(string,
1260 nfs_xprt_protocol_tokens, args);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001261
1262 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001263 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001264 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001265 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001266 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001267 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001268 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001269 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001270 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001271 errors++;
1272 dfprintk(MOUNT, "NFS: unrecognized "
1273 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001274 }
1275 break;
1276 case Opt_addr:
1277 string = match_strdup(args);
1278 if (string == NULL)
1279 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001280 nfs_parse_ip_address(string, strlen(string),
1281 (struct sockaddr *)
1282 &mnt->nfs_server.address,
1283 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001284 kfree(string);
1285 break;
1286 case Opt_clientaddr:
1287 string = match_strdup(args);
1288 if (string == NULL)
1289 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001290 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001291 mnt->client_address = string;
1292 break;
Chuck Lever33832032007-12-10 14:59:13 -05001293 case Opt_mounthost:
1294 string = match_strdup(args);
1295 if (string == NULL)
1296 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001297 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001298 mnt->mount_server.hostname = string;
1299 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001300 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001301 string = match_strdup(args);
1302 if (string == NULL)
1303 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001304 nfs_parse_ip_address(string, strlen(string),
1305 (struct sockaddr *)
1306 &mnt->mount_server.address,
1307 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001308 kfree(string);
1309 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001310 case Opt_lookupcache:
1311 string = match_strdup(args);
1312 if (string == NULL)
1313 goto out_nomem;
1314 token = match_token(string,
1315 nfs_lookupcache_tokens, args);
1316 kfree(string);
1317 switch (token) {
1318 case Opt_lookupcache_all:
1319 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1320 break;
1321 case Opt_lookupcache_positive:
1322 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1323 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1324 break;
1325 case Opt_lookupcache_none:
1326 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1327 break;
1328 default:
1329 errors++;
1330 dfprintk(MOUNT, "NFS: invalid "
1331 "lookupcache argument\n");
1332 };
1333 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001334
Chuck Leverf45663c2008-06-24 19:28:02 -04001335 /*
1336 * Special options
1337 */
1338 case Opt_sloppy:
1339 sloppy = 1;
1340 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1341 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001342 case Opt_userspace:
1343 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001344 dfprintk(MOUNT, "NFS: ignoring mount option "
1345 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001346 break;
1347
1348 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001349 errors++;
1350 dfprintk(MOUNT, "NFS: unrecognized mount option "
1351 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001352 }
1353 }
1354
Chuck Leveraf904de2008-09-08 11:58:13 -04001355 if (errors > 0) {
1356 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1357 errors, (errors == 1 ? "" : "s"));
1358 if (!sloppy)
1359 return 0;
1360 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001361 return 1;
1362
1363out_nomem:
1364 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1365 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001366out_security_failure:
1367 free_secdata(secdata);
1368 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1369 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001370}
1371
1372/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001373 * Use the remote server's MOUNT service to request the NFS file handle
1374 * corresponding to the provided path.
1375 */
1376static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1377 struct nfs_fh *root_fh)
1378{
Chuck Leverc5d120f2008-12-23 15:21:35 -05001379 struct nfs_mount_request request = {
1380 .sap = (struct sockaddr *)
1381 &args->mount_server.address,
1382 .dirpath = args->nfs_server.export_path,
1383 .protocol = args->mount_server.protocol,
1384 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001385 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001386 };
Chuck Lever4c568012007-12-10 14:59:28 -05001387 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001388
1389 if (args->mount_server.version == 0) {
1390 if (args->flags & NFS_MOUNT_VER3)
1391 args->mount_server.version = NFS_MNT3_VERSION;
1392 else
1393 args->mount_server.version = NFS_MNT_VERSION;
1394 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001395 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001396
Chuck Lever33832032007-12-10 14:59:13 -05001397 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001398 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001399 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001400 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001401
Chuck Lever0076d7b2007-07-01 12:13:49 -04001402 /*
1403 * Construct the mount server's address.
1404 */
Chuck Lever4c568012007-12-10 14:59:28 -05001405 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001406 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001407 args->nfs_server.addrlen);
1408 args->mount_server.addrlen = args->nfs_server.addrlen;
1409 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001410 request.salen = args->mount_server.addrlen;
Chuck Lever4c568012007-12-10 14:59:28 -05001411
James Lentiniaad70002007-09-24 17:32:49 -04001412 /*
1413 * autobind will be used if mount_server.port == 0
1414 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001415 nfs_set_port(request.sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001416
1417 /*
1418 * Now ask the mount server to map our export path
1419 * to a file handle.
1420 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001421 status = nfs_mount(&request);
Chuck Leverefd83402007-09-11 18:00:58 -04001422 if (status == 0)
1423 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001424
Chuck Lever396cee972008-06-12 12:37:49 -04001425 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Leverc5d120f2008-12-23 15:21:35 -05001426 request.hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001427 return status;
1428}
1429
Chuck Leverd1aa0822008-06-23 12:36:45 -04001430static int nfs_parse_simple_hostname(const char *dev_name,
1431 char **hostname, size_t maxnamlen,
1432 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001433{
1434 size_t len;
1435 char *colon, *comma;
1436
1437 colon = strchr(dev_name, ':');
1438 if (colon == NULL)
1439 goto out_bad_devname;
1440
1441 len = colon - dev_name;
1442 if (len > maxnamlen)
1443 goto out_hostname;
1444
1445 /* N.B. caller will free nfs_server.hostname in all cases */
1446 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1447 if (!*hostname)
1448 goto out_nomem;
1449
1450 /* kill possible hostname list: not supported */
1451 comma = strchr(*hostname, ',');
1452 if (comma != NULL) {
1453 if (comma == *hostname)
1454 goto out_bad_devname;
1455 *comma = '\0';
1456 }
1457
1458 colon++;
1459 len = strlen(colon);
1460 if (len > maxpathlen)
1461 goto out_path;
1462 *export_path = kstrndup(colon, len, GFP_KERNEL);
1463 if (!*export_path)
1464 goto out_nomem;
1465
1466 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1467 return 0;
1468
1469out_bad_devname:
1470 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1471 return -EINVAL;
1472
1473out_nomem:
1474 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1475 return -ENOMEM;
1476
1477out_hostname:
1478 dfprintk(MOUNT, "NFS: server hostname too long\n");
1479 return -ENAMETOOLONG;
1480
1481out_path:
1482 dfprintk(MOUNT, "NFS: export pathname too long\n");
1483 return -ENAMETOOLONG;
1484}
1485
1486/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001487 * Hostname has square brackets around it because it contains one or
1488 * more colons. We look for the first closing square bracket, and a
1489 * colon must follow it.
1490 */
1491static int nfs_parse_protected_hostname(const char *dev_name,
1492 char **hostname, size_t maxnamlen,
1493 char **export_path, size_t maxpathlen)
1494{
1495 size_t len;
1496 char *start, *end;
1497
1498 start = (char *)(dev_name + 1);
1499
1500 end = strchr(start, ']');
1501 if (end == NULL)
1502 goto out_bad_devname;
1503 if (*(end + 1) != ':')
1504 goto out_bad_devname;
1505
1506 len = end - start;
1507 if (len > maxnamlen)
1508 goto out_hostname;
1509
1510 /* N.B. caller will free nfs_server.hostname in all cases */
1511 *hostname = kstrndup(start, len, GFP_KERNEL);
1512 if (*hostname == NULL)
1513 goto out_nomem;
1514
1515 end += 2;
1516 len = strlen(end);
1517 if (len > maxpathlen)
1518 goto out_path;
1519 *export_path = kstrndup(end, len, GFP_KERNEL);
1520 if (!*export_path)
1521 goto out_nomem;
1522
1523 return 0;
1524
1525out_bad_devname:
1526 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1527 return -EINVAL;
1528
1529out_nomem:
1530 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1531 return -ENOMEM;
1532
1533out_hostname:
1534 dfprintk(MOUNT, "NFS: server hostname too long\n");
1535 return -ENAMETOOLONG;
1536
1537out_path:
1538 dfprintk(MOUNT, "NFS: export pathname too long\n");
1539 return -ENAMETOOLONG;
1540}
1541
1542/*
1543 * Split "dev_name" into "hostname:export_path".
1544 *
1545 * The leftmost colon demarks the split between the server's hostname
1546 * and the export path. If the hostname starts with a left square
1547 * bracket, then it may contain colons.
1548 *
1549 * Note: caller frees hostname and export path, even on error.
1550 */
1551static int nfs_parse_devname(const char *dev_name,
1552 char **hostname, size_t maxnamlen,
1553 char **export_path, size_t maxpathlen)
1554{
1555 if (*dev_name == '[')
1556 return nfs_parse_protected_hostname(dev_name,
1557 hostname, maxnamlen,
1558 export_path, maxpathlen);
1559
1560 return nfs_parse_simple_hostname(dev_name,
1561 hostname, maxnamlen,
1562 export_path, maxpathlen);
1563}
1564
1565/*
David Howells54ceac42006-08-22 20:06:13 -04001566 * Validate the NFS2/NFS3 mount data
1567 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001568 *
1569 * For option strings, user space handles the following behaviors:
1570 *
1571 * + DNS: mapping server host name to IP address ("addr=" option)
1572 *
1573 * + failure mode: how to behave if a mount request can't be handled
1574 * immediately ("fg/bg" option)
1575 *
1576 * + retry: how often to retry a mount request ("retry=" option)
1577 *
1578 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1579 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001580 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001581static int nfs_validate_mount_data(void *options,
1582 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001583 struct nfs_fh *mntfh,
1584 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001585{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001586 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001587
Chuck Lever5df36e72007-07-01 12:12:56 -04001588 if (data == NULL)
1589 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001590
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001591 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1592 args->rsize = NFS_MAX_FILE_IO_SIZE;
1593 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001594 args->acregmin = NFS_DEF_ACREGMIN;
1595 args->acregmax = NFS_DEF_ACREGMAX;
1596 args->acdirmin = NFS_DEF_ACDIRMIN;
1597 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001598 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001599 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001600 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001601 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001602
David Howellsf7b422b2006-06-09 09:34:33 -04001603 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001604 case 1:
1605 data->namlen = 0;
1606 case 2:
1607 data->bsize = 0;
1608 case 3:
1609 if (data->flags & NFS_MOUNT_VER3)
1610 goto out_no_v3;
1611 data->root.size = NFS2_FHSIZE;
1612 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1613 case 4:
1614 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1615 goto out_no_sec;
1616 case 5:
1617 memset(data->context, 0, sizeof(data->context));
1618 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001619 if (data->flags & NFS_MOUNT_VER3) {
1620 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1621 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001622 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001623 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001624 mntfh->size = NFS2_FHSIZE;
1625
Chuck Lever5df36e72007-07-01 12:12:56 -04001626
1627 memcpy(mntfh->data, data->root.data, mntfh->size);
1628 if (mntfh->size < sizeof(mntfh->data))
1629 memset(mntfh->data + mntfh->size, 0,
1630 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001631
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001632 /*
1633 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1634 * can deal with.
1635 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001636 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001637 args->rsize = data->rsize;
1638 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001639 args->timeo = data->timeo;
1640 args->retrans = data->retrans;
1641 args->acregmin = data->acregmin;
1642 args->acregmax = data->acregmax;
1643 args->acdirmin = data->acdirmin;
1644 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001645
1646 memcpy(&args->nfs_server.address, &data->addr,
1647 sizeof(data->addr));
1648 args->nfs_server.addrlen = sizeof(data->addr);
1649 if (!nfs_verify_server_address((struct sockaddr *)
1650 &args->nfs_server.address))
1651 goto out_no_address;
1652
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001653 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001654 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001655 /* N.B. caller will free nfs_server.hostname in all cases */
1656 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1657 args->namlen = data->namlen;
1658 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001659
1660 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1661 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001662 if (!args->nfs_server.hostname)
1663 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001664
1665 /*
1666 * The legacy version 6 binary mount data from userspace has a
1667 * field used only to transport selinux information into the
1668 * the kernel. To continue to support that functionality we
1669 * have a touch of selinux knowledge here in the NFS code. The
1670 * userspace code converted context=blah to just blah so we are
1671 * converting back to the full string selinux understands.
1672 */
1673 if (data->context[0]){
1674#ifdef CONFIG_SECURITY_SELINUX
1675 int rc;
1676 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1677 if (!opts_str)
1678 return -ENOMEM;
1679 strcpy(opts_str, "context=");
1680 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1681 strcat(opts_str, &data->context[0]);
1682 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1683 kfree(opts_str);
1684 if (rc)
1685 return rc;
1686#else
1687 return -EINVAL;
1688#endif
1689 }
1690
Chuck Lever5df36e72007-07-01 12:12:56 -04001691 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001692 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001693 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001694
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001695 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001696 return -EINVAL;
1697
Chuck Lever6e88e062007-09-24 15:39:50 -04001698 if (!nfs_verify_server_address((struct sockaddr *)
1699 &args->nfs_server.address))
1700 goto out_no_address;
1701
Chuck Levered596a82008-06-26 17:47:05 -04001702 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1703 args->nfs_server.port);
1704
Trond Myklebust259875e2008-07-02 14:43:47 -04001705 nfs_set_mount_transport_protocol(args);
1706
Chuck Leverdc045892008-06-23 12:36:37 -04001707 status = nfs_parse_devname(dev_name,
1708 &args->nfs_server.hostname,
1709 PAGE_SIZE,
1710 &args->nfs_server.export_path,
1711 NFS_MAXPATHLEN);
1712 if (!status)
1713 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001714
Chuck Leverdc045892008-06-23 12:36:37 -04001715 kfree(args->nfs_server.export_path);
1716 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001717
Chuck Lever136d5582007-07-01 12:13:54 -04001718 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001719 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001720
Chuck Lever136d5582007-07-01 12:13:54 -04001721 break;
1722 }
David Howellsf7b422b2006-06-09 09:34:33 -04001723 }
David Howells54ceac42006-08-22 20:06:13 -04001724
David Howellsf7b422b2006-06-09 09:34:33 -04001725#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001726 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001727 goto out_v3_not_compiled;
1728#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001729
David Howells54ceac42006-08-22 20:06:13 -04001730 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001731
1732out_no_data:
1733 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1734 return -EINVAL;
1735
1736out_no_v3:
1737 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1738 data->version);
1739 return -EINVAL;
1740
1741out_no_sec:
1742 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1743 return -EINVAL;
1744
Chuck Lever5df36e72007-07-01 12:12:56 -04001745#ifndef CONFIG_NFS_V3
1746out_v3_not_compiled:
1747 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1748 return -EPROTONOSUPPORT;
1749#endif /* !CONFIG_NFS_V3 */
1750
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001751out_nomem:
1752 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1753 return -ENOMEM;
1754
Chuck Lever5df36e72007-07-01 12:12:56 -04001755out_no_address:
1756 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1757 return -EINVAL;
1758
1759out_invalid_fh:
1760 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1761 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001762}
1763
Jeff Layton48b605f2008-06-10 15:38:39 -04001764static int
1765nfs_compare_remount_data(struct nfs_server *nfss,
1766 struct nfs_parsed_mount_data *data)
1767{
1768 if (data->flags != nfss->flags ||
1769 data->rsize != nfss->rsize ||
1770 data->wsize != nfss->wsize ||
1771 data->retrans != nfss->client->cl_timeout->to_retries ||
1772 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1773 data->acregmin != nfss->acregmin / HZ ||
1774 data->acregmax != nfss->acregmax / HZ ||
1775 data->acdirmin != nfss->acdirmin / HZ ||
1776 data->acdirmax != nfss->acdirmax / HZ ||
1777 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1778 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1779 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1780 data->nfs_server.addrlen) != 0)
1781 return -EINVAL;
1782
1783 return 0;
1784}
1785
1786static int
1787nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1788{
1789 int error;
1790 struct nfs_server *nfss = sb->s_fs_info;
1791 struct nfs_parsed_mount_data *data;
1792 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1793 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001794 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001795
1796 /*
1797 * Userspace mount programs that send binary options generally send
1798 * them populated with default values. We have no way to know which
1799 * ones were explicitly specified. Fall back to legacy behavior and
1800 * just return success.
1801 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001802 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1803 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1804 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001805 return 0;
1806
1807 data = kzalloc(sizeof(*data), GFP_KERNEL);
1808 if (data == NULL)
1809 return -ENOMEM;
1810
1811 /* fill out struct with values from existing mount */
1812 data->flags = nfss->flags;
1813 data->rsize = nfss->rsize;
1814 data->wsize = nfss->wsize;
1815 data->retrans = nfss->client->cl_timeout->to_retries;
1816 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1817 data->acregmin = nfss->acregmin / HZ;
1818 data->acregmax = nfss->acregmax / HZ;
1819 data->acdirmin = nfss->acdirmin / HZ;
1820 data->acdirmax = nfss->acdirmax / HZ;
1821 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1822 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1823 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1824 data->nfs_server.addrlen);
1825
1826 /* overwrite those values with any that were specified */
1827 error = nfs_parse_mount_options((char *)options, data);
1828 if (error < 0)
1829 goto out;
1830
1831 /* compare new mount options with old ones */
1832 error = nfs_compare_remount_data(nfss, data);
1833out:
1834 kfree(data);
1835 return error;
1836}
1837
David Howells54ceac42006-08-22 20:06:13 -04001838/*
1839 * Initialise the common bits of the superblock
1840 */
1841static inline void nfs_initialise_sb(struct super_block *sb)
1842{
1843 struct nfs_server *server = NFS_SB(sb);
1844
1845 sb->s_magic = NFS_SUPER_MAGIC;
1846
1847 /* We probably want something more informative here */
1848 snprintf(sb->s_id, sizeof(sb->s_id),
1849 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1850
1851 if (sb->s_blocksize == 0)
1852 sb->s_blocksize = nfs_block_bits(server->wsize,
1853 &sb->s_blocksize_bits);
1854
1855 if (server->flags & NFS_MOUNT_NOAC)
1856 sb->s_flags |= MS_SYNCHRONOUS;
1857
1858 nfs_super_set_maxbytes(sb, server->maxfilesize);
1859}
1860
1861/*
1862 * Finish setting up an NFS2/3 superblock
1863 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001864static void nfs_fill_super(struct super_block *sb,
1865 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001866{
1867 struct nfs_server *server = NFS_SB(sb);
1868
1869 sb->s_blocksize_bits = 0;
1870 sb->s_blocksize = 0;
1871 if (data->bsize)
1872 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1873
1874 if (server->flags & NFS_MOUNT_VER3) {
1875 /* The VFS shouldn't apply the umask to mode bits. We will do
1876 * so ourselves when necessary.
1877 */
1878 sb->s_flags |= MS_POSIXACL;
1879 sb->s_time_gran = 1;
1880 }
1881
1882 sb->s_op = &nfs_sops;
1883 nfs_initialise_sb(sb);
1884}
1885
1886/*
1887 * Finish setting up a cloned NFS2/3 superblock
1888 */
1889static void nfs_clone_super(struct super_block *sb,
1890 const struct super_block *old_sb)
1891{
1892 struct nfs_server *server = NFS_SB(sb);
1893
1894 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1895 sb->s_blocksize = old_sb->s_blocksize;
1896 sb->s_maxbytes = old_sb->s_maxbytes;
1897
1898 if (server->flags & NFS_MOUNT_VER3) {
1899 /* The VFS shouldn't apply the umask to mode bits. We will do
1900 * so ourselves when necessary.
1901 */
1902 sb->s_flags |= MS_POSIXACL;
1903 sb->s_time_gran = 1;
1904 }
1905
1906 sb->s_op = old_sb->s_op;
1907 nfs_initialise_sb(sb);
1908}
1909
Trond Myklebust275a5d22007-05-16 16:53:28 -04001910static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1911{
1912 const struct nfs_server *a = s->s_fs_info;
1913 const struct rpc_clnt *clnt_a = a->client;
1914 const struct rpc_clnt *clnt_b = b->client;
1915
1916 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1917 goto Ebusy;
1918 if (a->nfs_client != b->nfs_client)
1919 goto Ebusy;
1920 if (a->flags != b->flags)
1921 goto Ebusy;
1922 if (a->wsize != b->wsize)
1923 goto Ebusy;
1924 if (a->rsize != b->rsize)
1925 goto Ebusy;
1926 if (a->acregmin != b->acregmin)
1927 goto Ebusy;
1928 if (a->acregmax != b->acregmax)
1929 goto Ebusy;
1930 if (a->acdirmin != b->acdirmin)
1931 goto Ebusy;
1932 if (a->acdirmax != b->acdirmax)
1933 goto Ebusy;
1934 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1935 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001936 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001937Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001938 return 0;
1939}
1940
1941struct nfs_sb_mountdata {
1942 struct nfs_server *server;
1943 int mntflags;
1944};
1945
1946static int nfs_set_super(struct super_block *s, void *data)
1947{
1948 struct nfs_sb_mountdata *sb_mntdata = data;
1949 struct nfs_server *server = sb_mntdata->server;
1950 int ret;
1951
1952 s->s_flags = sb_mntdata->mntflags;
1953 s->s_fs_info = server;
1954 ret = set_anon_super(s, server);
1955 if (ret == 0)
1956 server->s_dev = s->s_dev;
1957 return ret;
1958}
1959
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001960static int nfs_compare_super_address(struct nfs_server *server1,
1961 struct nfs_server *server2)
1962{
1963 struct sockaddr *sap1, *sap2;
1964
1965 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1966 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1967
1968 if (sap1->sa_family != sap2->sa_family)
1969 return 0;
1970
1971 switch (sap1->sa_family) {
1972 case AF_INET: {
1973 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1974 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1975 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1976 return 0;
1977 if (sin1->sin_port != sin2->sin_port)
1978 return 0;
1979 break;
1980 }
1981 case AF_INET6: {
1982 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1983 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1984 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1985 return 0;
1986 if (sin1->sin6_port != sin2->sin6_port)
1987 return 0;
1988 break;
1989 }
1990 default:
1991 return 0;
1992 }
1993
1994 return 1;
1995}
1996
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001997static int nfs_compare_super(struct super_block *sb, void *data)
1998{
1999 struct nfs_sb_mountdata *sb_mntdata = data;
2000 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2001 int mntflags = sb_mntdata->mntflags;
2002
Chuck Leverfd00a8f2007-12-10 14:57:38 -05002003 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002004 return 0;
2005 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2006 if (old->flags & NFS_MOUNT_UNSHARED)
2007 return 0;
2008 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2009 return 0;
2010 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04002011}
2012
Miklos Szeredifa799752008-04-30 00:54:33 -07002013static int nfs_bdi_register(struct nfs_server *server)
2014{
2015 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2016}
2017
David Howells54ceac42006-08-22 20:06:13 -04002018static int nfs_get_sb(struct file_system_type *fs_type,
2019 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2020{
2021 struct nfs_server *server = NULL;
2022 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04002023 struct nfs_parsed_mount_data *data;
2024 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002025 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002026 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002027 struct nfs_sb_mountdata sb_mntdata = {
2028 .mntflags = flags,
2029 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002030 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04002031
Trond Myklebust33852a12008-06-19 14:20:11 -04002032 data = kzalloc(sizeof(*data), GFP_KERNEL);
2033 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2034 if (data == NULL || mntfh == NULL)
2035 goto out_free_fh;
2036
2037 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002038
David Howells54ceac42006-08-22 20:06:13 -04002039 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002040 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002041 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002042 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002043
2044 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002045 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002046 if (IS_ERR(server)) {
2047 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002048 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002049 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002050 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002051
Trond Myklebust75180df2007-05-16 16:53:28 -04002052 if (server->flags & NFS_MOUNT_UNSHARED)
2053 compare_super = NULL;
2054
David Howells54ceac42006-08-22 20:06:13 -04002055 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002056 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002057 if (IS_ERR(s)) {
2058 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002059 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002060 }
2061
David Howells54ceac42006-08-22 20:06:13 -04002062 if (s->s_fs_info != server) {
2063 nfs_free_server(server);
2064 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002065 } else {
2066 error = nfs_bdi_register(server);
2067 if (error)
2068 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002069 }
David Howells54ceac42006-08-22 20:06:13 -04002070
2071 if (!s->s_root) {
2072 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002073 nfs_fill_super(s, data);
David Howells08734042009-04-03 16:42:42 +01002074 nfs_fscache_get_super_cookie(s, data);
David Howells54ceac42006-08-22 20:06:13 -04002075 }
2076
Trond Myklebust33852a12008-06-19 14:20:11 -04002077 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002078 if (IS_ERR(mntroot)) {
2079 error = PTR_ERR(mntroot);
2080 goto error_splat_super;
2081 }
2082
Trond Myklebust33852a12008-06-19 14:20:11 -04002083 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002084 if (error)
2085 goto error_splat_root;
2086
David Howellsf7b422b2006-06-09 09:34:33 -04002087 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002088 mnt->mnt_sb = s;
2089 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002090 error = 0;
2091
2092out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002093 kfree(data->nfs_server.hostname);
2094 kfree(data->mount_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002095 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002096 security_free_mnt_opts(&data->lsm_opts);
2097out_free_fh:
2098 kfree(mntfh);
2099 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002100 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002101
David Howells54ceac42006-08-22 20:06:13 -04002102out_err_nosb:
2103 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002104 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002105
Eric Parisf9c3a382008-03-05 14:20:18 -05002106error_splat_root:
2107 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002108error_splat_super:
2109 up_write(&s->s_umount);
2110 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002111 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002112}
2113
David Howells54ceac42006-08-22 20:06:13 -04002114/*
2115 * Destroy an NFS2/3 superblock
2116 */
David Howellsf7b422b2006-06-09 09:34:33 -04002117static void nfs_kill_super(struct super_block *s)
2118{
2119 struct nfs_server *server = NFS_SB(s);
2120
Miklos Szeredifa799752008-04-30 00:54:33 -07002121 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002122 kill_anon_super(s);
David Howells08734042009-04-03 16:42:42 +01002123 nfs_fscache_release_super_cookie(s);
David Howells54ceac42006-08-22 20:06:13 -04002124 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002125}
2126
David Howells54ceac42006-08-22 20:06:13 -04002127/*
2128 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2129 */
2130static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2131 const char *dev_name, void *raw_data,
2132 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002133{
2134 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002135 struct super_block *s;
2136 struct nfs_server *server;
2137 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002138 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002139 struct nfs_sb_mountdata sb_mntdata = {
2140 .mntflags = flags,
2141 };
David Howells54ceac42006-08-22 20:06:13 -04002142 int error;
2143
2144 dprintk("--> nfs_xdev_get_sb()\n");
2145
2146 /* create a new volume representation */
2147 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2148 if (IS_ERR(server)) {
2149 error = PTR_ERR(server);
2150 goto out_err_noserver;
2151 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002152 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002153
Trond Myklebust75180df2007-05-16 16:53:28 -04002154 if (server->flags & NFS_MOUNT_UNSHARED)
2155 compare_super = NULL;
2156
David Howells54ceac42006-08-22 20:06:13 -04002157 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002158 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002159 if (IS_ERR(s)) {
2160 error = PTR_ERR(s);
2161 goto out_err_nosb;
2162 }
2163
2164 if (s->s_fs_info != server) {
2165 nfs_free_server(server);
2166 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002167 } else {
2168 error = nfs_bdi_register(server);
2169 if (error)
2170 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002171 }
2172
2173 if (!s->s_root) {
2174 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002175 nfs_clone_super(s, data->sb);
2176 }
2177
2178 mntroot = nfs_get_root(s, data->fh);
2179 if (IS_ERR(mntroot)) {
2180 error = PTR_ERR(mntroot);
2181 goto error_splat_super;
2182 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002183 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002184 dput(mntroot);
2185 error = -ESTALE;
2186 goto error_splat_super;
2187 }
David Howells54ceac42006-08-22 20:06:13 -04002188
2189 s->s_flags |= MS_ACTIVE;
2190 mnt->mnt_sb = s;
2191 mnt->mnt_root = mntroot;
2192
Eric Parisf9c3a382008-03-05 14:20:18 -05002193 /* clone any lsm security options from the parent to the new sb */
2194 security_sb_clone_mnt_opts(data->sb, s);
2195
David Howells54ceac42006-08-22 20:06:13 -04002196 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2197 return 0;
2198
2199out_err_nosb:
2200 nfs_free_server(server);
2201out_err_noserver:
2202 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2203 return error;
2204
2205error_splat_super:
2206 up_write(&s->s_umount);
2207 deactivate_super(s);
2208 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2209 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002210}
2211
2212#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002213
2214/*
2215 * Finish setting up a cloned NFS4 superblock
2216 */
2217static void nfs4_clone_super(struct super_block *sb,
2218 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002219{
David Howells54ceac42006-08-22 20:06:13 -04002220 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2221 sb->s_blocksize = old_sb->s_blocksize;
2222 sb->s_maxbytes = old_sb->s_maxbytes;
2223 sb->s_time_gran = 1;
2224 sb->s_op = old_sb->s_op;
2225 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002226}
2227
2228/*
2229 * Set up an NFS4 superblock
2230 */
David Howells54ceac42006-08-22 20:06:13 -04002231static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002232{
David Howellsf7b422b2006-06-09 09:34:33 -04002233 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002234 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002235 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002236}
2237
David Howells54ceac42006-08-22 20:06:13 -04002238/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002239 * Validate NFSv4 mount options
2240 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002241static int nfs4_validate_mount_data(void *options,
2242 struct nfs_parsed_mount_data *args,
2243 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002244{
Chuck Lever4c568012007-12-10 14:59:28 -05002245 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002246 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002247 char *c;
2248
2249 if (data == NULL)
2250 goto out_no_data;
2251
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002252 args->rsize = NFS_MAX_FILE_IO_SIZE;
2253 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002254 args->acregmin = NFS_DEF_ACREGMIN;
2255 args->acregmax = NFS_DEF_ACREGMAX;
2256 args->acdirmin = NFS_DEF_ACDIRMIN;
2257 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002258 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002259 args->auth_flavors[0] = RPC_AUTH_UNIX;
2260 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002261
Chuck Leverf0768eb2007-07-01 12:13:01 -04002262 switch (data->version) {
2263 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002264 ap = (struct sockaddr_in *)&args->nfs_server.address;
2265 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002266 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002267 if (data->host_addrlen == 0)
2268 goto out_no_address;
2269 args->nfs_server.addrlen = data->host_addrlen;
2270 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002271 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002272 if (!nfs_verify_server_address((struct sockaddr *)
2273 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002274 goto out_no_address;
2275
Chuck Lever6738b252008-06-24 16:33:54 -04002276 if (data->auth_flavourlen) {
2277 if (data->auth_flavourlen > 1)
2278 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002279 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002280 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002281 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002282 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002283 }
2284
2285 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2286 if (IS_ERR(c))
2287 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002288 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002289
2290 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2291 if (IS_ERR(c))
2292 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002293 args->nfs_server.export_path = c;
2294 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002295
2296 c = strndup_user(data->client_addr.data, 16);
2297 if (IS_ERR(c))
2298 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002299 args->client_address = c;
2300
2301 /*
2302 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2303 * can deal with.
2304 */
2305
2306 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2307 args->rsize = data->rsize;
2308 args->wsize = data->wsize;
2309 args->timeo = data->timeo;
2310 args->retrans = data->retrans;
2311 args->acregmin = data->acregmin;
2312 args->acregmax = data->acregmax;
2313 args->acdirmin = data->acdirmin;
2314 args->acdirmax = data->acdirmax;
2315 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002316 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002317
2318 break;
Chuck Lever80071222007-07-01 12:13:59 -04002319 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002320 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002321
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002322 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002323 return -EINVAL;
2324
2325 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002326 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002327 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002328
Chuck Levered596a82008-06-26 17:47:05 -04002329 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2330 args->nfs_server.port);
2331
Trond Myklebust259875e2008-07-02 14:43:47 -04002332 nfs_validate_transport_protocol(args);
2333
Chuck Lever6738b252008-06-24 16:33:54 -04002334 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002335 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002336
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002337 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002338 goto out_no_client_address;
2339
Chuck Leverdc045892008-06-23 12:36:37 -04002340 status = nfs_parse_devname(dev_name,
2341 &args->nfs_server.hostname,
2342 NFS4_MAXNAMLEN,
2343 &args->nfs_server.export_path,
2344 NFS4_MAXPATHLEN);
2345 if (status < 0)
2346 return status;
2347
Chuck Lever80071222007-07-01 12:13:59 -04002348 break;
2349 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002350 }
2351
2352 return 0;
2353
2354out_no_data:
2355 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2356 return -EINVAL;
2357
2358out_inval_auth:
2359 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2360 data->auth_flavourlen);
2361 return -EINVAL;
2362
2363out_no_address:
2364 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2365 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002366
2367out_no_client_address:
2368 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2369 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002370}
2371
2372/*
David Howells54ceac42006-08-22 20:06:13 -04002373 * Get the superblock for an NFS4 mountpoint
2374 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002375static int nfs4_get_sb(struct file_system_type *fs_type,
2376 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002377{
Trond Myklebust33852a12008-06-19 14:20:11 -04002378 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002379 struct super_block *s;
2380 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002381 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002382 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002383 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002384 struct nfs_sb_mountdata sb_mntdata = {
2385 .mntflags = flags,
2386 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002387 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002388
Trond Myklebust33852a12008-06-19 14:20:11 -04002389 data = kzalloc(sizeof(*data), GFP_KERNEL);
2390 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2391 if (data == NULL || mntfh == NULL)
2392 goto out_free_fh;
2393
2394 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002395
Chuck Leverf0768eb2007-07-01 12:13:01 -04002396 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002397 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002398 if (error < 0)
2399 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002400
David Howells54ceac42006-08-22 20:06:13 -04002401 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002402 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002403 if (IS_ERR(server)) {
2404 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002405 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002406 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002407 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002408
Trond Myklebust75180df2007-05-16 16:53:28 -04002409 if (server->flags & NFS4_MOUNT_UNSHARED)
2410 compare_super = NULL;
2411
David Howells54ceac42006-08-22 20:06:13 -04002412 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002413 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002414 if (IS_ERR(s)) {
2415 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002416 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002417 }
2418
Trond Myklebust5dd31772006-08-24 01:03:05 -04002419 if (s->s_fs_info != server) {
2420 nfs_free_server(server);
2421 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002422 } else {
2423 error = nfs_bdi_register(server);
2424 if (error)
2425 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002426 }
2427
David Howells54ceac42006-08-22 20:06:13 -04002428 if (!s->s_root) {
2429 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002430 nfs4_fill_super(s);
David Howells08734042009-04-03 16:42:42 +01002431 nfs_fscache_get_super_cookie(s, data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002432 }
David Howellsf7b422b2006-06-09 09:34:33 -04002433
Trond Myklebust33852a12008-06-19 14:20:11 -04002434 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002435 if (IS_ERR(mntroot)) {
2436 error = PTR_ERR(mntroot);
2437 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002438 }
David Howells54ceac42006-08-22 20:06:13 -04002439
Trond Myklebust33852a12008-06-19 14:20:11 -04002440 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002441 if (error)
2442 goto error_splat_root;
2443
David Howellsf7b422b2006-06-09 09:34:33 -04002444 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002445 mnt->mnt_sb = s;
2446 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002447 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002448
Chuck Lever29eb9812007-07-01 12:12:30 -04002449out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002450 kfree(data->client_address);
2451 kfree(data->nfs_server.export_path);
2452 kfree(data->nfs_server.hostname);
David Howells08734042009-04-03 16:42:42 +01002453 kfree(data->fscache_uniq);
Trond Myklebust33852a12008-06-19 14:20:11 -04002454 security_free_mnt_opts(&data->lsm_opts);
2455out_free_fh:
2456 kfree(mntfh);
2457 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002458 return error;
David Howells54ceac42006-08-22 20:06:13 -04002459
Chuck Lever29eb9812007-07-01 12:12:30 -04002460out_free:
2461 nfs_free_server(server);
2462 goto out;
2463
Eric Paris46c8ac72008-05-02 13:42:42 -07002464error_splat_root:
2465 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002466error_splat_super:
2467 up_write(&s->s_umount);
2468 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002469 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002470}
2471
2472static void nfs4_kill_super(struct super_block *sb)
2473{
2474 struct nfs_server *server = NFS_SB(sb);
2475
Trond Myklebust515d8612008-12-23 15:21:46 -05002476 nfs_super_return_all_delegations(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002477 kill_anon_super(sb);
2478
2479 nfs4_renewd_prepare_shutdown(server);
David Howells08734042009-04-03 16:42:42 +01002480 nfs_fscache_release_super_cookie(sb);
David Howells54ceac42006-08-22 20:06:13 -04002481 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002482}
2483
2484/*
David Howells54ceac42006-08-22 20:06:13 -04002485 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002486 */
David Howells54ceac42006-08-22 20:06:13 -04002487static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2488 const char *dev_name, void *raw_data,
2489 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002490{
2491 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002492 struct super_block *s;
2493 struct nfs_server *server;
2494 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002495 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002496 struct nfs_sb_mountdata sb_mntdata = {
2497 .mntflags = flags,
2498 };
David Howells54ceac42006-08-22 20:06:13 -04002499 int error;
2500
2501 dprintk("--> nfs4_xdev_get_sb()\n");
2502
2503 /* create a new volume representation */
2504 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2505 if (IS_ERR(server)) {
2506 error = PTR_ERR(server);
2507 goto out_err_noserver;
2508 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002509 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002510
Trond Myklebust75180df2007-05-16 16:53:28 -04002511 if (server->flags & NFS4_MOUNT_UNSHARED)
2512 compare_super = NULL;
2513
David Howells54ceac42006-08-22 20:06:13 -04002514 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002515 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002516 if (IS_ERR(s)) {
2517 error = PTR_ERR(s);
2518 goto out_err_nosb;
2519 }
2520
2521 if (s->s_fs_info != server) {
2522 nfs_free_server(server);
2523 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002524 } else {
2525 error = nfs_bdi_register(server);
2526 if (error)
2527 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002528 }
2529
2530 if (!s->s_root) {
2531 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002532 nfs4_clone_super(s, data->sb);
2533 }
2534
2535 mntroot = nfs4_get_root(s, data->fh);
2536 if (IS_ERR(mntroot)) {
2537 error = PTR_ERR(mntroot);
2538 goto error_splat_super;
2539 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002540 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2541 dput(mntroot);
2542 error = -ESTALE;
2543 goto error_splat_super;
2544 }
David Howells54ceac42006-08-22 20:06:13 -04002545
2546 s->s_flags |= MS_ACTIVE;
2547 mnt->mnt_sb = s;
2548 mnt->mnt_root = mntroot;
2549
Eric Paris46c8ac72008-05-02 13:42:42 -07002550 security_sb_clone_mnt_opts(data->sb, s);
2551
David Howells54ceac42006-08-22 20:06:13 -04002552 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2553 return 0;
2554
2555out_err_nosb:
2556 nfs_free_server(server);
2557out_err_noserver:
2558 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2559 return error;
2560
2561error_splat_super:
2562 up_write(&s->s_umount);
2563 deactivate_super(s);
2564 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2565 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002566}
2567
David Howells54ceac42006-08-22 20:06:13 -04002568/*
2569 * Create an NFS4 server record on referral traversal
2570 */
2571static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2572 const char *dev_name, void *raw_data,
2573 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002574{
2575 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002576 struct super_block *s;
2577 struct nfs_server *server;
2578 struct dentry *mntroot;
2579 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002580 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002581 struct nfs_sb_mountdata sb_mntdata = {
2582 .mntflags = flags,
2583 };
David Howells54ceac42006-08-22 20:06:13 -04002584 int error;
2585
2586 dprintk("--> nfs4_referral_get_sb()\n");
2587
2588 /* create a new volume representation */
2589 server = nfs4_create_referral_server(data, &mntfh);
2590 if (IS_ERR(server)) {
2591 error = PTR_ERR(server);
2592 goto out_err_noserver;
2593 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002594 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002595
Trond Myklebust75180df2007-05-16 16:53:28 -04002596 if (server->flags & NFS4_MOUNT_UNSHARED)
2597 compare_super = NULL;
2598
David Howells54ceac42006-08-22 20:06:13 -04002599 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002600 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002601 if (IS_ERR(s)) {
2602 error = PTR_ERR(s);
2603 goto out_err_nosb;
2604 }
2605
2606 if (s->s_fs_info != server) {
2607 nfs_free_server(server);
2608 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002609 } else {
2610 error = nfs_bdi_register(server);
2611 if (error)
2612 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002613 }
2614
2615 if (!s->s_root) {
2616 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002617 nfs4_fill_super(s);
2618 }
2619
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002620 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002621 if (IS_ERR(mntroot)) {
2622 error = PTR_ERR(mntroot);
2623 goto error_splat_super;
2624 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002625 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2626 dput(mntroot);
2627 error = -ESTALE;
2628 goto error_splat_super;
2629 }
David Howells54ceac42006-08-22 20:06:13 -04002630
2631 s->s_flags |= MS_ACTIVE;
2632 mnt->mnt_sb = s;
2633 mnt->mnt_root = mntroot;
2634
Eric Paris46c8ac72008-05-02 13:42:42 -07002635 security_sb_clone_mnt_opts(data->sb, s);
2636
David Howells54ceac42006-08-22 20:06:13 -04002637 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2638 return 0;
2639
2640out_err_nosb:
2641 nfs_free_server(server);
2642out_err_noserver:
2643 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2644 return error;
2645
2646error_splat_super:
2647 up_write(&s->s_umount);
2648 deactivate_super(s);
2649 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2650 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002651}
2652
David Howells54ceac42006-08-22 20:06:13 -04002653#endif /* CONFIG_NFS_V4 */