super.c 36.3 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
/*
 *  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)

static void nfs_umount_begin(struct vfsmount *, int);
64
static int  nfs_statfs(struct dentry *, struct kstatfs *);
D
David Howells 已提交
65 66
static int  nfs_show_options(struct seq_file *, struct vfsmount *);
static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
67 68 69
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 已提交
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
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
100 101 102 103 104 105
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 已提交
106 107 108 109 110 111 112 113 114 115 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
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

144 145
static struct shrinker *acl_shrinker;

D
David Howells 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
 * 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
165
	acl_shrinker = set_shrinker(DEFAULT_SEEKS, nfs_access_cache_shrinker);
D
David Howells 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	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)
{
183 184
	if (acl_shrinker != NULL)
		remove_shrinker(acl_shrinker);
D
David Howells 已提交
185 186 187 188 189 190 191 192 193 194
#ifdef CONFIG_NFS_V4
	unregister_filesystem(&nfs4_fs_type);
	nfs_unregister_sysctl();
#endif
	unregister_filesystem(&nfs_fs_type);
}

/*
 * Deliver file system statistics to userspace
 */
195
static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
D
David Howells 已提交
196
{
197
	struct nfs_server *server = NFS_SB(dentry->d_sb);
D
David Howells 已提交
198 199
	unsigned char blockbits;
	unsigned long blockres;
200
	struct nfs_fh *fh = NFS_FH(dentry->d_inode);
D
David Howells 已提交
201 202 203 204 205 206 207 208
	struct nfs_fattr fattr;
	struct nfs_fsstat res = {
			.fattr = &fattr,
	};
	int error;

	lock_kernel();

209
	error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
D
David Howells 已提交
210 211 212 213 214 215 216 217 218
	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.
	 */
219
	buf->f_frsize = dentry->d_sb->s_blocksize;
D
David Howells 已提交
220 221 222 223 224 225 226 227

	/*
	 * 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.
	 */
228 229
	buf->f_bsize = dentry->d_sb->s_blocksize;
	blockbits = dentry->d_sb->s_blocksize_bits;
D
David Howells 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	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;

}

250 251 252
/*
 * Map the security flavour number to a name
 */
253 254
static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
{
255
	static const struct {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		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 已提交
281 282 283 284 285
/*
 * 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 已提交
286
	static const struct proc_nfs_info {
D
David Howells 已提交
287
		int flag;
D
David Howells 已提交
288 289
		const char *str;
		const char *nostr;
D
David Howells 已提交
290 291 292 293 294 295 296 297 298
	} 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 已提交
299
	const struct proc_nfs_info *nfs_infop;
300
	struct nfs_client *clp = nfss->nfs_client;
D
David Howells 已提交
301
	char buf[12];
D
David Howells 已提交
302
	const char *proto;
D
David Howells 已提交
303

304
	seq_printf(m, ",vers=%d", clp->rpc_ops->version);
D
David Howells 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	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);
333 334
	seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
	seq_printf(m, ",retrans=%u", clp->retrans_count);
335
	seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
D
David Howells 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 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 381 382 383 384
}

/*
 * 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
385
	if (nfss->nfs_client->cl_nfsversion == 4) {
D
David Howells 已提交
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 431 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
		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;

461
	error = server->nfs_client->rpc_ops->getroot(server, rootfh, fsinfo);
D
David Howells 已提交
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 507 508 509 510
	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;
	}
511
	sb->s_root->d_op = server->nfs_client->rpc_ops->dentry_ops;
D
David Howells 已提交
512 513 514 515 516 517

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

	/* Get some general file system info */
	if (server->namelen == 0 &&
518
	    server->nfs_client->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
D
David Howells 已提交
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 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
		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;
}

/*
 * Create an RPC client handle.
 */
static struct rpc_clnt *
nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
{
584
	struct nfs_client	*clp;
585
	struct rpc_clnt		*clnt;
D
David Howells 已提交
586
	int			proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
587
	int			nfsversion = 2;
588
	int			err;
D
David Howells 已提交
589

590 591 592 593 594 595
#ifdef CONFIG_NFS_V3
	if (server->flags & NFS_MOUNT_VER3)
		nfsversion = 3;
#endif

	clp = nfs_get_client(server->hostname, &server->addr, nfsversion);
596 597 598 599 600
	if (!clp) {
		dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
		return ERR_PTR(PTR_ERR(clp));
	}

601 602 603
	if (clp->cl_cons_state == NFS_CS_INITING) {
		/* Check NFS protocol revision and initialize RPC op
		 * vector and file handle pool. */
604
#ifdef CONFIG_NFS_V3
605 606 607 608 609 610
		if (nfsversion == 3) {
			clp->rpc_ops = &nfs_v3_clientops;
			server->caps |= NFS_CAP_READDIRPLUS;
		} else {
			clp->rpc_ops = &nfs_v2_clientops;
		}
611
#else
612
		clp->rpc_ops = &nfs_v2_clientops;
613 614
#endif

615 616 617 618 619 620 621
		/* create transport and client */
		err = nfs_create_rpc_client(clp, proto, data->timeo,
					    data->retrans, RPC_AUTH_UNIX);
		if (err < 0)
			goto client_init_error;

		nfs_mark_client_ready(clp, 0);
D
David Howells 已提交
622
	}
623 624 625

	/* create an nfs_server-specific client */
	clnt = rpc_clone_client(clp->cl_rpcclient);
D
David Howells 已提交
626
	if (IS_ERR(clnt)) {
627 628 629
		dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
		nfs_put_client(clp);
		return ERR_PTR(PTR_ERR(clnt));
D
David Howells 已提交
630 631
	}

632 633 634 635 636 637 638 639 640
	if (data->pseudoflavor != clp->cl_rpcclient->cl_auth->au_flavor) {
		struct rpc_auth *auth;

		auth = rpcauth_create(data->pseudoflavor, server->client);
		if (IS_ERR(auth)) {
			dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
			return ERR_PTR(PTR_ERR(auth));
		}
	}
D
David Howells 已提交
641

642
	server->nfs_client = clp;
D
David Howells 已提交
643 644
	return clnt;

645 646
client_init_error:
	nfs_mark_client_ready(clp, err);
647
	nfs_put_client(clp);
648
	return ERR_PTR(err);
D
David Howells 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
}

/*
 * 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;

667
	server->client_acl = ERR_PTR(-EINVAL);
D
David Howells 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	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_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);
690
	sb->s_root->d_op = server->nfs_client->rpc_ops->dentry_ops;
D
David Howells 已提交
691 692 693 694 695 696 697 698 699 700 701
	sb->s_flags |= MS_ACTIVE;
	return server;
out_put_root:
	iput(root_inode);
out:
	return err;
}

/*
 * Copy an existing superblock and attach revised data
 */
702
static int nfs_clone_generic_sb(struct nfs_clone_mount *data,
D
David Howells 已提交
703
		struct super_block *(*fill_sb)(struct nfs_server *, struct nfs_clone_mount *),
704 705
		struct nfs_server *(*fill_server)(struct super_block *, struct nfs_clone_mount *),
		struct vfsmount *mnt)
D
David Howells 已提交
706 707 708 709 710
{
	struct nfs_server *server;
	struct nfs_server *parent = NFS_SB(data->sb);
	struct super_block *sb = ERR_PTR(-EINVAL);
	char *hostname;
711
	int error = -ENOMEM;
D
David Howells 已提交
712 713 714 715 716 717
	int len;

	server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
	if (server == NULL)
		goto out_err;
	memcpy(server, parent, sizeof(*server));
718
	atomic_inc(&server->nfs_client->cl_count);
D
David Howells 已提交
719 720 721 722 723 724 725 726
	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);

	sb = fill_sb(server, data);
727 728
	if (IS_ERR(sb)) {
		error = PTR_ERR(sb);
729
		goto free_hostname;
730
	}
731

732
	if (sb->s_root)
733
		goto out_share;
D
David Howells 已提交
734 735

	server = fill_server(sb, data);
736 737
	if (IS_ERR(server)) {
		error = PTR_ERR(server);
D
David Howells 已提交
738
		goto out_deactivate;
739 740
	}
	return simple_set_mnt(mnt, sb);
D
David Howells 已提交
741 742 743
out_deactivate:
	up_write(&sb->s_umount);
	deactivate_super(sb);
744
	return error;
745
out_share:
746
	kfree(server->hostname);
747
	nfs_put_client(server->nfs_client);
748 749
	kfree(server);
	return simple_set_mnt(mnt, sb);
D
David Howells 已提交
750 751 752
free_hostname:
	kfree(server->hostname);
free_server:
753
	nfs_put_client(server->nfs_client);
D
David Howells 已提交
754 755
	kfree(server);
out_err:
756
	return error;
D
David Howells 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
}

/*
 * 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);
809

D
David Howells 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	/* RFC 2623, sec 2.3.2 */
	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);
}

857 858
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 已提交
859 860 861 862 863 864 865
{
	int error;
	struct nfs_server *server = NULL;
	struct super_block *s;
	struct nfs_fh *root;
	struct nfs_mount_data *data = raw_data;

866
	error = -EINVAL;
D
David Howells 已提交
867 868
	if (data == NULL) {
		dprintk("%s: missing data argument\n", __FUNCTION__);
869
		goto out_err_noserver;
D
David Howells 已提交
870 871 872
	}
	if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
		dprintk("%s: bad mount version\n", __FUNCTION__);
873
		goto out_err_noserver;
D
David Howells 已提交
874 875 876 877 878 879 880 881 882 883 884
	}
	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);
885
				goto out_err_noserver;
D
David Howells 已提交
886 887 888 889 890 891 892 893
			}
			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);
894
				goto out_err_noserver;
D
David Howells 已提交
895 896 897 898 899 900
			}
		case 5:
			memset(data->context, 0, sizeof(data->context));
	}
#ifndef CONFIG_NFS_V3
	/* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
901
	error = -EPROTONOSUPPORT;
D
David Howells 已提交
902 903
	if (data->flags & NFS_MOUNT_VER3) {
		dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
904
		goto out_err_noserver;
D
David Howells 已提交
905 906 907
	}
#endif /* CONFIG_NFS_V3 */

908
	error = -ENOMEM;
D
David Howells 已提交
909 910
	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
	if (!server)
911
		goto out_err_noserver;
D
David Howells 已提交
912 913
	/* Zero out the NFS state stuff */
	init_nfsv4_state(server);
914
	server->client = server->client_acl = ERR_PTR(-EINVAL);
D
David Howells 已提交
915 916 917 918 919 920

	root = &server->fh;
	if (data->flags & NFS_MOUNT_VER3)
		root->size = data->root.size;
	else
		root->size = NFS2_FHSIZE;
921
	error = -EINVAL;
D
David Howells 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
	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;
	}

	s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
937 938
	if (IS_ERR(s)) {
		error = PTR_ERR(s);
939
		goto out_err;
940 941 942
	}

	if (s->s_root)
943
		goto out_share;
D
David Howells 已提交
944 945 946 947 948 949 950

	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);
951
		return error;
D
David Howells 已提交
952 953
	}
	s->s_flags |= MS_ACTIVE;
954 955
	return simple_set_mnt(mnt, s);

956
out_share:
957 958 959
	kfree(server);
	return simple_set_mnt(mnt, s);

D
David Howells 已提交
960 961
out_err:
	kfree(server);
962 963
out_err_noserver:
	return error;
D
David Howells 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
}

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_acl))
		rpc_shutdown_client(server->client_acl);

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

	nfs_free_iostats(server->io_stats);
	kfree(server->hostname);
982
	nfs_put_client(server->nfs_client);
D
David Howells 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	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;
}

999 1000
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 已提交
1001 1002
{
	struct nfs_clone_mount *data = raw_data;
1003
	return nfs_clone_generic_sb(data, nfs_clone_sb, nfs_clone_server, mnt);
D
David Howells 已提交
1004 1005 1006 1007
}

#ifdef CONFIG_NFS_V4
static struct rpc_clnt *nfs4_create_client(struct nfs_server *server,
1008
	int timeo, int retrans, int proto, rpc_authflavor_t flavor)
D
David Howells 已提交
1009
{
1010
	struct nfs_client *clp;
D
David Howells 已提交
1011 1012 1013
	struct rpc_clnt *clnt = NULL;
	int err = -EIO;

1014
	clp = nfs_get_client(server->hostname, &server->addr, 4);
D
David Howells 已提交
1015 1016 1017 1018 1019 1020
	if (!clp) {
		dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
		return ERR_PTR(err);
	}

	/* Now create transport and client */
1021
	if (clp->cl_cons_state == NFS_CS_INITING) {
1022 1023
		clp->rpc_ops = &nfs_v4_clientops;

1024 1025
		err = nfs_create_rpc_client(clp, proto, timeo, retrans, flavor);
		if (err < 0)
1026
			goto client_init_error;
1027

D
David Howells 已提交
1028
		memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1029 1030 1031 1032 1033 1034 1035 1036
		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 已提交
1037
	}
1038

D
David Howells 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	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;
		}
	}
1056 1057 1058 1059 1060

	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 已提交
1061 1062
	return clnt;

1063 1064 1065
client_init_error:
	nfs_mark_client_ready(clp, err);
	nfs_put_client(clp);
D
David Howells 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	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;
	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;

	/* 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;
		}
	}

1108 1109
	server->client = nfs4_create_client(server, data->timeo, data->retrans,
					    data->proto, authflavour);
D
David Howells 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	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;
}

1160 1161
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 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170
{
	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__);
1171
		return -EINVAL;
D
David Howells 已提交
1172 1173 1174
	}
	if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
		dprintk("%s: bad mount version\n", __FUNCTION__);
1175
		return -EINVAL;
D
David Howells 已提交
1176 1177 1178 1179
	}

	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
	if (!server)
1180
		return -ENOMEM;
D
David Howells 已提交
1181 1182
	/* Zero out the NFS state stuff */
	init_nfsv4_state(server);
1183
	server->client = server->client_acl = ERR_PTR(-EINVAL);
D
David Howells 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

	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)) {
1202
		error = -EINVAL;
D
David Howells 已提交
1203 1204 1205
		goto out_free;
	}
	if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1206
		error = -EFAULT;
D
David Howells 已提交
1207 1208 1209 1210 1211 1212
		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__);
1213
		error = -EINVAL;
D
David Howells 已提交
1214 1215 1216 1217
		goto out_free;
	}

	s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1218 1219
	if (IS_ERR(s)) {
		error = PTR_ERR(s);
D
David Howells 已提交
1220
		goto out_free;
1221 1222 1223 1224 1225 1226 1227 1228
	}

	if (s->s_root) {
		kfree(server->mnt_path);
		kfree(server->hostname);
		kfree(server);
		return simple_set_mnt(mnt, s);
	}
D
David Howells 已提交
1229 1230 1231 1232 1233 1234 1235

	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);
1236
		return error;
D
David Howells 已提交
1237 1238
	}
	s->s_flags |= MS_ACTIVE;
1239
	return simple_set_mnt(mnt, s);
D
David Howells 已提交
1240
out_err:
1241
	error = PTR_ERR(p);
D
David Howells 已提交
1242 1243 1244 1245
out_free:
	kfree(server->mnt_path);
	kfree(server->hostname);
	kfree(server);
1246
	return error;
D
David Howells 已提交
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
}

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;
1295
	struct nfs_client *clp = server->nfs_client;
D
David Howells 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	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;
}

1321 1322
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 已提交
1323 1324
{
	struct nfs_clone_mount *data = raw_data;
1325
	return nfs_clone_generic_sb(data, nfs4_clone_sb, nfs_clone_server, mnt);
D
David Howells 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
}

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);
	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;

1363 1364
	nfs_put_client(server->nfs_client);
	server->nfs_client = NULL;
1365 1366
	server->client = nfs4_create_client(server, timeo, retrans, proto,
					    data->authflavor);
D
David Howells 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	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;
}

1379 1380
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 已提交
1381 1382
{
	struct nfs_clone_mount *data = raw_data;
1383
	return nfs_clone_generic_sb(data, nfs4_referral_sb, nfs4_referral_server, mnt);
D
David Howells 已提交
1384 1385 1386
}

#endif