super.c 40.0 KB
Newer Older
D
David Howells 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/*
 *  linux/fs/nfs/super.c
 *
 *  Copyright (C) 1992  Rick Sladkey
 *
 *  nfs superblock handling functions
 *
 *  Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
 *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
 *
 *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
 *  J.S.Peatfield@damtp.cam.ac.uk
 *
 *  Split from inode.c by David Howells <dhowells@redhat.com>
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>

#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/stats.h>
#include <linux/sunrpc/metrics.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_mount.h>
#include <linux/nfs4_mount.h>
#include <linux/lockd/bind.h>
#include <linux/smp_lock.h>
#include <linux/seq_file.h>
#include <linux/mount.h>
#include <linux/nfs_idmap.h>
#include <linux/vfs.h>
#include <linux/inet.h>
#include <linux/nfs_xdr.h>

#include <asm/system.h>
#include <asm/uaccess.h>

#include "nfs4_fs.h"
#include "callback.h"
#include "delegation.h"
#include "iostat.h"
#include "internal.h"

#define NFSDBG_FACILITY		NFSDBG_VFS

/* Maximum number of readahead requests
 * FIXME: this should really be a sysctl so that users may tune it to suit
 *        their needs. People that do NFS over a slow network, might for
 *        instance want to reduce it to something closer to 1 for improved
 *        interactive response.
 */
#define NFS_MAX_READAHEAD	(RPC_DEF_SLOT_TABLE - 1)

/*
 * RPC cruft for NFS
 */
static struct rpc_version * nfs_version[] = {
	NULL,
	NULL,
	&nfs_version2,
#if defined(CONFIG_NFS_V3)
	&nfs_version3,
#elif defined(CONFIG_NFS_V4)
	NULL,
#endif
#if defined(CONFIG_NFS_V4)
	&nfs_version4,
#endif
};

static struct rpc_program nfs_program = {
	.name			= "nfs",
	.number			= NFS_PROGRAM,
	.nrvers			= ARRAY_SIZE(nfs_version),
	.version		= nfs_version,
	.stats			= &nfs_rpcstat,
	.pipe_dir_name		= "/nfs",
};

struct rpc_stat nfs_rpcstat = {
	.program		= &nfs_program
};


#ifdef CONFIG_NFS_V3_ACL
static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
static struct rpc_version *	nfsacl_version[] = {
	[3]			= &nfsacl_version3,
};

struct rpc_program		nfsacl_program = {
	.name =			"nfsacl",
	.number =		NFS_ACL_PROGRAM,
	.nrvers =		ARRAY_SIZE(nfsacl_version),
	.version =		nfsacl_version,
	.stats =		&nfsacl_rpcstat,
};
#endif  /* CONFIG_NFS_V3_ACL */

static void nfs_umount_begin(struct vfsmount *, int);
110
static int  nfs_statfs(struct dentry *, struct kstatfs *);
D
David Howells 已提交
111 112
static int  nfs_show_options(struct seq_file *, struct vfsmount *);
static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
113 114 115
static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
static int nfs_clone_nfs_sb(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
D
David Howells 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static void nfs_kill_super(struct super_block *);

static struct file_system_type nfs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfs",
	.get_sb		= nfs_get_sb,
	.kill_sb	= nfs_kill_super,
	.fs_flags	= FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
};

struct file_system_type clone_nfs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfs",
	.get_sb		= nfs_clone_nfs_sb,
	.kill_sb	= nfs_kill_super,
	.fs_flags	= FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
};

static struct super_operations nfs_sops = {
	.alloc_inode	= nfs_alloc_inode,
	.destroy_inode	= nfs_destroy_inode,
	.write_inode	= nfs_write_inode,
	.statfs		= nfs_statfs,
	.clear_inode	= nfs_clear_inode,
	.umount_begin	= nfs_umount_begin,
	.show_options	= nfs_show_options,
	.show_stats	= nfs_show_stats,
};

#ifdef CONFIG_NFS_V4
146 147 148 149 150 151
static int nfs4_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
static int nfs_clone_nfs4_sb(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
static int nfs_referral_nfs4_sb(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
D
David Howells 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
static void nfs4_kill_super(struct super_block *sb);

static struct file_system_type nfs4_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfs4",
	.get_sb		= nfs4_get_sb,
	.kill_sb	= nfs4_kill_super,
	.fs_flags	= FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
};

struct file_system_type clone_nfs4_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfs4",
	.get_sb		= nfs_clone_nfs4_sb,
	.kill_sb	= nfs4_kill_super,
	.fs_flags	= FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
};

struct file_system_type nfs_referral_nfs4_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfs4",
	.get_sb		= nfs_referral_nfs4_sb,
	.kill_sb	= nfs4_kill_super,
	.fs_flags	= FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
};

static struct super_operations nfs4_sops = {
	.alloc_inode	= nfs_alloc_inode,
	.destroy_inode	= nfs_destroy_inode,
	.write_inode	= nfs_write_inode,
	.statfs		= nfs_statfs,
	.clear_inode	= nfs4_clear_inode,
	.umount_begin	= nfs_umount_begin,
	.show_options	= nfs_show_options,
	.show_stats	= nfs_show_stats,
};
#endif

190 191
static struct shrinker *acl_shrinker;

D
David Howells 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/*
 * Register the NFS filesystems
 */
int __init register_nfs_fs(void)
{
	int ret;

        ret = register_filesystem(&nfs_fs_type);
	if (ret < 0)
		goto error_0;

#ifdef CONFIG_NFS_V4
	ret = nfs_register_sysctl();
	if (ret < 0)
		goto error_1;
	ret = register_filesystem(&nfs4_fs_type);
	if (ret < 0)
		goto error_2;
#endif
211
	acl_shrinker = set_shrinker(DEFAULT_SEEKS, nfs_access_cache_shrinker);
D
David Howells 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	return 0;

#ifdef CONFIG_NFS_V4
error_2:
	nfs_unregister_sysctl();
error_1:
	unregister_filesystem(&nfs_fs_type);
#endif
error_0:
	return ret;
}

/*
 * Unregister the NFS filesystems
 */
void __exit unregister_nfs_fs(void)
{
229 230
	if (acl_shrinker != NULL)
		remove_shrinker(acl_shrinker);
D
David Howells 已提交
231 232 233 234 235 236 237 238 239 240
#ifdef CONFIG_NFS_V4
	unregister_filesystem(&nfs4_fs_type);
	nfs_unregister_sysctl();
#endif
	unregister_filesystem(&nfs_fs_type);
}

/*
 * Deliver file system statistics to userspace
 */
241
static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
D
David Howells 已提交
242
{
243
	struct nfs_server *server = NFS_SB(dentry->d_sb);
D
David Howells 已提交
244 245
	unsigned char blockbits;
	unsigned long blockres;
246
	struct nfs_fh *fh = NFS_FH(dentry->d_inode);
D
David Howells 已提交
247 248 249 250 251 252 253 254
	struct nfs_fattr fattr;
	struct nfs_fsstat res = {
			.fattr = &fattr,
	};
	int error;

	lock_kernel();

255
	error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
D
David Howells 已提交
256 257 258 259 260 261 262 263 264
	buf->f_type = NFS_SUPER_MAGIC;
	if (error < 0)
		goto out_err;

	/*
	 * Current versions of glibc do not correctly handle the
	 * case where f_frsize != f_bsize.  Eventually we want to
	 * report the value of wtmult in this field.
	 */
265
	buf->f_frsize = dentry->d_sb->s_blocksize;
D
David Howells 已提交
266 267 268 269 270 271 272 273

	/*
	 * On most *nix systems, f_blocks, f_bfree, and f_bavail
	 * are reported in units of f_frsize.  Linux hasn't had
	 * an f_frsize field in its statfs struct until recently,
	 * thus historically Linux's sys_statfs reports these
	 * fields in units of f_bsize.
	 */
274 275
	buf->f_bsize = dentry->d_sb->s_blocksize;
	blockbits = dentry->d_sb->s_blocksize_bits;
D
David Howells 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	blockres = (1 << blockbits) - 1;
	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
	buf->f_bavail = (res.abytes + blockres) >> blockbits;

	buf->f_files = res.tfiles;
	buf->f_ffree = res.afiles;

	buf->f_namelen = server->namelen;
 out:
	unlock_kernel();
	return 0;

 out_err:
	dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
	buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
	goto out;

}

296 297 298
/*
 * Map the security flavour number to a name
 */
299 300
static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
{
301
	static const struct {
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
		rpc_authflavor_t flavour;
		const char *str;
	} sec_flavours[] = {
		{ RPC_AUTH_NULL, "null" },
		{ RPC_AUTH_UNIX, "sys" },
		{ RPC_AUTH_GSS_KRB5, "krb5" },
		{ RPC_AUTH_GSS_KRB5I, "krb5i" },
		{ RPC_AUTH_GSS_KRB5P, "krb5p" },
		{ RPC_AUTH_GSS_LKEY, "lkey" },
		{ RPC_AUTH_GSS_LKEYI, "lkeyi" },
		{ RPC_AUTH_GSS_LKEYP, "lkeyp" },
		{ RPC_AUTH_GSS_SPKM, "spkm" },
		{ RPC_AUTH_GSS_SPKMI, "spkmi" },
		{ RPC_AUTH_GSS_SPKMP, "spkmp" },
		{ -1, "unknown" }
	};
	int i;

	for (i=0; sec_flavours[i].flavour != -1; i++) {
		if (sec_flavours[i].flavour == flavour)
			break;
	}
	return sec_flavours[i].str;
}

D
David Howells 已提交
327 328 329 330 331
/*
 * Describe the mount options in force on this server representation
 */
static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
{
D
David Howells 已提交
332
	static const struct proc_nfs_info {
D
David Howells 已提交
333
		int flag;
D
David Howells 已提交
334 335
		const char *str;
		const char *nostr;
D
David Howells 已提交
336 337 338 339 340 341 342 343 344
	} nfs_info[] = {
		{ NFS_MOUNT_SOFT, ",soft", ",hard" },
		{ NFS_MOUNT_INTR, ",intr", "" },
		{ NFS_MOUNT_NOCTO, ",nocto", "" },
		{ NFS_MOUNT_NOAC, ",noac", "" },
		{ NFS_MOUNT_NONLM, ",nolock", "" },
		{ NFS_MOUNT_NOACL, ",noacl", "" },
		{ 0, NULL, NULL }
	};
D
David Howells 已提交
345
	const struct proc_nfs_info *nfs_infop;
346
	struct nfs_client *clp = nfss->nfs_client;
D
David Howells 已提交
347
	char buf[12];
D
David Howells 已提交
348
	const char *proto;
D
David Howells 已提交
349

350
	seq_printf(m, ",vers=%d", clp->rpc_ops->version);
D
David Howells 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	seq_printf(m, ",rsize=%d", nfss->rsize);
	seq_printf(m, ",wsize=%d", nfss->wsize);
	if (nfss->acregmin != 3*HZ || showdefaults)
		seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
	if (nfss->acregmax != 60*HZ || showdefaults)
		seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
	if (nfss->acdirmin != 30*HZ || showdefaults)
		seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
	if (nfss->acdirmax != 60*HZ || showdefaults)
		seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
	for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
		if (nfss->flags & nfs_infop->flag)
			seq_puts(m, nfs_infop->str);
		else
			seq_puts(m, nfs_infop->nostr);
	}
	switch (nfss->client->cl_xprt->prot) {
		case IPPROTO_TCP:
			proto = "tcp";
			break;
		case IPPROTO_UDP:
			proto = "udp";
			break;
		default:
			snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
			proto = buf;
	}
	seq_printf(m, ",proto=%s", proto);
	seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
	seq_printf(m, ",retrans=%u", nfss->retrans_count);
381
	seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
D
David Howells 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
}

/*
 * Describe the mount options on this VFS mountpoint
 */
static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
{
	struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);

	nfs_show_mount_options(m, nfss, 0);

	seq_puts(m, ",addr=");
	seq_escape(m, nfss->hostname, " \t\n\\");

	return 0;
}

/*
 * Present statistical information for this VFS mountpoint
 */
static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
{
	int i, cpu;
	struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
	struct rpc_auth *auth = nfss->client->cl_auth;
	struct nfs_iostats totals = { };

	seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);

	/*
	 * Display all mount option settings
	 */
	seq_printf(m, "\n\topts:\t");
	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
	seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
	seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
	seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
	nfs_show_mount_options(m, nfss, 1);

	seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);

	seq_printf(m, "\n\tcaps:\t");
	seq_printf(m, "caps=0x%x", nfss->caps);
	seq_printf(m, ",wtmult=%d", nfss->wtmult);
	seq_printf(m, ",dtsize=%d", nfss->dtsize);
	seq_printf(m, ",bsize=%d", nfss->bsize);
	seq_printf(m, ",namelen=%d", nfss->namelen);

#ifdef CONFIG_NFS_V4
431
	if (nfss->nfs_client->cl_nfsversion == 4) {
D
David Howells 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
		seq_printf(m, "\n\tnfsv4:\t");
		seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
		seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
		seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
	}
#endif

	/*
	 * Display security flavor in effect for this mount
	 */
	seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
	if (auth->au_flavor)
		seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);

	/*
	 * Display superblock I/O counters
	 */
	for_each_possible_cpu(cpu) {
		struct nfs_iostats *stats;

		preempt_disable();
		stats = per_cpu_ptr(nfss->io_stats, cpu);

		for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
			totals.events[i] += stats->events[i];
		for (i = 0; i < __NFSIOS_BYTESMAX; i++)
			totals.bytes[i] += stats->bytes[i];

		preempt_enable();
	}

	seq_printf(m, "\n\tevents:\t");
	for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
		seq_printf(m, "%lu ", totals.events[i]);
	seq_printf(m, "\n\tbytes:\t");
	for (i = 0; i < __NFSIOS_BYTESMAX; i++)
		seq_printf(m, "%Lu ", totals.bytes[i]);
	seq_printf(m, "\n");

	rpc_print_iostats(m, nfss->client);

	return 0;
}

/*
 * Begin unmount by attempting to remove all automounted mountpoints we added
 * in response to traversals
 */
static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
{
	struct nfs_server *server;
	struct rpc_clnt	*rpc;

	shrink_submounts(vfsmnt, &nfs_automount_list);
	if (!(flags & MNT_FORCE))
		return;
	/* -EIO all pending I/O */
	server = NFS_SB(vfsmnt->mnt_sb);
	rpc = server->client;
	if (!IS_ERR(rpc))
		rpc_killall_tasks(rpc);
	rpc = server->client_acl;
	if (!IS_ERR(rpc))
		rpc_killall_tasks(rpc);
}

/*
 * Obtain the root inode of the file system.
 */
static struct inode *
nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
{
	struct nfs_server	*server = NFS_SB(sb);
	int			error;

507
	error = server->nfs_client->rpc_ops->getroot(server, rootfh, fsinfo);
D
David Howells 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	if (error < 0) {
		dprintk("nfs_get_root: getattr error = %d\n", -error);
		return ERR_PTR(error);
	}

	server->fsid = fsinfo->fattr->fsid;
	return nfs_fhget(sb, rootfh, fsinfo->fattr);
}

/*
 * Do NFS version-independent mount processing, and sanity checking
 */
static int
nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
{
	struct nfs_server	*server;
	struct inode		*root_inode;
	struct nfs_fattr	fattr;
	struct nfs_fsinfo	fsinfo = {
					.fattr = &fattr,
				};
	struct nfs_pathconf pathinfo = {
			.fattr = &fattr,
	};
	int no_root_error = 0;
	unsigned long max_rpc_payload;

	/* We probably want something more informative here */
	snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));

	server = NFS_SB(sb);

	sb->s_magic      = NFS_SUPER_MAGIC;

	server->io_stats = nfs_alloc_iostats();
	if (server->io_stats == NULL)
		return -ENOMEM;

	root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
	/* Did getting the root inode fail? */
	if (IS_ERR(root_inode)) {
		no_root_error = PTR_ERR(root_inode);
		goto out_no_root;
	}
	sb->s_root = d_alloc_root(root_inode);
	if (!sb->s_root) {
		no_root_error = -ENOMEM;
		goto out_no_root;
	}
557
	sb->s_root->d_op = server->nfs_client->rpc_ops->dentry_ops;
D
David Howells 已提交
558 559 560 561 562 563

	/* mount time stamp, in seconds */
	server->mount_time = jiffies;

	/* Get some general file system info */
	if (server->namelen == 0 &&
564
	    server->nfs_client->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
D
David Howells 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		server->namelen = pathinfo.max_namelen;
	/* Work out a lot of parameters */
	if (server->rsize == 0)
		server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
	if (server->wsize == 0)
		server->wsize = nfs_block_size(fsinfo.wtpref, NULL);

	if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
		server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
	if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
		server->wsize = nfs_block_size(fsinfo.wtmax, NULL);

	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
	if (server->rsize > max_rpc_payload)
		server->rsize = max_rpc_payload;
	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
		server->rsize = NFS_MAX_FILE_IO_SIZE;
	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;

	if (server->wsize > max_rpc_payload)
		server->wsize = max_rpc_payload;
	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
		server->wsize = NFS_MAX_FILE_IO_SIZE;
	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;

	if (sb->s_blocksize == 0)
		sb->s_blocksize = nfs_block_bits(server->wsize,
							 &sb->s_blocksize_bits);
	server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);

	server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
	if (server->dtsize > PAGE_CACHE_SIZE)
		server->dtsize = PAGE_CACHE_SIZE;
	if (server->dtsize > server->rsize)
		server->dtsize = server->rsize;

	if (server->flags & NFS_MOUNT_NOAC) {
		server->acregmin = server->acregmax = 0;
		server->acdirmin = server->acdirmax = 0;
		sb->s_flags |= MS_SYNCHRONOUS;
	}
	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;

	nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);

	server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
	server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;

	/* We're airborne Set socket buffersize */
	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
	return 0;
	/* Yargs. It didn't work out. */
out_no_root:
	dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
	if (!IS_ERR(root_inode))
		iput(root_inode);
	return no_root_error;
}

/*
 * Initialise the timeout values for a connection
 */
static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
{
	to->to_initval = timeo * HZ / 10;
	to->to_retries = retrans;
	if (!to->to_retries)
		to->to_retries = 2;

	switch (proto) {
	case IPPROTO_TCP:
		if (!to->to_initval)
			to->to_initval = 60 * HZ;
		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
			to->to_initval = NFS_MAX_TCP_TIMEOUT;
		to->to_increment = to->to_initval;
		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
		to->to_exponential = 0;
		break;
	case IPPROTO_UDP:
	default:
		if (!to->to_initval)
			to->to_initval = 11 * HZ / 10;
		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
			to->to_initval = NFS_MAX_UDP_TIMEOUT;
		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
		to->to_exponential = 1;
		break;
	}
}

/*
 * Create an RPC client handle.
 */
static struct rpc_clnt *
nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
{
662
	struct nfs_client	*clp;
D
David Howells 已提交
663 664 665 666
	struct rpc_timeout	timeparms;
	struct rpc_xprt		*xprt = NULL;
	struct rpc_clnt		*clnt = NULL;
	int			proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
667
	int			nfsversion = 2;
D
David Howells 已提交
668

669 670 671 672 673 674
#ifdef CONFIG_NFS_V3
	if (server->flags & NFS_MOUNT_VER3)
		nfsversion = 3;
#endif

	clp = nfs_get_client(server->hostname, &server->addr, nfsversion);
675 676 677 678 679
	if (!clp) {
		dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
		return ERR_PTR(PTR_ERR(clp));
	}

D
David Howells 已提交
680 681 682 683 684
	nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);

	server->retrans_timeo = timeparms.to_initval;
	server->retrans_count = timeparms.to_retries;

685 686 687 688 689 690 691 692 693 694 695 696 697
	/* Check NFS protocol revision and initialize RPC op vector
	 * and file handle pool. */
#ifdef CONFIG_NFS_V3
	if (nfsversion == 3) {
		clp->rpc_ops = &nfs_v3_clientops;
		server->caps |= NFS_CAP_READDIRPLUS;
	} else {
		clp->rpc_ops = &nfs_v2_clientops;
	}
#else
	clp->rpc_ops = &nfs_v2_clientops;
#endif

D
David Howells 已提交
698 699 700 701 702
	/* create transport and client */
	xprt = xprt_create_proto(proto, &server->addr, &timeparms);
	if (IS_ERR(xprt)) {
		dprintk("%s: cannot create RPC transport. Error = %ld\n",
				__FUNCTION__, PTR_ERR(xprt));
703 704
		nfs_mark_client_ready(clp, PTR_ERR(xprt));
		nfs_put_client(clp);
D
David Howells 已提交
705 706 707
		return (struct rpc_clnt *)xprt;
	}
	clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
708
				 clp->cl_nfsversion, data->pseudoflavor);
D
David Howells 已提交
709 710 711 712 713 714 715 716 717
	if (IS_ERR(clnt)) {
		dprintk("%s: cannot create RPC client. Error = %ld\n",
				__FUNCTION__, PTR_ERR(xprt));
		goto out_fail;
	}

	clnt->cl_intr     = 1;
	clnt->cl_softrtry = 1;

718 719
	nfs_mark_client_ready(clp, 0);
	server->nfs_client = clp;
D
David Howells 已提交
720 721 722
	return clnt;

out_fail:
723 724
	nfs_mark_client_ready(clp, PTR_ERR(xprt));
	nfs_put_client(clp);
D
David Howells 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	return clnt;
}

/*
 * Clone a server record
 */
static struct nfs_server *nfs_clone_server(struct super_block *sb, struct nfs_clone_mount *data)
{
	struct nfs_server *server = NFS_SB(sb);
	struct nfs_server *parent = NFS_SB(data->sb);
	struct inode *root_inode;
	struct nfs_fsinfo fsinfo;
	void *err = ERR_PTR(-ENOMEM);

	sb->s_op = data->sb->s_op;
	sb->s_blocksize = data->sb->s_blocksize;
	sb->s_blocksize_bits = data->sb->s_blocksize_bits;
	sb->s_maxbytes = data->sb->s_maxbytes;

	server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
	server->io_stats = nfs_alloc_iostats();
	if (server->io_stats == NULL)
		goto out;

	server->client = rpc_clone_client(parent->client);
	if (IS_ERR((err = server->client)))
		goto out;

	if (!IS_ERR(parent->client_sys)) {
		server->client_sys = rpc_clone_client(parent->client_sys);
		if (IS_ERR((err = server->client_sys)))
			goto out;
	}
	if (!IS_ERR(parent->client_acl)) {
		server->client_acl = rpc_clone_client(parent->client_acl);
		if (IS_ERR((err = server->client_acl)))
			goto out;
	}
	root_inode = nfs_fhget(sb, data->fh, data->fattr);
	if (!root_inode)
		goto out;
	sb->s_root = d_alloc_root(root_inode);
	if (!sb->s_root)
		goto out_put_root;
	fsinfo.fattr = data->fattr;
	if (NFS_PROTO(root_inode)->fsinfo(server, data->fh, &fsinfo) == 0)
		nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
772
	sb->s_root->d_op = server->nfs_client->rpc_ops->dentry_ops;
D
David Howells 已提交
773 774 775 776 777 778 779 780 781 782 783
	sb->s_flags |= MS_ACTIVE;
	return server;
out_put_root:
	iput(root_inode);
out:
	return err;
}

/*
 * Copy an existing superblock and attach revised data
 */
784
static int nfs_clone_generic_sb(struct nfs_clone_mount *data,
D
David Howells 已提交
785
		struct super_block *(*fill_sb)(struct nfs_server *, struct nfs_clone_mount *),
786 787
		struct nfs_server *(*fill_server)(struct super_block *, struct nfs_clone_mount *),
		struct vfsmount *mnt)
D
David Howells 已提交
788 789 790 791 792
{
	struct nfs_server *server;
	struct nfs_server *parent = NFS_SB(data->sb);
	struct super_block *sb = ERR_PTR(-EINVAL);
	char *hostname;
793
	int error = -ENOMEM;
D
David Howells 已提交
794 795 796 797 798 799
	int len;

	server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
	if (server == NULL)
		goto out_err;
	memcpy(server, parent, sizeof(*server));
800
	atomic_inc(&server->nfs_client->cl_count);
D
David Howells 已提交
801 802 803 804 805 806
	hostname = (data->hostname != NULL) ? data->hostname : parent->hostname;
	len = strlen(hostname) + 1;
	server->hostname = kmalloc(len, GFP_KERNEL);
	if (server->hostname == NULL)
		goto free_server;
	memcpy(server->hostname, hostname, len);
807 808
	error = rpciod_up();
	if (error != 0)
D
David Howells 已提交
809 810 811
		goto free_hostname;

	sb = fill_sb(server, data);
812 813
	if (IS_ERR(sb)) {
		error = PTR_ERR(sb);
D
David Howells 已提交
814
		goto kill_rpciod;
815 816 817 818
	}
		
	if (sb->s_root)
		goto out_rpciod_down;
D
David Howells 已提交
819 820

	server = fill_server(sb, data);
821 822
	if (IS_ERR(server)) {
		error = PTR_ERR(server);
D
David Howells 已提交
823
		goto out_deactivate;
824 825
	}
	return simple_set_mnt(mnt, sb);
D
David Howells 已提交
826 827 828
out_deactivate:
	up_write(&sb->s_umount);
	deactivate_super(sb);
829 830 831 832
	return error;
out_rpciod_down:
	rpciod_down();
	kfree(server->hostname);
833
	nfs_put_client(server->nfs_client);
834 835
	kfree(server);
	return simple_set_mnt(mnt, sb);
D
David Howells 已提交
836 837 838 839 840
kill_rpciod:
	rpciod_down();
free_hostname:
	kfree(server->hostname);
free_server:
841
	nfs_put_client(server->nfs_client);
D
David Howells 已提交
842 843
	kfree(server);
out_err:
844
	return error;
D
David Howells 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

/*
 * Set up an NFS2/3 superblock
 *
 * The way this works is that the mount process passes a structure
 * in the data argument which contains the server's IP address
 * and the root file handle obtained from the server's mount
 * daemon. We stash these away in the private superblock fields.
 */
static int
nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
{
	struct nfs_server	*server;
	rpc_authflavor_t	authflavor;

	server           = NFS_SB(sb);
	sb->s_blocksize_bits = 0;
	sb->s_blocksize = 0;
	if (data->bsize)
		sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
	if (data->rsize)
		server->rsize = nfs_block_size(data->rsize, NULL);
	if (data->wsize)
		server->wsize = nfs_block_size(data->wsize, NULL);
	server->flags    = data->flags & NFS_MOUNT_FLAGMASK;

	server->acregmin = data->acregmin*HZ;
	server->acregmax = data->acregmax*HZ;
	server->acdirmin = data->acdirmin*HZ;
	server->acdirmax = data->acdirmax*HZ;

	/* Start lockd here, before we might error out */
	if (!(server->flags & NFS_MOUNT_NONLM))
		lockd_up();

	server->namelen  = data->namlen;
	server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
	if (!server->hostname)
		return -ENOMEM;
	strcpy(server->hostname, data->hostname);

	/* Fill in pseudoflavor for mount version < 5 */
	if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
		data->pseudoflavor = RPC_AUTH_UNIX;
	authflavor = data->pseudoflavor;	/* save for sb_init() */
	/* XXX maybe we want to add a server->pseudoflavor field */

	/* Create RPC client handles */
	server->client = nfs_create_client(server, data);
	if (IS_ERR(server->client))
		return PTR_ERR(server->client);
897

D
David Howells 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
	/* RFC 2623, sec 2.3.2 */
	if (authflavor != RPC_AUTH_UNIX) {
		struct rpc_auth *auth;

		server->client_sys = rpc_clone_client(server->client);
		if (IS_ERR(server->client_sys))
			return PTR_ERR(server->client_sys);
		auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
		if (IS_ERR(auth))
			return PTR_ERR(auth);
	} else {
		atomic_inc(&server->client->cl_count);
		server->client_sys = server->client;
	}
	if (server->flags & NFS_MOUNT_VER3) {
#ifdef CONFIG_NFS_V3_ACL
		if (!(server->flags & NFS_MOUNT_NOACL)) {
			server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
			/* No errors! Assume that Sun nfsacls are supported */
			if (!IS_ERR(server->client_acl))
				server->caps |= NFS_CAP_ACLS;
		}
#else
		server->flags &= ~NFS_MOUNT_NOACL;
#endif /* CONFIG_NFS_V3_ACL */
		/*
		 * The VFS shouldn't apply the umask to mode bits. We will
		 * do so ourselves when necessary.
		 */
		sb->s_flags |= MS_POSIXACL;
		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
			server->namelen = NFS3_MAXNAMLEN;
		sb->s_time_gran = 1;
	} else {
		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
			server->namelen = NFS2_MAXNAMLEN;
	}

	sb->s_op = &nfs_sops;
	return nfs_sb_init(sb, authflavor);
}

static int nfs_set_super(struct super_block *s, void *data)
{
	s->s_fs_info = data;
	return set_anon_super(s, data);
}

static int nfs_compare_super(struct super_block *sb, void *data)
{
	struct nfs_server *server = data;
	struct nfs_server *old = NFS_SB(sb);

	if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
		return 0;
	if (old->addr.sin_port != server->addr.sin_port)
		return 0;
	return !nfs_compare_fh(&old->fh, &server->fh);
}

958 959
static int nfs_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
D
David Howells 已提交
960 961 962 963 964 965 966
{
	int error;
	struct nfs_server *server = NULL;
	struct super_block *s;
	struct nfs_fh *root;
	struct nfs_mount_data *data = raw_data;

967
	error = -EINVAL;
D
David Howells 已提交
968 969
	if (data == NULL) {
		dprintk("%s: missing data argument\n", __FUNCTION__);
970
		goto out_err_noserver;
D
David Howells 已提交
971 972 973
	}
	if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
		dprintk("%s: bad mount version\n", __FUNCTION__);
974
		goto out_err_noserver;
D
David Howells 已提交
975 976 977 978 979 980 981 982 983 984 985
	}
	switch (data->version) {
		case 1:
			data->namlen = 0;
		case 2:
			data->bsize  = 0;
		case 3:
			if (data->flags & NFS_MOUNT_VER3) {
				dprintk("%s: mount structure version %d does not support NFSv3\n",
						__FUNCTION__,
						data->version);
986
				goto out_err_noserver;
D
David Howells 已提交
987 988 989 990 991 992 993 994
			}
			data->root.size = NFS2_FHSIZE;
			memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
		case 4:
			if (data->flags & NFS_MOUNT_SECFLAVOUR) {
				dprintk("%s: mount structure version %d does not support strong security\n",
						__FUNCTION__,
						data->version);
995
				goto out_err_noserver;
D
David Howells 已提交
996 997 998 999 1000 1001
			}
		case 5:
			memset(data->context, 0, sizeof(data->context));
	}
#ifndef CONFIG_NFS_V3
	/* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
1002
	error = -EPROTONOSUPPORT;
D
David Howells 已提交
1003 1004
	if (data->flags & NFS_MOUNT_VER3) {
		dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
1005
		goto out_err_noserver;
D
David Howells 已提交
1006 1007 1008
	}
#endif /* CONFIG_NFS_V3 */

1009
	error = -ENOMEM;
D
David Howells 已提交
1010 1011
	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
	if (!server)
1012
		goto out_err_noserver;
D
David Howells 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021
	/* Zero out the NFS state stuff */
	init_nfsv4_state(server);
	server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);

	root = &server->fh;
	if (data->flags & NFS_MOUNT_VER3)
		root->size = data->root.size;
	else
		root->size = NFS2_FHSIZE;
1022
	error = -EINVAL;
D
David Howells 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	if (root->size > sizeof(root->data)) {
		dprintk("%s: invalid root filehandle\n", __FUNCTION__);
		goto out_err;
	}
	memcpy(root->data, data->root.data, root->size);

	/* We now require that the mount process passes the remote address */
	memcpy(&server->addr, &data->addr, sizeof(server->addr));
	if (server->addr.sin_addr.s_addr == INADDR_ANY) {
		dprintk("%s: mount program didn't pass remote address!\n",
				__FUNCTION__);
		goto out_err;
	}

	/* Fire up rpciod if not yet running */
1038 1039 1040 1041
	error = rpciod_up();
	if (error < 0) {
		dprintk("%s: couldn't start rpciod! Error = %d\n",
				__FUNCTION__, error);
D
David Howells 已提交
1042 1043 1044 1045
		goto out_err;
	}

	s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1046 1047 1048 1049 1050 1051
	if (IS_ERR(s)) {
		error = PTR_ERR(s);
		goto out_err_rpciod;
	}

	if (s->s_root)
D
David Howells 已提交
1052 1053 1054 1055 1056 1057 1058 1059
		goto out_rpciod_down;

	s->s_flags = flags;

	error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
	if (error) {
		up_write(&s->s_umount);
		deactivate_super(s);
1060
		return error;
D
David Howells 已提交
1061 1062
	}
	s->s_flags |= MS_ACTIVE;
1063 1064
	return simple_set_mnt(mnt, s);

D
David Howells 已提交
1065 1066
out_rpciod_down:
	rpciod_down();
1067 1068 1069 1070 1071
	kfree(server);
	return simple_set_mnt(mnt, s);

out_err_rpciod:
	rpciod_down();
D
David Howells 已提交
1072 1073
out_err:
	kfree(server);
1074 1075
out_err_noserver:
	return error;
D
David Howells 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
}

static void nfs_kill_super(struct super_block *s)
{
	struct nfs_server *server = NFS_SB(s);

	kill_anon_super(s);

	if (!IS_ERR(server->client))
		rpc_shutdown_client(server->client);
	if (!IS_ERR(server->client_sys))
		rpc_shutdown_client(server->client_sys);
	if (!IS_ERR(server->client_acl))
		rpc_shutdown_client(server->client_acl);

	if (!(server->flags & NFS_MOUNT_NONLM))
		lockd_down();	/* release rpc.lockd */

	rpciod_down();		/* release rpciod */

	nfs_free_iostats(server->io_stats);
	kfree(server->hostname);
1098
	nfs_put_client(server->nfs_client);
D
David Howells 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	kfree(server);
	nfs_release_automount_timer();
}

static struct super_block *nfs_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
{
	struct super_block *sb;

	server->fsid = data->fattr->fsid;
	nfs_copy_fh(&server->fh, data->fh);
	sb = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
	if (!IS_ERR(sb) && sb->s_root == NULL && !(server->flags & NFS_MOUNT_NONLM))
		lockd_up();
	return sb;
}

1115 1116
static int nfs_clone_nfs_sb(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
D
David Howells 已提交
1117 1118
{
	struct nfs_clone_mount *data = raw_data;
1119
	return nfs_clone_generic_sb(data, nfs_clone_sb, nfs_clone_server, mnt);
D
David Howells 已提交
1120 1121 1122 1123 1124 1125
}

#ifdef CONFIG_NFS_V4
static struct rpc_clnt *nfs4_create_client(struct nfs_server *server,
	struct rpc_timeout *timeparms, int proto, rpc_authflavor_t flavor)
{
1126
	struct nfs_client *clp;
D
David Howells 已提交
1127 1128 1129 1130
	struct rpc_xprt *xprt = NULL;
	struct rpc_clnt *clnt = NULL;
	int err = -EIO;

1131
	clp = nfs_get_client(server->hostname, &server->addr, 4);
D
David Howells 已提交
1132 1133 1134 1135 1136 1137
	if (!clp) {
		dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
		return ERR_PTR(err);
	}

	/* Now create transport and client */
1138
	if (clp->cl_cons_state == NFS_CS_INITING) {
1139 1140
		clp->rpc_ops = &nfs_v4_clientops;

D
David Howells 已提交
1141 1142 1143 1144 1145
		xprt = xprt_create_proto(proto, &server->addr, timeparms);
		if (IS_ERR(xprt)) {
			err = PTR_ERR(xprt);
			dprintk("%s: cannot create RPC transport. Error = %d\n",
					__FUNCTION__, err);
1146
			goto client_init_error;
D
David Howells 已提交
1147 1148 1149 1150
		}
		/* Bind to a reserved port! */
		xprt->resvport = 1;
		clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1151
				clp->cl_nfsversion, flavor);
D
David Howells 已提交
1152 1153 1154 1155
		if (IS_ERR(clnt)) {
			err = PTR_ERR(clnt);
			dprintk("%s: cannot create RPC client. Error = %d\n",
					__FUNCTION__, err);
1156
			goto client_init_error;
D
David Howells 已提交
1157 1158 1159 1160 1161
		}
		clnt->cl_intr     = 1;
		clnt->cl_softrtry = 1;
		clp->cl_rpcclient = clnt;
		memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1162 1163 1164 1165 1166 1167 1168 1169
		err = nfs_idmap_new(clp);
		if (err < 0) {
			dprintk("%s: failed to create idmapper.\n",
				__FUNCTION__);
			goto client_init_error;
		}
		__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
		nfs_mark_client_ready(clp, 0);
D
David Howells 已提交
1170
	}
1171

D
David Howells 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	clnt = rpc_clone_client(clp->cl_rpcclient);

	if (IS_ERR(clnt)) {
		dprintk("%s: cannot create RPC client. Error = %d\n",
				__FUNCTION__, err);
		return clnt;
	}

	if (clnt->cl_auth->au_flavor != flavor) {
		struct rpc_auth *auth;

		auth = rpcauth_create(flavor, clnt);
		if (IS_ERR(auth)) {
			dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
			return (struct rpc_clnt *)auth;
		}
	}
1189 1190 1191 1192 1193

	server->nfs_client = clp;
	down_write(&clp->cl_sem);
	list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
	up_write(&clp->cl_sem);
D
David Howells 已提交
1194 1195
	return clnt;

1196 1197 1198
client_init_error:
	nfs_mark_client_ready(clp, err);
	nfs_put_client(clp);
D
David Howells 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
	return ERR_PTR(err);
}

/*
 * Set up an NFS4 superblock
 */
static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
{
	struct nfs_server *server;
	struct rpc_timeout timeparms;
	rpc_authflavor_t authflavour;
	int err = -EIO;

	sb->s_blocksize_bits = 0;
	sb->s_blocksize = 0;
	server = NFS_SB(sb);
	if (data->rsize != 0)
		server->rsize = nfs_block_size(data->rsize, NULL);
	if (data->wsize != 0)
		server->wsize = nfs_block_size(data->wsize, NULL);
	server->flags = data->flags & NFS_MOUNT_FLAGMASK;
	server->caps = NFS_CAP_ATOMIC_OPEN;

	server->acregmin = data->acregmin*HZ;
	server->acregmax = data->acregmax*HZ;
	server->acdirmin = data->acdirmin*HZ;
	server->acdirmax = data->acdirmax*HZ;

	nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);

	server->retrans_timeo = timeparms.to_initval;
	server->retrans_count = timeparms.to_retries;

	/* Now create transport and client */
	authflavour = RPC_AUTH_UNIX;
	if (data->auth_flavourlen != 0) {
		if (data->auth_flavourlen != 1) {
			dprintk("%s: Invalid number of RPC auth flavours %d.\n",
					__FUNCTION__, data->auth_flavourlen);
			err = -EINVAL;
			goto out_fail;
		}
		if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
			err = -EFAULT;
			goto out_fail;
		}
	}

	server->client = nfs4_create_client(server, &timeparms, data->proto, authflavour);
	if (IS_ERR(server->client)) {
		err = PTR_ERR(server->client);
			dprintk("%s: cannot create RPC client. Error = %d\n",
					__FUNCTION__, err);
			goto out_fail;
	}

	sb->s_time_gran = 1;

	sb->s_op = &nfs4_sops;
	err = nfs_sb_init(sb, authflavour);

 out_fail:
	return err;
}

static int nfs4_compare_super(struct super_block *sb, void *data)
{
	struct nfs_server *server = data;
	struct nfs_server *old = NFS_SB(sb);

	if (strcmp(server->hostname, old->hostname) != 0)
		return 0;
	if (strcmp(server->mnt_path, old->mnt_path) != 0)
		return 0;
	return 1;
}

static void *
nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
{
	void *p = NULL;

	if (!src->len)
		return ERR_PTR(-EINVAL);
	if (src->len < maxlen)
		maxlen = src->len;
	if (dst == NULL) {
		p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
		if (p == NULL)
			return ERR_PTR(-ENOMEM);
	}
	if (copy_from_user(dst, src->data, maxlen)) {
		kfree(p);
		return ERR_PTR(-EFAULT);
	}
	dst[maxlen] = '\0';
	return dst;
}

1298 1299
static int nfs4_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
D
David Howells 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308
{
	int error;
	struct nfs_server *server;
	struct super_block *s;
	struct nfs4_mount_data *data = raw_data;
	void *p;

	if (data == NULL) {
		dprintk("%s: missing data argument\n", __FUNCTION__);
1309
		return -EINVAL;
D
David Howells 已提交
1310 1311 1312
	}
	if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
		dprintk("%s: bad mount version\n", __FUNCTION__);
1313
		return -EINVAL;
D
David Howells 已提交
1314 1315 1316 1317
	}

	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
	if (!server)
1318
		return -ENOMEM;
D
David Howells 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
	/* Zero out the NFS state stuff */
	init_nfsv4_state(server);
	server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);

	p = nfs_copy_user_string(NULL, &data->hostname, 256);
	if (IS_ERR(p))
		goto out_err;
	server->hostname = p;

	p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
	if (IS_ERR(p))
		goto out_err;
	server->mnt_path = p;

	p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
			sizeof(server->ip_addr) - 1);
	if (IS_ERR(p))
		goto out_err;

	/* We now require that the mount process passes the remote address */
	if (data->host_addrlen != sizeof(server->addr)) {
1340
		error = -EINVAL;
D
David Howells 已提交
1341 1342 1343
		goto out_free;
	}
	if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1344
		error = -EFAULT;
D
David Howells 已提交
1345 1346 1347 1348 1349 1350
		goto out_free;
	}
	if (server->addr.sin_family != AF_INET ||
	    server->addr.sin_addr.s_addr == INADDR_ANY) {
		dprintk("%s: mount program didn't pass remote IP address!\n",
				__FUNCTION__);
1351
		error = -EINVAL;
D
David Howells 已提交
1352 1353 1354 1355
		goto out_free;
	}

	s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1356 1357
	if (IS_ERR(s)) {
		error = PTR_ERR(s);
D
David Howells 已提交
1358
		goto out_free;
1359 1360 1361 1362 1363 1364 1365 1366
	}

	if (s->s_root) {
		kfree(server->mnt_path);
		kfree(server->hostname);
		kfree(server);
		return simple_set_mnt(mnt, s);
	}
D
David Howells 已提交
1367 1368 1369 1370 1371 1372 1373

	s->s_flags = flags;

	error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
	if (error) {
		up_write(&s->s_umount);
		deactivate_super(s);
1374
		return error;
D
David Howells 已提交
1375 1376
	}
	s->s_flags |= MS_ACTIVE;
1377
	return simple_set_mnt(mnt, s);
D
David Howells 已提交
1378
out_err:
1379
	error = PTR_ERR(p);
D
David Howells 已提交
1380 1381 1382 1383
out_free:
	kfree(server->mnt_path);
	kfree(server->hostname);
	kfree(server);
1384
	return error;
D
David Howells 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
}

static void nfs4_kill_super(struct super_block *sb)
{
	struct nfs_server *server = NFS_SB(sb);

	nfs_return_all_delegations(sb);
	kill_anon_super(sb);

	nfs4_renewd_prepare_shutdown(server);

	if (server->client != NULL && !IS_ERR(server->client))
		rpc_shutdown_client(server->client);

	destroy_nfsv4_state(server);

	nfs_free_iostats(server->io_stats);
	kfree(server->hostname);
	kfree(server);
	nfs_release_automount_timer();
}

/*
 * Constructs the SERVER-side path
 */
static inline char *nfs4_dup_path(const struct dentry *dentry)
{
	char *page = (char *) __get_free_page(GFP_USER);
	char *path;

	path = nfs4_path(dentry, page, PAGE_SIZE);
	if (!IS_ERR(path)) {
		int len = PAGE_SIZE + page - path;
		char *tmp = path;

		path = kmalloc(len, GFP_KERNEL);
		if (path)
			memcpy(path, tmp, len);
		else
			path = ERR_PTR(-ENOMEM);
	}
	free_page((unsigned long)page);
	return path;
}

static struct super_block *nfs4_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
{
	const struct dentry *dentry = data->dentry;
1433
	struct nfs_client *clp = server->nfs_client;
D
David Howells 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	struct super_block *sb;

	server->fsid = data->fattr->fsid;
	nfs_copy_fh(&server->fh, data->fh);
	server->mnt_path = nfs4_dup_path(dentry);
	if (IS_ERR(server->mnt_path)) {
		sb = (struct super_block *)server->mnt_path;
		goto err;
	}
	sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
	if (IS_ERR(sb) || sb->s_root)
		goto free_path;
	nfs4_server_capabilities(server, &server->fh);

	down_write(&clp->cl_sem);
	list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
	up_write(&clp->cl_sem);
	return sb;
free_path:
	kfree(server->mnt_path);
err:
	server->mnt_path = NULL;
	return sb;
}

1459 1460
static int nfs_clone_nfs4_sb(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
D
David Howells 已提交
1461 1462
{
	struct nfs_clone_mount *data = raw_data;
1463
	return nfs_clone_generic_sb(data, nfs4_clone_sb, nfs_clone_server, mnt);
D
David Howells 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
}

static struct super_block *nfs4_referral_sb(struct nfs_server *server, struct nfs_clone_mount *data)
{
	struct super_block *sb = ERR_PTR(-ENOMEM);
	int len;

	len = strlen(data->mnt_path) + 1;
	server->mnt_path = kmalloc(len, GFP_KERNEL);
	if (server->mnt_path == NULL)
		goto err;
	memcpy(server->mnt_path, data->mnt_path, len);
	memcpy(&server->addr, data->addr, sizeof(struct sockaddr_in));

	sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
	if (IS_ERR(sb) || sb->s_root)
		goto free_path;
	return sb;
free_path:
	kfree(server->mnt_path);
err:
	server->mnt_path = NULL;
	return sb;
}

static struct nfs_server *nfs4_referral_server(struct super_block *sb, struct nfs_clone_mount *data)
{
	struct nfs_server *server = NFS_SB(sb);
	struct rpc_timeout timeparms;
	int proto, timeo, retrans;
	void *err;

	proto = IPPROTO_TCP;
	/* Since we are following a referral and there may be alternatives,
	   set the timeouts and retries to low values */
	timeo = 2;
	retrans = 1;
	nfs_init_timeout_values(&timeparms, proto, timeo, retrans);

1503 1504
	nfs_put_client(server->nfs_client);
	server->nfs_client = NULL;
D
David Howells 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	server->client = nfs4_create_client(server, &timeparms, proto, data->authflavor);
	if (IS_ERR((err = server->client)))
		goto out_err;

	sb->s_time_gran = 1;
	sb->s_op = &nfs4_sops;
	err = ERR_PTR(nfs_sb_init(sb, data->authflavor));
	if (!IS_ERR(err))
		return server;
out_err:
	return (struct nfs_server *)err;
}

1518 1519
static int nfs_referral_nfs4_sb(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
D
David Howells 已提交
1520 1521
{
	struct nfs_clone_mount *data = raw_data;
1522
	return nfs_clone_generic_sb(data, nfs4_referral_sb, nfs4_referral_server, mnt);
D
David Howells 已提交
1523 1524 1525
}

#endif