blob: 9abcd2b329f7357b0af65065613e996b643e1747 [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 *);
Steve Dicksonef818a22007-11-08 04:05:04 -0500212static void nfs_put_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400213static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400214
215static struct file_system_type nfs_fs_type = {
216 .owner = THIS_MODULE,
217 .name = "nfs",
218 .get_sb = nfs_get_sb,
219 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700220 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400221};
222
David Howells54ceac42006-08-22 20:06:13 -0400223struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400224 .owner = THIS_MODULE,
225 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400226 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400227 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700228 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400229};
230
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800231static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400232 .alloc_inode = nfs_alloc_inode,
233 .destroy_inode = nfs_destroy_inode,
234 .write_inode = nfs_write_inode,
Steve Dicksonef818a22007-11-08 04:05:04 -0500235 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400236 .statfs = nfs_statfs,
237 .clear_inode = nfs_clear_inode,
238 .umount_begin = nfs_umount_begin,
239 .show_options = nfs_show_options,
240 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400241 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400242};
243
244#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400245static int nfs4_get_sb(struct file_system_type *fs_type,
246 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400247static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
248 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
249static int nfs4_referral_get_sb(struct file_system_type *fs_type,
250 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400251static void nfs4_kill_super(struct super_block *sb);
252
253static struct file_system_type nfs4_fs_type = {
254 .owner = THIS_MODULE,
255 .name = "nfs4",
256 .get_sb = nfs4_get_sb,
257 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700258 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400259};
260
David Howells54ceac42006-08-22 20:06:13 -0400261struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400262 .owner = THIS_MODULE,
263 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400264 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400265 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700266 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400267};
268
David Howells54ceac42006-08-22 20:06:13 -0400269struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400270 .owner = THIS_MODULE,
271 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400272 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400273 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700274 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400275};
276
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800277static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400278 .alloc_inode = nfs_alloc_inode,
279 .destroy_inode = nfs_destroy_inode,
280 .write_inode = nfs_write_inode,
281 .statfs = nfs_statfs,
282 .clear_inode = nfs4_clear_inode,
283 .umount_begin = nfs_umount_begin,
284 .show_options = nfs_show_options,
285 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400286 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400287};
288#endif
289
Rusty Russell8e1f9362007-07-17 04:03:17 -0700290static struct shrinker acl_shrinker = {
291 .shrink = nfs_access_cache_shrinker,
292 .seeks = DEFAULT_SEEKS,
293};
Trond Myklebust979df722006-07-25 11:28:19 -0400294
David Howellsf7b422b2006-06-09 09:34:33 -0400295/*
296 * Register the NFS filesystems
297 */
298int __init register_nfs_fs(void)
299{
300 int ret;
301
302 ret = register_filesystem(&nfs_fs_type);
303 if (ret < 0)
304 goto error_0;
305
David Howellsf7b422b2006-06-09 09:34:33 -0400306 ret = nfs_register_sysctl();
307 if (ret < 0)
308 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800309#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400310 ret = register_filesystem(&nfs4_fs_type);
311 if (ret < 0)
312 goto error_2;
313#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700314 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400315 return 0;
316
317#ifdef CONFIG_NFS_V4
318error_2:
319 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800320#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400321error_1:
322 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400323error_0:
324 return ret;
325}
326
327/*
328 * Unregister the NFS filesystems
329 */
330void __exit unregister_nfs_fs(void)
331{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700332 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400333#ifdef CONFIG_NFS_V4
334 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400335#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700336 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400337 unregister_filesystem(&nfs_fs_type);
338}
339
Steve Dicksonef818a22007-11-08 04:05:04 -0500340void nfs_sb_active(struct nfs_server *server)
341{
342 atomic_inc(&server->active);
343}
344
345void nfs_sb_deactive(struct nfs_server *server)
346{
347 if (atomic_dec_and_test(&server->active))
348 wake_up(&server->active_wq);
349}
350
351static void nfs_put_super(struct super_block *sb)
352{
353 struct nfs_server *server = NFS_SB(sb);
354 /*
355 * Make sure there are no outstanding ops to this server.
356 * If so, wait for them to finish before allowing the
357 * unmount to continue.
358 */
359 wait_event(server->active_wq, atomic_read(&server->active) == 0);
360}
361
David Howellsf7b422b2006-06-09 09:34:33 -0400362/*
363 * Deliver file system statistics to userspace
364 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400365static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400366{
David Howells0c7d90c2006-08-22 20:06:10 -0400367 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400368 unsigned char blockbits;
369 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400370 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400371 struct nfs_fattr fattr;
372 struct nfs_fsstat res = {
373 .fattr = &fattr,
374 };
375 int error;
376
David Howells8fa5c002006-08-22 20:06:12 -0400377 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400378 if (error < 0)
379 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700380 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400381
382 /*
383 * Current versions of glibc do not correctly handle the
384 * case where f_frsize != f_bsize. Eventually we want to
385 * report the value of wtmult in this field.
386 */
David Howells0c7d90c2006-08-22 20:06:10 -0400387 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400388
389 /*
390 * On most *nix systems, f_blocks, f_bfree, and f_bavail
391 * are reported in units of f_frsize. Linux hasn't had
392 * an f_frsize field in its statfs struct until recently,
393 * thus historically Linux's sys_statfs reports these
394 * fields in units of f_bsize.
395 */
David Howells0c7d90c2006-08-22 20:06:10 -0400396 buf->f_bsize = dentry->d_sb->s_blocksize;
397 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400398 blockres = (1 << blockbits) - 1;
399 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
400 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
401 buf->f_bavail = (res.abytes + blockres) >> blockbits;
402
403 buf->f_files = res.tfiles;
404 buf->f_ffree = res.afiles;
405
406 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700407
David Howellsf7b422b2006-06-09 09:34:33 -0400408 return 0;
409
410 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700411 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700412 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400413}
414
David Howells7d4e2742006-08-22 20:06:07 -0400415/*
416 * Map the security flavour number to a name
417 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400418static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
419{
David Howells7d4e2742006-08-22 20:06:07 -0400420 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400421 rpc_authflavor_t flavour;
422 const char *str;
423 } sec_flavours[] = {
424 { RPC_AUTH_NULL, "null" },
425 { RPC_AUTH_UNIX, "sys" },
426 { RPC_AUTH_GSS_KRB5, "krb5" },
427 { RPC_AUTH_GSS_KRB5I, "krb5i" },
428 { RPC_AUTH_GSS_KRB5P, "krb5p" },
429 { RPC_AUTH_GSS_LKEY, "lkey" },
430 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
431 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
432 { RPC_AUTH_GSS_SPKM, "spkm" },
433 { RPC_AUTH_GSS_SPKMI, "spkmi" },
434 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400435 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400436 };
437 int i;
438
Chuck Lever4d81cd12007-07-01 12:12:40 -0400439 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400440 if (sec_flavours[i].flavour == flavour)
441 break;
442 }
443 return sec_flavours[i].str;
444}
445
Chuck Lever82d101d2008-03-14 14:10:37 -0400446static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
447 int showdefaults)
448{
449 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
450
451 switch (sap->sa_family) {
452 case AF_INET: {
453 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
454 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
455 NIPQUAD(sin->sin_addr.s_addr));
456 break;
457 }
458 case AF_INET6: {
459 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
460 seq_printf(m, ",mountaddr=" NIP6_FMT,
461 NIP6(sin6->sin6_addr));
462 break;
463 }
464 default:
465 if (showdefaults)
466 seq_printf(m, ",mountaddr=unspecified");
467 }
468
469 if (nfss->mountd_version || showdefaults)
470 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
471 if (nfss->mountd_port || showdefaults)
472 seq_printf(m, ",mountport=%u", nfss->mountd_port);
473
474 switch (nfss->mountd_protocol) {
475 case IPPROTO_UDP:
476 seq_printf(m, ",mountproto=udp");
477 break;
478 case IPPROTO_TCP:
479 seq_printf(m, ",mountproto=tcp");
480 break;
481 default:
482 if (showdefaults)
483 seq_printf(m, ",mountproto=auto");
484 }
485}
486
David Howellsf7b422b2006-06-09 09:34:33 -0400487/*
488 * Describe the mount options in force on this server representation
489 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400490static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
491 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400492{
David Howells509de812006-08-22 20:06:11 -0400493 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400494 int flag;
David Howells509de812006-08-22 20:06:11 -0400495 const char *str;
496 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400497 } nfs_info[] = {
498 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400499 { NFS_MOUNT_INTR, ",intr", ",nointr" },
500 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400501 { NFS_MOUNT_NOCTO, ",nocto", "" },
502 { NFS_MOUNT_NOAC, ",noac", "" },
503 { NFS_MOUNT_NONLM, ",nolock", "" },
504 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400505 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400506 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
David Howellsf7b422b2006-06-09 09:34:33 -0400507 { 0, NULL, NULL }
508 };
David Howells509de812006-08-22 20:06:11 -0400509 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400510 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400511 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400512
Chuck Lever82d101d2008-03-14 14:10:37 -0400513 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400514 seq_printf(m, ",rsize=%u", nfss->rsize);
515 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400516 if (nfss->bsize != 0)
517 seq_printf(m, ",bsize=%u", nfss->bsize);
518 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400519 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400520 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400521 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400522 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400523 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400524 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400525 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400526 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400527 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
528 if (nfss->flags & nfs_infop->flag)
529 seq_puts(m, nfs_infop->str);
530 else
531 seq_puts(m, nfs_infop->nostr);
532 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400533 seq_printf(m, ",proto=%s",
534 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400535 if (version == 4) {
536 if (nfss->port != NFS_PORT)
537 seq_printf(m, ",port=%u", nfss->port);
538 } else
539 if (nfss->port)
540 seq_printf(m, ",port=%u", nfss->port);
541
Trond Myklebust33170232007-12-20 16:03:59 -0500542 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
543 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400544 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400545
546 if (version != 4)
547 nfs_show_mountd_options(m, nfss, showdefaults);
548
549#ifdef CONFIG_NFS_V4
550 if (clp->rpc_ops->version == 4)
551 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
552#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400553}
554
555/*
556 * Describe the mount options on this VFS mountpoint
557 */
558static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
559{
560 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
561
562 nfs_show_mount_options(m, nfss, 0);
563
Chuck Lever5d8515c2007-12-10 14:57:16 -0500564 seq_printf(m, ",addr=%s",
565 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
566 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400567
568 return 0;
569}
570
571/*
572 * Present statistical information for this VFS mountpoint
573 */
574static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
575{
576 int i, cpu;
577 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
578 struct rpc_auth *auth = nfss->client->cl_auth;
579 struct nfs_iostats totals = { };
580
581 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
582
583 /*
584 * Display all mount option settings
585 */
586 seq_printf(m, "\n\topts:\t");
587 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
588 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
589 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
590 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
591 nfs_show_mount_options(m, nfss, 1);
592
593 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
594
595 seq_printf(m, "\n\tcaps:\t");
596 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400597 seq_printf(m, ",wtmult=%u", nfss->wtmult);
598 seq_printf(m, ",dtsize=%u", nfss->dtsize);
599 seq_printf(m, ",bsize=%u", nfss->bsize);
600 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400601
602#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500603 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400604 seq_printf(m, "\n\tnfsv4:\t");
605 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
606 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
607 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
608 }
609#endif
610
611 /*
612 * Display security flavor in effect for this mount
613 */
Chuck Lever2d767432008-03-14 14:10:08 -0400614 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400615 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400616 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400617
618 /*
619 * Display superblock I/O counters
620 */
621 for_each_possible_cpu(cpu) {
622 struct nfs_iostats *stats;
623
624 preempt_disable();
625 stats = per_cpu_ptr(nfss->io_stats, cpu);
626
627 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
628 totals.events[i] += stats->events[i];
629 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
630 totals.bytes[i] += stats->bytes[i];
631
632 preempt_enable();
633 }
634
635 seq_printf(m, "\n\tevents:\t");
636 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
637 seq_printf(m, "%lu ", totals.events[i]);
638 seq_printf(m, "\n\tbytes:\t");
639 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
640 seq_printf(m, "%Lu ", totals.bytes[i]);
641 seq_printf(m, "\n");
642
643 rpc_print_iostats(m, nfss->client);
644
645 return 0;
646}
647
648/*
649 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400650 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400651 */
Al Viro42faad92008-04-24 07:21:56 -0400652static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400653{
Al Viro42faad92008-04-24 07:21:56 -0400654 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400655 struct rpc_clnt *rpc;
656
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400657 /* -EIO all pending I/O */
658 rpc = server->client_acl;
659 if (!IS_ERR(rpc))
660 rpc_killall_tasks(rpc);
661 rpc = server->client;
662 if (!IS_ERR(rpc))
663 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400664}
665
666/*
Chuck Lever04dcd6e2007-12-10 14:57:53 -0500667 * Set the port number in an address. Be agnostic about the address family.
668 */
669static void nfs_set_port(struct sockaddr *sap, unsigned short port)
670{
671 switch (sap->sa_family) {
672 case AF_INET: {
673 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
674 ap->sin_port = htons(port);
675 break;
676 }
677 case AF_INET6: {
678 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
679 ap->sin6_port = htons(port);
680 break;
681 }
682 }
683}
684
685/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500686 * Sanity-check a server address provided by the mount command.
687 *
688 * Address family must be initialized, and address must not be
689 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400690 */
691static int nfs_verify_server_address(struct sockaddr *addr)
692{
693 switch (addr->sa_family) {
694 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500695 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700696 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500697 }
698 case AF_INET6: {
699 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
700 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400701 }
702 }
703
704 return 0;
705}
706
Chuck Leverce3b7e12008-06-23 12:36:53 -0400707static void nfs_parse_ipv4_address(char *string, size_t str_len,
708 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500709{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400710 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
711 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500712
Chuck Leverce3b7e12008-06-23 12:36:53 -0400713 if (str_len <= INET_ADDRSTRLEN) {
714 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
715 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500716
Chuck Leverce3b7e12008-06-23 12:36:53 -0400717 sin->sin_family = AF_INET;
718 *addr_len = sizeof(*sin);
719 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500720 return;
721 }
Chuck Lever9412b922007-12-10 14:59:21 -0500722
723 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400724 *addr_len = 0;
725}
726
Chuck Leverd8e77482008-06-23 12:37:01 -0400727#define IPV6_SCOPE_DELIMITER '%'
728
Chuck Leverce3b7e12008-06-23 12:36:53 -0400729#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Leverd8e77482008-06-23 12:37:01 -0400730static void nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
731 const char *delim,
732 struct sockaddr_in6 *sin6)
733{
734 char *p;
735 size_t len;
736
737 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
738 return ;
739 if (*delim != IPV6_SCOPE_DELIMITER)
740 return;
741
742 len = (string + str_len) - delim - 1;
743 p = kstrndup(delim + 1, len, GFP_KERNEL);
744 if (p) {
745 unsigned long scope_id = 0;
746 struct net_device *dev;
747
748 dev = dev_get_by_name(&init_net, p);
749 if (dev != NULL) {
750 scope_id = dev->ifindex;
751 dev_put(dev);
752 } else {
753 /* scope_id is set to zero on error */
754 strict_strtoul(p, 10, &scope_id);
755 }
756
757 kfree(p);
758 sin6->sin6_scope_id = scope_id;
759 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
760 }
761}
762
Chuck Leverce3b7e12008-06-23 12:36:53 -0400763static void nfs_parse_ipv6_address(char *string, size_t str_len,
764 struct sockaddr *sap, size_t *addr_len)
765{
766 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
767 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400768 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400769
770 if (str_len <= INET6_ADDRSTRLEN) {
771 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
772 (int)str_len, string);
773
774 sin6->sin6_family = AF_INET6;
775 *addr_len = sizeof(*sin6);
Chuck Leverd8e77482008-06-23 12:37:01 -0400776 if (in6_pton(string, str_len, addr, IPV6_SCOPE_DELIMITER, &delim)) {
777 nfs_parse_ipv6_scope_id(string, str_len, delim, sin6);
Chuck Leverce3b7e12008-06-23 12:36:53 -0400778 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400779 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400780 }
781
782 sap->sa_family = AF_UNSPEC;
783 *addr_len = 0;
784}
785#else
786static void nfs_parse_ipv6_address(char *string, size_t str_len,
787 struct sockaddr *sap, size_t *addr_len)
788{
789 sap->sa_family = AF_UNSPEC;
790 *addr_len = 0;
791}
792#endif
793
794/*
795 * Construct a sockaddr based on the contents of a string that contains
796 * an IP address in presentation format.
797 *
798 * If there is a problem constructing the new sockaddr, set the address
799 * family to AF_UNSPEC.
800 */
801static void nfs_parse_ip_address(char *string, size_t str_len,
802 struct sockaddr *sap, size_t *addr_len)
803{
804 unsigned int i, colons;
805
806 colons = 0;
807 for (i = 0; i < str_len; i++)
808 if (string[i] == ':')
809 colons++;
810
811 if (colons >= 2)
812 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
813 else
814 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500815}
816
817/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400818 * Sanity check the NFS transport protocol.
819 *
820 */
821static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
822{
823 switch (mnt->nfs_server.protocol) {
824 case XPRT_TRANSPORT_UDP:
825 case XPRT_TRANSPORT_TCP:
826 case XPRT_TRANSPORT_RDMA:
827 break;
828 default:
829 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
830 }
831}
832
833/*
834 * For text based NFSv2/v3 mounts, the mount protocol transport default
835 * settings should depend upon the specified NFS transport.
836 */
837static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
838{
839 nfs_validate_transport_protocol(mnt);
840
841 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
842 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
843 return;
844 switch (mnt->nfs_server.protocol) {
845 case XPRT_TRANSPORT_UDP:
846 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
847 break;
848 case XPRT_TRANSPORT_TCP:
849 case XPRT_TRANSPORT_RDMA:
850 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
851 }
852}
853
854/*
Chuck Lever01060c82008-06-24 16:33:38 -0400855 * Parse the value of the 'sec=' option.
856 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400857 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400858 */
859static int nfs_parse_security_flavors(char *value,
860 struct nfs_parsed_mount_data *mnt)
861{
862 substring_t args[MAX_OPT_ARGS];
863
864 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
865
866 switch (match_token(value, nfs_secflavor_tokens, args)) {
867 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400868 mnt->auth_flavor_len = 0;
869 mnt->auth_flavors[0] = RPC_AUTH_NULL;
870 break;
871 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400872 mnt->auth_flavor_len = 0;
873 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
874 break;
875 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400876 mnt->auth_flavor_len = 1;
877 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
878 break;
879 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400880 mnt->auth_flavor_len = 1;
881 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
882 break;
883 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400884 mnt->auth_flavor_len = 1;
885 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
886 break;
887 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400888 mnt->auth_flavor_len = 1;
889 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
890 break;
891 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400892 mnt->auth_flavor_len = 1;
893 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
894 break;
895 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400896 mnt->auth_flavor_len = 1;
897 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
898 break;
899 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400900 mnt->auth_flavor_len = 1;
901 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
902 break;
903 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400904 mnt->auth_flavor_len = 1;
905 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
906 break;
907 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400908 mnt->auth_flavor_len = 1;
909 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
910 break;
911 default:
912 return 0;
913 }
914
915 return 1;
916}
917
Chuck Leverf45663c2008-06-24 19:28:02 -0400918static void nfs_parse_invalid_value(const char *option)
919{
920 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
921}
922
Chuck Lever01060c82008-06-24 16:33:38 -0400923/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400924 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400925 * a data structure. The whole mount string is processed; bad options are
926 * skipped as they are encountered. If there were no errors, return 1;
927 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400928 */
929static int nfs_parse_mount_options(char *raw,
930 struct nfs_parsed_mount_data *mnt)
931{
Eric Parisf9c3a382008-03-05 14:20:18 -0500932 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400933 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400934
935 if (!raw) {
936 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
937 return 1;
938 }
939 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
940
Eric Parisf9c3a382008-03-05 14:20:18 -0500941 secdata = alloc_secdata();
942 if (!secdata)
943 goto out_nomem;
944
945 rc = security_sb_copy_data(raw, secdata);
946 if (rc)
947 goto out_security_failure;
948
949 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
950 if (rc)
951 goto out_security_failure;
952
953 free_secdata(secdata);
954
Chuck Leverbf0fd762007-07-01 12:13:44 -0400955 while ((p = strsep(&raw, ",")) != NULL) {
956 substring_t args[MAX_OPT_ARGS];
957 int option, token;
958
959 if (!*p)
960 continue;
961
962 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
963
964 token = match_token(p, nfs_mount_option_tokens, args);
965 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400966
967 /*
968 * boolean options: foo/nofoo
969 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400970 case Opt_soft:
971 mnt->flags |= NFS_MOUNT_SOFT;
972 break;
973 case Opt_hard:
974 mnt->flags &= ~NFS_MOUNT_SOFT;
975 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400976 case Opt_posix:
977 mnt->flags |= NFS_MOUNT_POSIX;
978 break;
979 case Opt_noposix:
980 mnt->flags &= ~NFS_MOUNT_POSIX;
981 break;
982 case Opt_cto:
983 mnt->flags &= ~NFS_MOUNT_NOCTO;
984 break;
985 case Opt_nocto:
986 mnt->flags |= NFS_MOUNT_NOCTO;
987 break;
988 case Opt_ac:
989 mnt->flags &= ~NFS_MOUNT_NOAC;
990 break;
991 case Opt_noac:
992 mnt->flags |= NFS_MOUNT_NOAC;
993 break;
994 case Opt_lock:
995 mnt->flags &= ~NFS_MOUNT_NONLM;
996 break;
997 case Opt_nolock:
998 mnt->flags |= NFS_MOUNT_NONLM;
999 break;
1000 case Opt_v2:
1001 mnt->flags &= ~NFS_MOUNT_VER3;
1002 break;
1003 case Opt_v3:
1004 mnt->flags |= NFS_MOUNT_VER3;
1005 break;
1006 case Opt_udp:
1007 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001008 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001009 break;
1010 case Opt_tcp:
1011 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001012 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001013 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001014 case Opt_rdma:
1015 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1016 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001017 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001018 case Opt_acl:
1019 mnt->flags &= ~NFS_MOUNT_NOACL;
1020 break;
1021 case Opt_noacl:
1022 mnt->flags |= NFS_MOUNT_NOACL;
1023 break;
1024 case Opt_rdirplus:
1025 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1026 break;
1027 case Opt_nordirplus:
1028 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1029 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001030 case Opt_sharecache:
1031 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1032 break;
1033 case Opt_nosharecache:
1034 mnt->flags |= NFS_MOUNT_UNSHARED;
1035 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001036
Chuck Leverf45663c2008-06-24 19:28:02 -04001037 /*
1038 * options that take numeric values
1039 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001040 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001041 if (match_int(args, &option) ||
1042 option < 0 || option > USHORT_MAX) {
1043 errors++;
1044 nfs_parse_invalid_value("port");
1045 } else
1046 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001047 break;
1048 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001049 if (match_int(args, &option) || option < 0) {
1050 errors++;
1051 nfs_parse_invalid_value("rsize");
1052 } else
1053 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001054 break;
1055 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001056 if (match_int(args, &option) || option < 0) {
1057 errors++;
1058 nfs_parse_invalid_value("wsize");
1059 } else
1060 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001061 break;
1062 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001063 if (match_int(args, &option) || option < 0) {
1064 errors++;
1065 nfs_parse_invalid_value("bsize");
1066 } else
1067 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001068 break;
1069 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001070 if (match_int(args, &option) || option <= 0) {
1071 errors++;
1072 nfs_parse_invalid_value("timeo");
1073 } else
1074 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001075 break;
1076 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001077 if (match_int(args, &option) || option <= 0) {
1078 errors++;
1079 nfs_parse_invalid_value("retrans");
1080 } else
1081 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001082 break;
1083 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001084 if (match_int(args, &option) || option < 0) {
1085 errors++;
1086 nfs_parse_invalid_value("acregmin");
1087 } else
1088 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001089 break;
1090 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001091 if (match_int(args, &option) || option < 0) {
1092 errors++;
1093 nfs_parse_invalid_value("acregmax");
1094 } else
1095 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001096 break;
1097 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001098 if (match_int(args, &option) || option < 0) {
1099 errors++;
1100 nfs_parse_invalid_value("acdirmin");
1101 } else
1102 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001103 break;
1104 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001105 if (match_int(args, &option) || option < 0) {
1106 errors++;
1107 nfs_parse_invalid_value("acdirmax");
1108 } else
1109 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001110 break;
1111 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001112 if (match_int(args, &option) || option < 0) {
1113 errors++;
1114 nfs_parse_invalid_value("actimeo");
1115 } else
1116 mnt->acregmin = mnt->acregmax =
1117 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001118 break;
1119 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001120 if (match_int(args, &option) || option < 0) {
1121 errors++;
1122 nfs_parse_invalid_value("namlen");
1123 } else
1124 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001125 break;
1126 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001127 if (match_int(args, &option) ||
1128 option < 0 || option > USHORT_MAX) {
1129 errors++;
1130 nfs_parse_invalid_value("mountport");
1131 } else
1132 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001133 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001134 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001135 if (match_int(args, &option) ||
1136 option < NFS_MNT_VERSION ||
1137 option > NFS_MNT3_VERSION) {
1138 errors++;
1139 nfs_parse_invalid_value("mountvers");
1140 } else
1141 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001142 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001143 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001144 if (match_int(args, &option)) {
1145 errors++;
1146 nfs_parse_invalid_value("nfsvers");
1147 break;
1148 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001149 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001150 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001151 mnt->flags &= ~NFS_MOUNT_VER3;
1152 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001153 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001154 mnt->flags |= NFS_MOUNT_VER3;
1155 break;
1156 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001157 errors++;
1158 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001159 }
1160 break;
1161
Chuck Leverf45663c2008-06-24 19:28:02 -04001162 /*
1163 * options that take text values
1164 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001165 case Opt_sec:
1166 string = match_strdup(args);
1167 if (string == NULL)
1168 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001169 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001170 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001171 if (!rc) {
1172 errors++;
1173 dfprintk(MOUNT, "NFS: unrecognized "
1174 "security flavor\n");
1175 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001176 break;
1177 case Opt_proto:
1178 string = match_strdup(args);
1179 if (string == NULL)
1180 goto out_nomem;
1181 token = match_token(string,
1182 nfs_xprt_protocol_tokens, args);
1183 kfree(string);
1184
1185 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001186 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001187 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001188 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001189 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001190 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001192 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001193 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001194 case Opt_xprt_rdma:
1195 /* vector side protocols to TCP */
1196 mnt->flags |= NFS_MOUNT_TCP;
1197 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001198 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001199 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001200 errors++;
1201 dfprintk(MOUNT, "NFS: unrecognized "
1202 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001203 }
1204 break;
1205 case Opt_mountproto:
1206 string = match_strdup(args);
1207 if (string == NULL)
1208 goto out_nomem;
1209 token = match_token(string,
1210 nfs_xprt_protocol_tokens, args);
1211 kfree(string);
1212
1213 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001214 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001215 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001216 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001217 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001218 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001219 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001220 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001221 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001222 errors++;
1223 dfprintk(MOUNT, "NFS: unrecognized "
1224 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001225 }
1226 break;
1227 case Opt_addr:
1228 string = match_strdup(args);
1229 if (string == NULL)
1230 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001231 nfs_parse_ip_address(string, strlen(string),
1232 (struct sockaddr *)
1233 &mnt->nfs_server.address,
1234 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001235 kfree(string);
1236 break;
1237 case Opt_clientaddr:
1238 string = match_strdup(args);
1239 if (string == NULL)
1240 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001241 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001242 mnt->client_address = string;
1243 break;
Chuck Lever33832032007-12-10 14:59:13 -05001244 case Opt_mounthost:
1245 string = match_strdup(args);
1246 if (string == NULL)
1247 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001248 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001249 mnt->mount_server.hostname = string;
1250 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001251 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001252 string = match_strdup(args);
1253 if (string == NULL)
1254 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001255 nfs_parse_ip_address(string, strlen(string),
1256 (struct sockaddr *)
1257 &mnt->mount_server.address,
1258 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001259 kfree(string);
1260 break;
1261
Chuck Leverf45663c2008-06-24 19:28:02 -04001262 /*
1263 * Special options
1264 */
1265 case Opt_sloppy:
1266 sloppy = 1;
1267 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1268 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001269 case Opt_userspace:
1270 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001271 dfprintk(MOUNT, "NFS: ignoring mount option "
1272 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001273 break;
1274
1275 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001276 errors++;
1277 dfprintk(MOUNT, "NFS: unrecognized mount option "
1278 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001279 }
1280 }
1281
1282 return 1;
1283
1284out_nomem:
1285 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1286 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001287out_security_failure:
1288 free_secdata(secdata);
1289 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1290 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001291}
1292
1293/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001294 * Use the remote server's MOUNT service to request the NFS file handle
1295 * corresponding to the provided path.
1296 */
1297static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1298 struct nfs_fh *root_fh)
1299{
Chuck Lever4c568012007-12-10 14:59:28 -05001300 struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
Chuck Lever33832032007-12-10 14:59:13 -05001301 char *hostname;
Chuck Lever4c568012007-12-10 14:59:28 -05001302 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001303
1304 if (args->mount_server.version == 0) {
1305 if (args->flags & NFS_MOUNT_VER3)
1306 args->mount_server.version = NFS_MNT3_VERSION;
1307 else
1308 args->mount_server.version = NFS_MNT_VERSION;
1309 }
1310
Chuck Lever33832032007-12-10 14:59:13 -05001311 if (args->mount_server.hostname)
1312 hostname = args->mount_server.hostname;
1313 else
1314 hostname = args->nfs_server.hostname;
1315
Chuck Lever0076d7b2007-07-01 12:13:49 -04001316 /*
1317 * Construct the mount server's address.
1318 */
Chuck Lever4c568012007-12-10 14:59:28 -05001319 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1320 memcpy(sap, &args->nfs_server.address,
1321 args->nfs_server.addrlen);
1322 args->mount_server.addrlen = args->nfs_server.addrlen;
1323 }
1324
James Lentiniaad70002007-09-24 17:32:49 -04001325 /*
1326 * autobind will be used if mount_server.port == 0
1327 */
Chuck Lever4c568012007-12-10 14:59:28 -05001328 nfs_set_port(sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001329
1330 /*
1331 * Now ask the mount server to map our export path
1332 * to a file handle.
1333 */
Chuck Lever4c568012007-12-10 14:59:28 -05001334 status = nfs_mount(sap,
1335 args->mount_server.addrlen,
Chuck Lever33832032007-12-10 14:59:13 -05001336 hostname,
Chuck Lever0076d7b2007-07-01 12:13:49 -04001337 args->nfs_server.export_path,
1338 args->mount_server.version,
1339 args->mount_server.protocol,
1340 root_fh);
Chuck Leverefd83402007-09-11 18:00:58 -04001341 if (status == 0)
1342 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001343
Chuck Lever396cee972008-06-12 12:37:49 -04001344 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Lever33832032007-12-10 14:59:13 -05001345 hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001346 return status;
1347}
1348
Chuck Leverd1aa0822008-06-23 12:36:45 -04001349static int nfs_parse_simple_hostname(const char *dev_name,
1350 char **hostname, size_t maxnamlen,
1351 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001352{
1353 size_t len;
1354 char *colon, *comma;
1355
1356 colon = strchr(dev_name, ':');
1357 if (colon == NULL)
1358 goto out_bad_devname;
1359
1360 len = colon - dev_name;
1361 if (len > maxnamlen)
1362 goto out_hostname;
1363
1364 /* N.B. caller will free nfs_server.hostname in all cases */
1365 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1366 if (!*hostname)
1367 goto out_nomem;
1368
1369 /* kill possible hostname list: not supported */
1370 comma = strchr(*hostname, ',');
1371 if (comma != NULL) {
1372 if (comma == *hostname)
1373 goto out_bad_devname;
1374 *comma = '\0';
1375 }
1376
1377 colon++;
1378 len = strlen(colon);
1379 if (len > maxpathlen)
1380 goto out_path;
1381 *export_path = kstrndup(colon, len, GFP_KERNEL);
1382 if (!*export_path)
1383 goto out_nomem;
1384
1385 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1386 return 0;
1387
1388out_bad_devname:
1389 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1390 return -EINVAL;
1391
1392out_nomem:
1393 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1394 return -ENOMEM;
1395
1396out_hostname:
1397 dfprintk(MOUNT, "NFS: server hostname too long\n");
1398 return -ENAMETOOLONG;
1399
1400out_path:
1401 dfprintk(MOUNT, "NFS: export pathname too long\n");
1402 return -ENAMETOOLONG;
1403}
1404
1405/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001406 * Hostname has square brackets around it because it contains one or
1407 * more colons. We look for the first closing square bracket, and a
1408 * colon must follow it.
1409 */
1410static int nfs_parse_protected_hostname(const char *dev_name,
1411 char **hostname, size_t maxnamlen,
1412 char **export_path, size_t maxpathlen)
1413{
1414 size_t len;
1415 char *start, *end;
1416
1417 start = (char *)(dev_name + 1);
1418
1419 end = strchr(start, ']');
1420 if (end == NULL)
1421 goto out_bad_devname;
1422 if (*(end + 1) != ':')
1423 goto out_bad_devname;
1424
1425 len = end - start;
1426 if (len > maxnamlen)
1427 goto out_hostname;
1428
1429 /* N.B. caller will free nfs_server.hostname in all cases */
1430 *hostname = kstrndup(start, len, GFP_KERNEL);
1431 if (*hostname == NULL)
1432 goto out_nomem;
1433
1434 end += 2;
1435 len = strlen(end);
1436 if (len > maxpathlen)
1437 goto out_path;
1438 *export_path = kstrndup(end, len, GFP_KERNEL);
1439 if (!*export_path)
1440 goto out_nomem;
1441
1442 return 0;
1443
1444out_bad_devname:
1445 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1446 return -EINVAL;
1447
1448out_nomem:
1449 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1450 return -ENOMEM;
1451
1452out_hostname:
1453 dfprintk(MOUNT, "NFS: server hostname too long\n");
1454 return -ENAMETOOLONG;
1455
1456out_path:
1457 dfprintk(MOUNT, "NFS: export pathname too long\n");
1458 return -ENAMETOOLONG;
1459}
1460
1461/*
1462 * Split "dev_name" into "hostname:export_path".
1463 *
1464 * The leftmost colon demarks the split between the server's hostname
1465 * and the export path. If the hostname starts with a left square
1466 * bracket, then it may contain colons.
1467 *
1468 * Note: caller frees hostname and export path, even on error.
1469 */
1470static int nfs_parse_devname(const char *dev_name,
1471 char **hostname, size_t maxnamlen,
1472 char **export_path, size_t maxpathlen)
1473{
1474 if (*dev_name == '[')
1475 return nfs_parse_protected_hostname(dev_name,
1476 hostname, maxnamlen,
1477 export_path, maxpathlen);
1478
1479 return nfs_parse_simple_hostname(dev_name,
1480 hostname, maxnamlen,
1481 export_path, maxpathlen);
1482}
1483
1484/*
David Howells54ceac42006-08-22 20:06:13 -04001485 * Validate the NFS2/NFS3 mount data
1486 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001487 *
1488 * For option strings, user space handles the following behaviors:
1489 *
1490 * + DNS: mapping server host name to IP address ("addr=" option)
1491 *
1492 * + failure mode: how to behave if a mount request can't be handled
1493 * immediately ("fg/bg" option)
1494 *
1495 * + retry: how often to retry a mount request ("retry=" option)
1496 *
1497 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1498 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001499 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001500static int nfs_validate_mount_data(void *options,
1501 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001502 struct nfs_fh *mntfh,
1503 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001504{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001505 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001506
Chuck Lever5df36e72007-07-01 12:12:56 -04001507 if (data == NULL)
1508 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001509
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001510 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1511 args->rsize = NFS_MAX_FILE_IO_SIZE;
1512 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001513 args->acregmin = NFS_DEF_ACREGMIN;
1514 args->acregmax = NFS_DEF_ACREGMAX;
1515 args->acdirmin = NFS_DEF_ACDIRMIN;
1516 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001517 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001518 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001519 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001520 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001521
David Howellsf7b422b2006-06-09 09:34:33 -04001522 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001523 case 1:
1524 data->namlen = 0;
1525 case 2:
1526 data->bsize = 0;
1527 case 3:
1528 if (data->flags & NFS_MOUNT_VER3)
1529 goto out_no_v3;
1530 data->root.size = NFS2_FHSIZE;
1531 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1532 case 4:
1533 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1534 goto out_no_sec;
1535 case 5:
1536 memset(data->context, 0, sizeof(data->context));
1537 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001538 if (data->flags & NFS_MOUNT_VER3) {
1539 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1540 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001541 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001542 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001543 mntfh->size = NFS2_FHSIZE;
1544
Chuck Lever5df36e72007-07-01 12:12:56 -04001545
1546 memcpy(mntfh->data, data->root.data, mntfh->size);
1547 if (mntfh->size < sizeof(mntfh->data))
1548 memset(mntfh->data + mntfh->size, 0,
1549 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001550
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001551 /*
1552 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1553 * can deal with.
1554 */
1555 args->flags = data->flags;
1556 args->rsize = data->rsize;
1557 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001558 args->timeo = data->timeo;
1559 args->retrans = data->retrans;
1560 args->acregmin = data->acregmin;
1561 args->acregmax = data->acregmax;
1562 args->acdirmin = data->acdirmin;
1563 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001564
1565 memcpy(&args->nfs_server.address, &data->addr,
1566 sizeof(data->addr));
1567 args->nfs_server.addrlen = sizeof(data->addr);
1568 if (!nfs_verify_server_address((struct sockaddr *)
1569 &args->nfs_server.address))
1570 goto out_no_address;
1571
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001572 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001573 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001574 /* N.B. caller will free nfs_server.hostname in all cases */
1575 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1576 args->namlen = data->namlen;
1577 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001578
1579 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1580 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001581 if (!args->nfs_server.hostname)
1582 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001583
1584 /*
1585 * The legacy version 6 binary mount data from userspace has a
1586 * field used only to transport selinux information into the
1587 * the kernel. To continue to support that functionality we
1588 * have a touch of selinux knowledge here in the NFS code. The
1589 * userspace code converted context=blah to just blah so we are
1590 * converting back to the full string selinux understands.
1591 */
1592 if (data->context[0]){
1593#ifdef CONFIG_SECURITY_SELINUX
1594 int rc;
1595 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1596 if (!opts_str)
1597 return -ENOMEM;
1598 strcpy(opts_str, "context=");
1599 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1600 strcat(opts_str, &data->context[0]);
1601 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1602 kfree(opts_str);
1603 if (rc)
1604 return rc;
1605#else
1606 return -EINVAL;
1607#endif
1608 }
1609
Chuck Lever5df36e72007-07-01 12:12:56 -04001610 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001611 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001612 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001613
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001614 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001615 return -EINVAL;
1616
Chuck Lever6e88e062007-09-24 15:39:50 -04001617 if (!nfs_verify_server_address((struct sockaddr *)
1618 &args->nfs_server.address))
1619 goto out_no_address;
1620
Chuck Levered596a82008-06-26 17:47:05 -04001621 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1622 args->nfs_server.port);
1623
Trond Myklebust259875e2008-07-02 14:43:47 -04001624 nfs_set_mount_transport_protocol(args);
1625
Chuck Leverdc045892008-06-23 12:36:37 -04001626 status = nfs_parse_devname(dev_name,
1627 &args->nfs_server.hostname,
1628 PAGE_SIZE,
1629 &args->nfs_server.export_path,
1630 NFS_MAXPATHLEN);
1631 if (!status)
1632 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001633
Chuck Leverdc045892008-06-23 12:36:37 -04001634 kfree(args->nfs_server.export_path);
1635 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001636
Chuck Lever136d5582007-07-01 12:13:54 -04001637 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001638 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001639
Chuck Lever136d5582007-07-01 12:13:54 -04001640 break;
1641 }
David Howellsf7b422b2006-06-09 09:34:33 -04001642 }
David Howells54ceac42006-08-22 20:06:13 -04001643
David Howellsf7b422b2006-06-09 09:34:33 -04001644#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001645 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001646 goto out_v3_not_compiled;
1647#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001648
David Howells54ceac42006-08-22 20:06:13 -04001649 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001650
1651out_no_data:
1652 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1653 return -EINVAL;
1654
1655out_no_v3:
1656 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1657 data->version);
1658 return -EINVAL;
1659
1660out_no_sec:
1661 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1662 return -EINVAL;
1663
Chuck Lever5df36e72007-07-01 12:12:56 -04001664#ifndef CONFIG_NFS_V3
1665out_v3_not_compiled:
1666 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1667 return -EPROTONOSUPPORT;
1668#endif /* !CONFIG_NFS_V3 */
1669
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001670out_nomem:
1671 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1672 return -ENOMEM;
1673
Chuck Lever5df36e72007-07-01 12:12:56 -04001674out_no_address:
1675 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1676 return -EINVAL;
1677
1678out_invalid_fh:
1679 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1680 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001681}
1682
Jeff Layton48b605f2008-06-10 15:38:39 -04001683static int
1684nfs_compare_remount_data(struct nfs_server *nfss,
1685 struct nfs_parsed_mount_data *data)
1686{
1687 if (data->flags != nfss->flags ||
1688 data->rsize != nfss->rsize ||
1689 data->wsize != nfss->wsize ||
1690 data->retrans != nfss->client->cl_timeout->to_retries ||
1691 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1692 data->acregmin != nfss->acregmin / HZ ||
1693 data->acregmax != nfss->acregmax / HZ ||
1694 data->acdirmin != nfss->acdirmin / HZ ||
1695 data->acdirmax != nfss->acdirmax / HZ ||
1696 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1697 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1698 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1699 data->nfs_server.addrlen) != 0)
1700 return -EINVAL;
1701
1702 return 0;
1703}
1704
1705static int
1706nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1707{
1708 int error;
1709 struct nfs_server *nfss = sb->s_fs_info;
1710 struct nfs_parsed_mount_data *data;
1711 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1712 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001713 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001714
1715 /*
1716 * Userspace mount programs that send binary options generally send
1717 * them populated with default values. We have no way to know which
1718 * ones were explicitly specified. Fall back to legacy behavior and
1719 * just return success.
1720 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001721 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1722 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1723 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001724 return 0;
1725
1726 data = kzalloc(sizeof(*data), GFP_KERNEL);
1727 if (data == NULL)
1728 return -ENOMEM;
1729
1730 /* fill out struct with values from existing mount */
1731 data->flags = nfss->flags;
1732 data->rsize = nfss->rsize;
1733 data->wsize = nfss->wsize;
1734 data->retrans = nfss->client->cl_timeout->to_retries;
1735 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1736 data->acregmin = nfss->acregmin / HZ;
1737 data->acregmax = nfss->acregmax / HZ;
1738 data->acdirmin = nfss->acdirmin / HZ;
1739 data->acdirmax = nfss->acdirmax / HZ;
1740 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1741 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1742 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1743 data->nfs_server.addrlen);
1744
1745 /* overwrite those values with any that were specified */
1746 error = nfs_parse_mount_options((char *)options, data);
1747 if (error < 0)
1748 goto out;
1749
1750 /* compare new mount options with old ones */
1751 error = nfs_compare_remount_data(nfss, data);
1752out:
1753 kfree(data);
1754 return error;
1755}
1756
David Howells54ceac42006-08-22 20:06:13 -04001757/*
1758 * Initialise the common bits of the superblock
1759 */
1760static inline void nfs_initialise_sb(struct super_block *sb)
1761{
1762 struct nfs_server *server = NFS_SB(sb);
1763
1764 sb->s_magic = NFS_SUPER_MAGIC;
1765
1766 /* We probably want something more informative here */
1767 snprintf(sb->s_id, sizeof(sb->s_id),
1768 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1769
1770 if (sb->s_blocksize == 0)
1771 sb->s_blocksize = nfs_block_bits(server->wsize,
1772 &sb->s_blocksize_bits);
1773
1774 if (server->flags & NFS_MOUNT_NOAC)
1775 sb->s_flags |= MS_SYNCHRONOUS;
1776
1777 nfs_super_set_maxbytes(sb, server->maxfilesize);
1778}
1779
1780/*
1781 * Finish setting up an NFS2/3 superblock
1782 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001783static void nfs_fill_super(struct super_block *sb,
1784 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001785{
1786 struct nfs_server *server = NFS_SB(sb);
1787
1788 sb->s_blocksize_bits = 0;
1789 sb->s_blocksize = 0;
1790 if (data->bsize)
1791 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1792
1793 if (server->flags & NFS_MOUNT_VER3) {
1794 /* The VFS shouldn't apply the umask to mode bits. We will do
1795 * so ourselves when necessary.
1796 */
1797 sb->s_flags |= MS_POSIXACL;
1798 sb->s_time_gran = 1;
1799 }
1800
1801 sb->s_op = &nfs_sops;
1802 nfs_initialise_sb(sb);
1803}
1804
1805/*
1806 * Finish setting up a cloned NFS2/3 superblock
1807 */
1808static void nfs_clone_super(struct super_block *sb,
1809 const struct super_block *old_sb)
1810{
1811 struct nfs_server *server = NFS_SB(sb);
1812
1813 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1814 sb->s_blocksize = old_sb->s_blocksize;
1815 sb->s_maxbytes = old_sb->s_maxbytes;
1816
1817 if (server->flags & NFS_MOUNT_VER3) {
1818 /* The VFS shouldn't apply the umask to mode bits. We will do
1819 * so ourselves when necessary.
1820 */
1821 sb->s_flags |= MS_POSIXACL;
1822 sb->s_time_gran = 1;
1823 }
1824
1825 sb->s_op = old_sb->s_op;
1826 nfs_initialise_sb(sb);
1827}
1828
Trond Myklebust275a5d22007-05-16 16:53:28 -04001829#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1830
1831static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1832{
1833 const struct nfs_server *a = s->s_fs_info;
1834 const struct rpc_clnt *clnt_a = a->client;
1835 const struct rpc_clnt *clnt_b = b->client;
1836
1837 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1838 goto Ebusy;
1839 if (a->nfs_client != b->nfs_client)
1840 goto Ebusy;
1841 if (a->flags != b->flags)
1842 goto Ebusy;
1843 if (a->wsize != b->wsize)
1844 goto Ebusy;
1845 if (a->rsize != b->rsize)
1846 goto Ebusy;
1847 if (a->acregmin != b->acregmin)
1848 goto Ebusy;
1849 if (a->acregmax != b->acregmax)
1850 goto Ebusy;
1851 if (a->acdirmin != b->acdirmin)
1852 goto Ebusy;
1853 if (a->acdirmax != b->acdirmax)
1854 goto Ebusy;
1855 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1856 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001857 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001858Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001859 return 0;
1860}
1861
1862struct nfs_sb_mountdata {
1863 struct nfs_server *server;
1864 int mntflags;
1865};
1866
1867static int nfs_set_super(struct super_block *s, void *data)
1868{
1869 struct nfs_sb_mountdata *sb_mntdata = data;
1870 struct nfs_server *server = sb_mntdata->server;
1871 int ret;
1872
1873 s->s_flags = sb_mntdata->mntflags;
1874 s->s_fs_info = server;
1875 ret = set_anon_super(s, server);
1876 if (ret == 0)
1877 server->s_dev = s->s_dev;
1878 return ret;
1879}
1880
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001881static int nfs_compare_super_address(struct nfs_server *server1,
1882 struct nfs_server *server2)
1883{
1884 struct sockaddr *sap1, *sap2;
1885
1886 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1887 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1888
1889 if (sap1->sa_family != sap2->sa_family)
1890 return 0;
1891
1892 switch (sap1->sa_family) {
1893 case AF_INET: {
1894 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1895 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1896 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1897 return 0;
1898 if (sin1->sin_port != sin2->sin_port)
1899 return 0;
1900 break;
1901 }
1902 case AF_INET6: {
1903 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1904 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1905 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1906 return 0;
1907 if (sin1->sin6_port != sin2->sin6_port)
1908 return 0;
1909 break;
1910 }
1911 default:
1912 return 0;
1913 }
1914
1915 return 1;
1916}
1917
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001918static int nfs_compare_super(struct super_block *sb, void *data)
1919{
1920 struct nfs_sb_mountdata *sb_mntdata = data;
1921 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1922 int mntflags = sb_mntdata->mntflags;
1923
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001924 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001925 return 0;
1926 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1927 if (old->flags & NFS_MOUNT_UNSHARED)
1928 return 0;
1929 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1930 return 0;
1931 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001932}
1933
Miklos Szeredifa799752008-04-30 00:54:33 -07001934static int nfs_bdi_register(struct nfs_server *server)
1935{
1936 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1937}
1938
David Howells54ceac42006-08-22 20:06:13 -04001939static int nfs_get_sb(struct file_system_type *fs_type,
1940 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1941{
1942 struct nfs_server *server = NULL;
1943 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001944 struct nfs_parsed_mount_data *data;
1945 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001946 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001947 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001948 struct nfs_sb_mountdata sb_mntdata = {
1949 .mntflags = flags,
1950 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001951 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001952
Trond Myklebust33852a12008-06-19 14:20:11 -04001953 data = kzalloc(sizeof(*data), GFP_KERNEL);
1954 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1955 if (data == NULL || mntfh == NULL)
1956 goto out_free_fh;
1957
1958 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001959
David Howells54ceac42006-08-22 20:06:13 -04001960 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04001961 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04001962 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001963 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001964
1965 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001966 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001967 if (IS_ERR(server)) {
1968 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04001969 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001970 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001971 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04001972
Trond Myklebust75180df2007-05-16 16:53:28 -04001973 if (server->flags & NFS_MOUNT_UNSHARED)
1974 compare_super = NULL;
1975
David Howells54ceac42006-08-22 20:06:13 -04001976 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001977 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04001978 if (IS_ERR(s)) {
1979 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04001980 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04001981 }
1982
David Howells54ceac42006-08-22 20:06:13 -04001983 if (s->s_fs_info != server) {
1984 nfs_free_server(server);
1985 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07001986 } else {
1987 error = nfs_bdi_register(server);
1988 if (error)
1989 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04001990 }
David Howells54ceac42006-08-22 20:06:13 -04001991
1992 if (!s->s_root) {
1993 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001994 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04001995 }
1996
Trond Myklebust33852a12008-06-19 14:20:11 -04001997 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001998 if (IS_ERR(mntroot)) {
1999 error = PTR_ERR(mntroot);
2000 goto error_splat_super;
2001 }
2002
Trond Myklebust33852a12008-06-19 14:20:11 -04002003 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002004 if (error)
2005 goto error_splat_root;
2006
David Howellsf7b422b2006-06-09 09:34:33 -04002007 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002008 mnt->mnt_sb = s;
2009 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002010 error = 0;
2011
2012out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002013 kfree(data->nfs_server.hostname);
2014 kfree(data->mount_server.hostname);
2015 security_free_mnt_opts(&data->lsm_opts);
2016out_free_fh:
2017 kfree(mntfh);
2018 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002019 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002020
David Howells54ceac42006-08-22 20:06:13 -04002021out_err_nosb:
2022 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002023 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002024
Eric Parisf9c3a382008-03-05 14:20:18 -05002025error_splat_root:
2026 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002027error_splat_super:
2028 up_write(&s->s_umount);
2029 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002030 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002031}
2032
David Howells54ceac42006-08-22 20:06:13 -04002033/*
2034 * Destroy an NFS2/3 superblock
2035 */
David Howellsf7b422b2006-06-09 09:34:33 -04002036static void nfs_kill_super(struct super_block *s)
2037{
2038 struct nfs_server *server = NFS_SB(s);
2039
Miklos Szeredifa799752008-04-30 00:54:33 -07002040 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002041 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002042 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002043}
2044
David Howells54ceac42006-08-22 20:06:13 -04002045/*
2046 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2047 */
2048static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2049 const char *dev_name, void *raw_data,
2050 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002051{
2052 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002053 struct super_block *s;
2054 struct nfs_server *server;
2055 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002056 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002057 struct nfs_sb_mountdata sb_mntdata = {
2058 .mntflags = flags,
2059 };
David Howells54ceac42006-08-22 20:06:13 -04002060 int error;
2061
2062 dprintk("--> nfs_xdev_get_sb()\n");
2063
2064 /* create a new volume representation */
2065 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2066 if (IS_ERR(server)) {
2067 error = PTR_ERR(server);
2068 goto out_err_noserver;
2069 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002070 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002071
Trond Myklebust75180df2007-05-16 16:53:28 -04002072 if (server->flags & NFS_MOUNT_UNSHARED)
2073 compare_super = NULL;
2074
David Howells54ceac42006-08-22 20:06:13 -04002075 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002076 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002077 if (IS_ERR(s)) {
2078 error = PTR_ERR(s);
2079 goto out_err_nosb;
2080 }
2081
2082 if (s->s_fs_info != server) {
2083 nfs_free_server(server);
2084 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002085 } else {
2086 error = nfs_bdi_register(server);
2087 if (error)
2088 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002089 }
2090
2091 if (!s->s_root) {
2092 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002093 nfs_clone_super(s, data->sb);
2094 }
2095
2096 mntroot = nfs_get_root(s, data->fh);
2097 if (IS_ERR(mntroot)) {
2098 error = PTR_ERR(mntroot);
2099 goto error_splat_super;
2100 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002101 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002102 dput(mntroot);
2103 error = -ESTALE;
2104 goto error_splat_super;
2105 }
David Howells54ceac42006-08-22 20:06:13 -04002106
2107 s->s_flags |= MS_ACTIVE;
2108 mnt->mnt_sb = s;
2109 mnt->mnt_root = mntroot;
2110
Eric Parisf9c3a382008-03-05 14:20:18 -05002111 /* clone any lsm security options from the parent to the new sb */
2112 security_sb_clone_mnt_opts(data->sb, s);
2113
David Howells54ceac42006-08-22 20:06:13 -04002114 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2115 return 0;
2116
2117out_err_nosb:
2118 nfs_free_server(server);
2119out_err_noserver:
2120 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2121 return error;
2122
2123error_splat_super:
2124 up_write(&s->s_umount);
2125 deactivate_super(s);
2126 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2127 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002128}
2129
2130#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002131
2132/*
2133 * Finish setting up a cloned NFS4 superblock
2134 */
2135static void nfs4_clone_super(struct super_block *sb,
2136 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002137{
David Howells54ceac42006-08-22 20:06:13 -04002138 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2139 sb->s_blocksize = old_sb->s_blocksize;
2140 sb->s_maxbytes = old_sb->s_maxbytes;
2141 sb->s_time_gran = 1;
2142 sb->s_op = old_sb->s_op;
2143 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002144}
2145
2146/*
2147 * Set up an NFS4 superblock
2148 */
David Howells54ceac42006-08-22 20:06:13 -04002149static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002150{
David Howellsf7b422b2006-06-09 09:34:33 -04002151 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002152 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002153 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002154}
2155
David Howells54ceac42006-08-22 20:06:13 -04002156/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002157 * Validate NFSv4 mount options
2158 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002159static int nfs4_validate_mount_data(void *options,
2160 struct nfs_parsed_mount_data *args,
2161 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002162{
Chuck Lever4c568012007-12-10 14:59:28 -05002163 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002164 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002165 char *c;
2166
2167 if (data == NULL)
2168 goto out_no_data;
2169
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002170 args->rsize = NFS_MAX_FILE_IO_SIZE;
2171 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002172 args->acregmin = NFS_DEF_ACREGMIN;
2173 args->acregmax = NFS_DEF_ACREGMAX;
2174 args->acdirmin = NFS_DEF_ACDIRMIN;
2175 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002176 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002177 args->auth_flavors[0] = RPC_AUTH_UNIX;
2178 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002179
Chuck Leverf0768eb2007-07-01 12:13:01 -04002180 switch (data->version) {
2181 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002182 ap = (struct sockaddr_in *)&args->nfs_server.address;
2183 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002184 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002185 if (data->host_addrlen == 0)
2186 goto out_no_address;
2187 args->nfs_server.addrlen = data->host_addrlen;
2188 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002189 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002190 if (!nfs_verify_server_address((struct sockaddr *)
2191 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002192 goto out_no_address;
2193
Chuck Lever6738b252008-06-24 16:33:54 -04002194 if (data->auth_flavourlen) {
2195 if (data->auth_flavourlen > 1)
2196 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002197 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002198 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002199 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002200 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002201 }
2202
2203 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2204 if (IS_ERR(c))
2205 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002206 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002207
2208 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2209 if (IS_ERR(c))
2210 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002211 args->nfs_server.export_path = c;
2212 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002213
2214 c = strndup_user(data->client_addr.data, 16);
2215 if (IS_ERR(c))
2216 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002217 args->client_address = c;
2218
2219 /*
2220 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2221 * can deal with.
2222 */
2223
2224 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2225 args->rsize = data->rsize;
2226 args->wsize = data->wsize;
2227 args->timeo = data->timeo;
2228 args->retrans = data->retrans;
2229 args->acregmin = data->acregmin;
2230 args->acregmax = data->acregmax;
2231 args->acdirmin = data->acdirmin;
2232 args->acdirmax = data->acdirmax;
2233 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002234 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002235
2236 break;
Chuck Lever80071222007-07-01 12:13:59 -04002237 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002238 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002239
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002240 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002241 return -EINVAL;
2242
2243 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002244 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002245 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002246
Chuck Levered596a82008-06-26 17:47:05 -04002247 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2248 args->nfs_server.port);
2249
Trond Myklebust259875e2008-07-02 14:43:47 -04002250 nfs_validate_transport_protocol(args);
2251
Chuck Lever6738b252008-06-24 16:33:54 -04002252 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002253 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002254
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002255 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002256 goto out_no_client_address;
2257
Chuck Leverdc045892008-06-23 12:36:37 -04002258 status = nfs_parse_devname(dev_name,
2259 &args->nfs_server.hostname,
2260 NFS4_MAXNAMLEN,
2261 &args->nfs_server.export_path,
2262 NFS4_MAXPATHLEN);
2263 if (status < 0)
2264 return status;
2265
Chuck Lever80071222007-07-01 12:13:59 -04002266 break;
2267 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002268 }
2269
2270 return 0;
2271
2272out_no_data:
2273 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2274 return -EINVAL;
2275
2276out_inval_auth:
2277 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2278 data->auth_flavourlen);
2279 return -EINVAL;
2280
2281out_no_address:
2282 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2283 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002284
2285out_no_client_address:
2286 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2287 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002288}
2289
2290/*
David Howells54ceac42006-08-22 20:06:13 -04002291 * Get the superblock for an NFS4 mountpoint
2292 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002293static int nfs4_get_sb(struct file_system_type *fs_type,
2294 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002295{
Trond Myklebust33852a12008-06-19 14:20:11 -04002296 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002297 struct super_block *s;
2298 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002299 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002300 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002301 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002302 struct nfs_sb_mountdata sb_mntdata = {
2303 .mntflags = flags,
2304 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002305 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002306
Trond Myklebust33852a12008-06-19 14:20:11 -04002307 data = kzalloc(sizeof(*data), GFP_KERNEL);
2308 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2309 if (data == NULL || mntfh == NULL)
2310 goto out_free_fh;
2311
2312 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002313
Chuck Leverf0768eb2007-07-01 12:13:01 -04002314 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002315 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002316 if (error < 0)
2317 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002318
David Howells54ceac42006-08-22 20:06:13 -04002319 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002320 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002321 if (IS_ERR(server)) {
2322 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002323 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002324 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002325 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002326
Trond Myklebust75180df2007-05-16 16:53:28 -04002327 if (server->flags & NFS4_MOUNT_UNSHARED)
2328 compare_super = NULL;
2329
David Howells54ceac42006-08-22 20:06:13 -04002330 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002331 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002332 if (IS_ERR(s)) {
2333 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002334 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002335 }
2336
Trond Myklebust5dd31772006-08-24 01:03:05 -04002337 if (s->s_fs_info != server) {
2338 nfs_free_server(server);
2339 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002340 } else {
2341 error = nfs_bdi_register(server);
2342 if (error)
2343 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002344 }
2345
David Howells54ceac42006-08-22 20:06:13 -04002346 if (!s->s_root) {
2347 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002348 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002349 }
David Howellsf7b422b2006-06-09 09:34:33 -04002350
Trond Myklebust33852a12008-06-19 14:20:11 -04002351 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002352 if (IS_ERR(mntroot)) {
2353 error = PTR_ERR(mntroot);
2354 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002355 }
David Howells54ceac42006-08-22 20:06:13 -04002356
Trond Myklebust33852a12008-06-19 14:20:11 -04002357 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002358 if (error)
2359 goto error_splat_root;
2360
David Howellsf7b422b2006-06-09 09:34:33 -04002361 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002362 mnt->mnt_sb = s;
2363 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002364 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002365
Chuck Lever29eb9812007-07-01 12:12:30 -04002366out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002367 kfree(data->client_address);
2368 kfree(data->nfs_server.export_path);
2369 kfree(data->nfs_server.hostname);
2370 security_free_mnt_opts(&data->lsm_opts);
2371out_free_fh:
2372 kfree(mntfh);
2373 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002374 return error;
David Howells54ceac42006-08-22 20:06:13 -04002375
Chuck Lever29eb9812007-07-01 12:12:30 -04002376out_free:
2377 nfs_free_server(server);
2378 goto out;
2379
Eric Paris46c8ac72008-05-02 13:42:42 -07002380error_splat_root:
2381 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002382error_splat_super:
2383 up_write(&s->s_umount);
2384 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002385 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002386}
2387
2388static void nfs4_kill_super(struct super_block *sb)
2389{
2390 struct nfs_server *server = NFS_SB(sb);
2391
2392 nfs_return_all_delegations(sb);
2393 kill_anon_super(sb);
2394
2395 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002396 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002397}
2398
2399/*
David Howells54ceac42006-08-22 20:06:13 -04002400 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002401 */
David Howells54ceac42006-08-22 20:06:13 -04002402static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2403 const char *dev_name, void *raw_data,
2404 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002405{
2406 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002407 struct super_block *s;
2408 struct nfs_server *server;
2409 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002410 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002411 struct nfs_sb_mountdata sb_mntdata = {
2412 .mntflags = flags,
2413 };
David Howells54ceac42006-08-22 20:06:13 -04002414 int error;
2415
2416 dprintk("--> nfs4_xdev_get_sb()\n");
2417
2418 /* create a new volume representation */
2419 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2420 if (IS_ERR(server)) {
2421 error = PTR_ERR(server);
2422 goto out_err_noserver;
2423 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002424 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002425
Trond Myklebust75180df2007-05-16 16:53:28 -04002426 if (server->flags & NFS4_MOUNT_UNSHARED)
2427 compare_super = NULL;
2428
David Howells54ceac42006-08-22 20:06:13 -04002429 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002430 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002431 if (IS_ERR(s)) {
2432 error = PTR_ERR(s);
2433 goto out_err_nosb;
2434 }
2435
2436 if (s->s_fs_info != server) {
2437 nfs_free_server(server);
2438 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002439 } else {
2440 error = nfs_bdi_register(server);
2441 if (error)
2442 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002443 }
2444
2445 if (!s->s_root) {
2446 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002447 nfs4_clone_super(s, data->sb);
2448 }
2449
2450 mntroot = nfs4_get_root(s, data->fh);
2451 if (IS_ERR(mntroot)) {
2452 error = PTR_ERR(mntroot);
2453 goto error_splat_super;
2454 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002455 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2456 dput(mntroot);
2457 error = -ESTALE;
2458 goto error_splat_super;
2459 }
David Howells54ceac42006-08-22 20:06:13 -04002460
2461 s->s_flags |= MS_ACTIVE;
2462 mnt->mnt_sb = s;
2463 mnt->mnt_root = mntroot;
2464
Eric Paris46c8ac72008-05-02 13:42:42 -07002465 security_sb_clone_mnt_opts(data->sb, s);
2466
David Howells54ceac42006-08-22 20:06:13 -04002467 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2468 return 0;
2469
2470out_err_nosb:
2471 nfs_free_server(server);
2472out_err_noserver:
2473 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2474 return error;
2475
2476error_splat_super:
2477 up_write(&s->s_umount);
2478 deactivate_super(s);
2479 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2480 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002481}
2482
David Howells54ceac42006-08-22 20:06:13 -04002483/*
2484 * Create an NFS4 server record on referral traversal
2485 */
2486static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2487 const char *dev_name, void *raw_data,
2488 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002489{
2490 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002491 struct super_block *s;
2492 struct nfs_server *server;
2493 struct dentry *mntroot;
2494 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002495 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002496 struct nfs_sb_mountdata sb_mntdata = {
2497 .mntflags = flags,
2498 };
David Howells54ceac42006-08-22 20:06:13 -04002499 int error;
2500
2501 dprintk("--> nfs4_referral_get_sb()\n");
2502
2503 /* create a new volume representation */
2504 server = nfs4_create_referral_server(data, &mntfh);
2505 if (IS_ERR(server)) {
2506 error = PTR_ERR(server);
2507 goto out_err_noserver;
2508 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002509 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002510
Trond Myklebust75180df2007-05-16 16:53:28 -04002511 if (server->flags & NFS4_MOUNT_UNSHARED)
2512 compare_super = NULL;
2513
David Howells54ceac42006-08-22 20:06:13 -04002514 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002515 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002516 if (IS_ERR(s)) {
2517 error = PTR_ERR(s);
2518 goto out_err_nosb;
2519 }
2520
2521 if (s->s_fs_info != server) {
2522 nfs_free_server(server);
2523 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002524 } else {
2525 error = nfs_bdi_register(server);
2526 if (error)
2527 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002528 }
2529
2530 if (!s->s_root) {
2531 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002532 nfs4_fill_super(s);
2533 }
2534
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002535 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002536 if (IS_ERR(mntroot)) {
2537 error = PTR_ERR(mntroot);
2538 goto error_splat_super;
2539 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002540 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2541 dput(mntroot);
2542 error = -ESTALE;
2543 goto error_splat_super;
2544 }
David Howells54ceac42006-08-22 20:06:13 -04002545
2546 s->s_flags |= MS_ACTIVE;
2547 mnt->mnt_sb = s;
2548 mnt->mnt_root = mntroot;
2549
Eric Paris46c8ac72008-05-02 13:42:42 -07002550 security_sb_clone_mnt_opts(data->sb, s);
2551
David Howells54ceac42006-08-22 20:06:13 -04002552 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2553 return 0;
2554
2555out_err_nosb:
2556 nfs_free_server(server);
2557out_err_noserver:
2558 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2559 return error;
2560
2561error_splat_super:
2562 up_write(&s->s_umount);
2563 deactivate_super(s);
2564 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2565 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002566}
2567
David Howells54ceac42006-08-22 20:06:13 -04002568#endif /* CONFIG_NFS_V4 */