aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nfs/super.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/nfs/super.c')
-rw-r--r--fs/nfs/super.c1533
1 files changed, 1533 insertions, 0 deletions
diff --git a/fs/nfs/super.c b/fs/nfs/super.c
new file mode 100644
index 000000000000..b977748553d3
--- /dev/null
+++ b/fs/nfs/super.c
@@ -0,0 +1,1533 @@
1/*
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);
110static int nfs_statfs(struct dentry *, struct kstatfs *);
111static int nfs_show_options(struct seq_file *, struct vfsmount *);
112static int nfs_show_stats(struct seq_file *, struct vfsmount *);
113static 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);
116static 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
146static 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);
152static 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
190static const int nfs_set_port_min = 0;
191static const int nfs_set_port_max = 65535;
192
193static int param_set_port(const char *val, struct kernel_param *kp)
194{
195 char *endp;
196 int num = simple_strtol(val, &endp, 0);
197 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
198 return -EINVAL;
199 *((int *)kp->arg) = num;
200 return 0;
201}
202
203module_param_call(callback_tcpport, param_set_port, param_get_int,
204 &nfs_callback_set_tcpport, 0644);
205
206static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
207{
208 char *endp;
209 int num = simple_strtol(val, &endp, 0);
210 int jif = num * HZ;
211 if (endp == val || *endp || num < 0 || jif < num)
212 return -EINVAL;
213 *((int *)kp->arg) = jif;
214 return 0;
215}
216
217module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
218 &nfs_idmap_cache_timeout, 0644);
219
220/*
221 * Register the NFS filesystems
222 */
223int __init register_nfs_fs(void)
224{
225 int ret;
226
227 ret = register_filesystem(&nfs_fs_type);
228 if (ret < 0)
229 goto error_0;
230
231#ifdef CONFIG_NFS_V4
232 ret = nfs_register_sysctl();
233 if (ret < 0)
234 goto error_1;
235 ret = register_filesystem(&nfs4_fs_type);
236 if (ret < 0)
237 goto error_2;
238#endif
239 return 0;
240
241#ifdef CONFIG_NFS_V4
242error_2:
243 nfs_unregister_sysctl();
244error_1:
245 unregister_filesystem(&nfs_fs_type);
246#endif
247error_0:
248 return ret;
249}
250
251/*
252 * Unregister the NFS filesystems
253 */
254void __exit unregister_nfs_fs(void)
255{
256#ifdef CONFIG_NFS_V4
257 unregister_filesystem(&nfs4_fs_type);
258 nfs_unregister_sysctl();
259#endif
260 unregister_filesystem(&nfs_fs_type);
261}
262
263/*
264 * Deliver file system statistics to userspace
265 */
266static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
267{
268 struct super_block *sb = dentry->d_sb;
269 struct nfs_server *server = NFS_SB(sb);
270 unsigned char blockbits;
271 unsigned long blockres;
272 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
273 struct nfs_fattr fattr;
274 struct nfs_fsstat res = {
275 .fattr = &fattr,
276 };
277 int error;
278
279 lock_kernel();
280
281 error = server->rpc_ops->statfs(server, rootfh, &res);
282 buf->f_type = NFS_SUPER_MAGIC;
283 if (error < 0)
284 goto out_err;
285
286 /*
287 * Current versions of glibc do not correctly handle the
288 * case where f_frsize != f_bsize. Eventually we want to
289 * report the value of wtmult in this field.
290 */
291 buf->f_frsize = sb->s_blocksize;
292
293 /*
294 * On most *nix systems, f_blocks, f_bfree, and f_bavail
295 * are reported in units of f_frsize. Linux hasn't had
296 * an f_frsize field in its statfs struct until recently,
297 * thus historically Linux's sys_statfs reports these
298 * fields in units of f_bsize.
299 */
300 buf->f_bsize = sb->s_blocksize;
301 blockbits = sb->s_blocksize_bits;
302 blockres = (1 << blockbits) - 1;
303 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
304 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
305 buf->f_bavail = (res.abytes + blockres) >> blockbits;
306
307 buf->f_files = res.tfiles;
308 buf->f_ffree = res.afiles;
309
310 buf->f_namelen = server->namelen;
311 out:
312 unlock_kernel();
313 return 0;
314
315 out_err:
316 dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
317 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
318 goto out;
319
320}
321
322static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
323{
324 static struct {
325 rpc_authflavor_t flavour;
326 const char *str;
327 } sec_flavours[] = {
328 { RPC_AUTH_NULL, "null" },
329 { RPC_AUTH_UNIX, "sys" },
330 { RPC_AUTH_GSS_KRB5, "krb5" },
331 { RPC_AUTH_GSS_KRB5I, "krb5i" },
332 { RPC_AUTH_GSS_KRB5P, "krb5p" },
333 { RPC_AUTH_GSS_LKEY, "lkey" },
334 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
335 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
336 { RPC_AUTH_GSS_SPKM, "spkm" },
337 { RPC_AUTH_GSS_SPKMI, "spkmi" },
338 { RPC_AUTH_GSS_SPKMP, "spkmp" },
339 { -1, "unknown" }
340 };
341 int i;
342
343 for (i=0; sec_flavours[i].flavour != -1; i++) {
344 if (sec_flavours[i].flavour == flavour)
345 break;
346 }
347 return sec_flavours[i].str;
348}
349
350/*
351 * Describe the mount options in force on this server representation
352 */
353static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
354{
355 static struct proc_nfs_info {
356 int flag;
357 char *str;
358 char *nostr;
359 } nfs_info[] = {
360 { NFS_MOUNT_SOFT, ",soft", ",hard" },
361 { NFS_MOUNT_INTR, ",intr", "" },
362 { NFS_MOUNT_NOCTO, ",nocto", "" },
363 { NFS_MOUNT_NOAC, ",noac", "" },
364 { NFS_MOUNT_NONLM, ",nolock", "" },
365 { NFS_MOUNT_NOACL, ",noacl", "" },
366 { 0, NULL, NULL }
367 };
368 struct proc_nfs_info *nfs_infop;
369 char buf[12];
370 char *proto;
371
372 seq_printf(m, ",vers=%d", nfss->rpc_ops->version);
373 seq_printf(m, ",rsize=%d", nfss->rsize);
374 seq_printf(m, ",wsize=%d", nfss->wsize);
375 if (nfss->acregmin != 3*HZ || showdefaults)
376 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
377 if (nfss->acregmax != 60*HZ || showdefaults)
378 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
379 if (nfss->acdirmin != 30*HZ || showdefaults)
380 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
381 if (nfss->acdirmax != 60*HZ || showdefaults)
382 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
383 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
384 if (nfss->flags & nfs_infop->flag)
385 seq_puts(m, nfs_infop->str);
386 else
387 seq_puts(m, nfs_infop->nostr);
388 }
389 switch (nfss->client->cl_xprt->prot) {
390 case IPPROTO_TCP:
391 proto = "tcp";
392 break;
393 case IPPROTO_UDP:
394 proto = "udp";
395 break;
396 default:
397 snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
398 proto = buf;
399 }
400 seq_printf(m, ",proto=%s", proto);
401 seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
402 seq_printf(m, ",retrans=%u", nfss->retrans_count);
403 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
404}
405
406/*
407 * Describe the mount options on this VFS mountpoint
408 */
409static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
410{
411 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
412
413 nfs_show_mount_options(m, nfss, 0);
414
415 seq_puts(m, ",addr=");
416 seq_escape(m, nfss->hostname, " \t\n\\");
417
418 return 0;
419}
420
421/*
422 * Present statistical information for this VFS mountpoint
423 */
424static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
425{
426 int i, cpu;
427 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
428 struct rpc_auth *auth = nfss->client->cl_auth;
429 struct nfs_iostats totals = { };
430
431 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
432
433 /*
434 * Display all mount option settings
435 */
436 seq_printf(m, "\n\topts:\t");
437 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
438 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
439 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
440 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
441 nfs_show_mount_options(m, nfss, 1);
442
443 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
444
445 seq_printf(m, "\n\tcaps:\t");
446 seq_printf(m, "caps=0x%x", nfss->caps);
447 seq_printf(m, ",wtmult=%d", nfss->wtmult);
448 seq_printf(m, ",dtsize=%d", nfss->dtsize);
449 seq_printf(m, ",bsize=%d", nfss->bsize);
450 seq_printf(m, ",namelen=%d", nfss->namelen);
451
452#ifdef CONFIG_NFS_V4
453 if (nfss->rpc_ops->version == 4) {
454 seq_printf(m, "\n\tnfsv4:\t");
455 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
456 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
457 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
458 }
459#endif
460
461 /*
462 * Display security flavor in effect for this mount
463 */
464 seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
465 if (auth->au_flavor)
466 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
467
468 /*
469 * Display superblock I/O counters
470 */
471 for_each_possible_cpu(cpu) {
472 struct nfs_iostats *stats;
473
474 preempt_disable();
475 stats = per_cpu_ptr(nfss->io_stats, cpu);
476
477 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
478 totals.events[i] += stats->events[i];
479 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
480 totals.bytes[i] += stats->bytes[i];
481
482 preempt_enable();
483 }
484
485 seq_printf(m, "\n\tevents:\t");
486 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
487 seq_printf(m, "%lu ", totals.events[i]);
488 seq_printf(m, "\n\tbytes:\t");
489 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
490 seq_printf(m, "%Lu ", totals.bytes[i]);
491 seq_printf(m, "\n");
492
493 rpc_print_iostats(m, nfss->client);
494
495 return 0;
496}
497
498/*
499 * Begin unmount by attempting to remove all automounted mountpoints we added
500 * in response to traversals
501 */
502static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
503{
504 struct nfs_server *server;
505 struct rpc_clnt *rpc;
506
507 shrink_submounts(vfsmnt, &nfs_automount_list);
508 if (!(flags & MNT_FORCE))
509 return;
510 /* -EIO all pending I/O */
511 server = NFS_SB(vfsmnt->mnt_sb);
512 rpc = server->client;
513 if (!IS_ERR(rpc))
514 rpc_killall_tasks(rpc);
515 rpc = server->client_acl;
516 if (!IS_ERR(rpc))
517 rpc_killall_tasks(rpc);
518}
519
520/*
521 * Obtain the root inode of the file system.
522 */
523static struct inode *
524nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
525{
526 struct nfs_server *server = NFS_SB(sb);
527 int error;
528
529 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
530 if (error < 0) {
531 dprintk("nfs_get_root: getattr error = %d\n", -error);
532 return ERR_PTR(error);
533 }
534
535 server->fsid = fsinfo->fattr->fsid;
536 return nfs_fhget(sb, rootfh, fsinfo->fattr);
537}
538
539/*
540 * Do NFS version-independent mount processing, and sanity checking
541 */
542static int
543nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
544{
545 struct nfs_server *server;
546 struct inode *root_inode;
547 struct nfs_fattr fattr;
548 struct nfs_fsinfo fsinfo = {
549 .fattr = &fattr,
550 };
551 struct nfs_pathconf pathinfo = {
552 .fattr = &fattr,
553 };
554 int no_root_error = 0;
555 unsigned long max_rpc_payload;
556
557 /* We probably want something more informative here */
558 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
559
560 server = NFS_SB(sb);
561
562 sb->s_magic = NFS_SUPER_MAGIC;
563
564 server->io_stats = nfs_alloc_iostats();
565 if (server->io_stats == NULL)
566 return -ENOMEM;
567
568 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
569 /* Did getting the root inode fail? */
570 if (IS_ERR(root_inode)) {
571 no_root_error = PTR_ERR(root_inode);
572 goto out_no_root;
573 }
574 sb->s_root = d_alloc_root(root_inode);
575 if (!sb->s_root) {
576 no_root_error = -ENOMEM;
577 goto out_no_root;
578 }
579 sb->s_root->d_op = server->rpc_ops->dentry_ops;
580
581 /* mount time stamp, in seconds */
582 server->mount_time = jiffies;
583
584 /* Get some general file system info */
585 if (server->namelen == 0 &&
586 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
587 server->namelen = pathinfo.max_namelen;
588 /* Work out a lot of parameters */
589 if (server->rsize == 0)
590 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
591 if (server->wsize == 0)
592 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
593
594 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
595 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
596 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
597 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
598
599 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
600 if (server->rsize > max_rpc_payload)
601 server->rsize = max_rpc_payload;
602 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
603 server->rsize = NFS_MAX_FILE_IO_SIZE;
604 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
605
606 if (server->wsize > max_rpc_payload)
607 server->wsize = max_rpc_payload;
608 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
609 server->wsize = NFS_MAX_FILE_IO_SIZE;
610 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
611
612 if (sb->s_blocksize == 0)
613 sb->s_blocksize = nfs_block_bits(server->wsize,
614 &sb->s_blocksize_bits);
615 server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
616
617 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
618 if (server->dtsize > PAGE_CACHE_SIZE)
619 server->dtsize = PAGE_CACHE_SIZE;
620 if (server->dtsize > server->rsize)
621 server->dtsize = server->rsize;
622
623 if (server->flags & NFS_MOUNT_NOAC) {
624 server->acregmin = server->acregmax = 0;
625 server->acdirmin = server->acdirmax = 0;
626 sb->s_flags |= MS_SYNCHRONOUS;
627 }
628 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
629
630 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
631
632 server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
633 server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
634
635 /* We're airborne Set socket buffersize */
636 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
637 return 0;
638 /* Yargs. It didn't work out. */
639out_no_root:
640 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
641 if (!IS_ERR(root_inode))
642 iput(root_inode);
643 return no_root_error;
644}
645
646/*
647 * Initialise the timeout values for a connection
648 */
649static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
650{
651 to->to_initval = timeo * HZ / 10;
652 to->to_retries = retrans;
653 if (!to->to_retries)
654 to->to_retries = 2;
655
656 switch (proto) {
657 case IPPROTO_TCP:
658 if (!to->to_initval)
659 to->to_initval = 60 * HZ;
660 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
661 to->to_initval = NFS_MAX_TCP_TIMEOUT;
662 to->to_increment = to->to_initval;
663 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
664 to->to_exponential = 0;
665 break;
666 case IPPROTO_UDP:
667 default:
668 if (!to->to_initval)
669 to->to_initval = 11 * HZ / 10;
670 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
671 to->to_initval = NFS_MAX_UDP_TIMEOUT;
672 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
673 to->to_exponential = 1;
674 break;
675 }
676}
677
678/*
679 * Create an RPC client handle.
680 */
681static struct rpc_clnt *
682nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
683{
684 struct rpc_timeout timeparms;
685 struct rpc_xprt *xprt = NULL;
686 struct rpc_clnt *clnt = NULL;
687 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
688
689 nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);
690
691 server->retrans_timeo = timeparms.to_initval;
692 server->retrans_count = timeparms.to_retries;
693
694 /* create transport and client */
695 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
696 if (IS_ERR(xprt)) {
697 dprintk("%s: cannot create RPC transport. Error = %ld\n",
698 __FUNCTION__, PTR_ERR(xprt));
699 return (struct rpc_clnt *)xprt;
700 }
701 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
702 server->rpc_ops->version, data->pseudoflavor);
703 if (IS_ERR(clnt)) {
704 dprintk("%s: cannot create RPC client. Error = %ld\n",
705 __FUNCTION__, PTR_ERR(xprt));
706 goto out_fail;
707 }
708
709 clnt->cl_intr = 1;
710 clnt->cl_softrtry = 1;
711
712 return clnt;
713
714out_fail:
715 return clnt;
716}
717
718/*
719 * Clone a server record
720 */
721static struct nfs_server *nfs_clone_server(struct super_block *sb, struct nfs_clone_mount *data)
722{
723 struct nfs_server *server = NFS_SB(sb);
724 struct nfs_server *parent = NFS_SB(data->sb);
725 struct inode *root_inode;
726 struct nfs_fsinfo fsinfo;
727 void *err = ERR_PTR(-ENOMEM);
728
729 sb->s_op = data->sb->s_op;
730 sb->s_blocksize = data->sb->s_blocksize;
731 sb->s_blocksize_bits = data->sb->s_blocksize_bits;
732 sb->s_maxbytes = data->sb->s_maxbytes;
733
734 server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
735 server->io_stats = nfs_alloc_iostats();
736 if (server->io_stats == NULL)
737 goto out;
738
739 server->client = rpc_clone_client(parent->client);
740 if (IS_ERR((err = server->client)))
741 goto out;
742
743 if (!IS_ERR(parent->client_sys)) {
744 server->client_sys = rpc_clone_client(parent->client_sys);
745 if (IS_ERR((err = server->client_sys)))
746 goto out;
747 }
748 if (!IS_ERR(parent->client_acl)) {
749 server->client_acl = rpc_clone_client(parent->client_acl);
750 if (IS_ERR((err = server->client_acl)))
751 goto out;
752 }
753 root_inode = nfs_fhget(sb, data->fh, data->fattr);
754 if (!root_inode)
755 goto out;
756 sb->s_root = d_alloc_root(root_inode);
757 if (!sb->s_root)
758 goto out_put_root;
759 fsinfo.fattr = data->fattr;
760 if (NFS_PROTO(root_inode)->fsinfo(server, data->fh, &fsinfo) == 0)
761 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
762 sb->s_root->d_op = server->rpc_ops->dentry_ops;
763 sb->s_flags |= MS_ACTIVE;
764 return server;
765out_put_root:
766 iput(root_inode);
767out:
768 return err;
769}
770
771/*
772 * Copy an existing superblock and attach revised data
773 */
774static int nfs_clone_generic_sb(struct nfs_clone_mount *data,
775 struct super_block *(*fill_sb)(struct nfs_server *, struct nfs_clone_mount *),
776 struct nfs_server *(*fill_server)(struct super_block *, struct nfs_clone_mount *),
777 struct vfsmount *mnt)
778{
779 struct nfs_server *server;
780 struct nfs_server *parent = NFS_SB(data->sb);
781 struct super_block *sb = ERR_PTR(-EINVAL);
782 char *hostname;
783 int error = -ENOMEM;
784 int len;
785
786 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
787 if (server == NULL)
788 goto out_err;
789 memcpy(server, parent, sizeof(*server));
790 hostname = (data->hostname != NULL) ? data->hostname : parent->hostname;
791 len = strlen(hostname) + 1;
792 server->hostname = kmalloc(len, GFP_KERNEL);
793 if (server->hostname == NULL)
794 goto free_server;
795 memcpy(server->hostname, hostname, len);
796 error = rpciod_up();
797 if (error != 0)
798 goto free_hostname;
799
800 sb = fill_sb(server, data);
801 if (IS_ERR(sb)) {
802 error = PTR_ERR(sb);
803 goto kill_rpciod;
804 }
805
806 if (sb->s_root)
807 goto out_rpciod_down;
808
809 server = fill_server(sb, data);
810 if (IS_ERR(server)) {
811 error = PTR_ERR(server);
812 goto out_deactivate;
813 }
814 return simple_set_mnt(mnt, sb);
815out_deactivate:
816 up_write(&sb->s_umount);
817 deactivate_super(sb);
818 return error;
819out_rpciod_down:
820 rpciod_down();
821 kfree(server->hostname);
822 kfree(server);
823 return simple_set_mnt(mnt, sb);
824kill_rpciod:
825 rpciod_down();
826free_hostname:
827 kfree(server->hostname);
828free_server:
829 kfree(server);
830out_err:
831 return error;
832}
833
834/*
835 * Set up an NFS2/3 superblock
836 *
837 * The way this works is that the mount process passes a structure
838 * in the data argument which contains the server's IP address
839 * and the root file handle obtained from the server's mount
840 * daemon. We stash these away in the private superblock fields.
841 */
842static int
843nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
844{
845 struct nfs_server *server;
846 rpc_authflavor_t authflavor;
847
848 server = NFS_SB(sb);
849 sb->s_blocksize_bits = 0;
850 sb->s_blocksize = 0;
851 if (data->bsize)
852 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
853 if (data->rsize)
854 server->rsize = nfs_block_size(data->rsize, NULL);
855 if (data->wsize)
856 server->wsize = nfs_block_size(data->wsize, NULL);
857 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
858
859 server->acregmin = data->acregmin*HZ;
860 server->acregmax = data->acregmax*HZ;
861 server->acdirmin = data->acdirmin*HZ;
862 server->acdirmax = data->acdirmax*HZ;
863
864 /* Start lockd here, before we might error out */
865 if (!(server->flags & NFS_MOUNT_NONLM))
866 lockd_up();
867
868 server->namelen = data->namlen;
869 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
870 if (!server->hostname)
871 return -ENOMEM;
872 strcpy(server->hostname, data->hostname);
873
874 /* Check NFS protocol revision and initialize RPC op vector
875 * and file handle pool. */
876#ifdef CONFIG_NFS_V3
877 if (server->flags & NFS_MOUNT_VER3) {
878 server->rpc_ops = &nfs_v3_clientops;
879 server->caps |= NFS_CAP_READDIRPLUS;
880 } else {
881 server->rpc_ops = &nfs_v2_clientops;
882 }
883#else
884 server->rpc_ops = &nfs_v2_clientops;
885#endif
886
887 /* Fill in pseudoflavor for mount version < 5 */
888 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
889 data->pseudoflavor = RPC_AUTH_UNIX;
890 authflavor = data->pseudoflavor; /* save for sb_init() */
891 /* XXX maybe we want to add a server->pseudoflavor field */
892
893 /* Create RPC client handles */
894 server->client = nfs_create_client(server, data);
895 if (IS_ERR(server->client))
896 return PTR_ERR(server->client);
897 /* RFC 2623, sec 2.3.2 */
898 if (authflavor != RPC_AUTH_UNIX) {
899 struct rpc_auth *auth;
900
901 server->client_sys = rpc_clone_client(server->client);
902 if (IS_ERR(server->client_sys))
903 return PTR_ERR(server->client_sys);
904 auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
905 if (IS_ERR(auth))
906 return PTR_ERR(auth);
907 } else {
908 atomic_inc(&server->client->cl_count);
909 server->client_sys = server->client;
910 }
911 if (server->flags & NFS_MOUNT_VER3) {
912#ifdef CONFIG_NFS_V3_ACL
913 if (!(server->flags & NFS_MOUNT_NOACL)) {
914 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
915 /* No errors! Assume that Sun nfsacls are supported */
916 if (!IS_ERR(server->client_acl))
917 server->caps |= NFS_CAP_ACLS;
918 }
919#else
920 server->flags &= ~NFS_MOUNT_NOACL;
921#endif /* CONFIG_NFS_V3_ACL */
922 /*
923 * The VFS shouldn't apply the umask to mode bits. We will
924 * do so ourselves when necessary.
925 */
926 sb->s_flags |= MS_POSIXACL;
927 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
928 server->namelen = NFS3_MAXNAMLEN;
929 sb->s_time_gran = 1;
930 } else {
931 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
932 server->namelen = NFS2_MAXNAMLEN;
933 }
934
935 sb->s_op = &nfs_sops;
936 return nfs_sb_init(sb, authflavor);
937}
938
939static int nfs_set_super(struct super_block *s, void *data)
940{
941 s->s_fs_info = data;
942 return set_anon_super(s, data);
943}
944
945static int nfs_compare_super(struct super_block *sb, void *data)
946{
947 struct nfs_server *server = data;
948 struct nfs_server *old = NFS_SB(sb);
949
950 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
951 return 0;
952 if (old->addr.sin_port != server->addr.sin_port)
953 return 0;
954 return !nfs_compare_fh(&old->fh, &server->fh);
955}
956
957static int nfs_get_sb(struct file_system_type *fs_type,
958 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
959{
960 int error;
961 struct nfs_server *server = NULL;
962 struct super_block *s;
963 struct nfs_fh *root;
964 struct nfs_mount_data *data = raw_data;
965
966 error = -EINVAL;
967 if (data == NULL) {
968 dprintk("%s: missing data argument\n", __FUNCTION__);
969 goto out_err_noserver;
970 }
971 if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
972 dprintk("%s: bad mount version\n", __FUNCTION__);
973 goto out_err_noserver;
974 }
975 switch (data->version) {
976 case 1:
977 data->namlen = 0;
978 case 2:
979 data->bsize = 0;
980 case 3:
981 if (data->flags & NFS_MOUNT_VER3) {
982 dprintk("%s: mount structure version %d does not support NFSv3\n",
983 __FUNCTION__,
984 data->version);
985 goto out_err_noserver;
986 }
987 data->root.size = NFS2_FHSIZE;
988 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
989 case 4:
990 if (data->flags & NFS_MOUNT_SECFLAVOUR) {
991 dprintk("%s: mount structure version %d does not support strong security\n",
992 __FUNCTION__,
993 data->version);
994 goto out_err_noserver;
995 }
996 case 5:
997 memset(data->context, 0, sizeof(data->context));
998 }
999#ifndef CONFIG_NFS_V3
1000 /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
1001 error = -EPROTONOSUPPORT;
1002 if (data->flags & NFS_MOUNT_VER3) {
1003 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
1004 goto out_err_noserver;
1005 }
1006#endif /* CONFIG_NFS_V3 */
1007
1008 error = -ENOMEM;
1009 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1010 if (!server)
1011 goto out_err_noserver;
1012 /* Zero out the NFS state stuff */
1013 init_nfsv4_state(server);
1014 server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
1015
1016 root = &server->fh;
1017 if (data->flags & NFS_MOUNT_VER3)
1018 root->size = data->root.size;
1019 else
1020 root->size = NFS2_FHSIZE;
1021 error = -EINVAL;
1022 if (root->size > sizeof(root->data)) {
1023 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
1024 goto out_err;
1025 }
1026 memcpy(root->data, data->root.data, root->size);
1027
1028 /* We now require that the mount process passes the remote address */
1029 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1030 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1031 dprintk("%s: mount program didn't pass remote address!\n",
1032 __FUNCTION__);
1033 goto out_err;
1034 }
1035
1036 /* Fire up rpciod if not yet running */
1037 error = rpciod_up();
1038 if (error < 0) {
1039 dprintk("%s: couldn't start rpciod! Error = %d\n",
1040 __FUNCTION__, error);
1041 goto out_err;
1042 }
1043
1044 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1045 if (IS_ERR(s)) {
1046 error = PTR_ERR(s);
1047 goto out_err_rpciod;
1048 }
1049
1050 if (s->s_root)
1051 goto out_rpciod_down;
1052
1053 s->s_flags = flags;
1054
1055 error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1056 if (error) {
1057 up_write(&s->s_umount);
1058 deactivate_super(s);
1059 return error;
1060 }
1061 s->s_flags |= MS_ACTIVE;
1062 return simple_set_mnt(mnt, s);
1063
1064out_rpciod_down:
1065 rpciod_down();
1066 kfree(server);
1067 return simple_set_mnt(mnt, s);
1068
1069out_err_rpciod:
1070 rpciod_down();
1071out_err:
1072 kfree(server);
1073out_err_noserver:
1074 return error;
1075}
1076
1077static void nfs_kill_super(struct super_block *s)
1078{
1079 struct nfs_server *server = NFS_SB(s);
1080
1081 kill_anon_super(s);
1082
1083 if (!IS_ERR(server->client))
1084 rpc_shutdown_client(server->client);
1085 if (!IS_ERR(server->client_sys))
1086 rpc_shutdown_client(server->client_sys);
1087 if (!IS_ERR(server->client_acl))
1088 rpc_shutdown_client(server->client_acl);
1089
1090 if (!(server->flags & NFS_MOUNT_NONLM))
1091 lockd_down(); /* release rpc.lockd */
1092
1093 rpciod_down(); /* release rpciod */
1094
1095 nfs_free_iostats(server->io_stats);
1096 kfree(server->hostname);
1097 kfree(server);
1098 nfs_release_automount_timer();
1099}
1100
1101static struct super_block *nfs_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
1102{
1103 struct super_block *sb;
1104
1105 server->fsid = data->fattr->fsid;
1106 nfs_copy_fh(&server->fh, data->fh);
1107 sb = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1108 if (!IS_ERR(sb) && sb->s_root == NULL && !(server->flags & NFS_MOUNT_NONLM))
1109 lockd_up();
1110 return sb;
1111}
1112
1113static int nfs_clone_nfs_sb(struct file_system_type *fs_type,
1114 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1115{
1116 struct nfs_clone_mount *data = raw_data;
1117 return nfs_clone_generic_sb(data, nfs_clone_sb, nfs_clone_server, mnt);
1118}
1119
1120#ifdef CONFIG_NFS_V4
1121static struct rpc_clnt *nfs4_create_client(struct nfs_server *server,
1122 struct rpc_timeout *timeparms, int proto, rpc_authflavor_t flavor)
1123{
1124 struct nfs4_client *clp;
1125 struct rpc_xprt *xprt = NULL;
1126 struct rpc_clnt *clnt = NULL;
1127 int err = -EIO;
1128
1129 clp = nfs4_get_client(&server->addr.sin_addr);
1130 if (!clp) {
1131 dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
1132 return ERR_PTR(err);
1133 }
1134
1135 /* Now create transport and client */
1136 down_write(&clp->cl_sem);
1137 if (IS_ERR(clp->cl_rpcclient)) {
1138 xprt = xprt_create_proto(proto, &server->addr, timeparms);
1139 if (IS_ERR(xprt)) {
1140 up_write(&clp->cl_sem);
1141 err = PTR_ERR(xprt);
1142 dprintk("%s: cannot create RPC transport. Error = %d\n",
1143 __FUNCTION__, err);
1144 goto out_fail;
1145 }
1146 /* Bind to a reserved port! */
1147 xprt->resvport = 1;
1148 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1149 server->rpc_ops->version, flavor);
1150 if (IS_ERR(clnt)) {
1151 up_write(&clp->cl_sem);
1152 err = PTR_ERR(clnt);
1153 dprintk("%s: cannot create RPC client. Error = %d\n",
1154 __FUNCTION__, err);
1155 goto out_fail;
1156 }
1157 clnt->cl_intr = 1;
1158 clnt->cl_softrtry = 1;
1159 clp->cl_rpcclient = clnt;
1160 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1161 nfs_idmap_new(clp);
1162 }
1163 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1164 clnt = rpc_clone_client(clp->cl_rpcclient);
1165 if (!IS_ERR(clnt))
1166 server->nfs4_state = clp;
1167 up_write(&clp->cl_sem);
1168 clp = NULL;
1169
1170 if (IS_ERR(clnt)) {
1171 dprintk("%s: cannot create RPC client. Error = %d\n",
1172 __FUNCTION__, err);
1173 return clnt;
1174 }
1175
1176 if (server->nfs4_state->cl_idmap == NULL) {
1177 dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
1178 return ERR_PTR(-ENOMEM);
1179 }
1180
1181 if (clnt->cl_auth->au_flavor != flavor) {
1182 struct rpc_auth *auth;
1183
1184 auth = rpcauth_create(flavor, clnt);
1185 if (IS_ERR(auth)) {
1186 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
1187 return (struct rpc_clnt *)auth;
1188 }
1189 }
1190 return clnt;
1191
1192 out_fail:
1193 if (clp)
1194 nfs4_put_client(clp);
1195 return ERR_PTR(err);
1196}
1197
1198/*
1199 * Set up an NFS4 superblock
1200 */
1201static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1202{
1203 struct nfs_server *server;
1204 struct rpc_timeout timeparms;
1205 rpc_authflavor_t authflavour;
1206 int err = -EIO;
1207
1208 sb->s_blocksize_bits = 0;
1209 sb->s_blocksize = 0;
1210 server = NFS_SB(sb);
1211 if (data->rsize != 0)
1212 server->rsize = nfs_block_size(data->rsize, NULL);
1213 if (data->wsize != 0)
1214 server->wsize = nfs_block_size(data->wsize, NULL);
1215 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1216 server->caps = NFS_CAP_ATOMIC_OPEN;
1217
1218 server->acregmin = data->acregmin*HZ;
1219 server->acregmax = data->acregmax*HZ;
1220 server->acdirmin = data->acdirmin*HZ;
1221 server->acdirmax = data->acdirmax*HZ;
1222
1223 server->rpc_ops = &nfs_v4_clientops;
1224
1225 nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
1226
1227 server->retrans_timeo = timeparms.to_initval;
1228 server->retrans_count = timeparms.to_retries;
1229
1230 /* Now create transport and client */
1231 authflavour = RPC_AUTH_UNIX;
1232 if (data->auth_flavourlen != 0) {
1233 if (data->auth_flavourlen != 1) {
1234 dprintk("%s: Invalid number of RPC auth flavours %d.\n",
1235 __FUNCTION__, data->auth_flavourlen);
1236 err = -EINVAL;
1237 goto out_fail;
1238 }
1239 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1240 err = -EFAULT;
1241 goto out_fail;
1242 }
1243 }
1244
1245 server->client = nfs4_create_client(server, &timeparms, data->proto, authflavour);
1246 if (IS_ERR(server->client)) {
1247 err = PTR_ERR(server->client);
1248 dprintk("%s: cannot create RPC client. Error = %d\n",
1249 __FUNCTION__, err);
1250 goto out_fail;
1251 }
1252
1253 sb->s_time_gran = 1;
1254
1255 sb->s_op = &nfs4_sops;
1256 err = nfs_sb_init(sb, authflavour);
1257
1258 out_fail:
1259 return err;
1260}
1261
1262static int nfs4_compare_super(struct super_block *sb, void *data)
1263{
1264 struct nfs_server *server = data;
1265 struct nfs_server *old = NFS_SB(sb);
1266
1267 if (strcmp(server->hostname, old->hostname) != 0)
1268 return 0;
1269 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1270 return 0;
1271 return 1;
1272}
1273
1274static void *
1275nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1276{
1277 void *p = NULL;
1278
1279 if (!src->len)
1280 return ERR_PTR(-EINVAL);
1281 if (src->len < maxlen)
1282 maxlen = src->len;
1283 if (dst == NULL) {
1284 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1285 if (p == NULL)
1286 return ERR_PTR(-ENOMEM);
1287 }
1288 if (copy_from_user(dst, src->data, maxlen)) {
1289 kfree(p);
1290 return ERR_PTR(-EFAULT);
1291 }
1292 dst[maxlen] = '\0';
1293 return dst;
1294}
1295
1296static int nfs4_get_sb(struct file_system_type *fs_type,
1297 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1298{
1299 int error;
1300 struct nfs_server *server;
1301 struct super_block *s;
1302 struct nfs4_mount_data *data = raw_data;
1303 void *p;
1304
1305 if (data == NULL) {
1306 dprintk("%s: missing data argument\n", __FUNCTION__);
1307 return -EINVAL;
1308 }
1309 if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
1310 dprintk("%s: bad mount version\n", __FUNCTION__);
1311 return -EINVAL;
1312 }
1313
1314 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1315 if (!server)
1316 return -ENOMEM;
1317 /* Zero out the NFS state stuff */
1318 init_nfsv4_state(server);
1319 server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
1320
1321 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1322 if (IS_ERR(p))
1323 goto out_err;
1324 server->hostname = p;
1325
1326 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1327 if (IS_ERR(p))
1328 goto out_err;
1329 server->mnt_path = p;
1330
1331 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1332 sizeof(server->ip_addr) - 1);
1333 if (IS_ERR(p))
1334 goto out_err;
1335
1336 /* We now require that the mount process passes the remote address */
1337 if (data->host_addrlen != sizeof(server->addr)) {
1338 error = -EINVAL;
1339 goto out_free;
1340 }
1341 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1342 error = -EFAULT;
1343 goto out_free;
1344 }
1345 if (server->addr.sin_family != AF_INET ||
1346 server->addr.sin_addr.s_addr == INADDR_ANY) {
1347 dprintk("%s: mount program didn't pass remote IP address!\n",
1348 __FUNCTION__);
1349 error = -EINVAL;
1350 goto out_free;
1351 }
1352
1353 /* Fire up rpciod if not yet running */
1354 error = rpciod_up();
1355 if (error < 0) {
1356 dprintk("%s: couldn't start rpciod! Error = %d\n",
1357 __FUNCTION__, error);
1358 goto out_free;
1359 }
1360
1361 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1362
1363 if (IS_ERR(s)) {
1364 error = PTR_ERR(s);
1365 goto out_free;
1366 }
1367
1368 if (s->s_root) {
1369 kfree(server->mnt_path);
1370 kfree(server->hostname);
1371 kfree(server);
1372 return simple_set_mnt(mnt, s);
1373 }
1374
1375 s->s_flags = flags;
1376
1377 error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1378 if (error) {
1379 up_write(&s->s_umount);
1380 deactivate_super(s);
1381 return error;
1382 }
1383 s->s_flags |= MS_ACTIVE;
1384 return simple_set_mnt(mnt, s);
1385out_err:
1386 error = PTR_ERR(p);
1387out_free:
1388 kfree(server->mnt_path);
1389 kfree(server->hostname);
1390 kfree(server);
1391 return error;
1392}
1393
1394static void nfs4_kill_super(struct super_block *sb)
1395{
1396 struct nfs_server *server = NFS_SB(sb);
1397
1398 nfs_return_all_delegations(sb);
1399 kill_anon_super(sb);
1400
1401 nfs4_renewd_prepare_shutdown(server);
1402
1403 if (server->client != NULL && !IS_ERR(server->client))
1404 rpc_shutdown_client(server->client);
1405
1406 destroy_nfsv4_state(server);
1407
1408 rpciod_down();
1409
1410 nfs_free_iostats(server->io_stats);
1411 kfree(server->hostname);
1412 kfree(server);
1413 nfs_release_automount_timer();
1414}
1415
1416/*
1417 * Constructs the SERVER-side path
1418 */
1419static inline char *nfs4_dup_path(const struct dentry *dentry)
1420{
1421 char *page = (char *) __get_free_page(GFP_USER);
1422 char *path;
1423
1424 path = nfs4_path(dentry, page, PAGE_SIZE);
1425 if (!IS_ERR(path)) {
1426 int len = PAGE_SIZE + page - path;
1427 char *tmp = path;
1428
1429 path = kmalloc(len, GFP_KERNEL);
1430 if (path)
1431 memcpy(path, tmp, len);
1432 else
1433 path = ERR_PTR(-ENOMEM);
1434 }
1435 free_page((unsigned long)page);
1436 return path;
1437}
1438
1439static struct super_block *nfs4_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
1440{
1441 const struct dentry *dentry = data->dentry;
1442 struct nfs4_client *clp = server->nfs4_state;
1443 struct super_block *sb;
1444
1445 server->fsid = data->fattr->fsid;
1446 nfs_copy_fh(&server->fh, data->fh);
1447 server->mnt_path = nfs4_dup_path(dentry);
1448 if (IS_ERR(server->mnt_path)) {
1449 sb = (struct super_block *)server->mnt_path;
1450 goto err;
1451 }
1452 sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
1453 if (IS_ERR(sb) || sb->s_root)
1454 goto free_path;
1455 nfs4_server_capabilities(server, &server->fh);
1456
1457 down_write(&clp->cl_sem);
1458 atomic_inc(&clp->cl_count);
1459 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1460 up_write(&clp->cl_sem);
1461 return sb;
1462free_path:
1463 kfree(server->mnt_path);
1464err:
1465 server->mnt_path = NULL;
1466 return sb;
1467}
1468
1469static int nfs_clone_nfs4_sb(struct file_system_type *fs_type,
1470 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1471{
1472 struct nfs_clone_mount *data = raw_data;
1473 return nfs_clone_generic_sb(data, nfs4_clone_sb, nfs_clone_server, mnt);
1474}
1475
1476static struct super_block *nfs4_referral_sb(struct nfs_server *server, struct nfs_clone_mount *data)
1477{
1478 struct super_block *sb = ERR_PTR(-ENOMEM);
1479 int len;
1480
1481 len = strlen(data->mnt_path) + 1;
1482 server->mnt_path = kmalloc(len, GFP_KERNEL);
1483 if (server->mnt_path == NULL)
1484 goto err;
1485 memcpy(server->mnt_path, data->mnt_path, len);
1486 memcpy(&server->addr, data->addr, sizeof(struct sockaddr_in));
1487
1488 sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
1489 if (IS_ERR(sb) || sb->s_root)
1490 goto free_path;
1491 return sb;
1492free_path:
1493 kfree(server->mnt_path);
1494err:
1495 server->mnt_path = NULL;
1496 return sb;
1497}
1498
1499static struct nfs_server *nfs4_referral_server(struct super_block *sb, struct nfs_clone_mount *data)
1500{
1501 struct nfs_server *server = NFS_SB(sb);
1502 struct rpc_timeout timeparms;
1503 int proto, timeo, retrans;
1504 void *err;
1505
1506 proto = IPPROTO_TCP;
1507 /* Since we are following a referral and there may be alternatives,
1508 set the timeouts and retries to low values */
1509 timeo = 2;
1510 retrans = 1;
1511 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
1512
1513 server->client = nfs4_create_client(server, &timeparms, proto, data->authflavor);
1514 if (IS_ERR((err = server->client)))
1515 goto out_err;
1516
1517 sb->s_time_gran = 1;
1518 sb->s_op = &nfs4_sops;
1519 err = ERR_PTR(nfs_sb_init(sb, data->authflavor));
1520 if (!IS_ERR(err))
1521 return server;
1522out_err:
1523 return (struct nfs_server *)err;
1524}
1525
1526static int nfs_referral_nfs4_sb(struct file_system_type *fs_type,
1527 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1528{
1529 struct nfs_clone_mount *data = raw_data;
1530 return nfs_clone_generic_sb(data, nfs4_referral_sb, nfs4_referral_server, mnt);
1531}
1532
1533#endif