blob: 9c1a960f5b94d12ae4f6190ed99e116e73b2e6bb [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 *
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * 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 Leverbf0fd762007-07-01 12:13:44 -040078
79 /* Mount options that take integer arguments */
80 Opt_port,
81 Opt_rsize, Opt_wsize, Opt_bsize,
82 Opt_timeo, Opt_retrans,
83 Opt_acregmin, Opt_acregmax,
84 Opt_acdirmin, Opt_acdirmax,
85 Opt_actimeo,
86 Opt_namelen,
87 Opt_mountport,
Chuck Levere887cbc2007-10-26 13:32:29 -040088 Opt_mountvers,
Chuck Leverad879ce2007-10-26 13:32:24 -040089 Opt_nfsvers,
Chuck Leverbf0fd762007-07-01 12:13:44 -040090
91 /* Mount options that take string arguments */
Chuck Lever33832032007-12-10 14:59:13 -050092 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
Chuck Lever0ac83772007-09-11 18:01:04 -040093 Opt_addr, Opt_mountaddr, Opt_clientaddr,
Chuck Leverbf0fd762007-07-01 12:13:44 -040094
95 /* Mount options that are ignored */
96 Opt_userspace, Opt_deprecated,
97
98 Opt_err
99};
100
101static match_table_t nfs_mount_option_tokens = {
102 { Opt_userspace, "bg" },
103 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400104 { Opt_userspace, "retry=%s" },
105
Chuck Leverbf0fd762007-07-01 12:13:44 -0400106 { Opt_soft, "soft" },
107 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400108 { Opt_deprecated, "intr" },
109 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400110 { Opt_posix, "posix" },
111 { Opt_noposix, "noposix" },
112 { Opt_cto, "cto" },
113 { Opt_nocto, "nocto" },
114 { Opt_ac, "ac" },
115 { Opt_noac, "noac" },
116 { Opt_lock, "lock" },
117 { Opt_nolock, "nolock" },
118 { Opt_v2, "v2" },
119 { Opt_v3, "v3" },
120 { Opt_udp, "udp" },
121 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400122 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400123 { Opt_acl, "acl" },
124 { Opt_noacl, "noacl" },
125 { Opt_rdirplus, "rdirplus" },
126 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400127 { Opt_sharecache, "sharecache" },
128 { Opt_nosharecache, "nosharecache" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400129
130 { Opt_port, "port=%u" },
131 { Opt_rsize, "rsize=%u" },
132 { Opt_wsize, "wsize=%u" },
133 { Opt_bsize, "bsize=%u" },
134 { Opt_timeo, "timeo=%u" },
135 { Opt_retrans, "retrans=%u" },
136 { Opt_acregmin, "acregmin=%u" },
137 { Opt_acregmax, "acregmax=%u" },
138 { Opt_acdirmin, "acdirmin=%u" },
139 { Opt_acdirmax, "acdirmax=%u" },
140 { Opt_actimeo, "actimeo=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400141 { Opt_namelen, "namlen=%u" },
142 { Opt_mountport, "mountport=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400143 { Opt_mountvers, "mountvers=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400144 { Opt_nfsvers, "nfsvers=%u" },
145 { Opt_nfsvers, "vers=%u" },
146
147 { Opt_sec, "sec=%s" },
148 { Opt_proto, "proto=%s" },
149 { Opt_mountproto, "mountproto=%s" },
150 { Opt_addr, "addr=%s" },
151 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500152 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400153 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400154
155 { Opt_err, NULL }
156};
157
158enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400159 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400160
161 Opt_xprt_err
162};
163
164static match_table_t nfs_xprt_protocol_tokens = {
165 { Opt_xprt_udp, "udp" },
166 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400167 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400168
169 { Opt_xprt_err, NULL }
170};
171
172enum {
173 Opt_sec_none, Opt_sec_sys,
174 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
175 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
176 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
177
178 Opt_sec_err
179};
180
181static match_table_t nfs_secflavor_tokens = {
182 { Opt_sec_none, "none" },
183 { Opt_sec_none, "null" },
184 { Opt_sec_sys, "sys" },
185
186 { Opt_sec_krb5, "krb5" },
187 { Opt_sec_krb5i, "krb5i" },
188 { Opt_sec_krb5p, "krb5p" },
189
190 { Opt_sec_lkey, "lkey" },
191 { Opt_sec_lkeyi, "lkeyi" },
192 { Opt_sec_lkeyp, "lkeyp" },
193
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500194 { Opt_sec_spkm, "spkm3" },
195 { Opt_sec_spkmi, "spkm3i" },
196 { Opt_sec_spkmp, "spkm3p" },
197
Chuck Leverbf0fd762007-07-01 12:13:44 -0400198 { Opt_sec_err, NULL }
199};
200
201
Al Viro42faad92008-04-24 07:21:56 -0400202static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400203static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400204static int nfs_show_options(struct seq_file *, struct vfsmount *);
205static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400206static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400207static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400208 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400209static void nfs_kill_super(struct super_block *);
Steve Dicksonef818a22007-11-08 04:05:04 -0500210static void nfs_put_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400211static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400212
213static struct file_system_type nfs_fs_type = {
214 .owner = THIS_MODULE,
215 .name = "nfs",
216 .get_sb = nfs_get_sb,
217 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700218 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400219};
220
David Howells54ceac42006-08-22 20:06:13 -0400221struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400222 .owner = THIS_MODULE,
223 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400224 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400225 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700226 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400227};
228
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800229static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400230 .alloc_inode = nfs_alloc_inode,
231 .destroy_inode = nfs_destroy_inode,
232 .write_inode = nfs_write_inode,
Steve Dicksonef818a22007-11-08 04:05:04 -0500233 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400234 .statfs = nfs_statfs,
235 .clear_inode = nfs_clear_inode,
236 .umount_begin = nfs_umount_begin,
237 .show_options = nfs_show_options,
238 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400239 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400240};
241
242#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400243static int nfs4_get_sb(struct file_system_type *fs_type,
244 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400245static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
246 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
247static int nfs4_referral_get_sb(struct file_system_type *fs_type,
248 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400249static void nfs4_kill_super(struct super_block *sb);
250
251static struct file_system_type nfs4_fs_type = {
252 .owner = THIS_MODULE,
253 .name = "nfs4",
254 .get_sb = nfs4_get_sb,
255 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700256 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400257};
258
David Howells54ceac42006-08-22 20:06:13 -0400259struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400260 .owner = THIS_MODULE,
261 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400262 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400263 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700264 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400265};
266
David Howells54ceac42006-08-22 20:06:13 -0400267struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400268 .owner = THIS_MODULE,
269 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400270 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400271 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700272 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400273};
274
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800275static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400276 .alloc_inode = nfs_alloc_inode,
277 .destroy_inode = nfs_destroy_inode,
278 .write_inode = nfs_write_inode,
279 .statfs = nfs_statfs,
280 .clear_inode = nfs4_clear_inode,
281 .umount_begin = nfs_umount_begin,
282 .show_options = nfs_show_options,
283 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400284 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400285};
286#endif
287
Rusty Russell8e1f9362007-07-17 04:03:17 -0700288static struct shrinker acl_shrinker = {
289 .shrink = nfs_access_cache_shrinker,
290 .seeks = DEFAULT_SEEKS,
291};
Trond Myklebust979df722006-07-25 11:28:19 -0400292
David Howellsf7b422b2006-06-09 09:34:33 -0400293/*
294 * Register the NFS filesystems
295 */
296int __init register_nfs_fs(void)
297{
298 int ret;
299
300 ret = register_filesystem(&nfs_fs_type);
301 if (ret < 0)
302 goto error_0;
303
David Howellsf7b422b2006-06-09 09:34:33 -0400304 ret = nfs_register_sysctl();
305 if (ret < 0)
306 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800307#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400308 ret = register_filesystem(&nfs4_fs_type);
309 if (ret < 0)
310 goto error_2;
311#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700312 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400313 return 0;
314
315#ifdef CONFIG_NFS_V4
316error_2:
317 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800318#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400319error_1:
320 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400321error_0:
322 return ret;
323}
324
325/*
326 * Unregister the NFS filesystems
327 */
328void __exit unregister_nfs_fs(void)
329{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700330 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400331#ifdef CONFIG_NFS_V4
332 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400333#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700334 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400335 unregister_filesystem(&nfs_fs_type);
336}
337
Steve Dicksonef818a22007-11-08 04:05:04 -0500338void nfs_sb_active(struct nfs_server *server)
339{
340 atomic_inc(&server->active);
341}
342
343void nfs_sb_deactive(struct nfs_server *server)
344{
345 if (atomic_dec_and_test(&server->active))
346 wake_up(&server->active_wq);
347}
348
349static void nfs_put_super(struct super_block *sb)
350{
351 struct nfs_server *server = NFS_SB(sb);
352 /*
353 * Make sure there are no outstanding ops to this server.
354 * If so, wait for them to finish before allowing the
355 * unmount to continue.
356 */
357 wait_event(server->active_wq, atomic_read(&server->active) == 0);
358}
359
David Howellsf7b422b2006-06-09 09:34:33 -0400360/*
361 * Deliver file system statistics to userspace
362 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400363static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400364{
David Howells0c7d90c2006-08-22 20:06:10 -0400365 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400366 unsigned char blockbits;
367 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400368 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400369 struct nfs_fattr fattr;
370 struct nfs_fsstat res = {
371 .fattr = &fattr,
372 };
373 int error;
374
375 lock_kernel();
376
David Howells8fa5c002006-08-22 20:06:12 -0400377 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400378 if (error < 0)
379 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700380 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400381
382 /*
383 * Current versions of glibc do not correctly handle the
384 * case where f_frsize != f_bsize. Eventually we want to
385 * report the value of wtmult in this field.
386 */
David Howells0c7d90c2006-08-22 20:06:10 -0400387 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400388
389 /*
390 * On most *nix systems, f_blocks, f_bfree, and f_bavail
391 * are reported in units of f_frsize. Linux hasn't had
392 * an f_frsize field in its statfs struct until recently,
393 * thus historically Linux's sys_statfs reports these
394 * fields in units of f_bsize.
395 */
David Howells0c7d90c2006-08-22 20:06:10 -0400396 buf->f_bsize = dentry->d_sb->s_blocksize;
397 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400398 blockres = (1 << blockbits) - 1;
399 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
400 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
401 buf->f_bavail = (res.abytes + blockres) >> blockbits;
402
403 buf->f_files = res.tfiles;
404 buf->f_ffree = res.afiles;
405
406 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700407
David Howellsf7b422b2006-06-09 09:34:33 -0400408 unlock_kernel();
409 return 0;
410
411 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700412 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700413 unlock_kernel();
414 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400415}
416
David Howells7d4e2742006-08-22 20:06:07 -0400417/*
418 * Map the security flavour number to a name
419 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400420static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
421{
David Howells7d4e2742006-08-22 20:06:07 -0400422 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400423 rpc_authflavor_t flavour;
424 const char *str;
425 } sec_flavours[] = {
426 { RPC_AUTH_NULL, "null" },
427 { RPC_AUTH_UNIX, "sys" },
428 { RPC_AUTH_GSS_KRB5, "krb5" },
429 { RPC_AUTH_GSS_KRB5I, "krb5i" },
430 { RPC_AUTH_GSS_KRB5P, "krb5p" },
431 { RPC_AUTH_GSS_LKEY, "lkey" },
432 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
433 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
434 { RPC_AUTH_GSS_SPKM, "spkm" },
435 { RPC_AUTH_GSS_SPKMI, "spkmi" },
436 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400437 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400438 };
439 int i;
440
Chuck Lever4d81cd12007-07-01 12:12:40 -0400441 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400442 if (sec_flavours[i].flavour == flavour)
443 break;
444 }
445 return sec_flavours[i].str;
446}
447
Chuck Lever82d101d2008-03-14 14:10:37 -0400448static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
449 int showdefaults)
450{
451 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
452
453 switch (sap->sa_family) {
454 case AF_INET: {
455 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
456 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
457 NIPQUAD(sin->sin_addr.s_addr));
458 break;
459 }
460 case AF_INET6: {
461 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
462 seq_printf(m, ",mountaddr=" NIP6_FMT,
463 NIP6(sin6->sin6_addr));
464 break;
465 }
466 default:
467 if (showdefaults)
468 seq_printf(m, ",mountaddr=unspecified");
469 }
470
471 if (nfss->mountd_version || showdefaults)
472 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
473 if (nfss->mountd_port || showdefaults)
474 seq_printf(m, ",mountport=%u", nfss->mountd_port);
475
476 switch (nfss->mountd_protocol) {
477 case IPPROTO_UDP:
478 seq_printf(m, ",mountproto=udp");
479 break;
480 case IPPROTO_TCP:
481 seq_printf(m, ",mountproto=tcp");
482 break;
483 default:
484 if (showdefaults)
485 seq_printf(m, ",mountproto=auto");
486 }
487}
488
David Howellsf7b422b2006-06-09 09:34:33 -0400489/*
490 * Describe the mount options in force on this server representation
491 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400492static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
493 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400494{
David Howells509de812006-08-22 20:06:11 -0400495 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400496 int flag;
David Howells509de812006-08-22 20:06:11 -0400497 const char *str;
498 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400499 } nfs_info[] = {
500 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400501 { NFS_MOUNT_INTR, ",intr", ",nointr" },
502 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400503 { NFS_MOUNT_NOCTO, ",nocto", "" },
504 { NFS_MOUNT_NOAC, ",noac", "" },
505 { NFS_MOUNT_NONLM, ",nolock", "" },
506 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400507 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400508 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
David Howellsf7b422b2006-06-09 09:34:33 -0400509 { 0, NULL, NULL }
510 };
David Howells509de812006-08-22 20:06:11 -0400511 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400512 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400513 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400514
Chuck Lever82d101d2008-03-14 14:10:37 -0400515 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400516 seq_printf(m, ",rsize=%u", nfss->rsize);
517 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400518 if (nfss->bsize != 0)
519 seq_printf(m, ",bsize=%u", nfss->bsize);
520 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400521 if (nfss->acregmin != 3*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400522 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400523 if (nfss->acregmax != 60*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400524 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400525 if (nfss->acdirmin != 30*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400526 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400527 if (nfss->acdirmax != 60*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400528 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400529 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
530 if (nfss->flags & nfs_infop->flag)
531 seq_puts(m, nfs_infop->str);
532 else
533 seq_puts(m, nfs_infop->nostr);
534 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400535 seq_printf(m, ",proto=%s",
536 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400537 if (version == 4) {
538 if (nfss->port != NFS_PORT)
539 seq_printf(m, ",port=%u", nfss->port);
540 } else
541 if (nfss->port)
542 seq_printf(m, ",port=%u", nfss->port);
543
Trond Myklebust33170232007-12-20 16:03:59 -0500544 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
545 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400546 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400547
548 if (version != 4)
549 nfs_show_mountd_options(m, nfss, showdefaults);
550
551#ifdef CONFIG_NFS_V4
552 if (clp->rpc_ops->version == 4)
553 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
554#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400555}
556
557/*
558 * Describe the mount options on this VFS mountpoint
559 */
560static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
561{
562 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
563
564 nfs_show_mount_options(m, nfss, 0);
565
Chuck Lever5d8515c2007-12-10 14:57:16 -0500566 seq_printf(m, ",addr=%s",
567 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
568 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400569
570 return 0;
571}
572
573/*
574 * Present statistical information for this VFS mountpoint
575 */
576static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
577{
578 int i, cpu;
579 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
580 struct rpc_auth *auth = nfss->client->cl_auth;
581 struct nfs_iostats totals = { };
582
583 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
584
585 /*
586 * Display all mount option settings
587 */
588 seq_printf(m, "\n\topts:\t");
589 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
590 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
591 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
592 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
593 nfs_show_mount_options(m, nfss, 1);
594
595 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
596
597 seq_printf(m, "\n\tcaps:\t");
598 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400599 seq_printf(m, ",wtmult=%u", nfss->wtmult);
600 seq_printf(m, ",dtsize=%u", nfss->dtsize);
601 seq_printf(m, ",bsize=%u", nfss->bsize);
602 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400603
604#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500605 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400606 seq_printf(m, "\n\tnfsv4:\t");
607 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
608 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
609 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
610 }
611#endif
612
613 /*
614 * Display security flavor in effect for this mount
615 */
Chuck Lever2d767432008-03-14 14:10:08 -0400616 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400617 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400618 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400619
620 /*
621 * Display superblock I/O counters
622 */
623 for_each_possible_cpu(cpu) {
624 struct nfs_iostats *stats;
625
626 preempt_disable();
627 stats = per_cpu_ptr(nfss->io_stats, cpu);
628
629 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
630 totals.events[i] += stats->events[i];
631 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
632 totals.bytes[i] += stats->bytes[i];
633
634 preempt_enable();
635 }
636
637 seq_printf(m, "\n\tevents:\t");
638 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
639 seq_printf(m, "%lu ", totals.events[i]);
640 seq_printf(m, "\n\tbytes:\t");
641 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
642 seq_printf(m, "%Lu ", totals.bytes[i]);
643 seq_printf(m, "\n");
644
645 rpc_print_iostats(m, nfss->client);
646
647 return 0;
648}
649
650/*
651 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400652 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400653 */
Al Viro42faad92008-04-24 07:21:56 -0400654static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400655{
Al Viro42faad92008-04-24 07:21:56 -0400656 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400657 struct rpc_clnt *rpc;
658
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400659 /* -EIO all pending I/O */
660 rpc = server->client_acl;
661 if (!IS_ERR(rpc))
662 rpc_killall_tasks(rpc);
663 rpc = server->client;
664 if (!IS_ERR(rpc))
665 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400666}
667
668/*
Chuck Lever04dcd6e2007-12-10 14:57:53 -0500669 * Set the port number in an address. Be agnostic about the address family.
670 */
671static void nfs_set_port(struct sockaddr *sap, unsigned short port)
672{
673 switch (sap->sa_family) {
674 case AF_INET: {
675 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
676 ap->sin_port = htons(port);
677 break;
678 }
679 case AF_INET6: {
680 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
681 ap->sin6_port = htons(port);
682 break;
683 }
684 }
685}
686
687/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500688 * Sanity-check a server address provided by the mount command.
689 *
690 * Address family must be initialized, and address must not be
691 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400692 */
693static int nfs_verify_server_address(struct sockaddr *addr)
694{
695 switch (addr->sa_family) {
696 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500697 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700698 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500699 }
700 case AF_INET6: {
701 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
702 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400703 }
704 }
705
706 return 0;
707}
708
Chuck Leverce3b7e12008-06-23 12:36:53 -0400709static void nfs_parse_ipv4_address(char *string, size_t str_len,
710 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500711{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400712 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
713 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500714
Chuck Leverce3b7e12008-06-23 12:36:53 -0400715 if (str_len <= INET_ADDRSTRLEN) {
716 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
717 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500718
Chuck Leverce3b7e12008-06-23 12:36:53 -0400719 sin->sin_family = AF_INET;
720 *addr_len = sizeof(*sin);
721 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500722 return;
723 }
Chuck Lever9412b922007-12-10 14:59:21 -0500724
725 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400726 *addr_len = 0;
727}
728
Chuck Leverd8e77482008-06-23 12:37:01 -0400729#define IPV6_SCOPE_DELIMITER '%'
730
Chuck Leverce3b7e12008-06-23 12:36:53 -0400731#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Leverd8e77482008-06-23 12:37:01 -0400732static void nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
733 const char *delim,
734 struct sockaddr_in6 *sin6)
735{
736 char *p;
737 size_t len;
738
739 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
740 return ;
741 if (*delim != IPV6_SCOPE_DELIMITER)
742 return;
743
744 len = (string + str_len) - delim - 1;
745 p = kstrndup(delim + 1, len, GFP_KERNEL);
746 if (p) {
747 unsigned long scope_id = 0;
748 struct net_device *dev;
749
750 dev = dev_get_by_name(&init_net, p);
751 if (dev != NULL) {
752 scope_id = dev->ifindex;
753 dev_put(dev);
754 } else {
755 /* scope_id is set to zero on error */
756 strict_strtoul(p, 10, &scope_id);
757 }
758
759 kfree(p);
760 sin6->sin6_scope_id = scope_id;
761 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
762 }
763}
764
Chuck Leverce3b7e12008-06-23 12:36:53 -0400765static void nfs_parse_ipv6_address(char *string, size_t str_len,
766 struct sockaddr *sap, size_t *addr_len)
767{
768 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
769 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400770 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400771
772 if (str_len <= INET6_ADDRSTRLEN) {
773 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
774 (int)str_len, string);
775
776 sin6->sin6_family = AF_INET6;
777 *addr_len = sizeof(*sin6);
Chuck Leverd8e77482008-06-23 12:37:01 -0400778 if (in6_pton(string, str_len, addr, IPV6_SCOPE_DELIMITER, &delim)) {
779 nfs_parse_ipv6_scope_id(string, str_len, delim, sin6);
Chuck Leverce3b7e12008-06-23 12:36:53 -0400780 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400781 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400782 }
783
784 sap->sa_family = AF_UNSPEC;
785 *addr_len = 0;
786}
787#else
788static void nfs_parse_ipv6_address(char *string, size_t str_len,
789 struct sockaddr *sap, size_t *addr_len)
790{
791 sap->sa_family = AF_UNSPEC;
792 *addr_len = 0;
793}
794#endif
795
796/*
797 * Construct a sockaddr based on the contents of a string that contains
798 * an IP address in presentation format.
799 *
800 * If there is a problem constructing the new sockaddr, set the address
801 * family to AF_UNSPEC.
802 */
803static void nfs_parse_ip_address(char *string, size_t str_len,
804 struct sockaddr *sap, size_t *addr_len)
805{
806 unsigned int i, colons;
807
808 colons = 0;
809 for (i = 0; i < str_len; i++)
810 if (string[i] == ':')
811 colons++;
812
813 if (colons >= 2)
814 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
815 else
816 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500817}
818
819/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400820 * Sanity check the NFS transport protocol.
821 *
822 */
823static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
824{
825 switch (mnt->nfs_server.protocol) {
826 case XPRT_TRANSPORT_UDP:
827 case XPRT_TRANSPORT_TCP:
828 case XPRT_TRANSPORT_RDMA:
829 break;
830 default:
831 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
832 }
833}
834
835/*
836 * For text based NFSv2/v3 mounts, the mount protocol transport default
837 * settings should depend upon the specified NFS transport.
838 */
839static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
840{
841 nfs_validate_transport_protocol(mnt);
842
843 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
844 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
845 return;
846 switch (mnt->nfs_server.protocol) {
847 case XPRT_TRANSPORT_UDP:
848 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
849 break;
850 case XPRT_TRANSPORT_TCP:
851 case XPRT_TRANSPORT_RDMA:
852 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
853 }
854}
855
856/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400857 * Error-check and convert a string of mount options from user space into
858 * a data structure
859 */
860static int nfs_parse_mount_options(char *raw,
861 struct nfs_parsed_mount_data *mnt)
862{
Eric Parisf9c3a382008-03-05 14:20:18 -0500863 char *p, *string, *secdata;
Eric Parisf9c3a382008-03-05 14:20:18 -0500864 int rc;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400865
866 if (!raw) {
867 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
868 return 1;
869 }
870 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
871
Eric Parisf9c3a382008-03-05 14:20:18 -0500872 secdata = alloc_secdata();
873 if (!secdata)
874 goto out_nomem;
875
876 rc = security_sb_copy_data(raw, secdata);
877 if (rc)
878 goto out_security_failure;
879
880 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
881 if (rc)
882 goto out_security_failure;
883
884 free_secdata(secdata);
885
Chuck Leverbf0fd762007-07-01 12:13:44 -0400886 while ((p = strsep(&raw, ",")) != NULL) {
887 substring_t args[MAX_OPT_ARGS];
888 int option, token;
889
890 if (!*p)
891 continue;
892
893 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
894
895 token = match_token(p, nfs_mount_option_tokens, args);
896 switch (token) {
897 case Opt_soft:
898 mnt->flags |= NFS_MOUNT_SOFT;
899 break;
900 case Opt_hard:
901 mnt->flags &= ~NFS_MOUNT_SOFT;
902 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400903 case Opt_posix:
904 mnt->flags |= NFS_MOUNT_POSIX;
905 break;
906 case Opt_noposix:
907 mnt->flags &= ~NFS_MOUNT_POSIX;
908 break;
909 case Opt_cto:
910 mnt->flags &= ~NFS_MOUNT_NOCTO;
911 break;
912 case Opt_nocto:
913 mnt->flags |= NFS_MOUNT_NOCTO;
914 break;
915 case Opt_ac:
916 mnt->flags &= ~NFS_MOUNT_NOAC;
917 break;
918 case Opt_noac:
919 mnt->flags |= NFS_MOUNT_NOAC;
920 break;
921 case Opt_lock:
922 mnt->flags &= ~NFS_MOUNT_NONLM;
923 break;
924 case Opt_nolock:
925 mnt->flags |= NFS_MOUNT_NONLM;
926 break;
927 case Opt_v2:
928 mnt->flags &= ~NFS_MOUNT_VER3;
929 break;
930 case Opt_v3:
931 mnt->flags |= NFS_MOUNT_VER3;
932 break;
933 case Opt_udp:
934 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400935 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400936 break;
937 case Opt_tcp:
938 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400939 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400940 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400941 case Opt_rdma:
942 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
943 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400944 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400945 case Opt_acl:
946 mnt->flags &= ~NFS_MOUNT_NOACL;
947 break;
948 case Opt_noacl:
949 mnt->flags |= NFS_MOUNT_NOACL;
950 break;
951 case Opt_rdirplus:
952 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
953 break;
954 case Opt_nordirplus:
955 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
956 break;
Trond Myklebust75180df2007-05-16 16:53:28 -0400957 case Opt_sharecache:
958 mnt->flags &= ~NFS_MOUNT_UNSHARED;
959 break;
960 case Opt_nosharecache:
961 mnt->flags |= NFS_MOUNT_UNSHARED;
962 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400963
964 case Opt_port:
965 if (match_int(args, &option))
966 return 0;
967 if (option < 0 || option > 65535)
968 return 0;
Chuck Leverf22d6d72008-03-14 14:10:22 -0400969 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400970 break;
971 case Opt_rsize:
972 if (match_int(args, &mnt->rsize))
973 return 0;
974 break;
975 case Opt_wsize:
976 if (match_int(args, &mnt->wsize))
977 return 0;
978 break;
979 case Opt_bsize:
980 if (match_int(args, &option))
981 return 0;
982 if (option < 0)
983 return 0;
984 mnt->bsize = option;
985 break;
986 case Opt_timeo:
987 if (match_int(args, &mnt->timeo))
988 return 0;
989 break;
990 case Opt_retrans:
991 if (match_int(args, &mnt->retrans))
992 return 0;
993 break;
994 case Opt_acregmin:
995 if (match_int(args, &mnt->acregmin))
996 return 0;
997 break;
998 case Opt_acregmax:
999 if (match_int(args, &mnt->acregmax))
1000 return 0;
1001 break;
1002 case Opt_acdirmin:
1003 if (match_int(args, &mnt->acdirmin))
1004 return 0;
1005 break;
1006 case Opt_acdirmax:
1007 if (match_int(args, &mnt->acdirmax))
1008 return 0;
1009 break;
1010 case Opt_actimeo:
1011 if (match_int(args, &option))
1012 return 0;
1013 if (option < 0)
1014 return 0;
1015 mnt->acregmin =
1016 mnt->acregmax =
1017 mnt->acdirmin =
1018 mnt->acdirmax = option;
1019 break;
1020 case Opt_namelen:
1021 if (match_int(args, &mnt->namlen))
1022 return 0;
1023 break;
1024 case Opt_mountport:
1025 if (match_int(args, &option))
1026 return 0;
1027 if (option < 0 || option > 65535)
1028 return 0;
1029 mnt->mount_server.port = option;
1030 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001031 case Opt_mountvers:
1032 if (match_int(args, &option))
1033 return 0;
1034 if (option < 0)
1035 return 0;
1036 mnt->mount_server.version = option;
1037 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001038 case Opt_nfsvers:
1039 if (match_int(args, &option))
1040 return 0;
1041 switch (option) {
1042 case 2:
1043 mnt->flags &= ~NFS_MOUNT_VER3;
1044 break;
1045 case 3:
1046 mnt->flags |= NFS_MOUNT_VER3;
1047 break;
1048 default:
1049 goto out_unrec_vers;
1050 }
1051 break;
1052
1053 case Opt_sec:
1054 string = match_strdup(args);
1055 if (string == NULL)
1056 goto out_nomem;
1057 token = match_token(string, nfs_secflavor_tokens, args);
1058 kfree(string);
1059
1060 /*
1061 * The flags setting is for v2/v3. The flavor_len
1062 * setting is for v4. v2/v3 also need to know the
1063 * difference between NULL and UNIX.
1064 */
1065 switch (token) {
1066 case Opt_sec_none:
1067 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
1068 mnt->auth_flavor_len = 0;
1069 mnt->auth_flavors[0] = RPC_AUTH_NULL;
1070 break;
1071 case Opt_sec_sys:
1072 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
1073 mnt->auth_flavor_len = 0;
1074 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
1075 break;
1076 case Opt_sec_krb5:
1077 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1078 mnt->auth_flavor_len = 1;
1079 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
1080 break;
1081 case Opt_sec_krb5i:
1082 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1083 mnt->auth_flavor_len = 1;
1084 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
1085 break;
1086 case Opt_sec_krb5p:
1087 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1088 mnt->auth_flavor_len = 1;
1089 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
1090 break;
1091 case Opt_sec_lkey:
1092 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1093 mnt->auth_flavor_len = 1;
1094 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
1095 break;
1096 case Opt_sec_lkeyi:
1097 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1098 mnt->auth_flavor_len = 1;
1099 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
1100 break;
1101 case Opt_sec_lkeyp:
1102 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1103 mnt->auth_flavor_len = 1;
1104 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
1105 break;
1106 case Opt_sec_spkm:
1107 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1108 mnt->auth_flavor_len = 1;
1109 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
1110 break;
1111 case Opt_sec_spkmi:
1112 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1113 mnt->auth_flavor_len = 1;
1114 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
1115 break;
1116 case Opt_sec_spkmp:
1117 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1118 mnt->auth_flavor_len = 1;
1119 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
1120 break;
1121 default:
1122 goto out_unrec_sec;
1123 }
1124 break;
1125 case Opt_proto:
1126 string = match_strdup(args);
1127 if (string == NULL)
1128 goto out_nomem;
1129 token = match_token(string,
1130 nfs_xprt_protocol_tokens, args);
1131 kfree(string);
1132
1133 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001134 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001135 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001136 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001137 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001138 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001139 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001140 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001141 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001142 case Opt_xprt_rdma:
1143 /* vector side protocols to TCP */
1144 mnt->flags |= NFS_MOUNT_TCP;
1145 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001146 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001147 default:
1148 goto out_unrec_xprt;
1149 }
1150 break;
1151 case Opt_mountproto:
1152 string = match_strdup(args);
1153 if (string == NULL)
1154 goto out_nomem;
1155 token = match_token(string,
1156 nfs_xprt_protocol_tokens, args);
1157 kfree(string);
1158
1159 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001160 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001161 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001162 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001163 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001164 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001165 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001166 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001167 default:
1168 goto out_unrec_xprt;
1169 }
1170 break;
1171 case Opt_addr:
1172 string = match_strdup(args);
1173 if (string == NULL)
1174 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001175 nfs_parse_ip_address(string, strlen(string),
1176 (struct sockaddr *)
1177 &mnt->nfs_server.address,
1178 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001179 kfree(string);
1180 break;
1181 case Opt_clientaddr:
1182 string = match_strdup(args);
1183 if (string == NULL)
1184 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001185 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001186 mnt->client_address = string;
1187 break;
Chuck Lever33832032007-12-10 14:59:13 -05001188 case Opt_mounthost:
1189 string = match_strdup(args);
1190 if (string == NULL)
1191 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001192 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001193 mnt->mount_server.hostname = string;
1194 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001195 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001196 string = match_strdup(args);
1197 if (string == NULL)
1198 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001199 nfs_parse_ip_address(string, strlen(string),
1200 (struct sockaddr *)
1201 &mnt->mount_server.address,
1202 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001203 kfree(string);
1204 break;
1205
1206 case Opt_userspace:
1207 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001208 dfprintk(MOUNT, "NFS: ignoring mount option "
1209 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001210 break;
1211
1212 default:
1213 goto out_unknown;
1214 }
1215 }
1216
Chuck Leverf22d6d72008-03-14 14:10:22 -04001217 nfs_set_port((struct sockaddr *)&mnt->nfs_server.address,
1218 mnt->nfs_server.port);
Chuck Lever04dcd6e2007-12-10 14:57:53 -05001219
Chuck Leverbf0fd762007-07-01 12:13:44 -04001220 return 1;
1221
1222out_nomem:
1223 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1224 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001225out_security_failure:
1226 free_secdata(secdata);
1227 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1228 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001229out_unrec_vers:
1230 printk(KERN_INFO "NFS: unrecognized NFS version number\n");
1231 return 0;
1232
1233out_unrec_xprt:
1234 printk(KERN_INFO "NFS: unrecognized transport protocol\n");
1235 return 0;
1236
1237out_unrec_sec:
1238 printk(KERN_INFO "NFS: unrecognized security flavor\n");
1239 return 0;
1240
1241out_unknown:
1242 printk(KERN_INFO "NFS: unknown mount option: %s\n", p);
1243 return 0;
1244}
1245
1246/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001247 * Use the remote server's MOUNT service to request the NFS file handle
1248 * corresponding to the provided path.
1249 */
1250static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1251 struct nfs_fh *root_fh)
1252{
Chuck Lever4c568012007-12-10 14:59:28 -05001253 struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
Chuck Lever33832032007-12-10 14:59:13 -05001254 char *hostname;
Chuck Lever4c568012007-12-10 14:59:28 -05001255 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001256
1257 if (args->mount_server.version == 0) {
1258 if (args->flags & NFS_MOUNT_VER3)
1259 args->mount_server.version = NFS_MNT3_VERSION;
1260 else
1261 args->mount_server.version = NFS_MNT_VERSION;
1262 }
1263
Chuck Lever33832032007-12-10 14:59:13 -05001264 if (args->mount_server.hostname)
1265 hostname = args->mount_server.hostname;
1266 else
1267 hostname = args->nfs_server.hostname;
1268
Chuck Lever0076d7b2007-07-01 12:13:49 -04001269 /*
1270 * Construct the mount server's address.
1271 */
Chuck Lever4c568012007-12-10 14:59:28 -05001272 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1273 memcpy(sap, &args->nfs_server.address,
1274 args->nfs_server.addrlen);
1275 args->mount_server.addrlen = args->nfs_server.addrlen;
1276 }
1277
James Lentiniaad70002007-09-24 17:32:49 -04001278 /*
1279 * autobind will be used if mount_server.port == 0
1280 */
Chuck Lever4c568012007-12-10 14:59:28 -05001281 nfs_set_port(sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001282
1283 /*
1284 * Now ask the mount server to map our export path
1285 * to a file handle.
1286 */
Chuck Lever4c568012007-12-10 14:59:28 -05001287 status = nfs_mount(sap,
1288 args->mount_server.addrlen,
Chuck Lever33832032007-12-10 14:59:13 -05001289 hostname,
Chuck Lever0076d7b2007-07-01 12:13:49 -04001290 args->nfs_server.export_path,
1291 args->mount_server.version,
1292 args->mount_server.protocol,
1293 root_fh);
Chuck Leverefd83402007-09-11 18:00:58 -04001294 if (status == 0)
1295 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001296
Chuck Lever396cee972008-06-12 12:37:49 -04001297 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Lever33832032007-12-10 14:59:13 -05001298 hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001299 return status;
1300}
1301
Chuck Leverd1aa0822008-06-23 12:36:45 -04001302static int nfs_parse_simple_hostname(const char *dev_name,
1303 char **hostname, size_t maxnamlen,
1304 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001305{
1306 size_t len;
1307 char *colon, *comma;
1308
1309 colon = strchr(dev_name, ':');
1310 if (colon == NULL)
1311 goto out_bad_devname;
1312
1313 len = colon - dev_name;
1314 if (len > maxnamlen)
1315 goto out_hostname;
1316
1317 /* N.B. caller will free nfs_server.hostname in all cases */
1318 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1319 if (!*hostname)
1320 goto out_nomem;
1321
1322 /* kill possible hostname list: not supported */
1323 comma = strchr(*hostname, ',');
1324 if (comma != NULL) {
1325 if (comma == *hostname)
1326 goto out_bad_devname;
1327 *comma = '\0';
1328 }
1329
1330 colon++;
1331 len = strlen(colon);
1332 if (len > maxpathlen)
1333 goto out_path;
1334 *export_path = kstrndup(colon, len, GFP_KERNEL);
1335 if (!*export_path)
1336 goto out_nomem;
1337
1338 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1339 return 0;
1340
1341out_bad_devname:
1342 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1343 return -EINVAL;
1344
1345out_nomem:
1346 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1347 return -ENOMEM;
1348
1349out_hostname:
1350 dfprintk(MOUNT, "NFS: server hostname too long\n");
1351 return -ENAMETOOLONG;
1352
1353out_path:
1354 dfprintk(MOUNT, "NFS: export pathname too long\n");
1355 return -ENAMETOOLONG;
1356}
1357
1358/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001359 * Hostname has square brackets around it because it contains one or
1360 * more colons. We look for the first closing square bracket, and a
1361 * colon must follow it.
1362 */
1363static int nfs_parse_protected_hostname(const char *dev_name,
1364 char **hostname, size_t maxnamlen,
1365 char **export_path, size_t maxpathlen)
1366{
1367 size_t len;
1368 char *start, *end;
1369
1370 start = (char *)(dev_name + 1);
1371
1372 end = strchr(start, ']');
1373 if (end == NULL)
1374 goto out_bad_devname;
1375 if (*(end + 1) != ':')
1376 goto out_bad_devname;
1377
1378 len = end - start;
1379 if (len > maxnamlen)
1380 goto out_hostname;
1381
1382 /* N.B. caller will free nfs_server.hostname in all cases */
1383 *hostname = kstrndup(start, len, GFP_KERNEL);
1384 if (*hostname == NULL)
1385 goto out_nomem;
1386
1387 end += 2;
1388 len = strlen(end);
1389 if (len > maxpathlen)
1390 goto out_path;
1391 *export_path = kstrndup(end, len, GFP_KERNEL);
1392 if (!*export_path)
1393 goto out_nomem;
1394
1395 return 0;
1396
1397out_bad_devname:
1398 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1399 return -EINVAL;
1400
1401out_nomem:
1402 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1403 return -ENOMEM;
1404
1405out_hostname:
1406 dfprintk(MOUNT, "NFS: server hostname too long\n");
1407 return -ENAMETOOLONG;
1408
1409out_path:
1410 dfprintk(MOUNT, "NFS: export pathname too long\n");
1411 return -ENAMETOOLONG;
1412}
1413
1414/*
1415 * Split "dev_name" into "hostname:export_path".
1416 *
1417 * The leftmost colon demarks the split between the server's hostname
1418 * and the export path. If the hostname starts with a left square
1419 * bracket, then it may contain colons.
1420 *
1421 * Note: caller frees hostname and export path, even on error.
1422 */
1423static int nfs_parse_devname(const char *dev_name,
1424 char **hostname, size_t maxnamlen,
1425 char **export_path, size_t maxpathlen)
1426{
1427 if (*dev_name == '[')
1428 return nfs_parse_protected_hostname(dev_name,
1429 hostname, maxnamlen,
1430 export_path, maxpathlen);
1431
1432 return nfs_parse_simple_hostname(dev_name,
1433 hostname, maxnamlen,
1434 export_path, maxpathlen);
1435}
1436
1437/*
David Howells54ceac42006-08-22 20:06:13 -04001438 * Validate the NFS2/NFS3 mount data
1439 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001440 *
1441 * For option strings, user space handles the following behaviors:
1442 *
1443 * + DNS: mapping server host name to IP address ("addr=" option)
1444 *
1445 * + failure mode: how to behave if a mount request can't be handled
1446 * immediately ("fg/bg" option)
1447 *
1448 * + retry: how often to retry a mount request ("retry=" option)
1449 *
1450 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1451 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001452 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001453static int nfs_validate_mount_data(void *options,
1454 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001455 struct nfs_fh *mntfh,
1456 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001457{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001458 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001459
Chuck Lever5df36e72007-07-01 12:12:56 -04001460 if (data == NULL)
1461 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001462
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001463 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1464 args->rsize = NFS_MAX_FILE_IO_SIZE;
1465 args->wsize = NFS_MAX_FILE_IO_SIZE;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001466 args->acregmin = 3;
1467 args->acregmax = 60;
1468 args->acdirmin = 30;
1469 args->acdirmax = 60;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001470 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001471 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001472 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001473
David Howellsf7b422b2006-06-09 09:34:33 -04001474 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001475 case 1:
1476 data->namlen = 0;
1477 case 2:
1478 data->bsize = 0;
1479 case 3:
1480 if (data->flags & NFS_MOUNT_VER3)
1481 goto out_no_v3;
1482 data->root.size = NFS2_FHSIZE;
1483 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1484 case 4:
1485 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1486 goto out_no_sec;
1487 case 5:
1488 memset(data->context, 0, sizeof(data->context));
1489 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001490 if (data->flags & NFS_MOUNT_VER3) {
1491 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1492 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001493 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001494 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001495 mntfh->size = NFS2_FHSIZE;
1496
Chuck Lever5df36e72007-07-01 12:12:56 -04001497
1498 memcpy(mntfh->data, data->root.data, mntfh->size);
1499 if (mntfh->size < sizeof(mntfh->data))
1500 memset(mntfh->data + mntfh->size, 0,
1501 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001502
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001503 /*
1504 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1505 * can deal with.
1506 */
1507 args->flags = data->flags;
1508 args->rsize = data->rsize;
1509 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001510 args->timeo = data->timeo;
1511 args->retrans = data->retrans;
1512 args->acregmin = data->acregmin;
1513 args->acregmax = data->acregmax;
1514 args->acdirmin = data->acdirmin;
1515 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001516
1517 memcpy(&args->nfs_server.address, &data->addr,
1518 sizeof(data->addr));
1519 args->nfs_server.addrlen = sizeof(data->addr);
1520 if (!nfs_verify_server_address((struct sockaddr *)
1521 &args->nfs_server.address))
1522 goto out_no_address;
1523
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001524 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001525 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001526 /* N.B. caller will free nfs_server.hostname in all cases */
1527 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1528 args->namlen = data->namlen;
1529 args->bsize = data->bsize;
1530 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001531 if (!args->nfs_server.hostname)
1532 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001533
1534 /*
1535 * The legacy version 6 binary mount data from userspace has a
1536 * field used only to transport selinux information into the
1537 * the kernel. To continue to support that functionality we
1538 * have a touch of selinux knowledge here in the NFS code. The
1539 * userspace code converted context=blah to just blah so we are
1540 * converting back to the full string selinux understands.
1541 */
1542 if (data->context[0]){
1543#ifdef CONFIG_SECURITY_SELINUX
1544 int rc;
1545 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1546 if (!opts_str)
1547 return -ENOMEM;
1548 strcpy(opts_str, "context=");
1549 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1550 strcat(opts_str, &data->context[0]);
1551 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1552 kfree(opts_str);
1553 if (rc)
1554 return rc;
1555#else
1556 return -EINVAL;
1557#endif
1558 }
1559
Chuck Lever5df36e72007-07-01 12:12:56 -04001560 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001561 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001562 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001563
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001564 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001565 return -EINVAL;
1566
Chuck Lever6e88e062007-09-24 15:39:50 -04001567 if (!nfs_verify_server_address((struct sockaddr *)
1568 &args->nfs_server.address))
1569 goto out_no_address;
1570
Trond Myklebust259875e2008-07-02 14:43:47 -04001571 nfs_set_mount_transport_protocol(args);
1572
Chuck Leverdc045892008-06-23 12:36:37 -04001573 status = nfs_parse_devname(dev_name,
1574 &args->nfs_server.hostname,
1575 PAGE_SIZE,
1576 &args->nfs_server.export_path,
1577 NFS_MAXPATHLEN);
1578 if (!status)
1579 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001580
Chuck Leverdc045892008-06-23 12:36:37 -04001581 kfree(args->nfs_server.export_path);
1582 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001583
Chuck Lever136d5582007-07-01 12:13:54 -04001584 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001585 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001586
Chuck Lever136d5582007-07-01 12:13:54 -04001587 break;
1588 }
David Howellsf7b422b2006-06-09 09:34:33 -04001589 }
David Howells54ceac42006-08-22 20:06:13 -04001590
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001591 if (!(args->flags & NFS_MOUNT_SECFLAVOUR))
1592 args->auth_flavors[0] = RPC_AUTH_UNIX;
Trond Myklebust36b15c52006-08-22 20:06:14 -04001593
David Howellsf7b422b2006-06-09 09:34:33 -04001594#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001595 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001596 goto out_v3_not_compiled;
1597#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001598
David Howells54ceac42006-08-22 20:06:13 -04001599 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001600
1601out_no_data:
1602 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1603 return -EINVAL;
1604
1605out_no_v3:
1606 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1607 data->version);
1608 return -EINVAL;
1609
1610out_no_sec:
1611 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1612 return -EINVAL;
1613
Chuck Lever5df36e72007-07-01 12:12:56 -04001614#ifndef CONFIG_NFS_V3
1615out_v3_not_compiled:
1616 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1617 return -EPROTONOSUPPORT;
1618#endif /* !CONFIG_NFS_V3 */
1619
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001620out_nomem:
1621 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1622 return -ENOMEM;
1623
Chuck Lever5df36e72007-07-01 12:12:56 -04001624out_no_address:
1625 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1626 return -EINVAL;
1627
1628out_invalid_fh:
1629 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1630 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001631}
1632
Jeff Layton48b605f2008-06-10 15:38:39 -04001633static int
1634nfs_compare_remount_data(struct nfs_server *nfss,
1635 struct nfs_parsed_mount_data *data)
1636{
1637 if (data->flags != nfss->flags ||
1638 data->rsize != nfss->rsize ||
1639 data->wsize != nfss->wsize ||
1640 data->retrans != nfss->client->cl_timeout->to_retries ||
1641 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1642 data->acregmin != nfss->acregmin / HZ ||
1643 data->acregmax != nfss->acregmax / HZ ||
1644 data->acdirmin != nfss->acdirmin / HZ ||
1645 data->acdirmax != nfss->acdirmax / HZ ||
1646 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1647 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1648 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1649 data->nfs_server.addrlen) != 0)
1650 return -EINVAL;
1651
1652 return 0;
1653}
1654
1655static int
1656nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1657{
1658 int error;
1659 struct nfs_server *nfss = sb->s_fs_info;
1660 struct nfs_parsed_mount_data *data;
1661 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1662 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001663 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001664
1665 /*
1666 * Userspace mount programs that send binary options generally send
1667 * them populated with default values. We have no way to know which
1668 * ones were explicitly specified. Fall back to legacy behavior and
1669 * just return success.
1670 */
Trond Myklebustcd100722008-06-17 16:12:00 -04001671 if ((nfsvers == 4 && options4->version == 1) ||
1672 (nfsvers <= 3 && options->version >= 1 &&
Jeff Layton48b605f2008-06-10 15:38:39 -04001673 options->version <= 6))
1674 return 0;
1675
1676 data = kzalloc(sizeof(*data), GFP_KERNEL);
1677 if (data == NULL)
1678 return -ENOMEM;
1679
1680 /* fill out struct with values from existing mount */
1681 data->flags = nfss->flags;
1682 data->rsize = nfss->rsize;
1683 data->wsize = nfss->wsize;
1684 data->retrans = nfss->client->cl_timeout->to_retries;
1685 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1686 data->acregmin = nfss->acregmin / HZ;
1687 data->acregmax = nfss->acregmax / HZ;
1688 data->acdirmin = nfss->acdirmin / HZ;
1689 data->acdirmax = nfss->acdirmax / HZ;
1690 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1691 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1692 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1693 data->nfs_server.addrlen);
1694
1695 /* overwrite those values with any that were specified */
1696 error = nfs_parse_mount_options((char *)options, data);
1697 if (error < 0)
1698 goto out;
1699
1700 /* compare new mount options with old ones */
1701 error = nfs_compare_remount_data(nfss, data);
1702out:
1703 kfree(data);
1704 return error;
1705}
1706
David Howells54ceac42006-08-22 20:06:13 -04001707/*
1708 * Initialise the common bits of the superblock
1709 */
1710static inline void nfs_initialise_sb(struct super_block *sb)
1711{
1712 struct nfs_server *server = NFS_SB(sb);
1713
1714 sb->s_magic = NFS_SUPER_MAGIC;
1715
1716 /* We probably want something more informative here */
1717 snprintf(sb->s_id, sizeof(sb->s_id),
1718 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1719
1720 if (sb->s_blocksize == 0)
1721 sb->s_blocksize = nfs_block_bits(server->wsize,
1722 &sb->s_blocksize_bits);
1723
1724 if (server->flags & NFS_MOUNT_NOAC)
1725 sb->s_flags |= MS_SYNCHRONOUS;
1726
1727 nfs_super_set_maxbytes(sb, server->maxfilesize);
1728}
1729
1730/*
1731 * Finish setting up an NFS2/3 superblock
1732 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001733static void nfs_fill_super(struct super_block *sb,
1734 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001735{
1736 struct nfs_server *server = NFS_SB(sb);
1737
1738 sb->s_blocksize_bits = 0;
1739 sb->s_blocksize = 0;
1740 if (data->bsize)
1741 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1742
1743 if (server->flags & NFS_MOUNT_VER3) {
1744 /* The VFS shouldn't apply the umask to mode bits. We will do
1745 * so ourselves when necessary.
1746 */
1747 sb->s_flags |= MS_POSIXACL;
1748 sb->s_time_gran = 1;
1749 }
1750
1751 sb->s_op = &nfs_sops;
1752 nfs_initialise_sb(sb);
1753}
1754
1755/*
1756 * Finish setting up a cloned NFS2/3 superblock
1757 */
1758static void nfs_clone_super(struct super_block *sb,
1759 const struct super_block *old_sb)
1760{
1761 struct nfs_server *server = NFS_SB(sb);
1762
1763 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1764 sb->s_blocksize = old_sb->s_blocksize;
1765 sb->s_maxbytes = old_sb->s_maxbytes;
1766
1767 if (server->flags & NFS_MOUNT_VER3) {
1768 /* The VFS shouldn't apply the umask to mode bits. We will do
1769 * so ourselves when necessary.
1770 */
1771 sb->s_flags |= MS_POSIXACL;
1772 sb->s_time_gran = 1;
1773 }
1774
1775 sb->s_op = old_sb->s_op;
1776 nfs_initialise_sb(sb);
1777}
1778
Trond Myklebust275a5d22007-05-16 16:53:28 -04001779#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1780
1781static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1782{
1783 const struct nfs_server *a = s->s_fs_info;
1784 const struct rpc_clnt *clnt_a = a->client;
1785 const struct rpc_clnt *clnt_b = b->client;
1786
1787 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1788 goto Ebusy;
1789 if (a->nfs_client != b->nfs_client)
1790 goto Ebusy;
1791 if (a->flags != b->flags)
1792 goto Ebusy;
1793 if (a->wsize != b->wsize)
1794 goto Ebusy;
1795 if (a->rsize != b->rsize)
1796 goto Ebusy;
1797 if (a->acregmin != b->acregmin)
1798 goto Ebusy;
1799 if (a->acregmax != b->acregmax)
1800 goto Ebusy;
1801 if (a->acdirmin != b->acdirmin)
1802 goto Ebusy;
1803 if (a->acdirmax != b->acdirmax)
1804 goto Ebusy;
1805 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1806 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001807 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001808Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001809 return 0;
1810}
1811
1812struct nfs_sb_mountdata {
1813 struct nfs_server *server;
1814 int mntflags;
1815};
1816
1817static int nfs_set_super(struct super_block *s, void *data)
1818{
1819 struct nfs_sb_mountdata *sb_mntdata = data;
1820 struct nfs_server *server = sb_mntdata->server;
1821 int ret;
1822
1823 s->s_flags = sb_mntdata->mntflags;
1824 s->s_fs_info = server;
1825 ret = set_anon_super(s, server);
1826 if (ret == 0)
1827 server->s_dev = s->s_dev;
1828 return ret;
1829}
1830
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001831static int nfs_compare_super_address(struct nfs_server *server1,
1832 struct nfs_server *server2)
1833{
1834 struct sockaddr *sap1, *sap2;
1835
1836 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1837 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1838
1839 if (sap1->sa_family != sap2->sa_family)
1840 return 0;
1841
1842 switch (sap1->sa_family) {
1843 case AF_INET: {
1844 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1845 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1846 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1847 return 0;
1848 if (sin1->sin_port != sin2->sin_port)
1849 return 0;
1850 break;
1851 }
1852 case AF_INET6: {
1853 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1854 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1855 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1856 return 0;
1857 if (sin1->sin6_port != sin2->sin6_port)
1858 return 0;
1859 break;
1860 }
1861 default:
1862 return 0;
1863 }
1864
1865 return 1;
1866}
1867
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001868static int nfs_compare_super(struct super_block *sb, void *data)
1869{
1870 struct nfs_sb_mountdata *sb_mntdata = data;
1871 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1872 int mntflags = sb_mntdata->mntflags;
1873
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001874 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001875 return 0;
1876 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1877 if (old->flags & NFS_MOUNT_UNSHARED)
1878 return 0;
1879 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1880 return 0;
1881 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001882}
1883
Miklos Szeredifa799752008-04-30 00:54:33 -07001884static int nfs_bdi_register(struct nfs_server *server)
1885{
1886 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1887}
1888
David Howells54ceac42006-08-22 20:06:13 -04001889static int nfs_get_sb(struct file_system_type *fs_type,
1890 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1891{
1892 struct nfs_server *server = NULL;
1893 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001894 struct nfs_parsed_mount_data *data;
1895 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001896 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001897 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001898 struct nfs_sb_mountdata sb_mntdata = {
1899 .mntflags = flags,
1900 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001901 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001902
Trond Myklebust33852a12008-06-19 14:20:11 -04001903 data = kzalloc(sizeof(*data), GFP_KERNEL);
1904 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1905 if (data == NULL || mntfh == NULL)
1906 goto out_free_fh;
1907
1908 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001909
David Howells54ceac42006-08-22 20:06:13 -04001910 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04001911 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04001912 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001913 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001914
1915 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001916 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001917 if (IS_ERR(server)) {
1918 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04001919 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001920 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001921 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04001922
Trond Myklebust75180df2007-05-16 16:53:28 -04001923 if (server->flags & NFS_MOUNT_UNSHARED)
1924 compare_super = NULL;
1925
David Howells54ceac42006-08-22 20:06:13 -04001926 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001927 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04001928 if (IS_ERR(s)) {
1929 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04001930 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04001931 }
1932
David Howells54ceac42006-08-22 20:06:13 -04001933 if (s->s_fs_info != server) {
1934 nfs_free_server(server);
1935 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07001936 } else {
1937 error = nfs_bdi_register(server);
1938 if (error)
1939 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04001940 }
David Howells54ceac42006-08-22 20:06:13 -04001941
1942 if (!s->s_root) {
1943 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001944 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04001945 }
1946
Trond Myklebust33852a12008-06-19 14:20:11 -04001947 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001948 if (IS_ERR(mntroot)) {
1949 error = PTR_ERR(mntroot);
1950 goto error_splat_super;
1951 }
1952
Trond Myklebust33852a12008-06-19 14:20:11 -04001953 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001954 if (error)
1955 goto error_splat_root;
1956
David Howellsf7b422b2006-06-09 09:34:33 -04001957 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04001958 mnt->mnt_sb = s;
1959 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04001960 error = 0;
1961
1962out:
Trond Myklebust33852a12008-06-19 14:20:11 -04001963 kfree(data->nfs_server.hostname);
1964 kfree(data->mount_server.hostname);
1965 security_free_mnt_opts(&data->lsm_opts);
1966out_free_fh:
1967 kfree(mntfh);
1968 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04001969 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04001970
David Howells54ceac42006-08-22 20:06:13 -04001971out_err_nosb:
1972 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04001973 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001974
Eric Parisf9c3a382008-03-05 14:20:18 -05001975error_splat_root:
1976 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04001977error_splat_super:
1978 up_write(&s->s_umount);
1979 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04001980 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04001981}
1982
David Howells54ceac42006-08-22 20:06:13 -04001983/*
1984 * Destroy an NFS2/3 superblock
1985 */
David Howellsf7b422b2006-06-09 09:34:33 -04001986static void nfs_kill_super(struct super_block *s)
1987{
1988 struct nfs_server *server = NFS_SB(s);
1989
Miklos Szeredifa799752008-04-30 00:54:33 -07001990 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04001991 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04001992 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04001993}
1994
David Howells54ceac42006-08-22 20:06:13 -04001995/*
1996 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
1997 */
1998static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
1999 const char *dev_name, void *raw_data,
2000 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002001{
2002 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002003 struct super_block *s;
2004 struct nfs_server *server;
2005 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002006 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002007 struct nfs_sb_mountdata sb_mntdata = {
2008 .mntflags = flags,
2009 };
David Howells54ceac42006-08-22 20:06:13 -04002010 int error;
2011
2012 dprintk("--> nfs_xdev_get_sb()\n");
2013
2014 /* create a new volume representation */
2015 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2016 if (IS_ERR(server)) {
2017 error = PTR_ERR(server);
2018 goto out_err_noserver;
2019 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002020 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002021
Trond Myklebust75180df2007-05-16 16:53:28 -04002022 if (server->flags & NFS_MOUNT_UNSHARED)
2023 compare_super = NULL;
2024
David Howells54ceac42006-08-22 20:06:13 -04002025 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002026 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002027 if (IS_ERR(s)) {
2028 error = PTR_ERR(s);
2029 goto out_err_nosb;
2030 }
2031
2032 if (s->s_fs_info != server) {
2033 nfs_free_server(server);
2034 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002035 } else {
2036 error = nfs_bdi_register(server);
2037 if (error)
2038 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002039 }
2040
2041 if (!s->s_root) {
2042 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002043 nfs_clone_super(s, data->sb);
2044 }
2045
2046 mntroot = nfs_get_root(s, data->fh);
2047 if (IS_ERR(mntroot)) {
2048 error = PTR_ERR(mntroot);
2049 goto error_splat_super;
2050 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002051 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002052 dput(mntroot);
2053 error = -ESTALE;
2054 goto error_splat_super;
2055 }
David Howells54ceac42006-08-22 20:06:13 -04002056
2057 s->s_flags |= MS_ACTIVE;
2058 mnt->mnt_sb = s;
2059 mnt->mnt_root = mntroot;
2060
Eric Parisf9c3a382008-03-05 14:20:18 -05002061 /* clone any lsm security options from the parent to the new sb */
2062 security_sb_clone_mnt_opts(data->sb, s);
2063
David Howells54ceac42006-08-22 20:06:13 -04002064 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2065 return 0;
2066
2067out_err_nosb:
2068 nfs_free_server(server);
2069out_err_noserver:
2070 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2071 return error;
2072
2073error_splat_super:
2074 up_write(&s->s_umount);
2075 deactivate_super(s);
2076 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2077 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002078}
2079
2080#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002081
2082/*
2083 * Finish setting up a cloned NFS4 superblock
2084 */
2085static void nfs4_clone_super(struct super_block *sb,
2086 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002087{
David Howells54ceac42006-08-22 20:06:13 -04002088 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2089 sb->s_blocksize = old_sb->s_blocksize;
2090 sb->s_maxbytes = old_sb->s_maxbytes;
2091 sb->s_time_gran = 1;
2092 sb->s_op = old_sb->s_op;
2093 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002094}
2095
2096/*
2097 * Set up an NFS4 superblock
2098 */
David Howells54ceac42006-08-22 20:06:13 -04002099static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002100{
David Howellsf7b422b2006-06-09 09:34:33 -04002101 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002102 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002103 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002104}
2105
David Howells54ceac42006-08-22 20:06:13 -04002106/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002107 * Validate NFSv4 mount options
2108 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002109static int nfs4_validate_mount_data(void *options,
2110 struct nfs_parsed_mount_data *args,
2111 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002112{
Chuck Lever4c568012007-12-10 14:59:28 -05002113 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002114 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002115 char *c;
2116
2117 if (data == NULL)
2118 goto out_no_data;
2119
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002120 args->rsize = NFS_MAX_FILE_IO_SIZE;
2121 args->wsize = NFS_MAX_FILE_IO_SIZE;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002122 args->acregmin = 3;
2123 args->acregmax = 60;
2124 args->acdirmin = 30;
2125 args->acdirmax = 60;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002126 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002127
Chuck Leverf0768eb2007-07-01 12:13:01 -04002128 switch (data->version) {
2129 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002130 ap = (struct sockaddr_in *)&args->nfs_server.address;
2131 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002132 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002133 if (data->host_addrlen == 0)
2134 goto out_no_address;
2135 args->nfs_server.addrlen = data->host_addrlen;
2136 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002137 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002138 if (!nfs_verify_server_address((struct sockaddr *)
2139 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002140 goto out_no_address;
2141
2142 switch (data->auth_flavourlen) {
2143 case 0:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002144 args->auth_flavors[0] = RPC_AUTH_UNIX;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002145 break;
2146 case 1:
Trond Myklebust20c71f52007-09-20 20:23:51 -04002147 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002148 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002149 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002150 return -EFAULT;
2151 break;
2152 default:
2153 goto out_inval_auth;
2154 }
2155
2156 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2157 if (IS_ERR(c))
2158 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002159 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002160
2161 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2162 if (IS_ERR(c))
2163 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002164 args->nfs_server.export_path = c;
2165 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002166
2167 c = strndup_user(data->client_addr.data, 16);
2168 if (IS_ERR(c))
2169 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002170 args->client_address = c;
2171
2172 /*
2173 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2174 * can deal with.
2175 */
2176
2177 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2178 args->rsize = data->rsize;
2179 args->wsize = data->wsize;
2180 args->timeo = data->timeo;
2181 args->retrans = data->retrans;
2182 args->acregmin = data->acregmin;
2183 args->acregmax = data->acregmax;
2184 args->acdirmin = data->acdirmin;
2185 args->acdirmax = data->acdirmax;
2186 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002187 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002188
2189 break;
Chuck Lever80071222007-07-01 12:13:59 -04002190 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002191 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002192
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002193 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002194 return -EINVAL;
2195
2196 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002197 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002198 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002199
Trond Myklebust259875e2008-07-02 14:43:47 -04002200 nfs_validate_transport_protocol(args);
2201
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002202 switch (args->auth_flavor_len) {
Chuck Lever80071222007-07-01 12:13:59 -04002203 case 0:
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002204 args->auth_flavors[0] = RPC_AUTH_UNIX;
Chuck Lever80071222007-07-01 12:13:59 -04002205 break;
2206 case 1:
Chuck Lever80071222007-07-01 12:13:59 -04002207 break;
2208 default:
2209 goto out_inval_auth;
2210 }
2211
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002212 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002213 goto out_no_client_address;
2214
Chuck Leverdc045892008-06-23 12:36:37 -04002215 status = nfs_parse_devname(dev_name,
2216 &args->nfs_server.hostname,
2217 NFS4_MAXNAMLEN,
2218 &args->nfs_server.export_path,
2219 NFS4_MAXPATHLEN);
2220 if (status < 0)
2221 return status;
2222
Chuck Lever80071222007-07-01 12:13:59 -04002223 break;
2224 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002225 }
2226
2227 return 0;
2228
2229out_no_data:
2230 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2231 return -EINVAL;
2232
2233out_inval_auth:
2234 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2235 data->auth_flavourlen);
2236 return -EINVAL;
2237
2238out_no_address:
2239 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2240 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002241
2242out_no_client_address:
2243 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2244 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002245}
2246
2247/*
David Howells54ceac42006-08-22 20:06:13 -04002248 * Get the superblock for an NFS4 mountpoint
2249 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002250static int nfs4_get_sb(struct file_system_type *fs_type,
2251 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002252{
Trond Myklebust33852a12008-06-19 14:20:11 -04002253 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002254 struct super_block *s;
2255 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002256 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002257 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002258 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002259 struct nfs_sb_mountdata sb_mntdata = {
2260 .mntflags = flags,
2261 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002262 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002263
Trond Myklebust33852a12008-06-19 14:20:11 -04002264 data = kzalloc(sizeof(*data), GFP_KERNEL);
2265 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2266 if (data == NULL || mntfh == NULL)
2267 goto out_free_fh;
2268
2269 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002270
Chuck Leverf0768eb2007-07-01 12:13:01 -04002271 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002272 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002273 if (error < 0)
2274 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002275
David Howells54ceac42006-08-22 20:06:13 -04002276 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002277 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002278 if (IS_ERR(server)) {
2279 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002280 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002281 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002282 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002283
Trond Myklebust75180df2007-05-16 16:53:28 -04002284 if (server->flags & NFS4_MOUNT_UNSHARED)
2285 compare_super = NULL;
2286
David Howells54ceac42006-08-22 20:06:13 -04002287 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002288 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002289 if (IS_ERR(s)) {
2290 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002291 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002292 }
2293
Trond Myklebust5dd31772006-08-24 01:03:05 -04002294 if (s->s_fs_info != server) {
2295 nfs_free_server(server);
2296 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002297 } else {
2298 error = nfs_bdi_register(server);
2299 if (error)
2300 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002301 }
2302
David Howells54ceac42006-08-22 20:06:13 -04002303 if (!s->s_root) {
2304 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002305 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002306 }
David Howellsf7b422b2006-06-09 09:34:33 -04002307
Trond Myklebust33852a12008-06-19 14:20:11 -04002308 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002309 if (IS_ERR(mntroot)) {
2310 error = PTR_ERR(mntroot);
2311 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002312 }
David Howells54ceac42006-08-22 20:06:13 -04002313
Trond Myklebust33852a12008-06-19 14:20:11 -04002314 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002315 if (error)
2316 goto error_splat_root;
2317
David Howellsf7b422b2006-06-09 09:34:33 -04002318 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002319 mnt->mnt_sb = s;
2320 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002321 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002322
Chuck Lever29eb9812007-07-01 12:12:30 -04002323out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002324 kfree(data->client_address);
2325 kfree(data->nfs_server.export_path);
2326 kfree(data->nfs_server.hostname);
2327 security_free_mnt_opts(&data->lsm_opts);
2328out_free_fh:
2329 kfree(mntfh);
2330 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002331 return error;
David Howells54ceac42006-08-22 20:06:13 -04002332
Chuck Lever29eb9812007-07-01 12:12:30 -04002333out_free:
2334 nfs_free_server(server);
2335 goto out;
2336
Eric Paris46c8ac72008-05-02 13:42:42 -07002337error_splat_root:
2338 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002339error_splat_super:
2340 up_write(&s->s_umount);
2341 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002342 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002343}
2344
2345static void nfs4_kill_super(struct super_block *sb)
2346{
2347 struct nfs_server *server = NFS_SB(sb);
2348
2349 nfs_return_all_delegations(sb);
2350 kill_anon_super(sb);
2351
2352 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002353 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002354}
2355
2356/*
David Howells54ceac42006-08-22 20:06:13 -04002357 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002358 */
David Howells54ceac42006-08-22 20:06:13 -04002359static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2360 const char *dev_name, void *raw_data,
2361 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002362{
2363 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002364 struct super_block *s;
2365 struct nfs_server *server;
2366 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002367 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002368 struct nfs_sb_mountdata sb_mntdata = {
2369 .mntflags = flags,
2370 };
David Howells54ceac42006-08-22 20:06:13 -04002371 int error;
2372
2373 dprintk("--> nfs4_xdev_get_sb()\n");
2374
2375 /* create a new volume representation */
2376 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2377 if (IS_ERR(server)) {
2378 error = PTR_ERR(server);
2379 goto out_err_noserver;
2380 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002381 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002382
Trond Myklebust75180df2007-05-16 16:53:28 -04002383 if (server->flags & NFS4_MOUNT_UNSHARED)
2384 compare_super = NULL;
2385
David Howells54ceac42006-08-22 20:06:13 -04002386 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002387 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002388 if (IS_ERR(s)) {
2389 error = PTR_ERR(s);
2390 goto out_err_nosb;
2391 }
2392
2393 if (s->s_fs_info != server) {
2394 nfs_free_server(server);
2395 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002396 } else {
2397 error = nfs_bdi_register(server);
2398 if (error)
2399 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002400 }
2401
2402 if (!s->s_root) {
2403 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002404 nfs4_clone_super(s, data->sb);
2405 }
2406
2407 mntroot = nfs4_get_root(s, data->fh);
2408 if (IS_ERR(mntroot)) {
2409 error = PTR_ERR(mntroot);
2410 goto error_splat_super;
2411 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002412 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2413 dput(mntroot);
2414 error = -ESTALE;
2415 goto error_splat_super;
2416 }
David Howells54ceac42006-08-22 20:06:13 -04002417
2418 s->s_flags |= MS_ACTIVE;
2419 mnt->mnt_sb = s;
2420 mnt->mnt_root = mntroot;
2421
Eric Paris46c8ac72008-05-02 13:42:42 -07002422 security_sb_clone_mnt_opts(data->sb, s);
2423
David Howells54ceac42006-08-22 20:06:13 -04002424 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2425 return 0;
2426
2427out_err_nosb:
2428 nfs_free_server(server);
2429out_err_noserver:
2430 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2431 return error;
2432
2433error_splat_super:
2434 up_write(&s->s_umount);
2435 deactivate_super(s);
2436 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2437 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002438}
2439
David Howells54ceac42006-08-22 20:06:13 -04002440/*
2441 * Create an NFS4 server record on referral traversal
2442 */
2443static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2444 const char *dev_name, void *raw_data,
2445 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002446{
2447 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002448 struct super_block *s;
2449 struct nfs_server *server;
2450 struct dentry *mntroot;
2451 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002452 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002453 struct nfs_sb_mountdata sb_mntdata = {
2454 .mntflags = flags,
2455 };
David Howells54ceac42006-08-22 20:06:13 -04002456 int error;
2457
2458 dprintk("--> nfs4_referral_get_sb()\n");
2459
2460 /* create a new volume representation */
2461 server = nfs4_create_referral_server(data, &mntfh);
2462 if (IS_ERR(server)) {
2463 error = PTR_ERR(server);
2464 goto out_err_noserver;
2465 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002466 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002467
Trond Myklebust75180df2007-05-16 16:53:28 -04002468 if (server->flags & NFS4_MOUNT_UNSHARED)
2469 compare_super = NULL;
2470
David Howells54ceac42006-08-22 20:06:13 -04002471 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002472 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002473 if (IS_ERR(s)) {
2474 error = PTR_ERR(s);
2475 goto out_err_nosb;
2476 }
2477
2478 if (s->s_fs_info != server) {
2479 nfs_free_server(server);
2480 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002481 } else {
2482 error = nfs_bdi_register(server);
2483 if (error)
2484 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002485 }
2486
2487 if (!s->s_root) {
2488 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002489 nfs4_fill_super(s);
2490 }
2491
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002492 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002493 if (IS_ERR(mntroot)) {
2494 error = PTR_ERR(mntroot);
2495 goto error_splat_super;
2496 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002497 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2498 dput(mntroot);
2499 error = -ESTALE;
2500 goto error_splat_super;
2501 }
David Howells54ceac42006-08-22 20:06:13 -04002502
2503 s->s_flags |= MS_ACTIVE;
2504 mnt->mnt_sb = s;
2505 mnt->mnt_root = mntroot;
2506
Eric Paris46c8ac72008-05-02 13:42:42 -07002507 security_sb_clone_mnt_opts(data->sb, s);
2508
David Howells54ceac42006-08-22 20:06:13 -04002509 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2510 return 0;
2511
2512out_err_nosb:
2513 nfs_free_server(server);
2514out_err_noserver:
2515 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2516 return error;
2517
2518error_splat_super:
2519 up_write(&s->s_umount);
2520 deactivate_super(s);
2521 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2522 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002523}
2524
David Howells54ceac42006-08-22 20:06:13 -04002525#endif /* CONFIG_NFS_V4 */