blob: 63497345806b318e48ae45028564b5c9b6b59d66 [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 *
16 */
17
18#include <linux/config.h>
19#include <linux/module.h>
20#include <linux/init.h>
21
22#include <linux/time.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/string.h>
26#include <linux/stat.h>
27#include <linux/errno.h>
28#include <linux/unistd.h>
29#include <linux/sunrpc/clnt.h>
30#include <linux/sunrpc/stats.h>
31#include <linux/sunrpc/metrics.h>
32#include <linux/nfs_fs.h>
33#include <linux/nfs_mount.h>
34#include <linux/nfs4_mount.h>
35#include <linux/lockd/bind.h>
36#include <linux/smp_lock.h>
37#include <linux/seq_file.h>
38#include <linux/mount.h>
39#include <linux/nfs_idmap.h>
40#include <linux/vfs.h>
41#include <linux/inet.h>
42#include <linux/nfs_xdr.h>
43
44#include <asm/system.h>
45#include <asm/uaccess.h>
46
47#include "nfs4_fs.h"
48#include "callback.h"
49#include "delegation.h"
50#include "iostat.h"
51#include "internal.h"
52
53#define NFSDBG_FACILITY NFSDBG_VFS
54
55/* Maximum number of readahead requests
56 * FIXME: this should really be a sysctl so that users may tune it to suit
57 * their needs. People that do NFS over a slow network, might for
58 * instance want to reduce it to something closer to 1 for improved
59 * interactive response.
60 */
61#define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
62
63/*
64 * RPC cruft for NFS
65 */
66static struct rpc_version * nfs_version[] = {
67 NULL,
68 NULL,
69 &nfs_version2,
70#if defined(CONFIG_NFS_V3)
71 &nfs_version3,
72#elif defined(CONFIG_NFS_V4)
73 NULL,
74#endif
75#if defined(CONFIG_NFS_V4)
76 &nfs_version4,
77#endif
78};
79
80static struct rpc_program nfs_program = {
81 .name = "nfs",
82 .number = NFS_PROGRAM,
83 .nrvers = ARRAY_SIZE(nfs_version),
84 .version = nfs_version,
85 .stats = &nfs_rpcstat,
86 .pipe_dir_name = "/nfs",
87};
88
89struct rpc_stat nfs_rpcstat = {
90 .program = &nfs_program
91};
92
93
94#ifdef CONFIG_NFS_V3_ACL
95static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
96static struct rpc_version * nfsacl_version[] = {
97 [3] = &nfsacl_version3,
98};
99
100struct rpc_program nfsacl_program = {
101 .name = "nfsacl",
102 .number = NFS_ACL_PROGRAM,
103 .nrvers = ARRAY_SIZE(nfsacl_version),
104 .version = nfsacl_version,
105 .stats = &nfsacl_rpcstat,
106};
107#endif /* CONFIG_NFS_V3_ACL */
108
109static void nfs_umount_begin(struct vfsmount *, int);
Trond Myklebust816724e2006-06-24 08:41:41 -0400110static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400111static int nfs_show_options(struct seq_file *, struct vfsmount *);
112static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400113static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
114static int nfs_clone_nfs_sb(struct file_system_type *fs_type,
115 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400116static void nfs_kill_super(struct super_block *);
117
118static struct file_system_type nfs_fs_type = {
119 .owner = THIS_MODULE,
120 .name = "nfs",
121 .get_sb = nfs_get_sb,
122 .kill_sb = nfs_kill_super,
123 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
124};
125
126struct file_system_type clone_nfs_fs_type = {
127 .owner = THIS_MODULE,
128 .name = "nfs",
129 .get_sb = nfs_clone_nfs_sb,
130 .kill_sb = nfs_kill_super,
131 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
132};
133
134static struct super_operations nfs_sops = {
135 .alloc_inode = nfs_alloc_inode,
136 .destroy_inode = nfs_destroy_inode,
137 .write_inode = nfs_write_inode,
138 .statfs = nfs_statfs,
139 .clear_inode = nfs_clear_inode,
140 .umount_begin = nfs_umount_begin,
141 .show_options = nfs_show_options,
142 .show_stats = nfs_show_stats,
143};
144
145#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400146static int nfs4_get_sb(struct file_system_type *fs_type,
147 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
148static int nfs_clone_nfs4_sb(struct file_system_type *fs_type,
149 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
150static int nfs_referral_nfs4_sb(struct file_system_type *fs_type,
151 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400152static void nfs4_kill_super(struct super_block *sb);
153
154static struct file_system_type nfs4_fs_type = {
155 .owner = THIS_MODULE,
156 .name = "nfs4",
157 .get_sb = nfs4_get_sb,
158 .kill_sb = nfs4_kill_super,
159 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
160};
161
162struct file_system_type clone_nfs4_fs_type = {
163 .owner = THIS_MODULE,
164 .name = "nfs4",
165 .get_sb = nfs_clone_nfs4_sb,
166 .kill_sb = nfs4_kill_super,
167 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
168};
169
170struct file_system_type nfs_referral_nfs4_fs_type = {
171 .owner = THIS_MODULE,
172 .name = "nfs4",
173 .get_sb = nfs_referral_nfs4_sb,
174 .kill_sb = nfs4_kill_super,
175 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
176};
177
178static struct super_operations nfs4_sops = {
179 .alloc_inode = nfs_alloc_inode,
180 .destroy_inode = nfs_destroy_inode,
181 .write_inode = nfs_write_inode,
182 .statfs = nfs_statfs,
183 .clear_inode = nfs4_clear_inode,
184 .umount_begin = nfs_umount_begin,
185 .show_options = nfs_show_options,
186 .show_stats = nfs_show_stats,
187};
188#endif
189
Trond Myklebust979df722006-07-25 11:28:19 -0400190static struct shrinker *acl_shrinker;
191
David Howellsf7b422b2006-06-09 09:34:33 -0400192/*
193 * Register the NFS filesystems
194 */
195int __init register_nfs_fs(void)
196{
197 int ret;
198
199 ret = register_filesystem(&nfs_fs_type);
200 if (ret < 0)
201 goto error_0;
202
203#ifdef CONFIG_NFS_V4
204 ret = nfs_register_sysctl();
205 if (ret < 0)
206 goto error_1;
207 ret = register_filesystem(&nfs4_fs_type);
208 if (ret < 0)
209 goto error_2;
210#endif
Trond Myklebust979df722006-07-25 11:28:19 -0400211 acl_shrinker = set_shrinker(DEFAULT_SEEKS, nfs_access_cache_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400212 return 0;
213
214#ifdef CONFIG_NFS_V4
215error_2:
216 nfs_unregister_sysctl();
217error_1:
218 unregister_filesystem(&nfs_fs_type);
219#endif
220error_0:
221 return ret;
222}
223
224/*
225 * Unregister the NFS filesystems
226 */
227void __exit unregister_nfs_fs(void)
228{
Trond Myklebust979df722006-07-25 11:28:19 -0400229 if (acl_shrinker != NULL)
230 remove_shrinker(acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400231#ifdef CONFIG_NFS_V4
232 unregister_filesystem(&nfs4_fs_type);
233 nfs_unregister_sysctl();
234#endif
235 unregister_filesystem(&nfs_fs_type);
236}
237
238/*
239 * Deliver file system statistics to userspace
240 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400241static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400242{
Trond Myklebust816724e2006-06-24 08:41:41 -0400243 struct super_block *sb = dentry->d_sb;
David Howellsf7b422b2006-06-09 09:34:33 -0400244 struct nfs_server *server = NFS_SB(sb);
245 unsigned char blockbits;
246 unsigned long blockres;
247 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
248 struct nfs_fattr fattr;
249 struct nfs_fsstat res = {
250 .fattr = &fattr,
251 };
252 int error;
253
254 lock_kernel();
255
256 error = server->rpc_ops->statfs(server, rootfh, &res);
257 buf->f_type = NFS_SUPER_MAGIC;
258 if (error < 0)
259 goto out_err;
260
261 /*
262 * Current versions of glibc do not correctly handle the
263 * case where f_frsize != f_bsize. Eventually we want to
264 * report the value of wtmult in this field.
265 */
266 buf->f_frsize = sb->s_blocksize;
267
268 /*
269 * On most *nix systems, f_blocks, f_bfree, and f_bavail
270 * are reported in units of f_frsize. Linux hasn't had
271 * an f_frsize field in its statfs struct until recently,
272 * thus historically Linux's sys_statfs reports these
273 * fields in units of f_bsize.
274 */
275 buf->f_bsize = sb->s_blocksize;
276 blockbits = sb->s_blocksize_bits;
277 blockres = (1 << blockbits) - 1;
278 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
279 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
280 buf->f_bavail = (res.abytes + blockres) >> blockbits;
281
282 buf->f_files = res.tfiles;
283 buf->f_ffree = res.afiles;
284
285 buf->f_namelen = server->namelen;
286 out:
287 unlock_kernel();
288 return 0;
289
290 out_err:
291 dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
292 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
293 goto out;
294
295}
296
David Howells7d4e2742006-08-22 20:06:07 -0400297/*
298 * Map the security flavour number to a name
299 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400300static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
301{
David Howells7d4e2742006-08-22 20:06:07 -0400302 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400303 rpc_authflavor_t flavour;
304 const char *str;
305 } sec_flavours[] = {
306 { RPC_AUTH_NULL, "null" },
307 { RPC_AUTH_UNIX, "sys" },
308 { RPC_AUTH_GSS_KRB5, "krb5" },
309 { RPC_AUTH_GSS_KRB5I, "krb5i" },
310 { RPC_AUTH_GSS_KRB5P, "krb5p" },
311 { RPC_AUTH_GSS_LKEY, "lkey" },
312 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
313 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
314 { RPC_AUTH_GSS_SPKM, "spkm" },
315 { RPC_AUTH_GSS_SPKMI, "spkmi" },
316 { RPC_AUTH_GSS_SPKMP, "spkmp" },
317 { -1, "unknown" }
318 };
319 int i;
320
321 for (i=0; sec_flavours[i].flavour != -1; i++) {
322 if (sec_flavours[i].flavour == flavour)
323 break;
324 }
325 return sec_flavours[i].str;
326}
327
David Howellsf7b422b2006-06-09 09:34:33 -0400328/*
329 * Describe the mount options in force on this server representation
330 */
331static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
332{
333 static struct proc_nfs_info {
334 int flag;
335 char *str;
336 char *nostr;
337 } nfs_info[] = {
338 { NFS_MOUNT_SOFT, ",soft", ",hard" },
339 { NFS_MOUNT_INTR, ",intr", "" },
340 { NFS_MOUNT_NOCTO, ",nocto", "" },
341 { NFS_MOUNT_NOAC, ",noac", "" },
342 { NFS_MOUNT_NONLM, ",nolock", "" },
343 { NFS_MOUNT_NOACL, ",noacl", "" },
344 { 0, NULL, NULL }
345 };
346 struct proc_nfs_info *nfs_infop;
347 char buf[12];
348 char *proto;
349
350 seq_printf(m, ",vers=%d", nfss->rpc_ops->version);
351 seq_printf(m, ",rsize=%d", nfss->rsize);
352 seq_printf(m, ",wsize=%d", nfss->wsize);
353 if (nfss->acregmin != 3*HZ || showdefaults)
354 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
355 if (nfss->acregmax != 60*HZ || showdefaults)
356 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
357 if (nfss->acdirmin != 30*HZ || showdefaults)
358 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
359 if (nfss->acdirmax != 60*HZ || showdefaults)
360 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
361 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
362 if (nfss->flags & nfs_infop->flag)
363 seq_puts(m, nfs_infop->str);
364 else
365 seq_puts(m, nfs_infop->nostr);
366 }
367 switch (nfss->client->cl_xprt->prot) {
368 case IPPROTO_TCP:
369 proto = "tcp";
370 break;
371 case IPPROTO_UDP:
372 proto = "udp";
373 break;
374 default:
375 snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
376 proto = buf;
377 }
378 seq_printf(m, ",proto=%s", proto);
379 seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
380 seq_printf(m, ",retrans=%u", nfss->retrans_count);
Trond Myklebust81039f12006-06-09 09:34:34 -0400381 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
David Howellsf7b422b2006-06-09 09:34:33 -0400382}
383
384/*
385 * Describe the mount options on this VFS mountpoint
386 */
387static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
388{
389 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
390
391 nfs_show_mount_options(m, nfss, 0);
392
393 seq_puts(m, ",addr=");
394 seq_escape(m, nfss->hostname, " \t\n\\");
395
396 return 0;
397}
398
399/*
400 * Present statistical information for this VFS mountpoint
401 */
402static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
403{
404 int i, cpu;
405 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
406 struct rpc_auth *auth = nfss->client->cl_auth;
407 struct nfs_iostats totals = { };
408
409 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
410
411 /*
412 * Display all mount option settings
413 */
414 seq_printf(m, "\n\topts:\t");
415 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
416 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
417 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
418 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
419 nfs_show_mount_options(m, nfss, 1);
420
421 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
422
423 seq_printf(m, "\n\tcaps:\t");
424 seq_printf(m, "caps=0x%x", nfss->caps);
425 seq_printf(m, ",wtmult=%d", nfss->wtmult);
426 seq_printf(m, ",dtsize=%d", nfss->dtsize);
427 seq_printf(m, ",bsize=%d", nfss->bsize);
428 seq_printf(m, ",namelen=%d", nfss->namelen);
429
430#ifdef CONFIG_NFS_V4
431 if (nfss->rpc_ops->version == 4) {
432 seq_printf(m, "\n\tnfsv4:\t");
433 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
434 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
435 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
436 }
437#endif
438
439 /*
440 * Display security flavor in effect for this mount
441 */
442 seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
443 if (auth->au_flavor)
444 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
445
446 /*
447 * Display superblock I/O counters
448 */
449 for_each_possible_cpu(cpu) {
450 struct nfs_iostats *stats;
451
452 preempt_disable();
453 stats = per_cpu_ptr(nfss->io_stats, cpu);
454
455 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
456 totals.events[i] += stats->events[i];
457 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
458 totals.bytes[i] += stats->bytes[i];
459
460 preempt_enable();
461 }
462
463 seq_printf(m, "\n\tevents:\t");
464 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
465 seq_printf(m, "%lu ", totals.events[i]);
466 seq_printf(m, "\n\tbytes:\t");
467 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
468 seq_printf(m, "%Lu ", totals.bytes[i]);
469 seq_printf(m, "\n");
470
471 rpc_print_iostats(m, nfss->client);
472
473 return 0;
474}
475
476/*
477 * Begin unmount by attempting to remove all automounted mountpoints we added
478 * in response to traversals
479 */
480static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
481{
482 struct nfs_server *server;
483 struct rpc_clnt *rpc;
484
485 shrink_submounts(vfsmnt, &nfs_automount_list);
486 if (!(flags & MNT_FORCE))
487 return;
488 /* -EIO all pending I/O */
489 server = NFS_SB(vfsmnt->mnt_sb);
490 rpc = server->client;
491 if (!IS_ERR(rpc))
492 rpc_killall_tasks(rpc);
493 rpc = server->client_acl;
494 if (!IS_ERR(rpc))
495 rpc_killall_tasks(rpc);
496}
497
498/*
499 * Obtain the root inode of the file system.
500 */
501static struct inode *
502nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
503{
504 struct nfs_server *server = NFS_SB(sb);
505 int error;
506
507 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
508 if (error < 0) {
509 dprintk("nfs_get_root: getattr error = %d\n", -error);
510 return ERR_PTR(error);
511 }
512
513 server->fsid = fsinfo->fattr->fsid;
514 return nfs_fhget(sb, rootfh, fsinfo->fattr);
515}
516
517/*
518 * Do NFS version-independent mount processing, and sanity checking
519 */
520static int
521nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
522{
523 struct nfs_server *server;
524 struct inode *root_inode;
525 struct nfs_fattr fattr;
526 struct nfs_fsinfo fsinfo = {
527 .fattr = &fattr,
528 };
529 struct nfs_pathconf pathinfo = {
530 .fattr = &fattr,
531 };
532 int no_root_error = 0;
533 unsigned long max_rpc_payload;
534
535 /* We probably want something more informative here */
536 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
537
538 server = NFS_SB(sb);
539
540 sb->s_magic = NFS_SUPER_MAGIC;
541
542 server->io_stats = nfs_alloc_iostats();
543 if (server->io_stats == NULL)
544 return -ENOMEM;
545
546 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
547 /* Did getting the root inode fail? */
548 if (IS_ERR(root_inode)) {
549 no_root_error = PTR_ERR(root_inode);
550 goto out_no_root;
551 }
552 sb->s_root = d_alloc_root(root_inode);
553 if (!sb->s_root) {
554 no_root_error = -ENOMEM;
555 goto out_no_root;
556 }
557 sb->s_root->d_op = server->rpc_ops->dentry_ops;
558
559 /* mount time stamp, in seconds */
560 server->mount_time = jiffies;
561
562 /* Get some general file system info */
563 if (server->namelen == 0 &&
564 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
565 server->namelen = pathinfo.max_namelen;
566 /* Work out a lot of parameters */
567 if (server->rsize == 0)
568 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
569 if (server->wsize == 0)
570 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
571
572 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
573 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
574 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
575 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
576
577 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
578 if (server->rsize > max_rpc_payload)
579 server->rsize = max_rpc_payload;
580 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
581 server->rsize = NFS_MAX_FILE_IO_SIZE;
582 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
583
584 if (server->wsize > max_rpc_payload)
585 server->wsize = max_rpc_payload;
586 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
587 server->wsize = NFS_MAX_FILE_IO_SIZE;
588 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
589
590 if (sb->s_blocksize == 0)
591 sb->s_blocksize = nfs_block_bits(server->wsize,
592 &sb->s_blocksize_bits);
593 server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
594
595 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
596 if (server->dtsize > PAGE_CACHE_SIZE)
597 server->dtsize = PAGE_CACHE_SIZE;
598 if (server->dtsize > server->rsize)
599 server->dtsize = server->rsize;
600
601 if (server->flags & NFS_MOUNT_NOAC) {
602 server->acregmin = server->acregmax = 0;
603 server->acdirmin = server->acdirmax = 0;
604 sb->s_flags |= MS_SYNCHRONOUS;
605 }
606 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
607
608 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
609
610 server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
611 server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
612
613 /* We're airborne Set socket buffersize */
614 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
615 return 0;
616 /* Yargs. It didn't work out. */
617out_no_root:
618 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
619 if (!IS_ERR(root_inode))
620 iput(root_inode);
621 return no_root_error;
622}
623
624/*
625 * Initialise the timeout values for a connection
626 */
627static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
628{
629 to->to_initval = timeo * HZ / 10;
630 to->to_retries = retrans;
631 if (!to->to_retries)
632 to->to_retries = 2;
633
634 switch (proto) {
635 case IPPROTO_TCP:
636 if (!to->to_initval)
637 to->to_initval = 60 * HZ;
638 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
639 to->to_initval = NFS_MAX_TCP_TIMEOUT;
640 to->to_increment = to->to_initval;
641 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
642 to->to_exponential = 0;
643 break;
644 case IPPROTO_UDP:
645 default:
646 if (!to->to_initval)
647 to->to_initval = 11 * HZ / 10;
648 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
649 to->to_initval = NFS_MAX_UDP_TIMEOUT;
650 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
651 to->to_exponential = 1;
652 break;
653 }
654}
655
656/*
657 * Create an RPC client handle.
658 */
659static struct rpc_clnt *
660nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
661{
662 struct rpc_timeout timeparms;
663 struct rpc_xprt *xprt = NULL;
664 struct rpc_clnt *clnt = NULL;
665 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
666
667 nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);
668
669 server->retrans_timeo = timeparms.to_initval;
670 server->retrans_count = timeparms.to_retries;
671
672 /* create transport and client */
673 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
674 if (IS_ERR(xprt)) {
675 dprintk("%s: cannot create RPC transport. Error = %ld\n",
676 __FUNCTION__, PTR_ERR(xprt));
677 return (struct rpc_clnt *)xprt;
678 }
679 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
680 server->rpc_ops->version, data->pseudoflavor);
681 if (IS_ERR(clnt)) {
682 dprintk("%s: cannot create RPC client. Error = %ld\n",
683 __FUNCTION__, PTR_ERR(xprt));
684 goto out_fail;
685 }
686
687 clnt->cl_intr = 1;
688 clnt->cl_softrtry = 1;
689
690 return clnt;
691
692out_fail:
693 return clnt;
694}
695
696/*
697 * Clone a server record
698 */
699static struct nfs_server *nfs_clone_server(struct super_block *sb, struct nfs_clone_mount *data)
700{
701 struct nfs_server *server = NFS_SB(sb);
702 struct nfs_server *parent = NFS_SB(data->sb);
703 struct inode *root_inode;
704 struct nfs_fsinfo fsinfo;
705 void *err = ERR_PTR(-ENOMEM);
706
707 sb->s_op = data->sb->s_op;
708 sb->s_blocksize = data->sb->s_blocksize;
709 sb->s_blocksize_bits = data->sb->s_blocksize_bits;
710 sb->s_maxbytes = data->sb->s_maxbytes;
711
712 server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
713 server->io_stats = nfs_alloc_iostats();
714 if (server->io_stats == NULL)
715 goto out;
716
717 server->client = rpc_clone_client(parent->client);
718 if (IS_ERR((err = server->client)))
719 goto out;
720
721 if (!IS_ERR(parent->client_sys)) {
722 server->client_sys = rpc_clone_client(parent->client_sys);
723 if (IS_ERR((err = server->client_sys)))
724 goto out;
725 }
726 if (!IS_ERR(parent->client_acl)) {
727 server->client_acl = rpc_clone_client(parent->client_acl);
728 if (IS_ERR((err = server->client_acl)))
729 goto out;
730 }
731 root_inode = nfs_fhget(sb, data->fh, data->fattr);
732 if (!root_inode)
733 goto out;
734 sb->s_root = d_alloc_root(root_inode);
735 if (!sb->s_root)
736 goto out_put_root;
737 fsinfo.fattr = data->fattr;
738 if (NFS_PROTO(root_inode)->fsinfo(server, data->fh, &fsinfo) == 0)
739 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
740 sb->s_root->d_op = server->rpc_ops->dentry_ops;
741 sb->s_flags |= MS_ACTIVE;
742 return server;
743out_put_root:
744 iput(root_inode);
745out:
746 return err;
747}
748
749/*
750 * Copy an existing superblock and attach revised data
751 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400752static int nfs_clone_generic_sb(struct nfs_clone_mount *data,
David Howellsf7b422b2006-06-09 09:34:33 -0400753 struct super_block *(*fill_sb)(struct nfs_server *, struct nfs_clone_mount *),
Trond Myklebust816724e2006-06-24 08:41:41 -0400754 struct nfs_server *(*fill_server)(struct super_block *, struct nfs_clone_mount *),
755 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -0400756{
757 struct nfs_server *server;
758 struct nfs_server *parent = NFS_SB(data->sb);
759 struct super_block *sb = ERR_PTR(-EINVAL);
David Howellsf7b422b2006-06-09 09:34:33 -0400760 char *hostname;
Trond Myklebust816724e2006-06-24 08:41:41 -0400761 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -0400762 int len;
763
764 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
765 if (server == NULL)
766 goto out_err;
767 memcpy(server, parent, sizeof(*server));
768 hostname = (data->hostname != NULL) ? data->hostname : parent->hostname;
769 len = strlen(hostname) + 1;
770 server->hostname = kmalloc(len, GFP_KERNEL);
771 if (server->hostname == NULL)
772 goto free_server;
773 memcpy(server->hostname, hostname, len);
Trond Myklebust816724e2006-06-24 08:41:41 -0400774 error = rpciod_up();
775 if (error != 0)
David Howellsf7b422b2006-06-09 09:34:33 -0400776 goto free_hostname;
777
778 sb = fill_sb(server, data);
Trond Myklebust816724e2006-06-24 08:41:41 -0400779 if (IS_ERR(sb)) {
780 error = PTR_ERR(sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400781 goto kill_rpciod;
Trond Myklebust816724e2006-06-24 08:41:41 -0400782 }
783
784 if (sb->s_root)
785 goto out_rpciod_down;
David Howellsf7b422b2006-06-09 09:34:33 -0400786
787 server = fill_server(sb, data);
Trond Myklebust816724e2006-06-24 08:41:41 -0400788 if (IS_ERR(server)) {
789 error = PTR_ERR(server);
David Howellsf7b422b2006-06-09 09:34:33 -0400790 goto out_deactivate;
Trond Myklebust816724e2006-06-24 08:41:41 -0400791 }
792 return simple_set_mnt(mnt, sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400793out_deactivate:
794 up_write(&sb->s_umount);
795 deactivate_super(sb);
Trond Myklebust816724e2006-06-24 08:41:41 -0400796 return error;
797out_rpciod_down:
798 rpciod_down();
799 kfree(server->hostname);
800 kfree(server);
801 return simple_set_mnt(mnt, sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400802kill_rpciod:
803 rpciod_down();
804free_hostname:
805 kfree(server->hostname);
806free_server:
807 kfree(server);
808out_err:
Trond Myklebust816724e2006-06-24 08:41:41 -0400809 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400810}
811
812/*
813 * Set up an NFS2/3 superblock
814 *
815 * The way this works is that the mount process passes a structure
816 * in the data argument which contains the server's IP address
817 * and the root file handle obtained from the server's mount
818 * daemon. We stash these away in the private superblock fields.
819 */
820static int
821nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
822{
823 struct nfs_server *server;
824 rpc_authflavor_t authflavor;
825
826 server = NFS_SB(sb);
827 sb->s_blocksize_bits = 0;
828 sb->s_blocksize = 0;
829 if (data->bsize)
830 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
831 if (data->rsize)
832 server->rsize = nfs_block_size(data->rsize, NULL);
833 if (data->wsize)
834 server->wsize = nfs_block_size(data->wsize, NULL);
835 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
836
837 server->acregmin = data->acregmin*HZ;
838 server->acregmax = data->acregmax*HZ;
839 server->acdirmin = data->acdirmin*HZ;
840 server->acdirmax = data->acdirmax*HZ;
841
842 /* Start lockd here, before we might error out */
843 if (!(server->flags & NFS_MOUNT_NONLM))
844 lockd_up();
845
846 server->namelen = data->namlen;
847 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
848 if (!server->hostname)
849 return -ENOMEM;
850 strcpy(server->hostname, data->hostname);
851
852 /* Check NFS protocol revision and initialize RPC op vector
853 * and file handle pool. */
854#ifdef CONFIG_NFS_V3
855 if (server->flags & NFS_MOUNT_VER3) {
856 server->rpc_ops = &nfs_v3_clientops;
857 server->caps |= NFS_CAP_READDIRPLUS;
858 } else {
859 server->rpc_ops = &nfs_v2_clientops;
860 }
861#else
862 server->rpc_ops = &nfs_v2_clientops;
863#endif
864
865 /* Fill in pseudoflavor for mount version < 5 */
866 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
867 data->pseudoflavor = RPC_AUTH_UNIX;
868 authflavor = data->pseudoflavor; /* save for sb_init() */
869 /* XXX maybe we want to add a server->pseudoflavor field */
870
871 /* Create RPC client handles */
872 server->client = nfs_create_client(server, data);
873 if (IS_ERR(server->client))
874 return PTR_ERR(server->client);
875 /* RFC 2623, sec 2.3.2 */
876 if (authflavor != RPC_AUTH_UNIX) {
877 struct rpc_auth *auth;
878
879 server->client_sys = rpc_clone_client(server->client);
880 if (IS_ERR(server->client_sys))
881 return PTR_ERR(server->client_sys);
882 auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
883 if (IS_ERR(auth))
884 return PTR_ERR(auth);
885 } else {
886 atomic_inc(&server->client->cl_count);
887 server->client_sys = server->client;
888 }
889 if (server->flags & NFS_MOUNT_VER3) {
890#ifdef CONFIG_NFS_V3_ACL
891 if (!(server->flags & NFS_MOUNT_NOACL)) {
892 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
893 /* No errors! Assume that Sun nfsacls are supported */
894 if (!IS_ERR(server->client_acl))
895 server->caps |= NFS_CAP_ACLS;
896 }
897#else
898 server->flags &= ~NFS_MOUNT_NOACL;
899#endif /* CONFIG_NFS_V3_ACL */
900 /*
901 * The VFS shouldn't apply the umask to mode bits. We will
902 * do so ourselves when necessary.
903 */
904 sb->s_flags |= MS_POSIXACL;
905 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
906 server->namelen = NFS3_MAXNAMLEN;
907 sb->s_time_gran = 1;
908 } else {
909 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
910 server->namelen = NFS2_MAXNAMLEN;
911 }
912
913 sb->s_op = &nfs_sops;
914 return nfs_sb_init(sb, authflavor);
915}
916
917static int nfs_set_super(struct super_block *s, void *data)
918{
919 s->s_fs_info = data;
920 return set_anon_super(s, data);
921}
922
923static int nfs_compare_super(struct super_block *sb, void *data)
924{
925 struct nfs_server *server = data;
926 struct nfs_server *old = NFS_SB(sb);
927
928 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
929 return 0;
930 if (old->addr.sin_port != server->addr.sin_port)
931 return 0;
932 return !nfs_compare_fh(&old->fh, &server->fh);
933}
934
Trond Myklebust816724e2006-06-24 08:41:41 -0400935static int nfs_get_sb(struct file_system_type *fs_type,
936 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -0400937{
938 int error;
939 struct nfs_server *server = NULL;
940 struct super_block *s;
941 struct nfs_fh *root;
942 struct nfs_mount_data *data = raw_data;
943
Trond Myklebust816724e2006-06-24 08:41:41 -0400944 error = -EINVAL;
David Howellsf7b422b2006-06-09 09:34:33 -0400945 if (data == NULL) {
946 dprintk("%s: missing data argument\n", __FUNCTION__);
Trond Myklebust816724e2006-06-24 08:41:41 -0400947 goto out_err_noserver;
David Howellsf7b422b2006-06-09 09:34:33 -0400948 }
949 if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
950 dprintk("%s: bad mount version\n", __FUNCTION__);
Trond Myklebust816724e2006-06-24 08:41:41 -0400951 goto out_err_noserver;
David Howellsf7b422b2006-06-09 09:34:33 -0400952 }
953 switch (data->version) {
954 case 1:
955 data->namlen = 0;
956 case 2:
957 data->bsize = 0;
958 case 3:
959 if (data->flags & NFS_MOUNT_VER3) {
960 dprintk("%s: mount structure version %d does not support NFSv3\n",
961 __FUNCTION__,
962 data->version);
Trond Myklebust816724e2006-06-24 08:41:41 -0400963 goto out_err_noserver;
David Howellsf7b422b2006-06-09 09:34:33 -0400964 }
965 data->root.size = NFS2_FHSIZE;
966 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
967 case 4:
968 if (data->flags & NFS_MOUNT_SECFLAVOUR) {
969 dprintk("%s: mount structure version %d does not support strong security\n",
970 __FUNCTION__,
971 data->version);
Trond Myklebust816724e2006-06-24 08:41:41 -0400972 goto out_err_noserver;
David Howellsf7b422b2006-06-09 09:34:33 -0400973 }
974 case 5:
975 memset(data->context, 0, sizeof(data->context));
976 }
977#ifndef CONFIG_NFS_V3
978 /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
Trond Myklebust816724e2006-06-24 08:41:41 -0400979 error = -EPROTONOSUPPORT;
David Howellsf7b422b2006-06-09 09:34:33 -0400980 if (data->flags & NFS_MOUNT_VER3) {
981 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
Trond Myklebust816724e2006-06-24 08:41:41 -0400982 goto out_err_noserver;
David Howellsf7b422b2006-06-09 09:34:33 -0400983 }
984#endif /* CONFIG_NFS_V3 */
985
Trond Myklebust816724e2006-06-24 08:41:41 -0400986 error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -0400987 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
988 if (!server)
Trond Myklebust816724e2006-06-24 08:41:41 -0400989 goto out_err_noserver;
David Howellsf7b422b2006-06-09 09:34:33 -0400990 /* Zero out the NFS state stuff */
991 init_nfsv4_state(server);
992 server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
993
994 root = &server->fh;
995 if (data->flags & NFS_MOUNT_VER3)
996 root->size = data->root.size;
997 else
998 root->size = NFS2_FHSIZE;
Trond Myklebust816724e2006-06-24 08:41:41 -0400999 error = -EINVAL;
David Howellsf7b422b2006-06-09 09:34:33 -04001000 if (root->size > sizeof(root->data)) {
1001 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
1002 goto out_err;
1003 }
1004 memcpy(root->data, data->root.data, root->size);
1005
1006 /* We now require that the mount process passes the remote address */
1007 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1008 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1009 dprintk("%s: mount program didn't pass remote address!\n",
1010 __FUNCTION__);
1011 goto out_err;
1012 }
1013
1014 /* Fire up rpciod if not yet running */
Trond Myklebust816724e2006-06-24 08:41:41 -04001015 error = rpciod_up();
1016 if (error < 0) {
1017 dprintk("%s: couldn't start rpciod! Error = %d\n",
1018 __FUNCTION__, error);
David Howellsf7b422b2006-06-09 09:34:33 -04001019 goto out_err;
1020 }
1021
1022 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
Trond Myklebust816724e2006-06-24 08:41:41 -04001023 if (IS_ERR(s)) {
1024 error = PTR_ERR(s);
1025 goto out_err_rpciod;
1026 }
1027
1028 if (s->s_root)
David Howellsf7b422b2006-06-09 09:34:33 -04001029 goto out_rpciod_down;
1030
1031 s->s_flags = flags;
1032
1033 error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1034 if (error) {
1035 up_write(&s->s_umount);
1036 deactivate_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04001037 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001038 }
1039 s->s_flags |= MS_ACTIVE;
Trond Myklebust816724e2006-06-24 08:41:41 -04001040 return simple_set_mnt(mnt, s);
1041
David Howellsf7b422b2006-06-09 09:34:33 -04001042out_rpciod_down:
1043 rpciod_down();
Trond Myklebust816724e2006-06-24 08:41:41 -04001044 kfree(server);
1045 return simple_set_mnt(mnt, s);
1046
1047out_err_rpciod:
1048 rpciod_down();
David Howellsf7b422b2006-06-09 09:34:33 -04001049out_err:
1050 kfree(server);
Trond Myklebust816724e2006-06-24 08:41:41 -04001051out_err_noserver:
1052 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001053}
1054
1055static void nfs_kill_super(struct super_block *s)
1056{
1057 struct nfs_server *server = NFS_SB(s);
1058
1059 kill_anon_super(s);
1060
1061 if (!IS_ERR(server->client))
1062 rpc_shutdown_client(server->client);
1063 if (!IS_ERR(server->client_sys))
1064 rpc_shutdown_client(server->client_sys);
1065 if (!IS_ERR(server->client_acl))
1066 rpc_shutdown_client(server->client_acl);
1067
1068 if (!(server->flags & NFS_MOUNT_NONLM))
1069 lockd_down(); /* release rpc.lockd */
1070
1071 rpciod_down(); /* release rpciod */
1072
1073 nfs_free_iostats(server->io_stats);
1074 kfree(server->hostname);
1075 kfree(server);
1076 nfs_release_automount_timer();
1077}
1078
1079static struct super_block *nfs_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
1080{
1081 struct super_block *sb;
1082
1083 server->fsid = data->fattr->fsid;
1084 nfs_copy_fh(&server->fh, data->fh);
1085 sb = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1086 if (!IS_ERR(sb) && sb->s_root == NULL && !(server->flags & NFS_MOUNT_NONLM))
1087 lockd_up();
1088 return sb;
1089}
1090
Trond Myklebust816724e2006-06-24 08:41:41 -04001091static int nfs_clone_nfs_sb(struct file_system_type *fs_type,
1092 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001093{
1094 struct nfs_clone_mount *data = raw_data;
Trond Myklebust816724e2006-06-24 08:41:41 -04001095 return nfs_clone_generic_sb(data, nfs_clone_sb, nfs_clone_server, mnt);
David Howellsf7b422b2006-06-09 09:34:33 -04001096}
1097
1098#ifdef CONFIG_NFS_V4
1099static struct rpc_clnt *nfs4_create_client(struct nfs_server *server,
1100 struct rpc_timeout *timeparms, int proto, rpc_authflavor_t flavor)
1101{
1102 struct nfs4_client *clp;
1103 struct rpc_xprt *xprt = NULL;
1104 struct rpc_clnt *clnt = NULL;
1105 int err = -EIO;
1106
1107 clp = nfs4_get_client(&server->addr.sin_addr);
1108 if (!clp) {
1109 dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
1110 return ERR_PTR(err);
1111 }
1112
1113 /* Now create transport and client */
1114 down_write(&clp->cl_sem);
1115 if (IS_ERR(clp->cl_rpcclient)) {
1116 xprt = xprt_create_proto(proto, &server->addr, timeparms);
1117 if (IS_ERR(xprt)) {
1118 up_write(&clp->cl_sem);
1119 err = PTR_ERR(xprt);
1120 dprintk("%s: cannot create RPC transport. Error = %d\n",
1121 __FUNCTION__, err);
1122 goto out_fail;
1123 }
1124 /* Bind to a reserved port! */
1125 xprt->resvport = 1;
1126 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1127 server->rpc_ops->version, flavor);
1128 if (IS_ERR(clnt)) {
1129 up_write(&clp->cl_sem);
1130 err = PTR_ERR(clnt);
1131 dprintk("%s: cannot create RPC client. Error = %d\n",
1132 __FUNCTION__, err);
1133 goto out_fail;
1134 }
1135 clnt->cl_intr = 1;
1136 clnt->cl_softrtry = 1;
1137 clp->cl_rpcclient = clnt;
1138 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1139 nfs_idmap_new(clp);
1140 }
1141 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1142 clnt = rpc_clone_client(clp->cl_rpcclient);
1143 if (!IS_ERR(clnt))
1144 server->nfs4_state = clp;
1145 up_write(&clp->cl_sem);
1146 clp = NULL;
1147
1148 if (IS_ERR(clnt)) {
1149 dprintk("%s: cannot create RPC client. Error = %d\n",
1150 __FUNCTION__, err);
1151 return clnt;
1152 }
1153
1154 if (server->nfs4_state->cl_idmap == NULL) {
1155 dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
1156 return ERR_PTR(-ENOMEM);
1157 }
1158
1159 if (clnt->cl_auth->au_flavor != flavor) {
1160 struct rpc_auth *auth;
1161
1162 auth = rpcauth_create(flavor, clnt);
1163 if (IS_ERR(auth)) {
1164 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
1165 return (struct rpc_clnt *)auth;
1166 }
1167 }
1168 return clnt;
1169
1170 out_fail:
1171 if (clp)
1172 nfs4_put_client(clp);
1173 return ERR_PTR(err);
1174}
1175
1176/*
1177 * Set up an NFS4 superblock
1178 */
1179static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1180{
1181 struct nfs_server *server;
1182 struct rpc_timeout timeparms;
1183 rpc_authflavor_t authflavour;
1184 int err = -EIO;
1185
1186 sb->s_blocksize_bits = 0;
1187 sb->s_blocksize = 0;
1188 server = NFS_SB(sb);
1189 if (data->rsize != 0)
1190 server->rsize = nfs_block_size(data->rsize, NULL);
1191 if (data->wsize != 0)
1192 server->wsize = nfs_block_size(data->wsize, NULL);
1193 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1194 server->caps = NFS_CAP_ATOMIC_OPEN;
1195
1196 server->acregmin = data->acregmin*HZ;
1197 server->acregmax = data->acregmax*HZ;
1198 server->acdirmin = data->acdirmin*HZ;
1199 server->acdirmax = data->acdirmax*HZ;
1200
1201 server->rpc_ops = &nfs_v4_clientops;
1202
1203 nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
1204
1205 server->retrans_timeo = timeparms.to_initval;
1206 server->retrans_count = timeparms.to_retries;
1207
1208 /* Now create transport and client */
1209 authflavour = RPC_AUTH_UNIX;
1210 if (data->auth_flavourlen != 0) {
1211 if (data->auth_flavourlen != 1) {
1212 dprintk("%s: Invalid number of RPC auth flavours %d.\n",
1213 __FUNCTION__, data->auth_flavourlen);
1214 err = -EINVAL;
1215 goto out_fail;
1216 }
1217 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1218 err = -EFAULT;
1219 goto out_fail;
1220 }
1221 }
1222
1223 server->client = nfs4_create_client(server, &timeparms, data->proto, authflavour);
1224 if (IS_ERR(server->client)) {
1225 err = PTR_ERR(server->client);
1226 dprintk("%s: cannot create RPC client. Error = %d\n",
1227 __FUNCTION__, err);
1228 goto out_fail;
1229 }
1230
1231 sb->s_time_gran = 1;
1232
1233 sb->s_op = &nfs4_sops;
1234 err = nfs_sb_init(sb, authflavour);
1235
1236 out_fail:
1237 return err;
1238}
1239
1240static int nfs4_compare_super(struct super_block *sb, void *data)
1241{
1242 struct nfs_server *server = data;
1243 struct nfs_server *old = NFS_SB(sb);
1244
1245 if (strcmp(server->hostname, old->hostname) != 0)
1246 return 0;
1247 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1248 return 0;
1249 return 1;
1250}
1251
1252static void *
1253nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1254{
1255 void *p = NULL;
1256
1257 if (!src->len)
1258 return ERR_PTR(-EINVAL);
1259 if (src->len < maxlen)
1260 maxlen = src->len;
1261 if (dst == NULL) {
1262 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1263 if (p == NULL)
1264 return ERR_PTR(-ENOMEM);
1265 }
1266 if (copy_from_user(dst, src->data, maxlen)) {
1267 kfree(p);
1268 return ERR_PTR(-EFAULT);
1269 }
1270 dst[maxlen] = '\0';
1271 return dst;
1272}
1273
Trond Myklebust816724e2006-06-24 08:41:41 -04001274static int nfs4_get_sb(struct file_system_type *fs_type,
1275 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001276{
1277 int error;
1278 struct nfs_server *server;
1279 struct super_block *s;
1280 struct nfs4_mount_data *data = raw_data;
1281 void *p;
1282
1283 if (data == NULL) {
1284 dprintk("%s: missing data argument\n", __FUNCTION__);
Trond Myklebust816724e2006-06-24 08:41:41 -04001285 return -EINVAL;
David Howellsf7b422b2006-06-09 09:34:33 -04001286 }
1287 if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
1288 dprintk("%s: bad mount version\n", __FUNCTION__);
Trond Myklebust816724e2006-06-24 08:41:41 -04001289 return -EINVAL;
David Howellsf7b422b2006-06-09 09:34:33 -04001290 }
1291
1292 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1293 if (!server)
Trond Myklebust816724e2006-06-24 08:41:41 -04001294 return -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04001295 /* Zero out the NFS state stuff */
1296 init_nfsv4_state(server);
1297 server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
1298
1299 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1300 if (IS_ERR(p))
1301 goto out_err;
1302 server->hostname = p;
1303
1304 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1305 if (IS_ERR(p))
1306 goto out_err;
1307 server->mnt_path = p;
1308
1309 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1310 sizeof(server->ip_addr) - 1);
1311 if (IS_ERR(p))
1312 goto out_err;
1313
1314 /* We now require that the mount process passes the remote address */
1315 if (data->host_addrlen != sizeof(server->addr)) {
Trond Myklebust816724e2006-06-24 08:41:41 -04001316 error = -EINVAL;
David Howellsf7b422b2006-06-09 09:34:33 -04001317 goto out_free;
1318 }
1319 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
Trond Myklebust816724e2006-06-24 08:41:41 -04001320 error = -EFAULT;
David Howellsf7b422b2006-06-09 09:34:33 -04001321 goto out_free;
1322 }
1323 if (server->addr.sin_family != AF_INET ||
1324 server->addr.sin_addr.s_addr == INADDR_ANY) {
1325 dprintk("%s: mount program didn't pass remote IP address!\n",
1326 __FUNCTION__);
Trond Myklebust816724e2006-06-24 08:41:41 -04001327 error = -EINVAL;
David Howellsf7b422b2006-06-09 09:34:33 -04001328 goto out_free;
1329 }
1330
1331 /* Fire up rpciod if not yet running */
Trond Myklebust816724e2006-06-24 08:41:41 -04001332 error = rpciod_up();
1333 if (error < 0) {
1334 dprintk("%s: couldn't start rpciod! Error = %d\n",
1335 __FUNCTION__, error);
David Howellsf7b422b2006-06-09 09:34:33 -04001336 goto out_free;
1337 }
1338
1339 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
Trond Myklebust816724e2006-06-24 08:41:41 -04001340 if (IS_ERR(s)) {
1341 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04001342 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04001343 }
1344
1345 if (s->s_root) {
1346 kfree(server->mnt_path);
1347 kfree(server->hostname);
1348 kfree(server);
1349 return simple_set_mnt(mnt, s);
1350 }
David Howellsf7b422b2006-06-09 09:34:33 -04001351
1352 s->s_flags = flags;
1353
1354 error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1355 if (error) {
1356 up_write(&s->s_umount);
1357 deactivate_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04001358 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001359 }
1360 s->s_flags |= MS_ACTIVE;
Trond Myklebust816724e2006-06-24 08:41:41 -04001361 return simple_set_mnt(mnt, s);
David Howellsf7b422b2006-06-09 09:34:33 -04001362out_err:
Trond Myklebust816724e2006-06-24 08:41:41 -04001363 error = PTR_ERR(p);
David Howellsf7b422b2006-06-09 09:34:33 -04001364out_free:
1365 kfree(server->mnt_path);
1366 kfree(server->hostname);
1367 kfree(server);
Trond Myklebust816724e2006-06-24 08:41:41 -04001368 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04001369}
1370
1371static void nfs4_kill_super(struct super_block *sb)
1372{
1373 struct nfs_server *server = NFS_SB(sb);
1374
1375 nfs_return_all_delegations(sb);
1376 kill_anon_super(sb);
1377
1378 nfs4_renewd_prepare_shutdown(server);
1379
1380 if (server->client != NULL && !IS_ERR(server->client))
1381 rpc_shutdown_client(server->client);
1382
1383 destroy_nfsv4_state(server);
1384
1385 rpciod_down();
1386
1387 nfs_free_iostats(server->io_stats);
1388 kfree(server->hostname);
1389 kfree(server);
1390 nfs_release_automount_timer();
1391}
1392
1393/*
1394 * Constructs the SERVER-side path
1395 */
1396static inline char *nfs4_dup_path(const struct dentry *dentry)
1397{
1398 char *page = (char *) __get_free_page(GFP_USER);
1399 char *path;
1400
1401 path = nfs4_path(dentry, page, PAGE_SIZE);
1402 if (!IS_ERR(path)) {
1403 int len = PAGE_SIZE + page - path;
1404 char *tmp = path;
1405
1406 path = kmalloc(len, GFP_KERNEL);
1407 if (path)
1408 memcpy(path, tmp, len);
1409 else
1410 path = ERR_PTR(-ENOMEM);
1411 }
1412 free_page((unsigned long)page);
1413 return path;
1414}
1415
1416static struct super_block *nfs4_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
1417{
1418 const struct dentry *dentry = data->dentry;
1419 struct nfs4_client *clp = server->nfs4_state;
1420 struct super_block *sb;
1421
1422 server->fsid = data->fattr->fsid;
1423 nfs_copy_fh(&server->fh, data->fh);
1424 server->mnt_path = nfs4_dup_path(dentry);
1425 if (IS_ERR(server->mnt_path)) {
1426 sb = (struct super_block *)server->mnt_path;
1427 goto err;
1428 }
1429 sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
1430 if (IS_ERR(sb) || sb->s_root)
1431 goto free_path;
1432 nfs4_server_capabilities(server, &server->fh);
1433
1434 down_write(&clp->cl_sem);
1435 atomic_inc(&clp->cl_count);
1436 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1437 up_write(&clp->cl_sem);
1438 return sb;
1439free_path:
1440 kfree(server->mnt_path);
1441err:
1442 server->mnt_path = NULL;
1443 return sb;
1444}
1445
Trond Myklebust816724e2006-06-24 08:41:41 -04001446static int nfs_clone_nfs4_sb(struct file_system_type *fs_type,
1447 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001448{
1449 struct nfs_clone_mount *data = raw_data;
Trond Myklebust816724e2006-06-24 08:41:41 -04001450 return nfs_clone_generic_sb(data, nfs4_clone_sb, nfs_clone_server, mnt);
David Howellsf7b422b2006-06-09 09:34:33 -04001451}
1452
1453static struct super_block *nfs4_referral_sb(struct nfs_server *server, struct nfs_clone_mount *data)
1454{
1455 struct super_block *sb = ERR_PTR(-ENOMEM);
1456 int len;
1457
1458 len = strlen(data->mnt_path) + 1;
1459 server->mnt_path = kmalloc(len, GFP_KERNEL);
1460 if (server->mnt_path == NULL)
1461 goto err;
1462 memcpy(server->mnt_path, data->mnt_path, len);
1463 memcpy(&server->addr, data->addr, sizeof(struct sockaddr_in));
1464
1465 sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
1466 if (IS_ERR(sb) || sb->s_root)
1467 goto free_path;
1468 return sb;
1469free_path:
1470 kfree(server->mnt_path);
1471err:
1472 server->mnt_path = NULL;
1473 return sb;
1474}
1475
1476static struct nfs_server *nfs4_referral_server(struct super_block *sb, struct nfs_clone_mount *data)
1477{
1478 struct nfs_server *server = NFS_SB(sb);
1479 struct rpc_timeout timeparms;
1480 int proto, timeo, retrans;
1481 void *err;
1482
1483 proto = IPPROTO_TCP;
1484 /* Since we are following a referral and there may be alternatives,
1485 set the timeouts and retries to low values */
1486 timeo = 2;
1487 retrans = 1;
1488 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
1489
1490 server->client = nfs4_create_client(server, &timeparms, proto, data->authflavor);
1491 if (IS_ERR((err = server->client)))
1492 goto out_err;
1493
1494 sb->s_time_gran = 1;
1495 sb->s_op = &nfs4_sops;
1496 err = ERR_PTR(nfs_sb_init(sb, data->authflavor));
1497 if (!IS_ERR(err))
1498 return server;
1499out_err:
1500 return (struct nfs_server *)err;
1501}
1502
Trond Myklebust816724e2006-06-24 08:41:41 -04001503static int nfs_referral_nfs4_sb(struct file_system_type *fs_type,
1504 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04001505{
1506 struct nfs_clone_mount *data = raw_data;
Trond Myklebust816724e2006-06-24 08:41:41 -04001507 return nfs_clone_generic_sb(data, nfs4_referral_sb, nfs4_referral_server, mnt);
David Howellsf7b422b2006-06-09 09:34:33 -04001508}
1509
1510#endif