blob: 2b0c8e132b54e29def9e30cf44d86f72c6dfa109 [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;
471 seq_printf(m, ",mountaddr=" NIP6_FMT,
472 NIP6(sin6->sin6_addr));
473 break;
474 }
475 default:
476 if (showdefaults)
477 seq_printf(m, ",mountaddr=unspecified");
478 }
479
480 if (nfss->mountd_version || showdefaults)
481 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
482 if (nfss->mountd_port || showdefaults)
483 seq_printf(m, ",mountport=%u", nfss->mountd_port);
484
485 switch (nfss->mountd_protocol) {
486 case IPPROTO_UDP:
487 seq_printf(m, ",mountproto=udp");
488 break;
489 case IPPROTO_TCP:
490 seq_printf(m, ",mountproto=tcp");
491 break;
492 default:
493 if (showdefaults)
494 seq_printf(m, ",mountproto=auto");
495 }
496}
497
David Howellsf7b422b2006-06-09 09:34:33 -0400498/*
499 * Describe the mount options in force on this server representation
500 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400501static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
502 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400503{
David Howells509de812006-08-22 20:06:11 -0400504 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400505 int flag;
David Howells509de812006-08-22 20:06:11 -0400506 const char *str;
507 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400508 } nfs_info[] = {
509 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400510 { NFS_MOUNT_INTR, ",intr", ",nointr" },
511 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400512 { NFS_MOUNT_NOCTO, ",nocto", "" },
513 { NFS_MOUNT_NOAC, ",noac", "" },
514 { NFS_MOUNT_NONLM, ",nolock", "" },
515 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400516 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400517 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
David Howellsf7b422b2006-06-09 09:34:33 -0400518 { 0, NULL, NULL }
519 };
David Howells509de812006-08-22 20:06:11 -0400520 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400521 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400522 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400523
Chuck Lever82d101d2008-03-14 14:10:37 -0400524 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400525 seq_printf(m, ",rsize=%u", nfss->rsize);
526 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400527 if (nfss->bsize != 0)
528 seq_printf(m, ",bsize=%u", nfss->bsize);
529 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400530 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400531 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400532 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400533 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400534 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400535 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400536 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400537 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400538 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
539 if (nfss->flags & nfs_infop->flag)
540 seq_puts(m, nfs_infop->str);
541 else
542 seq_puts(m, nfs_infop->nostr);
543 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400544 seq_printf(m, ",proto=%s",
545 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400546 if (version == 4) {
547 if (nfss->port != NFS_PORT)
548 seq_printf(m, ",port=%u", nfss->port);
549 } else
550 if (nfss->port)
551 seq_printf(m, ",port=%u", nfss->port);
552
Trond Myklebust33170232007-12-20 16:03:59 -0500553 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
554 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400555 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400556
557 if (version != 4)
558 nfs_show_mountd_options(m, nfss, showdefaults);
559
560#ifdef CONFIG_NFS_V4
561 if (clp->rpc_ops->version == 4)
562 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
563#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400564}
565
566/*
567 * Describe the mount options on this VFS mountpoint
568 */
569static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
570{
571 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
572
573 nfs_show_mount_options(m, nfss, 0);
574
Chuck Lever5d8515c2007-12-10 14:57:16 -0500575 seq_printf(m, ",addr=%s",
576 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
577 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400578
579 return 0;
580}
581
582/*
583 * Present statistical information for this VFS mountpoint
584 */
585static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
586{
587 int i, cpu;
588 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
589 struct rpc_auth *auth = nfss->client->cl_auth;
590 struct nfs_iostats totals = { };
591
592 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
593
594 /*
595 * Display all mount option settings
596 */
597 seq_printf(m, "\n\topts:\t");
598 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
599 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
600 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
601 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
602 nfs_show_mount_options(m, nfss, 1);
603
604 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
605
606 seq_printf(m, "\n\tcaps:\t");
607 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400608 seq_printf(m, ",wtmult=%u", nfss->wtmult);
609 seq_printf(m, ",dtsize=%u", nfss->dtsize);
610 seq_printf(m, ",bsize=%u", nfss->bsize);
611 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400612
613#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500614 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400615 seq_printf(m, "\n\tnfsv4:\t");
616 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
617 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
618 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
619 }
620#endif
621
622 /*
623 * Display security flavor in effect for this mount
624 */
Chuck Lever2d767432008-03-14 14:10:08 -0400625 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400626 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400627 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400628
629 /*
630 * Display superblock I/O counters
631 */
632 for_each_possible_cpu(cpu) {
633 struct nfs_iostats *stats;
634
635 preempt_disable();
636 stats = per_cpu_ptr(nfss->io_stats, cpu);
637
638 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
639 totals.events[i] += stats->events[i];
640 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
641 totals.bytes[i] += stats->bytes[i];
642
643 preempt_enable();
644 }
645
646 seq_printf(m, "\n\tevents:\t");
647 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
648 seq_printf(m, "%lu ", totals.events[i]);
649 seq_printf(m, "\n\tbytes:\t");
650 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
651 seq_printf(m, "%Lu ", totals.bytes[i]);
652 seq_printf(m, "\n");
653
654 rpc_print_iostats(m, nfss->client);
655
656 return 0;
657}
658
659/*
660 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400661 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400662 */
Al Viro42faad92008-04-24 07:21:56 -0400663static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400664{
Al Viro42faad92008-04-24 07:21:56 -0400665 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400666 struct rpc_clnt *rpc;
667
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400668 /* -EIO all pending I/O */
669 rpc = server->client_acl;
670 if (!IS_ERR(rpc))
671 rpc_killall_tasks(rpc);
672 rpc = server->client;
673 if (!IS_ERR(rpc))
674 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400675}
676
677/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500678 * Sanity-check a server address provided by the mount command.
679 *
680 * Address family must be initialized, and address must not be
681 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400682 */
683static int nfs_verify_server_address(struct sockaddr *addr)
684{
685 switch (addr->sa_family) {
686 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500687 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700688 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500689 }
690 case AF_INET6: {
691 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
692 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400693 }
694 }
695
696 return 0;
697}
698
Chuck Leverce3b7e12008-06-23 12:36:53 -0400699static void nfs_parse_ipv4_address(char *string, size_t str_len,
700 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500701{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400702 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
703 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500704
Chuck Leverce3b7e12008-06-23 12:36:53 -0400705 if (str_len <= INET_ADDRSTRLEN) {
706 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
707 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500708
Chuck Leverce3b7e12008-06-23 12:36:53 -0400709 sin->sin_family = AF_INET;
710 *addr_len = sizeof(*sin);
711 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500712 return;
713 }
Chuck Lever9412b922007-12-10 14:59:21 -0500714
715 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400716 *addr_len = 0;
717}
718
719#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400720static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
721 const char *delim,
722 struct sockaddr_in6 *sin6)
Chuck Leverd8e77482008-06-23 12:37:01 -0400723{
724 char *p;
725 size_t len;
726
Chuck Lever5e2e7722008-10-08 15:38:10 -0400727 if ((string + str_len) == delim)
728 return 1;
729
Chuck Leverd8e77482008-06-23 12:37:01 -0400730 if (*delim != IPV6_SCOPE_DELIMITER)
Chuck Lever5e2e7722008-10-08 15:38:10 -0400731 return 0;
732
733 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
734 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400735
736 len = (string + str_len) - delim - 1;
737 p = kstrndup(delim + 1, len, GFP_KERNEL);
738 if (p) {
739 unsigned long scope_id = 0;
740 struct net_device *dev;
741
742 dev = dev_get_by_name(&init_net, p);
743 if (dev != NULL) {
744 scope_id = dev->ifindex;
745 dev_put(dev);
746 } else {
Chuck Lever5e2e7722008-10-08 15:38:10 -0400747 if (strict_strtoul(p, 10, &scope_id) == 0) {
748 kfree(p);
749 return 0;
750 }
Chuck Leverd8e77482008-06-23 12:37:01 -0400751 }
752
753 kfree(p);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400754
Chuck Leverd8e77482008-06-23 12:37:01 -0400755 sin6->sin6_scope_id = scope_id;
756 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400757 return 1;
Chuck Leverd8e77482008-06-23 12:37:01 -0400758 }
Chuck Lever5e2e7722008-10-08 15:38:10 -0400759
760 return 0;
Chuck Leverd8e77482008-06-23 12:37:01 -0400761}
762
Chuck Leverce3b7e12008-06-23 12:36:53 -0400763static void nfs_parse_ipv6_address(char *string, size_t str_len,
764 struct sockaddr *sap, size_t *addr_len)
765{
766 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
767 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400768 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400769
770 if (str_len <= INET6_ADDRSTRLEN) {
771 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
772 (int)str_len, string);
773
774 sin6->sin6_family = AF_INET6;
775 *addr_len = sizeof(*sin6);
Chuck Lever5e2e7722008-10-08 15:38:10 -0400776 if (in6_pton(string, str_len, addr,
777 IPV6_SCOPE_DELIMITER, &delim) != 0) {
778 if (nfs_parse_ipv6_scope_id(string, str_len,
779 delim, sin6) != 0)
780 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400781 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400782 }
783
784 sap->sa_family = AF_UNSPEC;
785 *addr_len = 0;
786}
787#else
788static void nfs_parse_ipv6_address(char *string, size_t str_len,
789 struct sockaddr *sap, size_t *addr_len)
790{
791 sap->sa_family = AF_UNSPEC;
792 *addr_len = 0;
793}
794#endif
795
796/*
797 * Construct a sockaddr based on the contents of a string that contains
798 * an IP address in presentation format.
799 *
800 * If there is a problem constructing the new sockaddr, set the address
801 * family to AF_UNSPEC.
802 */
J. Bruce Fieldsea31a4432008-08-20 16:10:23 -0400803void nfs_parse_ip_address(char *string, size_t str_len,
Chuck Leverce3b7e12008-06-23 12:36:53 -0400804 struct sockaddr *sap, size_t *addr_len)
805{
806 unsigned int i, colons;
807
808 colons = 0;
809 for (i = 0; i < str_len; i++)
810 if (string[i] == ':')
811 colons++;
812
813 if (colons >= 2)
814 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
815 else
816 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500817}
818
819/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400820 * Sanity check the NFS transport protocol.
821 *
822 */
823static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
824{
825 switch (mnt->nfs_server.protocol) {
826 case XPRT_TRANSPORT_UDP:
827 case XPRT_TRANSPORT_TCP:
828 case XPRT_TRANSPORT_RDMA:
829 break;
830 default:
831 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
832 }
833}
834
835/*
836 * For text based NFSv2/v3 mounts, the mount protocol transport default
837 * settings should depend upon the specified NFS transport.
838 */
839static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
840{
841 nfs_validate_transport_protocol(mnt);
842
843 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
844 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
845 return;
846 switch (mnt->nfs_server.protocol) {
847 case XPRT_TRANSPORT_UDP:
848 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
849 break;
850 case XPRT_TRANSPORT_TCP:
851 case XPRT_TRANSPORT_RDMA:
852 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
853 }
854}
855
856/*
Chuck Lever01060c82008-06-24 16:33:38 -0400857 * Parse the value of the 'sec=' option.
858 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400859 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400860 */
861static int nfs_parse_security_flavors(char *value,
862 struct nfs_parsed_mount_data *mnt)
863{
864 substring_t args[MAX_OPT_ARGS];
865
866 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
867
868 switch (match_token(value, nfs_secflavor_tokens, args)) {
869 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400870 mnt->auth_flavor_len = 0;
871 mnt->auth_flavors[0] = RPC_AUTH_NULL;
872 break;
873 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400874 mnt->auth_flavor_len = 0;
875 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
876 break;
877 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400878 mnt->auth_flavor_len = 1;
879 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
880 break;
881 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400882 mnt->auth_flavor_len = 1;
883 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
884 break;
885 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400886 mnt->auth_flavor_len = 1;
887 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
888 break;
889 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400890 mnt->auth_flavor_len = 1;
891 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
892 break;
893 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400894 mnt->auth_flavor_len = 1;
895 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
896 break;
897 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400898 mnt->auth_flavor_len = 1;
899 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
900 break;
901 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400902 mnt->auth_flavor_len = 1;
903 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
904 break;
905 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400906 mnt->auth_flavor_len = 1;
907 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
908 break;
909 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400910 mnt->auth_flavor_len = 1;
911 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
912 break;
913 default:
914 return 0;
915 }
916
917 return 1;
918}
919
Chuck Leverf45663c2008-06-24 19:28:02 -0400920static void nfs_parse_invalid_value(const char *option)
921{
922 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
923}
924
Chuck Lever01060c82008-06-24 16:33:38 -0400925/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400926 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400927 * a data structure. The whole mount string is processed; bad options are
928 * skipped as they are encountered. If there were no errors, return 1;
929 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400930 */
931static int nfs_parse_mount_options(char *raw,
932 struct nfs_parsed_mount_data *mnt)
933{
Eric Parisf9c3a382008-03-05 14:20:18 -0500934 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400935 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400936
937 if (!raw) {
938 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
939 return 1;
940 }
941 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
942
Eric Parisf9c3a382008-03-05 14:20:18 -0500943 secdata = alloc_secdata();
944 if (!secdata)
945 goto out_nomem;
946
947 rc = security_sb_copy_data(raw, secdata);
948 if (rc)
949 goto out_security_failure;
950
951 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
952 if (rc)
953 goto out_security_failure;
954
955 free_secdata(secdata);
956
Chuck Leverbf0fd762007-07-01 12:13:44 -0400957 while ((p = strsep(&raw, ",")) != NULL) {
958 substring_t args[MAX_OPT_ARGS];
959 int option, token;
960
961 if (!*p)
962 continue;
963
964 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
965
966 token = match_token(p, nfs_mount_option_tokens, args);
967 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400968
969 /*
970 * boolean options: foo/nofoo
971 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400972 case Opt_soft:
973 mnt->flags |= NFS_MOUNT_SOFT;
974 break;
975 case Opt_hard:
976 mnt->flags &= ~NFS_MOUNT_SOFT;
977 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400978 case Opt_posix:
979 mnt->flags |= NFS_MOUNT_POSIX;
980 break;
981 case Opt_noposix:
982 mnt->flags &= ~NFS_MOUNT_POSIX;
983 break;
984 case Opt_cto:
985 mnt->flags &= ~NFS_MOUNT_NOCTO;
986 break;
987 case Opt_nocto:
988 mnt->flags |= NFS_MOUNT_NOCTO;
989 break;
990 case Opt_ac:
991 mnt->flags &= ~NFS_MOUNT_NOAC;
992 break;
993 case Opt_noac:
994 mnt->flags |= NFS_MOUNT_NOAC;
995 break;
996 case Opt_lock:
997 mnt->flags &= ~NFS_MOUNT_NONLM;
998 break;
999 case Opt_nolock:
1000 mnt->flags |= NFS_MOUNT_NONLM;
1001 break;
1002 case Opt_v2:
1003 mnt->flags &= ~NFS_MOUNT_VER3;
1004 break;
1005 case Opt_v3:
1006 mnt->flags |= NFS_MOUNT_VER3;
1007 break;
1008 case Opt_udp:
1009 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001010 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001011 break;
1012 case Opt_tcp:
1013 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001014 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001015 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001016 case Opt_rdma:
1017 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1018 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001019 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001020 case Opt_acl:
1021 mnt->flags &= ~NFS_MOUNT_NOACL;
1022 break;
1023 case Opt_noacl:
1024 mnt->flags |= NFS_MOUNT_NOACL;
1025 break;
1026 case Opt_rdirplus:
1027 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1028 break;
1029 case Opt_nordirplus:
1030 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1031 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001032 case Opt_sharecache:
1033 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1034 break;
1035 case Opt_nosharecache:
1036 mnt->flags |= NFS_MOUNT_UNSHARED;
1037 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001038
Chuck Leverf45663c2008-06-24 19:28:02 -04001039 /*
1040 * options that take numeric values
1041 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001042 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001043 if (match_int(args, &option) ||
1044 option < 0 || option > USHORT_MAX) {
1045 errors++;
1046 nfs_parse_invalid_value("port");
1047 } else
1048 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001049 break;
1050 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001051 if (match_int(args, &option) || option < 0) {
1052 errors++;
1053 nfs_parse_invalid_value("rsize");
1054 } else
1055 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001056 break;
1057 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001058 if (match_int(args, &option) || option < 0) {
1059 errors++;
1060 nfs_parse_invalid_value("wsize");
1061 } else
1062 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001063 break;
1064 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001065 if (match_int(args, &option) || option < 0) {
1066 errors++;
1067 nfs_parse_invalid_value("bsize");
1068 } else
1069 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001070 break;
1071 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001072 if (match_int(args, &option) || option <= 0) {
1073 errors++;
1074 nfs_parse_invalid_value("timeo");
1075 } else
1076 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001077 break;
1078 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001079 if (match_int(args, &option) || option <= 0) {
1080 errors++;
1081 nfs_parse_invalid_value("retrans");
1082 } else
1083 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001084 break;
1085 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001086 if (match_int(args, &option) || option < 0) {
1087 errors++;
1088 nfs_parse_invalid_value("acregmin");
1089 } else
1090 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001091 break;
1092 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001093 if (match_int(args, &option) || option < 0) {
1094 errors++;
1095 nfs_parse_invalid_value("acregmax");
1096 } else
1097 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001098 break;
1099 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001100 if (match_int(args, &option) || option < 0) {
1101 errors++;
1102 nfs_parse_invalid_value("acdirmin");
1103 } else
1104 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001105 break;
1106 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001107 if (match_int(args, &option) || option < 0) {
1108 errors++;
1109 nfs_parse_invalid_value("acdirmax");
1110 } else
1111 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001112 break;
1113 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001114 if (match_int(args, &option) || option < 0) {
1115 errors++;
1116 nfs_parse_invalid_value("actimeo");
1117 } else
1118 mnt->acregmin = mnt->acregmax =
1119 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001120 break;
1121 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001122 if (match_int(args, &option) || option < 0) {
1123 errors++;
1124 nfs_parse_invalid_value("namlen");
1125 } else
1126 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001127 break;
1128 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001129 if (match_int(args, &option) ||
1130 option < 0 || option > USHORT_MAX) {
1131 errors++;
1132 nfs_parse_invalid_value("mountport");
1133 } else
1134 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001135 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001136 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001137 if (match_int(args, &option) ||
1138 option < NFS_MNT_VERSION ||
1139 option > NFS_MNT3_VERSION) {
1140 errors++;
1141 nfs_parse_invalid_value("mountvers");
1142 } else
1143 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001144 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001145 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001146 if (match_int(args, &option)) {
1147 errors++;
1148 nfs_parse_invalid_value("nfsvers");
1149 break;
1150 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001151 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001152 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001153 mnt->flags &= ~NFS_MOUNT_VER3;
1154 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001155 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001156 mnt->flags |= NFS_MOUNT_VER3;
1157 break;
1158 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001159 errors++;
1160 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001161 }
1162 break;
1163
Chuck Leverf45663c2008-06-24 19:28:02 -04001164 /*
1165 * options that take text values
1166 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001167 case Opt_sec:
1168 string = match_strdup(args);
1169 if (string == NULL)
1170 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001171 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001172 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001173 if (!rc) {
1174 errors++;
1175 dfprintk(MOUNT, "NFS: unrecognized "
1176 "security flavor\n");
1177 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001178 break;
1179 case Opt_proto:
1180 string = match_strdup(args);
1181 if (string == NULL)
1182 goto out_nomem;
1183 token = match_token(string,
1184 nfs_xprt_protocol_tokens, args);
1185 kfree(string);
1186
1187 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001188 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001189 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001190 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001192 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001193 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001194 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001195 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001196 case Opt_xprt_rdma:
1197 /* vector side protocols to TCP */
1198 mnt->flags |= NFS_MOUNT_TCP;
1199 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001200 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001201 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001202 errors++;
1203 dfprintk(MOUNT, "NFS: unrecognized "
1204 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001205 }
1206 break;
1207 case Opt_mountproto:
1208 string = match_strdup(args);
1209 if (string == NULL)
1210 goto out_nomem;
1211 token = match_token(string,
1212 nfs_xprt_protocol_tokens, args);
1213 kfree(string);
1214
1215 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001216 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001217 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001218 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001219 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001220 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001221 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001222 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001223 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001224 errors++;
1225 dfprintk(MOUNT, "NFS: unrecognized "
1226 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001227 }
1228 break;
1229 case Opt_addr:
1230 string = match_strdup(args);
1231 if (string == NULL)
1232 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001233 nfs_parse_ip_address(string, strlen(string),
1234 (struct sockaddr *)
1235 &mnt->nfs_server.address,
1236 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001237 kfree(string);
1238 break;
1239 case Opt_clientaddr:
1240 string = match_strdup(args);
1241 if (string == NULL)
1242 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001243 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001244 mnt->client_address = string;
1245 break;
Chuck Lever33832032007-12-10 14:59:13 -05001246 case Opt_mounthost:
1247 string = match_strdup(args);
1248 if (string == NULL)
1249 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001250 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001251 mnt->mount_server.hostname = string;
1252 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001253 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001254 string = match_strdup(args);
1255 if (string == NULL)
1256 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001257 nfs_parse_ip_address(string, strlen(string),
1258 (struct sockaddr *)
1259 &mnt->mount_server.address,
1260 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001261 kfree(string);
1262 break;
Trond Myklebust7973c1f2008-07-15 17:58:14 -04001263 case Opt_lookupcache:
1264 string = match_strdup(args);
1265 if (string == NULL)
1266 goto out_nomem;
1267 token = match_token(string,
1268 nfs_lookupcache_tokens, args);
1269 kfree(string);
1270 switch (token) {
1271 case Opt_lookupcache_all:
1272 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1273 break;
1274 case Opt_lookupcache_positive:
1275 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1276 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1277 break;
1278 case Opt_lookupcache_none:
1279 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1280 break;
1281 default:
1282 errors++;
1283 dfprintk(MOUNT, "NFS: invalid "
1284 "lookupcache argument\n");
1285 };
1286 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001287
Chuck Leverf45663c2008-06-24 19:28:02 -04001288 /*
1289 * Special options
1290 */
1291 case Opt_sloppy:
1292 sloppy = 1;
1293 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1294 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001295 case Opt_userspace:
1296 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001297 dfprintk(MOUNT, "NFS: ignoring mount option "
1298 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001299 break;
1300
1301 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001302 errors++;
1303 dfprintk(MOUNT, "NFS: unrecognized mount option "
1304 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001305 }
1306 }
1307
Chuck Leveraf904de2008-09-08 11:58:13 -04001308 if (errors > 0) {
1309 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1310 errors, (errors == 1 ? "" : "s"));
1311 if (!sloppy)
1312 return 0;
1313 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001314 return 1;
1315
1316out_nomem:
1317 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1318 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001319out_security_failure:
1320 free_secdata(secdata);
1321 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1322 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001323}
1324
1325/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001326 * Use the remote server's MOUNT service to request the NFS file handle
1327 * corresponding to the provided path.
1328 */
1329static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1330 struct nfs_fh *root_fh)
1331{
Chuck Leverc5d120f2008-12-23 15:21:35 -05001332 struct nfs_mount_request request = {
1333 .sap = (struct sockaddr *)
1334 &args->mount_server.address,
1335 .dirpath = args->nfs_server.export_path,
1336 .protocol = args->mount_server.protocol,
1337 .fh = root_fh,
1338 };
Chuck Lever4c568012007-12-10 14:59:28 -05001339 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001340
1341 if (args->mount_server.version == 0) {
1342 if (args->flags & NFS_MOUNT_VER3)
1343 args->mount_server.version = NFS_MNT3_VERSION;
1344 else
1345 args->mount_server.version = NFS_MNT_VERSION;
1346 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001347 request.version = args->mount_server.version;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001348
Chuck Lever33832032007-12-10 14:59:13 -05001349 if (args->mount_server.hostname)
Chuck Leverc5d120f2008-12-23 15:21:35 -05001350 request.hostname = args->mount_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001351 else
Chuck Leverc5d120f2008-12-23 15:21:35 -05001352 request.hostname = args->nfs_server.hostname;
Chuck Lever33832032007-12-10 14:59:13 -05001353
Chuck Lever0076d7b2007-07-01 12:13:49 -04001354 /*
1355 * Construct the mount server's address.
1356 */
Chuck Lever4c568012007-12-10 14:59:28 -05001357 if (args->mount_server.address.ss_family == AF_UNSPEC) {
Chuck Leverc5d120f2008-12-23 15:21:35 -05001358 memcpy(request.sap, &args->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001359 args->nfs_server.addrlen);
1360 args->mount_server.addrlen = args->nfs_server.addrlen;
1361 }
Chuck Leverc5d120f2008-12-23 15:21:35 -05001362 request.salen = args->mount_server.addrlen;
Chuck Lever4c568012007-12-10 14:59:28 -05001363
James Lentiniaad70002007-09-24 17:32:49 -04001364 /*
1365 * autobind will be used if mount_server.port == 0
1366 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001367 nfs_set_port(request.sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001368
1369 /*
1370 * Now ask the mount server to map our export path
1371 * to a file handle.
1372 */
Chuck Leverc5d120f2008-12-23 15:21:35 -05001373 status = nfs_mount(&request);
Chuck Leverefd83402007-09-11 18:00:58 -04001374 if (status == 0)
1375 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001376
Chuck Lever396cee972008-06-12 12:37:49 -04001377 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Leverc5d120f2008-12-23 15:21:35 -05001378 request.hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001379 return status;
1380}
1381
Chuck Leverd1aa0822008-06-23 12:36:45 -04001382static int nfs_parse_simple_hostname(const char *dev_name,
1383 char **hostname, size_t maxnamlen,
1384 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001385{
1386 size_t len;
1387 char *colon, *comma;
1388
1389 colon = strchr(dev_name, ':');
1390 if (colon == NULL)
1391 goto out_bad_devname;
1392
1393 len = colon - dev_name;
1394 if (len > maxnamlen)
1395 goto out_hostname;
1396
1397 /* N.B. caller will free nfs_server.hostname in all cases */
1398 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1399 if (!*hostname)
1400 goto out_nomem;
1401
1402 /* kill possible hostname list: not supported */
1403 comma = strchr(*hostname, ',');
1404 if (comma != NULL) {
1405 if (comma == *hostname)
1406 goto out_bad_devname;
1407 *comma = '\0';
1408 }
1409
1410 colon++;
1411 len = strlen(colon);
1412 if (len > maxpathlen)
1413 goto out_path;
1414 *export_path = kstrndup(colon, len, GFP_KERNEL);
1415 if (!*export_path)
1416 goto out_nomem;
1417
1418 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1419 return 0;
1420
1421out_bad_devname:
1422 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1423 return -EINVAL;
1424
1425out_nomem:
1426 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1427 return -ENOMEM;
1428
1429out_hostname:
1430 dfprintk(MOUNT, "NFS: server hostname too long\n");
1431 return -ENAMETOOLONG;
1432
1433out_path:
1434 dfprintk(MOUNT, "NFS: export pathname too long\n");
1435 return -ENAMETOOLONG;
1436}
1437
1438/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001439 * Hostname has square brackets around it because it contains one or
1440 * more colons. We look for the first closing square bracket, and a
1441 * colon must follow it.
1442 */
1443static int nfs_parse_protected_hostname(const char *dev_name,
1444 char **hostname, size_t maxnamlen,
1445 char **export_path, size_t maxpathlen)
1446{
1447 size_t len;
1448 char *start, *end;
1449
1450 start = (char *)(dev_name + 1);
1451
1452 end = strchr(start, ']');
1453 if (end == NULL)
1454 goto out_bad_devname;
1455 if (*(end + 1) != ':')
1456 goto out_bad_devname;
1457
1458 len = end - start;
1459 if (len > maxnamlen)
1460 goto out_hostname;
1461
1462 /* N.B. caller will free nfs_server.hostname in all cases */
1463 *hostname = kstrndup(start, len, GFP_KERNEL);
1464 if (*hostname == NULL)
1465 goto out_nomem;
1466
1467 end += 2;
1468 len = strlen(end);
1469 if (len > maxpathlen)
1470 goto out_path;
1471 *export_path = kstrndup(end, len, GFP_KERNEL);
1472 if (!*export_path)
1473 goto out_nomem;
1474
1475 return 0;
1476
1477out_bad_devname:
1478 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1479 return -EINVAL;
1480
1481out_nomem:
1482 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1483 return -ENOMEM;
1484
1485out_hostname:
1486 dfprintk(MOUNT, "NFS: server hostname too long\n");
1487 return -ENAMETOOLONG;
1488
1489out_path:
1490 dfprintk(MOUNT, "NFS: export pathname too long\n");
1491 return -ENAMETOOLONG;
1492}
1493
1494/*
1495 * Split "dev_name" into "hostname:export_path".
1496 *
1497 * The leftmost colon demarks the split between the server's hostname
1498 * and the export path. If the hostname starts with a left square
1499 * bracket, then it may contain colons.
1500 *
1501 * Note: caller frees hostname and export path, even on error.
1502 */
1503static int nfs_parse_devname(const char *dev_name,
1504 char **hostname, size_t maxnamlen,
1505 char **export_path, size_t maxpathlen)
1506{
1507 if (*dev_name == '[')
1508 return nfs_parse_protected_hostname(dev_name,
1509 hostname, maxnamlen,
1510 export_path, maxpathlen);
1511
1512 return nfs_parse_simple_hostname(dev_name,
1513 hostname, maxnamlen,
1514 export_path, maxpathlen);
1515}
1516
1517/*
David Howells54ceac42006-08-22 20:06:13 -04001518 * Validate the NFS2/NFS3 mount data
1519 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001520 *
1521 * For option strings, user space handles the following behaviors:
1522 *
1523 * + DNS: mapping server host name to IP address ("addr=" option)
1524 *
1525 * + failure mode: how to behave if a mount request can't be handled
1526 * immediately ("fg/bg" option)
1527 *
1528 * + retry: how often to retry a mount request ("retry=" option)
1529 *
1530 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1531 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001532 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001533static int nfs_validate_mount_data(void *options,
1534 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001535 struct nfs_fh *mntfh,
1536 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001537{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001538 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001539
Chuck Lever5df36e72007-07-01 12:12:56 -04001540 if (data == NULL)
1541 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001542
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001543 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1544 args->rsize = NFS_MAX_FILE_IO_SIZE;
1545 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001546 args->acregmin = NFS_DEF_ACREGMIN;
1547 args->acregmax = NFS_DEF_ACREGMAX;
1548 args->acdirmin = NFS_DEF_ACDIRMIN;
1549 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001550 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001551 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001552 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001553 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001554
David Howellsf7b422b2006-06-09 09:34:33 -04001555 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001556 case 1:
1557 data->namlen = 0;
1558 case 2:
1559 data->bsize = 0;
1560 case 3:
1561 if (data->flags & NFS_MOUNT_VER3)
1562 goto out_no_v3;
1563 data->root.size = NFS2_FHSIZE;
1564 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1565 case 4:
1566 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1567 goto out_no_sec;
1568 case 5:
1569 memset(data->context, 0, sizeof(data->context));
1570 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001571 if (data->flags & NFS_MOUNT_VER3) {
1572 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1573 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001574 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001575 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001576 mntfh->size = NFS2_FHSIZE;
1577
Chuck Lever5df36e72007-07-01 12:12:56 -04001578
1579 memcpy(mntfh->data, data->root.data, mntfh->size);
1580 if (mntfh->size < sizeof(mntfh->data))
1581 memset(mntfh->data + mntfh->size, 0,
1582 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001583
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001584 /*
1585 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1586 * can deal with.
1587 */
Trond Myklebustff3525a2008-08-15 16:59:14 -04001588 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001589 args->rsize = data->rsize;
1590 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001591 args->timeo = data->timeo;
1592 args->retrans = data->retrans;
1593 args->acregmin = data->acregmin;
1594 args->acregmax = data->acregmax;
1595 args->acdirmin = data->acdirmin;
1596 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001597
1598 memcpy(&args->nfs_server.address, &data->addr,
1599 sizeof(data->addr));
1600 args->nfs_server.addrlen = sizeof(data->addr);
1601 if (!nfs_verify_server_address((struct sockaddr *)
1602 &args->nfs_server.address))
1603 goto out_no_address;
1604
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001605 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001606 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001607 /* N.B. caller will free nfs_server.hostname in all cases */
1608 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1609 args->namlen = data->namlen;
1610 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001611
1612 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1613 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001614 if (!args->nfs_server.hostname)
1615 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001616
1617 /*
1618 * The legacy version 6 binary mount data from userspace has a
1619 * field used only to transport selinux information into the
1620 * the kernel. To continue to support that functionality we
1621 * have a touch of selinux knowledge here in the NFS code. The
1622 * userspace code converted context=blah to just blah so we are
1623 * converting back to the full string selinux understands.
1624 */
1625 if (data->context[0]){
1626#ifdef CONFIG_SECURITY_SELINUX
1627 int rc;
1628 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1629 if (!opts_str)
1630 return -ENOMEM;
1631 strcpy(opts_str, "context=");
1632 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1633 strcat(opts_str, &data->context[0]);
1634 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1635 kfree(opts_str);
1636 if (rc)
1637 return rc;
1638#else
1639 return -EINVAL;
1640#endif
1641 }
1642
Chuck Lever5df36e72007-07-01 12:12:56 -04001643 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001644 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001645 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001646
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001647 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001648 return -EINVAL;
1649
Chuck Lever6e88e062007-09-24 15:39:50 -04001650 if (!nfs_verify_server_address((struct sockaddr *)
1651 &args->nfs_server.address))
1652 goto out_no_address;
1653
Chuck Levered596a82008-06-26 17:47:05 -04001654 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1655 args->nfs_server.port);
1656
Trond Myklebust259875e2008-07-02 14:43:47 -04001657 nfs_set_mount_transport_protocol(args);
1658
Chuck Leverdc045892008-06-23 12:36:37 -04001659 status = nfs_parse_devname(dev_name,
1660 &args->nfs_server.hostname,
1661 PAGE_SIZE,
1662 &args->nfs_server.export_path,
1663 NFS_MAXPATHLEN);
1664 if (!status)
1665 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001666
Chuck Leverdc045892008-06-23 12:36:37 -04001667 kfree(args->nfs_server.export_path);
1668 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001669
Chuck Lever136d5582007-07-01 12:13:54 -04001670 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001671 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001672
Chuck Lever136d5582007-07-01 12:13:54 -04001673 break;
1674 }
David Howellsf7b422b2006-06-09 09:34:33 -04001675 }
David Howells54ceac42006-08-22 20:06:13 -04001676
David Howellsf7b422b2006-06-09 09:34:33 -04001677#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001678 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001679 goto out_v3_not_compiled;
1680#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001681
David Howells54ceac42006-08-22 20:06:13 -04001682 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001683
1684out_no_data:
1685 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1686 return -EINVAL;
1687
1688out_no_v3:
1689 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1690 data->version);
1691 return -EINVAL;
1692
1693out_no_sec:
1694 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1695 return -EINVAL;
1696
Chuck Lever5df36e72007-07-01 12:12:56 -04001697#ifndef CONFIG_NFS_V3
1698out_v3_not_compiled:
1699 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1700 return -EPROTONOSUPPORT;
1701#endif /* !CONFIG_NFS_V3 */
1702
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001703out_nomem:
1704 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1705 return -ENOMEM;
1706
Chuck Lever5df36e72007-07-01 12:12:56 -04001707out_no_address:
1708 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1709 return -EINVAL;
1710
1711out_invalid_fh:
1712 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1713 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001714}
1715
Jeff Layton48b605f2008-06-10 15:38:39 -04001716static int
1717nfs_compare_remount_data(struct nfs_server *nfss,
1718 struct nfs_parsed_mount_data *data)
1719{
1720 if (data->flags != nfss->flags ||
1721 data->rsize != nfss->rsize ||
1722 data->wsize != nfss->wsize ||
1723 data->retrans != nfss->client->cl_timeout->to_retries ||
1724 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1725 data->acregmin != nfss->acregmin / HZ ||
1726 data->acregmax != nfss->acregmax / HZ ||
1727 data->acdirmin != nfss->acdirmin / HZ ||
1728 data->acdirmax != nfss->acdirmax / HZ ||
1729 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1730 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1731 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1732 data->nfs_server.addrlen) != 0)
1733 return -EINVAL;
1734
1735 return 0;
1736}
1737
1738static int
1739nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1740{
1741 int error;
1742 struct nfs_server *nfss = sb->s_fs_info;
1743 struct nfs_parsed_mount_data *data;
1744 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1745 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001746 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001747
1748 /*
1749 * Userspace mount programs that send binary options generally send
1750 * them populated with default values. We have no way to know which
1751 * ones were explicitly specified. Fall back to legacy behavior and
1752 * just return success.
1753 */
Marc Zyngier31c94462008-07-17 13:21:55 +02001754 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1755 (nfsvers <= 3 && (!options || (options->version >= 1 &&
1756 options->version <= 6))))
Jeff Layton48b605f2008-06-10 15:38:39 -04001757 return 0;
1758
1759 data = kzalloc(sizeof(*data), GFP_KERNEL);
1760 if (data == NULL)
1761 return -ENOMEM;
1762
1763 /* fill out struct with values from existing mount */
1764 data->flags = nfss->flags;
1765 data->rsize = nfss->rsize;
1766 data->wsize = nfss->wsize;
1767 data->retrans = nfss->client->cl_timeout->to_retries;
1768 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1769 data->acregmin = nfss->acregmin / HZ;
1770 data->acregmax = nfss->acregmax / HZ;
1771 data->acdirmin = nfss->acdirmin / HZ;
1772 data->acdirmax = nfss->acdirmax / HZ;
1773 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1774 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1775 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1776 data->nfs_server.addrlen);
1777
1778 /* overwrite those values with any that were specified */
1779 error = nfs_parse_mount_options((char *)options, data);
1780 if (error < 0)
1781 goto out;
1782
1783 /* compare new mount options with old ones */
1784 error = nfs_compare_remount_data(nfss, data);
1785out:
1786 kfree(data);
1787 return error;
1788}
1789
David Howells54ceac42006-08-22 20:06:13 -04001790/*
1791 * Initialise the common bits of the superblock
1792 */
1793static inline void nfs_initialise_sb(struct super_block *sb)
1794{
1795 struct nfs_server *server = NFS_SB(sb);
1796
1797 sb->s_magic = NFS_SUPER_MAGIC;
1798
1799 /* We probably want something more informative here */
1800 snprintf(sb->s_id, sizeof(sb->s_id),
1801 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1802
1803 if (sb->s_blocksize == 0)
1804 sb->s_blocksize = nfs_block_bits(server->wsize,
1805 &sb->s_blocksize_bits);
1806
1807 if (server->flags & NFS_MOUNT_NOAC)
1808 sb->s_flags |= MS_SYNCHRONOUS;
1809
1810 nfs_super_set_maxbytes(sb, server->maxfilesize);
1811}
1812
1813/*
1814 * Finish setting up an NFS2/3 superblock
1815 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001816static void nfs_fill_super(struct super_block *sb,
1817 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001818{
1819 struct nfs_server *server = NFS_SB(sb);
1820
1821 sb->s_blocksize_bits = 0;
1822 sb->s_blocksize = 0;
1823 if (data->bsize)
1824 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1825
1826 if (server->flags & NFS_MOUNT_VER3) {
1827 /* The VFS shouldn't apply the umask to mode bits. We will do
1828 * so ourselves when necessary.
1829 */
1830 sb->s_flags |= MS_POSIXACL;
1831 sb->s_time_gran = 1;
1832 }
1833
1834 sb->s_op = &nfs_sops;
1835 nfs_initialise_sb(sb);
1836}
1837
1838/*
1839 * Finish setting up a cloned NFS2/3 superblock
1840 */
1841static void nfs_clone_super(struct super_block *sb,
1842 const struct super_block *old_sb)
1843{
1844 struct nfs_server *server = NFS_SB(sb);
1845
1846 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1847 sb->s_blocksize = old_sb->s_blocksize;
1848 sb->s_maxbytes = old_sb->s_maxbytes;
1849
1850 if (server->flags & NFS_MOUNT_VER3) {
1851 /* The VFS shouldn't apply the umask to mode bits. We will do
1852 * so ourselves when necessary.
1853 */
1854 sb->s_flags |= MS_POSIXACL;
1855 sb->s_time_gran = 1;
1856 }
1857
1858 sb->s_op = old_sb->s_op;
1859 nfs_initialise_sb(sb);
1860}
1861
Trond Myklebust275a5d22007-05-16 16:53:28 -04001862#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1863
1864static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1865{
1866 const struct nfs_server *a = s->s_fs_info;
1867 const struct rpc_clnt *clnt_a = a->client;
1868 const struct rpc_clnt *clnt_b = b->client;
1869
1870 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1871 goto Ebusy;
1872 if (a->nfs_client != b->nfs_client)
1873 goto Ebusy;
1874 if (a->flags != b->flags)
1875 goto Ebusy;
1876 if (a->wsize != b->wsize)
1877 goto Ebusy;
1878 if (a->rsize != b->rsize)
1879 goto Ebusy;
1880 if (a->acregmin != b->acregmin)
1881 goto Ebusy;
1882 if (a->acregmax != b->acregmax)
1883 goto Ebusy;
1884 if (a->acdirmin != b->acdirmin)
1885 goto Ebusy;
1886 if (a->acdirmax != b->acdirmax)
1887 goto Ebusy;
1888 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1889 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001890 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001891Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001892 return 0;
1893}
1894
1895struct nfs_sb_mountdata {
1896 struct nfs_server *server;
1897 int mntflags;
1898};
1899
1900static int nfs_set_super(struct super_block *s, void *data)
1901{
1902 struct nfs_sb_mountdata *sb_mntdata = data;
1903 struct nfs_server *server = sb_mntdata->server;
1904 int ret;
1905
1906 s->s_flags = sb_mntdata->mntflags;
1907 s->s_fs_info = server;
1908 ret = set_anon_super(s, server);
1909 if (ret == 0)
1910 server->s_dev = s->s_dev;
1911 return ret;
1912}
1913
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001914static int nfs_compare_super_address(struct nfs_server *server1,
1915 struct nfs_server *server2)
1916{
1917 struct sockaddr *sap1, *sap2;
1918
1919 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1920 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1921
1922 if (sap1->sa_family != sap2->sa_family)
1923 return 0;
1924
1925 switch (sap1->sa_family) {
1926 case AF_INET: {
1927 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1928 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1929 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1930 return 0;
1931 if (sin1->sin_port != sin2->sin_port)
1932 return 0;
1933 break;
1934 }
1935 case AF_INET6: {
1936 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1937 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1938 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1939 return 0;
1940 if (sin1->sin6_port != sin2->sin6_port)
1941 return 0;
1942 break;
1943 }
1944 default:
1945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001951static int nfs_compare_super(struct super_block *sb, void *data)
1952{
1953 struct nfs_sb_mountdata *sb_mntdata = data;
1954 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1955 int mntflags = sb_mntdata->mntflags;
1956
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001957 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001958 return 0;
1959 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1960 if (old->flags & NFS_MOUNT_UNSHARED)
1961 return 0;
1962 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1963 return 0;
1964 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001965}
1966
Miklos Szeredifa799752008-04-30 00:54:33 -07001967static int nfs_bdi_register(struct nfs_server *server)
1968{
1969 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1970}
1971
David Howells54ceac42006-08-22 20:06:13 -04001972static int nfs_get_sb(struct file_system_type *fs_type,
1973 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1974{
1975 struct nfs_server *server = NULL;
1976 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001977 struct nfs_parsed_mount_data *data;
1978 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001979 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001980 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001981 struct nfs_sb_mountdata sb_mntdata = {
1982 .mntflags = flags,
1983 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001984 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001985
Trond Myklebust33852a12008-06-19 14:20:11 -04001986 data = kzalloc(sizeof(*data), GFP_KERNEL);
1987 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1988 if (data == NULL || mntfh == NULL)
1989 goto out_free_fh;
1990
1991 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001992
David Howells54ceac42006-08-22 20:06:13 -04001993 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04001994 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04001995 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001996 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001997
1998 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001999 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002000 if (IS_ERR(server)) {
2001 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04002002 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002003 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002004 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002005
Trond Myklebust75180df2007-05-16 16:53:28 -04002006 if (server->flags & NFS_MOUNT_UNSHARED)
2007 compare_super = NULL;
2008
David Howells54ceac42006-08-22 20:06:13 -04002009 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002010 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002011 if (IS_ERR(s)) {
2012 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04002013 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04002014 }
2015
David Howells54ceac42006-08-22 20:06:13 -04002016 if (s->s_fs_info != server) {
2017 nfs_free_server(server);
2018 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002019 } else {
2020 error = nfs_bdi_register(server);
2021 if (error)
2022 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002023 }
David Howells54ceac42006-08-22 20:06:13 -04002024
2025 if (!s->s_root) {
2026 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002027 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04002028 }
2029
Trond Myklebust33852a12008-06-19 14:20:11 -04002030 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002031 if (IS_ERR(mntroot)) {
2032 error = PTR_ERR(mntroot);
2033 goto error_splat_super;
2034 }
2035
Trond Myklebust33852a12008-06-19 14:20:11 -04002036 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002037 if (error)
2038 goto error_splat_root;
2039
David Howellsf7b422b2006-06-09 09:34:33 -04002040 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002041 mnt->mnt_sb = s;
2042 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002043 error = 0;
2044
2045out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002046 kfree(data->nfs_server.hostname);
2047 kfree(data->mount_server.hostname);
2048 security_free_mnt_opts(&data->lsm_opts);
2049out_free_fh:
2050 kfree(mntfh);
2051 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002052 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002053
David Howells54ceac42006-08-22 20:06:13 -04002054out_err_nosb:
2055 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002056 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002057
Eric Parisf9c3a382008-03-05 14:20:18 -05002058error_splat_root:
2059 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002060error_splat_super:
2061 up_write(&s->s_umount);
2062 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002063 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002064}
2065
David Howells54ceac42006-08-22 20:06:13 -04002066/*
2067 * Destroy an NFS2/3 superblock
2068 */
David Howellsf7b422b2006-06-09 09:34:33 -04002069static void nfs_kill_super(struct super_block *s)
2070{
2071 struct nfs_server *server = NFS_SB(s);
2072
Miklos Szeredifa799752008-04-30 00:54:33 -07002073 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002074 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002075 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002076}
2077
David Howells54ceac42006-08-22 20:06:13 -04002078/*
2079 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2080 */
2081static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2082 const char *dev_name, void *raw_data,
2083 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002084{
2085 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002086 struct super_block *s;
2087 struct nfs_server *server;
2088 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002089 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002090 struct nfs_sb_mountdata sb_mntdata = {
2091 .mntflags = flags,
2092 };
David Howells54ceac42006-08-22 20:06:13 -04002093 int error;
2094
2095 dprintk("--> nfs_xdev_get_sb()\n");
2096
2097 /* create a new volume representation */
2098 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2099 if (IS_ERR(server)) {
2100 error = PTR_ERR(server);
2101 goto out_err_noserver;
2102 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002103 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002104
Trond Myklebust75180df2007-05-16 16:53:28 -04002105 if (server->flags & NFS_MOUNT_UNSHARED)
2106 compare_super = NULL;
2107
David Howells54ceac42006-08-22 20:06:13 -04002108 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002109 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002110 if (IS_ERR(s)) {
2111 error = PTR_ERR(s);
2112 goto out_err_nosb;
2113 }
2114
2115 if (s->s_fs_info != server) {
2116 nfs_free_server(server);
2117 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002118 } else {
2119 error = nfs_bdi_register(server);
2120 if (error)
2121 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002122 }
2123
2124 if (!s->s_root) {
2125 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002126 nfs_clone_super(s, data->sb);
2127 }
2128
2129 mntroot = nfs_get_root(s, data->fh);
2130 if (IS_ERR(mntroot)) {
2131 error = PTR_ERR(mntroot);
2132 goto error_splat_super;
2133 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002134 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002135 dput(mntroot);
2136 error = -ESTALE;
2137 goto error_splat_super;
2138 }
David Howells54ceac42006-08-22 20:06:13 -04002139
2140 s->s_flags |= MS_ACTIVE;
2141 mnt->mnt_sb = s;
2142 mnt->mnt_root = mntroot;
2143
Eric Parisf9c3a382008-03-05 14:20:18 -05002144 /* clone any lsm security options from the parent to the new sb */
2145 security_sb_clone_mnt_opts(data->sb, s);
2146
David Howells54ceac42006-08-22 20:06:13 -04002147 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2148 return 0;
2149
2150out_err_nosb:
2151 nfs_free_server(server);
2152out_err_noserver:
2153 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2154 return error;
2155
2156error_splat_super:
2157 up_write(&s->s_umount);
2158 deactivate_super(s);
2159 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2160 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002161}
2162
2163#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002164
2165/*
2166 * Finish setting up a cloned NFS4 superblock
2167 */
2168static void nfs4_clone_super(struct super_block *sb,
2169 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002170{
David Howells54ceac42006-08-22 20:06:13 -04002171 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2172 sb->s_blocksize = old_sb->s_blocksize;
2173 sb->s_maxbytes = old_sb->s_maxbytes;
2174 sb->s_time_gran = 1;
2175 sb->s_op = old_sb->s_op;
2176 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002177}
2178
2179/*
2180 * Set up an NFS4 superblock
2181 */
David Howells54ceac42006-08-22 20:06:13 -04002182static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002183{
David Howellsf7b422b2006-06-09 09:34:33 -04002184 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002185 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002186 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002187}
2188
David Howells54ceac42006-08-22 20:06:13 -04002189/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002190 * Validate NFSv4 mount options
2191 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002192static int nfs4_validate_mount_data(void *options,
2193 struct nfs_parsed_mount_data *args,
2194 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002195{
Chuck Lever4c568012007-12-10 14:59:28 -05002196 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002197 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002198 char *c;
2199
2200 if (data == NULL)
2201 goto out_no_data;
2202
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002203 args->rsize = NFS_MAX_FILE_IO_SIZE;
2204 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002205 args->acregmin = NFS_DEF_ACREGMIN;
2206 args->acregmax = NFS_DEF_ACREGMAX;
2207 args->acdirmin = NFS_DEF_ACDIRMIN;
2208 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002209 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002210 args->auth_flavors[0] = RPC_AUTH_UNIX;
2211 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002212
Chuck Leverf0768eb2007-07-01 12:13:01 -04002213 switch (data->version) {
2214 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002215 ap = (struct sockaddr_in *)&args->nfs_server.address;
2216 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002217 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002218 if (data->host_addrlen == 0)
2219 goto out_no_address;
2220 args->nfs_server.addrlen = data->host_addrlen;
2221 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002222 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002223 if (!nfs_verify_server_address((struct sockaddr *)
2224 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002225 goto out_no_address;
2226
Chuck Lever6738b252008-06-24 16:33:54 -04002227 if (data->auth_flavourlen) {
2228 if (data->auth_flavourlen > 1)
2229 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002230 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002231 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002232 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002233 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002234 }
2235
2236 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2237 if (IS_ERR(c))
2238 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002239 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002240
2241 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2242 if (IS_ERR(c))
2243 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002244 args->nfs_server.export_path = c;
2245 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002246
2247 c = strndup_user(data->client_addr.data, 16);
2248 if (IS_ERR(c))
2249 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002250 args->client_address = c;
2251
2252 /*
2253 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2254 * can deal with.
2255 */
2256
2257 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2258 args->rsize = data->rsize;
2259 args->wsize = data->wsize;
2260 args->timeo = data->timeo;
2261 args->retrans = data->retrans;
2262 args->acregmin = data->acregmin;
2263 args->acregmax = data->acregmax;
2264 args->acdirmin = data->acdirmin;
2265 args->acdirmax = data->acdirmax;
2266 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002267 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002268
2269 break;
Chuck Lever80071222007-07-01 12:13:59 -04002270 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002271 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002272
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002273 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002274 return -EINVAL;
2275
2276 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002277 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002278 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002279
Chuck Levered596a82008-06-26 17:47:05 -04002280 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2281 args->nfs_server.port);
2282
Trond Myklebust259875e2008-07-02 14:43:47 -04002283 nfs_validate_transport_protocol(args);
2284
Chuck Lever6738b252008-06-24 16:33:54 -04002285 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002286 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002287
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002288 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002289 goto out_no_client_address;
2290
Chuck Leverdc045892008-06-23 12:36:37 -04002291 status = nfs_parse_devname(dev_name,
2292 &args->nfs_server.hostname,
2293 NFS4_MAXNAMLEN,
2294 &args->nfs_server.export_path,
2295 NFS4_MAXPATHLEN);
2296 if (status < 0)
2297 return status;
2298
Chuck Lever80071222007-07-01 12:13:59 -04002299 break;
2300 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002301 }
2302
2303 return 0;
2304
2305out_no_data:
2306 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2307 return -EINVAL;
2308
2309out_inval_auth:
2310 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2311 data->auth_flavourlen);
2312 return -EINVAL;
2313
2314out_no_address:
2315 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2316 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002317
2318out_no_client_address:
2319 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2320 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002321}
2322
2323/*
David Howells54ceac42006-08-22 20:06:13 -04002324 * Get the superblock for an NFS4 mountpoint
2325 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002326static int nfs4_get_sb(struct file_system_type *fs_type,
2327 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002328{
Trond Myklebust33852a12008-06-19 14:20:11 -04002329 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002330 struct super_block *s;
2331 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002332 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002333 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002334 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002335 struct nfs_sb_mountdata sb_mntdata = {
2336 .mntflags = flags,
2337 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002338 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002339
Trond Myklebust33852a12008-06-19 14:20:11 -04002340 data = kzalloc(sizeof(*data), GFP_KERNEL);
2341 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2342 if (data == NULL || mntfh == NULL)
2343 goto out_free_fh;
2344
2345 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002346
Chuck Leverf0768eb2007-07-01 12:13:01 -04002347 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002348 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002349 if (error < 0)
2350 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002351
David Howells54ceac42006-08-22 20:06:13 -04002352 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002353 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002354 if (IS_ERR(server)) {
2355 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002356 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002357 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002358 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002359
Trond Myklebust75180df2007-05-16 16:53:28 -04002360 if (server->flags & NFS4_MOUNT_UNSHARED)
2361 compare_super = NULL;
2362
David Howells54ceac42006-08-22 20:06:13 -04002363 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002364 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002365 if (IS_ERR(s)) {
2366 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002367 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002368 }
2369
Trond Myklebust5dd31772006-08-24 01:03:05 -04002370 if (s->s_fs_info != server) {
2371 nfs_free_server(server);
2372 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002373 } else {
2374 error = nfs_bdi_register(server);
2375 if (error)
2376 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002377 }
2378
David Howells54ceac42006-08-22 20:06:13 -04002379 if (!s->s_root) {
2380 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002381 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002382 }
David Howellsf7b422b2006-06-09 09:34:33 -04002383
Trond Myklebust33852a12008-06-19 14:20:11 -04002384 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002385 if (IS_ERR(mntroot)) {
2386 error = PTR_ERR(mntroot);
2387 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002388 }
David Howells54ceac42006-08-22 20:06:13 -04002389
Trond Myklebust33852a12008-06-19 14:20:11 -04002390 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002391 if (error)
2392 goto error_splat_root;
2393
David Howellsf7b422b2006-06-09 09:34:33 -04002394 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002395 mnt->mnt_sb = s;
2396 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002397 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002398
Chuck Lever29eb9812007-07-01 12:12:30 -04002399out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002400 kfree(data->client_address);
2401 kfree(data->nfs_server.export_path);
2402 kfree(data->nfs_server.hostname);
2403 security_free_mnt_opts(&data->lsm_opts);
2404out_free_fh:
2405 kfree(mntfh);
2406 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002407 return error;
David Howells54ceac42006-08-22 20:06:13 -04002408
Chuck Lever29eb9812007-07-01 12:12:30 -04002409out_free:
2410 nfs_free_server(server);
2411 goto out;
2412
Eric Paris46c8ac72008-05-02 13:42:42 -07002413error_splat_root:
2414 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002415error_splat_super:
2416 up_write(&s->s_umount);
2417 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002418 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002419}
2420
2421static void nfs4_kill_super(struct super_block *sb)
2422{
2423 struct nfs_server *server = NFS_SB(sb);
2424
2425 nfs_return_all_delegations(sb);
2426 kill_anon_super(sb);
2427
2428 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002429 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002430}
2431
2432/*
David Howells54ceac42006-08-22 20:06:13 -04002433 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002434 */
David Howells54ceac42006-08-22 20:06:13 -04002435static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2436 const char *dev_name, void *raw_data,
2437 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002438{
2439 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002440 struct super_block *s;
2441 struct nfs_server *server;
2442 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002443 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002444 struct nfs_sb_mountdata sb_mntdata = {
2445 .mntflags = flags,
2446 };
David Howells54ceac42006-08-22 20:06:13 -04002447 int error;
2448
2449 dprintk("--> nfs4_xdev_get_sb()\n");
2450
2451 /* create a new volume representation */
2452 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2453 if (IS_ERR(server)) {
2454 error = PTR_ERR(server);
2455 goto out_err_noserver;
2456 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002457 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002458
Trond Myklebust75180df2007-05-16 16:53:28 -04002459 if (server->flags & NFS4_MOUNT_UNSHARED)
2460 compare_super = NULL;
2461
David Howells54ceac42006-08-22 20:06:13 -04002462 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002463 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002464 if (IS_ERR(s)) {
2465 error = PTR_ERR(s);
2466 goto out_err_nosb;
2467 }
2468
2469 if (s->s_fs_info != server) {
2470 nfs_free_server(server);
2471 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002472 } else {
2473 error = nfs_bdi_register(server);
2474 if (error)
2475 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002476 }
2477
2478 if (!s->s_root) {
2479 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002480 nfs4_clone_super(s, data->sb);
2481 }
2482
2483 mntroot = nfs4_get_root(s, data->fh);
2484 if (IS_ERR(mntroot)) {
2485 error = PTR_ERR(mntroot);
2486 goto error_splat_super;
2487 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002488 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2489 dput(mntroot);
2490 error = -ESTALE;
2491 goto error_splat_super;
2492 }
David Howells54ceac42006-08-22 20:06:13 -04002493
2494 s->s_flags |= MS_ACTIVE;
2495 mnt->mnt_sb = s;
2496 mnt->mnt_root = mntroot;
2497
Eric Paris46c8ac72008-05-02 13:42:42 -07002498 security_sb_clone_mnt_opts(data->sb, s);
2499
David Howells54ceac42006-08-22 20:06:13 -04002500 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2501 return 0;
2502
2503out_err_nosb:
2504 nfs_free_server(server);
2505out_err_noserver:
2506 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2507 return error;
2508
2509error_splat_super:
2510 up_write(&s->s_umount);
2511 deactivate_super(s);
2512 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2513 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002514}
2515
David Howells54ceac42006-08-22 20:06:13 -04002516/*
2517 * Create an NFS4 server record on referral traversal
2518 */
2519static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2520 const char *dev_name, void *raw_data,
2521 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002522{
2523 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002524 struct super_block *s;
2525 struct nfs_server *server;
2526 struct dentry *mntroot;
2527 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002528 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002529 struct nfs_sb_mountdata sb_mntdata = {
2530 .mntflags = flags,
2531 };
David Howells54ceac42006-08-22 20:06:13 -04002532 int error;
2533
2534 dprintk("--> nfs4_referral_get_sb()\n");
2535
2536 /* create a new volume representation */
2537 server = nfs4_create_referral_server(data, &mntfh);
2538 if (IS_ERR(server)) {
2539 error = PTR_ERR(server);
2540 goto out_err_noserver;
2541 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002542 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002543
Trond Myklebust75180df2007-05-16 16:53:28 -04002544 if (server->flags & NFS4_MOUNT_UNSHARED)
2545 compare_super = NULL;
2546
David Howells54ceac42006-08-22 20:06:13 -04002547 /* Get a superblock - note that we may end up sharing one that already exists */
Andy Adamsonec9a05c2008-10-17 10:44:37 -04002548 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002549 if (IS_ERR(s)) {
2550 error = PTR_ERR(s);
2551 goto out_err_nosb;
2552 }
2553
2554 if (s->s_fs_info != server) {
2555 nfs_free_server(server);
2556 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002557 } else {
2558 error = nfs_bdi_register(server);
2559 if (error)
2560 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002561 }
2562
2563 if (!s->s_root) {
2564 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002565 nfs4_fill_super(s);
2566 }
2567
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002568 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002569 if (IS_ERR(mntroot)) {
2570 error = PTR_ERR(mntroot);
2571 goto error_splat_super;
2572 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002573 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2574 dput(mntroot);
2575 error = -ESTALE;
2576 goto error_splat_super;
2577 }
David Howells54ceac42006-08-22 20:06:13 -04002578
2579 s->s_flags |= MS_ACTIVE;
2580 mnt->mnt_sb = s;
2581 mnt->mnt_root = mntroot;
2582
Eric Paris46c8ac72008-05-02 13:42:42 -07002583 security_sb_clone_mnt_opts(data->sb, s);
2584
David Howells54ceac42006-08-22 20:06:13 -04002585 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2586 return 0;
2587
2588out_err_nosb:
2589 nfs_free_server(server);
2590out_err_noserver:
2591 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2592 return error;
2593
2594error_splat_super:
2595 up_write(&s->s_umount);
2596 deactivate_super(s);
2597 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2598 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002599}
2600
David Howells54ceac42006-08-22 20:06:13 -04002601#endif /* CONFIG_NFS_V4 */