blob: d8e062fe76b110a5cc66c8f0098e427e483c49f7 [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"
63
64#define NFSDBG_FACILITY NFSDBG_VFS
65
Chuck Leverbf0fd762007-07-01 12:13:44 -040066enum {
67 /* Mount options that take no arguments */
68 Opt_soft, Opt_hard,
Chuck Leverbf0fd762007-07-01 12:13:44 -040069 Opt_posix, Opt_noposix,
70 Opt_cto, Opt_nocto,
71 Opt_ac, Opt_noac,
72 Opt_lock, Opt_nolock,
73 Opt_v2, Opt_v3,
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040074 Opt_udp, Opt_tcp, Opt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -040075 Opt_acl, Opt_noacl,
76 Opt_rdirplus, Opt_nordirplus,
Trond Myklebust75180df2007-05-16 16:53:28 -040077 Opt_sharecache, Opt_nosharecache,
Chuck Leverd7403512008-12-23 15:21:37 -050078 Opt_resvport, Opt_noresvport,
Chuck Leverbf0fd762007-07-01 12:13:44 -040079
80 /* Mount options that take integer arguments */
81 Opt_port,
82 Opt_rsize, Opt_wsize, Opt_bsize,
83 Opt_timeo, Opt_retrans,
84 Opt_acregmin, Opt_acregmax,
85 Opt_acdirmin, Opt_acdirmax,
86 Opt_actimeo,
87 Opt_namelen,
88 Opt_mountport,
Chuck Levere887cbc2007-10-26 13:32:29 -040089 Opt_mountvers,
Chuck Leverad879ce2007-10-26 13:32:24 -040090 Opt_nfsvers,
Chuck Leverbf0fd762007-07-01 12:13:44 -040091
92 /* Mount options that take string arguments */
Chuck Lever33832032007-12-10 14:59:13 -050093 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
Chuck Lever0ac83772007-09-11 18:01:04 -040094 Opt_addr, Opt_mountaddr, Opt_clientaddr,
Trond Myklebust7973c1f2008-07-15 17:58:14 -040095 Opt_lookupcache,
Chuck Leverbf0fd762007-07-01 12:13:44 -040096
Chuck Leverf45663c2008-06-24 19:28:02 -040097 /* Special mount options */
98 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -040099
100 Opt_err
101};
102
Steven Whitehousea447c092008-10-13 10:46:57 +0100103static const match_table_t nfs_mount_option_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400104 { Opt_userspace, "bg" },
105 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400106 { Opt_userspace, "retry=%s" },
107
Chuck Leverf45663c2008-06-24 19:28:02 -0400108 { Opt_sloppy, "sloppy" },
109
Chuck Leverbf0fd762007-07-01 12:13:44 -0400110 { Opt_soft, "soft" },
111 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400112 { Opt_deprecated, "intr" },
113 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400114 { Opt_posix, "posix" },
115 { Opt_noposix, "noposix" },
116 { Opt_cto, "cto" },
117 { Opt_nocto, "nocto" },
118 { Opt_ac, "ac" },
119 { Opt_noac, "noac" },
120 { Opt_lock, "lock" },
121 { Opt_nolock, "nolock" },
122 { Opt_v2, "v2" },
123 { Opt_v3, "v3" },
124 { Opt_udp, "udp" },
125 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400126 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400127 { Opt_acl, "acl" },
128 { Opt_noacl, "noacl" },
129 { Opt_rdirplus, "rdirplus" },
130 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400131 { Opt_sharecache, "sharecache" },
132 { Opt_nosharecache, "nosharecache" },
Chuck Leverd7403512008-12-23 15:21:37 -0500133 { Opt_resvport, "resvport" },
134 { Opt_noresvport, "noresvport" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400135
136 { Opt_port, "port=%u" },
137 { Opt_rsize, "rsize=%u" },
138 { Opt_wsize, "wsize=%u" },
139 { Opt_bsize, "bsize=%u" },
140 { Opt_timeo, "timeo=%u" },
141 { Opt_retrans, "retrans=%u" },
142 { Opt_acregmin, "acregmin=%u" },
143 { Opt_acregmax, "acregmax=%u" },
144 { Opt_acdirmin, "acdirmin=%u" },
145 { Opt_acdirmax, "acdirmax=%u" },
146 { Opt_actimeo, "actimeo=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400147 { Opt_namelen, "namlen=%u" },
148 { Opt_mountport, "mountport=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400149 { Opt_mountvers, "mountvers=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400150 { Opt_nfsvers, "nfsvers=%u" },
151 { Opt_nfsvers, "vers=%u" },
152
153 { Opt_sec, "sec=%s" },
154 { Opt_proto, "proto=%s" },
155 { Opt_mountproto, "mountproto=%s" },
156 { Opt_addr, "addr=%s" },
157 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500158 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400159 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400160
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400161 { Opt_lookupcache, "lookupcache=%s" },
162
Chuck Leverbf0fd762007-07-01 12:13:44 -0400163 { Opt_err, NULL }
164};
165
166enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400167 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400168
169 Opt_xprt_err
170};
171
Steven Whitehousea447c092008-10-13 10:46:57 +0100172static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400173 { Opt_xprt_udp, "udp" },
174 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400175 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400176
177 { Opt_xprt_err, NULL }
178};
179
180enum {
181 Opt_sec_none, Opt_sec_sys,
182 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
183 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
184 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
185
186 Opt_sec_err
187};
188
Steven Whitehousea447c092008-10-13 10:46:57 +0100189static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400190 { Opt_sec_none, "none" },
191 { Opt_sec_none, "null" },
192 { Opt_sec_sys, "sys" },
193
194 { Opt_sec_krb5, "krb5" },
195 { Opt_sec_krb5i, "krb5i" },
196 { Opt_sec_krb5p, "krb5p" },
197
198 { Opt_sec_lkey, "lkey" },
199 { Opt_sec_lkeyi, "lkeyi" },
200 { Opt_sec_lkeyp, "lkeyp" },
201
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500202 { Opt_sec_spkm, "spkm3" },
203 { Opt_sec_spkmi, "spkm3i" },
204 { Opt_sec_spkmp, "spkm3p" },
205
Chuck Leverbf0fd762007-07-01 12:13:44 -0400206 { Opt_sec_err, NULL }
207};
208
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400209enum {
210 Opt_lookupcache_all, Opt_lookupcache_positive,
211 Opt_lookupcache_none,
212
213 Opt_lookupcache_err
214};
215
216static match_table_t nfs_lookupcache_tokens = {
217 { Opt_lookupcache_all, "all" },
218 { Opt_lookupcache_positive, "pos" },
219 { Opt_lookupcache_positive, "positive" },
220 { Opt_lookupcache_none, "none" },
221
222 { Opt_lookupcache_err, NULL }
223};
224
Chuck Leverbf0fd762007-07-01 12:13:44 -0400225
Al Viro42faad92008-04-24 07:21:56 -0400226static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400227static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400228static int nfs_show_options(struct seq_file *, struct vfsmount *);
229static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400230static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400231static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400232 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400233static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400234static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400235
236static struct file_system_type nfs_fs_type = {
237 .owner = THIS_MODULE,
238 .name = "nfs",
239 .get_sb = nfs_get_sb,
240 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700241 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400242};
243
David Howells54ceac42006-08-22 20:06:13 -0400244struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400245 .owner = THIS_MODULE,
246 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400247 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400248 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700249 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400250};
251
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800252static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400253 .alloc_inode = nfs_alloc_inode,
254 .destroy_inode = nfs_destroy_inode,
255 .write_inode = nfs_write_inode,
256 .statfs = nfs_statfs,
257 .clear_inode = nfs_clear_inode,
258 .umount_begin = nfs_umount_begin,
259 .show_options = nfs_show_options,
260 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400261 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400262};
263
264#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400265static int nfs4_get_sb(struct file_system_type *fs_type,
266 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400267static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
268 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
269static int nfs4_referral_get_sb(struct file_system_type *fs_type,
270 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400271static void nfs4_kill_super(struct super_block *sb);
272
273static struct file_system_type nfs4_fs_type = {
274 .owner = THIS_MODULE,
275 .name = "nfs4",
276 .get_sb = nfs4_get_sb,
277 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700278 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400279};
280
David Howells54ceac42006-08-22 20:06:13 -0400281struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400282 .owner = THIS_MODULE,
283 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400284 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400285 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700286 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400287};
288
David Howells54ceac42006-08-22 20:06:13 -0400289struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400290 .owner = THIS_MODULE,
291 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400292 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400293 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700294 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400295};
296
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800297static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400298 .alloc_inode = nfs_alloc_inode,
299 .destroy_inode = nfs_destroy_inode,
300 .write_inode = nfs_write_inode,
301 .statfs = nfs_statfs,
302 .clear_inode = nfs4_clear_inode,
303 .umount_begin = nfs_umount_begin,
304 .show_options = nfs_show_options,
305 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400306 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400307};
308#endif
309
Rusty Russell8e1f9362007-07-17 04:03:17 -0700310static struct shrinker acl_shrinker = {
311 .shrink = nfs_access_cache_shrinker,
312 .seeks = DEFAULT_SEEKS,
313};
Trond Myklebust979df722006-07-25 11:28:19 -0400314
David Howellsf7b422b2006-06-09 09:34:33 -0400315/*
316 * Register the NFS filesystems
317 */
318int __init register_nfs_fs(void)
319{
320 int ret;
321
322 ret = register_filesystem(&nfs_fs_type);
323 if (ret < 0)
324 goto error_0;
325
David Howellsf7b422b2006-06-09 09:34:33 -0400326 ret = nfs_register_sysctl();
327 if (ret < 0)
328 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800329#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400330 ret = register_filesystem(&nfs4_fs_type);
331 if (ret < 0)
332 goto error_2;
333#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700334 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400335 return 0;
336
337#ifdef CONFIG_NFS_V4
338error_2:
339 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800340#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400341error_1:
342 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400343error_0:
344 return ret;
345}
346
347/*
348 * Unregister the NFS filesystems
349 */
350void __exit unregister_nfs_fs(void)
351{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700352 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400353#ifdef CONFIG_NFS_V4
354 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400355#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700356 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400357 unregister_filesystem(&nfs_fs_type);
358}
359
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400360void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500361{
362 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400363
364 if (atomic_inc_return(&server->active) == 1)
365 atomic_inc(&sb->s_active);
366}
367
368void nfs_sb_deactive(struct super_block *sb)
369{
370 struct nfs_server *server = NFS_SB(sb);
371
372 if (atomic_dec_and_test(&server->active))
373 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500374}
375
David Howellsf7b422b2006-06-09 09:34:33 -0400376/*
377 * Deliver file system statistics to userspace
378 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400379static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400380{
David Howells0c7d90c2006-08-22 20:06:10 -0400381 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400382 unsigned char blockbits;
383 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400384 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400385 struct nfs_fattr fattr;
386 struct nfs_fsstat res = {
387 .fattr = &fattr,
388 };
389 int error;
390
David Howells8fa5c002006-08-22 20:06:12 -0400391 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400392 if (error < 0)
393 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700394 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400395
396 /*
397 * Current versions of glibc do not correctly handle the
398 * case where f_frsize != f_bsize. Eventually we want to
399 * report the value of wtmult in this field.
400 */
David Howells0c7d90c2006-08-22 20:06:10 -0400401 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400402
403 /*
404 * On most *nix systems, f_blocks, f_bfree, and f_bavail
405 * are reported in units of f_frsize. Linux hasn't had
406 * an f_frsize field in its statfs struct until recently,
407 * thus historically Linux's sys_statfs reports these
408 * fields in units of f_bsize.
409 */
David Howells0c7d90c2006-08-22 20:06:10 -0400410 buf->f_bsize = dentry->d_sb->s_blocksize;
411 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400412 blockres = (1 << blockbits) - 1;
413 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
414 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
415 buf->f_bavail = (res.abytes + blockres) >> blockbits;
416
417 buf->f_files = res.tfiles;
418 buf->f_ffree = res.afiles;
419
420 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700421
David Howellsf7b422b2006-06-09 09:34:33 -0400422 return 0;
423
424 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700425 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700426 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400427}
428
David Howells7d4e2742006-08-22 20:06:07 -0400429/*
430 * Map the security flavour number to a name
431 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400432static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
433{
David Howells7d4e2742006-08-22 20:06:07 -0400434 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400435 rpc_authflavor_t flavour;
436 const char *str;
437 } sec_flavours[] = {
438 { RPC_AUTH_NULL, "null" },
439 { RPC_AUTH_UNIX, "sys" },
440 { RPC_AUTH_GSS_KRB5, "krb5" },
441 { RPC_AUTH_GSS_KRB5I, "krb5i" },
442 { RPC_AUTH_GSS_KRB5P, "krb5p" },
443 { RPC_AUTH_GSS_LKEY, "lkey" },
444 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
445 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
446 { RPC_AUTH_GSS_SPKM, "spkm" },
447 { RPC_AUTH_GSS_SPKMI, "spkmi" },
448 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400449 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400450 };
451 int i;
452
Chuck Lever4d81cd12007-07-01 12:12:40 -0400453 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400454 if (sec_flavours[i].flavour == flavour)
455 break;
456 }
457 return sec_flavours[i].str;
458}
459
Chuck Lever82d101d2008-03-14 14:10:37 -0400460static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
461 int showdefaults)
462{
463 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
464
465 switch (sap->sa_family) {
466 case AF_INET: {
467 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
468 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
469 NIPQUAD(sin->sin_addr.s_addr));
470 break;
471 }
472 case AF_INET6: {
473 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
474 seq_printf(m, ",mountaddr=" NIP6_FMT,
475 NIP6(sin6->sin6_addr));
476 break;
477 }
478 default:
479 if (showdefaults)
480 seq_printf(m, ",mountaddr=unspecified");
481 }
482
483 if (nfss->mountd_version || showdefaults)
484 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
485 if (nfss->mountd_port || showdefaults)
486 seq_printf(m, ",mountport=%u", nfss->mountd_port);
487
488 switch (nfss->mountd_protocol) {
489 case IPPROTO_UDP:
490 seq_printf(m, ",mountproto=udp");
491 break;
492 case IPPROTO_TCP:
493 seq_printf(m, ",mountproto=tcp");
494 break;
495 default:
496 if (showdefaults)
497 seq_printf(m, ",mountproto=auto");
498 }
499}
500
David Howellsf7b422b2006-06-09 09:34:33 -0400501/*
502 * Describe the mount options in force on this server representation
503 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400504static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
505 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400506{
David Howells509de812006-08-22 20:06:11 -0400507 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400508 int flag;
David Howells509de812006-08-22 20:06:11 -0400509 const char *str;
510 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400511 } nfs_info[] = {
512 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400513 { NFS_MOUNT_INTR, ",intr", ",nointr" },
514 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400515 { NFS_MOUNT_NOCTO, ",nocto", "" },
516 { NFS_MOUNT_NOAC, ",noac", "" },
517 { NFS_MOUNT_NONLM, ",nolock", "" },
518 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400519 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Chuck Leverd7403512008-12-23 15:21:37 -0500520 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
521 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400522 { 0, NULL, NULL }
523 };
David Howells509de812006-08-22 20:06:11 -0400524 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400525 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400526 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400527
Chuck Lever82d101d2008-03-14 14:10:37 -0400528 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400529 seq_printf(m, ",rsize=%u", nfss->rsize);
530 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400531 if (nfss->bsize != 0)
532 seq_printf(m, ",bsize=%u", nfss->bsize);
533 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400534 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400535 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400536 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400537 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400538 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400539 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400540 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400541 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400542 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
543 if (nfss->flags & nfs_infop->flag)
544 seq_puts(m, nfs_infop->str);
545 else
546 seq_puts(m, nfs_infop->nostr);
547 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400548 seq_printf(m, ",proto=%s",
549 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400550 if (version == 4) {
551 if (nfss->port != NFS_PORT)
552 seq_printf(m, ",port=%u", nfss->port);
553 } else
554 if (nfss->port)
555 seq_printf(m, ",port=%u", nfss->port);
556
Trond Myklebust33170232007-12-20 16:03:59 -0500557 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
558 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400559 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400560
561 if (version != 4)
562 nfs_show_mountd_options(m, nfss, showdefaults);
563
564#ifdef CONFIG_NFS_V4
565 if (clp->rpc_ops->version == 4)
566 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
567#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400568}
569
570/*
571 * Describe the mount options on this VFS mountpoint
572 */
573static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
574{
575 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
576
577 nfs_show_mount_options(m, nfss, 0);
578
Chuck Lever5d8515c2007-12-10 14:57:16 -0500579 seq_printf(m, ",addr=%s",
580 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
581 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400582
583 return 0;
584}
585
586/*
587 * Present statistical information for this VFS mountpoint
588 */
589static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
590{
591 int i, cpu;
592 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
593 struct rpc_auth *auth = nfss->client->cl_auth;
594 struct nfs_iostats totals = { };
595
596 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
597
598 /*
599 * Display all mount option settings
600 */
601 seq_printf(m, "\n\topts:\t");
602 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
603 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
604 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
605 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
606 nfs_show_mount_options(m, nfss, 1);
607
608 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
609
610 seq_printf(m, "\n\tcaps:\t");
611 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400612 seq_printf(m, ",wtmult=%u", nfss->wtmult);
613 seq_printf(m, ",dtsize=%u", nfss->dtsize);
614 seq_printf(m, ",bsize=%u", nfss->bsize);
615 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400616
617#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500618 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400619 seq_printf(m, "\n\tnfsv4:\t");
620 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
621 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
622 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
623 }
624#endif
625
626 /*
627 * Display security flavor in effect for this mount
628 */
Chuck Lever2d767432008-03-14 14:10:08 -0400629 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400630 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400631 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400632
633 /*
634 * Display superblock I/O counters
635 */
636 for_each_possible_cpu(cpu) {
637 struct nfs_iostats *stats;
638
639 preempt_disable();
640 stats = per_cpu_ptr(nfss->io_stats, cpu);
641
642 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
643 totals.events[i] += stats->events[i];
644 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
645 totals.bytes[i] += stats->bytes[i];
646
647 preempt_enable();
648 }
649
650 seq_printf(m, "\n\tevents:\t");
651 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
652 seq_printf(m, "%lu ", totals.events[i]);
653 seq_printf(m, "\n\tbytes:\t");
654 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
655 seq_printf(m, "%Lu ", totals.bytes[i]);
656 seq_printf(m, "\n");
657
658 rpc_print_iostats(m, nfss->client);
659
660 return 0;
661}
662
663/*
664 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400665 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400666 */
Al Viro42faad92008-04-24 07:21:56 -0400667static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400668{
Al Viro42faad92008-04-24 07:21:56 -0400669 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400670 struct rpc_clnt *rpc;
671
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400672 /* -EIO all pending I/O */
673 rpc = server->client_acl;
674 if (!IS_ERR(rpc))
675 rpc_killall_tasks(rpc);
676 rpc = server->client;
677 if (!IS_ERR(rpc))
678 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400679}
680
681/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500682 * Sanity-check a server address provided by the mount command.
683 *
684 * Address family must be initialized, and address must not be
685 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400686 */
687static int nfs_verify_server_address(struct sockaddr *addr)
688{
689 switch (addr->sa_family) {
690 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500691 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700692 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500693 }
694 case AF_INET6: {
695 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
696 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400697 }
698 }
699
700 return 0;
701}
702
Chuck Leverce3b7e12008-06-23 12:36:53 -0400703static void nfs_parse_ipv4_address(char *string, size_t str_len,
704 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500705{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400706 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
707 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500708
Chuck Leverce3b7e12008-06-23 12:36:53 -0400709 if (str_len <= INET_ADDRSTRLEN) {
710 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
711 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500712
Chuck Leverce3b7e12008-06-23 12:36:53 -0400713 sin->sin_family = AF_INET;
714 *addr_len = sizeof(*sin);
715 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500716 return;
717 }
Chuck Lever9412b922007-12-10 14:59:21 -0500718
719 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400720 *addr_len = 0;
721}
722
723#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400724static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
725 const char *delim,
726 struct sockaddr_in6 *sin6)
Chuck Leverd8e77482008-06-23 12:37:01 -0400727{
728 char *p;
729 size_t len;
730
Chuck Lever5e2e7722008-10-08 15:38:10 -0400731 if ((string + str_len) == delim)
732 return 1;
733
Chuck Leverd8e77482008-06-23 12:37:01 -0400734 if (*delim != IPV6_SCOPE_DELIMITER)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400735 return 0;
736
737 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
738 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400739
740 len = (string + str_len) - delim - 1;
741 p = kstrndup(delim + 1, len, GFP_KERNEL);
742 if (p) {
743 unsigned long scope_id = 0;
744 struct net_device *dev;
745
746 dev = dev_get_by_name(&init_net, p);
747 if (dev != NULL) {
748 scope_id = dev->ifindex;
749 dev_put(dev);
750 } else {
Chuck Lever5e2e7722008-10-08 15:38:10 -0400751 if (strict_strtoul(p, 10, &scope_id) == 0) {
752 kfree(p);
753 return 0;
754 }
Chuck Leverd8e77482008-06-23 12:37:01 -0400755 }
756
757 kfree(p);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400758
Chuck Leverd8e77482008-06-23 12:37:01 -0400759 sin6->sin6_scope_id = scope_id;
760 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400761 return 1;
Chuck Leverd8e77482008-06-23 12:37:01 -0400762 }
Chuck Lever5e2e7722008-10-08 15:38:10 -0400763
764 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400765}
766
Chuck Leverce3b7e12008-06-23 12:36:53 -0400767static void nfs_parse_ipv6_address(char *string, size_t str_len,
768 struct sockaddr *sap, size_t *addr_len)
769{
770 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
771 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400772 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400773
774 if (str_len <= INET6_ADDRSTRLEN) {
775 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
776 (int)str_len, string);
777
778 sin6->sin6_family = AF_INET6;
779 *addr_len = sizeof(*sin6);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400780 if (in6_pton(string, str_len, addr,
781 IPV6_SCOPE_DELIMITER, &delim) != 0) {
782 if (nfs_parse_ipv6_scope_id(string, str_len,
783 delim, sin6) != 0)
784 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400785 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400786 }
787
788 sap->sa_family = AF_UNSPEC;
789 *addr_len = 0;
790}
791#else
792static void nfs_parse_ipv6_address(char *string, size_t str_len,
793 struct sockaddr *sap, size_t *addr_len)
794{
795 sap->sa_family = AF_UNSPEC;
796 *addr_len = 0;
797}
798#endif
799
800/*
801 * Construct a sockaddr based on the contents of a string that contains
802 * an IP address in presentation format.
803 *
804 * If there is a problem constructing the new sockaddr, set the address
805 * family to AF_UNSPEC.
806 */
J. Bruce Fieldsea31a4432008-08-20 16:10:23 -0400807void nfs_parse_ip_address(char *string, size_t str_len,
Chuck Leverce3b7e12008-06-23 12:36:53 -0400808 struct sockaddr *sap, size_t *addr_len)
809{
810 unsigned int i, colons;
811
812 colons = 0;
813 for (i = 0; i < str_len; i++)
814 if (string[i] == ':')
815 colons++;
816
817 if (colons >= 2)
818 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
819 else
820 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500821}
822
823/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400824 * Sanity check the NFS transport protocol.
825 *
826 */
827static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
828{
829 switch (mnt->nfs_server.protocol) {
830 case XPRT_TRANSPORT_UDP:
831 case XPRT_TRANSPORT_TCP:
832 case XPRT_TRANSPORT_RDMA:
833 break;
834 default:
835 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
836 }
837}
838
839/*
840 * For text based NFSv2/v3 mounts, the mount protocol transport default
841 * settings should depend upon the specified NFS transport.
842 */
843static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
844{
845 nfs_validate_transport_protocol(mnt);
846
847 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
848 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
849 return;
850 switch (mnt->nfs_server.protocol) {
851 case XPRT_TRANSPORT_UDP:
852 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
853 break;
854 case XPRT_TRANSPORT_TCP:
855 case XPRT_TRANSPORT_RDMA:
856 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
857 }
858}
859
860/*
Chuck Lever01060c82008-06-24 16:33:38 -0400861 * Parse the value of the 'sec=' option.
862 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400863 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400864 */
865static int nfs_parse_security_flavors(char *value,
866 struct nfs_parsed_mount_data *mnt)
867{
868 substring_t args[MAX_OPT_ARGS];
869
870 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
871
872 switch (match_token(value, nfs_secflavor_tokens, args)) {
873 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400874 mnt->auth_flavor_len = 0;
875 mnt->auth_flavors[0] = RPC_AUTH_NULL;
876 break;
877 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400878 mnt->auth_flavor_len = 0;
879 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
880 break;
881 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400882 mnt->auth_flavor_len = 1;
883 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
884 break;
885 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400886 mnt->auth_flavor_len = 1;
887 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
888 break;
889 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400890 mnt->auth_flavor_len = 1;
891 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
892 break;
893 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400894 mnt->auth_flavor_len = 1;
895 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
896 break;
897 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400898 mnt->auth_flavor_len = 1;
899 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
900 break;
901 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400902 mnt->auth_flavor_len = 1;
903 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
904 break;
905 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400906 mnt->auth_flavor_len = 1;
907 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
908 break;
909 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400910 mnt->auth_flavor_len = 1;
911 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
912 break;
913 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400914 mnt->auth_flavor_len = 1;
915 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
916 break;
917 default:
918 return 0;
919 }
920
921 return 1;
922}
923
Chuck Leverf45663c2008-06-24 19:28:02 -0400924static void nfs_parse_invalid_value(const char *option)
925{
926 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
927}
928
Chuck Lever01060c82008-06-24 16:33:38 -0400929/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400930 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400931 * a data structure. The whole mount string is processed; bad options are
932 * skipped as they are encountered. If there were no errors, return 1;
933 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400934 */
935static int nfs_parse_mount_options(char *raw,
936 struct nfs_parsed_mount_data *mnt)
937{
Eric Parisf9c3a382008-03-05 14:20:18 -0500938 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400939 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400940
941 if (!raw) {
942 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
943 return 1;
944 }
945 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
946
Eric Parisf9c3a382008-03-05 14:20:18 -0500947 secdata = alloc_secdata();
948 if (!secdata)
949 goto out_nomem;
950
951 rc = security_sb_copy_data(raw, secdata);
952 if (rc)
953 goto out_security_failure;
954
955 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
956 if (rc)
957 goto out_security_failure;
958
959 free_secdata(secdata);
960
Chuck Leverbf0fd762007-07-01 12:13:44 -0400961 while ((p = strsep(&raw, ",")) != NULL) {
962 substring_t args[MAX_OPT_ARGS];
963 int option, token;
964
965 if (!*p)
966 continue;
967
968 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
969
970 token = match_token(p, nfs_mount_option_tokens, args);
971 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400972
973 /*
974 * boolean options: foo/nofoo
975 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400976 case Opt_soft:
977 mnt->flags |= NFS_MOUNT_SOFT;
978 break;
979 case Opt_hard:
980 mnt->flags &= ~NFS_MOUNT_SOFT;
981 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400982 case Opt_posix:
983 mnt->flags |= NFS_MOUNT_POSIX;
984 break;
985 case Opt_noposix:
986 mnt->flags &= ~NFS_MOUNT_POSIX;
987 break;
988 case Opt_cto:
989 mnt->flags &= ~NFS_MOUNT_NOCTO;
990 break;
991 case Opt_nocto:
992 mnt->flags |= NFS_MOUNT_NOCTO;
993 break;
994 case Opt_ac:
995 mnt->flags &= ~NFS_MOUNT_NOAC;
996 break;
997 case Opt_noac:
998 mnt->flags |= NFS_MOUNT_NOAC;
999 break;
1000 case Opt_lock:
1001 mnt->flags &= ~NFS_MOUNT_NONLM;
1002 break;
1003 case Opt_nolock:
1004 mnt->flags |= NFS_MOUNT_NONLM;
1005 break;
1006 case Opt_v2:
1007 mnt->flags &= ~NFS_MOUNT_VER3;
1008 break;
1009 case Opt_v3:
1010 mnt->flags |= NFS_MOUNT_VER3;
1011 break;
1012 case Opt_udp:
1013 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001014 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001015 break;
1016 case Opt_tcp:
1017 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001018 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001019 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001020 case Opt_rdma:
1021 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1022 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001023 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001024 case Opt_acl:
1025 mnt->flags &= ~NFS_MOUNT_NOACL;
1026 break;
1027 case Opt_noacl:
1028 mnt->flags |= NFS_MOUNT_NOACL;
1029 break;
1030 case Opt_rdirplus:
1031 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1032 break;
1033 case Opt_nordirplus:
1034 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1035 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001036 case Opt_sharecache:
1037 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1038 break;
1039 case Opt_nosharecache:
1040 mnt->flags |= NFS_MOUNT_UNSHARED;
1041 break;
Chuck Leverd7403512008-12-23 15:21:37 -05001042 case Opt_resvport:
1043 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1044 break;
1045 case Opt_noresvport:
1046 mnt->flags |= NFS_MOUNT_NORESVPORT;
1047 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001048
Chuck Leverf45663c2008-06-24 19:28:02 -04001049 /*
1050 * options that take numeric values
1051 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001052 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001053 if (match_int(args, &option) ||
1054 option < 0 || option > USHORT_MAX) {
1055 errors++;
1056 nfs_parse_invalid_value("port");
1057 } else
1058 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001059 break;
1060 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001061 if (match_int(args, &option) || option < 0) {
1062 errors++;
1063 nfs_parse_invalid_value("rsize");
1064 } else
1065 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001066 break;
1067 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001068 if (match_int(args, &option) || option < 0) {
1069 errors++;
1070 nfs_parse_invalid_value("wsize");
1071 } else
1072 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001073 break;
1074 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001075 if (match_int(args, &option) || option < 0) {
1076 errors++;
1077 nfs_parse_invalid_value("bsize");
1078 } else
1079 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001080 break;
1081 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001082 if (match_int(args, &option) || option <= 0) {
1083 errors++;
1084 nfs_parse_invalid_value("timeo");
1085 } else
1086 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001087 break;
1088 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001089 if (match_int(args, &option) || option <= 0) {
1090 errors++;
1091 nfs_parse_invalid_value("retrans");
1092 } else
1093 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001094 break;
1095 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001096 if (match_int(args, &option) || option < 0) {
1097 errors++;
1098 nfs_parse_invalid_value("acregmin");
1099 } else
1100 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001101 break;
1102 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001103 if (match_int(args, &option) || option < 0) {
1104 errors++;
1105 nfs_parse_invalid_value("acregmax");
1106 } else
1107 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001108 break;
1109 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001110 if (match_int(args, &option) || option < 0) {
1111 errors++;
1112 nfs_parse_invalid_value("acdirmin");
1113 } else
1114 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001115 break;
1116 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001117 if (match_int(args, &option) || option < 0) {
1118 errors++;
1119 nfs_parse_invalid_value("acdirmax");
1120 } else
1121 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001122 break;
1123 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001124 if (match_int(args, &option) || option < 0) {
1125 errors++;
1126 nfs_parse_invalid_value("actimeo");
1127 } else
1128 mnt->acregmin = mnt->acregmax =
1129 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001130 break;
1131 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001132 if (match_int(args, &option) || option < 0) {
1133 errors++;
1134 nfs_parse_invalid_value("namlen");
1135 } else
1136 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001137 break;
1138 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001139 if (match_int(args, &option) ||
1140 option < 0 || option > USHORT_MAX) {
1141 errors++;
1142 nfs_parse_invalid_value("mountport");
1143 } else
1144 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001145 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001146 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001147 if (match_int(args, &option) ||
1148 option < NFS_MNT_VERSION ||
1149 option > NFS_MNT3_VERSION) {
1150 errors++;
1151 nfs_parse_invalid_value("mountvers");
1152 } else
1153 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001154 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001155 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001156 if (match_int(args, &option)) {
1157 errors++;
1158 nfs_parse_invalid_value("nfsvers");
1159 break;
1160 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001161 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001162 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001163 mnt->flags &= ~NFS_MOUNT_VER3;
1164 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001165 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001166 mnt->flags |= NFS_MOUNT_VER3;
1167 break;
1168 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001169 errors++;
1170 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001171 }
1172 break;
1173
Chuck Leverf45663c2008-06-24 19:28:02 -04001174 /*
1175 * options that take text values
1176 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001177 case Opt_sec:
1178 string = match_strdup(args);
1179 if (string == NULL)
1180 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001181 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001182 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001183 if (!rc) {
1184 errors++;
1185 dfprintk(MOUNT, "NFS: unrecognized "
1186 "security flavor\n");
1187 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001188 break;
1189 case Opt_proto:
1190 string = match_strdup(args);
1191 if (string == NULL)
1192 goto out_nomem;
1193 token = match_token(string,
1194 nfs_xprt_protocol_tokens, args);
1195 kfree(string);
1196
1197 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001198 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001199 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001200 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001201 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001202 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001203 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001204 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001205 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001206 case Opt_xprt_rdma:
1207 /* vector side protocols to TCP */
1208 mnt->flags |= NFS_MOUNT_TCP;
1209 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001210 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001211 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001212 errors++;
1213 dfprintk(MOUNT, "NFS: unrecognized "
1214 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001215 }
1216 break;
1217 case Opt_mountproto:
1218 string = match_strdup(args);
1219 if (string == NULL)
1220 goto out_nomem;
1221 token = match_token(string,
1222 nfs_xprt_protocol_tokens, args);
1223 kfree(string);
1224
1225 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001226 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001227 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001228 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001229 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001230 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001231 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001232 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001233 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001234 errors++;
1235 dfprintk(MOUNT, "NFS: unrecognized "
1236 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001237 }
1238 break;
1239 case Opt_addr:
1240 string = match_strdup(args);
1241 if (string == NULL)
1242 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001243 nfs_parse_ip_address(string, strlen(string),
1244 (struct sockaddr *)
1245 &mnt->nfs_server.address,
1246 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001247 kfree(string);
1248 break;
1249 case Opt_clientaddr:
1250 string = match_strdup(args);
1251 if (string == NULL)
1252 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001253 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001254 mnt->client_address = string;
1255 break;
Chuck Lever33832032007-12-10 14:59:13 -05001256 case Opt_mounthost:
1257 string = match_strdup(args);
1258 if (string == NULL)
1259 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001260 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001261 mnt->mount_server.hostname = string;
1262 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001263 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001264 string = match_strdup(args);
1265 if (string == NULL)
1266 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001267 nfs_parse_ip_address(string, strlen(string),
1268 (struct sockaddr *)
1269 &mnt->mount_server.address,
1270 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001271 kfree(string);
1272 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001273 case Opt_lookupcache:
1274 string = match_strdup(args);
1275 if (string == NULL)
1276 goto out_nomem;
1277 token = match_token(string,
1278 nfs_lookupcache_tokens, args);
1279 kfree(string);
1280 switch (token) {
1281 case Opt_lookupcache_all:
1282 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1283 break;
1284 case Opt_lookupcache_positive:
1285 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1286 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1287 break;
1288 case Opt_lookupcache_none:
1289 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1290 break;
1291 default:
1292 errors++;
1293 dfprintk(MOUNT, "NFS: invalid "
1294 "lookupcache argument\n");
1295 };
1296 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001297
Chuck Leverf45663c2008-06-24 19:28:02 -04001298 /*
1299 * Special options
1300 */
1301 case Opt_sloppy:
1302 sloppy = 1;
1303 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1304 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001305 case Opt_userspace:
1306 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001307 dfprintk(MOUNT, "NFS: ignoring mount option "
1308 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001309 break;
1310
1311 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001312 errors++;
1313 dfprintk(MOUNT, "NFS: unrecognized mount option "
1314 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001315 }
1316 }
1317
Chuck Leveraf904de2008-09-08 11:58:13 -04001318 if (errors > 0) {
1319 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1320 errors, (errors == 1 ? "" : "s"));
1321 if (!sloppy)
1322 return 0;
1323 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001324 return 1;
1325
1326out_nomem:
1327 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1328 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001329out_security_failure:
1330 free_secdata(secdata);
1331 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1332 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001333}
1334
1335/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001336 * Use the remote server's MOUNT service to request the NFS file handle
1337 * corresponding to the provided path.
1338 */
1339static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1340 struct nfs_fh *root_fh)
1341{
Chuck Leverc5d120f2008-12-23 15:21:35 -05001342 struct nfs_mount_request request = {
1343 .sap = (struct sockaddr *)
1344 &args->mount_server.address,
1345 .dirpath = args->nfs_server.export_path,
1346 .protocol = args->mount_server.protocol,
1347 .fh = root_fh,
Chuck Lever50a737f2008-12-23 15:21:37 -05001348 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
Chuck Leverc5d120f2008-12-23 15:21:35 -05001349 };
Chuck Lever4c568012007-12-10 14:59:28 -05001350 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001351
1352 if (args->mount_server.version == 0) {
1353 if (args->flags & NFS_MOUNT_VER3)
1354 args->mount_server.version = NFS_MNT3_VERSION;
1355 else
1356 args->mount_server.version = NFS_MNT_VERSION;
1357 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001358 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001359
Chuck Lever33832032007-12-10 14:59:13 -05001360 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001361 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001362 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001363 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001364
Chuck Lever0076d7b2007-07-01 12:13:49 -04001365 /*
1366 * Construct the mount server's address.
1367 */
Chuck Lever4c568012007-12-10 14:59:28 -05001368 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001369 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001370 args->nfs_server.addrlen);
1371 args->mount_server.addrlen = args->nfs_server.addrlen;
1372 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001373 request.salen = args->mount_server.addrlen;
Chuck Lever4c568012007-12-10 14:59:28 -05001374
James Lentiniaad70002007-09-24 17:32:49 -04001375 /*
1376 * autobind will be used if mount_server.port == 0
1377 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001378 nfs_set_port(request.sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001379
1380 /*
1381 * Now ask the mount server to map our export path
1382 * to a file handle.
1383 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001384 status = nfs_mount(&request);
Chuck Leverefd83402007-09-11 18:00:58 -04001385 if (status == 0)
1386 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001387
Chuck Lever396cee972008-06-12 12:37:49 -04001388 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Leverc5d120f2008-12-23 15:21:35 -05001389 request.hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001390 return status;
1391}
1392
Chuck Leverd1aa0822008-06-23 12:36:45 -04001393static int nfs_parse_simple_hostname(const char *dev_name,
1394 char **hostname, size_t maxnamlen,
1395 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001396{
1397 size_t len;
1398 char *colon, *comma;
1399
1400 colon = strchr(dev_name, ':');
1401 if (colon == NULL)
1402 goto out_bad_devname;
1403
1404 len = colon - dev_name;
1405 if (len > maxnamlen)
1406 goto out_hostname;
1407
1408 /* N.B. caller will free nfs_server.hostname in all cases */
1409 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1410 if (!*hostname)
1411 goto out_nomem;
1412
1413 /* kill possible hostname list: not supported */
1414 comma = strchr(*hostname, ',');
1415 if (comma != NULL) {
1416 if (comma == *hostname)
1417 goto out_bad_devname;
1418 *comma = '\0';
1419 }
1420
1421 colon++;
1422 len = strlen(colon);
1423 if (len > maxpathlen)
1424 goto out_path;
1425 *export_path = kstrndup(colon, len, GFP_KERNEL);
1426 if (!*export_path)
1427 goto out_nomem;
1428
1429 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1430 return 0;
1431
1432out_bad_devname:
1433 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1434 return -EINVAL;
1435
1436out_nomem:
1437 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1438 return -ENOMEM;
1439
1440out_hostname:
1441 dfprintk(MOUNT, "NFS: server hostname too long\n");
1442 return -ENAMETOOLONG;
1443
1444out_path:
1445 dfprintk(MOUNT, "NFS: export pathname too long\n");
1446 return -ENAMETOOLONG;
1447}
1448
1449/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001450 * Hostname has square brackets around it because it contains one or
1451 * more colons. We look for the first closing square bracket, and a
1452 * colon must follow it.
1453 */
1454static int nfs_parse_protected_hostname(const char *dev_name,
1455 char **hostname, size_t maxnamlen,
1456 char **export_path, size_t maxpathlen)
1457{
1458 size_t len;
1459 char *start, *end;
1460
1461 start = (char *)(dev_name + 1);
1462
1463 end = strchr(start, ']');
1464 if (end == NULL)
1465 goto out_bad_devname;
1466 if (*(end + 1) != ':')
1467 goto out_bad_devname;
1468
1469 len = end - start;
1470 if (len > maxnamlen)
1471 goto out_hostname;
1472
1473 /* N.B. caller will free nfs_server.hostname in all cases */
1474 *hostname = kstrndup(start, len, GFP_KERNEL);
1475 if (*hostname == NULL)
1476 goto out_nomem;
1477
1478 end += 2;
1479 len = strlen(end);
1480 if (len > maxpathlen)
1481 goto out_path;
1482 *export_path = kstrndup(end, len, GFP_KERNEL);
1483 if (!*export_path)
1484 goto out_nomem;
1485
1486 return 0;
1487
1488out_bad_devname:
1489 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1490 return -EINVAL;
1491
1492out_nomem:
1493 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1494 return -ENOMEM;
1495
1496out_hostname:
1497 dfprintk(MOUNT, "NFS: server hostname too long\n");
1498 return -ENAMETOOLONG;
1499
1500out_path:
1501 dfprintk(MOUNT, "NFS: export pathname too long\n");
1502 return -ENAMETOOLONG;
1503}
1504
1505/*
1506 * Split "dev_name" into "hostname:export_path".
1507 *
1508 * The leftmost colon demarks the split between the server's hostname
1509 * and the export path. If the hostname starts with a left square
1510 * bracket, then it may contain colons.
1511 *
1512 * Note: caller frees hostname and export path, even on error.
1513 */
1514static int nfs_parse_devname(const char *dev_name,
1515 char **hostname, size_t maxnamlen,
1516 char **export_path, size_t maxpathlen)
1517{
1518 if (*dev_name == '[')
1519 return nfs_parse_protected_hostname(dev_name,
1520 hostname, maxnamlen,
1521 export_path, maxpathlen);
1522
1523 return nfs_parse_simple_hostname(dev_name,
1524 hostname, maxnamlen,
1525 export_path, maxpathlen);
1526}
1527
1528/*
David Howells54ceac42006-08-22 20:06:13 -04001529 * Validate the NFS2/NFS3 mount data
1530 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001531 *
1532 * For option strings, user space handles the following behaviors:
1533 *
1534 * + DNS: mapping server host name to IP address ("addr=" option)
1535 *
1536 * + failure mode: how to behave if a mount request can't be handled
1537 * immediately ("fg/bg" option)
1538 *
1539 * + retry: how often to retry a mount request ("retry=" option)
1540 *
1541 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1542 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001543 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001544static int nfs_validate_mount_data(void *options,
1545 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001546 struct nfs_fh *mntfh,
1547 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001548{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001549 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001550
Chuck Lever5df36e72007-07-01 12:12:56 -04001551 if (data == NULL)
1552 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001553
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001554 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1555 args->rsize = NFS_MAX_FILE_IO_SIZE;
1556 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001557 args->acregmin = NFS_DEF_ACREGMIN;
1558 args->acregmax = NFS_DEF_ACREGMAX;
1559 args->acdirmin = NFS_DEF_ACDIRMIN;
1560 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001561 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001562 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001563 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001564 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001565
David Howellsf7b422b2006-06-09 09:34:33 -04001566 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001567 case 1:
1568 data->namlen = 0;
1569 case 2:
1570 data->bsize = 0;
1571 case 3:
1572 if (data->flags & NFS_MOUNT_VER3)
1573 goto out_no_v3;
1574 data->root.size = NFS2_FHSIZE;
1575 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1576 case 4:
1577 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1578 goto out_no_sec;
1579 case 5:
1580 memset(data->context, 0, sizeof(data->context));
1581 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001582 if (data->flags & NFS_MOUNT_VER3) {
1583 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1584 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001585 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001586 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001587 mntfh->size = NFS2_FHSIZE;
1588
Chuck Lever5df36e72007-07-01 12:12:56 -04001589
1590 memcpy(mntfh->data, data->root.data, mntfh->size);
1591 if (mntfh->size < sizeof(mntfh->data))
1592 memset(mntfh->data + mntfh->size, 0,
1593 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001594
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001595 /*
1596 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1597 * can deal with.
1598 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001599 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001600 args->rsize = data->rsize;
1601 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001602 args->timeo = data->timeo;
1603 args->retrans = data->retrans;
1604 args->acregmin = data->acregmin;
1605 args->acregmax = data->acregmax;
1606 args->acdirmin = data->acdirmin;
1607 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001608
1609 memcpy(&args->nfs_server.address, &data->addr,
1610 sizeof(data->addr));
1611 args->nfs_server.addrlen = sizeof(data->addr);
1612 if (!nfs_verify_server_address((struct sockaddr *)
1613 &args->nfs_server.address))
1614 goto out_no_address;
1615
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001616 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001617 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001618 /* N.B. caller will free nfs_server.hostname in all cases */
1619 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1620 args->namlen = data->namlen;
1621 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001622
1623 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1624 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001625 if (!args->nfs_server.hostname)
1626 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001627
1628 /*
1629 * The legacy version 6 binary mount data from userspace has a
1630 * field used only to transport selinux information into the
1631 * the kernel. To continue to support that functionality we
1632 * have a touch of selinux knowledge here in the NFS code. The
1633 * userspace code converted context=blah to just blah so we are
1634 * converting back to the full string selinux understands.
1635 */
1636 if (data->context[0]){
1637#ifdef CONFIG_SECURITY_SELINUX
1638 int rc;
1639 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1640 if (!opts_str)
1641 return -ENOMEM;
1642 strcpy(opts_str, "context=");
1643 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1644 strcat(opts_str, &data->context[0]);
1645 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1646 kfree(opts_str);
1647 if (rc)
1648 return rc;
1649#else
1650 return -EINVAL;
1651#endif
1652 }
1653
Chuck Lever5df36e72007-07-01 12:12:56 -04001654 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001655 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001656 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001657
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001658 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001659 return -EINVAL;
1660
Chuck Lever6e88e062007-09-24 15:39:50 -04001661 if (!nfs_verify_server_address((struct sockaddr *)
1662 &args->nfs_server.address))
1663 goto out_no_address;
1664
Chuck Levered596a82008-06-26 17:47:05 -04001665 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1666 args->nfs_server.port);
1667
Trond Myklebust259875e2008-07-02 14:43:47 -04001668 nfs_set_mount_transport_protocol(args);
1669
Chuck Leverdc045892008-06-23 12:36:37 -04001670 status = nfs_parse_devname(dev_name,
1671 &args->nfs_server.hostname,
1672 PAGE_SIZE,
1673 &args->nfs_server.export_path,
1674 NFS_MAXPATHLEN);
1675 if (!status)
1676 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001677
Chuck Leverdc045892008-06-23 12:36:37 -04001678 kfree(args->nfs_server.export_path);
1679 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001680
Chuck Lever136d5582007-07-01 12:13:54 -04001681 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001682 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001683
Chuck Lever136d5582007-07-01 12:13:54 -04001684 break;
1685 }
David Howellsf7b422b2006-06-09 09:34:33 -04001686 }
David Howells54ceac42006-08-22 20:06:13 -04001687
David Howellsf7b422b2006-06-09 09:34:33 -04001688#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001689 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001690 goto out_v3_not_compiled;
1691#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001692
David Howells54ceac42006-08-22 20:06:13 -04001693 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001694
1695out_no_data:
1696 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1697 return -EINVAL;
1698
1699out_no_v3:
1700 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1701 data->version);
1702 return -EINVAL;
1703
1704out_no_sec:
1705 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1706 return -EINVAL;
1707
Chuck Lever5df36e72007-07-01 12:12:56 -04001708#ifndef CONFIG_NFS_V3
1709out_v3_not_compiled:
1710 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1711 return -EPROTONOSUPPORT;
1712#endif /* !CONFIG_NFS_V3 */
1713
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001714out_nomem:
1715 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1716 return -ENOMEM;
1717
Chuck Lever5df36e72007-07-01 12:12:56 -04001718out_no_address:
1719 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1720 return -EINVAL;
1721
1722out_invalid_fh:
1723 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1724 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001725}
1726
Jeff Layton48b605f2008-06-10 15:38:39 -04001727static int
1728nfs_compare_remount_data(struct nfs_server *nfss,
1729 struct nfs_parsed_mount_data *data)
1730{
1731 if (data->flags != nfss->flags ||
1732 data->rsize != nfss->rsize ||
1733 data->wsize != nfss->wsize ||
1734 data->retrans != nfss->client->cl_timeout->to_retries ||
1735 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1736 data->acregmin != nfss->acregmin / HZ ||
1737 data->acregmax != nfss->acregmax / HZ ||
1738 data->acdirmin != nfss->acdirmin / HZ ||
1739 data->acdirmax != nfss->acdirmax / HZ ||
1740 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1741 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1742 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1743 data->nfs_server.addrlen) != 0)
1744 return -EINVAL;
1745
1746 return 0;
1747}
1748
1749static int
1750nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1751{
1752 int error;
1753 struct nfs_server *nfss = sb->s_fs_info;
1754 struct nfs_parsed_mount_data *data;
1755 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1756 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001757 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001758
1759 /*
1760 * Userspace mount programs that send binary options generally send
1761 * them populated with default values. We have no way to know which
1762 * ones were explicitly specified. Fall back to legacy behavior and
1763 * just return success.
1764 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001765 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1766 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1767 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001768 return 0;
1769
1770 data = kzalloc(sizeof(*data), GFP_KERNEL);
1771 if (data == NULL)
1772 return -ENOMEM;
1773
1774 /* fill out struct with values from existing mount */
1775 data->flags = nfss->flags;
1776 data->rsize = nfss->rsize;
1777 data->wsize = nfss->wsize;
1778 data->retrans = nfss->client->cl_timeout->to_retries;
1779 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1780 data->acregmin = nfss->acregmin / HZ;
1781 data->acregmax = nfss->acregmax / HZ;
1782 data->acdirmin = nfss->acdirmin / HZ;
1783 data->acdirmax = nfss->acdirmax / HZ;
1784 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1785 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1786 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1787 data->nfs_server.addrlen);
1788
1789 /* overwrite those values with any that were specified */
1790 error = nfs_parse_mount_options((char *)options, data);
1791 if (error < 0)
1792 goto out;
1793
1794 /* compare new mount options with old ones */
1795 error = nfs_compare_remount_data(nfss, data);
1796out:
1797 kfree(data);
1798 return error;
1799}
1800
David Howells54ceac42006-08-22 20:06:13 -04001801/*
1802 * Initialise the common bits of the superblock
1803 */
1804static inline void nfs_initialise_sb(struct super_block *sb)
1805{
1806 struct nfs_server *server = NFS_SB(sb);
1807
1808 sb->s_magic = NFS_SUPER_MAGIC;
1809
1810 /* We probably want something more informative here */
1811 snprintf(sb->s_id, sizeof(sb->s_id),
1812 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1813
1814 if (sb->s_blocksize == 0)
1815 sb->s_blocksize = nfs_block_bits(server->wsize,
1816 &sb->s_blocksize_bits);
1817
1818 if (server->flags & NFS_MOUNT_NOAC)
1819 sb->s_flags |= MS_SYNCHRONOUS;
1820
1821 nfs_super_set_maxbytes(sb, server->maxfilesize);
1822}
1823
1824/*
1825 * Finish setting up an NFS2/3 superblock
1826 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001827static void nfs_fill_super(struct super_block *sb,
1828 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001829{
1830 struct nfs_server *server = NFS_SB(sb);
1831
1832 sb->s_blocksize_bits = 0;
1833 sb->s_blocksize = 0;
1834 if (data->bsize)
1835 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1836
1837 if (server->flags & NFS_MOUNT_VER3) {
1838 /* The VFS shouldn't apply the umask to mode bits. We will do
1839 * so ourselves when necessary.
1840 */
1841 sb->s_flags |= MS_POSIXACL;
1842 sb->s_time_gran = 1;
1843 }
1844
1845 sb->s_op = &nfs_sops;
1846 nfs_initialise_sb(sb);
1847}
1848
1849/*
1850 * Finish setting up a cloned NFS2/3 superblock
1851 */
1852static void nfs_clone_super(struct super_block *sb,
1853 const struct super_block *old_sb)
1854{
1855 struct nfs_server *server = NFS_SB(sb);
1856
1857 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1858 sb->s_blocksize = old_sb->s_blocksize;
1859 sb->s_maxbytes = old_sb->s_maxbytes;
1860
1861 if (server->flags & NFS_MOUNT_VER3) {
1862 /* The VFS shouldn't apply the umask to mode bits. We will do
1863 * so ourselves when necessary.
1864 */
1865 sb->s_flags |= MS_POSIXACL;
1866 sb->s_time_gran = 1;
1867 }
1868
1869 sb->s_op = old_sb->s_op;
1870 nfs_initialise_sb(sb);
1871}
1872
Trond Myklebust275a5d22007-05-16 16:53:28 -04001873#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1874
1875static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1876{
1877 const struct nfs_server *a = s->s_fs_info;
1878 const struct rpc_clnt *clnt_a = a->client;
1879 const struct rpc_clnt *clnt_b = b->client;
1880
1881 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1882 goto Ebusy;
1883 if (a->nfs_client != b->nfs_client)
1884 goto Ebusy;
1885 if (a->flags != b->flags)
1886 goto Ebusy;
1887 if (a->wsize != b->wsize)
1888 goto Ebusy;
1889 if (a->rsize != b->rsize)
1890 goto Ebusy;
1891 if (a->acregmin != b->acregmin)
1892 goto Ebusy;
1893 if (a->acregmax != b->acregmax)
1894 goto Ebusy;
1895 if (a->acdirmin != b->acdirmin)
1896 goto Ebusy;
1897 if (a->acdirmax != b->acdirmax)
1898 goto Ebusy;
1899 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1900 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001901 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001902Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001903 return 0;
1904}
1905
1906struct nfs_sb_mountdata {
1907 struct nfs_server *server;
1908 int mntflags;
1909};
1910
1911static int nfs_set_super(struct super_block *s, void *data)
1912{
1913 struct nfs_sb_mountdata *sb_mntdata = data;
1914 struct nfs_server *server = sb_mntdata->server;
1915 int ret;
1916
1917 s->s_flags = sb_mntdata->mntflags;
1918 s->s_fs_info = server;
1919 ret = set_anon_super(s, server);
1920 if (ret == 0)
1921 server->s_dev = s->s_dev;
1922 return ret;
1923}
1924
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001925static int nfs_compare_super_address(struct nfs_server *server1,
1926 struct nfs_server *server2)
1927{
1928 struct sockaddr *sap1, *sap2;
1929
1930 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1931 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1932
1933 if (sap1->sa_family != sap2->sa_family)
1934 return 0;
1935
1936 switch (sap1->sa_family) {
1937 case AF_INET: {
1938 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1939 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1940 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1941 return 0;
1942 if (sin1->sin_port != sin2->sin_port)
1943 return 0;
1944 break;
1945 }
1946 case AF_INET6: {
1947 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1948 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1949 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1950 return 0;
1951 if (sin1->sin6_port != sin2->sin6_port)
1952 return 0;
1953 break;
1954 }
1955 default:
1956 return 0;
1957 }
1958
1959 return 1;
1960}
1961
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001962static int nfs_compare_super(struct super_block *sb, void *data)
1963{
1964 struct nfs_sb_mountdata *sb_mntdata = data;
1965 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1966 int mntflags = sb_mntdata->mntflags;
1967
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001968 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001969 return 0;
1970 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1971 if (old->flags & NFS_MOUNT_UNSHARED)
1972 return 0;
1973 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1974 return 0;
1975 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001976}
1977
Miklos Szeredifa799752008-04-30 00:54:33 -07001978static int nfs_bdi_register(struct nfs_server *server)
1979{
1980 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1981}
1982
David Howells54ceac42006-08-22 20:06:13 -04001983static int nfs_get_sb(struct file_system_type *fs_type,
1984 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1985{
1986 struct nfs_server *server = NULL;
1987 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001988 struct nfs_parsed_mount_data *data;
1989 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001990 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001991 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001992 struct nfs_sb_mountdata sb_mntdata = {
1993 .mntflags = flags,
1994 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001995 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001996
Trond Myklebust33852a12008-06-19 14:20:11 -04001997 data = kzalloc(sizeof(*data), GFP_KERNEL);
1998 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1999 if (data == NULL || mntfh == NULL)
2000 goto out_free_fh;
2001
2002 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002003
David Howells54ceac42006-08-22 20:06:13 -04002004 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002005 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04002006 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04002007 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002008
2009 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002010 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002011 if (IS_ERR(server)) {
2012 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002013 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002014 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002015 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002016
Trond Myklebust75180df2007-05-16 16:53:28 -04002017 if (server->flags & NFS_MOUNT_UNSHARED)
2018 compare_super = NULL;
2019
David Howells54ceac42006-08-22 20:06:13 -04002020 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002021 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002022 if (IS_ERR(s)) {
2023 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002024 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002025 }
2026
David Howells54ceac42006-08-22 20:06:13 -04002027 if (s->s_fs_info != server) {
2028 nfs_free_server(server);
2029 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002030 } else {
2031 error = nfs_bdi_register(server);
2032 if (error)
2033 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002034 }
David Howells54ceac42006-08-22 20:06:13 -04002035
2036 if (!s->s_root) {
2037 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002038 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04002039 }
2040
Trond Myklebust33852a12008-06-19 14:20:11 -04002041 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002042 if (IS_ERR(mntroot)) {
2043 error = PTR_ERR(mntroot);
2044 goto error_splat_super;
2045 }
2046
Trond Myklebust33852a12008-06-19 14:20:11 -04002047 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002048 if (error)
2049 goto error_splat_root;
2050
David Howellsf7b422b2006-06-09 09:34:33 -04002051 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002052 mnt->mnt_sb = s;
2053 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002054 error = 0;
2055
2056out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002057 kfree(data->nfs_server.hostname);
2058 kfree(data->mount_server.hostname);
2059 security_free_mnt_opts(&data->lsm_opts);
2060out_free_fh:
2061 kfree(mntfh);
2062 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002063 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002064
David Howells54ceac42006-08-22 20:06:13 -04002065out_err_nosb:
2066 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002067 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002068
Eric Parisf9c3a382008-03-05 14:20:18 -05002069error_splat_root:
2070 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002071error_splat_super:
2072 up_write(&s->s_umount);
2073 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002074 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002075}
2076
David Howells54ceac42006-08-22 20:06:13 -04002077/*
2078 * Destroy an NFS2/3 superblock
2079 */
David Howellsf7b422b2006-06-09 09:34:33 -04002080static void nfs_kill_super(struct super_block *s)
2081{
2082 struct nfs_server *server = NFS_SB(s);
2083
Miklos Szeredifa799752008-04-30 00:54:33 -07002084 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002085 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002086 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002087}
2088
David Howells54ceac42006-08-22 20:06:13 -04002089/*
2090 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2091 */
2092static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2093 const char *dev_name, void *raw_data,
2094 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002095{
2096 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002097 struct super_block *s;
2098 struct nfs_server *server;
2099 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002100 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002101 struct nfs_sb_mountdata sb_mntdata = {
2102 .mntflags = flags,
2103 };
David Howells54ceac42006-08-22 20:06:13 -04002104 int error;
2105
2106 dprintk("--> nfs_xdev_get_sb()\n");
2107
2108 /* create a new volume representation */
2109 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2110 if (IS_ERR(server)) {
2111 error = PTR_ERR(server);
2112 goto out_err_noserver;
2113 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002114 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002115
Trond Myklebust75180df2007-05-16 16:53:28 -04002116 if (server->flags & NFS_MOUNT_UNSHARED)
2117 compare_super = NULL;
2118
David Howells54ceac42006-08-22 20:06:13 -04002119 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002120 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002121 if (IS_ERR(s)) {
2122 error = PTR_ERR(s);
2123 goto out_err_nosb;
2124 }
2125
2126 if (s->s_fs_info != server) {
2127 nfs_free_server(server);
2128 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002129 } else {
2130 error = nfs_bdi_register(server);
2131 if (error)
2132 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002133 }
2134
2135 if (!s->s_root) {
2136 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002137 nfs_clone_super(s, data->sb);
2138 }
2139
2140 mntroot = nfs_get_root(s, data->fh);
2141 if (IS_ERR(mntroot)) {
2142 error = PTR_ERR(mntroot);
2143 goto error_splat_super;
2144 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002145 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002146 dput(mntroot);
2147 error = -ESTALE;
2148 goto error_splat_super;
2149 }
David Howells54ceac42006-08-22 20:06:13 -04002150
2151 s->s_flags |= MS_ACTIVE;
2152 mnt->mnt_sb = s;
2153 mnt->mnt_root = mntroot;
2154
Eric Parisf9c3a382008-03-05 14:20:18 -05002155 /* clone any lsm security options from the parent to the new sb */
2156 security_sb_clone_mnt_opts(data->sb, s);
2157
David Howells54ceac42006-08-22 20:06:13 -04002158 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2159 return 0;
2160
2161out_err_nosb:
2162 nfs_free_server(server);
2163out_err_noserver:
2164 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2165 return error;
2166
2167error_splat_super:
2168 up_write(&s->s_umount);
2169 deactivate_super(s);
2170 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2171 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002172}
2173
2174#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002175
2176/*
2177 * Finish setting up a cloned NFS4 superblock
2178 */
2179static void nfs4_clone_super(struct super_block *sb,
2180 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002181{
David Howells54ceac42006-08-22 20:06:13 -04002182 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2183 sb->s_blocksize = old_sb->s_blocksize;
2184 sb->s_maxbytes = old_sb->s_maxbytes;
2185 sb->s_time_gran = 1;
2186 sb->s_op = old_sb->s_op;
2187 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002188}
2189
2190/*
2191 * Set up an NFS4 superblock
2192 */
David Howells54ceac42006-08-22 20:06:13 -04002193static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002194{
David Howellsf7b422b2006-06-09 09:34:33 -04002195 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002196 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002197 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002198}
2199
David Howells54ceac42006-08-22 20:06:13 -04002200/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002201 * Validate NFSv4 mount options
2202 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002203static int nfs4_validate_mount_data(void *options,
2204 struct nfs_parsed_mount_data *args,
2205 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002206{
Chuck Lever4c568012007-12-10 14:59:28 -05002207 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002208 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002209 char *c;
2210
2211 if (data == NULL)
2212 goto out_no_data;
2213
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002214 args->rsize = NFS_MAX_FILE_IO_SIZE;
2215 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002216 args->acregmin = NFS_DEF_ACREGMIN;
2217 args->acregmax = NFS_DEF_ACREGMAX;
2218 args->acdirmin = NFS_DEF_ACDIRMIN;
2219 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002220 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002221 args->auth_flavors[0] = RPC_AUTH_UNIX;
2222 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002223
Chuck Leverf0768eb2007-07-01 12:13:01 -04002224 switch (data->version) {
2225 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002226 ap = (struct sockaddr_in *)&args->nfs_server.address;
2227 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002228 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002229 if (data->host_addrlen == 0)
2230 goto out_no_address;
2231 args->nfs_server.addrlen = data->host_addrlen;
2232 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002233 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002234 if (!nfs_verify_server_address((struct sockaddr *)
2235 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002236 goto out_no_address;
2237
Chuck Lever6738b252008-06-24 16:33:54 -04002238 if (data->auth_flavourlen) {
2239 if (data->auth_flavourlen > 1)
2240 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002241 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002242 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002243 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002244 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002245 }
2246
2247 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2248 if (IS_ERR(c))
2249 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002250 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002251
2252 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2253 if (IS_ERR(c))
2254 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002255 args->nfs_server.export_path = c;
2256 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002257
2258 c = strndup_user(data->client_addr.data, 16);
2259 if (IS_ERR(c))
2260 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002261 args->client_address = c;
2262
2263 /*
2264 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2265 * can deal with.
2266 */
2267
2268 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2269 args->rsize = data->rsize;
2270 args->wsize = data->wsize;
2271 args->timeo = data->timeo;
2272 args->retrans = data->retrans;
2273 args->acregmin = data->acregmin;
2274 args->acregmax = data->acregmax;
2275 args->acdirmin = data->acdirmin;
2276 args->acdirmax = data->acdirmax;
2277 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002278 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002279
2280 break;
Chuck Lever80071222007-07-01 12:13:59 -04002281 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002282 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002283
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002284 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002285 return -EINVAL;
2286
2287 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002288 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002289 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002290
Chuck Levered596a82008-06-26 17:47:05 -04002291 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2292 args->nfs_server.port);
2293
Trond Myklebust259875e2008-07-02 14:43:47 -04002294 nfs_validate_transport_protocol(args);
2295
Chuck Lever6738b252008-06-24 16:33:54 -04002296 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002297 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002298
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002299 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002300 goto out_no_client_address;
2301
Chuck Leverdc045892008-06-23 12:36:37 -04002302 status = nfs_parse_devname(dev_name,
2303 &args->nfs_server.hostname,
2304 NFS4_MAXNAMLEN,
2305 &args->nfs_server.export_path,
2306 NFS4_MAXPATHLEN);
2307 if (status < 0)
2308 return status;
2309
Chuck Lever80071222007-07-01 12:13:59 -04002310 break;
2311 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002312 }
2313
2314 return 0;
2315
2316out_no_data:
2317 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2318 return -EINVAL;
2319
2320out_inval_auth:
2321 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2322 data->auth_flavourlen);
2323 return -EINVAL;
2324
2325out_no_address:
2326 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2327 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002328
2329out_no_client_address:
2330 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2331 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002332}
2333
2334/*
David Howells54ceac42006-08-22 20:06:13 -04002335 * Get the superblock for an NFS4 mountpoint
2336 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002337static int nfs4_get_sb(struct file_system_type *fs_type,
2338 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002339{
Trond Myklebust33852a12008-06-19 14:20:11 -04002340 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002341 struct super_block *s;
2342 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002343 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002344 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002345 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002346 struct nfs_sb_mountdata sb_mntdata = {
2347 .mntflags = flags,
2348 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002349 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002350
Trond Myklebust33852a12008-06-19 14:20:11 -04002351 data = kzalloc(sizeof(*data), GFP_KERNEL);
2352 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2353 if (data == NULL || mntfh == NULL)
2354 goto out_free_fh;
2355
2356 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002357
Chuck Leverf0768eb2007-07-01 12:13:01 -04002358 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002359 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002360 if (error < 0)
2361 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002362
David Howells54ceac42006-08-22 20:06:13 -04002363 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002364 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002365 if (IS_ERR(server)) {
2366 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002367 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002368 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002369 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002370
Trond Myklebust75180df2007-05-16 16:53:28 -04002371 if (server->flags & NFS4_MOUNT_UNSHARED)
2372 compare_super = NULL;
2373
David Howells54ceac42006-08-22 20:06:13 -04002374 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002375 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002376 if (IS_ERR(s)) {
2377 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002378 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002379 }
2380
Trond Myklebust5dd31772006-08-24 01:03:05 -04002381 if (s->s_fs_info != server) {
2382 nfs_free_server(server);
2383 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002384 } else {
2385 error = nfs_bdi_register(server);
2386 if (error)
2387 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002388 }
2389
David Howells54ceac42006-08-22 20:06:13 -04002390 if (!s->s_root) {
2391 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002392 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002393 }
David Howellsf7b422b2006-06-09 09:34:33 -04002394
Trond Myklebust33852a12008-06-19 14:20:11 -04002395 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002396 if (IS_ERR(mntroot)) {
2397 error = PTR_ERR(mntroot);
2398 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002399 }
David Howells54ceac42006-08-22 20:06:13 -04002400
Trond Myklebust33852a12008-06-19 14:20:11 -04002401 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002402 if (error)
2403 goto error_splat_root;
2404
David Howellsf7b422b2006-06-09 09:34:33 -04002405 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002406 mnt->mnt_sb = s;
2407 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002408 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002409
Chuck Lever29eb9812007-07-01 12:12:30 -04002410out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002411 kfree(data->client_address);
2412 kfree(data->nfs_server.export_path);
2413 kfree(data->nfs_server.hostname);
2414 security_free_mnt_opts(&data->lsm_opts);
2415out_free_fh:
2416 kfree(mntfh);
2417 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002418 return error;
David Howells54ceac42006-08-22 20:06:13 -04002419
Chuck Lever29eb9812007-07-01 12:12:30 -04002420out_free:
2421 nfs_free_server(server);
2422 goto out;
2423
Eric Paris46c8ac72008-05-02 13:42:42 -07002424error_splat_root:
2425 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002426error_splat_super:
2427 up_write(&s->s_umount);
2428 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002429 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002430}
2431
2432static void nfs4_kill_super(struct super_block *sb)
2433{
2434 struct nfs_server *server = NFS_SB(sb);
2435
2436 nfs_return_all_delegations(sb);
2437 kill_anon_super(sb);
2438
2439 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002440 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002441}
2442
2443/*
David Howells54ceac42006-08-22 20:06:13 -04002444 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002445 */
David Howells54ceac42006-08-22 20:06:13 -04002446static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2447 const char *dev_name, void *raw_data,
2448 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002449{
2450 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002451 struct super_block *s;
2452 struct nfs_server *server;
2453 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002454 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002455 struct nfs_sb_mountdata sb_mntdata = {
2456 .mntflags = flags,
2457 };
David Howells54ceac42006-08-22 20:06:13 -04002458 int error;
2459
2460 dprintk("--> nfs4_xdev_get_sb()\n");
2461
2462 /* create a new volume representation */
2463 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2464 if (IS_ERR(server)) {
2465 error = PTR_ERR(server);
2466 goto out_err_noserver;
2467 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002468 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002469
Trond Myklebust75180df2007-05-16 16:53:28 -04002470 if (server->flags & NFS4_MOUNT_UNSHARED)
2471 compare_super = NULL;
2472
David Howells54ceac42006-08-22 20:06:13 -04002473 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002474 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002475 if (IS_ERR(s)) {
2476 error = PTR_ERR(s);
2477 goto out_err_nosb;
2478 }
2479
2480 if (s->s_fs_info != server) {
2481 nfs_free_server(server);
2482 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002483 } else {
2484 error = nfs_bdi_register(server);
2485 if (error)
2486 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002487 }
2488
2489 if (!s->s_root) {
2490 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002491 nfs4_clone_super(s, data->sb);
2492 }
2493
2494 mntroot = nfs4_get_root(s, data->fh);
2495 if (IS_ERR(mntroot)) {
2496 error = PTR_ERR(mntroot);
2497 goto error_splat_super;
2498 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002499 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2500 dput(mntroot);
2501 error = -ESTALE;
2502 goto error_splat_super;
2503 }
David Howells54ceac42006-08-22 20:06:13 -04002504
2505 s->s_flags |= MS_ACTIVE;
2506 mnt->mnt_sb = s;
2507 mnt->mnt_root = mntroot;
2508
Eric Paris46c8ac72008-05-02 13:42:42 -07002509 security_sb_clone_mnt_opts(data->sb, s);
2510
David Howells54ceac42006-08-22 20:06:13 -04002511 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2512 return 0;
2513
2514out_err_nosb:
2515 nfs_free_server(server);
2516out_err_noserver:
2517 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2518 return error;
2519
2520error_splat_super:
2521 up_write(&s->s_umount);
2522 deactivate_super(s);
2523 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2524 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002525}
2526
David Howells54ceac42006-08-22 20:06:13 -04002527/*
2528 * Create an NFS4 server record on referral traversal
2529 */
2530static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2531 const char *dev_name, void *raw_data,
2532 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002533{
2534 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002535 struct super_block *s;
2536 struct nfs_server *server;
2537 struct dentry *mntroot;
2538 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002539 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002540 struct nfs_sb_mountdata sb_mntdata = {
2541 .mntflags = flags,
2542 };
David Howells54ceac42006-08-22 20:06:13 -04002543 int error;
2544
2545 dprintk("--> nfs4_referral_get_sb()\n");
2546
2547 /* create a new volume representation */
2548 server = nfs4_create_referral_server(data, &mntfh);
2549 if (IS_ERR(server)) {
2550 error = PTR_ERR(server);
2551 goto out_err_noserver;
2552 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002553 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002554
Trond Myklebust75180df2007-05-16 16:53:28 -04002555 if (server->flags & NFS4_MOUNT_UNSHARED)
2556 compare_super = NULL;
2557
David Howells54ceac42006-08-22 20:06:13 -04002558 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002559 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002560 if (IS_ERR(s)) {
2561 error = PTR_ERR(s);
2562 goto out_err_nosb;
2563 }
2564
2565 if (s->s_fs_info != server) {
2566 nfs_free_server(server);
2567 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002568 } else {
2569 error = nfs_bdi_register(server);
2570 if (error)
2571 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002572 }
2573
2574 if (!s->s_root) {
2575 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002576 nfs4_fill_super(s);
2577 }
2578
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002579 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002580 if (IS_ERR(mntroot)) {
2581 error = PTR_ERR(mntroot);
2582 goto error_splat_super;
2583 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002584 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2585 dput(mntroot);
2586 error = -ESTALE;
2587 goto error_splat_super;
2588 }
David Howells54ceac42006-08-22 20:06:13 -04002589
2590 s->s_flags |= MS_ACTIVE;
2591 mnt->mnt_sb = s;
2592 mnt->mnt_root = mntroot;
2593
Eric Paris46c8ac72008-05-02 13:42:42 -07002594 security_sb_clone_mnt_opts(data->sb, s);
2595
David Howells54ceac42006-08-22 20:06:13 -04002596 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2597 return 0;
2598
2599out_err_nosb:
2600 nfs_free_server(server);
2601out_err_noserver:
2602 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2603 return error;
2604
2605error_splat_super:
2606 up_write(&s->s_umount);
2607 deactivate_super(s);
2608 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2609 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002610}
2611
David Howells54ceac42006-08-22 20:06:13 -04002612#endif /* CONFIG_NFS_V4 */