blob: eb391d8d70ba023f94d9212d585f64f19abd27ab [file] [log] [blame]
David Howellsf7b422b2006-06-09 09:34:33 -04001/*
2 * linux/fs/nfs/super.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs superblock handling functions
7 *
Alan Cox526719b2008-10-27 15:19:48 +00008 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
David Howellsf7b422b2006-06-09 09:34:33 -04009 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
10 *
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
13 *
14 * Split from inode.c by David Howells <dhowells@redhat.com>
15 *
David Howells54ceac42006-08-22 20:06:13 -040016 * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17 * particular server are held in the same superblock
18 * - NFS superblocks can have several effective roots to the dentry tree
19 * - directory type roots are spliced into the tree when a path from one root reaches the root
20 * of another (see nfs_lookup())
David Howellsf7b422b2006-06-09 09:34:33 -040021 */
22
David Howellsf7b422b2006-06-09 09:34:33 -040023#include <linux/module.h>
24#include <linux/init.h>
25
26#include <linux/time.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/string.h>
30#include <linux/stat.h>
31#include <linux/errno.h>
32#include <linux/unistd.h>
33#include <linux/sunrpc/clnt.h>
34#include <linux/sunrpc/stats.h>
35#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040036#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040037#include <linux/sunrpc/xprtrdma.h>
David Howellsf7b422b2006-06-09 09:34:33 -040038#include <linux/nfs_fs.h>
39#include <linux/nfs_mount.h>
40#include <linux/nfs4_mount.h>
41#include <linux/lockd/bind.h>
42#include <linux/smp_lock.h>
43#include <linux/seq_file.h>
44#include <linux/mount.h>
45#include <linux/nfs_idmap.h>
46#include <linux/vfs.h>
47#include <linux/inet.h>
Chuck Leverfd00a8f2007-12-10 14:57:38 -050048#include <linux/in6.h>
49#include <net/ipv6.h>
Chuck Leverd8e77482008-06-23 12:37:01 -040050#include <linux/netdevice.h>
David Howellsf7b422b2006-06-09 09:34:33 -040051#include <linux/nfs_xdr.h>
Adrian Bunkb5d5dfb2007-02-12 00:53:40 -080052#include <linux/magic.h>
Chuck Leverbf0fd762007-07-01 12:13:44 -040053#include <linux/parser.h>
David Howellsf7b422b2006-06-09 09:34:33 -040054
55#include <asm/system.h>
56#include <asm/uaccess.h>
57
58#include "nfs4_fs.h"
59#include "callback.h"
60#include "delegation.h"
61#include "iostat.h"
62#include "internal.h"
63
64#define NFSDBG_FACILITY NFSDBG_VFS
65
Chuck Leverbf0fd762007-07-01 12:13:44 -040066enum {
67 /* Mount options that take no arguments */
68 Opt_soft, Opt_hard,
Chuck Leverbf0fd762007-07-01 12:13:44 -040069 Opt_posix, Opt_noposix,
70 Opt_cto, Opt_nocto,
71 Opt_ac, Opt_noac,
72 Opt_lock, Opt_nolock,
73 Opt_v2, Opt_v3,
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040074 Opt_udp, Opt_tcp, Opt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -040075 Opt_acl, Opt_noacl,
76 Opt_rdirplus, Opt_nordirplus,
Trond Myklebust75180df2007-05-16 16:53:28 -040077 Opt_sharecache, Opt_nosharecache,
Chuck 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,
Trond Myklebust7973c1f2008-07-15 17:58:14 -040094 Opt_lookupcache,
Chuck Leverbf0fd762007-07-01 12:13:44 -040095
Chuck Leverf45663c2008-06-24 19:28:02 -040096 /* Special mount options */
97 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -040098
99 Opt_err
100};
101
Steven Whitehousea447c092008-10-13 10:46:57 +0100102static const match_table_t nfs_mount_option_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400103 { Opt_userspace, "bg" },
104 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400105 { Opt_userspace, "retry=%s" },
106
Chuck Leverf45663c2008-06-24 19:28:02 -0400107 { Opt_sloppy, "sloppy" },
108
Chuck Leverbf0fd762007-07-01 12:13:44 -0400109 { Opt_soft, "soft" },
110 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400111 { Opt_deprecated, "intr" },
112 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400113 { Opt_posix, "posix" },
114 { Opt_noposix, "noposix" },
115 { Opt_cto, "cto" },
116 { Opt_nocto, "nocto" },
117 { Opt_ac, "ac" },
118 { Opt_noac, "noac" },
119 { Opt_lock, "lock" },
120 { Opt_nolock, "nolock" },
121 { Opt_v2, "v2" },
122 { Opt_v3, "v3" },
123 { Opt_udp, "udp" },
124 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400125 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400126 { Opt_acl, "acl" },
127 { Opt_noacl, "noacl" },
128 { Opt_rdirplus, "rdirplus" },
129 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400130 { Opt_sharecache, "sharecache" },
131 { Opt_nosharecache, "nosharecache" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400132
133 { Opt_port, "port=%u" },
134 { Opt_rsize, "rsize=%u" },
135 { Opt_wsize, "wsize=%u" },
136 { Opt_bsize, "bsize=%u" },
137 { Opt_timeo, "timeo=%u" },
138 { Opt_retrans, "retrans=%u" },
139 { Opt_acregmin, "acregmin=%u" },
140 { Opt_acregmax, "acregmax=%u" },
141 { Opt_acdirmin, "acdirmin=%u" },
142 { Opt_acdirmax, "acdirmax=%u" },
143 { Opt_actimeo, "actimeo=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400144 { Opt_namelen, "namlen=%u" },
145 { Opt_mountport, "mountport=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400146 { Opt_mountvers, "mountvers=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400147 { Opt_nfsvers, "nfsvers=%u" },
148 { Opt_nfsvers, "vers=%u" },
149
150 { Opt_sec, "sec=%s" },
151 { Opt_proto, "proto=%s" },
152 { Opt_mountproto, "mountproto=%s" },
153 { Opt_addr, "addr=%s" },
154 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500155 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400156 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400157
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400158 { Opt_lookupcache, "lookupcache=%s" },
159
Chuck Leverbf0fd762007-07-01 12:13:44 -0400160 { Opt_err, NULL }
161};
162
163enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400164 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400165
166 Opt_xprt_err
167};
168
Steven Whitehousea447c092008-10-13 10:46:57 +0100169static const match_table_t nfs_xprt_protocol_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400170 { Opt_xprt_udp, "udp" },
171 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400172 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400173
174 { Opt_xprt_err, NULL }
175};
176
177enum {
178 Opt_sec_none, Opt_sec_sys,
179 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
180 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
181 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
182
183 Opt_sec_err
184};
185
Steven Whitehousea447c092008-10-13 10:46:57 +0100186static const match_table_t nfs_secflavor_tokens = {
Chuck Leverbf0fd762007-07-01 12:13:44 -0400187 { Opt_sec_none, "none" },
188 { Opt_sec_none, "null" },
189 { Opt_sec_sys, "sys" },
190
191 { Opt_sec_krb5, "krb5" },
192 { Opt_sec_krb5i, "krb5i" },
193 { Opt_sec_krb5p, "krb5p" },
194
195 { Opt_sec_lkey, "lkey" },
196 { Opt_sec_lkeyi, "lkeyi" },
197 { Opt_sec_lkeyp, "lkeyp" },
198
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500199 { Opt_sec_spkm, "spkm3" },
200 { Opt_sec_spkmi, "spkm3i" },
201 { Opt_sec_spkmp, "spkm3p" },
202
Chuck Leverbf0fd762007-07-01 12:13:44 -0400203 { Opt_sec_err, NULL }
204};
205
Trond Myklebust7973c1f2008-07-15 17:58:14 -0400206enum {
207 Opt_lookupcache_all, Opt_lookupcache_positive,
208 Opt_lookupcache_none,
209
210 Opt_lookupcache_err
211};
212
213static match_table_t nfs_lookupcache_tokens = {
214 { Opt_lookupcache_all, "all" },
215 { Opt_lookupcache_positive, "pos" },
216 { Opt_lookupcache_positive, "positive" },
217 { Opt_lookupcache_none, "none" },
218
219 { Opt_lookupcache_err, NULL }
220};
221
Chuck Leverbf0fd762007-07-01 12:13:44 -0400222
Al Viro42faad92008-04-24 07:21:56 -0400223static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400224static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400225static int nfs_show_options(struct seq_file *, struct vfsmount *);
226static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400227static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400228static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400229 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400230static void nfs_kill_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400231static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400232
233static struct file_system_type nfs_fs_type = {
234 .owner = THIS_MODULE,
235 .name = "nfs",
236 .get_sb = nfs_get_sb,
237 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700238 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400239};
240
David Howells54ceac42006-08-22 20:06:13 -0400241struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400242 .owner = THIS_MODULE,
243 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400244 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400245 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700246 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400247};
248
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800249static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400250 .alloc_inode = nfs_alloc_inode,
251 .destroy_inode = nfs_destroy_inode,
252 .write_inode = nfs_write_inode,
253 .statfs = nfs_statfs,
254 .clear_inode = nfs_clear_inode,
255 .umount_begin = nfs_umount_begin,
256 .show_options = nfs_show_options,
257 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400258 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400259};
260
261#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400262static int nfs4_get_sb(struct file_system_type *fs_type,
263 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400264static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
265 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
266static int nfs4_referral_get_sb(struct file_system_type *fs_type,
267 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400268static void nfs4_kill_super(struct super_block *sb);
269
270static struct file_system_type nfs4_fs_type = {
271 .owner = THIS_MODULE,
272 .name = "nfs4",
273 .get_sb = nfs4_get_sb,
274 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700275 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400276};
277
David Howells54ceac42006-08-22 20:06:13 -0400278struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400279 .owner = THIS_MODULE,
280 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400281 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400282 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700283 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400284};
285
David Howells54ceac42006-08-22 20:06:13 -0400286struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400287 .owner = THIS_MODULE,
288 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400289 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400290 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700291 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400292};
293
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800294static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400295 .alloc_inode = nfs_alloc_inode,
296 .destroy_inode = nfs_destroy_inode,
297 .write_inode = nfs_write_inode,
298 .statfs = nfs_statfs,
299 .clear_inode = nfs4_clear_inode,
300 .umount_begin = nfs_umount_begin,
301 .show_options = nfs_show_options,
302 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400303 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400304};
305#endif
306
Rusty Russell8e1f9362007-07-17 04:03:17 -0700307static struct shrinker acl_shrinker = {
308 .shrink = nfs_access_cache_shrinker,
309 .seeks = DEFAULT_SEEKS,
310};
Trond Myklebust979df722006-07-25 11:28:19 -0400311
David Howellsf7b422b2006-06-09 09:34:33 -0400312/*
313 * Register the NFS filesystems
314 */
315int __init register_nfs_fs(void)
316{
317 int ret;
318
319 ret = register_filesystem(&nfs_fs_type);
320 if (ret < 0)
321 goto error_0;
322
David Howellsf7b422b2006-06-09 09:34:33 -0400323 ret = nfs_register_sysctl();
324 if (ret < 0)
325 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800326#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400327 ret = register_filesystem(&nfs4_fs_type);
328 if (ret < 0)
329 goto error_2;
330#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700331 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400332 return 0;
333
334#ifdef CONFIG_NFS_V4
335error_2:
336 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800337#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400338error_1:
339 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400340error_0:
341 return ret;
342}
343
344/*
345 * Unregister the NFS filesystems
346 */
347void __exit unregister_nfs_fs(void)
348{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700349 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400350#ifdef CONFIG_NFS_V4
351 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400352#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700353 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400354 unregister_filesystem(&nfs_fs_type);
355}
356
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400357void nfs_sb_active(struct super_block *sb)
Steve Dicksonef818a22007-11-08 04:05:04 -0500358{
359 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400360
361 if (atomic_inc_return(&server->active) == 1)
362 atomic_inc(&sb->s_active);
363}
364
365void nfs_sb_deactive(struct super_block *sb)
366{
367 struct nfs_server *server = NFS_SB(sb);
368
369 if (atomic_dec_and_test(&server->active))
370 deactivate_super(sb);
Steve Dicksonef818a22007-11-08 04:05:04 -0500371}
372
David Howellsf7b422b2006-06-09 09:34:33 -0400373/*
374 * Deliver file system statistics to userspace
375 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400376static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400377{
David Howells0c7d90c2006-08-22 20:06:10 -0400378 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400379 unsigned char blockbits;
380 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400381 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400382 struct nfs_fattr fattr;
383 struct nfs_fsstat res = {
384 .fattr = &fattr,
385 };
386 int error;
387
David Howells8fa5c002006-08-22 20:06:12 -0400388 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400389 if (error < 0)
390 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700391 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400392
393 /*
394 * Current versions of glibc do not correctly handle the
395 * case where f_frsize != f_bsize. Eventually we want to
396 * report the value of wtmult in this field.
397 */
David Howells0c7d90c2006-08-22 20:06:10 -0400398 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400399
400 /*
401 * On most *nix systems, f_blocks, f_bfree, and f_bavail
402 * are reported in units of f_frsize. Linux hasn't had
403 * an f_frsize field in its statfs struct until recently,
404 * thus historically Linux's sys_statfs reports these
405 * fields in units of f_bsize.
406 */
David Howells0c7d90c2006-08-22 20:06:10 -0400407 buf->f_bsize = dentry->d_sb->s_blocksize;
408 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400409 blockres = (1 << blockbits) - 1;
410 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
411 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
412 buf->f_bavail = (res.abytes + blockres) >> blockbits;
413
414 buf->f_files = res.tfiles;
415 buf->f_ffree = res.afiles;
416
417 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700418
David Howellsf7b422b2006-06-09 09:34:33 -0400419 return 0;
420
421 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700422 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700423 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400424}
425
David Howells7d4e2742006-08-22 20:06:07 -0400426/*
427 * Map the security flavour number to a name
428 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400429static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
430{
David Howells7d4e2742006-08-22 20:06:07 -0400431 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400432 rpc_authflavor_t flavour;
433 const char *str;
434 } sec_flavours[] = {
435 { RPC_AUTH_NULL, "null" },
436 { RPC_AUTH_UNIX, "sys" },
437 { RPC_AUTH_GSS_KRB5, "krb5" },
438 { RPC_AUTH_GSS_KRB5I, "krb5i" },
439 { RPC_AUTH_GSS_KRB5P, "krb5p" },
440 { RPC_AUTH_GSS_LKEY, "lkey" },
441 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
442 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
443 { RPC_AUTH_GSS_SPKM, "spkm" },
444 { RPC_AUTH_GSS_SPKMI, "spkmi" },
445 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400446 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400447 };
448 int i;
449
Chuck Lever4d81cd12007-07-01 12:12:40 -0400450 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400451 if (sec_flavours[i].flavour == flavour)
452 break;
453 }
454 return sec_flavours[i].str;
455}
456
Chuck Lever82d101d2008-03-14 14:10:37 -0400457static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
458 int showdefaults)
459{
460 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
461
462 switch (sap->sa_family) {
463 case AF_INET: {
464 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
465 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
466 NIPQUAD(sin->sin_addr.s_addr));
467 break;
468 }
469 case AF_INET6: {
470 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700471 seq_printf(m, ",mountaddr=%pI6", &sin6->sin6_addr);
Chuck Lever82d101d2008-03-14 14:10:37 -0400472 break;
473 }
474 default:
475 if (showdefaults)
476 seq_printf(m, ",mountaddr=unspecified");
477 }
478
479 if (nfss->mountd_version || showdefaults)
480 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
481 if (nfss->mountd_port || showdefaults)
482 seq_printf(m, ",mountport=%u", nfss->mountd_port);
483
484 switch (nfss->mountd_protocol) {
485 case IPPROTO_UDP:
486 seq_printf(m, ",mountproto=udp");
487 break;
488 case IPPROTO_TCP:
489 seq_printf(m, ",mountproto=tcp");
490 break;
491 default:
492 if (showdefaults)
493 seq_printf(m, ",mountproto=auto");
494 }
495}
496
David Howellsf7b422b2006-06-09 09:34:33 -0400497/*
498 * Describe the mount options in force on this server representation
499 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400500static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
501 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400502{
David Howells509de812006-08-22 20:06:11 -0400503 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400504 int flag;
David Howells509de812006-08-22 20:06:11 -0400505 const char *str;
506 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400507 } nfs_info[] = {
508 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400509 { NFS_MOUNT_INTR, ",intr", ",nointr" },
510 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400511 { NFS_MOUNT_NOCTO, ",nocto", "" },
512 { NFS_MOUNT_NOAC, ",noac", "" },
513 { NFS_MOUNT_NONLM, ",nolock", "" },
514 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400515 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400516 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
David Howellsf7b422b2006-06-09 09:34:33 -0400517 { 0, NULL, NULL }
518 };
David Howells509de812006-08-22 20:06:11 -0400519 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400520 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400521 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400522
Chuck Lever82d101d2008-03-14 14:10:37 -0400523 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400524 seq_printf(m, ",rsize=%u", nfss->rsize);
525 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400526 if (nfss->bsize != 0)
527 seq_printf(m, ",bsize=%u", nfss->bsize);
528 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400529 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400530 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400531 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400532 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400533 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400534 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400535 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400536 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400537 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
538 if (nfss->flags & nfs_infop->flag)
539 seq_puts(m, nfs_infop->str);
540 else
541 seq_puts(m, nfs_infop->nostr);
542 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400543 seq_printf(m, ",proto=%s",
544 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400545 if (version == 4) {
546 if (nfss->port != NFS_PORT)
547 seq_printf(m, ",port=%u", nfss->port);
548 } else
549 if (nfss->port)
550 seq_printf(m, ",port=%u", nfss->port);
551
Trond Myklebust33170232007-12-20 16:03:59 -0500552 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
553 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400554 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400555
556 if (version != 4)
557 nfs_show_mountd_options(m, nfss, showdefaults);
558
559#ifdef CONFIG_NFS_V4
560 if (clp->rpc_ops->version == 4)
561 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
562#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400563}
564
565/*
566 * Describe the mount options on this VFS mountpoint
567 */
568static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
569{
570 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
571
572 nfs_show_mount_options(m, nfss, 0);
573
Chuck Lever5d8515c2007-12-10 14:57:16 -0500574 seq_printf(m, ",addr=%s",
575 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
576 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400577
578 return 0;
579}
580
581/*
582 * Present statistical information for this VFS mountpoint
583 */
584static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
585{
586 int i, cpu;
587 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
588 struct rpc_auth *auth = nfss->client->cl_auth;
589 struct nfs_iostats totals = { };
590
591 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
592
593 /*
594 * Display all mount option settings
595 */
596 seq_printf(m, "\n\topts:\t");
597 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
598 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
599 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
600 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
601 nfs_show_mount_options(m, nfss, 1);
602
603 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
604
605 seq_printf(m, "\n\tcaps:\t");
606 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400607 seq_printf(m, ",wtmult=%u", nfss->wtmult);
608 seq_printf(m, ",dtsize=%u", nfss->dtsize);
609 seq_printf(m, ",bsize=%u", nfss->bsize);
610 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400611
612#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500613 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400614 seq_printf(m, "\n\tnfsv4:\t");
615 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
616 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
617 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
618 }
619#endif
620
621 /*
622 * Display security flavor in effect for this mount
623 */
Chuck Lever2d767432008-03-14 14:10:08 -0400624 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400625 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400626 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400627
628 /*
629 * Display superblock I/O counters
630 */
631 for_each_possible_cpu(cpu) {
632 struct nfs_iostats *stats;
633
634 preempt_disable();
635 stats = per_cpu_ptr(nfss->io_stats, cpu);
636
637 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
638 totals.events[i] += stats->events[i];
639 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
640 totals.bytes[i] += stats->bytes[i];
641
642 preempt_enable();
643 }
644
645 seq_printf(m, "\n\tevents:\t");
646 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
647 seq_printf(m, "%lu ", totals.events[i]);
648 seq_printf(m, "\n\tbytes:\t");
649 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
650 seq_printf(m, "%Lu ", totals.bytes[i]);
651 seq_printf(m, "\n");
652
653 rpc_print_iostats(m, nfss->client);
654
655 return 0;
656}
657
658/*
659 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400660 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400661 */
Al Viro42faad92008-04-24 07:21:56 -0400662static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400663{
Al Viro42faad92008-04-24 07:21:56 -0400664 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400665 struct rpc_clnt *rpc;
666
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400667 /* -EIO all pending I/O */
668 rpc = server->client_acl;
669 if (!IS_ERR(rpc))
670 rpc_killall_tasks(rpc);
671 rpc = server->client;
672 if (!IS_ERR(rpc))
673 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400674}
675
676/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500677 * Sanity-check a server address provided by the mount command.
678 *
679 * Address family must be initialized, and address must not be
680 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400681 */
682static int nfs_verify_server_address(struct sockaddr *addr)
683{
684 switch (addr->sa_family) {
685 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500686 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700687 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500688 }
689 case AF_INET6: {
690 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
691 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400692 }
693 }
694
695 return 0;
696}
697
Chuck Leverce3b7e12008-06-23 12:36:53 -0400698static void nfs_parse_ipv4_address(char *string, size_t str_len,
699 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500700{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400701 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
702 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500703
Chuck Leverce3b7e12008-06-23 12:36:53 -0400704 if (str_len <= INET_ADDRSTRLEN) {
705 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
706 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500707
Chuck Leverce3b7e12008-06-23 12:36:53 -0400708 sin->sin_family = AF_INET;
709 *addr_len = sizeof(*sin);
710 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500711 return;
712 }
Chuck Lever9412b922007-12-10 14:59:21 -0500713
714 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400715 *addr_len = 0;
716}
717
718#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400719static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
720 const char *delim,
721 struct sockaddr_in6 *sin6)
Chuck Leverd8e77482008-06-23 12:37:01 -0400722{
723 char *p;
724 size_t len;
725
Chuck Lever5e2e7722008-10-08 15:38:10 -0400726 if ((string + str_len) == delim)
727 return 1;
728
Chuck Leverd8e77482008-06-23 12:37:01 -0400729 if (*delim != IPV6_SCOPE_DELIMITER)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400730 return 0;
731
732 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
733 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400734
735 len = (string + str_len) - delim - 1;
736 p = kstrndup(delim + 1, len, GFP_KERNEL);
737 if (p) {
738 unsigned long scope_id = 0;
739 struct net_device *dev;
740
741 dev = dev_get_by_name(&init_net, p);
742 if (dev != NULL) {
743 scope_id = dev->ifindex;
744 dev_put(dev);
745 } else {
Chuck Lever5e2e7722008-10-08 15:38:10 -0400746 if (strict_strtoul(p, 10, &scope_id) == 0) {
747 kfree(p);
748 return 0;
749 }
Chuck Leverd8e77482008-06-23 12:37:01 -0400750 }
751
752 kfree(p);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400753
Chuck Leverd8e77482008-06-23 12:37:01 -0400754 sin6->sin6_scope_id = scope_id;
755 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400756 return 1;
Chuck Leverd8e77482008-06-23 12:37:01 -0400757 }
Chuck Lever5e2e7722008-10-08 15:38:10 -0400758
759 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400760}
761
Chuck Leverce3b7e12008-06-23 12:36:53 -0400762static void nfs_parse_ipv6_address(char *string, size_t str_len,
763 struct sockaddr *sap, size_t *addr_len)
764{
765 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
766 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400767 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400768
769 if (str_len <= INET6_ADDRSTRLEN) {
770 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
771 (int)str_len, string);
772
773 sin6->sin6_family = AF_INET6;
774 *addr_len = sizeof(*sin6);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400775 if (in6_pton(string, str_len, addr,
776 IPV6_SCOPE_DELIMITER, &delim) != 0) {
777 if (nfs_parse_ipv6_scope_id(string, str_len,
778 delim, sin6) != 0)
779 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400780 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400781 }
782
783 sap->sa_family = AF_UNSPEC;
784 *addr_len = 0;
785}
786#else
787static void nfs_parse_ipv6_address(char *string, size_t str_len,
788 struct sockaddr *sap, size_t *addr_len)
789{
790 sap->sa_family = AF_UNSPEC;
791 *addr_len = 0;
792}
793#endif
794
795/*
796 * Construct a sockaddr based on the contents of a string that contains
797 * an IP address in presentation format.
798 *
799 * If there is a problem constructing the new sockaddr, set the address
800 * family to AF_UNSPEC.
801 */
J. Bruce Fieldsea31a4432008-08-20 16:10:23 -0400802void nfs_parse_ip_address(char *string, size_t str_len,
Chuck Leverce3b7e12008-06-23 12:36:53 -0400803 struct sockaddr *sap, size_t *addr_len)
804{
805 unsigned int i, colons;
806
807 colons = 0;
808 for (i = 0; i < str_len; i++)
809 if (string[i] == ':')
810 colons++;
811
812 if (colons >= 2)
813 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
814 else
815 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500816}
817
818/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400819 * Sanity check the NFS transport protocol.
820 *
821 */
822static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
823{
824 switch (mnt->nfs_server.protocol) {
825 case XPRT_TRANSPORT_UDP:
826 case XPRT_TRANSPORT_TCP:
827 case XPRT_TRANSPORT_RDMA:
828 break;
829 default:
830 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
831 }
832}
833
834/*
835 * For text based NFSv2/v3 mounts, the mount protocol transport default
836 * settings should depend upon the specified NFS transport.
837 */
838static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
839{
840 nfs_validate_transport_protocol(mnt);
841
842 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
843 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
844 return;
845 switch (mnt->nfs_server.protocol) {
846 case XPRT_TRANSPORT_UDP:
847 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
848 break;
849 case XPRT_TRANSPORT_TCP:
850 case XPRT_TRANSPORT_RDMA:
851 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
852 }
853}
854
855/*
Chuck Lever01060c82008-06-24 16:33:38 -0400856 * Parse the value of the 'sec=' option.
857 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400858 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400859 */
860static int nfs_parse_security_flavors(char *value,
861 struct nfs_parsed_mount_data *mnt)
862{
863 substring_t args[MAX_OPT_ARGS];
864
865 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
866
867 switch (match_token(value, nfs_secflavor_tokens, args)) {
868 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400869 mnt->auth_flavor_len = 0;
870 mnt->auth_flavors[0] = RPC_AUTH_NULL;
871 break;
872 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400873 mnt->auth_flavor_len = 0;
874 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
875 break;
876 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400877 mnt->auth_flavor_len = 1;
878 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
879 break;
880 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400881 mnt->auth_flavor_len = 1;
882 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
883 break;
884 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400885 mnt->auth_flavor_len = 1;
886 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
887 break;
888 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400889 mnt->auth_flavor_len = 1;
890 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
891 break;
892 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400893 mnt->auth_flavor_len = 1;
894 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
895 break;
896 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400897 mnt->auth_flavor_len = 1;
898 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
899 break;
900 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400901 mnt->auth_flavor_len = 1;
902 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
903 break;
904 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400905 mnt->auth_flavor_len = 1;
906 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
907 break;
908 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400909 mnt->auth_flavor_len = 1;
910 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
911 break;
912 default:
913 return 0;
914 }
915
916 return 1;
917}
918
Chuck Leverf45663c2008-06-24 19:28:02 -0400919static void nfs_parse_invalid_value(const char *option)
920{
921 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
922}
923
Chuck Lever01060c82008-06-24 16:33:38 -0400924/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400925 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400926 * a data structure. The whole mount string is processed; bad options are
927 * skipped as they are encountered. If there were no errors, return 1;
928 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400929 */
930static int nfs_parse_mount_options(char *raw,
931 struct nfs_parsed_mount_data *mnt)
932{
Eric Parisf9c3a382008-03-05 14:20:18 -0500933 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400934 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400935
936 if (!raw) {
937 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
938 return 1;
939 }
940 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
941
Eric Parisf9c3a382008-03-05 14:20:18 -0500942 secdata = alloc_secdata();
943 if (!secdata)
944 goto out_nomem;
945
946 rc = security_sb_copy_data(raw, secdata);
947 if (rc)
948 goto out_security_failure;
949
950 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
951 if (rc)
952 goto out_security_failure;
953
954 free_secdata(secdata);
955
Chuck Leverbf0fd762007-07-01 12:13:44 -0400956 while ((p = strsep(&raw, ",")) != NULL) {
957 substring_t args[MAX_OPT_ARGS];
958 int option, token;
959
960 if (!*p)
961 continue;
962
963 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
964
965 token = match_token(p, nfs_mount_option_tokens, args);
966 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400967
968 /*
969 * boolean options: foo/nofoo
970 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400971 case Opt_soft:
972 mnt->flags |= NFS_MOUNT_SOFT;
973 break;
974 case Opt_hard:
975 mnt->flags &= ~NFS_MOUNT_SOFT;
976 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400977 case Opt_posix:
978 mnt->flags |= NFS_MOUNT_POSIX;
979 break;
980 case Opt_noposix:
981 mnt->flags &= ~NFS_MOUNT_POSIX;
982 break;
983 case Opt_cto:
984 mnt->flags &= ~NFS_MOUNT_NOCTO;
985 break;
986 case Opt_nocto:
987 mnt->flags |= NFS_MOUNT_NOCTO;
988 break;
989 case Opt_ac:
990 mnt->flags &= ~NFS_MOUNT_NOAC;
991 break;
992 case Opt_noac:
993 mnt->flags |= NFS_MOUNT_NOAC;
994 break;
995 case Opt_lock:
996 mnt->flags &= ~NFS_MOUNT_NONLM;
997 break;
998 case Opt_nolock:
999 mnt->flags |= NFS_MOUNT_NONLM;
1000 break;
1001 case Opt_v2:
1002 mnt->flags &= ~NFS_MOUNT_VER3;
1003 break;
1004 case Opt_v3:
1005 mnt->flags |= NFS_MOUNT_VER3;
1006 break;
1007 case Opt_udp:
1008 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001009 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001010 break;
1011 case Opt_tcp:
1012 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001013 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001014 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001015 case Opt_rdma:
1016 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1017 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001018 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001019 case Opt_acl:
1020 mnt->flags &= ~NFS_MOUNT_NOACL;
1021 break;
1022 case Opt_noacl:
1023 mnt->flags |= NFS_MOUNT_NOACL;
1024 break;
1025 case Opt_rdirplus:
1026 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1027 break;
1028 case Opt_nordirplus:
1029 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1030 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001031 case Opt_sharecache:
1032 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1033 break;
1034 case Opt_nosharecache:
1035 mnt->flags |= NFS_MOUNT_UNSHARED;
1036 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001037
Chuck Leverf45663c2008-06-24 19:28:02 -04001038 /*
1039 * options that take numeric values
1040 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001041 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001042 if (match_int(args, &option) ||
1043 option < 0 || option > USHORT_MAX) {
1044 errors++;
1045 nfs_parse_invalid_value("port");
1046 } else
1047 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001048 break;
1049 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001050 if (match_int(args, &option) || option < 0) {
1051 errors++;
1052 nfs_parse_invalid_value("rsize");
1053 } else
1054 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001055 break;
1056 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001057 if (match_int(args, &option) || option < 0) {
1058 errors++;
1059 nfs_parse_invalid_value("wsize");
1060 } else
1061 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001062 break;
1063 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001064 if (match_int(args, &option) || option < 0) {
1065 errors++;
1066 nfs_parse_invalid_value("bsize");
1067 } else
1068 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001069 break;
1070 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001071 if (match_int(args, &option) || option <= 0) {
1072 errors++;
1073 nfs_parse_invalid_value("timeo");
1074 } else
1075 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001076 break;
1077 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001078 if (match_int(args, &option) || option <= 0) {
1079 errors++;
1080 nfs_parse_invalid_value("retrans");
1081 } else
1082 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001083 break;
1084 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001085 if (match_int(args, &option) || option < 0) {
1086 errors++;
1087 nfs_parse_invalid_value("acregmin");
1088 } else
1089 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001090 break;
1091 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001092 if (match_int(args, &option) || option < 0) {
1093 errors++;
1094 nfs_parse_invalid_value("acregmax");
1095 } else
1096 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001097 break;
1098 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001099 if (match_int(args, &option) || option < 0) {
1100 errors++;
1101 nfs_parse_invalid_value("acdirmin");
1102 } else
1103 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001104 break;
1105 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001106 if (match_int(args, &option) || option < 0) {
1107 errors++;
1108 nfs_parse_invalid_value("acdirmax");
1109 } else
1110 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001111 break;
1112 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001113 if (match_int(args, &option) || option < 0) {
1114 errors++;
1115 nfs_parse_invalid_value("actimeo");
1116 } else
1117 mnt->acregmin = mnt->acregmax =
1118 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001119 break;
1120 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001121 if (match_int(args, &option) || option < 0) {
1122 errors++;
1123 nfs_parse_invalid_value("namlen");
1124 } else
1125 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001126 break;
1127 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001128 if (match_int(args, &option) ||
1129 option < 0 || option > USHORT_MAX) {
1130 errors++;
1131 nfs_parse_invalid_value("mountport");
1132 } else
1133 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001134 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001135 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001136 if (match_int(args, &option) ||
1137 option < NFS_MNT_VERSION ||
1138 option > NFS_MNT3_VERSION) {
1139 errors++;
1140 nfs_parse_invalid_value("mountvers");
1141 } else
1142 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001143 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001144 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001145 if (match_int(args, &option)) {
1146 errors++;
1147 nfs_parse_invalid_value("nfsvers");
1148 break;
1149 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001150 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001151 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001152 mnt->flags &= ~NFS_MOUNT_VER3;
1153 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001154 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001155 mnt->flags |= NFS_MOUNT_VER3;
1156 break;
1157 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001158 errors++;
1159 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001160 }
1161 break;
1162
Chuck Leverf45663c2008-06-24 19:28:02 -04001163 /*
1164 * options that take text values
1165 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001166 case Opt_sec:
1167 string = match_strdup(args);
1168 if (string == NULL)
1169 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001170 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001171 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001172 if (!rc) {
1173 errors++;
1174 dfprintk(MOUNT, "NFS: unrecognized "
1175 "security flavor\n");
1176 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001177 break;
1178 case Opt_proto:
1179 string = match_strdup(args);
1180 if (string == NULL)
1181 goto out_nomem;
1182 token = match_token(string,
1183 nfs_xprt_protocol_tokens, args);
1184 kfree(string);
1185
1186 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001187 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001188 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001189 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001190 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001191 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001192 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001193 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001194 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001195 case Opt_xprt_rdma:
1196 /* vector side protocols to TCP */
1197 mnt->flags |= NFS_MOUNT_TCP;
1198 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001199 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001200 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001201 errors++;
1202 dfprintk(MOUNT, "NFS: unrecognized "
1203 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001204 }
1205 break;
1206 case Opt_mountproto:
1207 string = match_strdup(args);
1208 if (string == NULL)
1209 goto out_nomem;
1210 token = match_token(string,
1211 nfs_xprt_protocol_tokens, args);
1212 kfree(string);
1213
1214 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001215 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001216 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001217 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001218 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001219 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001220 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001221 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001222 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001223 errors++;
1224 dfprintk(MOUNT, "NFS: unrecognized "
1225 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001226 }
1227 break;
1228 case Opt_addr:
1229 string = match_strdup(args);
1230 if (string == NULL)
1231 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001232 nfs_parse_ip_address(string, strlen(string),
1233 (struct sockaddr *)
1234 &mnt->nfs_server.address,
1235 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001236 kfree(string);
1237 break;
1238 case Opt_clientaddr:
1239 string = match_strdup(args);
1240 if (string == NULL)
1241 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001242 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001243 mnt->client_address = string;
1244 break;
Chuck Lever33832032007-12-10 14:59:13 -05001245 case Opt_mounthost:
1246 string = match_strdup(args);
1247 if (string == NULL)
1248 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001249 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001250 mnt->mount_server.hostname = string;
1251 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001252 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001253 string = match_strdup(args);
1254 if (string == NULL)
1255 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001256 nfs_parse_ip_address(string, strlen(string),
1257 (struct sockaddr *)
1258 &mnt->mount_server.address,
1259 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001260 kfree(string);
1261 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001262 case Opt_lookupcache:
1263 string = match_strdup(args);
1264 if (string == NULL)
1265 goto out_nomem;
1266 token = match_token(string,
1267 nfs_lookupcache_tokens, args);
1268 kfree(string);
1269 switch (token) {
1270 case Opt_lookupcache_all:
1271 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1272 break;
1273 case Opt_lookupcache_positive:
1274 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1275 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1276 break;
1277 case Opt_lookupcache_none:
1278 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1279 break;
1280 default:
1281 errors++;
1282 dfprintk(MOUNT, "NFS: invalid "
1283 "lookupcache argument\n");
1284 };
1285 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001286
Chuck Leverf45663c2008-06-24 19:28:02 -04001287 /*
1288 * Special options
1289 */
1290 case Opt_sloppy:
1291 sloppy = 1;
1292 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1293 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001294 case Opt_userspace:
1295 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001296 dfprintk(MOUNT, "NFS: ignoring mount option "
1297 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001298 break;
1299
1300 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001301 errors++;
1302 dfprintk(MOUNT, "NFS: unrecognized mount option "
1303 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001304 }
1305 }
1306
Chuck Leveraf904de2008-09-08 11:58:13 -04001307 if (errors > 0) {
1308 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1309 errors, (errors == 1 ? "" : "s"));
1310 if (!sloppy)
1311 return 0;
1312 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001313 return 1;
1314
1315out_nomem:
1316 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1317 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001318out_security_failure:
1319 free_secdata(secdata);
1320 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1321 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001322}
1323
1324/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001325 * Use the remote server's MOUNT service to request the NFS file handle
1326 * corresponding to the provided path.
1327 */
1328static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1329 struct nfs_fh *root_fh)
1330{
Chuck Lever4c568012007-12-10 14:59:28 -05001331 struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
Chuck Lever33832032007-12-10 14:59:13 -05001332 char *hostname;
Chuck Lever4c568012007-12-10 14:59:28 -05001333 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001334
1335 if (args->mount_server.version == 0) {
1336 if (args->flags & NFS_MOUNT_VER3)
1337 args->mount_server.version = NFS_MNT3_VERSION;
1338 else
1339 args->mount_server.version = NFS_MNT_VERSION;
1340 }
1341
Chuck Lever33832032007-12-10 14:59:13 -05001342 if (args->mount_server.hostname)
1343 hostname = args->mount_server.hostname;
1344 else
1345 hostname = args->nfs_server.hostname;
1346
Chuck Lever0076d7b2007-07-01 12:13:49 -04001347 /*
1348 * Construct the mount server's address.
1349 */
Chuck Lever4c568012007-12-10 14:59:28 -05001350 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1351 memcpy(sap, &args->nfs_server.address,
1352 args->nfs_server.addrlen);
1353 args->mount_server.addrlen = args->nfs_server.addrlen;
1354 }
1355
James Lentiniaad70002007-09-24 17:32:49 -04001356 /*
1357 * autobind will be used if mount_server.port == 0
1358 */
Chuck Lever4c568012007-12-10 14:59:28 -05001359 nfs_set_port(sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001360
1361 /*
1362 * Now ask the mount server to map our export path
1363 * to a file handle.
1364 */
Chuck Lever4c568012007-12-10 14:59:28 -05001365 status = nfs_mount(sap,
1366 args->mount_server.addrlen,
Chuck Lever33832032007-12-10 14:59:13 -05001367 hostname,
Chuck Lever0076d7b2007-07-01 12:13:49 -04001368 args->nfs_server.export_path,
1369 args->mount_server.version,
1370 args->mount_server.protocol,
1371 root_fh);
Chuck Leverefd83402007-09-11 18:00:58 -04001372 if (status == 0)
1373 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001374
Chuck Lever396cee972008-06-12 12:37:49 -04001375 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Lever33832032007-12-10 14:59:13 -05001376 hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001377 return status;
1378}
1379
Chuck Leverd1aa0822008-06-23 12:36:45 -04001380static int nfs_parse_simple_hostname(const char *dev_name,
1381 char **hostname, size_t maxnamlen,
1382 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001383{
1384 size_t len;
1385 char *colon, *comma;
1386
1387 colon = strchr(dev_name, ':');
1388 if (colon == NULL)
1389 goto out_bad_devname;
1390
1391 len = colon - dev_name;
1392 if (len > maxnamlen)
1393 goto out_hostname;
1394
1395 /* N.B. caller will free nfs_server.hostname in all cases */
1396 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1397 if (!*hostname)
1398 goto out_nomem;
1399
1400 /* kill possible hostname list: not supported */
1401 comma = strchr(*hostname, ',');
1402 if (comma != NULL) {
1403 if (comma == *hostname)
1404 goto out_bad_devname;
1405 *comma = '\0';
1406 }
1407
1408 colon++;
1409 len = strlen(colon);
1410 if (len > maxpathlen)
1411 goto out_path;
1412 *export_path = kstrndup(colon, len, GFP_KERNEL);
1413 if (!*export_path)
1414 goto out_nomem;
1415
1416 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1417 return 0;
1418
1419out_bad_devname:
1420 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1421 return -EINVAL;
1422
1423out_nomem:
1424 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1425 return -ENOMEM;
1426
1427out_hostname:
1428 dfprintk(MOUNT, "NFS: server hostname too long\n");
1429 return -ENAMETOOLONG;
1430
1431out_path:
1432 dfprintk(MOUNT, "NFS: export pathname too long\n");
1433 return -ENAMETOOLONG;
1434}
1435
1436/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001437 * Hostname has square brackets around it because it contains one or
1438 * more colons. We look for the first closing square bracket, and a
1439 * colon must follow it.
1440 */
1441static int nfs_parse_protected_hostname(const char *dev_name,
1442 char **hostname, size_t maxnamlen,
1443 char **export_path, size_t maxpathlen)
1444{
1445 size_t len;
1446 char *start, *end;
1447
1448 start = (char *)(dev_name + 1);
1449
1450 end = strchr(start, ']');
1451 if (end == NULL)
1452 goto out_bad_devname;
1453 if (*(end + 1) != ':')
1454 goto out_bad_devname;
1455
1456 len = end - start;
1457 if (len > maxnamlen)
1458 goto out_hostname;
1459
1460 /* N.B. caller will free nfs_server.hostname in all cases */
1461 *hostname = kstrndup(start, len, GFP_KERNEL);
1462 if (*hostname == NULL)
1463 goto out_nomem;
1464
1465 end += 2;
1466 len = strlen(end);
1467 if (len > maxpathlen)
1468 goto out_path;
1469 *export_path = kstrndup(end, len, GFP_KERNEL);
1470 if (!*export_path)
1471 goto out_nomem;
1472
1473 return 0;
1474
1475out_bad_devname:
1476 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1477 return -EINVAL;
1478
1479out_nomem:
1480 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1481 return -ENOMEM;
1482
1483out_hostname:
1484 dfprintk(MOUNT, "NFS: server hostname too long\n");
1485 return -ENAMETOOLONG;
1486
1487out_path:
1488 dfprintk(MOUNT, "NFS: export pathname too long\n");
1489 return -ENAMETOOLONG;
1490}
1491
1492/*
1493 * Split "dev_name" into "hostname:export_path".
1494 *
1495 * The leftmost colon demarks the split between the server's hostname
1496 * and the export path. If the hostname starts with a left square
1497 * bracket, then it may contain colons.
1498 *
1499 * Note: caller frees hostname and export path, even on error.
1500 */
1501static int nfs_parse_devname(const char *dev_name,
1502 char **hostname, size_t maxnamlen,
1503 char **export_path, size_t maxpathlen)
1504{
1505 if (*dev_name == '[')
1506 return nfs_parse_protected_hostname(dev_name,
1507 hostname, maxnamlen,
1508 export_path, maxpathlen);
1509
1510 return nfs_parse_simple_hostname(dev_name,
1511 hostname, maxnamlen,
1512 export_path, maxpathlen);
1513}
1514
1515/*
David Howells54ceac42006-08-22 20:06:13 -04001516 * Validate the NFS2/NFS3 mount data
1517 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001518 *
1519 * For option strings, user space handles the following behaviors:
1520 *
1521 * + DNS: mapping server host name to IP address ("addr=" option)
1522 *
1523 * + failure mode: how to behave if a mount request can't be handled
1524 * immediately ("fg/bg" option)
1525 *
1526 * + retry: how often to retry a mount request ("retry=" option)
1527 *
1528 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1529 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001530 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001531static int nfs_validate_mount_data(void *options,
1532 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001533 struct nfs_fh *mntfh,
1534 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001535{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001536 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001537
Chuck Lever5df36e72007-07-01 12:12:56 -04001538 if (data == NULL)
1539 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001540
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001541 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1542 args->rsize = NFS_MAX_FILE_IO_SIZE;
1543 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001544 args->acregmin = NFS_DEF_ACREGMIN;
1545 args->acregmax = NFS_DEF_ACREGMAX;
1546 args->acdirmin = NFS_DEF_ACDIRMIN;
1547 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001548 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001549 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001550 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001551 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001552
David Howellsf7b422b2006-06-09 09:34:33 -04001553 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001554 case 1:
1555 data->namlen = 0;
1556 case 2:
1557 data->bsize = 0;
1558 case 3:
1559 if (data->flags & NFS_MOUNT_VER3)
1560 goto out_no_v3;
1561 data->root.size = NFS2_FHSIZE;
1562 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1563 case 4:
1564 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1565 goto out_no_sec;
1566 case 5:
1567 memset(data->context, 0, sizeof(data->context));
1568 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001569 if (data->flags & NFS_MOUNT_VER3) {
1570 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1571 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001572 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001573 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001574 mntfh->size = NFS2_FHSIZE;
1575
Chuck Lever5df36e72007-07-01 12:12:56 -04001576
1577 memcpy(mntfh->data, data->root.data, mntfh->size);
1578 if (mntfh->size < sizeof(mntfh->data))
1579 memset(mntfh->data + mntfh->size, 0,
1580 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001581
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001582 /*
1583 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1584 * can deal with.
1585 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001586 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001587 args->rsize = data->rsize;
1588 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001589 args->timeo = data->timeo;
1590 args->retrans = data->retrans;
1591 args->acregmin = data->acregmin;
1592 args->acregmax = data->acregmax;
1593 args->acdirmin = data->acdirmin;
1594 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001595
1596 memcpy(&args->nfs_server.address, &data->addr,
1597 sizeof(data->addr));
1598 args->nfs_server.addrlen = sizeof(data->addr);
1599 if (!nfs_verify_server_address((struct sockaddr *)
1600 &args->nfs_server.address))
1601 goto out_no_address;
1602
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001603 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001604 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001605 /* N.B. caller will free nfs_server.hostname in all cases */
1606 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1607 args->namlen = data->namlen;
1608 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001609
1610 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1611 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001612 if (!args->nfs_server.hostname)
1613 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001614
1615 /*
1616 * The legacy version 6 binary mount data from userspace has a
1617 * field used only to transport selinux information into the
1618 * the kernel. To continue to support that functionality we
1619 * have a touch of selinux knowledge here in the NFS code. The
1620 * userspace code converted context=blah to just blah so we are
1621 * converting back to the full string selinux understands.
1622 */
1623 if (data->context[0]){
1624#ifdef CONFIG_SECURITY_SELINUX
1625 int rc;
1626 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1627 if (!opts_str)
1628 return -ENOMEM;
1629 strcpy(opts_str, "context=");
1630 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1631 strcat(opts_str, &data->context[0]);
1632 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1633 kfree(opts_str);
1634 if (rc)
1635 return rc;
1636#else
1637 return -EINVAL;
1638#endif
1639 }
1640
Chuck Lever5df36e72007-07-01 12:12:56 -04001641 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001642 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001643 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001644
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001645 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001646 return -EINVAL;
1647
Chuck Lever6e88e062007-09-24 15:39:50 -04001648 if (!nfs_verify_server_address((struct sockaddr *)
1649 &args->nfs_server.address))
1650 goto out_no_address;
1651
Chuck Levered596a82008-06-26 17:47:05 -04001652 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1653 args->nfs_server.port);
1654
Trond Myklebust259875e2008-07-02 14:43:47 -04001655 nfs_set_mount_transport_protocol(args);
1656
Chuck Leverdc045892008-06-23 12:36:37 -04001657 status = nfs_parse_devname(dev_name,
1658 &args->nfs_server.hostname,
1659 PAGE_SIZE,
1660 &args->nfs_server.export_path,
1661 NFS_MAXPATHLEN);
1662 if (!status)
1663 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001664
Chuck Leverdc045892008-06-23 12:36:37 -04001665 kfree(args->nfs_server.export_path);
1666 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001667
Chuck Lever136d5582007-07-01 12:13:54 -04001668 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001669 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001670
Chuck Lever136d5582007-07-01 12:13:54 -04001671 break;
1672 }
David Howellsf7b422b2006-06-09 09:34:33 -04001673 }
David Howells54ceac42006-08-22 20:06:13 -04001674
David Howellsf7b422b2006-06-09 09:34:33 -04001675#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001676 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001677 goto out_v3_not_compiled;
1678#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001679
David Howells54ceac42006-08-22 20:06:13 -04001680 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001681
1682out_no_data:
1683 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1684 return -EINVAL;
1685
1686out_no_v3:
1687 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1688 data->version);
1689 return -EINVAL;
1690
1691out_no_sec:
1692 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1693 return -EINVAL;
1694
Chuck Lever5df36e72007-07-01 12:12:56 -04001695#ifndef CONFIG_NFS_V3
1696out_v3_not_compiled:
1697 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1698 return -EPROTONOSUPPORT;
1699#endif /* !CONFIG_NFS_V3 */
1700
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001701out_nomem:
1702 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1703 return -ENOMEM;
1704
Chuck Lever5df36e72007-07-01 12:12:56 -04001705out_no_address:
1706 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1707 return -EINVAL;
1708
1709out_invalid_fh:
1710 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1711 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001712}
1713
Jeff Layton48b605f2008-06-10 15:38:39 -04001714static int
1715nfs_compare_remount_data(struct nfs_server *nfss,
1716 struct nfs_parsed_mount_data *data)
1717{
1718 if (data->flags != nfss->flags ||
1719 data->rsize != nfss->rsize ||
1720 data->wsize != nfss->wsize ||
1721 data->retrans != nfss->client->cl_timeout->to_retries ||
1722 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1723 data->acregmin != nfss->acregmin / HZ ||
1724 data->acregmax != nfss->acregmax / HZ ||
1725 data->acdirmin != nfss->acdirmin / HZ ||
1726 data->acdirmax != nfss->acdirmax / HZ ||
1727 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1728 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1729 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1730 data->nfs_server.addrlen) != 0)
1731 return -EINVAL;
1732
1733 return 0;
1734}
1735
1736static int
1737nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1738{
1739 int error;
1740 struct nfs_server *nfss = sb->s_fs_info;
1741 struct nfs_parsed_mount_data *data;
1742 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1743 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001744 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001745
1746 /*
1747 * Userspace mount programs that send binary options generally send
1748 * them populated with default values. We have no way to know which
1749 * ones were explicitly specified. Fall back to legacy behavior and
1750 * just return success.
1751 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001752 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1753 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1754 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001755 return 0;
1756
1757 data = kzalloc(sizeof(*data), GFP_KERNEL);
1758 if (data == NULL)
1759 return -ENOMEM;
1760
1761 /* fill out struct with values from existing mount */
1762 data->flags = nfss->flags;
1763 data->rsize = nfss->rsize;
1764 data->wsize = nfss->wsize;
1765 data->retrans = nfss->client->cl_timeout->to_retries;
1766 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1767 data->acregmin = nfss->acregmin / HZ;
1768 data->acregmax = nfss->acregmax / HZ;
1769 data->acdirmin = nfss->acdirmin / HZ;
1770 data->acdirmax = nfss->acdirmax / HZ;
1771 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1772 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1773 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1774 data->nfs_server.addrlen);
1775
1776 /* overwrite those values with any that were specified */
1777 error = nfs_parse_mount_options((char *)options, data);
1778 if (error < 0)
1779 goto out;
1780
1781 /* compare new mount options with old ones */
1782 error = nfs_compare_remount_data(nfss, data);
1783out:
1784 kfree(data);
1785 return error;
1786}
1787
David Howells54ceac42006-08-22 20:06:13 -04001788/*
1789 * Initialise the common bits of the superblock
1790 */
1791static inline void nfs_initialise_sb(struct super_block *sb)
1792{
1793 struct nfs_server *server = NFS_SB(sb);
1794
1795 sb->s_magic = NFS_SUPER_MAGIC;
1796
1797 /* We probably want something more informative here */
1798 snprintf(sb->s_id, sizeof(sb->s_id),
1799 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1800
1801 if (sb->s_blocksize == 0)
1802 sb->s_blocksize = nfs_block_bits(server->wsize,
1803 &sb->s_blocksize_bits);
1804
1805 if (server->flags & NFS_MOUNT_NOAC)
1806 sb->s_flags |= MS_SYNCHRONOUS;
1807
1808 nfs_super_set_maxbytes(sb, server->maxfilesize);
1809}
1810
1811/*
1812 * Finish setting up an NFS2/3 superblock
1813 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001814static void nfs_fill_super(struct super_block *sb,
1815 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001816{
1817 struct nfs_server *server = NFS_SB(sb);
1818
1819 sb->s_blocksize_bits = 0;
1820 sb->s_blocksize = 0;
1821 if (data->bsize)
1822 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1823
1824 if (server->flags & NFS_MOUNT_VER3) {
1825 /* The VFS shouldn't apply the umask to mode bits. We will do
1826 * so ourselves when necessary.
1827 */
1828 sb->s_flags |= MS_POSIXACL;
1829 sb->s_time_gran = 1;
1830 }
1831
1832 sb->s_op = &nfs_sops;
1833 nfs_initialise_sb(sb);
1834}
1835
1836/*
1837 * Finish setting up a cloned NFS2/3 superblock
1838 */
1839static void nfs_clone_super(struct super_block *sb,
1840 const struct super_block *old_sb)
1841{
1842 struct nfs_server *server = NFS_SB(sb);
1843
1844 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1845 sb->s_blocksize = old_sb->s_blocksize;
1846 sb->s_maxbytes = old_sb->s_maxbytes;
1847
1848 if (server->flags & NFS_MOUNT_VER3) {
1849 /* The VFS shouldn't apply the umask to mode bits. We will do
1850 * so ourselves when necessary.
1851 */
1852 sb->s_flags |= MS_POSIXACL;
1853 sb->s_time_gran = 1;
1854 }
1855
1856 sb->s_op = old_sb->s_op;
1857 nfs_initialise_sb(sb);
1858}
1859
Trond Myklebust275a5d22007-05-16 16:53:28 -04001860#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1861
1862static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1863{
1864 const struct nfs_server *a = s->s_fs_info;
1865 const struct rpc_clnt *clnt_a = a->client;
1866 const struct rpc_clnt *clnt_b = b->client;
1867
1868 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1869 goto Ebusy;
1870 if (a->nfs_client != b->nfs_client)
1871 goto Ebusy;
1872 if (a->flags != b->flags)
1873 goto Ebusy;
1874 if (a->wsize != b->wsize)
1875 goto Ebusy;
1876 if (a->rsize != b->rsize)
1877 goto Ebusy;
1878 if (a->acregmin != b->acregmin)
1879 goto Ebusy;
1880 if (a->acregmax != b->acregmax)
1881 goto Ebusy;
1882 if (a->acdirmin != b->acdirmin)
1883 goto Ebusy;
1884 if (a->acdirmax != b->acdirmax)
1885 goto Ebusy;
1886 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1887 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001888 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001889Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001890 return 0;
1891}
1892
1893struct nfs_sb_mountdata {
1894 struct nfs_server *server;
1895 int mntflags;
1896};
1897
1898static int nfs_set_super(struct super_block *s, void *data)
1899{
1900 struct nfs_sb_mountdata *sb_mntdata = data;
1901 struct nfs_server *server = sb_mntdata->server;
1902 int ret;
1903
1904 s->s_flags = sb_mntdata->mntflags;
1905 s->s_fs_info = server;
1906 ret = set_anon_super(s, server);
1907 if (ret == 0)
1908 server->s_dev = s->s_dev;
1909 return ret;
1910}
1911
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001912static int nfs_compare_super_address(struct nfs_server *server1,
1913 struct nfs_server *server2)
1914{
1915 struct sockaddr *sap1, *sap2;
1916
1917 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1918 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1919
1920 if (sap1->sa_family != sap2->sa_family)
1921 return 0;
1922
1923 switch (sap1->sa_family) {
1924 case AF_INET: {
1925 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1926 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1927 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1928 return 0;
1929 if (sin1->sin_port != sin2->sin_port)
1930 return 0;
1931 break;
1932 }
1933 case AF_INET6: {
1934 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1935 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1936 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1937 return 0;
1938 if (sin1->sin6_port != sin2->sin6_port)
1939 return 0;
1940 break;
1941 }
1942 default:
1943 return 0;
1944 }
1945
1946 return 1;
1947}
1948
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001949static int nfs_compare_super(struct super_block *sb, void *data)
1950{
1951 struct nfs_sb_mountdata *sb_mntdata = data;
1952 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1953 int mntflags = sb_mntdata->mntflags;
1954
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001955 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001956 return 0;
1957 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1958 if (old->flags & NFS_MOUNT_UNSHARED)
1959 return 0;
1960 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1961 return 0;
1962 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001963}
1964
Miklos Szeredifa799752008-04-30 00:54:33 -07001965static int nfs_bdi_register(struct nfs_server *server)
1966{
1967 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1968}
1969
David Howells54ceac42006-08-22 20:06:13 -04001970static int nfs_get_sb(struct file_system_type *fs_type,
1971 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1972{
1973 struct nfs_server *server = NULL;
1974 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001975 struct nfs_parsed_mount_data *data;
1976 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001977 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001978 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001979 struct nfs_sb_mountdata sb_mntdata = {
1980 .mntflags = flags,
1981 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001982 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001983
Trond Myklebust33852a12008-06-19 14:20:11 -04001984 data = kzalloc(sizeof(*data), GFP_KERNEL);
1985 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1986 if (data == NULL || mntfh == NULL)
1987 goto out_free_fh;
1988
1989 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001990
David Howells54ceac42006-08-22 20:06:13 -04001991 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04001992 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04001993 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001994 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001995
1996 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001997 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001998 if (IS_ERR(server)) {
1999 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002000 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002001 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002002 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002003
Trond Myklebust75180df2007-05-16 16:53:28 -04002004 if (server->flags & NFS_MOUNT_UNSHARED)
2005 compare_super = NULL;
2006
David Howells54ceac42006-08-22 20:06:13 -04002007 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002008 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002009 if (IS_ERR(s)) {
2010 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002011 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002012 }
2013
David Howells54ceac42006-08-22 20:06:13 -04002014 if (s->s_fs_info != server) {
2015 nfs_free_server(server);
2016 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002017 } else {
2018 error = nfs_bdi_register(server);
2019 if (error)
2020 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002021 }
David Howells54ceac42006-08-22 20:06:13 -04002022
2023 if (!s->s_root) {
2024 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002025 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04002026 }
2027
Trond Myklebust33852a12008-06-19 14:20:11 -04002028 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002029 if (IS_ERR(mntroot)) {
2030 error = PTR_ERR(mntroot);
2031 goto error_splat_super;
2032 }
2033
Trond Myklebust33852a12008-06-19 14:20:11 -04002034 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002035 if (error)
2036 goto error_splat_root;
2037
David Howellsf7b422b2006-06-09 09:34:33 -04002038 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002039 mnt->mnt_sb = s;
2040 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002041 error = 0;
2042
2043out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002044 kfree(data->nfs_server.hostname);
2045 kfree(data->mount_server.hostname);
2046 security_free_mnt_opts(&data->lsm_opts);
2047out_free_fh:
2048 kfree(mntfh);
2049 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002050 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002051
David Howells54ceac42006-08-22 20:06:13 -04002052out_err_nosb:
2053 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002054 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002055
Eric Parisf9c3a382008-03-05 14:20:18 -05002056error_splat_root:
2057 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002058error_splat_super:
2059 up_write(&s->s_umount);
2060 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002061 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002062}
2063
David Howells54ceac42006-08-22 20:06:13 -04002064/*
2065 * Destroy an NFS2/3 superblock
2066 */
David Howellsf7b422b2006-06-09 09:34:33 -04002067static void nfs_kill_super(struct super_block *s)
2068{
2069 struct nfs_server *server = NFS_SB(s);
2070
Miklos Szeredifa799752008-04-30 00:54:33 -07002071 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002072 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002073 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002074}
2075
David Howells54ceac42006-08-22 20:06:13 -04002076/*
2077 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2078 */
2079static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2080 const char *dev_name, void *raw_data,
2081 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002082{
2083 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002084 struct super_block *s;
2085 struct nfs_server *server;
2086 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002087 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002088 struct nfs_sb_mountdata sb_mntdata = {
2089 .mntflags = flags,
2090 };
David Howells54ceac42006-08-22 20:06:13 -04002091 int error;
2092
2093 dprintk("--> nfs_xdev_get_sb()\n");
2094
2095 /* create a new volume representation */
2096 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2097 if (IS_ERR(server)) {
2098 error = PTR_ERR(server);
2099 goto out_err_noserver;
2100 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002101 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002102
Trond Myklebust75180df2007-05-16 16:53:28 -04002103 if (server->flags & NFS_MOUNT_UNSHARED)
2104 compare_super = NULL;
2105
David Howells54ceac42006-08-22 20:06:13 -04002106 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002107 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002108 if (IS_ERR(s)) {
2109 error = PTR_ERR(s);
2110 goto out_err_nosb;
2111 }
2112
2113 if (s->s_fs_info != server) {
2114 nfs_free_server(server);
2115 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002116 } else {
2117 error = nfs_bdi_register(server);
2118 if (error)
2119 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002120 }
2121
2122 if (!s->s_root) {
2123 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002124 nfs_clone_super(s, data->sb);
2125 }
2126
2127 mntroot = nfs_get_root(s, data->fh);
2128 if (IS_ERR(mntroot)) {
2129 error = PTR_ERR(mntroot);
2130 goto error_splat_super;
2131 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002132 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002133 dput(mntroot);
2134 error = -ESTALE;
2135 goto error_splat_super;
2136 }
David Howells54ceac42006-08-22 20:06:13 -04002137
2138 s->s_flags |= MS_ACTIVE;
2139 mnt->mnt_sb = s;
2140 mnt->mnt_root = mntroot;
2141
Eric Parisf9c3a382008-03-05 14:20:18 -05002142 /* clone any lsm security options from the parent to the new sb */
2143 security_sb_clone_mnt_opts(data->sb, s);
2144
David Howells54ceac42006-08-22 20:06:13 -04002145 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2146 return 0;
2147
2148out_err_nosb:
2149 nfs_free_server(server);
2150out_err_noserver:
2151 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2152 return error;
2153
2154error_splat_super:
2155 up_write(&s->s_umount);
2156 deactivate_super(s);
2157 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2158 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002159}
2160
2161#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002162
2163/*
2164 * Finish setting up a cloned NFS4 superblock
2165 */
2166static void nfs4_clone_super(struct super_block *sb,
2167 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002168{
David Howells54ceac42006-08-22 20:06:13 -04002169 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2170 sb->s_blocksize = old_sb->s_blocksize;
2171 sb->s_maxbytes = old_sb->s_maxbytes;
2172 sb->s_time_gran = 1;
2173 sb->s_op = old_sb->s_op;
2174 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002175}
2176
2177/*
2178 * Set up an NFS4 superblock
2179 */
David Howells54ceac42006-08-22 20:06:13 -04002180static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002181{
David Howellsf7b422b2006-06-09 09:34:33 -04002182 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002183 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002184 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002185}
2186
David Howells54ceac42006-08-22 20:06:13 -04002187/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002188 * Validate NFSv4 mount options
2189 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002190static int nfs4_validate_mount_data(void *options,
2191 struct nfs_parsed_mount_data *args,
2192 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002193{
Chuck Lever4c568012007-12-10 14:59:28 -05002194 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002195 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002196 char *c;
2197
2198 if (data == NULL)
2199 goto out_no_data;
2200
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002201 args->rsize = NFS_MAX_FILE_IO_SIZE;
2202 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002203 args->acregmin = NFS_DEF_ACREGMIN;
2204 args->acregmax = NFS_DEF_ACREGMAX;
2205 args->acdirmin = NFS_DEF_ACDIRMIN;
2206 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002207 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002208 args->auth_flavors[0] = RPC_AUTH_UNIX;
2209 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002210
Chuck Leverf0768eb2007-07-01 12:13:01 -04002211 switch (data->version) {
2212 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002213 ap = (struct sockaddr_in *)&args->nfs_server.address;
2214 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002215 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002216 if (data->host_addrlen == 0)
2217 goto out_no_address;
2218 args->nfs_server.addrlen = data->host_addrlen;
2219 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002220 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002221 if (!nfs_verify_server_address((struct sockaddr *)
2222 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002223 goto out_no_address;
2224
Chuck Lever6738b252008-06-24 16:33:54 -04002225 if (data->auth_flavourlen) {
2226 if (data->auth_flavourlen > 1)
2227 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002228 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002229 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002230 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002231 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002232 }
2233
2234 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2235 if (IS_ERR(c))
2236 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002237 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002238
2239 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2240 if (IS_ERR(c))
2241 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002242 args->nfs_server.export_path = c;
2243 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002244
2245 c = strndup_user(data->client_addr.data, 16);
2246 if (IS_ERR(c))
2247 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002248 args->client_address = c;
2249
2250 /*
2251 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2252 * can deal with.
2253 */
2254
2255 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2256 args->rsize = data->rsize;
2257 args->wsize = data->wsize;
2258 args->timeo = data->timeo;
2259 args->retrans = data->retrans;
2260 args->acregmin = data->acregmin;
2261 args->acregmax = data->acregmax;
2262 args->acdirmin = data->acdirmin;
2263 args->acdirmax = data->acdirmax;
2264 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002265 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002266
2267 break;
Chuck Lever80071222007-07-01 12:13:59 -04002268 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002269 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002270
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002271 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002272 return -EINVAL;
2273
2274 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002275 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002276 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002277
Chuck Levered596a82008-06-26 17:47:05 -04002278 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2279 args->nfs_server.port);
2280
Trond Myklebust259875e2008-07-02 14:43:47 -04002281 nfs_validate_transport_protocol(args);
2282
Chuck Lever6738b252008-06-24 16:33:54 -04002283 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002284 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002285
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002286 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002287 goto out_no_client_address;
2288
Chuck Leverdc045892008-06-23 12:36:37 -04002289 status = nfs_parse_devname(dev_name,
2290 &args->nfs_server.hostname,
2291 NFS4_MAXNAMLEN,
2292 &args->nfs_server.export_path,
2293 NFS4_MAXPATHLEN);
2294 if (status < 0)
2295 return status;
2296
Chuck Lever80071222007-07-01 12:13:59 -04002297 break;
2298 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002299 }
2300
2301 return 0;
2302
2303out_no_data:
2304 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2305 return -EINVAL;
2306
2307out_inval_auth:
2308 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2309 data->auth_flavourlen);
2310 return -EINVAL;
2311
2312out_no_address:
2313 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2314 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002315
2316out_no_client_address:
2317 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2318 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002319}
2320
2321/*
David Howells54ceac42006-08-22 20:06:13 -04002322 * Get the superblock for an NFS4 mountpoint
2323 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002324static int nfs4_get_sb(struct file_system_type *fs_type,
2325 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002326{
Trond Myklebust33852a12008-06-19 14:20:11 -04002327 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002328 struct super_block *s;
2329 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002330 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002331 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002332 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002333 struct nfs_sb_mountdata sb_mntdata = {
2334 .mntflags = flags,
2335 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002336 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002337
Trond Myklebust33852a12008-06-19 14:20:11 -04002338 data = kzalloc(sizeof(*data), GFP_KERNEL);
2339 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2340 if (data == NULL || mntfh == NULL)
2341 goto out_free_fh;
2342
2343 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002344
Chuck Leverf0768eb2007-07-01 12:13:01 -04002345 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002346 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002347 if (error < 0)
2348 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002349
David Howells54ceac42006-08-22 20:06:13 -04002350 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002351 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002352 if (IS_ERR(server)) {
2353 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002354 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002355 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002356 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002357
Trond Myklebust75180df2007-05-16 16:53:28 -04002358 if (server->flags & NFS4_MOUNT_UNSHARED)
2359 compare_super = NULL;
2360
David Howells54ceac42006-08-22 20:06:13 -04002361 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002362 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002363 if (IS_ERR(s)) {
2364 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002365 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002366 }
2367
Trond Myklebust5dd31772006-08-24 01:03:05 -04002368 if (s->s_fs_info != server) {
2369 nfs_free_server(server);
2370 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002371 } else {
2372 error = nfs_bdi_register(server);
2373 if (error)
2374 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002375 }
2376
David Howells54ceac42006-08-22 20:06:13 -04002377 if (!s->s_root) {
2378 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002379 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002380 }
David Howellsf7b422b2006-06-09 09:34:33 -04002381
Trond Myklebust33852a12008-06-19 14:20:11 -04002382 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002383 if (IS_ERR(mntroot)) {
2384 error = PTR_ERR(mntroot);
2385 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002386 }
David Howells54ceac42006-08-22 20:06:13 -04002387
Trond Myklebust33852a12008-06-19 14:20:11 -04002388 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002389 if (error)
2390 goto error_splat_root;
2391
David Howellsf7b422b2006-06-09 09:34:33 -04002392 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002393 mnt->mnt_sb = s;
2394 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002395 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002396
Chuck Lever29eb9812007-07-01 12:12:30 -04002397out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002398 kfree(data->client_address);
2399 kfree(data->nfs_server.export_path);
2400 kfree(data->nfs_server.hostname);
2401 security_free_mnt_opts(&data->lsm_opts);
2402out_free_fh:
2403 kfree(mntfh);
2404 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002405 return error;
David Howells54ceac42006-08-22 20:06:13 -04002406
Chuck Lever29eb9812007-07-01 12:12:30 -04002407out_free:
2408 nfs_free_server(server);
2409 goto out;
2410
Eric Paris46c8ac72008-05-02 13:42:42 -07002411error_splat_root:
2412 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002413error_splat_super:
2414 up_write(&s->s_umount);
2415 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002416 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002417}
2418
2419static void nfs4_kill_super(struct super_block *sb)
2420{
2421 struct nfs_server *server = NFS_SB(sb);
2422
2423 nfs_return_all_delegations(sb);
2424 kill_anon_super(sb);
2425
2426 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002427 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002428}
2429
2430/*
David Howells54ceac42006-08-22 20:06:13 -04002431 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002432 */
David Howells54ceac42006-08-22 20:06:13 -04002433static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2434 const char *dev_name, void *raw_data,
2435 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002436{
2437 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002438 struct super_block *s;
2439 struct nfs_server *server;
2440 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002441 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002442 struct nfs_sb_mountdata sb_mntdata = {
2443 .mntflags = flags,
2444 };
David Howells54ceac42006-08-22 20:06:13 -04002445 int error;
2446
2447 dprintk("--> nfs4_xdev_get_sb()\n");
2448
2449 /* create a new volume representation */
2450 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2451 if (IS_ERR(server)) {
2452 error = PTR_ERR(server);
2453 goto out_err_noserver;
2454 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002455 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002456
Trond Myklebust75180df2007-05-16 16:53:28 -04002457 if (server->flags & NFS4_MOUNT_UNSHARED)
2458 compare_super = NULL;
2459
David Howells54ceac42006-08-22 20:06:13 -04002460 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002461 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002462 if (IS_ERR(s)) {
2463 error = PTR_ERR(s);
2464 goto out_err_nosb;
2465 }
2466
2467 if (s->s_fs_info != server) {
2468 nfs_free_server(server);
2469 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002470 } else {
2471 error = nfs_bdi_register(server);
2472 if (error)
2473 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002474 }
2475
2476 if (!s->s_root) {
2477 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002478 nfs4_clone_super(s, data->sb);
2479 }
2480
2481 mntroot = nfs4_get_root(s, data->fh);
2482 if (IS_ERR(mntroot)) {
2483 error = PTR_ERR(mntroot);
2484 goto error_splat_super;
2485 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002486 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2487 dput(mntroot);
2488 error = -ESTALE;
2489 goto error_splat_super;
2490 }
David Howells54ceac42006-08-22 20:06:13 -04002491
2492 s->s_flags |= MS_ACTIVE;
2493 mnt->mnt_sb = s;
2494 mnt->mnt_root = mntroot;
2495
Eric Paris46c8ac72008-05-02 13:42:42 -07002496 security_sb_clone_mnt_opts(data->sb, s);
2497
David Howells54ceac42006-08-22 20:06:13 -04002498 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2499 return 0;
2500
2501out_err_nosb:
2502 nfs_free_server(server);
2503out_err_noserver:
2504 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2505 return error;
2506
2507error_splat_super:
2508 up_write(&s->s_umount);
2509 deactivate_super(s);
2510 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2511 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002512}
2513
David Howells54ceac42006-08-22 20:06:13 -04002514/*
2515 * Create an NFS4 server record on referral traversal
2516 */
2517static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2518 const char *dev_name, void *raw_data,
2519 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002520{
2521 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002522 struct super_block *s;
2523 struct nfs_server *server;
2524 struct dentry *mntroot;
2525 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002526 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002527 struct nfs_sb_mountdata sb_mntdata = {
2528 .mntflags = flags,
2529 };
David Howells54ceac42006-08-22 20:06:13 -04002530 int error;
2531
2532 dprintk("--> nfs4_referral_get_sb()\n");
2533
2534 /* create a new volume representation */
2535 server = nfs4_create_referral_server(data, &mntfh);
2536 if (IS_ERR(server)) {
2537 error = PTR_ERR(server);
2538 goto out_err_noserver;
2539 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002540 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002541
Trond Myklebust75180df2007-05-16 16:53:28 -04002542 if (server->flags & NFS4_MOUNT_UNSHARED)
2543 compare_super = NULL;
2544
David Howells54ceac42006-08-22 20:06:13 -04002545 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002546 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002547 if (IS_ERR(s)) {
2548 error = PTR_ERR(s);
2549 goto out_err_nosb;
2550 }
2551
2552 if (s->s_fs_info != server) {
2553 nfs_free_server(server);
2554 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002555 } else {
2556 error = nfs_bdi_register(server);
2557 if (error)
2558 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002559 }
2560
2561 if (!s->s_root) {
2562 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002563 nfs4_fill_super(s);
2564 }
2565
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002566 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002567 if (IS_ERR(mntroot)) {
2568 error = PTR_ERR(mntroot);
2569 goto error_splat_super;
2570 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002571 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2572 dput(mntroot);
2573 error = -ESTALE;
2574 goto error_splat_super;
2575 }
David Howells54ceac42006-08-22 20:06:13 -04002576
2577 s->s_flags |= MS_ACTIVE;
2578 mnt->mnt_sb = s;
2579 mnt->mnt_root = mntroot;
2580
Eric Paris46c8ac72008-05-02 13:42:42 -07002581 security_sb_clone_mnt_opts(data->sb, s);
2582
David Howells54ceac42006-08-22 20:06:13 -04002583 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2584 return 0;
2585
2586out_err_nosb:
2587 nfs_free_server(server);
2588out_err_noserver:
2589 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2590 return error;
2591
2592error_splat_super:
2593 up_write(&s->s_umount);
2594 deactivate_super(s);
2595 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2596 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002597}
2598
David Howells54ceac42006-08-22 20:06:13 -04002599#endif /* CONFIG_NFS_V4 */