blob: e527fab40419e4ad5619e74db72ffc589e11e930 [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
Chuck Leverf45663c2008-06-24 19:28:02 -040095 /* Special mount options */
96 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -040097
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 Leverf45663c2008-06-24 19:28:02 -0400106 { Opt_sloppy, "sloppy" },
107
Chuck Leverbf0fd762007-07-01 12:13:44 -0400108 { Opt_soft, "soft" },
109 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400110 { Opt_deprecated, "intr" },
111 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400112 { Opt_posix, "posix" },
113 { Opt_noposix, "noposix" },
114 { Opt_cto, "cto" },
115 { Opt_nocto, "nocto" },
116 { Opt_ac, "ac" },
117 { Opt_noac, "noac" },
118 { Opt_lock, "lock" },
119 { Opt_nolock, "nolock" },
120 { Opt_v2, "v2" },
121 { Opt_v3, "v3" },
122 { Opt_udp, "udp" },
123 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400124 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400125 { Opt_acl, "acl" },
126 { Opt_noacl, "noacl" },
127 { Opt_rdirplus, "rdirplus" },
128 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400129 { Opt_sharecache, "sharecache" },
130 { Opt_nosharecache, "nosharecache" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400131
132 { Opt_port, "port=%u" },
133 { Opt_rsize, "rsize=%u" },
134 { Opt_wsize, "wsize=%u" },
135 { Opt_bsize, "bsize=%u" },
136 { Opt_timeo, "timeo=%u" },
137 { Opt_retrans, "retrans=%u" },
138 { Opt_acregmin, "acregmin=%u" },
139 { Opt_acregmax, "acregmax=%u" },
140 { Opt_acdirmin, "acdirmin=%u" },
141 { Opt_acdirmax, "acdirmax=%u" },
142 { Opt_actimeo, "actimeo=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400143 { Opt_namelen, "namlen=%u" },
144 { Opt_mountport, "mountport=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400145 { Opt_mountvers, "mountvers=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400146 { Opt_nfsvers, "nfsvers=%u" },
147 { Opt_nfsvers, "vers=%u" },
148
149 { Opt_sec, "sec=%s" },
150 { Opt_proto, "proto=%s" },
151 { Opt_mountproto, "mountproto=%s" },
152 { Opt_addr, "addr=%s" },
153 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500154 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400155 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400156
157 { Opt_err, NULL }
158};
159
160enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400161 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400162
163 Opt_xprt_err
164};
165
166static match_table_t nfs_xprt_protocol_tokens = {
167 { Opt_xprt_udp, "udp" },
168 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400169 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400170
171 { Opt_xprt_err, NULL }
172};
173
174enum {
175 Opt_sec_none, Opt_sec_sys,
176 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
177 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
178 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
179
180 Opt_sec_err
181};
182
183static match_table_t nfs_secflavor_tokens = {
184 { Opt_sec_none, "none" },
185 { Opt_sec_none, "null" },
186 { Opt_sec_sys, "sys" },
187
188 { Opt_sec_krb5, "krb5" },
189 { Opt_sec_krb5i, "krb5i" },
190 { Opt_sec_krb5p, "krb5p" },
191
192 { Opt_sec_lkey, "lkey" },
193 { Opt_sec_lkeyi, "lkeyi" },
194 { Opt_sec_lkeyp, "lkeyp" },
195
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500196 { Opt_sec_spkm, "spkm3" },
197 { Opt_sec_spkmi, "spkm3i" },
198 { Opt_sec_spkmp, "spkm3p" },
199
Chuck Leverbf0fd762007-07-01 12:13:44 -0400200 { Opt_sec_err, NULL }
201};
202
203
Al Viro42faad92008-04-24 07:21:56 -0400204static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400205static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400206static int nfs_show_options(struct seq_file *, struct vfsmount *);
207static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400208static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400209static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400210 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400211static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400212static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400213
214static struct file_system_type nfs_fs_type = {
215 .owner = THIS_MODULE,
216 .name = "nfs",
217 .get_sb = nfs_get_sb,
218 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700219 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400220};
221
David Howells54ceac42006-08-22 20:06:13 -0400222struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400223 .owner = THIS_MODULE,
224 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400225 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400226 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700227 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400228};
229
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800230static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400231 .alloc_inode = nfs_alloc_inode,
232 .destroy_inode = nfs_destroy_inode,
233 .write_inode = nfs_write_inode,
234 .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
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400338void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500339{
340 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400341
342 if (atomic_inc_return(&server->active) == 1)
343 atomic_inc(&sb->s_active);
344}
345
346void nfs_sb_deactive(struct super_block *sb)
347{
348 struct nfs_server *server = NFS_SB(sb);
349
350 if (atomic_dec_and_test(&server->active))
351 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500352}
353
David Howellsf7b422b2006-06-09 09:34:33 -0400354/*
355 * Deliver file system statistics to userspace
356 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400357static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400358{
David Howells0c7d90c2006-08-22 20:06:10 -0400359 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400360 unsigned char blockbits;
361 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400362 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400363 struct nfs_fattr fattr;
364 struct nfs_fsstat res = {
365 .fattr = &fattr,
366 };
367 int error;
368
David Howells8fa5c002006-08-22 20:06:12 -0400369 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400370 if (error < 0)
371 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700372 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400373
374 /*
375 * Current versions of glibc do not correctly handle the
376 * case where f_frsize != f_bsize. Eventually we want to
377 * report the value of wtmult in this field.
378 */
David Howells0c7d90c2006-08-22 20:06:10 -0400379 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400380
381 /*
382 * On most *nix systems, f_blocks, f_bfree, and f_bavail
383 * are reported in units of f_frsize. Linux hasn't had
384 * an f_frsize field in its statfs struct until recently,
385 * thus historically Linux's sys_statfs reports these
386 * fields in units of f_bsize.
387 */
David Howells0c7d90c2006-08-22 20:06:10 -0400388 buf->f_bsize = dentry->d_sb->s_blocksize;
389 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400390 blockres = (1 << blockbits) - 1;
391 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
392 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
393 buf->f_bavail = (res.abytes + blockres) >> blockbits;
394
395 buf->f_files = res.tfiles;
396 buf->f_ffree = res.afiles;
397
398 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700399
David Howellsf7b422b2006-06-09 09:34:33 -0400400 return 0;
401
402 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700403 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700404 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400405}
406
David Howells7d4e2742006-08-22 20:06:07 -0400407/*
408 * Map the security flavour number to a name
409 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400410static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
411{
David Howells7d4e2742006-08-22 20:06:07 -0400412 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400413 rpc_authflavor_t flavour;
414 const char *str;
415 } sec_flavours[] = {
416 { RPC_AUTH_NULL, "null" },
417 { RPC_AUTH_UNIX, "sys" },
418 { RPC_AUTH_GSS_KRB5, "krb5" },
419 { RPC_AUTH_GSS_KRB5I, "krb5i" },
420 { RPC_AUTH_GSS_KRB5P, "krb5p" },
421 { RPC_AUTH_GSS_LKEY, "lkey" },
422 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
423 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
424 { RPC_AUTH_GSS_SPKM, "spkm" },
425 { RPC_AUTH_GSS_SPKMI, "spkmi" },
426 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400427 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400428 };
429 int i;
430
Chuck Lever4d81cd12007-07-01 12:12:40 -0400431 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400432 if (sec_flavours[i].flavour == flavour)
433 break;
434 }
435 return sec_flavours[i].str;
436}
437
Chuck Lever82d101d2008-03-14 14:10:37 -0400438static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
439 int showdefaults)
440{
441 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
442
443 switch (sap->sa_family) {
444 case AF_INET: {
445 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
446 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
447 NIPQUAD(sin->sin_addr.s_addr));
448 break;
449 }
450 case AF_INET6: {
451 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
452 seq_printf(m, ",mountaddr=" NIP6_FMT,
453 NIP6(sin6->sin6_addr));
454 break;
455 }
456 default:
457 if (showdefaults)
458 seq_printf(m, ",mountaddr=unspecified");
459 }
460
461 if (nfss->mountd_version || showdefaults)
462 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
463 if (nfss->mountd_port || showdefaults)
464 seq_printf(m, ",mountport=%u", nfss->mountd_port);
465
466 switch (nfss->mountd_protocol) {
467 case IPPROTO_UDP:
468 seq_printf(m, ",mountproto=udp");
469 break;
470 case IPPROTO_TCP:
471 seq_printf(m, ",mountproto=tcp");
472 break;
473 default:
474 if (showdefaults)
475 seq_printf(m, ",mountproto=auto");
476 }
477}
478
David Howellsf7b422b2006-06-09 09:34:33 -0400479/*
480 * Describe the mount options in force on this server representation
481 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400482static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
483 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400484{
David Howells509de812006-08-22 20:06:11 -0400485 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400486 int flag;
David Howells509de812006-08-22 20:06:11 -0400487 const char *str;
488 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400489 } nfs_info[] = {
490 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400491 { NFS_MOUNT_INTR, ",intr", ",nointr" },
492 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400493 { NFS_MOUNT_NOCTO, ",nocto", "" },
494 { NFS_MOUNT_NOAC, ",noac", "" },
495 { NFS_MOUNT_NONLM, ",nolock", "" },
496 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400497 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400498 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
David Howellsf7b422b2006-06-09 09:34:33 -0400499 { 0, NULL, NULL }
500 };
David Howells509de812006-08-22 20:06:11 -0400501 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400502 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400503 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400504
Chuck Lever82d101d2008-03-14 14:10:37 -0400505 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400506 seq_printf(m, ",rsize=%u", nfss->rsize);
507 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400508 if (nfss->bsize != 0)
509 seq_printf(m, ",bsize=%u", nfss->bsize);
510 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400511 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400512 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400513 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400514 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400515 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400516 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400517 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400518 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400519 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
520 if (nfss->flags & nfs_infop->flag)
521 seq_puts(m, nfs_infop->str);
522 else
523 seq_puts(m, nfs_infop->nostr);
524 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400525 seq_printf(m, ",proto=%s",
526 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400527 if (version == 4) {
528 if (nfss->port != NFS_PORT)
529 seq_printf(m, ",port=%u", nfss->port);
530 } else
531 if (nfss->port)
532 seq_printf(m, ",port=%u", nfss->port);
533
Trond Myklebust33170232007-12-20 16:03:59 -0500534 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
535 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400536 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400537
538 if (version != 4)
539 nfs_show_mountd_options(m, nfss, showdefaults);
540
541#ifdef CONFIG_NFS_V4
542 if (clp->rpc_ops->version == 4)
543 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
544#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400545}
546
547/*
548 * Describe the mount options on this VFS mountpoint
549 */
550static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
551{
552 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
553
554 nfs_show_mount_options(m, nfss, 0);
555
Chuck Lever5d8515c2007-12-10 14:57:16 -0500556 seq_printf(m, ",addr=%s",
557 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
558 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400559
560 return 0;
561}
562
563/*
564 * Present statistical information for this VFS mountpoint
565 */
566static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
567{
568 int i, cpu;
569 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
570 struct rpc_auth *auth = nfss->client->cl_auth;
571 struct nfs_iostats totals = { };
572
573 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
574
575 /*
576 * Display all mount option settings
577 */
578 seq_printf(m, "\n\topts:\t");
579 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
580 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
581 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
582 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
583 nfs_show_mount_options(m, nfss, 1);
584
585 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
586
587 seq_printf(m, "\n\tcaps:\t");
588 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400589 seq_printf(m, ",wtmult=%u", nfss->wtmult);
590 seq_printf(m, ",dtsize=%u", nfss->dtsize);
591 seq_printf(m, ",bsize=%u", nfss->bsize);
592 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400593
594#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500595 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400596 seq_printf(m, "\n\tnfsv4:\t");
597 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
598 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
599 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
600 }
601#endif
602
603 /*
604 * Display security flavor in effect for this mount
605 */
Chuck Lever2d767432008-03-14 14:10:08 -0400606 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400607 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400608 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400609
610 /*
611 * Display superblock I/O counters
612 */
613 for_each_possible_cpu(cpu) {
614 struct nfs_iostats *stats;
615
616 preempt_disable();
617 stats = per_cpu_ptr(nfss->io_stats, cpu);
618
619 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
620 totals.events[i] += stats->events[i];
621 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
622 totals.bytes[i] += stats->bytes[i];
623
624 preempt_enable();
625 }
626
627 seq_printf(m, "\n\tevents:\t");
628 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
629 seq_printf(m, "%lu ", totals.events[i]);
630 seq_printf(m, "\n\tbytes:\t");
631 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
632 seq_printf(m, "%Lu ", totals.bytes[i]);
633 seq_printf(m, "\n");
634
635 rpc_print_iostats(m, nfss->client);
636
637 return 0;
638}
639
640/*
641 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400642 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400643 */
Al Viro42faad92008-04-24 07:21:56 -0400644static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400645{
Al Viro42faad92008-04-24 07:21:56 -0400646 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400647 struct rpc_clnt *rpc;
648
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400649 /* -EIO all pending I/O */
650 rpc = server->client_acl;
651 if (!IS_ERR(rpc))
652 rpc_killall_tasks(rpc);
653 rpc = server->client;
654 if (!IS_ERR(rpc))
655 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400656}
657
658/*
Chuck Lever04dcd6e2007-12-10 14:57:53 -0500659 * Set the port number in an address. Be agnostic about the address family.
660 */
661static void nfs_set_port(struct sockaddr *sap, unsigned short port)
662{
663 switch (sap->sa_family) {
664 case AF_INET: {
665 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
666 ap->sin_port = htons(port);
667 break;
668 }
669 case AF_INET6: {
670 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
671 ap->sin6_port = htons(port);
672 break;
673 }
674 }
675}
676
677/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500678 * Sanity-check a server address provided by the mount command.
679 *
680 * Address family must be initialized, and address must not be
681 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400682 */
683static int nfs_verify_server_address(struct sockaddr *addr)
684{
685 switch (addr->sa_family) {
686 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500687 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700688 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500689 }
690 case AF_INET6: {
691 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
692 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400693 }
694 }
695
696 return 0;
697}
698
Chuck Leverce3b7e12008-06-23 12:36:53 -0400699static void nfs_parse_ipv4_address(char *string, size_t str_len,
700 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500701{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400702 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
703 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500704
Chuck Leverce3b7e12008-06-23 12:36:53 -0400705 if (str_len <= INET_ADDRSTRLEN) {
706 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
707 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500708
Chuck Leverce3b7e12008-06-23 12:36:53 -0400709 sin->sin_family = AF_INET;
710 *addr_len = sizeof(*sin);
711 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500712 return;
713 }
Chuck Lever9412b922007-12-10 14:59:21 -0500714
715 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400716 *addr_len = 0;
717}
718
Chuck Leverd8e77482008-06-23 12:37:01 -0400719#define IPV6_SCOPE_DELIMITER '%'
720
Chuck Leverce3b7e12008-06-23 12:36:53 -0400721#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Leverd8e77482008-06-23 12:37:01 -0400722static void nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
723 const char *delim,
724 struct sockaddr_in6 *sin6)
725{
726 char *p;
727 size_t len;
728
729 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
730 return ;
731 if (*delim != IPV6_SCOPE_DELIMITER)
732 return;
733
734 len = (string + str_len) - delim - 1;
735 p = kstrndup(delim + 1, len, GFP_KERNEL);
736 if (p) {
737 unsigned long scope_id = 0;
738 struct net_device *dev;
739
740 dev = dev_get_by_name(&init_net, p);
741 if (dev != NULL) {
742 scope_id = dev->ifindex;
743 dev_put(dev);
744 } else {
745 /* scope_id is set to zero on error */
746 strict_strtoul(p, 10, &scope_id);
747 }
748
749 kfree(p);
750 sin6->sin6_scope_id = scope_id;
751 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
752 }
753}
754
Chuck Leverce3b7e12008-06-23 12:36:53 -0400755static void nfs_parse_ipv6_address(char *string, size_t str_len,
756 struct sockaddr *sap, size_t *addr_len)
757{
758 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
759 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400760 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400761
762 if (str_len <= INET6_ADDRSTRLEN) {
763 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
764 (int)str_len, string);
765
766 sin6->sin6_family = AF_INET6;
767 *addr_len = sizeof(*sin6);
Chuck Leverd8e77482008-06-23 12:37:01 -0400768 if (in6_pton(string, str_len, addr, IPV6_SCOPE_DELIMITER, &delim)) {
769 nfs_parse_ipv6_scope_id(string, str_len, delim, sin6);
Chuck Leverce3b7e12008-06-23 12:36:53 -0400770 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400771 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400772 }
773
774 sap->sa_family = AF_UNSPEC;
775 *addr_len = 0;
776}
777#else
778static void nfs_parse_ipv6_address(char *string, size_t str_len,
779 struct sockaddr *sap, size_t *addr_len)
780{
781 sap->sa_family = AF_UNSPEC;
782 *addr_len = 0;
783}
784#endif
785
786/*
787 * Construct a sockaddr based on the contents of a string that contains
788 * an IP address in presentation format.
789 *
790 * If there is a problem constructing the new sockaddr, set the address
791 * family to AF_UNSPEC.
792 */
793static void nfs_parse_ip_address(char *string, size_t str_len,
794 struct sockaddr *sap, size_t *addr_len)
795{
796 unsigned int i, colons;
797
798 colons = 0;
799 for (i = 0; i < str_len; i++)
800 if (string[i] == ':')
801 colons++;
802
803 if (colons >= 2)
804 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
805 else
806 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500807}
808
809/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400810 * Sanity check the NFS transport protocol.
811 *
812 */
813static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
814{
815 switch (mnt->nfs_server.protocol) {
816 case XPRT_TRANSPORT_UDP:
817 case XPRT_TRANSPORT_TCP:
818 case XPRT_TRANSPORT_RDMA:
819 break;
820 default:
821 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
822 }
823}
824
825/*
826 * For text based NFSv2/v3 mounts, the mount protocol transport default
827 * settings should depend upon the specified NFS transport.
828 */
829static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
830{
831 nfs_validate_transport_protocol(mnt);
832
833 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
834 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
835 return;
836 switch (mnt->nfs_server.protocol) {
837 case XPRT_TRANSPORT_UDP:
838 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
839 break;
840 case XPRT_TRANSPORT_TCP:
841 case XPRT_TRANSPORT_RDMA:
842 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
843 }
844}
845
846/*
Chuck Lever01060c82008-06-24 16:33:38 -0400847 * Parse the value of the 'sec=' option.
848 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400849 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400850 */
851static int nfs_parse_security_flavors(char *value,
852 struct nfs_parsed_mount_data *mnt)
853{
854 substring_t args[MAX_OPT_ARGS];
855
856 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
857
858 switch (match_token(value, nfs_secflavor_tokens, args)) {
859 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400860 mnt->auth_flavor_len = 0;
861 mnt->auth_flavors[0] = RPC_AUTH_NULL;
862 break;
863 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400864 mnt->auth_flavor_len = 0;
865 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
866 break;
867 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400868 mnt->auth_flavor_len = 1;
869 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
870 break;
871 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400872 mnt->auth_flavor_len = 1;
873 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
874 break;
875 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400876 mnt->auth_flavor_len = 1;
877 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
878 break;
879 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400880 mnt->auth_flavor_len = 1;
881 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
882 break;
883 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400884 mnt->auth_flavor_len = 1;
885 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
886 break;
887 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400888 mnt->auth_flavor_len = 1;
889 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
890 break;
891 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400892 mnt->auth_flavor_len = 1;
893 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
894 break;
895 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400896 mnt->auth_flavor_len = 1;
897 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
898 break;
899 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400900 mnt->auth_flavor_len = 1;
901 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
902 break;
903 default:
904 return 0;
905 }
906
907 return 1;
908}
909
Chuck Leverf45663c2008-06-24 19:28:02 -0400910static void nfs_parse_invalid_value(const char *option)
911{
912 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
913}
914
Chuck Lever01060c82008-06-24 16:33:38 -0400915/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400916 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400917 * a data structure. The whole mount string is processed; bad options are
918 * skipped as they are encountered. If there were no errors, return 1;
919 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400920 */
921static int nfs_parse_mount_options(char *raw,
922 struct nfs_parsed_mount_data *mnt)
923{
Eric Parisf9c3a382008-03-05 14:20:18 -0500924 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400925 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400926
927 if (!raw) {
928 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
929 return 1;
930 }
931 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
932
Eric Parisf9c3a382008-03-05 14:20:18 -0500933 secdata = alloc_secdata();
934 if (!secdata)
935 goto out_nomem;
936
937 rc = security_sb_copy_data(raw, secdata);
938 if (rc)
939 goto out_security_failure;
940
941 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
942 if (rc)
943 goto out_security_failure;
944
945 free_secdata(secdata);
946
Chuck Leverbf0fd762007-07-01 12:13:44 -0400947 while ((p = strsep(&raw, ",")) != NULL) {
948 substring_t args[MAX_OPT_ARGS];
949 int option, token;
950
951 if (!*p)
952 continue;
953
954 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
955
956 token = match_token(p, nfs_mount_option_tokens, args);
957 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400958
959 /*
960 * boolean options: foo/nofoo
961 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400962 case Opt_soft:
963 mnt->flags |= NFS_MOUNT_SOFT;
964 break;
965 case Opt_hard:
966 mnt->flags &= ~NFS_MOUNT_SOFT;
967 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400968 case Opt_posix:
969 mnt->flags |= NFS_MOUNT_POSIX;
970 break;
971 case Opt_noposix:
972 mnt->flags &= ~NFS_MOUNT_POSIX;
973 break;
974 case Opt_cto:
975 mnt->flags &= ~NFS_MOUNT_NOCTO;
976 break;
977 case Opt_nocto:
978 mnt->flags |= NFS_MOUNT_NOCTO;
979 break;
980 case Opt_ac:
981 mnt->flags &= ~NFS_MOUNT_NOAC;
982 break;
983 case Opt_noac:
984 mnt->flags |= NFS_MOUNT_NOAC;
985 break;
986 case Opt_lock:
987 mnt->flags &= ~NFS_MOUNT_NONLM;
988 break;
989 case Opt_nolock:
990 mnt->flags |= NFS_MOUNT_NONLM;
991 break;
992 case Opt_v2:
993 mnt->flags &= ~NFS_MOUNT_VER3;
994 break;
995 case Opt_v3:
996 mnt->flags |= NFS_MOUNT_VER3;
997 break;
998 case Opt_udp:
999 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001000 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001001 break;
1002 case Opt_tcp:
1003 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001004 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001005 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001006 case Opt_rdma:
1007 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1008 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001009 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001010 case Opt_acl:
1011 mnt->flags &= ~NFS_MOUNT_NOACL;
1012 break;
1013 case Opt_noacl:
1014 mnt->flags |= NFS_MOUNT_NOACL;
1015 break;
1016 case Opt_rdirplus:
1017 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1018 break;
1019 case Opt_nordirplus:
1020 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1021 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001022 case Opt_sharecache:
1023 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1024 break;
1025 case Opt_nosharecache:
1026 mnt->flags |= NFS_MOUNT_UNSHARED;
1027 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001028
Chuck Leverf45663c2008-06-24 19:28:02 -04001029 /*
1030 * options that take numeric values
1031 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001032 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001033 if (match_int(args, &option) ||
1034 option < 0 || option > USHORT_MAX) {
1035 errors++;
1036 nfs_parse_invalid_value("port");
1037 } else
1038 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001039 break;
1040 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001041 if (match_int(args, &option) || option < 0) {
1042 errors++;
1043 nfs_parse_invalid_value("rsize");
1044 } else
1045 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001046 break;
1047 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001048 if (match_int(args, &option) || option < 0) {
1049 errors++;
1050 nfs_parse_invalid_value("wsize");
1051 } else
1052 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001053 break;
1054 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001055 if (match_int(args, &option) || option < 0) {
1056 errors++;
1057 nfs_parse_invalid_value("bsize");
1058 } else
1059 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001060 break;
1061 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001062 if (match_int(args, &option) || option <= 0) {
1063 errors++;
1064 nfs_parse_invalid_value("timeo");
1065 } else
1066 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001067 break;
1068 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001069 if (match_int(args, &option) || option <= 0) {
1070 errors++;
1071 nfs_parse_invalid_value("retrans");
1072 } else
1073 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001074 break;
1075 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001076 if (match_int(args, &option) || option < 0) {
1077 errors++;
1078 nfs_parse_invalid_value("acregmin");
1079 } else
1080 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001081 break;
1082 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001083 if (match_int(args, &option) || option < 0) {
1084 errors++;
1085 nfs_parse_invalid_value("acregmax");
1086 } else
1087 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001088 break;
1089 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001090 if (match_int(args, &option) || option < 0) {
1091 errors++;
1092 nfs_parse_invalid_value("acdirmin");
1093 } else
1094 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001095 break;
1096 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001097 if (match_int(args, &option) || option < 0) {
1098 errors++;
1099 nfs_parse_invalid_value("acdirmax");
1100 } else
1101 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001102 break;
1103 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001104 if (match_int(args, &option) || option < 0) {
1105 errors++;
1106 nfs_parse_invalid_value("actimeo");
1107 } else
1108 mnt->acregmin = mnt->acregmax =
1109 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001110 break;
1111 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001112 if (match_int(args, &option) || option < 0) {
1113 errors++;
1114 nfs_parse_invalid_value("namlen");
1115 } else
1116 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001117 break;
1118 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001119 if (match_int(args, &option) ||
1120 option < 0 || option > USHORT_MAX) {
1121 errors++;
1122 nfs_parse_invalid_value("mountport");
1123 } else
1124 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001125 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001126 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001127 if (match_int(args, &option) ||
1128 option < NFS_MNT_VERSION ||
1129 option > NFS_MNT3_VERSION) {
1130 errors++;
1131 nfs_parse_invalid_value("mountvers");
1132 } else
1133 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001134 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001135 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001136 if (match_int(args, &option)) {
1137 errors++;
1138 nfs_parse_invalid_value("nfsvers");
1139 break;
1140 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001141 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001142 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001143 mnt->flags &= ~NFS_MOUNT_VER3;
1144 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001145 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001146 mnt->flags |= NFS_MOUNT_VER3;
1147 break;
1148 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001149 errors++;
1150 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001151 }
1152 break;
1153
Chuck Leverf45663c2008-06-24 19:28:02 -04001154 /*
1155 * options that take text values
1156 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001157 case Opt_sec:
1158 string = match_strdup(args);
1159 if (string == NULL)
1160 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001161 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001162 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001163 if (!rc) {
1164 errors++;
1165 dfprintk(MOUNT, "NFS: unrecognized "
1166 "security flavor\n");
1167 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001168 break;
1169 case Opt_proto:
1170 string = match_strdup(args);
1171 if (string == NULL)
1172 goto out_nomem;
1173 token = match_token(string,
1174 nfs_xprt_protocol_tokens, args);
1175 kfree(string);
1176
1177 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001178 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001179 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001180 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001181 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001182 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001183 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001184 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001185 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001186 case Opt_xprt_rdma:
1187 /* vector side protocols to TCP */
1188 mnt->flags |= NFS_MOUNT_TCP;
1189 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001190 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001192 errors++;
1193 dfprintk(MOUNT, "NFS: unrecognized "
1194 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001195 }
1196 break;
1197 case Opt_mountproto:
1198 string = match_strdup(args);
1199 if (string == NULL)
1200 goto out_nomem;
1201 token = match_token(string,
1202 nfs_xprt_protocol_tokens, args);
1203 kfree(string);
1204
1205 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001206 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001207 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001208 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001209 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001210 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001211 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001212 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001213 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001214 errors++;
1215 dfprintk(MOUNT, "NFS: unrecognized "
1216 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001217 }
1218 break;
1219 case Opt_addr:
1220 string = match_strdup(args);
1221 if (string == NULL)
1222 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001223 nfs_parse_ip_address(string, strlen(string),
1224 (struct sockaddr *)
1225 &mnt->nfs_server.address,
1226 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001227 kfree(string);
1228 break;
1229 case Opt_clientaddr:
1230 string = match_strdup(args);
1231 if (string == NULL)
1232 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001233 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001234 mnt->client_address = string;
1235 break;
Chuck Lever33832032007-12-10 14:59:13 -05001236 case Opt_mounthost:
1237 string = match_strdup(args);
1238 if (string == NULL)
1239 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001240 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001241 mnt->mount_server.hostname = string;
1242 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001243 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001244 string = match_strdup(args);
1245 if (string == NULL)
1246 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001247 nfs_parse_ip_address(string, strlen(string),
1248 (struct sockaddr *)
1249 &mnt->mount_server.address,
1250 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001251 kfree(string);
1252 break;
1253
Chuck Leverf45663c2008-06-24 19:28:02 -04001254 /*
1255 * Special options
1256 */
1257 case Opt_sloppy:
1258 sloppy = 1;
1259 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1260 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001261 case Opt_userspace:
1262 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001263 dfprintk(MOUNT, "NFS: ignoring mount option "
1264 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001265 break;
1266
1267 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001268 errors++;
1269 dfprintk(MOUNT, "NFS: unrecognized mount option "
1270 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001271 }
1272 }
1273
Chuck Leveraf904de2008-09-08 11:58:13 -04001274 if (errors > 0) {
1275 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1276 errors, (errors == 1 ? "" : "s"));
1277 if (!sloppy)
1278 return 0;
1279 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001280 return 1;
1281
1282out_nomem:
1283 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1284 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001285out_security_failure:
1286 free_secdata(secdata);
1287 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1288 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001289}
1290
1291/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001292 * Use the remote server's MOUNT service to request the NFS file handle
1293 * corresponding to the provided path.
1294 */
1295static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1296 struct nfs_fh *root_fh)
1297{
Chuck Lever4c568012007-12-10 14:59:28 -05001298 struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
Chuck Lever33832032007-12-10 14:59:13 -05001299 char *hostname;
Chuck Lever4c568012007-12-10 14:59:28 -05001300 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001301
1302 if (args->mount_server.version == 0) {
1303 if (args->flags & NFS_MOUNT_VER3)
1304 args->mount_server.version = NFS_MNT3_VERSION;
1305 else
1306 args->mount_server.version = NFS_MNT_VERSION;
1307 }
1308
Chuck Lever33832032007-12-10 14:59:13 -05001309 if (args->mount_server.hostname)
1310 hostname = args->mount_server.hostname;
1311 else
1312 hostname = args->nfs_server.hostname;
1313
Chuck Lever0076d7b2007-07-01 12:13:49 -04001314 /*
1315 * Construct the mount server's address.
1316 */
Chuck Lever4c568012007-12-10 14:59:28 -05001317 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1318 memcpy(sap, &args->nfs_server.address,
1319 args->nfs_server.addrlen);
1320 args->mount_server.addrlen = args->nfs_server.addrlen;
1321 }
1322
James Lentiniaad70002007-09-24 17:32:49 -04001323 /*
1324 * autobind will be used if mount_server.port == 0
1325 */
Chuck Lever4c568012007-12-10 14:59:28 -05001326 nfs_set_port(sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001327
1328 /*
1329 * Now ask the mount server to map our export path
1330 * to a file handle.
1331 */
Chuck Lever4c568012007-12-10 14:59:28 -05001332 status = nfs_mount(sap,
1333 args->mount_server.addrlen,
Chuck Lever33832032007-12-10 14:59:13 -05001334 hostname,
Chuck Lever0076d7b2007-07-01 12:13:49 -04001335 args->nfs_server.export_path,
1336 args->mount_server.version,
1337 args->mount_server.protocol,
1338 root_fh);
Chuck Leverefd83402007-09-11 18:00:58 -04001339 if (status == 0)
1340 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001341
Chuck Lever396cee972008-06-12 12:37:49 -04001342 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Lever33832032007-12-10 14:59:13 -05001343 hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001344 return status;
1345}
1346
Chuck Leverd1aa0822008-06-23 12:36:45 -04001347static int nfs_parse_simple_hostname(const char *dev_name,
1348 char **hostname, size_t maxnamlen,
1349 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001350{
1351 size_t len;
1352 char *colon, *comma;
1353
1354 colon = strchr(dev_name, ':');
1355 if (colon == NULL)
1356 goto out_bad_devname;
1357
1358 len = colon - dev_name;
1359 if (len > maxnamlen)
1360 goto out_hostname;
1361
1362 /* N.B. caller will free nfs_server.hostname in all cases */
1363 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1364 if (!*hostname)
1365 goto out_nomem;
1366
1367 /* kill possible hostname list: not supported */
1368 comma = strchr(*hostname, ',');
1369 if (comma != NULL) {
1370 if (comma == *hostname)
1371 goto out_bad_devname;
1372 *comma = '\0';
1373 }
1374
1375 colon++;
1376 len = strlen(colon);
1377 if (len > maxpathlen)
1378 goto out_path;
1379 *export_path = kstrndup(colon, len, GFP_KERNEL);
1380 if (!*export_path)
1381 goto out_nomem;
1382
1383 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1384 return 0;
1385
1386out_bad_devname:
1387 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1388 return -EINVAL;
1389
1390out_nomem:
1391 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1392 return -ENOMEM;
1393
1394out_hostname:
1395 dfprintk(MOUNT, "NFS: server hostname too long\n");
1396 return -ENAMETOOLONG;
1397
1398out_path:
1399 dfprintk(MOUNT, "NFS: export pathname too long\n");
1400 return -ENAMETOOLONG;
1401}
1402
1403/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001404 * Hostname has square brackets around it because it contains one or
1405 * more colons. We look for the first closing square bracket, and a
1406 * colon must follow it.
1407 */
1408static int nfs_parse_protected_hostname(const char *dev_name,
1409 char **hostname, size_t maxnamlen,
1410 char **export_path, size_t maxpathlen)
1411{
1412 size_t len;
1413 char *start, *end;
1414
1415 start = (char *)(dev_name + 1);
1416
1417 end = strchr(start, ']');
1418 if (end == NULL)
1419 goto out_bad_devname;
1420 if (*(end + 1) != ':')
1421 goto out_bad_devname;
1422
1423 len = end - start;
1424 if (len > maxnamlen)
1425 goto out_hostname;
1426
1427 /* N.B. caller will free nfs_server.hostname in all cases */
1428 *hostname = kstrndup(start, len, GFP_KERNEL);
1429 if (*hostname == NULL)
1430 goto out_nomem;
1431
1432 end += 2;
1433 len = strlen(end);
1434 if (len > maxpathlen)
1435 goto out_path;
1436 *export_path = kstrndup(end, len, GFP_KERNEL);
1437 if (!*export_path)
1438 goto out_nomem;
1439
1440 return 0;
1441
1442out_bad_devname:
1443 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1444 return -EINVAL;
1445
1446out_nomem:
1447 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1448 return -ENOMEM;
1449
1450out_hostname:
1451 dfprintk(MOUNT, "NFS: server hostname too long\n");
1452 return -ENAMETOOLONG;
1453
1454out_path:
1455 dfprintk(MOUNT, "NFS: export pathname too long\n");
1456 return -ENAMETOOLONG;
1457}
1458
1459/*
1460 * Split "dev_name" into "hostname:export_path".
1461 *
1462 * The leftmost colon demarks the split between the server's hostname
1463 * and the export path. If the hostname starts with a left square
1464 * bracket, then it may contain colons.
1465 *
1466 * Note: caller frees hostname and export path, even on error.
1467 */
1468static int nfs_parse_devname(const char *dev_name,
1469 char **hostname, size_t maxnamlen,
1470 char **export_path, size_t maxpathlen)
1471{
1472 if (*dev_name == '[')
1473 return nfs_parse_protected_hostname(dev_name,
1474 hostname, maxnamlen,
1475 export_path, maxpathlen);
1476
1477 return nfs_parse_simple_hostname(dev_name,
1478 hostname, maxnamlen,
1479 export_path, maxpathlen);
1480}
1481
1482/*
David Howells54ceac42006-08-22 20:06:13 -04001483 * Validate the NFS2/NFS3 mount data
1484 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001485 *
1486 * For option strings, user space handles the following behaviors:
1487 *
1488 * + DNS: mapping server host name to IP address ("addr=" option)
1489 *
1490 * + failure mode: how to behave if a mount request can't be handled
1491 * immediately ("fg/bg" option)
1492 *
1493 * + retry: how often to retry a mount request ("retry=" option)
1494 *
1495 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1496 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001497 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001498static int nfs_validate_mount_data(void *options,
1499 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001500 struct nfs_fh *mntfh,
1501 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001502{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001503 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001504
Chuck Lever5df36e72007-07-01 12:12:56 -04001505 if (data == NULL)
1506 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001507
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001508 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1509 args->rsize = NFS_MAX_FILE_IO_SIZE;
1510 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001511 args->acregmin = NFS_DEF_ACREGMIN;
1512 args->acregmax = NFS_DEF_ACREGMAX;
1513 args->acdirmin = NFS_DEF_ACDIRMIN;
1514 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001515 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001516 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001517 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001518 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001519
David Howellsf7b422b2006-06-09 09:34:33 -04001520 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001521 case 1:
1522 data->namlen = 0;
1523 case 2:
1524 data->bsize = 0;
1525 case 3:
1526 if (data->flags & NFS_MOUNT_VER3)
1527 goto out_no_v3;
1528 data->root.size = NFS2_FHSIZE;
1529 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1530 case 4:
1531 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1532 goto out_no_sec;
1533 case 5:
1534 memset(data->context, 0, sizeof(data->context));
1535 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001536 if (data->flags & NFS_MOUNT_VER3) {
1537 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1538 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001539 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001540 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001541 mntfh->size = NFS2_FHSIZE;
1542
Chuck Lever5df36e72007-07-01 12:12:56 -04001543
1544 memcpy(mntfh->data, data->root.data, mntfh->size);
1545 if (mntfh->size < sizeof(mntfh->data))
1546 memset(mntfh->data + mntfh->size, 0,
1547 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001548
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001549 /*
1550 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1551 * can deal with.
1552 */
1553 args->flags = data->flags;
1554 args->rsize = data->rsize;
1555 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001556 args->timeo = data->timeo;
1557 args->retrans = data->retrans;
1558 args->acregmin = data->acregmin;
1559 args->acregmax = data->acregmax;
1560 args->acdirmin = data->acdirmin;
1561 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001562
1563 memcpy(&args->nfs_server.address, &data->addr,
1564 sizeof(data->addr));
1565 args->nfs_server.addrlen = sizeof(data->addr);
1566 if (!nfs_verify_server_address((struct sockaddr *)
1567 &args->nfs_server.address))
1568 goto out_no_address;
1569
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001570 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001571 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001572 /* N.B. caller will free nfs_server.hostname in all cases */
1573 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1574 args->namlen = data->namlen;
1575 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001576
1577 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1578 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001579 if (!args->nfs_server.hostname)
1580 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001581
1582 /*
1583 * The legacy version 6 binary mount data from userspace has a
1584 * field used only to transport selinux information into the
1585 * the kernel. To continue to support that functionality we
1586 * have a touch of selinux knowledge here in the NFS code. The
1587 * userspace code converted context=blah to just blah so we are
1588 * converting back to the full string selinux understands.
1589 */
1590 if (data->context[0]){
1591#ifdef CONFIG_SECURITY_SELINUX
1592 int rc;
1593 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1594 if (!opts_str)
1595 return -ENOMEM;
1596 strcpy(opts_str, "context=");
1597 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1598 strcat(opts_str, &data->context[0]);
1599 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1600 kfree(opts_str);
1601 if (rc)
1602 return rc;
1603#else
1604 return -EINVAL;
1605#endif
1606 }
1607
Chuck Lever5df36e72007-07-01 12:12:56 -04001608 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001609 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001610 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001611
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001612 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001613 return -EINVAL;
1614
Chuck Lever6e88e062007-09-24 15:39:50 -04001615 if (!nfs_verify_server_address((struct sockaddr *)
1616 &args->nfs_server.address))
1617 goto out_no_address;
1618
Chuck Levered596a82008-06-26 17:47:05 -04001619 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1620 args->nfs_server.port);
1621
Trond Myklebust259875e2008-07-02 14:43:47 -04001622 nfs_set_mount_transport_protocol(args);
1623
Chuck Leverdc045892008-06-23 12:36:37 -04001624 status = nfs_parse_devname(dev_name,
1625 &args->nfs_server.hostname,
1626 PAGE_SIZE,
1627 &args->nfs_server.export_path,
1628 NFS_MAXPATHLEN);
1629 if (!status)
1630 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001631
Chuck Leverdc045892008-06-23 12:36:37 -04001632 kfree(args->nfs_server.export_path);
1633 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001634
Chuck Lever136d5582007-07-01 12:13:54 -04001635 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001636 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001637
Chuck Lever136d5582007-07-01 12:13:54 -04001638 break;
1639 }
David Howellsf7b422b2006-06-09 09:34:33 -04001640 }
David Howells54ceac42006-08-22 20:06:13 -04001641
David Howellsf7b422b2006-06-09 09:34:33 -04001642#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001643 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001644 goto out_v3_not_compiled;
1645#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001646
David Howells54ceac42006-08-22 20:06:13 -04001647 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001648
1649out_no_data:
1650 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1651 return -EINVAL;
1652
1653out_no_v3:
1654 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1655 data->version);
1656 return -EINVAL;
1657
1658out_no_sec:
1659 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1660 return -EINVAL;
1661
Chuck Lever5df36e72007-07-01 12:12:56 -04001662#ifndef CONFIG_NFS_V3
1663out_v3_not_compiled:
1664 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1665 return -EPROTONOSUPPORT;
1666#endif /* !CONFIG_NFS_V3 */
1667
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001668out_nomem:
1669 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1670 return -ENOMEM;
1671
Chuck Lever5df36e72007-07-01 12:12:56 -04001672out_no_address:
1673 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1674 return -EINVAL;
1675
1676out_invalid_fh:
1677 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1678 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001679}
1680
Jeff Layton48b605f2008-06-10 15:38:39 -04001681static int
1682nfs_compare_remount_data(struct nfs_server *nfss,
1683 struct nfs_parsed_mount_data *data)
1684{
1685 if (data->flags != nfss->flags ||
1686 data->rsize != nfss->rsize ||
1687 data->wsize != nfss->wsize ||
1688 data->retrans != nfss->client->cl_timeout->to_retries ||
1689 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1690 data->acregmin != nfss->acregmin / HZ ||
1691 data->acregmax != nfss->acregmax / HZ ||
1692 data->acdirmin != nfss->acdirmin / HZ ||
1693 data->acdirmax != nfss->acdirmax / HZ ||
1694 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1695 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1696 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1697 data->nfs_server.addrlen) != 0)
1698 return -EINVAL;
1699
1700 return 0;
1701}
1702
1703static int
1704nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1705{
1706 int error;
1707 struct nfs_server *nfss = sb->s_fs_info;
1708 struct nfs_parsed_mount_data *data;
1709 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1710 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001711 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001712
1713 /*
1714 * Userspace mount programs that send binary options generally send
1715 * them populated with default values. We have no way to know which
1716 * ones were explicitly specified. Fall back to legacy behavior and
1717 * just return success.
1718 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001719 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1720 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1721 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001722 return 0;
1723
1724 data = kzalloc(sizeof(*data), GFP_KERNEL);
1725 if (data == NULL)
1726 return -ENOMEM;
1727
1728 /* fill out struct with values from existing mount */
1729 data->flags = nfss->flags;
1730 data->rsize = nfss->rsize;
1731 data->wsize = nfss->wsize;
1732 data->retrans = nfss->client->cl_timeout->to_retries;
1733 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1734 data->acregmin = nfss->acregmin / HZ;
1735 data->acregmax = nfss->acregmax / HZ;
1736 data->acdirmin = nfss->acdirmin / HZ;
1737 data->acdirmax = nfss->acdirmax / HZ;
1738 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1739 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1740 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1741 data->nfs_server.addrlen);
1742
1743 /* overwrite those values with any that were specified */
1744 error = nfs_parse_mount_options((char *)options, data);
1745 if (error < 0)
1746 goto out;
1747
1748 /* compare new mount options with old ones */
1749 error = nfs_compare_remount_data(nfss, data);
1750out:
1751 kfree(data);
1752 return error;
1753}
1754
David Howells54ceac42006-08-22 20:06:13 -04001755/*
1756 * Initialise the common bits of the superblock
1757 */
1758static inline void nfs_initialise_sb(struct super_block *sb)
1759{
1760 struct nfs_server *server = NFS_SB(sb);
1761
1762 sb->s_magic = NFS_SUPER_MAGIC;
1763
1764 /* We probably want something more informative here */
1765 snprintf(sb->s_id, sizeof(sb->s_id),
1766 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1767
1768 if (sb->s_blocksize == 0)
1769 sb->s_blocksize = nfs_block_bits(server->wsize,
1770 &sb->s_blocksize_bits);
1771
1772 if (server->flags & NFS_MOUNT_NOAC)
1773 sb->s_flags |= MS_SYNCHRONOUS;
1774
1775 nfs_super_set_maxbytes(sb, server->maxfilesize);
1776}
1777
1778/*
1779 * Finish setting up an NFS2/3 superblock
1780 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001781static void nfs_fill_super(struct super_block *sb,
1782 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001783{
1784 struct nfs_server *server = NFS_SB(sb);
1785
1786 sb->s_blocksize_bits = 0;
1787 sb->s_blocksize = 0;
1788 if (data->bsize)
1789 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1790
1791 if (server->flags & NFS_MOUNT_VER3) {
1792 /* The VFS shouldn't apply the umask to mode bits. We will do
1793 * so ourselves when necessary.
1794 */
1795 sb->s_flags |= MS_POSIXACL;
1796 sb->s_time_gran = 1;
1797 }
1798
1799 sb->s_op = &nfs_sops;
1800 nfs_initialise_sb(sb);
1801}
1802
1803/*
1804 * Finish setting up a cloned NFS2/3 superblock
1805 */
1806static void nfs_clone_super(struct super_block *sb,
1807 const struct super_block *old_sb)
1808{
1809 struct nfs_server *server = NFS_SB(sb);
1810
1811 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1812 sb->s_blocksize = old_sb->s_blocksize;
1813 sb->s_maxbytes = old_sb->s_maxbytes;
1814
1815 if (server->flags & NFS_MOUNT_VER3) {
1816 /* The VFS shouldn't apply the umask to mode bits. We will do
1817 * so ourselves when necessary.
1818 */
1819 sb->s_flags |= MS_POSIXACL;
1820 sb->s_time_gran = 1;
1821 }
1822
1823 sb->s_op = old_sb->s_op;
1824 nfs_initialise_sb(sb);
1825}
1826
Trond Myklebust275a5d22007-05-16 16:53:28 -04001827#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1828
1829static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1830{
1831 const struct nfs_server *a = s->s_fs_info;
1832 const struct rpc_clnt *clnt_a = a->client;
1833 const struct rpc_clnt *clnt_b = b->client;
1834
1835 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1836 goto Ebusy;
1837 if (a->nfs_client != b->nfs_client)
1838 goto Ebusy;
1839 if (a->flags != b->flags)
1840 goto Ebusy;
1841 if (a->wsize != b->wsize)
1842 goto Ebusy;
1843 if (a->rsize != b->rsize)
1844 goto Ebusy;
1845 if (a->acregmin != b->acregmin)
1846 goto Ebusy;
1847 if (a->acregmax != b->acregmax)
1848 goto Ebusy;
1849 if (a->acdirmin != b->acdirmin)
1850 goto Ebusy;
1851 if (a->acdirmax != b->acdirmax)
1852 goto Ebusy;
1853 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1854 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001855 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001856Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001857 return 0;
1858}
1859
1860struct nfs_sb_mountdata {
1861 struct nfs_server *server;
1862 int mntflags;
1863};
1864
1865static int nfs_set_super(struct super_block *s, void *data)
1866{
1867 struct nfs_sb_mountdata *sb_mntdata = data;
1868 struct nfs_server *server = sb_mntdata->server;
1869 int ret;
1870
1871 s->s_flags = sb_mntdata->mntflags;
1872 s->s_fs_info = server;
1873 ret = set_anon_super(s, server);
1874 if (ret == 0)
1875 server->s_dev = s->s_dev;
1876 return ret;
1877}
1878
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001879static int nfs_compare_super_address(struct nfs_server *server1,
1880 struct nfs_server *server2)
1881{
1882 struct sockaddr *sap1, *sap2;
1883
1884 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1885 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1886
1887 if (sap1->sa_family != sap2->sa_family)
1888 return 0;
1889
1890 switch (sap1->sa_family) {
1891 case AF_INET: {
1892 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1893 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1894 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1895 return 0;
1896 if (sin1->sin_port != sin2->sin_port)
1897 return 0;
1898 break;
1899 }
1900 case AF_INET6: {
1901 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1902 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1903 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1904 return 0;
1905 if (sin1->sin6_port != sin2->sin6_port)
1906 return 0;
1907 break;
1908 }
1909 default:
1910 return 0;
1911 }
1912
1913 return 1;
1914}
1915
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001916static int nfs_compare_super(struct super_block *sb, void *data)
1917{
1918 struct nfs_sb_mountdata *sb_mntdata = data;
1919 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1920 int mntflags = sb_mntdata->mntflags;
1921
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001922 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001923 return 0;
1924 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1925 if (old->flags & NFS_MOUNT_UNSHARED)
1926 return 0;
1927 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1928 return 0;
1929 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001930}
1931
Miklos Szeredifa799752008-04-30 00:54:33 -07001932static int nfs_bdi_register(struct nfs_server *server)
1933{
1934 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1935}
1936
David Howells54ceac42006-08-22 20:06:13 -04001937static int nfs_get_sb(struct file_system_type *fs_type,
1938 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1939{
1940 struct nfs_server *server = NULL;
1941 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001942 struct nfs_parsed_mount_data *data;
1943 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001944 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001945 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001946 struct nfs_sb_mountdata sb_mntdata = {
1947 .mntflags = flags,
1948 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001949 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001950
Trond Myklebust33852a12008-06-19 14:20:11 -04001951 data = kzalloc(sizeof(*data), GFP_KERNEL);
1952 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1953 if (data == NULL || mntfh == NULL)
1954 goto out_free_fh;
1955
1956 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001957
David Howells54ceac42006-08-22 20:06:13 -04001958 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04001959 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04001960 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001961 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001962
1963 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001964 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001965 if (IS_ERR(server)) {
1966 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04001967 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001968 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001969 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04001970
Trond Myklebust75180df2007-05-16 16:53:28 -04001971 if (server->flags & NFS_MOUNT_UNSHARED)
1972 compare_super = NULL;
1973
David Howells54ceac42006-08-22 20:06:13 -04001974 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001975 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04001976 if (IS_ERR(s)) {
1977 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04001978 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04001979 }
1980
David Howells54ceac42006-08-22 20:06:13 -04001981 if (s->s_fs_info != server) {
1982 nfs_free_server(server);
1983 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07001984 } else {
1985 error = nfs_bdi_register(server);
1986 if (error)
1987 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04001988 }
David Howells54ceac42006-08-22 20:06:13 -04001989
1990 if (!s->s_root) {
1991 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001992 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04001993 }
1994
Trond Myklebust33852a12008-06-19 14:20:11 -04001995 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001996 if (IS_ERR(mntroot)) {
1997 error = PTR_ERR(mntroot);
1998 goto error_splat_super;
1999 }
2000
Trond Myklebust33852a12008-06-19 14:20:11 -04002001 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002002 if (error)
2003 goto error_splat_root;
2004
David Howellsf7b422b2006-06-09 09:34:33 -04002005 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002006 mnt->mnt_sb = s;
2007 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002008 error = 0;
2009
2010out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002011 kfree(data->nfs_server.hostname);
2012 kfree(data->mount_server.hostname);
2013 security_free_mnt_opts(&data->lsm_opts);
2014out_free_fh:
2015 kfree(mntfh);
2016 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002017 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002018
David Howells54ceac42006-08-22 20:06:13 -04002019out_err_nosb:
2020 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002021 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002022
Eric Parisf9c3a382008-03-05 14:20:18 -05002023error_splat_root:
2024 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002025error_splat_super:
2026 up_write(&s->s_umount);
2027 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002028 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002029}
2030
David Howells54ceac42006-08-22 20:06:13 -04002031/*
2032 * Destroy an NFS2/3 superblock
2033 */
David Howellsf7b422b2006-06-09 09:34:33 -04002034static void nfs_kill_super(struct super_block *s)
2035{
2036 struct nfs_server *server = NFS_SB(s);
2037
Miklos Szeredifa799752008-04-30 00:54:33 -07002038 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002039 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002040 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002041}
2042
David Howells54ceac42006-08-22 20:06:13 -04002043/*
2044 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2045 */
2046static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2047 const char *dev_name, void *raw_data,
2048 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002049{
2050 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002051 struct super_block *s;
2052 struct nfs_server *server;
2053 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002054 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002055 struct nfs_sb_mountdata sb_mntdata = {
2056 .mntflags = flags,
2057 };
David Howells54ceac42006-08-22 20:06:13 -04002058 int error;
2059
2060 dprintk("--> nfs_xdev_get_sb()\n");
2061
2062 /* create a new volume representation */
2063 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2064 if (IS_ERR(server)) {
2065 error = PTR_ERR(server);
2066 goto out_err_noserver;
2067 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002068 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002069
Trond Myklebust75180df2007-05-16 16:53:28 -04002070 if (server->flags & NFS_MOUNT_UNSHARED)
2071 compare_super = NULL;
2072
David Howells54ceac42006-08-22 20:06:13 -04002073 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002074 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002075 if (IS_ERR(s)) {
2076 error = PTR_ERR(s);
2077 goto out_err_nosb;
2078 }
2079
2080 if (s->s_fs_info != server) {
2081 nfs_free_server(server);
2082 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002083 } else {
2084 error = nfs_bdi_register(server);
2085 if (error)
2086 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002087 }
2088
2089 if (!s->s_root) {
2090 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002091 nfs_clone_super(s, data->sb);
2092 }
2093
2094 mntroot = nfs_get_root(s, data->fh);
2095 if (IS_ERR(mntroot)) {
2096 error = PTR_ERR(mntroot);
2097 goto error_splat_super;
2098 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002099 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002100 dput(mntroot);
2101 error = -ESTALE;
2102 goto error_splat_super;
2103 }
David Howells54ceac42006-08-22 20:06:13 -04002104
2105 s->s_flags |= MS_ACTIVE;
2106 mnt->mnt_sb = s;
2107 mnt->mnt_root = mntroot;
2108
Eric Parisf9c3a382008-03-05 14:20:18 -05002109 /* clone any lsm security options from the parent to the new sb */
2110 security_sb_clone_mnt_opts(data->sb, s);
2111
David Howells54ceac42006-08-22 20:06:13 -04002112 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2113 return 0;
2114
2115out_err_nosb:
2116 nfs_free_server(server);
2117out_err_noserver:
2118 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2119 return error;
2120
2121error_splat_super:
2122 up_write(&s->s_umount);
2123 deactivate_super(s);
2124 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2125 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002126}
2127
2128#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002129
2130/*
2131 * Finish setting up a cloned NFS4 superblock
2132 */
2133static void nfs4_clone_super(struct super_block *sb,
2134 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002135{
David Howells54ceac42006-08-22 20:06:13 -04002136 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2137 sb->s_blocksize = old_sb->s_blocksize;
2138 sb->s_maxbytes = old_sb->s_maxbytes;
2139 sb->s_time_gran = 1;
2140 sb->s_op = old_sb->s_op;
2141 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002142}
2143
2144/*
2145 * Set up an NFS4 superblock
2146 */
David Howells54ceac42006-08-22 20:06:13 -04002147static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002148{
David Howellsf7b422b2006-06-09 09:34:33 -04002149 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002150 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002151 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002152}
2153
David Howells54ceac42006-08-22 20:06:13 -04002154/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002155 * Validate NFSv4 mount options
2156 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002157static int nfs4_validate_mount_data(void *options,
2158 struct nfs_parsed_mount_data *args,
2159 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002160{
Chuck Lever4c568012007-12-10 14:59:28 -05002161 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002162 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002163 char *c;
2164
2165 if (data == NULL)
2166 goto out_no_data;
2167
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002168 args->rsize = NFS_MAX_FILE_IO_SIZE;
2169 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002170 args->acregmin = NFS_DEF_ACREGMIN;
2171 args->acregmax = NFS_DEF_ACREGMAX;
2172 args->acdirmin = NFS_DEF_ACDIRMIN;
2173 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002174 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002175 args->auth_flavors[0] = RPC_AUTH_UNIX;
2176 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002177
Chuck Leverf0768eb2007-07-01 12:13:01 -04002178 switch (data->version) {
2179 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002180 ap = (struct sockaddr_in *)&args->nfs_server.address;
2181 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002182 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002183 if (data->host_addrlen == 0)
2184 goto out_no_address;
2185 args->nfs_server.addrlen = data->host_addrlen;
2186 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002187 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002188 if (!nfs_verify_server_address((struct sockaddr *)
2189 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002190 goto out_no_address;
2191
Chuck Lever6738b252008-06-24 16:33:54 -04002192 if (data->auth_flavourlen) {
2193 if (data->auth_flavourlen > 1)
2194 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002195 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002196 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002197 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002198 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002199 }
2200
2201 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2202 if (IS_ERR(c))
2203 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002204 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002205
2206 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2207 if (IS_ERR(c))
2208 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002209 args->nfs_server.export_path = c;
2210 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002211
2212 c = strndup_user(data->client_addr.data, 16);
2213 if (IS_ERR(c))
2214 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002215 args->client_address = c;
2216
2217 /*
2218 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2219 * can deal with.
2220 */
2221
2222 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2223 args->rsize = data->rsize;
2224 args->wsize = data->wsize;
2225 args->timeo = data->timeo;
2226 args->retrans = data->retrans;
2227 args->acregmin = data->acregmin;
2228 args->acregmax = data->acregmax;
2229 args->acdirmin = data->acdirmin;
2230 args->acdirmax = data->acdirmax;
2231 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002232 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002233
2234 break;
Chuck Lever80071222007-07-01 12:13:59 -04002235 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002236 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002237
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002238 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002239 return -EINVAL;
2240
2241 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002242 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002243 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002244
Chuck Levered596a82008-06-26 17:47:05 -04002245 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2246 args->nfs_server.port);
2247
Trond Myklebust259875e2008-07-02 14:43:47 -04002248 nfs_validate_transport_protocol(args);
2249
Chuck Lever6738b252008-06-24 16:33:54 -04002250 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002251 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002252
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002253 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002254 goto out_no_client_address;
2255
Chuck Leverdc045892008-06-23 12:36:37 -04002256 status = nfs_parse_devname(dev_name,
2257 &args->nfs_server.hostname,
2258 NFS4_MAXNAMLEN,
2259 &args->nfs_server.export_path,
2260 NFS4_MAXPATHLEN);
2261 if (status < 0)
2262 return status;
2263
Chuck Lever80071222007-07-01 12:13:59 -04002264 break;
2265 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002266 }
2267
2268 return 0;
2269
2270out_no_data:
2271 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2272 return -EINVAL;
2273
2274out_inval_auth:
2275 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2276 data->auth_flavourlen);
2277 return -EINVAL;
2278
2279out_no_address:
2280 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2281 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002282
2283out_no_client_address:
2284 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2285 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002286}
2287
2288/*
David Howells54ceac42006-08-22 20:06:13 -04002289 * Get the superblock for an NFS4 mountpoint
2290 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002291static int nfs4_get_sb(struct file_system_type *fs_type,
2292 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002293{
Trond Myklebust33852a12008-06-19 14:20:11 -04002294 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002295 struct super_block *s;
2296 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002297 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002298 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002299 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002300 struct nfs_sb_mountdata sb_mntdata = {
2301 .mntflags = flags,
2302 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002303 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002304
Trond Myklebust33852a12008-06-19 14:20:11 -04002305 data = kzalloc(sizeof(*data), GFP_KERNEL);
2306 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2307 if (data == NULL || mntfh == NULL)
2308 goto out_free_fh;
2309
2310 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002311
Chuck Leverf0768eb2007-07-01 12:13:01 -04002312 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002313 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002314 if (error < 0)
2315 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002316
David Howells54ceac42006-08-22 20:06:13 -04002317 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002318 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002319 if (IS_ERR(server)) {
2320 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002321 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002322 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002323 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002324
Trond Myklebust75180df2007-05-16 16:53:28 -04002325 if (server->flags & NFS4_MOUNT_UNSHARED)
2326 compare_super = NULL;
2327
David Howells54ceac42006-08-22 20:06:13 -04002328 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002329 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002330 if (IS_ERR(s)) {
2331 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002332 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002333 }
2334
Trond Myklebust5dd31772006-08-24 01:03:05 -04002335 if (s->s_fs_info != server) {
2336 nfs_free_server(server);
2337 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002338 } else {
2339 error = nfs_bdi_register(server);
2340 if (error)
2341 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002342 }
2343
David Howells54ceac42006-08-22 20:06:13 -04002344 if (!s->s_root) {
2345 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002346 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002347 }
David Howellsf7b422b2006-06-09 09:34:33 -04002348
Trond Myklebust33852a12008-06-19 14:20:11 -04002349 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002350 if (IS_ERR(mntroot)) {
2351 error = PTR_ERR(mntroot);
2352 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002353 }
David Howells54ceac42006-08-22 20:06:13 -04002354
Trond Myklebust33852a12008-06-19 14:20:11 -04002355 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002356 if (error)
2357 goto error_splat_root;
2358
David Howellsf7b422b2006-06-09 09:34:33 -04002359 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002360 mnt->mnt_sb = s;
2361 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002362 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002363
Chuck Lever29eb9812007-07-01 12:12:30 -04002364out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002365 kfree(data->client_address);
2366 kfree(data->nfs_server.export_path);
2367 kfree(data->nfs_server.hostname);
2368 security_free_mnt_opts(&data->lsm_opts);
2369out_free_fh:
2370 kfree(mntfh);
2371 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002372 return error;
David Howells54ceac42006-08-22 20:06:13 -04002373
Chuck Lever29eb9812007-07-01 12:12:30 -04002374out_free:
2375 nfs_free_server(server);
2376 goto out;
2377
Eric Paris46c8ac72008-05-02 13:42:42 -07002378error_splat_root:
2379 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002380error_splat_super:
2381 up_write(&s->s_umount);
2382 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002383 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002384}
2385
2386static void nfs4_kill_super(struct super_block *sb)
2387{
2388 struct nfs_server *server = NFS_SB(sb);
2389
2390 nfs_return_all_delegations(sb);
2391 kill_anon_super(sb);
2392
2393 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002394 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002395}
2396
2397/*
David Howells54ceac42006-08-22 20:06:13 -04002398 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002399 */
David Howells54ceac42006-08-22 20:06:13 -04002400static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2401 const char *dev_name, void *raw_data,
2402 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002403{
2404 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002405 struct super_block *s;
2406 struct nfs_server *server;
2407 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002408 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002409 struct nfs_sb_mountdata sb_mntdata = {
2410 .mntflags = flags,
2411 };
David Howells54ceac42006-08-22 20:06:13 -04002412 int error;
2413
2414 dprintk("--> nfs4_xdev_get_sb()\n");
2415
2416 /* create a new volume representation */
2417 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2418 if (IS_ERR(server)) {
2419 error = PTR_ERR(server);
2420 goto out_err_noserver;
2421 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002422 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002423
Trond Myklebust75180df2007-05-16 16:53:28 -04002424 if (server->flags & NFS4_MOUNT_UNSHARED)
2425 compare_super = NULL;
2426
David Howells54ceac42006-08-22 20:06:13 -04002427 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002428 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002429 if (IS_ERR(s)) {
2430 error = PTR_ERR(s);
2431 goto out_err_nosb;
2432 }
2433
2434 if (s->s_fs_info != server) {
2435 nfs_free_server(server);
2436 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002437 } else {
2438 error = nfs_bdi_register(server);
2439 if (error)
2440 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002441 }
2442
2443 if (!s->s_root) {
2444 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002445 nfs4_clone_super(s, data->sb);
2446 }
2447
2448 mntroot = nfs4_get_root(s, data->fh);
2449 if (IS_ERR(mntroot)) {
2450 error = PTR_ERR(mntroot);
2451 goto error_splat_super;
2452 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002453 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2454 dput(mntroot);
2455 error = -ESTALE;
2456 goto error_splat_super;
2457 }
David Howells54ceac42006-08-22 20:06:13 -04002458
2459 s->s_flags |= MS_ACTIVE;
2460 mnt->mnt_sb = s;
2461 mnt->mnt_root = mntroot;
2462
Eric Paris46c8ac72008-05-02 13:42:42 -07002463 security_sb_clone_mnt_opts(data->sb, s);
2464
David Howells54ceac42006-08-22 20:06:13 -04002465 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2466 return 0;
2467
2468out_err_nosb:
2469 nfs_free_server(server);
2470out_err_noserver:
2471 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2472 return error;
2473
2474error_splat_super:
2475 up_write(&s->s_umount);
2476 deactivate_super(s);
2477 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2478 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002479}
2480
David Howells54ceac42006-08-22 20:06:13 -04002481/*
2482 * Create an NFS4 server record on referral traversal
2483 */
2484static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2485 const char *dev_name, void *raw_data,
2486 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002487{
2488 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002489 struct super_block *s;
2490 struct nfs_server *server;
2491 struct dentry *mntroot;
2492 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002493 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002494 struct nfs_sb_mountdata sb_mntdata = {
2495 .mntflags = flags,
2496 };
David Howells54ceac42006-08-22 20:06:13 -04002497 int error;
2498
2499 dprintk("--> nfs4_referral_get_sb()\n");
2500
2501 /* create a new volume representation */
2502 server = nfs4_create_referral_server(data, &mntfh);
2503 if (IS_ERR(server)) {
2504 error = PTR_ERR(server);
2505 goto out_err_noserver;
2506 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002507 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002508
Trond Myklebust75180df2007-05-16 16:53:28 -04002509 if (server->flags & NFS4_MOUNT_UNSHARED)
2510 compare_super = NULL;
2511
David Howells54ceac42006-08-22 20:06:13 -04002512 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002513 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002514 if (IS_ERR(s)) {
2515 error = PTR_ERR(s);
2516 goto out_err_nosb;
2517 }
2518
2519 if (s->s_fs_info != server) {
2520 nfs_free_server(server);
2521 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002522 } else {
2523 error = nfs_bdi_register(server);
2524 if (error)
2525 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002526 }
2527
2528 if (!s->s_root) {
2529 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002530 nfs4_fill_super(s);
2531 }
2532
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002533 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002534 if (IS_ERR(mntroot)) {
2535 error = PTR_ERR(mntroot);
2536 goto error_splat_super;
2537 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002538 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2539 dput(mntroot);
2540 error = -ESTALE;
2541 goto error_splat_super;
2542 }
David Howells54ceac42006-08-22 20:06:13 -04002543
2544 s->s_flags |= MS_ACTIVE;
2545 mnt->mnt_sb = s;
2546 mnt->mnt_root = mntroot;
2547
Eric Paris46c8ac72008-05-02 13:42:42 -07002548 security_sb_clone_mnt_opts(data->sb, s);
2549
David Howells54ceac42006-08-22 20:06:13 -04002550 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2551 return 0;
2552
2553out_err_nosb:
2554 nfs_free_server(server);
2555out_err_noserver:
2556 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2557 return error;
2558
2559error_splat_super:
2560 up_write(&s->s_umount);
2561 deactivate_super(s);
2562 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2563 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002564}
2565
David Howells54ceac42006-08-22 20:06:13 -04002566#endif /* CONFIG_NFS_V4 */