blob: 48db52a7067a626e96aad2a06249f661671823e9 [file] [log] [blame]
David Howellsf7b422b2006-06-09 09:34:33 -04001/*
2 * linux/fs/nfs/super.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs superblock handling functions
7 *
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
10 *
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
13 *
14 * Split from inode.c by David Howells <dhowells@redhat.com>
15 *
David Howells54ceac42006-08-22 20:06:13 -040016 * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17 * particular server are held in the same superblock
18 * - NFS superblocks can have several effective roots to the dentry tree
19 * - directory type roots are spliced into the tree when a path from one root reaches the root
20 * of another (see nfs_lookup())
David Howellsf7b422b2006-06-09 09:34:33 -040021 */
22
David Howellsf7b422b2006-06-09 09:34:33 -040023#include <linux/module.h>
24#include <linux/init.h>
25
26#include <linux/time.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/string.h>
30#include <linux/stat.h>
31#include <linux/errno.h>
32#include <linux/unistd.h>
33#include <linux/sunrpc/clnt.h>
34#include <linux/sunrpc/stats.h>
35#include <linux/sunrpc/metrics.h>
36#include <linux/nfs_fs.h>
37#include <linux/nfs_mount.h>
38#include <linux/nfs4_mount.h>
39#include <linux/lockd/bind.h>
40#include <linux/smp_lock.h>
41#include <linux/seq_file.h>
42#include <linux/mount.h>
43#include <linux/nfs_idmap.h>
44#include <linux/vfs.h>
45#include <linux/inet.h>
46#include <linux/nfs_xdr.h>
Adrian Bunkb5d5dfb2007-02-12 00:53:40 -080047#include <linux/magic.h>
Chuck Leverbf0fd762007-07-01 12:13:44 -040048#include <linux/parser.h>
David Howellsf7b422b2006-06-09 09:34:33 -040049
50#include <asm/system.h>
51#include <asm/uaccess.h>
52
53#include "nfs4_fs.h"
54#include "callback.h"
55#include "delegation.h"
56#include "iostat.h"
57#include "internal.h"
58
59#define NFSDBG_FACILITY NFSDBG_VFS
60
Chuck Leverbf0fd762007-07-01 12:13:44 -040061
62struct nfs_parsed_mount_data {
63 int flags;
64 int rsize, wsize;
65 int timeo, retrans;
66 int acregmin, acregmax,
67 acdirmin, acdirmax;
68 int namlen;
69 unsigned int bsize;
70 unsigned int auth_flavor_len;
71 rpc_authflavor_t auth_flavors[1];
72 char *client_address;
73
74 struct {
75 struct sockaddr_in address;
76 unsigned int program;
77 unsigned int version;
78 unsigned short port;
79 int protocol;
80 } mount_server;
81
82 struct {
83 struct sockaddr_in address;
84 char *hostname;
85 char *export_path;
86 unsigned int program;
87 int protocol;
88 } nfs_server;
89};
90
91enum {
92 /* Mount options that take no arguments */
93 Opt_soft, Opt_hard,
94 Opt_intr, Opt_nointr,
95 Opt_posix, Opt_noposix,
96 Opt_cto, Opt_nocto,
97 Opt_ac, Opt_noac,
98 Opt_lock, Opt_nolock,
99 Opt_v2, Opt_v3,
100 Opt_udp, Opt_tcp,
101 Opt_acl, Opt_noacl,
102 Opt_rdirplus, Opt_nordirplus,
103
104 /* Mount options that take integer arguments */
105 Opt_port,
106 Opt_rsize, Opt_wsize, Opt_bsize,
107 Opt_timeo, Opt_retrans,
108 Opt_acregmin, Opt_acregmax,
109 Opt_acdirmin, Opt_acdirmax,
110 Opt_actimeo,
111 Opt_namelen,
112 Opt_mountport,
113 Opt_mountprog, Opt_mountvers,
114 Opt_nfsprog, Opt_nfsvers,
115
116 /* Mount options that take string arguments */
117 Opt_sec, Opt_proto, Opt_mountproto,
118 Opt_addr, Opt_mounthost, Opt_clientaddr,
119
120 /* Mount options that are ignored */
121 Opt_userspace, Opt_deprecated,
122
123 Opt_err
124};
125
126static match_table_t nfs_mount_option_tokens = {
127 { Opt_userspace, "bg" },
128 { Opt_userspace, "fg" },
129 { Opt_soft, "soft" },
130 { Opt_hard, "hard" },
131 { Opt_intr, "intr" },
132 { Opt_nointr, "nointr" },
133 { Opt_posix, "posix" },
134 { Opt_noposix, "noposix" },
135 { Opt_cto, "cto" },
136 { Opt_nocto, "nocto" },
137 { Opt_ac, "ac" },
138 { Opt_noac, "noac" },
139 { Opt_lock, "lock" },
140 { Opt_nolock, "nolock" },
141 { Opt_v2, "v2" },
142 { Opt_v3, "v3" },
143 { Opt_udp, "udp" },
144 { Opt_tcp, "tcp" },
145 { Opt_acl, "acl" },
146 { Opt_noacl, "noacl" },
147 { Opt_rdirplus, "rdirplus" },
148 { Opt_nordirplus, "nordirplus" },
149
150 { Opt_port, "port=%u" },
151 { Opt_rsize, "rsize=%u" },
152 { Opt_wsize, "wsize=%u" },
153 { Opt_bsize, "bsize=%u" },
154 { Opt_timeo, "timeo=%u" },
155 { Opt_retrans, "retrans=%u" },
156 { Opt_acregmin, "acregmin=%u" },
157 { Opt_acregmax, "acregmax=%u" },
158 { Opt_acdirmin, "acdirmin=%u" },
159 { Opt_acdirmax, "acdirmax=%u" },
160 { Opt_actimeo, "actimeo=%u" },
161 { Opt_userspace, "retry=%u" },
162 { Opt_namelen, "namlen=%u" },
163 { Opt_mountport, "mountport=%u" },
164 { Opt_mountprog, "mountprog=%u" },
165 { Opt_mountvers, "mountvers=%u" },
166 { Opt_nfsprog, "nfsprog=%u" },
167 { Opt_nfsvers, "nfsvers=%u" },
168 { Opt_nfsvers, "vers=%u" },
169
170 { Opt_sec, "sec=%s" },
171 { Opt_proto, "proto=%s" },
172 { Opt_mountproto, "mountproto=%s" },
173 { Opt_addr, "addr=%s" },
174 { Opt_clientaddr, "clientaddr=%s" },
175 { Opt_mounthost, "mounthost=%s" },
176
177 { Opt_err, NULL }
178};
179
180enum {
181 Opt_xprt_udp, Opt_xprt_tcp,
182
183 Opt_xprt_err
184};
185
186static match_table_t nfs_xprt_protocol_tokens = {
187 { Opt_xprt_udp, "udp" },
188 { Opt_xprt_tcp, "tcp" },
189
190 { Opt_xprt_err, NULL }
191};
192
193enum {
194 Opt_sec_none, Opt_sec_sys,
195 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
196 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
197 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
198
199 Opt_sec_err
200};
201
202static match_table_t nfs_secflavor_tokens = {
203 { Opt_sec_none, "none" },
204 { Opt_sec_none, "null" },
205 { Opt_sec_sys, "sys" },
206
207 { Opt_sec_krb5, "krb5" },
208 { Opt_sec_krb5i, "krb5i" },
209 { Opt_sec_krb5p, "krb5p" },
210
211 { Opt_sec_lkey, "lkey" },
212 { Opt_sec_lkeyi, "lkeyi" },
213 { Opt_sec_lkeyp, "lkeyp" },
214
215 { Opt_sec_err, NULL }
216};
217
218
David Howellsf7b422b2006-06-09 09:34:33 -0400219static void nfs_umount_begin(struct vfsmount *, int);
Trond Myklebust816724e2006-06-24 08:41:41 -0400220static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400221static int nfs_show_options(struct seq_file *, struct vfsmount *);
222static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400223static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400224static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400225 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400226static void nfs_kill_super(struct super_block *);
227
228static struct file_system_type nfs_fs_type = {
229 .owner = THIS_MODULE,
230 .name = "nfs",
231 .get_sb = nfs_get_sb,
232 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700233 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400234};
235
David Howells54ceac42006-08-22 20:06:13 -0400236struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400237 .owner = THIS_MODULE,
238 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400239 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400240 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700241 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400242};
243
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800244static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400245 .alloc_inode = nfs_alloc_inode,
246 .destroy_inode = nfs_destroy_inode,
247 .write_inode = nfs_write_inode,
248 .statfs = nfs_statfs,
249 .clear_inode = nfs_clear_inode,
250 .umount_begin = nfs_umount_begin,
251 .show_options = nfs_show_options,
252 .show_stats = nfs_show_stats,
253};
254
255#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400256static int nfs4_get_sb(struct file_system_type *fs_type,
257 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400258static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
259 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
260static int nfs4_referral_get_sb(struct file_system_type *fs_type,
261 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400262static void nfs4_kill_super(struct super_block *sb);
263
264static struct file_system_type nfs4_fs_type = {
265 .owner = THIS_MODULE,
266 .name = "nfs4",
267 .get_sb = nfs4_get_sb,
268 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700269 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400270};
271
David Howells54ceac42006-08-22 20:06:13 -0400272struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400273 .owner = THIS_MODULE,
274 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400275 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400276 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700277 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400278};
279
David Howells54ceac42006-08-22 20:06:13 -0400280struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400281 .owner = THIS_MODULE,
282 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400283 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400284 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700285 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400286};
287
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800288static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400289 .alloc_inode = nfs_alloc_inode,
290 .destroy_inode = nfs_destroy_inode,
291 .write_inode = nfs_write_inode,
292 .statfs = nfs_statfs,
293 .clear_inode = nfs4_clear_inode,
294 .umount_begin = nfs_umount_begin,
295 .show_options = nfs_show_options,
296 .show_stats = nfs_show_stats,
297};
298#endif
299
Trond Myklebust979df722006-07-25 11:28:19 -0400300static struct shrinker *acl_shrinker;
301
David Howellsf7b422b2006-06-09 09:34:33 -0400302/*
303 * Register the NFS filesystems
304 */
305int __init register_nfs_fs(void)
306{
307 int ret;
308
309 ret = register_filesystem(&nfs_fs_type);
310 if (ret < 0)
311 goto error_0;
312
David Howellsf7b422b2006-06-09 09:34:33 -0400313 ret = nfs_register_sysctl();
314 if (ret < 0)
315 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800316#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400317 ret = register_filesystem(&nfs4_fs_type);
318 if (ret < 0)
319 goto error_2;
320#endif
Trond Myklebust979df722006-07-25 11:28:19 -0400321 acl_shrinker = set_shrinker(DEFAULT_SEEKS, nfs_access_cache_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400322 return 0;
323
324#ifdef CONFIG_NFS_V4
325error_2:
326 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800327#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400328error_1:
329 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400330error_0:
331 return ret;
332}
333
334/*
335 * Unregister the NFS filesystems
336 */
337void __exit unregister_nfs_fs(void)
338{
Trond Myklebust979df722006-07-25 11:28:19 -0400339 if (acl_shrinker != NULL)
340 remove_shrinker(acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400341#ifdef CONFIG_NFS_V4
342 unregister_filesystem(&nfs4_fs_type);
343 nfs_unregister_sysctl();
344#endif
345 unregister_filesystem(&nfs_fs_type);
346}
347
348/*
349 * Deliver file system statistics to userspace
350 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400351static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400352{
David Howells0c7d90c2006-08-22 20:06:10 -0400353 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400354 unsigned char blockbits;
355 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400356 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400357 struct nfs_fattr fattr;
358 struct nfs_fsstat res = {
359 .fattr = &fattr,
360 };
361 int error;
362
363 lock_kernel();
364
David Howells8fa5c002006-08-22 20:06:12 -0400365 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400366 if (error < 0)
367 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700368 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400369
370 /*
371 * Current versions of glibc do not correctly handle the
372 * case where f_frsize != f_bsize. Eventually we want to
373 * report the value of wtmult in this field.
374 */
David Howells0c7d90c2006-08-22 20:06:10 -0400375 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400376
377 /*
378 * On most *nix systems, f_blocks, f_bfree, and f_bavail
379 * are reported in units of f_frsize. Linux hasn't had
380 * an f_frsize field in its statfs struct until recently,
381 * thus historically Linux's sys_statfs reports these
382 * fields in units of f_bsize.
383 */
David Howells0c7d90c2006-08-22 20:06:10 -0400384 buf->f_bsize = dentry->d_sb->s_blocksize;
385 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400386 blockres = (1 << blockbits) - 1;
387 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
388 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
389 buf->f_bavail = (res.abytes + blockres) >> blockbits;
390
391 buf->f_files = res.tfiles;
392 buf->f_ffree = res.afiles;
393
394 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700395
David Howellsf7b422b2006-06-09 09:34:33 -0400396 unlock_kernel();
397 return 0;
398
399 out_err:
400 dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700401 unlock_kernel();
402 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400403}
404
David Howells7d4e2742006-08-22 20:06:07 -0400405/*
406 * Map the security flavour number to a name
407 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400408static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
409{
David Howells7d4e2742006-08-22 20:06:07 -0400410 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400411 rpc_authflavor_t flavour;
412 const char *str;
413 } sec_flavours[] = {
414 { RPC_AUTH_NULL, "null" },
415 { RPC_AUTH_UNIX, "sys" },
416 { RPC_AUTH_GSS_KRB5, "krb5" },
417 { RPC_AUTH_GSS_KRB5I, "krb5i" },
418 { RPC_AUTH_GSS_KRB5P, "krb5p" },
419 { RPC_AUTH_GSS_LKEY, "lkey" },
420 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
421 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
422 { RPC_AUTH_GSS_SPKM, "spkm" },
423 { RPC_AUTH_GSS_SPKMI, "spkmi" },
424 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400425 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400426 };
427 int i;
428
Chuck Lever4d81cd12007-07-01 12:12:40 -0400429 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400430 if (sec_flavours[i].flavour == flavour)
431 break;
432 }
433 return sec_flavours[i].str;
434}
435
David Howellsf7b422b2006-06-09 09:34:33 -0400436/*
437 * Describe the mount options in force on this server representation
438 */
439static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
440{
David Howells509de812006-08-22 20:06:11 -0400441 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400442 int flag;
David Howells509de812006-08-22 20:06:11 -0400443 const char *str;
444 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400445 } nfs_info[] = {
446 { NFS_MOUNT_SOFT, ",soft", ",hard" },
447 { NFS_MOUNT_INTR, ",intr", "" },
448 { NFS_MOUNT_NOCTO, ",nocto", "" },
449 { NFS_MOUNT_NOAC, ",noac", "" },
450 { NFS_MOUNT_NONLM, ",nolock", "" },
451 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400452 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400453 { 0, NULL, NULL }
454 };
David Howells509de812006-08-22 20:06:11 -0400455 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400456 struct nfs_client *clp = nfss->nfs_client;
David Howellsf7b422b2006-06-09 09:34:33 -0400457 char buf[12];
David Howells509de812006-08-22 20:06:11 -0400458 const char *proto;
David Howellsf7b422b2006-06-09 09:34:33 -0400459
David Howells8fa5c002006-08-22 20:06:12 -0400460 seq_printf(m, ",vers=%d", clp->rpc_ops->version);
David Howellsf7b422b2006-06-09 09:34:33 -0400461 seq_printf(m, ",rsize=%d", nfss->rsize);
462 seq_printf(m, ",wsize=%d", nfss->wsize);
463 if (nfss->acregmin != 3*HZ || showdefaults)
464 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
465 if (nfss->acregmax != 60*HZ || showdefaults)
466 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
467 if (nfss->acdirmin != 30*HZ || showdefaults)
468 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
469 if (nfss->acdirmax != 60*HZ || showdefaults)
470 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
471 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
472 if (nfss->flags & nfs_infop->flag)
473 seq_puts(m, nfs_infop->str);
474 else
475 seq_puts(m, nfs_infop->nostr);
476 }
477 switch (nfss->client->cl_xprt->prot) {
478 case IPPROTO_TCP:
479 proto = "tcp";
480 break;
481 case IPPROTO_UDP:
482 proto = "udp";
483 break;
484 default:
485 snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
486 proto = buf;
487 }
488 seq_printf(m, ",proto=%s", proto);
David Howells5006a762006-08-22 20:06:12 -0400489 seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
490 seq_printf(m, ",retrans=%u", clp->retrans_count);
Trond Myklebust81039f12006-06-09 09:34:34 -0400491 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
David Howellsf7b422b2006-06-09 09:34:33 -0400492}
493
494/*
495 * Describe the mount options on this VFS mountpoint
496 */
497static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
498{
499 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
500
501 nfs_show_mount_options(m, nfss, 0);
502
503 seq_puts(m, ",addr=");
David Howells54ceac42006-08-22 20:06:13 -0400504 seq_escape(m, nfss->nfs_client->cl_hostname, " \t\n\\");
David Howellsf7b422b2006-06-09 09:34:33 -0400505
506 return 0;
507}
508
509/*
510 * Present statistical information for this VFS mountpoint
511 */
512static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
513{
514 int i, cpu;
515 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
516 struct rpc_auth *auth = nfss->client->cl_auth;
517 struct nfs_iostats totals = { };
518
519 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
520
521 /*
522 * Display all mount option settings
523 */
524 seq_printf(m, "\n\topts:\t");
525 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
526 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
527 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
528 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
529 nfs_show_mount_options(m, nfss, 1);
530
531 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
532
533 seq_printf(m, "\n\tcaps:\t");
534 seq_printf(m, "caps=0x%x", nfss->caps);
535 seq_printf(m, ",wtmult=%d", nfss->wtmult);
536 seq_printf(m, ",dtsize=%d", nfss->dtsize);
537 seq_printf(m, ",bsize=%d", nfss->bsize);
538 seq_printf(m, ",namelen=%d", nfss->namelen);
539
540#ifdef CONFIG_NFS_V4
David Howells8fa5c002006-08-22 20:06:12 -0400541 if (nfss->nfs_client->cl_nfsversion == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400542 seq_printf(m, "\n\tnfsv4:\t");
543 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
544 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
545 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
546 }
547#endif
548
549 /*
550 * Display security flavor in effect for this mount
551 */
552 seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
553 if (auth->au_flavor)
554 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
555
556 /*
557 * Display superblock I/O counters
558 */
559 for_each_possible_cpu(cpu) {
560 struct nfs_iostats *stats;
561
562 preempt_disable();
563 stats = per_cpu_ptr(nfss->io_stats, cpu);
564
565 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
566 totals.events[i] += stats->events[i];
567 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
568 totals.bytes[i] += stats->bytes[i];
569
570 preempt_enable();
571 }
572
573 seq_printf(m, "\n\tevents:\t");
574 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
575 seq_printf(m, "%lu ", totals.events[i]);
576 seq_printf(m, "\n\tbytes:\t");
577 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
578 seq_printf(m, "%Lu ", totals.bytes[i]);
579 seq_printf(m, "\n");
580
581 rpc_print_iostats(m, nfss->client);
582
583 return 0;
584}
585
586/*
587 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400588 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400589 */
590static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
591{
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400592 struct nfs_server *server = NFS_SB(vfsmnt->mnt_sb);
593 struct rpc_clnt *rpc;
594
David Howellsf7b422b2006-06-09 09:34:33 -0400595 shrink_submounts(vfsmnt, &nfs_automount_list);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400596
597 if (!(flags & MNT_FORCE))
598 return;
599 /* -EIO all pending I/O */
600 rpc = server->client_acl;
601 if (!IS_ERR(rpc))
602 rpc_killall_tasks(rpc);
603 rpc = server->client;
604 if (!IS_ERR(rpc))
605 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400606}
607
608/*
Chuck Leverfc50d582007-07-01 12:12:46 -0400609 * Sanity-check a server address provided by the mount command
610 */
611static int nfs_verify_server_address(struct sockaddr *addr)
612{
613 switch (addr->sa_family) {
614 case AF_INET: {
615 struct sockaddr_in *sa = (struct sockaddr_in *) addr;
616 if (sa->sin_addr.s_addr != INADDR_ANY)
617 return 1;
618 break;
619 }
620 }
621
622 return 0;
623}
624
625/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400626 * Error-check and convert a string of mount options from user space into
627 * a data structure
628 */
629static int nfs_parse_mount_options(char *raw,
630 struct nfs_parsed_mount_data *mnt)
631{
632 char *p, *string;
633
634 if (!raw) {
635 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
636 return 1;
637 }
638 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
639
640 while ((p = strsep(&raw, ",")) != NULL) {
641 substring_t args[MAX_OPT_ARGS];
642 int option, token;
643
644 if (!*p)
645 continue;
646
647 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
648
649 token = match_token(p, nfs_mount_option_tokens, args);
650 switch (token) {
651 case Opt_soft:
652 mnt->flags |= NFS_MOUNT_SOFT;
653 break;
654 case Opt_hard:
655 mnt->flags &= ~NFS_MOUNT_SOFT;
656 break;
657 case Opt_intr:
658 mnt->flags |= NFS_MOUNT_INTR;
659 break;
660 case Opt_nointr:
661 mnt->flags &= ~NFS_MOUNT_INTR;
662 break;
663 case Opt_posix:
664 mnt->flags |= NFS_MOUNT_POSIX;
665 break;
666 case Opt_noposix:
667 mnt->flags &= ~NFS_MOUNT_POSIX;
668 break;
669 case Opt_cto:
670 mnt->flags &= ~NFS_MOUNT_NOCTO;
671 break;
672 case Opt_nocto:
673 mnt->flags |= NFS_MOUNT_NOCTO;
674 break;
675 case Opt_ac:
676 mnt->flags &= ~NFS_MOUNT_NOAC;
677 break;
678 case Opt_noac:
679 mnt->flags |= NFS_MOUNT_NOAC;
680 break;
681 case Opt_lock:
682 mnt->flags &= ~NFS_MOUNT_NONLM;
683 break;
684 case Opt_nolock:
685 mnt->flags |= NFS_MOUNT_NONLM;
686 break;
687 case Opt_v2:
688 mnt->flags &= ~NFS_MOUNT_VER3;
689 break;
690 case Opt_v3:
691 mnt->flags |= NFS_MOUNT_VER3;
692 break;
693 case Opt_udp:
694 mnt->flags &= ~NFS_MOUNT_TCP;
695 mnt->nfs_server.protocol = IPPROTO_UDP;
696 mnt->timeo = 7;
697 mnt->retrans = 5;
698 break;
699 case Opt_tcp:
700 mnt->flags |= NFS_MOUNT_TCP;
701 mnt->nfs_server.protocol = IPPROTO_TCP;
702 mnt->timeo = 600;
703 mnt->retrans = 2;
704 break;
705 case Opt_acl:
706 mnt->flags &= ~NFS_MOUNT_NOACL;
707 break;
708 case Opt_noacl:
709 mnt->flags |= NFS_MOUNT_NOACL;
710 break;
711 case Opt_rdirplus:
712 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
713 break;
714 case Opt_nordirplus:
715 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
716 break;
717
718 case Opt_port:
719 if (match_int(args, &option))
720 return 0;
721 if (option < 0 || option > 65535)
722 return 0;
723 mnt->nfs_server.address.sin_port = htonl(option);
724 break;
725 case Opt_rsize:
726 if (match_int(args, &mnt->rsize))
727 return 0;
728 break;
729 case Opt_wsize:
730 if (match_int(args, &mnt->wsize))
731 return 0;
732 break;
733 case Opt_bsize:
734 if (match_int(args, &option))
735 return 0;
736 if (option < 0)
737 return 0;
738 mnt->bsize = option;
739 break;
740 case Opt_timeo:
741 if (match_int(args, &mnt->timeo))
742 return 0;
743 break;
744 case Opt_retrans:
745 if (match_int(args, &mnt->retrans))
746 return 0;
747 break;
748 case Opt_acregmin:
749 if (match_int(args, &mnt->acregmin))
750 return 0;
751 break;
752 case Opt_acregmax:
753 if (match_int(args, &mnt->acregmax))
754 return 0;
755 break;
756 case Opt_acdirmin:
757 if (match_int(args, &mnt->acdirmin))
758 return 0;
759 break;
760 case Opt_acdirmax:
761 if (match_int(args, &mnt->acdirmax))
762 return 0;
763 break;
764 case Opt_actimeo:
765 if (match_int(args, &option))
766 return 0;
767 if (option < 0)
768 return 0;
769 mnt->acregmin =
770 mnt->acregmax =
771 mnt->acdirmin =
772 mnt->acdirmax = option;
773 break;
774 case Opt_namelen:
775 if (match_int(args, &mnt->namlen))
776 return 0;
777 break;
778 case Opt_mountport:
779 if (match_int(args, &option))
780 return 0;
781 if (option < 0 || option > 65535)
782 return 0;
783 mnt->mount_server.port = option;
784 break;
785 case Opt_mountprog:
786 if (match_int(args, &option))
787 return 0;
788 if (option < 0)
789 return 0;
790 mnt->mount_server.program = option;
791 break;
792 case Opt_mountvers:
793 if (match_int(args, &option))
794 return 0;
795 if (option < 0)
796 return 0;
797 mnt->mount_server.version = option;
798 break;
799 case Opt_nfsprog:
800 if (match_int(args, &option))
801 return 0;
802 if (option < 0)
803 return 0;
804 mnt->nfs_server.program = option;
805 break;
806 case Opt_nfsvers:
807 if (match_int(args, &option))
808 return 0;
809 switch (option) {
810 case 2:
811 mnt->flags &= ~NFS_MOUNT_VER3;
812 break;
813 case 3:
814 mnt->flags |= NFS_MOUNT_VER3;
815 break;
816 default:
817 goto out_unrec_vers;
818 }
819 break;
820
821 case Opt_sec:
822 string = match_strdup(args);
823 if (string == NULL)
824 goto out_nomem;
825 token = match_token(string, nfs_secflavor_tokens, args);
826 kfree(string);
827
828 /*
829 * The flags setting is for v2/v3. The flavor_len
830 * setting is for v4. v2/v3 also need to know the
831 * difference between NULL and UNIX.
832 */
833 switch (token) {
834 case Opt_sec_none:
835 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
836 mnt->auth_flavor_len = 0;
837 mnt->auth_flavors[0] = RPC_AUTH_NULL;
838 break;
839 case Opt_sec_sys:
840 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
841 mnt->auth_flavor_len = 0;
842 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
843 break;
844 case Opt_sec_krb5:
845 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
846 mnt->auth_flavor_len = 1;
847 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
848 break;
849 case Opt_sec_krb5i:
850 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
851 mnt->auth_flavor_len = 1;
852 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
853 break;
854 case Opt_sec_krb5p:
855 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
856 mnt->auth_flavor_len = 1;
857 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
858 break;
859 case Opt_sec_lkey:
860 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
861 mnt->auth_flavor_len = 1;
862 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
863 break;
864 case Opt_sec_lkeyi:
865 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
866 mnt->auth_flavor_len = 1;
867 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
868 break;
869 case Opt_sec_lkeyp:
870 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
871 mnt->auth_flavor_len = 1;
872 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
873 break;
874 case Opt_sec_spkm:
875 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
876 mnt->auth_flavor_len = 1;
877 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
878 break;
879 case Opt_sec_spkmi:
880 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
881 mnt->auth_flavor_len = 1;
882 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
883 break;
884 case Opt_sec_spkmp:
885 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
886 mnt->auth_flavor_len = 1;
887 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
888 break;
889 default:
890 goto out_unrec_sec;
891 }
892 break;
893 case Opt_proto:
894 string = match_strdup(args);
895 if (string == NULL)
896 goto out_nomem;
897 token = match_token(string,
898 nfs_xprt_protocol_tokens, args);
899 kfree(string);
900
901 switch (token) {
902 case Opt_udp:
903 mnt->flags &= ~NFS_MOUNT_TCP;
904 mnt->nfs_server.protocol = IPPROTO_UDP;
905 mnt->timeo = 7;
906 mnt->retrans = 5;
907 break;
908 case Opt_tcp:
909 mnt->flags |= NFS_MOUNT_TCP;
910 mnt->nfs_server.protocol = IPPROTO_TCP;
911 mnt->timeo = 600;
912 mnt->retrans = 2;
913 break;
914 default:
915 goto out_unrec_xprt;
916 }
917 break;
918 case Opt_mountproto:
919 string = match_strdup(args);
920 if (string == NULL)
921 goto out_nomem;
922 token = match_token(string,
923 nfs_xprt_protocol_tokens, args);
924 kfree(string);
925
926 switch (token) {
927 case Opt_udp:
928 mnt->mount_server.protocol = IPPROTO_UDP;
929 break;
930 case Opt_tcp:
931 mnt->mount_server.protocol = IPPROTO_TCP;
932 break;
933 default:
934 goto out_unrec_xprt;
935 }
936 break;
937 case Opt_addr:
938 string = match_strdup(args);
939 if (string == NULL)
940 goto out_nomem;
941 mnt->nfs_server.address.sin_family = AF_INET;
942 mnt->nfs_server.address.sin_addr.s_addr =
943 in_aton(string);
944 kfree(string);
945 break;
946 case Opt_clientaddr:
947 string = match_strdup(args);
948 if (string == NULL)
949 goto out_nomem;
950 mnt->client_address = string;
951 break;
952 case Opt_mounthost:
953 string = match_strdup(args);
954 if (string == NULL)
955 goto out_nomem;
956 mnt->mount_server.address.sin_family = AF_INET;
957 mnt->mount_server.address.sin_addr.s_addr =
958 in_aton(string);
959 kfree(string);
960 break;
961
962 case Opt_userspace:
963 case Opt_deprecated:
964 break;
965
966 default:
967 goto out_unknown;
968 }
969 }
970
971 return 1;
972
973out_nomem:
974 printk(KERN_INFO "NFS: not enough memory to parse option\n");
975 return 0;
976
977out_unrec_vers:
978 printk(KERN_INFO "NFS: unrecognized NFS version number\n");
979 return 0;
980
981out_unrec_xprt:
982 printk(KERN_INFO "NFS: unrecognized transport protocol\n");
983 return 0;
984
985out_unrec_sec:
986 printk(KERN_INFO "NFS: unrecognized security flavor\n");
987 return 0;
988
989out_unknown:
990 printk(KERN_INFO "NFS: unknown mount option: %s\n", p);
991 return 0;
992}
993
994/*
Chuck Lever0076d7b2007-07-01 12:13:49 -0400995 * Use the remote server's MOUNT service to request the NFS file handle
996 * corresponding to the provided path.
997 */
998static int nfs_try_mount(struct nfs_parsed_mount_data *args,
999 struct nfs_fh *root_fh)
1000{
1001 struct sockaddr_in sin;
1002 int status;
1003
1004 if (args->mount_server.version == 0) {
1005 if (args->flags & NFS_MOUNT_VER3)
1006 args->mount_server.version = NFS_MNT3_VERSION;
1007 else
1008 args->mount_server.version = NFS_MNT_VERSION;
1009 }
1010
1011 /*
1012 * Construct the mount server's address.
1013 */
1014 if (args->mount_server.address.sin_addr.s_addr != INADDR_ANY)
1015 sin = args->mount_server.address;
1016 else
1017 sin = args->nfs_server.address;
1018 if (args->mount_server.port == 0) {
1019 status = rpcb_getport_sync(&sin,
1020 args->mount_server.program,
1021 args->mount_server.version,
1022 args->mount_server.protocol);
1023 if (status < 0)
1024 goto out_err;
1025 sin.sin_port = htons(status);
1026 } else
1027 sin.sin_port = htons(args->mount_server.port);
1028
1029 /*
1030 * Now ask the mount server to map our export path
1031 * to a file handle.
1032 */
1033 status = nfs_mount((struct sockaddr *) &sin,
1034 sizeof(sin),
1035 args->nfs_server.hostname,
1036 args->nfs_server.export_path,
1037 args->mount_server.version,
1038 args->mount_server.protocol,
1039 root_fh);
1040 if (status < 0)
1041 goto out_err;
1042
1043 return status;
1044
1045out_err:
1046 dfprintk(MOUNT, "NFS: unable to contact server on host "
1047 NIPQUAD_FMT "\n", NIPQUAD(sin.sin_addr.s_addr));
1048 return status;
1049}
1050
1051/*
David Howells54ceac42006-08-22 20:06:13 -04001052 * Validate the NFS2/NFS3 mount data
1053 * - fills in the mount root filehandle
David Howellsf7b422b2006-06-09 09:34:33 -04001054 */
David Howells54ceac42006-08-22 20:06:13 -04001055static int nfs_validate_mount_data(struct nfs_mount_data *data,
1056 struct nfs_fh *mntfh)
David Howellsf7b422b2006-06-09 09:34:33 -04001057{
Chuck Lever5df36e72007-07-01 12:12:56 -04001058 if (data == NULL)
1059 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001060
David Howellsf7b422b2006-06-09 09:34:33 -04001061 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001062 case 1:
1063 data->namlen = 0;
1064 case 2:
1065 data->bsize = 0;
1066 case 3:
1067 if (data->flags & NFS_MOUNT_VER3)
1068 goto out_no_v3;
1069 data->root.size = NFS2_FHSIZE;
1070 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1071 case 4:
1072 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1073 goto out_no_sec;
1074 case 5:
1075 memset(data->context, 0, sizeof(data->context));
1076 case 6:
1077 if (data->flags & NFS_MOUNT_VER3)
1078 mntfh->size = data->root.size;
1079 else
1080 mntfh->size = NFS2_FHSIZE;
1081
1082 if (mntfh->size > sizeof(mntfh->data))
1083 goto out_invalid_fh;
1084
1085 memcpy(mntfh->data, data->root.data, mntfh->size);
1086 if (mntfh->size < sizeof(mntfh->data))
1087 memset(mntfh->data + mntfh->size, 0,
1088 sizeof(mntfh->data) - mntfh->size);
1089 break;
1090 default:
1091 goto out_bad_version;
David Howellsf7b422b2006-06-09 09:34:33 -04001092 }
David Howells54ceac42006-08-22 20:06:13 -04001093
Trond Myklebust36b15c52006-08-22 20:06:14 -04001094 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
1095 data->pseudoflavor = RPC_AUTH_UNIX;
1096
David Howellsf7b422b2006-06-09 09:34:33 -04001097#ifndef CONFIG_NFS_V3
David Howells54ceac42006-08-22 20:06:13 -04001098 if (data->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001099 goto out_v3_not_compiled;
1100#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001101
Chuck Lever5df36e72007-07-01 12:12:56 -04001102 if (!nfs_verify_server_address((struct sockaddr *) &data->addr))
1103 goto out_no_address;
David Howells54ceac42006-08-22 20:06:13 -04001104
1105 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001106
1107out_no_data:
1108 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1109 return -EINVAL;
1110
1111out_no_v3:
1112 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1113 data->version);
1114 return -EINVAL;
1115
1116out_no_sec:
1117 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1118 return -EINVAL;
1119
1120out_bad_version:
1121 dfprintk(MOUNT, "NFS: bad nfs_mount_data version %d\n",
1122 data->version);
1123 return -EINVAL;
1124
1125#ifndef CONFIG_NFS_V3
1126out_v3_not_compiled:
1127 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1128 return -EPROTONOSUPPORT;
1129#endif /* !CONFIG_NFS_V3 */
1130
1131out_no_address:
1132 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1133 return -EINVAL;
1134
1135out_invalid_fh:
1136 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1137 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001138}
1139
1140/*
1141 * Initialise the common bits of the superblock
1142 */
1143static inline void nfs_initialise_sb(struct super_block *sb)
1144{
1145 struct nfs_server *server = NFS_SB(sb);
1146
1147 sb->s_magic = NFS_SUPER_MAGIC;
1148
1149 /* We probably want something more informative here */
1150 snprintf(sb->s_id, sizeof(sb->s_id),
1151 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1152
1153 if (sb->s_blocksize == 0)
1154 sb->s_blocksize = nfs_block_bits(server->wsize,
1155 &sb->s_blocksize_bits);
1156
1157 if (server->flags & NFS_MOUNT_NOAC)
1158 sb->s_flags |= MS_SYNCHRONOUS;
1159
1160 nfs_super_set_maxbytes(sb, server->maxfilesize);
1161}
1162
1163/*
1164 * Finish setting up an NFS2/3 superblock
1165 */
1166static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
1167{
1168 struct nfs_server *server = NFS_SB(sb);
1169
1170 sb->s_blocksize_bits = 0;
1171 sb->s_blocksize = 0;
1172 if (data->bsize)
1173 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1174
1175 if (server->flags & NFS_MOUNT_VER3) {
1176 /* The VFS shouldn't apply the umask to mode bits. We will do
1177 * so ourselves when necessary.
1178 */
1179 sb->s_flags |= MS_POSIXACL;
1180 sb->s_time_gran = 1;
1181 }
1182
1183 sb->s_op = &nfs_sops;
1184 nfs_initialise_sb(sb);
1185}
1186
1187/*
1188 * Finish setting up a cloned NFS2/3 superblock
1189 */
1190static void nfs_clone_super(struct super_block *sb,
1191 const struct super_block *old_sb)
1192{
1193 struct nfs_server *server = NFS_SB(sb);
1194
1195 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1196 sb->s_blocksize = old_sb->s_blocksize;
1197 sb->s_maxbytes = old_sb->s_maxbytes;
1198
1199 if (server->flags & NFS_MOUNT_VER3) {
1200 /* The VFS shouldn't apply the umask to mode bits. We will do
1201 * so ourselves when necessary.
1202 */
1203 sb->s_flags |= MS_POSIXACL;
1204 sb->s_time_gran = 1;
1205 }
1206
1207 sb->s_op = old_sb->s_op;
1208 nfs_initialise_sb(sb);
1209}
1210
1211static int nfs_set_super(struct super_block *s, void *_server)
1212{
1213 struct nfs_server *server = _server;
1214 int ret;
1215
1216 s->s_fs_info = server;
1217 ret = set_anon_super(s, server);
1218 if (ret == 0)
1219 server->s_dev = s->s_dev;
1220 return ret;
1221}
1222
1223static int nfs_compare_super(struct super_block *sb, void *data)
1224{
1225 struct nfs_server *server = data, *old = NFS_SB(sb);
1226
1227 if (old->nfs_client != server->nfs_client)
1228 return 0;
1229 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1230 return 0;
1231 return 1;
1232}
1233
1234static int nfs_get_sb(struct file_system_type *fs_type,
1235 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1236{
1237 struct nfs_server *server = NULL;
1238 struct super_block *s;
1239 struct nfs_fh mntfh;
1240 struct nfs_mount_data *data = raw_data;
1241 struct dentry *mntroot;
1242 int error;
1243
1244 /* Validate the mount data */
1245 error = nfs_validate_mount_data(data, &mntfh);
1246 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001247 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001248
1249 /* Get a volume representation */
1250 server = nfs_create_server(data, &mntfh);
1251 if (IS_ERR(server)) {
1252 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04001253 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001254 }
1255
1256 /* Get a superblock - note that we may end up sharing one that already exists */
David Howellsf7b422b2006-06-09 09:34:33 -04001257 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
Trond Myklebust816724e2006-06-24 08:41:41 -04001258 if (IS_ERR(s)) {
1259 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04001260 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04001261 }
1262
David Howells54ceac42006-08-22 20:06:13 -04001263 if (s->s_fs_info != server) {
1264 nfs_free_server(server);
1265 server = NULL;
David Howellsf7b422b2006-06-09 09:34:33 -04001266 }
David Howells54ceac42006-08-22 20:06:13 -04001267
1268 if (!s->s_root) {
1269 /* initial superblock/root creation */
1270 s->s_flags = flags;
1271 nfs_fill_super(s, data);
1272 }
1273
1274 mntroot = nfs_get_root(s, &mntfh);
1275 if (IS_ERR(mntroot)) {
1276 error = PTR_ERR(mntroot);
1277 goto error_splat_super;
1278 }
1279
David Howellsf7b422b2006-06-09 09:34:33 -04001280 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04001281 mnt->mnt_sb = s;
1282 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04001283 error = 0;
1284
1285out:
1286 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04001287
David Howells54ceac42006-08-22 20:06:13 -04001288out_err_nosb:
1289 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04001290 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001291
1292error_splat_super:
1293 up_write(&s->s_umount);
1294 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04001295 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04001296}
1297
David Howells54ceac42006-08-22 20:06:13 -04001298/*
1299 * Destroy an NFS2/3 superblock
1300 */
David Howellsf7b422b2006-06-09 09:34:33 -04001301static void nfs_kill_super(struct super_block *s)
1302{
1303 struct nfs_server *server = NFS_SB(s);
1304
1305 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04001306 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04001307}
1308
David Howells54ceac42006-08-22 20:06:13 -04001309/*
1310 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
1311 */
1312static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
1313 const char *dev_name, void *raw_data,
1314 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001315{
1316 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04001317 struct super_block *s;
1318 struct nfs_server *server;
1319 struct dentry *mntroot;
1320 int error;
1321
1322 dprintk("--> nfs_xdev_get_sb()\n");
1323
1324 /* create a new volume representation */
1325 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1326 if (IS_ERR(server)) {
1327 error = PTR_ERR(server);
1328 goto out_err_noserver;
1329 }
1330
1331 /* Get a superblock - note that we may end up sharing one that already exists */
1332 s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1333 if (IS_ERR(s)) {
1334 error = PTR_ERR(s);
1335 goto out_err_nosb;
1336 }
1337
1338 if (s->s_fs_info != server) {
1339 nfs_free_server(server);
1340 server = NULL;
1341 }
1342
1343 if (!s->s_root) {
1344 /* initial superblock/root creation */
1345 s->s_flags = flags;
1346 nfs_clone_super(s, data->sb);
1347 }
1348
1349 mntroot = nfs_get_root(s, data->fh);
1350 if (IS_ERR(mntroot)) {
1351 error = PTR_ERR(mntroot);
1352 goto error_splat_super;
1353 }
1354
1355 s->s_flags |= MS_ACTIVE;
1356 mnt->mnt_sb = s;
1357 mnt->mnt_root = mntroot;
1358
1359 dprintk("<-- nfs_xdev_get_sb() = 0\n");
1360 return 0;
1361
1362out_err_nosb:
1363 nfs_free_server(server);
1364out_err_noserver:
1365 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
1366 return error;
1367
1368error_splat_super:
1369 up_write(&s->s_umount);
1370 deactivate_super(s);
1371 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
1372 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001373}
1374
1375#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04001376
1377/*
1378 * Finish setting up a cloned NFS4 superblock
1379 */
1380static void nfs4_clone_super(struct super_block *sb,
1381 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04001382{
David Howells54ceac42006-08-22 20:06:13 -04001383 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1384 sb->s_blocksize = old_sb->s_blocksize;
1385 sb->s_maxbytes = old_sb->s_maxbytes;
1386 sb->s_time_gran = 1;
1387 sb->s_op = old_sb->s_op;
1388 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04001389}
1390
1391/*
1392 * Set up an NFS4 superblock
1393 */
David Howells54ceac42006-08-22 20:06:13 -04001394static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04001395{
David Howellsf7b422b2006-06-09 09:34:33 -04001396 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04001397 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04001398 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04001399}
1400
David Howells54ceac42006-08-22 20:06:13 -04001401/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04001402 * Validate NFSv4 mount options
1403 */
1404static int nfs4_validate_mount_data(struct nfs4_mount_data **options,
1405 const char *dev_name,
1406 struct sockaddr_in *addr,
1407 rpc_authflavor_t *authflavour,
1408 char **hostname,
1409 char **mntpath,
1410 char **ip_addr)
1411{
1412 struct nfs4_mount_data *data = *options;
1413 char *c;
1414
1415 if (data == NULL)
1416 goto out_no_data;
1417
1418 switch (data->version) {
1419 case 1:
1420 if (data->host_addrlen != sizeof(*addr))
1421 goto out_no_address;
1422 if (copy_from_user(addr, data->host_addr, sizeof(*addr)))
1423 return -EFAULT;
1424 if (addr->sin_port == 0)
1425 addr->sin_port = htons(NFS_PORT);
1426 if (!nfs_verify_server_address((struct sockaddr *) addr))
1427 goto out_no_address;
1428
1429 switch (data->auth_flavourlen) {
1430 case 0:
1431 *authflavour = RPC_AUTH_UNIX;
1432 break;
1433 case 1:
1434 if (copy_from_user(authflavour, data->auth_flavours,
1435 sizeof(*authflavour)))
1436 return -EFAULT;
1437 break;
1438 default:
1439 goto out_inval_auth;
1440 }
1441
1442 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1443 if (IS_ERR(c))
1444 return PTR_ERR(c);
1445 *hostname = c;
1446
1447 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1448 if (IS_ERR(c))
1449 return PTR_ERR(c);
1450 *mntpath = c;
1451 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *mntpath);
1452
1453 c = strndup_user(data->client_addr.data, 16);
1454 if (IS_ERR(c))
1455 return PTR_ERR(c);
1456 *ip_addr = c;
1457
1458 break;
1459 default:
1460 goto out_bad_version;
1461 }
1462
1463 return 0;
1464
1465out_no_data:
1466 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
1467 return -EINVAL;
1468
1469out_inval_auth:
1470 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
1471 data->auth_flavourlen);
1472 return -EINVAL;
1473
1474out_no_address:
1475 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
1476 return -EINVAL;
1477
1478out_bad_version:
1479 dfprintk(MOUNT, "NFS4: bad nfs_mount_data version %d\n",
1480 data->version);
1481 return -EINVAL;
1482}
1483
1484/*
David Howells54ceac42006-08-22 20:06:13 -04001485 * Get the superblock for an NFS4 mountpoint
1486 */
Trond Myklebust816724e2006-06-24 08:41:41 -04001487static int nfs4_get_sb(struct file_system_type *fs_type,
1488 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001489{
David Howellsf7b422b2006-06-09 09:34:33 -04001490 struct nfs4_mount_data *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04001491 struct super_block *s;
1492 struct nfs_server *server;
1493 struct sockaddr_in addr;
1494 rpc_authflavor_t authflavour;
1495 struct nfs_fh mntfh;
1496 struct dentry *mntroot;
Chuck Leverf0768eb2007-07-01 12:13:01 -04001497 char *mntpath = NULL, *hostname = NULL, *ip_addr = NULL;
David Howells54ceac42006-08-22 20:06:13 -04001498 int error;
David Howellsf7b422b2006-06-09 09:34:33 -04001499
Chuck Leverf0768eb2007-07-01 12:13:01 -04001500 /* Validate the mount data */
1501 error = nfs4_validate_mount_data(&data, dev_name, &addr, &authflavour,
1502 &hostname, &mntpath, &ip_addr);
1503 if (error < 0)
1504 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04001505
David Howells54ceac42006-08-22 20:06:13 -04001506 /* Get a volume representation */
1507 server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
1508 authflavour, &mntfh);
1509 if (IS_ERR(server)) {
1510 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04001511 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04001512 }
1513
David Howells54ceac42006-08-22 20:06:13 -04001514 /* Get a superblock - note that we may end up sharing one that already exists */
1515 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
Trond Myklebust816724e2006-06-24 08:41:41 -04001516 if (IS_ERR(s)) {
1517 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04001518 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04001519 }
1520
Trond Myklebust5dd31772006-08-24 01:03:05 -04001521 if (s->s_fs_info != server) {
1522 nfs_free_server(server);
1523 server = NULL;
1524 }
1525
David Howells54ceac42006-08-22 20:06:13 -04001526 if (!s->s_root) {
1527 /* initial superblock/root creation */
1528 s->s_flags = flags;
David Howells54ceac42006-08-22 20:06:13 -04001529 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04001530 }
David Howellsf7b422b2006-06-09 09:34:33 -04001531
David Howells54ceac42006-08-22 20:06:13 -04001532 mntroot = nfs4_get_root(s, &mntfh);
1533 if (IS_ERR(mntroot)) {
1534 error = PTR_ERR(mntroot);
1535 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04001536 }
David Howells54ceac42006-08-22 20:06:13 -04001537
David Howellsf7b422b2006-06-09 09:34:33 -04001538 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04001539 mnt->mnt_sb = s;
1540 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04001541 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04001542
Chuck Lever29eb9812007-07-01 12:12:30 -04001543out:
1544 kfree(ip_addr);
David Howells54ceac42006-08-22 20:06:13 -04001545 kfree(mntpath);
1546 kfree(hostname);
Trond Myklebust816724e2006-06-24 08:41:41 -04001547 return error;
David Howells54ceac42006-08-22 20:06:13 -04001548
Chuck Lever29eb9812007-07-01 12:12:30 -04001549out_free:
1550 nfs_free_server(server);
1551 goto out;
1552
David Howells54ceac42006-08-22 20:06:13 -04001553error_splat_super:
1554 up_write(&s->s_umount);
1555 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04001556 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04001557}
1558
1559static void nfs4_kill_super(struct super_block *sb)
1560{
1561 struct nfs_server *server = NFS_SB(sb);
1562
1563 nfs_return_all_delegations(sb);
1564 kill_anon_super(sb);
1565
1566 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04001567 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04001568}
1569
1570/*
David Howells54ceac42006-08-22 20:06:13 -04001571 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04001572 */
David Howells54ceac42006-08-22 20:06:13 -04001573static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
1574 const char *dev_name, void *raw_data,
1575 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001576{
1577 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04001578 struct super_block *s;
1579 struct nfs_server *server;
1580 struct dentry *mntroot;
1581 int error;
1582
1583 dprintk("--> nfs4_xdev_get_sb()\n");
1584
1585 /* create a new volume representation */
1586 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1587 if (IS_ERR(server)) {
1588 error = PTR_ERR(server);
1589 goto out_err_noserver;
1590 }
1591
1592 /* Get a superblock - note that we may end up sharing one that already exists */
1593 s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1594 if (IS_ERR(s)) {
1595 error = PTR_ERR(s);
1596 goto out_err_nosb;
1597 }
1598
1599 if (s->s_fs_info != server) {
1600 nfs_free_server(server);
1601 server = NULL;
1602 }
1603
1604 if (!s->s_root) {
1605 /* initial superblock/root creation */
1606 s->s_flags = flags;
1607 nfs4_clone_super(s, data->sb);
1608 }
1609
1610 mntroot = nfs4_get_root(s, data->fh);
1611 if (IS_ERR(mntroot)) {
1612 error = PTR_ERR(mntroot);
1613 goto error_splat_super;
1614 }
1615
1616 s->s_flags |= MS_ACTIVE;
1617 mnt->mnt_sb = s;
1618 mnt->mnt_root = mntroot;
1619
1620 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
1621 return 0;
1622
1623out_err_nosb:
1624 nfs_free_server(server);
1625out_err_noserver:
1626 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
1627 return error;
1628
1629error_splat_super:
1630 up_write(&s->s_umount);
1631 deactivate_super(s);
1632 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
1633 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001634}
1635
David Howells54ceac42006-08-22 20:06:13 -04001636/*
1637 * Create an NFS4 server record on referral traversal
1638 */
1639static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1640 const char *dev_name, void *raw_data,
1641 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001642{
1643 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04001644 struct super_block *s;
1645 struct nfs_server *server;
1646 struct dentry *mntroot;
1647 struct nfs_fh mntfh;
1648 int error;
1649
1650 dprintk("--> nfs4_referral_get_sb()\n");
1651
1652 /* create a new volume representation */
1653 server = nfs4_create_referral_server(data, &mntfh);
1654 if (IS_ERR(server)) {
1655 error = PTR_ERR(server);
1656 goto out_err_noserver;
1657 }
1658
1659 /* Get a superblock - note that we may end up sharing one that already exists */
1660 s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1661 if (IS_ERR(s)) {
1662 error = PTR_ERR(s);
1663 goto out_err_nosb;
1664 }
1665
1666 if (s->s_fs_info != server) {
1667 nfs_free_server(server);
1668 server = NULL;
1669 }
1670
1671 if (!s->s_root) {
1672 /* initial superblock/root creation */
1673 s->s_flags = flags;
1674 nfs4_fill_super(s);
1675 }
1676
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001677 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001678 if (IS_ERR(mntroot)) {
1679 error = PTR_ERR(mntroot);
1680 goto error_splat_super;
1681 }
1682
1683 s->s_flags |= MS_ACTIVE;
1684 mnt->mnt_sb = s;
1685 mnt->mnt_root = mntroot;
1686
1687 dprintk("<-- nfs4_referral_get_sb() = 0\n");
1688 return 0;
1689
1690out_err_nosb:
1691 nfs_free_server(server);
1692out_err_noserver:
1693 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1694 return error;
1695
1696error_splat_super:
1697 up_write(&s->s_umount);
1698 deactivate_super(s);
1699 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1700 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001701}
1702
David Howells54ceac42006-08-22 20:06:13 -04001703#endif /* CONFIG_NFS_V4 */